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