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