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