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