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);
179 /* sanity checks of EP before actually submitting */
180 retval = usb_urb_ep_type_check(urb);
184 retval = usb_submit_urb(urb, GFP_ATOMIC);
191 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
199 Setup and start timer.
201 void line6_start_timer(struct timer_list *timer, unsigned long msecs,
202 void (*function)(struct timer_list *t))
204 timer->function = function;
205 mod_timer(timer, jiffies + msecs_to_jiffies(msecs));
207 EXPORT_SYMBOL_GPL(line6_start_timer);
210 Asynchronously send raw message.
212 int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
218 /* create message: */
219 msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
224 urb = usb_alloc_urb(0, GFP_ATOMIC);
231 /* set message data: */
233 msg->buffer = buffer;
238 return line6_send_raw_message_async_part(msg, urb);
240 EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
243 Send asynchronous device version request.
245 int line6_version_request_async(struct usb_line6 *line6)
250 buffer = kmemdup(line6_request_version,
251 sizeof(line6_request_version), GFP_ATOMIC);
255 retval = line6_send_raw_message_async(line6, buffer,
256 sizeof(line6_request_version));
260 EXPORT_SYMBOL_GPL(line6_version_request_async);
263 Send sysex message in pieces of wMaxPacketSize bytes.
265 int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
268 return line6_send_raw_message(line6, buffer,
269 size + SYSEX_EXTRA_SIZE) -
272 EXPORT_SYMBOL_GPL(line6_send_sysex_message);
275 Allocate buffer for sysex message and prepare header.
276 @param code sysex message code
277 @param size number of bytes between code and sysex end
279 char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
282 char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
287 buffer[0] = LINE6_SYSEX_BEGIN;
288 memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
289 buffer[sizeof(line6_midi_id) + 1] = code1;
290 buffer[sizeof(line6_midi_id) + 2] = code2;
291 buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
294 EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
297 Notification of data received from the Line 6 device.
299 static void line6_data_received(struct urb *urb)
301 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
302 struct midi_buffer *mb = &line6->line6midi->midibuf_in;
305 if (urb->status == -ESHUTDOWN)
308 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
310 line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
312 if (done < urb->actual_length) {
313 line6_midibuf_ignore(mb, done);
314 dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
315 done, urb->actual_length);
320 line6_midibuf_read(mb, line6->buffer_message,
321 LINE6_MIDI_MESSAGE_MAXLEN);
326 line6->message_length = done;
327 line6_midi_receive(line6, line6->buffer_message, done);
329 if (line6->process_message)
330 line6->process_message(line6);
333 line6->buffer_message = urb->transfer_buffer;
334 line6->message_length = urb->actual_length;
335 if (line6->process_message)
336 line6->process_message(line6);
337 line6->buffer_message = NULL;
340 line6_start_listen(line6);
343 #define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */
344 #define LINE6_READ_WRITE_MAX_RETRIES 50
347 Read data from device.
349 int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
352 struct usb_device *usbdev = line6->usbdev;
357 if (address > 0xffff || datalen > 0xff)
360 len = kmalloc(sizeof(*len), GFP_KERNEL);
364 /* query the serial number: */
365 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
366 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
367 (datalen << 8) | 0x21, address,
368 NULL, 0, LINE6_TIMEOUT);
371 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
375 /* Wait for data length. We'll get 0xff until length arrives. */
376 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
377 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
379 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
380 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
382 0x0012, 0x0000, len, 1,
385 dev_err(line6->ifcdev,
386 "receive length failed (error %d)\n", ret);
396 dev_err(line6->ifcdev, "read failed after %d retries\n",
399 } else if (*len != datalen) {
400 /* should be equal or something went wrong */
401 dev_err(line6->ifcdev,
402 "length mismatch (expected %d, got %d)\n",
403 (int)datalen, (int)*len);
407 /* receive the result: */
408 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
409 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
410 0x0013, 0x0000, data, datalen,
414 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
420 EXPORT_SYMBOL_GPL(line6_read_data);
423 Write data to device.
425 int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
428 struct usb_device *usbdev = line6->usbdev;
430 unsigned char *status;
433 if (address > 0xffff || datalen > 0xffff)
436 status = kmalloc(sizeof(*status), GFP_KERNEL);
440 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
441 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
442 0x0022, address, data, datalen,
446 dev_err(line6->ifcdev,
447 "write request failed (error %d)\n", ret);
451 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
452 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
454 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
456 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
459 status, 1, LINE6_TIMEOUT);
462 dev_err(line6->ifcdev,
463 "receiving status failed (error %d)\n", ret);
471 if (*status == 0xff) {
472 dev_err(line6->ifcdev, "write failed after %d retries\n",
475 } else if (*status != 0) {
476 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
483 EXPORT_SYMBOL_GPL(line6_write_data);
486 Read Line 6 device serial number.
487 (POD, TonePort, GuitarPort)
489 int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
491 return line6_read_data(line6, 0x80d0, serial_number,
492 sizeof(*serial_number));
494 EXPORT_SYMBOL_GPL(line6_read_serial_number);
499 static void line6_destruct(struct snd_card *card)
501 struct usb_line6 *line6 = card->private_data;
502 struct usb_device *usbdev = line6->usbdev;
504 /* Free buffer memory first. We cannot depend on the existence of private
505 * data from the (podhd) module, it may be gone already during this call
507 kfree(line6->buffer_message);
509 kfree(line6->buffer_listen);
511 /* then free URBs: */
512 usb_free_urb(line6->urb_listen);
513 line6->urb_listen = NULL;
515 /* decrement reference counters: */
519 static void line6_get_usb_properties(struct usb_line6 *line6)
521 struct usb_device *usbdev = line6->usbdev;
522 const struct line6_properties *properties = line6->properties;
524 struct usb_host_endpoint *ep = NULL;
526 if (properties->capabilities & LINE6_CAP_CONTROL) {
527 if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
528 pipe = usb_rcvintpipe(line6->usbdev,
529 line6->properties->ep_ctrl_r);
531 pipe = usb_rcvbulkpipe(line6->usbdev,
532 line6->properties->ep_ctrl_r);
534 ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
537 /* Control data transfer properties */
539 line6->interval = ep->desc.bInterval;
540 line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
542 if (properties->capabilities & LINE6_CAP_CONTROL) {
543 dev_err(line6->ifcdev,
544 "endpoint not available, using fallback values");
546 line6->interval = LINE6_FALLBACK_INTERVAL;
547 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
550 /* Isochronous transfer properties */
551 if (usbdev->speed == USB_SPEED_LOW) {
552 line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
553 line6->iso_buffers = USB_LOW_ISO_BUFFERS;
555 line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
556 line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
560 /* Enable buffering of incoming messages, flush the buffer */
561 static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
563 struct usb_line6 *line6 = hw->private_data;
565 /* NOTE: hwdep layer provides atomicity here */
567 line6->messages.active = 1;
573 static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
575 struct usb_line6 *line6 = hw->private_data;
577 line6->messages.active = 0;
582 /* Read from circular buffer, return to user */
584 line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
587 struct usb_line6 *line6 = hwdep->private_data;
589 unsigned int out_count;
591 if (mutex_lock_interruptible(&line6->messages.read_lock))
594 while (kfifo_len(&line6->messages.fifo) == 0) {
595 mutex_unlock(&line6->messages.read_lock);
597 rv = wait_event_interruptible(
598 line6->messages.wait_queue,
599 kfifo_len(&line6->messages.fifo) != 0);
603 if (mutex_lock_interruptible(&line6->messages.read_lock))
607 if (kfifo_peek_len(&line6->messages.fifo) > count) {
608 /* Buffer too small; allow re-read of the current item... */
611 rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
616 mutex_unlock(&line6->messages.read_lock);
620 /* Write directly (no buffering) to device by user*/
622 line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
625 struct usb_line6 *line6 = hwdep->private_data;
629 if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
630 /* This is an arbitrary limit - still better than nothing... */
634 data_copy = memdup_user(data, count);
635 if (IS_ERR(data_copy))
636 return PTR_ERR(data_copy);
638 rv = line6_send_raw_message(line6, data_copy, count);
644 static const struct snd_hwdep_ops hwdep_ops = {
645 .open = line6_hwdep_open,
646 .release = line6_hwdep_release,
647 .read = line6_hwdep_read,
648 .write = line6_hwdep_write,
651 /* Insert into circular buffer */
652 static void line6_hwdep_push_message(struct usb_line6 *line6)
654 if (!line6->messages.active)
657 if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
658 /* No race condition here, there's only one writer */
659 kfifo_in(&line6->messages.fifo,
660 line6->buffer_message, line6->message_length);
661 } /* else TODO: signal overflow */
663 wake_up_interruptible(&line6->messages.wait_queue);
666 static int line6_hwdep_init(struct usb_line6 *line6)
669 struct snd_hwdep *hwdep;
671 /* TODO: usb_driver_claim_interface(); */
672 line6->process_message = line6_hwdep_push_message;
673 line6->messages.active = 0;
674 init_waitqueue_head(&line6->messages.wait_queue);
675 mutex_init(&line6->messages.read_lock);
676 INIT_KFIFO(line6->messages.fifo);
678 err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
681 strcpy(hwdep->name, "config");
682 hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
683 hwdep->ops = hwdep_ops;
684 hwdep->private_data = line6;
685 hwdep->exclusive = true;
691 static int line6_init_cap_control(struct usb_line6 *line6)
695 /* initialize USB buffers: */
696 line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
697 if (!line6->buffer_listen)
700 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
701 if (!line6->urb_listen)
704 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
705 line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
706 if (!line6->buffer_message)
709 ret = line6_init_midi(line6);
713 ret = line6_hwdep_init(line6);
718 ret = line6_start_listen(line6);
720 dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
727 static void line6_startup_work(struct work_struct *work)
729 struct usb_line6 *line6 =
730 container_of(work, struct usb_line6, startup_work.work);
733 line6->startup(line6);
739 int line6_probe(struct usb_interface *interface,
740 const struct usb_device_id *id,
741 const char *driver_name,
742 const struct line6_properties *properties,
743 int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
746 struct usb_device *usbdev = interface_to_usbdev(interface);
747 struct snd_card *card;
748 struct usb_line6 *line6;
749 int interface_number;
752 if (WARN_ON(data_size < sizeof(*line6)))
755 /* we don't handle multiple configurations */
756 if (usbdev->descriptor.bNumConfigurations != 1)
759 ret = snd_card_new(&interface->dev,
760 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
761 THIS_MODULE, data_size, &card);
765 /* store basic data: */
766 line6 = card->private_data;
768 line6->properties = properties;
769 line6->usbdev = usbdev;
770 line6->ifcdev = &interface->dev;
771 INIT_DELAYED_WORK(&line6->startup_work, line6_startup_work);
773 strcpy(card->id, properties->id);
774 strcpy(card->driver, driver_name);
775 strcpy(card->shortname, properties->name);
776 sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
777 dev_name(line6->ifcdev));
778 card->private_free = line6_destruct;
780 usb_set_intfdata(interface, line6);
782 /* increment reference counters: */
785 /* initialize device info: */
786 dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
788 /* query interface number */
789 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
791 /* TODO reserves the bus bandwidth even without actual transfer */
792 ret = usb_set_interface(usbdev, interface_number,
793 properties->altsetting);
795 dev_err(&interface->dev, "set_interface failed\n");
799 line6_get_usb_properties(line6);
801 if (properties->capabilities & LINE6_CAP_CONTROL) {
802 ret = line6_init_cap_control(line6);
807 /* initialize device data based on device: */
808 ret = private_init(line6, id);
812 /* creation of additional special files should go here */
814 dev_info(&interface->dev, "Line 6 %s now attached\n",
820 /* we can call disconnect callback here because no close-sync is
821 * needed yet at this point
823 line6_disconnect(interface);
826 EXPORT_SYMBOL_GPL(line6_probe);
829 Line 6 device disconnected.
831 void line6_disconnect(struct usb_interface *interface)
833 struct usb_line6 *line6 = usb_get_intfdata(interface);
834 struct usb_device *usbdev = interface_to_usbdev(interface);
839 if (WARN_ON(usbdev != line6->usbdev))
842 cancel_delayed_work_sync(&line6->startup_work);
844 if (line6->urb_listen != NULL)
845 line6_stop_listen(line6);
847 snd_card_disconnect(line6->card);
849 line6_pcm_disconnect(line6->line6pcm);
850 if (line6->disconnect)
851 line6->disconnect(line6);
853 dev_info(&interface->dev, "Line 6 %s now disconnected\n",
854 line6->properties->name);
856 /* make sure the device isn't destructed twice: */
857 usb_set_intfdata(interface, NULL);
859 snd_card_free_when_closed(line6->card);
861 EXPORT_SYMBOL_GPL(line6_disconnect);
866 Suspend Line 6 device.
868 int line6_suspend(struct usb_interface *interface, pm_message_t message)
870 struct usb_line6 *line6 = usb_get_intfdata(interface);
871 struct snd_line6_pcm *line6pcm = line6->line6pcm;
873 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
875 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
876 line6_stop_listen(line6);
878 if (line6pcm != NULL) {
879 snd_pcm_suspend_all(line6pcm->pcm);
885 EXPORT_SYMBOL_GPL(line6_suspend);
888 Resume Line 6 device.
890 int line6_resume(struct usb_interface *interface)
892 struct usb_line6 *line6 = usb_get_intfdata(interface);
894 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
895 line6_start_listen(line6);
897 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
900 EXPORT_SYMBOL_GPL(line6_resume);
902 #endif /* CONFIG_PM */
904 MODULE_AUTHOR(DRIVER_AUTHOR);
905 MODULE_DESCRIPTION(DRIVER_DESC);
906 MODULE_LICENSE("GPL");