cx231xx-input.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 <linux/slab.h>
  25. #include "cx231xx.h"
  26. static unsigned int ir_debug;
  27. module_param(ir_debug, int, 0644);
  28. MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
  29. #define MODULE_NAME "cx231xx"
  30. #define i2cdprintk(fmt, arg...) \
  31. if (ir_debug) { \
  32. printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
  33. }
  34. #define dprintk(fmt, arg...) \
  35. if (ir_debug) { \
  36. printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
  37. }
  38. /**********************************************************
  39. Polling structure used by cx231xx IR's
  40. **********************************************************/
  41. struct cx231xx_ir_poll_result {
  42. unsigned int toggle_bit:1;
  43. unsigned int read_count:7;
  44. u8 rc_address;
  45. u8 rc_data[4];
  46. };
  47. struct cx231xx_IR {
  48. struct cx231xx *dev;
  49. struct input_dev *input;
  50. struct ir_input_state ir;
  51. char name[32];
  52. char phys[32];
  53. /* poll external decoder */
  54. int polling;
  55. struct work_struct work;
  56. struct timer_list timer;
  57. unsigned int last_toggle:1;
  58. unsigned int last_readcount;
  59. unsigned int repeat_interval;
  60. int (*get_key) (struct cx231xx_IR *, struct cx231xx_ir_poll_result *);
  61. };
  62. /**********************************************************
  63. Polling code for cx231xx
  64. **********************************************************/
  65. static void cx231xx_ir_handle_key(struct cx231xx_IR *ir)
  66. {
  67. int result;
  68. int do_sendkey = 0;
  69. struct cx231xx_ir_poll_result poll_result;
  70. /* read the registers containing the IR status */
  71. result = ir->get_key(ir, &poll_result);
  72. if (result < 0) {
  73. dprintk("ir->get_key() failed %d\n", result);
  74. return;
  75. }
  76. dprintk("ir->get_key result tb=%02x rc=%02x lr=%02x data=%02x\n",
  77. poll_result.toggle_bit, poll_result.read_count,
  78. ir->last_readcount, poll_result.rc_data[0]);
  79. if (ir->dev->chip_id == CHIP_ID_EM2874) {
  80. /* The em2874 clears the readcount field every time the
  81. register is read. The em2860/2880 datasheet says that it
  82. is supposed to clear the readcount, but it doesn't. So with
  83. the em2874, we are looking for a non-zero read count as
  84. opposed to a readcount that is incrementing */
  85. ir->last_readcount = 0;
  86. }
  87. if (poll_result.read_count == 0) {
  88. /* The button has not been pressed since the last read */
  89. } else if (ir->last_toggle != poll_result.toggle_bit) {
  90. /* A button has been pressed */
  91. dprintk("button has been pressed\n");
  92. ir->last_toggle = poll_result.toggle_bit;
  93. ir->repeat_interval = 0;
  94. do_sendkey = 1;
  95. } else if (poll_result.toggle_bit == ir->last_toggle &&
  96. poll_result.read_count > 0 &&
  97. poll_result.read_count != ir->last_readcount) {
  98. /* The button is still being held down */
  99. dprintk("button being held down\n");
  100. /* Debouncer for first keypress */
  101. if (ir->repeat_interval++ > 9) {
  102. /* Start repeating after 1 second */
  103. do_sendkey = 1;
  104. }
  105. }
  106. if (do_sendkey) {
  107. dprintk("sending keypress\n");
  108. ir_input_keydown(ir->input, &ir->ir, poll_result.rc_data[0]);
  109. ir_input_nokey(ir->input, &ir->ir);
  110. }
  111. ir->last_readcount = poll_result.read_count;
  112. return;
  113. }
  114. static void ir_timer(unsigned long data)
  115. {
  116. struct cx231xx_IR *ir = (struct cx231xx_IR *)data;
  117. schedule_work(&ir->work);
  118. }
  119. static void cx231xx_ir_work(struct work_struct *work)
  120. {
  121. struct cx231xx_IR *ir = container_of(work, struct cx231xx_IR, work);
  122. cx231xx_ir_handle_key(ir);
  123. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
  124. }
  125. void cx231xx_ir_start(struct cx231xx_IR *ir)
  126. {
  127. setup_timer(&ir->timer, ir_timer, (unsigned long)ir);
  128. INIT_WORK(&ir->work, cx231xx_ir_work);
  129. schedule_work(&ir->work);
  130. }
  131. static void cx231xx_ir_stop(struct cx231xx_IR *ir)
  132. {
  133. del_timer_sync(&ir->timer);
  134. flush_scheduled_work();
  135. }
  136. int cx231xx_ir_init(struct cx231xx *dev)
  137. {
  138. struct cx231xx_IR *ir;
  139. struct input_dev *input_dev;
  140. u8 ir_config;
  141. int err = -ENOMEM;
  142. if (dev->board.ir_codes == NULL) {
  143. /* No remote control support */
  144. return 0;
  145. }
  146. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  147. input_dev = input_allocate_device();
  148. if (!ir || !input_dev)
  149. goto err_out_free;
  150. ir->input = input_dev;
  151. /* Setup the proper handler based on the chip */
  152. switch (dev->chip_id) {
  153. default:
  154. printk("Unrecognized cx231xx chip id: IR not supported\n");
  155. goto err_out_free;
  156. }
  157. /* This is how often we ask the chip for IR information */
  158. ir->polling = 100; /* ms */
  159. /* init input device */
  160. snprintf(ir->name, sizeof(ir->name), "cx231xx IR (%s)", dev->name);
  161. usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
  162. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  163. err = ir_input_init(input_dev, &ir->ir, IR_TYPE_OTHER);
  164. if (err < 0)
  165. goto err_out_free;
  166. input_dev->name = ir->name;
  167. input_dev->phys = ir->phys;
  168. input_dev->id.bustype = BUS_USB;
  169. input_dev->id.version = 1;
  170. input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  171. input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  172. input_dev->dev.parent = &dev->udev->dev;
  173. /* record handles to ourself */
  174. ir->dev = dev;
  175. dev->ir = ir;
  176. cx231xx_ir_start(ir);
  177. /* all done */
  178. err = ir_input_register(ir->input, dev->board.ir_codes,
  179. NULL, MODULE_NAME);
  180. if (err)
  181. goto err_out_stop;
  182. return 0;
  183. err_out_stop:
  184. cx231xx_ir_stop(ir);
  185. dev->ir = NULL;
  186. err_out_free:
  187. kfree(ir);
  188. return err;
  189. }
  190. int cx231xx_ir_fini(struct cx231xx *dev)
  191. {
  192. struct cx231xx_IR *ir = dev->ir;
  193. /* skip detach on non attached boards */
  194. if (!ir)
  195. return 0;
  196. cx231xx_ir_stop(ir);
  197. ir_input_unregister(ir->input);
  198. kfree(ir);
  199. /* done */
  200. dev->ir = NULL;
  201. return 0;
  202. }