em28xx-input.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. handle em28xx IR remotes via linux kernel input layer.
  3. Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  4. Markus Rechberger <mrechberger@gmail.com>
  5. Mauro Carvalho Chehab <mchehab@infradead.org>
  6. Sascha Sommer <saschasommer@freenet.de>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/input.h>
  24. #include <linux/usb.h>
  25. #include "em28xx.h"
  26. #define EM28XX_SNAPSHOT_KEY KEY_CAMERA
  27. #define EM28XX_SBUTTON_QUERY_INTERVAL 500
  28. #define EM28XX_R0C_USBSUSP_SNAPSHOT 0x20
  29. static unsigned int ir_debug;
  30. module_param(ir_debug, int, 0644);
  31. MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
  32. #define i2cdprintk(fmt, arg...) \
  33. if (ir_debug) { \
  34. printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
  35. }
  36. #define dprintk(fmt, arg...) \
  37. if (ir_debug) { \
  38. printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
  39. }
  40. /**********************************************************
  41. Polling structure used by em28xx IR's
  42. **********************************************************/
  43. struct em28xx_ir_poll_result {
  44. unsigned int toggle_bit:1;
  45. unsigned int read_count:7;
  46. u8 rc_address;
  47. u8 rc_data[4]; /* 1 byte on em2860/2880, 4 on em2874 */
  48. };
  49. struct em28xx_IR {
  50. struct em28xx *dev;
  51. struct input_dev *input;
  52. struct ir_input_state ir;
  53. char name[32];
  54. char phys[32];
  55. /* poll external decoder */
  56. int polling;
  57. struct delayed_work work;
  58. unsigned int last_toggle:1;
  59. unsigned int full_code:1;
  60. unsigned int last_readcount;
  61. unsigned int repeat_interval;
  62. int (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *);
  63. /* IR device properties */
  64. struct ir_dev_props props;
  65. };
  66. /**********************************************************
  67. I2C IR based get keycodes - should be used with ir-kbd-i2c
  68. **********************************************************/
  69. int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  70. {
  71. unsigned char b;
  72. /* poll IR chip */
  73. if (1 != i2c_master_recv(ir->c, &b, 1)) {
  74. i2cdprintk("read error\n");
  75. return -EIO;
  76. }
  77. /* it seems that 0xFE indicates that a button is still hold
  78. down, while 0xff indicates that no button is hold
  79. down. 0xfe sequences are sometimes interrupted by 0xFF */
  80. i2cdprintk("key %02x\n", b);
  81. if (b == 0xff)
  82. return 0;
  83. if (b == 0xfe)
  84. /* keep old data */
  85. return 1;
  86. *ir_key = b;
  87. *ir_raw = b;
  88. return 1;
  89. }
  90. int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  91. {
  92. unsigned char buf[2];
  93. u16 code;
  94. int size;
  95. /* poll IR chip */
  96. size = i2c_master_recv(ir->c, buf, sizeof(buf));
  97. if (size != 2)
  98. return -EIO;
  99. /* Does eliminate repeated parity code */
  100. if (buf[1] == 0xff)
  101. return 0;
  102. ir->old = buf[1];
  103. /*
  104. * Rearranges bits to the right order.
  105. * The bit order were determined experimentally by using
  106. * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
  107. * The RC5 code has 14 bits, but we've experimentally determined
  108. * the meaning for only 11 bits.
  109. * So, the code translation is not complete. Yet, it is enough to
  110. * work with the provided RC5 IR.
  111. */
  112. code =
  113. ((buf[0] & 0x01) ? 0x0020 : 0) | /* 0010 0000 */
  114. ((buf[0] & 0x02) ? 0x0010 : 0) | /* 0001 0000 */
  115. ((buf[0] & 0x04) ? 0x0008 : 0) | /* 0000 1000 */
  116. ((buf[0] & 0x08) ? 0x0004 : 0) | /* 0000 0100 */
  117. ((buf[0] & 0x10) ? 0x0002 : 0) | /* 0000 0010 */
  118. ((buf[0] & 0x20) ? 0x0001 : 0) | /* 0000 0001 */
  119. ((buf[1] & 0x08) ? 0x1000 : 0) | /* 0001 0000 */
  120. ((buf[1] & 0x10) ? 0x0800 : 0) | /* 0000 1000 */
  121. ((buf[1] & 0x20) ? 0x0400 : 0) | /* 0000 0100 */
  122. ((buf[1] & 0x40) ? 0x0200 : 0) | /* 0000 0010 */
  123. ((buf[1] & 0x80) ? 0x0100 : 0); /* 0000 0001 */
  124. i2cdprintk("ir hauppauge (em2840): code=0x%02x (rcv=0x%02x%02x)\n",
  125. code, buf[1], buf[0]);
  126. /* return key */
  127. *ir_key = code;
  128. *ir_raw = code;
  129. return 1;
  130. }
  131. int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key,
  132. u32 *ir_raw)
  133. {
  134. unsigned char buf[3];
  135. /* poll IR chip */
  136. if (3 != i2c_master_recv(ir->c, buf, 3)) {
  137. i2cdprintk("read error\n");
  138. return -EIO;
  139. }
  140. i2cdprintk("key %02x\n", buf[2]&0x3f);
  141. if (buf[0] != 0x00)
  142. return 0;
  143. *ir_key = buf[2]&0x3f;
  144. *ir_raw = buf[2]&0x3f;
  145. return 1;
  146. }
  147. int em28xx_get_key_winfast_usbii_deluxe(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  148. {
  149. unsigned char subaddr, keydetect, key;
  150. struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0, .buf = &subaddr, .len = 1},
  151. { .addr = ir->c->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} };
  152. subaddr = 0x10;
  153. if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
  154. i2cdprintk("read error\n");
  155. return -EIO;
  156. }
  157. if (keydetect == 0x00)
  158. return 0;
  159. subaddr = 0x00;
  160. msg[1].buf = &key;
  161. if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
  162. i2cdprintk("read error\n");
  163. return -EIO;
  164. }
  165. if (key == 0x00)
  166. return 0;
  167. *ir_key = key;
  168. *ir_raw = key;
  169. return 1;
  170. }
  171. /**********************************************************
  172. Poll based get keycode functions
  173. **********************************************************/
  174. /* This is for the em2860/em2880 */
  175. static int default_polling_getkey(struct em28xx_IR *ir,
  176. struct em28xx_ir_poll_result *poll_result)
  177. {
  178. struct em28xx *dev = ir->dev;
  179. int rc;
  180. u8 msg[3] = { 0, 0, 0 };
  181. /* Read key toggle, brand, and key code
  182. on registers 0x45, 0x46 and 0x47
  183. */
  184. rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
  185. msg, sizeof(msg));
  186. if (rc < 0)
  187. return rc;
  188. /* Infrared toggle (Reg 0x45[7]) */
  189. poll_result->toggle_bit = (msg[0] >> 7);
  190. /* Infrared read count (Reg 0x45[6:0] */
  191. poll_result->read_count = (msg[0] & 0x7f);
  192. /* Remote Control Address (Reg 0x46) */
  193. poll_result->rc_address = msg[1];
  194. /* Remote Control Data (Reg 0x47) */
  195. poll_result->rc_data[0] = msg[2];
  196. return 0;
  197. }
  198. static int em2874_polling_getkey(struct em28xx_IR *ir,
  199. struct em28xx_ir_poll_result *poll_result)
  200. {
  201. struct em28xx *dev = ir->dev;
  202. int rc;
  203. u8 msg[5] = { 0, 0, 0, 0, 0 };
  204. /* Read key toggle, brand, and key code
  205. on registers 0x51-55
  206. */
  207. rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
  208. msg, sizeof(msg));
  209. if (rc < 0)
  210. return rc;
  211. /* Infrared toggle (Reg 0x51[7]) */
  212. poll_result->toggle_bit = (msg[0] >> 7);
  213. /* Infrared read count (Reg 0x51[6:0] */
  214. poll_result->read_count = (msg[0] & 0x7f);
  215. /* Remote Control Address (Reg 0x52) */
  216. poll_result->rc_address = msg[1];
  217. /* Remote Control Data (Reg 0x53-55) */
  218. poll_result->rc_data[0] = msg[2];
  219. poll_result->rc_data[1] = msg[3];
  220. poll_result->rc_data[2] = msg[4];
  221. return 0;
  222. }
  223. /**********************************************************
  224. Polling code for em28xx
  225. **********************************************************/
  226. static void em28xx_ir_handle_key(struct em28xx_IR *ir)
  227. {
  228. int result;
  229. int do_sendkey = 0;
  230. struct em28xx_ir_poll_result poll_result;
  231. /* read the registers containing the IR status */
  232. result = ir->get_key(ir, &poll_result);
  233. if (result < 0) {
  234. dprintk("ir->get_key() failed %d\n", result);
  235. return;
  236. }
  237. dprintk("ir->get_key result tb=%02x rc=%02x lr=%02x data=%02x%02x\n",
  238. poll_result.toggle_bit, poll_result.read_count,
  239. ir->last_readcount, poll_result.rc_address,
  240. poll_result.rc_data[0]);
  241. if (ir->dev->chip_id == CHIP_ID_EM2874) {
  242. /* The em2874 clears the readcount field every time the
  243. register is read. The em2860/2880 datasheet says that it
  244. is supposed to clear the readcount, but it doesn't. So with
  245. the em2874, we are looking for a non-zero read count as
  246. opposed to a readcount that is incrementing */
  247. ir->last_readcount = 0;
  248. }
  249. if (poll_result.read_count == 0) {
  250. /* The button has not been pressed since the last read */
  251. } else if (ir->last_toggle != poll_result.toggle_bit) {
  252. /* A button has been pressed */
  253. dprintk("button has been pressed\n");
  254. ir->last_toggle = poll_result.toggle_bit;
  255. ir->repeat_interval = 0;
  256. do_sendkey = 1;
  257. } else if (poll_result.toggle_bit == ir->last_toggle &&
  258. poll_result.read_count > 0 &&
  259. poll_result.read_count != ir->last_readcount) {
  260. /* The button is still being held down */
  261. dprintk("button being held down\n");
  262. /* Debouncer for first keypress */
  263. if (ir->repeat_interval++ > 9) {
  264. /* Start repeating after 1 second */
  265. do_sendkey = 1;
  266. }
  267. }
  268. if (do_sendkey) {
  269. dprintk("sending keypress\n");
  270. if (ir->full_code)
  271. ir_input_keydown(ir->input, &ir->ir,
  272. poll_result.rc_address << 8 |
  273. poll_result.rc_data[0]);
  274. else
  275. ir_input_keydown(ir->input, &ir->ir,
  276. poll_result.rc_data[0]);
  277. ir_input_nokey(ir->input, &ir->ir);
  278. }
  279. ir->last_readcount = poll_result.read_count;
  280. return;
  281. }
  282. static void em28xx_ir_work(struct work_struct *work)
  283. {
  284. struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
  285. em28xx_ir_handle_key(ir);
  286. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  287. }
  288. static void em28xx_ir_start(struct em28xx_IR *ir)
  289. {
  290. INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
  291. schedule_delayed_work(&ir->work, 0);
  292. }
  293. static void em28xx_ir_stop(struct em28xx_IR *ir)
  294. {
  295. cancel_delayed_work_sync(&ir->work);
  296. }
  297. int em28xx_ir_change_protocol(void *priv, u64 ir_type)
  298. {
  299. int rc = 0;
  300. struct em28xx_IR *ir = priv;
  301. struct em28xx *dev = ir->dev;
  302. u8 ir_config = EM2874_IR_RC5;
  303. /* Adjust xclk based o IR table for RC5/NEC tables */
  304. dev->board.ir_codes->ir_type = IR_TYPE_OTHER;
  305. if (ir_type == IR_TYPE_RC5) {
  306. dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
  307. ir->full_code = 1;
  308. } else if (ir_type == IR_TYPE_NEC) {
  309. dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
  310. ir_config = EM2874_IR_NEC;
  311. ir->full_code = 1;
  312. } else
  313. rc = -EINVAL;
  314. dev->board.ir_codes->ir_type = ir_type;
  315. em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
  316. EM28XX_XCLK_IR_RC5_MODE);
  317. /* Setup the proper handler based on the chip */
  318. switch (dev->chip_id) {
  319. case CHIP_ID_EM2860:
  320. case CHIP_ID_EM2883:
  321. ir->get_key = default_polling_getkey;
  322. break;
  323. case CHIP_ID_EM2874:
  324. ir->get_key = em2874_polling_getkey;
  325. em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
  326. break;
  327. default:
  328. printk("Unrecognized em28xx chip id: IR not supported\n");
  329. rc = -EINVAL;
  330. }
  331. return rc;
  332. }
  333. int em28xx_ir_init(struct em28xx *dev)
  334. {
  335. struct em28xx_IR *ir;
  336. struct input_dev *input_dev;
  337. int err = -ENOMEM;
  338. if (dev->board.ir_codes == NULL) {
  339. /* No remote control support */
  340. return 0;
  341. }
  342. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  343. input_dev = input_allocate_device();
  344. if (!ir || !input_dev)
  345. goto err_out_free;
  346. /* record handles to ourself */
  347. ir->dev = dev;
  348. dev->ir = ir;
  349. ir->input = input_dev;
  350. /*
  351. * em2874 supports more protocols. For now, let's just announce
  352. * the two protocols that were already tested
  353. */
  354. ir->props.allowed_protos = IR_TYPE_RC5 | IR_TYPE_NEC;
  355. ir->props.priv = ir;
  356. ir->props.change_protocol = em28xx_ir_change_protocol;
  357. /* This is how often we ask the chip for IR information */
  358. ir->polling = 100; /* ms */
  359. /* init input device */
  360. snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)",
  361. dev->name);
  362. usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
  363. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  364. /* Set IR protocol */
  365. em28xx_ir_change_protocol(ir, dev->board.ir_codes->ir_type);
  366. err = ir_input_init(input_dev, &ir->ir, IR_TYPE_OTHER);
  367. if (err < 0)
  368. goto err_out_free;
  369. input_dev->name = ir->name;
  370. input_dev->phys = ir->phys;
  371. input_dev->id.bustype = BUS_USB;
  372. input_dev->id.version = 1;
  373. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  374. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  375. input_dev->dev.parent = &dev->udev->dev;
  376. em28xx_ir_start(ir);
  377. /* all done */
  378. err = ir_input_register(ir->input, dev->board.ir_codes,
  379. &ir->props);
  380. if (err)
  381. goto err_out_stop;
  382. return 0;
  383. err_out_stop:
  384. em28xx_ir_stop(ir);
  385. dev->ir = NULL;
  386. err_out_free:
  387. kfree(ir);
  388. return err;
  389. }
  390. int em28xx_ir_fini(struct em28xx *dev)
  391. {
  392. struct em28xx_IR *ir = dev->ir;
  393. /* skip detach on non attached boards */
  394. if (!ir)
  395. return 0;
  396. em28xx_ir_stop(ir);
  397. ir_input_unregister(ir->input);
  398. kfree(ir);
  399. /* done */
  400. dev->ir = NULL;
  401. return 0;
  402. }
  403. /**********************************************************
  404. Handle Webcam snapshot button
  405. **********************************************************/
  406. static void em28xx_query_sbutton(struct work_struct *work)
  407. {
  408. /* Poll the register and see if the button is depressed */
  409. struct em28xx *dev =
  410. container_of(work, struct em28xx, sbutton_query_work.work);
  411. int ret;
  412. ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
  413. if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
  414. u8 cleared;
  415. /* Button is depressed, clear the register */
  416. cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;
  417. em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);
  418. /* Not emulate the keypress */
  419. input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
  420. 1);
  421. /* Now unpress the key */
  422. input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
  423. 0);
  424. }
  425. /* Schedule next poll */
  426. schedule_delayed_work(&dev->sbutton_query_work,
  427. msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
  428. }
  429. void em28xx_register_snapshot_button(struct em28xx *dev)
  430. {
  431. struct input_dev *input_dev;
  432. int err;
  433. em28xx_info("Registering snapshot button...\n");
  434. input_dev = input_allocate_device();
  435. if (!input_dev) {
  436. em28xx_errdev("input_allocate_device failed\n");
  437. return;
  438. }
  439. usb_make_path(dev->udev, dev->snapshot_button_path,
  440. sizeof(dev->snapshot_button_path));
  441. strlcat(dev->snapshot_button_path, "/sbutton",
  442. sizeof(dev->snapshot_button_path));
  443. INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);
  444. input_dev->name = "em28xx snapshot button";
  445. input_dev->phys = dev->snapshot_button_path;
  446. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  447. set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
  448. input_dev->keycodesize = 0;
  449. input_dev->keycodemax = 0;
  450. input_dev->id.bustype = BUS_USB;
  451. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  452. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  453. input_dev->id.version = 1;
  454. input_dev->dev.parent = &dev->udev->dev;
  455. err = input_register_device(input_dev);
  456. if (err) {
  457. em28xx_errdev("input_register_device failed\n");
  458. input_free_device(input_dev);
  459. return;
  460. }
  461. dev->sbutton_input_dev = input_dev;
  462. schedule_delayed_work(&dev->sbutton_query_work,
  463. msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
  464. return;
  465. }
  466. void em28xx_deregister_snapshot_button(struct em28xx *dev)
  467. {
  468. if (dev->sbutton_input_dev != NULL) {
  469. em28xx_info("Deregistering snapshot button\n");
  470. cancel_rearming_delayed_work(&dev->sbutton_query_work);
  471. input_unregister_device(dev->sbutton_input_dev);
  472. dev->sbutton_input_dev = NULL;
  473. }
  474. return;
  475. }