em28xx-input.c 13 KB

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