Pull bugzilla-5452 into release branch
[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/input.h>
22 #include <linux/usb.h>
23 #include <linux/usb_input.h>
24
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"
29
30 /* Parameters that can be passed to the driver. */
31 static int debug;
32 module_param(debug, int, 0444);
33 MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
34
35 /* Vendor and product ids */
36 #define USB_KEYSPAN_VENDOR_ID           0x06CD
37 #define USB_KEYSPAN_PRODUCT_UIA11       0x0202
38
39 /* Defines for converting the data from the remote. */
40 #define ZERO            0x18
41 #define ZERO_MASK       0x1F    /* 5 bits for a 0 */
42 #define ONE             0x3C
43 #define ONE_MASK        0x3F    /* 6 bits for a 1 */
44 #define SYNC            0x3F80
45 #define SYNC_MASK       0x3FFF  /* 14 bits for a SYNC sequence */
46 #define STOP            0x00
47 #define STOP_MASK       0x1F    /* 5 bits for the STOP sequence */
48 #define GAP             0xFF
49
50 #define RECV_SIZE       8       /* The UIA-11 type have a 8 byte limit. */
51
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 */
56 };
57
58 /* Structure to store all the real stuff that a remote sends to us. */
59 struct keyspan_message {
60         u16     system;
61         u8      button;
62         u8      toggle;
63 };
64
65 /* Structure used for all the bit testing magic needed to be done. */
66 struct bit_tester {
67         u32     tester;
68         int     len;
69         int     pos;
70         int     bits_left;
71         u8      buffer[32];
72 };
73
74 /* Structure to hold all of our driver specific stuff */
75 struct usb_keyspan {
76         char                            name[128];
77         char                            phys[64];
78         struct usb_device*              udev;
79         struct input_dev                *input;
80         struct usb_interface*           interface;
81         struct usb_endpoint_descriptor* in_endpoint;
82         struct urb*                     irq_urb;
83         int                             open;
84         dma_addr_t                      in_dma;
85         unsigned char*                  in_buffer;
86
87         /* variables used to parse messages from remote. */
88         struct bit_tester               data;
89         int                             stage;
90         int                             toggle;
91 };
92
93 /*
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.
97  */
98 static const int keyspan_key_table[] = {
99         KEY_RESERVED,           /* 0 is just a place holder. */
100         KEY_RESERVED,
101         KEY_STOP,
102         KEY_PLAYCD,
103         KEY_RESERVED,
104         KEY_PREVIOUSSONG,
105         KEY_REWIND,
106         KEY_FORWARD,
107         KEY_NEXTSONG,
108         KEY_RESERVED,
109         KEY_RESERVED,
110         KEY_RESERVED,
111         KEY_PAUSE,
112         KEY_VOLUMEUP,
113         KEY_RESERVED,
114         KEY_RESERVED,
115         KEY_RESERVED,
116         KEY_VOLUMEDOWN,
117         KEY_RESERVED,
118         KEY_UP,
119         KEY_RESERVED,
120         KEY_MUTE,
121         KEY_LEFT,
122         KEY_ENTER,
123         KEY_RIGHT,
124         KEY_RESERVED,
125         KEY_RESERVED,
126         KEY_DOWN,
127         KEY_RESERVED,
128         KEY_KPASTERISK,
129         KEY_RESERVED,
130         KEY_MENU
131 };
132
133 static struct usb_driver keyspan_driver;
134
135 /*
136  * Debug routine that prints out what we've received from the remote.
137  */
138 static void keyspan_print(struct usb_keyspan* dev) /*unsigned char* data)*/
139 {
140         char codes[4 * RECV_SIZE];
141         int i;
142
143         for (i = 0; i < RECV_SIZE; i++)
144                 snprintf(codes + i * 3, 4, "%02x ", dev->in_buffer[i]);
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  */
153 static 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->dev,
164                         "%s - Error ran out of data. pos: %d, len: %d\n",
165                         __FUNCTION__, dev->data.pos, dev->data.len);
166                 return -1;
167         }
168
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;
174         }
175
176         return 0;
177 }
178
179 /*
180  * Routine that handles all the logic needed to parse out the message from the remote.
181  */
182 static void keyspan_check_data(struct usb_keyspan *remote, struct pt_regs *regs)
183 {
184         int i;
185         int found = 0;
186         struct keyspan_message message;
187
188         switch(remote->stage) {
189         case 0:
190                 /*
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.
193                  */
194                 for (i = 0; i < RECV_SIZE && remote->in_buffer[i] == GAP; ++i);
195
196                 if (i < RECV_SIZE) {
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;
202                         remote->stage = 1;
203                 }
204                 break;
205
206         case 1:
207                 /*
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.
210                  */
211                 memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
212                 remote->data.len += RECV_SIZE;
213
214                 found = 0;
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) {
218                                         remote->stage = 0;
219                                         return;
220                                 }
221
222                                 if ((remote->data.tester & SYNC_MASK) == SYNC) {
223                                         remote->data.tester = remote->data.tester >> 14;
224                                         remote->data.bits_left -= 14;
225                                         found = 1;
226                                         break;
227                                 } else {
228                                         remote->data.tester = remote->data.tester >> 1;
229                                         --remote->data.bits_left;
230                                 }
231                         }
232                 }
233
234                 if (!found) {
235                         remote->stage = 0;
236                         remote->data.len = 0;
237                 } else {
238                         remote->stage = 2;
239                 }
240                 break;
241
242         case 2:
243                 /*
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.
247                  */
248                 memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
249                 remote->data.len += RECV_SIZE;
250
251                 message.system = 0;
252                 for (i = 0; i < 9; i++) {
253                         keyspan_load_tester(remote, 6);
254
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;
263                         } else {
264                                 err("%s - Unknown sequence found in system data.\n", __FUNCTION__);
265                                 remote->stage = 0;
266                                 return;
267                         }
268                 }
269
270                 message.button = 0;
271                 for (i = 0; i < 5; i++) {
272                         keyspan_load_tester(remote, 6);
273
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;
282                         } else {
283                                 err("%s - Unknown sequence found in button data.\n", __FUNCTION__);
284                                 remote->stage = 0;
285                                 return;
286                         }
287                 }
288
289                 keyspan_load_tester(remote, 6);
290                 if ((remote->data.tester & ZERO_MASK) == ZERO) {
291                         message.toggle = 0;
292                         remote->data.tester = remote->data.tester >> 5;
293                         remote->data.bits_left -= 5;
294                 } else if ((remote->data.tester & ONE_MASK) == ONE) {
295                         message.toggle = 1;
296                         remote->data.tester = remote->data.tester >> 6;
297                         remote->data.bits_left -= 6;
298                 } else {
299                         err("%s - Error in message, invalid toggle.\n", __FUNCTION__);
300                         remote->stage = 0;
301                         return;
302                 }
303
304                 keyspan_load_tester(remote, 5);
305                 if ((remote->data.tester & STOP_MASK) == STOP) {
306                         remote->data.tester = remote->data.tester >> 5;
307                         remote->data.bits_left -= 5;
308                 } else {
309                         err("Bad message recieved, no stop bit found.\n");
310                 }
311
312                 dev_dbg(&remote->udev->dev,
313                         "%s found valid message: system: %d, button: %d, toggle: %d\n",
314                         __FUNCTION__, message.system, message.button, message.toggle);
315
316                 if (message.toggle != remote->toggle) {
317                         input_regs(remote->input, regs);
318                         input_report_key(remote->input, keyspan_key_table[message.button], 1);
319                         input_report_key(remote->input, keyspan_key_table[message.button], 0);
320                         input_sync(remote->input);
321                         remote->toggle = message.toggle;
322                 }
323
324                 remote->stage = 0;
325                 break;
326         }
327 }
328
329 /*
330  * Routine for sending all the initialization messages to the remote.
331  */
332 static int keyspan_setup(struct usb_device* dev)
333 {
334         int retval = 0;
335
336         retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
337                                  0x11, 0x40, 0x5601, 0x0, NULL, 0, 0);
338         if (retval) {
339                 dev_dbg(&dev->dev, "%s - failed to set bit rate due to error: %d\n",
340                         __FUNCTION__, retval);
341                 return(retval);
342         }
343
344         retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
345                                  0x44, 0x40, 0x0, 0x0, NULL, 0, 0);
346         if (retval) {
347                 dev_dbg(&dev->dev, "%s - failed to set resume sensitivity due to error: %d\n",
348                         __FUNCTION__, retval);
349                 return(retval);
350         }
351
352         retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
353                                  0x22, 0x40, 0x0, 0x0, NULL, 0, 0);
354         if (retval) {
355                 dev_dbg(&dev->dev, "%s - failed to turn receive on due to error: %d\n",
356                         __FUNCTION__, retval);
357                 return(retval);
358         }
359
360         dev_dbg(&dev->dev, "%s - Setup complete.\n", __FUNCTION__);
361         return(retval);
362 }
363
364 /*
365  * Routine used to handle a new message that has come in.
366  */
367 static void keyspan_irq_recv(struct urb *urb, struct pt_regs *regs)
368 {
369         struct usb_keyspan *dev = urb->context;
370         int retval;
371
372         /* Check our status in case we need to bail out early. */
373         switch (urb->status) {
374         case 0:
375                 break;
376
377         /* Device went away so don't keep trying to read from it. */
378         case -ECONNRESET:
379         case -ENOENT:
380         case -ESHUTDOWN:
381                 return;
382
383         default:
384                 goto resubmit;
385                 break;
386         }
387
388         if (debug)
389                 keyspan_print(dev);
390
391         keyspan_check_data(dev, regs);
392
393 resubmit:
394         retval = usb_submit_urb(urb, GFP_ATOMIC);
395         if (retval)
396                 err ("%s - usb_submit_urb failed with result: %d", __FUNCTION__, retval);
397 }
398
399 static int keyspan_open(struct input_dev *dev)
400 {
401         struct usb_keyspan *remote = dev->private;
402
403         remote->irq_urb->dev = remote->udev;
404         if (usb_submit_urb(remote->irq_urb, GFP_KERNEL))
405                 return -EIO;
406
407         return 0;
408 }
409
410 static void keyspan_close(struct input_dev *dev)
411 {
412         struct usb_keyspan *remote = dev->private;
413
414         usb_kill_urb(remote->irq_urb);
415 }
416
417 static struct usb_endpoint_descriptor *keyspan_get_in_endpoint(struct usb_host_interface *iface)
418 {
419
420         struct usb_endpoint_descriptor *endpoint;
421         int i;
422
423         for (i = 0; i < iface->desc.bNumEndpoints; ++i) {
424                 endpoint = &iface->endpoint[i].desc;
425
426                 if ((endpoint->bEndpointAddress & USB_DIR_IN) &&
427                     ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
428                         /* we found our interrupt in endpoint */
429                         return endpoint;
430                 }
431         }
432
433         return NULL;
434 }
435
436 /*
437  * Routine that sets up the driver to handle a specific USB device detected on the bus.
438  */
439 static int keyspan_probe(struct usb_interface *interface, const struct usb_device_id *id)
440 {
441         struct usb_device *udev = interface_to_usbdev(interface);
442         struct usb_endpoint_descriptor *endpoint;
443         struct usb_keyspan *remote;
444         struct input_dev *input_dev;
445         int i, retval;
446
447         endpoint = keyspan_get_in_endpoint(interface->cur_altsetting);
448         if (!endpoint)
449                 return -ENODEV;
450
451         remote = kzalloc(sizeof(*remote), GFP_KERNEL);
452         input_dev = input_allocate_device();
453         if (!remote || !input_dev) {
454                 retval = -ENOMEM;
455                 goto fail1;
456         }
457
458         remote->udev = udev;
459         remote->input = input_dev;
460         remote->interface = interface;
461         remote->in_endpoint = endpoint;
462         remote->toggle = -1;    /* Set to -1 so we will always not match the toggle from the first remote message. */
463
464         remote->in_buffer = usb_buffer_alloc(udev, RECV_SIZE, SLAB_ATOMIC, &remote->in_dma);
465         if (!remote->in_buffer) {
466                 retval = -ENOMEM;
467                 goto fail1;
468         }
469
470         remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
471         if (!remote->irq_urb) {
472                 retval = -ENOMEM;
473                 goto fail2;
474         }
475
476         retval = keyspan_setup(udev);
477         if (retval) {
478                 retval = -ENODEV;
479                 goto fail3;
480         }
481
482         if (udev->manufacturer)
483                 strlcpy(remote->name, udev->manufacturer, sizeof(remote->name));
484
485         if (udev->product) {
486                 if (udev->manufacturer)
487                         strlcat(remote->name, " ", sizeof(remote->name));
488                 strlcat(remote->name, udev->product, sizeof(remote->name));
489         }
490
491         if (!strlen(remote->name))
492                 snprintf(remote->name, sizeof(remote->name),
493                          "USB Keyspan Remote %04x:%04x",
494                          le16_to_cpu(udev->descriptor.idVendor),
495                          le16_to_cpu(udev->descriptor.idProduct));
496
497         usb_make_path(udev, remote->phys, sizeof(remote->phys));
498         strlcat(remote->phys, "/input0", sizeof(remote->phys));
499
500         input_dev->name = remote->name;
501         input_dev->phys = remote->phys;
502         usb_to_input_id(udev, &input_dev->id);
503         input_dev->cdev.dev = &interface->dev;
504
505         input_dev->evbit[0] = BIT(EV_KEY);              /* We will only report KEY events. */
506         for (i = 0; i < ARRAY_SIZE(keyspan_key_table); i++)
507                 if (keyspan_key_table[i] != KEY_RESERVED)
508                         set_bit(keyspan_key_table[i], input_dev->keybit);
509
510         input_dev->private = remote;
511         input_dev->open = keyspan_open;
512         input_dev->close = keyspan_close;
513
514         /*
515          * Initialize the URB to access the device. The urb gets sent to the device in keyspan_open()
516          */
517         usb_fill_int_urb(remote->irq_urb,
518                          remote->udev, usb_rcvintpipe(remote->udev, remote->in_endpoint->bEndpointAddress),
519                          remote->in_buffer, RECV_SIZE, keyspan_irq_recv, remote,
520                          remote->in_endpoint->bInterval);
521         remote->irq_urb->transfer_dma = remote->in_dma;
522         remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
523
524         /* we can register the device now, as it is ready */
525         input_register_device(remote->input);
526
527         /* save our data pointer in this interface device */
528         usb_set_intfdata(interface, remote);
529
530         return 0;
531
532  fail3: usb_free_urb(remote->irq_urb);
533  fail2: usb_buffer_free(udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
534  fail1: kfree(remote);
535         input_free_device(input_dev);
536
537         return retval;
538 }
539
540 /*
541  * Routine called when a device is disconnected from the USB.
542  */
543 static void keyspan_disconnect(struct usb_interface *interface)
544 {
545         struct usb_keyspan *remote;
546
547         remote = usb_get_intfdata(interface);
548         usb_set_intfdata(interface, NULL);
549
550         if (remote) {   /* We have a valid driver structure so clean up everything we allocated. */
551                 input_unregister_device(remote->input);
552                 usb_kill_urb(remote->irq_urb);
553                 usb_free_urb(remote->irq_urb);
554                 usb_buffer_free(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
555                 kfree(remote);
556         }
557 }
558
559 /*
560  * Standard driver set up sections
561  */
562 static struct usb_driver keyspan_driver =
563 {
564         .name =         "keyspan_remote",
565         .probe =        keyspan_probe,
566         .disconnect =   keyspan_disconnect,
567         .id_table =     keyspan_table
568 };
569
570 static int __init usb_keyspan_init(void)
571 {
572         int result;
573
574         /* register this driver with the USB subsystem */
575         result = usb_register(&keyspan_driver);
576         if (result)
577                 err("usb_register failed. Error number %d\n", result);
578
579         return result;
580 }
581
582 static void __exit usb_keyspan_exit(void)
583 {
584         /* deregister this driver with the USB subsystem */
585         usb_deregister(&keyspan_driver);
586 }
587
588 module_init(usb_keyspan_init);
589 module_exit(usb_keyspan_exit);
590
591 MODULE_DEVICE_TABLE(usb, keyspan_table);
592 MODULE_AUTHOR(DRIVER_AUTHOR);
593 MODULE_DESCRIPTION(DRIVER_DESC);
594 MODULE_LICENSE(DRIVER_LICENSE);