cx88-input.c 12 KB

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