em28xx-input.c 15 KB

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