The discovered bit in PGCCSR register indicates if the device has been
[linux-2.6/next.git] / drivers / staging / line6 / driver.c
blob851b762319cf20b8b2b90c9624a1b33d51f80593
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 "revision.h"
25 #include "toneport.h"
26 #include "usbdefs.h"
27 #include "variax.h"
29 #define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
30 #define DRIVER_DESC "Line6 USB Driver"
31 #define DRIVER_VERSION "0.9.1beta" DRIVER_REVISION
33 /* table of devices that work with this driver */
34 static const struct usb_device_id line6_id_table[] = {
35 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXT)},
36 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXTLIVE)},
37 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXTPRO)},
38 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_GUITARPORT)},
39 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_POCKETPOD)},
40 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODSTUDIO_GX)},
41 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODSTUDIO_UX1)},
42 {USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODSTUDIO_UX2)},
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 {},
55 MODULE_DEVICE_TABLE(usb, line6_id_table);
57 /* *INDENT-OFF* */
58 static struct line6_properties line6_properties_table[] = {
59 { "BassPODxt", "BassPODxt", LINE6_BIT_BASSPODXT, LINE6_BIT_CONTROL_PCM_HWMON },
60 { "BassPODxtLive", "BassPODxt Live", LINE6_BIT_BASSPODXTLIVE, LINE6_BIT_CONTROL_PCM_HWMON },
61 { "BassPODxtPro", "BassPODxt Pro", LINE6_BIT_BASSPODXTPRO, LINE6_BIT_CONTROL_PCM_HWMON },
62 { "GuitarPort", "GuitarPort", LINE6_BIT_GUITARPORT, LINE6_BIT_PCM },
63 { "PocketPOD", "Pocket POD", LINE6_BIT_POCKETPOD, LINE6_BIT_CONTROL },
64 { "PODStudioGX", "POD Studio GX", LINE6_BIT_PODSTUDIO_GX, LINE6_BIT_PCM },
65 { "PODStudioUX1", "POD Studio UX1", LINE6_BIT_PODSTUDIO_UX1, LINE6_BIT_PCM },
66 { "PODStudioUX2", "POD Studio UX2", LINE6_BIT_PODSTUDIO_UX2, LINE6_BIT_PCM },
67 { "PODX3", "POD X3", LINE6_BIT_PODX3, LINE6_BIT_PCM },
68 { "PODX3Live", "POD X3 Live", LINE6_BIT_PODX3LIVE, LINE6_BIT_PCM },
69 { "PODxt", "PODxt", LINE6_BIT_PODXT, LINE6_BIT_CONTROL_PCM_HWMON },
70 { "PODxtLive", "PODxt Live", LINE6_BIT_PODXTLIVE, LINE6_BIT_CONTROL_PCM_HWMON },
71 { "PODxtPro", "PODxt Pro", LINE6_BIT_PODXTPRO, LINE6_BIT_CONTROL_PCM_HWMON },
72 { "TonePortGX", "TonePort GX", LINE6_BIT_TONEPORT_GX, LINE6_BIT_PCM },
73 { "TonePortUX1", "TonePort UX1", LINE6_BIT_TONEPORT_UX1, LINE6_BIT_PCM },
74 { "TonePortUX2", "TonePort UX2", LINE6_BIT_TONEPORT_UX2, LINE6_BIT_PCM },
75 { "Variax", "Variax Workbench", LINE6_BIT_VARIAX, LINE6_BIT_CONTROL }
77 /* *INDENT-ON* */
80 This is Line6's MIDI manufacturer ID.
82 const unsigned char line6_midi_id[] = {
83 0x00, 0x01, 0x0c
87 Code to request version of POD, Variax interface
88 (and maybe other devices).
90 static const char line6_request_version0[] = {
91 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
95 Copy of version request code with GFP_KERNEL flag for use in URB.
97 static const char *line6_request_version;
99 struct usb_line6 *line6_devices[LINE6_MAX_DEVICES];
102 Class for asynchronous messages.
104 struct message {
105 struct usb_line6 *line6;
106 const char *buffer;
107 int size;
108 int done;
112 Forward declarations.
114 static void line6_data_received(struct urb *urb);
115 static int line6_send_raw_message_async_part(struct message *msg,
116 struct urb *urb);
119 Start to listen on endpoint.
121 static int line6_start_listen(struct usb_line6 *line6)
123 int err;
124 usb_fill_int_urb(line6->urb_listen, line6->usbdev,
125 usb_rcvintpipe(line6->usbdev, line6->ep_control_read),
126 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
127 line6_data_received, line6, line6->interval);
128 line6->urb_listen->actual_length = 0;
129 err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
130 return err;
134 Stop listening on endpoint.
136 static void line6_stop_listen(struct usb_line6 *line6)
138 usb_kill_urb(line6->urb_listen);
141 #ifdef CONFIG_LINE6_USB_DUMP_ANY
143 Write hexdump to syslog.
145 void line6_write_hexdump(struct usb_line6 *line6, char dir,
146 const unsigned char *buffer, int size)
148 static const int BYTES_PER_LINE = 8;
149 char hexdump[100];
150 char asc[BYTES_PER_LINE + 1];
151 int i, j;
153 for (i = 0; i < size; i += BYTES_PER_LINE) {
154 int hexdumpsize = sizeof(hexdump);
155 char *p = hexdump;
156 int n = min(size - i, BYTES_PER_LINE);
157 asc[n] = 0;
159 for (j = 0; j < BYTES_PER_LINE; ++j) {
160 int bytes;
162 if (j < n) {
163 unsigned char val = buffer[i + j];
164 bytes = snprintf(p, hexdumpsize, " %02X", val);
165 asc[j] = ((val >= 0x20)
166 && (val < 0x7f)) ? val : '.';
167 } else
168 bytes = snprintf(p, hexdumpsize, " ");
170 if (bytes > hexdumpsize)
171 break; /* buffer overflow */
173 p += bytes;
174 hexdumpsize -= bytes;
177 dev_info(line6->ifcdev, "%c%04X:%s %s\n", dir, i, hexdump, asc);
180 #endif
182 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
184 Dump URB data to syslog.
186 static void line6_dump_urb(struct urb *urb)
188 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
190 if (urb->status < 0)
191 return;
193 line6_write_hexdump(line6, 'R', (unsigned char *)urb->transfer_buffer,
194 urb->actual_length);
196 #endif
199 Send raw message in pieces of wMaxPacketSize bytes.
201 int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
202 int size)
204 int i, done = 0;
206 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
207 line6_write_hexdump(line6, 'S', buffer, size);
208 #endif
210 for (i = 0; i < size; i += line6->max_packet_size) {
211 int partial;
212 const char *frag_buf = buffer + i;
213 int frag_size = min(line6->max_packet_size, size - i);
214 int retval;
216 retval = usb_interrupt_msg(line6->usbdev,
217 usb_sndintpipe(line6->usbdev,
218 line6->ep_control_write),
219 (char *)frag_buf, frag_size,
220 &partial, LINE6_TIMEOUT * HZ);
222 if (retval) {
223 dev_err(line6->ifcdev,
224 "usb_interrupt_msg failed (%d)\n", retval);
225 break;
228 done += frag_size;
231 return done;
235 Notification of completion of asynchronous request transmission.
237 static void line6_async_request_sent(struct urb *urb)
239 struct message *msg = (struct message *)urb->context;
241 if (msg->done >= msg->size) {
242 usb_free_urb(urb);
243 kfree(msg);
244 } else
245 line6_send_raw_message_async_part(msg, urb);
249 Asynchronously send part of a raw message.
251 static int line6_send_raw_message_async_part(struct message *msg,
252 struct urb *urb)
254 int retval;
255 struct usb_line6 *line6 = msg->line6;
256 int done = msg->done;
257 int bytes = min(msg->size - done, line6->max_packet_size);
259 usb_fill_int_urb(urb, line6->usbdev,
260 usb_sndintpipe(line6->usbdev, line6->ep_control_write),
261 (char *)msg->buffer + done, bytes,
262 line6_async_request_sent, msg, line6->interval);
264 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
265 line6_write_hexdump(line6, 'S', (char *)msg->buffer + done, bytes);
266 #endif
268 msg->done += bytes;
269 retval = usb_submit_urb(urb, GFP_ATOMIC);
271 if (retval < 0) {
272 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
273 __func__, retval);
274 usb_free_urb(urb);
275 kfree(msg);
276 return -EINVAL;
279 return 0;
283 Setup and start timer.
285 void line6_start_timer(struct timer_list *timer, unsigned int msecs,
286 void (*function) (unsigned long), unsigned long data)
288 setup_timer(timer, function, data);
289 timer->expires = jiffies + msecs * HZ / 1000;
290 add_timer(timer);
294 Asynchronously send raw message.
296 int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
297 int size)
299 struct message *msg;
300 struct urb *urb;
302 /* create message: */
303 msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
305 if (msg == NULL) {
306 dev_err(line6->ifcdev, "Out of memory\n");
307 return -ENOMEM;
310 /* create URB: */
311 urb = usb_alloc_urb(0, GFP_ATOMIC);
313 if (urb == NULL) {
314 kfree(msg);
315 dev_err(line6->ifcdev, "Out of memory\n");
316 return -ENOMEM;
319 /* set message data: */
320 msg->line6 = line6;
321 msg->buffer = buffer;
322 msg->size = size;
323 msg->done = 0;
325 /* start sending: */
326 return line6_send_raw_message_async_part(msg, urb);
330 Send asynchronous device version request.
332 int line6_version_request_async(struct usb_line6 *line6)
334 return line6_send_raw_message_async(line6, line6_request_version,
335 sizeof(line6_request_version0));
339 Send sysex message in pieces of wMaxPacketSize bytes.
341 int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
342 int size)
344 return line6_send_raw_message(line6, buffer,
345 size + SYSEX_EXTRA_SIZE) -
346 SYSEX_EXTRA_SIZE;
350 Send sysex message in pieces of wMaxPacketSize bytes.
352 int line6_send_sysex_message_async(struct usb_line6 *line6, const char *buffer,
353 int size)
355 return line6_send_raw_message_async(line6, buffer,
356 size + SYSEX_EXTRA_SIZE) -
357 SYSEX_EXTRA_SIZE;
361 Allocate buffer for sysex message and prepare header.
362 @param code sysex message code
363 @param size number of bytes between code and sysex end
365 char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
366 int size)
368 char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
370 if (!buffer) {
371 dev_err(line6->ifcdev, "out of memory\n");
372 return NULL;
375 buffer[0] = LINE6_SYSEX_BEGIN;
376 memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
377 buffer[sizeof(line6_midi_id) + 1] = code1;
378 buffer[sizeof(line6_midi_id) + 2] = code2;
379 buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
380 return buffer;
384 Notification of data received from the Line6 device.
386 static void line6_data_received(struct urb *urb)
388 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
389 struct MidiBuffer *mb = &line6->line6midi->midibuf_in;
390 int done;
392 if (urb->status == -ESHUTDOWN)
393 return;
395 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
396 line6_dump_urb(urb);
397 #endif
399 done =
400 line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
402 if (done < urb->actual_length) {
403 line6_midibuf_ignore(mb, done);
404 DEBUG_MESSAGES(dev_err
405 (line6->ifcdev,
406 "%d %d buffer overflow - message skipped\n",
407 done, urb->actual_length));
410 for (;;) {
411 done =
412 line6_midibuf_read(mb, line6->buffer_message,
413 LINE6_MESSAGE_MAXLEN);
415 if (done == 0)
416 break;
418 /* MIDI input filter */
419 if (line6_midibuf_skip_message
420 (mb, line6->line6midi->midi_mask_receive))
421 continue;
423 line6->message_length = done;
424 #ifdef CONFIG_LINE6_USB_DUMP_MIDI
425 line6_write_hexdump(line6, 'r', line6->buffer_message, done);
426 #endif
427 line6_midi_receive(line6, line6->buffer_message, done);
429 switch (line6->usbdev->descriptor.idProduct) {
430 case LINE6_DEVID_BASSPODXT:
431 case LINE6_DEVID_BASSPODXTLIVE:
432 case LINE6_DEVID_BASSPODXTPRO:
433 case LINE6_DEVID_PODXT:
434 case LINE6_DEVID_PODXTPRO:
435 case LINE6_DEVID_POCKETPOD:
436 line6_pod_process_message((struct usb_line6_pod *)
437 line6);
438 break;
440 case LINE6_DEVID_PODXTLIVE:
441 switch (line6->interface_number) {
442 case PODXTLIVE_INTERFACE_POD:
443 line6_pod_process_message((struct usb_line6_pod
444 *)line6);
445 break;
447 case PODXTLIVE_INTERFACE_VARIAX:
448 line6_variax_process_message((struct
449 usb_line6_variax
450 *)line6);
451 break;
453 default:
454 dev_err(line6->ifcdev,
455 "PODxt Live interface %d not supported\n",
456 line6->interface_number);
458 break;
460 case LINE6_DEVID_VARIAX:
461 line6_variax_process_message((struct usb_line6_variax *)
462 line6);
463 break;
465 default:
466 MISSING_CASE;
470 line6_start_listen(line6);
474 Send channel number (i.e., switch to a different sound).
476 int line6_send_program(struct usb_line6 *line6, int value)
478 int retval;
479 unsigned char *buffer;
480 int partial;
482 buffer = kmalloc(2, GFP_KERNEL);
484 if (!buffer) {
485 dev_err(line6->ifcdev, "out of memory\n");
486 return -ENOMEM;
489 buffer[0] = LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST;
490 buffer[1] = value;
492 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
493 line6_write_hexdump(line6, 'S', buffer, 2);
494 #endif
496 retval = usb_interrupt_msg(line6->usbdev,
497 usb_sndintpipe(line6->usbdev,
498 line6->ep_control_write),
499 buffer, 2, &partial, LINE6_TIMEOUT * HZ);
501 if (retval)
502 dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
503 retval);
505 kfree(buffer);
506 return retval;
510 Transmit Line6 control parameter.
512 int line6_transmit_parameter(struct usb_line6 *line6, int param, int value)
514 int retval;
515 unsigned char *buffer;
516 int partial;
518 buffer = kmalloc(3, GFP_KERNEL);
520 if (!buffer) {
521 dev_err(line6->ifcdev, "out of memory\n");
522 return -ENOMEM;
525 buffer[0] = LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST;
526 buffer[1] = param;
527 buffer[2] = value;
529 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
530 line6_write_hexdump(line6, 'S', buffer, 3);
531 #endif
533 retval = usb_interrupt_msg(line6->usbdev,
534 usb_sndintpipe(line6->usbdev,
535 line6->ep_control_write),
536 buffer, 3, &partial, LINE6_TIMEOUT * HZ);
538 if (retval)
539 dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
540 retval);
542 kfree(buffer);
543 return retval;
547 Read data from device.
549 int line6_read_data(struct usb_line6 *line6, int address, void *data,
550 size_t datalen)
552 struct usb_device *usbdev = line6->usbdev;
553 int ret;
554 unsigned char len;
556 /* query the serial number: */
557 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
558 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
559 (datalen << 8) | 0x21, address,
560 NULL, 0, LINE6_TIMEOUT * HZ);
562 if (ret < 0) {
563 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
564 return ret;
567 /* Wait for data length. We'll get a couple of 0xff until length arrives. */
568 do {
569 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
570 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
571 USB_DIR_IN,
572 0x0012, 0x0000, &len, 1,
573 LINE6_TIMEOUT * HZ);
574 if (ret < 0) {
575 dev_err(line6->ifcdev,
576 "receive length failed (error %d)\n", ret);
577 return ret;
579 } while (len == 0xff);
581 if (len != datalen) {
582 /* should be equal or something went wrong */
583 dev_err(line6->ifcdev,
584 "length mismatch (expected %d, got %d)\n",
585 (int)datalen, (int)len);
586 return -EINVAL;
589 /* receive the result: */
590 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
591 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
592 0x0013, 0x0000, data, datalen,
593 LINE6_TIMEOUT * HZ);
595 if (ret < 0) {
596 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
597 return ret;
600 return 0;
604 Write data to device.
606 int line6_write_data(struct usb_line6 *line6, int address, void *data,
607 size_t datalen)
609 struct usb_device *usbdev = line6->usbdev;
610 int ret;
611 unsigned char status;
613 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
614 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
615 0x0022, address, data, datalen,
616 LINE6_TIMEOUT * HZ);
618 if (ret < 0) {
619 dev_err(line6->ifcdev,
620 "write request failed (error %d)\n", ret);
621 return ret;
624 do {
625 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
626 0x67,
627 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
628 USB_DIR_IN,
629 0x0012, 0x0000,
630 &status, 1, LINE6_TIMEOUT * HZ);
632 if (ret < 0) {
633 dev_err(line6->ifcdev,
634 "receiving status failed (error %d)\n", ret);
635 return ret;
637 } while (status == 0xff);
639 if (status != 0) {
640 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
641 return -EINVAL;
644 return 0;
648 Read Line6 device serial number.
649 (POD, TonePort, GuitarPort)
651 int line6_read_serial_number(struct usb_line6 *line6, int *serial_number)
653 return line6_read_data(line6, 0x80d0, serial_number,
654 sizeof(*serial_number));
658 No operation (i.e., unsupported).
660 ssize_t line6_nop_read(struct device *dev, struct device_attribute *attr,
661 char *buf)
663 return 0;
667 No operation (i.e., unsupported).
669 ssize_t line6_nop_write(struct device *dev, struct device_attribute *attr,
670 const char *buf, size_t count)
672 return count;
676 "write" request on "raw" special file.
678 #ifdef CONFIG_LINE6_USB_RAW
679 ssize_t line6_set_raw(struct device *dev, struct device_attribute *attr,
680 const char *buf, size_t count)
682 struct usb_interface *interface = to_usb_interface(dev);
683 struct usb_line6 *line6 = usb_get_intfdata(interface);
684 line6_send_raw_message(line6, buf, count);
685 return count;
687 #endif
690 Generic destructor.
692 static void line6_destruct(struct usb_interface *interface)
694 struct usb_line6 *line6;
696 if (interface == NULL)
697 return;
698 line6 = usb_get_intfdata(interface);
699 if (line6 == NULL)
700 return;
702 /* free buffer memory first: */
703 kfree(line6->buffer_message);
704 kfree(line6->buffer_listen);
706 /* then free URBs: */
707 usb_free_urb(line6->urb_listen);
709 /* make sure the device isn't destructed twice: */
710 usb_set_intfdata(interface, NULL);
712 /* free interface data: */
713 kfree(line6);
717 Probe USB device.
719 static int line6_probe(struct usb_interface *interface,
720 const struct usb_device_id *id)
722 int devtype;
723 struct usb_device *usbdev = NULL;
724 struct usb_line6 *line6 = NULL;
725 const struct line6_properties *properties;
726 int devnum;
727 int interface_number, alternate = 0;
728 int product;
729 int size = 0;
730 int ep_read = 0, ep_write = 0;
731 int ret;
733 if (interface == NULL)
734 return -ENODEV;
735 usbdev = interface_to_usbdev(interface);
736 if (usbdev == NULL)
737 return -ENODEV;
739 /* we don't handle multiple configurations */
740 if (usbdev->descriptor.bNumConfigurations != 1) {
741 ret = -ENODEV;
742 goto err_put;
745 /* check vendor and product id */
746 for (devtype = ARRAY_SIZE(line6_id_table) - 1; devtype--;) {
747 u16 idVendor = le16_to_cpu(usbdev->descriptor.idVendor);
748 u16 idProduct = le16_to_cpu(usbdev->descriptor.idProduct);
750 if (idVendor == line6_id_table[devtype].idVendor &&
751 idProduct == line6_id_table[devtype].idProduct)
752 break;
755 if (devtype < 0) {
756 ret = -ENODEV;
757 goto err_put;
760 /* find free slot in device table: */
761 for (devnum = 0; devnum < LINE6_MAX_DEVICES; ++devnum)
762 if (line6_devices[devnum] == NULL)
763 break;
765 if (devnum == LINE6_MAX_DEVICES) {
766 ret = -ENODEV;
767 goto err_put;
770 /* initialize device info: */
771 properties = &line6_properties_table[devtype];
772 dev_info(&interface->dev, "Line6 %s found\n", properties->name);
773 product = le16_to_cpu(usbdev->descriptor.idProduct);
775 /* query interface number */
776 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
778 switch (product) {
779 case LINE6_DEVID_BASSPODXTLIVE:
780 case LINE6_DEVID_PODXTLIVE:
781 case LINE6_DEVID_VARIAX:
782 alternate = 1;
783 break;
785 case LINE6_DEVID_POCKETPOD:
786 switch (interface_number) {
787 case 0:
788 return 0; /* this interface has no endpoints */
789 case 1:
790 alternate = 0;
791 break;
792 default:
793 MISSING_CASE;
795 break;
797 case LINE6_DEVID_PODX3:
798 case LINE6_DEVID_PODX3LIVE:
799 switch (interface_number) {
800 case 0:
801 alternate = 1;
802 break;
803 case 1:
804 alternate = 0;
805 break;
806 default:
807 MISSING_CASE;
809 break;
811 case LINE6_DEVID_BASSPODXT:
812 case LINE6_DEVID_BASSPODXTPRO:
813 case LINE6_DEVID_PODXT:
814 case LINE6_DEVID_PODXTPRO:
815 alternate = 5;
816 break;
818 case LINE6_DEVID_GUITARPORT:
819 case LINE6_DEVID_PODSTUDIO_GX:
820 case LINE6_DEVID_PODSTUDIO_UX1:
821 case LINE6_DEVID_TONEPORT_GX:
822 case LINE6_DEVID_TONEPORT_UX1:
823 alternate = 2; /* 1..4 seem to be ok */
824 break;
826 case LINE6_DEVID_TONEPORT_UX2:
827 case LINE6_DEVID_PODSTUDIO_UX2:
828 switch (interface_number) {
829 case 0:
830 /* defaults to 44.1kHz, 16-bit */
831 alternate = 2;
832 break;
833 case 1:
834 /* don't know yet what this is ...
835 alternate = 1;
836 break;
838 return -ENODEV;
839 default:
840 MISSING_CASE;
842 break;
844 default:
845 MISSING_CASE;
846 ret = -ENODEV;
847 goto err_put;
850 ret = usb_set_interface(usbdev, interface_number, alternate);
851 if (ret < 0) {
852 dev_err(&interface->dev, "set_interface failed\n");
853 goto err_put;
856 /* initialize device data based on product id: */
857 switch (product) {
858 case LINE6_DEVID_BASSPODXT:
859 case LINE6_DEVID_BASSPODXTLIVE:
860 case LINE6_DEVID_BASSPODXTPRO:
861 case LINE6_DEVID_PODXT:
862 case LINE6_DEVID_PODXTPRO:
863 size = sizeof(struct usb_line6_pod);
864 ep_read = 0x84;
865 ep_write = 0x03;
866 break;
868 case LINE6_DEVID_POCKETPOD:
869 size = sizeof(struct usb_line6_pod);
870 ep_read = 0x82;
871 ep_write = 0x02;
872 break;
874 case LINE6_DEVID_PODX3:
875 case LINE6_DEVID_PODX3LIVE:
876 /* currently unused! */
877 size = sizeof(struct usb_line6_pod);
878 ep_read = 0x81;
879 ep_write = 0x01;
880 break;
882 case LINE6_DEVID_PODSTUDIO_GX:
883 case LINE6_DEVID_PODSTUDIO_UX1:
884 case LINE6_DEVID_PODSTUDIO_UX2:
885 case LINE6_DEVID_TONEPORT_GX:
886 case LINE6_DEVID_TONEPORT_UX1:
887 case LINE6_DEVID_TONEPORT_UX2:
888 case LINE6_DEVID_GUITARPORT:
889 size = sizeof(struct usb_line6_toneport);
890 /* these don't have a control channel */
891 break;
893 case LINE6_DEVID_PODXTLIVE:
894 switch (interface_number) {
895 case PODXTLIVE_INTERFACE_POD:
896 size = sizeof(struct usb_line6_pod);
897 ep_read = 0x84;
898 ep_write = 0x03;
899 break;
901 case PODXTLIVE_INTERFACE_VARIAX:
902 size = sizeof(struct usb_line6_variax);
903 ep_read = 0x86;
904 ep_write = 0x05;
905 break;
907 default:
908 ret = -ENODEV;
909 goto err_put;
911 break;
913 case LINE6_DEVID_VARIAX:
914 size = sizeof(struct usb_line6_variax);
915 ep_read = 0x82;
916 ep_write = 0x01;
917 break;
919 default:
920 MISSING_CASE;
921 ret = -ENODEV;
922 goto err_put;
925 if (size == 0) {
926 dev_err(line6->ifcdev,
927 "driver bug: interface data size not set\n");
928 ret = -ENODEV;
929 goto err_put;
932 line6 = kzalloc(size, GFP_KERNEL);
934 if (line6 == NULL) {
935 dev_err(&interface->dev, "Out of memory\n");
936 ret = -ENODEV;
937 goto err_put;
940 /* store basic data: */
941 line6->interface_number = interface_number;
942 line6->properties = properties;
943 line6->usbdev = usbdev;
944 line6->ifcdev = &interface->dev;
945 line6->ep_control_read = ep_read;
946 line6->ep_control_write = ep_write;
947 line6->product = product;
949 /* get data from endpoint descriptor (see usb_maxpacket): */
951 struct usb_host_endpoint *ep;
952 unsigned epnum =
953 usb_pipeendpoint(usb_rcvintpipe(usbdev, ep_read));
954 ep = usbdev->ep_in[epnum];
956 if (ep != NULL) {
957 line6->interval = ep->desc.bInterval;
958 line6->max_packet_size =
959 le16_to_cpu(ep->desc.wMaxPacketSize);
960 } else {
961 line6->interval = LINE6_FALLBACK_INTERVAL;
962 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
963 dev_err(line6->ifcdev,
964 "endpoint not available, using fallback values");
968 usb_set_intfdata(interface, line6);
970 if (properties->capabilities & LINE6_BIT_CONTROL) {
971 /* initialize USB buffers: */
972 line6->buffer_listen =
973 kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
975 if (line6->buffer_listen == NULL) {
976 dev_err(&interface->dev, "Out of memory\n");
977 ret = -ENOMEM;
978 goto err_destruct;
981 line6->buffer_message =
982 kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
984 if (line6->buffer_message == NULL) {
985 dev_err(&interface->dev, "Out of memory\n");
986 ret = -ENOMEM;
987 goto err_destruct;
990 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
992 if (line6->urb_listen == NULL) {
993 dev_err(&interface->dev, "Out of memory\n");
994 line6_destruct(interface);
995 ret = -ENOMEM;
996 goto err_destruct;
999 ret = line6_start_listen(line6);
1000 if (ret < 0) {
1001 dev_err(&interface->dev, "%s: usb_submit_urb failed\n",
1002 __func__);
1003 goto err_destruct;
1007 /* initialize device data based on product id: */
1008 switch (product) {
1009 case LINE6_DEVID_BASSPODXT:
1010 case LINE6_DEVID_BASSPODXTLIVE:
1011 case LINE6_DEVID_BASSPODXTPRO:
1012 case LINE6_DEVID_POCKETPOD:
1013 case LINE6_DEVID_PODX3:
1014 case LINE6_DEVID_PODX3LIVE:
1015 case LINE6_DEVID_PODXT:
1016 case LINE6_DEVID_PODXTPRO:
1017 ret = line6_pod_init(interface, (struct usb_line6_pod *)line6);
1018 break;
1020 case LINE6_DEVID_PODXTLIVE:
1021 switch (interface_number) {
1022 case PODXTLIVE_INTERFACE_POD:
1023 ret =
1024 line6_pod_init(interface,
1025 (struct usb_line6_pod *)line6);
1026 break;
1028 case PODXTLIVE_INTERFACE_VARIAX:
1029 ret =
1030 line6_variax_init(interface,
1031 (struct usb_line6_variax *)line6);
1032 break;
1034 default:
1035 dev_err(&interface->dev,
1036 "PODxt Live interface %d not supported\n",
1037 interface_number);
1038 ret = -ENODEV;
1041 break;
1043 case LINE6_DEVID_VARIAX:
1044 ret =
1045 line6_variax_init(interface,
1046 (struct usb_line6_variax *)line6);
1047 break;
1049 case LINE6_DEVID_PODSTUDIO_GX:
1050 case LINE6_DEVID_PODSTUDIO_UX1:
1051 case LINE6_DEVID_PODSTUDIO_UX2:
1052 case LINE6_DEVID_TONEPORT_GX:
1053 case LINE6_DEVID_TONEPORT_UX1:
1054 case LINE6_DEVID_TONEPORT_UX2:
1055 case LINE6_DEVID_GUITARPORT:
1056 ret =
1057 line6_toneport_init(interface,
1058 (struct usb_line6_toneport *)line6);
1059 break;
1061 default:
1062 MISSING_CASE;
1063 ret = -ENODEV;
1066 if (ret < 0)
1067 goto err_destruct;
1069 ret = sysfs_create_link(&interface->dev.kobj, &usbdev->dev.kobj,
1070 "usb_device");
1071 if (ret < 0)
1072 goto err_destruct;
1074 /* creation of additional special files should go here */
1076 dev_info(&interface->dev, "Line6 %s now attached\n",
1077 line6->properties->name);
1078 line6_devices[devnum] = line6;
1080 switch (product) {
1081 case LINE6_DEVID_PODX3:
1082 case LINE6_DEVID_PODX3LIVE:
1083 dev_info(&interface->dev,
1084 "NOTE: the Line6 %s is detected, but not yet supported\n",
1085 line6->properties->name);
1088 /* increment reference counters: */
1089 usb_get_intf(interface);
1090 usb_get_dev(usbdev);
1092 return 0;
1094 err_destruct:
1095 line6_destruct(interface);
1096 err_put:
1097 return ret;
1101 Line6 device disconnected.
1103 static void line6_disconnect(struct usb_interface *interface)
1105 struct usb_line6 *line6;
1106 struct usb_device *usbdev;
1107 int interface_number, i;
1109 if (interface == NULL)
1110 return;
1111 usbdev = interface_to_usbdev(interface);
1112 if (usbdev == NULL)
1113 return;
1115 /* removal of additional special files should go here */
1117 sysfs_remove_link(&interface->dev.kobj, "usb_device");
1119 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
1120 line6 = usb_get_intfdata(interface);
1122 if (line6 != NULL) {
1123 if (line6->urb_listen != NULL)
1124 line6_stop_listen(line6);
1126 if (usbdev != line6->usbdev)
1127 dev_err(line6->ifcdev,
1128 "driver bug: inconsistent usb device\n");
1130 switch (line6->usbdev->descriptor.idProduct) {
1131 case LINE6_DEVID_BASSPODXT:
1132 case LINE6_DEVID_BASSPODXTLIVE:
1133 case LINE6_DEVID_BASSPODXTPRO:
1134 case LINE6_DEVID_POCKETPOD:
1135 case LINE6_DEVID_PODX3:
1136 case LINE6_DEVID_PODX3LIVE:
1137 case LINE6_DEVID_PODXT:
1138 case LINE6_DEVID_PODXTPRO:
1139 line6_pod_disconnect(interface);
1140 break;
1142 case LINE6_DEVID_PODXTLIVE:
1143 switch (interface_number) {
1144 case PODXTLIVE_INTERFACE_POD:
1145 line6_pod_disconnect(interface);
1146 break;
1148 case PODXTLIVE_INTERFACE_VARIAX:
1149 line6_variax_disconnect(interface);
1150 break;
1153 break;
1155 case LINE6_DEVID_VARIAX:
1156 line6_variax_disconnect(interface);
1157 break;
1159 case LINE6_DEVID_PODSTUDIO_GX:
1160 case LINE6_DEVID_PODSTUDIO_UX1:
1161 case LINE6_DEVID_PODSTUDIO_UX2:
1162 case LINE6_DEVID_TONEPORT_GX:
1163 case LINE6_DEVID_TONEPORT_UX1:
1164 case LINE6_DEVID_TONEPORT_UX2:
1165 case LINE6_DEVID_GUITARPORT:
1166 line6_toneport_disconnect(interface);
1167 break;
1169 default:
1170 MISSING_CASE;
1173 dev_info(&interface->dev, "Line6 %s now disconnected\n",
1174 line6->properties->name);
1176 for (i = LINE6_MAX_DEVICES; i--;)
1177 if (line6_devices[i] == line6)
1178 line6_devices[i] = NULL;
1181 line6_destruct(interface);
1183 /* decrement reference counters: */
1184 usb_put_intf(interface);
1185 usb_put_dev(usbdev);
1188 #ifdef CONFIG_PM
1191 Suspend Line6 device.
1193 static int line6_suspend(struct usb_interface *interface, pm_message_t message)
1195 struct usb_line6 *line6 = usb_get_intfdata(interface);
1196 struct snd_line6_pcm *line6pcm = line6->line6pcm;
1198 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
1200 if (line6->properties->capabilities & LINE6_BIT_CONTROL)
1201 line6_stop_listen(line6);
1203 if (line6pcm != NULL) {
1204 snd_pcm_suspend_all(line6pcm->pcm);
1205 line6_pcm_disconnect(line6pcm);
1206 line6pcm->flags = 0;
1209 return 0;
1213 Resume Line6 device.
1215 static int line6_resume(struct usb_interface *interface)
1217 struct usb_line6 *line6 = usb_get_intfdata(interface);
1219 if (line6->properties->capabilities & LINE6_BIT_CONTROL)
1220 line6_start_listen(line6);
1222 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
1223 return 0;
1227 Resume Line6 device after reset.
1229 static int line6_reset_resume(struct usb_interface *interface)
1231 struct usb_line6 *line6 = usb_get_intfdata(interface);
1233 switch (line6->usbdev->descriptor.idProduct) {
1234 case LINE6_DEVID_PODSTUDIO_GX:
1235 case LINE6_DEVID_PODSTUDIO_UX1:
1236 case LINE6_DEVID_PODSTUDIO_UX2:
1237 case LINE6_DEVID_TONEPORT_GX:
1238 case LINE6_DEVID_TONEPORT_UX1:
1239 case LINE6_DEVID_TONEPORT_UX2:
1240 case LINE6_DEVID_GUITARPORT:
1241 line6_toneport_reset_resume((struct usb_line6_toneport *)line6);
1244 return line6_resume(interface);
1247 #endif /* CONFIG_PM */
1249 static struct usb_driver line6_driver = {
1250 .name = DRIVER_NAME,
1251 .probe = line6_probe,
1252 .disconnect = line6_disconnect,
1253 #ifdef CONFIG_PM
1254 .suspend = line6_suspend,
1255 .resume = line6_resume,
1256 .reset_resume = line6_reset_resume,
1257 #endif
1258 .id_table = line6_id_table,
1262 Module initialization.
1264 static int __init line6_init(void)
1266 int i, retval;
1268 printk(KERN_INFO "%s driver version %s\n", DRIVER_NAME, DRIVER_VERSION);
1270 for (i = LINE6_MAX_DEVICES; i--;)
1271 line6_devices[i] = NULL;
1273 retval = usb_register(&line6_driver);
1275 if (retval) {
1276 err("usb_register failed. Error number %d", retval);
1277 return retval;
1280 line6_request_version = kmalloc(sizeof(line6_request_version0),
1281 GFP_KERNEL);
1283 if (line6_request_version == NULL) {
1284 err("Out of memory");
1285 return -ENOMEM;
1288 memcpy((char *)line6_request_version, line6_request_version0,
1289 sizeof(line6_request_version0));
1291 return retval;
1295 Module cleanup.
1297 static void __exit line6_exit(void)
1299 int i;
1300 struct usb_line6 *line6;
1301 struct snd_line6_pcm *line6pcm;
1303 /* stop all PCM channels */
1304 for (i = LINE6_MAX_DEVICES; i--;) {
1305 line6 = line6_devices[i];
1307 if (line6 == NULL)
1308 continue;
1310 line6pcm = line6->line6pcm;
1312 if (line6pcm == NULL)
1313 continue;
1315 line6_pcm_stop(line6pcm, ~0);
1318 usb_deregister(&line6_driver);
1319 kfree(line6_request_version);
1322 module_init(line6_init);
1323 module_exit(line6_exit);
1325 MODULE_AUTHOR(DRIVER_AUTHOR);
1326 MODULE_DESCRIPTION(DRIVER_DESC);
1327 MODULE_LICENSE("GPL");
1328 MODULE_VERSION(DRIVER_VERSION);