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