ir-common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * $Id: ir-common.c,v 1.11 2005/07/07 14:44:43 mchehab 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. /* Keys 0 to 9 */
  42. [ 0x00 ] = KEY_KP0,
  43. [ 0x01 ] = KEY_KP1,
  44. [ 0x02 ] = KEY_KP2,
  45. [ 0x03 ] = KEY_KP3,
  46. [ 0x04 ] = KEY_KP4,
  47. [ 0x05 ] = KEY_KP5,
  48. [ 0x06 ] = KEY_KP6,
  49. [ 0x07 ] = KEY_KP7,
  50. [ 0x08 ] = KEY_KP8,
  51. [ 0x09 ] = KEY_KP9,
  52. [ 0x0b ] = KEY_CHANNEL, /* channel / program (japan: 11) */
  53. [ 0x0c ] = KEY_POWER, /* standby */
  54. [ 0x0d ] = KEY_MUTE, /* mute / demute */
  55. [ 0x0f ] = KEY_TV, /* display */
  56. [ 0x10 ] = KEY_VOLUMEUP,
  57. [ 0x11 ] = KEY_VOLUMEDOWN,
  58. [ 0x12 ] = KEY_BRIGHTNESSUP,
  59. [ 0x13 ] = KEY_BRIGHTNESSDOWN,
  60. [ 0x1e ] = KEY_SEARCH, /* search + */
  61. [ 0x20 ] = KEY_CHANNELUP, /* channel / program + */
  62. [ 0x21 ] = KEY_CHANNELDOWN, /* channel / program - */
  63. [ 0x22 ] = KEY_CHANNEL, /* alt / channel */
  64. [ 0x23 ] = KEY_LANGUAGE, /* 1st / 2nd language */
  65. [ 0x26 ] = KEY_SLEEP, /* sleeptimer */
  66. [ 0x2e ] = KEY_MENU, /* 2nd controls (USA: menu) */
  67. [ 0x30 ] = KEY_PAUSE,
  68. [ 0x32 ] = KEY_REWIND,
  69. [ 0x33 ] = KEY_GOTO,
  70. [ 0x35 ] = KEY_PLAY,
  71. [ 0x36 ] = KEY_STOP,
  72. [ 0x37 ] = KEY_RECORD, /* recording */
  73. [ 0x3c ] = KEY_TEXT, /* teletext submode (Japan: 12) */
  74. [ 0x3d ] = KEY_SUSPEND, /* system standby */
  75. };
  76. EXPORT_SYMBOL_GPL(ir_codes_rc5_tv);
  77. /* Table for Leadtek Winfast Remote Controls - used by both bttv and cx88 */
  78. IR_KEYTAB_TYPE ir_codes_winfast[IR_KEYTAB_SIZE] = {
  79. /* Keys 0 to 9 */
  80. [ 18 ] = KEY_KP0,
  81. [ 5 ] = KEY_KP1,
  82. [ 6 ] = KEY_KP2,
  83. [ 7 ] = KEY_KP3,
  84. [ 9 ] = KEY_KP4,
  85. [ 10 ] = KEY_KP5,
  86. [ 11 ] = KEY_KP6,
  87. [ 13 ] = KEY_KP7,
  88. [ 14 ] = KEY_KP8,
  89. [ 15 ] = KEY_KP9,
  90. [ 0 ] = KEY_POWER,
  91. [ 2 ] = KEY_TUNER, /* TV/FM */
  92. [ 30 ] = KEY_VIDEO,
  93. [ 4 ] = KEY_VOLUMEUP,
  94. [ 8 ] = KEY_VOLUMEDOWN,
  95. [ 12 ] = KEY_CHANNELUP,
  96. [ 16 ] = KEY_CHANNELDOWN,
  97. [ 3 ] = KEY_ZOOM, /* fullscreen */
  98. [ 31 ] = KEY_SUBTITLE, /* closed caption/teletext */
  99. [ 32 ] = KEY_SLEEP,
  100. [ 20 ] = KEY_MUTE,
  101. [ 43 ] = KEY_RED,
  102. [ 44 ] = KEY_GREEN,
  103. [ 45 ] = KEY_YELLOW,
  104. [ 46 ] = KEY_BLUE,
  105. [ 24 ] = KEY_KPPLUS, /* fine tune + */
  106. [ 25 ] = KEY_KPMINUS, /* fine tune - */
  107. [ 33 ] = KEY_KPDOT,
  108. [ 19 ] = KEY_KPENTER,
  109. [ 34 ] = KEY_BACK,
  110. [ 35 ] = KEY_PLAYPAUSE,
  111. [ 36 ] = KEY_NEXT,
  112. [ 38 ] = KEY_STOP,
  113. [ 39 ] = KEY_RECORD
  114. };
  115. EXPORT_SYMBOL_GPL(ir_codes_winfast);
  116. /* empty keytable, can be used as placeholder for not-yet created keytables */
  117. IR_KEYTAB_TYPE ir_codes_empty[IR_KEYTAB_SIZE] = {
  118. [ 42 ] = KEY_COFFEE,
  119. };
  120. EXPORT_SYMBOL_GPL(ir_codes_empty);
  121. /* Hauppauge: the newer, gray remotes (seems there are multiple
  122. * slightly different versions), shipped with cx88+ivtv cards.
  123. * almost rc5 coding, but some non-standard keys */
  124. IR_KEYTAB_TYPE ir_codes_hauppauge_new[IR_KEYTAB_SIZE] = {
  125. /* Keys 0 to 9 */
  126. [ 0x00 ] = KEY_KP0,
  127. [ 0x01 ] = KEY_KP1,
  128. [ 0x02 ] = KEY_KP2,
  129. [ 0x03 ] = KEY_KP3,
  130. [ 0x04 ] = KEY_KP4,
  131. [ 0x05 ] = KEY_KP5,
  132. [ 0x06 ] = KEY_KP6,
  133. [ 0x07 ] = KEY_KP7,
  134. [ 0x08 ] = KEY_KP8,
  135. [ 0x09 ] = KEY_KP9,
  136. [ 0x0a ] = KEY_TEXT, /* keypad asterisk as well */
  137. [ 0x0b ] = KEY_RED, /* red button */
  138. [ 0x0c ] = KEY_RADIO,
  139. [ 0x0d ] = KEY_MENU,
  140. [ 0x0e ] = KEY_SUBTITLE, /* also the # key */
  141. [ 0x0f ] = KEY_MUTE,
  142. [ 0x10 ] = KEY_VOLUMEUP,
  143. [ 0x11 ] = KEY_VOLUMEDOWN,
  144. [ 0x12 ] = KEY_PREVIOUS, /* previous channel */
  145. [ 0x14 ] = KEY_UP,
  146. [ 0x15 ] = KEY_DOWN,
  147. [ 0x16 ] = KEY_LEFT,
  148. [ 0x17 ] = KEY_RIGHT,
  149. [ 0x18 ] = KEY_VIDEO, /* Videos */
  150. [ 0x19 ] = KEY_AUDIO, /* Music */
  151. /* 0x1a: Pictures - presume this means
  152. "Multimedia Home Platform" -
  153. no "PICTURES" key in input.h
  154. */
  155. [ 0x1a ] = KEY_MHP,
  156. [ 0x1b ] = KEY_EPG, /* Guide */
  157. [ 0x1c ] = KEY_TV,
  158. [ 0x1e ] = KEY_NEXTSONG, /* skip >| */
  159. [ 0x1f ] = KEY_EXIT, /* back/exit */
  160. [ 0x20 ] = KEY_CHANNELUP, /* channel / program + */
  161. [ 0x21 ] = KEY_CHANNELDOWN, /* channel / program - */
  162. [ 0x22 ] = KEY_CHANNEL, /* source (old black remote) */
  163. [ 0x24 ] = KEY_PREVIOUSSONG, /* replay |< */
  164. [ 0x25 ] = KEY_ENTER, /* OK */
  165. [ 0x26 ] = KEY_SLEEP, /* minimize (old black remote) */
  166. [ 0x29 ] = KEY_BLUE, /* blue key */
  167. [ 0x2e ] = KEY_GREEN, /* green button */
  168. [ 0x30 ] = KEY_PAUSE, /* pause */
  169. [ 0x32 ] = KEY_REWIND, /* backward << */
  170. [ 0x34 ] = KEY_FASTFORWARD, /* forward >> */
  171. [ 0x35 ] = KEY_PLAY,
  172. [ 0x36 ] = KEY_STOP,
  173. [ 0x37 ] = KEY_RECORD, /* recording */
  174. [ 0x38 ] = KEY_YELLOW, /* yellow key */
  175. [ 0x3b ] = KEY_SELECT, /* top right button */
  176. [ 0x3c ] = KEY_ZOOM, /* full */
  177. [ 0x3d ] = KEY_POWER, /* system power (green button) */
  178. };
  179. EXPORT_SYMBOL(ir_codes_hauppauge_new);
  180. IR_KEYTAB_TYPE ir_codes_pixelview[IR_KEYTAB_SIZE] = {
  181. [ 2 ] = KEY_KP0,
  182. [ 1 ] = KEY_KP1,
  183. [ 11 ] = KEY_KP2,
  184. [ 27 ] = KEY_KP3,
  185. [ 5 ] = KEY_KP4,
  186. [ 9 ] = KEY_KP5,
  187. [ 21 ] = KEY_KP6,
  188. [ 6 ] = KEY_KP7,
  189. [ 10 ] = KEY_KP8,
  190. [ 18 ] = KEY_KP9,
  191. [ 3 ] = KEY_TUNER, /* TV/FM */
  192. [ 7 ] = KEY_SEARCH, /* scan */
  193. [ 28 ] = KEY_ZOOM, /* full screen */
  194. [ 30 ] = KEY_POWER,
  195. [ 23 ] = KEY_VOLUMEDOWN,
  196. [ 31 ] = KEY_VOLUMEUP,
  197. [ 20 ] = KEY_CHANNELDOWN,
  198. [ 22 ] = KEY_CHANNELUP,
  199. [ 24 ] = KEY_MUTE,
  200. [ 0 ] = KEY_LIST, /* source */
  201. [ 19 ] = KEY_INFO, /* loop */
  202. [ 16 ] = KEY_LAST, /* +100 */
  203. [ 13 ] = KEY_CLEAR, /* reset */
  204. [ 12 ] = BTN_RIGHT, /* fun++ */
  205. [ 4 ] = BTN_LEFT, /* fun-- */
  206. [ 14 ] = KEY_GOTO, /* function */
  207. [ 15 ] = KEY_STOP, /* freeze */
  208. };
  209. EXPORT_SYMBOL(ir_codes_pixelview);
  210. /* -------------------------------------------------------------------------- */
  211. static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
  212. {
  213. if (KEY_RESERVED == ir->keycode) {
  214. printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
  215. dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
  216. return;
  217. }
  218. dprintk(1,"%s: key event code=%d down=%d\n",
  219. dev->name,ir->keycode,ir->keypressed);
  220. input_report_key(dev,ir->keycode,ir->keypressed);
  221. input_sync(dev);
  222. }
  223. /* -------------------------------------------------------------------------- */
  224. void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
  225. int ir_type, IR_KEYTAB_TYPE *ir_codes)
  226. {
  227. int i;
  228. ir->ir_type = ir_type;
  229. if (ir_codes)
  230. memcpy(ir->ir_codes, ir_codes, sizeof(ir->ir_codes));
  231. init_input_dev(dev);
  232. dev->keycode = ir->ir_codes;
  233. dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
  234. dev->keycodemax = IR_KEYTAB_SIZE;
  235. for (i = 0; i < IR_KEYTAB_SIZE; i++)
  236. set_bit(ir->ir_codes[i], dev->keybit);
  237. clear_bit(0, dev->keybit);
  238. set_bit(EV_KEY, dev->evbit);
  239. if (repeat)
  240. set_bit(EV_REP, dev->evbit);
  241. }
  242. void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
  243. {
  244. if (ir->keypressed) {
  245. ir->keypressed = 0;
  246. ir_input_key_event(dev,ir);
  247. }
  248. }
  249. void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
  250. u32 ir_key, u32 ir_raw)
  251. {
  252. u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
  253. if (ir->keypressed && ir->keycode != keycode) {
  254. ir->keypressed = 0;
  255. ir_input_key_event(dev,ir);
  256. }
  257. if (!ir->keypressed) {
  258. ir->ir_key = ir_key;
  259. ir->ir_raw = ir_raw;
  260. ir->keycode = keycode;
  261. ir->keypressed = 1;
  262. ir_input_key_event(dev,ir);
  263. }
  264. }
  265. /* -------------------------------------------------------------------------- */
  266. u32 ir_extract_bits(u32 data, u32 mask)
  267. {
  268. int mbit, vbit;
  269. u32 value;
  270. value = 0;
  271. vbit = 0;
  272. for (mbit = 0; mbit < 32; mbit++) {
  273. if (!(mask & ((u32)1 << mbit)))
  274. continue;
  275. if (data & ((u32)1 << mbit))
  276. value |= (1 << vbit);
  277. vbit++;
  278. }
  279. return value;
  280. }
  281. static int inline getbit(u32 *samples, int bit)
  282. {
  283. return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
  284. }
  285. /* sump raw samples for visual debugging ;) */
  286. int ir_dump_samples(u32 *samples, int count)
  287. {
  288. int i, bit, start;
  289. printk(KERN_DEBUG "ir samples: ");
  290. start = 0;
  291. for (i = 0; i < count * 32; i++) {
  292. bit = getbit(samples,i);
  293. if (bit)
  294. start = 1;
  295. if (0 == start)
  296. continue;
  297. printk("%s", bit ? "#" : "_");
  298. }
  299. printk("\n");
  300. return 0;
  301. }
  302. /* decode raw samples, biphase coding, used by rc5 for example */
  303. int ir_decode_biphase(u32 *samples, int count, int low, int high)
  304. {
  305. int i,last,bit,len,flips;
  306. u32 value;
  307. /* find start bit (1) */
  308. for (i = 0; i < 32; i++) {
  309. bit = getbit(samples,i);
  310. if (bit)
  311. break;
  312. }
  313. /* go decoding */
  314. len = 0;
  315. flips = 0;
  316. value = 1;
  317. for (; i < count * 32; i++) {
  318. if (len > high)
  319. break;
  320. if (flips > 1)
  321. break;
  322. last = bit;
  323. bit = getbit(samples,i);
  324. if (last == bit) {
  325. len++;
  326. continue;
  327. }
  328. if (len < low) {
  329. len++;
  330. flips++;
  331. continue;
  332. }
  333. value <<= 1;
  334. value |= bit;
  335. flips = 0;
  336. len = 1;
  337. }
  338. return value;
  339. }
  340. EXPORT_SYMBOL_GPL(ir_input_init);
  341. EXPORT_SYMBOL_GPL(ir_input_nokey);
  342. EXPORT_SYMBOL_GPL(ir_input_keydown);
  343. EXPORT_SYMBOL_GPL(ir_extract_bits);
  344. EXPORT_SYMBOL_GPL(ir_dump_samples);
  345. EXPORT_SYMBOL_GPL(ir_decode_biphase);
  346. /*
  347. * Local variables:
  348. * c-basic-offset: 8
  349. * End:
  350. */