ir-kbd-gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. *
  3. * Copyright (c) 2003 Gerd Knorr
  4. * Copyright (c) 2003 Pavel Machek
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/input.h>
  26. #include <linux/pci.h>
  27. #include <media/ir-common.h>
  28. #include "bttv.h"
  29. /* ---------------------------------------------------------------------- */
  30. static IR_KEYTAB_TYPE ir_codes_avermedia[IR_KEYTAB_SIZE] = {
  31. [ 34 ] = KEY_KP0,
  32. [ 40 ] = KEY_KP1,
  33. [ 24 ] = KEY_KP2,
  34. [ 56 ] = KEY_KP3,
  35. [ 36 ] = KEY_KP4,
  36. [ 20 ] = KEY_KP5,
  37. [ 52 ] = KEY_KP6,
  38. [ 44 ] = KEY_KP7,
  39. [ 28 ] = KEY_KP8,
  40. [ 60 ] = KEY_KP9,
  41. [ 48 ] = KEY_EJECTCD, // Unmarked on my controller
  42. [ 0 ] = KEY_POWER,
  43. [ 18 ] = BTN_LEFT, // DISPLAY/L
  44. [ 50 ] = BTN_RIGHT, // LOOP/R
  45. [ 10 ] = KEY_MUTE,
  46. [ 38 ] = KEY_RECORD,
  47. [ 22 ] = KEY_PAUSE,
  48. [ 54 ] = KEY_STOP,
  49. [ 30 ] = KEY_VOLUMEDOWN,
  50. [ 62 ] = KEY_VOLUMEUP,
  51. [ 32 ] = KEY_TUNER, // TV/FM
  52. [ 16 ] = KEY_CD,
  53. [ 8 ] = KEY_VIDEO,
  54. [ 4 ] = KEY_AUDIO,
  55. [ 12 ] = KEY_ZOOM, // full screen
  56. [ 2 ] = KEY_INFO, // preview
  57. [ 42 ] = KEY_SEARCH, // autoscan
  58. [ 26 ] = KEY_STOP, // freeze
  59. [ 58 ] = KEY_RECORD, // capture
  60. [ 6 ] = KEY_PLAY, // unmarked
  61. [ 46 ] = KEY_RED, // unmarked
  62. [ 14 ] = KEY_GREEN, // unmarked
  63. [ 33 ] = KEY_YELLOW, // unmarked
  64. [ 17 ] = KEY_CHANNELDOWN,
  65. [ 49 ] = KEY_CHANNELUP,
  66. [ 1 ] = KEY_BLUE, // unmarked
  67. };
  68. /* Matt Jesson <dvb@jesson.eclipse.co.uk */
  69. static IR_KEYTAB_TYPE ir_codes_avermedia_dvbt[IR_KEYTAB_SIZE] = {
  70. [ 0x28 ] = KEY_KP0, //'0' / 'enter'
  71. [ 0x22 ] = KEY_KP1, //'1'
  72. [ 0x12 ] = KEY_KP2, //'2' / 'up arrow'
  73. [ 0x32 ] = KEY_KP3, //'3'
  74. [ 0x24 ] = KEY_KP4, //'4' / 'left arrow'
  75. [ 0x14 ] = KEY_KP5, //'5'
  76. [ 0x34 ] = KEY_KP6, //'6' / 'right arrow'
  77. [ 0x26 ] = KEY_KP7, //'7'
  78. [ 0x16 ] = KEY_KP8, //'8' / 'down arrow'
  79. [ 0x36 ] = KEY_KP9, //'9'
  80. [ 0x20 ] = KEY_LIST, // 'source'
  81. [ 0x10 ] = KEY_TEXT, // 'teletext'
  82. [ 0x00 ] = KEY_POWER, // 'power'
  83. [ 0x04 ] = KEY_AUDIO, // 'audio'
  84. [ 0x06 ] = KEY_ZOOM, // 'full screen'
  85. [ 0x18 ] = KEY_VIDEO, // 'display'
  86. [ 0x38 ] = KEY_SEARCH, // 'loop'
  87. [ 0x08 ] = KEY_INFO, // 'preview'
  88. [ 0x2a ] = KEY_REWIND, // 'backward <<'
  89. [ 0x1a ] = KEY_FASTFORWARD, // 'forward >>'
  90. [ 0x3a ] = KEY_RECORD, // 'capture'
  91. [ 0x0a ] = KEY_MUTE, // 'mute'
  92. [ 0x2c ] = KEY_RECORD, // 'record'
  93. [ 0x1c ] = KEY_PAUSE, // 'pause'
  94. [ 0x3c ] = KEY_STOP, // 'stop'
  95. [ 0x0c ] = KEY_PLAY, // 'play'
  96. [ 0x2e ] = KEY_RED, // 'red'
  97. [ 0x01 ] = KEY_BLUE, // 'blue' / 'cancel'
  98. [ 0x0e ] = KEY_YELLOW, // 'yellow' / 'ok'
  99. [ 0x21 ] = KEY_GREEN, // 'green'
  100. [ 0x11 ] = KEY_CHANNELDOWN, // 'channel -'
  101. [ 0x31 ] = KEY_CHANNELUP, // 'channel +'
  102. [ 0x1e ] = KEY_VOLUMEDOWN, // 'volume -'
  103. [ 0x3e ] = KEY_VOLUMEUP, // 'volume +'
  104. };
  105. /* Attila Kondoros <attila.kondoros@chello.hu> */
  106. static IR_KEYTAB_TYPE ir_codes_apac_viewcomp[IR_KEYTAB_SIZE] = {
  107. [ 1 ] = KEY_KP1,
  108. [ 2 ] = KEY_KP2,
  109. [ 3 ] = KEY_KP3,
  110. [ 4 ] = KEY_KP4,
  111. [ 5 ] = KEY_KP5,
  112. [ 6 ] = KEY_KP6,
  113. [ 7 ] = KEY_KP7,
  114. [ 8 ] = KEY_KP8,
  115. [ 9 ] = KEY_KP9,
  116. [ 0 ] = KEY_KP0,
  117. [ 23 ] = KEY_LAST, // +100
  118. [ 10 ] = KEY_LIST, // recall
  119. [ 28 ] = KEY_TUNER, // TV/FM
  120. [ 21 ] = KEY_SEARCH, // scan
  121. [ 18 ] = KEY_POWER, // power
  122. [ 31 ] = KEY_VOLUMEDOWN, // vol up
  123. [ 27 ] = KEY_VOLUMEUP, // vol down
  124. [ 30 ] = KEY_CHANNELDOWN, // chn up
  125. [ 26 ] = KEY_CHANNELUP, // chn down
  126. [ 17 ] = KEY_VIDEO, // video
  127. [ 15 ] = KEY_ZOOM, // full screen
  128. [ 19 ] = KEY_MUTE, // mute/unmute
  129. [ 16 ] = KEY_TEXT, // min
  130. [ 13 ] = KEY_STOP, // freeze
  131. [ 14 ] = KEY_RECORD, // record
  132. [ 29 ] = KEY_PLAYPAUSE, // stop
  133. [ 25 ] = KEY_PLAY, // play
  134. [ 22 ] = KEY_GOTO, // osd
  135. [ 20 ] = KEY_REFRESH, // default
  136. [ 12 ] = KEY_KPPLUS, // fine tune >>>>
  137. [ 24 ] = KEY_KPMINUS // fine tune <<<<
  138. };
  139. /* ---------------------------------------------------------------------- */
  140. /* Ricardo Cerqueira <v4l@cerqueira.org> */
  141. /* Weird matching, since the remote has "uncommon" keys */
  142. static IR_KEYTAB_TYPE ir_codes_conceptronic[IR_KEYTAB_SIZE] = {
  143. [ 30 ] = KEY_POWER, // power
  144. [ 7 ] = KEY_SWITCHVIDEOMODE, // source
  145. [ 28 ] = KEY_SEARCH, // scan
  146. /* FIXME: duplicate keycodes?
  147. *
  148. * These four keys seem to share the same GPIO as CH+, CH-, <<< and >>>
  149. * The GPIO values are
  150. * 6397fb for both "Scan <" and "CH -",
  151. * 639ffb for "Scan >" and "CH+",
  152. * 6384fb for "Tune <" and "<<<",
  153. * 638cfb for "Tune >" and ">>>", regardless of the mask.
  154. *
  155. * [ 23 ] = KEY_BACK, // fm scan <<
  156. * [ 31 ] = KEY_FORWARD, // fm scan >>
  157. *
  158. * [ 4 ] = KEY_LEFT, // fm tuning <
  159. * [ 12 ] = KEY_RIGHT, // fm tuning >
  160. *
  161. * For now, these four keys are disabled. Pressing them will generate
  162. * the CH+/CH-/<<</>>> events
  163. */
  164. [ 3 ] = KEY_TUNER, // TV/FM
  165. [ 0 ] = KEY_RECORD,
  166. [ 8 ] = KEY_STOP,
  167. [ 17 ] = KEY_PLAY,
  168. [ 26 ] = KEY_PLAYPAUSE, // freeze
  169. [ 25 ] = KEY_ZOOM, // zoom
  170. [ 15 ] = KEY_TEXT, // min
  171. [ 1 ] = KEY_KP1,
  172. [ 11 ] = KEY_KP2,
  173. [ 27 ] = KEY_KP3,
  174. [ 5 ] = KEY_KP4,
  175. [ 9 ] = KEY_KP5,
  176. [ 21 ] = KEY_KP6,
  177. [ 6 ] = KEY_KP7,
  178. [ 10 ] = KEY_KP8,
  179. [ 18 ] = KEY_KP9,
  180. [ 2 ] = KEY_KP0,
  181. [ 16 ] = KEY_LAST, // +100
  182. [ 19 ] = KEY_LIST, // recall
  183. [ 31 ] = KEY_CHANNELUP, // chn down
  184. [ 23 ] = KEY_CHANNELDOWN, // chn up
  185. [ 22 ] = KEY_VOLUMEUP, // vol down
  186. [ 20 ] = KEY_VOLUMEDOWN, // vol up
  187. [ 4 ] = KEY_KPMINUS, // <<<
  188. [ 14 ] = KEY_SETUP, // function
  189. [ 12 ] = KEY_KPPLUS, // >>>
  190. [ 13 ] = KEY_GOTO, // mts
  191. [ 29 ] = KEY_REFRESH, // reset
  192. [ 24 ] = KEY_MUTE // mute/unmute
  193. };
  194. struct IR {
  195. struct bttv_sub_device *sub;
  196. struct input_dev *input;
  197. struct ir_input_state ir;
  198. char name[32];
  199. char phys[32];
  200. u32 mask_keycode;
  201. u32 mask_keydown;
  202. u32 mask_keyup;
  203. int polling;
  204. u32 last_gpio;
  205. struct work_struct work;
  206. struct timer_list timer;
  207. };
  208. static int debug;
  209. module_param(debug, int, 0644); /* debug level (0,1,2) */
  210. #define DEVNAME "ir-kbd-gpio"
  211. #define dprintk(fmt, arg...) if (debug) \
  212. printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
  213. static void ir_irq(struct bttv_sub_device *sub);
  214. static int ir_probe(struct device *dev);
  215. static int ir_remove(struct device *dev);
  216. static struct bttv_sub_driver driver = {
  217. .drv = {
  218. .name = DEVNAME,
  219. .probe = ir_probe,
  220. .remove = ir_remove,
  221. },
  222. .gpio_irq = ir_irq,
  223. };
  224. /* ---------------------------------------------------------------------- */
  225. static void ir_handle_key(struct IR *ir)
  226. {
  227. u32 gpio,data;
  228. /* read gpio value */
  229. gpio = bttv_gpio_read(ir->sub->core);
  230. if (ir->polling) {
  231. if (ir->last_gpio == gpio)
  232. return;
  233. ir->last_gpio = gpio;
  234. }
  235. /* extract data */
  236. data = ir_extract_bits(gpio, ir->mask_keycode);
  237. dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
  238. gpio, data,
  239. ir->polling ? "poll" : "irq",
  240. (gpio & ir->mask_keydown) ? " down" : "",
  241. (gpio & ir->mask_keyup) ? " up" : "");
  242. if (ir->mask_keydown) {
  243. /* bit set on keydown */
  244. if (gpio & ir->mask_keydown) {
  245. ir_input_keydown(ir->input, &ir->ir, data, data);
  246. } else {
  247. ir_input_nokey(ir->input, &ir->ir);
  248. }
  249. } else if (ir->mask_keyup) {
  250. /* bit cleared on keydown */
  251. if (0 == (gpio & ir->mask_keyup)) {
  252. ir_input_keydown(ir->input, &ir->ir, data, data);
  253. } else {
  254. ir_input_nokey(ir->input, &ir->ir);
  255. }
  256. } else {
  257. /* can't disturgissh keydown/up :-/ */
  258. ir_input_keydown(ir->input, &ir->ir, data, data);
  259. ir_input_nokey(ir->input, &ir->ir);
  260. }
  261. }
  262. static void ir_irq(struct bttv_sub_device *sub)
  263. {
  264. struct IR *ir = dev_get_drvdata(&sub->dev);
  265. if (!ir->polling)
  266. ir_handle_key(ir);
  267. }
  268. static void ir_timer(unsigned long data)
  269. {
  270. struct IR *ir = (struct IR*)data;
  271. schedule_work(&ir->work);
  272. }
  273. static void ir_work(void *data)
  274. {
  275. struct IR *ir = data;
  276. unsigned long timeout;
  277. ir_handle_key(ir);
  278. timeout = jiffies + (ir->polling * HZ / 1000);
  279. mod_timer(&ir->timer, timeout);
  280. }
  281. /* ---------------------------------------------------------------------- */
  282. static int ir_probe(struct device *dev)
  283. {
  284. struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
  285. struct IR *ir;
  286. struct input_dev *input_dev;
  287. IR_KEYTAB_TYPE *ir_codes = NULL;
  288. int ir_type = IR_TYPE_OTHER;
  289. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  290. input_dev = input_allocate_device();
  291. if (!ir || !input_dev) {
  292. kfree(ir);
  293. input_free_device(input_dev);
  294. return -ENOMEM;
  295. }
  296. /* detect & configure */
  297. switch (sub->core->type) {
  298. case BTTV_AVERMEDIA:
  299. case BTTV_AVPHONE98:
  300. case BTTV_AVERMEDIA98:
  301. ir_codes = ir_codes_avermedia;
  302. ir->mask_keycode = 0xf88000;
  303. ir->mask_keydown = 0x010000;
  304. ir->polling = 50; // ms
  305. break;
  306. case BTTV_AVDVBT_761:
  307. case BTTV_AVDVBT_771:
  308. ir_codes = ir_codes_avermedia_dvbt;
  309. ir->mask_keycode = 0x0f00c0;
  310. ir->mask_keydown = 0x000020;
  311. ir->polling = 50; // ms
  312. break;
  313. case BTTV_PXELVWPLTVPAK:
  314. ir_codes = ir_codes_pixelview;
  315. ir->mask_keycode = 0x003e00;
  316. ir->mask_keyup = 0x010000;
  317. ir->polling = 50; // ms
  318. break;
  319. case BTTV_PV_BT878P_9B:
  320. case BTTV_PV_BT878P_PLUS:
  321. ir_codes = ir_codes_pixelview;
  322. ir->mask_keycode = 0x001f00;
  323. ir->mask_keyup = 0x008000;
  324. ir->polling = 50; // ms
  325. break;
  326. case BTTV_WINFAST2000:
  327. ir_codes = ir_codes_winfast;
  328. ir->mask_keycode = 0x1f8;
  329. break;
  330. case BTTV_MAGICTVIEW061:
  331. case BTTV_MAGICTVIEW063:
  332. ir_codes = ir_codes_winfast;
  333. ir->mask_keycode = 0x0008e000;
  334. ir->mask_keydown = 0x00200000;
  335. break;
  336. case BTTV_APAC_VIEWCOMP:
  337. ir_codes = ir_codes_apac_viewcomp;
  338. ir->mask_keycode = 0x001f00;
  339. ir->mask_keyup = 0x008000;
  340. ir->polling = 50; // ms
  341. break;
  342. case BTTV_CONCEPTRONIC_CTVFMI2:
  343. ir_codes = ir_codes_conceptronic;
  344. ir->mask_keycode = 0x001F00;
  345. ir->mask_keyup = 0x006000;
  346. ir->polling = 50; // ms
  347. break;
  348. }
  349. if (NULL == ir_codes) {
  350. kfree(ir);
  351. input_free_device(input_dev);
  352. return -ENODEV;
  353. }
  354. /* init hardware-specific stuff */
  355. bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
  356. ir->sub = sub;
  357. /* init input device */
  358. snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
  359. sub->core->type);
  360. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  361. pci_name(sub->core->pci));
  362. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  363. input_dev->name = ir->name;
  364. input_dev->phys = ir->phys;
  365. input_dev->id.bustype = BUS_PCI;
  366. input_dev->id.version = 1;
  367. if (sub->core->pci->subsystem_vendor) {
  368. input_dev->id.vendor = sub->core->pci->subsystem_vendor;
  369. input_dev->id.product = sub->core->pci->subsystem_device;
  370. } else {
  371. input_dev->id.vendor = sub->core->pci->vendor;
  372. input_dev->id.product = sub->core->pci->device;
  373. }
  374. input_dev->cdev.dev = &sub->core->pci->dev;
  375. if (ir->polling) {
  376. INIT_WORK(&ir->work, ir_work, ir);
  377. init_timer(&ir->timer);
  378. ir->timer.function = ir_timer;
  379. ir->timer.data = (unsigned long)ir;
  380. schedule_work(&ir->work);
  381. }
  382. /* all done */
  383. dev_set_drvdata(dev, ir);
  384. input_register_device(ir->input);
  385. return 0;
  386. }
  387. static int ir_remove(struct device *dev)
  388. {
  389. struct IR *ir = dev_get_drvdata(dev);
  390. if (ir->polling) {
  391. del_timer(&ir->timer);
  392. flush_scheduled_work();
  393. }
  394. input_unregister_device(ir->input);
  395. kfree(ir);
  396. return 0;
  397. }
  398. /* ---------------------------------------------------------------------- */
  399. MODULE_AUTHOR("Gerd Knorr, Pavel Machek");
  400. MODULE_DESCRIPTION("input driver for bt8x8 gpio IR remote controls");
  401. MODULE_LICENSE("GPL");
  402. static int ir_init(void)
  403. {
  404. return bttv_sub_register(&driver, "remote");
  405. }
  406. static void ir_fini(void)
  407. {
  408. bttv_sub_unregister(&driver);
  409. }
  410. module_init(ir_init);
  411. module_exit(ir_fini);
  412. /*
  413. * Local variables:
  414. * c-basic-offset: 8
  415. * End:
  416. */