keyspan_remote.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * keyspan_remote: USB driver for the Keyspan DMR
  3. *
  4. * Copyright (C) 2005 Zymeta Corporation - Michael Downey (downey@zymeta.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. *
  10. * This driver has been put together with the support of Innosys, Inc.
  11. * and Keyspan, Inc the manufacturers of the Keyspan USB DMR product.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/usb/input.h>
  18. /* Parameters that can be passed to the driver. */
  19. static int debug;
  20. module_param(debug, int, 0444);
  21. MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
  22. /* Vendor and product ids */
  23. #define USB_KEYSPAN_VENDOR_ID 0x06CD
  24. #define USB_KEYSPAN_PRODUCT_UIA11 0x0202
  25. /* Defines for converting the data from the remote. */
  26. #define ZERO 0x18
  27. #define ZERO_MASK 0x1F /* 5 bits for a 0 */
  28. #define ONE 0x3C
  29. #define ONE_MASK 0x3F /* 6 bits for a 1 */
  30. #define SYNC 0x3F80
  31. #define SYNC_MASK 0x3FFF /* 14 bits for a SYNC sequence */
  32. #define STOP 0x00
  33. #define STOP_MASK 0x1F /* 5 bits for the STOP sequence */
  34. #define GAP 0xFF
  35. #define RECV_SIZE 8 /* The UIA-11 type have a 8 byte limit. */
  36. /*
  37. * Table that maps the 31 possible keycodes to input keys.
  38. * Currently there are 15 and 17 button models so RESERVED codes
  39. * are blank areas in the mapping.
  40. */
  41. static const unsigned short keyspan_key_table[] = {
  42. KEY_RESERVED, /* 0 is just a place holder. */
  43. KEY_RESERVED,
  44. KEY_STOP,
  45. KEY_PLAYCD,
  46. KEY_RESERVED,
  47. KEY_PREVIOUSSONG,
  48. KEY_REWIND,
  49. KEY_FORWARD,
  50. KEY_NEXTSONG,
  51. KEY_RESERVED,
  52. KEY_RESERVED,
  53. KEY_RESERVED,
  54. KEY_PAUSE,
  55. KEY_VOLUMEUP,
  56. KEY_RESERVED,
  57. KEY_RESERVED,
  58. KEY_RESERVED,
  59. KEY_VOLUMEDOWN,
  60. KEY_RESERVED,
  61. KEY_UP,
  62. KEY_RESERVED,
  63. KEY_MUTE,
  64. KEY_LEFT,
  65. KEY_ENTER,
  66. KEY_RIGHT,
  67. KEY_RESERVED,
  68. KEY_RESERVED,
  69. KEY_DOWN,
  70. KEY_RESERVED,
  71. KEY_KPASTERISK,
  72. KEY_RESERVED,
  73. KEY_MENU
  74. };
  75. /* table of devices that work with this driver */
  76. static const struct usb_device_id keyspan_table[] = {
  77. { USB_DEVICE(USB_KEYSPAN_VENDOR_ID, USB_KEYSPAN_PRODUCT_UIA11) },
  78. { } /* Terminating entry */
  79. };
  80. /* Structure to store all the real stuff that a remote sends to us. */
  81. struct keyspan_message {
  82. u16 system;
  83. u8 button;
  84. u8 toggle;
  85. };
  86. /* Structure used for all the bit testing magic needed to be done. */
  87. struct bit_tester {
  88. u32 tester;
  89. int len;
  90. int pos;
  91. int bits_left;
  92. u8 buffer[32];
  93. };
  94. /* Structure to hold all of our driver specific stuff */
  95. struct usb_keyspan {
  96. char name[128];
  97. char phys[64];
  98. unsigned short keymap[ARRAY_SIZE(keyspan_key_table)];
  99. struct usb_device *udev;
  100. struct input_dev *input;
  101. struct usb_interface *interface;
  102. struct usb_endpoint_descriptor *in_endpoint;
  103. struct urb* irq_urb;
  104. int open;
  105. dma_addr_t in_dma;
  106. unsigned char *in_buffer;
  107. /* variables used to parse messages from remote. */
  108. struct bit_tester data;
  109. int stage;
  110. int toggle;
  111. };
  112. static struct usb_driver keyspan_driver;
  113. /*
  114. * Debug routine that prints out what we've received from the remote.
  115. */
  116. static void keyspan_print(struct usb_keyspan* dev) /*unsigned char* data)*/
  117. {
  118. char codes[4 * RECV_SIZE];
  119. int i;
  120. for (i = 0; i < RECV_SIZE; i++)
  121. snprintf(codes + i * 3, 4, "%02x ", dev->in_buffer[i]);
  122. dev_info(&dev->udev->dev, "%s\n", codes);
  123. }
  124. /*
  125. * Routine that manages the bit_tester structure. It makes sure that there are
  126. * at least bits_needed bits loaded into the tester.
  127. */
  128. static int keyspan_load_tester(struct usb_keyspan* dev, int bits_needed)
  129. {
  130. if (dev->data.bits_left >= bits_needed)
  131. return 0;
  132. /*
  133. * Somehow we've missed the last message. The message will be repeated
  134. * though so it's not too big a deal
  135. */
  136. if (dev->data.pos >= dev->data.len) {
  137. dev_dbg(&dev->interface->dev,
  138. "%s - Error ran out of data. pos: %d, len: %d\n",
  139. __func__, dev->data.pos, dev->data.len);
  140. return -1;
  141. }
  142. /* Load as much as we can into the tester. */
  143. while ((dev->data.bits_left + 7 < (sizeof(dev->data.tester) * 8)) &&
  144. (dev->data.pos < dev->data.len)) {
  145. dev->data.tester += (dev->data.buffer[dev->data.pos++] << dev->data.bits_left);
  146. dev->data.bits_left += 8;
  147. }
  148. return 0;
  149. }
  150. static void keyspan_report_button(struct usb_keyspan *remote, int button, int press)
  151. {
  152. struct input_dev *input = remote->input;
  153. input_event(input, EV_MSC, MSC_SCAN, button);
  154. input_report_key(input, remote->keymap[button], press);
  155. input_sync(input);
  156. }
  157. /*
  158. * Routine that handles all the logic needed to parse out the message from the remote.
  159. */
  160. static void keyspan_check_data(struct usb_keyspan *remote)
  161. {
  162. int i;
  163. int found = 0;
  164. struct keyspan_message message;
  165. switch(remote->stage) {
  166. case 0:
  167. /*
  168. * In stage 0 we want to find the start of a message. The remote sends a 0xFF as filler.
  169. * So the first byte that isn't a FF should be the start of a new message.
  170. */
  171. for (i = 0; i < RECV_SIZE && remote->in_buffer[i] == GAP; ++i);
  172. if (i < RECV_SIZE) {
  173. memcpy(remote->data.buffer, remote->in_buffer, RECV_SIZE);
  174. remote->data.len = RECV_SIZE;
  175. remote->data.pos = 0;
  176. remote->data.tester = 0;
  177. remote->data.bits_left = 0;
  178. remote->stage = 1;
  179. }
  180. break;
  181. case 1:
  182. /*
  183. * Stage 1 we should have 16 bytes and should be able to detect a
  184. * SYNC. The SYNC is 14 bits, 7 0's and then 7 1's.
  185. */
  186. memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
  187. remote->data.len += RECV_SIZE;
  188. found = 0;
  189. while ((remote->data.bits_left >= 14 || remote->data.pos < remote->data.len) && !found) {
  190. for (i = 0; i < 8; ++i) {
  191. if (keyspan_load_tester(remote, 14) != 0) {
  192. remote->stage = 0;
  193. return;
  194. }
  195. if ((remote->data.tester & SYNC_MASK) == SYNC) {
  196. remote->data.tester = remote->data.tester >> 14;
  197. remote->data.bits_left -= 14;
  198. found = 1;
  199. break;
  200. } else {
  201. remote->data.tester = remote->data.tester >> 1;
  202. --remote->data.bits_left;
  203. }
  204. }
  205. }
  206. if (!found) {
  207. remote->stage = 0;
  208. remote->data.len = 0;
  209. } else {
  210. remote->stage = 2;
  211. }
  212. break;
  213. case 2:
  214. /*
  215. * Stage 2 we should have 24 bytes which will be enough for a full
  216. * message. We need to parse out the system code, button code,
  217. * toggle code, and stop.
  218. */
  219. memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
  220. remote->data.len += RECV_SIZE;
  221. message.system = 0;
  222. for (i = 0; i < 9; i++) {
  223. keyspan_load_tester(remote, 6);
  224. if ((remote->data.tester & ZERO_MASK) == ZERO) {
  225. message.system = message.system << 1;
  226. remote->data.tester = remote->data.tester >> 5;
  227. remote->data.bits_left -= 5;
  228. } else if ((remote->data.tester & ONE_MASK) == ONE) {
  229. message.system = (message.system << 1) + 1;
  230. remote->data.tester = remote->data.tester >> 6;
  231. remote->data.bits_left -= 6;
  232. } else {
  233. dev_err(&remote->interface->dev,
  234. "%s - Unknown sequence found in system data.\n",
  235. __func__);
  236. remote->stage = 0;
  237. return;
  238. }
  239. }
  240. message.button = 0;
  241. for (i = 0; i < 5; i++) {
  242. keyspan_load_tester(remote, 6);
  243. if ((remote->data.tester & ZERO_MASK) == ZERO) {
  244. message.button = message.button << 1;
  245. remote->data.tester = remote->data.tester >> 5;
  246. remote->data.bits_left -= 5;
  247. } else if ((remote->data.tester & ONE_MASK) == ONE) {
  248. message.button = (message.button << 1) + 1;
  249. remote->data.tester = remote->data.tester >> 6;
  250. remote->data.bits_left -= 6;
  251. } else {
  252. dev_err(&remote->interface->dev,
  253. "%s - Unknown sequence found in button data.\n",
  254. __func__);
  255. remote->stage = 0;
  256. return;
  257. }
  258. }
  259. keyspan_load_tester(remote, 6);
  260. if ((remote->data.tester & ZERO_MASK) == ZERO) {
  261. message.toggle = 0;
  262. remote->data.tester = remote->data.tester >> 5;
  263. remote->data.bits_left -= 5;
  264. } else if ((remote->data.tester & ONE_MASK) == ONE) {
  265. message.toggle = 1;
  266. remote->data.tester = remote->data.tester >> 6;
  267. remote->data.bits_left -= 6;
  268. } else {
  269. dev_err(&remote->interface->dev,
  270. "%s - Error in message, invalid toggle.\n",
  271. __func__);
  272. remote->stage = 0;
  273. return;
  274. }
  275. keyspan_load_tester(remote, 5);
  276. if ((remote->data.tester & STOP_MASK) == STOP) {
  277. remote->data.tester = remote->data.tester >> 5;
  278. remote->data.bits_left -= 5;
  279. } else {
  280. dev_err(&remote->interface->dev,
  281. "Bad message received, no stop bit found.\n");
  282. }
  283. dev_dbg(&remote->interface->dev,
  284. "%s found valid message: system: %d, button: %d, toggle: %d\n",
  285. __func__, message.system, message.button, message.toggle);
  286. if (message.toggle != remote->toggle) {
  287. keyspan_report_button(remote, message.button, 1);
  288. keyspan_report_button(remote, message.button, 0);
  289. remote->toggle = message.toggle;
  290. }
  291. remote->stage = 0;
  292. break;
  293. }
  294. }
  295. /*
  296. * Routine for sending all the initialization messages to the remote.
  297. */
  298. static int keyspan_setup(struct usb_device* dev)
  299. {
  300. int retval = 0;
  301. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  302. 0x11, 0x40, 0x5601, 0x0, NULL, 0,
  303. USB_CTRL_SET_TIMEOUT);
  304. if (retval) {
  305. dev_dbg(&dev->dev, "%s - failed to set bit rate due to error: %d\n",
  306. __func__, retval);
  307. return(retval);
  308. }
  309. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  310. 0x44, 0x40, 0x0, 0x0, NULL, 0,
  311. USB_CTRL_SET_TIMEOUT);
  312. if (retval) {
  313. dev_dbg(&dev->dev, "%s - failed to set resume sensitivity due to error: %d\n",
  314. __func__, retval);
  315. return(retval);
  316. }
  317. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  318. 0x22, 0x40, 0x0, 0x0, NULL, 0,
  319. USB_CTRL_SET_TIMEOUT);
  320. if (retval) {
  321. dev_dbg(&dev->dev, "%s - failed to turn receive on due to error: %d\n",
  322. __func__, retval);
  323. return(retval);
  324. }
  325. dev_dbg(&dev->dev, "%s - Setup complete.\n", __func__);
  326. return(retval);
  327. }
  328. /*
  329. * Routine used to handle a new message that has come in.
  330. */
  331. static void keyspan_irq_recv(struct urb *urb)
  332. {
  333. struct usb_keyspan *dev = urb->context;
  334. int retval;
  335. /* Check our status in case we need to bail out early. */
  336. switch (urb->status) {
  337. case 0:
  338. break;
  339. /* Device went away so don't keep trying to read from it. */
  340. case -ECONNRESET:
  341. case -ENOENT:
  342. case -ESHUTDOWN:
  343. return;
  344. default:
  345. goto resubmit;
  346. }
  347. if (debug)
  348. keyspan_print(dev);
  349. keyspan_check_data(dev);
  350. resubmit:
  351. retval = usb_submit_urb(urb, GFP_ATOMIC);
  352. if (retval)
  353. dev_err(&dev->interface->dev,
  354. "%s - usb_submit_urb failed with result: %d\n",
  355. __func__, retval);
  356. }
  357. static int keyspan_open(struct input_dev *dev)
  358. {
  359. struct usb_keyspan *remote = input_get_drvdata(dev);
  360. remote->irq_urb->dev = remote->udev;
  361. if (usb_submit_urb(remote->irq_urb, GFP_KERNEL))
  362. return -EIO;
  363. return 0;
  364. }
  365. static void keyspan_close(struct input_dev *dev)
  366. {
  367. struct usb_keyspan *remote = input_get_drvdata(dev);
  368. usb_kill_urb(remote->irq_urb);
  369. }
  370. static struct usb_endpoint_descriptor *keyspan_get_in_endpoint(struct usb_host_interface *iface)
  371. {
  372. struct usb_endpoint_descriptor *endpoint;
  373. int i;
  374. for (i = 0; i < iface->desc.bNumEndpoints; ++i) {
  375. endpoint = &iface->endpoint[i].desc;
  376. if (usb_endpoint_is_int_in(endpoint)) {
  377. /* we found our interrupt in endpoint */
  378. return endpoint;
  379. }
  380. }
  381. return NULL;
  382. }
  383. /*
  384. * Routine that sets up the driver to handle a specific USB device detected on the bus.
  385. */
  386. static int keyspan_probe(struct usb_interface *interface, const struct usb_device_id *id)
  387. {
  388. struct usb_device *udev = interface_to_usbdev(interface);
  389. struct usb_endpoint_descriptor *endpoint;
  390. struct usb_keyspan *remote;
  391. struct input_dev *input_dev;
  392. int i, error;
  393. endpoint = keyspan_get_in_endpoint(interface->cur_altsetting);
  394. if (!endpoint)
  395. return -ENODEV;
  396. remote = kzalloc(sizeof(*remote), GFP_KERNEL);
  397. input_dev = input_allocate_device();
  398. if (!remote || !input_dev) {
  399. error = -ENOMEM;
  400. goto fail1;
  401. }
  402. remote->udev = udev;
  403. remote->input = input_dev;
  404. remote->interface = interface;
  405. remote->in_endpoint = endpoint;
  406. remote->toggle = -1; /* Set to -1 so we will always not match the toggle from the first remote message. */
  407. remote->in_buffer = usb_alloc_coherent(udev, RECV_SIZE, GFP_KERNEL, &remote->in_dma);
  408. if (!remote->in_buffer) {
  409. error = -ENOMEM;
  410. goto fail1;
  411. }
  412. remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  413. if (!remote->irq_urb) {
  414. error = -ENOMEM;
  415. goto fail2;
  416. }
  417. error = keyspan_setup(udev);
  418. if (error) {
  419. error = -ENODEV;
  420. goto fail3;
  421. }
  422. if (udev->manufacturer)
  423. strlcpy(remote->name, udev->manufacturer, sizeof(remote->name));
  424. if (udev->product) {
  425. if (udev->manufacturer)
  426. strlcat(remote->name, " ", sizeof(remote->name));
  427. strlcat(remote->name, udev->product, sizeof(remote->name));
  428. }
  429. if (!strlen(remote->name))
  430. snprintf(remote->name, sizeof(remote->name),
  431. "USB Keyspan Remote %04x:%04x",
  432. le16_to_cpu(udev->descriptor.idVendor),
  433. le16_to_cpu(udev->descriptor.idProduct));
  434. usb_make_path(udev, remote->phys, sizeof(remote->phys));
  435. strlcat(remote->phys, "/input0", sizeof(remote->phys));
  436. memcpy(remote->keymap, keyspan_key_table, sizeof(remote->keymap));
  437. input_dev->name = remote->name;
  438. input_dev->phys = remote->phys;
  439. usb_to_input_id(udev, &input_dev->id);
  440. input_dev->dev.parent = &interface->dev;
  441. input_dev->keycode = remote->keymap;
  442. input_dev->keycodesize = sizeof(unsigned short);
  443. input_dev->keycodemax = ARRAY_SIZE(remote->keymap);
  444. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  445. __set_bit(EV_KEY, input_dev->evbit);
  446. for (i = 0; i < ARRAY_SIZE(keyspan_key_table); i++)
  447. __set_bit(keyspan_key_table[i], input_dev->keybit);
  448. __clear_bit(KEY_RESERVED, input_dev->keybit);
  449. input_set_drvdata(input_dev, remote);
  450. input_dev->open = keyspan_open;
  451. input_dev->close = keyspan_close;
  452. /*
  453. * Initialize the URB to access the device.
  454. * The urb gets sent to the device in keyspan_open()
  455. */
  456. usb_fill_int_urb(remote->irq_urb,
  457. remote->udev,
  458. usb_rcvintpipe(remote->udev, endpoint->bEndpointAddress),
  459. remote->in_buffer, RECV_SIZE, keyspan_irq_recv, remote,
  460. endpoint->bInterval);
  461. remote->irq_urb->transfer_dma = remote->in_dma;
  462. remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  463. /* we can register the device now, as it is ready */
  464. error = input_register_device(remote->input);
  465. if (error)
  466. goto fail3;
  467. /* save our data pointer in this interface device */
  468. usb_set_intfdata(interface, remote);
  469. return 0;
  470. fail3: usb_free_urb(remote->irq_urb);
  471. fail2: usb_free_coherent(udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
  472. fail1: kfree(remote);
  473. input_free_device(input_dev);
  474. return error;
  475. }
  476. /*
  477. * Routine called when a device is disconnected from the USB.
  478. */
  479. static void keyspan_disconnect(struct usb_interface *interface)
  480. {
  481. struct usb_keyspan *remote;
  482. remote = usb_get_intfdata(interface);
  483. usb_set_intfdata(interface, NULL);
  484. if (remote) { /* We have a valid driver structure so clean up everything we allocated. */
  485. input_unregister_device(remote->input);
  486. usb_kill_urb(remote->irq_urb);
  487. usb_free_urb(remote->irq_urb);
  488. usb_free_coherent(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
  489. kfree(remote);
  490. }
  491. }
  492. /*
  493. * Standard driver set up sections
  494. */
  495. static struct usb_driver keyspan_driver =
  496. {
  497. .name = "keyspan_remote",
  498. .probe = keyspan_probe,
  499. .disconnect = keyspan_disconnect,
  500. .id_table = keyspan_table
  501. };
  502. module_usb_driver(keyspan_driver);
  503. MODULE_DEVICE_TABLE(usb, keyspan_table);
  504. MODULE_AUTHOR("Michael Downey <downey@zymeta.com>");
  505. MODULE_DESCRIPTION("Driver for the USB Keyspan remote control.");
  506. MODULE_LICENSE("GPL");