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