keyspan_remote.c 15 KB

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