1 // SPDX-License-Identifier: GPL-2.0-only
3 * Line 6 Linux USB driver
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/export.h>
11 #include <linux/slab.h>
12 #include <linux/usb.h>
14 #include <sound/core.h>
15 #include <sound/initval.h>
16 #include <sound/hwdep.h>
24 #define DRIVER_DESC "Line 6 USB Driver"
27 This is Line 6's MIDI manufacturer ID.
29 const unsigned char line6_midi_id[3] = {
32 EXPORT_SYMBOL_GPL(line6_midi_id);
35 Code to request version of POD, Variax interface
36 (and maybe other devices).
38 static const char line6_request_version[] = {
39 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
43 Class for asynchronous messages.
46 struct usb_line6 *line6;
55 static void line6_data_received(struct urb *urb);
56 static int line6_send_raw_message_async_part(struct message *msg,
60 Start to listen on endpoint.
62 static int line6_start_listen(struct usb_line6 *line6)
66 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
67 usb_fill_int_urb(line6->urb_listen, line6->usbdev,
68 usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
69 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
70 line6_data_received, line6, line6->interval);
72 usb_fill_bulk_urb(line6->urb_listen, line6->usbdev,
73 usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r),
74 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
75 line6_data_received, line6);
78 /* sanity checks of EP before actually submitting */
79 if (usb_urb_ep_type_check(line6->urb_listen)) {
80 dev_err(line6->ifcdev, "invalid control EP\n");
84 line6->urb_listen->actual_length = 0;
85 err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
90 Stop listening on endpoint.
92 static void line6_stop_listen(struct usb_line6 *line6)
94 usb_kill_urb(line6->urb_listen);
98 Send raw message in pieces of wMaxPacketSize bytes.
100 int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
104 const struct line6_properties *properties = line6->properties;
106 for (i = 0; i < size; i += line6->max_packet_size) {
108 const char *frag_buf = buffer + i;
109 int frag_size = min(line6->max_packet_size, size - i);
112 if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
113 retval = usb_interrupt_msg(line6->usbdev,
114 usb_sndintpipe(line6->usbdev, properties->ep_ctrl_w),
115 (char *)frag_buf, frag_size,
116 &partial, LINE6_TIMEOUT);
118 retval = usb_bulk_msg(line6->usbdev,
119 usb_sndbulkpipe(line6->usbdev, properties->ep_ctrl_w),
120 (char *)frag_buf, frag_size,
121 &partial, LINE6_TIMEOUT);
125 dev_err(line6->ifcdev,
126 "usb_bulk_msg failed (%d)\n", retval);
135 EXPORT_SYMBOL_GPL(line6_send_raw_message);
138 Notification of completion of asynchronous request transmission.
140 static void line6_async_request_sent(struct urb *urb)
142 struct message *msg = (struct message *)urb->context;
144 if (msg->done >= msg->size) {
148 line6_send_raw_message_async_part(msg, urb);
152 Asynchronously send part of a raw message.
154 static int line6_send_raw_message_async_part(struct message *msg,
158 struct usb_line6 *line6 = msg->line6;
159 int done = msg->done;
160 int bytes = min(msg->size - done, line6->max_packet_size);
162 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
163 usb_fill_int_urb(urb, line6->usbdev,
164 usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
165 (char *)msg->buffer + done, bytes,
166 line6_async_request_sent, msg, line6->interval);
168 usb_fill_bulk_urb(urb, line6->usbdev,
169 usb_sndbulkpipe(line6->usbdev, line6->properties->ep_ctrl_w),
170 (char *)msg->buffer + done, bytes,
171 line6_async_request_sent, msg);
176 /* sanity checks of EP before actually submitting */
177 retval = usb_urb_ep_type_check(urb);
181 retval = usb_submit_urb(urb, GFP_ATOMIC);
188 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
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,
308 LINE6_MIDIBUF_READ_RX);
313 line6->message_length = done;
314 line6_midi_receive(line6, line6->buffer_message, done);
316 if (line6->process_message)
317 line6->process_message(line6);
320 line6->buffer_message = urb->transfer_buffer;
321 line6->message_length = urb->actual_length;
322 if (line6->process_message)
323 line6->process_message(line6);
324 line6->buffer_message = NULL;
327 line6_start_listen(line6);
330 #define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */
331 #define LINE6_READ_WRITE_MAX_RETRIES 50
334 Read data from device.
336 int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
339 struct usb_device *usbdev = line6->usbdev;
344 if (address > 0xffff || datalen > 0xff)
347 /* query the serial number: */
348 ret = usb_control_msg_send(usbdev, 0, 0x67,
349 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
350 (datalen << 8) | 0x21, address, NULL, 0,
351 LINE6_TIMEOUT, GFP_KERNEL);
353 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
357 /* Wait for data length. We'll get 0xff until length arrives. */
358 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
359 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
361 ret = usb_control_msg_recv(usbdev, 0, 0x67,
362 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
363 0x0012, 0x0000, &len, 1,
364 LINE6_TIMEOUT, GFP_KERNEL);
366 dev_err(line6->ifcdev,
367 "receive length failed (error %d)\n", ret);
377 dev_err(line6->ifcdev, "read failed after %d retries\n",
380 } else if (len != datalen) {
381 /* should be equal or something went wrong */
382 dev_err(line6->ifcdev,
383 "length mismatch (expected %d, got %d)\n",
388 /* receive the result: */
389 ret = usb_control_msg_recv(usbdev, 0, 0x67,
390 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
391 0x0013, 0x0000, data, datalen, LINE6_TIMEOUT,
394 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
399 EXPORT_SYMBOL_GPL(line6_read_data);
402 Write data to device.
404 int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
407 struct usb_device *usbdev = line6->usbdev;
409 unsigned char *status;
412 if (address > 0xffff || datalen > 0xffff)
415 status = kmalloc(1, GFP_KERNEL);
419 ret = usb_control_msg_send(usbdev, 0, 0x67,
420 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
421 0x0022, address, data, datalen, LINE6_TIMEOUT,
424 dev_err(line6->ifcdev,
425 "write request failed (error %d)\n", ret);
429 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
430 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
432 ret = usb_control_msg_recv(usbdev, 0, 0x67,
433 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
434 0x0012, 0x0000, status, 1, LINE6_TIMEOUT,
437 dev_err(line6->ifcdev,
438 "receiving status failed (error %d)\n", ret);
446 if (*status == 0xff) {
447 dev_err(line6->ifcdev, "write failed after %d retries\n",
450 } else if (*status != 0) {
451 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
458 EXPORT_SYMBOL_GPL(line6_write_data);
461 Read Line 6 device serial number.
462 (POD, TonePort, GuitarPort)
464 int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
466 return line6_read_data(line6, 0x80d0, serial_number,
467 sizeof(*serial_number));
469 EXPORT_SYMBOL_GPL(line6_read_serial_number);
474 static void line6_destruct(struct snd_card *card)
476 struct usb_line6 *line6 = card->private_data;
477 struct usb_device *usbdev = line6->usbdev;
479 /* Free buffer memory first. We cannot depend on the existence of private
480 * data from the (podhd) module, it may be gone already during this call
482 kfree(line6->buffer_message);
484 kfree(line6->buffer_listen);
486 /* then free URBs: */
487 usb_free_urb(line6->urb_listen);
488 line6->urb_listen = NULL;
490 /* decrement reference counters: */
494 static void line6_get_usb_properties(struct usb_line6 *line6)
496 struct usb_device *usbdev = line6->usbdev;
497 const struct line6_properties *properties = line6->properties;
499 struct usb_host_endpoint *ep = NULL;
501 if (properties->capabilities & LINE6_CAP_CONTROL) {
502 if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
503 pipe = usb_rcvintpipe(line6->usbdev,
504 line6->properties->ep_ctrl_r);
506 pipe = usb_rcvbulkpipe(line6->usbdev,
507 line6->properties->ep_ctrl_r);
509 ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
512 /* Control data transfer properties */
514 line6->interval = ep->desc.bInterval;
515 line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
517 if (properties->capabilities & LINE6_CAP_CONTROL) {
518 dev_err(line6->ifcdev,
519 "endpoint not available, using fallback values");
521 line6->interval = LINE6_FALLBACK_INTERVAL;
522 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
525 /* Isochronous transfer properties */
526 if (usbdev->speed == USB_SPEED_LOW) {
527 line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
528 line6->iso_buffers = USB_LOW_ISO_BUFFERS;
530 line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
531 line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
535 /* Enable buffering of incoming messages, flush the buffer */
536 static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
538 struct usb_line6 *line6 = hw->private_data;
540 /* NOTE: hwdep layer provides atomicity here */
542 line6->messages.active = 1;
543 line6->messages.nonblock = file->f_flags & O_NONBLOCK ? 1 : 0;
549 static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
551 struct usb_line6 *line6 = hw->private_data;
553 line6->messages.active = 0;
558 /* Read from circular buffer, return to user */
560 line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
563 struct usb_line6 *line6 = hwdep->private_data;
565 unsigned int out_count;
567 if (mutex_lock_interruptible(&line6->messages.read_lock))
570 while (kfifo_len(&line6->messages.fifo) == 0) {
571 mutex_unlock(&line6->messages.read_lock);
573 if (line6->messages.nonblock)
576 rv = wait_event_interruptible(
577 line6->messages.wait_queue,
578 kfifo_len(&line6->messages.fifo) != 0);
582 if (mutex_lock_interruptible(&line6->messages.read_lock))
586 if (kfifo_peek_len(&line6->messages.fifo) > count) {
587 /* Buffer too small; allow re-read of the current item... */
590 rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
595 mutex_unlock(&line6->messages.read_lock);
599 /* Write directly (no buffering) to device by user*/
601 line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
604 struct usb_line6 *line6 = hwdep->private_data;
608 if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
609 /* This is an arbitrary limit - still better than nothing... */
613 data_copy = memdup_user(data, count);
614 if (IS_ERR(data_copy))
615 return PTR_ERR(data_copy);
617 rv = line6_send_raw_message(line6, data_copy, count);
624 line6_hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
627 struct usb_line6 *line6 = hwdep->private_data;
629 poll_wait(file, &line6->messages.wait_queue, wait);
631 mutex_lock(&line6->messages.read_lock);
632 rv = kfifo_len(&line6->messages.fifo) == 0 ? 0 : EPOLLIN | EPOLLRDNORM;
633 mutex_unlock(&line6->messages.read_lock);
638 static const struct snd_hwdep_ops hwdep_ops = {
639 .open = line6_hwdep_open,
640 .release = line6_hwdep_release,
641 .read = line6_hwdep_read,
642 .write = line6_hwdep_write,
643 .poll = line6_hwdep_poll,
646 /* Insert into circular buffer */
647 static void line6_hwdep_push_message(struct usb_line6 *line6)
649 if (!line6->messages.active)
652 if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
653 /* No race condition here, there's only one writer */
654 kfifo_in(&line6->messages.fifo,
655 line6->buffer_message, line6->message_length);
656 } /* else TODO: signal overflow */
658 wake_up_interruptible(&line6->messages.wait_queue);
661 static int line6_hwdep_init(struct usb_line6 *line6)
664 struct snd_hwdep *hwdep;
666 /* TODO: usb_driver_claim_interface(); */
667 line6->process_message = line6_hwdep_push_message;
668 line6->messages.active = 0;
669 init_waitqueue_head(&line6->messages.wait_queue);
670 mutex_init(&line6->messages.read_lock);
671 INIT_KFIFO(line6->messages.fifo);
673 err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
676 strcpy(hwdep->name, "config");
677 hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
678 hwdep->ops = hwdep_ops;
679 hwdep->private_data = line6;
680 hwdep->exclusive = true;
686 static int line6_init_cap_control(struct usb_line6 *line6)
690 /* initialize USB buffers: */
691 line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
692 if (!line6->buffer_listen)
695 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
696 if (!line6->urb_listen)
699 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
700 line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
701 if (!line6->buffer_message)
704 ret = line6_init_midi(line6);
708 ret = line6_hwdep_init(line6);
713 ret = line6_start_listen(line6);
715 dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
722 static void line6_startup_work(struct work_struct *work)
724 struct usb_line6 *line6 =
725 container_of(work, struct usb_line6, startup_work.work);
728 line6->startup(line6);
734 int line6_probe(struct usb_interface *interface,
735 const struct usb_device_id *id,
736 const char *driver_name,
737 const struct line6_properties *properties,
738 int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
741 struct usb_device *usbdev = interface_to_usbdev(interface);
742 struct snd_card *card;
743 struct usb_line6 *line6;
744 int interface_number;
747 if (WARN_ON(data_size < sizeof(*line6)))
750 /* we don't handle multiple configurations */
751 if (usbdev->descriptor.bNumConfigurations != 1)
754 ret = snd_card_new(&interface->dev,
755 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
756 THIS_MODULE, data_size, &card);
760 /* store basic data: */
761 line6 = card->private_data;
763 line6->properties = properties;
764 line6->usbdev = usbdev;
765 line6->ifcdev = &interface->dev;
766 INIT_DELAYED_WORK(&line6->startup_work, line6_startup_work);
768 strcpy(card->id, properties->id);
769 strcpy(card->driver, driver_name);
770 strcpy(card->shortname, properties->name);
771 sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
772 dev_name(line6->ifcdev));
773 card->private_free = line6_destruct;
775 usb_set_intfdata(interface, line6);
777 /* increment reference counters: */
780 /* initialize device info: */
781 dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
783 /* query interface number */
784 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
786 /* TODO reserves the bus bandwidth even without actual transfer */
787 ret = usb_set_interface(usbdev, interface_number,
788 properties->altsetting);
790 dev_err(&interface->dev, "set_interface failed\n");
794 line6_get_usb_properties(line6);
796 if (properties->capabilities & LINE6_CAP_CONTROL) {
797 ret = line6_init_cap_control(line6);
802 /* initialize device data based on device: */
803 ret = private_init(line6, id);
807 /* creation of additional special files should go here */
809 dev_info(&interface->dev, "Line 6 %s now attached\n",
815 /* we can call disconnect callback here because no close-sync is
816 * needed yet at this point
818 line6_disconnect(interface);
821 EXPORT_SYMBOL_GPL(line6_probe);
824 Line 6 device disconnected.
826 void line6_disconnect(struct usb_interface *interface)
828 struct usb_line6 *line6 = usb_get_intfdata(interface);
829 struct usb_device *usbdev = interface_to_usbdev(interface);
834 if (WARN_ON(usbdev != line6->usbdev))
837 cancel_delayed_work_sync(&line6->startup_work);
839 if (line6->urb_listen != NULL)
840 line6_stop_listen(line6);
842 snd_card_disconnect(line6->card);
844 line6_pcm_disconnect(line6->line6pcm);
845 if (line6->disconnect)
846 line6->disconnect(line6);
848 dev_info(&interface->dev, "Line 6 %s now disconnected\n",
849 line6->properties->name);
851 /* make sure the device isn't destructed twice: */
852 usb_set_intfdata(interface, NULL);
854 snd_card_free_when_closed(line6->card);
856 EXPORT_SYMBOL_GPL(line6_disconnect);
861 Suspend Line 6 device.
863 int line6_suspend(struct usb_interface *interface, pm_message_t message)
865 struct usb_line6 *line6 = usb_get_intfdata(interface);
866 struct snd_line6_pcm *line6pcm = line6->line6pcm;
868 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
870 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
871 line6_stop_listen(line6);
873 if (line6pcm != NULL)
878 EXPORT_SYMBOL_GPL(line6_suspend);
881 Resume Line 6 device.
883 int line6_resume(struct usb_interface *interface)
885 struct usb_line6 *line6 = usb_get_intfdata(interface);
887 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
888 line6_start_listen(line6);
890 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
893 EXPORT_SYMBOL_GPL(line6_resume);
895 #endif /* CONFIG_PM */
897 MODULE_AUTHOR(DRIVER_AUTHOR);
898 MODULE_DESCRIPTION(DRIVER_DESC);
899 MODULE_LICENSE("GPL");