dvb-usb-remote.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* dvb-usb-remote.c is part of the DVB USB library.
  2. *
  3. * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
  4. * see dvb-usb-init.c for copyright information.
  5. *
  6. * This file contains functions for initializing the input-device and for handling remote-control-queries.
  7. */
  8. #include "dvb-usb-common.h"
  9. #include <linux/usb/input.h>
  10. static unsigned int
  11. legacy_dvb_usb_get_keymap_index(const struct input_keymap_entry *ke,
  12. struct rc_map_table *keymap,
  13. unsigned int keymap_size)
  14. {
  15. unsigned int index;
  16. unsigned int scancode;
  17. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  18. index = ke->index;
  19. } else {
  20. if (input_scancode_to_scalar(ke, &scancode))
  21. return keymap_size;
  22. /* See if we can match the raw key code. */
  23. for (index = 0; index < keymap_size; index++)
  24. if (keymap[index].scancode == scancode)
  25. break;
  26. /* See if there is an unused hole in the map */
  27. if (index >= keymap_size) {
  28. for (index = 0; index < keymap_size; index++) {
  29. if (keymap[index].keycode == KEY_RESERVED ||
  30. keymap[index].keycode == KEY_UNKNOWN) {
  31. break;
  32. }
  33. }
  34. }
  35. }
  36. return index;
  37. }
  38. static int legacy_dvb_usb_getkeycode(struct input_dev *dev,
  39. struct input_keymap_entry *ke)
  40. {
  41. struct dvb_usb_device *d = input_get_drvdata(dev);
  42. struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
  43. unsigned int keymap_size = d->props.rc.legacy.rc_map_size;
  44. unsigned int index;
  45. index = legacy_dvb_usb_get_keymap_index(ke, keymap, keymap_size);
  46. if (index >= keymap_size)
  47. return -EINVAL;
  48. ke->keycode = keymap[index].keycode;
  49. if (ke->keycode == KEY_UNKNOWN)
  50. ke->keycode = KEY_RESERVED;
  51. ke->len = sizeof(keymap[index].scancode);
  52. memcpy(&ke->scancode, &keymap[index].scancode, ke->len);
  53. ke->index = index;
  54. return 0;
  55. }
  56. static int legacy_dvb_usb_setkeycode(struct input_dev *dev,
  57. const struct input_keymap_entry *ke,
  58. unsigned int *old_keycode)
  59. {
  60. struct dvb_usb_device *d = input_get_drvdata(dev);
  61. struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
  62. unsigned int keymap_size = d->props.rc.legacy.rc_map_size;
  63. unsigned int index;
  64. index = legacy_dvb_usb_get_keymap_index(ke, keymap, keymap_size);
  65. /*
  66. * FIXME: Currently, it is not possible to increase the size of
  67. * scancode table. For it to happen, one possibility
  68. * would be to allocate a table with key_map_size + 1,
  69. * copying data, appending the new key on it, and freeing
  70. * the old one - or maybe just allocating some spare space
  71. */
  72. if (index >= keymap_size)
  73. return -EINVAL;
  74. *old_keycode = keymap[index].keycode;
  75. keymap->keycode = ke->keycode;
  76. __set_bit(ke->keycode, dev->keybit);
  77. if (*old_keycode != KEY_RESERVED) {
  78. __clear_bit(*old_keycode, dev->keybit);
  79. for (index = 0; index < keymap_size; index++) {
  80. if (keymap[index].keycode == *old_keycode) {
  81. __set_bit(*old_keycode, dev->keybit);
  82. break;
  83. }
  84. }
  85. }
  86. return 0;
  87. }
  88. /* Remote-control poll function - called every dib->rc_query_interval ms to see
  89. * whether the remote control has received anything.
  90. *
  91. * TODO: Fix the repeat rate of the input device.
  92. */
  93. static void legacy_dvb_usb_read_remote_control(struct work_struct *work)
  94. {
  95. struct dvb_usb_device *d =
  96. container_of(work, struct dvb_usb_device, rc_query_work.work);
  97. u32 event;
  98. int state;
  99. /* TODO: need a lock here. We can simply skip checking for the remote control
  100. if we're busy. */
  101. /* when the parameter has been set to 1 via sysfs while the driver was running */
  102. if (dvb_usb_disable_rc_polling)
  103. return;
  104. if (d->props.rc.legacy.rc_query(d,&event,&state)) {
  105. err("error while querying for an remote control event.");
  106. goto schedule;
  107. }
  108. switch (state) {
  109. case REMOTE_NO_KEY_PRESSED:
  110. break;
  111. case REMOTE_KEY_PRESSED:
  112. deb_rc("key pressed\n");
  113. d->last_event = event;
  114. case REMOTE_KEY_REPEAT:
  115. deb_rc("key repeated\n");
  116. input_event(d->input_dev, EV_KEY, event, 1);
  117. input_sync(d->input_dev);
  118. input_event(d->input_dev, EV_KEY, d->last_event, 0);
  119. input_sync(d->input_dev);
  120. break;
  121. default:
  122. break;
  123. }
  124. /* improved repeat handling ???
  125. switch (state) {
  126. case REMOTE_NO_KEY_PRESSED:
  127. deb_rc("NO KEY PRESSED\n");
  128. if (d->last_state != REMOTE_NO_KEY_PRESSED) {
  129. deb_rc("releasing event %d\n",d->last_event);
  130. input_event(d->rc_input_dev, EV_KEY, d->last_event, 0);
  131. input_sync(d->rc_input_dev);
  132. }
  133. d->last_state = REMOTE_NO_KEY_PRESSED;
  134. d->last_event = 0;
  135. break;
  136. case REMOTE_KEY_PRESSED:
  137. deb_rc("KEY PRESSED\n");
  138. deb_rc("pressing event %d\n",event);
  139. input_event(d->rc_input_dev, EV_KEY, event, 1);
  140. input_sync(d->rc_input_dev);
  141. d->last_event = event;
  142. d->last_state = REMOTE_KEY_PRESSED;
  143. break;
  144. case REMOTE_KEY_REPEAT:
  145. deb_rc("KEY_REPEAT\n");
  146. if (d->last_state != REMOTE_NO_KEY_PRESSED) {
  147. deb_rc("repeating event %d\n",d->last_event);
  148. input_event(d->rc_input_dev, EV_KEY, d->last_event, 2);
  149. input_sync(d->rc_input_dev);
  150. d->last_state = REMOTE_KEY_REPEAT;
  151. }
  152. default:
  153. break;
  154. }
  155. */
  156. schedule:
  157. schedule_delayed_work(&d->rc_query_work,msecs_to_jiffies(d->props.rc.legacy.rc_interval));
  158. }
  159. static int legacy_dvb_usb_remote_init(struct dvb_usb_device *d)
  160. {
  161. int i, err, rc_interval;
  162. struct input_dev *input_dev;
  163. input_dev = input_allocate_device();
  164. if (!input_dev)
  165. return -ENOMEM;
  166. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  167. input_dev->name = "IR-receiver inside an USB DVB receiver";
  168. input_dev->phys = d->rc_phys;
  169. usb_to_input_id(d->udev, &input_dev->id);
  170. input_dev->dev.parent = &d->udev->dev;
  171. d->input_dev = input_dev;
  172. d->rc_dev = NULL;
  173. input_dev->getkeycode = legacy_dvb_usb_getkeycode;
  174. input_dev->setkeycode = legacy_dvb_usb_setkeycode;
  175. /* set the bits for the keys */
  176. deb_rc("key map size: %d\n", d->props.rc.legacy.rc_map_size);
  177. for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
  178. deb_rc("setting bit for event %d item %d\n",
  179. d->props.rc.legacy.rc_map_table[i].keycode, i);
  180. set_bit(d->props.rc.legacy.rc_map_table[i].keycode, input_dev->keybit);
  181. }
  182. /* setting these two values to non-zero, we have to manage key repeats */
  183. input_dev->rep[REP_PERIOD] = d->props.rc.legacy.rc_interval;
  184. input_dev->rep[REP_DELAY] = d->props.rc.legacy.rc_interval + 150;
  185. input_set_drvdata(input_dev, d);
  186. err = input_register_device(input_dev);
  187. if (err)
  188. input_free_device(input_dev);
  189. rc_interval = d->props.rc.legacy.rc_interval;
  190. INIT_DELAYED_WORK(&d->rc_query_work, legacy_dvb_usb_read_remote_control);
  191. info("schedule remote query interval to %d msecs.", rc_interval);
  192. schedule_delayed_work(&d->rc_query_work,
  193. msecs_to_jiffies(rc_interval));
  194. d->state |= DVB_USB_STATE_REMOTE;
  195. return err;
  196. }
  197. /* Remote-control poll function - called every dib->rc_query_interval ms to see
  198. * whether the remote control has received anything.
  199. *
  200. * TODO: Fix the repeat rate of the input device.
  201. */
  202. static void dvb_usb_read_remote_control(struct work_struct *work)
  203. {
  204. struct dvb_usb_device *d =
  205. container_of(work, struct dvb_usb_device, rc_query_work.work);
  206. int err;
  207. /* TODO: need a lock here. We can simply skip checking for the remote control
  208. if we're busy. */
  209. /* when the parameter has been set to 1 via sysfs while the
  210. * driver was running, or when bulk mode is enabled after IR init
  211. */
  212. if (dvb_usb_disable_rc_polling || d->props.rc.core.bulk_mode)
  213. return;
  214. err = d->props.rc.core.rc_query(d);
  215. if (err)
  216. err("error %d while querying for an remote control event.", err);
  217. schedule_delayed_work(&d->rc_query_work,
  218. msecs_to_jiffies(d->props.rc.core.rc_interval));
  219. }
  220. static int rc_core_dvb_usb_remote_init(struct dvb_usb_device *d)
  221. {
  222. int err, rc_interval;
  223. struct rc_dev *dev;
  224. dev = rc_allocate_device();
  225. if (!dev)
  226. return -ENOMEM;
  227. dev->driver_name = d->props.rc.core.module_name;
  228. dev->map_name = d->props.rc.core.rc_codes;
  229. dev->change_protocol = d->props.rc.core.change_protocol;
  230. dev->allowed_protocols = d->props.rc.core.allowed_protos;
  231. dev->driver_type = d->props.rc.core.driver_type;
  232. usb_to_input_id(d->udev, &dev->input_id);
  233. dev->input_name = "IR-receiver inside an USB DVB receiver";
  234. dev->input_phys = d->rc_phys;
  235. dev->dev.parent = &d->udev->dev;
  236. dev->priv = d;
  237. err = rc_register_device(dev);
  238. if (err < 0) {
  239. rc_free_device(dev);
  240. return err;
  241. }
  242. d->input_dev = NULL;
  243. d->rc_dev = dev;
  244. if (!d->props.rc.core.rc_query || d->props.rc.core.bulk_mode)
  245. return 0;
  246. /* Polling mode - initialize a work queue for handling it */
  247. INIT_DELAYED_WORK(&d->rc_query_work, dvb_usb_read_remote_control);
  248. rc_interval = d->props.rc.core.rc_interval;
  249. info("schedule remote query interval to %d msecs.", rc_interval);
  250. schedule_delayed_work(&d->rc_query_work,
  251. msecs_to_jiffies(rc_interval));
  252. return 0;
  253. }
  254. int dvb_usb_remote_init(struct dvb_usb_device *d)
  255. {
  256. int err;
  257. if (dvb_usb_disable_rc_polling)
  258. return 0;
  259. if (d->props.rc.legacy.rc_map_table && d->props.rc.legacy.rc_query)
  260. d->props.rc.mode = DVB_RC_LEGACY;
  261. else if (d->props.rc.core.rc_codes)
  262. d->props.rc.mode = DVB_RC_CORE;
  263. else
  264. return 0;
  265. usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
  266. strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
  267. /* Start the remote-control polling. */
  268. if (d->props.rc.legacy.rc_interval < 40)
  269. d->props.rc.legacy.rc_interval = 100; /* default */
  270. if (d->props.rc.mode == DVB_RC_LEGACY)
  271. err = legacy_dvb_usb_remote_init(d);
  272. else
  273. err = rc_core_dvb_usb_remote_init(d);
  274. if (err)
  275. return err;
  276. d->state |= DVB_USB_STATE_REMOTE;
  277. return 0;
  278. }
  279. int dvb_usb_remote_exit(struct dvb_usb_device *d)
  280. {
  281. if (d->state & DVB_USB_STATE_REMOTE) {
  282. cancel_delayed_work_sync(&d->rc_query_work);
  283. if (d->props.rc.mode == DVB_RC_LEGACY)
  284. input_unregister_device(d->input_dev);
  285. else
  286. rc_unregister_device(d->rc_dev);
  287. }
  288. d->state &= ~DVB_USB_STATE_REMOTE;
  289. return 0;
  290. }
  291. #define DVB_USB_RC_NEC_EMPTY 0x00
  292. #define DVB_USB_RC_NEC_KEY_PRESSED 0x01
  293. #define DVB_USB_RC_NEC_KEY_REPEATED 0x02
  294. int dvb_usb_nec_rc_key_to_event(struct dvb_usb_device *d,
  295. u8 keybuf[5], u32 *event, int *state)
  296. {
  297. int i;
  298. struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
  299. *event = 0;
  300. *state = REMOTE_NO_KEY_PRESSED;
  301. switch (keybuf[0]) {
  302. case DVB_USB_RC_NEC_EMPTY:
  303. break;
  304. case DVB_USB_RC_NEC_KEY_PRESSED:
  305. if ((u8) ~keybuf[1] != keybuf[2] ||
  306. (u8) ~keybuf[3] != keybuf[4]) {
  307. deb_err("remote control checksum failed.\n");
  308. break;
  309. }
  310. /* See if we can match the raw key code. */
  311. for (i = 0; i < d->props.rc.legacy.rc_map_size; i++)
  312. if (rc5_custom(&keymap[i]) == keybuf[1] &&
  313. rc5_data(&keymap[i]) == keybuf[3]) {
  314. *event = keymap[i].keycode;
  315. *state = REMOTE_KEY_PRESSED;
  316. return 0;
  317. }
  318. deb_err("key mapping failed - no appropriate key found in keymapping\n");
  319. break;
  320. case DVB_USB_RC_NEC_KEY_REPEATED:
  321. *state = REMOTE_KEY_REPEAT;
  322. break;
  323. default:
  324. deb_err("unknown type of remote status: %d\n",keybuf[0]);
  325. break;
  326. }
  327. return 0;
  328. }
  329. EXPORT_SYMBOL(dvb_usb_nec_rc_key_to_event);