cx88-input.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. case CX88_BOARD_HAUPPAUGE_HVR4000:
  195. case CX88_BOARD_HAUPPAUGE_HVR4000LITE:
  196. ir_codes = ir_codes_hauppauge_new;
  197. ir_type = IR_TYPE_RC5;
  198. ir->sampling = 1;
  199. break;
  200. case CX88_BOARD_WINFAST_DTV2000H:
  201. ir_codes = ir_codes_winfast;
  202. ir->gpio_addr = MO_GP0_IO;
  203. ir->mask_keycode = 0x8f8;
  204. ir->mask_keyup = 0x100;
  205. ir->polling = 50; /* ms */
  206. break;
  207. case CX88_BOARD_WINFAST2000XP_EXPERT:
  208. case CX88_BOARD_WINFAST_DTV1000:
  209. ir_codes = ir_codes_winfast;
  210. ir->gpio_addr = MO_GP0_IO;
  211. ir->mask_keycode = 0x8f8;
  212. ir->mask_keyup = 0x100;
  213. ir->polling = 1; /* ms */
  214. break;
  215. case CX88_BOARD_IODATA_GVBCTV7E:
  216. ir_codes = ir_codes_iodata_bctv7e;
  217. ir->gpio_addr = MO_GP0_IO;
  218. ir->mask_keycode = 0xfd;
  219. ir->mask_keydown = 0x02;
  220. ir->polling = 5; /* ms */
  221. break;
  222. case CX88_BOARD_PROLINK_PLAYTVPVR:
  223. case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
  224. ir_codes = ir_codes_pixelview;
  225. ir->gpio_addr = MO_GP1_IO;
  226. ir->mask_keycode = 0x1f;
  227. ir->mask_keyup = 0x80;
  228. ir->polling = 1; /* ms */
  229. break;
  230. case CX88_BOARD_PROLINK_PV_8000GT:
  231. case CX88_BOARD_PROLINK_PV_GLOBAL_XTREME:
  232. ir_codes = ir_codes_pixelview_new;
  233. ir->gpio_addr = MO_GP1_IO;
  234. ir->mask_keycode = 0x3f;
  235. ir->mask_keyup = 0x80;
  236. ir->polling = 1; /* ms */
  237. break;
  238. case CX88_BOARD_KWORLD_LTV883:
  239. ir_codes = ir_codes_pixelview;
  240. ir->gpio_addr = MO_GP1_IO;
  241. ir->mask_keycode = 0x1f;
  242. ir->mask_keyup = 0x60;
  243. ir->polling = 1; /* ms */
  244. break;
  245. case CX88_BOARD_ADSTECH_DVB_T_PCI:
  246. ir_codes = ir_codes_adstech_dvb_t_pci;
  247. ir->gpio_addr = MO_GP1_IO;
  248. ir->mask_keycode = 0xbf;
  249. ir->mask_keyup = 0x40;
  250. ir->polling = 50; /* ms */
  251. break;
  252. case CX88_BOARD_MSI_TVANYWHERE_MASTER:
  253. ir_codes = ir_codes_msi_tvanywhere;
  254. ir->gpio_addr = MO_GP1_IO;
  255. ir->mask_keycode = 0x1f;
  256. ir->mask_keyup = 0x40;
  257. ir->polling = 1; /* ms */
  258. break;
  259. case CX88_BOARD_AVERTV_303:
  260. case CX88_BOARD_AVERTV_STUDIO_303:
  261. ir_codes = ir_codes_avertv_303;
  262. ir->gpio_addr = MO_GP2_IO;
  263. ir->mask_keycode = 0xfb;
  264. ir->mask_keydown = 0x02;
  265. ir->polling = 50; /* ms */
  266. break;
  267. case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
  268. ir_codes = ir_codes_dntv_live_dvbt_pro;
  269. ir_type = IR_TYPE_PD;
  270. ir->sampling = 0xff00; /* address */
  271. break;
  272. case CX88_BOARD_NORWOOD_MICRO:
  273. ir_codes = ir_codes_norwood;
  274. ir->gpio_addr = MO_GP1_IO;
  275. ir->mask_keycode = 0x0e;
  276. ir->mask_keyup = 0x80;
  277. ir->polling = 50; /* ms */
  278. break;
  279. case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
  280. ir_codes = ir_codes_npgtech;
  281. ir->gpio_addr = MO_GP0_IO;
  282. ir->mask_keycode = 0xfa;
  283. ir->polling = 50; /* ms */
  284. break;
  285. case CX88_BOARD_PINNACLE_PCTV_HD_800i:
  286. ir_codes = ir_codes_pinnacle_pctv_hd;
  287. ir_type = IR_TYPE_RC5;
  288. ir->sampling = 1;
  289. break;
  290. case CX88_BOARD_POWERCOLOR_REAL_ANGEL:
  291. ir_codes = ir_codes_powercolor_real_angel;
  292. ir->gpio_addr = MO_GP2_IO;
  293. ir->mask_keycode = 0x7e;
  294. ir->polling = 100; /* ms */
  295. break;
  296. }
  297. if (NULL == ir_codes) {
  298. err = -ENODEV;
  299. goto err_out_free;
  300. }
  301. /* init input device */
  302. snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)", core->board.name);
  303. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
  304. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  305. input_dev->name = ir->name;
  306. input_dev->phys = ir->phys;
  307. input_dev->id.bustype = BUS_PCI;
  308. input_dev->id.version = 1;
  309. if (pci->subsystem_vendor) {
  310. input_dev->id.vendor = pci->subsystem_vendor;
  311. input_dev->id.product = pci->subsystem_device;
  312. } else {
  313. input_dev->id.vendor = pci->vendor;
  314. input_dev->id.product = pci->device;
  315. }
  316. input_dev->dev.parent = &pci->dev;
  317. /* record handles to ourself */
  318. ir->core = core;
  319. core->ir = ir;
  320. cx88_ir_start(core, ir);
  321. /* all done */
  322. err = input_register_device(ir->input);
  323. if (err)
  324. goto err_out_stop;
  325. return 0;
  326. err_out_stop:
  327. cx88_ir_stop(core, ir);
  328. core->ir = NULL;
  329. err_out_free:
  330. input_free_device(input_dev);
  331. kfree(ir);
  332. return err;
  333. }
  334. int cx88_ir_fini(struct cx88_core *core)
  335. {
  336. struct cx88_IR *ir = core->ir;
  337. /* skip detach on non attached boards */
  338. if (NULL == ir)
  339. return 0;
  340. cx88_ir_stop(core, ir);
  341. input_unregister_device(ir->input);
  342. kfree(ir);
  343. /* done */
  344. core->ir = NULL;
  345. return 0;
  346. }
  347. /* ---------------------------------------------------------------------- */
  348. void cx88_ir_irq(struct cx88_core *core)
  349. {
  350. struct cx88_IR *ir = core->ir;
  351. u32 samples, ircode;
  352. int i, start, range, toggle, dev, code;
  353. if (NULL == ir)
  354. return;
  355. if (!ir->sampling)
  356. return;
  357. samples = cx_read(MO_SAMPLE_IO);
  358. if (0 != samples && 0xffffffff != samples) {
  359. /* record sample data */
  360. if (ir->scount < ARRAY_SIZE(ir->samples))
  361. ir->samples[ir->scount++] = samples;
  362. return;
  363. }
  364. if (!ir->scount) {
  365. /* nothing to sample */
  366. if (ir->ir.keypressed && time_after(jiffies, ir->release))
  367. ir_input_nokey(ir->input, &ir->ir);
  368. return;
  369. }
  370. /* have a complete sample */
  371. if (ir->scount < ARRAY_SIZE(ir->samples))
  372. ir->samples[ir->scount++] = samples;
  373. for (i = 0; i < ir->scount; i++)
  374. ir->samples[i] = ~ir->samples[i];
  375. if (ir_debug)
  376. ir_dump_samples(ir->samples, ir->scount);
  377. /* decode it */
  378. switch (core->boardnr) {
  379. case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
  380. case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
  381. ircode = ir_decode_pulsedistance(ir->samples, ir->scount, 1, 4);
  382. if (ircode == 0xffffffff) { /* decoding error */
  383. ir_dprintk("pulse distance decoding error\n");
  384. break;
  385. }
  386. ir_dprintk("pulse distance decoded: %x\n", ircode);
  387. if (ircode == 0) { /* key still pressed */
  388. ir_dprintk("pulse distance decoded repeat code\n");
  389. ir->release = jiffies + msecs_to_jiffies(120);
  390. break;
  391. }
  392. if ((ircode & 0xffff) != (ir->sampling & 0xffff)) { /* wrong address */
  393. ir_dprintk("pulse distance decoded wrong address\n");
  394. break;
  395. }
  396. if (((~ircode >> 24) & 0xff) != ((ircode >> 16) & 0xff)) { /* wrong checksum */
  397. ir_dprintk("pulse distance decoded wrong check sum\n");
  398. break;
  399. }
  400. ir_dprintk("Key Code: %x\n", (ircode >> 16) & 0x7f);
  401. ir_input_keydown(ir->input, &ir->ir, (ircode >> 16) & 0x7f, (ircode >> 16) & 0xff);
  402. ir->release = jiffies + msecs_to_jiffies(120);
  403. break;
  404. case CX88_BOARD_HAUPPAUGE:
  405. case CX88_BOARD_HAUPPAUGE_DVB_T1:
  406. case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
  407. case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
  408. case CX88_BOARD_HAUPPAUGE_HVR1100:
  409. case CX88_BOARD_HAUPPAUGE_HVR3000:
  410. case CX88_BOARD_HAUPPAUGE_HVR4000:
  411. case CX88_BOARD_HAUPPAUGE_HVR4000LITE:
  412. ircode = ir_decode_biphase(ir->samples, ir->scount, 5, 7);
  413. ir_dprintk("biphase decoded: %x\n", ircode);
  414. /*
  415. * RC5 has an extension bit which adds a new range
  416. * of available codes, this is detected here. Also
  417. * hauppauge remotes (black/silver) always use
  418. * specific device ids. If we do not filter the
  419. * device ids then messages destined for devices
  420. * such as TVs (id=0) will get through to the
  421. * device causing mis-fired events.
  422. */
  423. /* split rc5 data block ... */
  424. start = (ircode & 0x2000) >> 13;
  425. range = (ircode & 0x1000) >> 12;
  426. toggle= (ircode & 0x0800) >> 11;
  427. dev = (ircode & 0x07c0) >> 6;
  428. code = (ircode & 0x003f) | ((range << 6) ^ 0x0040);
  429. if( start != 1)
  430. /* no key pressed */
  431. break;
  432. if ( dev != 0x1e && dev != 0x1f )
  433. /* not a hauppauge remote */
  434. break;
  435. ir_input_keydown(ir->input, &ir->ir, code, ircode);
  436. ir->release = jiffies + msecs_to_jiffies(120);
  437. break;
  438. case CX88_BOARD_PINNACLE_PCTV_HD_800i:
  439. ircode = ir_decode_biphase(ir->samples, ir->scount, 5, 7);
  440. ir_dprintk("biphase decoded: %x\n", ircode);
  441. if ((ircode & 0xfffff000) != 0x3000)
  442. break;
  443. ir_input_keydown(ir->input, &ir->ir, ircode & 0x3f, ircode);
  444. ir->release = jiffies + msecs_to_jiffies(120);
  445. break;
  446. }
  447. ir->scount = 0;
  448. return;
  449. }
  450. /* ---------------------------------------------------------------------- */
  451. MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
  452. MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
  453. MODULE_LICENSE("GPL");
  454. /*
  455. * Local variables:
  456. * c-basic-offset: 8
  457. * End:
  458. */