ir-common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * $Id: ir-common.c,v 1.10 2005/05/22 19:23:39 nsh Exp $
  3. *
  4. * some common structs and functions to handle infrared remotes via
  5. * input layer ...
  6. *
  7. * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <media/ir-common.h>
  26. /* -------------------------------------------------------------------------- */
  27. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  28. MODULE_LICENSE("GPL");
  29. static int repeat = 1;
  30. module_param(repeat, int, 0444);
  31. MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
  32. static int debug = 0; /* debug level (0,1,2) */
  33. module_param(debug, int, 0644);
  34. #define dprintk(level, fmt, arg...) if (debug >= level) \
  35. printk(KERN_DEBUG fmt , ## arg)
  36. /* -------------------------------------------------------------------------- */
  37. /* generic RC5 keytable */
  38. /* see http://users.pandora.be/nenya/electronics/rc5/codes00.htm */
  39. /* used by old (black) Hauppauge remotes */
  40. IR_KEYTAB_TYPE ir_codes_rc5_tv[IR_KEYTAB_SIZE] = {
  41. [ 0x00 ] = KEY_KP0, // 0
  42. [ 0x01 ] = KEY_KP1, // 1
  43. [ 0x02 ] = KEY_KP2, // 2
  44. [ 0x03 ] = KEY_KP3, // 3
  45. [ 0x04 ] = KEY_KP4, // 4
  46. [ 0x05 ] = KEY_KP5, // 5
  47. [ 0x06 ] = KEY_KP6, // 6
  48. [ 0x07 ] = KEY_KP7, // 7
  49. [ 0x08 ] = KEY_KP8, // 8
  50. [ 0x09 ] = KEY_KP9, // 9
  51. [ 0x0b ] = KEY_CHANNEL, // channel / program (japan: 11)
  52. [ 0x0c ] = KEY_POWER, // standby
  53. [ 0x0d ] = KEY_MUTE, // mute / demute
  54. [ 0x0f ] = KEY_TV, // display
  55. [ 0x10 ] = KEY_VOLUMEUP, // volume +
  56. [ 0x11 ] = KEY_VOLUMEDOWN, // volume -
  57. [ 0x12 ] = KEY_BRIGHTNESSUP, // brightness +
  58. [ 0x13 ] = KEY_BRIGHTNESSDOWN, // brightness -
  59. [ 0x1e ] = KEY_SEARCH, // search +
  60. [ 0x20 ] = KEY_CHANNELUP, // channel / program +
  61. [ 0x21 ] = KEY_CHANNELDOWN, // channel / program -
  62. [ 0x22 ] = KEY_CHANNEL, // alt / channel
  63. [ 0x23 ] = KEY_LANGUAGE, // 1st / 2nd language
  64. [ 0x26 ] = KEY_SLEEP, // sleeptimer
  65. [ 0x2e ] = KEY_MENU, // 2nd controls (USA: menu)
  66. [ 0x30 ] = KEY_PAUSE, // pause
  67. [ 0x32 ] = KEY_REWIND, // rewind
  68. [ 0x33 ] = KEY_GOTO, // go to
  69. [ 0x35 ] = KEY_PLAY, // play
  70. [ 0x36 ] = KEY_STOP, // stop
  71. [ 0x37 ] = KEY_RECORD, // recording
  72. [ 0x3c ] = KEY_TEXT, // teletext submode (Japan: 12)
  73. [ 0x3d ] = KEY_SUSPEND, // system standby
  74. #if 0 /* FIXME */
  75. [ 0x0a ] = KEY_RESERVED, // 1/2/3 digits (japan: 10)
  76. [ 0x0e ] = KEY_RESERVED, // P.P. (personal preference)
  77. [ 0x14 ] = KEY_RESERVED, // colour saturation +
  78. [ 0x15 ] = KEY_RESERVED, // colour saturation -
  79. [ 0x16 ] = KEY_RESERVED, // bass +
  80. [ 0x17 ] = KEY_RESERVED, // bass -
  81. [ 0x18 ] = KEY_RESERVED, // treble +
  82. [ 0x19 ] = KEY_RESERVED, // treble -
  83. [ 0x1a ] = KEY_RESERVED, // balance right
  84. [ 0x1b ] = KEY_RESERVED, // balance left
  85. [ 0x1c ] = KEY_RESERVED, // contrast +
  86. [ 0x1d ] = KEY_RESERVED, // contrast -
  87. [ 0x1f ] = KEY_RESERVED, // tint/hue +
  88. [ 0x24 ] = KEY_RESERVED, // spacial stereo on/off
  89. [ 0x25 ] = KEY_RESERVED, // mono / stereo (USA)
  90. [ 0x27 ] = KEY_RESERVED, // tint / hue -
  91. [ 0x28 ] = KEY_RESERVED, // RF switch/PIP select
  92. [ 0x29 ] = KEY_RESERVED, // vote
  93. [ 0x2a ] = KEY_RESERVED, // timed page/channel clck
  94. [ 0x2b ] = KEY_RESERVED, // increment (USA)
  95. [ 0x2c ] = KEY_RESERVED, // decrement (USA)
  96. [ 0x2d ] = KEY_RESERVED, //
  97. [ 0x2f ] = KEY_RESERVED, // PIP shift
  98. [ 0x31 ] = KEY_RESERVED, // erase
  99. [ 0x34 ] = KEY_RESERVED, // wind
  100. [ 0x38 ] = KEY_RESERVED, // external 1
  101. [ 0x39 ] = KEY_RESERVED, // external 2
  102. [ 0x3a ] = KEY_RESERVED, // PIP display mode
  103. [ 0x3b ] = KEY_RESERVED, // view data mode / advance
  104. [ 0x3e ] = KEY_RESERVED, // crispener on/off
  105. [ 0x3f ] = KEY_RESERVED, // system select
  106. #endif
  107. };
  108. EXPORT_SYMBOL_GPL(ir_codes_rc5_tv);
  109. /* Table for Leadtek Winfast Remote Controls - used by both bttv and cx88 */
  110. IR_KEYTAB_TYPE ir_codes_winfast[IR_KEYTAB_SIZE] = {
  111. [ 5 ] = KEY_KP1,
  112. [ 6 ] = KEY_KP2,
  113. [ 7 ] = KEY_KP3,
  114. [ 9 ] = KEY_KP4,
  115. [ 10 ] = KEY_KP5,
  116. [ 11 ] = KEY_KP6,
  117. [ 13 ] = KEY_KP7,
  118. [ 14 ] = KEY_KP8,
  119. [ 15 ] = KEY_KP9,
  120. [ 18 ] = KEY_KP0,
  121. [ 0 ] = KEY_POWER,
  122. // [ 27 ] = MTS button
  123. [ 2 ] = KEY_TUNER, // TV/FM
  124. [ 30 ] = KEY_VIDEO,
  125. // [ 22 ] = display button
  126. [ 4 ] = KEY_VOLUMEUP,
  127. [ 8 ] = KEY_VOLUMEDOWN,
  128. [ 12 ] = KEY_CHANNELUP,
  129. [ 16 ] = KEY_CHANNELDOWN,
  130. [ 3 ] = KEY_ZOOM, // fullscreen
  131. [ 31 ] = KEY_SUBTITLE, // closed caption/teletext
  132. [ 32 ] = KEY_SLEEP,
  133. // [ 41 ] = boss key
  134. [ 20 ] = KEY_MUTE,
  135. [ 43 ] = KEY_RED,
  136. [ 44 ] = KEY_GREEN,
  137. [ 45 ] = KEY_YELLOW,
  138. [ 46 ] = KEY_BLUE,
  139. [ 24 ] = KEY_KPPLUS, //fine tune +
  140. [ 25 ] = KEY_KPMINUS, //fine tune -
  141. // [ 42 ] = picture in picture
  142. [ 33 ] = KEY_KPDOT,
  143. [ 19 ] = KEY_KPENTER,
  144. // [ 17 ] = recall
  145. [ 34 ] = KEY_BACK,
  146. [ 35 ] = KEY_PLAYPAUSE,
  147. [ 36 ] = KEY_NEXT,
  148. // [ 37 ] = time shifting
  149. [ 38 ] = KEY_STOP,
  150. [ 39 ] = KEY_RECORD
  151. // [ 40 ] = snapshot
  152. };
  153. EXPORT_SYMBOL_GPL(ir_codes_winfast);
  154. /* empty keytable, can be used as placeholder for not-yet created keytables */
  155. IR_KEYTAB_TYPE ir_codes_empty[IR_KEYTAB_SIZE] = {
  156. [ 42 ] = KEY_COFFEE,
  157. };
  158. EXPORT_SYMBOL_GPL(ir_codes_empty);
  159. /* Hauppauge: the newer, gray remotes (seems there are multiple
  160. * slightly different versions), shipped with cx88+ivtv cards.
  161. * almost rc5 coding, but some non-standard keys */
  162. IR_KEYTAB_TYPE ir_codes_hauppauge_new[IR_KEYTAB_SIZE] = {
  163. [ 0x00 ] = KEY_KP0, // 0
  164. [ 0x01 ] = KEY_KP1, // 1
  165. [ 0x02 ] = KEY_KP2, // 2
  166. [ 0x03 ] = KEY_KP3, // 3
  167. [ 0x04 ] = KEY_KP4, // 4
  168. [ 0x05 ] = KEY_KP5, // 5
  169. [ 0x06 ] = KEY_KP6, // 6
  170. [ 0x07 ] = KEY_KP7, // 7
  171. [ 0x08 ] = KEY_KP8, // 8
  172. [ 0x09 ] = KEY_KP9, // 9
  173. [ 0x0a ] = KEY_TEXT, // keypad asterisk as well
  174. [ 0x0b ] = KEY_RED, // red button
  175. [ 0x0c ] = KEY_RADIO, // radio
  176. [ 0x0d ] = KEY_MENU, // menu
  177. [ 0x0e ] = KEY_SUBTITLE, // also the # key
  178. [ 0x0f ] = KEY_MUTE, // mute
  179. [ 0x10 ] = KEY_VOLUMEUP, // volume +
  180. [ 0x11 ] = KEY_VOLUMEDOWN, // volume -
  181. [ 0x12 ] = KEY_PREVIOUS, // previous channel
  182. [ 0x14 ] = KEY_UP, // up
  183. [ 0x15 ] = KEY_DOWN, // down
  184. [ 0x16 ] = KEY_LEFT, // left
  185. [ 0x17 ] = KEY_RIGHT, // right
  186. [ 0x18 ] = KEY_VIDEO, // Videos
  187. [ 0x19 ] = KEY_AUDIO, // Music
  188. [ 0x1a ] = KEY_MHP, // Pictures - presume this means "Multimedia Home Platform"- no "PICTURES" key in input.h
  189. [ 0x1b ] = KEY_EPG, // Guide
  190. [ 0x1c ] = KEY_TV, // TV
  191. [ 0x1e ] = KEY_NEXTSONG, // skip >|
  192. [ 0x1f ] = KEY_EXIT, // back/exit
  193. [ 0x20 ] = KEY_CHANNELUP, // channel / program +
  194. [ 0x21 ] = KEY_CHANNELDOWN, // channel / program -
  195. [ 0x22 ] = KEY_CHANNEL, // source (old black remote)
  196. [ 0x24 ] = KEY_PREVIOUSSONG, // replay |<
  197. [ 0x25 ] = KEY_ENTER, // OK
  198. [ 0x26 ] = KEY_SLEEP, // minimize (old black remote)
  199. [ 0x29 ] = KEY_BLUE, // blue key
  200. [ 0x2e ] = KEY_GREEN, // green button
  201. [ 0x30 ] = KEY_PAUSE, // pause
  202. [ 0x32 ] = KEY_REWIND, // backward <<
  203. [ 0x34 ] = KEY_FASTFORWARD, // forward >>
  204. [ 0x35 ] = KEY_PLAY, // play
  205. [ 0x36 ] = KEY_STOP, // stop
  206. [ 0x37 ] = KEY_RECORD, // recording
  207. [ 0x38 ] = KEY_YELLOW, // yellow key
  208. [ 0x3b ] = KEY_SELECT, // top right button
  209. [ 0x3c ] = KEY_ZOOM, // full
  210. [ 0x3d ] = KEY_POWER, // system power (green button)
  211. };
  212. EXPORT_SYMBOL(ir_codes_hauppauge_new);
  213. IR_KEYTAB_TYPE ir_codes_pixelview[IR_KEYTAB_SIZE] = {
  214. [ 2 ] = KEY_KP0,
  215. [ 1 ] = KEY_KP1,
  216. [ 11 ] = KEY_KP2,
  217. [ 27 ] = KEY_KP3,
  218. [ 5 ] = KEY_KP4,
  219. [ 9 ] = KEY_KP5,
  220. [ 21 ] = KEY_KP6,
  221. [ 6 ] = KEY_KP7,
  222. [ 10 ] = KEY_KP8,
  223. [ 18 ] = KEY_KP9,
  224. [ 3 ] = KEY_TUNER, // TV/FM
  225. [ 7 ] = KEY_SEARCH, // scan
  226. [ 28 ] = KEY_ZOOM, // full screen
  227. [ 30 ] = KEY_POWER,
  228. [ 23 ] = KEY_VOLUMEDOWN,
  229. [ 31 ] = KEY_VOLUMEUP,
  230. [ 20 ] = KEY_CHANNELDOWN,
  231. [ 22 ] = KEY_CHANNELUP,
  232. [ 24 ] = KEY_MUTE,
  233. [ 0 ] = KEY_LIST, // source
  234. [ 19 ] = KEY_INFO, // loop
  235. [ 16 ] = KEY_LAST, // +100
  236. [ 13 ] = KEY_CLEAR, // reset
  237. [ 12 ] = BTN_RIGHT, // fun++
  238. [ 4 ] = BTN_LEFT, // fun--
  239. [ 14 ] = KEY_GOTO, // function
  240. [ 15 ] = KEY_STOP, // freeze
  241. };
  242. EXPORT_SYMBOL(ir_codes_pixelview);
  243. /* -------------------------------------------------------------------------- */
  244. static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
  245. {
  246. if (KEY_RESERVED == ir->keycode) {
  247. printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
  248. dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
  249. return;
  250. }
  251. dprintk(1,"%s: key event code=%d down=%d\n",
  252. dev->name,ir->keycode,ir->keypressed);
  253. input_report_key(dev,ir->keycode,ir->keypressed);
  254. input_sync(dev);
  255. }
  256. /* -------------------------------------------------------------------------- */
  257. void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
  258. int ir_type, IR_KEYTAB_TYPE *ir_codes)
  259. {
  260. int i;
  261. ir->ir_type = ir_type;
  262. if (ir_codes)
  263. memcpy(ir->ir_codes, ir_codes, sizeof(ir->ir_codes));
  264. init_input_dev(dev);
  265. dev->keycode = ir->ir_codes;
  266. dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
  267. dev->keycodemax = IR_KEYTAB_SIZE;
  268. for (i = 0; i < IR_KEYTAB_SIZE; i++)
  269. set_bit(ir->ir_codes[i], dev->keybit);
  270. clear_bit(0, dev->keybit);
  271. set_bit(EV_KEY, dev->evbit);
  272. if (repeat)
  273. set_bit(EV_REP, dev->evbit);
  274. }
  275. void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
  276. {
  277. if (ir->keypressed) {
  278. ir->keypressed = 0;
  279. ir_input_key_event(dev,ir);
  280. }
  281. }
  282. void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
  283. u32 ir_key, u32 ir_raw)
  284. {
  285. u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
  286. if (ir->keypressed && ir->keycode != keycode) {
  287. ir->keypressed = 0;
  288. ir_input_key_event(dev,ir);
  289. }
  290. if (!ir->keypressed) {
  291. ir->ir_key = ir_key;
  292. ir->ir_raw = ir_raw;
  293. ir->keycode = keycode;
  294. ir->keypressed = 1;
  295. ir_input_key_event(dev,ir);
  296. }
  297. #if 0
  298. /* maybe do something like this ??? */
  299. input_event(a, EV_IR, ir->ir_type, ir->ir_raw);
  300. #endif
  301. }
  302. /* -------------------------------------------------------------------------- */
  303. u32 ir_extract_bits(u32 data, u32 mask)
  304. {
  305. int mbit, vbit;
  306. u32 value;
  307. value = 0;
  308. vbit = 0;
  309. for (mbit = 0; mbit < 32; mbit++) {
  310. if (!(mask & ((u32)1 << mbit)))
  311. continue;
  312. if (data & ((u32)1 << mbit))
  313. value |= (1 << vbit);
  314. vbit++;
  315. }
  316. return value;
  317. }
  318. static int inline getbit(u32 *samples, int bit)
  319. {
  320. return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
  321. }
  322. /* sump raw samples for visual debugging ;) */
  323. int ir_dump_samples(u32 *samples, int count)
  324. {
  325. int i, bit, start;
  326. printk(KERN_DEBUG "ir samples: ");
  327. start = 0;
  328. for (i = 0; i < count * 32; i++) {
  329. bit = getbit(samples,i);
  330. if (bit)
  331. start = 1;
  332. if (0 == start)
  333. continue;
  334. printk("%s", bit ? "#" : "_");
  335. }
  336. printk("\n");
  337. return 0;
  338. }
  339. /* decode raw samples, biphase coding, used by rc5 for example */
  340. int ir_decode_biphase(u32 *samples, int count, int low, int high)
  341. {
  342. int i,last,bit,len,flips;
  343. u32 value;
  344. /* find start bit (1) */
  345. for (i = 0; i < 32; i++) {
  346. bit = getbit(samples,i);
  347. if (bit)
  348. break;
  349. }
  350. /* go decoding */
  351. len = 0;
  352. flips = 0;
  353. value = 1;
  354. for (; i < count * 32; i++) {
  355. if (len > high)
  356. break;
  357. if (flips > 1)
  358. break;
  359. last = bit;
  360. bit = getbit(samples,i);
  361. if (last == bit) {
  362. len++;
  363. continue;
  364. }
  365. if (len < low) {
  366. len++;
  367. flips++;
  368. continue;
  369. }
  370. value <<= 1;
  371. value |= bit;
  372. flips = 0;
  373. len = 1;
  374. }
  375. return value;
  376. }
  377. EXPORT_SYMBOL_GPL(ir_input_init);
  378. EXPORT_SYMBOL_GPL(ir_input_nokey);
  379. EXPORT_SYMBOL_GPL(ir_input_keydown);
  380. EXPORT_SYMBOL_GPL(ir_extract_bits);
  381. EXPORT_SYMBOL_GPL(ir_dump_samples);
  382. EXPORT_SYMBOL_GPL(ir_decode_biphase);
  383. /*
  384. * Local variables:
  385. * c-basic-offset: 8
  386. * End:
  387. */