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>
26 #define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
27 #define DRIVER_DESC "Line 6 USB Driver"
30 This is Line 6's MIDI manufacturer ID.
32 const unsigned char line6_midi_id[3] = {
35 EXPORT_SYMBOL_GPL(line6_midi_id);
38 Code to request version of POD, Variax interface
39 (and maybe other devices).
41 static const char line6_request_version[] = {
42 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
46 Class for asynchronous messages.
49 struct usb_line6 *line6;
58 static void line6_data_received(struct urb *urb);
59 static int line6_send_raw_message_async_part(struct message *msg,
63 Start to listen on endpoint.
65 static int line6_start_listen(struct usb_line6 *line6)
69 usb_fill_int_urb(line6->urb_listen, line6->usbdev,
70 usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
71 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
72 line6_data_received, line6, line6->interval);
73 line6->urb_listen->actual_length = 0;
74 err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
79 Stop listening on endpoint.
81 static void line6_stop_listen(struct usb_line6 *line6)
83 usb_kill_urb(line6->urb_listen);
87 Send raw message in pieces of wMaxPacketSize bytes.
89 static int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
94 for (i = 0; i < size; i += line6->max_packet_size) {
96 const char *frag_buf = buffer + i;
97 int frag_size = min(line6->max_packet_size, size - i);
100 retval = usb_interrupt_msg(line6->usbdev,
101 usb_sndintpipe(line6->usbdev,
102 line6->properties->ep_ctrl_w),
103 (char *)frag_buf, frag_size,
104 &partial, LINE6_TIMEOUT * HZ);
107 dev_err(line6->ifcdev,
108 "usb_interrupt_msg failed (%d)\n", retval);
119 Notification of completion of asynchronous request transmission.
121 static void line6_async_request_sent(struct urb *urb)
123 struct message *msg = (struct message *)urb->context;
125 if (msg->done >= msg->size) {
129 line6_send_raw_message_async_part(msg, urb);
133 Asynchronously send part of a raw message.
135 static int line6_send_raw_message_async_part(struct message *msg,
139 struct usb_line6 *line6 = msg->line6;
140 int done = msg->done;
141 int bytes = min(msg->size - done, line6->max_packet_size);
143 usb_fill_int_urb(urb, line6->usbdev,
144 usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
145 (char *)msg->buffer + done, bytes,
146 line6_async_request_sent, msg, line6->interval);
149 retval = usb_submit_urb(urb, GFP_ATOMIC);
152 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
163 Setup and start timer.
165 void line6_start_timer(struct timer_list *timer, unsigned long msecs,
166 void (*function)(unsigned long), unsigned long data)
168 setup_timer(timer, function, data);
169 mod_timer(timer, jiffies + msecs_to_jiffies(msecs));
171 EXPORT_SYMBOL_GPL(line6_start_timer);
174 Asynchronously send raw message.
176 int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
182 /* create message: */
183 msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
188 urb = usb_alloc_urb(0, GFP_ATOMIC);
195 /* set message data: */
197 msg->buffer = buffer;
202 return line6_send_raw_message_async_part(msg, urb);
204 EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
207 Send asynchronous device version request.
209 int line6_version_request_async(struct usb_line6 *line6)
214 buffer = kmemdup(line6_request_version,
215 sizeof(line6_request_version), GFP_ATOMIC);
219 retval = line6_send_raw_message_async(line6, buffer,
220 sizeof(line6_request_version));
224 EXPORT_SYMBOL_GPL(line6_version_request_async);
227 Send sysex message in pieces of wMaxPacketSize bytes.
229 int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
232 return line6_send_raw_message(line6, buffer,
233 size + SYSEX_EXTRA_SIZE) -
236 EXPORT_SYMBOL_GPL(line6_send_sysex_message);
239 Allocate buffer for sysex message and prepare header.
240 @param code sysex message code
241 @param size number of bytes between code and sysex end
243 char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
246 char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
251 buffer[0] = LINE6_SYSEX_BEGIN;
252 memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
253 buffer[sizeof(line6_midi_id) + 1] = code1;
254 buffer[sizeof(line6_midi_id) + 2] = code2;
255 buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
258 EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
261 Notification of data received from the Line 6 device.
263 static void line6_data_received(struct urb *urb)
265 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
266 struct midi_buffer *mb = &line6->line6midi->midibuf_in;
269 if (urb->status == -ESHUTDOWN)
273 line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
275 if (done < urb->actual_length) {
276 line6_midibuf_ignore(mb, done);
277 dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
278 done, urb->actual_length);
283 line6_midibuf_read(mb, line6->buffer_message,
284 LINE6_MESSAGE_MAXLEN);
289 line6->message_length = done;
290 line6_midi_receive(line6, line6->buffer_message, done);
292 if (line6->process_message)
293 line6->process_message(line6);
296 line6_start_listen(line6);
299 #define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */
300 #define LINE6_READ_WRITE_MAX_RETRIES 50
303 Read data from device.
305 int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
308 struct usb_device *usbdev = line6->usbdev;
313 if (address > 0xffff || datalen > 0xff)
316 /* query the serial number: */
317 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
318 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
319 (datalen << 8) | 0x21, address,
320 NULL, 0, LINE6_TIMEOUT * HZ);
323 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
327 /* Wait for data length. We'll get 0xff until length arrives. */
328 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
329 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
331 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
332 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
334 0x0012, 0x0000, &len, 1,
337 dev_err(line6->ifcdev,
338 "receive length failed (error %d)\n", ret);
347 dev_err(line6->ifcdev, "read failed after %d retries\n",
350 } else if (len != datalen) {
351 /* should be equal or something went wrong */
352 dev_err(line6->ifcdev,
353 "length mismatch (expected %d, got %d)\n",
354 (int)datalen, (int)len);
358 /* receive the result: */
359 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
360 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
361 0x0013, 0x0000, data, datalen,
365 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
371 EXPORT_SYMBOL_GPL(line6_read_data);
374 Write data to device.
376 int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
379 struct usb_device *usbdev = line6->usbdev;
381 unsigned char status;
384 if (address > 0xffff || datalen > 0xffff)
387 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
388 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
389 0x0022, address, data, datalen,
393 dev_err(line6->ifcdev,
394 "write request failed (error %d)\n", ret);
398 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
399 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
401 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
403 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
406 &status, 1, LINE6_TIMEOUT * HZ);
409 dev_err(line6->ifcdev,
410 "receiving status failed (error %d)\n", ret);
418 if (status == 0xff) {
419 dev_err(line6->ifcdev, "write failed after %d retries\n",
422 } else if (status != 0) {
423 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
429 EXPORT_SYMBOL_GPL(line6_write_data);
432 Read Line 6 device serial number.
433 (POD, TonePort, GuitarPort)
435 int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
437 return line6_read_data(line6, 0x80d0, serial_number,
438 sizeof(*serial_number));
440 EXPORT_SYMBOL_GPL(line6_read_serial_number);
445 static void line6_destruct(struct snd_card *card)
447 struct usb_line6 *line6 = card->private_data;
448 struct usb_device *usbdev = line6->usbdev;
450 /* free buffer memory first: */
451 kfree(line6->buffer_message);
452 kfree(line6->buffer_listen);
454 /* then free URBs: */
455 usb_free_urb(line6->urb_listen);
457 /* decrement reference counters: */
461 /* get data from endpoint descriptor (see usb_maxpacket): */
462 static void line6_get_interval(struct usb_line6 *line6)
464 struct usb_device *usbdev = line6->usbdev;
465 struct usb_host_endpoint *ep;
466 unsigned pipe = usb_rcvintpipe(usbdev, line6->properties->ep_ctrl_r);
467 unsigned epnum = usb_pipeendpoint(pipe);
469 ep = usbdev->ep_in[epnum];
471 line6->interval = ep->desc.bInterval;
472 line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
474 dev_err(line6->ifcdev,
475 "endpoint not available, using fallback values");
476 line6->interval = LINE6_FALLBACK_INTERVAL;
477 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
481 static int line6_init_cap_control(struct usb_line6 *line6)
485 /* initialize USB buffers: */
486 line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
487 if (!line6->buffer_listen)
490 line6->buffer_message = kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
491 if (!line6->buffer_message)
494 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
495 if (!line6->urb_listen)
498 ret = line6_start_listen(line6);
500 dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
510 int line6_probe(struct usb_interface *interface,
511 const struct usb_device_id *id,
512 const char *driver_name,
513 const struct line6_properties *properties,
514 int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
517 struct usb_device *usbdev = interface_to_usbdev(interface);
518 struct snd_card *card;
519 struct usb_line6 *line6;
520 int interface_number;
523 if (WARN_ON(data_size < sizeof(*line6)))
526 /* we don't handle multiple configurations */
527 if (usbdev->descriptor.bNumConfigurations != 1)
530 ret = snd_card_new(&interface->dev,
531 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
532 THIS_MODULE, data_size, &card);
536 /* store basic data: */
537 line6 = card->private_data;
539 line6->properties = properties;
540 line6->usbdev = usbdev;
541 line6->ifcdev = &interface->dev;
543 strcpy(card->id, properties->id);
544 strcpy(card->driver, driver_name);
545 strcpy(card->shortname, properties->name);
546 sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
547 dev_name(line6->ifcdev));
548 card->private_free = line6_destruct;
550 usb_set_intfdata(interface, line6);
552 /* increment reference counters: */
555 /* initialize device info: */
556 dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
558 /* query interface number */
559 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
561 ret = usb_set_interface(usbdev, interface_number,
562 properties->altsetting);
564 dev_err(&interface->dev, "set_interface failed\n");
568 line6_get_interval(line6);
570 if (properties->capabilities & LINE6_CAP_CONTROL) {
571 ret = line6_init_cap_control(line6);
576 /* initialize device data based on device: */
577 ret = private_init(line6, id);
581 /* creation of additional special files should go here */
583 dev_info(&interface->dev, "Line 6 %s now attached\n",
589 if (line6->disconnect)
590 line6->disconnect(line6);
594 EXPORT_SYMBOL_GPL(line6_probe);
597 Line 6 device disconnected.
599 void line6_disconnect(struct usb_interface *interface)
601 struct usb_line6 *line6 = usb_get_intfdata(interface);
602 struct usb_device *usbdev = interface_to_usbdev(interface);
607 if (WARN_ON(usbdev != line6->usbdev))
610 if (line6->urb_listen != NULL)
611 line6_stop_listen(line6);
613 snd_card_disconnect(line6->card);
615 line6_pcm_disconnect(line6->line6pcm);
616 if (line6->disconnect)
617 line6->disconnect(line6);
619 dev_info(&interface->dev, "Line 6 %s now disconnected\n",
620 line6->properties->name);
622 /* make sure the device isn't destructed twice: */
623 usb_set_intfdata(interface, NULL);
625 snd_card_free_when_closed(line6->card);
627 EXPORT_SYMBOL_GPL(line6_disconnect);
632 Suspend Line 6 device.
634 int line6_suspend(struct usb_interface *interface, pm_message_t message)
636 struct usb_line6 *line6 = usb_get_intfdata(interface);
637 struct snd_line6_pcm *line6pcm = line6->line6pcm;
639 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
641 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
642 line6_stop_listen(line6);
644 if (line6pcm != NULL) {
645 snd_pcm_suspend_all(line6pcm->pcm);
651 EXPORT_SYMBOL_GPL(line6_suspend);
654 Resume Line 6 device.
656 int line6_resume(struct usb_interface *interface)
658 struct usb_line6 *line6 = usb_get_intfdata(interface);
660 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
661 line6_start_listen(line6);
663 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
666 EXPORT_SYMBOL_GPL(line6_resume);
668 #endif /* CONFIG_PM */
670 MODULE_AUTHOR(DRIVER_AUTHOR);
671 MODULE_DESCRIPTION(DRIVER_DESC);
672 MODULE_LICENSE("GPL");