aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/input/keyspan_remote.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/input/keyspan_remote.c')
-rw-r--r--drivers/usb/input/keyspan_remote.c633
1 files changed, 633 insertions, 0 deletions
diff --git a/drivers/usb/input/keyspan_remote.c b/drivers/usb/input/keyspan_remote.c
new file mode 100644
index 000000000000..67dc93685203
--- /dev/null
+++ b/drivers/usb/input/keyspan_remote.c
@@ -0,0 +1,633 @@
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
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
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"
28
29/* Parameters that can be passed to the driver. */
30static int debug;
31module_param(debug, int, 0444);
32MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
33
34/* Vendor and product ids */
35#define USB_KEYSPAN_VENDOR_ID 0x06CD
36#define USB_KEYSPAN_PRODUCT_UIA11 0x0202
37
38/* Defines for converting the data from the remote. */
39#define ZERO 0x18
40#define ZERO_MASK 0x1F /* 5 bits for a 0 */
41#define ONE 0x3C
42#define ONE_MASK 0x3F /* 6 bits for a 1 */
43#define SYNC 0x3F80
44#define SYNC_MASK 0x3FFF /* 14 bits for a SYNC sequence */
45#define STOP 0x00
46#define STOP_MASK 0x1F /* 5 bits for the STOP sequence */
47#define GAP 0xFF
48
49#define RECV_SIZE 8 /* The UIA-11 type have a 8 byte limit. */
50
51/* table of devices that work with this driver */
52static struct usb_device_id keyspan_table[] = {
53 { USB_DEVICE(USB_KEYSPAN_VENDOR_ID, USB_KEYSPAN_PRODUCT_UIA11) },
54 { } /* Terminating entry */
55};
56
57/* Structure to store all the real stuff that a remote sends to us. */
58struct keyspan_message {
59 u16 system;
60 u8 button;
61 u8 toggle;
62};
63
64/* Structure used for all the bit testing magic needed to be done. */
65struct bit_tester {
66 u32 tester;
67 int len;
68 int pos;
69 int bits_left;
70 u8 buffer[32];
71};
72
73/* Structure to hold all of our driver specific stuff */
74struct usb_keyspan {
75 char name[128];
76 char phys[64];
77 struct usb_device* udev;
78 struct input_dev input;
79 struct usb_interface* interface;
80 struct usb_endpoint_descriptor* in_endpoint;
81 struct urb* irq_urb;
82 int open;
83 dma_addr_t in_dma;
84 unsigned char* in_buffer;
85
86 /* variables used to parse messages from remote. */
87 struct bit_tester data;
88 int stage;
89 int toggle;
90};
91
92/*
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.
96 */
97static int keyspan_key_table[] = {
98 KEY_RESERVED, /* 0 is just a place holder. */
99 KEY_RESERVED,
100 KEY_STOP,
101 KEY_PLAYCD,
102 KEY_RESERVED,
103 KEY_PREVIOUSSONG,
104 KEY_REWIND,
105 KEY_FORWARD,
106 KEY_NEXTSONG,
107 KEY_RESERVED,
108 KEY_RESERVED,
109 KEY_RESERVED,
110 KEY_PAUSE,
111 KEY_VOLUMEUP,
112 KEY_RESERVED,
113 KEY_RESERVED,
114 KEY_RESERVED,
115 KEY_VOLUMEDOWN,
116 KEY_RESERVED,
117 KEY_UP,
118 KEY_RESERVED,
119 KEY_MUTE,
120 KEY_LEFT,
121 KEY_ENTER,
122 KEY_RIGHT,
123 KEY_RESERVED,
124 KEY_RESERVED,
125 KEY_DOWN,
126 KEY_RESERVED,
127 KEY_KPASTERISK,
128 KEY_RESERVED,
129 KEY_MENU
130};
131
132static struct usb_driver keyspan_driver;
133
134/*
135 * Debug routine that prints out what we've received from the remote.
136 */
137static void keyspan_print(struct usb_keyspan* dev) /*unsigned char* data)*/
138{
139 char codes[4*RECV_SIZE];
140 int i;
141
142 for (i = 0; i < RECV_SIZE; i++) {
143 snprintf(codes+i*3, 4, "%02x ", dev->in_buffer[i]);
144 }
145
146 dev_info(&dev->udev->dev, "%s\n", codes);
147}
148
149/*
150 * Routine that manages the bit_tester structure. It makes sure that there are
151 * at least bits_needed bits loaded into the tester.
152 */
153static int keyspan_load_tester(struct usb_keyspan* dev, int bits_needed)
154{
155 if (dev->data.bits_left >= bits_needed)
156 return(0);
157
158 /*
159 * Somehow we've missed the last message. The message will be repeated
160 * though so it's not too big a deal
161 */
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);
165 return(-1);
166 }
167
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;
173 }
174
175 return(0);
176}
177
178/*
179 * Routine that handles all the logic needed to parse out the message from the remote.
180 */
181static void keyspan_check_data(struct usb_keyspan *remote, struct pt_regs *regs)
182{
183 int i;
184 int found = 0;
185 struct keyspan_message message;
186
187 switch(remote->stage) {
188 case 0:
189 /*
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.
192 */
193 for (i = 0; i < RECV_SIZE && remote->in_buffer[i] == GAP; ++i);
194
195 if (i < RECV_SIZE) {
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;
201 remote->stage = 1;
202 }
203 break;
204
205 case 1:
206 /*
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.
209 */
210 memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
211 remote->data.len += RECV_SIZE;
212
213 found = 0;
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) {
217 remote->stage = 0;
218 return;
219 }
220
221 if ((remote->data.tester & SYNC_MASK) == SYNC) {
222 remote->data.tester = remote->data.tester >> 14;
223 remote->data.bits_left -= 14;
224 found = 1;
225 break;
226 } else {
227 remote->data.tester = remote->data.tester >> 1;
228 --remote->data.bits_left;
229 }
230 }
231 }
232
233 if (!found) {
234 remote->stage = 0;
235 remote->data.len = 0;
236 } else {
237 remote->stage = 2;
238 }
239 break;
240
241 case 2:
242 /*
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.
246 */
247 memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
248 remote->data.len += RECV_SIZE;
249
250 message.system = 0;
251 for (i = 0; i < 9; i++) {
252 keyspan_load_tester(remote, 6);
253
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;
262 } else {
263 err("%s - Unknown sequence found in system data.\n", __FUNCTION__);
264 remote->stage = 0;
265 return;
266 }
267 }
268
269 message.button = 0;
270 for (i = 0; i < 5; i++) {
271 keyspan_load_tester(remote, 6);
272
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;
281 } else {
282 err("%s - Unknown sequence found in button data.\n", __FUNCTION__);
283 remote->stage = 0;
284 return;
285 }
286 }
287
288 keyspan_load_tester(remote, 6);
289 if ((remote->data.tester & ZERO_MASK) == ZERO) {
290 message.toggle = 0;
291 remote->data.tester = remote->data.tester >> 5;
292 remote->data.bits_left -= 5;
293 } else if ((remote->data.tester & ONE_MASK) == ONE) {
294 message.toggle = 1;
295 remote->data.tester = remote->data.tester >> 6;
296 remote->data.bits_left -= 6;
297 } else {
298 err("%s - Error in message, invalid toggle.\n", __FUNCTION__);
299 }
300
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;
305 } else {
306 err("Bad message recieved, no stop bit found.\n");
307 }
308
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);
312
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;
319 }
320
321 remote->stage = 0;
322 break;
323 }
324}
325
326/*
327 * Routine for sending all the initialization messages to the remote.
328 */
329static int keyspan_setup(struct usb_device* dev)
330{
331 int retval = 0;
332
333 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
334 0x11, 0x40, 0x5601, 0x0, NULL, 0, 0);
335 if (retval) {
336 dev_dbg(&dev->dev, "%s - failed to set bit rate due to error: %d\n",
337 __FUNCTION__, retval);
338 return(retval);
339 }
340
341 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
342 0x44, 0x40, 0x0, 0x0, NULL, 0, 0);
343 if (retval) {
344 dev_dbg(&dev->dev, "%s - failed to set resume sensitivity due to error: %d\n",
345 __FUNCTION__, retval);
346 return(retval);
347 }
348
349 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
350 0x22, 0x40, 0x0, 0x0, NULL, 0, 0);
351 if (retval) {
352 dev_dbg(&dev->dev, "%s - failed to turn receive on due to error: %d\n",
353 __FUNCTION__, retval);
354 return(retval);
355 }
356
357 dev_dbg(&dev->dev, "%s - Setup complete.\n", __FUNCTION__);
358 return(retval);
359}
360
361/*
362 * Routine used to handle a new message that has come in.
363 */
364static void keyspan_irq_recv(struct urb *urb, struct pt_regs *regs)
365{
366 struct usb_keyspan *dev = urb->context;
367 int retval;
368
369 /* Check our status in case we need to bail out early. */
370 switch (urb->status) {
371 case 0:
372 break;
373
374 /* Device went away so don't keep trying to read from it. */
375 case -ECONNRESET:
376 case -ENOENT:
377 case -ESHUTDOWN:
378 return;
379
380 default:
381 goto resubmit;
382 break;
383 }
384
385 if (debug)
386 keyspan_print(dev);
387
388 keyspan_check_data(dev, regs);
389
390resubmit:
391 retval = usb_submit_urb(urb, GFP_ATOMIC);
392 if (retval)
393 err ("%s - usb_submit_urb failed with result: %d", __FUNCTION__, retval);
394}
395
396static int keyspan_open(struct input_dev *dev)
397{
398 struct usb_keyspan *remote = dev->private;
399
400 if (remote->open++)
401 return 0;
402
403 remote->irq_urb->dev = remote->udev;
404 if (usb_submit_urb(remote->irq_urb, GFP_KERNEL)) {
405 remote->open--;
406 return -EIO;
407 }
408
409 return 0;
410}
411
412static void keyspan_close(struct input_dev *dev)
413{
414 struct usb_keyspan *remote = dev->private;
415
416 if (!--remote->open)
417 usb_kill_urb(remote->irq_urb);
418}
419
420/*
421 * Routine that sets up the driver to handle a specific USB device detected on the bus.
422 */
423static int keyspan_probe(struct usb_interface *interface, const struct usb_device_id *id)
424{
425 int i;
426 int retval = -ENOMEM;
427 char path[64];
428 char *buf;
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));
433
434 /* See if the offered device matches what we can accept */
435 if ((udev->descriptor.idVendor != USB_KEYSPAN_VENDOR_ID) ||
436 (udev->descriptor.idProduct != USB_KEYSPAN_PRODUCT_UIA11) )
437 return -ENODEV;
438
439 /* allocate memory for our device state and initialize it */
440 remote = kmalloc(sizeof(*remote), GFP_KERNEL);
441 if (remote == NULL) {
442 err("Out of memory\n");
443 goto error;
444 }
445 memset(remote, 0x00, sizeof(*remote));
446
447 remote->udev = udev;
448 remote->interface = interface;
449 remote->toggle = -1; /* Set to -1 so we will always not match the toggle from the first remote message. */
450
451 /* set up the endpoint information */
452 /* use only the first in interrupt endpoint */
453 iface_desc = interface->cur_altsetting;
454 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
455 endpoint = &iface_desc->endpoint[i].desc;
456
457 if (!remote->in_endpoint &&
458 (endpoint->bEndpointAddress & USB_DIR_IN) &&
459 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
460 /* we found our interrupt in endpoint */
461 remote->in_endpoint = endpoint;
462
463 remote->in_buffer = usb_buffer_alloc(remote->udev, RECV_SIZE, SLAB_ATOMIC, &remote->in_dma);
464 if (!remote->in_buffer) {
465 retval = -ENOMEM;
466 goto error;
467 }
468 }
469 }
470
471 if (!remote->in_endpoint) {
472 err("Could not find interrupt input endpoint.\n");
473 retval = -ENODEV;
474 goto error;
475 }
476
477 remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
478 if (!remote->irq_urb) {
479 err("Failed to allocate urb.\n");
480 retval = -ENOMEM;
481 goto error;
482 }
483
484 retval = keyspan_setup(remote->udev);
485 if (retval) {
486 err("Failed to setup device.\n");
487 retval = -ENODEV;
488 goto error;
489 }
490
491 /*
492 * Setup the input system with the bits we are going to be reporting
493 */
494 remote->input.evbit[0] = BIT(EV_KEY); /* We will only report KEY events. */
495 for (i = 0; i < 32; ++i) {
496 if (keyspan_key_table[i] != KEY_RESERVED) {
497 set_bit(keyspan_key_table[i], remote->input.keybit);
498 }
499 }
500
501 remote->input.private = remote;
502 remote->input.open = keyspan_open;
503 remote->input.close = keyspan_close;
504
505 usb_make_path(remote->udev, path, 64);
506 sprintf(remote->phys, "%s/input0", path);
507
508 remote->input.name = remote->name;
509 remote->input.phys = remote->phys;
510 remote->input.id.bustype = BUS_USB;
511 remote->input.id.vendor = le16_to_cpu(remote->udev->descriptor.idVendor);
512 remote->input.id.product = le16_to_cpu(remote->udev->descriptor.idProduct);
513 remote->input.id.version = le16_to_cpu(remote->udev->descriptor.bcdDevice);
514
515 if (!(buf = kmalloc(63, GFP_KERNEL))) {
516 usb_buffer_free(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
517 kfree(remote);
518 return -ENOMEM;
519 }
520
521 if (remote->udev->descriptor.iManufacturer &&
522 usb_string(remote->udev, remote->udev->descriptor.iManufacturer, buf, 63) > 0)
523 strcat(remote->name, buf);
524
525 if (remote->udev->descriptor.iProduct &&
526 usb_string(remote->udev, remote->udev->descriptor.iProduct, buf, 63) > 0)
527 sprintf(remote->name, "%s %s", remote->name, buf);
528
529 if (!strlen(remote->name))
530 sprintf(remote->name, "USB Keyspan Remote %04x:%04x",
531 remote->input.id.vendor, remote->input.id.product);
532
533 kfree(buf);
534
535 /*
536 * Initialize the URB to access the device. The urb gets sent to the device in keyspan_open()
537 */
538 usb_fill_int_urb(remote->irq_urb,
539 remote->udev, usb_rcvintpipe(remote->udev, remote->in_endpoint->bEndpointAddress),
540 remote->in_buffer, RECV_SIZE, keyspan_irq_recv, remote,
541 remote->in_endpoint->bInterval);
542 remote->irq_urb->transfer_dma = remote->in_dma;
543 remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
544
545 /* we can register the device now, as it is ready */
546 input_register_device(&remote->input);
547
548 /* save our data pointer in this interface device */
549 usb_set_intfdata(interface, remote);
550
551 /* let the user know what node this device is now attached to */
552 info("connected: %s on %s", remote->name, path);
553 return 0;
554
555error:
556 /*
557 * In case of error we need to clean up any allocated buffers
558 */
559 if (remote->irq_urb)
560 usb_free_urb(remote->irq_urb);
561
562 if (remote->in_buffer)
563 usb_buffer_free(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
564
565 if (remote)
566 kfree(remote);
567
568 return retval;
569}
570
571/*
572 * Routine called when a device is disconnected from the USB.
573 */
574static void keyspan_disconnect(struct usb_interface *interface)
575{
576 struct usb_keyspan *remote;
577
578 /* prevent keyspan_open() from racing keyspan_disconnect() */
579 lock_kernel();
580
581 remote = usb_get_intfdata(interface);
582 usb_set_intfdata(interface, NULL);
583
584 if (remote) { /* We have a valid driver structure so clean up everything we allocated. */
585 input_unregister_device(&remote->input);
586 usb_kill_urb(remote->irq_urb);
587 usb_free_urb(remote->irq_urb);
588 usb_buffer_free(interface_to_usbdev(interface), RECV_SIZE, remote->in_buffer, remote->in_dma);
589 kfree(remote);
590 }
591
592 unlock_kernel();
593
594 info("USB Keyspan now disconnected");
595}
596
597/*
598 * Standard driver set up sections
599 */
600static struct usb_driver keyspan_driver =
601{
602 .owner = THIS_MODULE,
603 .name = "keyspan_remote",
604 .probe = keyspan_probe,
605 .disconnect = keyspan_disconnect,
606 .id_table = keyspan_table
607};
608
609static int __init usb_keyspan_init(void)
610{
611 int result;
612
613 /* register this driver with the USB subsystem */
614 result = usb_register(&keyspan_driver);
615 if (result)
616 err("usb_register failed. Error number %d\n", result);
617
618 return result;
619}
620
621static void __exit usb_keyspan_exit(void)
622{
623 /* deregister this driver with the USB subsystem */
624 usb_deregister(&keyspan_driver);
625}
626
627module_init(usb_keyspan_init);
628module_exit(usb_keyspan_exit);
629
630MODULE_DEVICE_TABLE(usb, keyspan_table);
631MODULE_AUTHOR(DRIVER_AUTHOR);
632MODULE_DESCRIPTION(DRIVER_DESC);
633MODULE_LICENSE(DRIVER_LICENSE);