2 * Line 6 Linux USB driver
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/export.h>
15 #include <linux/slab.h>
16 #include <linux/usb.h>
18 #include <sound/core.h>
19 #include <sound/initval.h>
20 #include <sound/hwdep.h>
27 #define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
28 #define DRIVER_DESC "Line 6 USB Driver"
31 This is Line 6's MIDI manufacturer ID.
33 const unsigned char line6_midi_id[3] = {
36 EXPORT_SYMBOL_GPL(line6_midi_id);
39 Code to request version of POD, Variax interface
40 (and maybe other devices).
42 static const char line6_request_version[] = {
43 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
47 Class for asynchronous messages.
50 struct usb_line6 *line6;
59 static void line6_data_received(struct urb *urb);
60 static int line6_send_raw_message_async_part(struct message *msg,
64 Start to listen on endpoint.
66 static int line6_start_listen(struct usb_line6 *line6)
70 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
71 usb_fill_int_urb(line6->urb_listen, line6->usbdev,
72 usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
73 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
74 line6_data_received, line6, line6->interval);
76 usb_fill_bulk_urb(line6->urb_listen, line6->usbdev,
77 usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r),
78 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
79 line6_data_received, line6);
82 /* sanity checks of EP before actually submitting */
83 if (usb_urb_ep_type_check(line6->urb_listen)) {
84 dev_err(line6->ifcdev, "invalid control EP\n");
88 line6->urb_listen->actual_length = 0;
89 err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
94 Stop listening on endpoint.
96 static void line6_stop_listen(struct usb_line6 *line6)
98 usb_kill_urb(line6->urb_listen);
102 Send raw message in pieces of wMaxPacketSize bytes.
104 static int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
108 const struct line6_properties *properties = line6->properties;
110 for (i = 0; i < size; i += line6->max_packet_size) {
112 const char *frag_buf = buffer + i;
113 int frag_size = min(line6->max_packet_size, size - i);
116 if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
117 retval = usb_interrupt_msg(line6->usbdev,
118 usb_sndintpipe(line6->usbdev, properties->ep_ctrl_w),
119 (char *)frag_buf, frag_size,
120 &partial, LINE6_TIMEOUT);
122 retval = usb_bulk_msg(line6->usbdev,
123 usb_sndbulkpipe(line6->usbdev, properties->ep_ctrl_w),
124 (char *)frag_buf, frag_size,
125 &partial, LINE6_TIMEOUT);
129 dev_err(line6->ifcdev,
130 "usb_bulk_msg failed (%d)\n", retval);
141 Notification of completion of asynchronous request transmission.
143 static void line6_async_request_sent(struct urb *urb)
145 struct message *msg = (struct message *)urb->context;
147 if (msg->done >= msg->size) {
151 line6_send_raw_message_async_part(msg, urb);
155 Asynchronously send part of a raw message.
157 static int line6_send_raw_message_async_part(struct message *msg,
161 struct usb_line6 *line6 = msg->line6;
162 int done = msg->done;
163 int bytes = min(msg->size - done, line6->max_packet_size);
165 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
166 usb_fill_int_urb(urb, line6->usbdev,
167 usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
168 (char *)msg->buffer + done, bytes,
169 line6_async_request_sent, msg, line6->interval);
171 usb_fill_bulk_urb(urb, line6->usbdev,
172 usb_sndbulkpipe(line6->usbdev, line6->properties->ep_ctrl_w),
173 (char *)msg->buffer + done, bytes,
174 line6_async_request_sent, msg);
178 retval = usb_submit_urb(urb, GFP_ATOMIC);
181 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
192 Setup and start timer.
194 void line6_start_timer(struct timer_list *timer, unsigned long msecs,
195 void (*function)(unsigned long), unsigned long data)
197 setup_timer(timer, function, data);
198 mod_timer(timer, jiffies + msecs_to_jiffies(msecs));
200 EXPORT_SYMBOL_GPL(line6_start_timer);
203 Asynchronously send raw message.
205 int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
211 /* create message: */
212 msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
217 urb = usb_alloc_urb(0, GFP_ATOMIC);
224 /* set message data: */
226 msg->buffer = buffer;
231 return line6_send_raw_message_async_part(msg, urb);
233 EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
236 Send asynchronous device version request.
238 int line6_version_request_async(struct usb_line6 *line6)
243 buffer = kmemdup(line6_request_version,
244 sizeof(line6_request_version), GFP_ATOMIC);
248 retval = line6_send_raw_message_async(line6, buffer,
249 sizeof(line6_request_version));
253 EXPORT_SYMBOL_GPL(line6_version_request_async);
256 Send sysex message in pieces of wMaxPacketSize bytes.
258 int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
261 return line6_send_raw_message(line6, buffer,
262 size + SYSEX_EXTRA_SIZE) -
265 EXPORT_SYMBOL_GPL(line6_send_sysex_message);
268 Allocate buffer for sysex message and prepare header.
269 @param code sysex message code
270 @param size number of bytes between code and sysex end
272 char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
275 char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
280 buffer[0] = LINE6_SYSEX_BEGIN;
281 memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
282 buffer[sizeof(line6_midi_id) + 1] = code1;
283 buffer[sizeof(line6_midi_id) + 2] = code2;
284 buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
287 EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
290 Notification of data received from the Line 6 device.
292 static void line6_data_received(struct urb *urb)
294 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
295 struct midi_buffer *mb = &line6->line6midi->midibuf_in;
298 if (urb->status == -ESHUTDOWN)
301 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
303 line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
305 if (done < urb->actual_length) {
306 line6_midibuf_ignore(mb, done);
307 dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
308 done, urb->actual_length);
313 line6_midibuf_read(mb, line6->buffer_message,
314 LINE6_MIDI_MESSAGE_MAXLEN);
319 line6->message_length = done;
320 line6_midi_receive(line6, line6->buffer_message, done);
322 if (line6->process_message)
323 line6->process_message(line6);
326 line6->buffer_message = urb->transfer_buffer;
327 line6->message_length = urb->actual_length;
328 if (line6->process_message)
329 line6->process_message(line6);
330 line6->buffer_message = NULL;
333 line6_start_listen(line6);
336 #define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */
337 #define LINE6_READ_WRITE_MAX_RETRIES 50
340 Read data from device.
342 int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
345 struct usb_device *usbdev = line6->usbdev;
350 if (address > 0xffff || datalen > 0xff)
353 len = kmalloc(sizeof(*len), GFP_KERNEL);
357 /* query the serial number: */
358 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
359 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
360 (datalen << 8) | 0x21, address,
361 NULL, 0, LINE6_TIMEOUT);
364 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
368 /* Wait for data length. We'll get 0xff until length arrives. */
369 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
370 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
372 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
373 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
375 0x0012, 0x0000, len, 1,
378 dev_err(line6->ifcdev,
379 "receive length failed (error %d)\n", ret);
389 dev_err(line6->ifcdev, "read failed after %d retries\n",
392 } else if (*len != datalen) {
393 /* should be equal or something went wrong */
394 dev_err(line6->ifcdev,
395 "length mismatch (expected %d, got %d)\n",
396 (int)datalen, (int)*len);
400 /* receive the result: */
401 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
402 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
403 0x0013, 0x0000, data, datalen,
407 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
413 EXPORT_SYMBOL_GPL(line6_read_data);
416 Write data to device.
418 int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
421 struct usb_device *usbdev = line6->usbdev;
423 unsigned char *status;
426 if (address > 0xffff || datalen > 0xffff)
429 status = kmalloc(sizeof(*status), GFP_KERNEL);
433 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
434 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
435 0x0022, address, data, datalen,
439 dev_err(line6->ifcdev,
440 "write request failed (error %d)\n", ret);
444 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
445 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
447 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
449 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
452 status, 1, LINE6_TIMEOUT);
455 dev_err(line6->ifcdev,
456 "receiving status failed (error %d)\n", ret);
464 if (*status == 0xff) {
465 dev_err(line6->ifcdev, "write failed after %d retries\n",
468 } else if (*status != 0) {
469 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
476 EXPORT_SYMBOL_GPL(line6_write_data);
479 Read Line 6 device serial number.
480 (POD, TonePort, GuitarPort)
482 int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
484 return line6_read_data(line6, 0x80d0, serial_number,
485 sizeof(*serial_number));
487 EXPORT_SYMBOL_GPL(line6_read_serial_number);
492 static void line6_destruct(struct snd_card *card)
494 struct usb_line6 *line6 = card->private_data;
495 struct usb_device *usbdev = line6->usbdev;
497 /* Free buffer memory first. We cannot depend on the existence of private
498 * data from the (podhd) module, it may be gone already during this call
500 kfree(line6->buffer_message);
502 kfree(line6->buffer_listen);
504 /* then free URBs: */
505 usb_free_urb(line6->urb_listen);
506 line6->urb_listen = NULL;
508 /* decrement reference counters: */
512 static void line6_get_usb_properties(struct usb_line6 *line6)
514 struct usb_device *usbdev = line6->usbdev;
515 const struct line6_properties *properties = line6->properties;
517 struct usb_host_endpoint *ep = NULL;
519 if (properties->capabilities & LINE6_CAP_CONTROL) {
520 if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
521 pipe = usb_rcvintpipe(line6->usbdev,
522 line6->properties->ep_ctrl_r);
524 pipe = usb_rcvbulkpipe(line6->usbdev,
525 line6->properties->ep_ctrl_r);
527 ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
530 /* Control data transfer properties */
532 line6->interval = ep->desc.bInterval;
533 line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
535 if (properties->capabilities & LINE6_CAP_CONTROL) {
536 dev_err(line6->ifcdev,
537 "endpoint not available, using fallback values");
539 line6->interval = LINE6_FALLBACK_INTERVAL;
540 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
543 /* Isochronous transfer properties */
544 if (usbdev->speed == USB_SPEED_LOW) {
545 line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
546 line6->iso_buffers = USB_LOW_ISO_BUFFERS;
548 line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
549 line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
553 /* Enable buffering of incoming messages, flush the buffer */
554 static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
556 struct usb_line6 *line6 = hw->private_data;
558 /* NOTE: hwdep layer provides atomicity here */
560 line6->messages.active = 1;
566 static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
568 struct usb_line6 *line6 = hw->private_data;
570 line6->messages.active = 0;
575 /* Read from circular buffer, return to user */
577 line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
580 struct usb_line6 *line6 = hwdep->private_data;
582 unsigned int out_count;
584 if (mutex_lock_interruptible(&line6->messages.read_lock))
587 while (kfifo_len(&line6->messages.fifo) == 0) {
588 mutex_unlock(&line6->messages.read_lock);
590 rv = wait_event_interruptible(
591 line6->messages.wait_queue,
592 kfifo_len(&line6->messages.fifo) != 0);
596 if (mutex_lock_interruptible(&line6->messages.read_lock))
600 if (kfifo_peek_len(&line6->messages.fifo) > count) {
601 /* Buffer too small; allow re-read of the current item... */
604 rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
609 mutex_unlock(&line6->messages.read_lock);
613 /* Write directly (no buffering) to device by user*/
615 line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
618 struct usb_line6 *line6 = hwdep->private_data;
622 if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
623 /* This is an arbitrary limit - still better than nothing... */
627 data_copy = memdup_user(data, count);
628 if (IS_ERR(data_copy))
629 return PTR_ERR(data_copy);
631 rv = line6_send_raw_message(line6, data_copy, count);
637 static const struct snd_hwdep_ops hwdep_ops = {
638 .open = line6_hwdep_open,
639 .release = line6_hwdep_release,
640 .read = line6_hwdep_read,
641 .write = line6_hwdep_write,
644 /* Insert into circular buffer */
645 static void line6_hwdep_push_message(struct usb_line6 *line6)
647 if (!line6->messages.active)
650 if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
651 /* No race condition here, there's only one writer */
652 kfifo_in(&line6->messages.fifo,
653 line6->buffer_message, line6->message_length);
654 } /* else TODO: signal overflow */
656 wake_up_interruptible(&line6->messages.wait_queue);
659 static int line6_hwdep_init(struct usb_line6 *line6)
662 struct snd_hwdep *hwdep;
664 /* TODO: usb_driver_claim_interface(); */
665 line6->process_message = line6_hwdep_push_message;
666 line6->messages.active = 0;
667 init_waitqueue_head(&line6->messages.wait_queue);
668 mutex_init(&line6->messages.read_lock);
669 INIT_KFIFO(line6->messages.fifo);
671 err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
674 strcpy(hwdep->name, "config");
675 hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
676 hwdep->ops = hwdep_ops;
677 hwdep->private_data = line6;
678 hwdep->exclusive = true;
684 static int line6_init_cap_control(struct usb_line6 *line6)
688 /* initialize USB buffers: */
689 line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
690 if (!line6->buffer_listen)
693 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
694 if (!line6->urb_listen)
697 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
698 line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
699 if (!line6->buffer_message)
702 ret = line6_init_midi(line6);
706 ret = line6_hwdep_init(line6);
711 ret = line6_start_listen(line6);
713 dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
723 int line6_probe(struct usb_interface *interface,
724 const struct usb_device_id *id,
725 const char *driver_name,
726 const struct line6_properties *properties,
727 int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
730 struct usb_device *usbdev = interface_to_usbdev(interface);
731 struct snd_card *card;
732 struct usb_line6 *line6;
733 int interface_number;
736 if (WARN_ON(data_size < sizeof(*line6)))
739 /* we don't handle multiple configurations */
740 if (usbdev->descriptor.bNumConfigurations != 1)
743 ret = snd_card_new(&interface->dev,
744 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
745 THIS_MODULE, data_size, &card);
749 /* store basic data: */
750 line6 = card->private_data;
752 line6->properties = properties;
753 line6->usbdev = usbdev;
754 line6->ifcdev = &interface->dev;
756 strcpy(card->id, properties->id);
757 strcpy(card->driver, driver_name);
758 strcpy(card->shortname, properties->name);
759 sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
760 dev_name(line6->ifcdev));
761 card->private_free = line6_destruct;
763 usb_set_intfdata(interface, line6);
765 /* increment reference counters: */
768 /* initialize device info: */
769 dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
771 /* query interface number */
772 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
774 /* TODO reserves the bus bandwidth even without actual transfer */
775 ret = usb_set_interface(usbdev, interface_number,
776 properties->altsetting);
778 dev_err(&interface->dev, "set_interface failed\n");
782 line6_get_usb_properties(line6);
784 if (properties->capabilities & LINE6_CAP_CONTROL) {
785 ret = line6_init_cap_control(line6);
790 /* initialize device data based on device: */
791 ret = private_init(line6, id);
795 /* creation of additional special files should go here */
797 dev_info(&interface->dev, "Line 6 %s now attached\n",
803 /* we can call disconnect callback here because no close-sync is
804 * needed yet at this point
806 line6_disconnect(interface);
809 EXPORT_SYMBOL_GPL(line6_probe);
812 Line 6 device disconnected.
814 void line6_disconnect(struct usb_interface *interface)
816 struct usb_line6 *line6 = usb_get_intfdata(interface);
817 struct usb_device *usbdev = interface_to_usbdev(interface);
822 if (WARN_ON(usbdev != line6->usbdev))
825 if (line6->urb_listen != NULL)
826 line6_stop_listen(line6);
828 snd_card_disconnect(line6->card);
830 line6_pcm_disconnect(line6->line6pcm);
831 if (line6->disconnect)
832 line6->disconnect(line6);
834 dev_info(&interface->dev, "Line 6 %s now disconnected\n",
835 line6->properties->name);
837 /* make sure the device isn't destructed twice: */
838 usb_set_intfdata(interface, NULL);
840 snd_card_free_when_closed(line6->card);
842 EXPORT_SYMBOL_GPL(line6_disconnect);
847 Suspend Line 6 device.
849 int line6_suspend(struct usb_interface *interface, pm_message_t message)
851 struct usb_line6 *line6 = usb_get_intfdata(interface);
852 struct snd_line6_pcm *line6pcm = line6->line6pcm;
854 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
856 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
857 line6_stop_listen(line6);
859 if (line6pcm != NULL) {
860 snd_pcm_suspend_all(line6pcm->pcm);
866 EXPORT_SYMBOL_GPL(line6_suspend);
869 Resume Line 6 device.
871 int line6_resume(struct usb_interface *interface)
873 struct usb_line6 *line6 = usb_get_intfdata(interface);
875 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
876 line6_start_listen(line6);
878 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
881 EXPORT_SYMBOL_GPL(line6_resume);
883 #endif /* CONFIG_PM */
885 MODULE_AUTHOR(DRIVER_AUTHOR);
886 MODULE_DESCRIPTION(DRIVER_DESC);
887 MODULE_LICENSE("GPL");