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>
23 #include <linux/usb_input.h>
25 #define DRIVER_VERSION "v0.1"
26 #define DRIVER_AUTHOR "Michael Downey <downey@zymeta.com>"
27 #define DRIVER_DESC "Driver for the USB Keyspan remote control."
28 #define DRIVER_LICENSE "GPL"
30 /* Parameters that can be passed to the driver. */
32 module_param(debug, int, 0444);
33 MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
35 /* Vendor and product ids */
36 #define USB_KEYSPAN_VENDOR_ID 0x06CD
37 #define USB_KEYSPAN_PRODUCT_UIA11 0x0202
39 /* Defines for converting the data from the remote. */
41 #define ZERO_MASK 0x1F /* 5 bits for a 0 */
43 #define ONE_MASK 0x3F /* 6 bits for a 1 */
45 #define SYNC_MASK 0x3FFF /* 14 bits for a SYNC sequence */
47 #define STOP_MASK 0x1F /* 5 bits for the STOP sequence */
50 #define RECV_SIZE 8 /* The UIA-11 type have a 8 byte limit. */
52 /* table of devices that work with this driver */
53 static struct usb_device_id keyspan_table[] = {
54 { USB_DEVICE(USB_KEYSPAN_VENDOR_ID, USB_KEYSPAN_PRODUCT_UIA11) },
55 { } /* Terminating entry */
58 /* Structure to store all the real stuff that a remote sends to us. */
59 struct keyspan_message {
65 /* Structure used for all the bit testing magic needed to be done. */
74 /* Structure to hold all of our driver specific stuff */
78 struct usb_device* udev;
79 struct input_dev *input;
80 struct usb_interface* interface;
81 struct usb_endpoint_descriptor* in_endpoint;
85 unsigned char* in_buffer;
87 /* variables used to parse messages from remote. */
88 struct bit_tester data;
94 * Table that maps the 31 possible keycodes to input keys.
95 * Currently there are 15 and 17 button models so RESERVED codes
96 * are blank areas in the mapping.
98 static const int keyspan_key_table[] = {
99 KEY_RESERVED, /* 0 is just a place holder. */
133 static struct usb_driver keyspan_driver;
136 * Debug routine that prints out what we've received from the remote.
138 static void keyspan_print(struct usb_keyspan* dev) /*unsigned char* data)*/
140 char codes[4 * RECV_SIZE];
143 for (i = 0; i < RECV_SIZE; i++)
144 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->dev,
164 "%s - Error ran out of data. pos: %d, len: %d\n",
165 __FUNCTION__, dev->data.pos, dev->data.len);
169 /* Load as much as we can into the tester. */
170 while ((dev->data.bits_left + 7 < (sizeof(dev->data.tester) * 8)) &&
171 (dev->data.pos < dev->data.len)) {
172 dev->data.tester += (dev->data.buffer[dev->data.pos++] << dev->data.bits_left);
173 dev->data.bits_left += 8;
180 * Routine that handles all the logic needed to parse out the message from the remote.
182 static void keyspan_check_data(struct usb_keyspan *remote, struct pt_regs *regs)
186 struct keyspan_message message;
188 switch(remote->stage) {
191 * In stage 0 we want to find the start of a message. The remote sends a 0xFF as filler.
192 * So the first byte that isn't a FF should be the start of a new message.
194 for (i = 0; i < RECV_SIZE && remote->in_buffer[i] == GAP; ++i);
197 memcpy(remote->data.buffer, remote->in_buffer, RECV_SIZE);
198 remote->data.len = RECV_SIZE;
199 remote->data.pos = 0;
200 remote->data.tester = 0;
201 remote->data.bits_left = 0;
208 * Stage 1 we should have 16 bytes and should be able to detect a
209 * SYNC. The SYNC is 14 bits, 7 0's and then 7 1's.
211 memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
212 remote->data.len += RECV_SIZE;
215 while ((remote->data.bits_left >= 14 || remote->data.pos < remote->data.len) && !found) {
216 for (i = 0; i < 8; ++i) {
217 if (keyspan_load_tester(remote, 14) != 0) {
222 if ((remote->data.tester & SYNC_MASK) == SYNC) {
223 remote->data.tester = remote->data.tester >> 14;
224 remote->data.bits_left -= 14;
228 remote->data.tester = remote->data.tester >> 1;
229 --remote->data.bits_left;
236 remote->data.len = 0;
244 * Stage 2 we should have 24 bytes which will be enough for a full
245 * message. We need to parse out the system code, button code,
246 * toggle code, and stop.
248 memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
249 remote->data.len += RECV_SIZE;
252 for (i = 0; i < 9; i++) {
253 keyspan_load_tester(remote, 6);
255 if ((remote->data.tester & ZERO_MASK) == ZERO) {
256 message.system = message.system << 1;
257 remote->data.tester = remote->data.tester >> 5;
258 remote->data.bits_left -= 5;
259 } else if ((remote->data.tester & ONE_MASK) == ONE) {
260 message.system = (message.system << 1) + 1;
261 remote->data.tester = remote->data.tester >> 6;
262 remote->data.bits_left -= 6;
264 err("%s - Unknown sequence found in system data.\n", __FUNCTION__);
271 for (i = 0; i < 5; i++) {
272 keyspan_load_tester(remote, 6);
274 if ((remote->data.tester & ZERO_MASK) == ZERO) {
275 message.button = message.button << 1;
276 remote->data.tester = remote->data.tester >> 5;
277 remote->data.bits_left -= 5;
278 } else if ((remote->data.tester & ONE_MASK) == ONE) {
279 message.button = (message.button << 1) + 1;
280 remote->data.tester = remote->data.tester >> 6;
281 remote->data.bits_left -= 6;
283 err("%s - Unknown sequence found in button data.\n", __FUNCTION__);
289 keyspan_load_tester(remote, 6);
290 if ((remote->data.tester & ZERO_MASK) == ZERO) {
292 remote->data.tester = remote->data.tester >> 5;
293 remote->data.bits_left -= 5;
294 } else if ((remote->data.tester & ONE_MASK) == ONE) {
296 remote->data.tester = remote->data.tester >> 6;
297 remote->data.bits_left -= 6;
299 err("%s - Error in message, invalid toggle.\n", __FUNCTION__);
302 keyspan_load_tester(remote, 5);
303 if ((remote->data.tester & STOP_MASK) == STOP) {
304 remote->data.tester = remote->data.tester >> 5;
305 remote->data.bits_left -= 5;
307 err("Bad message recieved, no stop bit found.\n");
310 dev_dbg(&remote->udev->dev,
311 "%s found valid message: system: %d, button: %d, toggle: %d\n",
312 __FUNCTION__, message.system, message.button, message.toggle);
314 if (message.toggle != remote->toggle) {
315 input_regs(remote->input, regs);
316 input_report_key(remote->input, keyspan_key_table[message.button], 1);
317 input_report_key(remote->input, keyspan_key_table[message.button], 0);
318 input_sync(remote->input);
319 remote->toggle = message.toggle;
328 * Routine for sending all the initialization messages to the remote.
330 static int keyspan_setup(struct usb_device* dev)
334 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
335 0x11, 0x40, 0x5601, 0x0, NULL, 0, 0);
337 dev_dbg(&dev->dev, "%s - failed to set bit rate due to error: %d\n",
338 __FUNCTION__, retval);
342 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
343 0x44, 0x40, 0x0, 0x0, NULL, 0, 0);
345 dev_dbg(&dev->dev, "%s - failed to set resume sensitivity due to error: %d\n",
346 __FUNCTION__, retval);
350 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
351 0x22, 0x40, 0x0, 0x0, NULL, 0, 0);
353 dev_dbg(&dev->dev, "%s - failed to turn receive on due to error: %d\n",
354 __FUNCTION__, retval);
358 dev_dbg(&dev->dev, "%s - Setup complete.\n", __FUNCTION__);
363 * Routine used to handle a new message that has come in.
365 static void keyspan_irq_recv(struct urb *urb, struct pt_regs *regs)
367 struct usb_keyspan *dev = urb->context;
370 /* Check our status in case we need to bail out early. */
371 switch (urb->status) {
375 /* Device went away so don't keep trying to read from it. */
389 keyspan_check_data(dev, regs);
392 retval = usb_submit_urb(urb, GFP_ATOMIC);
394 err ("%s - usb_submit_urb failed with result: %d", __FUNCTION__, retval);
397 static int keyspan_open(struct input_dev *dev)
399 struct usb_keyspan *remote = dev->private;
401 remote->irq_urb->dev = remote->udev;
402 if (usb_submit_urb(remote->irq_urb, GFP_KERNEL))
408 static void keyspan_close(struct input_dev *dev)
410 struct usb_keyspan *remote = dev->private;
412 usb_kill_urb(remote->irq_urb);
415 static struct usb_endpoint_descriptor *keyspan_get_in_endpoint(struct usb_host_interface *iface)
418 struct usb_endpoint_descriptor *endpoint;
421 for (i = 0; i < iface->desc.bNumEndpoints; ++i) {
422 endpoint = &iface->endpoint[i].desc;
424 if ((endpoint->bEndpointAddress & USB_DIR_IN) &&
425 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
426 /* we found our interrupt in endpoint */
435 * Routine that sets up the driver to handle a specific USB device detected on the bus.
437 static int keyspan_probe(struct usb_interface *interface, const struct usb_device_id *id)
439 struct usb_device *udev = interface_to_usbdev(interface);
440 struct usb_endpoint_descriptor *endpoint;
441 struct usb_keyspan *remote;
442 struct input_dev *input_dev;
445 endpoint = keyspan_get_in_endpoint(interface->cur_altsetting);
449 remote = kzalloc(sizeof(*remote), GFP_KERNEL);
450 input_dev = input_allocate_device();
451 if (!remote || !input_dev) {
457 remote->input = input_dev;
458 remote->interface = interface;
459 remote->in_endpoint = endpoint;
460 remote->toggle = -1; /* Set to -1 so we will always not match the toggle from the first remote message. */
462 remote->in_buffer = usb_buffer_alloc(udev, RECV_SIZE, SLAB_ATOMIC, &remote->in_dma);
463 if (!remote->in_buffer) {
468 remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
469 if (!remote->irq_urb) {
474 retval = keyspan_setup(udev);
480 if (udev->manufacturer)
481 strlcpy(remote->name, udev->manufacturer, sizeof(remote->name));
484 if (udev->manufacturer)
485 strlcat(remote->name, " ", sizeof(remote->name));
486 strlcat(remote->name, udev->product, sizeof(remote->name));
489 if (!strlen(remote->name))
490 snprintf(remote->name, sizeof(remote->name),
491 "USB Keyspan Remote %04x:%04x",
492 le16_to_cpu(udev->descriptor.idVendor),
493 le16_to_cpu(udev->descriptor.idProduct));
495 usb_make_path(udev, remote->phys, sizeof(remote->phys));
496 strlcat(remote->phys, "/input0", sizeof(remote->phys));
498 input_dev->name = remote->name;
499 input_dev->phys = remote->phys;
500 usb_to_input_id(udev, &input_dev->id);
501 input_dev->cdev.dev = &interface->dev;
503 input_dev->evbit[0] = BIT(EV_KEY); /* We will only report KEY events. */
504 for (i = 0; i < ARRAY_SIZE(keyspan_key_table); i++)
505 if (keyspan_key_table[i] != KEY_RESERVED)
506 set_bit(keyspan_key_table[i], input_dev->keybit);
508 input_dev->private = remote;
509 input_dev->open = keyspan_open;
510 input_dev->close = keyspan_close;
513 * Initialize the URB to access the device. The urb gets sent to the device in keyspan_open()
515 usb_fill_int_urb(remote->irq_urb,
516 remote->udev, usb_rcvintpipe(remote->udev, remote->in_endpoint->bEndpointAddress),
517 remote->in_buffer, RECV_SIZE, keyspan_irq_recv, remote,
518 remote->in_endpoint->bInterval);
519 remote->irq_urb->transfer_dma = remote->in_dma;
520 remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
522 /* we can register the device now, as it is ready */
523 input_register_device(remote->input);
525 /* save our data pointer in this interface device */
526 usb_set_intfdata(interface, remote);
530 fail3: usb_free_urb(remote->irq_urb);
531 fail2: usb_buffer_free(udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
532 fail1: kfree(remote);
533 input_free_device(input_dev);
539 * Routine called when a device is disconnected from the USB.
541 static void keyspan_disconnect(struct usb_interface *interface)
543 struct usb_keyspan *remote;
545 remote = usb_get_intfdata(interface);
546 usb_set_intfdata(interface, NULL);
548 if (remote) { /* We have a valid driver structure so clean up everything we allocated. */
549 input_unregister_device(remote->input);
550 usb_kill_urb(remote->irq_urb);
551 usb_free_urb(remote->irq_urb);
552 usb_buffer_free(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
558 * Standard driver set up sections
560 static struct usb_driver keyspan_driver =
562 .name = "keyspan_remote",
563 .probe = keyspan_probe,
564 .disconnect = keyspan_disconnect,
565 .id_table = keyspan_table
568 static int __init usb_keyspan_init(void)
572 /* register this driver with the USB subsystem */
573 result = usb_register(&keyspan_driver);
575 err("usb_register failed. Error number %d\n", result);
580 static void __exit usb_keyspan_exit(void)
582 /* deregister this driver with the USB subsystem */
583 usb_deregister(&keyspan_driver);
586 module_init(usb_keyspan_init);
587 module_exit(usb_keyspan_exit);
589 MODULE_DEVICE_TABLE(usb, keyspan_table);
590 MODULE_AUTHOR(DRIVER_AUTHOR);
591 MODULE_DESCRIPTION(DRIVER_DESC);
592 MODULE_LICENSE(DRIVER_LICENSE);