cx88-input.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. *
  3. * Device driver for GPIO attached remote control interfaces
  4. * on Conexant 2388x based TV/DVB cards.
  5. *
  6. * Copyright (c) 2003 Pavel Machek
  7. * Copyright (c) 2004 Gerd Knorr
  8. * Copyright (c) 2004, 2005 Chris Pascoe
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/input.h>
  27. #include <linux/pci.h>
  28. #include <linux/module.h>
  29. #include "cx88.h"
  30. #include <media/ir-common.h>
  31. /* ---------------------------------------------------------------------- */
  32. struct cx88_IR {
  33. struct cx88_core *core;
  34. struct input_dev *input;
  35. struct ir_input_state ir;
  36. char name[32];
  37. char phys[32];
  38. /* sample from gpio pin 16 */
  39. u32 sampling;
  40. u32 samples[16];
  41. int scount;
  42. unsigned long release;
  43. /* poll external decoder */
  44. int polling;
  45. struct work_struct work;
  46. struct timer_list timer;
  47. u32 gpio_addr;
  48. u32 last_gpio;
  49. u32 mask_keycode;
  50. u32 mask_keydown;
  51. u32 mask_keyup;
  52. };
  53. static int ir_debug;
  54. module_param(ir_debug, int, 0644); /* debug level [IR] */
  55. MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
  56. #define ir_dprintk(fmt, arg...) if (ir_debug) \
  57. printk(KERN_DEBUG "%s IR: " fmt , ir->core->name , ##arg)
  58. /* ---------------------------------------------------------------------- */
  59. static void cx88_ir_handle_key(struct cx88_IR *ir)
  60. {
  61. struct cx88_core *core = ir->core;
  62. u32 gpio, data, auxgpio;
  63. /* read gpio value */
  64. gpio = cx_read(ir->gpio_addr);
  65. switch (core->boardnr) {
  66. case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
  67. /* This board apparently uses a combination of 2 GPIO
  68. to represent the keys. Additionally, the second GPIO
  69. can be used for parity.
  70. Example:
  71. for key "5"
  72. gpio = 0x758, auxgpio = 0xe5 or 0xf5
  73. for key "Power"
  74. gpio = 0x758, auxgpio = 0xed or 0xfd
  75. */
  76. auxgpio = cx_read(MO_GP1_IO);
  77. /* Take out the parity part */
  78. gpio=(gpio & 0x7fd) + (auxgpio & 0xef);
  79. break;
  80. case CX88_BOARD_WINFAST_DTV1000:
  81. gpio = (gpio & 0x6ff) | ((cx_read(MO_GP1_IO) << 8) & 0x900);
  82. auxgpio = gpio;
  83. break;
  84. default:
  85. auxgpio = gpio;
  86. }
  87. if (ir->polling) {
  88. if (ir->last_gpio == auxgpio)
  89. return;
  90. ir->last_gpio = auxgpio;
  91. }
  92. /* extract data */
  93. data = ir_extract_bits(gpio, ir->mask_keycode);
  94. ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
  95. gpio, data,
  96. ir->polling ? "poll" : "irq",
  97. (gpio & ir->mask_keydown) ? " down" : "",
  98. (gpio & ir->mask_keyup) ? " up" : "");
  99. if (ir->core->boardnr == CX88_BOARD_NORWOOD_MICRO) {
  100. u32 gpio_key = cx_read(MO_GP0_IO);
  101. data = (data << 4) | ((gpio_key & 0xf0) >> 4);
  102. ir_input_keydown(ir->input, &ir->ir, data, data);
  103. ir_input_nokey(ir->input, &ir->ir);
  104. } else if (ir->mask_keydown) {
  105. /* bit set on keydown */
  106. if (gpio & ir->mask_keydown) {
  107. ir_input_keydown(ir->input, &ir->ir, data, data);
  108. } else {
  109. ir_input_nokey(ir->input, &ir->ir);
  110. }
  111. } else if (ir->mask_keyup) {
  112. /* bit cleared on keydown */
  113. if (0 == (gpio & ir->mask_keyup)) {
  114. ir_input_keydown(ir->input, &ir->ir, data, data);
  115. } else {
  116. ir_input_nokey(ir->input, &ir->ir);
  117. }
  118. } else {
  119. /* can't distinguish keydown/up :-/ */
  120. ir_input_keydown(ir->input, &ir->ir, data, data);
  121. ir_input_nokey(ir->input, &ir->ir);
  122. }
  123. }
  124. static void ir_timer(unsigned long data)
  125. {
  126. struct cx88_IR *ir = (struct cx88_IR *)data;
  127. schedule_work(&ir->work);
  128. }
  129. static void cx88_ir_work(struct work_struct *work)
  130. {
  131. struct cx88_IR *ir = container_of(work, struct cx88_IR, work);
  132. cx88_ir_handle_key(ir);
  133. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
  134. }
  135. void cx88_ir_start(struct cx88_core *core, struct cx88_IR *ir)
  136. {
  137. if (ir->polling) {
  138. setup_timer(&ir->timer, ir_timer, (unsigned long)ir);
  139. INIT_WORK(&ir->work, cx88_ir_work);
  140. schedule_work(&ir->work);
  141. }
  142. if (ir->sampling) {
  143. core->pci_irqmask |= PCI_INT_IR_SMPINT;
  144. cx_write(MO_DDS_IO, 0xa80a80); /* 4 kHz sample rate */
  145. cx_write(MO_DDSCFG_IO, 0x5); /* enable */
  146. }
  147. }
  148. void cx88_ir_stop(struct cx88_core *core, struct cx88_IR *ir)
  149. {
  150. if (ir->sampling) {
  151. cx_write(MO_DDSCFG_IO, 0x0);
  152. core->pci_irqmask &= ~PCI_INT_IR_SMPINT;
  153. }
  154. if (ir->polling) {
  155. del_timer_sync(&ir->timer);
  156. flush_scheduled_work();
  157. }
  158. }
  159. /* ---------------------------------------------------------------------- */
  160. int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
  161. {
  162. struct cx88_IR *ir;
  163. struct input_dev *input_dev;
  164. IR_KEYTAB_TYPE *ir_codes = NULL;
  165. int ir_type = IR_TYPE_OTHER;
  166. int err = -ENOMEM;
  167. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  168. input_dev = input_allocate_device();
  169. if (!ir || !input_dev)
  170. goto err_out_free;
  171. ir->input = input_dev;
  172. /* detect & configure */
  173. switch (core->boardnr) {
  174. case CX88_BOARD_DNTV_LIVE_DVB_T:
  175. case CX88_BOARD_KWORLD_DVB_T:
  176. case CX88_BOARD_KWORLD_DVB_T_CX22702:
  177. ir_codes = ir_codes_dntv_live_dvb_t;
  178. ir->gpio_addr = MO_GP1_IO;
  179. ir->mask_keycode = 0x1f;
  180. ir->mask_keyup = 0x60;
  181. ir->polling = 50; /* ms */
  182. break;
  183. case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
  184. ir_codes = ir_codes_cinergy_1400;
  185. ir_type = IR_TYPE_PD;
  186. ir->sampling = 0xeb04; /* address */
  187. break;
  188. case CX88_BOARD_HAUPPAUGE:
  189. case CX88_BOARD_HAUPPAUGE_DVB_T1:
  190. case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
  191. case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
  192. case CX88_BOARD_HAUPPAUGE_HVR1100:
  193. case CX88_BOARD_HAUPPAUGE_HVR3000:
  194. ir_codes = ir_codes_hauppauge_new;
  195. ir_type = IR_TYPE_RC5;
  196. ir->sampling = 1;
  197. break;
  198. case CX88_BOARD_WINFAST_DTV2000H:
  199. ir_codes = ir_codes_winfast;
  200. ir->gpio_addr = MO_GP0_IO;
  201. ir->mask_keycode = 0x8f8;
  202. ir->mask_keyup = 0x100;
  203. ir->polling = 50; /* ms */
  204. break;
  205. case CX88_BOARD_WINFAST2000XP_EXPERT:
  206. case CX88_BOARD_WINFAST_DTV1000:
  207. ir_codes = ir_codes_winfast;
  208. ir->gpio_addr = MO_GP0_IO;
  209. ir->mask_keycode = 0x8f8;
  210. ir->mask_keyup = 0x100;
  211. ir->polling = 1; /* ms */
  212. break;
  213. case CX88_BOARD_IODATA_GVBCTV7E:
  214. ir_codes = ir_codes_iodata_bctv7e;
  215. ir->gpio_addr = MO_GP0_IO;
  216. ir->mask_keycode = 0xfd;
  217. ir->mask_keydown = 0x02;
  218. ir->polling = 5; /* ms */
  219. break;
  220. case CX88_BOARD_PROLINK_PLAYTVPVR:
  221. case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
  222. ir_codes = ir_codes_pixelview;
  223. ir->gpio_addr = MO_GP1_IO;
  224. ir->mask_keycode = 0x1f;
  225. ir->mask_keyup = 0x80;
  226. ir->polling = 1; /* ms */
  227. break;
  228. case CX88_BOARD_PROLINK_PV_8000GT:
  229. ir_codes = ir_codes_pixelview_new;
  230. ir->gpio_addr = MO_GP1_IO;
  231. ir->mask_keycode = 0x3f;
  232. ir->mask_keyup = 0x80;
  233. ir->polling = 1; /* ms */
  234. break;
  235. case CX88_BOARD_KWORLD_LTV883:
  236. ir_codes = ir_codes_pixelview;
  237. ir->gpio_addr = MO_GP1_IO;
  238. ir->mask_keycode = 0x1f;
  239. ir->mask_keyup = 0x60;
  240. ir->polling = 1; /* ms */
  241. break;
  242. case CX88_BOARD_ADSTECH_DVB_T_PCI:
  243. ir_codes = ir_codes_adstech_dvb_t_pci;
  244. ir->gpio_addr = MO_GP1_IO;
  245. ir->mask_keycode = 0xbf;
  246. ir->mask_keyup = 0x40;
  247. ir->polling = 50; /* ms */
  248. break;
  249. case CX88_BOARD_MSI_TVANYWHERE_MASTER:
  250. ir_codes = ir_codes_msi_tvanywhere;
  251. ir->gpio_addr = MO_GP1_IO;
  252. ir->mask_keycode = 0x1f;
  253. ir->mask_keyup = 0x40;
  254. ir->polling = 1; /* ms */
  255. break;
  256. case CX88_BOARD_AVERTV_303:
  257. case CX88_BOARD_AVERTV_STUDIO_303:
  258. ir_codes = ir_codes_avertv_303;
  259. ir->gpio_addr = MO_GP2_IO;
  260. ir->mask_keycode = 0xfb;
  261. ir->mask_keydown = 0x02;
  262. ir->polling = 50; /* ms */
  263. break;
  264. case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
  265. ir_codes = ir_codes_dntv_live_dvbt_pro;
  266. ir_type = IR_TYPE_PD;
  267. ir->sampling = 0xff00; /* address */
  268. break;
  269. case CX88_BOARD_NORWOOD_MICRO:
  270. ir_codes = ir_codes_norwood;
  271. ir->gpio_addr = MO_GP1_IO;
  272. ir->mask_keycode = 0x0e;
  273. ir->mask_keyup = 0x80;
  274. ir->polling = 50; /* ms */
  275. break;
  276. case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
  277. ir_codes = ir_codes_npgtech;
  278. ir->gpio_addr = MO_GP0_IO;
  279. ir->mask_keycode = 0xfa;
  280. ir->polling = 50; /* ms */
  281. break;
  282. case CX88_BOARD_PINNACLE_PCTV_HD_800i:
  283. ir_codes = ir_codes_pinnacle_pctv_hd;
  284. ir_type = IR_TYPE_RC5;
  285. ir->sampling = 1;
  286. break;
  287. case CX88_BOARD_POWERCOLOR_REAL_ANGEL:
  288. ir_codes = ir_codes_powercolor_real_angel;
  289. ir->gpio_addr = MO_GP2_IO;
  290. ir->mask_keycode = 0x7e;
  291. ir->polling = 100; /* ms */
  292. break;
  293. }
  294. if (NULL == ir_codes) {
  295. err = -ENODEV;
  296. goto err_out_free;
  297. }
  298. /* init input device */
  299. snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)", core->board.name);
  300. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
  301. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  302. input_dev->name = ir->name;
  303. input_dev->phys = ir->phys;
  304. input_dev->id.bustype = BUS_PCI;
  305. input_dev->id.version = 1;
  306. if (pci->subsystem_vendor) {
  307. input_dev->id.vendor = pci->subsystem_vendor;
  308. input_dev->id.product = pci->subsystem_device;
  309. } else {
  310. input_dev->id.vendor = pci->vendor;
  311. input_dev->id.product = pci->device;
  312. }
  313. input_dev->dev.parent = &pci->dev;
  314. /* record handles to ourself */
  315. ir->core = core;
  316. core->ir = ir;
  317. cx88_ir_start(core, ir);
  318. /* all done */
  319. err = input_register_device(ir->input);
  320. if (err)
  321. goto err_out_stop;
  322. return 0;
  323. err_out_stop:
  324. cx88_ir_stop(core, ir);
  325. core->ir = NULL;
  326. err_out_free:
  327. input_free_device(input_dev);
  328. kfree(ir);
  329. return err;
  330. }
  331. int cx88_ir_fini(struct cx88_core *core)
  332. {
  333. struct cx88_IR *ir = core->ir;
  334. /* skip detach on non attached boards */
  335. if (NULL == ir)
  336. return 0;
  337. cx88_ir_stop(core, ir);
  338. input_unregister_device(ir->input);
  339. kfree(ir);
  340. /* done */
  341. core->ir = NULL;
  342. return 0;
  343. }
  344. /* ---------------------------------------------------------------------- */
  345. void cx88_ir_irq(struct cx88_core *core)
  346. {
  347. struct cx88_IR *ir = core->ir;
  348. u32 samples, ircode;
  349. int i;
  350. if (NULL == ir)
  351. return;
  352. if (!ir->sampling)
  353. return;
  354. samples = cx_read(MO_SAMPLE_IO);
  355. if (0 != samples && 0xffffffff != samples) {
  356. /* record sample data */
  357. if (ir->scount < ARRAY_SIZE(ir->samples))
  358. ir->samples[ir->scount++] = samples;
  359. return;
  360. }
  361. if (!ir->scount) {
  362. /* nothing to sample */
  363. if (ir->ir.keypressed && time_after(jiffies, ir->release))
  364. ir_input_nokey(ir->input, &ir->ir);
  365. return;
  366. }
  367. /* have a complete sample */
  368. if (ir->scount < ARRAY_SIZE(ir->samples))
  369. ir->samples[ir->scount++] = samples;
  370. for (i = 0; i < ir->scount; i++)
  371. ir->samples[i] = ~ir->samples[i];
  372. if (ir_debug)
  373. ir_dump_samples(ir->samples, ir->scount);
  374. /* decode it */
  375. switch (core->boardnr) {
  376. case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
  377. case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
  378. ircode = ir_decode_pulsedistance(ir->samples, ir->scount, 1, 4);
  379. if (ircode == 0xffffffff) { /* decoding error */
  380. ir_dprintk("pulse distance decoding error\n");
  381. break;
  382. }
  383. ir_dprintk("pulse distance decoded: %x\n", ircode);
  384. if (ircode == 0) { /* key still pressed */
  385. ir_dprintk("pulse distance decoded repeat code\n");
  386. ir->release = jiffies + msecs_to_jiffies(120);
  387. break;
  388. }
  389. if ((ircode & 0xffff) != (ir->sampling & 0xffff)) { /* wrong address */
  390. ir_dprintk("pulse distance decoded wrong address\n");
  391. break;
  392. }
  393. if (((~ircode >> 24) & 0xff) != ((ircode >> 16) & 0xff)) { /* wrong checksum */
  394. ir_dprintk("pulse distance decoded wrong check sum\n");
  395. break;
  396. }
  397. ir_dprintk("Key Code: %x\n", (ircode >> 16) & 0x7f);
  398. ir_input_keydown(ir->input, &ir->ir, (ircode >> 16) & 0x7f, (ircode >> 16) & 0xff);
  399. ir->release = jiffies + msecs_to_jiffies(120);
  400. break;
  401. case CX88_BOARD_HAUPPAUGE:
  402. case CX88_BOARD_HAUPPAUGE_DVB_T1:
  403. case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
  404. case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
  405. case CX88_BOARD_HAUPPAUGE_HVR1100:
  406. case CX88_BOARD_HAUPPAUGE_HVR3000:
  407. case CX88_BOARD_PINNACLE_PCTV_HD_800i:
  408. ircode = ir_decode_biphase(ir->samples, ir->scount, 5, 7);
  409. ir_dprintk("biphase decoded: %x\n", ircode);
  410. if ((ircode & 0xfffff000) != 0x3000)
  411. break;
  412. ir_input_keydown(ir->input, &ir->ir, ircode & 0x3f, ircode);
  413. ir->release = jiffies + msecs_to_jiffies(120);
  414. break;
  415. }
  416. ir->scount = 0;
  417. return;
  418. }
  419. /* ---------------------------------------------------------------------- */
  420. MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
  421. MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
  422. MODULE_LICENSE("GPL");
  423. /*
  424. * Local variables:
  425. * c-basic-offset: 8
  426. * End:
  427. */