• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Line6 Linux USB driver - 0.9.1beta
3  *
4  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
5  *
6  *	This program is free software; you can redistribute it and/or
7  *	modify it under the terms of the GNU General Public License as
8  *	published by the Free Software Foundation, version 2.
9  *
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/usb.h>
16 
17 #include "audio.h"
18 #include "capture.h"
19 #include "driver.h"
20 #include "midi.h"
21 #include "playback.h"
22 #include "pod.h"
23 #include "podhd.h"
24 #include "revision.h"
25 #include "toneport.h"
26 #include "usbdefs.h"
27 #include "variax.h"
28 
29 #define DRIVER_AUTHOR  "Markus Grabner <grabner@icg.tugraz.at>"
30 #define DRIVER_DESC    "Line6 USB Driver"
31 #define DRIVER_VERSION "0.9.1beta" DRIVER_REVISION
32 
33 /* table of devices that work with this driver */
34 static const struct usb_device_id line6_id_table[] = {
35 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXT)},
36 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXTLIVE)},
37 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXTPRO)},
38 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_GUITARPORT)},
39 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_POCKETPOD)},
40 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODHD300)},
41 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODHD400)},
42 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODHD500)},
43 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODSTUDIO_GX)},
44 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODSTUDIO_UX1)},
45 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODSTUDIO_UX2)},
46 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODX3)},
47 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODX3LIVE)},
48 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXT)},
49 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXTLIVE)},
50 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXTPRO)},
51 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_GX)},
52 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_UX1)},
53 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_UX2)},
54 	{USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_VARIAX)},
55 	{},
56 };
57 
58 MODULE_DEVICE_TABLE(usb, line6_id_table);
59 
60 #define L6PROP(dev_bit, dev_id, dev_name, dev_cap)\
61 	{.device_bit = LINE6_BIT_##dev_bit, .id = dev_id,\
62 	 .name = dev_name, .capabilities = LINE6_BIT_##dev_cap}
63 
64 /* *INDENT-OFF* */
65 static const struct line6_properties line6_properties_table[] = {
66 	L6PROP(BASSPODXT,     "BassPODxt",     "BassPODxt",        CTRL_PCM_HW),
67 	L6PROP(BASSPODXTLIVE, "BassPODxtLive", "BassPODxt Live",   CTRL_PCM_HW),
68 	L6PROP(BASSPODXTPRO,  "BassPODxtPro",  "BassPODxt Pro",    CTRL_PCM_HW),
69 	L6PROP(GUITARPORT,    "GuitarPort",    "GuitarPort",       PCM),
70 	L6PROP(POCKETPOD,     "PocketPOD",     "Pocket POD",       CONTROL),
71 	L6PROP(PODHD300,      "PODHD300",      "POD HD300",        CTRL_PCM_HW),
72 	L6PROP(PODHD400,      "PODHD400",      "POD HD400",        CTRL_PCM_HW),
73 	L6PROP(PODHD500,      "PODHD500",      "POD HD500",        CTRL_PCM_HW),
74 	L6PROP(PODSTUDIO_GX,  "PODStudioGX",   "POD Studio GX",    PCM),
75 	L6PROP(PODSTUDIO_UX1, "PODStudioUX1",  "POD Studio UX1",   PCM),
76 	L6PROP(PODSTUDIO_UX2, "PODStudioUX2",  "POD Studio UX2",   PCM),
77 	L6PROP(PODX3,         "PODX3",         "POD X3",           PCM),
78 	L6PROP(PODX3LIVE,     "PODX3Live",     "POD X3 Live",      PCM),
79 	L6PROP(PODXT,         "PODxt",         "PODxt",            CTRL_PCM_HW),
80 	L6PROP(PODXTLIVE,     "PODxtLive",     "PODxt Live",       CTRL_PCM_HW),
81 	L6PROP(PODXTPRO,      "PODxtPro",      "PODxt Pro",        CTRL_PCM_HW),
82 	L6PROP(TONEPORT_GX,   "TonePortGX",    "TonePort GX",      PCM),
83 	L6PROP(TONEPORT_UX1,  "TonePortUX1",   "TonePort UX1",     PCM),
84 	L6PROP(TONEPORT_UX2,  "TonePortUX2",   "TonePort UX2",     PCM),
85 	L6PROP(VARIAX,        "Variax",        "Variax Workbench", CONTROL),
86 };
87 /* *INDENT-ON* */
88 
89 /*
90 	This is Line6's MIDI manufacturer ID.
91 */
92 const unsigned char line6_midi_id[] = {
93 	0x00, 0x01, 0x0c
94 };
95 
96 /*
97 	Code to request version of POD, Variax interface
98 	(and maybe other devices).
99 */
100 static const char line6_request_version[] = {
101 	0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
102 };
103 
104 /**
105 	 Class for asynchronous messages.
106 */
107 struct message {
108 	struct usb_line6 *line6;
109 	const char *buffer;
110 	int size;
111 	int done;
112 };
113 
114 /*
115 	Forward declarations.
116 */
117 static void line6_data_received(struct urb *urb);
118 static int line6_send_raw_message_async_part(struct message *msg,
119 					     struct urb *urb);
120 
121 /*
122 	Start to listen on endpoint.
123 */
line6_start_listen(struct usb_line6 * line6)124 static int line6_start_listen(struct usb_line6 *line6)
125 {
126 	int err;
127 
128 	usb_fill_int_urb(line6->urb_listen, line6->usbdev,
129 			 usb_rcvintpipe(line6->usbdev, line6->ep_control_read),
130 			 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
131 			 line6_data_received, line6, line6->interval);
132 	line6->urb_listen->actual_length = 0;
133 	err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
134 	return err;
135 }
136 
137 /*
138 	Stop listening on endpoint.
139 */
line6_stop_listen(struct usb_line6 * line6)140 static void line6_stop_listen(struct usb_line6 *line6)
141 {
142 	usb_kill_urb(line6->urb_listen);
143 }
144 
145 /*
146 	Send raw message in pieces of wMaxPacketSize bytes.
147 */
line6_send_raw_message(struct usb_line6 * line6,const char * buffer,int size)148 int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
149 			   int size)
150 {
151 	int i, done = 0;
152 
153 	for (i = 0; i < size; i += line6->max_packet_size) {
154 		int partial;
155 		const char *frag_buf = buffer + i;
156 		int frag_size = min(line6->max_packet_size, size - i);
157 		int retval;
158 
159 		retval = usb_interrupt_msg(line6->usbdev,
160 					usb_sndintpipe(line6->usbdev,
161 						line6->ep_control_write),
162 					(char *)frag_buf, frag_size,
163 					&partial, LINE6_TIMEOUT * HZ);
164 
165 		if (retval) {
166 			dev_err(line6->ifcdev,
167 				"usb_interrupt_msg failed (%d)\n", retval);
168 			break;
169 		}
170 
171 		done += frag_size;
172 	}
173 
174 	return done;
175 }
176 
177 /*
178 	Notification of completion of asynchronous request transmission.
179 */
line6_async_request_sent(struct urb * urb)180 static void line6_async_request_sent(struct urb *urb)
181 {
182 	struct message *msg = (struct message *)urb->context;
183 
184 	if (msg->done >= msg->size) {
185 		usb_free_urb(urb);
186 		kfree(msg);
187 	} else
188 		line6_send_raw_message_async_part(msg, urb);
189 }
190 
191 /*
192 	Asynchronously send part of a raw message.
193 */
line6_send_raw_message_async_part(struct message * msg,struct urb * urb)194 static int line6_send_raw_message_async_part(struct message *msg,
195 					     struct urb *urb)
196 {
197 	int retval;
198 	struct usb_line6 *line6 = msg->line6;
199 	int done = msg->done;
200 	int bytes = min(msg->size - done, line6->max_packet_size);
201 
202 	usb_fill_int_urb(urb, line6->usbdev,
203 			 usb_sndintpipe(line6->usbdev, line6->ep_control_write),
204 			 (char *)msg->buffer + done, bytes,
205 			 line6_async_request_sent, msg, line6->interval);
206 
207 	msg->done += bytes;
208 	retval = usb_submit_urb(urb, GFP_ATOMIC);
209 
210 	if (retval < 0) {
211 		dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
212 			__func__, retval);
213 		usb_free_urb(urb);
214 		kfree(msg);
215 		return retval;
216 	}
217 
218 	return 0;
219 }
220 
221 /*
222 	Setup and start timer.
223 */
line6_start_timer(struct timer_list * timer,unsigned int msecs,void (* function)(unsigned long),unsigned long data)224 void line6_start_timer(struct timer_list *timer, unsigned int msecs,
225 		       void (*function)(unsigned long), unsigned long data)
226 {
227 	setup_timer(timer, function, data);
228 	timer->expires = jiffies + msecs * HZ / 1000;
229 	add_timer(timer);
230 }
231 
232 /*
233 	Asynchronously send raw message.
234 */
line6_send_raw_message_async(struct usb_line6 * line6,const char * buffer,int size)235 int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
236 				 int size)
237 {
238 	struct message *msg;
239 	struct urb *urb;
240 
241 	/* create message: */
242 	msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
243 	if (msg == NULL)
244 		return -ENOMEM;
245 
246 	/* create URB: */
247 	urb = usb_alloc_urb(0, GFP_ATOMIC);
248 
249 	if (urb == NULL) {
250 		kfree(msg);
251 		dev_err(line6->ifcdev, "Out of memory\n");
252 		return -ENOMEM;
253 	}
254 
255 	/* set message data: */
256 	msg->line6 = line6;
257 	msg->buffer = buffer;
258 	msg->size = size;
259 	msg->done = 0;
260 
261 	/* start sending: */
262 	return line6_send_raw_message_async_part(msg, urb);
263 }
264 
265 /*
266 	Send asynchronous device version request.
267 */
line6_version_request_async(struct usb_line6 * line6)268 int line6_version_request_async(struct usb_line6 *line6)
269 {
270 	char *buffer;
271 	int retval;
272 
273 	buffer = kmemdup(line6_request_version,
274 			sizeof(line6_request_version), GFP_ATOMIC);
275 	if (buffer == NULL) {
276 		dev_err(line6->ifcdev, "Out of memory");
277 		return -ENOMEM;
278 	}
279 
280 	retval = line6_send_raw_message_async(line6, buffer,
281 					      sizeof(line6_request_version));
282 	kfree(buffer);
283 	return retval;
284 }
285 
286 /*
287 	Send sysex message in pieces of wMaxPacketSize bytes.
288 */
line6_send_sysex_message(struct usb_line6 * line6,const char * buffer,int size)289 int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
290 			     int size)
291 {
292 	return line6_send_raw_message(line6, buffer,
293 				      size + SYSEX_EXTRA_SIZE) -
294 	    SYSEX_EXTRA_SIZE;
295 }
296 
297 /*
298 	Allocate buffer for sysex message and prepare header.
299 	@param code sysex message code
300 	@param size number of bytes between code and sysex end
301 */
line6_alloc_sysex_buffer(struct usb_line6 * line6,int code1,int code2,int size)302 char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
303 			       int size)
304 {
305 	char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
306 
307 	if (!buffer)
308 		return NULL;
309 
310 	buffer[0] = LINE6_SYSEX_BEGIN;
311 	memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
312 	buffer[sizeof(line6_midi_id) + 1] = code1;
313 	buffer[sizeof(line6_midi_id) + 2] = code2;
314 	buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
315 	return buffer;
316 }
317 
318 /*
319 	Notification of data received from the Line6 device.
320 */
line6_data_received(struct urb * urb)321 static void line6_data_received(struct urb *urb)
322 {
323 	struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
324 	struct midi_buffer *mb = &line6->line6midi->midibuf_in;
325 	int done;
326 
327 	if (urb->status == -ESHUTDOWN)
328 		return;
329 
330 	done =
331 	    line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
332 
333 	if (done < urb->actual_length) {
334 		line6_midibuf_ignore(mb, done);
335 		dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
336 			done, urb->actual_length);
337 	}
338 
339 	for (;;) {
340 		done =
341 		    line6_midibuf_read(mb, line6->buffer_message,
342 				       LINE6_MESSAGE_MAXLEN);
343 
344 		if (done == 0)
345 			break;
346 
347 		line6->message_length = done;
348 		line6_midi_receive(line6, line6->buffer_message, done);
349 
350 		switch (le16_to_cpu(line6->usbdev->descriptor.idProduct)) {
351 		case LINE6_DEVID_BASSPODXT:
352 		case LINE6_DEVID_BASSPODXTLIVE:
353 		case LINE6_DEVID_BASSPODXTPRO:
354 		case LINE6_DEVID_PODXT:
355 		case LINE6_DEVID_PODXTPRO:
356 		case LINE6_DEVID_POCKETPOD:
357 			line6_pod_process_message((struct usb_line6_pod *)
358 						  line6);
359 			break;
360 
361 		case LINE6_DEVID_PODHD300:
362 		case LINE6_DEVID_PODHD400:
363 		case LINE6_DEVID_PODHD500:
364 			break; /* let userspace handle MIDI */
365 
366 		case LINE6_DEVID_PODXTLIVE:
367 			switch (line6->interface_number) {
368 			case PODXTLIVE_INTERFACE_POD:
369 				line6_pod_process_message((struct usb_line6_pod
370 							   *)line6);
371 				break;
372 
373 			case PODXTLIVE_INTERFACE_VARIAX:
374 				line6_variax_process_message((struct
375 							      usb_line6_variax
376 							      *)line6);
377 				break;
378 
379 			default:
380 				dev_err(line6->ifcdev,
381 					"PODxt Live interface %d not supported\n",
382 					line6->interface_number);
383 			}
384 			break;
385 
386 		case LINE6_DEVID_VARIAX:
387 			line6_variax_process_message((struct usb_line6_variax *)
388 						     line6);
389 			break;
390 
391 		default:
392 			MISSING_CASE;
393 		}
394 	}
395 
396 	line6_start_listen(line6);
397 }
398 
399 /*
400 	Send channel number (i.e., switch to a different sound).
401 */
line6_send_program(struct usb_line6 * line6,u8 value)402 int line6_send_program(struct usb_line6 *line6, u8 value)
403 {
404 	int retval;
405 	unsigned char *buffer;
406 	int partial;
407 
408 	buffer = kmalloc(2, GFP_KERNEL);
409 	if (!buffer)
410 		return -ENOMEM;
411 
412 	buffer[0] = LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST;
413 	buffer[1] = value;
414 
415 	retval = usb_interrupt_msg(line6->usbdev,
416 				   usb_sndintpipe(line6->usbdev,
417 						  line6->ep_control_write),
418 				   buffer, 2, &partial, LINE6_TIMEOUT * HZ);
419 
420 	if (retval)
421 		dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
422 			retval);
423 
424 	kfree(buffer);
425 	return retval;
426 }
427 
428 /*
429 	Transmit Line6 control parameter.
430 */
line6_transmit_parameter(struct usb_line6 * line6,int param,u8 value)431 int line6_transmit_parameter(struct usb_line6 *line6, int param, u8 value)
432 {
433 	int retval;
434 	unsigned char *buffer;
435 	int partial;
436 
437 	buffer = kmalloc(3, GFP_KERNEL);
438 	if (!buffer)
439 		return -ENOMEM;
440 
441 	buffer[0] = LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST;
442 	buffer[1] = param;
443 	buffer[2] = value;
444 
445 	retval = usb_interrupt_msg(line6->usbdev,
446 				   usb_sndintpipe(line6->usbdev,
447 						  line6->ep_control_write),
448 				   buffer, 3, &partial, LINE6_TIMEOUT * HZ);
449 
450 	if (retval)
451 		dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
452 			retval);
453 
454 	kfree(buffer);
455 	return retval;
456 }
457 
458 /*
459 	Read data from device.
460 */
line6_read_data(struct usb_line6 * line6,int address,void * data,size_t datalen)461 int line6_read_data(struct usb_line6 *line6, int address, void *data,
462 		    size_t datalen)
463 {
464 	struct usb_device *usbdev = line6->usbdev;
465 	int ret;
466 	unsigned char len;
467 
468 	/* query the serial number: */
469 	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
470 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
471 			      (datalen << 8) | 0x21, address,
472 			      NULL, 0, LINE6_TIMEOUT * HZ);
473 
474 	if (ret < 0) {
475 		dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
476 		return ret;
477 	}
478 
479 	/* Wait for data length. We'll get 0xff until length arrives. */
480 	do {
481 		ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
482 				      USB_TYPE_VENDOR | USB_RECIP_DEVICE |
483 				      USB_DIR_IN,
484 				      0x0012, 0x0000, &len, 1,
485 				      LINE6_TIMEOUT * HZ);
486 		if (ret < 0) {
487 			dev_err(line6->ifcdev,
488 				"receive length failed (error %d)\n", ret);
489 			return ret;
490 		}
491 	} while (len == 0xff);
492 
493 	if (len != datalen) {
494 		/* should be equal or something went wrong */
495 		dev_err(line6->ifcdev,
496 			"length mismatch (expected %d, got %d)\n",
497 			(int)datalen, (int)len);
498 		return -EINVAL;
499 	}
500 
501 	/* receive the result: */
502 	ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
503 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
504 			      0x0013, 0x0000, data, datalen,
505 			      LINE6_TIMEOUT * HZ);
506 
507 	if (ret < 0) {
508 		dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
509 		return ret;
510 	}
511 
512 	return 0;
513 }
514 
515 /*
516 	Write data to device.
517 */
line6_write_data(struct usb_line6 * line6,int address,void * data,size_t datalen)518 int line6_write_data(struct usb_line6 *line6, int address, void *data,
519 		     size_t datalen)
520 {
521 	struct usb_device *usbdev = line6->usbdev;
522 	int ret;
523 	unsigned char status;
524 
525 	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
526 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
527 			      0x0022, address, data, datalen,
528 			      LINE6_TIMEOUT * HZ);
529 
530 	if (ret < 0) {
531 		dev_err(line6->ifcdev,
532 			"write request failed (error %d)\n", ret);
533 		return ret;
534 	}
535 
536 	do {
537 		ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
538 				      0x67,
539 				      USB_TYPE_VENDOR | USB_RECIP_DEVICE |
540 				      USB_DIR_IN,
541 				      0x0012, 0x0000,
542 				      &status, 1, LINE6_TIMEOUT * HZ);
543 
544 		if (ret < 0) {
545 			dev_err(line6->ifcdev,
546 				"receiving status failed (error %d)\n", ret);
547 			return ret;
548 		}
549 	} while (status == 0xff);
550 
551 	if (status != 0) {
552 		dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
553 		return -EINVAL;
554 	}
555 
556 	return 0;
557 }
558 
559 /*
560 	Read Line6 device serial number.
561 	(POD, TonePort, GuitarPort)
562 */
line6_read_serial_number(struct usb_line6 * line6,int * serial_number)563 int line6_read_serial_number(struct usb_line6 *line6, int *serial_number)
564 {
565 	return line6_read_data(line6, 0x80d0, serial_number,
566 			       sizeof(*serial_number));
567 }
568 
569 /*
570 	No operation (i.e., unsupported).
571 */
line6_nop_read(struct device * dev,struct device_attribute * attr,char * buf)572 ssize_t line6_nop_read(struct device *dev, struct device_attribute *attr,
573 		       char *buf)
574 {
575 	return 0;
576 }
577 
578 /*
579 	Generic destructor.
580 */
line6_destruct(struct usb_interface * interface)581 static void line6_destruct(struct usb_interface *interface)
582 {
583 	struct usb_line6 *line6;
584 
585 	if (interface == NULL)
586 		return;
587 	line6 = usb_get_intfdata(interface);
588 	if (line6 == NULL)
589 		return;
590 
591 	/* free buffer memory first: */
592 	kfree(line6->buffer_message);
593 	kfree(line6->buffer_listen);
594 
595 	/* then free URBs: */
596 	usb_free_urb(line6->urb_listen);
597 
598 	/* make sure the device isn't destructed twice: */
599 	usb_set_intfdata(interface, NULL);
600 
601 	/* free interface data: */
602 	kfree(line6);
603 }
604 
605 /*
606 	Probe USB device.
607 */
line6_probe(struct usb_interface * interface,const struct usb_device_id * id)608 static int line6_probe(struct usb_interface *interface,
609 		       const struct usb_device_id *id)
610 {
611 	int devtype;
612 	struct usb_device *usbdev;
613 	struct usb_line6 *line6;
614 	const struct line6_properties *properties;
615 	int interface_number, alternate = 0;
616 	int product;
617 	int size = 0;
618 	int ep_read = 0, ep_write = 0;
619 	int ret;
620 
621 	if (interface == NULL)
622 		return -ENODEV;
623 	usbdev = interface_to_usbdev(interface);
624 	if (usbdev == NULL)
625 		return -ENODEV;
626 
627 	/* we don't handle multiple configurations */
628 	if (usbdev->descriptor.bNumConfigurations != 1) {
629 		ret = -ENODEV;
630 		goto err_put;
631 	}
632 
633 	/* check vendor and product id */
634 	for (devtype = ARRAY_SIZE(line6_id_table) - 1; devtype--;) {
635 		u16 idVendor = le16_to_cpu(usbdev->descriptor.idVendor);
636 		u16 idProduct = le16_to_cpu(usbdev->descriptor.idProduct);
637 
638 		if (idVendor == line6_id_table[devtype].idVendor &&
639 		    idProduct == line6_id_table[devtype].idProduct)
640 			break;
641 	}
642 
643 	if (devtype < 0) {
644 		ret = -ENODEV;
645 		goto err_put;
646 	}
647 
648 	/* initialize device info: */
649 	properties = &line6_properties_table[devtype];
650 	dev_info(&interface->dev, "Line6 %s found\n", properties->name);
651 	product = le16_to_cpu(usbdev->descriptor.idProduct);
652 
653 	/* query interface number */
654 	interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
655 
656 	switch (product) {
657 	case LINE6_DEVID_BASSPODXTLIVE:
658 	case LINE6_DEVID_PODXTLIVE:
659 	case LINE6_DEVID_VARIAX:
660 		alternate = 1;
661 		break;
662 
663 	case LINE6_DEVID_POCKETPOD:
664 		switch (interface_number) {
665 		case 0:
666 			return -ENODEV;	/* this interface has no endpoints */
667 		case 1:
668 			alternate = 0;
669 			break;
670 		default:
671 			MISSING_CASE;
672 		}
673 		break;
674 
675 	case LINE6_DEVID_PODHD500:
676 	case LINE6_DEVID_PODX3:
677 	case LINE6_DEVID_PODX3LIVE:
678 		switch (interface_number) {
679 		case 0:
680 			alternate = 1;
681 			break;
682 		case 1:
683 			alternate = 0;
684 			break;
685 		default:
686 			MISSING_CASE;
687 		}
688 		break;
689 
690 	case LINE6_DEVID_BASSPODXT:
691 	case LINE6_DEVID_BASSPODXTPRO:
692 	case LINE6_DEVID_PODXT:
693 	case LINE6_DEVID_PODXTPRO:
694 	case LINE6_DEVID_PODHD300:
695 	case LINE6_DEVID_PODHD400:
696 		alternate = 5;
697 		break;
698 
699 	case LINE6_DEVID_GUITARPORT:
700 	case LINE6_DEVID_PODSTUDIO_GX:
701 	case LINE6_DEVID_PODSTUDIO_UX1:
702 	case LINE6_DEVID_TONEPORT_GX:
703 	case LINE6_DEVID_TONEPORT_UX1:
704 		alternate = 2;	/* 1..4 seem to be ok */
705 		break;
706 
707 	case LINE6_DEVID_TONEPORT_UX2:
708 	case LINE6_DEVID_PODSTUDIO_UX2:
709 		switch (interface_number) {
710 		case 0:
711 			/* defaults to 44.1kHz, 16-bit */
712 			alternate = 2;
713 			break;
714 		case 1:
715 			/* don't know yet what this is ...
716 			   alternate = 1;
717 			   break;
718 			 */
719 			return -ENODEV;
720 		default:
721 			MISSING_CASE;
722 		}
723 		break;
724 
725 	default:
726 		MISSING_CASE;
727 		ret = -ENODEV;
728 		goto err_put;
729 	}
730 
731 	ret = usb_set_interface(usbdev, interface_number, alternate);
732 	if (ret < 0) {
733 		dev_err(&interface->dev, "set_interface failed\n");
734 		goto err_put;
735 	}
736 
737 	/* initialize device data based on product id: */
738 	switch (product) {
739 	case LINE6_DEVID_BASSPODXT:
740 	case LINE6_DEVID_BASSPODXTLIVE:
741 	case LINE6_DEVID_BASSPODXTPRO:
742 	case LINE6_DEVID_PODXT:
743 	case LINE6_DEVID_PODXTPRO:
744 		size = sizeof(struct usb_line6_pod);
745 		ep_read = 0x84;
746 		ep_write = 0x03;
747 		break;
748 
749 	case LINE6_DEVID_PODHD300:
750 	case LINE6_DEVID_PODHD400:
751 		size = sizeof(struct usb_line6_podhd);
752 		ep_read = 0x84;
753 		ep_write = 0x03;
754 		break;
755 
756 	case LINE6_DEVID_PODHD500:
757 		size = sizeof(struct usb_line6_podhd);
758 		ep_read = 0x81;
759 		ep_write = 0x01;
760 		break;
761 
762 	case LINE6_DEVID_POCKETPOD:
763 		size = sizeof(struct usb_line6_pod);
764 		ep_read = 0x82;
765 		ep_write = 0x02;
766 		break;
767 
768 	case LINE6_DEVID_PODX3:
769 	case LINE6_DEVID_PODX3LIVE:
770 		/* currently unused! */
771 		size = sizeof(struct usb_line6_pod);
772 		ep_read = 0x81;
773 		ep_write = 0x01;
774 		break;
775 
776 	case LINE6_DEVID_PODSTUDIO_GX:
777 	case LINE6_DEVID_PODSTUDIO_UX1:
778 	case LINE6_DEVID_PODSTUDIO_UX2:
779 	case LINE6_DEVID_TONEPORT_GX:
780 	case LINE6_DEVID_TONEPORT_UX1:
781 	case LINE6_DEVID_TONEPORT_UX2:
782 	case LINE6_DEVID_GUITARPORT:
783 		size = sizeof(struct usb_line6_toneport);
784 		/* these don't have a control channel */
785 		break;
786 
787 	case LINE6_DEVID_PODXTLIVE:
788 		switch (interface_number) {
789 		case PODXTLIVE_INTERFACE_POD:
790 			size = sizeof(struct usb_line6_pod);
791 			ep_read = 0x84;
792 			ep_write = 0x03;
793 			break;
794 
795 		case PODXTLIVE_INTERFACE_VARIAX:
796 			size = sizeof(struct usb_line6_variax);
797 			ep_read = 0x86;
798 			ep_write = 0x05;
799 			break;
800 
801 		default:
802 			ret = -ENODEV;
803 			goto err_put;
804 		}
805 		break;
806 
807 	case LINE6_DEVID_VARIAX:
808 		size = sizeof(struct usb_line6_variax);
809 		ep_read = 0x82;
810 		ep_write = 0x01;
811 		break;
812 
813 	default:
814 		MISSING_CASE;
815 		ret = -ENODEV;
816 		goto err_put;
817 	}
818 
819 	if (size == 0) {
820 		dev_err(&interface->dev,
821 			"driver bug: interface data size not set\n");
822 		ret = -ENODEV;
823 		goto err_put;
824 	}
825 
826 	line6 = kzalloc(size, GFP_KERNEL);
827 	if (line6 == NULL) {
828 		ret = -ENODEV;
829 		goto err_put;
830 	}
831 
832 	/* store basic data: */
833 	line6->interface_number = interface_number;
834 	line6->properties = properties;
835 	line6->usbdev = usbdev;
836 	line6->ifcdev = &interface->dev;
837 	line6->ep_control_read = ep_read;
838 	line6->ep_control_write = ep_write;
839 	line6->product = product;
840 
841 	/* get data from endpoint descriptor (see usb_maxpacket): */
842 	{
843 		struct usb_host_endpoint *ep;
844 		unsigned epnum =
845 		    usb_pipeendpoint(usb_rcvintpipe(usbdev, ep_read));
846 		ep = usbdev->ep_in[epnum];
847 
848 		if (ep != NULL) {
849 			line6->interval = ep->desc.bInterval;
850 			line6->max_packet_size =
851 			    le16_to_cpu(ep->desc.wMaxPacketSize);
852 		} else {
853 			line6->interval = LINE6_FALLBACK_INTERVAL;
854 			line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
855 			dev_err(line6->ifcdev,
856 				"endpoint not available, using fallback values");
857 		}
858 	}
859 
860 	usb_set_intfdata(interface, line6);
861 
862 	if (properties->capabilities & LINE6_BIT_CONTROL) {
863 		/* initialize USB buffers: */
864 		line6->buffer_listen =
865 		    kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
866 		if (line6->buffer_listen == NULL) {
867 			ret = -ENOMEM;
868 			goto err_destruct;
869 		}
870 
871 		line6->buffer_message =
872 		    kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
873 		if (line6->buffer_message == NULL) {
874 			ret = -ENOMEM;
875 			goto err_destruct;
876 		}
877 
878 		line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
879 
880 		if (line6->urb_listen == NULL) {
881 			dev_err(&interface->dev, "Out of memory\n");
882 			line6_destruct(interface);
883 			ret = -ENOMEM;
884 			goto err_destruct;
885 		}
886 
887 		ret = line6_start_listen(line6);
888 		if (ret < 0) {
889 			dev_err(&interface->dev, "%s: usb_submit_urb failed\n",
890 				__func__);
891 			goto err_destruct;
892 		}
893 	}
894 
895 	/* initialize device data based on product id: */
896 	switch (product) {
897 	case LINE6_DEVID_BASSPODXT:
898 	case LINE6_DEVID_BASSPODXTLIVE:
899 	case LINE6_DEVID_BASSPODXTPRO:
900 	case LINE6_DEVID_POCKETPOD:
901 	case LINE6_DEVID_PODX3:
902 	case LINE6_DEVID_PODX3LIVE:
903 	case LINE6_DEVID_PODXT:
904 	case LINE6_DEVID_PODXTPRO:
905 		ret = line6_pod_init(interface, (struct usb_line6_pod *)line6);
906 		break;
907 
908 	case LINE6_DEVID_PODHD300:
909 	case LINE6_DEVID_PODHD400:
910 	case LINE6_DEVID_PODHD500:
911 		ret = line6_podhd_init(interface,
912 				       (struct usb_line6_podhd *)line6);
913 		break;
914 
915 	case LINE6_DEVID_PODXTLIVE:
916 		switch (interface_number) {
917 		case PODXTLIVE_INTERFACE_POD:
918 			ret =
919 			    line6_pod_init(interface,
920 					   (struct usb_line6_pod *)line6);
921 			break;
922 
923 		case PODXTLIVE_INTERFACE_VARIAX:
924 			ret =
925 			    line6_variax_init(interface,
926 					      (struct usb_line6_variax *)line6);
927 			break;
928 
929 		default:
930 			dev_err(&interface->dev,
931 				"PODxt Live interface %d not supported\n",
932 				interface_number);
933 			ret = -ENODEV;
934 		}
935 
936 		break;
937 
938 	case LINE6_DEVID_VARIAX:
939 		ret =
940 		    line6_variax_init(interface,
941 				      (struct usb_line6_variax *)line6);
942 		break;
943 
944 	case LINE6_DEVID_PODSTUDIO_GX:
945 	case LINE6_DEVID_PODSTUDIO_UX1:
946 	case LINE6_DEVID_PODSTUDIO_UX2:
947 	case LINE6_DEVID_TONEPORT_GX:
948 	case LINE6_DEVID_TONEPORT_UX1:
949 	case LINE6_DEVID_TONEPORT_UX2:
950 	case LINE6_DEVID_GUITARPORT:
951 		ret =
952 		    line6_toneport_init(interface,
953 					(struct usb_line6_toneport *)line6);
954 		break;
955 
956 	default:
957 		MISSING_CASE;
958 		ret = -ENODEV;
959 	}
960 
961 	if (ret < 0)
962 		goto err_destruct;
963 
964 	ret = sysfs_create_link(&interface->dev.kobj, &usbdev->dev.kobj,
965 				"usb_device");
966 	if (ret < 0)
967 		goto err_destruct;
968 
969 	/* creation of additional special files should go here */
970 
971 	dev_info(&interface->dev, "Line6 %s now attached\n",
972 		 line6->properties->name);
973 
974 	switch (product) {
975 	case LINE6_DEVID_PODX3:
976 	case LINE6_DEVID_PODX3LIVE:
977 		dev_info(&interface->dev,
978 			 "NOTE: the Line6 %s is detected, but not yet supported\n",
979 			 line6->properties->name);
980 	}
981 
982 	/* increment reference counters: */
983 	usb_get_intf(interface);
984 	usb_get_dev(usbdev);
985 
986 	return 0;
987 
988 err_destruct:
989 	line6_destruct(interface);
990 err_put:
991 	return ret;
992 }
993 
994 /*
995 	Line6 device disconnected.
996 */
line6_disconnect(struct usb_interface * interface)997 static void line6_disconnect(struct usb_interface *interface)
998 {
999 	struct usb_line6 *line6;
1000 	struct usb_device *usbdev;
1001 	int interface_number;
1002 
1003 	if (interface == NULL)
1004 		return;
1005 	usbdev = interface_to_usbdev(interface);
1006 	if (usbdev == NULL)
1007 		return;
1008 
1009 	/* removal of additional special files should go here */
1010 
1011 	sysfs_remove_link(&interface->dev.kobj, "usb_device");
1012 
1013 	interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
1014 	line6 = usb_get_intfdata(interface);
1015 
1016 	if (line6 != NULL) {
1017 		if (line6->urb_listen != NULL)
1018 			line6_stop_listen(line6);
1019 
1020 		if (usbdev != line6->usbdev)
1021 			dev_err(line6->ifcdev,
1022 				"driver bug: inconsistent usb device\n");
1023 
1024 		switch (le16_to_cpu(line6->usbdev->descriptor.idProduct)) {
1025 		case LINE6_DEVID_BASSPODXT:
1026 		case LINE6_DEVID_BASSPODXTLIVE:
1027 		case LINE6_DEVID_BASSPODXTPRO:
1028 		case LINE6_DEVID_POCKETPOD:
1029 		case LINE6_DEVID_PODX3:
1030 		case LINE6_DEVID_PODX3LIVE:
1031 		case LINE6_DEVID_PODXT:
1032 		case LINE6_DEVID_PODXTPRO:
1033 			line6_pod_disconnect(interface);
1034 			break;
1035 
1036 		case LINE6_DEVID_PODHD300:
1037 		case LINE6_DEVID_PODHD400:
1038 		case LINE6_DEVID_PODHD500:
1039 			line6_podhd_disconnect(interface);
1040 			break;
1041 
1042 		case LINE6_DEVID_PODXTLIVE:
1043 			switch (interface_number) {
1044 			case PODXTLIVE_INTERFACE_POD:
1045 				line6_pod_disconnect(interface);
1046 				break;
1047 
1048 			case PODXTLIVE_INTERFACE_VARIAX:
1049 				line6_variax_disconnect(interface);
1050 				break;
1051 			}
1052 
1053 			break;
1054 
1055 		case LINE6_DEVID_VARIAX:
1056 			line6_variax_disconnect(interface);
1057 			break;
1058 
1059 		case LINE6_DEVID_PODSTUDIO_GX:
1060 		case LINE6_DEVID_PODSTUDIO_UX1:
1061 		case LINE6_DEVID_PODSTUDIO_UX2:
1062 		case LINE6_DEVID_TONEPORT_GX:
1063 		case LINE6_DEVID_TONEPORT_UX1:
1064 		case LINE6_DEVID_TONEPORT_UX2:
1065 		case LINE6_DEVID_GUITARPORT:
1066 			line6_toneport_disconnect(interface);
1067 			break;
1068 
1069 		default:
1070 			MISSING_CASE;
1071 		}
1072 
1073 		dev_info(&interface->dev, "Line6 %s now disconnected\n",
1074 			 line6->properties->name);
1075 	}
1076 
1077 	line6_destruct(interface);
1078 
1079 	/* decrement reference counters: */
1080 	usb_put_intf(interface);
1081 	usb_put_dev(usbdev);
1082 }
1083 
1084 #ifdef CONFIG_PM
1085 
1086 /*
1087 	Suspend Line6 device.
1088 */
line6_suspend(struct usb_interface * interface,pm_message_t message)1089 static int line6_suspend(struct usb_interface *interface, pm_message_t message)
1090 {
1091 	struct usb_line6 *line6 = usb_get_intfdata(interface);
1092 	struct snd_line6_pcm *line6pcm = line6->line6pcm;
1093 
1094 	snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
1095 
1096 	if (line6->properties->capabilities & LINE6_BIT_CONTROL)
1097 		line6_stop_listen(line6);
1098 
1099 	if (line6pcm != NULL) {
1100 		snd_pcm_suspend_all(line6pcm->pcm);
1101 		line6_pcm_disconnect(line6pcm);
1102 		line6pcm->flags = 0;
1103 	}
1104 
1105 	return 0;
1106 }
1107 
1108 /*
1109 	Resume Line6 device.
1110 */
line6_resume(struct usb_interface * interface)1111 static int line6_resume(struct usb_interface *interface)
1112 {
1113 	struct usb_line6 *line6 = usb_get_intfdata(interface);
1114 
1115 	if (line6->properties->capabilities & LINE6_BIT_CONTROL)
1116 		line6_start_listen(line6);
1117 
1118 	snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
1119 	return 0;
1120 }
1121 
1122 /*
1123 	Resume Line6 device after reset.
1124 */
line6_reset_resume(struct usb_interface * interface)1125 static int line6_reset_resume(struct usb_interface *interface)
1126 {
1127 	struct usb_line6 *line6 = usb_get_intfdata(interface);
1128 
1129 	switch (le16_to_cpu(line6->usbdev->descriptor.idProduct)) {
1130 	case LINE6_DEVID_PODSTUDIO_GX:
1131 	case LINE6_DEVID_PODSTUDIO_UX1:
1132 	case LINE6_DEVID_PODSTUDIO_UX2:
1133 	case LINE6_DEVID_TONEPORT_GX:
1134 	case LINE6_DEVID_TONEPORT_UX1:
1135 	case LINE6_DEVID_TONEPORT_UX2:
1136 	case LINE6_DEVID_GUITARPORT:
1137 		line6_toneport_reset_resume((struct usb_line6_toneport *)line6);
1138 	}
1139 
1140 	return line6_resume(interface);
1141 }
1142 
1143 #endif /* CONFIG_PM */
1144 
1145 static struct usb_driver line6_driver = {
1146 	.name = DRIVER_NAME,
1147 	.probe = line6_probe,
1148 	.disconnect = line6_disconnect,
1149 #ifdef CONFIG_PM
1150 	.suspend = line6_suspend,
1151 	.resume = line6_resume,
1152 	.reset_resume = line6_reset_resume,
1153 #endif
1154 	.id_table = line6_id_table,
1155 };
1156 
1157 module_usb_driver(line6_driver);
1158 
1159 MODULE_AUTHOR(DRIVER_AUTHOR);
1160 MODULE_DESCRIPTION(DRIVER_DESC);
1161 MODULE_LICENSE("GPL");
1162 MODULE_VERSION(DRIVER_VERSION);
1163