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