bttv-input.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. *
  3. * Copyright (c) 2003 Gerd Knorr
  4. * Copyright (c) 2003 Pavel Machek
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/input.h>
  25. #include <linux/slab.h>
  26. #include "bttv.h"
  27. #include "bttvp.h"
  28. static int ir_debug;
  29. module_param(ir_debug, int, 0644);
  30. static int ir_rc5_remote_gap = 885;
  31. module_param(ir_rc5_remote_gap, int, 0644);
  32. #undef dprintk
  33. #define dprintk(arg...) do { \
  34. if (ir_debug >= 1) \
  35. printk(arg); \
  36. } while (0)
  37. #define DEVNAME "bttv-input"
  38. #define MODULE_NAME "bttv"
  39. /* ---------------------------------------------------------------------- */
  40. static void ir_handle_key(struct bttv *btv)
  41. {
  42. struct card_ir *ir = btv->remote;
  43. u32 gpio,data;
  44. /* read gpio value */
  45. gpio = bttv_gpio_read(&btv->c);
  46. if (ir->polling) {
  47. if (ir->last_gpio == gpio)
  48. return;
  49. ir->last_gpio = gpio;
  50. }
  51. /* extract data */
  52. data = ir_extract_bits(gpio, ir->mask_keycode);
  53. dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
  54. gpio, data,
  55. ir->polling ? "poll" : "irq",
  56. (gpio & ir->mask_keydown) ? " down" : "",
  57. (gpio & ir->mask_keyup) ? " up" : "");
  58. if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||
  59. (ir->mask_keyup && !(gpio & ir->mask_keyup))) {
  60. ir_keydown_notimeout(ir->dev, data, 0);
  61. } else {
  62. /* HACK: Probably, ir->mask_keydown is missing
  63. for this board */
  64. if (btv->c.type == BTTV_BOARD_WINFAST2000)
  65. ir_keydown_notimeout(ir->dev, data, 0);
  66. ir_keyup(ir->dev);
  67. }
  68. }
  69. static void ir_enltv_handle_key(struct bttv *btv)
  70. {
  71. struct card_ir *ir = btv->remote;
  72. u32 gpio, data, keyup;
  73. /* read gpio value */
  74. gpio = bttv_gpio_read(&btv->c);
  75. /* extract data */
  76. data = ir_extract_bits(gpio, ir->mask_keycode);
  77. /* Check if it is keyup */
  78. keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;
  79. if ((ir->last_gpio & 0x7f) != data) {
  80. dprintk(KERN_INFO DEVNAME ": gpio=0x%x code=%d | %s\n",
  81. gpio, data,
  82. (gpio & ir->mask_keyup) ? " up" : "up/down");
  83. ir_keydown_notimeout(ir->dev, data, 0);
  84. if (keyup)
  85. ir_keyup(ir->dev);
  86. } else {
  87. if ((ir->last_gpio & 1 << 31) == keyup)
  88. return;
  89. dprintk(KERN_INFO DEVNAME ":(cnt) gpio=0x%x code=%d | %s\n",
  90. gpio, data,
  91. (gpio & ir->mask_keyup) ? " up" : "down");
  92. if (keyup)
  93. ir_keyup(ir->dev);
  94. else
  95. ir_keydown_notimeout(ir->dev, data, 0);
  96. }
  97. ir->last_gpio = data | keyup;
  98. }
  99. void bttv_input_irq(struct bttv *btv)
  100. {
  101. struct card_ir *ir = btv->remote;
  102. if (!ir->polling)
  103. ir_handle_key(btv);
  104. }
  105. static void bttv_input_timer(unsigned long data)
  106. {
  107. struct bttv *btv = (struct bttv*)data;
  108. struct card_ir *ir = btv->remote;
  109. if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
  110. ir_enltv_handle_key(btv);
  111. else
  112. ir_handle_key(btv);
  113. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
  114. }
  115. /*
  116. * FIXME: Nebula digi uses the legacy way to decode RC5, instead of relying
  117. * on the rc-core way. As we need to be sure that both IRQ transitions are
  118. * properly triggered, Better to touch it only with this hardware for
  119. * testing.
  120. */
  121. #define RC5_START(x) (((x) >> 12) & 3)
  122. #define RC5_TOGGLE(x) (((x) >> 11) & 1)
  123. #define RC5_ADDR(x) (((x) >> 6) & 31)
  124. #define RC5_INSTR(x) ((x) & 63)
  125. /* decode raw bit pattern to RC5 code */
  126. static u32 bttv_rc5_decode(unsigned int code)
  127. {
  128. unsigned int org_code = code;
  129. unsigned int pair;
  130. unsigned int rc5 = 0;
  131. int i;
  132. for (i = 0; i < 14; ++i) {
  133. pair = code & 0x3;
  134. code >>= 2;
  135. rc5 <<= 1;
  136. switch (pair) {
  137. case 0:
  138. case 2:
  139. break;
  140. case 1:
  141. rc5 |= 1;
  142. break;
  143. case 3:
  144. dprintk(KERN_INFO DEVNAME ":rc5_decode(%x) bad code\n",
  145. org_code);
  146. return 0;
  147. }
  148. }
  149. dprintk(KERN_INFO DEVNAME ":"
  150. "code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
  151. "instr=%x\n", rc5, org_code, RC5_START(rc5),
  152. RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
  153. return rc5;
  154. }
  155. void bttv_rc5_timer_end(unsigned long data)
  156. {
  157. struct card_ir *ir = (struct card_ir *)data;
  158. struct timeval tv;
  159. unsigned long current_jiffies;
  160. u32 gap;
  161. u32 rc5 = 0;
  162. /* get time */
  163. current_jiffies = jiffies;
  164. do_gettimeofday(&tv);
  165. /* avoid overflow with gap >1s */
  166. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  167. gap = 200000;
  168. } else {
  169. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  170. tv.tv_usec - ir->base_time.tv_usec;
  171. }
  172. /* signal we're ready to start a new code */
  173. ir->active = 0;
  174. /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
  175. if (gap < 28000) {
  176. dprintk(KERN_INFO DEVNAME ": spurious timer_end\n");
  177. return;
  178. }
  179. if (ir->last_bit < 20) {
  180. /* ignore spurious codes (caused by light/other remotes) */
  181. dprintk(KERN_INFO DEVNAME ": short code: %x\n", ir->code);
  182. } else {
  183. ir->code = (ir->code << ir->shift_by) | 1;
  184. rc5 = bttv_rc5_decode(ir->code);
  185. /* two start bits? */
  186. if (RC5_START(rc5) != ir->start) {
  187. printk(KERN_INFO DEVNAME ":"
  188. " rc5 start bits invalid: %u\n", RC5_START(rc5));
  189. /* right address? */
  190. } else if (RC5_ADDR(rc5) == ir->addr) {
  191. u32 toggle = RC5_TOGGLE(rc5);
  192. u32 instr = RC5_INSTR(rc5);
  193. /* Good code */
  194. ir_keydown(ir->dev, instr, toggle);
  195. dprintk(KERN_INFO DEVNAME ":"
  196. " instruction %x, toggle %x\n",
  197. instr, toggle);
  198. }
  199. }
  200. }
  201. static int bttv_rc5_irq(struct bttv *btv)
  202. {
  203. struct card_ir *ir = btv->remote;
  204. struct timeval tv;
  205. u32 gpio;
  206. u32 gap;
  207. unsigned long current_jiffies;
  208. /* read gpio port */
  209. gpio = bttv_gpio_read(&btv->c);
  210. /* remote IRQ? */
  211. if (!(gpio & 0x20))
  212. return 0;
  213. /* get time of bit */
  214. current_jiffies = jiffies;
  215. do_gettimeofday(&tv);
  216. /* avoid overflow with gap >1s */
  217. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  218. gap = 200000;
  219. } else {
  220. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  221. tv.tv_usec - ir->base_time.tv_usec;
  222. }
  223. /* active code => add bit */
  224. if (ir->active) {
  225. /* only if in the code (otherwise spurious IRQ or timer
  226. late) */
  227. if (ir->last_bit < 28) {
  228. ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
  229. ir_rc5_remote_gap;
  230. ir->code |= 1 << ir->last_bit;
  231. }
  232. /* starting new code */
  233. } else {
  234. ir->active = 1;
  235. ir->code = 0;
  236. ir->base_time = tv;
  237. ir->last_bit = 0;
  238. mod_timer(&ir->timer_end,
  239. current_jiffies + msecs_to_jiffies(30));
  240. }
  241. /* toggle GPIO pin 4 to reset the irq */
  242. bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
  243. bttv_gpio_write(&btv->c, gpio | (1 << 4));
  244. return 1;
  245. }
  246. /* ---------------------------------------------------------------------- */
  247. static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
  248. {
  249. if (ir->polling) {
  250. setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
  251. ir->timer.expires = jiffies + msecs_to_jiffies(1000);
  252. add_timer(&ir->timer);
  253. } else if (ir->rc5_gpio) {
  254. /* set timer_end for code completion */
  255. init_timer(&ir->timer_end);
  256. ir->timer_end.function = bttv_rc5_timer_end;
  257. ir->timer_end.data = (unsigned long)ir;
  258. ir->shift_by = 1;
  259. ir->start = 3;
  260. ir->addr = 0x0;
  261. ir->rc5_remote_gap = ir_rc5_remote_gap;
  262. }
  263. }
  264. static void bttv_ir_stop(struct bttv *btv)
  265. {
  266. if (btv->remote->polling) {
  267. del_timer_sync(&btv->remote->timer);
  268. flush_scheduled_work();
  269. }
  270. if (btv->remote->rc5_gpio) {
  271. u32 gpio;
  272. del_timer_sync(&btv->remote->timer_end);
  273. flush_scheduled_work();
  274. gpio = bttv_gpio_read(&btv->c);
  275. bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
  276. }
  277. }
  278. /*
  279. * Get_key functions used by I2C remotes
  280. */
  281. static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  282. {
  283. unsigned char b;
  284. /* poll IR chip */
  285. if (1 != i2c_master_recv(ir->c, &b, 1)) {
  286. dprintk(KERN_INFO DEVNAME ": read error\n");
  287. return -EIO;
  288. }
  289. /* ignore 0xaa */
  290. if (b==0xaa)
  291. return 0;
  292. dprintk(KERN_INFO DEVNAME ": key %02x\n", b);
  293. *ir_key = b;
  294. *ir_raw = b;
  295. return 1;
  296. }
  297. /* Instantiate the I2C IR receiver device, if present */
  298. void __devinit init_bttv_i2c_ir(struct bttv *btv)
  299. {
  300. const unsigned short addr_list[] = {
  301. 0x1a, 0x18, 0x64, 0x30, 0x71,
  302. I2C_CLIENT_END
  303. };
  304. struct i2c_board_info info;
  305. if (0 != btv->i2c_rc)
  306. return;
  307. memset(&info, 0, sizeof(struct i2c_board_info));
  308. memset(&btv->init_data, 0, sizeof(btv->init_data));
  309. strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
  310. switch (btv->c.type) {
  311. case BTTV_BOARD_PV951:
  312. btv->init_data.name = "PV951";
  313. btv->init_data.get_key = get_key_pv951;
  314. btv->init_data.ir_codes = RC_MAP_PV951;
  315. info.addr = 0x4b;
  316. break;
  317. default:
  318. /*
  319. * The external IR receiver is at i2c address 0x34 (0x35 for
  320. * reads). Future Hauppauge cards will have an internal
  321. * receiver at 0x30 (0x31 for reads). In theory, both can be
  322. * fitted, and Hauppauge suggest an external overrides an
  323. * internal.
  324. * That's why we probe 0x1a (~0x34) first. CB
  325. */
  326. i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
  327. return;
  328. }
  329. if (btv->init_data.name)
  330. info.platform_data = &btv->init_data;
  331. i2c_new_device(&btv->c.i2c_adap, &info);
  332. return;
  333. }
  334. int __devexit fini_bttv_i2c(struct bttv *btv)
  335. {
  336. if (0 != btv->i2c_rc)
  337. return 0;
  338. return i2c_del_adapter(&btv->c.i2c_adap);
  339. }
  340. int bttv_input_init(struct bttv *btv)
  341. {
  342. struct card_ir *ir;
  343. char *ir_codes = NULL;
  344. struct rc_dev *rc;
  345. int err = -ENOMEM;
  346. if (!btv->has_remote)
  347. return -ENODEV;
  348. ir = kzalloc(sizeof(*ir),GFP_KERNEL);
  349. rc = rc_allocate_device();
  350. if (!ir || !rc)
  351. goto err_out_free;
  352. /* detect & configure */
  353. switch (btv->c.type) {
  354. case BTTV_BOARD_AVERMEDIA:
  355. case BTTV_BOARD_AVPHONE98:
  356. case BTTV_BOARD_AVERMEDIA98:
  357. ir_codes = RC_MAP_AVERMEDIA;
  358. ir->mask_keycode = 0xf88000;
  359. ir->mask_keydown = 0x010000;
  360. ir->polling = 50; // ms
  361. break;
  362. case BTTV_BOARD_AVDVBT_761:
  363. case BTTV_BOARD_AVDVBT_771:
  364. ir_codes = RC_MAP_AVERMEDIA_DVBT;
  365. ir->mask_keycode = 0x0f00c0;
  366. ir->mask_keydown = 0x000020;
  367. ir->polling = 50; // ms
  368. break;
  369. case BTTV_BOARD_PXELVWPLTVPAK:
  370. ir_codes = RC_MAP_PIXELVIEW;
  371. ir->mask_keycode = 0x003e00;
  372. ir->mask_keyup = 0x010000;
  373. ir->polling = 50; // ms
  374. break;
  375. case BTTV_BOARD_PV_M4900:
  376. case BTTV_BOARD_PV_BT878P_9B:
  377. case BTTV_BOARD_PV_BT878P_PLUS:
  378. ir_codes = RC_MAP_PIXELVIEW;
  379. ir->mask_keycode = 0x001f00;
  380. ir->mask_keyup = 0x008000;
  381. ir->polling = 50; // ms
  382. break;
  383. case BTTV_BOARD_WINFAST2000:
  384. ir_codes = RC_MAP_WINFAST;
  385. ir->mask_keycode = 0x1f8;
  386. break;
  387. case BTTV_BOARD_MAGICTVIEW061:
  388. case BTTV_BOARD_MAGICTVIEW063:
  389. ir_codes = RC_MAP_WINFAST;
  390. ir->mask_keycode = 0x0008e000;
  391. ir->mask_keydown = 0x00200000;
  392. break;
  393. case BTTV_BOARD_APAC_VIEWCOMP:
  394. ir_codes = RC_MAP_APAC_VIEWCOMP;
  395. ir->mask_keycode = 0x001f00;
  396. ir->mask_keyup = 0x008000;
  397. ir->polling = 50; // ms
  398. break;
  399. case BTTV_BOARD_ASKEY_CPH03X:
  400. case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
  401. case BTTV_BOARD_CONTVFMI:
  402. ir_codes = RC_MAP_PIXELVIEW;
  403. ir->mask_keycode = 0x001F00;
  404. ir->mask_keyup = 0x006000;
  405. ir->polling = 50; // ms
  406. break;
  407. case BTTV_BOARD_NEBULA_DIGITV:
  408. ir_codes = RC_MAP_NEBULA;
  409. btv->custom_irq = bttv_rc5_irq;
  410. ir->rc5_gpio = 1;
  411. break;
  412. case BTTV_BOARD_MACHTV_MAGICTV:
  413. ir_codes = RC_MAP_APAC_VIEWCOMP;
  414. ir->mask_keycode = 0x001F00;
  415. ir->mask_keyup = 0x004000;
  416. ir->polling = 50; /* ms */
  417. break;
  418. case BTTV_BOARD_KOZUMI_KTV_01C:
  419. ir_codes = RC_MAP_PCTV_SEDNA;
  420. ir->mask_keycode = 0x001f00;
  421. ir->mask_keyup = 0x006000;
  422. ir->polling = 50; /* ms */
  423. break;
  424. case BTTV_BOARD_ENLTV_FM_2:
  425. ir_codes = RC_MAP_ENCORE_ENLTV2;
  426. ir->mask_keycode = 0x00fd00;
  427. ir->mask_keyup = 0x000080;
  428. ir->polling = 1; /* ms */
  429. ir->last_gpio = ir_extract_bits(bttv_gpio_read(&btv->c),
  430. ir->mask_keycode);
  431. break;
  432. }
  433. if (NULL == ir_codes) {
  434. dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
  435. err = -ENODEV;
  436. goto err_out_free;
  437. }
  438. if (ir->rc5_gpio) {
  439. u32 gpio;
  440. /* enable remote irq */
  441. bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
  442. gpio = bttv_gpio_read(&btv->c);
  443. bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
  444. bttv_gpio_write(&btv->c, gpio | (1 << 4));
  445. } else {
  446. /* init hardware-specific stuff */
  447. bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
  448. }
  449. /* init input device */
  450. ir->dev = rc;
  451. snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
  452. btv->c.type);
  453. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  454. pci_name(btv->c.pci));
  455. rc->input_name = ir->name;
  456. rc->input_phys = ir->phys;
  457. rc->input_id.bustype = BUS_PCI;
  458. rc->input_id.version = 1;
  459. if (btv->c.pci->subsystem_vendor) {
  460. rc->input_id.vendor = btv->c.pci->subsystem_vendor;
  461. rc->input_id.product = btv->c.pci->subsystem_device;
  462. } else {
  463. rc->input_id.vendor = btv->c.pci->vendor;
  464. rc->input_id.product = btv->c.pci->device;
  465. }
  466. rc->dev.parent = &btv->c.pci->dev;
  467. rc->map_name = ir_codes;
  468. rc->driver_name = MODULE_NAME;
  469. btv->remote = ir;
  470. bttv_ir_start(btv, ir);
  471. /* all done */
  472. err = rc_register_device(rc);
  473. if (err)
  474. goto err_out_stop;
  475. return 0;
  476. err_out_stop:
  477. bttv_ir_stop(btv);
  478. btv->remote = NULL;
  479. err_out_free:
  480. rc_free_device(rc);
  481. kfree(ir);
  482. return err;
  483. }
  484. void bttv_input_fini(struct bttv *btv)
  485. {
  486. if (btv->remote == NULL)
  487. return;
  488. bttv_ir_stop(btv);
  489. rc_unregister_device(btv->remote->dev);
  490. kfree(btv->remote);
  491. btv->remote = NULL;
  492. }