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