av7110_ir.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include <linux/types.h>
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/moduleparam.h>
  5. #include <linux/input.h>
  6. #include <linux/proc_fs.h>
  7. #include <asm/bitops.h>
  8. #include "av7110.h"
  9. #include "av7110_hw.h"
  10. #define UP_TIMEOUT (HZ*7/25)
  11. /* enable ir debugging by or'ing debug with 16 */
  12. static int av_cnt;
  13. static struct av7110 *av_list[4];
  14. static struct input_dev *input_dev;
  15. static u8 delay_timer_finished;
  16. static u16 key_map [256] = {
  17. KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7,
  18. KEY_8, KEY_9, KEY_BACK, 0, KEY_POWER, KEY_MUTE, 0, KEY_INFO,
  19. KEY_VOLUMEUP, KEY_VOLUMEDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  20. KEY_CHANNELUP, KEY_CHANNELDOWN, 0, 0, 0, 0, 0, 0,
  21. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  22. 0, 0, 0, 0, KEY_TEXT, 0, 0, KEY_TV, 0, 0, 0, 0, 0, KEY_SETUP, 0, 0,
  23. 0, 0, 0, KEY_SUBTITLE, 0, 0, KEY_LANGUAGE, 0,
  24. KEY_RADIO, 0, 0, 0, 0, KEY_EXIT, 0, 0,
  25. KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_OK, 0, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RED, KEY_GREEN, KEY_YELLOW,
  27. KEY_BLUE, 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_LIST, 0, 0, 0, 0, 0, 0,
  28. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  30. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32. 0, 0, 0, 0, KEY_UP, KEY_UP, KEY_DOWN, KEY_DOWN,
  33. 0, 0, 0, 0, KEY_EPG, 0, 0, 0,
  34. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  35. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  36. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_VCR
  37. };
  38. static void av7110_emit_keyup(unsigned long data)
  39. {
  40. if (!data || !test_bit(data, input_dev->key))
  41. return;
  42. input_event(input_dev, EV_KEY, data, !!0);
  43. }
  44. static struct timer_list keyup_timer = { .function = av7110_emit_keyup };
  45. static void av7110_emit_key(unsigned long parm)
  46. {
  47. struct av7110 *av7110 = (struct av7110 *) parm;
  48. u32 ir_config = av7110->ir_config;
  49. u32 ircom = av7110->ir_command;
  50. u8 data;
  51. u8 addr;
  52. static u16 old_toggle = 0;
  53. u16 new_toggle;
  54. u16 keycode;
  55. /* extract device address and data */
  56. switch (ir_config & 0x0003) {
  57. case 0: /* RC5: 5 bits device address, 6 bits data */
  58. data = ircom & 0x3f;
  59. addr = (ircom >> 6) & 0x1f;
  60. break;
  61. case 1: /* RCMM: 8(?) bits device address, 8(?) bits data */
  62. data = ircom & 0xff;
  63. addr = (ircom >> 8) & 0xff;
  64. break;
  65. case 2: /* extended RC5: 5 bits device address, 7 bits data */
  66. data = ircom & 0x3f;
  67. addr = (ircom >> 6) & 0x1f;
  68. /* invert 7th data bit for backward compatibility with RC5 keymaps */
  69. if (!(ircom & 0x1000))
  70. data |= 0x40;
  71. break;
  72. default:
  73. printk("invalid ir_config %x\n", ir_config);
  74. return;
  75. }
  76. keycode = key_map[data];
  77. dprintk(16, "code %08x -> addr %i data 0x%02x -> keycode %i\n",
  78. ircom, addr, data, keycode);
  79. /* check device address (if selected) */
  80. if (ir_config & 0x4000)
  81. if (addr != ((ir_config >> 16) & 0xff))
  82. return;
  83. if (!keycode) {
  84. printk ("%s: unknown key 0x%02x!!\n", __FUNCTION__, data);
  85. return;
  86. }
  87. if ((ir_config & 0x0003) == 1)
  88. new_toggle = 0; /* RCMM */
  89. else
  90. new_toggle = (ircom & 0x800); /* RC5, extended RC5 */
  91. if (timer_pending(&keyup_timer)) {
  92. del_timer(&keyup_timer);
  93. if (keyup_timer.data != keycode || new_toggle != old_toggle) {
  94. delay_timer_finished = 0;
  95. input_event(input_dev, EV_KEY, keyup_timer.data, !!0);
  96. input_event(input_dev, EV_KEY, keycode, !0);
  97. } else
  98. if (delay_timer_finished)
  99. input_event(input_dev, EV_KEY, keycode, 2);
  100. } else {
  101. delay_timer_finished = 0;
  102. input_event(input_dev, EV_KEY, keycode, !0);
  103. }
  104. keyup_timer.expires = jiffies + UP_TIMEOUT;
  105. keyup_timer.data = keycode;
  106. add_timer(&keyup_timer);
  107. old_toggle = new_toggle;
  108. }
  109. static void input_register_keys(void)
  110. {
  111. int i;
  112. memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
  113. for (i = 0; i < ARRAY_SIZE(key_map); i++) {
  114. if (key_map[i] > KEY_MAX)
  115. key_map[i] = 0;
  116. else if (key_map[i] > KEY_RESERVED)
  117. set_bit(key_map[i], input_dev->keybit);
  118. }
  119. }
  120. static void input_repeat_key(unsigned long data)
  121. {
  122. /* called by the input driver after rep[REP_DELAY] ms */
  123. delay_timer_finished = 1;
  124. }
  125. static int av7110_setup_irc_config(struct av7110 *av7110, u32 ir_config)
  126. {
  127. int ret = 0;
  128. dprintk(4, "%p\n", av7110);
  129. if (av7110) {
  130. ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetIR, 1, ir_config);
  131. av7110->ir_config = ir_config;
  132. }
  133. return ret;
  134. }
  135. static int av7110_ir_write_proc(struct file *file, const char __user *buffer,
  136. unsigned long count, void *data)
  137. {
  138. char *page;
  139. int size = 4 + 256 * sizeof(u16);
  140. u32 ir_config;
  141. int i;
  142. if (count < size)
  143. return -EINVAL;
  144. page = (char *) vmalloc(size);
  145. if (!page)
  146. return -ENOMEM;
  147. if (copy_from_user(page, buffer, size)) {
  148. vfree(page);
  149. return -EFAULT;
  150. }
  151. memcpy(&ir_config, page, 4);
  152. memcpy(&key_map, page + 4, 256 * sizeof(u16));
  153. vfree(page);
  154. if (FW_VERSION(av_list[0]->arm_app) >= 0x2620 && !(ir_config & 0x0001))
  155. ir_config |= 0x0002; /* enable extended RC5 */
  156. for (i = 0; i < av_cnt; i++)
  157. av7110_setup_irc_config(av_list[i], ir_config);
  158. input_register_keys();
  159. return count;
  160. }
  161. static void ir_handler(struct av7110 *av7110, u32 ircom)
  162. {
  163. dprintk(4, "ircommand = %08x\n", ircom);
  164. av7110->ir_command = ircom;
  165. tasklet_schedule(&av7110->ir_tasklet);
  166. }
  167. int __devinit av7110_ir_init(struct av7110 *av7110)
  168. {
  169. static struct proc_dir_entry *e;
  170. if (av_cnt >= sizeof av_list/sizeof av_list[0])
  171. return -ENOSPC;
  172. av7110_setup_irc_config(av7110, 0x0001);
  173. av_list[av_cnt++] = av7110;
  174. if (av_cnt == 1) {
  175. init_timer(&keyup_timer);
  176. keyup_timer.data = 0;
  177. input_dev = input_allocate_device();
  178. if (!input_dev)
  179. return -ENOMEM;
  180. input_dev->name = "DVB on-card IR receiver";
  181. set_bit(EV_KEY, input_dev->evbit);
  182. set_bit(EV_REP, input_dev->evbit);
  183. input_register_keys();
  184. input_register_device(input_dev);
  185. input_dev->timer.function = input_repeat_key;
  186. e = create_proc_entry("av7110_ir", S_IFREG | S_IRUGO | S_IWUSR, NULL);
  187. if (e) {
  188. e->write_proc = av7110_ir_write_proc;
  189. e->size = 4 + 256 * sizeof(u16);
  190. }
  191. }
  192. tasklet_init(&av7110->ir_tasklet, av7110_emit_key, (unsigned long) av7110);
  193. av7110->ir_handler = ir_handler;
  194. return 0;
  195. }
  196. void __devexit av7110_ir_exit(struct av7110 *av7110)
  197. {
  198. int i;
  199. if (av_cnt == 0)
  200. return;
  201. av7110->ir_handler = NULL;
  202. tasklet_kill(&av7110->ir_tasklet);
  203. for (i = 0; i < av_cnt; i++)
  204. if (av_list[i] == av7110) {
  205. av_list[i] = av_list[av_cnt-1];
  206. av_list[av_cnt-1] = NULL;
  207. break;
  208. }
  209. if (av_cnt == 1) {
  210. del_timer_sync(&keyup_timer);
  211. remove_proc_entry("av7110_ir", NULL);
  212. input_unregister_device(input_dev);
  213. }
  214. av_cnt--;
  215. }
  216. //MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
  217. //MODULE_LICENSE("GPL");