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