i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / staging / line6 / driver.c
blob6a1959e16e00f0344cddd3519164dba9ba4ec56c
1 /*
2 * Line6 Linux USB driver - 0.9.1beta
4 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
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.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/usb.h>
17 #include "audio.h"
18 #include "capture.h"
19 #include "control.h"
20 #include "driver.h"
21 #include "midi.h"
22 #include "playback.h"
23 #include "pod.h"
24 #include "podhd.h"
25 #include "revision.h"
26 #include "toneport.h"
27 #include "usbdefs.h"
28 #include "variax.h"
30 #define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
31 #define DRIVER_DESC "Line6 USB Driver"
32 #define DRIVER_VERSION "0.9.1beta" DRIVER_REVISION
34 /* table of devices that work with this driver */
35 static const struct usb_device_id line6_id_table[] = {
36 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXT)},
37 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXTLIVE)},
38 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXTPRO)},
39 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_GUITARPORT)},
40 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_POCKETPOD)},
41 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODHD300)},
42 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODHD500)},
43 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODSTUDIO_GX)},
44 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODSTUDIO_UX1)},
45 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODSTUDIO_UX2)},
46 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODX3)},
47 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODX3LIVE)},
48 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXT)},
49 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXTLIVE)},
50 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXTPRO)},
51 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_GX)},
52 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_UX1)},
53 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_UX2)},
54 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_VARIAX)},
55 {},
58 MODULE_DEVICE_TABLE(usb, line6_id_table);
60 /* *INDENT-OFF* */
61 static struct line6_properties line6_properties_table[] = {
62 { LINE6_BIT_BASSPODXT, "BassPODxt", "BassPODxt", LINE6_BIT_CONTROL_PCM_HWMON },
63 { LINE6_BIT_BASSPODXTLIVE, "BassPODxtLive", "BassPODxt Live", LINE6_BIT_CONTROL_PCM_HWMON },
64 { LINE6_BIT_BASSPODXTPRO, "BassPODxtPro", "BassPODxt Pro", LINE6_BIT_CONTROL_PCM_HWMON },
65 { LINE6_BIT_GUITARPORT, "GuitarPort", "GuitarPort", LINE6_BIT_PCM },
66 { LINE6_BIT_POCKETPOD, "PocketPOD", "Pocket POD", LINE6_BIT_CONTROL },
67 { LINE6_BIT_PODHD300, "PODHD300", "POD HD300", LINE6_BIT_CONTROL_PCM_HWMON },
68 { LINE6_BIT_PODHD500, "PODHD500", "POD HD500", LINE6_BIT_CONTROL_PCM_HWMON },
69 { LINE6_BIT_PODSTUDIO_GX, "PODStudioGX", "POD Studio GX", LINE6_BIT_PCM },
70 { LINE6_BIT_PODSTUDIO_UX1, "PODStudioUX1", "POD Studio UX1", LINE6_BIT_PCM },
71 { LINE6_BIT_PODSTUDIO_UX2, "PODStudioUX2", "POD Studio UX2", LINE6_BIT_PCM },
72 { LINE6_BIT_PODX3, "PODX3", "POD X3", LINE6_BIT_PCM },
73 { LINE6_BIT_PODX3LIVE, "PODX3Live", "POD X3 Live", LINE6_BIT_PCM },
74 { LINE6_BIT_PODXT, "PODxt", "PODxt", LINE6_BIT_CONTROL_PCM_HWMON },
75 { LINE6_BIT_PODXTLIVE, "PODxtLive", "PODxt Live", LINE6_BIT_CONTROL_PCM_HWMON },
76 { LINE6_BIT_PODXTPRO, "PODxtPro", "PODxt Pro", LINE6_BIT_CONTROL_PCM_HWMON },
77 { LINE6_BIT_TONEPORT_GX, "TonePortGX", "TonePort GX", LINE6_BIT_PCM },
78 { LINE6_BIT_TONEPORT_UX1, "TonePortUX1", "TonePort UX1", LINE6_BIT_PCM },
79 { LINE6_BIT_TONEPORT_UX2, "TonePortUX2", "TonePort UX2", LINE6_BIT_PCM },
80 { LINE6_BIT_VARIAX, "Variax", "Variax Workbench", LINE6_BIT_CONTROL },
82 /* *INDENT-ON* */
85 This is Line6's MIDI manufacturer ID.
87 const unsigned char line6_midi_id[] = {
88 0x00, 0x01, 0x0c
92 Code to request version of POD, Variax interface
93 (and maybe other devices).
95 static const char line6_request_version0[] = {
96 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
100 Copy of version request code with GFP_KERNEL flag for use in URB.
102 static const char *line6_request_version;
104 struct usb_line6 *line6_devices[LINE6_MAX_DEVICES];
107 Class for asynchronous messages.
109 struct message {
110 struct usb_line6 *line6;
111 const char *buffer;
112 int size;
113 int done;
117 Forward declarations.
119 static void line6_data_received(struct urb *urb);
120 static int line6_send_raw_message_async_part(struct message *msg,
121 struct urb *urb);
124 Start to listen on endpoint.
126 static int line6_start_listen(struct usb_line6 *line6)
128 int err;
129 usb_fill_int_urb(line6->urb_listen, line6->usbdev,
130 usb_rcvintpipe(line6->usbdev, line6->ep_control_read),
131 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
132 line6_data_received, line6, line6->interval);
133 line6->urb_listen->actual_length = 0;
134 err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
135 return err;
139 Stop listening on endpoint.
141 static void line6_stop_listen(struct usb_line6 *line6)
143 usb_kill_urb(line6->urb_listen);
146 #ifdef CONFIG_LINE6_USB_DUMP_ANY
148 Write hexdump to syslog.
150 void line6_write_hexdump(struct usb_line6 *line6, char dir,
151 const unsigned char *buffer, int size)
153 static const int BYTES_PER_LINE = 8;
154 char hexdump[100];
155 char asc[BYTES_PER_LINE + 1];
156 int i, j;
158 for (i = 0; i < size; i += BYTES_PER_LINE) {
159 int hexdumpsize = sizeof(hexdump);
160 char *p = hexdump;
161 int n = min(size - i, BYTES_PER_LINE);
162 asc[n] = 0;
164 for (j = 0; j < BYTES_PER_LINE; ++j) {
165 int bytes;
167 if (j < n) {
168 unsigned char val = buffer[i + j];
169 bytes = snprintf(p, hexdumpsize, " %02X", val);
170 asc[j] = ((val >= 0x20)
171 && (val < 0x7f)) ? val : '.';
172 } else
173 bytes = snprintf(p, hexdumpsize, " ");
175 if (bytes > hexdumpsize)
176 break; /* buffer overflow */
178 p += bytes;
179 hexdumpsize -= bytes;
182 dev_info(line6->ifcdev, "%c%04X:%s %s\n", dir, i, hexdump, asc);
185 #endif
187 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
189 Dump URB data to syslog.
191 static void line6_dump_urb(struct urb *urb)
193 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
195 if (urb->status < 0)
196 return;
198 line6_write_hexdump(line6, 'R', (unsigned char *)urb->transfer_buffer,
199 urb->actual_length);
201 #endif
204 Send raw message in pieces of wMaxPacketSize bytes.
206 int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
207 int size)
209 int i, done = 0;
211 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
212 line6_write_hexdump(line6, 'S', buffer, size);
213 #endif
215 for (i = 0; i < size; i += line6->max_packet_size) {
216 int partial;
217 const char *frag_buf = buffer + i;
218 int frag_size = min(line6->max_packet_size, size - i);
219 int retval;
221 retval = usb_interrupt_msg(line6->usbdev,
222 usb_sndintpipe(line6->usbdev,
223 line6->ep_control_write),
224 (char *)frag_buf, frag_size,
225 &partial, LINE6_TIMEOUT * HZ);
227 if (retval) {
228 dev_err(line6->ifcdev,
229 "usb_interrupt_msg failed (%d)\n", retval);
230 break;
233 done += frag_size;
236 return done;
240 Notification of completion of asynchronous request transmission.
242 static void line6_async_request_sent(struct urb *urb)
244 struct message *msg = (struct message *)urb->context;
246 if (msg->done >= msg->size) {
247 usb_free_urb(urb);
248 kfree(msg);
249 } else
250 line6_send_raw_message_async_part(msg, urb);
254 Asynchronously send part of a raw message.
256 static int line6_send_raw_message_async_part(struct message *msg,
257 struct urb *urb)
259 int retval;
260 struct usb_line6 *line6 = msg->line6;
261 int done = msg->done;
262 int bytes = min(msg->size - done, line6->max_packet_size);
264 usb_fill_int_urb(urb, line6->usbdev,
265 usb_sndintpipe(line6->usbdev, line6->ep_control_write),
266 (char *)msg->buffer + done, bytes,
267 line6_async_request_sent, msg, line6->interval);
269 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
270 line6_write_hexdump(line6, 'S', (char *)msg->buffer + done, bytes);
271 #endif
273 msg->done += bytes;
274 retval = usb_submit_urb(urb, GFP_ATOMIC);
276 if (retval < 0) {
277 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
278 __func__, retval);
279 usb_free_urb(urb);
280 kfree(msg);
281 return -EINVAL;
284 return 0;
288 Setup and start timer.
290 void line6_start_timer(struct timer_list *timer, unsigned int msecs,
291 void (*function) (unsigned long), unsigned long data)
293 setup_timer(timer, function, data);
294 timer->expires = jiffies + msecs * HZ / 1000;
295 add_timer(timer);
299 Asynchronously send raw message.
301 int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
302 int size)
304 struct message *msg;
305 struct urb *urb;
307 /* create message: */
308 msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
310 if (msg == NULL) {
311 dev_err(line6->ifcdev, "Out of memory\n");
312 return -ENOMEM;
315 /* create URB: */
316 urb = usb_alloc_urb(0, GFP_ATOMIC);
318 if (urb == NULL) {
319 kfree(msg);
320 dev_err(line6->ifcdev, "Out of memory\n");
321 return -ENOMEM;
324 /* set message data: */
325 msg->line6 = line6;
326 msg->buffer = buffer;
327 msg->size = size;
328 msg->done = 0;
330 /* start sending: */
331 return line6_send_raw_message_async_part(msg, urb);
335 Send asynchronous device version request.
337 int line6_version_request_async(struct usb_line6 *line6)
339 return line6_send_raw_message_async(line6, line6_request_version,
340 sizeof(line6_request_version0));
344 Send sysex message in pieces of wMaxPacketSize bytes.
346 int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
347 int size)
349 return line6_send_raw_message(line6, buffer,
350 size + SYSEX_EXTRA_SIZE) -
351 SYSEX_EXTRA_SIZE;
355 Send sysex message in pieces of wMaxPacketSize bytes.
357 int line6_send_sysex_message_async(struct usb_line6 *line6, const char *buffer,
358 int size)
360 return line6_send_raw_message_async(line6, buffer,
361 size + SYSEX_EXTRA_SIZE) -
362 SYSEX_EXTRA_SIZE;
366 Allocate buffer for sysex message and prepare header.
367 @param code sysex message code
368 @param size number of bytes between code and sysex end
370 char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
371 int size)
373 char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
375 if (!buffer) {
376 dev_err(line6->ifcdev, "out of memory\n");
377 return NULL;
380 buffer[0] = LINE6_SYSEX_BEGIN;
381 memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
382 buffer[sizeof(line6_midi_id) + 1] = code1;
383 buffer[sizeof(line6_midi_id) + 2] = code2;
384 buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
385 return buffer;
389 Notification of data received from the Line6 device.
391 static void line6_data_received(struct urb *urb)
393 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
394 struct MidiBuffer *mb = &line6->line6midi->midibuf_in;
395 int done;
397 if (urb->status == -ESHUTDOWN)
398 return;
400 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
401 line6_dump_urb(urb);
402 #endif
404 done =
405 line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
407 if (done < urb->actual_length) {
408 line6_midibuf_ignore(mb, done);
409 DEBUG_MESSAGES(dev_err
410 (line6->ifcdev,
411 "%d %d buffer overflow - message skipped\n",
412 done, urb->actual_length));
415 for (;;) {
416 done =
417 line6_midibuf_read(mb, line6->buffer_message,
418 LINE6_MESSAGE_MAXLEN);
420 if (done == 0)
421 break;
423 /* MIDI input filter */
424 if (line6_midibuf_skip_message
425 (mb, line6->line6midi->midi_mask_receive))
426 continue;
428 line6->message_length = done;
429 #ifdef CONFIG_LINE6_USB_DUMP_MIDI
430 line6_write_hexdump(line6, 'r', line6->buffer_message, done);
431 #endif
432 line6_midi_receive(line6, line6->buffer_message, done);
434 switch (line6->usbdev->descriptor.idProduct) {
435 case LINE6_DEVID_BASSPODXT:
436 case LINE6_DEVID_BASSPODXTLIVE:
437 case LINE6_DEVID_BASSPODXTPRO:
438 case LINE6_DEVID_PODXT:
439 case LINE6_DEVID_PODXTPRO:
440 case LINE6_DEVID_POCKETPOD:
441 line6_pod_process_message((struct usb_line6_pod *)
442 line6);
443 break;
445 case LINE6_DEVID_PODHD300:
446 case LINE6_DEVID_PODHD500:
447 break; /* let userspace handle MIDI */
449 case LINE6_DEVID_PODXTLIVE:
450 switch (line6->interface_number) {
451 case PODXTLIVE_INTERFACE_POD:
452 line6_pod_process_message((struct usb_line6_pod
453 *)line6);
454 break;
456 case PODXTLIVE_INTERFACE_VARIAX:
457 line6_variax_process_message((struct
458 usb_line6_variax
459 *)line6);
460 break;
462 default:
463 dev_err(line6->ifcdev,
464 "PODxt Live interface %d not supported\n",
465 line6->interface_number);
467 break;
469 case LINE6_DEVID_VARIAX:
470 line6_variax_process_message((struct usb_line6_variax *)
471 line6);
472 break;
474 default:
475 MISSING_CASE;
479 line6_start_listen(line6);
483 Send channel number (i.e., switch to a different sound).
485 int line6_send_program(struct usb_line6 *line6, int value)
487 int retval;
488 unsigned char *buffer;
489 int partial;
491 buffer = kmalloc(2, GFP_KERNEL);
493 if (!buffer) {
494 dev_err(line6->ifcdev, "out of memory\n");
495 return -ENOMEM;
498 buffer[0] = LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST;
499 buffer[1] = value;
501 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
502 line6_write_hexdump(line6, 'S', buffer, 2);
503 #endif
505 retval = usb_interrupt_msg(line6->usbdev,
506 usb_sndintpipe(line6->usbdev,
507 line6->ep_control_write),
508 buffer, 2, &partial, LINE6_TIMEOUT * HZ);
510 if (retval)
511 dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
512 retval);
514 kfree(buffer);
515 return retval;
519 Transmit Line6 control parameter.
521 int line6_transmit_parameter(struct usb_line6 *line6, int param, int value)
523 int retval;
524 unsigned char *buffer;
525 int partial;
527 buffer = kmalloc(3, GFP_KERNEL);
529 if (!buffer) {
530 dev_err(line6->ifcdev, "out of memory\n");
531 return -ENOMEM;
534 buffer[0] = LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST;
535 buffer[1] = param;
536 buffer[2] = value;
538 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
539 line6_write_hexdump(line6, 'S', buffer, 3);
540 #endif
542 retval = usb_interrupt_msg(line6->usbdev,
543 usb_sndintpipe(line6->usbdev,
544 line6->ep_control_write),
545 buffer, 3, &partial, LINE6_TIMEOUT * HZ);
547 if (retval)
548 dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
549 retval);
551 kfree(buffer);
552 return retval;
556 Read data from device.
558 int line6_read_data(struct usb_line6 *line6, int address, void *data,
559 size_t datalen)
561 struct usb_device *usbdev = line6->usbdev;
562 int ret;
563 unsigned char len;
565 /* query the serial number: */
566 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
567 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
568 (datalen << 8) | 0x21, address,
569 NULL, 0, LINE6_TIMEOUT * HZ);
571 if (ret < 0) {
572 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
573 return ret;
576 /* Wait for data length. We'll get a couple of 0xff until length arrives. */
577 do {
578 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
579 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
580 USB_DIR_IN,
581 0x0012, 0x0000, &len, 1,
582 LINE6_TIMEOUT * HZ);
583 if (ret < 0) {
584 dev_err(line6->ifcdev,
585 "receive length failed (error %d)\n", ret);
586 return ret;
588 } while (len == 0xff);
590 if (len != datalen) {
591 /* should be equal or something went wrong */
592 dev_err(line6->ifcdev,
593 "length mismatch (expected %d, got %d)\n",
594 (int)datalen, (int)len);
595 return -EINVAL;
598 /* receive the result: */
599 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
600 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
601 0x0013, 0x0000, data, datalen,
602 LINE6_TIMEOUT * HZ);
604 if (ret < 0) {
605 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
606 return ret;
609 return 0;
613 Write data to device.
615 int line6_write_data(struct usb_line6 *line6, int address, void *data,
616 size_t datalen)
618 struct usb_device *usbdev = line6->usbdev;
619 int ret;
620 unsigned char status;
622 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
623 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
624 0x0022, address, data, datalen,
625 LINE6_TIMEOUT * HZ);
627 if (ret < 0) {
628 dev_err(line6->ifcdev,
629 "write request failed (error %d)\n", ret);
630 return ret;
633 do {
634 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
635 0x67,
636 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
637 USB_DIR_IN,
638 0x0012, 0x0000,
639 &status, 1, LINE6_TIMEOUT * HZ);
641 if (ret < 0) {
642 dev_err(line6->ifcdev,
643 "receiving status failed (error %d)\n", ret);
644 return ret;
646 } while (status == 0xff);
648 if (status != 0) {
649 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
650 return -EINVAL;
653 return 0;
657 Read Line6 device serial number.
658 (POD, TonePort, GuitarPort)
660 int line6_read_serial_number(struct usb_line6 *line6, int *serial_number)
662 return line6_read_data(line6, 0x80d0, serial_number,
663 sizeof(*serial_number));
667 No operation (i.e., unsupported).
669 ssize_t line6_nop_read(struct device *dev, struct device_attribute *attr,
670 char *buf)
672 return 0;
676 No operation (i.e., unsupported).
678 ssize_t line6_nop_write(struct device *dev, struct device_attribute *attr,
679 const char *buf, size_t count)
681 return count;
685 "write" request on "raw" special file.
687 #ifdef CONFIG_LINE6_USB_RAW
688 ssize_t line6_set_raw(struct device *dev, struct device_attribute *attr,
689 const char *buf, size_t count)
691 struct usb_interface *interface = to_usb_interface(dev);
692 struct usb_line6 *line6 = usb_get_intfdata(interface);
693 line6_send_raw_message(line6, buf, count);
694 return count;
696 #endif
699 Generic destructor.
701 static void line6_destruct(struct usb_interface *interface)
703 struct usb_line6 *line6;
705 if (interface == NULL)
706 return;
707 line6 = usb_get_intfdata(interface);
708 if (line6 == NULL)
709 return;
711 /* free buffer memory first: */
712 kfree(line6->buffer_message);
713 kfree(line6->buffer_listen);
715 /* then free URBs: */
716 usb_free_urb(line6->urb_listen);
718 /* make sure the device isn't destructed twice: */
719 usb_set_intfdata(interface, NULL);
721 /* free interface data: */
722 kfree(line6);
726 Probe USB device.
728 static int line6_probe(struct usb_interface *interface,
729 const struct usb_device_id *id)
731 int devtype;
732 struct usb_device *usbdev;
733 struct usb_line6 *line6;
734 const struct line6_properties *properties;
735 int devnum;
736 int interface_number, alternate = 0;
737 int product;
738 int size = 0;
739 int ep_read = 0, ep_write = 0;
740 int ret;
742 if (interface == NULL)
743 return -ENODEV;
744 usbdev = interface_to_usbdev(interface);
745 if (usbdev == NULL)
746 return -ENODEV;
748 /* we don't handle multiple configurations */
749 if (usbdev->descriptor.bNumConfigurations != 1) {
750 ret = -ENODEV;
751 goto err_put;
754 /* check vendor and product id */
755 for (devtype = ARRAY_SIZE(line6_id_table) - 1; devtype--;) {
756 u16 idVendor = le16_to_cpu(usbdev->descriptor.idVendor);
757 u16 idProduct = le16_to_cpu(usbdev->descriptor.idProduct);
759 if (idVendor == line6_id_table[devtype].idVendor &&
760 idProduct == line6_id_table[devtype].idProduct)
761 break;
764 if (devtype < 0) {
765 ret = -ENODEV;
766 goto err_put;
769 /* find free slot in device table: */
770 for (devnum = 0; devnum < LINE6_MAX_DEVICES; ++devnum)
771 if (line6_devices[devnum] == NULL)
772 break;
774 if (devnum == LINE6_MAX_DEVICES) {
775 ret = -ENODEV;
776 goto err_put;
779 /* initialize device info: */
780 properties = &line6_properties_table[devtype];
781 dev_info(&interface->dev, "Line6 %s found\n", properties->name);
782 product = le16_to_cpu(usbdev->descriptor.idProduct);
784 /* query interface number */
785 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
787 switch (product) {
788 case LINE6_DEVID_BASSPODXTLIVE:
789 case LINE6_DEVID_PODXTLIVE:
790 case LINE6_DEVID_VARIAX:
791 alternate = 1;
792 break;
794 case LINE6_DEVID_POCKETPOD:
795 switch (interface_number) {
796 case 0:
797 return 0; /* this interface has no endpoints */
798 case 1:
799 alternate = 0;
800 break;
801 default:
802 MISSING_CASE;
804 break;
806 case LINE6_DEVID_PODHD500:
807 case LINE6_DEVID_PODX3:
808 case LINE6_DEVID_PODX3LIVE:
809 switch (interface_number) {
810 case 0:
811 alternate = 1;
812 break;
813 case 1:
814 alternate = 0;
815 break;
816 default:
817 MISSING_CASE;
819 break;
821 case LINE6_DEVID_BASSPODXT:
822 case LINE6_DEVID_BASSPODXTPRO:
823 case LINE6_DEVID_PODXT:
824 case LINE6_DEVID_PODXTPRO:
825 case LINE6_DEVID_PODHD300:
826 alternate = 5;
827 break;
829 case LINE6_DEVID_GUITARPORT:
830 case LINE6_DEVID_PODSTUDIO_GX:
831 case LINE6_DEVID_PODSTUDIO_UX1:
832 case LINE6_DEVID_TONEPORT_GX:
833 case LINE6_DEVID_TONEPORT_UX1:
834 alternate = 2; /* 1..4 seem to be ok */
835 break;
837 case LINE6_DEVID_TONEPORT_UX2:
838 case LINE6_DEVID_PODSTUDIO_UX2:
839 switch (interface_number) {
840 case 0:
841 /* defaults to 44.1kHz, 16-bit */
842 alternate = 2;
843 break;
844 case 1:
845 /* don't know yet what this is ...
846 alternate = 1;
847 break;
849 return -ENODEV;
850 default:
851 MISSING_CASE;
853 break;
855 default:
856 MISSING_CASE;
857 ret = -ENODEV;
858 goto err_put;
861 ret = usb_set_interface(usbdev, interface_number, alternate);
862 if (ret < 0) {
863 dev_err(&interface->dev, "set_interface failed\n");
864 goto err_put;
867 /* initialize device data based on product id: */
868 switch (product) {
869 case LINE6_DEVID_BASSPODXT:
870 case LINE6_DEVID_BASSPODXTLIVE:
871 case LINE6_DEVID_BASSPODXTPRO:
872 case LINE6_DEVID_PODXT:
873 case LINE6_DEVID_PODXTPRO:
874 size = sizeof(struct usb_line6_pod);
875 ep_read = 0x84;
876 ep_write = 0x03;
877 break;
879 case LINE6_DEVID_PODHD300:
880 size = sizeof(struct usb_line6_podhd);
881 ep_read = 0x84;
882 ep_write = 0x03;
883 break;
885 case LINE6_DEVID_PODHD500:
886 size = sizeof(struct usb_line6_podhd);
887 ep_read = 0x81;
888 ep_write = 0x01;
889 break;
891 case LINE6_DEVID_POCKETPOD:
892 size = sizeof(struct usb_line6_pod);
893 ep_read = 0x82;
894 ep_write = 0x02;
895 break;
897 case LINE6_DEVID_PODX3:
898 case LINE6_DEVID_PODX3LIVE:
899 /* currently unused! */
900 size = sizeof(struct usb_line6_pod);
901 ep_read = 0x81;
902 ep_write = 0x01;
903 break;
905 case LINE6_DEVID_PODSTUDIO_GX:
906 case LINE6_DEVID_PODSTUDIO_UX1:
907 case LINE6_DEVID_PODSTUDIO_UX2:
908 case LINE6_DEVID_TONEPORT_GX:
909 case LINE6_DEVID_TONEPORT_UX1:
910 case LINE6_DEVID_TONEPORT_UX2:
911 case LINE6_DEVID_GUITARPORT:
912 size = sizeof(struct usb_line6_toneport);
913 /* these don't have a control channel */
914 break;
916 case LINE6_DEVID_PODXTLIVE:
917 switch (interface_number) {
918 case PODXTLIVE_INTERFACE_POD:
919 size = sizeof(struct usb_line6_pod);
920 ep_read = 0x84;
921 ep_write = 0x03;
922 break;
924 case PODXTLIVE_INTERFACE_VARIAX:
925 size = sizeof(struct usb_line6_variax);
926 ep_read = 0x86;
927 ep_write = 0x05;
928 break;
930 default:
931 ret = -ENODEV;
932 goto err_put;
934 break;
936 case LINE6_DEVID_VARIAX:
937 size = sizeof(struct usb_line6_variax);
938 ep_read = 0x82;
939 ep_write = 0x01;
940 break;
942 default:
943 MISSING_CASE;
944 ret = -ENODEV;
945 goto err_put;
948 if (size == 0) {
949 dev_err(&interface->dev,
950 "driver bug: interface data size not set\n");
951 ret = -ENODEV;
952 goto err_put;
955 line6 = kzalloc(size, GFP_KERNEL);
957 if (line6 == NULL) {
958 dev_err(&interface->dev, "Out of memory\n");
959 ret = -ENODEV;
960 goto err_put;
963 /* store basic data: */
964 line6->interface_number = interface_number;
965 line6->properties = properties;
966 line6->usbdev = usbdev;
967 line6->ifcdev = &interface->dev;
968 line6->ep_control_read = ep_read;
969 line6->ep_control_write = ep_write;
970 line6->product = product;
972 /* get data from endpoint descriptor (see usb_maxpacket): */
974 struct usb_host_endpoint *ep;
975 unsigned epnum =
976 usb_pipeendpoint(usb_rcvintpipe(usbdev, ep_read));
977 ep = usbdev->ep_in[epnum];
979 if (ep != NULL) {
980 line6->interval = ep->desc.bInterval;
981 line6->max_packet_size =
982 le16_to_cpu(ep->desc.wMaxPacketSize);
983 } else {
984 line6->interval = LINE6_FALLBACK_INTERVAL;
985 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
986 dev_err(line6->ifcdev,
987 "endpoint not available, using fallback values");
991 usb_set_intfdata(interface, line6);
993 if (properties->capabilities & LINE6_BIT_CONTROL) {
994 /* initialize USB buffers: */
995 line6->buffer_listen =
996 kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
998 if (line6->buffer_listen == NULL) {
999 dev_err(&interface->dev, "Out of memory\n");
1000 ret = -ENOMEM;
1001 goto err_destruct;
1004 line6->buffer_message =
1005 kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
1007 if (line6->buffer_message == NULL) {
1008 dev_err(&interface->dev, "Out of memory\n");
1009 ret = -ENOMEM;
1010 goto err_destruct;
1013 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
1015 if (line6->urb_listen == NULL) {
1016 dev_err(&interface->dev, "Out of memory\n");
1017 line6_destruct(interface);
1018 ret = -ENOMEM;
1019 goto err_destruct;
1022 ret = line6_start_listen(line6);
1023 if (ret < 0) {
1024 dev_err(&interface->dev, "%s: usb_submit_urb failed\n",
1025 __func__);
1026 goto err_destruct;
1030 /* initialize device data based on product id: */
1031 switch (product) {
1032 case LINE6_DEVID_BASSPODXT:
1033 case LINE6_DEVID_BASSPODXTLIVE:
1034 case LINE6_DEVID_BASSPODXTPRO:
1035 case LINE6_DEVID_POCKETPOD:
1036 case LINE6_DEVID_PODX3:
1037 case LINE6_DEVID_PODX3LIVE:
1038 case LINE6_DEVID_PODXT:
1039 case LINE6_DEVID_PODXTPRO:
1040 ret = line6_pod_init(interface, (struct usb_line6_pod *)line6);
1041 break;
1043 case LINE6_DEVID_PODHD300:
1044 case LINE6_DEVID_PODHD500:
1045 ret = line6_podhd_init(interface,
1046 (struct usb_line6_podhd *)line6);
1047 break;
1049 case LINE6_DEVID_PODXTLIVE:
1050 switch (interface_number) {
1051 case PODXTLIVE_INTERFACE_POD:
1052 ret =
1053 line6_pod_init(interface,
1054 (struct usb_line6_pod *)line6);
1055 break;
1057 case PODXTLIVE_INTERFACE_VARIAX:
1058 ret =
1059 line6_variax_init(interface,
1060 (struct usb_line6_variax *)line6);
1061 break;
1063 default:
1064 dev_err(&interface->dev,
1065 "PODxt Live interface %d not supported\n",
1066 interface_number);
1067 ret = -ENODEV;
1070 break;
1072 case LINE6_DEVID_VARIAX:
1073 ret =
1074 line6_variax_init(interface,
1075 (struct usb_line6_variax *)line6);
1076 break;
1078 case LINE6_DEVID_PODSTUDIO_GX:
1079 case LINE6_DEVID_PODSTUDIO_UX1:
1080 case LINE6_DEVID_PODSTUDIO_UX2:
1081 case LINE6_DEVID_TONEPORT_GX:
1082 case LINE6_DEVID_TONEPORT_UX1:
1083 case LINE6_DEVID_TONEPORT_UX2:
1084 case LINE6_DEVID_GUITARPORT:
1085 ret =
1086 line6_toneport_init(interface,
1087 (struct usb_line6_toneport *)line6);
1088 break;
1090 default:
1091 MISSING_CASE;
1092 ret = -ENODEV;
1095 if (ret < 0)
1096 goto err_destruct;
1098 ret = sysfs_create_link(&interface->dev.kobj, &usbdev->dev.kobj,
1099 "usb_device");
1100 if (ret < 0)
1101 goto err_destruct;
1103 /* creation of additional special files should go here */
1105 dev_info(&interface->dev, "Line6 %s now attached\n",
1106 line6->properties->name);
1107 line6_devices[devnum] = line6;
1109 switch (product) {
1110 case LINE6_DEVID_PODX3:
1111 case LINE6_DEVID_PODX3LIVE:
1112 dev_info(&interface->dev,
1113 "NOTE: the Line6 %s is detected, but not yet supported\n",
1114 line6->properties->name);
1117 /* increment reference counters: */
1118 usb_get_intf(interface);
1119 usb_get_dev(usbdev);
1121 return 0;
1123 err_destruct:
1124 line6_destruct(interface);
1125 err_put:
1126 return ret;
1130 Line6 device disconnected.
1132 static void line6_disconnect(struct usb_interface *interface)
1134 struct usb_line6 *line6;
1135 struct usb_device *usbdev;
1136 int interface_number, i;
1138 if (interface == NULL)
1139 return;
1140 usbdev = interface_to_usbdev(interface);
1141 if (usbdev == NULL)
1142 return;
1144 /* removal of additional special files should go here */
1146 sysfs_remove_link(&interface->dev.kobj, "usb_device");
1148 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
1149 line6 = usb_get_intfdata(interface);
1151 if (line6 != NULL) {
1152 if (line6->urb_listen != NULL)
1153 line6_stop_listen(line6);
1155 if (usbdev != line6->usbdev)
1156 dev_err(line6->ifcdev,
1157 "driver bug: inconsistent usb device\n");
1159 switch (line6->usbdev->descriptor.idProduct) {
1160 case LINE6_DEVID_BASSPODXT:
1161 case LINE6_DEVID_BASSPODXTLIVE:
1162 case LINE6_DEVID_BASSPODXTPRO:
1163 case LINE6_DEVID_POCKETPOD:
1164 case LINE6_DEVID_PODX3:
1165 case LINE6_DEVID_PODX3LIVE:
1166 case LINE6_DEVID_PODXT:
1167 case LINE6_DEVID_PODXTPRO:
1168 line6_pod_disconnect(interface);
1169 break;
1171 case LINE6_DEVID_PODHD300:
1172 case LINE6_DEVID_PODHD500:
1173 line6_podhd_disconnect(interface);
1174 break;
1176 case LINE6_DEVID_PODXTLIVE:
1177 switch (interface_number) {
1178 case PODXTLIVE_INTERFACE_POD:
1179 line6_pod_disconnect(interface);
1180 break;
1182 case PODXTLIVE_INTERFACE_VARIAX:
1183 line6_variax_disconnect(interface);
1184 break;
1187 break;
1189 case LINE6_DEVID_VARIAX:
1190 line6_variax_disconnect(interface);
1191 break;
1193 case LINE6_DEVID_PODSTUDIO_GX:
1194 case LINE6_DEVID_PODSTUDIO_UX1:
1195 case LINE6_DEVID_PODSTUDIO_UX2:
1196 case LINE6_DEVID_TONEPORT_GX:
1197 case LINE6_DEVID_TONEPORT_UX1:
1198 case LINE6_DEVID_TONEPORT_UX2:
1199 case LINE6_DEVID_GUITARPORT:
1200 line6_toneport_disconnect(interface);
1201 break;
1203 default:
1204 MISSING_CASE;
1207 dev_info(&interface->dev, "Line6 %s now disconnected\n",
1208 line6->properties->name);
1210 for (i = LINE6_MAX_DEVICES; i--;)
1211 if (line6_devices[i] == line6)
1212 line6_devices[i] = NULL;
1215 line6_destruct(interface);
1217 /* decrement reference counters: */
1218 usb_put_intf(interface);
1219 usb_put_dev(usbdev);
1222 #ifdef CONFIG_PM
1225 Suspend Line6 device.
1227 static int line6_suspend(struct usb_interface *interface, pm_message_t message)
1229 struct usb_line6 *line6 = usb_get_intfdata(interface);
1230 struct snd_line6_pcm *line6pcm = line6->line6pcm;
1232 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
1234 if (line6->properties->capabilities & LINE6_BIT_CONTROL)
1235 line6_stop_listen(line6);
1237 if (line6pcm != NULL) {
1238 snd_pcm_suspend_all(line6pcm->pcm);
1239 line6_pcm_disconnect(line6pcm);
1240 line6pcm->flags = 0;
1243 return 0;
1247 Resume Line6 device.
1249 static int line6_resume(struct usb_interface *interface)
1251 struct usb_line6 *line6 = usb_get_intfdata(interface);
1253 if (line6->properties->capabilities & LINE6_BIT_CONTROL)
1254 line6_start_listen(line6);
1256 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
1257 return 0;
1261 Resume Line6 device after reset.
1263 static int line6_reset_resume(struct usb_interface *interface)
1265 struct usb_line6 *line6 = usb_get_intfdata(interface);
1267 switch (line6->usbdev->descriptor.idProduct) {
1268 case LINE6_DEVID_PODSTUDIO_GX:
1269 case LINE6_DEVID_PODSTUDIO_UX1:
1270 case LINE6_DEVID_PODSTUDIO_UX2:
1271 case LINE6_DEVID_TONEPORT_GX:
1272 case LINE6_DEVID_TONEPORT_UX1:
1273 case LINE6_DEVID_TONEPORT_UX2:
1274 case LINE6_DEVID_GUITARPORT:
1275 line6_toneport_reset_resume((struct usb_line6_toneport *)line6);
1278 return line6_resume(interface);
1281 #endif /* CONFIG_PM */
1283 static struct usb_driver line6_driver = {
1284 .name = DRIVER_NAME,
1285 .probe = line6_probe,
1286 .disconnect = line6_disconnect,
1287 #ifdef CONFIG_PM
1288 .suspend = line6_suspend,
1289 .resume = line6_resume,
1290 .reset_resume = line6_reset_resume,
1291 #endif
1292 .id_table = line6_id_table,
1296 Module initialization.
1298 static int __init line6_init(void)
1300 int i, retval;
1302 printk(KERN_INFO "%s driver version %s\n", DRIVER_NAME, DRIVER_VERSION);
1304 for (i = LINE6_MAX_DEVICES; i--;)
1305 line6_devices[i] = NULL;
1307 retval = usb_register(&line6_driver);
1309 if (retval) {
1310 err("usb_register failed. Error number %d", retval);
1311 return retval;
1314 line6_request_version = kmalloc(sizeof(line6_request_version0),
1315 GFP_KERNEL);
1317 if (line6_request_version == NULL) {
1318 err("Out of memory");
1319 return -ENOMEM;
1322 memcpy((char *)line6_request_version, line6_request_version0,
1323 sizeof(line6_request_version0));
1325 return retval;
1329 Module cleanup.
1331 static void __exit line6_exit(void)
1333 int i;
1334 struct usb_line6 *line6;
1335 struct snd_line6_pcm *line6pcm;
1337 /* stop all PCM channels */
1338 for (i = LINE6_MAX_DEVICES; i--;) {
1339 line6 = line6_devices[i];
1341 if (line6 == NULL)
1342 continue;
1344 line6pcm = line6->line6pcm;
1346 if (line6pcm == NULL)
1347 continue;
1349 line6_pcm_stop(line6pcm, ~0);
1352 usb_deregister(&line6_driver);
1353 kfree(line6_request_version);
1356 module_init(line6_init);
1357 module_exit(line6_exit);
1359 MODULE_AUTHOR(DRIVER_AUTHOR);
1360 MODULE_DESCRIPTION(DRIVER_DESC);
1361 MODULE_LICENSE("GPL");
1362 MODULE_VERSION(DRIVER_VERSION);