Merge remote branch 'origin' into secretlab/next-spi
[sfrench/cifs-2.6.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                 return -ENODEV;
684
685         /* check vendor and product id */
686         for (devtype = ARRAY_SIZE(line6_id_table) - 1; devtype--;) {
687                 u16 idVendor = le16_to_cpu(usbdev->descriptor.idVendor);
688                 u16 idProduct = le16_to_cpu(usbdev->descriptor.idProduct);
689
690                 if (idVendor == line6_id_table[devtype].idVendor
691                      && idProduct == line6_id_table[devtype].idProduct)
692                         break;
693         }
694
695         if (devtype < 0)
696                 return -ENODEV;
697
698         /* find free slot in device table: */
699         for (devnum = 0; devnum < LINE6_MAX_DEVICES; ++devnum)
700                 if (line6_devices[devnum] == NULL)
701                         break;
702
703         if (devnum == LINE6_MAX_DEVICES)
704                 return -ENODEV;
705
706         /* initialize device info: */
707         properties = &line6_properties_table[devtype];
708         dev_info(&interface->dev, "Line6 %s found\n", properties->name);
709         product = le16_to_cpu(usbdev->descriptor.idProduct);
710
711         /* query interface number */
712         interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
713
714         switch (product) {
715         case LINE6_DEVID_BASSPODXTLIVE:
716         case LINE6_DEVID_POCKETPOD:
717         case LINE6_DEVID_PODXTLIVE:
718         case LINE6_DEVID_VARIAX:
719                 alternate = 1;
720                 break;
721
722         case LINE6_DEVID_PODX3:
723         case LINE6_DEVID_PODX3LIVE:
724                 switch (interface_number) {
725                 case 0:
726                         alternate = 1;
727                         break;
728                 case 1:
729                         alternate = 0;
730                         break;
731                 default:
732                         MISSING_CASE;
733                 }
734                 break;
735
736         case LINE6_DEVID_BASSPODXT:
737         case LINE6_DEVID_BASSPODXTPRO:
738         case LINE6_DEVID_PODXT:
739         case LINE6_DEVID_PODXTPRO:
740                 alternate = 5;
741                 break;
742
743         case LINE6_DEVID_TONEPORT_GX:
744         case LINE6_DEVID_GUITARPORT:
745                 alternate = 2;  /* 1..4 seem to be ok */
746                 break;
747
748         case LINE6_DEVID_TONEPORT_UX1:
749         case LINE6_DEVID_TONEPORT_UX2:
750                 switch (interface_number) {
751                 case 0:
752                         /* defaults to 44.1kHz, 16-bit */
753                         alternate = 2;
754                         break;
755                 case 1:
756                         alternate = 0;
757                         break;
758                 default:
759                         MISSING_CASE;
760                 }
761                 break;
762
763         default:
764                 MISSING_CASE;
765                 return -ENODEV;
766         }
767
768         ret = usb_set_interface(usbdev, interface_number, alternate);
769         if (ret < 0) {
770                 dev_err(&interface->dev, "set_interface failed\n");
771                 return ret;
772         }
773
774         /* initialize device data based on product id: */
775         switch (product) {
776         case LINE6_DEVID_BASSPODXT:
777         case LINE6_DEVID_BASSPODXTLIVE:
778         case LINE6_DEVID_BASSPODXTPRO:
779         case LINE6_DEVID_POCKETPOD:
780         case LINE6_DEVID_PODXT:
781         case LINE6_DEVID_PODXTPRO:
782                 size = sizeof(struct usb_line6_pod);
783                 ep_read  = 0x84;
784                 ep_write = 0x03;
785                 break;
786
787         case LINE6_DEVID_PODX3:
788         case LINE6_DEVID_PODX3LIVE:
789                 /* currently unused! */
790                 size = sizeof(struct usb_line6_pod);
791                 ep_read  = 0x81;
792                 ep_write = 0x01;
793                 break;
794
795         case LINE6_DEVID_TONEPORT_GX:
796         case LINE6_DEVID_TONEPORT_UX1:
797         case LINE6_DEVID_TONEPORT_UX2:
798         case LINE6_DEVID_GUITARPORT:
799                 size = sizeof(struct usb_line6_toneport);
800                 /* these don't have a control channel */
801                 break;
802
803         case LINE6_DEVID_PODXTLIVE:
804                 switch (interface_number) {
805                 case PODXTLIVE_INTERFACE_POD:
806                         size = sizeof(struct usb_line6_pod);
807                         ep_read  = 0x84;
808                         ep_write = 0x03;
809                         break;
810
811                 case PODXTLIVE_INTERFACE_VARIAX:
812                         size = sizeof(struct usb_line6_variax);
813                         ep_read  = 0x86;
814                         ep_write = 0x05;
815                         break;
816
817                 default:
818                         return -ENODEV;
819                 }
820                 break;
821
822         case LINE6_DEVID_VARIAX:
823                 size = sizeof(struct usb_line6_variax);
824                 ep_read  = 0x82;
825                 ep_write = 0x01;
826                 break;
827
828         default:
829                 MISSING_CASE;
830                 return -ENODEV;
831         }
832
833         if (size == 0) {
834                 dev_err(line6->ifcdev, "driver bug: interface data size not set\n");
835                 return -ENODEV;
836         }
837
838         line6 = kzalloc(size, GFP_KERNEL);
839
840         if (line6 == NULL) {
841                 dev_err(&interface->dev, "Out of memory\n");
842                 return -ENOMEM;
843         }
844
845         /* store basic data: */
846         line6->interface_number = interface_number;
847         line6->properties = properties;
848         line6->usbdev = usbdev;
849         line6->ifcdev = &interface->dev;
850         line6->ep_control_read = ep_read;
851         line6->ep_control_write = ep_write;
852         line6->product = product;
853
854         /* get data from endpoint descriptor (see usb_maxpacket): */
855         {
856                 struct usb_host_endpoint *ep;
857                 unsigned epnum = usb_pipeendpoint(usb_rcvintpipe(usbdev, ep_read));
858                 ep = usbdev->ep_in[epnum];
859
860                 if (ep != NULL) {
861                         line6->interval = ep->desc.bInterval;
862                         line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
863                 } else {
864                         line6->interval = LINE6_FALLBACK_INTERVAL;
865                         line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
866                         dev_err(line6->ifcdev, "endpoint not available, using fallback values");
867                 }
868         }
869
870         usb_set_intfdata(interface, line6);
871
872         if (properties->capabilities & LINE6_BIT_CONTROL) {
873                 /* initialize USB buffers: */
874                 line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
875
876                 if (line6->buffer_listen == NULL) {
877                         dev_err(&interface->dev, "Out of memory\n");
878                         line6_destruct(interface);
879                         return -ENOMEM;
880                 }
881
882                 line6->buffer_message = kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
883
884                 if (line6->buffer_message == NULL) {
885                         dev_err(&interface->dev, "Out of memory\n");
886                         line6_destruct(interface);
887                         return -ENOMEM;
888                 }
889
890                 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
891
892                 if (line6->urb_listen == NULL) {
893                         dev_err(&interface->dev, "Out of memory\n");
894                         line6_destruct(interface);
895                         return -ENOMEM;
896                 }
897
898                 ret = line6_start_listen(line6);
899                 if (ret < 0) {
900                         dev_err(&interface->dev, "%s: usb_submit_urb failed\n",
901                                 __func__);
902                         line6_destruct(interface);
903                         return ret;
904                 }
905         }
906
907         /* initialize device data based on product id: */
908         switch (product) {
909         case LINE6_DEVID_BASSPODXT:
910         case LINE6_DEVID_BASSPODXTLIVE:
911         case LINE6_DEVID_BASSPODXTPRO:
912         case LINE6_DEVID_POCKETPOD:
913         case LINE6_DEVID_PODX3:
914         case LINE6_DEVID_PODX3LIVE:
915         case LINE6_DEVID_PODXT:
916         case LINE6_DEVID_PODXTPRO:
917                 ret = pod_init(interface, (struct usb_line6_pod *)line6);
918                 break;
919
920         case LINE6_DEVID_PODXTLIVE:
921                 switch (interface_number) {
922                 case PODXTLIVE_INTERFACE_POD:
923                         ret = pod_init(interface, (struct usb_line6_pod *)line6);
924                         break;
925
926                 case PODXTLIVE_INTERFACE_VARIAX:
927                         ret = variax_init(interface, (struct usb_line6_variax *)line6);
928                         break;
929
930                 default:
931                         dev_err(&interface->dev,
932                                 "PODxt Live interface %d not supported\n",
933                                 interface_number);
934                         ret = -ENODEV;
935                 }
936
937                 break;
938
939         case LINE6_DEVID_VARIAX:
940                 ret = variax_init(interface, (struct usb_line6_variax *)line6);
941                 break;
942
943         case LINE6_DEVID_TONEPORT_GX:
944         case LINE6_DEVID_TONEPORT_UX1:
945         case LINE6_DEVID_TONEPORT_UX2:
946         case LINE6_DEVID_GUITARPORT:
947                 ret = toneport_init(interface, (struct usb_line6_toneport *)line6);
948                 break;
949
950         default:
951                 MISSING_CASE;
952                 ret = -ENODEV;
953         }
954
955         if (ret < 0) {
956                 line6_destruct(interface);
957                 return ret;
958         }
959
960         ret = sysfs_create_link(&interface->dev.kobj, &usbdev->dev.kobj,
961                                 "usb_device");
962         if (ret < 0) {
963                 line6_destruct(interface);
964                 return ret;
965         }
966
967         dev_info(&interface->dev, "Line6 %s now attached\n",
968                  line6->properties->name);
969         line6_devices[devnum] = line6;
970         line6_list_devices();
971         return ret;
972 }
973
974 /*
975         Line6 device disconnected.
976 */
977 static void line6_disconnect(struct usb_interface *interface)
978 {
979         struct usb_line6 *line6;
980         struct usb_device *usbdev;
981         int interface_number, i;
982
983         if (interface == NULL)
984                 return;
985         usbdev = interface_to_usbdev(interface);
986         if (usbdev == NULL)
987                 return;
988
989         sysfs_remove_link(&interface->dev.kobj, "usb_device");
990
991         interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
992         line6 = usb_get_intfdata(interface);
993
994         if (line6 != NULL) {
995                 if (line6->urb_listen != NULL)
996                         usb_kill_urb(line6->urb_listen);
997
998                 if (usbdev != line6->usbdev)
999                         dev_err(line6->ifcdev,
1000                                 "driver bug: inconsistent usb device\n");
1001
1002                 switch (line6->usbdev->descriptor.idProduct) {
1003                 case LINE6_DEVID_BASSPODXT:
1004                 case LINE6_DEVID_BASSPODXTLIVE:
1005                 case LINE6_DEVID_BASSPODXTPRO:
1006                 case LINE6_DEVID_POCKETPOD:
1007                 case LINE6_DEVID_PODX3:
1008                 case LINE6_DEVID_PODX3LIVE:
1009                 case LINE6_DEVID_PODXT:
1010                 case LINE6_DEVID_PODXTPRO:
1011                         pod_disconnect(interface);
1012                         break;
1013
1014                 case LINE6_DEVID_PODXTLIVE:
1015                         switch (interface_number) {
1016                         case PODXTLIVE_INTERFACE_POD:
1017                                 pod_disconnect(interface);
1018                                 break;
1019
1020                         case PODXTLIVE_INTERFACE_VARIAX:
1021                                 variax_disconnect(interface);
1022                                 break;
1023                         }
1024
1025                         break;
1026
1027                 case LINE6_DEVID_VARIAX:
1028                         variax_disconnect(interface);
1029                         break;
1030
1031                 case LINE6_DEVID_TONEPORT_GX:
1032                 case LINE6_DEVID_TONEPORT_UX1:
1033                 case LINE6_DEVID_TONEPORT_UX2:
1034                 case LINE6_DEVID_GUITARPORT:
1035                         toneport_disconnect(interface);
1036                         break;
1037
1038                 default:
1039                         MISSING_CASE;
1040                 }
1041
1042                 dev_info(&interface->dev, "Line6 %s now disconnected\n", line6->properties->name);
1043
1044                 for (i = LINE6_MAX_DEVICES; i--;) {
1045                         if (line6_devices[i] == line6)
1046                                 line6_devices[i] = NULL;
1047                 }
1048         }
1049
1050         line6_destruct(interface);
1051
1052         /* decrement reference counters: */
1053         usb_put_intf(interface);
1054         usb_put_dev(usbdev);
1055
1056         line6_list_devices();
1057 }
1058
1059 static struct usb_driver line6_driver = {
1060         .name = DRIVER_NAME,
1061         .probe = line6_probe,
1062         .disconnect = line6_disconnect,
1063         .id_table = line6_id_table,
1064 };
1065
1066 /*
1067         Module initialization.
1068 */
1069 static int __init line6_init(void)
1070 {
1071         int i, retval;
1072
1073         printk(KERN_INFO "%s driver version %s%s\n",
1074                DRIVER_NAME, DRIVER_VERSION, DRIVER_REVISION);
1075         line6_workqueue = create_workqueue(DRIVER_NAME);
1076
1077         if (line6_workqueue == NULL) {
1078                 err("couldn't create workqueue");
1079                 return -EINVAL;
1080         }
1081
1082         for (i = LINE6_MAX_DEVICES; i--;)
1083                 line6_devices[i] = NULL;
1084
1085         retval = usb_register(&line6_driver);
1086
1087         if (retval)
1088                 err("usb_register failed. Error number %d", retval);
1089
1090         return retval;
1091 }
1092
1093 /*
1094         Module cleanup.
1095 */
1096 static void __exit line6_exit(void)
1097 {
1098         destroy_workqueue(line6_workqueue);
1099         usb_deregister(&line6_driver);
1100 }
1101
1102 module_init(line6_init);
1103 module_exit(line6_exit);
1104
1105 MODULE_AUTHOR(DRIVER_AUTHOR);
1106 MODULE_DESCRIPTION(DRIVER_DESC);
1107 MODULE_LICENSE("GPL");
1108 MODULE_VERSION(DRIVER_VERSION);