driver.c 16.3 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>
T
Takashi Iwai 已提交
14
#include <linux/export.h>
15
#include <linux/slab.h>
M
Markus Grabner 已提交
16 17
#include <linux/usb.h>

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

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

#define DRIVER_AUTHOR  "Markus Grabner <grabner@icg.tugraz.at>"
#define DRIVER_DESC    "Line6 USB Driver"
30
#define DRIVER_VERSION "0.9.1beta" DRIVER_REVISION
M
Markus Grabner 已提交
31 32 33 34

/*
	This is Line6's MIDI manufacturer ID.
*/
35 36 37
const unsigned char line6_midi_id[] = {
	0x00, 0x01, 0x0c
};
T
Takashi Iwai 已提交
38
EXPORT_SYMBOL_GPL(line6_midi_id);
39 40 41 42 43

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

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

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

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

72
	usb_fill_int_urb(line6->urb_listen, line6->usbdev,
73 74 75
		usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
		line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
		line6_data_received, line6, line6->interval);
M
Markus Grabner 已提交
76
	line6->urb_listen->actual_length = 0;
77
	err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
78 79 80 81 82 83 84 85 86
	return err;
}

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

/*
90
	Send raw message in pieces of wMaxPacketSize bytes.
M
Markus Grabner 已提交
91
*/
92 93
int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
			   int size)
M
Markus Grabner 已提交
94 95 96
{
	int i, done = 0;

97 98
	for (i = 0; i < size; i += line6->max_packet_size) {
		int partial;
M
Markus Grabner 已提交
99 100
		const char *frag_buf = buffer + i;
		int frag_size = min(line6->max_packet_size, size - i);
101 102 103
		int retval;

		retval = usb_interrupt_msg(line6->usbdev,
D
Davide Berardi 已提交
104
					usb_sndintpipe(line6->usbdev,
105
						line6->properties->ep_ctrl_w),
D
Davide Berardi 已提交
106 107
					(char *)frag_buf, frag_size,
					&partial, LINE6_TIMEOUT * HZ);
M
Markus Grabner 已提交
108

109 110 111
		if (retval) {
			dev_err(line6->ifcdev,
				"usb_interrupt_msg failed (%d)\n", retval);
M
Markus Grabner 已提交
112 113 114
			break;
		}

115
		done += frag_size;
M
Markus Grabner 已提交
116 117 118 119 120 121 122 123
	}

	return done;
}

/*
	Notification of completion of asynchronous request transmission.
*/
124
static void line6_async_request_sent(struct urb *urb)
M
Markus Grabner 已提交
125 126 127
{
	struct message *msg = (struct message *)urb->context;

128
	if (msg->done >= msg->size) {
M
Markus Grabner 已提交
129 130
		usb_free_urb(urb);
		kfree(msg);
131
	} else
M
Markus Grabner 已提交
132 133 134 135 136 137
		line6_send_raw_message_async_part(msg, urb);
}

/*
	Asynchronously send part of a raw message.
*/
138 139
static int line6_send_raw_message_async_part(struct message *msg,
					     struct urb *urb)
M
Markus Grabner 已提交
140 141 142 143 144 145
{
	int retval;
	struct usb_line6 *line6 = msg->line6;
	int done = msg->done;
	int bytes = min(msg->size - done, line6->max_packet_size);

146
	usb_fill_int_urb(urb, line6->usbdev,
147 148 149
		usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
		(char *)msg->buffer + done, bytes,
		line6_async_request_sent, msg, line6->interval);
M
Markus Grabner 已提交
150 151 152 153

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

154 155 156
	if (retval < 0) {
		dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
			__func__, retval);
M
Markus Grabner 已提交
157 158
		usb_free_urb(urb);
		kfree(msg);
159
		return retval;
M
Markus Grabner 已提交
160 161 162 163 164
	}

	return 0;
}

165 166 167 168
/*
	Setup and start timer.
*/
void line6_start_timer(struct timer_list *timer, unsigned int msecs,
169
		       void (*function)(unsigned long), unsigned long data)
170 171
{
	setup_timer(timer, function, data);
172
	mod_timer(timer, jiffies + msecs * HZ / 1000);
173
}
T
Takashi Iwai 已提交
174
EXPORT_SYMBOL_GPL(line6_start_timer);
175

M
Markus Grabner 已提交
176 177 178
/*
	Asynchronously send raw message.
*/
179 180
int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
				 int size)
M
Markus Grabner 已提交
181 182 183 184 185 186
{
	struct message *msg;
	struct urb *urb;

	/* create message: */
	msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
187
	if (msg == NULL)
M
Markus Grabner 已提交
188 189 190 191 192
		return -ENOMEM;

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

193
	if (urb == NULL) {
M
Markus Grabner 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206
		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 已提交
207
EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
M
Markus Grabner 已提交
208

209 210 211 212 213
/*
	Send asynchronous device version request.
*/
int line6_version_request_async(struct usb_line6 *line6)
{
214 215 216
	char *buffer;
	int retval;

217 218
	buffer = kmemdup(line6_request_version,
			sizeof(line6_request_version), GFP_ATOMIC);
219
	if (buffer == NULL)
220 221 222 223 224 225
		return -ENOMEM;

	retval = line6_send_raw_message_async(line6, buffer,
					      sizeof(line6_request_version));
	kfree(buffer);
	return retval;
226
}
T
Takashi Iwai 已提交
227
EXPORT_SYMBOL_GPL(line6_version_request_async);
228

M
Markus Grabner 已提交
229 230 231
/*
	Send sysex message in pieces of wMaxPacketSize bytes.
*/
232 233
int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
			     int size)
M
Markus Grabner 已提交
234
{
235 236 237
	return line6_send_raw_message(line6, buffer,
				      size + SYSEX_EXTRA_SIZE) -
	    SYSEX_EXTRA_SIZE;
M
Markus Grabner 已提交
238
}
T
Takashi Iwai 已提交
239
EXPORT_SYMBOL_GPL(line6_send_sysex_message);
M
Markus Grabner 已提交
240 241 242 243 244 245

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

251
	if (!buffer)
252
		return NULL;
M
Markus Grabner 已提交
253 254 255 256 257 258 259 260

	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 已提交
261
EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
M
Markus Grabner 已提交
262 263 264 265

/*
	Notification of data received from the Line6 device.
*/
266
static void line6_data_received(struct urb *urb)
M
Markus Grabner 已提交
267 268
{
	struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
269
	struct midi_buffer *mb = &line6->line6midi->midibuf_in;
M
Markus Grabner 已提交
270 271
	int done;

272
	if (urb->status == -ESHUTDOWN)
M
Markus Grabner 已提交
273 274
		return;

275 276
	done =
	    line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
M
Markus Grabner 已提交
277

278
	if (done < urb->actual_length) {
279
		line6_midibuf_ignore(mb, done);
280 281
		dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
			done, urb->actual_length);
M
Markus Grabner 已提交
282 283
	}

284
	for (;;) {
285 286 287
		done =
		    line6_midibuf_read(mb, line6->buffer_message,
				       LINE6_MESSAGE_MAXLEN);
M
Markus Grabner 已提交
288

289
		if (done == 0)
M
Markus Grabner 已提交
290 291 292 293 294
			break;

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

295 296
		if (line6->process_message)
			line6->process_message(line6);
M
Markus Grabner 已提交
297 298 299 300 301 302 303 304
	}

	line6_start_listen(line6);
}

/*
	Send channel number (i.e., switch to a different sound).
*/
305
int line6_send_program(struct usb_line6 *line6, u8 value)
M
Markus Grabner 已提交
306
{
307
	int retval;
M
Markus Grabner 已提交
308
	unsigned char *buffer;
309 310 311
	int partial;

	buffer = kmalloc(2, GFP_KERNEL);
312
	if (!buffer)
M
Markus Grabner 已提交
313 314 315 316 317
		return -ENOMEM;

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

318 319
	retval = usb_interrupt_msg(line6->usbdev,
				   usb_sndintpipe(line6->usbdev,
320
						  line6->properties->ep_ctrl_w),
321 322 323
				   buffer, 2, &partial, LINE6_TIMEOUT * HZ);

	if (retval)
324 325
		dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
			retval);
326 327 328

	kfree(buffer);
	return retval;
M
Markus Grabner 已提交
329 330 331 332 333
}

/*
	Transmit Line6 control parameter.
*/
334
int line6_transmit_parameter(struct usb_line6 *line6, int param, u8 value)
M
Markus Grabner 已提交
335
{
336
	int retval;
M
Markus Grabner 已提交
337
	unsigned char *buffer;
338 339 340
	int partial;

	buffer = kmalloc(3, GFP_KERNEL);
341
	if (!buffer)
M
Markus Grabner 已提交
342 343 344 345 346 347
		return -ENOMEM;

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

348
	retval = usb_interrupt_msg(line6->usbdev,
349
				   usb_sndintpipe(line6->usbdev,
350
						  line6->properties->ep_ctrl_w),
351 352 353
				   buffer, 3, &partial, LINE6_TIMEOUT * HZ);

	if (retval)
354 355
		dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
			retval);
356 357 358

	kfree(buffer);
	return retval;
M
Markus Grabner 已提交
359 360 361 362 363
}

/*
	Read data from device.
*/
364 365
int line6_read_data(struct usb_line6 *line6, int address, void *data,
		    size_t datalen)
M
Markus Grabner 已提交
366 367 368 369 370 371 372
{
	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,
373 374 375
			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
			      (datalen << 8) | 0x21, address,
			      NULL, 0, LINE6_TIMEOUT * HZ);
M
Markus Grabner 已提交
376

377
	if (ret < 0) {
M
Markus Grabner 已提交
378 379 380 381
		dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
		return ret;
	}

382
	/* Wait for data length. We'll get 0xff until length arrives. */
M
Markus Grabner 已提交
383 384
	do {
		ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
385 386 387 388 389 390 391
				      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 已提交
392 393
			return ret;
		}
394
	} while (len == 0xff);
395 396 397 398 399 400

	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 已提交
401 402 403 404 405
		return -EINVAL;
	}

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

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

	return 0;
}
T
Takashi Iwai 已提交
417
EXPORT_SYMBOL_GPL(line6_read_data);
M
Markus Grabner 已提交
418 419 420 421

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

429 430 431 432
	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 已提交
433

434 435 436
	if (ret < 0) {
		dev_err(line6->ifcdev,
			"write request failed (error %d)\n", ret);
M
Markus Grabner 已提交
437 438 439 440
		return ret;
	}

	do {
441 442 443 444 445 446 447 448 449 450
		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 已提交
451 452
			return ret;
		}
453
	} while (status == 0xff);
M
Markus Grabner 已提交
454

455
	if (status != 0) {
M
Markus Grabner 已提交
456 457 458 459 460 461
		dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
		return -EINVAL;
	}

	return 0;
}
T
Takashi Iwai 已提交
462
EXPORT_SYMBOL_GPL(line6_write_data);
M
Markus Grabner 已提交
463 464 465 466 467 468 469

/*
	Read Line6 device serial number.
	(POD, TonePort, GuitarPort)
*/
int line6_read_serial_number(struct usb_line6 *line6, int *serial_number)
{
470 471
	return line6_read_data(line6, 0x80d0, serial_number,
			       sizeof(*serial_number));
M
Markus Grabner 已提交
472
}
T
Takashi Iwai 已提交
473
EXPORT_SYMBOL_GPL(line6_read_serial_number);
M
Markus Grabner 已提交
474 475 476 477

/*
	No operation (i.e., unsupported).
*/
478 479
ssize_t line6_nop_read(struct device *dev, struct device_attribute *attr,
		       char *buf)
M
Markus Grabner 已提交
480 481 482
{
	return 0;
}
T
Takashi Iwai 已提交
483
EXPORT_SYMBOL_GPL(line6_nop_read);
M
Markus Grabner 已提交
484 485

/*
486
	Card destructor.
M
Markus Grabner 已提交
487
*/
488
static void line6_destruct(struct snd_card *card)
M
Markus Grabner 已提交
489
{
490 491
	struct usb_line6 *line6 = card->private_data;
	struct usb_device *usbdev;
492

493
	if (!line6)
494
		return;
495
	usbdev = line6->usbdev;
M
Markus Grabner 已提交
496 497

	/* free buffer memory first: */
498 499
	kfree(line6->buffer_message);
	kfree(line6->buffer_listen);
M
Markus Grabner 已提交
500 501

	/* then free URBs: */
502
	usb_free_urb(line6->urb_listen);
M
Markus Grabner 已提交
503

504 505 506 507 508
	/* free interface data: */
	kfree(line6);

	/* decrement reference counters: */
	usb_put_dev(usbdev);
M
Markus Grabner 已提交
509 510 511 512 513
}

/*
	Probe USB device.
*/
T
Takashi Iwai 已提交
514 515 516 517
int line6_probe(struct usb_interface *interface,
		struct usb_line6 *line6,
		const struct line6_properties *properties,
		int (*private_init)(struct usb_interface *, struct usb_line6 *))
M
Markus Grabner 已提交
518
{
519
	struct usb_device *usbdev;
520
	struct snd_card *card;
521
	int interface_number;
M
Markus Grabner 已提交
522 523
	int ret;

T
Takashi Iwai 已提交
524 525 526 527
	if (!interface) {
		ret = -ENODEV;
		goto err_put;
	}
M
Markus Grabner 已提交
528
	usbdev = interface_to_usbdev(interface);
T
Takashi Iwai 已提交
529 530 531 532
	if (!usbdev) {
		ret = -ENODEV;
		goto err_put;
	}
M
Markus Grabner 已提交
533 534

	/* we don't handle multiple configurations */
535 536 537 538
	if (usbdev->descriptor.bNumConfigurations != 1) {
		ret = -ENODEV;
		goto err_put;
	}
M
Markus Grabner 已提交
539 540 541 542 543 544 545

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

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

546 547
	ret = usb_set_interface(usbdev, interface_number,
			properties->altsetting);
548
	if (ret < 0) {
M
Markus Grabner 已提交
549
		dev_err(&interface->dev, "set_interface failed\n");
550
		goto err_put;
M
Markus Grabner 已提交
551 552 553 554 555 556 557 558 559 560
	}

	/* store basic data: */
	line6->properties = properties;
	line6->usbdev = usbdev;
	line6->ifcdev = &interface->dev;

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

565
		if (ep != NULL) {
M
Markus Grabner 已提交
566
			line6->interval = ep->desc.bInterval;
567 568
			line6->max_packet_size =
			    le16_to_cpu(ep->desc.wMaxPacketSize);
569
		} else {
M
Markus Grabner 已提交
570 571
			line6->interval = LINE6_FALLBACK_INTERVAL;
			line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
572 573
			dev_err(line6->ifcdev,
				"endpoint not available, using fallback values");
M
Markus Grabner 已提交
574 575 576
		}
	}

577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	ret = snd_card_new(line6->ifcdev,
			   SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
			   THIS_MODULE, 0, &card);
	if (ret < 0)
		goto err_put;

	line6->card = card;
	strcpy(card->id, line6->properties->id);
	strcpy(card->driver, DRIVER_NAME);
	strcpy(card->shortname, line6->properties->name);
	sprintf(card->longname, "Line6 %s at USB %s", line6->properties->name,
		dev_name(line6->ifcdev));
	card->private_data = line6;
	card->private_free = line6_destruct;

M
Markus Grabner 已提交
592 593
	usb_set_intfdata(interface, line6);

594 595 596
	/* increment reference counters: */
	usb_get_dev(usbdev);

597
	if (properties->capabilities & LINE6_CAP_CONTROL) {
M
Markus Grabner 已提交
598
		/* initialize USB buffers: */
599 600
		line6->buffer_listen =
		    kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
601
		if (line6->buffer_listen == NULL) {
602 603
			ret = -ENOMEM;
			goto err_destruct;
M
Markus Grabner 已提交
604 605
		}

606 607
		line6->buffer_message =
		    kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
608
		if (line6->buffer_message == NULL) {
609 610
			ret = -ENOMEM;
			goto err_destruct;
M
Markus Grabner 已提交
611 612 613 614
		}

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

615
		if (line6->urb_listen == NULL) {
616 617
			ret = -ENOMEM;
			goto err_destruct;
M
Markus Grabner 已提交
618 619
		}

620 621 622 623
		ret = line6_start_listen(line6);
		if (ret < 0) {
			dev_err(&interface->dev, "%s: usb_submit_urb failed\n",
				__func__);
624
			goto err_destruct;
M
Markus Grabner 已提交
625 626 627
		}
	}

628
	/* initialize device data based on device: */
T
Takashi Iwai 已提交
629
	ret = private_init(interface, line6);
630 631
	if (ret < 0)
		goto err_destruct;
M
Markus Grabner 已提交
632

633 634
	/* creation of additional special files should go here */

635 636
	dev_info(&interface->dev, "Line6 %s now attached\n",
		 line6->properties->name);
637

638 639
	return 0;

640 641 642
 err_destruct:
	snd_card_free(card);
 err_put:
M
Markus Grabner 已提交
643 644
	return ret;
}
T
Takashi Iwai 已提交
645
EXPORT_SYMBOL_GPL(line6_probe);
M
Markus Grabner 已提交
646 647 648 649

/*
	Line6 device disconnected.
*/
T
Takashi Iwai 已提交
650
void line6_disconnect(struct usb_interface *interface)
M
Markus Grabner 已提交
651 652 653
{
	struct usb_line6 *line6;
	struct usb_device *usbdev;
654
	int interface_number;
M
Markus Grabner 已提交
655

656 657
	if (interface == NULL)
		return;
M
Markus Grabner 已提交
658
	usbdev = interface_to_usbdev(interface);
659 660
	if (usbdev == NULL)
		return;
M
Markus Grabner 已提交
661 662 663

	interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
	line6 = usb_get_intfdata(interface);
664 665
	if (!line6)
		return;
M
Markus Grabner 已提交
666

667 668
	if (line6->urb_listen != NULL)
		line6_stop_listen(line6);
M
Markus Grabner 已提交
669

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

673 674
	snd_card_disconnect(line6->card);
	if (line6->disconnect)
675
		line6->disconnect(interface);
M
Markus Grabner 已提交
676

677 678
	dev_info(&interface->dev, "Line6 %s now disconnected\n",
		 line6->properties->name);
M
Markus Grabner 已提交
679

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

683
	snd_card_free_when_closed(line6->card);
684
}
T
Takashi Iwai 已提交
685
EXPORT_SYMBOL_GPL(line6_disconnect);
686 687 688 689 690 691

#ifdef CONFIG_PM

/*
	Suspend Line6 device.
*/
T
Takashi Iwai 已提交
692
int line6_suspend(struct usb_interface *interface, pm_message_t message)
693 694 695 696 697 698
{
	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);

699
	if (line6->properties->capabilities & LINE6_CAP_CONTROL)
700 701 702 703 704 705 706 707 708 709
		line6_stop_listen(line6);

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

	return 0;
}
T
Takashi Iwai 已提交
710
EXPORT_SYMBOL_GPL(line6_suspend);
711 712 713 714

/*
	Resume Line6 device.
*/
T
Takashi Iwai 已提交
715
int line6_resume(struct usb_interface *interface)
716 717 718
{
	struct usb_line6 *line6 = usb_get_intfdata(interface);

719
	if (line6->properties->capabilities & LINE6_CAP_CONTROL)
720 721 722 723 724
		line6_start_listen(line6);

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

727
#endif /* CONFIG_PM */
728

M
Markus Grabner 已提交
729 730 731 732
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
MODULE_VERSION(DRIVER_VERSION);