dvb-usb-remote.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* dvb-usb-remote.c is part of the DVB USB library.
  2. *
  3. * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
  4. * see dvb-usb-init.c for copyright information.
  5. *
  6. * This file contains functions for initializing the the input-device and for handling remote-control-queries.
  7. */
  8. #include "dvb-usb-common.h"
  9. /* Remote-control poll function - called every dib->rc_query_interval ms to see
  10. * whether the remote control has received anything.
  11. *
  12. * TODO: Fix the repeat rate of the input device.
  13. */
  14. static void dvb_usb_read_remote_control(void *data)
  15. {
  16. struct dvb_usb_device *d = data;
  17. u32 event;
  18. int state;
  19. /* TODO: need a lock here. We can simply skip checking for the remote control
  20. if we're busy. */
  21. if (d->props.rc_query(d,&event,&state)) {
  22. err("error while querying for an remote control event.");
  23. goto schedule;
  24. }
  25. switch (state) {
  26. case REMOTE_NO_KEY_PRESSED:
  27. break;
  28. case REMOTE_KEY_PRESSED:
  29. deb_rc("key pressed\n");
  30. d->last_event = event;
  31. case REMOTE_KEY_REPEAT:
  32. deb_rc("key repeated\n");
  33. input_event(&d->rc_input_dev, EV_KEY, event, 1);
  34. input_event(&d->rc_input_dev, EV_KEY, d->last_event, 0);
  35. input_sync(&d->rc_input_dev);
  36. break;
  37. default:
  38. break;
  39. }
  40. /* improved repeat handling ???
  41. switch (state) {
  42. case REMOTE_NO_KEY_PRESSED:
  43. deb_rc("NO KEY PRESSED\n");
  44. if (d->last_state != REMOTE_NO_KEY_PRESSED) {
  45. deb_rc("releasing event %d\n",d->last_event);
  46. input_event(&d->rc_input_dev, EV_KEY, d->last_event, 0);
  47. input_sync(&d->rc_input_dev);
  48. }
  49. d->last_state = REMOTE_NO_KEY_PRESSED;
  50. d->last_event = 0;
  51. break;
  52. case REMOTE_KEY_PRESSED:
  53. deb_rc("KEY PRESSED\n");
  54. deb_rc("pressing event %d\n",event);
  55. input_event(&d->rc_input_dev, EV_KEY, event, 1);
  56. input_sync(&d->rc_input_dev);
  57. d->last_event = event;
  58. d->last_state = REMOTE_KEY_PRESSED;
  59. break;
  60. case REMOTE_KEY_REPEAT:
  61. deb_rc("KEY_REPEAT\n");
  62. if (d->last_state != REMOTE_NO_KEY_PRESSED) {
  63. deb_rc("repeating event %d\n",d->last_event);
  64. input_event(&d->rc_input_dev, EV_KEY, d->last_event, 2);
  65. input_sync(&d->rc_input_dev);
  66. d->last_state = REMOTE_KEY_REPEAT;
  67. }
  68. default:
  69. break;
  70. }
  71. */
  72. schedule:
  73. schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
  74. }
  75. int dvb_usb_remote_init(struct dvb_usb_device *d)
  76. {
  77. int i;
  78. if (d->props.rc_key_map == NULL)
  79. return 0;
  80. /* Initialise the remote-control structures.*/
  81. init_input_dev(&d->rc_input_dev);
  82. d->rc_input_dev.evbit[0] = BIT(EV_KEY);
  83. d->rc_input_dev.keycodesize = sizeof(unsigned char);
  84. d->rc_input_dev.keycodemax = KEY_MAX;
  85. d->rc_input_dev.name = "IR-receiver inside an USB DVB receiver";
  86. /* set the bits for the keys */
  87. deb_rc("key map size: %d\n",d->props.rc_key_map_size);
  88. for (i = 0; i < d->props.rc_key_map_size; i++) {
  89. deb_rc("setting bit for event %d item %d\n",d->props.rc_key_map[i].event, i);
  90. set_bit(d->props.rc_key_map[i].event, d->rc_input_dev.keybit);
  91. }
  92. /* Start the remote-control polling. */
  93. if (d->props.rc_interval < 40)
  94. d->props.rc_interval = 100; /* default */
  95. /* setting these two values to non-zero, we have to manage key repeats */
  96. d->rc_input_dev.rep[REP_PERIOD] = d->props.rc_interval;
  97. d->rc_input_dev.rep[REP_DELAY] = d->props.rc_interval + 150;
  98. input_register_device(&d->rc_input_dev);
  99. INIT_WORK(&d->rc_query_work, dvb_usb_read_remote_control, d);
  100. info("schedule remote query interval to %d msecs.",d->props.rc_interval);
  101. schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc_interval));
  102. d->state |= DVB_USB_STATE_REMOTE;
  103. return 0;
  104. }
  105. int dvb_usb_remote_exit(struct dvb_usb_device *d)
  106. {
  107. if (d->state & DVB_USB_STATE_REMOTE) {
  108. cancel_delayed_work(&d->rc_query_work);
  109. flush_scheduled_work();
  110. input_unregister_device(&d->rc_input_dev);
  111. }
  112. d->state &= ~DVB_USB_STATE_REMOTE;
  113. return 0;
  114. }
  115. #define DVB_USB_RC_NEC_EMPTY 0x00
  116. #define DVB_USB_RC_NEC_KEY_PRESSED 0x01
  117. #define DVB_USB_RC_NEC_KEY_REPEATED 0x02
  118. int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
  119. u8 keybuf[5], u32 *event, int *state)
  120. {
  121. int i;
  122. struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
  123. *event = 0;
  124. *state = REMOTE_NO_KEY_PRESSED;
  125. switch (keybuf[0]) {
  126. case DVB_USB_RC_NEC_EMPTY:
  127. break;
  128. case DVB_USB_RC_NEC_KEY_PRESSED:
  129. if ((u8) ~keybuf[1] != keybuf[2] ||
  130. (u8) ~keybuf[3] != keybuf[4]) {
  131. deb_err("remote control checksum failed.\n");
  132. break;
  133. }
  134. /* See if we can match the raw key code. */
  135. for (i = 0; i < sizeof(keymap)/sizeof(struct dvb_usb_rc_key); i++)
  136. if (keymap[i].custom == keybuf[1] &&
  137. keymap[i].data == keybuf[3]) {
  138. *event = keymap[i].event;
  139. *state = REMOTE_KEY_PRESSED;
  140. break;
  141. }
  142. deb_err("key mapping failed - no appropriate key found in keymapping\n");
  143. break;
  144. case DVB_USB_RC_NEC_KEY_REPEATED:
  145. *state = REMOTE_KEY_REPEAT;
  146. break;
  147. default:
  148. deb_err("unkown type of remote status: %d\n",keybuf[0]);
  149. break;
  150. }
  151. return 0;
  152. }
  153. EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);