2 * Line6 Linux USB driver - 0.9.1beta
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 "Line6 USB Driver"
32 This is Line6'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 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 Line6 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 Send channel number (i.e., switch to a different sound).
304 int line6_send_program(struct usb_line6 *line6, u8 value)
307 unsigned char *buffer;
310 buffer = kmalloc(2, GFP_KERNEL);
314 buffer[0] = LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST;
317 retval = usb_interrupt_msg(line6->usbdev,
318 usb_sndintpipe(line6->usbdev,
319 line6->properties->ep_ctrl_w),
320 buffer, 2, &partial, LINE6_TIMEOUT * HZ);
323 dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
331 Transmit Line6 control parameter.
333 int line6_transmit_parameter(struct usb_line6 *line6, int param, u8 value)
336 unsigned char *buffer;
339 buffer = kmalloc(3, GFP_KERNEL);
343 buffer[0] = LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST;
347 retval = usb_interrupt_msg(line6->usbdev,
348 usb_sndintpipe(line6->usbdev,
349 line6->properties->ep_ctrl_w),
350 buffer, 3, &partial, LINE6_TIMEOUT * HZ);
353 dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
361 Read data from device.
363 int line6_read_data(struct usb_line6 *line6, int address, void *data,
366 struct usb_device *usbdev = line6->usbdev;
370 /* query the serial number: */
371 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
372 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
373 (datalen << 8) | 0x21, address,
374 NULL, 0, LINE6_TIMEOUT * HZ);
377 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
381 /* Wait for data length. We'll get 0xff until length arrives. */
383 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
384 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
386 0x0012, 0x0000, &len, 1,
389 dev_err(line6->ifcdev,
390 "receive length failed (error %d)\n", ret);
393 } while (len == 0xff);
395 if (len != datalen) {
396 /* should be equal or something went wrong */
397 dev_err(line6->ifcdev,
398 "length mismatch (expected %d, got %d)\n",
399 (int)datalen, (int)len);
403 /* receive the result: */
404 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
405 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
406 0x0013, 0x0000, data, datalen,
410 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
416 EXPORT_SYMBOL_GPL(line6_read_data);
419 Write data to device.
421 int line6_write_data(struct usb_line6 *line6, int address, void *data,
424 struct usb_device *usbdev = line6->usbdev;
426 unsigned char status;
428 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
429 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
430 0x0022, address, data, datalen,
434 dev_err(line6->ifcdev,
435 "write request failed (error %d)\n", ret);
440 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
442 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
445 &status, 1, LINE6_TIMEOUT * HZ);
448 dev_err(line6->ifcdev,
449 "receiving status failed (error %d)\n", ret);
452 } while (status == 0xff);
455 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
461 EXPORT_SYMBOL_GPL(line6_write_data);
464 Read Line6 device serial number.
465 (POD, TonePort, GuitarPort)
467 int line6_read_serial_number(struct usb_line6 *line6, int *serial_number)
469 return line6_read_data(line6, 0x80d0, serial_number,
470 sizeof(*serial_number));
472 EXPORT_SYMBOL_GPL(line6_read_serial_number);
475 No operation (i.e., unsupported).
477 ssize_t line6_nop_read(struct device *dev, struct device_attribute *attr,
482 EXPORT_SYMBOL_GPL(line6_nop_read);
487 static void line6_destruct(struct snd_card *card)
489 struct usb_line6 *line6 = card->private_data;
490 struct usb_device *usbdev;
494 usbdev = line6->usbdev;
496 /* free buffer memory first: */
497 kfree(line6->buffer_message);
498 kfree(line6->buffer_listen);
500 /* then free URBs: */
501 usb_free_urb(line6->urb_listen);
503 /* free interface data: */
506 /* decrement reference counters: */
513 int line6_probe(struct usb_interface *interface,
514 struct usb_line6 *line6,
515 const struct line6_properties *properties,
516 int (*private_init)(struct usb_interface *, struct usb_line6 *))
518 struct usb_device *usbdev;
519 struct snd_card *card;
520 int interface_number;
527 usbdev = interface_to_usbdev(interface);
533 /* we don't handle multiple configurations */
534 if (usbdev->descriptor.bNumConfigurations != 1) {
539 /* initialize device info: */
540 dev_info(&interface->dev, "Line6 %s found\n", properties->name);
542 /* query interface number */
543 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
545 ret = usb_set_interface(usbdev, interface_number,
546 properties->altsetting);
548 dev_err(&interface->dev, "set_interface failed\n");
552 /* store basic data: */
553 line6->properties = properties;
554 line6->usbdev = usbdev;
555 line6->ifcdev = &interface->dev;
557 /* get data from endpoint descriptor (see usb_maxpacket): */
559 struct usb_host_endpoint *ep;
560 unsigned pipe = usb_rcvintpipe(usbdev, properties->ep_ctrl_r);
561 unsigned epnum = usb_pipeendpoint(pipe);
562 ep = usbdev->ep_in[epnum];
565 line6->interval = ep->desc.bInterval;
566 line6->max_packet_size =
567 le16_to_cpu(ep->desc.wMaxPacketSize);
569 line6->interval = LINE6_FALLBACK_INTERVAL;
570 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
571 dev_err(line6->ifcdev,
572 "endpoint not available, using fallback values");
576 ret = snd_card_new(line6->ifcdev,
577 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
578 THIS_MODULE, 0, &card);
583 strcpy(card->id, line6->properties->id);
584 strcpy(card->driver, DRIVER_NAME);
585 strcpy(card->shortname, line6->properties->name);
586 sprintf(card->longname, "Line6 %s at USB %s", line6->properties->name,
587 dev_name(line6->ifcdev));
588 card->private_data = line6;
589 card->private_free = line6_destruct;
591 usb_set_intfdata(interface, line6);
593 /* increment reference counters: */
596 if (properties->capabilities & LINE6_CAP_CONTROL) {
597 /* initialize USB buffers: */
598 line6->buffer_listen =
599 kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
600 if (line6->buffer_listen == NULL) {
605 line6->buffer_message =
606 kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
607 if (line6->buffer_message == NULL) {
612 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
614 if (line6->urb_listen == NULL) {
619 ret = line6_start_listen(line6);
621 dev_err(&interface->dev, "%s: usb_submit_urb failed\n",
627 /* initialize device data based on device: */
628 ret = private_init(interface, line6);
632 /* creation of additional special files should go here */
634 dev_info(&interface->dev, "Line6 %s now attached\n",
635 line6->properties->name);
644 EXPORT_SYMBOL_GPL(line6_probe);
647 Line6 device disconnected.
649 void line6_disconnect(struct usb_interface *interface)
651 struct usb_line6 *line6;
652 struct usb_device *usbdev;
653 int interface_number;
655 if (interface == NULL)
657 usbdev = interface_to_usbdev(interface);
661 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
662 line6 = usb_get_intfdata(interface);
666 if (line6->urb_listen != NULL)
667 line6_stop_listen(line6);
669 if (usbdev != line6->usbdev)
670 dev_err(line6->ifcdev, "driver bug: inconsistent usb device\n");
672 snd_card_disconnect(line6->card);
673 if (line6->disconnect)
674 line6->disconnect(interface);
676 dev_info(&interface->dev, "Line6 %s now disconnected\n",
677 line6->properties->name);
679 /* make sure the device isn't destructed twice: */
680 usb_set_intfdata(interface, NULL);
682 snd_card_free_when_closed(line6->card);
684 EXPORT_SYMBOL_GPL(line6_disconnect);
689 Suspend Line6 device.
691 int line6_suspend(struct usb_interface *interface, pm_message_t message)
693 struct usb_line6 *line6 = usb_get_intfdata(interface);
694 struct snd_line6_pcm *line6pcm = line6->line6pcm;
696 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
698 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
699 line6_stop_listen(line6);
701 if (line6pcm != NULL) {
702 snd_pcm_suspend_all(line6pcm->pcm);
703 line6_pcm_disconnect(line6pcm);
709 EXPORT_SYMBOL_GPL(line6_suspend);
714 int line6_resume(struct usb_interface *interface)
716 struct usb_line6 *line6 = usb_get_intfdata(interface);
718 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
719 line6_start_listen(line6);
721 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
724 EXPORT_SYMBOL_GPL(line6_resume);
726 #endif /* CONFIG_PM */
728 MODULE_AUTHOR(DRIVER_AUTHOR);
729 MODULE_DESCRIPTION(DRIVER_DESC);
730 MODULE_LICENSE("GPL");