ir-kbd-gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. struct IR {
  141. struct bttv_sub_device *sub;
  142. struct input_dev input;
  143. struct ir_input_state ir;
  144. char name[32];
  145. char phys[32];
  146. u32 mask_keycode;
  147. u32 mask_keydown;
  148. u32 mask_keyup;
  149. int polling;
  150. u32 last_gpio;
  151. struct work_struct work;
  152. struct timer_list timer;
  153. };
  154. static int debug;
  155. module_param(debug, int, 0644); /* debug level (0,1,2) */
  156. #define DEVNAME "ir-kbd-gpio"
  157. #define dprintk(fmt, arg...) if (debug) \
  158. printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
  159. static void ir_irq(struct bttv_sub_device *sub);
  160. static int ir_probe(struct device *dev);
  161. static int ir_remove(struct device *dev);
  162. static struct bttv_sub_driver driver = {
  163. .drv = {
  164. .name = DEVNAME,
  165. .probe = ir_probe,
  166. .remove = ir_remove,
  167. },
  168. .gpio_irq = ir_irq,
  169. };
  170. /* ---------------------------------------------------------------------- */
  171. static void ir_handle_key(struct IR *ir)
  172. {
  173. u32 gpio,data;
  174. /* read gpio value */
  175. gpio = bttv_gpio_read(ir->sub->core);
  176. if (ir->polling) {
  177. if (ir->last_gpio == gpio)
  178. return;
  179. ir->last_gpio = gpio;
  180. }
  181. /* extract data */
  182. data = ir_extract_bits(gpio, ir->mask_keycode);
  183. dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
  184. gpio, data,
  185. ir->polling ? "poll" : "irq",
  186. (gpio & ir->mask_keydown) ? " down" : "",
  187. (gpio & ir->mask_keyup) ? " up" : "");
  188. if (ir->mask_keydown) {
  189. /* bit set on keydown */
  190. if (gpio & ir->mask_keydown) {
  191. ir_input_keydown(&ir->input,&ir->ir,data,data);
  192. } else {
  193. ir_input_nokey(&ir->input,&ir->ir);
  194. }
  195. } else if (ir->mask_keyup) {
  196. /* bit cleared on keydown */
  197. if (0 == (gpio & ir->mask_keyup)) {
  198. ir_input_keydown(&ir->input,&ir->ir,data,data);
  199. } else {
  200. ir_input_nokey(&ir->input,&ir->ir);
  201. }
  202. } else {
  203. /* can't disturgissh keydown/up :-/ */
  204. ir_input_keydown(&ir->input,&ir->ir,data,data);
  205. ir_input_nokey(&ir->input,&ir->ir);
  206. }
  207. }
  208. static void ir_irq(struct bttv_sub_device *sub)
  209. {
  210. struct IR *ir = dev_get_drvdata(&sub->dev);
  211. if (!ir->polling)
  212. ir_handle_key(ir);
  213. }
  214. static void ir_timer(unsigned long data)
  215. {
  216. struct IR *ir = (struct IR*)data;
  217. schedule_work(&ir->work);
  218. }
  219. static void ir_work(void *data)
  220. {
  221. struct IR *ir = data;
  222. unsigned long timeout;
  223. ir_handle_key(ir);
  224. timeout = jiffies + (ir->polling * HZ / 1000);
  225. mod_timer(&ir->timer, timeout);
  226. }
  227. /* ---------------------------------------------------------------------- */
  228. static int ir_probe(struct device *dev)
  229. {
  230. struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
  231. struct IR *ir;
  232. IR_KEYTAB_TYPE *ir_codes = NULL;
  233. int ir_type = IR_TYPE_OTHER;
  234. ir = kmalloc(sizeof(*ir),GFP_KERNEL);
  235. if (NULL == ir)
  236. return -ENOMEM;
  237. memset(ir,0,sizeof(*ir));
  238. /* detect & configure */
  239. switch (sub->core->type) {
  240. case BTTV_AVERMEDIA:
  241. case BTTV_AVPHONE98:
  242. case BTTV_AVERMEDIA98:
  243. ir_codes = ir_codes_avermedia;
  244. ir->mask_keycode = 0xf88000;
  245. ir->mask_keydown = 0x010000;
  246. ir->polling = 50; // ms
  247. break;
  248. case BTTV_AVDVBT_761:
  249. case BTTV_AVDVBT_771:
  250. ir_codes = ir_codes_avermedia_dvbt;
  251. ir->mask_keycode = 0x0f00c0;
  252. ir->mask_keydown = 0x000020;
  253. ir->polling = 50; // ms
  254. break;
  255. case BTTV_PXELVWPLTVPAK:
  256. ir_codes = ir_codes_pixelview;
  257. ir->mask_keycode = 0x003e00;
  258. ir->mask_keyup = 0x010000;
  259. ir->polling = 50; // ms
  260. break;
  261. case BTTV_PV_BT878P_9B:
  262. case BTTV_PV_BT878P_PLUS:
  263. ir_codes = ir_codes_pixelview;
  264. ir->mask_keycode = 0x001f00;
  265. ir->mask_keyup = 0x008000;
  266. ir->polling = 50; // ms
  267. break;
  268. case BTTV_WINFAST2000:
  269. ir_codes = ir_codes_winfast;
  270. ir->mask_keycode = 0x1f8;
  271. break;
  272. case BTTV_MAGICTVIEW061:
  273. case BTTV_MAGICTVIEW063:
  274. ir_codes = ir_codes_winfast;
  275. ir->mask_keycode = 0x0008e000;
  276. ir->mask_keydown = 0x00200000;
  277. break;
  278. case BTTV_APAC_VIEWCOMP:
  279. ir_codes = ir_codes_apac_viewcomp;
  280. ir->mask_keycode = 0x001f00;
  281. ir->mask_keyup = 0x008000;
  282. ir->polling = 50; // ms
  283. break;
  284. }
  285. if (NULL == ir_codes) {
  286. kfree(ir);
  287. return -ENODEV;
  288. }
  289. /* init hardware-specific stuff */
  290. bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
  291. ir->sub = sub;
  292. /* init input device */
  293. snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
  294. sub->core->type);
  295. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  296. pci_name(sub->core->pci));
  297. ir_input_init(&ir->input, &ir->ir, ir_type, ir_codes);
  298. ir->input.name = ir->name;
  299. ir->input.phys = ir->phys;
  300. ir->input.id.bustype = BUS_PCI;
  301. ir->input.id.version = 1;
  302. if (sub->core->pci->subsystem_vendor) {
  303. ir->input.id.vendor = sub->core->pci->subsystem_vendor;
  304. ir->input.id.product = sub->core->pci->subsystem_device;
  305. } else {
  306. ir->input.id.vendor = sub->core->pci->vendor;
  307. ir->input.id.product = sub->core->pci->device;
  308. }
  309. ir->input.dev = &sub->core->pci->dev;
  310. if (ir->polling) {
  311. INIT_WORK(&ir->work, ir_work, ir);
  312. init_timer(&ir->timer);
  313. ir->timer.function = ir_timer;
  314. ir->timer.data = (unsigned long)ir;
  315. schedule_work(&ir->work);
  316. }
  317. /* all done */
  318. dev_set_drvdata(dev,ir);
  319. input_register_device(&ir->input);
  320. printk(DEVNAME ": %s detected at %s\n",ir->input.name,ir->input.phys);
  321. return 0;
  322. }
  323. static int ir_remove(struct device *dev)
  324. {
  325. struct IR *ir = dev_get_drvdata(dev);
  326. if (ir->polling) {
  327. del_timer(&ir->timer);
  328. flush_scheduled_work();
  329. }
  330. input_unregister_device(&ir->input);
  331. kfree(ir);
  332. return 0;
  333. }
  334. /* ---------------------------------------------------------------------- */
  335. MODULE_AUTHOR("Gerd Knorr, Pavel Machek");
  336. MODULE_DESCRIPTION("input driver for bt8x8 gpio IR remote controls");
  337. MODULE_LICENSE("GPL");
  338. static int ir_init(void)
  339. {
  340. return bttv_sub_register(&driver, "remote");
  341. }
  342. static void ir_fini(void)
  343. {
  344. bttv_sub_unregister(&driver);
  345. }
  346. module_init(ir_init);
  347. module_exit(ir_fini);
  348. /*
  349. * Local variables:
  350. * c-basic-offset: 8
  351. * End:
  352. */