xref: /linux/sound/usb/usx2y/us122l.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
4  */
5 
6 #include <linux/slab.h>
7 #include <linux/usb.h>
8 #include <linux/usb/audio.h>
9 #include <linux/module.h>
10 #include <sound/core.h>
11 #include <sound/hwdep.h>
12 #include <sound/pcm.h>
13 #include <sound/initval.h>
14 #define MODNAME "US122L"
15 #include "usb_stream.c"
16 #include "../usbaudio.h"
17 #include "../midi.h"
18 #include "us122l.h"
19 
20 MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
21 MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
22 MODULE_LICENSE("GPL");
23 
24 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-max */
25 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* Id for this card */
26 							/* Enable this card */
27 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
28 
29 module_param_array(index, int, NULL, 0444);
30 MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
31 module_param_array(id, charp, NULL, 0444);
32 MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
33 module_param_array(enable, bool, NULL, 0444);
34 MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
35 
36 /* driver_info flags */
37 #define US122L_FLAG_US144	BIT(0)
38 
39 static int snd_us122l_card_used[SNDRV_CARDS];
40 
41 static int us122l_create_usbmidi(struct snd_card *card)
42 {
43 	static struct snd_usb_midi_endpoint_info quirk_data = {
44 		.out_ep = 4,
45 		.in_ep = 3,
46 		.out_cables =	0x001,
47 		.in_cables =	0x001
48 	};
49 	static struct snd_usb_audio_quirk quirk = {
50 		.vendor_name =	"US122L",
51 		.product_name =	NAME_ALLCAPS,
52 		.ifnum = 	1,
53 		.type = QUIRK_MIDI_US122L,
54 		.data = &quirk_data
55 	};
56 	struct usb_device *dev = US122L(card)->dev;
57 	struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
58 
59 	return snd_usbmidi_create(card, iface,
60 				  &US122L(card)->midi_list, &quirk);
61 }
62 
63 static int us144_create_usbmidi(struct snd_card *card)
64 {
65 	static struct snd_usb_midi_endpoint_info quirk_data = {
66 		.out_ep = 4,
67 		.in_ep = 3,
68 		.out_cables =	0x001,
69 		.in_cables =	0x001
70 	};
71 	static struct snd_usb_audio_quirk quirk = {
72 		.vendor_name =	"US144",
73 		.product_name =	NAME_ALLCAPS,
74 		.ifnum = 	0,
75 		.type = QUIRK_MIDI_US122L,
76 		.data = &quirk_data
77 	};
78 	struct usb_device *dev = US122L(card)->dev;
79 	struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
80 
81 	return snd_usbmidi_create(card, iface,
82 				  &US122L(card)->midi_list, &quirk);
83 }
84 
85 /*
86  * Wrapper for usb_control_msg().
87  * Allocates a temp buffer to prevent dmaing from/to the stack.
88  */
89 static int us122l_ctl_msg(struct usb_device *dev, unsigned int pipe,
90 			  __u8 request, __u8 requesttype,
91 			  __u16 value, __u16 index, void *data,
92 			  __u16 size, int timeout)
93 {
94 	int err;
95 	void *buf = NULL;
96 
97 	if (size > 0) {
98 		buf = kmemdup(data, size, GFP_KERNEL);
99 		if (!buf)
100 			return -ENOMEM;
101 	}
102 	err = usb_control_msg(dev, pipe, request, requesttype,
103 			      value, index, buf, size, timeout);
104 	if (size > 0) {
105 		memcpy(data, buf, size);
106 		kfree(buf);
107 	}
108 	return err;
109 }
110 
111 static void pt_info_set(struct usb_device *dev, u8 v)
112 {
113 	int ret;
114 
115 	ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
116 			      'I',
117 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
118 			      v, 0, NULL, 0, 1000);
119 	snd_printdd(KERN_DEBUG "%i\n", ret);
120 }
121 
122 static void usb_stream_hwdep_vm_open(struct vm_area_struct *area)
123 {
124 	struct us122l *us122l = area->vm_private_data;
125 	atomic_inc(&us122l->mmap_count);
126 	snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
127 }
128 
129 static vm_fault_t usb_stream_hwdep_vm_fault(struct vm_fault *vmf)
130 {
131 	unsigned long offset;
132 	struct page *page;
133 	void *vaddr;
134 	struct us122l *us122l = vmf->vma->vm_private_data;
135 	struct usb_stream *s;
136 
137 	mutex_lock(&us122l->mutex);
138 	s = us122l->sk.s;
139 	if (!s)
140 		goto unlock;
141 
142 	offset = vmf->pgoff << PAGE_SHIFT;
143 	if (offset < PAGE_ALIGN(s->read_size))
144 		vaddr = (char *)s + offset;
145 	else {
146 		offset -= PAGE_ALIGN(s->read_size);
147 		if (offset >= PAGE_ALIGN(s->write_size))
148 			goto unlock;
149 
150 		vaddr = us122l->sk.write_page + offset;
151 	}
152 	page = virt_to_page(vaddr);
153 
154 	get_page(page);
155 	mutex_unlock(&us122l->mutex);
156 
157 	vmf->page = page;
158 
159 	return 0;
160 unlock:
161 	mutex_unlock(&us122l->mutex);
162 	return VM_FAULT_SIGBUS;
163 }
164 
165 static void usb_stream_hwdep_vm_close(struct vm_area_struct *area)
166 {
167 	struct us122l *us122l = area->vm_private_data;
168 	atomic_dec(&us122l->mmap_count);
169 	snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
170 }
171 
172 static const struct vm_operations_struct usb_stream_hwdep_vm_ops = {
173 	.open = usb_stream_hwdep_vm_open,
174 	.fault = usb_stream_hwdep_vm_fault,
175 	.close = usb_stream_hwdep_vm_close,
176 };
177 
178 
179 static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
180 {
181 	struct us122l	*us122l = hw->private_data;
182 	struct usb_interface *iface;
183 	snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
184 	if (hw->used >= 2)
185 		return -EBUSY;
186 
187 	if (!us122l->first)
188 		us122l->first = file;
189 
190 	if (us122l->is_us144) {
191 		iface = usb_ifnum_to_if(us122l->dev, 0);
192 		usb_autopm_get_interface(iface);
193 	}
194 	iface = usb_ifnum_to_if(us122l->dev, 1);
195 	usb_autopm_get_interface(iface);
196 	return 0;
197 }
198 
199 static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
200 {
201 	struct us122l	*us122l = hw->private_data;
202 	struct usb_interface *iface;
203 	snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
204 
205 	if (us122l->is_us144) {
206 		iface = usb_ifnum_to_if(us122l->dev, 0);
207 		usb_autopm_put_interface(iface);
208 	}
209 	iface = usb_ifnum_to_if(us122l->dev, 1);
210 	usb_autopm_put_interface(iface);
211 	if (us122l->first == file)
212 		us122l->first = NULL;
213 	mutex_lock(&us122l->mutex);
214 	if (us122l->master == file)
215 		us122l->master = us122l->slave;
216 
217 	us122l->slave = NULL;
218 	mutex_unlock(&us122l->mutex);
219 	return 0;
220 }
221 
222 static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
223 				 struct file *filp, struct vm_area_struct *area)
224 {
225 	unsigned long	size = area->vm_end - area->vm_start;
226 	struct us122l	*us122l = hw->private_data;
227 	unsigned long offset;
228 	struct usb_stream *s;
229 	int err = 0;
230 	bool read;
231 
232 	offset = area->vm_pgoff << PAGE_SHIFT;
233 	mutex_lock(&us122l->mutex);
234 	s = us122l->sk.s;
235 	read = offset < s->read_size;
236 	if (read && area->vm_flags & VM_WRITE) {
237 		err = -EPERM;
238 		goto out;
239 	}
240 	snd_printdd(KERN_DEBUG "%lu %u\n", size,
241 		    read ? s->read_size : s->write_size);
242 	/* if userspace tries to mmap beyond end of our buffer, fail */
243 	if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
244 		snd_printk(KERN_WARNING "%lu > %u\n", size,
245 			   read ? s->read_size : s->write_size);
246 		err = -EINVAL;
247 		goto out;
248 	}
249 
250 	area->vm_ops = &usb_stream_hwdep_vm_ops;
251 	area->vm_flags |= VM_DONTDUMP;
252 	if (!read)
253 		area->vm_flags |= VM_DONTEXPAND;
254 	area->vm_private_data = us122l;
255 	atomic_inc(&us122l->mmap_count);
256 out:
257 	mutex_unlock(&us122l->mutex);
258 	return err;
259 }
260 
261 static __poll_t usb_stream_hwdep_poll(struct snd_hwdep *hw,
262 					  struct file *file, poll_table *wait)
263 {
264 	struct us122l	*us122l = hw->private_data;
265 	unsigned	*polled;
266 	__poll_t	mask;
267 
268 	poll_wait(file, &us122l->sk.sleep, wait);
269 
270 	mask = EPOLLIN | EPOLLOUT | EPOLLWRNORM | EPOLLERR;
271 	if (mutex_trylock(&us122l->mutex)) {
272 		struct usb_stream *s = us122l->sk.s;
273 		if (s && s->state == usb_stream_ready) {
274 			if (us122l->first == file)
275 				polled = &s->periods_polled;
276 			else
277 				polled = &us122l->second_periods_polled;
278 			if (*polled != s->periods_done) {
279 				*polled = s->periods_done;
280 				mask = EPOLLIN | EPOLLOUT | EPOLLWRNORM;
281 			} else
282 				mask = 0;
283 		}
284 		mutex_unlock(&us122l->mutex);
285 	}
286 	return mask;
287 }
288 
289 static void us122l_stop(struct us122l *us122l)
290 {
291 	struct list_head *p;
292 	list_for_each(p, &us122l->midi_list)
293 		snd_usbmidi_input_stop(p);
294 
295 	usb_stream_stop(&us122l->sk);
296 	usb_stream_free(&us122l->sk);
297 }
298 
299 static int us122l_set_sample_rate(struct usb_device *dev, int rate)
300 {
301 	unsigned int ep = 0x81;
302 	unsigned char data[3];
303 	int err;
304 
305 	data[0] = rate;
306 	data[1] = rate >> 8;
307 	data[2] = rate >> 16;
308 	err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
309 			     USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
310 			     UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000);
311 	if (err < 0)
312 		snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
313 			   dev->devnum, rate, ep);
314 	return err;
315 }
316 
317 static bool us122l_start(struct us122l *us122l,
318 			 unsigned rate, unsigned period_frames)
319 {
320 	struct list_head *p;
321 	int err;
322 	unsigned use_packsize = 0;
323 	bool success = false;
324 
325 	if (us122l->dev->speed == USB_SPEED_HIGH) {
326 		/* The us-122l's descriptor defaults to iso max_packsize 78,
327 		   which isn't needed for samplerates <= 48000.
328 		   Lets save some memory:
329 		*/
330 		switch (rate) {
331 		case 44100:
332 			use_packsize = 36;
333 			break;
334 		case 48000:
335 			use_packsize = 42;
336 			break;
337 		case 88200:
338 			use_packsize = 72;
339 			break;
340 		}
341 	}
342 	if (!usb_stream_new(&us122l->sk, us122l->dev, 1, 2,
343 			    rate, use_packsize, period_frames, 6))
344 		goto out;
345 
346 	err = us122l_set_sample_rate(us122l->dev, rate);
347 	if (err < 0) {
348 		us122l_stop(us122l);
349 		snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
350 		goto out;
351 	}
352 	err = usb_stream_start(&us122l->sk);
353 	if (err < 0) {
354 		us122l_stop(us122l);
355 		snd_printk(KERN_ERR "us122l_start error %i \n", err);
356 		goto out;
357 	}
358 	list_for_each(p, &us122l->midi_list)
359 		snd_usbmidi_input_start(p);
360 	success = true;
361 out:
362 	return success;
363 }
364 
365 static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
366 				  unsigned cmd, unsigned long arg)
367 {
368 	struct usb_stream_config cfg;
369 	struct us122l *us122l = hw->private_data;
370 	struct usb_stream *s;
371 	unsigned min_period_frames;
372 	int err = 0;
373 	bool high_speed;
374 
375 	if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
376 		return -ENOTTY;
377 
378 	if (copy_from_user(&cfg, (void __user *)arg, sizeof(cfg)))
379 		return -EFAULT;
380 
381 	if (cfg.version != USB_STREAM_INTERFACE_VERSION)
382 		return -ENXIO;
383 
384 	high_speed = us122l->dev->speed == USB_SPEED_HIGH;
385 	if ((cfg.sample_rate != 44100 && cfg.sample_rate != 48000  &&
386 	     (!high_speed ||
387 	      (cfg.sample_rate != 88200 && cfg.sample_rate != 96000))) ||
388 	    cfg.frame_size != 6 ||
389 	    cfg.period_frames > 0x3000)
390 		return -EINVAL;
391 
392 	switch (cfg.sample_rate) {
393 	case 44100:
394 		min_period_frames = 48;
395 		break;
396 	case 48000:
397 		min_period_frames = 52;
398 		break;
399 	default:
400 		min_period_frames = 104;
401 		break;
402 	}
403 	if (!high_speed)
404 		min_period_frames <<= 1;
405 	if (cfg.period_frames < min_period_frames)
406 		return -EINVAL;
407 
408 	snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
409 
410 	mutex_lock(&us122l->mutex);
411 	s = us122l->sk.s;
412 	if (!us122l->master)
413 		us122l->master = file;
414 	else if (us122l->master != file) {
415 		if (!s || memcmp(&cfg, &s->cfg, sizeof(cfg))) {
416 			err = -EIO;
417 			goto unlock;
418 		}
419 		us122l->slave = file;
420 	}
421 	if (!s || memcmp(&cfg, &s->cfg, sizeof(cfg)) ||
422 	    s->state == usb_stream_xrun) {
423 		us122l_stop(us122l);
424 		if (!us122l_start(us122l, cfg.sample_rate, cfg.period_frames))
425 			err = -EIO;
426 		else
427 			err = 1;
428 	}
429 unlock:
430 	mutex_unlock(&us122l->mutex);
431 	wake_up_all(&us122l->sk.sleep);
432 	return err;
433 }
434 
435 #define SND_USB_STREAM_ID "USB STREAM"
436 static int usb_stream_hwdep_new(struct snd_card *card)
437 {
438 	int err;
439 	struct snd_hwdep *hw;
440 	struct usb_device *dev = US122L(card)->dev;
441 
442 	err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
443 	if (err < 0)
444 		return err;
445 
446 	hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
447 	hw->private_data = US122L(card);
448 	hw->ops.open = usb_stream_hwdep_open;
449 	hw->ops.release = usb_stream_hwdep_release;
450 	hw->ops.ioctl = usb_stream_hwdep_ioctl;
451 	hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
452 	hw->ops.mmap = usb_stream_hwdep_mmap;
453 	hw->ops.poll = usb_stream_hwdep_poll;
454 
455 	sprintf(hw->name, "/dev/bus/usb/%03d/%03d/hwdeppcm",
456 		dev->bus->busnum, dev->devnum);
457 	return 0;
458 }
459 
460 
461 static bool us122l_create_card(struct snd_card *card)
462 {
463 	int err;
464 	struct us122l *us122l = US122L(card);
465 
466 	if (us122l->is_us144) {
467 		err = usb_set_interface(us122l->dev, 0, 1);
468 		if (err) {
469 			snd_printk(KERN_ERR "usb_set_interface error \n");
470 			return false;
471 		}
472 	}
473 	err = usb_set_interface(us122l->dev, 1, 1);
474 	if (err) {
475 		snd_printk(KERN_ERR "usb_set_interface error \n");
476 		return false;
477 	}
478 
479 	pt_info_set(us122l->dev, 0x11);
480 	pt_info_set(us122l->dev, 0x10);
481 
482 	if (!us122l_start(us122l, 44100, 256))
483 		return false;
484 
485 	if (us122l->is_us144)
486 		err = us144_create_usbmidi(card);
487 	else
488 		err = us122l_create_usbmidi(card);
489 	if (err < 0) {
490 		snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err);
491 		goto stop;
492 	}
493 	err = usb_stream_hwdep_new(card);
494 	if (err < 0) {
495 /* release the midi resources */
496 		struct list_head *p;
497 		list_for_each(p, &us122l->midi_list)
498 			snd_usbmidi_disconnect(p);
499 
500 		goto stop;
501 	}
502 	return true;
503 
504 stop:
505 	us122l_stop(us122l);
506 	return false;
507 }
508 
509 static void snd_us122l_free(struct snd_card *card)
510 {
511 	struct us122l	*us122l = US122L(card);
512 	int		index = us122l->card_index;
513 	if (index >= 0  &&  index < SNDRV_CARDS)
514 		snd_us122l_card_used[index] = 0;
515 }
516 
517 static int usx2y_create_card(struct usb_device *device,
518 			     struct usb_interface *intf,
519 			     struct snd_card **cardp,
520 			     unsigned long flags)
521 {
522 	int		dev;
523 	struct snd_card *card;
524 	int err;
525 
526 	for (dev = 0; dev < SNDRV_CARDS; ++dev)
527 		if (enable[dev] && !snd_us122l_card_used[dev])
528 			break;
529 	if (dev >= SNDRV_CARDS)
530 		return -ENODEV;
531 	err = snd_card_new(&intf->dev, index[dev], id[dev], THIS_MODULE,
532 			   sizeof(struct us122l), &card);
533 	if (err < 0)
534 		return err;
535 	snd_us122l_card_used[US122L(card)->card_index = dev] = 1;
536 	card->private_free = snd_us122l_free;
537 	US122L(card)->dev = device;
538 	mutex_init(&US122L(card)->mutex);
539 	init_waitqueue_head(&US122L(card)->sk.sleep);
540 	US122L(card)->is_us144 = flags & US122L_FLAG_US144;
541 	INIT_LIST_HEAD(&US122L(card)->midi_list);
542 	strcpy(card->driver, "USB "NAME_ALLCAPS"");
543 	sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
544 	sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
545 		card->shortname,
546 		le16_to_cpu(device->descriptor.idVendor),
547 		le16_to_cpu(device->descriptor.idProduct),
548 		0,
549 		US122L(card)->dev->bus->busnum,
550 		US122L(card)->dev->devnum
551 		);
552 	*cardp = card;
553 	return 0;
554 }
555 
556 static int us122l_usb_probe(struct usb_interface *intf,
557 			    const struct usb_device_id *device_id,
558 			    struct snd_card **cardp)
559 {
560 	struct usb_device *device = interface_to_usbdev(intf);
561 	struct snd_card *card;
562 	int err;
563 
564 	err = usx2y_create_card(device, intf, &card, device_id->driver_info);
565 	if (err < 0)
566 		return err;
567 
568 	if (!us122l_create_card(card)) {
569 		snd_card_free(card);
570 		return -EINVAL;
571 	}
572 
573 	err = snd_card_register(card);
574 	if (err < 0) {
575 		snd_card_free(card);
576 		return err;
577 	}
578 
579 	usb_get_intf(usb_ifnum_to_if(device, 0));
580 	usb_get_dev(device);
581 	*cardp = card;
582 	return 0;
583 }
584 
585 static int snd_us122l_probe(struct usb_interface *intf,
586 			    const struct usb_device_id *id)
587 {
588 	struct usb_device *device = interface_to_usbdev(intf);
589 	struct snd_card *card;
590 	int err;
591 
592 	if (id->driver_info & US122L_FLAG_US144 &&
593 			device->speed == USB_SPEED_HIGH) {
594 		snd_printk(KERN_ERR "disable ehci-hcd to run US-144 \n");
595 		return -ENODEV;
596 	}
597 
598 	snd_printdd(KERN_DEBUG"%p:%i\n",
599 		    intf, intf->cur_altsetting->desc.bInterfaceNumber);
600 	if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
601 		return 0;
602 
603 	err = us122l_usb_probe(usb_get_intf(intf), id, &card);
604 	if (err < 0) {
605 		usb_put_intf(intf);
606 		return err;
607 	}
608 
609 	usb_set_intfdata(intf, card);
610 	return 0;
611 }
612 
613 static void snd_us122l_disconnect(struct usb_interface *intf)
614 {
615 	struct snd_card *card;
616 	struct us122l *us122l;
617 	struct list_head *p;
618 
619 	card = usb_get_intfdata(intf);
620 	if (!card)
621 		return;
622 
623 	snd_card_disconnect(card);
624 
625 	us122l = US122L(card);
626 	mutex_lock(&us122l->mutex);
627 	us122l_stop(us122l);
628 	mutex_unlock(&us122l->mutex);
629 
630 /* release the midi resources */
631 	list_for_each(p, &us122l->midi_list) {
632 		snd_usbmidi_disconnect(p);
633 	}
634 
635 	usb_put_intf(usb_ifnum_to_if(us122l->dev, 0));
636 	usb_put_intf(usb_ifnum_to_if(us122l->dev, 1));
637 	usb_put_dev(us122l->dev);
638 
639 	while (atomic_read(&us122l->mmap_count))
640 		msleep(500);
641 
642 	snd_card_free(card);
643 }
644 
645 static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
646 {
647 	struct snd_card *card;
648 	struct us122l *us122l;
649 	struct list_head *p;
650 
651 	card = usb_get_intfdata(intf);
652 	if (!card)
653 		return 0;
654 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
655 
656 	us122l = US122L(card);
657 	if (!us122l)
658 		return 0;
659 
660 	list_for_each(p, &us122l->midi_list)
661 		snd_usbmidi_input_stop(p);
662 
663 	mutex_lock(&us122l->mutex);
664 	usb_stream_stop(&us122l->sk);
665 	mutex_unlock(&us122l->mutex);
666 
667 	return 0;
668 }
669 
670 static int snd_us122l_resume(struct usb_interface *intf)
671 {
672 	struct snd_card *card;
673 	struct us122l *us122l;
674 	struct list_head *p;
675 	int err;
676 
677 	card = usb_get_intfdata(intf);
678 	if (!card)
679 		return 0;
680 
681 	us122l = US122L(card);
682 	if (!us122l)
683 		return 0;
684 
685 	mutex_lock(&us122l->mutex);
686 	/* needed, doesn't restart without: */
687 	if (us122l->is_us144) {
688 		err = usb_set_interface(us122l->dev, 0, 1);
689 		if (err) {
690 			snd_printk(KERN_ERR "usb_set_interface error \n");
691 			goto unlock;
692 		}
693 	}
694 	err = usb_set_interface(us122l->dev, 1, 1);
695 	if (err) {
696 		snd_printk(KERN_ERR "usb_set_interface error \n");
697 		goto unlock;
698 	}
699 
700 	pt_info_set(us122l->dev, 0x11);
701 	pt_info_set(us122l->dev, 0x10);
702 
703 	err = us122l_set_sample_rate(us122l->dev,
704 				     us122l->sk.s->cfg.sample_rate);
705 	if (err < 0) {
706 		snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
707 		goto unlock;
708 	}
709 	err = usb_stream_start(&us122l->sk);
710 	if (err)
711 		goto unlock;
712 
713 	list_for_each(p, &us122l->midi_list)
714 		snd_usbmidi_input_start(p);
715 unlock:
716 	mutex_unlock(&us122l->mutex);
717 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
718 	return err;
719 }
720 
721 static const struct usb_device_id snd_us122l_usb_id_table[] = {
722 	{
723 		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
724 		.idVendor =	0x0644,
725 		.idProduct =	USB_ID_US122L
726 	},
727 	{	/* US-144 only works at USB1.1! Disable module ehci-hcd. */
728 		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
729 		.idVendor =	0x0644,
730 		.idProduct =	USB_ID_US144,
731 		.driver_info =	US122L_FLAG_US144
732 	},
733 	{
734 		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
735 		.idVendor =	0x0644,
736 		.idProduct =	USB_ID_US122MKII
737 	},
738 	{
739 		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
740 		.idVendor =	0x0644,
741 		.idProduct =	USB_ID_US144MKII,
742 		.driver_info =	US122L_FLAG_US144
743 	},
744 	{ /* terminator */ }
745 };
746 
747 MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
748 static struct usb_driver snd_us122l_usb_driver = {
749 	.name =		"snd-usb-us122l",
750 	.probe =	snd_us122l_probe,
751 	.disconnect =	snd_us122l_disconnect,
752 	.suspend =	snd_us122l_suspend,
753 	.resume =	snd_us122l_resume,
754 	.reset_resume =	snd_us122l_resume,
755 	.id_table =	snd_us122l_usb_id_table,
756 	.supports_autosuspend = 1
757 };
758 
759 module_usb_driver(snd_us122l_usb_driver);
760