em28xx-input.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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->c.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 {
  44. struct em28xx *dev;
  45. struct input_dev *input;
  46. struct ir_input_state ir;
  47. char name[32];
  48. char phys[32];
  49. /* poll external decoder */
  50. int polling;
  51. struct work_struct work;
  52. struct timer_list timer;
  53. u32 last_gpio;
  54. u32 mask_keycode;
  55. u32 mask_keydown;
  56. u32 mask_keyup;
  57. u32 mask_repeat;
  58. int (*get_key)(struct em28xx_IR *);
  59. };
  60. /**********************************************************
  61. I2C IR based get keycodes - should be used with ir-kbd-i2c
  62. **********************************************************/
  63. int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  64. {
  65. unsigned char b;
  66. /* poll IR chip */
  67. if (1 != i2c_master_recv(&ir->c, &b, 1)) {
  68. i2cdprintk("read error\n");
  69. return -EIO;
  70. }
  71. /* it seems that 0xFE indicates that a button is still hold
  72. down, while 0xff indicates that no button is hold
  73. down. 0xfe sequences are sometimes interrupted by 0xFF */
  74. i2cdprintk("key %02x\n", b);
  75. if (b == 0xff)
  76. return 0;
  77. if (b == 0xfe)
  78. /* keep old data */
  79. return 1;
  80. *ir_key = b;
  81. *ir_raw = b;
  82. return 1;
  83. }
  84. int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  85. {
  86. unsigned char buf[2];
  87. unsigned char code;
  88. /* poll IR chip */
  89. if (2 != i2c_master_recv(&ir->c, buf, 2))
  90. return -EIO;
  91. /* Does eliminate repeated parity code */
  92. if (buf[1] == 0xff)
  93. return 0;
  94. ir->old = buf[1];
  95. /* Rearranges bits to the right order */
  96. code = ((buf[0]&0x01)<<5) | /* 0010 0000 */
  97. ((buf[0]&0x02)<<3) | /* 0001 0000 */
  98. ((buf[0]&0x04)<<1) | /* 0000 1000 */
  99. ((buf[0]&0x08)>>1) | /* 0000 0100 */
  100. ((buf[0]&0x10)>>3) | /* 0000 0010 */
  101. ((buf[0]&0x20)>>5); /* 0000 0001 */
  102. i2cdprintk("ir hauppauge (em2840): code=0x%02x (rcv=0x%02x)\n",
  103. code, buf[0]);
  104. /* return key */
  105. *ir_key = code;
  106. *ir_raw = code;
  107. return 1;
  108. }
  109. int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key,
  110. u32 *ir_raw)
  111. {
  112. unsigned char buf[3];
  113. /* poll IR chip */
  114. if (3 != i2c_master_recv(&ir->c, buf, 3)) {
  115. i2cdprintk("read error\n");
  116. return -EIO;
  117. }
  118. i2cdprintk("key %02x\n", buf[2]&0x3f);
  119. if (buf[0] != 0x00)
  120. return 0;
  121. *ir_key = buf[2]&0x3f;
  122. *ir_raw = buf[2]&0x3f;
  123. return 1;
  124. }
  125. /**********************************************************
  126. Poll based get keycode functions
  127. **********************************************************/
  128. static int default_polling_getkey(struct em28xx_IR *ir)
  129. {
  130. struct em28xx *dev = ir->dev;
  131. int rc;
  132. u8 msg[4] = { 0, 0, 0, 0 };
  133. /* Read key toggle, brand, and key code
  134. on registers 0x45, 0x46 and 0x47
  135. */
  136. rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
  137. msg, sizeof(msg));
  138. if (rc < 0)
  139. return rc;
  140. return (int)(le32_to_cpu(*(u32 *)msg));
  141. }
  142. /**********************************************************
  143. Polling code for em28xx
  144. **********************************************************/
  145. static void em28xx_ir_handle_key(struct em28xx_IR *ir)
  146. {
  147. int gpio;
  148. u32 data;
  149. /* read gpio value */
  150. gpio = ir->get_key(ir);
  151. if (gpio < 0)
  152. return;
  153. if (gpio == ir->last_gpio)
  154. return;
  155. ir->last_gpio = gpio;
  156. /* extract data */
  157. data = ir_extract_bits(gpio, ir->mask_keycode);
  158. dprintk("irq gpio=0x%x code=%d | poll%s%s\n",
  159. gpio, data,
  160. (gpio & ir->mask_keydown) ? " down" : "",
  161. (gpio & ir->mask_keyup) ? " up" : "");
  162. /* Generate keyup/keydown events */
  163. if (ir->mask_keydown) {
  164. /* bit set on keydown */
  165. if (gpio & ir->mask_keydown)
  166. ir_input_keydown(ir->input, &ir->ir, data, data);
  167. else
  168. ir_input_nokey(ir->input, &ir->ir);
  169. } else if (ir->mask_keyup) {
  170. /* bit cleared on keydown */
  171. if (!(gpio & ir->mask_keyup))
  172. ir_input_keydown(ir->input, &ir->ir, data, data);
  173. else
  174. ir_input_nokey(ir->input, &ir->ir);
  175. } else if (ir->mask_repeat) {
  176. int count = ir->mask_repeat & gpio;
  177. /* Avoid keyboard bouncing */
  178. if ((count == 1) || (count >= 5)) {
  179. ir_input_keydown(ir->input, &ir->ir, data, data);
  180. ir_input_nokey(ir->input, &ir->ir);
  181. }
  182. } else {
  183. /* can't distinguish keydown/up :-/ */
  184. ir_input_keydown(ir->input, &ir->ir, data, data);
  185. ir_input_nokey(ir->input, &ir->ir);
  186. }
  187. }
  188. static void ir_timer(unsigned long data)
  189. {
  190. struct em28xx_IR *ir = (struct em28xx_IR *)data;
  191. schedule_work(&ir->work);
  192. }
  193. static void em28xx_ir_work(struct work_struct *work)
  194. {
  195. struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work);
  196. em28xx_ir_handle_key(ir);
  197. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
  198. }
  199. void em28xx_ir_start(struct em28xx_IR *ir)
  200. {
  201. setup_timer(&ir->timer, ir_timer, (unsigned long)ir);
  202. INIT_WORK(&ir->work, em28xx_ir_work);
  203. schedule_work(&ir->work);
  204. }
  205. static void em28xx_ir_stop(struct em28xx_IR *ir)
  206. {
  207. del_timer_sync(&ir->timer);
  208. flush_scheduled_work();
  209. }
  210. int em28xx_ir_init(struct em28xx *dev)
  211. {
  212. struct em28xx_IR *ir;
  213. struct input_dev *input_dev;
  214. IR_KEYTAB_TYPE *ir_codes = NULL;
  215. int ir_type = IR_TYPE_OTHER;
  216. int err = -ENOMEM;
  217. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  218. input_dev = input_allocate_device();
  219. if (!ir || !input_dev)
  220. goto err_out_free;
  221. ir->input = input_dev;
  222. /* */
  223. ir->get_key = default_polling_getkey;
  224. ir->polling = 50; /* ms */
  225. /* detect & configure */
  226. switch (dev->model) {
  227. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
  228. ir_type = IR_TYPE_OTHER;
  229. ir_codes = ir_codes_hauppauge_new;
  230. ir->mask_keycode = 0x007f0000;
  231. ir->mask_repeat = 0x0000007f;
  232. break;
  233. }
  234. if (NULL == ir_codes) {
  235. err = -ENODEV;
  236. goto err_out_free;
  237. }
  238. /* init input device */
  239. snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)",
  240. dev->name);
  241. usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
  242. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  243. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  244. input_dev->name = ir->name;
  245. input_dev->phys = ir->phys;
  246. input_dev->id.bustype = BUS_USB;
  247. input_dev->id.version = 1;
  248. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  249. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  250. input_dev->dev.parent = &dev->udev->dev;
  251. /* record handles to ourself */
  252. ir->dev = dev;
  253. dev->ir = ir;
  254. /* Get the current key status, to avoid adding an
  255. unexistent key code */
  256. ir->last_gpio = ir->get_key(ir);
  257. em28xx_ir_start(ir);
  258. /* all done */
  259. err = input_register_device(ir->input);
  260. if (err)
  261. goto err_out_stop;
  262. return 0;
  263. err_out_stop:
  264. em28xx_ir_stop(ir);
  265. dev->ir = NULL;
  266. err_out_free:
  267. input_free_device(input_dev);
  268. kfree(ir);
  269. return err;
  270. }
  271. int em28xx_ir_fini(struct em28xx *dev)
  272. {
  273. struct em28xx_IR *ir = dev->ir;
  274. /* skip detach on non attached boards */
  275. if (!ir)
  276. return 0;
  277. em28xx_ir_stop(ir);
  278. input_unregister_device(ir->input);
  279. kfree(ir);
  280. /* done */
  281. dev->ir = NULL;
  282. return 0;
  283. }
  284. /**********************************************************
  285. Handle Webcam snapshot button
  286. **********************************************************/
  287. static void em28xx_query_sbutton(struct work_struct *work)
  288. {
  289. /* Poll the register and see if the button is depressed */
  290. struct em28xx *dev =
  291. container_of(work, struct em28xx, sbutton_query_work.work);
  292. int ret;
  293. ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
  294. if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
  295. u8 cleared;
  296. /* Button is depressed, clear the register */
  297. cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;
  298. em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);
  299. /* Not emulate the keypress */
  300. input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
  301. 1);
  302. /* Now unpress the key */
  303. input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
  304. 0);
  305. }
  306. /* Schedule next poll */
  307. schedule_delayed_work(&dev->sbutton_query_work,
  308. msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
  309. }
  310. void em28xx_register_snapshot_button(struct em28xx *dev)
  311. {
  312. struct input_dev *input_dev;
  313. int err;
  314. em28xx_info("Registering snapshot button...\n");
  315. input_dev = input_allocate_device();
  316. if (!input_dev) {
  317. em28xx_errdev("input_allocate_device failed\n");
  318. return;
  319. }
  320. usb_make_path(dev->udev, dev->snapshot_button_path,
  321. sizeof(dev->snapshot_button_path));
  322. strlcat(dev->snapshot_button_path, "/sbutton",
  323. sizeof(dev->snapshot_button_path));
  324. INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);
  325. input_dev->name = "em28xx snapshot button";
  326. input_dev->phys = dev->snapshot_button_path;
  327. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  328. set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
  329. input_dev->keycodesize = 0;
  330. input_dev->keycodemax = 0;
  331. input_dev->id.bustype = BUS_USB;
  332. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  333. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  334. input_dev->id.version = 1;
  335. input_dev->dev.parent = &dev->udev->dev;
  336. err = input_register_device(input_dev);
  337. if (err) {
  338. em28xx_errdev("input_register_device failed\n");
  339. input_free_device(input_dev);
  340. return;
  341. }
  342. dev->sbutton_input_dev = input_dev;
  343. schedule_delayed_work(&dev->sbutton_query_work,
  344. msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
  345. return;
  346. }
  347. void em28xx_deregister_snapshot_button(struct em28xx *dev)
  348. {
  349. if (dev->sbutton_input_dev != NULL) {
  350. em28xx_info("Deregistering snapshot button\n");
  351. cancel_rearming_delayed_work(&dev->sbutton_query_work);
  352. input_unregister_device(dev->sbutton_input_dev);
  353. dev->sbutton_input_dev = NULL;
  354. }
  355. return;
  356. }