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