Linux Audio

Check our new training course

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