cx88-input.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * $Id: cx88-input.c,v 1.9 2005/03/04 09:12:23 kraxel Exp $
  3. *
  4. * Device driver for GPIO attached remote control interfaces
  5. * on Conexant 2388x based TV/DVB cards.
  6. *
  7. * Copyright (c) 2003 Pavel Machek
  8. * Copyright (c) 2004 Gerd Knorr
  9. * Copyright (c) 2004 Chris Pascoe
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/input.h>
  28. #include <linux/pci.h>
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <media/ir-common.h>
  32. #include "cx88.h"
  33. /* ---------------------------------------------------------------------- */
  34. /* DigitalNow DNTV Live DVB-T Remote */
  35. static IR_KEYTAB_TYPE ir_codes_dntv_live_dvb_t[IR_KEYTAB_SIZE] = {
  36. [ 0x00 ] = KEY_ESC, // 'go up a level?'
  37. [ 0x01 ] = KEY_KP1, // '1'
  38. [ 0x02 ] = KEY_KP2, // '2'
  39. [ 0x03 ] = KEY_KP3, // '3'
  40. [ 0x04 ] = KEY_KP4, // '4'
  41. [ 0x05 ] = KEY_KP5, // '5'
  42. [ 0x06 ] = KEY_KP6, // '6'
  43. [ 0x07 ] = KEY_KP7, // '7'
  44. [ 0x08 ] = KEY_KP8, // '8'
  45. [ 0x09 ] = KEY_KP9, // '9'
  46. [ 0x0a ] = KEY_KP0, // '0'
  47. [ 0x0b ] = KEY_TUNER, // 'tv/fm'
  48. [ 0x0c ] = KEY_SEARCH, // 'scan'
  49. [ 0x0d ] = KEY_STOP, // 'stop'
  50. [ 0x0e ] = KEY_PAUSE, // 'pause'
  51. [ 0x0f ] = KEY_LIST, // 'source'
  52. [ 0x10 ] = KEY_MUTE, // 'mute'
  53. [ 0x11 ] = KEY_REWIND, // 'backward <<'
  54. [ 0x12 ] = KEY_POWER, // 'power'
  55. [ 0x13 ] = KEY_S, // 'snap'
  56. [ 0x14 ] = KEY_AUDIO, // 'stereo'
  57. [ 0x15 ] = KEY_CLEAR, // 'reset'
  58. [ 0x16 ] = KEY_PLAY, // 'play'
  59. [ 0x17 ] = KEY_ENTER, // 'enter'
  60. [ 0x18 ] = KEY_ZOOM, // 'full screen'
  61. [ 0x19 ] = KEY_FASTFORWARD, // 'forward >>'
  62. [ 0x1a ] = KEY_CHANNELUP, // 'channel +'
  63. [ 0x1b ] = KEY_VOLUMEUP, // 'volume +'
  64. [ 0x1c ] = KEY_INFO, // 'preview'
  65. [ 0x1d ] = KEY_RECORD, // 'record'
  66. [ 0x1e ] = KEY_CHANNELDOWN, // 'channel -'
  67. [ 0x1f ] = KEY_VOLUMEDOWN, // 'volume -'
  68. };
  69. /* ---------------------------------------------------------------------- */
  70. /* IO-DATA BCTV7E Remote */
  71. static IR_KEYTAB_TYPE ir_codes_iodata_bctv7e[IR_KEYTAB_SIZE] = {
  72. [ 0x40 ] = KEY_TV, // TV
  73. [ 0x20 ] = KEY_RADIO, // FM
  74. [ 0x60 ] = KEY_EPG, // EPG
  75. [ 0x00 ] = KEY_POWER, // power
  76. [ 0x50 ] = KEY_KP1, // 1
  77. [ 0x30 ] = KEY_KP2, // 2
  78. [ 0x70 ] = KEY_KP3, // 3
  79. [ 0x10 ] = KEY_L, // Live
  80. [ 0x48 ] = KEY_KP4, // 4
  81. [ 0x28 ] = KEY_KP5, // 5
  82. [ 0x68 ] = KEY_KP6, // 6
  83. [ 0x08 ] = KEY_T, // Time Shift
  84. [ 0x58 ] = KEY_KP7, // 7
  85. [ 0x38 ] = KEY_KP8, // 8
  86. [ 0x78 ] = KEY_KP9, // 9
  87. [ 0x18 ] = KEY_PLAYPAUSE, // Play
  88. [ 0x44 ] = KEY_KP0, // 10
  89. [ 0x24 ] = KEY_ENTER, // 11
  90. [ 0x64 ] = KEY_ESC, // 12
  91. [ 0x04 ] = KEY_M, // Multi
  92. [ 0x54 ] = KEY_VIDEO, // VIDEO
  93. [ 0x34 ] = KEY_CHANNELUP, // channel +
  94. [ 0x74 ] = KEY_VOLUMEUP, // volume +
  95. [ 0x14 ] = KEY_MUTE, // Mute
  96. [ 0x4c ] = KEY_S, // SVIDEO
  97. [ 0x2c ] = KEY_CHANNELDOWN, // channel -
  98. [ 0x6c ] = KEY_VOLUMEDOWN, // volume -
  99. [ 0x0c ] = KEY_ZOOM, // Zoom
  100. [ 0x5c ] = KEY_PAUSE, // pause
  101. [ 0x3c ] = KEY_C, // || (red)
  102. [ 0x7c ] = KEY_RECORD, // recording
  103. [ 0x1c ] = KEY_STOP, // stop
  104. [ 0x41 ] = KEY_REWIND, // backward <<
  105. [ 0x21 ] = KEY_PLAY, // play
  106. [ 0x61 ] = KEY_FASTFORWARD, // forward >>
  107. [ 0x01 ] = KEY_NEXT, // skip >|
  108. };
  109. /* ---------------------------------------------------------------------- */
  110. struct cx88_IR {
  111. struct cx88_core *core;
  112. struct input_dev input;
  113. struct ir_input_state ir;
  114. char name[32];
  115. char phys[32];
  116. /* sample from gpio pin 16 */
  117. int sampling;
  118. u32 samples[16];
  119. int scount;
  120. unsigned long release;
  121. /* poll external decoder */
  122. int polling;
  123. struct work_struct work;
  124. struct timer_list timer;
  125. u32 gpio_addr;
  126. u32 last_gpio;
  127. u32 mask_keycode;
  128. u32 mask_keydown;
  129. u32 mask_keyup;
  130. };
  131. static int ir_debug = 0;
  132. module_param(ir_debug, int, 0644); /* debug level [IR] */
  133. MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
  134. #define ir_dprintk(fmt, arg...) if (ir_debug) \
  135. printk(KERN_DEBUG "%s IR: " fmt , ir->core->name, ## arg)
  136. /* ---------------------------------------------------------------------- */
  137. static void cx88_ir_handle_key(struct cx88_IR *ir)
  138. {
  139. struct cx88_core *core = ir->core;
  140. u32 gpio, data;
  141. /* read gpio value */
  142. gpio = cx_read(ir->gpio_addr);
  143. if (ir->polling) {
  144. if (ir->last_gpio == gpio)
  145. return;
  146. ir->last_gpio = gpio;
  147. }
  148. /* extract data */
  149. data = ir_extract_bits(gpio, ir->mask_keycode);
  150. ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
  151. gpio, data,
  152. ir->polling ? "poll" : "irq",
  153. (gpio & ir->mask_keydown) ? " down" : "",
  154. (gpio & ir->mask_keyup) ? " up" : "");
  155. if (ir->mask_keydown) {
  156. /* bit set on keydown */
  157. if (gpio & ir->mask_keydown) {
  158. ir_input_keydown(&ir->input,&ir->ir,data,data);
  159. } else {
  160. ir_input_nokey(&ir->input,&ir->ir);
  161. }
  162. } else if (ir->mask_keyup) {
  163. /* bit cleared on keydown */
  164. if (0 == (gpio & ir->mask_keyup)) {
  165. ir_input_keydown(&ir->input,&ir->ir,data,data);
  166. } else {
  167. ir_input_nokey(&ir->input,&ir->ir);
  168. }
  169. } else {
  170. /* can't distinguish keydown/up :-/ */
  171. ir_input_keydown(&ir->input,&ir->ir,data,data);
  172. ir_input_nokey(&ir->input,&ir->ir);
  173. }
  174. }
  175. static void ir_timer(unsigned long data)
  176. {
  177. struct cx88_IR *ir = (struct cx88_IR*)data;
  178. schedule_work(&ir->work);
  179. }
  180. static void cx88_ir_work(void *data)
  181. {
  182. struct cx88_IR *ir = data;
  183. unsigned long timeout;
  184. cx88_ir_handle_key(ir);
  185. timeout = jiffies + (ir->polling * HZ / 1000);
  186. mod_timer(&ir->timer, timeout);
  187. }
  188. /* ---------------------------------------------------------------------- */
  189. int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
  190. {
  191. struct cx88_IR *ir;
  192. IR_KEYTAB_TYPE *ir_codes = NULL;
  193. int ir_type = IR_TYPE_OTHER;
  194. ir = kmalloc(sizeof(*ir),GFP_KERNEL);
  195. if (NULL == ir)
  196. return -ENOMEM;
  197. memset(ir,0,sizeof(*ir));
  198. /* detect & configure */
  199. switch (core->board) {
  200. case CX88_BOARD_DNTV_LIVE_DVB_T:
  201. ir_codes = ir_codes_dntv_live_dvb_t;
  202. ir->gpio_addr = MO_GP1_IO;
  203. ir->mask_keycode = 0x1f;
  204. ir->mask_keyup = 0x60;
  205. ir->polling = 50; // ms
  206. break;
  207. case CX88_BOARD_HAUPPAUGE:
  208. case CX88_BOARD_HAUPPAUGE_DVB_T1:
  209. ir_codes = ir_codes_hauppauge_new;
  210. ir_type = IR_TYPE_RC5;
  211. ir->sampling = 1;
  212. break;
  213. case CX88_BOARD_WINFAST2000XP_EXPERT:
  214. ir_codes = ir_codes_winfast;
  215. ir->gpio_addr = MO_GP0_IO;
  216. ir->mask_keycode = 0x8f8;
  217. ir->mask_keyup = 0x100;
  218. ir->polling = 1; // ms
  219. break;
  220. case CX88_BOARD_IODATA_GVBCTV7E:
  221. ir_codes = ir_codes_iodata_bctv7e;
  222. ir->gpio_addr = MO_GP0_IO;
  223. ir->mask_keycode = 0xfd;
  224. ir->mask_keydown = 0x02;
  225. ir->polling = 5; // ms
  226. break;
  227. case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
  228. ir_codes = ir_codes_pixelview;
  229. ir->gpio_addr = MO_GP1_IO;
  230. ir->mask_keycode = 0x1f;
  231. ir->mask_keyup = 0x80;
  232. ir->polling = 1; // ms
  233. break;
  234. }
  235. if (NULL == ir_codes) {
  236. kfree(ir);
  237. return -ENODEV;
  238. }
  239. /* init input device */
  240. snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)",
  241. cx88_boards[core->board].name);
  242. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  243. pci_name(pci));
  244. ir_input_init(&ir->input, &ir->ir, ir_type, ir_codes);
  245. ir->input.name = ir->name;
  246. ir->input.phys = ir->phys;
  247. ir->input.id.bustype = BUS_PCI;
  248. ir->input.id.version = 1;
  249. if (pci->subsystem_vendor) {
  250. ir->input.id.vendor = pci->subsystem_vendor;
  251. ir->input.id.product = pci->subsystem_device;
  252. } else {
  253. ir->input.id.vendor = pci->vendor;
  254. ir->input.id.product = pci->device;
  255. }
  256. /* record handles to ourself */
  257. ir->core = core;
  258. core->ir = ir;
  259. if (ir->polling) {
  260. INIT_WORK(&ir->work, cx88_ir_work, ir);
  261. init_timer(&ir->timer);
  262. ir->timer.function = ir_timer;
  263. ir->timer.data = (unsigned long)ir;
  264. schedule_work(&ir->work);
  265. }
  266. if (ir->sampling) {
  267. core->pci_irqmask |= (1<<18); // IR_SMP_INT
  268. cx_write(MO_DDS_IO, 0xa80a80); // 4 kHz sample rate
  269. cx_write(MO_DDSCFG_IO, 0x5); // enable
  270. }
  271. /* all done */
  272. input_register_device(&ir->input);
  273. printk("%s: registered IR remote control\n", core->name);
  274. return 0;
  275. }
  276. int cx88_ir_fini(struct cx88_core *core)
  277. {
  278. struct cx88_IR *ir = core->ir;
  279. /* skip detach on non attached boards */
  280. if (NULL == ir)
  281. return 0;
  282. if (ir->polling) {
  283. del_timer(&ir->timer);
  284. flush_scheduled_work();
  285. }
  286. input_unregister_device(&ir->input);
  287. kfree(ir);
  288. /* done */
  289. core->ir = NULL;
  290. return 0;
  291. }
  292. /* ---------------------------------------------------------------------- */
  293. void cx88_ir_irq(struct cx88_core *core)
  294. {
  295. struct cx88_IR *ir = core->ir;
  296. u32 samples,rc5;
  297. int i;
  298. if (NULL == ir)
  299. return;
  300. if (!ir->sampling)
  301. return;
  302. samples = cx_read(MO_SAMPLE_IO);
  303. if (0 != samples && 0xffffffff != samples) {
  304. /* record sample data */
  305. if (ir->scount < ARRAY_SIZE(ir->samples))
  306. ir->samples[ir->scount++] = samples;
  307. return;
  308. }
  309. if (!ir->scount) {
  310. /* nothing to sample */
  311. if (ir->ir.keypressed && time_after(jiffies,ir->release))
  312. ir_input_nokey(&ir->input,&ir->ir);
  313. return;
  314. }
  315. /* have a complete sample */
  316. if (ir->scount < ARRAY_SIZE(ir->samples))
  317. ir->samples[ir->scount++] = samples;
  318. for (i = 0; i < ir->scount; i++)
  319. ir->samples[i] = ~ir->samples[i];
  320. if (ir_debug)
  321. ir_dump_samples(ir->samples,ir->scount);
  322. /* decode it */
  323. switch (core->board) {
  324. case CX88_BOARD_HAUPPAUGE:
  325. case CX88_BOARD_HAUPPAUGE_DVB_T1:
  326. rc5 = ir_decode_biphase(ir->samples,ir->scount,5,7);
  327. ir_dprintk("biphase decoded: %x\n",rc5);
  328. if ((rc5 & 0xfffff000) != 0x3000)
  329. break;
  330. ir_input_keydown(&ir->input, &ir->ir, rc5 & 0x3f, rc5);
  331. ir->release = jiffies + msecs_to_jiffies(120);
  332. break;
  333. }
  334. ir->scount = 0;
  335. return;
  336. }
  337. /* ---------------------------------------------------------------------- */
  338. MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
  339. MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
  340. MODULE_LICENSE("GPL");
  341. /*
  342. * Local variables:
  343. * c-basic-offset: 8
  344. * End:
  345. */