cx88-input.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * $Id: cx88-input.c,v 1.13 2005/06/13 16:07:46 nsh 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. /* ADS Tech Instant TV DVB-T PCI Remote */
  111. static IR_KEYTAB_TYPE ir_codes_adstech_dvb_t_pci[IR_KEYTAB_SIZE] = {
  112. [ 0x5b ] = KEY_POWER,
  113. [ 0x5f ] = KEY_MUTE,
  114. [ 0x57 ] = KEY_1,
  115. [ 0x4f ] = KEY_2,
  116. [ 0x53 ] = KEY_3,
  117. [ 0x56 ] = KEY_4,
  118. [ 0x4e ] = KEY_5,
  119. [ 0x5e ] = KEY_6,
  120. [ 0x54 ] = KEY_7,
  121. [ 0x4c ] = KEY_8,
  122. [ 0x5c ] = KEY_9,
  123. [ 0x4d ] = KEY_0,
  124. [ 0x55 ] = KEY_GOTO,
  125. [ 0x5d ] = KEY_SEARCH,
  126. [ 0x17 ] = KEY_EPG, // Guide
  127. [ 0x1f ] = KEY_MENU,
  128. [ 0x0f ] = KEY_UP,
  129. [ 0x46 ] = KEY_DOWN,
  130. [ 0x16 ] = KEY_LEFT,
  131. [ 0x1e ] = KEY_RIGHT,
  132. [ 0x0e ] = KEY_SELECT, // Enter
  133. [ 0x5a ] = KEY_INFO,
  134. [ 0x52 ] = KEY_EXIT,
  135. [ 0x59 ] = KEY_PREVIOUS,
  136. [ 0x51 ] = KEY_NEXT,
  137. [ 0x58 ] = KEY_REWIND,
  138. [ 0x50 ] = KEY_FORWARD,
  139. [ 0x44 ] = KEY_PLAYPAUSE,
  140. [ 0x07 ] = KEY_STOP,
  141. [ 0x1b ] = KEY_RECORD,
  142. [ 0x13 ] = KEY_TUNER, // Live
  143. [ 0x0a ] = KEY_A,
  144. [ 0x12 ] = KEY_B,
  145. [ 0x03 ] = KEY_PROG1, // 1
  146. [ 0x01 ] = KEY_PROG2, // 2
  147. [ 0x00 ] = KEY_PROG3, // 3
  148. [ 0x06 ] = KEY_DVD,
  149. [ 0x48 ] = KEY_AUX, // Photo
  150. [ 0x40 ] = KEY_VIDEO,
  151. [ 0x19 ] = KEY_AUDIO, // Music
  152. [ 0x0b ] = KEY_CHANNELUP,
  153. [ 0x08 ] = KEY_CHANNELDOWN,
  154. [ 0x15 ] = KEY_VOLUMEUP,
  155. [ 0x1c ] = KEY_VOLUMEDOWN,
  156. };
  157. /* ---------------------------------------------------------------------- */
  158. /* MSI TV@nywhere remote */
  159. static IR_KEYTAB_TYPE ir_codes_msi_tvanywhere[IR_KEYTAB_SIZE] = {
  160. [ 0x00 ] = KEY_0, /* '0' */
  161. [ 0x01 ] = KEY_1, /* '1' */
  162. [ 0x02 ] = KEY_2, /* '2' */
  163. [ 0x03 ] = KEY_3, /* '3' */
  164. [ 0x04 ] = KEY_4, /* '4' */
  165. [ 0x05 ] = KEY_5, /* '5' */
  166. [ 0x06 ] = KEY_6, /* '6' */
  167. [ 0x07 ] = KEY_7, /* '7' */
  168. [ 0x08 ] = KEY_8, /* '8' */
  169. [ 0x09 ] = KEY_9, /* '9' */
  170. [ 0x0c ] = KEY_MUTE, /* 'Mute' */
  171. [ 0x0f ] = KEY_SCREEN, /* 'Full Screen' */
  172. [ 0x10 ] = KEY_F, /* 'Funtion' */
  173. [ 0x11 ] = KEY_T, /* 'Time shift' */
  174. [ 0x12 ] = KEY_POWER, /* 'Power' */
  175. [ 0x13 ] = KEY_MEDIA, /* 'MTS' */
  176. [ 0x14 ] = KEY_SLOW, /* 'Slow' */
  177. [ 0x16 ] = KEY_REWIND, /* 'backward <<' */
  178. [ 0x17 ] = KEY_ENTER, /* 'Return' */
  179. [ 0x18 ] = KEY_FASTFORWARD, /* 'forward >>' */
  180. [ 0x1a ] = KEY_CHANNELUP, /* 'Channel+' */
  181. [ 0x1b ] = KEY_VOLUMEUP, /* 'Volume+' */
  182. [ 0x1e ] = KEY_CHANNELDOWN, /* 'Channel-' */
  183. [ 0x1f ] = KEY_VOLUMEDOWN, /* 'Volume-' */
  184. };
  185. /* ---------------------------------------------------------------------- */
  186. struct cx88_IR {
  187. struct cx88_core *core;
  188. struct input_dev input;
  189. struct ir_input_state ir;
  190. char name[32];
  191. char phys[32];
  192. /* sample from gpio pin 16 */
  193. int sampling;
  194. u32 samples[16];
  195. int scount;
  196. unsigned long release;
  197. /* poll external decoder */
  198. int polling;
  199. struct work_struct work;
  200. struct timer_list timer;
  201. u32 gpio_addr;
  202. u32 last_gpio;
  203. u32 mask_keycode;
  204. u32 mask_keydown;
  205. u32 mask_keyup;
  206. };
  207. static int ir_debug = 0;
  208. module_param(ir_debug, int, 0644); /* debug level [IR] */
  209. MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
  210. #define ir_dprintk(fmt, arg...) if (ir_debug) \
  211. printk(KERN_DEBUG "%s IR: " fmt , ir->core->name, ## arg)
  212. /* ---------------------------------------------------------------------- */
  213. static void cx88_ir_handle_key(struct cx88_IR *ir)
  214. {
  215. struct cx88_core *core = ir->core;
  216. u32 gpio, data;
  217. /* read gpio value */
  218. gpio = cx_read(ir->gpio_addr);
  219. if (ir->polling) {
  220. if (ir->last_gpio == gpio)
  221. return;
  222. ir->last_gpio = gpio;
  223. }
  224. /* extract data */
  225. data = ir_extract_bits(gpio, ir->mask_keycode);
  226. ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
  227. gpio, data,
  228. ir->polling ? "poll" : "irq",
  229. (gpio & ir->mask_keydown) ? " down" : "",
  230. (gpio & ir->mask_keyup) ? " up" : "");
  231. if (ir->mask_keydown) {
  232. /* bit set on keydown */
  233. if (gpio & ir->mask_keydown) {
  234. ir_input_keydown(&ir->input,&ir->ir,data,data);
  235. } else {
  236. ir_input_nokey(&ir->input,&ir->ir);
  237. }
  238. } else if (ir->mask_keyup) {
  239. /* bit cleared on keydown */
  240. if (0 == (gpio & ir->mask_keyup)) {
  241. ir_input_keydown(&ir->input,&ir->ir,data,data);
  242. } else {
  243. ir_input_nokey(&ir->input,&ir->ir);
  244. }
  245. } else {
  246. /* can't distinguish keydown/up :-/ */
  247. ir_input_keydown(&ir->input,&ir->ir,data,data);
  248. ir_input_nokey(&ir->input,&ir->ir);
  249. }
  250. }
  251. static void ir_timer(unsigned long data)
  252. {
  253. struct cx88_IR *ir = (struct cx88_IR*)data;
  254. schedule_work(&ir->work);
  255. }
  256. static void cx88_ir_work(void *data)
  257. {
  258. struct cx88_IR *ir = data;
  259. unsigned long timeout;
  260. cx88_ir_handle_key(ir);
  261. timeout = jiffies + (ir->polling * HZ / 1000);
  262. mod_timer(&ir->timer, timeout);
  263. }
  264. /* ---------------------------------------------------------------------- */
  265. int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
  266. {
  267. struct cx88_IR *ir;
  268. IR_KEYTAB_TYPE *ir_codes = NULL;
  269. int ir_type = IR_TYPE_OTHER;
  270. ir = kmalloc(sizeof(*ir),GFP_KERNEL);
  271. if (NULL == ir)
  272. return -ENOMEM;
  273. memset(ir,0,sizeof(*ir));
  274. /* detect & configure */
  275. switch (core->board) {
  276. case CX88_BOARD_DNTV_LIVE_DVB_T:
  277. case CX88_BOARD_KWORLD_DVB_T:
  278. ir_codes = ir_codes_dntv_live_dvb_t;
  279. ir->gpio_addr = MO_GP1_IO;
  280. ir->mask_keycode = 0x1f;
  281. ir->mask_keyup = 0x60;
  282. ir->polling = 50; // ms
  283. break;
  284. case CX88_BOARD_HAUPPAUGE:
  285. case CX88_BOARD_HAUPPAUGE_DVB_T1:
  286. ir_codes = ir_codes_hauppauge_new;
  287. ir_type = IR_TYPE_RC5;
  288. ir->sampling = 1;
  289. break;
  290. case CX88_BOARD_WINFAST2000XP_EXPERT:
  291. ir_codes = ir_codes_winfast;
  292. ir->gpio_addr = MO_GP0_IO;
  293. ir->mask_keycode = 0x8f8;
  294. ir->mask_keyup = 0x100;
  295. ir->polling = 1; // ms
  296. break;
  297. case CX88_BOARD_IODATA_GVBCTV7E:
  298. ir_codes = ir_codes_iodata_bctv7e;
  299. ir->gpio_addr = MO_GP0_IO;
  300. ir->mask_keycode = 0xfd;
  301. ir->mask_keydown = 0x02;
  302. ir->polling = 5; // ms
  303. break;
  304. case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
  305. ir_codes = ir_codes_pixelview;
  306. ir->gpio_addr = MO_GP1_IO;
  307. ir->mask_keycode = 0x1f;
  308. ir->mask_keyup = 0x80;
  309. ir->polling = 1; // ms
  310. break;
  311. case CX88_BOARD_ADSTECH_DVB_T_PCI:
  312. ir_codes = ir_codes_adstech_dvb_t_pci;
  313. ir->gpio_addr = MO_GP1_IO;
  314. ir->mask_keycode = 0xbf;
  315. ir->mask_keyup = 0x40;
  316. ir->polling = 50; // ms
  317. break;
  318. case CX88_BOARD_MSI_TVANYWHERE_MASTER:
  319. ir_codes = ir_codes_msi_tvanywhere;
  320. ir->gpio_addr = MO_GP1_IO;
  321. ir->mask_keycode = 0x1f;
  322. ir->mask_keyup = 0x40;
  323. ir->polling = 1;
  324. break;
  325. }
  326. if (NULL == ir_codes) {
  327. kfree(ir);
  328. return -ENODEV;
  329. }
  330. /* init input device */
  331. snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)",
  332. cx88_boards[core->board].name);
  333. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  334. pci_name(pci));
  335. ir_input_init(&ir->input, &ir->ir, ir_type, ir_codes);
  336. ir->input.name = ir->name;
  337. ir->input.phys = ir->phys;
  338. ir->input.id.bustype = BUS_PCI;
  339. ir->input.id.version = 1;
  340. if (pci->subsystem_vendor) {
  341. ir->input.id.vendor = pci->subsystem_vendor;
  342. ir->input.id.product = pci->subsystem_device;
  343. } else {
  344. ir->input.id.vendor = pci->vendor;
  345. ir->input.id.product = pci->device;
  346. }
  347. /* record handles to ourself */
  348. ir->core = core;
  349. core->ir = ir;
  350. if (ir->polling) {
  351. INIT_WORK(&ir->work, cx88_ir_work, ir);
  352. init_timer(&ir->timer);
  353. ir->timer.function = ir_timer;
  354. ir->timer.data = (unsigned long)ir;
  355. schedule_work(&ir->work);
  356. }
  357. if (ir->sampling) {
  358. core->pci_irqmask |= (1<<18); // IR_SMP_INT
  359. cx_write(MO_DDS_IO, 0xa80a80); // 4 kHz sample rate
  360. cx_write(MO_DDSCFG_IO, 0x5); // enable
  361. }
  362. /* all done */
  363. input_register_device(&ir->input);
  364. printk("%s: registered IR remote control\n", core->name);
  365. return 0;
  366. }
  367. int cx88_ir_fini(struct cx88_core *core)
  368. {
  369. struct cx88_IR *ir = core->ir;
  370. /* skip detach on non attached boards */
  371. if (NULL == ir)
  372. return 0;
  373. if (ir->polling) {
  374. del_timer(&ir->timer);
  375. flush_scheduled_work();
  376. }
  377. input_unregister_device(&ir->input);
  378. kfree(ir);
  379. /* done */
  380. core->ir = NULL;
  381. return 0;
  382. }
  383. /* ---------------------------------------------------------------------- */
  384. void cx88_ir_irq(struct cx88_core *core)
  385. {
  386. struct cx88_IR *ir = core->ir;
  387. u32 samples,rc5;
  388. int i;
  389. if (NULL == ir)
  390. return;
  391. if (!ir->sampling)
  392. return;
  393. samples = cx_read(MO_SAMPLE_IO);
  394. if (0 != samples && 0xffffffff != samples) {
  395. /* record sample data */
  396. if (ir->scount < ARRAY_SIZE(ir->samples))
  397. ir->samples[ir->scount++] = samples;
  398. return;
  399. }
  400. if (!ir->scount) {
  401. /* nothing to sample */
  402. if (ir->ir.keypressed && time_after(jiffies,ir->release))
  403. ir_input_nokey(&ir->input,&ir->ir);
  404. return;
  405. }
  406. /* have a complete sample */
  407. if (ir->scount < ARRAY_SIZE(ir->samples))
  408. ir->samples[ir->scount++] = samples;
  409. for (i = 0; i < ir->scount; i++)
  410. ir->samples[i] = ~ir->samples[i];
  411. if (ir_debug)
  412. ir_dump_samples(ir->samples,ir->scount);
  413. /* decode it */
  414. switch (core->board) {
  415. case CX88_BOARD_HAUPPAUGE:
  416. case CX88_BOARD_HAUPPAUGE_DVB_T1:
  417. rc5 = ir_decode_biphase(ir->samples,ir->scount,5,7);
  418. ir_dprintk("biphase decoded: %x\n",rc5);
  419. if ((rc5 & 0xfffff000) != 0x3000)
  420. break;
  421. ir_input_keydown(&ir->input, &ir->ir, rc5 & 0x3f, rc5);
  422. ir->release = jiffies + msecs_to_jiffies(120);
  423. break;
  424. }
  425. ir->scount = 0;
  426. return;
  427. }
  428. /* ---------------------------------------------------------------------- */
  429. MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
  430. MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
  431. MODULE_LICENSE("GPL");
  432. /*
  433. * Local variables:
  434. * c-basic-offset: 8
  435. * End:
  436. */