Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Line 6 Linux USB driver
  4 *
  5 * Copyright (C) 2004-2010 Markus Grabner (line6@grabner-graz.at)
 
 
 
 
 
  6 */
  7
  8#include <linux/kernel.h>
  9#include <linux/module.h>
 10#include <linux/export.h>
 11#include <linux/slab.h>
 12#include <linux/usb.h>
 13
 14#include <sound/core.h>
 15#include <sound/initval.h>
 16#include <sound/hwdep.h>
 17
 18#include "capture.h"
 19#include "driver.h"
 20#include "midi.h"
 21#include "playback.h"
 22
 23#define DRIVER_AUTHOR  "Markus Grabner <line6@grabner-graz.at>"
 24#define DRIVER_DESC    "Line 6 USB Driver"
 25
 26/*
 27	This is Line 6's MIDI manufacturer ID.
 28*/
 29const unsigned char line6_midi_id[3] = {
 30	0x00, 0x01, 0x0c
 31};
 32EXPORT_SYMBOL_GPL(line6_midi_id);
 33
 34/*
 35	Code to request version of POD, Variax interface
 36	(and maybe other devices).
 37*/
 38static const char line6_request_version[] = {
 39	0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
 40};
 41
 42/*
 43	 Class for asynchronous messages.
 44*/
 45struct message {
 46	struct usb_line6 *line6;
 47	const char *buffer;
 48	int size;
 49	int done;
 50};
 51
 52/*
 53	Forward declarations.
 54*/
 55static void line6_data_received(struct urb *urb);
 56static int line6_send_raw_message_async_part(struct message *msg,
 57					     struct urb *urb);
 58
 59/*
 60	Start to listen on endpoint.
 61*/
 62static int line6_start_listen(struct usb_line6 *line6)
 63{
 64	int err;
 65
 66	if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
 67		usb_fill_int_urb(line6->urb_listen, line6->usbdev,
 68			usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
 69			line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
 70			line6_data_received, line6, line6->interval);
 71	} else {
 72		usb_fill_bulk_urb(line6->urb_listen, line6->usbdev,
 73			usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r),
 74			line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
 75			line6_data_received, line6);
 76	}
 77
 78	/* sanity checks of EP before actually submitting */
 79	if (usb_urb_ep_type_check(line6->urb_listen)) {
 80		dev_err(line6->ifcdev, "invalid control EP\n");
 81		return -EINVAL;
 82	}
 83
 84	line6->urb_listen->actual_length = 0;
 85	err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
 86	return err;
 87}
 88
 89/*
 90	Stop listening on endpoint.
 91*/
 92static void line6_stop_listen(struct usb_line6 *line6)
 93{
 94	usb_kill_urb(line6->urb_listen);
 95}
 96
 97/*
 98	Send raw message in pieces of wMaxPacketSize bytes.
 99*/
100int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
101				  int size)
102{
103	int i, done = 0;
104	const struct line6_properties *properties = line6->properties;
105
106	for (i = 0; i < size; i += line6->max_packet_size) {
107		int partial;
108		const char *frag_buf = buffer + i;
109		int frag_size = min(line6->max_packet_size, size - i);
110		int retval;
111
112		if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
113			retval = usb_interrupt_msg(line6->usbdev,
114						usb_sndintpipe(line6->usbdev, properties->ep_ctrl_w),
115						(char *)frag_buf, frag_size,
116						&partial, LINE6_TIMEOUT);
117		} else {
118			retval = usb_bulk_msg(line6->usbdev,
119						usb_sndbulkpipe(line6->usbdev, properties->ep_ctrl_w),
120						(char *)frag_buf, frag_size,
121						&partial, LINE6_TIMEOUT);
122		}
123
124		if (retval) {
125			dev_err(line6->ifcdev,
126				"usb_bulk_msg failed (%d)\n", retval);
127			break;
128		}
129
130		done += frag_size;
131	}
132
133	return done;
134}
135EXPORT_SYMBOL_GPL(line6_send_raw_message);
136
137/*
138	Notification of completion of asynchronous request transmission.
139*/
140static void line6_async_request_sent(struct urb *urb)
141{
142	struct message *msg = (struct message *)urb->context;
143
144	if (msg->done >= msg->size) {
145		usb_free_urb(urb);
146		kfree(msg);
147	} else
148		line6_send_raw_message_async_part(msg, urb);
149}
150
151/*
152	Asynchronously send part of a raw message.
153*/
154static int line6_send_raw_message_async_part(struct message *msg,
155					     struct urb *urb)
156{
157	int retval;
158	struct usb_line6 *line6 = msg->line6;
159	int done = msg->done;
160	int bytes = min(msg->size - done, line6->max_packet_size);
161
162	if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
163		usb_fill_int_urb(urb, line6->usbdev,
164			usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
165			(char *)msg->buffer + done, bytes,
166			line6_async_request_sent, msg, line6->interval);
167	} else {
168		usb_fill_bulk_urb(urb, line6->usbdev,
169			usb_sndbulkpipe(line6->usbdev, line6->properties->ep_ctrl_w),
170			(char *)msg->buffer + done, bytes,
171			line6_async_request_sent, msg);
172	}
173
174	msg->done += bytes;
175
176	/* sanity checks of EP before actually submitting */
177	retval = usb_urb_ep_type_check(urb);
178	if (retval < 0)
179		goto error;
180
181	retval = usb_submit_urb(urb, GFP_ATOMIC);
182	if (retval < 0)
183		goto error;
 
 
 
 
 
 
184
185	return 0;
 
186
187 error:
188	dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
189		__func__, retval);
190	usb_free_urb(urb);
191	kfree(msg);
192	return retval;
 
 
193}
 
194
195/*
196	Asynchronously send raw message.
197*/
198int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
199				 int size)
200{
201	struct message *msg;
202	struct urb *urb;
203
204	/* create message: */
205	msg = kzalloc(sizeof(struct message), GFP_ATOMIC);
206	if (msg == NULL)
207		return -ENOMEM;
208
209	/* create URB: */
210	urb = usb_alloc_urb(0, GFP_ATOMIC);
211
212	if (urb == NULL) {
213		kfree(msg);
214		return -ENOMEM;
215	}
216
217	/* set message data: */
218	msg->line6 = line6;
219	msg->buffer = buffer;
220	msg->size = size;
221	msg->done = 0;
222
223	/* start sending: */
224	return line6_send_raw_message_async_part(msg, urb);
225}
226EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
227
228/*
229	Send asynchronous device version request.
230*/
231int line6_version_request_async(struct usb_line6 *line6)
232{
233	char *buffer;
234	int retval;
235
236	buffer = kmemdup(line6_request_version,
237			sizeof(line6_request_version), GFP_ATOMIC);
238	if (buffer == NULL)
239		return -ENOMEM;
240
241	retval = line6_send_raw_message_async(line6, buffer,
242					      sizeof(line6_request_version));
243	kfree(buffer);
244	return retval;
245}
246EXPORT_SYMBOL_GPL(line6_version_request_async);
247
248/*
249	Send sysex message in pieces of wMaxPacketSize bytes.
250*/
251int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
252			     int size)
253{
254	return line6_send_raw_message(line6, buffer,
255				      size + SYSEX_EXTRA_SIZE) -
256	    SYSEX_EXTRA_SIZE;
257}
258EXPORT_SYMBOL_GPL(line6_send_sysex_message);
259
260/*
261	Allocate buffer for sysex message and prepare header.
262	@param code sysex message code
263	@param size number of bytes between code and sysex end
264*/
265char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
266			       int size)
267{
268	char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
269
270	if (!buffer)
271		return NULL;
272
273	buffer[0] = LINE6_SYSEX_BEGIN;
274	memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
275	buffer[sizeof(line6_midi_id) + 1] = code1;
276	buffer[sizeof(line6_midi_id) + 2] = code2;
277	buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
278	return buffer;
279}
280EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
281
282/*
283	Notification of data received from the Line 6 device.
284*/
285static void line6_data_received(struct urb *urb)
286{
287	struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
288	struct midi_buffer *mb = &line6->line6midi->midibuf_in;
289	unsigned long flags;
290	int done;
291
292	if (urb->status == -ESHUTDOWN)
293		return;
294
295	if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
296		spin_lock_irqsave(&line6->line6midi->lock, flags);
297		done =
298			line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
299
300		if (done < urb->actual_length) {
301			line6_midibuf_ignore(mb, done);
302			dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
303				done, urb->actual_length);
304		}
305		spin_unlock_irqrestore(&line6->line6midi->lock, flags);
306
307		for (;;) {
308			spin_lock_irqsave(&line6->line6midi->lock, flags);
309			done =
310				line6_midibuf_read(mb, line6->buffer_message,
311						   LINE6_MIDI_MESSAGE_MAXLEN,
312						   LINE6_MIDIBUF_READ_RX);
313			spin_unlock_irqrestore(&line6->line6midi->lock, flags);
314
315			if (done <= 0)
316				break;
317
318			line6->message_length = done;
319			line6_midi_receive(line6, line6->buffer_message, done);
320
321			if (line6->process_message)
322				line6->process_message(line6);
323		}
324	} else {
325		line6->buffer_message = urb->transfer_buffer;
326		line6->message_length = urb->actual_length;
327		if (line6->process_message)
328			line6->process_message(line6);
329		line6->buffer_message = NULL;
330	}
331
332	line6_start_listen(line6);
333}
334
335#define LINE6_READ_WRITE_STATUS_DELAY 2  /* milliseconds */
336#define LINE6_READ_WRITE_MAX_RETRIES 50
337
338/*
339	Read data from device.
340*/
341int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
342		    unsigned datalen)
343{
344	struct usb_device *usbdev = line6->usbdev;
345	int ret;
346	u8 len;
347	unsigned count;
348
349	if (address > 0xffff || datalen > 0xff)
350		return -EINVAL;
351
352	/* query the serial number: */
353	ret = usb_control_msg_send(usbdev, 0, 0x67,
354				   USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
355				   (datalen << 8) | 0x21, address, NULL, 0,
356				   LINE6_TIMEOUT, GFP_KERNEL);
357	if (ret) {
 
358		dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
359		goto exit;
360	}
361
362	/* Wait for data length. We'll get 0xff until length arrives. */
363	for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
364		mdelay(LINE6_READ_WRITE_STATUS_DELAY);
365
366		ret = usb_control_msg_recv(usbdev, 0, 0x67,
367					   USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
368					   0x0012, 0x0000, &len, 1,
369					   LINE6_TIMEOUT, GFP_KERNEL);
370		if (ret) {
 
371			dev_err(line6->ifcdev,
372				"receive length failed (error %d)\n", ret);
373			goto exit;
374		}
375
376		if (len != 0xff)
377			break;
378	}
379
380	ret = -EIO;
381	if (len == 0xff) {
382		dev_err(line6->ifcdev, "read failed after %d retries\n",
383			count);
384		goto exit;
385	} else if (len != datalen) {
386		/* should be equal or something went wrong */
387		dev_err(line6->ifcdev,
388			"length mismatch (expected %d, got %d)\n",
389			(int)datalen, len);
390		goto exit;
391	}
392
393	/* receive the result: */
394	ret = usb_control_msg_recv(usbdev, 0, 0x67,
395				   USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
396				   0x0013, 0x0000, data, datalen, LINE6_TIMEOUT,
397				   GFP_KERNEL);
398	if (ret)
 
399		dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
 
 
400
401exit:
402	return ret;
403}
404EXPORT_SYMBOL_GPL(line6_read_data);
405
406/*
407	Write data to device.
408*/
409int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
410		     unsigned datalen)
411{
412	struct usb_device *usbdev = line6->usbdev;
413	int ret;
414	unsigned char *status;
415	int count;
416
417	if (address > 0xffff || datalen > 0xffff)
418		return -EINVAL;
419
420	status = kmalloc(1, GFP_KERNEL);
421	if (!status)
422		return -ENOMEM;
 
423
424	ret = usb_control_msg_send(usbdev, 0, 0x67,
425				   USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
426				   0x0022, address, data, datalen, LINE6_TIMEOUT,
427				   GFP_KERNEL);
428	if (ret) {
429		dev_err(line6->ifcdev,
430			"write request failed (error %d)\n", ret);
431		goto exit;
432	}
433
434	for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
435		mdelay(LINE6_READ_WRITE_STATUS_DELAY);
436
437		ret = usb_control_msg_recv(usbdev, 0, 0x67,
438					   USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
439					   0x0012, 0x0000, status, 1, LINE6_TIMEOUT,
440					   GFP_KERNEL);
441		if (ret) {
 
 
 
442			dev_err(line6->ifcdev,
443				"receiving status failed (error %d)\n", ret);
444			goto exit;
445		}
446
447		if (*status != 0xff)
448			break;
449	}
450
451	if (*status == 0xff) {
452		dev_err(line6->ifcdev, "write failed after %d retries\n",
453			count);
454		ret = -EIO;
455	} else if (*status != 0) {
456		dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
457		ret = -EIO;
458	}
459exit:
460	kfree(status);
461	return ret;
462}
463EXPORT_SYMBOL_GPL(line6_write_data);
464
465/*
466	Read Line 6 device serial number.
467	(POD, TonePort, GuitarPort)
468*/
469int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
470{
471	return line6_read_data(line6, 0x80d0, serial_number,
472			       sizeof(*serial_number));
473}
474EXPORT_SYMBOL_GPL(line6_read_serial_number);
475
476/*
477	Card destructor.
478*/
479static void line6_destruct(struct snd_card *card)
480{
481	struct usb_line6 *line6 = card->private_data;
482	struct usb_device *usbdev = line6->usbdev;
483
484	/* Free buffer memory first. We cannot depend on the existence of private
485	 * data from the (podhd) module, it may be gone already during this call
486	 */
487	kfree(line6->buffer_message);
488
489	kfree(line6->buffer_listen);
490
491	/* then free URBs: */
492	usb_free_urb(line6->urb_listen);
493	line6->urb_listen = NULL;
494
495	/* decrement reference counters: */
496	usb_put_dev(usbdev);
497}
498
499static void line6_get_usb_properties(struct usb_line6 *line6)
 
500{
501	struct usb_device *usbdev = line6->usbdev;
502	const struct line6_properties *properties = line6->properties;
503	int pipe;
504	struct usb_host_endpoint *ep = NULL;
505
506	if (properties->capabilities & LINE6_CAP_CONTROL) {
507		if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
508			pipe = usb_rcvintpipe(line6->usbdev,
509				line6->properties->ep_ctrl_r);
510		} else {
511			pipe = usb_rcvbulkpipe(line6->usbdev,
512				line6->properties->ep_ctrl_r);
513		}
514		ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
515	}
516
517	/* Control data transfer properties */
518	if (ep) {
519		line6->interval = ep->desc.bInterval;
520		line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
521	} else {
522		if (properties->capabilities & LINE6_CAP_CONTROL) {
523			dev_err(line6->ifcdev,
524				"endpoint not available, using fallback values");
525		}
526		line6->interval = LINE6_FALLBACK_INTERVAL;
527		line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
528	}
529
530	/* Isochronous transfer properties */
531	if (usbdev->speed == USB_SPEED_LOW) {
532		line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
533		line6->iso_buffers = USB_LOW_ISO_BUFFERS;
534	} else {
535		line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
536		line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
537	}
538}
539
540/* Enable buffering of incoming messages, flush the buffer */
541static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
542{
543	struct usb_line6 *line6 = hw->private_data;
544
545	/* NOTE: hwdep layer provides atomicity here */
546
547	line6->messages.active = 1;
548	line6->messages.nonblock = file->f_flags & O_NONBLOCK ? 1 : 0;
549
550	return 0;
551}
552
553/* Stop buffering */
554static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
555{
556	struct usb_line6 *line6 = hw->private_data;
557
558	line6->messages.active = 0;
559
560	return 0;
561}
562
563/* Read from circular buffer, return to user */
564static long
565line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
566					loff_t *offset)
567{
568	struct usb_line6 *line6 = hwdep->private_data;
569	long rv = 0;
570	unsigned int out_count;
571
572	if (mutex_lock_interruptible(&line6->messages.read_lock))
573		return -ERESTARTSYS;
574
575	while (kfifo_len(&line6->messages.fifo) == 0) {
576		mutex_unlock(&line6->messages.read_lock);
577
578		if (line6->messages.nonblock)
579			return -EAGAIN;
580
581		rv = wait_event_interruptible(
582			line6->messages.wait_queue,
583			kfifo_len(&line6->messages.fifo) != 0);
584		if (rv < 0)
585			return rv;
586
587		if (mutex_lock_interruptible(&line6->messages.read_lock))
588			return -ERESTARTSYS;
589	}
590
591	if (kfifo_peek_len(&line6->messages.fifo) > count) {
592		/* Buffer too small; allow re-read of the current item... */
593		rv = -EINVAL;
594	} else {
595		rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
596		if (rv == 0)
597			rv = out_count;
598	}
599
600	mutex_unlock(&line6->messages.read_lock);
601	return rv;
602}
603
604/* Write directly (no buffering) to device by user*/
605static long
606line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
607					loff_t *offset)
608{
609	struct usb_line6 *line6 = hwdep->private_data;
610	int rv;
611	char *data_copy;
612
613	if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
614		/* This is an arbitrary limit - still better than nothing... */
615		return -EINVAL;
616	}
617
618	data_copy = memdup_user(data, count);
619	if (IS_ERR(data_copy))
620		return PTR_ERR(data_copy);
621
622	rv = line6_send_raw_message(line6, data_copy, count);
623
624	kfree(data_copy);
625	return rv;
626}
627
628static __poll_t
629line6_hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
630{
631	__poll_t rv;
632	struct usb_line6 *line6 = hwdep->private_data;
633
634	poll_wait(file, &line6->messages.wait_queue, wait);
635
636	mutex_lock(&line6->messages.read_lock);
637	rv = kfifo_len(&line6->messages.fifo) == 0 ? 0 : EPOLLIN | EPOLLRDNORM;
638	mutex_unlock(&line6->messages.read_lock);
639
640	return rv;
641}
642
643static const struct snd_hwdep_ops hwdep_ops = {
644	.open    = line6_hwdep_open,
645	.release = line6_hwdep_release,
646	.read    = line6_hwdep_read,
647	.write   = line6_hwdep_write,
648	.poll    = line6_hwdep_poll,
649};
650
651/* Insert into circular buffer */
652static void line6_hwdep_push_message(struct usb_line6 *line6)
653{
654	if (!line6->messages.active)
655		return;
656
657	if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
658		/* No race condition here, there's only one writer */
659		kfifo_in(&line6->messages.fifo,
660			line6->buffer_message, line6->message_length);
661	} /* else TODO: signal overflow */
662
663	wake_up_interruptible(&line6->messages.wait_queue);
664}
665
666static int line6_hwdep_init(struct usb_line6 *line6)
667{
668	int err;
669	struct snd_hwdep *hwdep;
670
671	/* TODO: usb_driver_claim_interface(); */
672	line6->process_message = line6_hwdep_push_message;
673	line6->messages.active = 0;
674	init_waitqueue_head(&line6->messages.wait_queue);
675	mutex_init(&line6->messages.read_lock);
676	INIT_KFIFO(line6->messages.fifo);
677
678	err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
679	if (err < 0)
680		goto end;
681	strcpy(hwdep->name, "config");
682	hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
683	hwdep->ops = hwdep_ops;
684	hwdep->private_data = line6;
685	hwdep->exclusive = true;
686
687end:
688	return err;
689}
690
691static int line6_init_cap_control(struct usb_line6 *line6)
692{
693	int ret;
694
695	/* initialize USB buffers: */
696	line6->buffer_listen = kzalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
697	if (!line6->buffer_listen)
698		return -ENOMEM;
699
 
 
 
 
700	line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
701	if (!line6->urb_listen)
702		return -ENOMEM;
703
704	if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
705		line6->buffer_message = kzalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
706		if (!line6->buffer_message)
707			return -ENOMEM;
708
709		ret = line6_init_midi(line6);
710		if (ret < 0)
711			return ret;
712	} else {
713		ret = line6_hwdep_init(line6);
714		if (ret < 0)
715			return ret;
716	}
717
718	ret = line6_start_listen(line6);
719	if (ret < 0) {
720		dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
721		return ret;
722	}
723
724	return 0;
725}
726
727static void line6_startup_work(struct work_struct *work)
728{
729	struct usb_line6 *line6 =
730		container_of(work, struct usb_line6, startup_work.work);
731
732	if (line6->startup)
733		line6->startup(line6);
734}
735
736/*
737	Probe USB device.
738*/
739int line6_probe(struct usb_interface *interface,
740		const struct usb_device_id *id,
741		const char *driver_name,
742		const struct line6_properties *properties,
743		int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
744		size_t data_size)
745{
746	struct usb_device *usbdev = interface_to_usbdev(interface);
747	struct snd_card *card;
748	struct usb_line6 *line6;
749	int interface_number;
750	int ret;
751
752	if (WARN_ON(data_size < sizeof(*line6)))
753		return -EINVAL;
754
755	/* we don't handle multiple configurations */
756	if (usbdev->descriptor.bNumConfigurations != 1)
757		return -ENODEV;
758
759	ret = snd_card_new(&interface->dev,
760			   SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
761			   THIS_MODULE, data_size, &card);
762	if (ret < 0)
763		return ret;
764
765	/* store basic data: */
766	line6 = card->private_data;
767	line6->card = card;
768	line6->properties = properties;
769	line6->usbdev = usbdev;
770	line6->ifcdev = &interface->dev;
771	INIT_DELAYED_WORK(&line6->startup_work, line6_startup_work);
772
773	strcpy(card->id, properties->id);
774	strcpy(card->driver, driver_name);
775	strcpy(card->shortname, properties->name);
776	sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
777		dev_name(line6->ifcdev));
778	card->private_free = line6_destruct;
779
780	usb_set_intfdata(interface, line6);
781
782	/* increment reference counters: */
783	usb_get_dev(usbdev);
784
785	/* initialize device info: */
786	dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
787
788	/* query interface number */
789	interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
790
791	/* TODO reserves the bus bandwidth even without actual transfer */
792	ret = usb_set_interface(usbdev, interface_number,
793				properties->altsetting);
794	if (ret < 0) {
795		dev_err(&interface->dev, "set_interface failed\n");
796		goto error;
797	}
798
799	line6_get_usb_properties(line6);
800
801	if (properties->capabilities & LINE6_CAP_CONTROL) {
802		ret = line6_init_cap_control(line6);
803		if (ret < 0)
804			goto error;
805	}
806
807	/* initialize device data based on device: */
808	ret = private_init(line6, id);
809	if (ret < 0)
810		goto error;
811
812	/* creation of additional special files should go here */
813
814	dev_info(&interface->dev, "Line 6 %s now attached\n",
815		 properties->name);
816
817	return 0;
818
819 error:
820	/* we can call disconnect callback here because no close-sync is
821	 * needed yet at this point
822	 */
823	line6_disconnect(interface);
824	return ret;
825}
826EXPORT_SYMBOL_GPL(line6_probe);
827
828/*
829	Line 6 device disconnected.
830*/
831void line6_disconnect(struct usb_interface *interface)
832{
833	struct usb_line6 *line6 = usb_get_intfdata(interface);
834	struct usb_device *usbdev = interface_to_usbdev(interface);
835
836	if (!line6)
837		return;
838
839	if (WARN_ON(usbdev != line6->usbdev))
840		return;
841
842	cancel_delayed_work_sync(&line6->startup_work);
843
844	if (line6->urb_listen != NULL)
845		line6_stop_listen(line6);
846
847	snd_card_disconnect(line6->card);
848	if (line6->line6pcm)
849		line6_pcm_disconnect(line6->line6pcm);
850	if (line6->disconnect)
851		line6->disconnect(line6);
852
853	dev_info(&interface->dev, "Line 6 %s now disconnected\n",
854		 line6->properties->name);
855
856	/* make sure the device isn't destructed twice: */
857	usb_set_intfdata(interface, NULL);
858
859	snd_card_free_when_closed(line6->card);
860}
861EXPORT_SYMBOL_GPL(line6_disconnect);
862
863#ifdef CONFIG_PM
864
865/*
866	Suspend Line 6 device.
867*/
868int line6_suspend(struct usb_interface *interface, pm_message_t message)
869{
870	struct usb_line6 *line6 = usb_get_intfdata(interface);
871	struct snd_line6_pcm *line6pcm = line6->line6pcm;
872
873	snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
874
875	if (line6->properties->capabilities & LINE6_CAP_CONTROL)
876		line6_stop_listen(line6);
877
878	if (line6pcm != NULL)
 
879		line6pcm->flags = 0;
 
880
881	return 0;
882}
883EXPORT_SYMBOL_GPL(line6_suspend);
884
885/*
886	Resume Line 6 device.
887*/
888int line6_resume(struct usb_interface *interface)
889{
890	struct usb_line6 *line6 = usb_get_intfdata(interface);
891
892	if (line6->properties->capabilities & LINE6_CAP_CONTROL)
893		line6_start_listen(line6);
894
895	snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
896	return 0;
897}
898EXPORT_SYMBOL_GPL(line6_resume);
899
900#endif /* CONFIG_PM */
901
902MODULE_AUTHOR(DRIVER_AUTHOR);
903MODULE_DESCRIPTION(DRIVER_DESC);
904MODULE_LICENSE("GPL");
905
v4.6
 
  1/*
  2 * Line 6 Linux USB driver
  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/export.h>
 15#include <linux/slab.h>
 16#include <linux/usb.h>
 17
 18#include <sound/core.h>
 19#include <sound/initval.h>
 
 20
 21#include "capture.h"
 22#include "driver.h"
 23#include "midi.h"
 24#include "playback.h"
 25
 26#define DRIVER_AUTHOR  "Markus Grabner <grabner@icg.tugraz.at>"
 27#define DRIVER_DESC    "Line 6 USB Driver"
 28
 29/*
 30	This is Line 6's MIDI manufacturer ID.
 31*/
 32const unsigned char line6_midi_id[] = {
 33	0x00, 0x01, 0x0c
 34};
 35EXPORT_SYMBOL_GPL(line6_midi_id);
 36
 37/*
 38	Code to request version of POD, Variax interface
 39	(and maybe other devices).
 40*/
 41static const char line6_request_version[] = {
 42	0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
 43};
 44
 45/*
 46	 Class for asynchronous messages.
 47*/
 48struct message {
 49	struct usb_line6 *line6;
 50	const char *buffer;
 51	int size;
 52	int done;
 53};
 54
 55/*
 56	Forward declarations.
 57*/
 58static void line6_data_received(struct urb *urb);
 59static int line6_send_raw_message_async_part(struct message *msg,
 60					     struct urb *urb);
 61
 62/*
 63	Start to listen on endpoint.
 64*/
 65static int line6_start_listen(struct usb_line6 *line6)
 66{
 67	int err;
 68
 69	usb_fill_int_urb(line6->urb_listen, line6->usbdev,
 70		usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
 71		line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
 72		line6_data_received, line6, line6->interval);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 73	line6->urb_listen->actual_length = 0;
 74	err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
 75	return err;
 76}
 77
 78/*
 79	Stop listening on endpoint.
 80*/
 81static void line6_stop_listen(struct usb_line6 *line6)
 82{
 83	usb_kill_urb(line6->urb_listen);
 84}
 85
 86/*
 87	Send raw message in pieces of wMaxPacketSize bytes.
 88*/
 89static int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
 90				  int size)
 91{
 92	int i, done = 0;
 
 93
 94	for (i = 0; i < size; i += line6->max_packet_size) {
 95		int partial;
 96		const char *frag_buf = buffer + i;
 97		int frag_size = min(line6->max_packet_size, size - i);
 98		int retval;
 99
100		retval = usb_interrupt_msg(line6->usbdev,
101					usb_sndintpipe(line6->usbdev,
102						line6->properties->ep_ctrl_w),
103					(char *)frag_buf, frag_size,
104					&partial, LINE6_TIMEOUT * HZ);
 
 
 
 
 
 
105
106		if (retval) {
107			dev_err(line6->ifcdev,
108				"usb_interrupt_msg failed (%d)\n", retval);
109			break;
110		}
111
112		done += frag_size;
113	}
114
115	return done;
116}
 
117
118/*
119	Notification of completion of asynchronous request transmission.
120*/
121static void line6_async_request_sent(struct urb *urb)
122{
123	struct message *msg = (struct message *)urb->context;
124
125	if (msg->done >= msg->size) {
126		usb_free_urb(urb);
127		kfree(msg);
128	} else
129		line6_send_raw_message_async_part(msg, urb);
130}
131
132/*
133	Asynchronously send part of a raw message.
134*/
135static int line6_send_raw_message_async_part(struct message *msg,
136					     struct urb *urb)
137{
138	int retval;
139	struct usb_line6 *line6 = msg->line6;
140	int done = msg->done;
141	int bytes = min(msg->size - done, line6->max_packet_size);
142
143	usb_fill_int_urb(urb, line6->usbdev,
144		usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
145		(char *)msg->buffer + done, bytes,
146		line6_async_request_sent, msg, line6->interval);
 
 
 
 
 
 
 
147
148	msg->done += bytes;
 
 
 
 
 
 
149	retval = usb_submit_urb(urb, GFP_ATOMIC);
150
151	if (retval < 0) {
152		dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
153			__func__, retval);
154		usb_free_urb(urb);
155		kfree(msg);
156		return retval;
157	}
158
159	return 0;
160}
161
162/*
163	Setup and start timer.
164*/
165void line6_start_timer(struct timer_list *timer, unsigned long msecs,
166		       void (*function)(unsigned long), unsigned long data)
167{
168	setup_timer(timer, function, data);
169	mod_timer(timer, jiffies + msecs_to_jiffies(msecs));
170}
171EXPORT_SYMBOL_GPL(line6_start_timer);
172
173/*
174	Asynchronously send raw message.
175*/
176int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
177				 int size)
178{
179	struct message *msg;
180	struct urb *urb;
181
182	/* create message: */
183	msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
184	if (msg == NULL)
185		return -ENOMEM;
186
187	/* create URB: */
188	urb = usb_alloc_urb(0, GFP_ATOMIC);
189
190	if (urb == NULL) {
191		kfree(msg);
192		return -ENOMEM;
193	}
194
195	/* set message data: */
196	msg->line6 = line6;
197	msg->buffer = buffer;
198	msg->size = size;
199	msg->done = 0;
200
201	/* start sending: */
202	return line6_send_raw_message_async_part(msg, urb);
203}
204EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
205
206/*
207	Send asynchronous device version request.
208*/
209int line6_version_request_async(struct usb_line6 *line6)
210{
211	char *buffer;
212	int retval;
213
214	buffer = kmemdup(line6_request_version,
215			sizeof(line6_request_version), GFP_ATOMIC);
216	if (buffer == NULL)
217		return -ENOMEM;
218
219	retval = line6_send_raw_message_async(line6, buffer,
220					      sizeof(line6_request_version));
221	kfree(buffer);
222	return retval;
223}
224EXPORT_SYMBOL_GPL(line6_version_request_async);
225
226/*
227	Send sysex message in pieces of wMaxPacketSize bytes.
228*/
229int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
230			     int size)
231{
232	return line6_send_raw_message(line6, buffer,
233				      size + SYSEX_EXTRA_SIZE) -
234	    SYSEX_EXTRA_SIZE;
235}
236EXPORT_SYMBOL_GPL(line6_send_sysex_message);
237
238/*
239	Allocate buffer for sysex message and prepare header.
240	@param code sysex message code
241	@param size number of bytes between code and sysex end
242*/
243char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
244			       int size)
245{
246	char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
247
248	if (!buffer)
249		return NULL;
250
251	buffer[0] = LINE6_SYSEX_BEGIN;
252	memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
253	buffer[sizeof(line6_midi_id) + 1] = code1;
254	buffer[sizeof(line6_midi_id) + 2] = code2;
255	buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
256	return buffer;
257}
258EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
259
260/*
261	Notification of data received from the Line 6 device.
262*/
263static void line6_data_received(struct urb *urb)
264{
265	struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
266	struct midi_buffer *mb = &line6->line6midi->midibuf_in;
 
267	int done;
268
269	if (urb->status == -ESHUTDOWN)
270		return;
271
272	done =
273	    line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
 
 
274
275	if (done < urb->actual_length) {
276		line6_midibuf_ignore(mb, done);
277		dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
278			done, urb->actual_length);
279	}
 
280
281	for (;;) {
282		done =
283		    line6_midibuf_read(mb, line6->buffer_message,
284				       LINE6_MESSAGE_MAXLEN);
 
 
 
285
286		if (done == 0)
287			break;
288
289		line6->message_length = done;
290		line6_midi_receive(line6, line6->buffer_message, done);
291
 
 
 
 
 
 
292		if (line6->process_message)
293			line6->process_message(line6);
 
294	}
295
296	line6_start_listen(line6);
297}
298
299#define LINE6_READ_WRITE_STATUS_DELAY 2  /* milliseconds */
300#define LINE6_READ_WRITE_MAX_RETRIES 50
301
302/*
303	Read data from device.
304*/
305int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
306		    unsigned datalen)
307{
308	struct usb_device *usbdev = line6->usbdev;
309	int ret;
310	unsigned char len;
311	unsigned count;
312
313	if (address > 0xffff || datalen > 0xff)
314		return -EINVAL;
315
316	/* query the serial number: */
317	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
318			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
319			      (datalen << 8) | 0x21, address,
320			      NULL, 0, LINE6_TIMEOUT * HZ);
321
322	if (ret < 0) {
323		dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
324		return ret;
325	}
326
327	/* Wait for data length. We'll get 0xff until length arrives. */
328	for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
329		mdelay(LINE6_READ_WRITE_STATUS_DELAY);
330
331		ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
332				      USB_TYPE_VENDOR | USB_RECIP_DEVICE |
333				      USB_DIR_IN,
334				      0x0012, 0x0000, &len, 1,
335				      LINE6_TIMEOUT * HZ);
336		if (ret < 0) {
337			dev_err(line6->ifcdev,
338				"receive length failed (error %d)\n", ret);
339			return ret;
340		}
341
342		if (len != 0xff)
343			break;
344	}
345
 
346	if (len == 0xff) {
347		dev_err(line6->ifcdev, "read failed after %d retries\n",
348			count);
349		return -EIO;
350	} else if (len != datalen) {
351		/* should be equal or something went wrong */
352		dev_err(line6->ifcdev,
353			"length mismatch (expected %d, got %d)\n",
354			(int)datalen, (int)len);
355		return -EIO;
356	}
357
358	/* receive the result: */
359	ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
360			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
361			      0x0013, 0x0000, data, datalen,
362			      LINE6_TIMEOUT * HZ);
363
364	if (ret < 0) {
365		dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
366		return ret;
367	}
368
369	return 0;
 
370}
371EXPORT_SYMBOL_GPL(line6_read_data);
372
373/*
374	Write data to device.
375*/
376int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
377		     unsigned datalen)
378{
379	struct usb_device *usbdev = line6->usbdev;
380	int ret;
381	unsigned char status;
382	int count;
383
384	if (address > 0xffff || datalen > 0xffff)
385		return -EINVAL;
386
387	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
388			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
389			      0x0022, address, data, datalen,
390			      LINE6_TIMEOUT * HZ);
391
392	if (ret < 0) {
 
 
 
 
393		dev_err(line6->ifcdev,
394			"write request failed (error %d)\n", ret);
395		return ret;
396	}
397
398	for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
399		mdelay(LINE6_READ_WRITE_STATUS_DELAY);
400
401		ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
402				      0x67,
403				      USB_TYPE_VENDOR | USB_RECIP_DEVICE |
404				      USB_DIR_IN,
405				      0x0012, 0x0000,
406				      &status, 1, LINE6_TIMEOUT * HZ);
407
408		if (ret < 0) {
409			dev_err(line6->ifcdev,
410				"receiving status failed (error %d)\n", ret);
411			return ret;
412		}
413
414		if (status != 0xff)
415			break;
416	}
417
418	if (status == 0xff) {
419		dev_err(line6->ifcdev, "write failed after %d retries\n",
420			count);
421		return -EIO;
422	} else if (status != 0) {
423		dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
424		return -EIO;
425	}
426
427	return 0;
 
428}
429EXPORT_SYMBOL_GPL(line6_write_data);
430
431/*
432	Read Line 6 device serial number.
433	(POD, TonePort, GuitarPort)
434*/
435int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
436{
437	return line6_read_data(line6, 0x80d0, serial_number,
438			       sizeof(*serial_number));
439}
440EXPORT_SYMBOL_GPL(line6_read_serial_number);
441
442/*
443	Card destructor.
444*/
445static void line6_destruct(struct snd_card *card)
446{
447	struct usb_line6 *line6 = card->private_data;
448	struct usb_device *usbdev = line6->usbdev;
449
450	/* free buffer memory first: */
 
 
451	kfree(line6->buffer_message);
 
452	kfree(line6->buffer_listen);
453
454	/* then free URBs: */
455	usb_free_urb(line6->urb_listen);
 
456
457	/* decrement reference counters: */
458	usb_put_dev(usbdev);
459}
460
461/* get data from endpoint descriptor (see usb_maxpacket): */
462static void line6_get_interval(struct usb_line6 *line6)
463{
464	struct usb_device *usbdev = line6->usbdev;
465	struct usb_host_endpoint *ep;
466	unsigned pipe = usb_rcvintpipe(usbdev, line6->properties->ep_ctrl_r);
467	unsigned epnum = usb_pipeendpoint(pipe);
468
469	ep = usbdev->ep_in[epnum];
 
 
 
 
 
 
 
 
 
 
 
470	if (ep) {
471		line6->interval = ep->desc.bInterval;
472		line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
473	} else {
474		dev_err(line6->ifcdev,
475			"endpoint not available, using fallback values");
 
 
476		line6->interval = LINE6_FALLBACK_INTERVAL;
477		line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
478	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
479}
480
481static int line6_init_cap_control(struct usb_line6 *line6)
482{
483	int ret;
484
485	/* initialize USB buffers: */
486	line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
487	if (!line6->buffer_listen)
488		return -ENOMEM;
489
490	line6->buffer_message = kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
491	if (!line6->buffer_message)
492		return -ENOMEM;
493
494	line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
495	if (!line6->urb_listen)
496		return -ENOMEM;
497
 
 
 
 
 
 
 
 
 
 
 
 
 
 
498	ret = line6_start_listen(line6);
499	if (ret < 0) {
500		dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
501		return ret;
502	}
503
504	return 0;
505}
506
 
 
 
 
 
 
 
 
 
507/*
508	Probe USB device.
509*/
510int line6_probe(struct usb_interface *interface,
511		const struct usb_device_id *id,
512		const char *driver_name,
513		const struct line6_properties *properties,
514		int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
515		size_t data_size)
516{
517	struct usb_device *usbdev = interface_to_usbdev(interface);
518	struct snd_card *card;
519	struct usb_line6 *line6;
520	int interface_number;
521	int ret;
522
523	if (WARN_ON(data_size < sizeof(*line6)))
524		return -EINVAL;
525
526	/* we don't handle multiple configurations */
527	if (usbdev->descriptor.bNumConfigurations != 1)
528		return -ENODEV;
529
530	ret = snd_card_new(&interface->dev,
531			   SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
532			   THIS_MODULE, data_size, &card);
533	if (ret < 0)
534		return ret;
535
536	/* store basic data: */
537	line6 = card->private_data;
538	line6->card = card;
539	line6->properties = properties;
540	line6->usbdev = usbdev;
541	line6->ifcdev = &interface->dev;
 
542
543	strcpy(card->id, properties->id);
544	strcpy(card->driver, driver_name);
545	strcpy(card->shortname, properties->name);
546	sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
547		dev_name(line6->ifcdev));
548	card->private_free = line6_destruct;
549
550	usb_set_intfdata(interface, line6);
551
552	/* increment reference counters: */
553	usb_get_dev(usbdev);
554
555	/* initialize device info: */
556	dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
557
558	/* query interface number */
559	interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
560
 
561	ret = usb_set_interface(usbdev, interface_number,
562				properties->altsetting);
563	if (ret < 0) {
564		dev_err(&interface->dev, "set_interface failed\n");
565		goto error;
566	}
567
568	line6_get_interval(line6);
569
570	if (properties->capabilities & LINE6_CAP_CONTROL) {
571		ret = line6_init_cap_control(line6);
572		if (ret < 0)
573			goto error;
574	}
575
576	/* initialize device data based on device: */
577	ret = private_init(line6, id);
578	if (ret < 0)
579		goto error;
580
581	/* creation of additional special files should go here */
582
583	dev_info(&interface->dev, "Line 6 %s now attached\n",
584		 properties->name);
585
586	return 0;
587
588 error:
589	if (line6->disconnect)
590		line6->disconnect(line6);
591	snd_card_free(card);
 
592	return ret;
593}
594EXPORT_SYMBOL_GPL(line6_probe);
595
596/*
597	Line 6 device disconnected.
598*/
599void line6_disconnect(struct usb_interface *interface)
600{
601	struct usb_line6 *line6 = usb_get_intfdata(interface);
602	struct usb_device *usbdev = interface_to_usbdev(interface);
603
604	if (!line6)
605		return;
606
607	if (WARN_ON(usbdev != line6->usbdev))
608		return;
609
 
 
610	if (line6->urb_listen != NULL)
611		line6_stop_listen(line6);
612
613	snd_card_disconnect(line6->card);
614	if (line6->line6pcm)
615		line6_pcm_disconnect(line6->line6pcm);
616	if (line6->disconnect)
617		line6->disconnect(line6);
618
619	dev_info(&interface->dev, "Line 6 %s now disconnected\n",
620		 line6->properties->name);
621
622	/* make sure the device isn't destructed twice: */
623	usb_set_intfdata(interface, NULL);
624
625	snd_card_free_when_closed(line6->card);
626}
627EXPORT_SYMBOL_GPL(line6_disconnect);
628
629#ifdef CONFIG_PM
630
631/*
632	Suspend Line 6 device.
633*/
634int line6_suspend(struct usb_interface *interface, pm_message_t message)
635{
636	struct usb_line6 *line6 = usb_get_intfdata(interface);
637	struct snd_line6_pcm *line6pcm = line6->line6pcm;
638
639	snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
640
641	if (line6->properties->capabilities & LINE6_CAP_CONTROL)
642		line6_stop_listen(line6);
643
644	if (line6pcm != NULL) {
645		snd_pcm_suspend_all(line6pcm->pcm);
646		line6pcm->flags = 0;
647	}
648
649	return 0;
650}
651EXPORT_SYMBOL_GPL(line6_suspend);
652
653/*
654	Resume Line 6 device.
655*/
656int line6_resume(struct usb_interface *interface)
657{
658	struct usb_line6 *line6 = usb_get_intfdata(interface);
659
660	if (line6->properties->capabilities & LINE6_CAP_CONTROL)
661		line6_start_listen(line6);
662
663	snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
664	return 0;
665}
666EXPORT_SYMBOL_GPL(line6_resume);
667
668#endif /* CONFIG_PM */
669
670MODULE_AUTHOR(DRIVER_AUTHOR);
671MODULE_DESCRIPTION(DRIVER_DESC);
672MODULE_LICENSE("GPL");