em28xx-input.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. };
  64. /**********************************************************
  65. I2C IR based get keycodes - should be used with ir-kbd-i2c
  66. **********************************************************/
  67. int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  68. {
  69. unsigned char b;
  70. /* poll IR chip */
  71. if (1 != i2c_master_recv(ir->c, &b, 1)) {
  72. i2cdprintk("read error\n");
  73. return -EIO;
  74. }
  75. /* it seems that 0xFE indicates that a button is still hold
  76. down, while 0xff indicates that no button is hold
  77. down. 0xfe sequences are sometimes interrupted by 0xFF */
  78. i2cdprintk("key %02x\n", b);
  79. if (b == 0xff)
  80. return 0;
  81. if (b == 0xfe)
  82. /* keep old data */
  83. return 1;
  84. *ir_key = b;
  85. *ir_raw = b;
  86. return 1;
  87. }
  88. int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  89. {
  90. unsigned char buf[2];
  91. unsigned char code;
  92. /* poll IR chip */
  93. if (2 != i2c_master_recv(ir->c, buf, 2))
  94. return -EIO;
  95. /* Does eliminate repeated parity code */
  96. if (buf[1] == 0xff)
  97. return 0;
  98. ir->old = buf[1];
  99. /* Rearranges bits to the right order */
  100. code = ((buf[0]&0x01)<<5) | /* 0010 0000 */
  101. ((buf[0]&0x02)<<3) | /* 0001 0000 */
  102. ((buf[0]&0x04)<<1) | /* 0000 1000 */
  103. ((buf[0]&0x08)>>1) | /* 0000 0100 */
  104. ((buf[0]&0x10)>>3) | /* 0000 0010 */
  105. ((buf[0]&0x20)>>5); /* 0000 0001 */
  106. i2cdprintk("ir hauppauge (em2840): code=0x%02x (rcv=0x%02x)\n",
  107. code, buf[0]);
  108. /* return key */
  109. *ir_key = code;
  110. *ir_raw = code;
  111. return 1;
  112. }
  113. int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key,
  114. u32 *ir_raw)
  115. {
  116. unsigned char buf[3];
  117. /* poll IR chip */
  118. if (3 != i2c_master_recv(ir->c, buf, 3)) {
  119. i2cdprintk("read error\n");
  120. return -EIO;
  121. }
  122. i2cdprintk("key %02x\n", buf[2]&0x3f);
  123. if (buf[0] != 0x00)
  124. return 0;
  125. *ir_key = buf[2]&0x3f;
  126. *ir_raw = buf[2]&0x3f;
  127. return 1;
  128. }
  129. /**********************************************************
  130. Poll based get keycode functions
  131. **********************************************************/
  132. /* This is for the em2860/em2880 */
  133. static int default_polling_getkey(struct em28xx_IR *ir,
  134. struct em28xx_ir_poll_result *poll_result)
  135. {
  136. struct em28xx *dev = ir->dev;
  137. int rc;
  138. u8 msg[3] = { 0, 0, 0 };
  139. /* Read key toggle, brand, and key code
  140. on registers 0x45, 0x46 and 0x47
  141. */
  142. rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
  143. msg, sizeof(msg));
  144. if (rc < 0)
  145. return rc;
  146. /* Infrared toggle (Reg 0x45[7]) */
  147. poll_result->toggle_bit = (msg[0] >> 7);
  148. /* Infrared read count (Reg 0x45[6:0] */
  149. poll_result->read_count = (msg[0] & 0x7f);
  150. /* Remote Control Address (Reg 0x46) */
  151. poll_result->rc_address = msg[1];
  152. /* Remote Control Data (Reg 0x47) */
  153. poll_result->rc_data[0] = msg[2];
  154. return 0;
  155. }
  156. static int em2874_polling_getkey(struct em28xx_IR *ir,
  157. struct em28xx_ir_poll_result *poll_result)
  158. {
  159. struct em28xx *dev = ir->dev;
  160. int rc;
  161. u8 msg[5] = { 0, 0, 0, 0, 0 };
  162. /* Read key toggle, brand, and key code
  163. on registers 0x51-55
  164. */
  165. rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
  166. msg, sizeof(msg));
  167. if (rc < 0)
  168. return rc;
  169. /* Infrared toggle (Reg 0x51[7]) */
  170. poll_result->toggle_bit = (msg[0] >> 7);
  171. /* Infrared read count (Reg 0x51[6:0] */
  172. poll_result->read_count = (msg[0] & 0x7f);
  173. /* Remote Control Address (Reg 0x52) */
  174. poll_result->rc_address = msg[1];
  175. /* Remote Control Data (Reg 0x53-55) */
  176. poll_result->rc_data[0] = msg[2];
  177. poll_result->rc_data[1] = msg[3];
  178. poll_result->rc_data[2] = msg[4];
  179. return 0;
  180. }
  181. /**********************************************************
  182. Polling code for em28xx
  183. **********************************************************/
  184. static void em28xx_ir_handle_key(struct em28xx_IR *ir)
  185. {
  186. int result;
  187. int do_sendkey = 0;
  188. struct em28xx_ir_poll_result poll_result;
  189. /* read the registers containing the IR status */
  190. result = ir->get_key(ir, &poll_result);
  191. if (result < 0) {
  192. dprintk("ir->get_key() failed %d\n", result);
  193. return;
  194. }
  195. dprintk("ir->get_key result tb=%02x rc=%02x lr=%02x data=%02x%02x\n",
  196. poll_result.toggle_bit, poll_result.read_count,
  197. ir->last_readcount, poll_result.rc_address,
  198. poll_result.rc_data[0]);
  199. if (ir->dev->chip_id == CHIP_ID_EM2874) {
  200. /* The em2874 clears the readcount field every time the
  201. register is read. The em2860/2880 datasheet says that it
  202. is supposed to clear the readcount, but it doesn't. So with
  203. the em2874, we are looking for a non-zero read count as
  204. opposed to a readcount that is incrementing */
  205. ir->last_readcount = 0;
  206. }
  207. if (poll_result.read_count == 0) {
  208. /* The button has not been pressed since the last read */
  209. } else if (ir->last_toggle != poll_result.toggle_bit) {
  210. /* A button has been pressed */
  211. dprintk("button has been pressed\n");
  212. ir->last_toggle = poll_result.toggle_bit;
  213. ir->repeat_interval = 0;
  214. do_sendkey = 1;
  215. } else if (poll_result.toggle_bit == ir->last_toggle &&
  216. poll_result.read_count > 0 &&
  217. poll_result.read_count != ir->last_readcount) {
  218. /* The button is still being held down */
  219. dprintk("button being held down\n");
  220. /* Debouncer for first keypress */
  221. if (ir->repeat_interval++ > 9) {
  222. /* Start repeating after 1 second */
  223. do_sendkey = 1;
  224. }
  225. }
  226. if (do_sendkey) {
  227. dprintk("sending keypress\n");
  228. if (ir->full_code)
  229. ir_input_keydown(ir->input, &ir->ir,
  230. poll_result.rc_address << 8 |
  231. poll_result.rc_data[0]);
  232. else
  233. ir_input_keydown(ir->input, &ir->ir,
  234. poll_result.rc_data[0]);
  235. ir_input_nokey(ir->input, &ir->ir);
  236. }
  237. ir->last_readcount = poll_result.read_count;
  238. return;
  239. }
  240. static void em28xx_ir_work(struct work_struct *work)
  241. {
  242. struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
  243. em28xx_ir_handle_key(ir);
  244. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  245. }
  246. static void em28xx_ir_start(struct em28xx_IR *ir)
  247. {
  248. INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
  249. schedule_delayed_work(&ir->work, 0);
  250. }
  251. static void em28xx_ir_stop(struct em28xx_IR *ir)
  252. {
  253. cancel_delayed_work_sync(&ir->work);
  254. }
  255. int em28xx_ir_init(struct em28xx *dev)
  256. {
  257. struct em28xx_IR *ir;
  258. struct input_dev *input_dev;
  259. u8 ir_config;
  260. int err = -ENOMEM;
  261. if (dev->board.ir_codes == NULL) {
  262. /* No remote control support */
  263. return 0;
  264. }
  265. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  266. input_dev = input_allocate_device();
  267. if (!ir || !input_dev)
  268. goto err_out_free;
  269. ir->input = input_dev;
  270. /* Setup the proper handler based on the chip */
  271. switch (dev->chip_id) {
  272. case CHIP_ID_EM2860:
  273. case CHIP_ID_EM2883:
  274. if (dev->model == EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950)
  275. ir->full_code = 1;
  276. ir->get_key = default_polling_getkey;
  277. break;
  278. case CHIP_ID_EM2874:
  279. ir->get_key = em2874_polling_getkey;
  280. /* For now we only support RC5, so enable it */
  281. ir_config = EM2874_IR_RC5;
  282. em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
  283. break;
  284. default:
  285. printk("Unrecognized em28xx chip id: IR not supported\n");
  286. goto err_out_free;
  287. }
  288. /* This is how often we ask the chip for IR information */
  289. ir->polling = 100; /* ms */
  290. /* init input device */
  291. snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)",
  292. dev->name);
  293. usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
  294. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  295. err = ir_input_init(input_dev, &ir->ir, IR_TYPE_OTHER,
  296. dev->board.ir_codes);
  297. if (err < 0)
  298. goto err_out_free;
  299. input_dev->name = ir->name;
  300. input_dev->phys = ir->phys;
  301. input_dev->id.bustype = BUS_USB;
  302. input_dev->id.version = 1;
  303. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  304. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  305. input_dev->dev.parent = &dev->udev->dev;
  306. /* record handles to ourself */
  307. ir->dev = dev;
  308. dev->ir = ir;
  309. em28xx_ir_start(ir);
  310. /* all done */
  311. err = input_register_device(ir->input);
  312. if (err)
  313. goto err_out_stop;
  314. return 0;
  315. err_out_stop:
  316. em28xx_ir_stop(ir);
  317. dev->ir = NULL;
  318. err_out_free:
  319. ir_input_free(input_dev);
  320. input_free_device(input_dev);
  321. kfree(ir);
  322. return err;
  323. }
  324. int em28xx_ir_fini(struct em28xx *dev)
  325. {
  326. struct em28xx_IR *ir = dev->ir;
  327. /* skip detach on non attached boards */
  328. if (!ir)
  329. return 0;
  330. em28xx_ir_stop(ir);
  331. ir_input_free(ir->input);
  332. input_unregister_device(ir->input);
  333. kfree(ir);
  334. /* done */
  335. dev->ir = NULL;
  336. return 0;
  337. }
  338. /**********************************************************
  339. Handle Webcam snapshot button
  340. **********************************************************/
  341. static void em28xx_query_sbutton(struct work_struct *work)
  342. {
  343. /* Poll the register and see if the button is depressed */
  344. struct em28xx *dev =
  345. container_of(work, struct em28xx, sbutton_query_work.work);
  346. int ret;
  347. ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
  348. if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
  349. u8 cleared;
  350. /* Button is depressed, clear the register */
  351. cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;
  352. em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);
  353. /* Not emulate the keypress */
  354. input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
  355. 1);
  356. /* Now unpress the key */
  357. input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
  358. 0);
  359. }
  360. /* Schedule next poll */
  361. schedule_delayed_work(&dev->sbutton_query_work,
  362. msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
  363. }
  364. void em28xx_register_snapshot_button(struct em28xx *dev)
  365. {
  366. struct input_dev *input_dev;
  367. int err;
  368. em28xx_info("Registering snapshot button...\n");
  369. input_dev = input_allocate_device();
  370. if (!input_dev) {
  371. em28xx_errdev("input_allocate_device failed\n");
  372. return;
  373. }
  374. usb_make_path(dev->udev, dev->snapshot_button_path,
  375. sizeof(dev->snapshot_button_path));
  376. strlcat(dev->snapshot_button_path, "/sbutton",
  377. sizeof(dev->snapshot_button_path));
  378. INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);
  379. input_dev->name = "em28xx snapshot button";
  380. input_dev->phys = dev->snapshot_button_path;
  381. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  382. set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
  383. input_dev->keycodesize = 0;
  384. input_dev->keycodemax = 0;
  385. input_dev->id.bustype = BUS_USB;
  386. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  387. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  388. input_dev->id.version = 1;
  389. input_dev->dev.parent = &dev->udev->dev;
  390. err = input_register_device(input_dev);
  391. if (err) {
  392. em28xx_errdev("input_register_device failed\n");
  393. input_free_device(input_dev);
  394. return;
  395. }
  396. dev->sbutton_input_dev = input_dev;
  397. schedule_delayed_work(&dev->sbutton_query_work,
  398. msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
  399. return;
  400. }
  401. void em28xx_deregister_snapshot_button(struct em28xx *dev)
  402. {
  403. if (dev->sbutton_input_dev != NULL) {
  404. em28xx_info("Deregistering snapshot button\n");
  405. cancel_rearming_delayed_work(&dev->sbutton_query_work);
  406. input_unregister_device(dev->sbutton_input_dev);
  407. dev->sbutton_input_dev = NULL;
  408. }
  409. return;
  410. }