cx231xx-input.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. poll_result.rc_data[0]);
  108. ir_input_nokey(ir->input, &ir->ir);
  109. }
  110. ir->last_readcount = poll_result.read_count;
  111. return;
  112. }
  113. static void ir_timer(unsigned long data)
  114. {
  115. struct cx231xx_IR *ir = (struct cx231xx_IR *)data;
  116. schedule_work(&ir->work);
  117. }
  118. static void cx231xx_ir_work(struct work_struct *work)
  119. {
  120. struct cx231xx_IR *ir = container_of(work, struct cx231xx_IR, work);
  121. cx231xx_ir_handle_key(ir);
  122. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
  123. }
  124. void cx231xx_ir_start(struct cx231xx_IR *ir)
  125. {
  126. setup_timer(&ir->timer, ir_timer, (unsigned long)ir);
  127. INIT_WORK(&ir->work, cx231xx_ir_work);
  128. schedule_work(&ir->work);
  129. }
  130. static void cx231xx_ir_stop(struct cx231xx_IR *ir)
  131. {
  132. del_timer_sync(&ir->timer);
  133. flush_scheduled_work();
  134. }
  135. int cx231xx_ir_init(struct cx231xx *dev)
  136. {
  137. struct cx231xx_IR *ir;
  138. struct input_dev *input_dev;
  139. u8 ir_config;
  140. int err = -ENOMEM;
  141. if (dev->board.ir_codes == NULL) {
  142. /* No remote control support */
  143. return 0;
  144. }
  145. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  146. input_dev = input_allocate_device();
  147. if (!ir || !input_dev)
  148. goto err_out_free;
  149. ir->input = input_dev;
  150. /* Setup the proper handler based on the chip */
  151. switch (dev->chip_id) {
  152. default:
  153. printk("Unrecognized cx231xx chip id: IR not supported\n");
  154. goto err_out_free;
  155. }
  156. /* This is how often we ask the chip for IR information */
  157. ir->polling = 100; /* ms */
  158. /* init input device */
  159. snprintf(ir->name, sizeof(ir->name), "cx231xx IR (%s)", dev->name);
  160. usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
  161. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  162. ir_input_init(input_dev, &ir->ir, IR_TYPE_OTHER, dev->board.ir_codes);
  163. input_dev->name = ir->name;
  164. input_dev->phys = ir->phys;
  165. input_dev->id.bustype = BUS_USB;
  166. input_dev->id.version = 1;
  167. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  168. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  169. input_dev->dev.parent = &dev->udev->dev;
  170. /* record handles to ourself */
  171. ir->dev = dev;
  172. dev->ir = ir;
  173. cx231xx_ir_start(ir);
  174. /* all done */
  175. err = input_register_device(ir->input);
  176. if (err)
  177. goto err_out_stop;
  178. return 0;
  179. err_out_stop:
  180. cx231xx_ir_stop(ir);
  181. dev->ir = NULL;
  182. err_out_free:
  183. input_free_device(input_dev);
  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. input_unregister_device(ir->input);
  195. kfree(ir);
  196. /* done */
  197. dev->ir = NULL;
  198. return 0;
  199. }