1 // SPDX-License-Identifier: GPL-2.0-only
3 * Line 6 Linux USB driver
5 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
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>
23 #define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
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 static 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
* HZ
);
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
* HZ
);
125 dev_err(line6
->ifcdev
,
126 "usb_bulk_msg failed (%d)\n", retval
);
137 Notification of completion of asynchronous request transmission.
139 static void line6_async_request_sent(struct urb
*urb
)
141 struct message
*msg
= (struct message
*)urb
->context
;
143 if (msg
->done
>= msg
->size
) {
147 line6_send_raw_message_async_part(msg
, urb
);
151 Asynchronously send part of a raw message.
153 static int line6_send_raw_message_async_part(struct message
*msg
,
157 struct usb_line6
*line6
= msg
->line6
;
158 int done
= msg
->done
;
159 int bytes
= min(msg
->size
- done
, line6
->max_packet_size
);
161 if (line6
->properties
->capabilities
& LINE6_CAP_CONTROL_MIDI
) {
162 usb_fill_int_urb(urb
, line6
->usbdev
,
163 usb_sndintpipe(line6
->usbdev
, line6
->properties
->ep_ctrl_w
),
164 (char *)msg
->buffer
+ done
, bytes
,
165 line6_async_request_sent
, msg
, line6
->interval
);
167 usb_fill_bulk_urb(urb
, line6
->usbdev
,
168 usb_sndbulkpipe(line6
->usbdev
, line6
->properties
->ep_ctrl_w
),
169 (char *)msg
->buffer
+ done
, bytes
,
170 line6_async_request_sent
, msg
);
175 /* sanity checks of EP before actually submitting */
176 retval
= usb_urb_ep_type_check(urb
);
180 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
187 dev_err(line6
->ifcdev
, "%s: usb_submit_urb failed (%d)\n",
195 Asynchronously send raw message.
197 int line6_send_raw_message_async(struct usb_line6
*line6
, const char *buffer
,
203 /* create message: */
204 msg
= kmalloc(sizeof(struct message
), GFP_ATOMIC
);
209 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
216 /* set message data: */
218 msg
->buffer
= buffer
;
223 return line6_send_raw_message_async_part(msg
, urb
);
225 EXPORT_SYMBOL_GPL(line6_send_raw_message_async
);
228 Send asynchronous device version request.
230 int line6_version_request_async(struct usb_line6
*line6
)
235 buffer
= kmemdup(line6_request_version
,
236 sizeof(line6_request_version
), GFP_ATOMIC
);
240 retval
= line6_send_raw_message_async(line6
, buffer
,
241 sizeof(line6_request_version
));
245 EXPORT_SYMBOL_GPL(line6_version_request_async
);
248 Send sysex message in pieces of wMaxPacketSize bytes.
250 int line6_send_sysex_message(struct usb_line6
*line6
, const char *buffer
,
253 return line6_send_raw_message(line6
, buffer
,
254 size
+ SYSEX_EXTRA_SIZE
) -
257 EXPORT_SYMBOL_GPL(line6_send_sysex_message
);
260 Allocate buffer for sysex message and prepare header.
261 @param code sysex message code
262 @param size number of bytes between code and sysex end
264 char *line6_alloc_sysex_buffer(struct usb_line6
*line6
, int code1
, int code2
,
267 char *buffer
= kmalloc(size
+ SYSEX_EXTRA_SIZE
, GFP_ATOMIC
);
272 buffer
[0] = LINE6_SYSEX_BEGIN
;
273 memcpy(buffer
+ 1, line6_midi_id
, sizeof(line6_midi_id
));
274 buffer
[sizeof(line6_midi_id
) + 1] = code1
;
275 buffer
[sizeof(line6_midi_id
) + 2] = code2
;
276 buffer
[sizeof(line6_midi_id
) + 3 + size
] = LINE6_SYSEX_END
;
279 EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer
);
282 Notification of data received from the Line 6 device.
284 static void line6_data_received(struct urb
*urb
)
286 struct usb_line6
*line6
= (struct usb_line6
*)urb
->context
;
287 struct midi_buffer
*mb
= &line6
->line6midi
->midibuf_in
;
290 if (urb
->status
== -ESHUTDOWN
)
293 if (line6
->properties
->capabilities
& LINE6_CAP_CONTROL_MIDI
) {
295 line6_midibuf_write(mb
, urb
->transfer_buffer
, urb
->actual_length
);
297 if (done
< urb
->actual_length
) {
298 line6_midibuf_ignore(mb
, done
);
299 dev_dbg(line6
->ifcdev
, "%d %d buffer overflow - message skipped\n",
300 done
, urb
->actual_length
);
305 line6_midibuf_read(mb
, line6
->buffer_message
,
306 LINE6_MIDI_MESSAGE_MAXLEN
);
311 line6
->message_length
= done
;
312 line6_midi_receive(line6
, line6
->buffer_message
, done
);
314 if (line6
->process_message
)
315 line6
->process_message(line6
);
318 line6
->buffer_message
= urb
->transfer_buffer
;
319 line6
->message_length
= urb
->actual_length
;
320 if (line6
->process_message
)
321 line6
->process_message(line6
);
322 line6
->buffer_message
= NULL
;
325 line6_start_listen(line6
);
328 #define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */
329 #define LINE6_READ_WRITE_MAX_RETRIES 50
332 Read data from device.
334 int line6_read_data(struct usb_line6
*line6
, unsigned address
, void *data
,
337 struct usb_device
*usbdev
= line6
->usbdev
;
342 if (address
> 0xffff || datalen
> 0xff)
345 len
= kmalloc(1, GFP_KERNEL
);
349 /* query the serial number: */
350 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0), 0x67,
351 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
352 (datalen
<< 8) | 0x21, address
,
353 NULL
, 0, LINE6_TIMEOUT
* HZ
);
356 dev_err(line6
->ifcdev
, "read request failed (error %d)\n", ret
);
360 /* Wait for data length. We'll get 0xff until length arrives. */
361 for (count
= 0; count
< LINE6_READ_WRITE_MAX_RETRIES
; count
++) {
362 mdelay(LINE6_READ_WRITE_STATUS_DELAY
);
364 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0), 0x67,
365 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
|
367 0x0012, 0x0000, len
, 1,
370 dev_err(line6
->ifcdev
,
371 "receive length failed (error %d)\n", ret
);
381 dev_err(line6
->ifcdev
, "read failed after %d retries\n",
384 } else if (*len
!= datalen
) {
385 /* should be equal or something went wrong */
386 dev_err(line6
->ifcdev
,
387 "length mismatch (expected %d, got %d)\n",
388 (int)datalen
, (int)*len
);
392 /* receive the result: */
393 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0), 0x67,
394 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
395 0x0013, 0x0000, data
, datalen
,
399 dev_err(line6
->ifcdev
, "read failed (error %d)\n", ret
);
405 EXPORT_SYMBOL_GPL(line6_read_data
);
408 Write data to device.
410 int line6_write_data(struct usb_line6
*line6
, unsigned address
, void *data
,
413 struct usb_device
*usbdev
= line6
->usbdev
;
415 unsigned char *status
;
418 if (address
> 0xffff || datalen
> 0xffff)
421 status
= kmalloc(1, GFP_KERNEL
);
425 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0), 0x67,
426 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
427 0x0022, address
, data
, datalen
,
431 dev_err(line6
->ifcdev
,
432 "write request failed (error %d)\n", ret
);
436 for (count
= 0; count
< LINE6_READ_WRITE_MAX_RETRIES
; count
++) {
437 mdelay(LINE6_READ_WRITE_STATUS_DELAY
);
439 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
441 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
|
444 status
, 1, LINE6_TIMEOUT
* HZ
);
447 dev_err(line6
->ifcdev
,
448 "receiving status failed (error %d)\n", ret
);
456 if (*status
== 0xff) {
457 dev_err(line6
->ifcdev
, "write failed after %d retries\n",
460 } else if (*status
!= 0) {
461 dev_err(line6
->ifcdev
, "write failed (error %d)\n", ret
);
468 EXPORT_SYMBOL_GPL(line6_write_data
);
471 Read Line 6 device serial number.
472 (POD, TonePort, GuitarPort)
474 int line6_read_serial_number(struct usb_line6
*line6
, u32
*serial_number
)
476 return line6_read_data(line6
, 0x80d0, serial_number
,
477 sizeof(*serial_number
));
479 EXPORT_SYMBOL_GPL(line6_read_serial_number
);
484 static void line6_destruct(struct snd_card
*card
)
486 struct usb_line6
*line6
= card
->private_data
;
487 struct usb_device
*usbdev
= line6
->usbdev
;
489 /* Free buffer memory first. We cannot depend on the existence of private
490 * data from the (podhd) module, it may be gone already during this call
492 kfree(line6
->buffer_message
);
494 kfree(line6
->buffer_listen
);
496 /* then free URBs: */
497 usb_free_urb(line6
->urb_listen
);
498 line6
->urb_listen
= NULL
;
500 /* decrement reference counters: */
504 static void line6_get_usb_properties(struct usb_line6
*line6
)
506 struct usb_device
*usbdev
= line6
->usbdev
;
507 const struct line6_properties
*properties
= line6
->properties
;
509 struct usb_host_endpoint
*ep
= NULL
;
511 if (properties
->capabilities
& LINE6_CAP_CONTROL
) {
512 if (properties
->capabilities
& LINE6_CAP_CONTROL_MIDI
) {
513 pipe
= usb_rcvintpipe(line6
->usbdev
,
514 line6
->properties
->ep_ctrl_r
);
516 pipe
= usb_rcvbulkpipe(line6
->usbdev
,
517 line6
->properties
->ep_ctrl_r
);
519 ep
= usbdev
->ep_in
[usb_pipeendpoint(pipe
)];
522 /* Control data transfer properties */
524 line6
->interval
= ep
->desc
.bInterval
;
525 line6
->max_packet_size
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
527 if (properties
->capabilities
& LINE6_CAP_CONTROL
) {
528 dev_err(line6
->ifcdev
,
529 "endpoint not available, using fallback values");
531 line6
->interval
= LINE6_FALLBACK_INTERVAL
;
532 line6
->max_packet_size
= LINE6_FALLBACK_MAXPACKETSIZE
;
535 /* Isochronous transfer properties */
536 if (usbdev
->speed
== USB_SPEED_LOW
) {
537 line6
->intervals_per_second
= USB_LOW_INTERVALS_PER_SECOND
;
538 line6
->iso_buffers
= USB_LOW_ISO_BUFFERS
;
540 line6
->intervals_per_second
= USB_HIGH_INTERVALS_PER_SECOND
;
541 line6
->iso_buffers
= USB_HIGH_ISO_BUFFERS
;
545 /* Enable buffering of incoming messages, flush the buffer */
546 static int line6_hwdep_open(struct snd_hwdep
*hw
, struct file
*file
)
548 struct usb_line6
*line6
= hw
->private_data
;
550 /* NOTE: hwdep layer provides atomicity here */
552 line6
->messages
.active
= 1;
558 static int line6_hwdep_release(struct snd_hwdep
*hw
, struct file
*file
)
560 struct usb_line6
*line6
= hw
->private_data
;
562 line6
->messages
.active
= 0;
567 /* Read from circular buffer, return to user */
569 line6_hwdep_read(struct snd_hwdep
*hwdep
, char __user
*buf
, long count
,
572 struct usb_line6
*line6
= hwdep
->private_data
;
574 unsigned int out_count
;
576 if (mutex_lock_interruptible(&line6
->messages
.read_lock
))
579 while (kfifo_len(&line6
->messages
.fifo
) == 0) {
580 mutex_unlock(&line6
->messages
.read_lock
);
582 rv
= wait_event_interruptible(
583 line6
->messages
.wait_queue
,
584 kfifo_len(&line6
->messages
.fifo
) != 0);
588 if (mutex_lock_interruptible(&line6
->messages
.read_lock
))
592 if (kfifo_peek_len(&line6
->messages
.fifo
) > count
) {
593 /* Buffer too small; allow re-read of the current item... */
596 rv
= kfifo_to_user(&line6
->messages
.fifo
, buf
, count
, &out_count
);
601 mutex_unlock(&line6
->messages
.read_lock
);
605 /* Write directly (no buffering) to device by user*/
607 line6_hwdep_write(struct snd_hwdep
*hwdep
, const char __user
*data
, long count
,
610 struct usb_line6
*line6
= hwdep
->private_data
;
614 if (count
> line6
->max_packet_size
* LINE6_RAW_MESSAGES_MAXCOUNT
) {
615 /* This is an arbitrary limit - still better than nothing... */
619 data_copy
= memdup_user(data
, count
);
620 if (IS_ERR(data_copy
))
621 return PTR_ERR(data_copy
);
623 rv
= line6_send_raw_message(line6
, data_copy
, count
);
629 static const struct snd_hwdep_ops hwdep_ops
= {
630 .open
= line6_hwdep_open
,
631 .release
= line6_hwdep_release
,
632 .read
= line6_hwdep_read
,
633 .write
= line6_hwdep_write
,
636 /* Insert into circular buffer */
637 static void line6_hwdep_push_message(struct usb_line6
*line6
)
639 if (!line6
->messages
.active
)
642 if (kfifo_avail(&line6
->messages
.fifo
) >= line6
->message_length
) {
643 /* No race condition here, there's only one writer */
644 kfifo_in(&line6
->messages
.fifo
,
645 line6
->buffer_message
, line6
->message_length
);
646 } /* else TODO: signal overflow */
648 wake_up_interruptible(&line6
->messages
.wait_queue
);
651 static int line6_hwdep_init(struct usb_line6
*line6
)
654 struct snd_hwdep
*hwdep
;
656 /* TODO: usb_driver_claim_interface(); */
657 line6
->process_message
= line6_hwdep_push_message
;
658 line6
->messages
.active
= 0;
659 init_waitqueue_head(&line6
->messages
.wait_queue
);
660 mutex_init(&line6
->messages
.read_lock
);
661 INIT_KFIFO(line6
->messages
.fifo
);
663 err
= snd_hwdep_new(line6
->card
, "config", 0, &hwdep
);
666 strcpy(hwdep
->name
, "config");
667 hwdep
->iface
= SNDRV_HWDEP_IFACE_LINE6
;
668 hwdep
->ops
= hwdep_ops
;
669 hwdep
->private_data
= line6
;
670 hwdep
->exclusive
= true;
676 static int line6_init_cap_control(struct usb_line6
*line6
)
680 /* initialize USB buffers: */
681 line6
->buffer_listen
= kmalloc(LINE6_BUFSIZE_LISTEN
, GFP_KERNEL
);
682 if (!line6
->buffer_listen
)
685 line6
->urb_listen
= usb_alloc_urb(0, GFP_KERNEL
);
686 if (!line6
->urb_listen
)
689 if (line6
->properties
->capabilities
& LINE6_CAP_CONTROL_MIDI
) {
690 line6
->buffer_message
= kmalloc(LINE6_MIDI_MESSAGE_MAXLEN
, GFP_KERNEL
);
691 if (!line6
->buffer_message
)
694 ret
= line6_hwdep_init(line6
);
699 ret
= line6_start_listen(line6
);
701 dev_err(line6
->ifcdev
, "cannot start listening: %d\n", ret
);
708 static void line6_startup_work(struct work_struct
*work
)
710 struct usb_line6
*line6
=
711 container_of(work
, struct usb_line6
, startup_work
.work
);
714 line6
->startup(line6
);
720 int line6_probe(struct usb_interface
*interface
,
721 const struct usb_device_id
*id
,
722 const char *driver_name
,
723 const struct line6_properties
*properties
,
724 int (*private_init
)(struct usb_line6
*, const struct usb_device_id
*id
),
727 struct usb_device
*usbdev
= interface_to_usbdev(interface
);
728 struct snd_card
*card
;
729 struct usb_line6
*line6
;
730 int interface_number
;
733 if (WARN_ON(data_size
< sizeof(*line6
)))
736 /* we don't handle multiple configurations */
737 if (usbdev
->descriptor
.bNumConfigurations
!= 1)
740 ret
= snd_card_new(&interface
->dev
,
741 SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
742 THIS_MODULE
, data_size
, &card
);
746 /* store basic data: */
747 line6
= card
->private_data
;
749 line6
->properties
= properties
;
750 line6
->usbdev
= usbdev
;
751 line6
->ifcdev
= &interface
->dev
;
752 INIT_DELAYED_WORK(&line6
->startup_work
, line6_startup_work
);
754 strcpy(card
->id
, properties
->id
);
755 strcpy(card
->driver
, driver_name
);
756 strcpy(card
->shortname
, properties
->name
);
757 sprintf(card
->longname
, "Line 6 %s at USB %s", properties
->name
,
758 dev_name(line6
->ifcdev
));
759 card
->private_free
= line6_destruct
;
761 usb_set_intfdata(interface
, line6
);
763 /* increment reference counters: */
766 /* initialize device info: */
767 dev_info(&interface
->dev
, "Line 6 %s found\n", properties
->name
);
769 /* query interface number */
770 interface_number
= interface
->cur_altsetting
->desc
.bInterfaceNumber
;
772 /* TODO reserves the bus bandwidth even without actual transfer */
773 ret
= usb_set_interface(usbdev
, interface_number
,
774 properties
->altsetting
);
776 dev_err(&interface
->dev
, "set_interface failed\n");
780 line6_get_usb_properties(line6
);
782 if (properties
->capabilities
& LINE6_CAP_CONTROL
) {
783 ret
= line6_init_cap_control(line6
);
788 /* initialize device data based on device: */
789 ret
= private_init(line6
, id
);
793 /* creation of additional special files should go here */
795 dev_info(&interface
->dev
, "Line 6 %s now attached\n",
801 /* we can call disconnect callback here because no close-sync is
802 * needed yet at this point
804 line6_disconnect(interface
);
807 EXPORT_SYMBOL_GPL(line6_probe
);
810 Line 6 device disconnected.
812 void line6_disconnect(struct usb_interface
*interface
)
814 struct usb_line6
*line6
= usb_get_intfdata(interface
);
815 struct usb_device
*usbdev
= interface_to_usbdev(interface
);
820 if (WARN_ON(usbdev
!= line6
->usbdev
))
823 cancel_delayed_work(&line6
->startup_work
);
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
)
864 EXPORT_SYMBOL_GPL(line6_suspend
);
867 Resume Line 6 device.
869 int line6_resume(struct usb_interface
*interface
)
871 struct usb_line6
*line6
= usb_get_intfdata(interface
);
873 if (line6
->properties
->capabilities
& LINE6_CAP_CONTROL
)
874 line6_start_listen(line6
);
876 snd_power_change_state(line6
->card
, SNDRV_CTL_POWER_D0
);
879 EXPORT_SYMBOL_GPL(line6_resume
);
881 #endif /* CONFIG_PM */
883 MODULE_AUTHOR(DRIVER_AUTHOR
);
884 MODULE_DESCRIPTION(DRIVER_DESC
);
885 MODULE_LICENSE("GPL");