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>
28 #define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
29 #define DRIVER_DESC "Line 6 USB Driver"
32 This is Line 6's MIDI manufacturer ID.
34 const unsigned char line6_midi_id
[] = {
37 EXPORT_SYMBOL_GPL(line6_midi_id
);
40 Code to request version of POD, Variax interface
41 (and maybe other devices).
43 static const char line6_request_version
[] = {
44 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
48 Class for asynchronous messages.
51 struct usb_line6
*line6
;
60 static void line6_data_received(struct urb
*urb
);
61 static int line6_send_raw_message_async_part(struct message
*msg
,
65 Start to listen on endpoint.
67 static int line6_start_listen(struct usb_line6
*line6
)
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
);
75 line6
->urb_listen
->actual_length
= 0;
76 err
= usb_submit_urb(line6
->urb_listen
, GFP_ATOMIC
);
81 Stop listening on endpoint.
83 static void line6_stop_listen(struct usb_line6
*line6
)
85 usb_kill_urb(line6
->urb_listen
);
89 Send raw message in pieces of wMaxPacketSize bytes.
91 static int line6_send_raw_message(struct usb_line6
*line6
, const char *buffer
,
96 for (i
= 0; i
< size
; i
+= line6
->max_packet_size
) {
98 const char *frag_buf
= buffer
+ i
;
99 int frag_size
= min(line6
->max_packet_size
, size
- i
);
102 retval
= usb_interrupt_msg(line6
->usbdev
,
103 usb_sndintpipe(line6
->usbdev
,
104 line6
->properties
->ep_ctrl_w
),
105 (char *)frag_buf
, frag_size
,
106 &partial
, LINE6_TIMEOUT
* HZ
);
109 dev_err(line6
->ifcdev
,
110 "usb_interrupt_msg failed (%d)\n", retval
);
121 Notification of completion of asynchronous request transmission.
123 static void line6_async_request_sent(struct urb
*urb
)
125 struct message
*msg
= (struct message
*)urb
->context
;
127 if (msg
->done
>= msg
->size
) {
131 line6_send_raw_message_async_part(msg
, urb
);
135 Asynchronously send part of a raw message.
137 static int line6_send_raw_message_async_part(struct message
*msg
,
141 struct usb_line6
*line6
= msg
->line6
;
142 int done
= msg
->done
;
143 int bytes
= min(msg
->size
- done
, line6
->max_packet_size
);
145 usb_fill_int_urb(urb
, line6
->usbdev
,
146 usb_sndintpipe(line6
->usbdev
, line6
->properties
->ep_ctrl_w
),
147 (char *)msg
->buffer
+ done
, bytes
,
148 line6_async_request_sent
, msg
, line6
->interval
);
151 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
154 dev_err(line6
->ifcdev
, "%s: usb_submit_urb failed (%d)\n",
165 Setup and start timer.
167 void line6_start_timer(struct timer_list
*timer
, unsigned int msecs
,
168 void (*function
)(unsigned long), unsigned long data
)
170 setup_timer(timer
, function
, data
);
171 mod_timer(timer
, jiffies
+ msecs
* HZ
/ 1000);
173 EXPORT_SYMBOL_GPL(line6_start_timer
);
176 Asynchronously send raw message.
178 int line6_send_raw_message_async(struct usb_line6
*line6
, const char *buffer
,
184 /* create message: */
185 msg
= kmalloc(sizeof(struct message
), GFP_ATOMIC
);
190 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
197 /* set message data: */
199 msg
->buffer
= buffer
;
204 return line6_send_raw_message_async_part(msg
, urb
);
206 EXPORT_SYMBOL_GPL(line6_send_raw_message_async
);
209 Send asynchronous device version request.
211 int line6_version_request_async(struct usb_line6
*line6
)
216 buffer
= kmemdup(line6_request_version
,
217 sizeof(line6_request_version
), GFP_ATOMIC
);
221 retval
= line6_send_raw_message_async(line6
, buffer
,
222 sizeof(line6_request_version
));
226 EXPORT_SYMBOL_GPL(line6_version_request_async
);
229 Send sysex message in pieces of wMaxPacketSize bytes.
231 int line6_send_sysex_message(struct usb_line6
*line6
, const char *buffer
,
234 return line6_send_raw_message(line6
, buffer
,
235 size
+ SYSEX_EXTRA_SIZE
) -
238 EXPORT_SYMBOL_GPL(line6_send_sysex_message
);
241 Allocate buffer for sysex message and prepare header.
242 @param code sysex message code
243 @param size number of bytes between code and sysex end
245 char *line6_alloc_sysex_buffer(struct usb_line6
*line6
, int code1
, int code2
,
248 char *buffer
= kmalloc(size
+ SYSEX_EXTRA_SIZE
, GFP_ATOMIC
);
253 buffer
[0] = LINE6_SYSEX_BEGIN
;
254 memcpy(buffer
+ 1, line6_midi_id
, sizeof(line6_midi_id
));
255 buffer
[sizeof(line6_midi_id
) + 1] = code1
;
256 buffer
[sizeof(line6_midi_id
) + 2] = code2
;
257 buffer
[sizeof(line6_midi_id
) + 3 + size
] = LINE6_SYSEX_END
;
260 EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer
);
263 Notification of data received from the Line 6 device.
265 static void line6_data_received(struct urb
*urb
)
267 struct usb_line6
*line6
= (struct usb_line6
*)urb
->context
;
268 struct midi_buffer
*mb
= &line6
->line6midi
->midibuf_in
;
271 if (urb
->status
== -ESHUTDOWN
)
275 line6_midibuf_write(mb
, urb
->transfer_buffer
, urb
->actual_length
);
277 if (done
< urb
->actual_length
) {
278 line6_midibuf_ignore(mb
, done
);
279 dev_dbg(line6
->ifcdev
, "%d %d buffer overflow - message skipped\n",
280 done
, urb
->actual_length
);
285 line6_midibuf_read(mb
, line6
->buffer_message
,
286 LINE6_MESSAGE_MAXLEN
);
291 line6
->message_length
= done
;
292 line6_midi_receive(line6
, line6
->buffer_message
, done
);
294 if (line6
->process_message
)
295 line6
->process_message(line6
);
298 line6_start_listen(line6
);
302 Read data from device.
304 int line6_read_data(struct usb_line6
*line6
, int address
, void *data
,
307 struct usb_device
*usbdev
= line6
->usbdev
;
311 /* query the serial number: */
312 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0), 0x67,
313 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
314 (datalen
<< 8) | 0x21, address
,
315 NULL
, 0, LINE6_TIMEOUT
* HZ
);
318 dev_err(line6
->ifcdev
, "read request failed (error %d)\n", ret
);
322 /* Wait for data length. We'll get 0xff until length arrives. */
324 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0), 0x67,
325 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
|
327 0x0012, 0x0000, &len
, 1,
330 dev_err(line6
->ifcdev
,
331 "receive length failed (error %d)\n", ret
);
334 } while (len
== 0xff);
336 if (len
!= datalen
) {
337 /* should be equal or something went wrong */
338 dev_err(line6
->ifcdev
,
339 "length mismatch (expected %d, got %d)\n",
340 (int)datalen
, (int)len
);
344 /* receive the result: */
345 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0), 0x67,
346 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
347 0x0013, 0x0000, data
, datalen
,
351 dev_err(line6
->ifcdev
, "read failed (error %d)\n", ret
);
357 EXPORT_SYMBOL_GPL(line6_read_data
);
360 Write data to device.
362 int line6_write_data(struct usb_line6
*line6
, int address
, void *data
,
365 struct usb_device
*usbdev
= line6
->usbdev
;
367 unsigned char status
;
369 ret
= usb_control_msg(usbdev
, usb_sndctrlpipe(usbdev
, 0), 0x67,
370 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
371 0x0022, address
, data
, datalen
,
375 dev_err(line6
->ifcdev
,
376 "write request failed (error %d)\n", ret
);
381 ret
= usb_control_msg(usbdev
, usb_rcvctrlpipe(usbdev
, 0),
383 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
|
386 &status
, 1, LINE6_TIMEOUT
* HZ
);
389 dev_err(line6
->ifcdev
,
390 "receiving status failed (error %d)\n", ret
);
393 } while (status
== 0xff);
396 dev_err(line6
->ifcdev
, "write failed (error %d)\n", ret
);
402 EXPORT_SYMBOL_GPL(line6_write_data
);
405 Read Line 6 device serial number.
406 (POD, TonePort, GuitarPort)
408 int line6_read_serial_number(struct usb_line6
*line6
, int *serial_number
)
410 return line6_read_data(line6
, 0x80d0, serial_number
,
411 sizeof(*serial_number
));
413 EXPORT_SYMBOL_GPL(line6_read_serial_number
);
418 static void line6_destruct(struct snd_card
*card
)
420 struct usb_line6
*line6
= card
->private_data
;
421 struct usb_device
*usbdev
;
425 usbdev
= line6
->usbdev
;
427 /* free buffer memory first: */
428 kfree(line6
->buffer_message
);
429 kfree(line6
->buffer_listen
);
431 /* then free URBs: */
432 usb_free_urb(line6
->urb_listen
);
434 /* free interface data: */
437 /* decrement reference counters: */
441 /* get data from endpoint descriptor (see usb_maxpacket): */
442 static void line6_get_interval(struct usb_line6
*line6
)
444 struct usb_device
*usbdev
= line6
->usbdev
;
445 struct usb_host_endpoint
*ep
;
446 unsigned pipe
= usb_rcvintpipe(usbdev
, line6
->properties
->ep_ctrl_r
);
447 unsigned epnum
= usb_pipeendpoint(pipe
);
449 ep
= usbdev
->ep_in
[epnum
];
451 line6
->interval
= ep
->desc
.bInterval
;
452 line6
->max_packet_size
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
454 dev_err(line6
->ifcdev
,
455 "endpoint not available, using fallback values");
456 line6
->interval
= LINE6_FALLBACK_INTERVAL
;
457 line6
->max_packet_size
= LINE6_FALLBACK_MAXPACKETSIZE
;
461 static int line6_init_cap_control(struct usb_line6
*line6
)
465 /* initialize USB buffers: */
466 line6
->buffer_listen
= kmalloc(LINE6_BUFSIZE_LISTEN
, GFP_KERNEL
);
467 if (!line6
->buffer_listen
)
470 line6
->buffer_message
= kmalloc(LINE6_MESSAGE_MAXLEN
, GFP_KERNEL
);
471 if (!line6
->buffer_message
)
474 line6
->urb_listen
= usb_alloc_urb(0, GFP_KERNEL
);
475 if (!line6
->urb_listen
)
478 ret
= line6_start_listen(line6
);
480 dev_err(line6
->ifcdev
, "cannot start listening: %d\n", ret
);
490 int line6_probe(struct usb_interface
*interface
,
491 const struct usb_device_id
*id
,
492 struct usb_line6
*line6
,
493 const struct line6_properties
*properties
,
494 int (*private_init
)(struct usb_line6
*, const struct usb_device_id
*id
))
496 struct usb_device
*usbdev
= interface_to_usbdev(interface
);
497 struct snd_card
*card
;
498 int interface_number
;
501 ret
= snd_card_new(&interface
->dev
,
502 SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
503 THIS_MODULE
, 0, &card
);
509 /* store basic data: */
511 line6
->properties
= properties
;
512 line6
->usbdev
= usbdev
;
513 line6
->ifcdev
= &interface
->dev
;
515 strcpy(card
->id
, line6
->properties
->id
);
516 strcpy(card
->driver
, DRIVER_NAME
);
517 strcpy(card
->shortname
, line6
->properties
->name
);
518 sprintf(card
->longname
, "Line 6 %s at USB %s", line6
->properties
->name
,
519 dev_name(line6
->ifcdev
));
520 card
->private_data
= line6
;
521 card
->private_free
= line6_destruct
;
523 usb_set_intfdata(interface
, line6
);
525 /* increment reference counters: */
528 /* we don't handle multiple configurations */
529 if (usbdev
->descriptor
.bNumConfigurations
!= 1) {
534 /* initialize device info: */
535 dev_info(&interface
->dev
, "Line 6 %s found\n", properties
->name
);
537 /* query interface number */
538 interface_number
= interface
->cur_altsetting
->desc
.bInterfaceNumber
;
540 ret
= usb_set_interface(usbdev
, interface_number
,
541 properties
->altsetting
);
543 dev_err(&interface
->dev
, "set_interface failed\n");
547 line6_get_interval(line6
);
549 if (properties
->capabilities
& LINE6_CAP_CONTROL
) {
550 ret
= line6_init_cap_control(line6
);
555 /* initialize device data based on device: */
556 ret
= private_init(line6
, id
);
560 /* creation of additional special files should go here */
562 dev_info(&interface
->dev
, "Line 6 %s now attached\n",
563 line6
->properties
->name
);
568 if (line6
->disconnect
)
569 line6
->disconnect(line6
);
573 EXPORT_SYMBOL_GPL(line6_probe
);
576 Line 6 device disconnected.
578 void line6_disconnect(struct usb_interface
*interface
)
580 struct usb_line6
*line6
= usb_get_intfdata(interface
);
581 struct usb_device
*usbdev
= interface_to_usbdev(interface
);
586 if (WARN_ON(usbdev
!= line6
->usbdev
))
589 if (line6
->urb_listen
!= NULL
)
590 line6_stop_listen(line6
);
592 snd_card_disconnect(line6
->card
);
594 line6_pcm_disconnect(line6
->line6pcm
);
595 if (line6
->disconnect
)
596 line6
->disconnect(line6
);
598 dev_info(&interface
->dev
, "Line 6 %s now disconnected\n",
599 line6
->properties
->name
);
601 /* make sure the device isn't destructed twice: */
602 usb_set_intfdata(interface
, NULL
);
604 snd_card_free_when_closed(line6
->card
);
606 EXPORT_SYMBOL_GPL(line6_disconnect
);
611 Suspend Line 6 device.
613 int line6_suspend(struct usb_interface
*interface
, pm_message_t message
)
615 struct usb_line6
*line6
= usb_get_intfdata(interface
);
616 struct snd_line6_pcm
*line6pcm
= line6
->line6pcm
;
618 snd_power_change_state(line6
->card
, SNDRV_CTL_POWER_D3hot
);
620 if (line6
->properties
->capabilities
& LINE6_CAP_CONTROL
)
621 line6_stop_listen(line6
);
623 if (line6pcm
!= NULL
) {
624 snd_pcm_suspend_all(line6pcm
->pcm
);
630 EXPORT_SYMBOL_GPL(line6_suspend
);
633 Resume Line 6 device.
635 int line6_resume(struct usb_interface
*interface
)
637 struct usb_line6
*line6
= usb_get_intfdata(interface
);
639 if (line6
->properties
->capabilities
& LINE6_CAP_CONTROL
)
640 line6_start_listen(line6
);
642 snd_power_change_state(line6
->card
, SNDRV_CTL_POWER_D0
);
645 EXPORT_SYMBOL_GPL(line6_resume
);
647 #endif /* CONFIG_PM */
649 MODULE_AUTHOR(DRIVER_AUTHOR
);
650 MODULE_DESCRIPTION(DRIVER_DESC
);
651 MODULE_LICENSE("GPL");