2 * keyspan_remote: USB driver for the Keyspan DMR
4 * Copyright (C) 2005 Zymeta Corporation - Michael Downey (downey@zymeta.com)
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.
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.
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/usb/input.h>
21 #define DRIVER_VERSION "v0.1"
22 #define DRIVER_AUTHOR "Michael Downey <downey@zymeta.com>"
23 #define DRIVER_DESC "Driver for the USB Keyspan remote control."
24 #define DRIVER_LICENSE "GPL"
26 /* Parameters that can be passed to the driver. */
28 module_param(debug, int, 0444);
29 MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
31 /* Vendor and product ids */
32 #define USB_KEYSPAN_VENDOR_ID 0x06CD
33 #define USB_KEYSPAN_PRODUCT_UIA11 0x0202
35 /* Defines for converting the data from the remote. */
37 #define ZERO_MASK 0x1F /* 5 bits for a 0 */
39 #define ONE_MASK 0x3F /* 6 bits for a 1 */
41 #define SYNC_MASK 0x3FFF /* 14 bits for a SYNC sequence */
43 #define STOP_MASK 0x1F /* 5 bits for the STOP sequence */
46 #define RECV_SIZE 8 /* The UIA-11 type have a 8 byte limit. */
49 * Table that maps the 31 possible keycodes to input keys.
50 * Currently there are 15 and 17 button models so RESERVED codes
51 * are blank areas in the mapping.
53 static const unsigned short keyspan_key_table[] = {
54 KEY_RESERVED, /* 0 is just a place holder. */
88 /* table of devices that work with this driver */
89 static struct usb_device_id keyspan_table[] = {
90 { USB_DEVICE(USB_KEYSPAN_VENDOR_ID, USB_KEYSPAN_PRODUCT_UIA11) },
91 { } /* Terminating entry */
94 /* Structure to store all the real stuff that a remote sends to us. */
95 struct keyspan_message {
101 /* Structure used for all the bit testing magic needed to be done. */
110 /* Structure to hold all of our driver specific stuff */
114 unsigned short keymap[ARRAY_SIZE(keyspan_key_table)];
115 struct usb_device *udev;
116 struct input_dev *input;
117 struct usb_interface *interface;
118 struct usb_endpoint_descriptor *in_endpoint;
122 unsigned char *in_buffer;
124 /* variables used to parse messages from remote. */
125 struct bit_tester data;
130 static struct usb_driver keyspan_driver;
133 * Debug routine that prints out what we've received from the remote.
135 static void keyspan_print(struct usb_keyspan* dev) /*unsigned char* data)*/
137 char codes[4 * RECV_SIZE];
140 for (i = 0; i < RECV_SIZE; i++)
141 snprintf(codes + i * 3, 4, "%02x ", dev->in_buffer[i]);
143 dev_info(&dev->udev->dev, "%s\n", codes);
147 * Routine that manages the bit_tester structure. It makes sure that there are
148 * at least bits_needed bits loaded into the tester.
150 static int keyspan_load_tester(struct usb_keyspan* dev, int bits_needed)
152 if (dev->data.bits_left >= bits_needed)
156 * Somehow we've missed the last message. The message will be repeated
157 * though so it's not too big a deal
159 if (dev->data.pos >= dev->data.len) {
160 dev_dbg(&dev->interface->dev,
161 "%s - Error ran out of data. pos: %d, len: %d\n",
162 __func__, dev->data.pos, dev->data.len);
166 /* Load as much as we can into the tester. */
167 while ((dev->data.bits_left + 7 < (sizeof(dev->data.tester) * 8)) &&
168 (dev->data.pos < dev->data.len)) {
169 dev->data.tester += (dev->data.buffer[dev->data.pos++] << dev->data.bits_left);
170 dev->data.bits_left += 8;
176 static void keyspan_report_button(struct usb_keyspan *remote, int button, int press)
178 struct input_dev *input = remote->input;
180 input_event(input, EV_MSC, MSC_SCAN, button);
181 input_report_key(input, remote->keymap[button], press);
186 * Routine that handles all the logic needed to parse out the message from the remote.
188 static void keyspan_check_data(struct usb_keyspan *remote)
192 struct keyspan_message message;
194 switch(remote->stage) {
197 * In stage 0 we want to find the start of a message. The remote sends a 0xFF as filler.
198 * So the first byte that isn't a FF should be the start of a new message.
200 for (i = 0; i < RECV_SIZE && remote->in_buffer[i] == GAP; ++i);
203 memcpy(remote->data.buffer, remote->in_buffer, RECV_SIZE);
204 remote->data.len = RECV_SIZE;
205 remote->data.pos = 0;
206 remote->data.tester = 0;
207 remote->data.bits_left = 0;
214 * Stage 1 we should have 16 bytes and should be able to detect a
215 * SYNC. The SYNC is 14 bits, 7 0's and then 7 1's.
217 memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
218 remote->data.len += RECV_SIZE;
221 while ((remote->data.bits_left >= 14 || remote->data.pos < remote->data.len) && !found) {
222 for (i = 0; i < 8; ++i) {
223 if (keyspan_load_tester(remote, 14) != 0) {
228 if ((remote->data.tester & SYNC_MASK) == SYNC) {
229 remote->data.tester = remote->data.tester >> 14;
230 remote->data.bits_left -= 14;
234 remote->data.tester = remote->data.tester >> 1;
235 --remote->data.bits_left;
242 remote->data.len = 0;
250 * Stage 2 we should have 24 bytes which will be enough for a full
251 * message. We need to parse out the system code, button code,
252 * toggle code, and stop.
254 memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
255 remote->data.len += RECV_SIZE;
258 for (i = 0; i < 9; i++) {
259 keyspan_load_tester(remote, 6);
261 if ((remote->data.tester & ZERO_MASK) == ZERO) {
262 message.system = message.system << 1;
263 remote->data.tester = remote->data.tester >> 5;
264 remote->data.bits_left -= 5;
265 } else if ((remote->data.tester & ONE_MASK) == ONE) {
266 message.system = (message.system << 1) + 1;
267 remote->data.tester = remote->data.tester >> 6;
268 remote->data.bits_left -= 6;
270 dev_err(&remote->interface->dev,
271 "%s - Unknown sequence found in system data.\n",
279 for (i = 0; i < 5; i++) {
280 keyspan_load_tester(remote, 6);
282 if ((remote->data.tester & ZERO_MASK) == ZERO) {
283 message.button = message.button << 1;
284 remote->data.tester = remote->data.tester >> 5;
285 remote->data.bits_left -= 5;
286 } else if ((remote->data.tester & ONE_MASK) == ONE) {
287 message.button = (message.button << 1) + 1;
288 remote->data.tester = remote->data.tester >> 6;
289 remote->data.bits_left -= 6;
291 dev_err(&remote->interface->dev,
292 "%s - Unknown sequence found in button data.\n",
299 keyspan_load_tester(remote, 6);
300 if ((remote->data.tester & ZERO_MASK) == ZERO) {
302 remote->data.tester = remote->data.tester >> 5;
303 remote->data.bits_left -= 5;
304 } else if ((remote->data.tester & ONE_MASK) == ONE) {
306 remote->data.tester = remote->data.tester >> 6;
307 remote->data.bits_left -= 6;
309 dev_err(&remote->interface->dev,
310 "%s - Error in message, invalid toggle.\n",
316 keyspan_load_tester(remote, 5);
317 if ((remote->data.tester & STOP_MASK) == STOP) {
318 remote->data.tester = remote->data.tester >> 5;
319 remote->data.bits_left -= 5;
321 dev_err(&remote->interface->dev,
322 "Bad message received, no stop bit found.\n");
325 dev_dbg(&remote->interface->dev,
326 "%s found valid message: system: %d, button: %d, toggle: %d\n",
327 __func__, message.system, message.button, message.toggle);
329 if (message.toggle != remote->toggle) {
330 keyspan_report_button(remote, message.button, 1);
331 keyspan_report_button(remote, message.button, 0);
332 remote->toggle = message.toggle;
341 * Routine for sending all the initialization messages to the remote.
343 static int keyspan_setup(struct usb_device* dev)
347 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
348 0x11, 0x40, 0x5601, 0x0, NULL, 0, 0);
350 dev_dbg(&dev->dev, "%s - failed to set bit rate due to error: %d\n",
355 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
356 0x44, 0x40, 0x0, 0x0, NULL, 0, 0);
358 dev_dbg(&dev->dev, "%s - failed to set resume sensitivity due to error: %d\n",
363 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
364 0x22, 0x40, 0x0, 0x0, NULL, 0, 0);
366 dev_dbg(&dev->dev, "%s - failed to turn receive on due to error: %d\n",
371 dev_dbg(&dev->dev, "%s - Setup complete.\n", __func__);
376 * Routine used to handle a new message that has come in.
378 static void keyspan_irq_recv(struct urb *urb)
380 struct usb_keyspan *dev = urb->context;
383 /* Check our status in case we need to bail out early. */
384 switch (urb->status) {
388 /* Device went away so don't keep trying to read from it. */
402 keyspan_check_data(dev);
405 retval = usb_submit_urb(urb, GFP_ATOMIC);
407 dev_err(&dev->interface->dev,
408 "%s - usb_submit_urb failed with result: %d\n",
412 static int keyspan_open(struct input_dev *dev)
414 struct usb_keyspan *remote = input_get_drvdata(dev);
416 remote->irq_urb->dev = remote->udev;
417 if (usb_submit_urb(remote->irq_urb, GFP_KERNEL))
423 static void keyspan_close(struct input_dev *dev)
425 struct usb_keyspan *remote = input_get_drvdata(dev);
427 usb_kill_urb(remote->irq_urb);
430 static struct usb_endpoint_descriptor *keyspan_get_in_endpoint(struct usb_host_interface *iface)
433 struct usb_endpoint_descriptor *endpoint;
436 for (i = 0; i < iface->desc.bNumEndpoints; ++i) {
437 endpoint = &iface->endpoint[i].desc;
439 if (usb_endpoint_is_int_in(endpoint)) {
440 /* we found our interrupt in endpoint */
449 * Routine that sets up the driver to handle a specific USB device detected on the bus.
451 static int keyspan_probe(struct usb_interface *interface, const struct usb_device_id *id)
453 struct usb_device *udev = interface_to_usbdev(interface);
454 struct usb_endpoint_descriptor *endpoint;
455 struct usb_keyspan *remote;
456 struct input_dev *input_dev;
459 endpoint = keyspan_get_in_endpoint(interface->cur_altsetting);
463 remote = kzalloc(sizeof(*remote), GFP_KERNEL);
464 input_dev = input_allocate_device();
465 if (!remote || !input_dev) {
471 remote->input = input_dev;
472 remote->interface = interface;
473 remote->in_endpoint = endpoint;
474 remote->toggle = -1; /* Set to -1 so we will always not match the toggle from the first remote message. */
476 remote->in_buffer = usb_alloc_coherent(udev, RECV_SIZE, GFP_ATOMIC, &remote->in_dma);
477 if (!remote->in_buffer) {
482 remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
483 if (!remote->irq_urb) {
488 error = keyspan_setup(udev);
494 if (udev->manufacturer)
495 strlcpy(remote->name, udev->manufacturer, sizeof(remote->name));
498 if (udev->manufacturer)
499 strlcat(remote->name, " ", sizeof(remote->name));
500 strlcat(remote->name, udev->product, sizeof(remote->name));
503 if (!strlen(remote->name))
504 snprintf(remote->name, sizeof(remote->name),
505 "USB Keyspan Remote %04x:%04x",
506 le16_to_cpu(udev->descriptor.idVendor),
507 le16_to_cpu(udev->descriptor.idProduct));
509 usb_make_path(udev, remote->phys, sizeof(remote->phys));
510 strlcat(remote->phys, "/input0", sizeof(remote->phys));
511 memcpy(remote->keymap, keyspan_key_table, sizeof(remote->keymap));
513 input_dev->name = remote->name;
514 input_dev->phys = remote->phys;
515 usb_to_input_id(udev, &input_dev->id);
516 input_dev->dev.parent = &interface->dev;
517 input_dev->keycode = remote->keymap;
518 input_dev->keycodesize = sizeof(unsigned short);
519 input_dev->keycodemax = ARRAY_SIZE(remote->keymap);
521 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
522 __set_bit(EV_KEY, input_dev->evbit);
523 for (i = 0; i < ARRAY_SIZE(keyspan_key_table); i++)
524 __set_bit(keyspan_key_table[i], input_dev->keybit);
525 __clear_bit(KEY_RESERVED, input_dev->keybit);
527 input_set_drvdata(input_dev, remote);
529 input_dev->open = keyspan_open;
530 input_dev->close = keyspan_close;
533 * Initialize the URB to access the device.
534 * The urb gets sent to the device in keyspan_open()
536 usb_fill_int_urb(remote->irq_urb,
538 usb_rcvintpipe(remote->udev, endpoint->bEndpointAddress),
539 remote->in_buffer, RECV_SIZE, keyspan_irq_recv, remote,
540 endpoint->bInterval);
541 remote->irq_urb->transfer_dma = remote->in_dma;
542 remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
544 /* we can register the device now, as it is ready */
545 error = input_register_device(remote->input);
549 /* save our data pointer in this interface device */
550 usb_set_intfdata(interface, remote);
554 fail3: usb_free_urb(remote->irq_urb);
555 fail2: usb_free_coherent(udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
556 fail1: kfree(remote);
557 input_free_device(input_dev);
563 * Routine called when a device is disconnected from the USB.
565 static void keyspan_disconnect(struct usb_interface *interface)
567 struct usb_keyspan *remote;
569 remote = usb_get_intfdata(interface);
570 usb_set_intfdata(interface, NULL);
572 if (remote) { /* We have a valid driver structure so clean up everything we allocated. */
573 input_unregister_device(remote->input);
574 usb_kill_urb(remote->irq_urb);
575 usb_free_urb(remote->irq_urb);
576 usb_free_coherent(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
582 * Standard driver set up sections
584 static struct usb_driver keyspan_driver =
586 .name = "keyspan_remote",
587 .probe = keyspan_probe,
588 .disconnect = keyspan_disconnect,
589 .id_table = keyspan_table
592 module_usb_driver(keyspan_driver);
594 MODULE_DEVICE_TABLE(usb, keyspan_table);
595 MODULE_AUTHOR(DRIVER_AUTHOR);
596 MODULE_DESCRIPTION(DRIVER_DESC);
597 MODULE_LICENSE(DRIVER_LICENSE);