em28xx-input.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
  218. ir_type = IR_TYPE_OTHER;
  219. ir_codes = ir_codes_hauppauge_new;
  220. ir->mask_keycode = 0x007f0000;
  221. break;
  222. }
  223. if (NULL == ir_codes) {
  224. err = -ENODEV;
  225. goto err_out_free;
  226. }
  227. /* init input device */
  228. snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)",
  229. dev->name);
  230. usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
  231. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  232. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  233. input_dev->name = ir->name;
  234. input_dev->phys = ir->phys;
  235. input_dev->id.bustype = BUS_USB;
  236. input_dev->id.version = 1;
  237. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  238. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  239. input_dev->dev.parent = &dev->udev->dev;
  240. /* record handles to ourself */
  241. ir->dev = dev;
  242. dev->ir = ir;
  243. /* Get the current key status, to avoid adding an
  244. unexistent key code */
  245. ir->last_gpio = ir->get_key(ir);
  246. em28xx_ir_start(ir);
  247. /* all done */
  248. err = input_register_device(ir->input);
  249. if (err)
  250. goto err_out_stop;
  251. return 0;
  252. err_out_stop:
  253. em28xx_ir_stop(ir);
  254. dev->ir = NULL;
  255. err_out_free:
  256. input_free_device(input_dev);
  257. kfree(ir);
  258. return err;
  259. }
  260. int em28xx_ir_fini(struct em28xx *dev)
  261. {
  262. struct em28xx_IR *ir = dev->ir;
  263. /* skip detach on non attached boards */
  264. if (!ir)
  265. return 0;
  266. em28xx_ir_stop(ir);
  267. input_unregister_device(ir->input);
  268. kfree(ir);
  269. /* done */
  270. dev->ir = NULL;
  271. return 0;
  272. }
  273. /**********************************************************
  274. Handle Webcam snapshot button
  275. **********************************************************/
  276. static void em28xx_query_sbutton(struct work_struct *work)
  277. {
  278. /* Poll the register and see if the button is depressed */
  279. struct em28xx *dev =
  280. container_of(work, struct em28xx, sbutton_query_work.work);
  281. int ret;
  282. ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
  283. if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
  284. u8 cleared;
  285. /* Button is depressed, clear the register */
  286. cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;
  287. em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);
  288. /* Not emulate the keypress */
  289. input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
  290. 1);
  291. /* Now unpress the key */
  292. input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
  293. 0);
  294. }
  295. /* Schedule next poll */
  296. schedule_delayed_work(&dev->sbutton_query_work,
  297. msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
  298. }
  299. void em28xx_register_snapshot_button(struct em28xx *dev)
  300. {
  301. struct input_dev *input_dev;
  302. int err;
  303. em28xx_info("Registering snapshot button...\n");
  304. input_dev = input_allocate_device();
  305. if (!input_dev) {
  306. em28xx_errdev("input_allocate_device failed\n");
  307. return;
  308. }
  309. usb_make_path(dev->udev, dev->snapshot_button_path,
  310. sizeof(dev->snapshot_button_path));
  311. strlcat(dev->snapshot_button_path, "/sbutton",
  312. sizeof(dev->snapshot_button_path));
  313. INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);
  314. input_dev->name = "em28xx snapshot button";
  315. input_dev->phys = dev->snapshot_button_path;
  316. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  317. set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
  318. input_dev->keycodesize = 0;
  319. input_dev->keycodemax = 0;
  320. input_dev->id.bustype = BUS_USB;
  321. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  322. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  323. input_dev->id.version = 1;
  324. input_dev->dev.parent = &dev->udev->dev;
  325. err = input_register_device(input_dev);
  326. if (err) {
  327. em28xx_errdev("input_register_device failed\n");
  328. input_free_device(input_dev);
  329. return;
  330. }
  331. dev->sbutton_input_dev = input_dev;
  332. schedule_delayed_work(&dev->sbutton_query_work,
  333. msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
  334. return;
  335. }
  336. void em28xx_deregister_snapshot_button(struct em28xx *dev)
  337. {
  338. if (dev->sbutton_input_dev != NULL) {
  339. em28xx_info("Deregistering snapshot button\n");
  340. cancel_rearming_delayed_work(&dev->sbutton_query_work);
  341. input_unregister_device(dev->sbutton_input_dev);
  342. dev->sbutton_input_dev = NULL;
  343. }
  344. return;
  345. }