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/config.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/input.h>
22 #include <linux/usb.h>
24 #define DRIVER_VERSION "v0.1"
25 #define DRIVER_AUTHOR "Michael Downey <downey@zymeta.com>"
26 #define DRIVER_DESC "Driver for the USB Keyspan remote control."
27 #define DRIVER_LICENSE "GPL"
29 /* Parameters that can be passed to the driver. */
31 module_param(debug, int, 0444);
32 MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
34 /* Vendor and product ids */
35 #define USB_KEYSPAN_VENDOR_ID 0x06CD
36 #define USB_KEYSPAN_PRODUCT_UIA11 0x0202
38 /* Defines for converting the data from the remote. */
40 #define ZERO_MASK 0x1F /* 5 bits for a 0 */
42 #define ONE_MASK 0x3F /* 6 bits for a 1 */
44 #define SYNC_MASK 0x3FFF /* 14 bits for a SYNC sequence */
46 #define STOP_MASK 0x1F /* 5 bits for the STOP sequence */
49 #define RECV_SIZE 8 /* The UIA-11 type have a 8 byte limit. */
51 /* table of devices that work with this driver */
52 static struct usb_device_id keyspan_table[] = {
53 { USB_DEVICE(USB_KEYSPAN_VENDOR_ID, USB_KEYSPAN_PRODUCT_UIA11) },
54 { } /* Terminating entry */
57 /* Structure to store all the real stuff that a remote sends to us. */
58 struct keyspan_message {
64 /* Structure used for all the bit testing magic needed to be done. */
73 /* Structure to hold all of our driver specific stuff */
77 struct usb_device* udev;
78 struct input_dev input;
79 struct usb_interface* interface;
80 struct usb_endpoint_descriptor* in_endpoint;
84 unsigned char* in_buffer;
86 /* variables used to parse messages from remote. */
87 struct bit_tester data;
93 * Table that maps the 31 possible keycodes to input keys.
94 * Currently there are 15 and 17 button models so RESERVED codes
95 * are blank areas in the mapping.
97 static int keyspan_key_table[] = {
98 KEY_RESERVED, /* 0 is just a place holder. */
132 static struct usb_driver keyspan_driver;
135 * Debug routine that prints out what we've received from the remote.
137 static void keyspan_print(struct usb_keyspan* dev) /*unsigned char* data)*/
139 char codes[4*RECV_SIZE];
142 for (i = 0; i < RECV_SIZE; i++) {
143 snprintf(codes+i*3, 4, "%02x ", dev->in_buffer[i]);
146 dev_info(&dev->udev->dev, "%s\n", codes);
150 * Routine that manages the bit_tester structure. It makes sure that there are
151 * at least bits_needed bits loaded into the tester.
153 static int keyspan_load_tester(struct usb_keyspan* dev, int bits_needed)
155 if (dev->data.bits_left >= bits_needed)
159 * Somehow we've missed the last message. The message will be repeated
160 * though so it's not too big a deal
162 if (dev->data.pos >= dev->data.len) {
163 dev_dbg(&dev->udev, "%s - Error ran out of data. pos: %d, len: %d\n",
164 __FUNCTION__, dev->data.pos, dev->data.len);
168 /* Load as much as we can into the tester. */
169 while ((dev->data.bits_left + 7 < (sizeof(dev->data.tester) * 8)) &&
170 (dev->data.pos < dev->data.len)) {
171 dev->data.tester += (dev->data.buffer[dev->data.pos++] << dev->data.bits_left);
172 dev->data.bits_left += 8;
179 * Routine that handles all the logic needed to parse out the message from the remote.
181 static void keyspan_check_data(struct usb_keyspan *remote, struct pt_regs *regs)
185 struct keyspan_message message;
187 switch(remote->stage) {
190 * In stage 0 we want to find the start of a message. The remote sends a 0xFF as filler.
191 * So the first byte that isn't a FF should be the start of a new message.
193 for (i = 0; i < RECV_SIZE && remote->in_buffer[i] == GAP; ++i);
196 memcpy(remote->data.buffer, remote->in_buffer, RECV_SIZE);
197 remote->data.len = RECV_SIZE;
198 remote->data.pos = 0;
199 remote->data.tester = 0;
200 remote->data.bits_left = 0;
207 * Stage 1 we should have 16 bytes and should be able to detect a
208 * SYNC. The SYNC is 14 bits, 7 0's and then 7 1's.
210 memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
211 remote->data.len += RECV_SIZE;
214 while ((remote->data.bits_left >= 14 || remote->data.pos < remote->data.len) && !found) {
215 for (i = 0; i < 8; ++i) {
216 if (keyspan_load_tester(remote, 14) != 0) {
221 if ((remote->data.tester & SYNC_MASK) == SYNC) {
222 remote->data.tester = remote->data.tester >> 14;
223 remote->data.bits_left -= 14;
227 remote->data.tester = remote->data.tester >> 1;
228 --remote->data.bits_left;
235 remote->data.len = 0;
243 * Stage 2 we should have 24 bytes which will be enough for a full
244 * message. We need to parse out the system code, button code,
245 * toggle code, and stop.
247 memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
248 remote->data.len += RECV_SIZE;
251 for (i = 0; i < 9; i++) {
252 keyspan_load_tester(remote, 6);
254 if ((remote->data.tester & ZERO_MASK) == ZERO) {
255 message.system = message.system << 1;
256 remote->data.tester = remote->data.tester >> 5;
257 remote->data.bits_left -= 5;
258 } else if ((remote->data.tester & ONE_MASK) == ONE) {
259 message.system = (message.system << 1) + 1;
260 remote->data.tester = remote->data.tester >> 6;
261 remote->data.bits_left -= 6;
263 err("%s - Unknown sequence found in system data.\n", __FUNCTION__);
270 for (i = 0; i < 5; i++) {
271 keyspan_load_tester(remote, 6);
273 if ((remote->data.tester & ZERO_MASK) == ZERO) {
274 message.button = message.button << 1;
275 remote->data.tester = remote->data.tester >> 5;
276 remote->data.bits_left -= 5;
277 } else if ((remote->data.tester & ONE_MASK) == ONE) {
278 message.button = (message.button << 1) + 1;
279 remote->data.tester = remote->data.tester >> 6;
280 remote->data.bits_left -= 6;
282 err("%s - Unknown sequence found in button data.\n", __FUNCTION__);
288 keyspan_load_tester(remote, 6);
289 if ((remote->data.tester & ZERO_MASK) == ZERO) {
291 remote->data.tester = remote->data.tester >> 5;
292 remote->data.bits_left -= 5;
293 } else if ((remote->data.tester & ONE_MASK) == ONE) {
295 remote->data.tester = remote->data.tester >> 6;
296 remote->data.bits_left -= 6;
298 err("%s - Error in message, invalid toggle.\n", __FUNCTION__);
301 keyspan_load_tester(remote, 5);
302 if ((remote->data.tester & STOP_MASK) == STOP) {
303 remote->data.tester = remote->data.tester >> 5;
304 remote->data.bits_left -= 5;
306 err("Bad message recieved, no stop bit found.\n");
309 dev_dbg(&remote->udev,
310 "%s found valid message: system: %d, button: %d, toggle: %d\n",
311 __FUNCTION__, message.system, message.button, message.toggle);
313 if (message.toggle != remote->toggle) {
314 input_regs(&remote->input, regs);
315 input_report_key(&remote->input, keyspan_key_table[message.button], 1);
316 input_report_key(&remote->input, keyspan_key_table[message.button], 0);
317 input_sync(&remote->input);
318 remote->toggle = message.toggle;
327 * Routine for sending all the initialization messages to the remote.
329 static int keyspan_setup(struct usb_device* dev)
333 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
334 0x11, 0x40, 0x5601, 0x0, NULL, 0, 0);
336 dev_dbg(&dev->dev, "%s - failed to set bit rate due to error: %d\n",
337 __FUNCTION__, retval);
341 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
342 0x44, 0x40, 0x0, 0x0, NULL, 0, 0);
344 dev_dbg(&dev->dev, "%s - failed to set resume sensitivity due to error: %d\n",
345 __FUNCTION__, retval);
349 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
350 0x22, 0x40, 0x0, 0x0, NULL, 0, 0);
352 dev_dbg(&dev->dev, "%s - failed to turn receive on due to error: %d\n",
353 __FUNCTION__, retval);
357 dev_dbg(&dev->dev, "%s - Setup complete.\n", __FUNCTION__);
362 * Routine used to handle a new message that has come in.
364 static void keyspan_irq_recv(struct urb *urb, struct pt_regs *regs)
366 struct usb_keyspan *dev = urb->context;
369 /* Check our status in case we need to bail out early. */
370 switch (urb->status) {
374 /* Device went away so don't keep trying to read from it. */
388 keyspan_check_data(dev, regs);
391 retval = usb_submit_urb(urb, GFP_ATOMIC);
393 err ("%s - usb_submit_urb failed with result: %d", __FUNCTION__, retval);
396 static int keyspan_open(struct input_dev *dev)
398 struct usb_keyspan *remote = dev->private;
403 remote->irq_urb->dev = remote->udev;
404 if (usb_submit_urb(remote->irq_urb, GFP_KERNEL)) {
412 static void keyspan_close(struct input_dev *dev)
414 struct usb_keyspan *remote = dev->private;
417 usb_kill_urb(remote->irq_urb);
421 * Routine that sets up the driver to handle a specific USB device detected on the bus.
423 static int keyspan_probe(struct usb_interface *interface, const struct usb_device_id *id)
426 int retval = -ENOMEM;
429 struct usb_keyspan *remote = NULL;
430 struct usb_host_interface *iface_desc;
431 struct usb_endpoint_descriptor *endpoint;
432 struct usb_device *udev = usb_get_dev(interface_to_usbdev(interface));
434 /* allocate memory for our device state and initialize it */
435 remote = kmalloc(sizeof(*remote), GFP_KERNEL);
436 if (remote == NULL) {
437 err("Out of memory\n");
440 memset(remote, 0x00, sizeof(*remote));
443 remote->interface = interface;
444 remote->toggle = -1; /* Set to -1 so we will always not match the toggle from the first remote message. */
446 /* set up the endpoint information */
447 /* use only the first in interrupt endpoint */
448 iface_desc = interface->cur_altsetting;
449 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
450 endpoint = &iface_desc->endpoint[i].desc;
452 if (!remote->in_endpoint &&
453 (endpoint->bEndpointAddress & USB_DIR_IN) &&
454 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
455 /* we found our interrupt in endpoint */
456 remote->in_endpoint = endpoint;
458 remote->in_buffer = usb_buffer_alloc(remote->udev, RECV_SIZE, SLAB_ATOMIC, &remote->in_dma);
459 if (!remote->in_buffer) {
466 if (!remote->in_endpoint) {
467 err("Could not find interrupt input endpoint.\n");
472 remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
473 if (!remote->irq_urb) {
474 err("Failed to allocate urb.\n");
479 retval = keyspan_setup(remote->udev);
481 err("Failed to setup device.\n");
487 * Setup the input system with the bits we are going to be reporting
489 remote->input.evbit[0] = BIT(EV_KEY); /* We will only report KEY events. */
490 for (i = 0; i < 32; ++i) {
491 if (keyspan_key_table[i] != KEY_RESERVED) {
492 set_bit(keyspan_key_table[i], remote->input.keybit);
496 remote->input.private = remote;
497 remote->input.open = keyspan_open;
498 remote->input.close = keyspan_close;
500 usb_make_path(remote->udev, path, 64);
501 sprintf(remote->phys, "%s/input0", path);
503 remote->input.name = remote->name;
504 remote->input.phys = remote->phys;
505 remote->input.id.bustype = BUS_USB;
506 remote->input.id.vendor = le16_to_cpu(remote->udev->descriptor.idVendor);
507 remote->input.id.product = le16_to_cpu(remote->udev->descriptor.idProduct);
508 remote->input.id.version = le16_to_cpu(remote->udev->descriptor.bcdDevice);
510 if (!(buf = kmalloc(63, GFP_KERNEL))) {
511 usb_buffer_free(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
516 if (remote->udev->descriptor.iManufacturer &&
517 usb_string(remote->udev, remote->udev->descriptor.iManufacturer, buf, 63) > 0)
518 strcat(remote->name, buf);
520 if (remote->udev->descriptor.iProduct &&
521 usb_string(remote->udev, remote->udev->descriptor.iProduct, buf, 63) > 0)
522 sprintf(remote->name, "%s %s", remote->name, buf);
524 if (!strlen(remote->name))
525 sprintf(remote->name, "USB Keyspan Remote %04x:%04x",
526 remote->input.id.vendor, remote->input.id.product);
531 * Initialize the URB to access the device. The urb gets sent to the device in keyspan_open()
533 usb_fill_int_urb(remote->irq_urb,
534 remote->udev, usb_rcvintpipe(remote->udev, remote->in_endpoint->bEndpointAddress),
535 remote->in_buffer, RECV_SIZE, keyspan_irq_recv, remote,
536 remote->in_endpoint->bInterval);
537 remote->irq_urb->transfer_dma = remote->in_dma;
538 remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
540 /* we can register the device now, as it is ready */
541 input_register_device(&remote->input);
543 /* save our data pointer in this interface device */
544 usb_set_intfdata(interface, remote);
546 /* let the user know what node this device is now attached to */
547 info("connected: %s on %s", remote->name, path);
552 * In case of error we need to clean up any allocated buffers
555 usb_free_urb(remote->irq_urb);
557 if (remote->in_buffer)
558 usb_buffer_free(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
567 * Routine called when a device is disconnected from the USB.
569 static void keyspan_disconnect(struct usb_interface *interface)
571 struct usb_keyspan *remote;
573 /* prevent keyspan_open() from racing keyspan_disconnect() */
576 remote = usb_get_intfdata(interface);
577 usb_set_intfdata(interface, NULL);
579 if (remote) { /* We have a valid driver structure so clean up everything we allocated. */
580 input_unregister_device(&remote->input);
581 usb_kill_urb(remote->irq_urb);
582 usb_free_urb(remote->irq_urb);
583 usb_buffer_free(interface_to_usbdev(interface), RECV_SIZE, remote->in_buffer, remote->in_dma);
589 info("USB Keyspan now disconnected");
593 * Standard driver set up sections
595 static struct usb_driver keyspan_driver =
597 .owner = THIS_MODULE,
598 .name = "keyspan_remote",
599 .probe = keyspan_probe,
600 .disconnect = keyspan_disconnect,
601 .id_table = keyspan_table
604 static int __init usb_keyspan_init(void)
608 /* register this driver with the USB subsystem */
609 result = usb_register(&keyspan_driver);
611 err("usb_register failed. Error number %d\n", result);
616 static void __exit usb_keyspan_exit(void)
618 /* deregister this driver with the USB subsystem */
619 usb_deregister(&keyspan_driver);
622 module_init(usb_keyspan_init);
623 module_exit(usb_keyspan_exit);
625 MODULE_DEVICE_TABLE(usb, keyspan_table);
626 MODULE_AUTHOR(DRIVER_AUTHOR);
627 MODULE_DESCRIPTION(DRIVER_DESC);
628 MODULE_LICENSE(DRIVER_LICENSE);