xref: /openbmc/linux/sound/usb/line6/driver.c (revision 1d27a0be)
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 	line6->messages.nonblock = file->f_flags & O_NONBLOCK ? 1 : 0;
554 
555 	return 0;
556 }
557 
558 /* Stop buffering */
559 static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
560 {
561 	struct usb_line6 *line6 = hw->private_data;
562 
563 	line6->messages.active = 0;
564 
565 	return 0;
566 }
567 
568 /* Read from circular buffer, return to user */
569 static long
570 line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
571 					loff_t *offset)
572 {
573 	struct usb_line6 *line6 = hwdep->private_data;
574 	long rv = 0;
575 	unsigned int out_count;
576 
577 	if (mutex_lock_interruptible(&line6->messages.read_lock))
578 		return -ERESTARTSYS;
579 
580 	while (kfifo_len(&line6->messages.fifo) == 0) {
581 		mutex_unlock(&line6->messages.read_lock);
582 
583 		if (line6->messages.nonblock)
584 			return -EAGAIN;
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 __poll_t
634 line6_hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
635 {
636 	__poll_t rv;
637 	struct usb_line6 *line6 = hwdep->private_data;
638 
639 	poll_wait(file, &line6->messages.wait_queue, wait);
640 
641 	mutex_lock(&line6->messages.read_lock);
642 	rv = kfifo_len(&line6->messages.fifo) == 0 ? 0 : EPOLLIN | EPOLLRDNORM;
643 	mutex_unlock(&line6->messages.read_lock);
644 
645 	return rv;
646 }
647 
648 static const struct snd_hwdep_ops hwdep_ops = {
649 	.open    = line6_hwdep_open,
650 	.release = line6_hwdep_release,
651 	.read    = line6_hwdep_read,
652 	.write   = line6_hwdep_write,
653 	.poll    = line6_hwdep_poll,
654 };
655 
656 /* Insert into circular buffer */
657 static void line6_hwdep_push_message(struct usb_line6 *line6)
658 {
659 	if (!line6->messages.active)
660 		return;
661 
662 	if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
663 		/* No race condition here, there's only one writer */
664 		kfifo_in(&line6->messages.fifo,
665 			line6->buffer_message, line6->message_length);
666 	} /* else TODO: signal overflow */
667 
668 	wake_up_interruptible(&line6->messages.wait_queue);
669 }
670 
671 static int line6_hwdep_init(struct usb_line6 *line6)
672 {
673 	int err;
674 	struct snd_hwdep *hwdep;
675 
676 	/* TODO: usb_driver_claim_interface(); */
677 	line6->process_message = line6_hwdep_push_message;
678 	line6->messages.active = 0;
679 	init_waitqueue_head(&line6->messages.wait_queue);
680 	mutex_init(&line6->messages.read_lock);
681 	INIT_KFIFO(line6->messages.fifo);
682 
683 	err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
684 	if (err < 0)
685 		goto end;
686 	strcpy(hwdep->name, "config");
687 	hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
688 	hwdep->ops = hwdep_ops;
689 	hwdep->private_data = line6;
690 	hwdep->exclusive = true;
691 
692 end:
693 	return err;
694 }
695 
696 static int line6_init_cap_control(struct usb_line6 *line6)
697 {
698 	int ret;
699 
700 	/* initialize USB buffers: */
701 	line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
702 	if (!line6->buffer_listen)
703 		return -ENOMEM;
704 
705 	line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
706 	if (!line6->urb_listen)
707 		return -ENOMEM;
708 
709 	if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
710 		line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
711 		if (!line6->buffer_message)
712 			return -ENOMEM;
713 	} else {
714 		ret = line6_hwdep_init(line6);
715 		if (ret < 0)
716 			return ret;
717 	}
718 
719 	ret = line6_start_listen(line6);
720 	if (ret < 0) {
721 		dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
722 		return ret;
723 	}
724 
725 	return 0;
726 }
727 
728 static void line6_startup_work(struct work_struct *work)
729 {
730 	struct usb_line6 *line6 =
731 		container_of(work, struct usb_line6, startup_work.work);
732 
733 	if (line6->startup)
734 		line6->startup(line6);
735 }
736 
737 /*
738 	Probe USB device.
739 */
740 int line6_probe(struct usb_interface *interface,
741 		const struct usb_device_id *id,
742 		const char *driver_name,
743 		const struct line6_properties *properties,
744 		int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
745 		size_t data_size)
746 {
747 	struct usb_device *usbdev = interface_to_usbdev(interface);
748 	struct snd_card *card;
749 	struct usb_line6 *line6;
750 	int interface_number;
751 	int ret;
752 
753 	if (WARN_ON(data_size < sizeof(*line6)))
754 		return -EINVAL;
755 
756 	/* we don't handle multiple configurations */
757 	if (usbdev->descriptor.bNumConfigurations != 1)
758 		return -ENODEV;
759 
760 	ret = snd_card_new(&interface->dev,
761 			   SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
762 			   THIS_MODULE, data_size, &card);
763 	if (ret < 0)
764 		return ret;
765 
766 	/* store basic data: */
767 	line6 = card->private_data;
768 	line6->card = card;
769 	line6->properties = properties;
770 	line6->usbdev = usbdev;
771 	line6->ifcdev = &interface->dev;
772 	INIT_DELAYED_WORK(&line6->startup_work, line6_startup_work);
773 
774 	strcpy(card->id, properties->id);
775 	strcpy(card->driver, driver_name);
776 	strcpy(card->shortname, properties->name);
777 	sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
778 		dev_name(line6->ifcdev));
779 	card->private_free = line6_destruct;
780 
781 	usb_set_intfdata(interface, line6);
782 
783 	/* increment reference counters: */
784 	usb_get_dev(usbdev);
785 
786 	/* initialize device info: */
787 	dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
788 
789 	/* query interface number */
790 	interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
791 
792 	/* TODO reserves the bus bandwidth even without actual transfer */
793 	ret = usb_set_interface(usbdev, interface_number,
794 				properties->altsetting);
795 	if (ret < 0) {
796 		dev_err(&interface->dev, "set_interface failed\n");
797 		goto error;
798 	}
799 
800 	line6_get_usb_properties(line6);
801 
802 	if (properties->capabilities & LINE6_CAP_CONTROL) {
803 		ret = line6_init_cap_control(line6);
804 		if (ret < 0)
805 			goto error;
806 	}
807 
808 	/* initialize device data based on device: */
809 	ret = private_init(line6, id);
810 	if (ret < 0)
811 		goto error;
812 
813 	/* creation of additional special files should go here */
814 
815 	dev_info(&interface->dev, "Line 6 %s now attached\n",
816 		 properties->name);
817 
818 	return 0;
819 
820  error:
821 	/* we can call disconnect callback here because no close-sync is
822 	 * needed yet at this point
823 	 */
824 	line6_disconnect(interface);
825 	return ret;
826 }
827 EXPORT_SYMBOL_GPL(line6_probe);
828 
829 /*
830 	Line 6 device disconnected.
831 */
832 void line6_disconnect(struct usb_interface *interface)
833 {
834 	struct usb_line6 *line6 = usb_get_intfdata(interface);
835 	struct usb_device *usbdev = interface_to_usbdev(interface);
836 
837 	if (!line6)
838 		return;
839 
840 	if (WARN_ON(usbdev != line6->usbdev))
841 		return;
842 
843 	cancel_delayed_work(&line6->startup_work);
844 
845 	if (line6->urb_listen != NULL)
846 		line6_stop_listen(line6);
847 
848 	snd_card_disconnect(line6->card);
849 	if (line6->line6pcm)
850 		line6_pcm_disconnect(line6->line6pcm);
851 	if (line6->disconnect)
852 		line6->disconnect(line6);
853 
854 	dev_info(&interface->dev, "Line 6 %s now disconnected\n",
855 		 line6->properties->name);
856 
857 	/* make sure the device isn't destructed twice: */
858 	usb_set_intfdata(interface, NULL);
859 
860 	snd_card_free_when_closed(line6->card);
861 }
862 EXPORT_SYMBOL_GPL(line6_disconnect);
863 
864 #ifdef CONFIG_PM
865 
866 /*
867 	Suspend Line 6 device.
868 */
869 int line6_suspend(struct usb_interface *interface, pm_message_t message)
870 {
871 	struct usb_line6 *line6 = usb_get_intfdata(interface);
872 	struct snd_line6_pcm *line6pcm = line6->line6pcm;
873 
874 	snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
875 
876 	if (line6->properties->capabilities & LINE6_CAP_CONTROL)
877 		line6_stop_listen(line6);
878 
879 	if (line6pcm != NULL)
880 		line6pcm->flags = 0;
881 
882 	return 0;
883 }
884 EXPORT_SYMBOL_GPL(line6_suspend);
885 
886 /*
887 	Resume Line 6 device.
888 */
889 int line6_resume(struct usb_interface *interface)
890 {
891 	struct usb_line6 *line6 = usb_get_intfdata(interface);
892 
893 	if (line6->properties->capabilities & LINE6_CAP_CONTROL)
894 		line6_start_listen(line6);
895 
896 	snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
897 	return 0;
898 }
899 EXPORT_SYMBOL_GPL(line6_resume);
900 
901 #endif /* CONFIG_PM */
902 
903 MODULE_AUTHOR(DRIVER_AUTHOR);
904 MODULE_DESCRIPTION(DRIVER_DESC);
905 MODULE_LICENSE("GPL");
906 
907