GNU Linux-libre 4.9.337-gnu1
[releases.git] / sound / usb / line6 / driver.c
1 /*
2  * Line 6 Linux USB driver
3  *
4  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
5  *
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.
9  *
10  */
11
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>
17
18 #include <sound/core.h>
19 #include <sound/initval.h>
20 #include <sound/hwdep.h>
21
22 #include "capture.h"
23 #include "driver.h"
24 #include "midi.h"
25 #include "playback.h"
26
27 #define DRIVER_AUTHOR  "Markus Grabner <grabner@icg.tugraz.at>"
28 #define DRIVER_DESC    "Line 6 USB Driver"
29
30 /*
31         This is Line 6's MIDI manufacturer ID.
32 */
33 const unsigned char line6_midi_id[3] = {
34         0x00, 0x01, 0x0c
35 };
36 EXPORT_SYMBOL_GPL(line6_midi_id);
37
38 /*
39         Code to request version of POD, Variax interface
40         (and maybe other devices).
41 */
42 static const char line6_request_version[] = {
43         0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
44 };
45
46 /*
47          Class for asynchronous messages.
48 */
49 struct message {
50         struct usb_line6 *line6;
51         const char *buffer;
52         int size;
53         int done;
54 };
55
56 /*
57         Forward declarations.
58 */
59 static void line6_data_received(struct urb *urb);
60 static int line6_send_raw_message_async_part(struct message *msg,
61                                              struct urb *urb);
62
63 /*
64         Start to listen on endpoint.
65 */
66 static int line6_start_listen(struct usb_line6 *line6)
67 {
68         int err;
69
70         if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
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         } else {
76                 usb_fill_bulk_urb(line6->urb_listen, line6->usbdev,
77                         usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r),
78                         line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
79                         line6_data_received, line6);
80         }
81         line6->urb_listen->actual_length = 0;
82         err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
83         return err;
84 }
85
86 /*
87         Stop listening on endpoint.
88 */
89 static void line6_stop_listen(struct usb_line6 *line6)
90 {
91         usb_kill_urb(line6->urb_listen);
92 }
93
94 /*
95         Send raw message in pieces of wMaxPacketSize bytes.
96 */
97 static int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
98                                   int size)
99 {
100         int i, done = 0;
101         const struct line6_properties *properties = line6->properties;
102
103         for (i = 0; i < size; i += line6->max_packet_size) {
104                 int partial;
105                 const char *frag_buf = buffer + i;
106                 int frag_size = min(line6->max_packet_size, size - i);
107                 int retval;
108
109                 if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
110                         retval = usb_interrupt_msg(line6->usbdev,
111                                                 usb_sndintpipe(line6->usbdev, properties->ep_ctrl_w),
112                                                 (char *)frag_buf, frag_size,
113                                                 &partial, LINE6_TIMEOUT);
114                 } else {
115                         retval = usb_bulk_msg(line6->usbdev,
116                                                 usb_sndbulkpipe(line6->usbdev, properties->ep_ctrl_w),
117                                                 (char *)frag_buf, frag_size,
118                                                 &partial, LINE6_TIMEOUT);
119                 }
120
121                 if (retval) {
122                         dev_err(line6->ifcdev,
123                                 "usb_bulk_msg failed (%d)\n", retval);
124                         break;
125                 }
126
127                 done += frag_size;
128         }
129
130         return done;
131 }
132
133 /*
134         Notification of completion of asynchronous request transmission.
135 */
136 static void line6_async_request_sent(struct urb *urb)
137 {
138         struct message *msg = (struct message *)urb->context;
139
140         if (msg->done >= msg->size) {
141                 usb_free_urb(urb);
142                 kfree(msg);
143         } else
144                 line6_send_raw_message_async_part(msg, urb);
145 }
146
147 /*
148         Asynchronously send part of a raw message.
149 */
150 static int line6_send_raw_message_async_part(struct message *msg,
151                                              struct urb *urb)
152 {
153         int retval;
154         struct usb_line6 *line6 = msg->line6;
155         int done = msg->done;
156         int bytes = min(msg->size - done, line6->max_packet_size);
157
158         if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
159                 usb_fill_int_urb(urb, line6->usbdev,
160                         usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
161                         (char *)msg->buffer + done, bytes,
162                         line6_async_request_sent, msg, line6->interval);
163         } else {
164                 usb_fill_bulk_urb(urb, line6->usbdev,
165                         usb_sndbulkpipe(line6->usbdev, line6->properties->ep_ctrl_w),
166                         (char *)msg->buffer + done, bytes,
167                         line6_async_request_sent, msg);
168         }
169
170         msg->done += bytes;
171         retval = usb_submit_urb(urb, GFP_ATOMIC);
172
173         if (retval < 0) {
174                 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
175                         __func__, retval);
176                 usb_free_urb(urb);
177                 kfree(msg);
178                 return retval;
179         }
180
181         return 0;
182 }
183
184 /*
185         Setup and start timer.
186 */
187 void line6_start_timer(struct timer_list *timer, unsigned long msecs,
188                        void (*function)(unsigned long), unsigned long data)
189 {
190         setup_timer(timer, function, data);
191         mod_timer(timer, jiffies + msecs_to_jiffies(msecs));
192 }
193 EXPORT_SYMBOL_GPL(line6_start_timer);
194
195 /*
196         Asynchronously send raw message.
197 */
198 int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
199                                  int size)
200 {
201         struct message *msg;
202         struct urb *urb;
203
204         /* create message: */
205         msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
206         if (msg == NULL)
207                 return -ENOMEM;
208
209         /* create URB: */
210         urb = usb_alloc_urb(0, GFP_ATOMIC);
211
212         if (urb == NULL) {
213                 kfree(msg);
214                 return -ENOMEM;
215         }
216
217         /* set message data: */
218         msg->line6 = line6;
219         msg->buffer = buffer;
220         msg->size = size;
221         msg->done = 0;
222
223         /* start sending: */
224         return line6_send_raw_message_async_part(msg, urb);
225 }
226 EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
227
228 /*
229         Send asynchronous device version request.
230 */
231 int line6_version_request_async(struct usb_line6 *line6)
232 {
233         char *buffer;
234         int retval;
235
236         buffer = kmemdup(line6_request_version,
237                         sizeof(line6_request_version), GFP_ATOMIC);
238         if (buffer == NULL)
239                 return -ENOMEM;
240
241         retval = line6_send_raw_message_async(line6, buffer,
242                                               sizeof(line6_request_version));
243         kfree(buffer);
244         return retval;
245 }
246 EXPORT_SYMBOL_GPL(line6_version_request_async);
247
248 /*
249         Send sysex message in pieces of wMaxPacketSize bytes.
250 */
251 int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
252                              int size)
253 {
254         return line6_send_raw_message(line6, buffer,
255                                       size + SYSEX_EXTRA_SIZE) -
256             SYSEX_EXTRA_SIZE;
257 }
258 EXPORT_SYMBOL_GPL(line6_send_sysex_message);
259
260 /*
261         Allocate buffer for sysex message and prepare header.
262         @param code sysex message code
263         @param size number of bytes between code and sysex end
264 */
265 char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
266                                int size)
267 {
268         char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
269
270         if (!buffer)
271                 return NULL;
272
273         buffer[0] = LINE6_SYSEX_BEGIN;
274         memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
275         buffer[sizeof(line6_midi_id) + 1] = code1;
276         buffer[sizeof(line6_midi_id) + 2] = code2;
277         buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
278         return buffer;
279 }
280 EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
281
282 /*
283         Notification of data received from the Line 6 device.
284 */
285 static void line6_data_received(struct urb *urb)
286 {
287         struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
288         struct midi_buffer *mb = &line6->line6midi->midibuf_in;
289         int done;
290
291         if (urb->status == -ESHUTDOWN)
292                 return;
293
294         if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
295                 done =
296                         line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
297
298                 if (done < urb->actual_length) {
299                         line6_midibuf_ignore(mb, done);
300                         dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
301                                 done, urb->actual_length);
302                 }
303
304                 for (;;) {
305                         done =
306                                 line6_midibuf_read(mb, line6->buffer_message,
307                                                    LINE6_MIDI_MESSAGE_MAXLEN,
308                                                    LINE6_MIDIBUF_READ_RX);
309
310                         if (done <= 0)
311                                 break;
312
313                         line6->message_length = done;
314                         line6_midi_receive(line6, line6->buffer_message, done);
315
316                         if (line6->process_message)
317                                 line6->process_message(line6);
318                 }
319         } else {
320                 line6->buffer_message = urb->transfer_buffer;
321                 line6->message_length = urb->actual_length;
322                 if (line6->process_message)
323                         line6->process_message(line6);
324                 line6->buffer_message = NULL;
325         }
326
327         line6_start_listen(line6);
328 }
329
330 #define LINE6_READ_WRITE_STATUS_DELAY 2  /* milliseconds */
331 #define LINE6_READ_WRITE_MAX_RETRIES 50
332
333 /*
334         Read data from device.
335 */
336 int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
337                     unsigned datalen)
338 {
339         struct usb_device *usbdev = line6->usbdev;
340         int ret;
341         unsigned char *len;
342         unsigned count;
343
344         if (address > 0xffff || datalen > 0xff)
345                 return -EINVAL;
346
347         len = kmalloc(sizeof(*len), GFP_KERNEL);
348         if (!len)
349                 return -ENOMEM;
350
351         /* query the serial number: */
352         ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
353                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
354                               (datalen << 8) | 0x21, address,
355                               NULL, 0, LINE6_TIMEOUT);
356
357         if (ret < 0) {
358                 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
359                 goto exit;
360         }
361
362         /* Wait for data length. We'll get 0xff until length arrives. */
363         for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
364                 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
365
366                 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
367                                       USB_TYPE_VENDOR | USB_RECIP_DEVICE |
368                                       USB_DIR_IN,
369                                       0x0012, 0x0000, len, 1,
370                                       LINE6_TIMEOUT);
371                 if (ret < 0) {
372                         dev_err(line6->ifcdev,
373                                 "receive length failed (error %d)\n", ret);
374                         goto exit;
375                 }
376
377                 if (*len != 0xff)
378                         break;
379         }
380
381         ret = -EIO;
382         if (*len == 0xff) {
383                 dev_err(line6->ifcdev, "read failed after %d retries\n",
384                         count);
385                 goto exit;
386         } else if (*len != datalen) {
387                 /* should be equal or something went wrong */
388                 dev_err(line6->ifcdev,
389                         "length mismatch (expected %d, got %d)\n",
390                         (int)datalen, (int)*len);
391                 goto exit;
392         }
393
394         /* receive the result: */
395         ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
396                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
397                               0x0013, 0x0000, data, datalen,
398                               LINE6_TIMEOUT);
399
400         if (ret < 0)
401                 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
402
403 exit:
404         kfree(len);
405         return ret;
406 }
407 EXPORT_SYMBOL_GPL(line6_read_data);
408
409 /*
410         Write data to device.
411 */
412 int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
413                      unsigned datalen)
414 {
415         struct usb_device *usbdev = line6->usbdev;
416         int ret;
417         unsigned char *status;
418         int count;
419
420         if (address > 0xffff || datalen > 0xffff)
421                 return -EINVAL;
422
423         status = kmalloc(sizeof(*status), GFP_KERNEL);
424         if (!status)
425                 return -ENOMEM;
426
427         ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
428                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
429                               0x0022, address, data, datalen,
430                               LINE6_TIMEOUT);
431
432         if (ret < 0) {
433                 dev_err(line6->ifcdev,
434                         "write request failed (error %d)\n", ret);
435                 goto exit;
436         }
437
438         for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
439                 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
440
441                 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
442                                       0x67,
443                                       USB_TYPE_VENDOR | USB_RECIP_DEVICE |
444                                       USB_DIR_IN,
445                                       0x0012, 0x0000,
446                                       status, 1, LINE6_TIMEOUT);
447
448                 if (ret < 0) {
449                         dev_err(line6->ifcdev,
450                                 "receiving status failed (error %d)\n", ret);
451                         goto exit;
452                 }
453
454                 if (*status != 0xff)
455                         break;
456         }
457
458         if (*status == 0xff) {
459                 dev_err(line6->ifcdev, "write failed after %d retries\n",
460                         count);
461                 ret = -EIO;
462         } else if (*status != 0) {
463                 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
464                 ret = -EIO;
465         }
466 exit:
467         kfree(status);
468         return ret;
469 }
470 EXPORT_SYMBOL_GPL(line6_write_data);
471
472 /*
473         Read Line 6 device serial number.
474         (POD, TonePort, GuitarPort)
475 */
476 int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
477 {
478         return line6_read_data(line6, 0x80d0, serial_number,
479                                sizeof(*serial_number));
480 }
481 EXPORT_SYMBOL_GPL(line6_read_serial_number);
482
483 /*
484         Card destructor.
485 */
486 static void line6_destruct(struct snd_card *card)
487 {
488         struct usb_line6 *line6 = card->private_data;
489         struct usb_device *usbdev = line6->usbdev;
490
491         /* Free buffer memory first. We cannot depend on the existence of private
492          * data from the (podhd) module, it may be gone already during this call
493          */
494         kfree(line6->buffer_message);
495
496         kfree(line6->buffer_listen);
497
498         /* then free URBs: */
499         usb_free_urb(line6->urb_listen);
500         line6->urb_listen = NULL;
501
502         /* decrement reference counters: */
503         usb_put_dev(usbdev);
504 }
505
506 /* get data from endpoint descriptor (see usb_maxpacket): */
507 static void line6_get_interval(struct usb_line6 *line6)
508 {
509         struct usb_device *usbdev = line6->usbdev;
510         const struct line6_properties *properties = line6->properties;
511         int pipe;
512         struct usb_host_endpoint *ep;
513
514         if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
515                 pipe =
516                         usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r);
517         } else {
518                 pipe =
519                         usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r);
520         }
521         ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
522
523         if (ep) {
524                 line6->interval = ep->desc.bInterval;
525                 if (usbdev->speed == USB_SPEED_LOW) {
526                         line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
527                         line6->iso_buffers = USB_LOW_ISO_BUFFERS;
528                 } else {
529                         line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
530                         line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
531                 }
532
533                 line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
534         } else {
535                 dev_err(line6->ifcdev,
536                         "endpoint not available, using fallback values");
537                 line6->interval = LINE6_FALLBACK_INTERVAL;
538                 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
539         }
540 }
541
542
543 /* Enable buffering of incoming messages, flush the buffer */
544 static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
545 {
546         struct usb_line6 *line6 = hw->private_data;
547
548         /* NOTE: hwdep layer provides atomicity here */
549
550         line6->messages.active = 1;
551
552         return 0;
553 }
554
555 /* Stop buffering */
556 static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
557 {
558         struct usb_line6 *line6 = hw->private_data;
559
560         line6->messages.active = 0;
561
562         return 0;
563 }
564
565 /* Read from circular buffer, return to user */
566 static long
567 line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
568                                         loff_t *offset)
569 {
570         struct usb_line6 *line6 = hwdep->private_data;
571         long rv = 0;
572         unsigned int out_count;
573
574         if (mutex_lock_interruptible(&line6->messages.read_lock))
575                 return -ERESTARTSYS;
576
577         while (kfifo_len(&line6->messages.fifo) == 0) {
578                 mutex_unlock(&line6->messages.read_lock);
579
580                 rv = wait_event_interruptible(
581                         line6->messages.wait_queue,
582                         kfifo_len(&line6->messages.fifo) != 0);
583                 if (rv < 0)
584                         return rv;
585
586                 if (mutex_lock_interruptible(&line6->messages.read_lock))
587                         return -ERESTARTSYS;
588         }
589
590         if (kfifo_peek_len(&line6->messages.fifo) > count) {
591                 /* Buffer too small; allow re-read of the current item... */
592                 rv = -EINVAL;
593         } else {
594                 rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
595                 if (rv == 0)
596                         rv = out_count;
597         }
598
599         mutex_unlock(&line6->messages.read_lock);
600         return rv;
601 }
602
603 /* Write directly (no buffering) to device by user*/
604 static long
605 line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
606                                         loff_t *offset)
607 {
608         struct usb_line6 *line6 = hwdep->private_data;
609         int rv;
610         char *data_copy;
611
612         if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
613                 /* This is an arbitrary limit - still better than nothing... */
614                 return -EINVAL;
615         }
616
617         data_copy = memdup_user(data, count);
618         if (IS_ERR(data_copy))
619                 return PTR_ERR(data_copy);
620
621         rv = line6_send_raw_message(line6, data_copy, count);
622
623         kfree(data_copy);
624         return rv;
625 }
626
627 static const struct snd_hwdep_ops hwdep_ops = {
628         .open    = line6_hwdep_open,
629         .release = line6_hwdep_release,
630         .read    = line6_hwdep_read,
631         .write   = line6_hwdep_write,
632 };
633
634 /* Insert into circular buffer */
635 static void line6_hwdep_push_message(struct usb_line6 *line6)
636 {
637         if (!line6->messages.active)
638                 return;
639
640         if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
641                 /* No race condition here, there's only one writer */
642                 kfifo_in(&line6->messages.fifo,
643                         line6->buffer_message, line6->message_length);
644         } /* else TODO: signal overflow */
645
646         wake_up_interruptible(&line6->messages.wait_queue);
647 }
648
649 static int line6_hwdep_init(struct usb_line6 *line6)
650 {
651         int err;
652         struct snd_hwdep *hwdep;
653
654         /* TODO: usb_driver_claim_interface(); */
655         line6->process_message = line6_hwdep_push_message;
656         line6->messages.active = 0;
657         init_waitqueue_head(&line6->messages.wait_queue);
658         mutex_init(&line6->messages.read_lock);
659         INIT_KFIFO(line6->messages.fifo);
660
661         err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
662         if (err < 0)
663                 goto end;
664         strcpy(hwdep->name, "config");
665         hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
666         hwdep->ops = hwdep_ops;
667         hwdep->private_data = line6;
668         hwdep->exclusive = true;
669
670 end:
671         return err;
672 }
673
674 static int line6_init_cap_control(struct usb_line6 *line6)
675 {
676         int ret;
677
678         /* initialize USB buffers: */
679         line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
680         if (!line6->buffer_listen)
681                 return -ENOMEM;
682
683         line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
684         if (!line6->urb_listen)
685                 return -ENOMEM;
686
687         if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
688                 line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
689                 if (!line6->buffer_message)
690                         return -ENOMEM;
691
692                 ret = line6_init_midi(line6);
693                 if (ret < 0)
694                         return ret;
695         } else {
696                 ret = line6_hwdep_init(line6);
697                 if (ret < 0)
698                         return ret;
699         }
700
701         ret = line6_start_listen(line6);
702         if (ret < 0) {
703                 dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
704                 return ret;
705         }
706
707         return 0;
708 }
709
710 /*
711         Probe USB device.
712 */
713 int line6_probe(struct usb_interface *interface,
714                 const struct usb_device_id *id,
715                 const char *driver_name,
716                 const struct line6_properties *properties,
717                 int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
718                 size_t data_size)
719 {
720         struct usb_device *usbdev = interface_to_usbdev(interface);
721         struct snd_card *card;
722         struct usb_line6 *line6;
723         int interface_number;
724         int ret;
725
726         if (WARN_ON(data_size < sizeof(*line6)))
727                 return -EINVAL;
728
729         /* we don't handle multiple configurations */
730         if (usbdev->descriptor.bNumConfigurations != 1)
731                 return -ENODEV;
732
733         ret = snd_card_new(&interface->dev,
734                            SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
735                            THIS_MODULE, data_size, &card);
736         if (ret < 0)
737                 return ret;
738
739         /* store basic data: */
740         line6 = card->private_data;
741         line6->card = card;
742         line6->properties = properties;
743         line6->usbdev = usbdev;
744         line6->ifcdev = &interface->dev;
745
746         strcpy(card->id, properties->id);
747         strcpy(card->driver, driver_name);
748         strcpy(card->shortname, properties->name);
749         sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
750                 dev_name(line6->ifcdev));
751         card->private_free = line6_destruct;
752
753         usb_set_intfdata(interface, line6);
754
755         /* increment reference counters: */
756         usb_get_dev(usbdev);
757
758         /* initialize device info: */
759         dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
760
761         /* query interface number */
762         interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
763
764         /* TODO reserves the bus bandwidth even without actual transfer */
765         ret = usb_set_interface(usbdev, interface_number,
766                                 properties->altsetting);
767         if (ret < 0) {
768                 dev_err(&interface->dev, "set_interface failed\n");
769                 goto error;
770         }
771
772         line6_get_interval(line6);
773
774         if (properties->capabilities & LINE6_CAP_CONTROL) {
775                 ret = line6_init_cap_control(line6);
776                 if (ret < 0)
777                         goto error;
778         }
779
780         /* initialize device data based on device: */
781         ret = private_init(line6, id);
782         if (ret < 0)
783                 goto error;
784
785         /* creation of additional special files should go here */
786
787         dev_info(&interface->dev, "Line 6 %s now attached\n",
788                  properties->name);
789
790         return 0;
791
792  error:
793         /* we can call disconnect callback here because no close-sync is
794          * needed yet at this point
795          */
796         line6_disconnect(interface);
797         return ret;
798 }
799 EXPORT_SYMBOL_GPL(line6_probe);
800
801 /*
802         Line 6 device disconnected.
803 */
804 void line6_disconnect(struct usb_interface *interface)
805 {
806         struct usb_line6 *line6 = usb_get_intfdata(interface);
807         struct usb_device *usbdev = interface_to_usbdev(interface);
808
809         if (!line6)
810                 return;
811
812         if (WARN_ON(usbdev != line6->usbdev))
813                 return;
814
815         if (line6->urb_listen != NULL)
816                 line6_stop_listen(line6);
817
818         snd_card_disconnect(line6->card);
819         if (line6->line6pcm)
820                 line6_pcm_disconnect(line6->line6pcm);
821         if (line6->disconnect)
822                 line6->disconnect(line6);
823
824         dev_info(&interface->dev, "Line 6 %s now disconnected\n",
825                  line6->properties->name);
826
827         /* make sure the device isn't destructed twice: */
828         usb_set_intfdata(interface, NULL);
829
830         snd_card_free_when_closed(line6->card);
831 }
832 EXPORT_SYMBOL_GPL(line6_disconnect);
833
834 #ifdef CONFIG_PM
835
836 /*
837         Suspend Line 6 device.
838 */
839 int line6_suspend(struct usb_interface *interface, pm_message_t message)
840 {
841         struct usb_line6 *line6 = usb_get_intfdata(interface);
842         struct snd_line6_pcm *line6pcm = line6->line6pcm;
843
844         snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
845
846         if (line6->properties->capabilities & LINE6_CAP_CONTROL)
847                 line6_stop_listen(line6);
848
849         if (line6pcm != NULL) {
850                 snd_pcm_suspend_all(line6pcm->pcm);
851                 line6pcm->flags = 0;
852         }
853
854         return 0;
855 }
856 EXPORT_SYMBOL_GPL(line6_suspend);
857
858 /*
859         Resume Line 6 device.
860 */
861 int line6_resume(struct usb_interface *interface)
862 {
863         struct usb_line6 *line6 = usb_get_intfdata(interface);
864
865         if (line6->properties->capabilities & LINE6_CAP_CONTROL)
866                 line6_start_listen(line6);
867
868         snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
869         return 0;
870 }
871 EXPORT_SYMBOL_GPL(line6_resume);
872
873 #endif /* CONFIG_PM */
874
875 MODULE_AUTHOR(DRIVER_AUTHOR);
876 MODULE_DESCRIPTION(DRIVER_DESC);
877 MODULE_LICENSE("GPL");
878