blob: 0ad061e5728ba83f5e0d40a2f5bc66107a0f2ba1 [file] [log] [blame]
Karsten Wiese030a07e2008-07-30 15:13:29 +02001/*
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
28MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
29MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
30MODULE_LICENSE("GPL");
31
32static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
33static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
34 /* Enable this card */
35static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
36
37module_param_array(index, int, NULL, 0444);
38MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
39module_param_array(id, charp, NULL, 0444);
40MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
41module_param_array(enable, bool, NULL, 0444);
42MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
43
44static int snd_us122l_card_used[SNDRV_CARDS];
45
46
47static 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)->chip.dev;
63 struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
64
Clemens Ladischd82af9f2009-11-16 12:23:46 +010065 return snd_usbmidi_create(card, iface,
66 &US122L(card)->midi_list, &quirk);
Karsten Wiese030a07e2008-07-30 15:13:29 +020067}
68
Tobias Hansen4f272342009-09-22 16:52:08 +020069static 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)->chip.dev;
85 struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
86
Clemens Ladischd82af9f2009-11-16 12:23:46 +010087 return snd_usbmidi_create(card, iface,
88 &US122L(card)->midi_list, &quirk);
Tobias Hansen4f272342009-09-22 16:52:08 +020089}
90
Karsten Wiese030a07e2008-07-30 15:13:29 +020091/*
92 * Wrapper for usb_control_msg().
93 * Allocates a temp buffer to prevent dmaing from/to the stack.
94 */
95static 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
117static 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
128static 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
135static 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;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200143
144 mutex_lock(&us122l->mutex);
145 s = us122l->sk.s;
146 if (!s)
Li Zefan428ffb72008-10-15 10:07:23 +0800147 goto unlock;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200148
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))
Li Zefan428ffb72008-10-15 10:07:23 +0800155 goto unlock;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200156
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;
Li Zefan428ffb72008-10-15 10:07:23 +0800165
166 return 0;
167unlock:
168 mutex_unlock(&us122l->mutex);
169 return VM_FAULT_SIGBUS;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200170}
171
172static 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
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400179static const struct vm_operations_struct usb_stream_hwdep_vm_ops = {
Karsten Wiese030a07e2008-07-30 15:13:29 +0200180 .open = usb_stream_hwdep_vm_open,
181 .fault = usb_stream_hwdep_vm_fault,
182 .close = usb_stream_hwdep_vm_close,
183};
184
185
186static 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;
Tobias Hansen4f272342009-09-22 16:52:08 +0200196
197 if (us122l->chip.dev->descriptor.idProduct == USB_ID_US144) {
198 iface = usb_ifnum_to_if(us122l->chip.dev, 0);
199 usb_autopm_get_interface(iface);
200 }
Karsten Wiese030a07e2008-07-30 15:13:29 +0200201 iface = usb_ifnum_to_if(us122l->chip.dev, 1);
202 usb_autopm_get_interface(iface);
203 return 0;
204}
205
206static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
207{
208 struct us122l *us122l = hw->private_data;
Tobias Hansen4f272342009-09-22 16:52:08 +0200209 struct usb_interface *iface;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200210 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
Tobias Hansen4f272342009-09-22 16:52:08 +0200211
212 if (us122l->chip.dev->descriptor.idProduct == USB_ID_US144) {
213 iface = usb_ifnum_to_if(us122l->chip.dev, 0);
214 usb_autopm_put_interface(iface);
215 }
216 iface = usb_ifnum_to_if(us122l->chip.dev, 1);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200217 usb_autopm_put_interface(iface);
218 if (us122l->first == file)
219 us122l->first = NULL;
220 mutex_lock(&us122l->mutex);
221 if (us122l->master == file)
222 us122l->master = us122l->slave;
223
224 us122l->slave = NULL;
225 mutex_unlock(&us122l->mutex);
226 return 0;
227}
228
229static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
230 struct file *filp, struct vm_area_struct *area)
231{
232 unsigned long size = area->vm_end - area->vm_start;
233 struct us122l *us122l = hw->private_data;
234 unsigned long offset;
235 struct usb_stream *s;
236 int err = 0;
237 bool read;
238
239 offset = area->vm_pgoff << PAGE_SHIFT;
240 mutex_lock(&us122l->mutex);
241 s = us122l->sk.s;
242 read = offset < s->read_size;
243 if (read && area->vm_flags & VM_WRITE) {
244 err = -EPERM;
245 goto out;
246 }
247 snd_printdd(KERN_DEBUG "%lu %u\n", size,
248 read ? s->read_size : s->write_size);
249 /* if userspace tries to mmap beyond end of our buffer, fail */
250 if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
251 snd_printk(KERN_WARNING "%lu > %u\n", size,
252 read ? s->read_size : s->write_size);
253 err = -EINVAL;
254 goto out;
255 }
256
257 area->vm_ops = &usb_stream_hwdep_vm_ops;
258 area->vm_flags |= VM_RESERVED;
259 area->vm_private_data = us122l;
260 atomic_inc(&us122l->mmap_count);
261out:
262 mutex_unlock(&us122l->mutex);
263 return err;
264}
265
266static unsigned int usb_stream_hwdep_poll(struct snd_hwdep *hw,
267 struct file *file, poll_table *wait)
268{
269 struct us122l *us122l = hw->private_data;
270 struct usb_stream *s = us122l->sk.s;
271 unsigned *polled;
272 unsigned int mask;
273
274 poll_wait(file, &us122l->sk.sleep, wait);
275
276 switch (s->state) {
277 case usb_stream_ready:
278 if (us122l->first == file)
279 polled = &s->periods_polled;
280 else
281 polled = &us122l->second_periods_polled;
282 if (*polled != s->periods_done) {
283 *polled = s->periods_done;
284 mask = POLLIN | POLLOUT | POLLWRNORM;
285 break;
286 }
287 /* Fall through */
288 mask = 0;
289 break;
290 default:
291 mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR;
292 break;
293 }
294 return mask;
295}
296
297static void us122l_stop(struct us122l *us122l)
298{
299 struct list_head *p;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100300 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200301 snd_usbmidi_input_stop(p);
302
303 usb_stream_stop(&us122l->sk);
304 usb_stream_free(&us122l->sk);
305}
306
307static int us122l_set_sample_rate(struct usb_device *dev, int rate)
308{
309 unsigned int ep = 0x81;
310 unsigned char data[3];
311 int err;
312
313 data[0] = rate;
314 data[1] = rate >> 8;
315 data[2] = rate >> 16;
316 err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
317 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
318 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000);
319 if (err < 0)
320 snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
321 dev->devnum, rate, ep);
322 return err;
323}
324
325static bool us122l_start(struct us122l *us122l,
326 unsigned rate, unsigned period_frames)
327{
328 struct list_head *p;
329 int err;
330 unsigned use_packsize = 0;
331 bool success = false;
332
333 if (us122l->chip.dev->speed == USB_SPEED_HIGH) {
334 /* The us-122l's descriptor defaults to iso max_packsize 78,
335 which isn't needed for samplerates <= 48000.
336 Lets save some memory:
337 */
338 switch (rate) {
339 case 44100:
340 use_packsize = 36;
341 break;
342 case 48000:
343 use_packsize = 42;
344 break;
345 case 88200:
346 use_packsize = 72;
347 break;
348 }
349 }
350 if (!usb_stream_new(&us122l->sk, us122l->chip.dev, 1, 2,
351 rate, use_packsize, period_frames, 6))
352 goto out;
353
354 err = us122l_set_sample_rate(us122l->chip.dev, rate);
355 if (err < 0) {
356 us122l_stop(us122l);
357 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
358 goto out;
359 }
360 err = usb_stream_start(&us122l->sk);
361 if (err < 0) {
362 us122l_stop(us122l);
363 snd_printk(KERN_ERR "us122l_start error %i \n", err);
364 goto out;
365 }
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100366 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200367 snd_usbmidi_input_start(p);
368 success = true;
369out:
370 return success;
371}
372
373static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
374 unsigned cmd, unsigned long arg)
375{
376 struct usb_stream_config *cfg;
377 struct us122l *us122l = hw->private_data;
378 unsigned min_period_frames;
379 int err = 0;
380 bool high_speed;
381
382 if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
383 return -ENOTTY;
384
Li Zefan85385c12009-04-10 09:43:59 +0800385 cfg = memdup_user((void *)arg, sizeof(*cfg));
386 if (IS_ERR(cfg))
387 return PTR_ERR(cfg);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200388
Karsten Wiese030a07e2008-07-30 15:13:29 +0200389 if (cfg->version != USB_STREAM_INTERFACE_VERSION) {
390 err = -ENXIO;
391 goto free;
392 }
393 high_speed = us122l->chip.dev->speed == USB_SPEED_HIGH;
394 if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000 &&
395 (!high_speed ||
396 (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) ||
397 cfg->frame_size != 6 ||
398 cfg->period_frames > 0x3000) {
399 err = -EINVAL;
400 goto free;
401 }
402 switch (cfg->sample_rate) {
403 case 44100:
404 min_period_frames = 48;
405 break;
406 case 48000:
407 min_period_frames = 52;
408 break;
409 default:
410 min_period_frames = 104;
411 break;
412 }
413 if (!high_speed)
414 min_period_frames <<= 1;
415 if (cfg->period_frames < min_period_frames) {
416 err = -EINVAL;
417 goto free;
418 }
419
420 snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
421
422 mutex_lock(&us122l->mutex);
423 if (!us122l->master)
424 us122l->master = file;
425 else if (us122l->master != file) {
426 if (memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg))) {
427 err = -EIO;
428 goto unlock;
429 }
430 us122l->slave = file;
431 }
432 if (!us122l->sk.s ||
433 memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg)) ||
434 us122l->sk.s->state == usb_stream_xrun) {
435 us122l_stop(us122l);
436 if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames))
437 err = -EIO;
438 else
439 err = 1;
440 }
441unlock:
442 mutex_unlock(&us122l->mutex);
443free:
444 kfree(cfg);
445 return err;
446}
447
448#define SND_USB_STREAM_ID "USB STREAM"
449static int usb_stream_hwdep_new(struct snd_card *card)
450{
451 int err;
452 struct snd_hwdep *hw;
453 struct usb_device *dev = US122L(card)->chip.dev;
454
455 err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
456 if (err < 0)
457 return err;
458
459 hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
460 hw->private_data = US122L(card);
461 hw->ops.open = usb_stream_hwdep_open;
462 hw->ops.release = usb_stream_hwdep_release;
463 hw->ops.ioctl = usb_stream_hwdep_ioctl;
464 hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
465 hw->ops.mmap = usb_stream_hwdep_mmap;
466 hw->ops.poll = usb_stream_hwdep_poll;
467
468 sprintf(hw->name, "/proc/bus/usb/%03d/%03d/hwdeppcm",
469 dev->bus->busnum, dev->devnum);
470 return 0;
471}
472
473
474static bool us122l_create_card(struct snd_card *card)
475{
476 int err;
477 struct us122l *us122l = US122L(card);
478
Tobias Hansen4f272342009-09-22 16:52:08 +0200479 if (us122l->chip.dev->descriptor.idProduct == USB_ID_US144) {
480 err = usb_set_interface(us122l->chip.dev, 0, 1);
481 if (err) {
482 snd_printk(KERN_ERR "usb_set_interface error \n");
483 return false;
484 }
485 }
Karsten Wiese030a07e2008-07-30 15:13:29 +0200486 err = usb_set_interface(us122l->chip.dev, 1, 1);
487 if (err) {
488 snd_printk(KERN_ERR "usb_set_interface error \n");
489 return false;
490 }
491
492 pt_info_set(us122l->chip.dev, 0x11);
493 pt_info_set(us122l->chip.dev, 0x10);
494
495 if (!us122l_start(us122l, 44100, 256))
496 return false;
497
Tobias Hansen4f272342009-09-22 16:52:08 +0200498 if (us122l->chip.dev->descriptor.idProduct == USB_ID_US144)
499 err = us144_create_usbmidi(card);
500 else
501 err = us122l_create_usbmidi(card);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200502 if (err < 0) {
503 snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err);
504 us122l_stop(us122l);
505 return false;
506 }
507 err = usb_stream_hwdep_new(card);
508 if (err < 0) {
509/* release the midi resources */
510 struct list_head *p;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100511 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200512 snd_usbmidi_disconnect(p);
513
514 us122l_stop(us122l);
515 return false;
516 }
517 return true;
518}
519
Karsten Wiese5d4af1b2009-04-20 13:01:21 +0200520static void snd_us122l_free(struct snd_card *card)
521{
522 struct us122l *us122l = US122L(card);
523 int index = us122l->chip.index;
524 if (index >= 0 && index < SNDRV_CARDS)
525 snd_us122l_card_used[index] = 0;
526}
527
Takashi Iwai51721f72008-12-28 16:55:08 +0100528static int usx2y_create_card(struct usb_device *device, struct snd_card **cardp)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200529{
530 int dev;
531 struct snd_card *card;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100532 int err;
533
Karsten Wiese030a07e2008-07-30 15:13:29 +0200534 for (dev = 0; dev < SNDRV_CARDS; ++dev)
535 if (enable[dev] && !snd_us122l_card_used[dev])
536 break;
537 if (dev >= SNDRV_CARDS)
Takashi Iwai51721f72008-12-28 16:55:08 +0100538 return -ENODEV;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100539 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
540 sizeof(struct us122l), &card);
541 if (err < 0)
Takashi Iwai51721f72008-12-28 16:55:08 +0100542 return err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200543 snd_us122l_card_used[US122L(card)->chip.index = dev] = 1;
Karsten Wiese5d4af1b2009-04-20 13:01:21 +0200544 card->private_free = snd_us122l_free;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200545 US122L(card)->chip.dev = device;
546 US122L(card)->chip.card = card;
547 mutex_init(&US122L(card)->mutex);
548 init_waitqueue_head(&US122L(card)->sk.sleep);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100549 INIT_LIST_HEAD(&US122L(card)->midi_list);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200550 strcpy(card->driver, "USB "NAME_ALLCAPS"");
551 sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
552 sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
553 card->shortname,
554 le16_to_cpu(device->descriptor.idVendor),
555 le16_to_cpu(device->descriptor.idProduct),
556 0,
557 US122L(card)->chip.dev->bus->busnum,
558 US122L(card)->chip.dev->devnum
559 );
Takashi Iwai51721f72008-12-28 16:55:08 +0100560 *cardp = card;
561 return 0;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200562}
563
Takashi Iwai51721f72008-12-28 16:55:08 +0100564static int us122l_usb_probe(struct usb_interface *intf,
565 const struct usb_device_id *device_id,
566 struct snd_card **cardp)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200567{
568 struct usb_device *device = interface_to_usbdev(intf);
Takashi Iwai51721f72008-12-28 16:55:08 +0100569 struct snd_card *card;
570 int err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200571
Takashi Iwai51721f72008-12-28 16:55:08 +0100572 err = usx2y_create_card(device, &card);
573 if (err < 0)
574 return err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200575
Takashi Iwai3f5d3462009-07-02 11:51:44 +0200576 snd_card_set_dev(card, &intf->dev);
Takashi Iwai51721f72008-12-28 16:55:08 +0100577 if (!us122l_create_card(card)) {
Karsten Wiese030a07e2008-07-30 15:13:29 +0200578 snd_card_free(card);
Takashi Iwai51721f72008-12-28 16:55:08 +0100579 return -EINVAL;
580 }
581
582 err = snd_card_register(card);
583 if (err < 0) {
584 snd_card_free(card);
585 return err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200586 }
587
Tobias Hansen4f272342009-09-22 16:52:08 +0200588 usb_get_intf(usb_ifnum_to_if(device, 0));
Karsten Wiese030a07e2008-07-30 15:13:29 +0200589 usb_get_dev(device);
Takashi Iwai51721f72008-12-28 16:55:08 +0100590 *cardp = card;
591 return 0;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200592}
593
594static int snd_us122l_probe(struct usb_interface *intf,
595 const struct usb_device_id *id)
596{
Tobias Hansen4f272342009-09-22 16:52:08 +0200597 struct usb_device *device = interface_to_usbdev(intf);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200598 struct snd_card *card;
Takashi Iwai51721f72008-12-28 16:55:08 +0100599 int err;
600
Tobias Hansen4f272342009-09-22 16:52:08 +0200601 if (device->descriptor.idProduct == USB_ID_US144
602 && device->speed == USB_SPEED_HIGH) {
603 snd_printk(KERN_ERR "disable ehci-hcd to run US-144 \n");
Tobias Hansena688e482009-10-12 16:24:15 +0200604 return -ENODEV;
Tobias Hansen4f272342009-09-22 16:52:08 +0200605 }
606
Karsten Wiese030a07e2008-07-30 15:13:29 +0200607 snd_printdd(KERN_DEBUG"%p:%i\n",
608 intf, intf->cur_altsetting->desc.bInterfaceNumber);
609 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
610 return 0;
611
Takashi Iwai51721f72008-12-28 16:55:08 +0100612 err = us122l_usb_probe(usb_get_intf(intf), id, &card);
613 if (err < 0) {
614 usb_put_intf(intf);
615 return err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200616 }
617
Takashi Iwai51721f72008-12-28 16:55:08 +0100618 usb_set_intfdata(intf, card);
619 return 0;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200620}
621
622static void snd_us122l_disconnect(struct usb_interface *intf)
623{
624 struct snd_card *card;
625 struct us122l *us122l;
626 struct list_head *p;
627
628 card = usb_get_intfdata(intf);
629 if (!card)
630 return;
631
632 snd_card_disconnect(card);
633
634 us122l = US122L(card);
635 mutex_lock(&us122l->mutex);
636 us122l_stop(us122l);
637 mutex_unlock(&us122l->mutex);
638 us122l->chip.shutdown = 1;
639
640/* release the midi resources */
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100641 list_for_each(p, &us122l->midi_list) {
Karsten Wiese030a07e2008-07-30 15:13:29 +0200642 snd_usbmidi_disconnect(p);
643 }
644
Tobias Hansen4f272342009-09-22 16:52:08 +0200645 usb_put_intf(usb_ifnum_to_if(us122l->chip.dev, 0));
646 usb_put_intf(usb_ifnum_to_if(us122l->chip.dev, 1));
Karsten Wiese5d4af1b2009-04-20 13:01:21 +0200647 usb_put_dev(us122l->chip.dev);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200648
649 while (atomic_read(&us122l->mmap_count))
650 msleep(500);
651
652 snd_card_free(card);
653}
654
655static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
656{
657 struct snd_card *card;
658 struct us122l *us122l;
659 struct list_head *p;
660
Julia Lawallf4e97492009-01-01 18:14:35 +0100661 card = usb_get_intfdata(intf);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200662 if (!card)
663 return 0;
664 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
665
666 us122l = US122L(card);
667 if (!us122l)
668 return 0;
669
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100670 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200671 snd_usbmidi_input_stop(p);
672
673 mutex_lock(&us122l->mutex);
674 usb_stream_stop(&us122l->sk);
675 mutex_unlock(&us122l->mutex);
676
677 return 0;
678}
679
680static int snd_us122l_resume(struct usb_interface *intf)
681{
682 struct snd_card *card;
683 struct us122l *us122l;
684 struct list_head *p;
685 int err;
686
Julia Lawallf4e97492009-01-01 18:14:35 +0100687 card = usb_get_intfdata(intf);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200688 if (!card)
689 return 0;
690
691 us122l = US122L(card);
692 if (!us122l)
693 return 0;
694
695 mutex_lock(&us122l->mutex);
696 /* needed, doesn't restart without: */
Tobias Hansen4f272342009-09-22 16:52:08 +0200697 if (us122l->chip.dev->descriptor.idProduct == USB_ID_US144) {
698 err = usb_set_interface(us122l->chip.dev, 0, 1);
699 if (err) {
700 snd_printk(KERN_ERR "usb_set_interface error \n");
701 goto unlock;
702 }
703 }
Karsten Wiese030a07e2008-07-30 15:13:29 +0200704 err = usb_set_interface(us122l->chip.dev, 1, 1);
705 if (err) {
706 snd_printk(KERN_ERR "usb_set_interface error \n");
707 goto unlock;
708 }
709
710 pt_info_set(us122l->chip.dev, 0x11);
711 pt_info_set(us122l->chip.dev, 0x10);
712
713 err = us122l_set_sample_rate(us122l->chip.dev,
714 us122l->sk.s->cfg.sample_rate);
715 if (err < 0) {
716 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
717 goto unlock;
718 }
719 err = usb_stream_start(&us122l->sk);
720 if (err)
721 goto unlock;
722
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100723 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200724 snd_usbmidi_input_start(p);
725unlock:
726 mutex_unlock(&us122l->mutex);
727 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
728 return err;
729}
730
731static struct usb_device_id snd_us122l_usb_id_table[] = {
732 {
733 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
734 .idVendor = 0x0644,
735 .idProduct = USB_ID_US122L
736 },
Tobias Hansen4f272342009-09-22 16:52:08 +0200737 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
738 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
739 .idVendor = 0x0644,
740 .idProduct = USB_ID_US144
741 },
Karsten Wiese030a07e2008-07-30 15:13:29 +0200742 { /* terminator */ }
743};
744
745MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
746static struct usb_driver snd_us122l_usb_driver = {
747 .name = "snd-usb-us122l",
748 .probe = snd_us122l_probe,
749 .disconnect = snd_us122l_disconnect,
750 .suspend = snd_us122l_suspend,
751 .resume = snd_us122l_resume,
752 .reset_resume = snd_us122l_resume,
753 .id_table = snd_us122l_usb_id_table,
754 .supports_autosuspend = 1
755};
756
757
758static int __init snd_us122l_module_init(void)
759{
760 return usb_register(&snd_us122l_usb_driver);
761}
762
763static void __exit snd_us122l_module_exit(void)
764{
765 usb_deregister(&snd_us122l_usb_driver);
766}
767
768module_init(snd_us122l_module_init)
769module_exit(snd_us122l_module_exit)