ir-kbd-gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * $Id: ir-kbd-gpio.c,v 1.13 2005/05/15 19:01:26 mchehab Exp $
  3. *
  4. * Copyright (c) 2003 Gerd Knorr
  5. * Copyright (c) 2003 Pavel Machek
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/input.h>
  27. #include <linux/pci.h>
  28. #include <media/ir-common.h>
  29. #include "bttv.h"
  30. /* ---------------------------------------------------------------------- */
  31. static IR_KEYTAB_TYPE ir_codes_avermedia[IR_KEYTAB_SIZE] = {
  32. [ 34 ] = KEY_KP0,
  33. [ 40 ] = KEY_KP1,
  34. [ 24 ] = KEY_KP2,
  35. [ 56 ] = KEY_KP3,
  36. [ 36 ] = KEY_KP4,
  37. [ 20 ] = KEY_KP5,
  38. [ 52 ] = KEY_KP6,
  39. [ 44 ] = KEY_KP7,
  40. [ 28 ] = KEY_KP8,
  41. [ 60 ] = KEY_KP9,
  42. [ 48 ] = KEY_EJECTCD, // Unmarked on my controller
  43. [ 0 ] = KEY_POWER,
  44. [ 18 ] = BTN_LEFT, // DISPLAY/L
  45. [ 50 ] = BTN_RIGHT, // LOOP/R
  46. [ 10 ] = KEY_MUTE,
  47. [ 38 ] = KEY_RECORD,
  48. [ 22 ] = KEY_PAUSE,
  49. [ 54 ] = KEY_STOP,
  50. [ 30 ] = KEY_VOLUMEDOWN,
  51. [ 62 ] = KEY_VOLUMEUP,
  52. [ 32 ] = KEY_TUNER, // TV/FM
  53. [ 16 ] = KEY_CD,
  54. [ 8 ] = KEY_VIDEO,
  55. [ 4 ] = KEY_AUDIO,
  56. [ 12 ] = KEY_ZOOM, // full screen
  57. [ 2 ] = KEY_INFO, // preview
  58. [ 42 ] = KEY_SEARCH, // autoscan
  59. [ 26 ] = KEY_STOP, // freeze
  60. [ 58 ] = KEY_RECORD, // capture
  61. [ 6 ] = KEY_PLAY, // unmarked
  62. [ 46 ] = KEY_RED, // unmarked
  63. [ 14 ] = KEY_GREEN, // unmarked
  64. [ 33 ] = KEY_YELLOW, // unmarked
  65. [ 17 ] = KEY_CHANNELDOWN,
  66. [ 49 ] = KEY_CHANNELUP,
  67. [ 1 ] = KEY_BLUE, // unmarked
  68. };
  69. /* Matt Jesson <dvb@jesson.eclipse.co.uk */
  70. static IR_KEYTAB_TYPE ir_codes_avermedia_dvbt[IR_KEYTAB_SIZE] = {
  71. [ 0x28 ] = KEY_KP0, //'0' / 'enter'
  72. [ 0x22 ] = KEY_KP1, //'1'
  73. [ 0x12 ] = KEY_KP2, //'2' / 'up arrow'
  74. [ 0x32 ] = KEY_KP3, //'3'
  75. [ 0x24 ] = KEY_KP4, //'4' / 'left arrow'
  76. [ 0x14 ] = KEY_KP5, //'5'
  77. [ 0x34 ] = KEY_KP6, //'6' / 'right arrow'
  78. [ 0x26 ] = KEY_KP7, //'7'
  79. [ 0x16 ] = KEY_KP8, //'8' / 'down arrow'
  80. [ 0x36 ] = KEY_KP9, //'9'
  81. [ 0x20 ] = KEY_LIST, // 'source'
  82. [ 0x10 ] = KEY_TEXT, // 'teletext'
  83. [ 0x00 ] = KEY_POWER, // 'power'
  84. [ 0x04 ] = KEY_AUDIO, // 'audio'
  85. [ 0x06 ] = KEY_ZOOM, // 'full screen'
  86. [ 0x18 ] = KEY_VIDEO, // 'display'
  87. [ 0x38 ] = KEY_SEARCH, // 'loop'
  88. [ 0x08 ] = KEY_INFO, // 'preview'
  89. [ 0x2a ] = KEY_REWIND, // 'backward <<'
  90. [ 0x1a ] = KEY_FASTFORWARD, // 'forward >>'
  91. [ 0x3a ] = KEY_RECORD, // 'capture'
  92. [ 0x0a ] = KEY_MUTE, // 'mute'
  93. [ 0x2c ] = KEY_RECORD, // 'record'
  94. [ 0x1c ] = KEY_PAUSE, // 'pause'
  95. [ 0x3c ] = KEY_STOP, // 'stop'
  96. [ 0x0c ] = KEY_PLAY, // 'play'
  97. [ 0x2e ] = KEY_RED, // 'red'
  98. [ 0x01 ] = KEY_BLUE, // 'blue' / 'cancel'
  99. [ 0x0e ] = KEY_YELLOW, // 'yellow' / 'ok'
  100. [ 0x21 ] = KEY_GREEN, // 'green'
  101. [ 0x11 ] = KEY_CHANNELDOWN, // 'channel -'
  102. [ 0x31 ] = KEY_CHANNELUP, // 'channel +'
  103. [ 0x1e ] = KEY_VOLUMEDOWN, // 'volume -'
  104. [ 0x3e ] = KEY_VOLUMEUP, // 'volume +'
  105. };
  106. /* Attila Kondoros <attila.kondoros@chello.hu> */
  107. static IR_KEYTAB_TYPE ir_codes_apac_viewcomp[IR_KEYTAB_SIZE] = {
  108. [ 1 ] = KEY_KP1,
  109. [ 2 ] = KEY_KP2,
  110. [ 3 ] = KEY_KP3,
  111. [ 4 ] = KEY_KP4,
  112. [ 5 ] = KEY_KP5,
  113. [ 6 ] = KEY_KP6,
  114. [ 7 ] = KEY_KP7,
  115. [ 8 ] = KEY_KP8,
  116. [ 9 ] = KEY_KP9,
  117. [ 0 ] = KEY_KP0,
  118. [ 23 ] = KEY_LAST, // +100
  119. [ 10 ] = KEY_LIST, // recall
  120. [ 28 ] = KEY_TUNER, // TV/FM
  121. [ 21 ] = KEY_SEARCH, // scan
  122. [ 18 ] = KEY_POWER, // power
  123. [ 31 ] = KEY_VOLUMEDOWN, // vol up
  124. [ 27 ] = KEY_VOLUMEUP, // vol down
  125. [ 30 ] = KEY_CHANNELDOWN, // chn up
  126. [ 26 ] = KEY_CHANNELUP, // chn down
  127. [ 17 ] = KEY_VIDEO, // video
  128. [ 15 ] = KEY_ZOOM, // full screen
  129. [ 19 ] = KEY_MUTE, // mute/unmute
  130. [ 16 ] = KEY_TEXT, // min
  131. [ 13 ] = KEY_STOP, // freeze
  132. [ 14 ] = KEY_RECORD, // record
  133. [ 29 ] = KEY_PLAYPAUSE, // stop
  134. [ 25 ] = KEY_PLAY, // play
  135. [ 22 ] = KEY_GOTO, // osd
  136. [ 20 ] = KEY_REFRESH, // default
  137. [ 12 ] = KEY_KPPLUS, // fine tune >>>>
  138. [ 24 ] = KEY_KPMINUS // fine tune <<<<
  139. };
  140. /* ---------------------------------------------------------------------- */
  141. struct IR {
  142. struct bttv_sub_device *sub;
  143. struct input_dev input;
  144. struct ir_input_state ir;
  145. char name[32];
  146. char phys[32];
  147. u32 mask_keycode;
  148. u32 mask_keydown;
  149. u32 mask_keyup;
  150. int polling;
  151. u32 last_gpio;
  152. struct work_struct work;
  153. struct timer_list timer;
  154. };
  155. static int debug;
  156. module_param(debug, int, 0644); /* debug level (0,1,2) */
  157. #define DEVNAME "ir-kbd-gpio"
  158. #define dprintk(fmt, arg...) if (debug) \
  159. printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
  160. static void ir_irq(struct bttv_sub_device *sub);
  161. static int ir_probe(struct device *dev);
  162. static int ir_remove(struct device *dev);
  163. static struct bttv_sub_driver driver = {
  164. .drv = {
  165. .name = DEVNAME,
  166. .probe = ir_probe,
  167. .remove = ir_remove,
  168. },
  169. .gpio_irq = ir_irq,
  170. };
  171. /* ---------------------------------------------------------------------- */
  172. static void ir_handle_key(struct IR *ir)
  173. {
  174. u32 gpio,data;
  175. /* read gpio value */
  176. gpio = bttv_gpio_read(ir->sub->core);
  177. if (ir->polling) {
  178. if (ir->last_gpio == gpio)
  179. return;
  180. ir->last_gpio = gpio;
  181. }
  182. /* extract data */
  183. data = ir_extract_bits(gpio, ir->mask_keycode);
  184. dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
  185. gpio, data,
  186. ir->polling ? "poll" : "irq",
  187. (gpio & ir->mask_keydown) ? " down" : "",
  188. (gpio & ir->mask_keyup) ? " up" : "");
  189. if (ir->mask_keydown) {
  190. /* bit set on keydown */
  191. if (gpio & ir->mask_keydown) {
  192. ir_input_keydown(&ir->input,&ir->ir,data,data);
  193. } else {
  194. ir_input_nokey(&ir->input,&ir->ir);
  195. }
  196. } else if (ir->mask_keyup) {
  197. /* bit cleared on keydown */
  198. if (0 == (gpio & ir->mask_keyup)) {
  199. ir_input_keydown(&ir->input,&ir->ir,data,data);
  200. } else {
  201. ir_input_nokey(&ir->input,&ir->ir);
  202. }
  203. } else {
  204. /* can't disturgissh keydown/up :-/ */
  205. ir_input_keydown(&ir->input,&ir->ir,data,data);
  206. ir_input_nokey(&ir->input,&ir->ir);
  207. }
  208. }
  209. static void ir_irq(struct bttv_sub_device *sub)
  210. {
  211. struct IR *ir = dev_get_drvdata(&sub->dev);
  212. if (!ir->polling)
  213. ir_handle_key(ir);
  214. }
  215. static void ir_timer(unsigned long data)
  216. {
  217. struct IR *ir = (struct IR*)data;
  218. schedule_work(&ir->work);
  219. }
  220. static void ir_work(void *data)
  221. {
  222. struct IR *ir = data;
  223. unsigned long timeout;
  224. ir_handle_key(ir);
  225. timeout = jiffies + (ir->polling * HZ / 1000);
  226. mod_timer(&ir->timer, timeout);
  227. }
  228. /* ---------------------------------------------------------------------- */
  229. static int ir_probe(struct device *dev)
  230. {
  231. struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
  232. struct IR *ir;
  233. IR_KEYTAB_TYPE *ir_codes = NULL;
  234. int ir_type = IR_TYPE_OTHER;
  235. ir = kmalloc(sizeof(*ir),GFP_KERNEL);
  236. if (NULL == ir)
  237. return -ENOMEM;
  238. memset(ir,0,sizeof(*ir));
  239. /* detect & configure */
  240. switch (sub->core->type) {
  241. case BTTV_AVERMEDIA:
  242. case BTTV_AVPHONE98:
  243. case BTTV_AVERMEDIA98:
  244. ir_codes = ir_codes_avermedia;
  245. ir->mask_keycode = 0xf88000;
  246. ir->mask_keydown = 0x010000;
  247. ir->polling = 50; // ms
  248. break;
  249. case BTTV_AVDVBT_761:
  250. case BTTV_AVDVBT_771:
  251. ir_codes = ir_codes_avermedia_dvbt;
  252. ir->mask_keycode = 0x0f00c0;
  253. ir->mask_keydown = 0x000020;
  254. ir->polling = 50; // ms
  255. break;
  256. case BTTV_PXELVWPLTVPAK:
  257. ir_codes = ir_codes_pixelview;
  258. ir->mask_keycode = 0x003e00;
  259. ir->mask_keyup = 0x010000;
  260. ir->polling = 50; // ms
  261. break;
  262. case BTTV_PV_BT878P_9B:
  263. case BTTV_PV_BT878P_PLUS:
  264. ir_codes = ir_codes_pixelview;
  265. ir->mask_keycode = 0x001f00;
  266. ir->mask_keyup = 0x008000;
  267. ir->polling = 50; // ms
  268. break;
  269. case BTTV_WINFAST2000:
  270. ir_codes = ir_codes_winfast;
  271. ir->mask_keycode = 0x1f8;
  272. break;
  273. case BTTV_MAGICTVIEW061:
  274. case BTTV_MAGICTVIEW063:
  275. ir_codes = ir_codes_winfast;
  276. ir->mask_keycode = 0x0008e000;
  277. ir->mask_keydown = 0x00200000;
  278. break;
  279. case BTTV_APAC_VIEWCOMP:
  280. ir_codes = ir_codes_apac_viewcomp;
  281. ir->mask_keycode = 0x001f00;
  282. ir->mask_keyup = 0x008000;
  283. ir->polling = 50; // ms
  284. break;
  285. }
  286. if (NULL == ir_codes) {
  287. kfree(ir);
  288. return -ENODEV;
  289. }
  290. /* init hardware-specific stuff */
  291. bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
  292. ir->sub = sub;
  293. /* init input device */
  294. snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
  295. sub->core->type);
  296. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  297. pci_name(sub->core->pci));
  298. ir_input_init(&ir->input, &ir->ir, ir_type, ir_codes);
  299. ir->input.name = ir->name;
  300. ir->input.phys = ir->phys;
  301. ir->input.id.bustype = BUS_PCI;
  302. ir->input.id.version = 1;
  303. if (sub->core->pci->subsystem_vendor) {
  304. ir->input.id.vendor = sub->core->pci->subsystem_vendor;
  305. ir->input.id.product = sub->core->pci->subsystem_device;
  306. } else {
  307. ir->input.id.vendor = sub->core->pci->vendor;
  308. ir->input.id.product = sub->core->pci->device;
  309. }
  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. */