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