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);
81 line6->urb_listen->actual_length = 0;
82 err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
87 Stop listening on endpoint.
89 static void line6_stop_listen(struct usb_line6 *line6)
91 usb_kill_urb(line6->urb_listen);
95 Send raw message in pieces of wMaxPacketSize bytes.
97 static int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
101 const struct line6_properties *properties = line6->properties;
103 for (i = 0; i < size; i += line6->max_packet_size) {
105 const char *frag_buf = buffer + i;
106 int frag_size = min(line6->max_packet_size, size - i);
109 if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
110 retval = usb_interrupt_msg(line6->usbdev,
111 usb_sndintpipe(line6->usbdev, properties->ep_ctrl_w),
112 (char *)frag_buf, frag_size,
113 &partial, LINE6_TIMEOUT);
115 retval = usb_bulk_msg(line6->usbdev,
116 usb_sndbulkpipe(line6->usbdev, properties->ep_ctrl_w),
117 (char *)frag_buf, frag_size,
118 &partial, LINE6_TIMEOUT);
122 dev_err(line6->ifcdev,
123 "usb_bulk_msg failed (%d)\n", retval);
134 Notification of completion of asynchronous request transmission.
136 static void line6_async_request_sent(struct urb *urb)
138 struct message *msg = (struct message *)urb->context;
140 if (msg->done >= msg->size) {
144 line6_send_raw_message_async_part(msg, urb);
148 Asynchronously send part of a raw message.
150 static int line6_send_raw_message_async_part(struct message *msg,
154 struct usb_line6 *line6 = msg->line6;
155 int done = msg->done;
156 int bytes = min(msg->size - done, line6->max_packet_size);
158 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
159 usb_fill_int_urb(urb, line6->usbdev,
160 usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
161 (char *)msg->buffer + done, bytes,
162 line6_async_request_sent, msg, line6->interval);
164 usb_fill_bulk_urb(urb, line6->usbdev,
165 usb_sndbulkpipe(line6->usbdev, line6->properties->ep_ctrl_w),
166 (char *)msg->buffer + done, bytes,
167 line6_async_request_sent, msg);
171 retval = usb_submit_urb(urb, GFP_ATOMIC);
174 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
185 Setup and start timer.
187 void line6_start_timer(struct timer_list *timer, unsigned long msecs,
188 void (*function)(unsigned long), unsigned long data)
190 setup_timer(timer, function, data);
191 mod_timer(timer, jiffies + msecs_to_jiffies(msecs));
193 EXPORT_SYMBOL_GPL(line6_start_timer);
196 Asynchronously send raw message.
198 int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
204 /* create message: */
205 msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
210 urb = usb_alloc_urb(0, GFP_ATOMIC);
217 /* set message data: */
219 msg->buffer = buffer;
224 return line6_send_raw_message_async_part(msg, urb);
226 EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
229 Send asynchronous device version request.
231 int line6_version_request_async(struct usb_line6 *line6)
236 buffer = kmemdup(line6_request_version,
237 sizeof(line6_request_version), GFP_ATOMIC);
241 retval = line6_send_raw_message_async(line6, buffer,
242 sizeof(line6_request_version));
246 EXPORT_SYMBOL_GPL(line6_version_request_async);
249 Send sysex message in pieces of wMaxPacketSize bytes.
251 int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
254 return line6_send_raw_message(line6, buffer,
255 size + SYSEX_EXTRA_SIZE) -
258 EXPORT_SYMBOL_GPL(line6_send_sysex_message);
261 Allocate buffer for sysex message and prepare header.
262 @param code sysex message code
263 @param size number of bytes between code and sysex end
265 char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
268 char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
273 buffer[0] = LINE6_SYSEX_BEGIN;
274 memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
275 buffer[sizeof(line6_midi_id) + 1] = code1;
276 buffer[sizeof(line6_midi_id) + 2] = code2;
277 buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
280 EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
283 Notification of data received from the Line 6 device.
285 static void line6_data_received(struct urb *urb)
287 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
288 struct midi_buffer *mb = &line6->line6midi->midibuf_in;
291 if (urb->status == -ESHUTDOWN)
294 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
296 line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
298 if (done < urb->actual_length) {
299 line6_midibuf_ignore(mb, done);
300 dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
301 done, urb->actual_length);
306 line6_midibuf_read(mb, line6->buffer_message,
307 LINE6_MIDI_MESSAGE_MAXLEN);
312 line6->message_length = done;
313 line6_midi_receive(line6, line6->buffer_message, done);
315 if (line6->process_message)
316 line6->process_message(line6);
319 line6->buffer_message = urb->transfer_buffer;
320 line6->message_length = urb->actual_length;
321 if (line6->process_message)
322 line6->process_message(line6);
323 line6->buffer_message = NULL;
326 line6_start_listen(line6);
329 #define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */
330 #define LINE6_READ_WRITE_MAX_RETRIES 50
333 Read data from device.
335 int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
338 struct usb_device *usbdev = line6->usbdev;
343 if (address > 0xffff || datalen > 0xff)
346 len = kmalloc(sizeof(*len), GFP_KERNEL);
350 /* query the serial number: */
351 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
352 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
353 (datalen << 8) | 0x21, address,
354 NULL, 0, LINE6_TIMEOUT);
357 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
361 /* Wait for data length. We'll get 0xff until length arrives. */
362 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
363 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
365 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
366 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
368 0x0012, 0x0000, len, 1,
371 dev_err(line6->ifcdev,
372 "receive length failed (error %d)\n", ret);
382 dev_err(line6->ifcdev, "read failed after %d retries\n",
385 } else if (*len != datalen) {
386 /* should be equal or something went wrong */
387 dev_err(line6->ifcdev,
388 "length mismatch (expected %d, got %d)\n",
389 (int)datalen, (int)*len);
393 /* receive the result: */
394 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
395 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
396 0x0013, 0x0000, data, datalen,
400 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
406 EXPORT_SYMBOL_GPL(line6_read_data);
409 Write data to device.
411 int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
414 struct usb_device *usbdev = line6->usbdev;
416 unsigned char *status;
419 if (address > 0xffff || datalen > 0xffff)
422 status = kmalloc(sizeof(*status), GFP_KERNEL);
426 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
427 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
428 0x0022, address, data, datalen,
432 dev_err(line6->ifcdev,
433 "write request failed (error %d)\n", ret);
437 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
438 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
440 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
442 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
445 status, 1, LINE6_TIMEOUT);
448 dev_err(line6->ifcdev,
449 "receiving status failed (error %d)\n", ret);
457 if (*status == 0xff) {
458 dev_err(line6->ifcdev, "write failed after %d retries\n",
461 } else if (*status != 0) {
462 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
469 EXPORT_SYMBOL_GPL(line6_write_data);
472 Read Line 6 device serial number.
473 (POD, TonePort, GuitarPort)
475 int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
477 return line6_read_data(line6, 0x80d0, serial_number,
478 sizeof(*serial_number));
480 EXPORT_SYMBOL_GPL(line6_read_serial_number);
485 static void line6_destruct(struct snd_card *card)
487 struct usb_line6 *line6 = card->private_data;
488 struct usb_device *usbdev = line6->usbdev;
490 /* Free buffer memory first. We cannot depend on the existence of private
491 * data from the (podhd) module, it may be gone already during this call
493 kfree(line6->buffer_message);
495 kfree(line6->buffer_listen);
497 /* then free URBs: */
498 usb_free_urb(line6->urb_listen);
499 line6->urb_listen = NULL;
501 /* decrement reference counters: */
505 /* get data from endpoint descriptor (see usb_maxpacket): */
506 static void line6_get_interval(struct usb_line6 *line6)
508 struct usb_device *usbdev = line6->usbdev;
509 const struct line6_properties *properties = line6->properties;
511 struct usb_host_endpoint *ep;
513 if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
515 usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r);
518 usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r);
520 ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
523 line6->interval = ep->desc.bInterval;
524 if (usbdev->speed == USB_SPEED_LOW) {
525 line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
526 line6->iso_buffers = USB_LOW_ISO_BUFFERS;
528 line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
529 line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
532 line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
534 dev_err(line6->ifcdev,
535 "endpoint not available, using fallback values");
536 line6->interval = LINE6_FALLBACK_INTERVAL;
537 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
542 /* Enable buffering of incoming messages, flush the buffer */
543 static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
545 struct usb_line6 *line6 = hw->private_data;
547 /* NOTE: hwdep layer provides atomicity here */
549 line6->messages.active = 1;
555 static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
557 struct usb_line6 *line6 = hw->private_data;
559 line6->messages.active = 0;
564 /* Read from circular buffer, return to user */
566 line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
569 struct usb_line6 *line6 = hwdep->private_data;
571 unsigned int out_count;
573 if (mutex_lock_interruptible(&line6->messages.read_lock))
576 while (kfifo_len(&line6->messages.fifo) == 0) {
577 mutex_unlock(&line6->messages.read_lock);
579 rv = wait_event_interruptible(
580 line6->messages.wait_queue,
581 kfifo_len(&line6->messages.fifo) != 0);
585 if (mutex_lock_interruptible(&line6->messages.read_lock))
589 if (kfifo_peek_len(&line6->messages.fifo) > count) {
590 /* Buffer too small; allow re-read of the current item... */
593 rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
598 mutex_unlock(&line6->messages.read_lock);
602 /* Write directly (no buffering) to device by user*/
604 line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
607 struct usb_line6 *line6 = hwdep->private_data;
611 if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
612 /* This is an arbitrary limit - still better than nothing... */
616 data_copy = memdup_user(data, count);
617 if (IS_ERR(data_copy))
618 return PTR_ERR(data_copy);
620 rv = line6_send_raw_message(line6, data_copy, count);
626 static const struct snd_hwdep_ops hwdep_ops = {
627 .open = line6_hwdep_open,
628 .release = line6_hwdep_release,
629 .read = line6_hwdep_read,
630 .write = line6_hwdep_write,
633 /* Insert into circular buffer */
634 static void line6_hwdep_push_message(struct usb_line6 *line6)
636 if (!line6->messages.active)
639 if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
640 /* No race condition here, there's only one writer */
641 kfifo_in(&line6->messages.fifo,
642 line6->buffer_message, line6->message_length);
643 } /* else TODO: signal overflow */
645 wake_up_interruptible(&line6->messages.wait_queue);
648 static int line6_hwdep_init(struct usb_line6 *line6)
651 struct snd_hwdep *hwdep;
653 /* TODO: usb_driver_claim_interface(); */
654 line6->process_message = line6_hwdep_push_message;
655 line6->messages.active = 0;
656 init_waitqueue_head(&line6->messages.wait_queue);
657 mutex_init(&line6->messages.read_lock);
658 INIT_KFIFO(line6->messages.fifo);
660 err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
663 strcpy(hwdep->name, "config");
664 hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
665 hwdep->ops = hwdep_ops;
666 hwdep->private_data = line6;
667 hwdep->exclusive = true;
673 static int line6_init_cap_control(struct usb_line6 *line6)
677 /* initialize USB buffers: */
678 line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
679 if (!line6->buffer_listen)
682 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
683 if (!line6->urb_listen)
686 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
687 line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
688 if (!line6->buffer_message)
691 ret = line6_init_midi(line6);
695 ret = line6_hwdep_init(line6);
700 ret = line6_start_listen(line6);
702 dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
712 int line6_probe(struct usb_interface *interface,
713 const struct usb_device_id *id,
714 const char *driver_name,
715 const struct line6_properties *properties,
716 int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
719 struct usb_device *usbdev = interface_to_usbdev(interface);
720 struct snd_card *card;
721 struct usb_line6 *line6;
722 int interface_number;
725 if (WARN_ON(data_size < sizeof(*line6)))
728 /* we don't handle multiple configurations */
729 if (usbdev->descriptor.bNumConfigurations != 1)
732 ret = snd_card_new(&interface->dev,
733 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
734 THIS_MODULE, data_size, &card);
738 /* store basic data: */
739 line6 = card->private_data;
741 line6->properties = properties;
742 line6->usbdev = usbdev;
743 line6->ifcdev = &interface->dev;
745 strcpy(card->id, properties->id);
746 strcpy(card->driver, driver_name);
747 strcpy(card->shortname, properties->name);
748 sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
749 dev_name(line6->ifcdev));
750 card->private_free = line6_destruct;
752 usb_set_intfdata(interface, line6);
754 /* increment reference counters: */
757 /* initialize device info: */
758 dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
760 /* query interface number */
761 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
763 /* TODO reserves the bus bandwidth even without actual transfer */
764 ret = usb_set_interface(usbdev, interface_number,
765 properties->altsetting);
767 dev_err(&interface->dev, "set_interface failed\n");
771 line6_get_interval(line6);
773 if (properties->capabilities & LINE6_CAP_CONTROL) {
774 ret = line6_init_cap_control(line6);
779 /* initialize device data based on device: */
780 ret = private_init(line6, id);
784 /* creation of additional special files should go here */
786 dev_info(&interface->dev, "Line 6 %s now attached\n",
792 /* we can call disconnect callback here because no close-sync is
793 * needed yet at this point
795 line6_disconnect(interface);
798 EXPORT_SYMBOL_GPL(line6_probe);
801 Line 6 device disconnected.
803 void line6_disconnect(struct usb_interface *interface)
805 struct usb_line6 *line6 = usb_get_intfdata(interface);
806 struct usb_device *usbdev = interface_to_usbdev(interface);
811 if (WARN_ON(usbdev != line6->usbdev))
814 if (line6->urb_listen != NULL)
815 line6_stop_listen(line6);
817 snd_card_disconnect(line6->card);
819 line6_pcm_disconnect(line6->line6pcm);
820 if (line6->disconnect)
821 line6->disconnect(line6);
823 dev_info(&interface->dev, "Line 6 %s now disconnected\n",
824 line6->properties->name);
826 /* make sure the device isn't destructed twice: */
827 usb_set_intfdata(interface, NULL);
829 snd_card_free_when_closed(line6->card);
831 EXPORT_SYMBOL_GPL(line6_disconnect);
836 Suspend Line 6 device.
838 int line6_suspend(struct usb_interface *interface, pm_message_t message)
840 struct usb_line6 *line6 = usb_get_intfdata(interface);
841 struct snd_line6_pcm *line6pcm = line6->line6pcm;
843 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
845 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
846 line6_stop_listen(line6);
848 if (line6pcm != NULL) {
849 snd_pcm_suspend_all(line6pcm->pcm);
855 EXPORT_SYMBOL_GPL(line6_suspend);
858 Resume Line 6 device.
860 int line6_resume(struct usb_interface *interface)
862 struct usb_line6 *line6 = usb_get_intfdata(interface);
864 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
865 line6_start_listen(line6);
867 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
870 EXPORT_SYMBOL_GPL(line6_resume);
872 #endif /* CONFIG_PM */
874 MODULE_AUTHOR(DRIVER_AUTHOR);
875 MODULE_DESCRIPTION(DRIVER_DESC);
876 MODULE_LICENSE("GPL");