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