Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Line 6 Linux USB driver
0004  *
0005  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/export.h>
0011 #include <linux/slab.h>
0012 #include <linux/usb.h>
0013 
0014 #include <sound/core.h>
0015 #include <sound/initval.h>
0016 #include <sound/hwdep.h>
0017 
0018 #include "capture.h"
0019 #include "driver.h"
0020 #include "midi.h"
0021 #include "playback.h"
0022 
0023 #define DRIVER_AUTHOR  "Markus Grabner <grabner@icg.tugraz.at>"
0024 #define DRIVER_DESC    "Line 6 USB Driver"
0025 
0026 /*
0027     This is Line 6's MIDI manufacturer ID.
0028 */
0029 const unsigned char line6_midi_id[3] = {
0030     0x00, 0x01, 0x0c
0031 };
0032 EXPORT_SYMBOL_GPL(line6_midi_id);
0033 
0034 /*
0035     Code to request version of POD, Variax interface
0036     (and maybe other devices).
0037 */
0038 static const char line6_request_version[] = {
0039     0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
0040 };
0041 
0042 /*
0043      Class for asynchronous messages.
0044 */
0045 struct message {
0046     struct usb_line6 *line6;
0047     const char *buffer;
0048     int size;
0049     int done;
0050 };
0051 
0052 /*
0053     Forward declarations.
0054 */
0055 static void line6_data_received(struct urb *urb);
0056 static int line6_send_raw_message_async_part(struct message *msg,
0057                          struct urb *urb);
0058 
0059 /*
0060     Start to listen on endpoint.
0061 */
0062 static int line6_start_listen(struct usb_line6 *line6)
0063 {
0064     int err;
0065 
0066     if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
0067         usb_fill_int_urb(line6->urb_listen, line6->usbdev,
0068             usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
0069             line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
0070             line6_data_received, line6, line6->interval);
0071     } else {
0072         usb_fill_bulk_urb(line6->urb_listen, line6->usbdev,
0073             usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r),
0074             line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
0075             line6_data_received, line6);
0076     }
0077 
0078     /* sanity checks of EP before actually submitting */
0079     if (usb_urb_ep_type_check(line6->urb_listen)) {
0080         dev_err(line6->ifcdev, "invalid control EP\n");
0081         return -EINVAL;
0082     }
0083 
0084     line6->urb_listen->actual_length = 0;
0085     err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
0086     return err;
0087 }
0088 
0089 /*
0090     Stop listening on endpoint.
0091 */
0092 static void line6_stop_listen(struct usb_line6 *line6)
0093 {
0094     usb_kill_urb(line6->urb_listen);
0095 }
0096 
0097 /*
0098     Send raw message in pieces of wMaxPacketSize bytes.
0099 */
0100 int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
0101                   int size)
0102 {
0103     int i, done = 0;
0104     const struct line6_properties *properties = line6->properties;
0105 
0106     for (i = 0; i < size; i += line6->max_packet_size) {
0107         int partial;
0108         const char *frag_buf = buffer + i;
0109         int frag_size = min(line6->max_packet_size, size - i);
0110         int retval;
0111 
0112         if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
0113             retval = usb_interrupt_msg(line6->usbdev,
0114                         usb_sndintpipe(line6->usbdev, properties->ep_ctrl_w),
0115                         (char *)frag_buf, frag_size,
0116                         &partial, LINE6_TIMEOUT);
0117         } else {
0118             retval = usb_bulk_msg(line6->usbdev,
0119                         usb_sndbulkpipe(line6->usbdev, properties->ep_ctrl_w),
0120                         (char *)frag_buf, frag_size,
0121                         &partial, LINE6_TIMEOUT);
0122         }
0123 
0124         if (retval) {
0125             dev_err(line6->ifcdev,
0126                 "usb_bulk_msg failed (%d)\n", retval);
0127             break;
0128         }
0129 
0130         done += frag_size;
0131     }
0132 
0133     return done;
0134 }
0135 EXPORT_SYMBOL_GPL(line6_send_raw_message);
0136 
0137 /*
0138     Notification of completion of asynchronous request transmission.
0139 */
0140 static void line6_async_request_sent(struct urb *urb)
0141 {
0142     struct message *msg = (struct message *)urb->context;
0143 
0144     if (msg->done >= msg->size) {
0145         usb_free_urb(urb);
0146         kfree(msg);
0147     } else
0148         line6_send_raw_message_async_part(msg, urb);
0149 }
0150 
0151 /*
0152     Asynchronously send part of a raw message.
0153 */
0154 static int line6_send_raw_message_async_part(struct message *msg,
0155                          struct urb *urb)
0156 {
0157     int retval;
0158     struct usb_line6 *line6 = msg->line6;
0159     int done = msg->done;
0160     int bytes = min(msg->size - done, line6->max_packet_size);
0161 
0162     if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
0163         usb_fill_int_urb(urb, line6->usbdev,
0164             usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
0165             (char *)msg->buffer + done, bytes,
0166             line6_async_request_sent, msg, line6->interval);
0167     } else {
0168         usb_fill_bulk_urb(urb, line6->usbdev,
0169             usb_sndbulkpipe(line6->usbdev, line6->properties->ep_ctrl_w),
0170             (char *)msg->buffer + done, bytes,
0171             line6_async_request_sent, msg);
0172     }
0173 
0174     msg->done += bytes;
0175 
0176     /* sanity checks of EP before actually submitting */
0177     retval = usb_urb_ep_type_check(urb);
0178     if (retval < 0)
0179         goto error;
0180 
0181     retval = usb_submit_urb(urb, GFP_ATOMIC);
0182     if (retval < 0)
0183         goto error;
0184 
0185     return 0;
0186 
0187  error:
0188     dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
0189         __func__, retval);
0190     usb_free_urb(urb);
0191     kfree(msg);
0192     return retval;
0193 }
0194 
0195 /*
0196     Asynchronously send raw message.
0197 */
0198 int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
0199                  int size)
0200 {
0201     struct message *msg;
0202     struct urb *urb;
0203 
0204     /* create message: */
0205     msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
0206     if (msg == NULL)
0207         return -ENOMEM;
0208 
0209     /* create URB: */
0210     urb = usb_alloc_urb(0, GFP_ATOMIC);
0211 
0212     if (urb == NULL) {
0213         kfree(msg);
0214         return -ENOMEM;
0215     }
0216 
0217     /* set message data: */
0218     msg->line6 = line6;
0219     msg->buffer = buffer;
0220     msg->size = size;
0221     msg->done = 0;
0222 
0223     /* start sending: */
0224     return line6_send_raw_message_async_part(msg, urb);
0225 }
0226 EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
0227 
0228 /*
0229     Send asynchronous device version request.
0230 */
0231 int line6_version_request_async(struct usb_line6 *line6)
0232 {
0233     char *buffer;
0234     int retval;
0235 
0236     buffer = kmemdup(line6_request_version,
0237             sizeof(line6_request_version), GFP_ATOMIC);
0238     if (buffer == NULL)
0239         return -ENOMEM;
0240 
0241     retval = line6_send_raw_message_async(line6, buffer,
0242                           sizeof(line6_request_version));
0243     kfree(buffer);
0244     return retval;
0245 }
0246 EXPORT_SYMBOL_GPL(line6_version_request_async);
0247 
0248 /*
0249     Send sysex message in pieces of wMaxPacketSize bytes.
0250 */
0251 int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
0252                  int size)
0253 {
0254     return line6_send_raw_message(line6, buffer,
0255                       size + SYSEX_EXTRA_SIZE) -
0256         SYSEX_EXTRA_SIZE;
0257 }
0258 EXPORT_SYMBOL_GPL(line6_send_sysex_message);
0259 
0260 /*
0261     Allocate buffer for sysex message and prepare header.
0262     @param code sysex message code
0263     @param size number of bytes between code and sysex end
0264 */
0265 char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
0266                    int size)
0267 {
0268     char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
0269 
0270     if (!buffer)
0271         return NULL;
0272 
0273     buffer[0] = LINE6_SYSEX_BEGIN;
0274     memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
0275     buffer[sizeof(line6_midi_id) + 1] = code1;
0276     buffer[sizeof(line6_midi_id) + 2] = code2;
0277     buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
0278     return buffer;
0279 }
0280 EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
0281 
0282 /*
0283     Notification of data received from the Line 6 device.
0284 */
0285 static void line6_data_received(struct urb *urb)
0286 {
0287     struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
0288     struct midi_buffer *mb = &line6->line6midi->midibuf_in;
0289     int done;
0290 
0291     if (urb->status == -ESHUTDOWN)
0292         return;
0293 
0294     if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
0295         done =
0296             line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
0297 
0298         if (done < urb->actual_length) {
0299             line6_midibuf_ignore(mb, done);
0300             dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
0301                 done, urb->actual_length);
0302         }
0303 
0304         for (;;) {
0305             done =
0306                 line6_midibuf_read(mb, line6->buffer_message,
0307                         LINE6_MIDI_MESSAGE_MAXLEN);
0308 
0309             if (done <= 0)
0310                 break;
0311 
0312             line6->message_length = done;
0313             line6_midi_receive(line6, line6->buffer_message, done);
0314 
0315             if (line6->process_message)
0316                 line6->process_message(line6);
0317         }
0318     } else {
0319         line6->buffer_message = urb->transfer_buffer;
0320         line6->message_length = urb->actual_length;
0321         if (line6->process_message)
0322             line6->process_message(line6);
0323         line6->buffer_message = NULL;
0324     }
0325 
0326     line6_start_listen(line6);
0327 }
0328 
0329 #define LINE6_READ_WRITE_STATUS_DELAY 2  /* milliseconds */
0330 #define LINE6_READ_WRITE_MAX_RETRIES 50
0331 
0332 /*
0333     Read data from device.
0334 */
0335 int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
0336             unsigned datalen)
0337 {
0338     struct usb_device *usbdev = line6->usbdev;
0339     int ret;
0340     u8 len;
0341     unsigned count;
0342 
0343     if (address > 0xffff || datalen > 0xff)
0344         return -EINVAL;
0345 
0346     /* query the serial number: */
0347     ret = usb_control_msg_send(usbdev, 0, 0x67,
0348                    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0349                    (datalen << 8) | 0x21, address, NULL, 0,
0350                    LINE6_TIMEOUT, GFP_KERNEL);
0351     if (ret) {
0352         dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
0353         goto exit;
0354     }
0355 
0356     /* Wait for data length. We'll get 0xff until length arrives. */
0357     for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
0358         mdelay(LINE6_READ_WRITE_STATUS_DELAY);
0359 
0360         ret = usb_control_msg_recv(usbdev, 0, 0x67,
0361                        USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0362                        0x0012, 0x0000, &len, 1,
0363                        LINE6_TIMEOUT, GFP_KERNEL);
0364         if (ret) {
0365             dev_err(line6->ifcdev,
0366                 "receive length failed (error %d)\n", ret);
0367             goto exit;
0368         }
0369 
0370         if (len != 0xff)
0371             break;
0372     }
0373 
0374     ret = -EIO;
0375     if (len == 0xff) {
0376         dev_err(line6->ifcdev, "read failed after %d retries\n",
0377             count);
0378         goto exit;
0379     } else if (len != datalen) {
0380         /* should be equal or something went wrong */
0381         dev_err(line6->ifcdev,
0382             "length mismatch (expected %d, got %d)\n",
0383             (int)datalen, len);
0384         goto exit;
0385     }
0386 
0387     /* receive the result: */
0388     ret = usb_control_msg_recv(usbdev, 0, 0x67,
0389                    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0390                    0x0013, 0x0000, data, datalen, LINE6_TIMEOUT,
0391                    GFP_KERNEL);
0392     if (ret)
0393         dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
0394 
0395 exit:
0396     return ret;
0397 }
0398 EXPORT_SYMBOL_GPL(line6_read_data);
0399 
0400 /*
0401     Write data to device.
0402 */
0403 int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
0404              unsigned datalen)
0405 {
0406     struct usb_device *usbdev = line6->usbdev;
0407     int ret;
0408     unsigned char *status;
0409     int count;
0410 
0411     if (address > 0xffff || datalen > 0xffff)
0412         return -EINVAL;
0413 
0414     status = kmalloc(1, GFP_KERNEL);
0415     if (!status)
0416         return -ENOMEM;
0417 
0418     ret = usb_control_msg_send(usbdev, 0, 0x67,
0419                    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0420                    0x0022, address, data, datalen, LINE6_TIMEOUT,
0421                    GFP_KERNEL);
0422     if (ret) {
0423         dev_err(line6->ifcdev,
0424             "write request failed (error %d)\n", ret);
0425         goto exit;
0426     }
0427 
0428     for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
0429         mdelay(LINE6_READ_WRITE_STATUS_DELAY);
0430 
0431         ret = usb_control_msg_recv(usbdev, 0, 0x67,
0432                        USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0433                        0x0012, 0x0000, status, 1, LINE6_TIMEOUT,
0434                        GFP_KERNEL);
0435         if (ret) {
0436             dev_err(line6->ifcdev,
0437                 "receiving status failed (error %d)\n", ret);
0438             goto exit;
0439         }
0440 
0441         if (*status != 0xff)
0442             break;
0443     }
0444 
0445     if (*status == 0xff) {
0446         dev_err(line6->ifcdev, "write failed after %d retries\n",
0447             count);
0448         ret = -EIO;
0449     } else if (*status != 0) {
0450         dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
0451         ret = -EIO;
0452     }
0453 exit:
0454     kfree(status);
0455     return ret;
0456 }
0457 EXPORT_SYMBOL_GPL(line6_write_data);
0458 
0459 /*
0460     Read Line 6 device serial number.
0461     (POD, TonePort, GuitarPort)
0462 */
0463 int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
0464 {
0465     return line6_read_data(line6, 0x80d0, serial_number,
0466                    sizeof(*serial_number));
0467 }
0468 EXPORT_SYMBOL_GPL(line6_read_serial_number);
0469 
0470 /*
0471     Card destructor.
0472 */
0473 static void line6_destruct(struct snd_card *card)
0474 {
0475     struct usb_line6 *line6 = card->private_data;
0476     struct usb_device *usbdev = line6->usbdev;
0477 
0478     /* Free buffer memory first. We cannot depend on the existence of private
0479      * data from the (podhd) module, it may be gone already during this call
0480      */
0481     kfree(line6->buffer_message);
0482 
0483     kfree(line6->buffer_listen);
0484 
0485     /* then free URBs: */
0486     usb_free_urb(line6->urb_listen);
0487     line6->urb_listen = NULL;
0488 
0489     /* decrement reference counters: */
0490     usb_put_dev(usbdev);
0491 }
0492 
0493 static void line6_get_usb_properties(struct usb_line6 *line6)
0494 {
0495     struct usb_device *usbdev = line6->usbdev;
0496     const struct line6_properties *properties = line6->properties;
0497     int pipe;
0498     struct usb_host_endpoint *ep = NULL;
0499 
0500     if (properties->capabilities & LINE6_CAP_CONTROL) {
0501         if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
0502             pipe = usb_rcvintpipe(line6->usbdev,
0503                 line6->properties->ep_ctrl_r);
0504         } else {
0505             pipe = usb_rcvbulkpipe(line6->usbdev,
0506                 line6->properties->ep_ctrl_r);
0507         }
0508         ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
0509     }
0510 
0511     /* Control data transfer properties */
0512     if (ep) {
0513         line6->interval = ep->desc.bInterval;
0514         line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
0515     } else {
0516         if (properties->capabilities & LINE6_CAP_CONTROL) {
0517             dev_err(line6->ifcdev,
0518                 "endpoint not available, using fallback values");
0519         }
0520         line6->interval = LINE6_FALLBACK_INTERVAL;
0521         line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
0522     }
0523 
0524     /* Isochronous transfer properties */
0525     if (usbdev->speed == USB_SPEED_LOW) {
0526         line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
0527         line6->iso_buffers = USB_LOW_ISO_BUFFERS;
0528     } else {
0529         line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
0530         line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
0531     }
0532 }
0533 
0534 /* Enable buffering of incoming messages, flush the buffer */
0535 static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
0536 {
0537     struct usb_line6 *line6 = hw->private_data;
0538 
0539     /* NOTE: hwdep layer provides atomicity here */
0540 
0541     line6->messages.active = 1;
0542     line6->messages.nonblock = file->f_flags & O_NONBLOCK ? 1 : 0;
0543 
0544     return 0;
0545 }
0546 
0547 /* Stop buffering */
0548 static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
0549 {
0550     struct usb_line6 *line6 = hw->private_data;
0551 
0552     line6->messages.active = 0;
0553 
0554     return 0;
0555 }
0556 
0557 /* Read from circular buffer, return to user */
0558 static long
0559 line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
0560                     loff_t *offset)
0561 {
0562     struct usb_line6 *line6 = hwdep->private_data;
0563     long rv = 0;
0564     unsigned int out_count;
0565 
0566     if (mutex_lock_interruptible(&line6->messages.read_lock))
0567         return -ERESTARTSYS;
0568 
0569     while (kfifo_len(&line6->messages.fifo) == 0) {
0570         mutex_unlock(&line6->messages.read_lock);
0571 
0572         if (line6->messages.nonblock)
0573             return -EAGAIN;
0574 
0575         rv = wait_event_interruptible(
0576             line6->messages.wait_queue,
0577             kfifo_len(&line6->messages.fifo) != 0);
0578         if (rv < 0)
0579             return rv;
0580 
0581         if (mutex_lock_interruptible(&line6->messages.read_lock))
0582             return -ERESTARTSYS;
0583     }
0584 
0585     if (kfifo_peek_len(&line6->messages.fifo) > count) {
0586         /* Buffer too small; allow re-read of the current item... */
0587         rv = -EINVAL;
0588     } else {
0589         rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
0590         if (rv == 0)
0591             rv = out_count;
0592     }
0593 
0594     mutex_unlock(&line6->messages.read_lock);
0595     return rv;
0596 }
0597 
0598 /* Write directly (no buffering) to device by user*/
0599 static long
0600 line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
0601                     loff_t *offset)
0602 {
0603     struct usb_line6 *line6 = hwdep->private_data;
0604     int rv;
0605     char *data_copy;
0606 
0607     if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
0608         /* This is an arbitrary limit - still better than nothing... */
0609         return -EINVAL;
0610     }
0611 
0612     data_copy = memdup_user(data, count);
0613     if (IS_ERR(data_copy))
0614         return PTR_ERR(data_copy);
0615 
0616     rv = line6_send_raw_message(line6, data_copy, count);
0617 
0618     kfree(data_copy);
0619     return rv;
0620 }
0621 
0622 static __poll_t
0623 line6_hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
0624 {
0625     __poll_t rv;
0626     struct usb_line6 *line6 = hwdep->private_data;
0627 
0628     poll_wait(file, &line6->messages.wait_queue, wait);
0629 
0630     mutex_lock(&line6->messages.read_lock);
0631     rv = kfifo_len(&line6->messages.fifo) == 0 ? 0 : EPOLLIN | EPOLLRDNORM;
0632     mutex_unlock(&line6->messages.read_lock);
0633 
0634     return rv;
0635 }
0636 
0637 static const struct snd_hwdep_ops hwdep_ops = {
0638     .open    = line6_hwdep_open,
0639     .release = line6_hwdep_release,
0640     .read    = line6_hwdep_read,
0641     .write   = line6_hwdep_write,
0642     .poll    = line6_hwdep_poll,
0643 };
0644 
0645 /* Insert into circular buffer */
0646 static void line6_hwdep_push_message(struct usb_line6 *line6)
0647 {
0648     if (!line6->messages.active)
0649         return;
0650 
0651     if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
0652         /* No race condition here, there's only one writer */
0653         kfifo_in(&line6->messages.fifo,
0654             line6->buffer_message, line6->message_length);
0655     } /* else TODO: signal overflow */
0656 
0657     wake_up_interruptible(&line6->messages.wait_queue);
0658 }
0659 
0660 static int line6_hwdep_init(struct usb_line6 *line6)
0661 {
0662     int err;
0663     struct snd_hwdep *hwdep;
0664 
0665     /* TODO: usb_driver_claim_interface(); */
0666     line6->process_message = line6_hwdep_push_message;
0667     line6->messages.active = 0;
0668     init_waitqueue_head(&line6->messages.wait_queue);
0669     mutex_init(&line6->messages.read_lock);
0670     INIT_KFIFO(line6->messages.fifo);
0671 
0672     err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
0673     if (err < 0)
0674         goto end;
0675     strcpy(hwdep->name, "config");
0676     hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
0677     hwdep->ops = hwdep_ops;
0678     hwdep->private_data = line6;
0679     hwdep->exclusive = true;
0680 
0681 end:
0682     return err;
0683 }
0684 
0685 static int line6_init_cap_control(struct usb_line6 *line6)
0686 {
0687     int ret;
0688 
0689     /* initialize USB buffers: */
0690     line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
0691     if (!line6->buffer_listen)
0692         return -ENOMEM;
0693 
0694     line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
0695     if (!line6->urb_listen)
0696         return -ENOMEM;
0697 
0698     if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
0699         line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
0700         if (!line6->buffer_message)
0701             return -ENOMEM;
0702 
0703         ret = line6_init_midi(line6);
0704         if (ret < 0)
0705             return ret;
0706     } else {
0707         ret = line6_hwdep_init(line6);
0708         if (ret < 0)
0709             return ret;
0710     }
0711 
0712     ret = line6_start_listen(line6);
0713     if (ret < 0) {
0714         dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
0715         return ret;
0716     }
0717 
0718     return 0;
0719 }
0720 
0721 static void line6_startup_work(struct work_struct *work)
0722 {
0723     struct usb_line6 *line6 =
0724         container_of(work, struct usb_line6, startup_work.work);
0725 
0726     if (line6->startup)
0727         line6->startup(line6);
0728 }
0729 
0730 /*
0731     Probe USB device.
0732 */
0733 int line6_probe(struct usb_interface *interface,
0734         const struct usb_device_id *id,
0735         const char *driver_name,
0736         const struct line6_properties *properties,
0737         int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
0738         size_t data_size)
0739 {
0740     struct usb_device *usbdev = interface_to_usbdev(interface);
0741     struct snd_card *card;
0742     struct usb_line6 *line6;
0743     int interface_number;
0744     int ret;
0745 
0746     if (WARN_ON(data_size < sizeof(*line6)))
0747         return -EINVAL;
0748 
0749     /* we don't handle multiple configurations */
0750     if (usbdev->descriptor.bNumConfigurations != 1)
0751         return -ENODEV;
0752 
0753     ret = snd_card_new(&interface->dev,
0754                SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
0755                THIS_MODULE, data_size, &card);
0756     if (ret < 0)
0757         return ret;
0758 
0759     /* store basic data: */
0760     line6 = card->private_data;
0761     line6->card = card;
0762     line6->properties = properties;
0763     line6->usbdev = usbdev;
0764     line6->ifcdev = &interface->dev;
0765     INIT_DELAYED_WORK(&line6->startup_work, line6_startup_work);
0766 
0767     strcpy(card->id, properties->id);
0768     strcpy(card->driver, driver_name);
0769     strcpy(card->shortname, properties->name);
0770     sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
0771         dev_name(line6->ifcdev));
0772     card->private_free = line6_destruct;
0773 
0774     usb_set_intfdata(interface, line6);
0775 
0776     /* increment reference counters: */
0777     usb_get_dev(usbdev);
0778 
0779     /* initialize device info: */
0780     dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
0781 
0782     /* query interface number */
0783     interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
0784 
0785     /* TODO reserves the bus bandwidth even without actual transfer */
0786     ret = usb_set_interface(usbdev, interface_number,
0787                 properties->altsetting);
0788     if (ret < 0) {
0789         dev_err(&interface->dev, "set_interface failed\n");
0790         goto error;
0791     }
0792 
0793     line6_get_usb_properties(line6);
0794 
0795     if (properties->capabilities & LINE6_CAP_CONTROL) {
0796         ret = line6_init_cap_control(line6);
0797         if (ret < 0)
0798             goto error;
0799     }
0800 
0801     /* initialize device data based on device: */
0802     ret = private_init(line6, id);
0803     if (ret < 0)
0804         goto error;
0805 
0806     /* creation of additional special files should go here */
0807 
0808     dev_info(&interface->dev, "Line 6 %s now attached\n",
0809          properties->name);
0810 
0811     return 0;
0812 
0813  error:
0814     /* we can call disconnect callback here because no close-sync is
0815      * needed yet at this point
0816      */
0817     line6_disconnect(interface);
0818     return ret;
0819 }
0820 EXPORT_SYMBOL_GPL(line6_probe);
0821 
0822 /*
0823     Line 6 device disconnected.
0824 */
0825 void line6_disconnect(struct usb_interface *interface)
0826 {
0827     struct usb_line6 *line6 = usb_get_intfdata(interface);
0828     struct usb_device *usbdev = interface_to_usbdev(interface);
0829 
0830     if (!line6)
0831         return;
0832 
0833     if (WARN_ON(usbdev != line6->usbdev))
0834         return;
0835 
0836     cancel_delayed_work_sync(&line6->startup_work);
0837 
0838     if (line6->urb_listen != NULL)
0839         line6_stop_listen(line6);
0840 
0841     snd_card_disconnect(line6->card);
0842     if (line6->line6pcm)
0843         line6_pcm_disconnect(line6->line6pcm);
0844     if (line6->disconnect)
0845         line6->disconnect(line6);
0846 
0847     dev_info(&interface->dev, "Line 6 %s now disconnected\n",
0848          line6->properties->name);
0849 
0850     /* make sure the device isn't destructed twice: */
0851     usb_set_intfdata(interface, NULL);
0852 
0853     snd_card_free_when_closed(line6->card);
0854 }
0855 EXPORT_SYMBOL_GPL(line6_disconnect);
0856 
0857 #ifdef CONFIG_PM
0858 
0859 /*
0860     Suspend Line 6 device.
0861 */
0862 int line6_suspend(struct usb_interface *interface, pm_message_t message)
0863 {
0864     struct usb_line6 *line6 = usb_get_intfdata(interface);
0865     struct snd_line6_pcm *line6pcm = line6->line6pcm;
0866 
0867     snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
0868 
0869     if (line6->properties->capabilities & LINE6_CAP_CONTROL)
0870         line6_stop_listen(line6);
0871 
0872     if (line6pcm != NULL)
0873         line6pcm->flags = 0;
0874 
0875     return 0;
0876 }
0877 EXPORT_SYMBOL_GPL(line6_suspend);
0878 
0879 /*
0880     Resume Line 6 device.
0881 */
0882 int line6_resume(struct usb_interface *interface)
0883 {
0884     struct usb_line6 *line6 = usb_get_intfdata(interface);
0885 
0886     if (line6->properties->capabilities & LINE6_CAP_CONTROL)
0887         line6_start_listen(line6);
0888 
0889     snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
0890     return 0;
0891 }
0892 EXPORT_SYMBOL_GPL(line6_resume);
0893 
0894 #endif /* CONFIG_PM */
0895 
0896 MODULE_AUTHOR(DRIVER_AUTHOR);
0897 MODULE_DESCRIPTION(DRIVER_DESC);
0898 MODULE_LICENSE("GPL");
0899