em28xx-input.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 dprintk(fmt, arg...) \
  33. if (ir_debug) { \
  34. printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg); \
  35. }
  36. /* ----------------------------------------------------------------------- */
  37. int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  38. {
  39. unsigned char b;
  40. /* poll IR chip */
  41. if (1 != i2c_master_recv(&ir->c, &b, 1)) {
  42. dprintk("read error\n");
  43. return -EIO;
  44. }
  45. /* it seems that 0xFE indicates that a button is still hold
  46. down, while 0xff indicates that no button is hold
  47. down. 0xfe sequences are sometimes interrupted by 0xFF */
  48. dprintk("key %02x\n", b);
  49. if (b == 0xff)
  50. return 0;
  51. if (b == 0xfe)
  52. /* keep old data */
  53. return 1;
  54. *ir_key = b;
  55. *ir_raw = b;
  56. return 1;
  57. }
  58. int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  59. {
  60. unsigned char buf[2];
  61. unsigned char code;
  62. /* poll IR chip */
  63. if (2 != i2c_master_recv(&ir->c, buf, 2))
  64. return -EIO;
  65. /* Does eliminate repeated parity code */
  66. if (buf[1] == 0xff)
  67. return 0;
  68. ir->old = buf[1];
  69. /* Rearranges bits to the right order */
  70. code = ((buf[0]&0x01)<<5) | /* 0010 0000 */
  71. ((buf[0]&0x02)<<3) | /* 0001 0000 */
  72. ((buf[0]&0x04)<<1) | /* 0000 1000 */
  73. ((buf[0]&0x08)>>1) | /* 0000 0100 */
  74. ((buf[0]&0x10)>>3) | /* 0000 0010 */
  75. ((buf[0]&0x20)>>5); /* 0000 0001 */
  76. dprintk("ir hauppauge (em2840): code=0x%02x (rcv=0x%02x)\n",
  77. code, buf[0]);
  78. /* return key */
  79. *ir_key = code;
  80. *ir_raw = code;
  81. return 1;
  82. }
  83. int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key,
  84. u32 *ir_raw)
  85. {
  86. unsigned char buf[3];
  87. /* poll IR chip */
  88. if (3 != i2c_master_recv(&ir->c, buf, 3)) {
  89. dprintk("read error\n");
  90. return -EIO;
  91. }
  92. dprintk("key %02x\n", buf[2]&0x3f);
  93. if (buf[0] != 0x00)
  94. return 0;
  95. *ir_key = buf[2]&0x3f;
  96. *ir_raw = buf[2]&0x3f;
  97. return 1;
  98. }
  99. static void em28xx_query_sbutton(struct work_struct *work)
  100. {
  101. /* Poll the register and see if the button is depressed */
  102. struct em28xx *dev =
  103. container_of(work, struct em28xx, sbutton_query_work.work);
  104. int ret;
  105. ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
  106. if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
  107. u8 cleared;
  108. /* Button is depressed, clear the register */
  109. cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;
  110. em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);
  111. /* Not emulate the keypress */
  112. input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
  113. 1);
  114. /* Now unpress the key */
  115. input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
  116. 0);
  117. }
  118. /* Schedule next poll */
  119. schedule_delayed_work(&dev->sbutton_query_work,
  120. msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
  121. }
  122. void em28xx_register_snapshot_button(struct em28xx *dev)
  123. {
  124. struct input_dev *input_dev;
  125. int err;
  126. em28xx_info("Registering snapshot button...\n");
  127. input_dev = input_allocate_device();
  128. if (!input_dev) {
  129. em28xx_errdev("input_allocate_device failed\n");
  130. return;
  131. }
  132. usb_make_path(dev->udev, dev->snapshot_button_path,
  133. sizeof(dev->snapshot_button_path));
  134. strlcat(dev->snapshot_button_path, "/sbutton",
  135. sizeof(dev->snapshot_button_path));
  136. INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);
  137. input_dev->name = "em28xx snapshot button";
  138. input_dev->phys = dev->snapshot_button_path;
  139. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  140. set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
  141. input_dev->keycodesize = 0;
  142. input_dev->keycodemax = 0;
  143. input_dev->id.bustype = BUS_USB;
  144. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  145. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  146. input_dev->id.version = 1;
  147. input_dev->dev.parent = &dev->udev->dev;
  148. err = input_register_device(input_dev);
  149. if (err) {
  150. em28xx_errdev("input_register_device failed\n");
  151. input_free_device(input_dev);
  152. return;
  153. }
  154. dev->sbutton_input_dev = input_dev;
  155. schedule_delayed_work(&dev->sbutton_query_work,
  156. msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
  157. return;
  158. }
  159. void em28xx_deregister_snapshot_button(struct em28xx *dev)
  160. {
  161. if (dev->sbutton_input_dev != NULL) {
  162. em28xx_info("Deregistering snapshot button\n");
  163. cancel_rearming_delayed_work(&dev->sbutton_query_work);
  164. input_unregister_device(dev->sbutton_input_dev);
  165. dev->sbutton_input_dev = NULL;
  166. }
  167. return;
  168. }
  169. /* ----------------------------------------------------------------------
  170. * Local variables:
  171. * c-basic-offset: 8
  172. * End:
  173. */