driver.c 25.0 KB
Newer Older
M
Markus Grabner 已提交
1
/*
2
 * Line6 Linux USB driver - 0.9.1beta
M
Markus Grabner 已提交
3
 *
4
 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
M
Markus Grabner 已提交
5 6 7 8 9 10 11 12 13
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License as
 *	published by the Free Software Foundation, version 2.
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
14
#include <linux/slab.h>
M
Markus Grabner 已提交
15 16 17 18
#include <linux/usb.h>

#include "audio.h"
#include "capture.h"
19
#include "driver.h"
M
Markus Grabner 已提交
20 21 22
#include "midi.h"
#include "playback.h"
#include "pod.h"
23
#include "podhd.h"
M
Markus Grabner 已提交
24 25 26 27 28 29 30
#include "revision.h"
#include "toneport.h"
#include "usbdefs.h"
#include "variax.h"

#define DRIVER_AUTHOR  "Markus Grabner <grabner@icg.tugraz.at>"
#define DRIVER_DESC    "Line6 USB Driver"
31
#define DRIVER_VERSION "0.9.1beta" DRIVER_REVISION
M
Markus Grabner 已提交
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

M
Markus Grabner 已提交
36
/* table of devices that work with this driver */
37
static const struct usb_device_id line6_id_table[] = {
38 39 40 41
	{ LINE6_DEVICE(0x4250),    .driver_info = LINE6_BASSPODXT },
	{ LINE6_DEVICE(0x4642),    .driver_info = LINE6_BASSPODXTLIVE },
	{ LINE6_DEVICE(0x4252),    .driver_info = LINE6_BASSPODXTPRO },
	{ LINE6_DEVICE(0x4750),    .driver_info = LINE6_GUITARPORT },
42
	{ LINE6_IF_NUM(0x5051, 1), .driver_info = LINE6_POCKETPOD },
43 44
	{ LINE6_DEVICE(0x5057),    .driver_info = LINE6_PODHD300 },
	{ LINE6_DEVICE(0x5058),    .driver_info = LINE6_PODHD400 },
45 46
	{ LINE6_IF_NUM(0x414D, 0), .driver_info = LINE6_PODHD500_0 },
	{ LINE6_IF_NUM(0x414D, 1), .driver_info = LINE6_PODHD500_1 },
47 48
	{ LINE6_DEVICE(0x4153),    .driver_info = LINE6_PODSTUDIO_GX },
	{ 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 52
	{ LINE6_IF_NUM(0x4650, 0), .driver_info = LINE6_PODXTLIVE_POD },
	{ LINE6_IF_NUM(0x4650, 1), .driver_info = LINE6_PODXTLIVE_VARIAX },
53 54 55
	{ LINE6_DEVICE(0x5050),    .driver_info = LINE6_PODXTPRO },
	{ LINE6_DEVICE(0x4147),    .driver_info = LINE6_TONEPORT_GX },
	{ 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
	{}
M
Markus Grabner 已提交
59
};
60

61
MODULE_DEVICE_TABLE(usb, line6_id_table);
M
Markus Grabner 已提交
62

D
Davide Berardi 已提交
63
static const struct line6_properties line6_properties_table[] = {
64 65 66
	[LINE6_BASSPODXT] = {
		.id = "BassPODxt",
		.name = "BassPODxt",
67 68 69
		.capabilities	= LINE6_CAP_CONTROL
				| LINE6_CAP_PCM
				| LINE6_CAP_HWMON,
70
		.altsetting = 5,
71 72
		.ep_ctrl_r = 0x84,
		.ep_ctrl_w = 0x03,
73 74
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
75 76 77 78
	},
	[LINE6_BASSPODXTLIVE] = {
		.id = "BassPODxtLive",
		.name = "BassPODxt Live",
79 80 81
		.capabilities	= LINE6_CAP_CONTROL
				| LINE6_CAP_PCM
				| LINE6_CAP_HWMON,
82
		.altsetting = 1,
83 84
		.ep_ctrl_r = 0x84,
		.ep_ctrl_w = 0x03,
85 86
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
87 88 89 90
	},
	[LINE6_BASSPODXTPRO] = {
		.id = "BassPODxtPro",
		.name = "BassPODxt Pro",
91 92 93
		.capabilities	= LINE6_CAP_CONTROL
				| LINE6_CAP_PCM
				| LINE6_CAP_HWMON,
94
		.altsetting = 5,
95 96
		.ep_ctrl_r = 0x84,
		.ep_ctrl_w = 0x03,
97 98
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
99 100 101 102
	},
	[LINE6_GUITARPORT] = {
		.id = "GuitarPort",
		.name = "GuitarPort",
103
		.capabilities	= LINE6_CAP_PCM,
104
		.altsetting = 2,  /* 1..4 seem to be ok */
105
		/* no control channel */
106 107
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
108 109 110 111
	},
	[LINE6_POCKETPOD] = {
		.id = "PocketPOD",
		.name = "Pocket POD",
112
		.capabilities	= LINE6_CAP_CONTROL,
113
		.altsetting = 0,
114 115
		.ep_ctrl_r = 0x82,
		.ep_ctrl_w = 0x02,
116
		/* no audio channel */
117 118 119 120
	},
	[LINE6_PODHD300] = {
		.id = "PODHD300",
		.name = "POD HD300",
121 122 123
		.capabilities	= LINE6_CAP_CONTROL
				| LINE6_CAP_PCM
				| LINE6_CAP_HWMON,
124
		.altsetting = 5,
125 126
		.ep_ctrl_r = 0x84,
		.ep_ctrl_w = 0x03,
127 128
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
129 130 131 132
	},
	[LINE6_PODHD400] = {
		.id = "PODHD400",
		.name = "POD HD400",
133 134 135
		.capabilities	= LINE6_CAP_CONTROL
				| LINE6_CAP_PCM
				| LINE6_CAP_HWMON,
136
		.altsetting = 5,
137 138
		.ep_ctrl_r = 0x84,
		.ep_ctrl_w = 0x03,
139 140
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
141
	},
142 143 144 145 146 147
	[LINE6_PODHD500_0] = {
		.id = "PODHD500",
		.name = "POD HD500",
		.capabilities	= LINE6_CAP_CONTROL
				| LINE6_CAP_PCM
				| LINE6_CAP_HWMON,
148
		.altsetting = 1,
149 150
		.ep_ctrl_r = 0x81,
		.ep_ctrl_w = 0x01,
151 152
		.ep_audio_r = 0x86,
		.ep_audio_w = 0x02,
153 154
	},
	[LINE6_PODHD500_1] = {
155 156
		.id = "PODHD500",
		.name = "POD HD500",
157 158 159
		.capabilities	= LINE6_CAP_CONTROL
				| LINE6_CAP_PCM
				| LINE6_CAP_HWMON,
160
		.altsetting = 1,
161 162
		.ep_ctrl_r = 0x81,
		.ep_ctrl_w = 0x01,
163 164
		.ep_audio_r = 0x86,
		.ep_audio_w = 0x02,
165 166 167 168
	},
	[LINE6_PODSTUDIO_GX] = {
		.id = "PODStudioGX",
		.name = "POD Studio GX",
169
		.capabilities	= LINE6_CAP_PCM,
170
		.altsetting = 2,  /* 1..4 seem to be ok */
171
		/* no control channel */
172 173
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
174 175 176 177
	},
	[LINE6_PODSTUDIO_UX1] = {
		.id = "PODStudioUX1",
		.name = "POD Studio UX1",
178
		.capabilities	= LINE6_CAP_PCM,
179
		.altsetting = 2,  /* 1..4 seem to be ok */
180
		/* no control channel */
181 182
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
183 184 185 186
	},
	[LINE6_PODSTUDIO_UX2] = {
		.id = "PODStudioUX2",
		.name = "POD Studio UX2",
187
		.capabilities	= LINE6_CAP_PCM,
188
		.altsetting = 2,  /* defaults to 44.1kHz, 16-bit */
189
		/* no control channel */
190 191
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
192 193 194 195
	},
	[LINE6_PODXT] = {
		.id = "PODxt",
		.name = "PODxt",
196 197 198
		.capabilities	= LINE6_CAP_CONTROL
				| LINE6_CAP_PCM
				| LINE6_CAP_HWMON,
199
		.altsetting = 5,
200 201
		.ep_ctrl_r = 0x84,
		.ep_ctrl_w = 0x03,
202 203
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
204
	},
205 206 207 208 209 210
	[LINE6_PODXTLIVE_POD] = {
		.id = "PODxtLive",
		.name = "PODxt Live",
		.capabilities	= LINE6_CAP_CONTROL
				| LINE6_CAP_PCM
				| LINE6_CAP_HWMON,
211
		.altsetting = 1,
212 213
		.ep_ctrl_r = 0x84,
		.ep_ctrl_w = 0x03,
214 215
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
216 217
	},
	[LINE6_PODXTLIVE_VARIAX] = {
218 219
		.id = "PODxtLive",
		.name = "PODxt Live",
220 221 222
		.capabilities	= LINE6_CAP_CONTROL
				| LINE6_CAP_PCM
				| LINE6_CAP_HWMON,
223
		.altsetting = 1,
224 225
		.ep_ctrl_r = 0x86,
		.ep_ctrl_w = 0x05,
226 227
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
228 229 230 231
	},
	[LINE6_PODXTPRO] = {
		.id = "PODxtPro",
		.name = "PODxt Pro",
232 233 234
		.capabilities	= LINE6_CAP_CONTROL
				| LINE6_CAP_PCM
				| LINE6_CAP_HWMON,
235
		.altsetting = 5,
236 237
		.ep_ctrl_r = 0x84,
		.ep_ctrl_w = 0x03,
238 239
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
240 241 242 243
	},
	[LINE6_TONEPORT_GX] = {
		.id = "TonePortGX",
		.name = "TonePort GX",
244
		.capabilities	= LINE6_CAP_PCM,
245
		.altsetting = 2,  /* 1..4 seem to be ok */
246
		/* no control channel */
247 248
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
249 250 251 252
	},
	[LINE6_TONEPORT_UX1] = {
		.id = "TonePortUX1",
		.name = "TonePort UX1",
253
		.capabilities	= LINE6_CAP_PCM,
254
		.altsetting = 2,  /* 1..4 seem to be ok */
255
		/* no control channel */
256 257
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
258 259 260 261
	},
	[LINE6_TONEPORT_UX2] = {
		.id = "TonePortUX2",
		.name = "TonePort UX2",
262
		.capabilities	= LINE6_CAP_PCM,
263
		.altsetting = 2,  /* defaults to 44.1kHz, 16-bit */
264
		/* no control channel */
265 266
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
267 268 269 270
	},
	[LINE6_VARIAX] = {
		.id = "Variax",
		.name = "Variax Workbench",
271
		.capabilities	= LINE6_CAP_CONTROL,
272
		.altsetting = 1,
273 274
		.ep_ctrl_r = 0x82,
		.ep_ctrl_w = 0x01,
275
		/* no audio channel */
276
	}
M
Markus Grabner 已提交
277 278 279 280 281
};

/*
	This is Line6's MIDI manufacturer ID.
*/
282 283 284 285 286 287 288 289
const unsigned char line6_midi_id[] = {
	0x00, 0x01, 0x0c
};

/*
	Code to request version of POD, Variax interface
	(and maybe other devices).
*/
290
static const char line6_request_version[] = {
291 292 293
	0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
};

M
Markus Grabner 已提交
294 295 296
/**
	 Class for asynchronous messages.
*/
297
struct message {
M
Markus Grabner 已提交
298 299 300 301 302 303 304 305 306
	struct usb_line6 *line6;
	const char *buffer;
	int size;
	int done;
};

/*
	Forward declarations.
*/
307
static void line6_data_received(struct urb *urb);
308 309
static int line6_send_raw_message_async_part(struct message *msg,
					     struct urb *urb);
M
Markus Grabner 已提交
310 311 312 313 314 315

/*
	Start to listen on endpoint.
*/
static int line6_start_listen(struct usb_line6 *line6)
{
316
	int err;
317

318
	usb_fill_int_urb(line6->urb_listen, line6->usbdev,
319 320 321
		usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
		line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
		line6_data_received, line6, line6->interval);
M
Markus Grabner 已提交
322
	line6->urb_listen->actual_length = 0;
323
	err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
324 325 326 327 328 329 330 331 332
	return err;
}

/*
	Stop listening on endpoint.
*/
static void line6_stop_listen(struct usb_line6 *line6)
{
	usb_kill_urb(line6->urb_listen);
M
Markus Grabner 已提交
333 334 335
}

/*
336
	Send raw message in pieces of wMaxPacketSize bytes.
M
Markus Grabner 已提交
337
*/
338 339
int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
			   int size)
M
Markus Grabner 已提交
340 341 342
{
	int i, done = 0;

343 344
	for (i = 0; i < size; i += line6->max_packet_size) {
		int partial;
M
Markus Grabner 已提交
345 346
		const char *frag_buf = buffer + i;
		int frag_size = min(line6->max_packet_size, size - i);
347 348 349
		int retval;

		retval = usb_interrupt_msg(line6->usbdev,
D
Davide Berardi 已提交
350
					usb_sndintpipe(line6->usbdev,
351
						line6->properties->ep_ctrl_w),
D
Davide Berardi 已提交
352 353
					(char *)frag_buf, frag_size,
					&partial, LINE6_TIMEOUT * HZ);
M
Markus Grabner 已提交
354

355 356 357
		if (retval) {
			dev_err(line6->ifcdev,
				"usb_interrupt_msg failed (%d)\n", retval);
M
Markus Grabner 已提交
358 359 360
			break;
		}

361
		done += frag_size;
M
Markus Grabner 已提交
362 363 364 365 366 367 368 369
	}

	return done;
}

/*
	Notification of completion of asynchronous request transmission.
*/
370
static void line6_async_request_sent(struct urb *urb)
M
Markus Grabner 已提交
371 372 373
{
	struct message *msg = (struct message *)urb->context;

374
	if (msg->done >= msg->size) {
M
Markus Grabner 已提交
375 376
		usb_free_urb(urb);
		kfree(msg);
377
	} else
M
Markus Grabner 已提交
378 379 380 381 382 383
		line6_send_raw_message_async_part(msg, urb);
}

/*
	Asynchronously send part of a raw message.
*/
384 385
static int line6_send_raw_message_async_part(struct message *msg,
					     struct urb *urb)
M
Markus Grabner 已提交
386 387 388 389 390 391
{
	int retval;
	struct usb_line6 *line6 = msg->line6;
	int done = msg->done;
	int bytes = min(msg->size - done, line6->max_packet_size);

392
	usb_fill_int_urb(urb, line6->usbdev,
393 394 395
		usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
		(char *)msg->buffer + done, bytes,
		line6_async_request_sent, msg, line6->interval);
M
Markus Grabner 已提交
396 397 398 399

	msg->done += bytes;
	retval = usb_submit_urb(urb, GFP_ATOMIC);

400 401 402
	if (retval < 0) {
		dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
			__func__, retval);
M
Markus Grabner 已提交
403 404
		usb_free_urb(urb);
		kfree(msg);
405
		return retval;
M
Markus Grabner 已提交
406 407 408 409 410
	}

	return 0;
}

411 412 413 414
/*
	Setup and start timer.
*/
void line6_start_timer(struct timer_list *timer, unsigned int msecs,
415
		       void (*function)(unsigned long), unsigned long data)
416 417 418 419 420 421
{
	setup_timer(timer, function, data);
	timer->expires = jiffies + msecs * HZ / 1000;
	add_timer(timer);
}

M
Markus Grabner 已提交
422 423 424
/*
	Asynchronously send raw message.
*/
425 426
int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
				 int size)
M
Markus Grabner 已提交
427 428 429 430 431 432
{
	struct message *msg;
	struct urb *urb;

	/* create message: */
	msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
433
	if (msg == NULL)
M
Markus Grabner 已提交
434 435 436 437 438
		return -ENOMEM;

	/* create URB: */
	urb = usb_alloc_urb(0, GFP_ATOMIC);

439
	if (urb == NULL) {
M
Markus Grabner 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
		kfree(msg);
		dev_err(line6->ifcdev, "Out of memory\n");
		return -ENOMEM;
	}

	/* set message data: */
	msg->line6 = line6;
	msg->buffer = buffer;
	msg->size = size;
	msg->done = 0;

	/* start sending: */
	return line6_send_raw_message_async_part(msg, urb);
}

455 456 457 458 459
/*
	Send asynchronous device version request.
*/
int line6_version_request_async(struct usb_line6 *line6)
{
460 461 462
	char *buffer;
	int retval;

463 464
	buffer = kmemdup(line6_request_version,
			sizeof(line6_request_version), GFP_ATOMIC);
465 466 467 468 469 470 471 472 473
	if (buffer == NULL) {
		dev_err(line6->ifcdev, "Out of memory");
		return -ENOMEM;
	}

	retval = line6_send_raw_message_async(line6, buffer,
					      sizeof(line6_request_version));
	kfree(buffer);
	return retval;
474 475
}

M
Markus Grabner 已提交
476 477 478
/*
	Send sysex message in pieces of wMaxPacketSize bytes.
*/
479 480
int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
			     int size)
M
Markus Grabner 已提交
481
{
482 483 484
	return line6_send_raw_message(line6, buffer,
				      size + SYSEX_EXTRA_SIZE) -
	    SYSEX_EXTRA_SIZE;
M
Markus Grabner 已提交
485 486 487 488 489 490 491
}

/*
	Allocate buffer for sysex message and prepare header.
	@param code sysex message code
	@param size number of bytes between code and sysex end
*/
492 493
char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
			       int size)
M
Markus Grabner 已提交
494
{
495
	char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
M
Markus Grabner 已提交
496

497
	if (!buffer)
498
		return NULL;
M
Markus Grabner 已提交
499 500 501 502 503 504 505 506 507 508 509 510

	buffer[0] = LINE6_SYSEX_BEGIN;
	memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
	buffer[sizeof(line6_midi_id) + 1] = code1;
	buffer[sizeof(line6_midi_id) + 2] = code2;
	buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
	return buffer;
}

/*
	Notification of data received from the Line6 device.
*/
511
static void line6_data_received(struct urb *urb)
M
Markus Grabner 已提交
512 513
{
	struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
514
	struct midi_buffer *mb = &line6->line6midi->midibuf_in;
M
Markus Grabner 已提交
515 516
	int done;

517
	if (urb->status == -ESHUTDOWN)
M
Markus Grabner 已提交
518 519
		return;

520 521
	done =
	    line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
M
Markus Grabner 已提交
522

523
	if (done < urb->actual_length) {
524
		line6_midibuf_ignore(mb, done);
525 526
		dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
			done, urb->actual_length);
M
Markus Grabner 已提交
527 528
	}

529
	for (;;) {
530 531 532
		done =
		    line6_midibuf_read(mb, line6->buffer_message,
				       LINE6_MESSAGE_MAXLEN);
M
Markus Grabner 已提交
533

534
		if (done == 0)
M
Markus Grabner 已提交
535 536 537 538 539
			break;

		line6->message_length = done;
		line6_midi_receive(line6, line6->buffer_message, done);

540 541
		if (line6->process_message)
			line6->process_message(line6);
M
Markus Grabner 已提交
542 543 544 545 546 547 548 549
	}

	line6_start_listen(line6);
}

/*
	Send channel number (i.e., switch to a different sound).
*/
550
int line6_send_program(struct usb_line6 *line6, u8 value)
M
Markus Grabner 已提交
551
{
552
	int retval;
M
Markus Grabner 已提交
553
	unsigned char *buffer;
554 555 556
	int partial;

	buffer = kmalloc(2, GFP_KERNEL);
557
	if (!buffer)
M
Markus Grabner 已提交
558 559 560 561 562
		return -ENOMEM;

	buffer[0] = LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST;
	buffer[1] = value;

563 564
	retval = usb_interrupt_msg(line6->usbdev,
				   usb_sndintpipe(line6->usbdev,
565
						  line6->properties->ep_ctrl_w),
566 567 568
				   buffer, 2, &partial, LINE6_TIMEOUT * HZ);

	if (retval)
569 570
		dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
			retval);
571 572 573

	kfree(buffer);
	return retval;
M
Markus Grabner 已提交
574 575 576 577 578
}

/*
	Transmit Line6 control parameter.
*/
579
int line6_transmit_parameter(struct usb_line6 *line6, int param, u8 value)
M
Markus Grabner 已提交
580
{
581
	int retval;
M
Markus Grabner 已提交
582
	unsigned char *buffer;
583 584 585
	int partial;

	buffer = kmalloc(3, GFP_KERNEL);
586
	if (!buffer)
M
Markus Grabner 已提交
587 588 589 590 591 592
		return -ENOMEM;

	buffer[0] = LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST;
	buffer[1] = param;
	buffer[2] = value;

593
	retval = usb_interrupt_msg(line6->usbdev,
594
				   usb_sndintpipe(line6->usbdev,
595
						  line6->properties->ep_ctrl_w),
596 597 598
				   buffer, 3, &partial, LINE6_TIMEOUT * HZ);

	if (retval)
599 600
		dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
			retval);
601 602 603

	kfree(buffer);
	return retval;
M
Markus Grabner 已提交
604 605 606 607 608
}

/*
	Read data from device.
*/
609 610
int line6_read_data(struct usb_line6 *line6, int address, void *data,
		    size_t datalen)
M
Markus Grabner 已提交
611 612 613 614 615 616 617
{
	struct usb_device *usbdev = line6->usbdev;
	int ret;
	unsigned char len;

	/* query the serial number: */
	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
618 619 620
			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
			      (datalen << 8) | 0x21, address,
			      NULL, 0, LINE6_TIMEOUT * HZ);
M
Markus Grabner 已提交
621

622
	if (ret < 0) {
M
Markus Grabner 已提交
623 624 625 626
		dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
		return ret;
	}

627
	/* Wait for data length. We'll get 0xff until length arrives. */
M
Markus Grabner 已提交
628 629
	do {
		ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
630 631 632 633 634 635 636
				      USB_TYPE_VENDOR | USB_RECIP_DEVICE |
				      USB_DIR_IN,
				      0x0012, 0x0000, &len, 1,
				      LINE6_TIMEOUT * HZ);
		if (ret < 0) {
			dev_err(line6->ifcdev,
				"receive length failed (error %d)\n", ret);
M
Markus Grabner 已提交
637 638
			return ret;
		}
639
	} while (len == 0xff);
640 641 642 643 644 645

	if (len != datalen) {
		/* should be equal or something went wrong */
		dev_err(line6->ifcdev,
			"length mismatch (expected %d, got %d)\n",
			(int)datalen, (int)len);
M
Markus Grabner 已提交
646 647 648 649 650
		return -EINVAL;
	}

	/* receive the result: */
	ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
651 652 653
			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
			      0x0013, 0x0000, data, datalen,
			      LINE6_TIMEOUT * HZ);
M
Markus Grabner 已提交
654

655
	if (ret < 0) {
M
Markus Grabner 已提交
656 657 658 659 660 661 662 663 664 665
		dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
		return ret;
	}

	return 0;
}

/*
	Write data to device.
*/
666 667
int line6_write_data(struct usb_line6 *line6, int address, void *data,
		     size_t datalen)
M
Markus Grabner 已提交
668 669 670 671 672
{
	struct usb_device *usbdev = line6->usbdev;
	int ret;
	unsigned char status;

673 674 675 676
	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
			      0x0022, address, data, datalen,
			      LINE6_TIMEOUT * HZ);
M
Markus Grabner 已提交
677

678 679 680
	if (ret < 0) {
		dev_err(line6->ifcdev,
			"write request failed (error %d)\n", ret);
M
Markus Grabner 已提交
681 682 683 684
		return ret;
	}

	do {
685 686 687 688 689 690 691 692 693 694
		ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
				      0x67,
				      USB_TYPE_VENDOR | USB_RECIP_DEVICE |
				      USB_DIR_IN,
				      0x0012, 0x0000,
				      &status, 1, LINE6_TIMEOUT * HZ);

		if (ret < 0) {
			dev_err(line6->ifcdev,
				"receiving status failed (error %d)\n", ret);
M
Markus Grabner 已提交
695 696
			return ret;
		}
697
	} while (status == 0xff);
M
Markus Grabner 已提交
698

699
	if (status != 0) {
M
Markus Grabner 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712
		dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
		return -EINVAL;
	}

	return 0;
}

/*
	Read Line6 device serial number.
	(POD, TonePort, GuitarPort)
*/
int line6_read_serial_number(struct usb_line6 *line6, int *serial_number)
{
713 714
	return line6_read_data(line6, 0x80d0, serial_number,
			       sizeof(*serial_number));
M
Markus Grabner 已提交
715 716 717 718 719
}

/*
	No operation (i.e., unsupported).
*/
720 721
ssize_t line6_nop_read(struct device *dev, struct device_attribute *attr,
		       char *buf)
M
Markus Grabner 已提交
722 723 724 725 726 727 728 729 730 731
{
	return 0;
}

/*
	Generic destructor.
*/
static void line6_destruct(struct usb_interface *interface)
{
	struct usb_line6 *line6;
732 733 734

	if (interface == NULL)
		return;
M
Markus Grabner 已提交
735
	line6 = usb_get_intfdata(interface);
736 737
	if (line6 == NULL)
		return;
M
Markus Grabner 已提交
738 739

	/* free buffer memory first: */
740 741
	kfree(line6->buffer_message);
	kfree(line6->buffer_listen);
M
Markus Grabner 已提交
742 743

	/* then free URBs: */
744
	usb_free_urb(line6->urb_listen);
M
Markus Grabner 已提交
745 746 747 748 749 750 751 752 753 754 755

	/* make sure the device isn't destructed twice: */
	usb_set_intfdata(interface, NULL);

	/* free interface data: */
	kfree(line6);
}

/*
	Probe USB device.
*/
756 757
static int line6_probe(struct usb_interface *interface,
		       const struct usb_device_id *id)
M
Markus Grabner 已提交
758
{
759
	enum line6_device_type devtype;
760 761
	struct usb_device *usbdev;
	struct usb_line6 *line6;
M
Markus Grabner 已提交
762
	const struct line6_properties *properties;
763
	int interface_number;
M
Markus Grabner 已提交
764 765 766
	int size = 0;
	int ret;

767 768
	if (interface == NULL)
		return -ENODEV;
M
Markus Grabner 已提交
769
	usbdev = interface_to_usbdev(interface);
770 771
	if (usbdev == NULL)
		return -ENODEV;
M
Markus Grabner 已提交
772 773

	/* we don't handle multiple configurations */
774 775 776 777
	if (usbdev->descriptor.bNumConfigurations != 1) {
		ret = -ENODEV;
		goto err_put;
	}
M
Markus Grabner 已提交
778

779
	devtype = id->driver_info;
M
Markus Grabner 已提交
780 781 782 783 784 785 786 787

	/* initialize device info: */
	properties = &line6_properties_table[devtype];
	dev_info(&interface->dev, "Line6 %s found\n", properties->name);

	/* query interface number */
	interface_number = interface->cur_altsetting->desc.bInterfaceNumber;

788 789
	ret = usb_set_interface(usbdev, interface_number,
			properties->altsetting);
790
	if (ret < 0) {
M
Markus Grabner 已提交
791
		dev_err(&interface->dev, "set_interface failed\n");
792
		goto err_put;
M
Markus Grabner 已提交
793 794
	}

795 796 797 798 799 800 801
	/* initialize device data based on device: */
	switch (devtype) {
	case LINE6_BASSPODXT:
	case LINE6_BASSPODXTLIVE:
	case LINE6_BASSPODXTPRO:
	case LINE6_PODXT:
	case LINE6_PODXTPRO:
M
Markus Grabner 已提交
802 803 804
		size = sizeof(struct usb_line6_pod);
		break;

805 806
	case LINE6_PODHD300:
	case LINE6_PODHD400:
807 808 809
		size = sizeof(struct usb_line6_podhd);
		break;

810 811
	case LINE6_PODHD500_0:
	case LINE6_PODHD500_1:
812 813 814
		size = sizeof(struct usb_line6_podhd);
		break;

815
	case LINE6_POCKETPOD:
816 817 818
		size = sizeof(struct usb_line6_pod);
		break;

819 820 821 822 823 824 825
	case LINE6_PODSTUDIO_GX:
	case LINE6_PODSTUDIO_UX1:
	case LINE6_PODSTUDIO_UX2:
	case LINE6_TONEPORT_GX:
	case LINE6_TONEPORT_UX1:
	case LINE6_TONEPORT_UX2:
	case LINE6_GUITARPORT:
M
Markus Grabner 已提交
826 827 828
		size = sizeof(struct usb_line6_toneport);
		break;

829 830 831
	case LINE6_PODXTLIVE_POD:
		size = sizeof(struct usb_line6_pod);
		break;
M
Markus Grabner 已提交
832

833 834
	case LINE6_PODXTLIVE_VARIAX:
		size = sizeof(struct usb_line6_variax);
M
Markus Grabner 已提交
835 836
		break;

837
	case LINE6_VARIAX:
M
Markus Grabner 已提交
838 839 840 841 842
		size = sizeof(struct usb_line6_variax);
		break;

	default:
		MISSING_CASE;
843 844
		ret = -ENODEV;
		goto err_put;
M
Markus Grabner 已提交
845 846
	}

847
	if (size == 0) {
848
		dev_err(&interface->dev,
849
			"driver bug: interface data size not set\n");
850 851
		ret = -ENODEV;
		goto err_put;
M
Markus Grabner 已提交
852 853 854
	}

	line6 = kzalloc(size, GFP_KERNEL);
855
	if (line6 == NULL) {
856 857
		ret = -ENODEV;
		goto err_put;
M
Markus Grabner 已提交
858 859 860 861 862 863
	}

	/* store basic data: */
	line6->properties = properties;
	line6->usbdev = usbdev;
	line6->ifcdev = &interface->dev;
864
	line6->type = devtype;
M
Markus Grabner 已提交
865 866 867 868

	/* get data from endpoint descriptor (see usb_maxpacket): */
	{
		struct usb_host_endpoint *ep;
869 870
		unsigned pipe = usb_rcvintpipe(usbdev, properties->ep_ctrl_r);
		unsigned epnum = usb_pipeendpoint(pipe);
M
Markus Grabner 已提交
871 872
		ep = usbdev->ep_in[epnum];

873
		if (ep != NULL) {
M
Markus Grabner 已提交
874
			line6->interval = ep->desc.bInterval;
875 876
			line6->max_packet_size =
			    le16_to_cpu(ep->desc.wMaxPacketSize);
877
		} else {
M
Markus Grabner 已提交
878 879
			line6->interval = LINE6_FALLBACK_INTERVAL;
			line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
880 881
			dev_err(line6->ifcdev,
				"endpoint not available, using fallback values");
M
Markus Grabner 已提交
882 883 884 885 886
		}
	}

	usb_set_intfdata(interface, line6);

887
	if (properties->capabilities & LINE6_CAP_CONTROL) {
M
Markus Grabner 已提交
888
		/* initialize USB buffers: */
889 890
		line6->buffer_listen =
		    kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
891
		if (line6->buffer_listen == NULL) {
892 893
			ret = -ENOMEM;
			goto err_destruct;
M
Markus Grabner 已提交
894 895
		}

896 897
		line6->buffer_message =
		    kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
898
		if (line6->buffer_message == NULL) {
899 900
			ret = -ENOMEM;
			goto err_destruct;
M
Markus Grabner 已提交
901 902 903 904
		}

		line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);

905
		if (line6->urb_listen == NULL) {
M
Markus Grabner 已提交
906 907
			dev_err(&interface->dev, "Out of memory\n");
			line6_destruct(interface);
908 909
			ret = -ENOMEM;
			goto err_destruct;
M
Markus Grabner 已提交
910 911
		}

912 913 914 915
		ret = line6_start_listen(line6);
		if (ret < 0) {
			dev_err(&interface->dev, "%s: usb_submit_urb failed\n",
				__func__);
916
			goto err_destruct;
M
Markus Grabner 已提交
917 918 919
		}
	}

920 921 922 923 924 925 926 927
	/* initialize device data based on device: */
	switch (devtype) {
	case LINE6_BASSPODXT:
	case LINE6_BASSPODXTLIVE:
	case LINE6_BASSPODXTPRO:
	case LINE6_POCKETPOD:
	case LINE6_PODXT:
	case LINE6_PODXTPRO:
928
		ret = line6_pod_init(interface, line6);
M
Markus Grabner 已提交
929 930
		break;

931 932
	case LINE6_PODHD300:
	case LINE6_PODHD400:
933 934
	case LINE6_PODHD500_0:
	case LINE6_PODHD500_1:
935
		ret = line6_podhd_init(interface, line6);
936 937
		break;

938
	case LINE6_PODXTLIVE_POD:
939
		ret = line6_pod_init(interface, line6);
940
		break;
M
Markus Grabner 已提交
941

942
	case LINE6_PODXTLIVE_VARIAX:
943
		ret = line6_variax_init(interface, line6);
M
Markus Grabner 已提交
944 945
		break;

946
	case LINE6_VARIAX:
947
		ret = line6_variax_init(interface, line6);
M
Markus Grabner 已提交
948 949
		break;

950 951 952 953 954 955 956
	case LINE6_PODSTUDIO_GX:
	case LINE6_PODSTUDIO_UX1:
	case LINE6_PODSTUDIO_UX2:
	case LINE6_TONEPORT_GX:
	case LINE6_TONEPORT_UX1:
	case LINE6_TONEPORT_UX2:
	case LINE6_GUITARPORT:
957
		ret = line6_toneport_init(interface, line6);
M
Markus Grabner 已提交
958 959 960 961 962 963 964
		break;

	default:
		MISSING_CASE;
		ret = -ENODEV;
	}

965 966
	if (ret < 0)
		goto err_destruct;
M
Markus Grabner 已提交
967

968 969
	ret = sysfs_create_link(&interface->dev.kobj, &usbdev->dev.kobj,
				"usb_device");
970 971
	if (ret < 0)
		goto err_destruct;
M
Markus Grabner 已提交
972

973 974
	/* creation of additional special files should go here */

975 976
	dev_info(&interface->dev, "Line6 %s now attached\n",
		 line6->properties->name);
977 978 979 980 981

	/* increment reference counters: */
	usb_get_intf(interface);
	usb_get_dev(usbdev);

982 983 984 985 986
	return 0;

err_destruct:
	line6_destruct(interface);
err_put:
M
Markus Grabner 已提交
987 988 989 990 991 992 993 994 995 996
	return ret;
}

/*
	Line6 device disconnected.
*/
static void line6_disconnect(struct usb_interface *interface)
{
	struct usb_line6 *line6;
	struct usb_device *usbdev;
997
	int interface_number;
M
Markus Grabner 已提交
998

999 1000
	if (interface == NULL)
		return;
M
Markus Grabner 已提交
1001
	usbdev = interface_to_usbdev(interface);
1002 1003
	if (usbdev == NULL)
		return;
M
Markus Grabner 已提交
1004

1005 1006
	/* removal of additional special files should go here */

M
Markus Grabner 已提交
1007 1008 1009 1010 1011
	sysfs_remove_link(&interface->dev.kobj, "usb_device");

	interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
	line6 = usb_get_intfdata(interface);

1012 1013
	if (line6 != NULL) {
		if (line6->urb_listen != NULL)
1014
			line6_stop_listen(line6);
M
Markus Grabner 已提交
1015

1016 1017 1018
		if (usbdev != line6->usbdev)
			dev_err(line6->ifcdev,
				"driver bug: inconsistent usb device\n");
M
Markus Grabner 已提交
1019

1020
		line6->disconnect(interface);
M
Markus Grabner 已提交
1021

1022 1023
		dev_info(&interface->dev, "Line6 %s now disconnected\n",
			 line6->properties->name);
M
Markus Grabner 已提交
1024 1025 1026 1027 1028 1029 1030
	}

	line6_destruct(interface);

	/* decrement reference counters: */
	usb_put_intf(interface);
	usb_put_dev(usbdev);
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
}

#ifdef CONFIG_PM

/*
	Suspend Line6 device.
*/
static int line6_suspend(struct usb_interface *interface, pm_message_t message)
{
	struct usb_line6 *line6 = usb_get_intfdata(interface);
	struct snd_line6_pcm *line6pcm = line6->line6pcm;

	snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);

1045
	if (line6->properties->capabilities & LINE6_CAP_CONTROL)
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
		line6_stop_listen(line6);

	if (line6pcm != NULL) {
		snd_pcm_suspend_all(line6pcm->pcm);
		line6_pcm_disconnect(line6pcm);
		line6pcm->flags = 0;
	}

	return 0;
}

/*
	Resume Line6 device.
*/
static int line6_resume(struct usb_interface *interface)
{
	struct usb_line6 *line6 = usb_get_intfdata(interface);

1064
	if (line6->properties->capabilities & LINE6_CAP_CONTROL)
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
		line6_start_listen(line6);

	snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
	return 0;
}

/*
	Resume Line6 device after reset.
*/
static int line6_reset_resume(struct usb_interface *interface)
{
	struct usb_line6 *line6 = usb_get_intfdata(interface);
M
Markus Grabner 已提交
1077

1078 1079 1080 1081 1082 1083 1084 1085
	switch (line6->type) {
	case LINE6_PODSTUDIO_GX:
	case LINE6_PODSTUDIO_UX1:
	case LINE6_PODSTUDIO_UX2:
	case LINE6_TONEPORT_GX:
	case LINE6_TONEPORT_UX1:
	case LINE6_TONEPORT_UX2:
	case LINE6_GUITARPORT:
1086
		line6_toneport_reset_resume((struct usb_line6_toneport *)line6);
1087 1088 1089

	default:
		break;
1090 1091 1092
	}

	return line6_resume(interface);
M
Markus Grabner 已提交
1093 1094
}

1095
#endif /* CONFIG_PM */
1096

M
Markus Grabner 已提交
1097 1098 1099 1100
static struct usb_driver line6_driver = {
	.name = DRIVER_NAME,
	.probe = line6_probe,
	.disconnect = line6_disconnect,
1101 1102 1103 1104 1105
#ifdef CONFIG_PM
	.suspend = line6_suspend,
	.resume = line6_resume,
	.reset_resume = line6_reset_resume,
#endif
M
Markus Grabner 已提交
1106 1107 1108
	.id_table = line6_id_table,
};

1109
module_usb_driver(line6_driver);
M
Markus Grabner 已提交
1110 1111 1112 1113 1114

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
MODULE_VERSION(DRIVER_VERSION);