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"
30 #define DRIVER_VERSION "0.9.1beta" DRIVER_REVISION
33 This is Line6's MIDI manufacturer ID.
35 const unsigned char line6_midi_id[] = {
38 EXPORT_SYMBOL_GPL(line6_midi_id);
41 Code to request version of POD, Variax interface
42 (and maybe other devices).
44 static const char line6_request_version[] = {
45 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
49 Class for asynchronous messages.
52 struct usb_line6 *line6;
61 static void line6_data_received(struct urb *urb);
62 static int line6_send_raw_message_async_part(struct message *msg,
66 Start to listen on endpoint.
68 static int line6_start_listen(struct usb_line6 *line6)
72 usb_fill_int_urb(line6->urb_listen, line6->usbdev,
73 usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
74 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
75 line6_data_received, line6, line6->interval);
76 line6->urb_listen->actual_length = 0;
77 err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
82 Stop listening on endpoint.
84 static void line6_stop_listen(struct usb_line6 *line6)
86 usb_kill_urb(line6->urb_listen);
90 Send raw message in pieces of wMaxPacketSize bytes.
92 int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
97 for (i = 0; i < size; i += line6->max_packet_size) {
99 const char *frag_buf = buffer + i;
100 int frag_size = min(line6->max_packet_size, size - i);
103 retval = usb_interrupt_msg(line6->usbdev,
104 usb_sndintpipe(line6->usbdev,
105 line6->properties->ep_ctrl_w),
106 (char *)frag_buf, frag_size,
107 &partial, LINE6_TIMEOUT * HZ);
110 dev_err(line6->ifcdev,
111 "usb_interrupt_msg failed (%d)\n", retval);
122 Notification of completion of asynchronous request transmission.
124 static void line6_async_request_sent(struct urb *urb)
126 struct message *msg = (struct message *)urb->context;
128 if (msg->done >= msg->size) {
132 line6_send_raw_message_async_part(msg, urb);
136 Asynchronously send part of a raw message.
138 static int line6_send_raw_message_async_part(struct message *msg,
142 struct usb_line6 *line6 = msg->line6;
143 int done = msg->done;
144 int bytes = min(msg->size - done, line6->max_packet_size);
146 usb_fill_int_urb(urb, line6->usbdev,
147 usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
148 (char *)msg->buffer + done, bytes,
149 line6_async_request_sent, msg, line6->interval);
152 retval = usb_submit_urb(urb, GFP_ATOMIC);
155 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
166 Setup and start timer.
168 void line6_start_timer(struct timer_list *timer, unsigned int msecs,
169 void (*function)(unsigned long), unsigned long data)
171 setup_timer(timer, function, data);
172 mod_timer(timer, jiffies + msecs * HZ / 1000);
174 EXPORT_SYMBOL_GPL(line6_start_timer);
177 Asynchronously send raw message.
179 int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
185 /* create message: */
186 msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
191 urb = usb_alloc_urb(0, GFP_ATOMIC);
198 /* set message data: */
200 msg->buffer = buffer;
205 return line6_send_raw_message_async_part(msg, urb);
207 EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
210 Send asynchronous device version request.
212 int line6_version_request_async(struct usb_line6 *line6)
217 buffer = kmemdup(line6_request_version,
218 sizeof(line6_request_version), GFP_ATOMIC);
222 retval = line6_send_raw_message_async(line6, buffer,
223 sizeof(line6_request_version));
227 EXPORT_SYMBOL_GPL(line6_version_request_async);
230 Send sysex message in pieces of wMaxPacketSize bytes.
232 int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
235 return line6_send_raw_message(line6, buffer,
236 size + SYSEX_EXTRA_SIZE) -
239 EXPORT_SYMBOL_GPL(line6_send_sysex_message);
242 Allocate buffer for sysex message and prepare header.
243 @param code sysex message code
244 @param size number of bytes between code and sysex end
246 char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
249 char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
254 buffer[0] = LINE6_SYSEX_BEGIN;
255 memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
256 buffer[sizeof(line6_midi_id) + 1] = code1;
257 buffer[sizeof(line6_midi_id) + 2] = code2;
258 buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
261 EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
264 Notification of data received from the Line6 device.
266 static void line6_data_received(struct urb *urb)
268 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
269 struct midi_buffer *mb = &line6->line6midi->midibuf_in;
272 if (urb->status == -ESHUTDOWN)
276 line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
278 if (done < urb->actual_length) {
279 line6_midibuf_ignore(mb, done);
280 dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
281 done, urb->actual_length);
286 line6_midibuf_read(mb, line6->buffer_message,
287 LINE6_MESSAGE_MAXLEN);
292 line6->message_length = done;
293 line6_midi_receive(line6, line6->buffer_message, done);
295 if (line6->process_message)
296 line6->process_message(line6);
299 line6_start_listen(line6);
303 Send channel number (i.e., switch to a different sound).
305 int line6_send_program(struct usb_line6 *line6, u8 value)
308 unsigned char *buffer;
311 buffer = kmalloc(2, GFP_KERNEL);
315 buffer[0] = LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST;
318 retval = usb_interrupt_msg(line6->usbdev,
319 usb_sndintpipe(line6->usbdev,
320 line6->properties->ep_ctrl_w),
321 buffer, 2, &partial, LINE6_TIMEOUT * HZ);
324 dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
332 Transmit Line6 control parameter.
334 int line6_transmit_parameter(struct usb_line6 *line6, int param, u8 value)
337 unsigned char *buffer;
340 buffer = kmalloc(3, GFP_KERNEL);
344 buffer[0] = LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST;
348 retval = usb_interrupt_msg(line6->usbdev,
349 usb_sndintpipe(line6->usbdev,
350 line6->properties->ep_ctrl_w),
351 buffer, 3, &partial, LINE6_TIMEOUT * HZ);
354 dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
362 Read data from device.
364 int line6_read_data(struct usb_line6 *line6, int address, void *data,
367 struct usb_device *usbdev = line6->usbdev;
371 /* query the serial number: */
372 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
373 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
374 (datalen << 8) | 0x21, address,
375 NULL, 0, LINE6_TIMEOUT * HZ);
378 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
382 /* Wait for data length. We'll get 0xff until length arrives. */
384 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
385 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
387 0x0012, 0x0000, &len, 1,
390 dev_err(line6->ifcdev,
391 "receive length failed (error %d)\n", ret);
394 } while (len == 0xff);
396 if (len != datalen) {
397 /* should be equal or something went wrong */
398 dev_err(line6->ifcdev,
399 "length mismatch (expected %d, got %d)\n",
400 (int)datalen, (int)len);
404 /* receive the result: */
405 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
406 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
407 0x0013, 0x0000, data, datalen,
411 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
417 EXPORT_SYMBOL_GPL(line6_read_data);
420 Write data to device.
422 int line6_write_data(struct usb_line6 *line6, int address, void *data,
425 struct usb_device *usbdev = line6->usbdev;
427 unsigned char status;
429 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
430 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
431 0x0022, address, data, datalen,
435 dev_err(line6->ifcdev,
436 "write request failed (error %d)\n", ret);
441 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
443 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
446 &status, 1, LINE6_TIMEOUT * HZ);
449 dev_err(line6->ifcdev,
450 "receiving status failed (error %d)\n", ret);
453 } while (status == 0xff);
456 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
462 EXPORT_SYMBOL_GPL(line6_write_data);
465 Read Line6 device serial number.
466 (POD, TonePort, GuitarPort)
468 int line6_read_serial_number(struct usb_line6 *line6, int *serial_number)
470 return line6_read_data(line6, 0x80d0, serial_number,
471 sizeof(*serial_number));
473 EXPORT_SYMBOL_GPL(line6_read_serial_number);
476 No operation (i.e., unsupported).
478 ssize_t line6_nop_read(struct device *dev, struct device_attribute *attr,
483 EXPORT_SYMBOL_GPL(line6_nop_read);
488 static void line6_destruct(struct snd_card *card)
490 struct usb_line6 *line6 = card->private_data;
491 struct usb_device *usbdev;
495 usbdev = line6->usbdev;
497 /* free buffer memory first: */
498 kfree(line6->buffer_message);
499 kfree(line6->buffer_listen);
501 /* then free URBs: */
502 usb_free_urb(line6->urb_listen);
504 /* free interface data: */
507 /* decrement reference counters: */
514 int line6_probe(struct usb_interface *interface,
515 struct usb_line6 *line6,
516 const struct line6_properties *properties,
517 int (*private_init)(struct usb_interface *, struct usb_line6 *))
519 struct usb_device *usbdev;
520 struct snd_card *card;
521 int interface_number;
528 usbdev = interface_to_usbdev(interface);
534 /* we don't handle multiple configurations */
535 if (usbdev->descriptor.bNumConfigurations != 1) {
540 /* initialize device info: */
541 dev_info(&interface->dev, "Line6 %s found\n", properties->name);
543 /* query interface number */
544 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
546 ret = usb_set_interface(usbdev, interface_number,
547 properties->altsetting);
549 dev_err(&interface->dev, "set_interface failed\n");
553 /* store basic data: */
554 line6->properties = properties;
555 line6->usbdev = usbdev;
556 line6->ifcdev = &interface->dev;
558 /* get data from endpoint descriptor (see usb_maxpacket): */
560 struct usb_host_endpoint *ep;
561 unsigned pipe = usb_rcvintpipe(usbdev, properties->ep_ctrl_r);
562 unsigned epnum = usb_pipeendpoint(pipe);
563 ep = usbdev->ep_in[epnum];
566 line6->interval = ep->desc.bInterval;
567 line6->max_packet_size =
568 le16_to_cpu(ep->desc.wMaxPacketSize);
570 line6->interval = LINE6_FALLBACK_INTERVAL;
571 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
572 dev_err(line6->ifcdev,
573 "endpoint not available, using fallback values");
577 ret = snd_card_new(line6->ifcdev,
578 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
579 THIS_MODULE, 0, &card);
584 strcpy(card->id, line6->properties->id);
585 strcpy(card->driver, DRIVER_NAME);
586 strcpy(card->shortname, line6->properties->name);
587 sprintf(card->longname, "Line6 %s at USB %s", line6->properties->name,
588 dev_name(line6->ifcdev));
589 card->private_data = line6;
590 card->private_free = line6_destruct;
592 usb_set_intfdata(interface, line6);
594 /* increment reference counters: */
597 if (properties->capabilities & LINE6_CAP_CONTROL) {
598 /* initialize USB buffers: */
599 line6->buffer_listen =
600 kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
601 if (line6->buffer_listen == NULL) {
606 line6->buffer_message =
607 kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
608 if (line6->buffer_message == NULL) {
613 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
615 if (line6->urb_listen == NULL) {
620 ret = line6_start_listen(line6);
622 dev_err(&interface->dev, "%s: usb_submit_urb failed\n",
628 /* initialize device data based on device: */
629 ret = private_init(interface, line6);
633 /* creation of additional special files should go here */
635 dev_info(&interface->dev, "Line6 %s now attached\n",
636 line6->properties->name);
645 EXPORT_SYMBOL_GPL(line6_probe);
648 Line6 device disconnected.
650 void line6_disconnect(struct usb_interface *interface)
652 struct usb_line6 *line6;
653 struct usb_device *usbdev;
654 int interface_number;
656 if (interface == NULL)
658 usbdev = interface_to_usbdev(interface);
662 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
663 line6 = usb_get_intfdata(interface);
667 if (line6->urb_listen != NULL)
668 line6_stop_listen(line6);
670 if (usbdev != line6->usbdev)
671 dev_err(line6->ifcdev, "driver bug: inconsistent usb device\n");
673 snd_card_disconnect(line6->card);
674 if (line6->disconnect)
675 line6->disconnect(interface);
677 dev_info(&interface->dev, "Line6 %s now disconnected\n",
678 line6->properties->name);
680 /* make sure the device isn't destructed twice: */
681 usb_set_intfdata(interface, NULL);
683 snd_card_free_when_closed(line6->card);
685 EXPORT_SYMBOL_GPL(line6_disconnect);
690 Suspend Line6 device.
692 int line6_suspend(struct usb_interface *interface, pm_message_t message)
694 struct usb_line6 *line6 = usb_get_intfdata(interface);
695 struct snd_line6_pcm *line6pcm = line6->line6pcm;
697 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
699 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
700 line6_stop_listen(line6);
702 if (line6pcm != NULL) {
703 snd_pcm_suspend_all(line6pcm->pcm);
704 line6_pcm_disconnect(line6pcm);
710 EXPORT_SYMBOL_GPL(line6_suspend);
715 int line6_resume(struct usb_interface *interface)
717 struct usb_line6 *line6 = usb_get_intfdata(interface);
719 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
720 line6_start_listen(line6);
722 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
725 EXPORT_SYMBOL_GPL(line6_resume);
727 #endif /* CONFIG_PM */
729 MODULE_AUTHOR(DRIVER_AUTHOR);
730 MODULE_DESCRIPTION(DRIVER_DESC);
731 MODULE_LICENSE("GPL");
732 MODULE_VERSION(DRIVER_VERSION);