bttv-input.c 14 KB

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