cx231xx-input.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. handle cx231xx IR remotes via linux kernel input layer.
  3. Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
  4. Based on em28xx driver
  5. < This is a place holder for IR now.>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/delay.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/input.h>
  23. #include <linux/usb.h>
  24. #include "cx231xx.h"
  25. static unsigned int ir_debug;
  26. module_param(ir_debug, int, 0644);
  27. MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
  28. #define i2cdprintk(fmt, arg...) \
  29. if (ir_debug) { \
  30. printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
  31. }
  32. #define dprintk(fmt, arg...) \
  33. if (ir_debug) { \
  34. printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
  35. }
  36. /**********************************************************
  37. Polling structure used by cx231xx IR's
  38. **********************************************************/
  39. struct cx231xx_ir_poll_result {
  40. unsigned int toggle_bit:1;
  41. unsigned int read_count:7;
  42. u8 rc_address;
  43. u8 rc_data[4];
  44. };
  45. struct cx231xx_IR {
  46. struct cx231xx *dev;
  47. struct input_dev *input;
  48. struct ir_input_state ir;
  49. char name[32];
  50. char phys[32];
  51. /* poll external decoder */
  52. int polling;
  53. struct work_struct work;
  54. struct timer_list timer;
  55. unsigned int last_toggle:1;
  56. unsigned int last_readcount;
  57. unsigned int repeat_interval;
  58. int (*get_key) (struct cx231xx_IR *, struct cx231xx_ir_poll_result *);
  59. };
  60. /**********************************************************
  61. Polling code for cx231xx
  62. **********************************************************/
  63. static void cx231xx_ir_handle_key(struct cx231xx_IR *ir)
  64. {
  65. int result;
  66. int do_sendkey = 0;
  67. struct cx231xx_ir_poll_result poll_result;
  68. /* read the registers containing the IR status */
  69. result = ir->get_key(ir, &poll_result);
  70. if (result < 0) {
  71. dprintk("ir->get_key() failed %d\n", result);
  72. return;
  73. }
  74. dprintk("ir->get_key result tb=%02x rc=%02x lr=%02x data=%02x\n",
  75. poll_result.toggle_bit, poll_result.read_count,
  76. ir->last_readcount, poll_result.rc_data[0]);
  77. if (ir->dev->chip_id == CHIP_ID_EM2874) {
  78. /* The em2874 clears the readcount field every time the
  79. register is read. The em2860/2880 datasheet says that it
  80. is supposed to clear the readcount, but it doesn't. So with
  81. the em2874, we are looking for a non-zero read count as
  82. opposed to a readcount that is incrementing */
  83. ir->last_readcount = 0;
  84. }
  85. if (poll_result.read_count == 0) {
  86. /* The button has not been pressed since the last read */
  87. } else if (ir->last_toggle != poll_result.toggle_bit) {
  88. /* A button has been pressed */
  89. dprintk("button has been pressed\n");
  90. ir->last_toggle = poll_result.toggle_bit;
  91. ir->repeat_interval = 0;
  92. do_sendkey = 1;
  93. } else if (poll_result.toggle_bit == ir->last_toggle &&
  94. poll_result.read_count > 0 &&
  95. poll_result.read_count != ir->last_readcount) {
  96. /* The button is still being held down */
  97. dprintk("button being held down\n");
  98. /* Debouncer for first keypress */
  99. if (ir->repeat_interval++ > 9) {
  100. /* Start repeating after 1 second */
  101. do_sendkey = 1;
  102. }
  103. }
  104. if (do_sendkey) {
  105. dprintk("sending keypress\n");
  106. ir_input_keydown(ir->input, &ir->ir, poll_result.rc_data[0]);
  107. ir_input_nokey(ir->input, &ir->ir);
  108. }
  109. ir->last_readcount = poll_result.read_count;
  110. return;
  111. }
  112. static void ir_timer(unsigned long data)
  113. {
  114. struct cx231xx_IR *ir = (struct cx231xx_IR *)data;
  115. schedule_work(&ir->work);
  116. }
  117. static void cx231xx_ir_work(struct work_struct *work)
  118. {
  119. struct cx231xx_IR *ir = container_of(work, struct cx231xx_IR, work);
  120. cx231xx_ir_handle_key(ir);
  121. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
  122. }
  123. void cx231xx_ir_start(struct cx231xx_IR *ir)
  124. {
  125. setup_timer(&ir->timer, ir_timer, (unsigned long)ir);
  126. INIT_WORK(&ir->work, cx231xx_ir_work);
  127. schedule_work(&ir->work);
  128. }
  129. static void cx231xx_ir_stop(struct cx231xx_IR *ir)
  130. {
  131. del_timer_sync(&ir->timer);
  132. flush_scheduled_work();
  133. }
  134. int cx231xx_ir_init(struct cx231xx *dev)
  135. {
  136. struct cx231xx_IR *ir;
  137. struct input_dev *input_dev;
  138. u8 ir_config;
  139. int err = -ENOMEM;
  140. if (dev->board.ir_codes == NULL) {
  141. /* No remote control support */
  142. return 0;
  143. }
  144. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  145. input_dev = input_allocate_device();
  146. if (!ir || !input_dev)
  147. goto err_out_free;
  148. ir->input = input_dev;
  149. /* Setup the proper handler based on the chip */
  150. switch (dev->chip_id) {
  151. default:
  152. printk("Unrecognized cx231xx chip id: IR not supported\n");
  153. goto err_out_free;
  154. }
  155. /* This is how often we ask the chip for IR information */
  156. ir->polling = 100; /* ms */
  157. /* init input device */
  158. snprintf(ir->name, sizeof(ir->name), "cx231xx IR (%s)", dev->name);
  159. usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
  160. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  161. err = ir_input_init(input_dev, &ir->ir, IR_TYPE_OTHER);
  162. if (err < 0)
  163. goto err_out_free;
  164. input_dev->name = ir->name;
  165. input_dev->phys = ir->phys;
  166. input_dev->id.bustype = BUS_USB;
  167. input_dev->id.version = 1;
  168. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  169. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  170. input_dev->dev.parent = &dev->udev->dev;
  171. /* record handles to ourself */
  172. ir->dev = dev;
  173. dev->ir = ir;
  174. cx231xx_ir_start(ir);
  175. /* all done */
  176. err = ir_input_register(ir->input, dev->board.ir_codes);
  177. if (err)
  178. goto err_out_stop;
  179. return 0;
  180. err_out_stop:
  181. cx231xx_ir_stop(ir);
  182. dev->ir = NULL;
  183. err_out_free:
  184. kfree(ir);
  185. return err;
  186. }
  187. int cx231xx_ir_fini(struct cx231xx *dev)
  188. {
  189. struct cx231xx_IR *ir = dev->ir;
  190. /* skip detach on non attached boards */
  191. if (!ir)
  192. return 0;
  193. cx231xx_ir_stop(ir);
  194. ir_input_unregister(ir->input);
  195. kfree(ir);
  196. /* done */
  197. dev->ir = NULL;
  198. return 0;
  199. }