em28xx-input.c 10 KB

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