ir-common.c 11 KB

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