Merge branch 'x86/urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux...
[pandora-kernel.git] / drivers / staging / line6 / driver.c
1 /*
2  * Line6 Linux USB driver - 0.8.0
3  *
4  * Copyright (C) 2004-2009 Markus Grabner (grabner@icg.tugraz.at)
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  */
11
12 #include "driver.h"
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/usb.h>
18
19 #include "audio.h"
20 #include "capture.h"
21 #include "control.h"
22 #include "midi.h"
23 #include "playback.h"
24 #include "pod.h"
25 #include "revision.h"
26 #include "toneport.h"
27 #include "usbdefs.h"
28 #include "variax.h"
29
30
31 #define DRIVER_AUTHOR  "Markus Grabner <grabner@icg.tugraz.at>"
32 #define DRIVER_DESC    "Line6 USB Driver"
33 #define DRIVER_VERSION "0.8.0"
34
35
36 /* table of devices that work with this driver */
37 static const struct usb_device_id line6_id_table[] = {
38         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXT) },
39         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXTLIVE) },
40         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXTPRO) },
41         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_GUITARPORT) },
42         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_POCKETPOD) },
43         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODX3) },
44         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODX3LIVE) },
45         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXT) },
46         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXTLIVE) },
47         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXTPRO) },
48         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_GX) },
49         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_UX1) },
50         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_UX2) },
51         { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_VARIAX) },
52         { },
53 };
54 MODULE_DEVICE_TABLE(usb, line6_id_table);
55
56 static struct line6_properties line6_properties_table[] = {
57         { "BassPODxt",        LINE6_BIT_BASSPODXT,     LINE6_BIT_CONTROL_PCM },
58         { "BassPODxt Live",   LINE6_BIT_BASSPODXTLIVE, LINE6_BIT_CONTROL_PCM },
59         { "BassPODxt Pro",    LINE6_BIT_BASSPODXTPRO,  LINE6_BIT_CONTROL_PCM },
60         { "GuitarPort",       LINE6_BIT_GUITARPORT,    LINE6_BIT_PCM         },
61         { "Pocket POD",       LINE6_BIT_POCKETPOD,     LINE6_BIT_CONTROL_PCM },
62         { "POD X3",           LINE6_BIT_PODX3,         LINE6_BIT_PCM         },
63         { "POD X3 Live",      LINE6_BIT_PODX3LIVE,     LINE6_BIT_PCM         },
64         { "PODxt",            LINE6_BIT_PODXT,         LINE6_BIT_CONTROL_PCM },
65         { "PODxt Live",       LINE6_BIT_PODXTLIVE,     LINE6_BIT_CONTROL_PCM },
66         { "PODxt Pro",        LINE6_BIT_PODXTPRO,      LINE6_BIT_CONTROL_PCM },
67         { "TonePort GX",      LINE6_BIT_TONEPORT_GX,   LINE6_BIT_PCM         },
68         { "TonePort UX1",     LINE6_BIT_TONEPORT_UX1,  LINE6_BIT_PCM         },
69         { "TonePort UX2",     LINE6_BIT_TONEPORT_UX2,  LINE6_BIT_PCM         },
70         { "Variax Workbench", LINE6_BIT_VARIAX,        LINE6_BIT_CONTROL     }
71 };
72
73
74 /*
75         This is Line6's MIDI manufacturer ID.
76 */
77 const unsigned char line6_midi_id[] = { 0x00, 0x01, 0x0c };
78
79 struct usb_line6 *line6_devices[LINE6_MAX_DEVICES];
80 struct workqueue_struct *line6_workqueue;
81
82
83 /**
84          Class for asynchronous messages.
85 */
86 struct message {
87         struct usb_line6 *line6;
88         const char *buffer;
89         int size;
90         int done;
91 };
92
93
94 /*
95         Forward declarations.
96 */
97 static void line6_data_received(struct urb *urb);
98 static int line6_send_raw_message_async_part(struct message *msg,
99                                              struct urb *urb);
100
101
102 /*
103         Start to listen on endpoint.
104 */
105 static int line6_start_listen(struct usb_line6 *line6)
106 {
107         usb_fill_int_urb(line6->urb_listen, line6->usbdev,
108                          usb_rcvintpipe(line6->usbdev, line6->ep_control_read),
109                          line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
110                          line6_data_received, line6, line6->interval);
111         line6->urb_listen->actual_length = 0;
112         return usb_submit_urb(line6->urb_listen, GFP_KERNEL);
113 }
114
115 #if DO_DUMP_ANY
116 /*
117         Write hexdump to syslog.
118 */
119 void line6_write_hexdump(struct usb_line6 *line6, char dir,
120                          const unsigned char *buffer, int size)
121 {
122         static const int BYTES_PER_LINE = 8;
123         char hexdump[100];
124         char asc[BYTES_PER_LINE + 1];
125         int i, j;
126
127         for (i = 0; i < size; i += BYTES_PER_LINE) {
128                 int hexdumpsize = sizeof(hexdump);
129                 char *p = hexdump;
130                 int n = min(size - i, BYTES_PER_LINE);
131                 asc[n] = 0;
132
133                 for (j = 0; j < BYTES_PER_LINE; ++j) {
134                         int bytes;
135
136                         if (j < n) {
137                                 unsigned char val = buffer[i + j];
138                                 bytes = snprintf(p, hexdumpsize, " %02X", val);
139                                 asc[j] = ((val >= 0x20) && (val < 0x7f)) ? val : '.';
140                         } else
141                                 bytes = snprintf(p, hexdumpsize, "   ");
142
143                         if (bytes > hexdumpsize)
144                                 break;  /* buffer overflow */
145
146                         p += bytes;
147                         hexdumpsize -= bytes;
148                 }
149
150                 dev_info(line6->ifcdev, "%c%04X:%s %s\n", dir, i, hexdump, asc);
151         }
152 }
153 #endif
154
155 #if DO_DUMP_URB_RECEIVE
156 /*
157         Dump URB data to syslog.
158 */
159 static void line6_dump_urb(struct urb *urb)
160 {
161         struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
162
163         if (urb->status < 0)
164                 return;
165
166         line6_write_hexdump(line6, 'R', (unsigned char *)urb->transfer_buffer,
167                             urb->actual_length);
168 }
169 #endif
170
171 /*
172         Send raw message in pieces of max_packet_size bytes.
173 */
174 int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
175                            int size)
176 {
177         int i, done = 0;
178         int actual_size;
179
180 #if DO_DUMP_URB_SEND
181         line6_write_hexdump(line6, 'S', buffer, size);
182 #endif
183
184         for (i = 0; i < size; i += actual_size) {
185                 const char *frag_buf = buffer + i;
186                 int frag_size = min(line6->max_packet_size, size - i);
187                 int retval;
188
189                 retval = usb_interrupt_msg(line6->usbdev,
190                                            usb_sndintpipe(line6->usbdev,
191                                                           line6->ep_control_write),
192                                            (char *)frag_buf, frag_size,
193                                            &actual_size, LINE6_TIMEOUT * HZ);
194
195                 if (retval) {
196                         dev_err(line6->ifcdev,
197                                 "usb_interrupt_msg failed (%d)\n", retval);
198                         break;
199                 }
200
201                 done += actual_size;
202         }
203
204         return done;
205 }
206
207 /*
208         Notification of completion of asynchronous request transmission.
209 */
210 static void line6_async_request_sent(struct urb *urb)
211 {
212         struct message *msg = (struct message *)urb->context;
213
214         if (msg->done >= msg->size) {
215                 usb_free_urb(urb);
216                 kfree(msg);
217         } else
218                 line6_send_raw_message_async_part(msg, urb);
219 }
220
221 /*
222         Asynchronously send part of a raw message.
223 */
224 static int line6_send_raw_message_async_part(struct message *msg,
225                                              struct urb *urb)
226 {
227         int retval;
228         struct usb_line6 *line6 = msg->line6;
229         int done = msg->done;
230         int bytes = min(msg->size - done, line6->max_packet_size);
231
232         usb_fill_int_urb(urb, line6->usbdev,
233                          usb_sndintpipe(line6->usbdev, line6->ep_control_write),
234                          (char *)msg->buffer + done, bytes,
235                          line6_async_request_sent, msg, line6->interval);
236
237 #if DO_DUMP_URB_SEND
238         line6_write_hexdump(line6, 'S', (char *)msg->buffer + done, bytes);
239 #endif
240
241         msg->done += bytes;
242         retval = usb_submit_urb(urb, GFP_ATOMIC);
243
244         if (retval < 0) {
245                 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
246                         __func__, retval);
247                 usb_free_urb(urb);
248                 kfree(msg);
249                 return -EINVAL;
250         }
251
252         return 0;
253 }
254
255 /*
256         Asynchronously send raw message.
257 */
258 int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
259                                  int size)
260 {
261         struct message *msg;
262         struct urb *urb;
263
264         /* create message: */
265         msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
266
267         if (msg == NULL) {
268                 dev_err(line6->ifcdev, "Out of memory\n");
269                 return -ENOMEM;
270         }
271
272         /* create URB: */
273         urb = usb_alloc_urb(0, GFP_ATOMIC);
274
275         if (urb == NULL) {
276                 kfree(msg);
277                 dev_err(line6->ifcdev, "Out of memory\n");
278                 return -ENOMEM;
279         }
280
281         /* set message data: */
282         msg->line6 = line6;
283         msg->buffer = buffer;
284         msg->size = size;
285         msg->done = 0;
286
287         /* start sending: */
288         return line6_send_raw_message_async_part(msg, urb);
289 }
290
291 /*
292         Send sysex message in pieces of wMaxPacketSize bytes.
293 */
294 int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
295                              int size)
296 {
297         return line6_send_raw_message(line6, buffer, size + SYSEX_EXTRA_SIZE) - SYSEX_EXTRA_SIZE;
298 }
299
300 /*
301         Allocate buffer for sysex message and prepare header.
302         @param code sysex message code
303         @param size number of bytes between code and sysex end
304 */
305 char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
306                                int size)
307 {
308         char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_KERNEL);
309
310         if (!buffer) {
311                 dev_err(line6->ifcdev, "out of memory\n");
312                 return NULL;
313         }
314
315         buffer[0] = LINE6_SYSEX_BEGIN;
316         memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
317         buffer[sizeof(line6_midi_id) + 1] = code1;
318         buffer[sizeof(line6_midi_id) + 2] = code2;
319         buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
320         return buffer;
321 }
322
323 /*
324         Notification of data received from the Line6 device.
325 */
326 static void line6_data_received(struct urb *urb)
327 {
328         struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
329         struct MidiBuffer *mb = &line6->line6midi->midibuf_in;
330         int done;
331
332         if (urb->status == -ESHUTDOWN)
333                 return;
334
335 #if DO_DUMP_URB_RECEIVE
336         line6_dump_urb(urb);
337 #endif
338
339         done = midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
340
341         if (done < urb->actual_length) {
342                 midibuf_ignore(mb, done);
343                 DEBUG_MESSAGES(dev_err(line6->ifcdev, "%d %d buffer overflow - message skipped\n", done, urb->actual_length));
344         }
345
346         for (;;) {
347                 done = midibuf_read(mb, line6->buffer_message, LINE6_MESSAGE_MAXLEN);
348
349                 if (done == 0)
350                         break;
351
352                 /* MIDI input filter */
353                 if (midibuf_skip_message(mb, line6->line6midi->midi_mask_receive))
354                         continue;
355
356                 line6->message_length = done;
357 #if DO_DUMP_MIDI_RECEIVE
358                 line6_write_hexdump(line6, 'r', line6->buffer_message, done);
359 #endif
360                 line6_midi_receive(line6, line6->buffer_message, done);
361
362                 switch (line6->usbdev->descriptor.idProduct) {
363                 case LINE6_DEVID_BASSPODXT:
364                 case LINE6_DEVID_BASSPODXTLIVE:
365                 case LINE6_DEVID_BASSPODXTPRO:
366                 case LINE6_DEVID_PODXT:
367                 case LINE6_DEVID_PODXTPRO:
368                 case LINE6_DEVID_POCKETPOD:
369                         pod_process_message((struct usb_line6_pod *)line6);
370                         break;
371
372                 case LINE6_DEVID_PODXTLIVE:
373                         switch (line6->interface_number) {
374                         case PODXTLIVE_INTERFACE_POD:
375                                 pod_process_message((struct usb_line6_pod *)line6);
376                                 break;
377
378                         case PODXTLIVE_INTERFACE_VARIAX:
379                                 variax_process_message((struct usb_line6_variax *)line6);
380                                 break;
381
382                         default:
383                                 dev_err(line6->ifcdev, "PODxt Live interface %d not supported\n", line6->interface_number);
384                         }
385                         break;
386
387                 case LINE6_DEVID_VARIAX:
388                         variax_process_message((struct usb_line6_variax *)line6);
389                         break;
390
391                 default:
392                         MISSING_CASE;
393                 }
394         }
395
396         line6_start_listen(line6);
397 }
398
399 static int line6_send(struct usb_line6 *line6, unsigned char *buf, size_t len)
400 {
401         int retval;
402         int partial;
403
404 #if DO_DUMP_URB_SEND
405         line6_write_hexdump(line6, 'S', buf, len);
406 #endif
407
408         retval = usb_interrupt_msg(line6->usbdev,
409                                    usb_sndintpipe(line6->usbdev,
410                                                   line6->ep_control_write),
411                                    buf, len, &partial,
412                                    LINE6_TIMEOUT * HZ);
413
414         if (retval) {
415                 dev_err(line6->ifcdev,
416                         "usb_interrupt_msg failed (%d)\n", retval);
417         }
418
419         if (partial != len) {
420                 dev_err(line6->ifcdev,
421                         "usb_interrupt_msg sent partial message (%d)\n",
422                          retval);
423         }
424
425         return retval;
426 }
427
428 /*
429         Send channel number (i.e., switch to a different sound).
430 */
431 int line6_send_program(struct usb_line6 *line6, int value)
432 {
433         unsigned char *buffer;
434         size_t len = 2;
435
436         buffer = kmalloc(len, GFP_KERNEL);
437         if (!buffer) {
438                 dev_err(line6->ifcdev, "out of memory\n");
439                 return -ENOMEM;
440         }
441
442         buffer[0] = LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST;
443         buffer[1] = value;
444
445         return line6_send(line6, buffer, len);
446 }
447
448 /*
449         Transmit Line6 control parameter.
450 */
451 int line6_transmit_parameter(struct usb_line6 *line6, int param, int value)
452 {
453         unsigned char *buffer;
454         size_t len = 3;
455
456         buffer = kmalloc(len, GFP_KERNEL);
457         if (!buffer) {
458                 dev_err(line6->ifcdev, "out of memory\n");
459                 return -ENOMEM;
460         }
461
462         buffer[0] = LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST;
463         buffer[1] = param;
464         buffer[2] = value;
465
466         return line6_send(line6, buffer, len);
467 }
468
469 /*
470         Read data from device.
471 */
472 int line6_read_data(struct usb_line6 *line6, int address, void *data, size_t datalen)
473 {
474         struct usb_device *usbdev = line6->usbdev;
475         int ret;
476         unsigned char len;
477
478         /* query the serial number: */
479         ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
480                                       USB_TYPE_VENDOR | USB_RECIP_DEVICE
481                                       | USB_DIR_OUT,
482                                       (datalen << 8) | 0x21, address,
483                                       NULL, 0, LINE6_TIMEOUT * HZ);
484
485         if (ret < 0) {
486                 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
487                 return ret;
488         }
489
490         /* Wait for data length. We'll get a couple of 0xff until length arrives. */
491         do {
492                 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
493                                       USB_TYPE_VENDOR | USB_RECIP_DEVICE |
494                                       USB_DIR_IN,
495                                       0x0012, 0x0000, &len, 1,
496                                       LINE6_TIMEOUT * HZ);
497                 if (ret < 0) {
498                         dev_err(line6->ifcdev,
499                                 "receive length failed (error %d)\n", ret);
500                         return ret;
501                 }
502         } while (len == 0xff);
503
504         if (len != datalen) {
505                 /* should be equal or something went wrong */
506                 dev_err(line6->ifcdev,
507                         "length mismatch (expected %d, got %d)\n",
508                         (int)datalen, (int)len);
509                 return -EINVAL;
510         }
511
512         /* receive the result: */
513         ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
514                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
515                               0x0013, 0x0000, data, datalen,
516                               LINE6_TIMEOUT * HZ);
517
518         if (ret < 0) {
519                 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
520                 return ret;
521         }
522
523         return 0;
524 }
525
526 /*
527         Write data to device.
528 */
529 int line6_write_data(struct usb_line6 *line6, int address, void *data,
530                      size_t datalen)
531 {
532         struct usb_device *usbdev = line6->usbdev;
533         int ret;
534         unsigned char status;
535
536         ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
537                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
538                               0x0022, address, data, datalen,
539                               LINE6_TIMEOUT * HZ);
540
541         if (ret < 0) {
542                 dev_err(line6->ifcdev,
543                         "write request failed (error %d)\n", ret);
544                 return ret;
545         }
546
547         do {
548                 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
549                                       0x67,
550                                       USB_TYPE_VENDOR | USB_RECIP_DEVICE |
551                                       USB_DIR_IN,
552                                       0x0012, 0x0000,
553                                       &status, 1, LINE6_TIMEOUT * HZ);
554
555                 if (ret < 0) {
556                         dev_err(line6->ifcdev,
557                                 "receiving status failed (error %d)\n", ret);
558                         return ret;
559                 }
560         }
561         while (status == 0xff)
562                 ;
563
564         if (status != 0) {
565                 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
566                 return -EINVAL;
567         }
568
569         return 0;
570 }
571
572 /*
573         Read Line6 device serial number.
574         (POD, TonePort, GuitarPort)
575 */
576 int line6_read_serial_number(struct usb_line6 *line6, int *serial_number)
577 {
578         return line6_read_data(line6, 0x80d0, serial_number, sizeof(*serial_number));
579 }
580
581 /*
582         No operation (i.e., unsupported).
583 */
584 ssize_t line6_nop_read(struct device *dev, struct device_attribute *attr,
585                        char *buf)
586 {
587         return 0;
588 }
589
590 /*
591         No operation (i.e., unsupported).
592 */
593 ssize_t line6_nop_write(struct device *dev, struct device_attribute *attr,
594                         const char *buf, size_t count)
595 {
596         return count;
597 }
598
599 /*
600         "write" request on "raw" special file.
601 */
602 #if CREATE_RAW_FILE
603 ssize_t line6_set_raw(struct device *dev, struct device_attribute *attr,
604                       const char *buf, size_t count)
605 {
606         struct usb_interface *interface = to_usb_interface(dev);
607         struct usb_line6 *line6 = usb_get_intfdata(interface);
608         line6_send_raw_message(line6, buf, count);
609         return count;
610 }
611 #endif
612
613 /*
614         Generic destructor.
615 */
616 static void line6_destruct(struct usb_interface *interface)
617 {
618         struct usb_line6 *line6;
619
620         if (interface == NULL)
621                 return;
622         line6 = usb_get_intfdata(interface);
623         if (line6 == NULL)
624                 return;
625
626         /* free buffer memory first: */
627         kfree(line6->buffer_message);
628         kfree(line6->buffer_listen);
629
630         /* then free URBs: */
631         usb_free_urb(line6->urb_listen);
632
633         /* make sure the device isn't destructed twice: */
634         usb_set_intfdata(interface, NULL);
635
636         /* free interface data: */
637         kfree(line6);
638 }
639
640 static void line6_list_devices(void)
641 {
642         int i;
643
644         for (i = 0; i < LINE6_MAX_DEVICES; ++i) {
645                 struct usb_line6 *dev = line6_devices[i];
646                 printk(KERN_INFO "Line6 device %d: ", i);
647
648                 if (dev == NULL)
649                         printk("(not used)\n");
650                 else
651                         printk("%s:%d\n", dev->properties->name, dev->interface_number);
652         }
653 }
654
655 /*
656         Probe USB device.
657 */
658 static int line6_probe(struct usb_interface *interface, const struct usb_device_id *id)
659 {
660         int devtype;
661         struct usb_device *usbdev = NULL;
662         struct usb_line6 *line6 = NULL;
663         const struct line6_properties *properties;
664         int devnum;
665         int interface_number, alternate = 0;
666         int product;
667         int size = 0;
668         int ep_read = 0, ep_write = 0;
669         int ret;
670
671         if (interface == NULL)
672                 return -ENODEV;
673         usbdev = interface_to_usbdev(interface);
674         if (usbdev == NULL)
675                 return -ENODEV;
676
677         /* increment reference counters: */
678         usb_get_intf(interface);
679         usb_get_dev(usbdev);
680
681         /* we don't handle multiple configurations */
682         if (usbdev->descriptor.bNumConfigurations != 1) {
683                 ret = -ENODEV;
684                 goto err_put;
685         }
686
687         /* check vendor and product id */
688         for (devtype = ARRAY_SIZE(line6_id_table) - 1; devtype--;) {
689                 u16 idVendor = le16_to_cpu(usbdev->descriptor.idVendor);
690                 u16 idProduct = le16_to_cpu(usbdev->descriptor.idProduct);
691
692                 if (idVendor == line6_id_table[devtype].idVendor
693                      && idProduct == line6_id_table[devtype].idProduct)
694                         break;
695         }
696
697         if (devtype < 0) {
698                 ret = -ENODEV;
699                 goto err_put;
700         }
701
702         /* find free slot in device table: */
703         for (devnum = 0; devnum < LINE6_MAX_DEVICES; ++devnum)
704                 if (line6_devices[devnum] == NULL)
705                         break;
706
707         if (devnum == LINE6_MAX_DEVICES) {
708                 ret = -ENODEV;
709                 goto err_put;
710         }
711
712         /* initialize device info: */
713         properties = &line6_properties_table[devtype];
714         dev_info(&interface->dev, "Line6 %s found\n", properties->name);
715         product = le16_to_cpu(usbdev->descriptor.idProduct);
716
717         /* query interface number */
718         interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
719
720         switch (product) {
721         case LINE6_DEVID_BASSPODXTLIVE:
722         case LINE6_DEVID_POCKETPOD:
723         case LINE6_DEVID_PODXTLIVE:
724         case LINE6_DEVID_VARIAX:
725                 alternate = 1;
726                 break;
727
728         case LINE6_DEVID_PODX3:
729         case LINE6_DEVID_PODX3LIVE:
730                 switch (interface_number) {
731                 case 0:
732                         alternate = 1;
733                         break;
734                 case 1:
735                         alternate = 0;
736                         break;
737                 default:
738                         MISSING_CASE;
739                 }
740                 break;
741
742         case LINE6_DEVID_BASSPODXT:
743         case LINE6_DEVID_BASSPODXTPRO:
744         case LINE6_DEVID_PODXT:
745         case LINE6_DEVID_PODXTPRO:
746                 alternate = 5;
747                 break;
748
749         case LINE6_DEVID_TONEPORT_GX:
750         case LINE6_DEVID_GUITARPORT:
751                 alternate = 2;  /* 1..4 seem to be ok */
752                 break;
753
754         case LINE6_DEVID_TONEPORT_UX1:
755         case LINE6_DEVID_TONEPORT_UX2:
756                 switch (interface_number) {
757                 case 0:
758                         /* defaults to 44.1kHz, 16-bit */
759                         alternate = 2;
760                         break;
761                 case 1:
762                         alternate = 0;
763                         break;
764                 default:
765                         MISSING_CASE;
766                 }
767                 break;
768
769         default:
770                 MISSING_CASE;
771                 ret = -ENODEV;
772                 goto err_put;
773         }
774
775         ret = usb_set_interface(usbdev, interface_number, alternate);
776         if (ret < 0) {
777                 dev_err(&interface->dev, "set_interface failed\n");
778                 goto err_put;
779         }
780
781         /* initialize device data based on product id: */
782         switch (product) {
783         case LINE6_DEVID_BASSPODXT:
784         case LINE6_DEVID_BASSPODXTLIVE:
785         case LINE6_DEVID_BASSPODXTPRO:
786         case LINE6_DEVID_POCKETPOD:
787         case LINE6_DEVID_PODXT:
788         case LINE6_DEVID_PODXTPRO:
789                 size = sizeof(struct usb_line6_pod);
790                 ep_read  = 0x84;
791                 ep_write = 0x03;
792                 break;
793
794         case LINE6_DEVID_PODX3:
795         case LINE6_DEVID_PODX3LIVE:
796                 /* currently unused! */
797                 size = sizeof(struct usb_line6_pod);
798                 ep_read  = 0x81;
799                 ep_write = 0x01;
800                 break;
801
802         case LINE6_DEVID_TONEPORT_GX:
803         case LINE6_DEVID_TONEPORT_UX1:
804         case LINE6_DEVID_TONEPORT_UX2:
805         case LINE6_DEVID_GUITARPORT:
806                 size = sizeof(struct usb_line6_toneport);
807                 /* these don't have a control channel */
808                 break;
809
810         case LINE6_DEVID_PODXTLIVE:
811                 switch (interface_number) {
812                 case PODXTLIVE_INTERFACE_POD:
813                         size = sizeof(struct usb_line6_pod);
814                         ep_read  = 0x84;
815                         ep_write = 0x03;
816                         break;
817
818                 case PODXTLIVE_INTERFACE_VARIAX:
819                         size = sizeof(struct usb_line6_variax);
820                         ep_read  = 0x86;
821                         ep_write = 0x05;
822                         break;
823
824                 default:
825                         ret = -ENODEV;
826                         goto err_put;
827                 }
828                 break;
829
830         case LINE6_DEVID_VARIAX:
831                 size = sizeof(struct usb_line6_variax);
832                 ep_read  = 0x82;
833                 ep_write = 0x01;
834                 break;
835
836         default:
837                 MISSING_CASE;
838                 ret = -ENODEV;
839                 goto err_put;
840         }
841
842         if (size == 0) {
843                 dev_err(line6->ifcdev, "driver bug: interface data size not set\n");
844                 ret = -ENODEV;
845                 goto err_put;
846         }
847
848         line6 = kzalloc(size, GFP_KERNEL);
849
850         if (line6 == NULL) {
851                 dev_err(&interface->dev, "Out of memory\n");
852                 ret = -ENODEV;
853                 goto err_put;
854         }
855
856         /* store basic data: */
857         line6->interface_number = interface_number;
858         line6->properties = properties;
859         line6->usbdev = usbdev;
860         line6->ifcdev = &interface->dev;
861         line6->ep_control_read = ep_read;
862         line6->ep_control_write = ep_write;
863         line6->product = product;
864
865         /* get data from endpoint descriptor (see usb_maxpacket): */
866         {
867                 struct usb_host_endpoint *ep;
868                 unsigned epnum = usb_pipeendpoint(usb_rcvintpipe(usbdev, ep_read));
869                 ep = usbdev->ep_in[epnum];
870
871                 if (ep != NULL) {
872                         line6->interval = ep->desc.bInterval;
873                         line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
874                 } else {
875                         line6->interval = LINE6_FALLBACK_INTERVAL;
876                         line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
877                         dev_err(line6->ifcdev, "endpoint not available, using fallback values");
878                 }
879         }
880
881         usb_set_intfdata(interface, line6);
882
883         if (properties->capabilities & LINE6_BIT_CONTROL) {
884                 /* initialize USB buffers: */
885                 line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
886
887                 if (line6->buffer_listen == NULL) {
888                         dev_err(&interface->dev, "Out of memory\n");
889                         ret = -ENOMEM;
890                         goto err_destruct;
891                 }
892
893                 line6->buffer_message = kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
894
895                 if (line6->buffer_message == NULL) {
896                         dev_err(&interface->dev, "Out of memory\n");
897                         ret = -ENOMEM;
898                         goto err_destruct;
899                 }
900
901                 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
902
903                 if (line6->urb_listen == NULL) {
904                         dev_err(&interface->dev, "Out of memory\n");
905                         line6_destruct(interface);
906                         ret = -ENOMEM;
907                         goto err_destruct;
908                 }
909
910                 ret = line6_start_listen(line6);
911                 if (ret < 0) {
912                         dev_err(&interface->dev, "%s: usb_submit_urb failed\n",
913                                 __func__);
914                         goto err_destruct;
915                 }
916         }
917
918         /* initialize device data based on product id: */
919         switch (product) {
920         case LINE6_DEVID_BASSPODXT:
921         case LINE6_DEVID_BASSPODXTLIVE:
922         case LINE6_DEVID_BASSPODXTPRO:
923         case LINE6_DEVID_POCKETPOD:
924         case LINE6_DEVID_PODX3:
925         case LINE6_DEVID_PODX3LIVE:
926         case LINE6_DEVID_PODXT:
927         case LINE6_DEVID_PODXTPRO:
928                 ret = pod_init(interface, (struct usb_line6_pod *)line6);
929                 break;
930
931         case LINE6_DEVID_PODXTLIVE:
932                 switch (interface_number) {
933                 case PODXTLIVE_INTERFACE_POD:
934                         ret = pod_init(interface, (struct usb_line6_pod *)line6);
935                         break;
936
937                 case PODXTLIVE_INTERFACE_VARIAX:
938                         ret = variax_init(interface, (struct usb_line6_variax *)line6);
939                         break;
940
941                 default:
942                         dev_err(&interface->dev,
943                                 "PODxt Live interface %d not supported\n",
944                                 interface_number);
945                         ret = -ENODEV;
946                 }
947
948                 break;
949
950         case LINE6_DEVID_VARIAX:
951                 ret = variax_init(interface, (struct usb_line6_variax *)line6);
952                 break;
953
954         case LINE6_DEVID_TONEPORT_GX:
955         case LINE6_DEVID_TONEPORT_UX1:
956         case LINE6_DEVID_TONEPORT_UX2:
957         case LINE6_DEVID_GUITARPORT:
958                 ret = toneport_init(interface, (struct usb_line6_toneport *)line6);
959                 break;
960
961         default:
962                 MISSING_CASE;
963                 ret = -ENODEV;
964         }
965
966         if (ret < 0)
967                 goto err_destruct;
968
969         ret = sysfs_create_link(&interface->dev.kobj, &usbdev->dev.kobj,
970                                 "usb_device");
971         if (ret < 0)
972                 goto err_destruct;
973
974         dev_info(&interface->dev, "Line6 %s now attached\n",
975                  line6->properties->name);
976         line6_devices[devnum] = line6;
977         line6_list_devices();
978         return 0;
979
980 err_destruct:
981         line6_destruct(interface);
982 err_put:
983         usb_put_intf(interface);
984         usb_put_dev(usbdev);
985         return ret;
986 }
987
988 /*
989         Line6 device disconnected.
990 */
991 static void line6_disconnect(struct usb_interface *interface)
992 {
993         struct usb_line6 *line6;
994         struct usb_device *usbdev;
995         int interface_number, i;
996
997         if (interface == NULL)
998                 return;
999         usbdev = interface_to_usbdev(interface);
1000         if (usbdev == NULL)
1001                 return;
1002
1003         sysfs_remove_link(&interface->dev.kobj, "usb_device");
1004
1005         interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
1006         line6 = usb_get_intfdata(interface);
1007
1008         if (line6 != NULL) {
1009                 if (line6->urb_listen != NULL)
1010                         usb_kill_urb(line6->urb_listen);
1011
1012                 if (usbdev != line6->usbdev)
1013                         dev_err(line6->ifcdev,
1014                                 "driver bug: inconsistent usb device\n");
1015
1016                 switch (line6->usbdev->descriptor.idProduct) {
1017                 case LINE6_DEVID_BASSPODXT:
1018                 case LINE6_DEVID_BASSPODXTLIVE:
1019                 case LINE6_DEVID_BASSPODXTPRO:
1020                 case LINE6_DEVID_POCKETPOD:
1021                 case LINE6_DEVID_PODX3:
1022                 case LINE6_DEVID_PODX3LIVE:
1023                 case LINE6_DEVID_PODXT:
1024                 case LINE6_DEVID_PODXTPRO:
1025                         pod_disconnect(interface);
1026                         break;
1027
1028                 case LINE6_DEVID_PODXTLIVE:
1029                         switch (interface_number) {
1030                         case PODXTLIVE_INTERFACE_POD:
1031                                 pod_disconnect(interface);
1032                                 break;
1033
1034                         case PODXTLIVE_INTERFACE_VARIAX:
1035                                 variax_disconnect(interface);
1036                                 break;
1037                         }
1038
1039                         break;
1040
1041                 case LINE6_DEVID_VARIAX:
1042                         variax_disconnect(interface);
1043                         break;
1044
1045                 case LINE6_DEVID_TONEPORT_GX:
1046                 case LINE6_DEVID_TONEPORT_UX1:
1047                 case LINE6_DEVID_TONEPORT_UX2:
1048                 case LINE6_DEVID_GUITARPORT:
1049                         toneport_disconnect(interface);
1050                         break;
1051
1052                 default:
1053                         MISSING_CASE;
1054                 }
1055
1056                 dev_info(&interface->dev, "Line6 %s now disconnected\n", line6->properties->name);
1057
1058                 for (i = LINE6_MAX_DEVICES; i--;) {
1059                         if (line6_devices[i] == line6)
1060                                 line6_devices[i] = NULL;
1061                 }
1062         }
1063
1064         line6_destruct(interface);
1065
1066         /* decrement reference counters: */
1067         usb_put_intf(interface);
1068         usb_put_dev(usbdev);
1069
1070         line6_list_devices();
1071 }
1072
1073 static struct usb_driver line6_driver = {
1074         .name = DRIVER_NAME,
1075         .probe = line6_probe,
1076         .disconnect = line6_disconnect,
1077         .id_table = line6_id_table,
1078 };
1079
1080 /*
1081         Module initialization.
1082 */
1083 static int __init line6_init(void)
1084 {
1085         int i, retval;
1086
1087         printk(KERN_INFO "%s driver version %s%s\n",
1088                DRIVER_NAME, DRIVER_VERSION, DRIVER_REVISION);
1089         line6_workqueue = create_workqueue(DRIVER_NAME);
1090
1091         if (line6_workqueue == NULL) {
1092                 err("couldn't create workqueue");
1093                 return -EINVAL;
1094         }
1095
1096         for (i = LINE6_MAX_DEVICES; i--;)
1097                 line6_devices[i] = NULL;
1098
1099         retval = usb_register(&line6_driver);
1100
1101         if (retval)
1102                 err("usb_register failed. Error number %d", retval);
1103
1104         return retval;
1105 }
1106
1107 /*
1108         Module cleanup.
1109 */
1110 static void __exit line6_exit(void)
1111 {
1112         destroy_workqueue(line6_workqueue);
1113         usb_deregister(&line6_driver);
1114 }
1115
1116 module_init(line6_init);
1117 module_exit(line6_exit);
1118
1119 MODULE_AUTHOR(DRIVER_AUTHOR);
1120 MODULE_DESCRIPTION(DRIVER_DESC);
1121 MODULE_LICENSE("GPL");
1122 MODULE_VERSION(DRIVER_VERSION);