bttv-input.c 14 KB

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