driver.c 21.1 KB
Newer Older
M
Markus Grabner 已提交
1
/*
2
 * Line 6 Linux USB driver
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>
T
Takashi Iwai 已提交
14
#include <linux/export.h>
15
#include <linux/slab.h>
M
Markus Grabner 已提交
16 17
#include <linux/usb.h>

18 19
#include <sound/core.h>
#include <sound/initval.h>
20
#include <sound/hwdep.h>
21

M
Markus Grabner 已提交
22
#include "capture.h"
23
#include "driver.h"
M
Markus Grabner 已提交
24 25 26 27
#include "midi.h"
#include "playback.h"

#define DRIVER_AUTHOR  "Markus Grabner <grabner@icg.tugraz.at>"
28
#define DRIVER_DESC    "Line 6 USB Driver"
M
Markus Grabner 已提交
29 30

/*
31
	This is Line 6's MIDI manufacturer ID.
M
Markus Grabner 已提交
32
*/
33
const unsigned char line6_midi_id[3] = {
34 35
	0x00, 0x01, 0x0c
};
T
Takashi Iwai 已提交
36
EXPORT_SYMBOL_GPL(line6_midi_id);
37 38 39 40 41

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

46
/*
M
Markus Grabner 已提交
47 48
	 Class for asynchronous messages.
*/
49
struct message {
M
Markus Grabner 已提交
50 51 52 53 54 55 56 57 58
	struct usb_line6 *line6;
	const char *buffer;
	int size;
	int done;
};

/*
	Forward declarations.
*/
59
static void line6_data_received(struct urb *urb);
60 61
static int line6_send_raw_message_async_part(struct message *msg,
					     struct urb *urb);
M
Markus Grabner 已提交
62 63 64 65 66 67

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

70 71 72 73 74 75 76 77 78 79 80
	if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
		usb_fill_int_urb(line6->urb_listen, line6->usbdev,
			usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
			line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
			line6_data_received, line6, line6->interval);
	} else {
		usb_fill_bulk_urb(line6->urb_listen, line6->usbdev,
			usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r),
			line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
			line6_data_received, line6);
	}
81 82 83 84 85 86 87

	/* sanity checks of EP before actually submitting */
	if (usb_urb_ep_type_check(line6->urb_listen)) {
		dev_err(line6->ifcdev, "invalid control EP\n");
		return -EINVAL;
	}

M
Markus Grabner 已提交
88
	line6->urb_listen->actual_length = 0;
89
	err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
90 91 92 93 94 95 96 97 98
	return err;
}

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

/*
102
	Send raw message in pieces of wMaxPacketSize bytes.
M
Markus Grabner 已提交
103
*/
104 105
static int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
				  int size)
M
Markus Grabner 已提交
106 107
{
	int i, done = 0;
108
	const struct line6_properties *properties = line6->properties;
M
Markus Grabner 已提交
109

110 111
	for (i = 0; i < size; i += line6->max_packet_size) {
		int partial;
M
Markus Grabner 已提交
112 113
		const char *frag_buf = buffer + i;
		int frag_size = min(line6->max_packet_size, size - i);
114 115
		int retval;

116 117 118 119 120 121 122 123 124 125 126
		if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
			retval = usb_interrupt_msg(line6->usbdev,
						usb_sndintpipe(line6->usbdev, properties->ep_ctrl_w),
						(char *)frag_buf, frag_size,
						&partial, LINE6_TIMEOUT * HZ);
		} else {
			retval = usb_bulk_msg(line6->usbdev,
						usb_sndbulkpipe(line6->usbdev, properties->ep_ctrl_w),
						(char *)frag_buf, frag_size,
						&partial, LINE6_TIMEOUT * HZ);
		}
M
Markus Grabner 已提交
127

128 129
		if (retval) {
			dev_err(line6->ifcdev,
130
				"usb_bulk_msg failed (%d)\n", retval);
M
Markus Grabner 已提交
131 132 133
			break;
		}

134
		done += frag_size;
M
Markus Grabner 已提交
135 136 137 138 139 140 141 142
	}

	return done;
}

/*
	Notification of completion of asynchronous request transmission.
*/
143
static void line6_async_request_sent(struct urb *urb)
M
Markus Grabner 已提交
144 145 146
{
	struct message *msg = (struct message *)urb->context;

147
	if (msg->done >= msg->size) {
M
Markus Grabner 已提交
148 149
		usb_free_urb(urb);
		kfree(msg);
150
	} else
M
Markus Grabner 已提交
151 152 153 154 155 156
		line6_send_raw_message_async_part(msg, urb);
}

/*
	Asynchronously send part of a raw message.
*/
157 158
static int line6_send_raw_message_async_part(struct message *msg,
					     struct urb *urb)
M
Markus Grabner 已提交
159 160 161 162 163 164
{
	int retval;
	struct usb_line6 *line6 = msg->line6;
	int done = msg->done;
	int bytes = min(msg->size - done, line6->max_packet_size);

165 166 167 168 169 170 171 172 173 174 175
	if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
		usb_fill_int_urb(urb, line6->usbdev,
			usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
			(char *)msg->buffer + done, bytes,
			line6_async_request_sent, msg, line6->interval);
	} else {
		usb_fill_bulk_urb(urb, line6->usbdev,
			usb_sndbulkpipe(line6->usbdev, line6->properties->ep_ctrl_w),
			(char *)msg->buffer + done, bytes,
			line6_async_request_sent, msg);
	}
M
Markus Grabner 已提交
176 177 178

	msg->done += bytes;

179 180 181 182 183 184 185 186
	/* sanity checks of EP before actually submitting */
	retval = usb_urb_ep_type_check(urb);
	if (retval < 0)
		goto error;

	retval = usb_submit_urb(urb, GFP_ATOMIC);
	if (retval < 0)
		goto error;
M
Markus Grabner 已提交
187 188

	return 0;
189 190 191 192 193 194 195

 error:
	dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
		__func__, retval);
	usb_free_urb(urb);
	kfree(msg);
	return retval;
M
Markus Grabner 已提交
196 197
}

198 199 200
/*
	Setup and start timer.
*/
201
void line6_start_timer(struct timer_list *timer, unsigned long msecs,
202
		       void (*function)(struct timer_list *t))
203
{
204
	timer->function = function;
205
	mod_timer(timer, jiffies + msecs_to_jiffies(msecs));
206
}
T
Takashi Iwai 已提交
207
EXPORT_SYMBOL_GPL(line6_start_timer);
208

M
Markus Grabner 已提交
209 210 211
/*
	Asynchronously send raw message.
*/
212 213
int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
				 int size)
M
Markus Grabner 已提交
214 215 216 217 218 219
{
	struct message *msg;
	struct urb *urb;

	/* create message: */
	msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
220
	if (msg == NULL)
M
Markus Grabner 已提交
221 222 223 224 225
		return -ENOMEM;

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

226
	if (urb == NULL) {
M
Markus Grabner 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239
		kfree(msg);
		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);
}
T
Takashi Iwai 已提交
240
EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
M
Markus Grabner 已提交
241

242 243 244 245 246
/*
	Send asynchronous device version request.
*/
int line6_version_request_async(struct usb_line6 *line6)
{
247 248 249
	char *buffer;
	int retval;

250 251
	buffer = kmemdup(line6_request_version,
			sizeof(line6_request_version), GFP_ATOMIC);
252
	if (buffer == NULL)
253 254 255 256 257 258
		return -ENOMEM;

	retval = line6_send_raw_message_async(line6, buffer,
					      sizeof(line6_request_version));
	kfree(buffer);
	return retval;
259
}
T
Takashi Iwai 已提交
260
EXPORT_SYMBOL_GPL(line6_version_request_async);
261

M
Markus Grabner 已提交
262 263 264
/*
	Send sysex message in pieces of wMaxPacketSize bytes.
*/
265 266
int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
			     int size)
M
Markus Grabner 已提交
267
{
268 269 270
	return line6_send_raw_message(line6, buffer,
				      size + SYSEX_EXTRA_SIZE) -
	    SYSEX_EXTRA_SIZE;
M
Markus Grabner 已提交
271
}
T
Takashi Iwai 已提交
272
EXPORT_SYMBOL_GPL(line6_send_sysex_message);
M
Markus Grabner 已提交
273 274 275 276 277 278

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

284
	if (!buffer)
285
		return NULL;
M
Markus Grabner 已提交
286 287 288 289 290 291 292 293

	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;
}
T
Takashi Iwai 已提交
294
EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
M
Markus Grabner 已提交
295 296

/*
297
	Notification of data received from the Line 6 device.
M
Markus Grabner 已提交
298
*/
299
static void line6_data_received(struct urb *urb)
M
Markus Grabner 已提交
300 301
{
	struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
302
	struct midi_buffer *mb = &line6->line6midi->midibuf_in;
M
Markus Grabner 已提交
303 304
	int done;

305
	if (urb->status == -ESHUTDOWN)
M
Markus Grabner 已提交
306 307
		return;

308 309 310
	if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
		done =
			line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
M
Markus Grabner 已提交
311

312 313 314 315 316
		if (done < urb->actual_length) {
			line6_midibuf_ignore(mb, done);
			dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
				done, urb->actual_length);
		}
M
Markus Grabner 已提交
317

318 319 320
		for (;;) {
			done =
				line6_midibuf_read(mb, line6->buffer_message,
321
						LINE6_MIDI_MESSAGE_MAXLEN);
M
Markus Grabner 已提交
322

323 324
			if (done == 0)
				break;
M
Markus Grabner 已提交
325

326 327
			line6->message_length = done;
			line6_midi_receive(line6, line6->buffer_message, done);
M
Markus Grabner 已提交
328

329 330 331 332
			if (line6->process_message)
				line6->process_message(line6);
		}
	} else {
333 334
		line6->buffer_message = urb->transfer_buffer;
		line6->message_length = urb->actual_length;
335 336
		if (line6->process_message)
			line6->process_message(line6);
337
		line6->buffer_message = NULL;
M
Markus Grabner 已提交
338 339 340 341 342
	}

	line6_start_listen(line6);
}

343
#define LINE6_READ_WRITE_STATUS_DELAY 2  /* milliseconds */
344
#define LINE6_READ_WRITE_MAX_RETRIES 50
345

M
Markus Grabner 已提交
346 347 348
/*
	Read data from device.
*/
349 350
int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
		    unsigned datalen)
M
Markus Grabner 已提交
351 352 353
{
	struct usb_device *usbdev = line6->usbdev;
	int ret;
354
	unsigned char *len;
355
	unsigned count;
M
Markus Grabner 已提交
356

357 358 359
	if (address > 0xffff || datalen > 0xff)
		return -EINVAL;

360 361 362 363
	len = kmalloc(sizeof(*len), GFP_KERNEL);
	if (!len)
		return -ENOMEM;

M
Markus Grabner 已提交
364 365
	/* query the serial number: */
	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
366 367 368
			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
			      (datalen << 8) | 0x21, address,
			      NULL, 0, LINE6_TIMEOUT * HZ);
M
Markus Grabner 已提交
369

370
	if (ret < 0) {
M
Markus Grabner 已提交
371
		dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
372
		goto exit;
M
Markus Grabner 已提交
373 374
	}

375
	/* Wait for data length. We'll get 0xff until length arrives. */
376
	for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
377 378
		mdelay(LINE6_READ_WRITE_STATUS_DELAY);

M
Markus Grabner 已提交
379
		ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
380 381
				      USB_TYPE_VENDOR | USB_RECIP_DEVICE |
				      USB_DIR_IN,
382
				      0x0012, 0x0000, len, 1,
383 384 385 386
				      LINE6_TIMEOUT * HZ);
		if (ret < 0) {
			dev_err(line6->ifcdev,
				"receive length failed (error %d)\n", ret);
387
			goto exit;
M
Markus Grabner 已提交
388
		}
389

390
		if (*len != 0xff)
391 392 393
			break;
	}

394 395
	ret = -EIO;
	if (*len == 0xff) {
396 397
		dev_err(line6->ifcdev, "read failed after %d retries\n",
			count);
398 399
		goto exit;
	} else if (*len != datalen) {
400 401 402
		/* should be equal or something went wrong */
		dev_err(line6->ifcdev,
			"length mismatch (expected %d, got %d)\n",
403 404
			(int)datalen, (int)*len);
		goto exit;
M
Markus Grabner 已提交
405 406 407 408
	}

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

413
	if (ret < 0)
M
Markus Grabner 已提交
414 415
		dev_err(line6->ifcdev, "read failed (error %d)\n", ret);

416 417 418
exit:
	kfree(len);
	return ret;
M
Markus Grabner 已提交
419
}
T
Takashi Iwai 已提交
420
EXPORT_SYMBOL_GPL(line6_read_data);
M
Markus Grabner 已提交
421 422 423 424

/*
	Write data to device.
*/
425 426
int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
		     unsigned datalen)
M
Markus Grabner 已提交
427 428 429
{
	struct usb_device *usbdev = line6->usbdev;
	int ret;
430
	unsigned char *status;
431
	int count;
M
Markus Grabner 已提交
432

433 434 435
	if (address > 0xffff || datalen > 0xffff)
		return -EINVAL;

436 437 438 439
	status = kmalloc(sizeof(*status), GFP_KERNEL);
	if (!status)
		return -ENOMEM;

440 441 442 443
	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 已提交
444

445 446 447
	if (ret < 0) {
		dev_err(line6->ifcdev,
			"write request failed (error %d)\n", ret);
448
		goto exit;
M
Markus Grabner 已提交
449 450
	}

451
	for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
452 453
		mdelay(LINE6_READ_WRITE_STATUS_DELAY);

454 455 456 457 458
		ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
				      0x67,
				      USB_TYPE_VENDOR | USB_RECIP_DEVICE |
				      USB_DIR_IN,
				      0x0012, 0x0000,
459
				      status, 1, LINE6_TIMEOUT * HZ);
460 461 462 463

		if (ret < 0) {
			dev_err(line6->ifcdev,
				"receiving status failed (error %d)\n", ret);
464
			goto exit;
M
Markus Grabner 已提交
465 466
		}

467
		if (*status != 0xff)
468 469 470
			break;
	}

471
	if (*status == 0xff) {
472 473
		dev_err(line6->ifcdev, "write failed after %d retries\n",
			count);
474 475
		ret = -EIO;
	} else if (*status != 0) {
M
Markus Grabner 已提交
476
		dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
477
		ret = -EIO;
M
Markus Grabner 已提交
478
	}
479 480 481
exit:
	kfree(status);
	return ret;
M
Markus Grabner 已提交
482
}
T
Takashi Iwai 已提交
483
EXPORT_SYMBOL_GPL(line6_write_data);
M
Markus Grabner 已提交
484 485

/*
486
	Read Line 6 device serial number.
M
Markus Grabner 已提交
487 488
	(POD, TonePort, GuitarPort)
*/
489
int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
M
Markus Grabner 已提交
490
{
491 492
	return line6_read_data(line6, 0x80d0, serial_number,
			       sizeof(*serial_number));
M
Markus Grabner 已提交
493
}
T
Takashi Iwai 已提交
494
EXPORT_SYMBOL_GPL(line6_read_serial_number);
M
Markus Grabner 已提交
495 496

/*
497
	Card destructor.
M
Markus Grabner 已提交
498
*/
499
static void line6_destruct(struct snd_card *card)
M
Markus Grabner 已提交
500
{
501
	struct usb_line6 *line6 = card->private_data;
502
	struct usb_device *usbdev = line6->usbdev;
M
Markus Grabner 已提交
503

504 505 506
	/* Free buffer memory first. We cannot depend on the existence of private
	 * data from the (podhd) module, it may be gone already during this call
	 */
507
	kfree(line6->buffer_message);
508

509
	kfree(line6->buffer_listen);
M
Markus Grabner 已提交
510 511

	/* then free URBs: */
512
	usb_free_urb(line6->urb_listen);
513
	line6->urb_listen = NULL;
M
Markus Grabner 已提交
514

515 516
	/* decrement reference counters: */
	usb_put_dev(usbdev);
M
Markus Grabner 已提交
517 518
}

519
static void line6_get_usb_properties(struct usb_line6 *line6)
T
Takashi Iwai 已提交
520 521
{
	struct usb_device *usbdev = line6->usbdev;
522 523
	const struct line6_properties *properties = line6->properties;
	int pipe;
524
	struct usb_host_endpoint *ep = NULL;
525

526 527 528 529 530 531 532 533 534
	if (properties->capabilities & LINE6_CAP_CONTROL) {
		if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
			pipe = usb_rcvintpipe(line6->usbdev,
				line6->properties->ep_ctrl_r);
		} else {
			pipe = usb_rcvbulkpipe(line6->usbdev,
				line6->properties->ep_ctrl_r);
		}
		ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
535
	}
T
Takashi Iwai 已提交
536

537
	/* Control data transfer properties */
T
Takashi Iwai 已提交
538 539 540 541
	if (ep) {
		line6->interval = ep->desc.bInterval;
		line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
	} else {
542 543 544 545
		if (properties->capabilities & LINE6_CAP_CONTROL) {
			dev_err(line6->ifcdev,
				"endpoint not available, using fallback values");
		}
T
Takashi Iwai 已提交
546 547 548 549
		line6->interval = LINE6_FALLBACK_INTERVAL;
		line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
	}

550 551 552 553 554 555 556 557 558
	/* Isochronous transfer properties */
	if (usbdev->speed == USB_SPEED_LOW) {
		line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
		line6->iso_buffers = USB_LOW_ISO_BUFFERS;
	} else {
		line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
		line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
	}
}
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

/* Enable buffering of incoming messages, flush the buffer */
static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
{
	struct usb_line6 *line6 = hw->private_data;

	/* NOTE: hwdep layer provides atomicity here */

	line6->messages.active = 1;

	return 0;
}

/* Stop buffering */
static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
{
	struct usb_line6 *line6 = hw->private_data;

	line6->messages.active = 0;

	return 0;
}

/* Read from circular buffer, return to user */
static long
line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
					loff_t *offset)
{
	struct usb_line6 *line6 = hwdep->private_data;
	long rv = 0;
	unsigned int out_count;

	if (mutex_lock_interruptible(&line6->messages.read_lock))
		return -ERESTARTSYS;

	while (kfifo_len(&line6->messages.fifo) == 0) {
		mutex_unlock(&line6->messages.read_lock);

		rv = wait_event_interruptible(
			line6->messages.wait_queue,
			kfifo_len(&line6->messages.fifo) != 0);
		if (rv < 0)
			return rv;

		if (mutex_lock_interruptible(&line6->messages.read_lock))
			return -ERESTARTSYS;
	}

	if (kfifo_peek_len(&line6->messages.fifo) > count) {
		/* Buffer too small; allow re-read of the current item... */
		rv = -EINVAL;
	} else {
		rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
		if (rv == 0)
			rv = out_count;
	}

	mutex_unlock(&line6->messages.read_lock);
	return rv;
}

/* Write directly (no buffering) to device by user*/
static long
line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
					loff_t *offset)
{
	struct usb_line6 *line6 = hwdep->private_data;
	int rv;
	char *data_copy;

	if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
		/* This is an arbitrary limit - still better than nothing... */
		return -EINVAL;
	}

	data_copy = memdup_user(data, count);
635 636
	if (IS_ERR(data_copy))
		return PTR_ERR(data_copy);
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690

	rv = line6_send_raw_message(line6, data_copy, count);

	kfree(data_copy);
	return rv;
}

static const struct snd_hwdep_ops hwdep_ops = {
	.open    = line6_hwdep_open,
	.release = line6_hwdep_release,
	.read    = line6_hwdep_read,
	.write   = line6_hwdep_write,
};

/* Insert into circular buffer */
static void line6_hwdep_push_message(struct usb_line6 *line6)
{
	if (!line6->messages.active)
		return;

	if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
		/* No race condition here, there's only one writer */
		kfifo_in(&line6->messages.fifo,
			line6->buffer_message, line6->message_length);
	} /* else TODO: signal overflow */

	wake_up_interruptible(&line6->messages.wait_queue);
}

static int line6_hwdep_init(struct usb_line6 *line6)
{
	int err;
	struct snd_hwdep *hwdep;

	/* TODO: usb_driver_claim_interface(); */
	line6->process_message = line6_hwdep_push_message;
	line6->messages.active = 0;
	init_waitqueue_head(&line6->messages.wait_queue);
	mutex_init(&line6->messages.read_lock);
	INIT_KFIFO(line6->messages.fifo);

	err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
	if (err < 0)
		goto end;
	strcpy(hwdep->name, "config");
	hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
	hwdep->ops = hwdep_ops;
	hwdep->private_data = line6;
	hwdep->exclusive = true;

end:
	return err;
}

691
static int line6_init_cap_control(struct usb_line6 *line6)
T
Takashi Iwai 已提交
692 693 694 695 696 697 698 699 700 701 702 703
{
	int ret;

	/* initialize USB buffers: */
	line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
	if (!line6->buffer_listen)
		return -ENOMEM;

	line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
	if (!line6->urb_listen)
		return -ENOMEM;

704
	if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
705
		line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
706 707
		if (!line6->buffer_message)
			return -ENOMEM;
708 709 710 711
	} else {
		ret = line6_hwdep_init(line6);
		if (ret < 0)
			return ret;
712 713
	}

T
Takashi Iwai 已提交
714 715 716 717 718 719 720 721 722
	ret = line6_start_listen(line6);
	if (ret < 0) {
		dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
		return ret;
	}

	return 0;
}

M
Markus Grabner 已提交
723 724 725
/*
	Probe USB device.
*/
T
Takashi Iwai 已提交
726
int line6_probe(struct usb_interface *interface,
727
		const struct usb_device_id *id,
728
		const char *driver_name,
T
Takashi Iwai 已提交
729
		const struct line6_properties *properties,
730 731
		int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
		size_t data_size)
M
Markus Grabner 已提交
732
{
733
	struct usb_device *usbdev = interface_to_usbdev(interface);
734
	struct snd_card *card;
735
	struct usb_line6 *line6;
736
	int interface_number;
M
Markus Grabner 已提交
737 738
	int ret;

739 740 741
	if (WARN_ON(data_size < sizeof(*line6)))
		return -EINVAL;

742 743 744 745
	/* we don't handle multiple configurations */
	if (usbdev->descriptor.bNumConfigurations != 1)
		return -ENODEV;

746 747
	ret = snd_card_new(&interface->dev,
			   SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
748 749
			   THIS_MODULE, data_size, &card);
	if (ret < 0)
750
		return ret;
M
Markus Grabner 已提交
751 752

	/* store basic data: */
753
	line6 = card->private_data;
754
	line6->card = card;
M
Markus Grabner 已提交
755 756 757 758
	line6->properties = properties;
	line6->usbdev = usbdev;
	line6->ifcdev = &interface->dev;

759
	strcpy(card->id, properties->id);
760
	strcpy(card->driver, driver_name);
761 762
	strcpy(card->shortname, properties->name);
	sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
763 764 765
		dev_name(line6->ifcdev));
	card->private_free = line6_destruct;

M
Markus Grabner 已提交
766 767
	usb_set_intfdata(interface, line6);

768 769 770
	/* increment reference counters: */
	usb_get_dev(usbdev);

771 772 773 774 775 776
	/* initialize device info: */
	dev_info(&interface->dev, "Line 6 %s found\n", properties->name);

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

777
	/* TODO reserves the bus bandwidth even without actual transfer */
778
	ret = usb_set_interface(usbdev, interface_number,
779
				properties->altsetting);
780 781 782 783 784
	if (ret < 0) {
		dev_err(&interface->dev, "set_interface failed\n");
		goto error;
	}

785
	line6_get_usb_properties(line6);
786

787 788
	if (properties->capabilities & LINE6_CAP_CONTROL) {
		ret = line6_init_cap_control(line6);
T
Takashi Iwai 已提交
789
		if (ret < 0)
790
			goto error;
M
Markus Grabner 已提交
791 792
	}

793
	/* initialize device data based on device: */
794
	ret = private_init(line6, id);
795
	if (ret < 0)
796
		goto error;
M
Markus Grabner 已提交
797

798 799
	/* creation of additional special files should go here */

800
	dev_info(&interface->dev, "Line 6 %s now attached\n",
801
		 properties->name);
802

803 804
	return 0;

805
 error:
806 807 808 809
	/* we can call disconnect callback here because no close-sync is
	 * needed yet at this point
	 */
	line6_disconnect(interface);
M
Markus Grabner 已提交
810 811
	return ret;
}
T
Takashi Iwai 已提交
812
EXPORT_SYMBOL_GPL(line6_probe);
M
Markus Grabner 已提交
813 814

/*
815
	Line 6 device disconnected.
M
Markus Grabner 已提交
816
*/
T
Takashi Iwai 已提交
817
void line6_disconnect(struct usb_interface *interface)
M
Markus Grabner 已提交
818
{
819 820
	struct usb_line6 *line6 = usb_get_intfdata(interface);
	struct usb_device *usbdev = interface_to_usbdev(interface);
M
Markus Grabner 已提交
821

822 823
	if (!line6)
		return;
M
Markus Grabner 已提交
824

825 826 827
	if (WARN_ON(usbdev != line6->usbdev))
		return;

828 829
	if (line6->urb_listen != NULL)
		line6_stop_listen(line6);
M
Markus Grabner 已提交
830

831
	snd_card_disconnect(line6->card);
832 833
	if (line6->line6pcm)
		line6_pcm_disconnect(line6->line6pcm);
834
	if (line6->disconnect)
835
		line6->disconnect(line6);
M
Markus Grabner 已提交
836

837
	dev_info(&interface->dev, "Line 6 %s now disconnected\n",
838
		 line6->properties->name);
M
Markus Grabner 已提交
839

840 841
	/* make sure the device isn't destructed twice: */
	usb_set_intfdata(interface, NULL);
T
Takashi Iwai 已提交
842

843
	snd_card_free_when_closed(line6->card);
844
}
T
Takashi Iwai 已提交
845
EXPORT_SYMBOL_GPL(line6_disconnect);
846 847 848 849

#ifdef CONFIG_PM

/*
850
	Suspend Line 6 device.
851
*/
T
Takashi Iwai 已提交
852
int line6_suspend(struct usb_interface *interface, pm_message_t message)
853 854 855 856 857 858
{
	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);

859
	if (line6->properties->capabilities & LINE6_CAP_CONTROL)
860 861 862 863 864 865 866 867 868
		line6_stop_listen(line6);

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

	return 0;
}
T
Takashi Iwai 已提交
869
EXPORT_SYMBOL_GPL(line6_suspend);
870 871

/*
872
	Resume Line 6 device.
873
*/
T
Takashi Iwai 已提交
874
int line6_resume(struct usb_interface *interface)
875 876 877
{
	struct usb_line6 *line6 = usb_get_intfdata(interface);

878
	if (line6->properties->capabilities & LINE6_CAP_CONTROL)
879 880 881 882 883
		line6_start_listen(line6);

	snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
	return 0;
}
T
Takashi Iwai 已提交
884
EXPORT_SYMBOL_GPL(line6_resume);
M
Markus Grabner 已提交
885

886
#endif /* CONFIG_PM */
887

M
Markus Grabner 已提交
888 889 890
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
891