aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sound/usb/Makefile13
-rw-r--r--sound/usb/card.c648
-rw-r--r--sound/usb/card.h105
-rw-r--r--sound/usb/debug.h15
-rw-r--r--sound/usb/endpoint.c358
-rw-r--r--sound/usb/endpoint.h11
-rw-r--r--sound/usb/format.c445
-rw-r--r--sound/usb/format.h8
-rw-r--r--sound/usb/helper.c112
-rw-r--r--sound/usb/helper.h32
-rw-r--r--sound/usb/midi.c (renamed from sound/usb/usbmidi.c)4
-rw-r--r--sound/usb/midi.h (renamed from sound/usb/usbmidi.h)0
-rw-r--r--sound/usb/misc/ua101.c2
-rw-r--r--sound/usb/pcm.c845
-rw-r--r--sound/usb/pcm.h14
-rw-r--r--sound/usb/proc.c163
-rw-r--r--sound/usb/proc.h8
-rw-r--r--sound/usb/quirks-table.h (renamed from sound/usb/usbquirks.h)0
-rw-r--r--sound/usb/quirks.c592
-rw-r--r--sound/usb/quirks.h23
-rw-r--r--sound/usb/urb.c989
-rw-r--r--sound/usb/urb.h21
-rw-r--r--sound/usb/usbaudio.c4051
-rw-r--r--sound/usb/usbaudio.h42
-rw-r--r--sound/usb/usbmixer.c1
-rw-r--r--sound/usb/usx2y/us122l.c2
-rw-r--r--sound/usb/usx2y/usbusx2y.h2
27 files changed, 4411 insertions, 4095 deletions
diff --git a/sound/usb/Makefile b/sound/usb/Makefile
index 423d829056f1..0758d8dc8cde 100644
--- a/sound/usb/Makefile
+++ b/sound/usb/Makefile
@@ -2,8 +2,17 @@
2# Makefile for ALSA 2# Makefile for ALSA
3# 3#
4 4
5snd-usb-audio-objs := usbaudio.o usbmixer.o 5snd-usb-audio-objs := card.o \
6snd-usbmidi-lib-objs := usbmidi.o 6 usbmixer.o \
7 proc.o \
8 quirks.o \
9 format.o \
10 endpoint.o \
11 urb.o \
12 pcm.o \
13 helper.o
14
15snd-usbmidi-lib-objs := midi.o
7 16
8# Toplevel Module Dependency 17# Toplevel Module Dependency
9obj-$(CONFIG_SND_USB_AUDIO) += snd-usb-audio.o snd-usbmidi-lib.o 18obj-$(CONFIG_SND_USB_AUDIO) += snd-usb-audio.o snd-usbmidi-lib.o
diff --git a/sound/usb/card.c b/sound/usb/card.c
new file mode 100644
index 000000000000..426aabc729d9
--- /dev/null
+++ b/sound/usb/card.c
@@ -0,0 +1,648 @@
1/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
5 *
6 * Many codes borrowed from audio.c by
7 * Alan Cox (alan@lxorguk.ukuu.org.uk)
8 * Thomas Sailer (sailer@ife.ee.ethz.ch)
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 *
26 * NOTES:
27 *
28 * - async unlink should be used for avoiding the sleep inside lock.
29 * 2.4.22 usb-uhci seems buggy for async unlinking and results in
30 * oops. in such a cse, pass async_unlink=0 option.
31 * - the linked URBs would be preferred but not used so far because of
32 * the instability of unlinking.
33 * - type II is not supported properly. there is no device which supports
34 * this type *correctly*. SB extigy looks as if it supports, but it's
35 * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
36 */
37
38
39#include <linux/bitops.h>
40#include <linux/init.h>
41#include <linux/list.h>
42#include <linux/slab.h>
43#include <linux/string.h>
44#include <linux/usb.h>
45#include <linux/moduleparam.h>
46#include <linux/mutex.h>
47#include <linux/usb/audio.h>
48
49#include <sound/core.h>
50#include <sound/info.h>
51#include <sound/pcm.h>
52#include <sound/pcm_params.h>
53#include <sound/initval.h>
54
55#include "usbaudio.h"
56#include "card.h"
57#include "midi.h"
58#include "usbmixer.h"
59#include "proc.h"
60#include "quirks.h"
61#include "endpoint.h"
62#include "helper.h"
63#include "debug.h"
64#include "pcm.h"
65#include "urb.h"
66#include "format.h"
67
68MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
69MODULE_DESCRIPTION("USB Audio");
70MODULE_LICENSE("GPL");
71MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
72
73
74static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
75static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
76static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
77/* Vendor/product IDs for this card */
78static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
79static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
80static int nrpacks = 8; /* max. number of packets per urb */
81static int async_unlink = 1;
82static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
83static int ignore_ctl_error;
84
85module_param_array(index, int, NULL, 0444);
86MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
87module_param_array(id, charp, NULL, 0444);
88MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
89module_param_array(enable, bool, NULL, 0444);
90MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
91module_param_array(vid, int, NULL, 0444);
92MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
93module_param_array(pid, int, NULL, 0444);
94MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
95module_param(nrpacks, int, 0644);
96MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
97module_param(async_unlink, bool, 0444);
98MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
99module_param_array(device_setup, int, NULL, 0444);
100MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
101module_param(ignore_ctl_error, bool, 0444);
102MODULE_PARM_DESC(ignore_ctl_error,
103 "Ignore errors from USB controller for mixer interfaces.");
104
105/*
106 * we keep the snd_usb_audio_t instances by ourselves for merging
107 * the all interfaces on the same card as one sound device.
108 */
109
110static DEFINE_MUTEX(register_mutex);
111static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
112static struct usb_driver usb_audio_driver;
113
114/*
115 * disconnect streams
116 * called from snd_usb_audio_disconnect()
117 */
118static void snd_usb_stream_disconnect(struct list_head *head)
119{
120 int idx;
121 struct snd_usb_stream *as;
122 struct snd_usb_substream *subs;
123
124 as = list_entry(head, struct snd_usb_stream, list);
125 for (idx = 0; idx < 2; idx++) {
126 subs = &as->substream[idx];
127 if (!subs->num_formats)
128 return;
129 snd_usb_release_substream_urbs(subs, 1);
130 subs->interface = -1;
131 }
132}
133
134static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
135{
136 struct usb_device *dev = chip->dev;
137 struct usb_host_interface *alts;
138 struct usb_interface_descriptor *altsd;
139 struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
140
141 if (!iface) {
142 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
143 dev->devnum, ctrlif, interface);
144 return -EINVAL;
145 }
146
147 if (usb_interface_claimed(iface)) {
148 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",
149 dev->devnum, ctrlif, interface);
150 return -EINVAL;
151 }
152
153 alts = &iface->altsetting[0];
154 altsd = get_iface_desc(alts);
155 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
156 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
157 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
158 int err = snd_usbmidi_create(chip->card, iface,
159 &chip->midi_list, NULL);
160 if (err < 0) {
161 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",
162 dev->devnum, ctrlif, interface);
163 return -EINVAL;
164 }
165 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
166
167 return 0;
168 }
169
170 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
171 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
172 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
173 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",
174 dev->devnum, ctrlif, interface, altsd->bInterfaceClass);
175 /* skip non-supported classes */
176 return -EINVAL;
177 }
178
179 if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
180 snd_printk(KERN_ERR "low speed audio streaming not supported\n");
181 return -EINVAL;
182 }
183
184 if (! snd_usb_parse_audio_endpoints(chip, interface)) {
185 usb_set_interface(dev, interface, 0); /* reset the current interface */
186 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
187 return -EINVAL;
188 }
189
190 return 0;
191}
192
193/*
194 * parse audio control descriptor and create pcm/midi streams
195 */
196static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
197{
198 struct usb_device *dev = chip->dev;
199 struct usb_host_interface *host_iface;
200 struct usb_interface_descriptor *altsd;
201 void *control_header;
202 int i, protocol;
203
204 /* find audiocontrol interface */
205 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
206 control_header = snd_usb_find_csint_desc(host_iface->extra,
207 host_iface->extralen,
208 NULL, UAC_HEADER);
209 altsd = get_iface_desc(host_iface);
210 protocol = altsd->bInterfaceProtocol;
211
212 if (!control_header) {
213 snd_printk(KERN_ERR "cannot find UAC_HEADER\n");
214 return -EINVAL;
215 }
216
217 switch (protocol) {
218 case UAC_VERSION_1: {
219 struct uac_ac_header_descriptor_v1 *h1 = control_header;
220
221 if (!h1->bInCollection) {
222 snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");
223 return -EINVAL;
224 }
225
226 if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
227 snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");
228 return -EINVAL;
229 }
230
231 for (i = 0; i < h1->bInCollection; i++)
232 snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
233
234 break;
235 }
236
237 case UAC_VERSION_2: {
238 struct uac_clock_source_descriptor *cs;
239 struct usb_interface_assoc_descriptor *assoc =
240 usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
241
242 if (!assoc) {
243 snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
244 return -EINVAL;
245 }
246
247 /* FIXME: for now, we expect there is at least one clock source
248 * descriptor and we always take the first one.
249 * We should properly support devices with multiple clock sources,
250 * clock selectors and sample rate conversion units. */
251
252 cs = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen,
253 NULL, UAC_CLOCK_SOURCE);
254
255 if (!cs) {
256 snd_printk(KERN_ERR "CLOCK_SOURCE descriptor not found\n");
257 return -EINVAL;
258 }
259
260 chip->clock_id = cs->bClockID;
261
262 for (i = 0; i < assoc->bInterfaceCount; i++) {
263 int intf = assoc->bFirstInterface + i;
264
265 if (intf != ctrlif)
266 snd_usb_create_stream(chip, ctrlif, intf);
267 }
268
269 break;
270 }
271
272 default:
273 snd_printk(KERN_ERR "unknown protocol version 0x%02x\n", protocol);
274 return -EINVAL;
275 }
276
277 return 0;
278}
279
280/*
281 * free the chip instance
282 *
283 * here we have to do not much, since pcm and controls are already freed
284 *
285 */
286
287static int snd_usb_audio_free(struct snd_usb_audio *chip)
288{
289 kfree(chip);
290 return 0;
291}
292
293static int snd_usb_audio_dev_free(struct snd_device *device)
294{
295 struct snd_usb_audio *chip = device->device_data;
296 return snd_usb_audio_free(chip);
297}
298
299
300/*
301 * create a chip instance and set its names.
302 */
303static int snd_usb_audio_create(struct usb_device *dev, int idx,
304 const struct snd_usb_audio_quirk *quirk,
305 struct snd_usb_audio **rchip)
306{
307 struct snd_card *card;
308 struct snd_usb_audio *chip;
309 int err, len;
310 char component[14];
311 static struct snd_device_ops ops = {
312 .dev_free = snd_usb_audio_dev_free,
313 };
314
315 *rchip = NULL;
316
317 if (snd_usb_get_speed(dev) != USB_SPEED_LOW &&
318 snd_usb_get_speed(dev) != USB_SPEED_FULL &&
319 snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
320 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
321 return -ENXIO;
322 }
323
324 err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
325 if (err < 0) {
326 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
327 return err;
328 }
329
330 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
331 if (! chip) {
332 snd_card_free(card);
333 return -ENOMEM;
334 }
335
336 chip->index = idx;
337 chip->dev = dev;
338 chip->card = card;
339 chip->setup = device_setup[idx];
340 chip->nrpacks = nrpacks;
341 chip->async_unlink = async_unlink;
342
343 chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
344 le16_to_cpu(dev->descriptor.idProduct));
345 INIT_LIST_HEAD(&chip->pcm_list);
346 INIT_LIST_HEAD(&chip->midi_list);
347 INIT_LIST_HEAD(&chip->mixer_list);
348
349 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
350 snd_usb_audio_free(chip);
351 snd_card_free(card);
352 return err;
353 }
354
355 strcpy(card->driver, "USB-Audio");
356 sprintf(component, "USB%04x:%04x",
357 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
358 snd_component_add(card, component);
359
360 /* retrieve the device string as shortname */
361 if (quirk && quirk->product_name) {
362 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
363 } else {
364 if (!dev->descriptor.iProduct ||
365 usb_string(dev, dev->descriptor.iProduct,
366 card->shortname, sizeof(card->shortname)) <= 0) {
367 /* no name available from anywhere, so use ID */
368 sprintf(card->shortname, "USB Device %#04x:%#04x",
369 USB_ID_VENDOR(chip->usb_id),
370 USB_ID_PRODUCT(chip->usb_id));
371 }
372 }
373
374 /* retrieve the vendor and device strings as longname */
375 if (quirk && quirk->vendor_name) {
376 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
377 } else {
378 if (dev->descriptor.iManufacturer)
379 len = usb_string(dev, dev->descriptor.iManufacturer,
380 card->longname, sizeof(card->longname));
381 else
382 len = 0;
383 /* we don't really care if there isn't any vendor string */
384 }
385 if (len > 0)
386 strlcat(card->longname, " ", sizeof(card->longname));
387
388 strlcat(card->longname, card->shortname, sizeof(card->longname));
389
390 len = strlcat(card->longname, " at ", sizeof(card->longname));
391
392 if (len < sizeof(card->longname))
393 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
394
395 strlcat(card->longname,
396 snd_usb_get_speed(dev) == USB_SPEED_LOW ? ", low speed" :
397 snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" :
398 ", high speed",
399 sizeof(card->longname));
400
401 snd_usb_audio_create_proc(chip);
402
403 *rchip = chip;
404 return 0;
405}
406
407/*
408 * probe the active usb device
409 *
410 * note that this can be called multiple times per a device, when it
411 * includes multiple audio control interfaces.
412 *
413 * thus we check the usb device pointer and creates the card instance
414 * only at the first time. the successive calls of this function will
415 * append the pcm interface to the corresponding card.
416 */
417static void *snd_usb_audio_probe(struct usb_device *dev,
418 struct usb_interface *intf,
419 const struct usb_device_id *usb_id)
420{
421 const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
422 int i, err;
423 struct snd_usb_audio *chip;
424 struct usb_host_interface *alts;
425 int ifnum;
426 u32 id;
427
428 alts = &intf->altsetting[0];
429 ifnum = get_iface_desc(alts)->bInterfaceNumber;
430 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
431 le16_to_cpu(dev->descriptor.idProduct));
432 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
433 goto __err_val;
434
435 if (snd_usb_apply_boot_quirk(dev, intf, quirk) < 0)
436 goto __err_val;
437
438 /*
439 * found a config. now register to ALSA
440 */
441
442 /* check whether it's already registered */
443 chip = NULL;
444 mutex_lock(&register_mutex);
445 for (i = 0; i < SNDRV_CARDS; i++) {
446 if (usb_chip[i] && usb_chip[i]->dev == dev) {
447 if (usb_chip[i]->shutdown) {
448 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
449 goto __error;
450 }
451 chip = usb_chip[i];
452 break;
453 }
454 }
455 if (! chip) {
456 /* it's a fresh one.
457 * now look for an empty slot and create a new card instance
458 */
459 for (i = 0; i < SNDRV_CARDS; i++)
460 if (enable[i] && ! usb_chip[i] &&
461 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
462 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
463 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
464 goto __error;
465 }
466 snd_card_set_dev(chip->card, &intf->dev);
467 break;
468 }
469 if (!chip) {
470 printk(KERN_ERR "no available usb audio device\n");
471 goto __error;
472 }
473 }
474
475 chip->txfr_quirk = 0;
476 err = 1; /* continue */
477 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
478 /* need some special handlings */
479 if ((err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk)) < 0)
480 goto __error;
481 }
482
483 if (err > 0) {
484 /* create normal USB audio interfaces */
485 if (snd_usb_create_streams(chip, ifnum) < 0 ||
486 snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
487 goto __error;
488 }
489 }
490
491 /* we are allowed to call snd_card_register() many times */
492 if (snd_card_register(chip->card) < 0) {
493 goto __error;
494 }
495
496 usb_chip[chip->index] = chip;
497 chip->num_interfaces++;
498 mutex_unlock(&register_mutex);
499 return chip;
500
501 __error:
502 if (chip && !chip->num_interfaces)
503 snd_card_free(chip->card);
504 mutex_unlock(&register_mutex);
505 __err_val:
506 return NULL;
507}
508
509/*
510 * we need to take care of counter, since disconnection can be called also
511 * many times as well as usb_audio_probe().
512 */
513static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
514{
515 struct snd_usb_audio *chip;
516 struct snd_card *card;
517 struct list_head *p;
518
519 if (ptr == (void *)-1L)
520 return;
521
522 chip = ptr;
523 card = chip->card;
524 mutex_lock(&register_mutex);
525 chip->shutdown = 1;
526 chip->num_interfaces--;
527 if (chip->num_interfaces <= 0) {
528 snd_card_disconnect(card);
529 /* release the pcm resources */
530 list_for_each(p, &chip->pcm_list) {
531 snd_usb_stream_disconnect(p);
532 }
533 /* release the midi resources */
534 list_for_each(p, &chip->midi_list) {
535 snd_usbmidi_disconnect(p);
536 }
537 /* release mixer resources */
538 list_for_each(p, &chip->mixer_list) {
539 snd_usb_mixer_disconnect(p);
540 }
541 usb_chip[chip->index] = NULL;
542 mutex_unlock(&register_mutex);
543 snd_card_free_when_closed(card);
544 } else {
545 mutex_unlock(&register_mutex);
546 }
547}
548
549/*
550 * new 2.5 USB kernel API
551 */
552static int usb_audio_probe(struct usb_interface *intf,
553 const struct usb_device_id *id)
554{
555 void *chip;
556 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
557 if (chip) {
558 usb_set_intfdata(intf, chip);
559 return 0;
560 } else
561 return -EIO;
562}
563
564static void usb_audio_disconnect(struct usb_interface *intf)
565{
566 snd_usb_audio_disconnect(interface_to_usbdev(intf),
567 usb_get_intfdata(intf));
568}
569
570#ifdef CONFIG_PM
571static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
572{
573 struct snd_usb_audio *chip = usb_get_intfdata(intf);
574 struct list_head *p;
575 struct snd_usb_stream *as;
576
577 if (chip == (void *)-1L)
578 return 0;
579
580 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
581 if (!chip->num_suspended_intf++) {
582 list_for_each(p, &chip->pcm_list) {
583 as = list_entry(p, struct snd_usb_stream, list);
584 snd_pcm_suspend_all(as->pcm);
585 }
586 }
587
588 return 0;
589}
590
591static int usb_audio_resume(struct usb_interface *intf)
592{
593 struct snd_usb_audio *chip = usb_get_intfdata(intf);
594
595 if (chip == (void *)-1L)
596 return 0;
597 if (--chip->num_suspended_intf)
598 return 0;
599 /*
600 * ALSA leaves material resumption to user space
601 * we just notify
602 */
603
604 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
605
606 return 0;
607}
608#endif /* CONFIG_PM */
609
610static struct usb_device_id usb_audio_ids [] = {
611#include "quirks-table.h"
612 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
613 .bInterfaceClass = USB_CLASS_AUDIO,
614 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
615 { } /* Terminating entry */
616};
617
618MODULE_DEVICE_TABLE (usb, usb_audio_ids);
619
620/*
621 * entry point for linux usb interface
622 */
623
624static struct usb_driver usb_audio_driver = {
625 .name = "snd-usb-audio",
626 .probe = usb_audio_probe,
627 .disconnect = usb_audio_disconnect,
628 .suspend = usb_audio_suspend,
629 .resume = usb_audio_resume,
630 .id_table = usb_audio_ids,
631};
632
633static int __init snd_usb_audio_init(void)
634{
635 if (nrpacks < 1 || nrpacks > MAX_PACKS) {
636 printk(KERN_WARNING "invalid nrpacks value.\n");
637 return -EINVAL;
638 }
639 return usb_register(&usb_audio_driver);
640}
641
642static void __exit snd_usb_audio_cleanup(void)
643{
644 usb_deregister(&usb_audio_driver);
645}
646
647module_init(snd_usb_audio_init);
648module_exit(snd_usb_audio_cleanup);
diff --git a/sound/usb/card.h b/sound/usb/card.h
new file mode 100644
index 000000000000..71f03c151030
--- /dev/null
+++ b/sound/usb/card.h
@@ -0,0 +1,105 @@
1#ifndef __USBAUDIO_CARD_H
2#define __USBAUDIO_CARD_H
3
4#define MAX_PACKS 20
5#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
6#define MAX_URBS 8
7#define SYNC_URBS 4 /* always four urbs for sync */
8#define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */
9
10struct audioformat {
11 struct list_head list;
12 snd_pcm_format_t format; /* format type */
13 unsigned int channels; /* # channels */
14 unsigned int fmt_type; /* USB audio format type (1-3) */
15 unsigned int frame_size; /* samples per frame for non-audio */
16 int iface; /* interface number */
17 unsigned char altsetting; /* corresponding alternate setting */
18 unsigned char altset_idx; /* array index of altenate setting */
19 unsigned char attributes; /* corresponding attributes of cs endpoint */
20 unsigned char endpoint; /* endpoint */
21 unsigned char ep_attr; /* endpoint attributes */
22 unsigned char datainterval; /* log_2 of data packet interval */
23 unsigned int maxpacksize; /* max. packet size */
24 unsigned int rates; /* rate bitmasks */
25 unsigned int rate_min, rate_max; /* min/max rates */
26 unsigned int nr_rates; /* number of rate table entries */
27 unsigned int *rate_table; /* rate table */
28};
29
30struct snd_usb_substream;
31
32struct snd_urb_ctx {
33 struct urb *urb;
34 unsigned int buffer_size; /* size of data buffer, if data URB */
35 struct snd_usb_substream *subs;
36 int index; /* index for urb array */
37 int packets; /* number of packets per urb */
38};
39
40struct snd_urb_ops {
41 int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
42 int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
43 int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
44 int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
45};
46
47struct snd_usb_substream {
48 struct snd_usb_stream *stream;
49 struct usb_device *dev;
50 struct snd_pcm_substream *pcm_substream;
51 int direction; /* playback or capture */
52 int interface; /* current interface */
53 int endpoint; /* assigned endpoint */
54 struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
55 unsigned int cur_rate; /* current rate (for hw_params callback) */
56 unsigned int period_bytes; /* current period bytes (for hw_params callback) */
57 unsigned int format; /* USB data format */
58 unsigned int datapipe; /* the data i/o pipe */
59 unsigned int syncpipe; /* 1 - async out or adaptive in */
60 unsigned int datainterval; /* log_2 of data packet interval */
61 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
62 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
63 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
64 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
65 unsigned int phase; /* phase accumulator */
66 unsigned int maxpacksize; /* max packet size in bytes */
67 unsigned int maxframesize; /* max packet size in frames */
68 unsigned int curpacksize; /* current packet size in bytes (for capture) */
69 unsigned int curframesize; /* current packet size in frames (for capture) */
70 unsigned int fill_max: 1; /* fill max packet size always */
71 unsigned int txfr_quirk:1; /* allow sub-frame alignment */
72 unsigned int fmt_type; /* USB audio format type (1-3) */
73
74 unsigned int running: 1; /* running status */
75
76 unsigned int hwptr_done; /* processed byte position in the buffer */
77 unsigned int transfer_done; /* processed frames since last period update */
78 unsigned long active_mask; /* bitmask of active urbs */
79 unsigned long unlink_mask; /* bitmask of unlinked urbs */
80
81 unsigned int nurbs; /* # urbs */
82 struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */
83 struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */
84 char *syncbuf; /* sync buffer for all sync URBs */
85 dma_addr_t sync_dma; /* DMA address of syncbuf */
86
87 u64 formats; /* format bitmasks (all or'ed) */
88 unsigned int num_formats; /* number of supported audio formats (list) */
89 struct list_head fmt_list; /* format list */
90 struct snd_pcm_hw_constraint_list rate_list; /* limited rates */
91 spinlock_t lock;
92
93 struct snd_urb_ops ops; /* callbacks (must be filled at init) */
94};
95
96struct snd_usb_stream {
97 struct snd_usb_audio *chip;
98 struct snd_pcm *pcm;
99 int pcm_index;
100 unsigned int fmt_type; /* USB audio format type (1-3) */
101 struct snd_usb_substream substream[2];
102 struct list_head list;
103};
104
105#endif /* __USBAUDIO_CARD_H */
diff --git a/sound/usb/debug.h b/sound/usb/debug.h
new file mode 100644
index 000000000000..343ec2d9ee66
--- /dev/null
+++ b/sound/usb/debug.h
@@ -0,0 +1,15 @@
1#ifndef __USBAUDIO_DEBUG_H
2#define __USBAUDIO_DEBUG_H
3
4/*
5 * h/w constraints
6 */
7
8#ifdef HW_CONST_DEBUG
9#define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
10#else
11#define hwc_debug(fmt, args...) /**/
12#endif
13
14#endif /* __USBAUDIO_DEBUG_H */
15
diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c
new file mode 100644
index 000000000000..3f53dee1270f
--- /dev/null
+++ b/sound/usb/endpoint.c
@@ -0,0 +1,358 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 */
17
18#include <linux/init.h>
19#include <linux/usb.h>
20#include <linux/usb/audio.h>
21
22#include <sound/core.h>
23#include <sound/pcm.h>
24
25#include "usbaudio.h"
26#include "card.h"
27#include "proc.h"
28#include "quirks.h"
29#include "endpoint.h"
30#include "urb.h"
31#include "pcm.h"
32#include "helper.h"
33#include "format.h"
34
35/*
36 * free a substream
37 */
38static void free_substream(struct snd_usb_substream *subs)
39{
40 struct list_head *p, *n;
41
42 if (!subs->num_formats)
43 return; /* not initialized */
44 list_for_each_safe(p, n, &subs->fmt_list) {
45 struct audioformat *fp = list_entry(p, struct audioformat, list);
46 kfree(fp->rate_table);
47 kfree(fp);
48 }
49 kfree(subs->rate_list.list);
50}
51
52
53/*
54 * free a usb stream instance
55 */
56static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
57{
58 free_substream(&stream->substream[0]);
59 free_substream(&stream->substream[1]);
60 list_del(&stream->list);
61 kfree(stream);
62}
63
64static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
65{
66 struct snd_usb_stream *stream = pcm->private_data;
67 if (stream) {
68 stream->pcm = NULL;
69 snd_usb_audio_stream_free(stream);
70 }
71}
72
73
74/*
75 * add this endpoint to the chip instance.
76 * if a stream with the same endpoint already exists, append to it.
77 * if not, create a new pcm stream.
78 */
79int snd_usb_add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)
80{
81 struct list_head *p;
82 struct snd_usb_stream *as;
83 struct snd_usb_substream *subs;
84 struct snd_pcm *pcm;
85 int err;
86
87 list_for_each(p, &chip->pcm_list) {
88 as = list_entry(p, struct snd_usb_stream, list);
89 if (as->fmt_type != fp->fmt_type)
90 continue;
91 subs = &as->substream[stream];
92 if (!subs->endpoint)
93 continue;
94 if (subs->endpoint == fp->endpoint) {
95 list_add_tail(&fp->list, &subs->fmt_list);
96 subs->num_formats++;
97 subs->formats |= 1ULL << fp->format;
98 return 0;
99 }
100 }
101 /* look for an empty stream */
102 list_for_each(p, &chip->pcm_list) {
103 as = list_entry(p, struct snd_usb_stream, list);
104 if (as->fmt_type != fp->fmt_type)
105 continue;
106 subs = &as->substream[stream];
107 if (subs->endpoint)
108 continue;
109 err = snd_pcm_new_stream(as->pcm, stream, 1);
110 if (err < 0)
111 return err;
112 snd_usb_init_substream(as, stream, fp);
113 return 0;
114 }
115
116 /* create a new pcm */
117 as = kzalloc(sizeof(*as), GFP_KERNEL);
118 if (!as)
119 return -ENOMEM;
120 as->pcm_index = chip->pcm_devs;
121 as->chip = chip;
122 as->fmt_type = fp->fmt_type;
123 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
124 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
125 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
126 &pcm);
127 if (err < 0) {
128 kfree(as);
129 return err;
130 }
131 as->pcm = pcm;
132 pcm->private_data = as;
133 pcm->private_free = snd_usb_audio_pcm_free;
134 pcm->info_flags = 0;
135 if (chip->pcm_devs > 0)
136 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
137 else
138 strcpy(pcm->name, "USB Audio");
139
140 snd_usb_init_substream(as, stream, fp);
141
142 list_add(&as->list, &chip->pcm_list);
143 chip->pcm_devs++;
144
145 snd_usb_proc_pcm_format_add(as);
146
147 return 0;
148}
149
150int snd_usb_parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
151{
152 struct usb_device *dev;
153 struct usb_interface *iface;
154 struct usb_host_interface *alts;
155 struct usb_interface_descriptor *altsd;
156 int i, altno, err, stream;
157 int format = 0, num_channels = 0;
158 struct audioformat *fp = NULL;
159 unsigned char *fmt, *csep;
160 int num, protocol;
161
162 dev = chip->dev;
163
164 /* parse the interface's altsettings */
165 iface = usb_ifnum_to_if(dev, iface_no);
166
167 num = iface->num_altsetting;
168
169 /*
170 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
171 * one misses syncpipe, and does not produce any sound.
172 */
173 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
174 num = 4;
175
176 for (i = 0; i < num; i++) {
177 alts = &iface->altsetting[i];
178 altsd = get_iface_desc(alts);
179 protocol = altsd->bInterfaceProtocol;
180 /* skip invalid one */
181 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
182 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
183 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
184 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
185 altsd->bNumEndpoints < 1 ||
186 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
187 continue;
188 /* must be isochronous */
189 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
190 USB_ENDPOINT_XFER_ISOC)
191 continue;
192 /* check direction */
193 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
194 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
195 altno = altsd->bAlternateSetting;
196
197 if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
198 continue;
199
200 /* get audio formats */
201 switch (protocol) {
202 case UAC_VERSION_1: {
203 struct uac_as_header_descriptor_v1 *as =
204 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
205
206 if (!as) {
207 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
208 dev->devnum, iface_no, altno);
209 continue;
210 }
211
212 if (as->bLength < sizeof(*as)) {
213 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
214 dev->devnum, iface_no, altno);
215 continue;
216 }
217
218 format = le16_to_cpu(as->wFormatTag); /* remember the format value */
219 break;
220 }
221
222 case UAC_VERSION_2: {
223 struct uac_as_header_descriptor_v2 *as =
224 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
225
226 if (!as) {
227 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
228 dev->devnum, iface_no, altno);
229 continue;
230 }
231
232 if (as->bLength < sizeof(*as)) {
233 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
234 dev->devnum, iface_no, altno);
235 continue;
236 }
237
238 num_channels = as->bNrChannels;
239 format = le32_to_cpu(as->bmFormats);
240
241 break;
242 }
243
244 default:
245 snd_printk(KERN_ERR "%d:%u:%d : unknown interface protocol %04x\n",
246 dev->devnum, iface_no, altno, protocol);
247 continue;
248 }
249
250 /* get format type */
251 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
252 if (!fmt) {
253 snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
254 dev->devnum, iface_no, altno);
255 continue;
256 }
257 if (((protocol == UAC_VERSION_1) && (fmt[0] < 8)) ||
258 ((protocol == UAC_VERSION_2) && (fmt[0] != 6))) {
259 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
260 dev->devnum, iface_no, altno);
261 continue;
262 }
263
264 /*
265 * Blue Microphones workaround: The last altsetting is identical
266 * with the previous one, except for a larger packet size, but
267 * is actually a mislabeled two-channel setting; ignore it.
268 */
269 if (fmt[4] == 1 && fmt[5] == 2 && altno == 2 && num == 3 &&
270 fp && fp->altsetting == 1 && fp->channels == 1 &&
271 fp->format == SNDRV_PCM_FORMAT_S16_LE &&
272 protocol == UAC_VERSION_1 &&
273 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
274 fp->maxpacksize * 2)
275 continue;
276
277 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
278 /* Creamware Noah has this descriptor after the 2nd endpoint */
279 if (!csep && altsd->bNumEndpoints >= 2)
280 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
281 if (!csep || csep[0] < 7 || csep[2] != UAC_EP_GENERAL) {
282 snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
283 " class specific endpoint descriptor\n",
284 dev->devnum, iface_no, altno);
285 csep = NULL;
286 }
287
288 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
289 if (! fp) {
290 snd_printk(KERN_ERR "cannot malloc\n");
291 return -ENOMEM;
292 }
293
294 fp->iface = iface_no;
295 fp->altsetting = altno;
296 fp->altset_idx = i;
297 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
298 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
299 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
300 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
301 /* num_channels is only set for v2 interfaces */
302 fp->channels = num_channels;
303 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
304 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
305 * (fp->maxpacksize & 0x7ff);
306 fp->attributes = csep ? csep[3] : 0;
307
308 /* some quirks for attributes here */
309
310 switch (chip->usb_id) {
311 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
312 /* Optoplay sets the sample rate attribute although
313 * it seems not supporting it in fact.
314 */
315 fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
316 break;
317 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
318 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
319 /* doesn't set the sample rate attribute, but supports it */
320 fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
321 break;
322 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
323 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
324 an older model 77d:223) */
325 /*
326 * plantronics headset and Griffin iMic have set adaptive-in
327 * although it's really not...
328 */
329 fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
330 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
331 fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
332 else
333 fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
334 break;
335 }
336
337 /* ok, let's parse further... */
338 if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {
339 kfree(fp->rate_table);
340 kfree(fp);
341 continue;
342 }
343
344 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
345 err = snd_usb_add_audio_endpoint(chip, stream, fp);
346 if (err < 0) {
347 kfree(fp->rate_table);
348 kfree(fp);
349 return err;
350 }
351 /* try to set the interface... */
352 usb_set_interface(chip->dev, iface_no, altno);
353 snd_usb_init_pitch(chip->dev, iface_no, alts, fp);
354 snd_usb_init_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
355 }
356 return 0;
357}
358
diff --git a/sound/usb/endpoint.h b/sound/usb/endpoint.h
new file mode 100644
index 000000000000..64dd0db023b2
--- /dev/null
+++ b/sound/usb/endpoint.h
@@ -0,0 +1,11 @@
1#ifndef __USBAUDIO_ENDPOINT_H
2#define __USBAUDIO_ENDPOINT_H
3
4int snd_usb_parse_audio_endpoints(struct snd_usb_audio *chip,
5 int iface_no);
6
7int snd_usb_add_audio_endpoint(struct snd_usb_audio *chip,
8 int stream,
9 struct audioformat *fp);
10
11#endif /* __USBAUDIO_ENDPOINT_H */
diff --git a/sound/usb/format.c b/sound/usb/format.c
new file mode 100644
index 000000000000..cbfe0c23dbd6
--- /dev/null
+++ b/sound/usb/format.c
@@ -0,0 +1,445 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 */
17
18#include <linux/init.h>
19#include <linux/usb.h>
20#include <linux/usb/audio.h>
21
22#include <sound/core.h>
23#include <sound/pcm.h>
24
25#include "usbaudio.h"
26#include "card.h"
27#include "quirks.h"
28#include "helper.h"
29#include "debug.h"
30
31/*
32 * parse the audio format type I descriptor
33 * and returns the corresponding pcm format
34 *
35 * @dev: usb device
36 * @fp: audioformat record
37 * @format: the format tag (wFormatTag)
38 * @fmt: the format type descriptor
39 */
40static int parse_audio_format_i_type(struct snd_usb_audio *chip,
41 struct audioformat *fp,
42 int format, void *_fmt,
43 int protocol)
44{
45 int pcm_format, i;
46 int sample_width, sample_bytes;
47
48 switch (protocol) {
49 case UAC_VERSION_1: {
50 struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
51 sample_width = fmt->bBitResolution;
52 sample_bytes = fmt->bSubframeSize;
53 break;
54 }
55
56 case UAC_VERSION_2: {
57 struct uac_format_type_i_ext_descriptor *fmt = _fmt;
58 sample_width = fmt->bBitResolution;
59 sample_bytes = fmt->bSubslotSize;
60
61 /*
62 * FIXME
63 * USB audio class v2 devices specify a bitmap of possible
64 * audio formats rather than one fix value. For now, we just
65 * pick one of them and report that as the only possible
66 * value for this setting.
67 * The bit allocation map is in fact compatible to the
68 * wFormatTag of the v1 AS streaming descriptors, which is why
69 * we can simply map the matrix.
70 */
71
72 for (i = 0; i < 5; i++)
73 if (format & (1UL << i)) {
74 format = i + 1;
75 break;
76 }
77
78 break;
79 }
80
81 default:
82 return -EINVAL;
83 }
84
85 /* FIXME: correct endianess and sign? */
86 pcm_format = -1;
87
88 switch (format) {
89 case UAC_FORMAT_TYPE_I_UNDEFINED: /* some devices don't define this correctly... */
90 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
91 chip->dev->devnum, fp->iface, fp->altsetting);
92 /* fall-through */
93 case UAC_FORMAT_TYPE_I_PCM:
94 if (sample_width > sample_bytes * 8) {
95 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
96 chip->dev->devnum, fp->iface, fp->altsetting,
97 sample_width, sample_bytes);
98 }
99 /* check the format byte size */
100 switch (sample_bytes) {
101 case 1:
102 pcm_format = SNDRV_PCM_FORMAT_S8;
103 break;
104 case 2:
105 if (snd_usb_is_big_endian_format(chip, fp))
106 pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
107 else
108 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
109 break;
110 case 3:
111 if (snd_usb_is_big_endian_format(chip, fp))
112 pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
113 else
114 pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
115 break;
116 case 4:
117 pcm_format = SNDRV_PCM_FORMAT_S32_LE;
118 break;
119 default:
120 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
121 chip->dev->devnum, fp->iface, fp->altsetting,
122 sample_width, sample_bytes);
123 break;
124 }
125 break;
126 case UAC_FORMAT_TYPE_I_PCM8:
127 pcm_format = SNDRV_PCM_FORMAT_U8;
128
129 /* Dallas DS4201 workaround: it advertises U8 format, but really
130 supports S8. */
131 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
132 pcm_format = SNDRV_PCM_FORMAT_S8;
133 break;
134 case UAC_FORMAT_TYPE_I_IEEE_FLOAT:
135 pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
136 break;
137 case UAC_FORMAT_TYPE_I_ALAW:
138 pcm_format = SNDRV_PCM_FORMAT_A_LAW;
139 break;
140 case UAC_FORMAT_TYPE_I_MULAW:
141 pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
142 break;
143 default:
144 snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
145 chip->dev->devnum, fp->iface, fp->altsetting, format);
146 break;
147 }
148 return pcm_format;
149}
150
151
152/*
153 * parse the format descriptor and stores the possible sample rates
154 * on the audioformat table (audio class v1).
155 *
156 * @dev: usb device
157 * @fp: audioformat record
158 * @fmt: the format descriptor
159 * @offset: the start offset of descriptor pointing the rate type
160 * (7 for type I and II, 8 for type II)
161 */
162static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
163 unsigned char *fmt, int offset)
164{
165 int nr_rates = fmt[offset];
166
167 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
168 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
169 chip->dev->devnum, fp->iface, fp->altsetting);
170 return -1;
171 }
172
173 if (nr_rates) {
174 /*
175 * build the rate table and bitmap flags
176 */
177 int r, idx;
178
179 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
180 if (fp->rate_table == NULL) {
181 snd_printk(KERN_ERR "cannot malloc\n");
182 return -1;
183 }
184
185 fp->nr_rates = 0;
186 fp->rate_min = fp->rate_max = 0;
187 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
188 unsigned int rate = combine_triple(&fmt[idx]);
189 if (!rate)
190 continue;
191 /* C-Media CM6501 mislabels its 96 kHz altsetting */
192 if (rate == 48000 && nr_rates == 1 &&
193 (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
194 chip->usb_id == USB_ID(0x0d8c, 0x0102)) &&
195 fp->altsetting == 5 && fp->maxpacksize == 392)
196 rate = 96000;
197 /* Creative VF0470 Live Cam reports 16 kHz instead of 8kHz */
198 if (rate == 16000 && chip->usb_id == USB_ID(0x041e, 0x4068))
199 rate = 8000;
200
201 fp->rate_table[fp->nr_rates] = rate;
202 if (!fp->rate_min || rate < fp->rate_min)
203 fp->rate_min = rate;
204 if (!fp->rate_max || rate > fp->rate_max)
205 fp->rate_max = rate;
206 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
207 fp->nr_rates++;
208 }
209 if (!fp->nr_rates) {
210 hwc_debug("All rates were zero. Skipping format!\n");
211 return -1;
212 }
213 } else {
214 /* continuous rates */
215 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
216 fp->rate_min = combine_triple(&fmt[offset + 1]);
217 fp->rate_max = combine_triple(&fmt[offset + 4]);
218 }
219 return 0;
220}
221
222/*
223 * parse the format descriptor and stores the possible sample rates
224 * on the audioformat table (audio class v2).
225 */
226static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,
227 struct audioformat *fp,
228 struct usb_host_interface *iface)
229{
230 struct usb_device *dev = chip->dev;
231 unsigned char tmp[2], *data;
232 int i, nr_rates, data_size, ret = 0;
233
234 /* get the number of sample rates first by only fetching 2 bytes */
235 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
236 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
237 0x0100, chip->clock_id << 8, tmp, sizeof(tmp), 1000);
238
239 if (ret < 0) {
240 snd_printk(KERN_ERR "unable to retrieve number of sample rates\n");
241 goto err;
242 }
243
244 nr_rates = (tmp[1] << 8) | tmp[0];
245 data_size = 2 + 12 * nr_rates;
246 data = kzalloc(data_size, GFP_KERNEL);
247 if (!data) {
248 ret = -ENOMEM;
249 goto err;
250 }
251
252 /* now get the full information */
253 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
254 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
255 0x0100, chip->clock_id << 8, data, data_size, 1000);
256
257 if (ret < 0) {
258 snd_printk(KERN_ERR "unable to retrieve sample rate range\n");
259 ret = -EINVAL;
260 goto err_free;
261 }
262
263 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
264 if (!fp->rate_table) {
265 ret = -ENOMEM;
266 goto err_free;
267 }
268
269 fp->nr_rates = 0;
270 fp->rate_min = fp->rate_max = 0;
271
272 for (i = 0; i < nr_rates; i++) {
273 int rate = combine_quad(&data[2 + 12 * i]);
274
275 fp->rate_table[fp->nr_rates] = rate;
276 if (!fp->rate_min || rate < fp->rate_min)
277 fp->rate_min = rate;
278 if (!fp->rate_max || rate > fp->rate_max)
279 fp->rate_max = rate;
280 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
281 fp->nr_rates++;
282 }
283
284err_free:
285 kfree(data);
286err:
287 return ret;
288}
289
290/*
291 * parse the format type I and III descriptors
292 */
293static int parse_audio_format_i(struct snd_usb_audio *chip,
294 struct audioformat *fp,
295 int format, void *_fmt,
296 struct usb_host_interface *iface)
297{
298 struct usb_interface_descriptor *altsd = get_iface_desc(iface);
299 struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
300 int protocol = altsd->bInterfaceProtocol;
301 int pcm_format, ret;
302
303 if (fmt->bFormatType == UAC_FORMAT_TYPE_III) {
304 /* FIXME: the format type is really IECxxx
305 * but we give normal PCM format to get the existing
306 * apps working...
307 */
308 switch (chip->usb_id) {
309
310 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
311 if (chip->setup == 0x00 &&
312 fp->altsetting == 6)
313 pcm_format = SNDRV_PCM_FORMAT_S16_BE;
314 else
315 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
316 break;
317 default:
318 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
319 }
320 } else {
321 pcm_format = parse_audio_format_i_type(chip, fp, format, fmt, protocol);
322 if (pcm_format < 0)
323 return -1;
324 }
325
326 fp->format = pcm_format;
327
328 /* gather possible sample rates */
329 /* audio class v1 reports possible sample rates as part of the
330 * proprietary class specific descriptor.
331 * audio class v2 uses class specific EP0 range requests for that.
332 */
333 switch (protocol) {
334 case UAC_VERSION_1:
335 fp->channels = fmt->bNrChannels;
336 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 7);
337 break;
338 case UAC_VERSION_2:
339 /* fp->channels is already set in this case */
340 ret = parse_audio_format_rates_v2(chip, fp, iface);
341 break;
342 }
343
344 if (fp->channels < 1) {
345 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
346 chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
347 return -1;
348 }
349
350 return ret;
351}
352
353/*
354 * parse the format type II descriptor
355 */
356static int parse_audio_format_ii(struct snd_usb_audio *chip,
357 struct audioformat *fp,
358 int format, void *_fmt,
359 struct usb_host_interface *iface)
360{
361 int brate, framesize, ret;
362 struct usb_interface_descriptor *altsd = get_iface_desc(iface);
363 int protocol = altsd->bInterfaceProtocol;
364
365 switch (format) {
366 case UAC_FORMAT_TYPE_II_AC3:
367 /* FIXME: there is no AC3 format defined yet */
368 // fp->format = SNDRV_PCM_FORMAT_AC3;
369 fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
370 break;
371 case UAC_FORMAT_TYPE_II_MPEG:
372 fp->format = SNDRV_PCM_FORMAT_MPEG;
373 break;
374 default:
375 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",
376 chip->dev->devnum, fp->iface, fp->altsetting, format);
377 fp->format = SNDRV_PCM_FORMAT_MPEG;
378 break;
379 }
380
381 fp->channels = 1;
382
383 switch (protocol) {
384 case UAC_VERSION_1: {
385 struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
386 brate = le16_to_cpu(fmt->wMaxBitRate);
387 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
388 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
389 fp->frame_size = framesize;
390 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
391 break;
392 }
393 case UAC_VERSION_2: {
394 struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
395 brate = le16_to_cpu(fmt->wMaxBitRate);
396 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
397 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
398 fp->frame_size = framesize;
399 ret = parse_audio_format_rates_v2(chip, fp, iface);
400 break;
401 }
402 }
403
404 return ret;
405}
406
407int snd_usb_parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,
408 int format, unsigned char *fmt, int stream,
409 struct usb_host_interface *iface)
410{
411 int err;
412
413 switch (fmt[3]) {
414 case UAC_FORMAT_TYPE_I:
415 case UAC_FORMAT_TYPE_III:
416 err = parse_audio_format_i(chip, fp, format, fmt, iface);
417 break;
418 case UAC_FORMAT_TYPE_II:
419 err = parse_audio_format_ii(chip, fp, format, fmt, iface);
420 break;
421 default:
422 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
423 chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);
424 return -1;
425 }
426 fp->fmt_type = fmt[3];
427 if (err < 0)
428 return err;
429#if 1
430 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
431 /* extigy apparently supports sample rates other than 48k
432 * but not in ordinary way. so we enable only 48k atm.
433 */
434 if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
435 chip->usb_id == USB_ID(0x041e, 0x3020) ||
436 chip->usb_id == USB_ID(0x041e, 0x3061)) {
437 if (fmt[3] == UAC_FORMAT_TYPE_I &&
438 fp->rates != SNDRV_PCM_RATE_48000 &&
439 fp->rates != SNDRV_PCM_RATE_96000)
440 return -1;
441 }
442#endif
443 return 0;
444}
445
diff --git a/sound/usb/format.h b/sound/usb/format.h
new file mode 100644
index 000000000000..8298c4e8ddfa
--- /dev/null
+++ b/sound/usb/format.h
@@ -0,0 +1,8 @@
1#ifndef __USBAUDIO_FORMAT_H
2#define __USBAUDIO_FORMAT_H
3
4int snd_usb_parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,
5 int format, unsigned char *fmt, int stream,
6 struct usb_host_interface *iface);
7
8#endif /* __USBAUDIO_FORMAT_H */
diff --git a/sound/usb/helper.c b/sound/usb/helper.c
new file mode 100644
index 000000000000..ba7dba4d5cf2
--- /dev/null
+++ b/sound/usb/helper.c
@@ -0,0 +1,112 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 */
17
18#include <linux/init.h>
19#include <linux/usb.h>
20
21#include "usbaudio.h"
22#include "helper.h"
23
24/*
25 * combine bytes and get an integer value
26 */
27unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
28{
29 switch (size) {
30 case 1: return *bytes;
31 case 2: return combine_word(bytes);
32 case 3: return combine_triple(bytes);
33 case 4: return combine_quad(bytes);
34 default: return 0;
35 }
36}
37
38/*
39 * parse descriptor buffer and return the pointer starting the given
40 * descriptor type.
41 */
42void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
43{
44 u8 *p, *end, *next;
45
46 p = descstart;
47 end = p + desclen;
48 for (; p < end;) {
49 if (p[0] < 2)
50 return NULL;
51 next = p + p[0];
52 if (next > end)
53 return NULL;
54 if (p[1] == dtype && (!after || (void *)p > after)) {
55 return p;
56 }
57 p = next;
58 }
59 return NULL;
60}
61
62/*
63 * find a class-specified interface descriptor with the given subtype.
64 */
65void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
66{
67 unsigned char *p = after;
68
69 while ((p = snd_usb_find_desc(buffer, buflen, p,
70 USB_DT_CS_INTERFACE)) != NULL) {
71 if (p[0] >= 3 && p[2] == dsubtype)
72 return p;
73 }
74 return NULL;
75}
76
77/*
78 * Wrapper for usb_control_msg().
79 * Allocates a temp buffer to prevent dmaing from/to the stack.
80 */
81int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
82 __u8 requesttype, __u16 value, __u16 index, void *data,
83 __u16 size, int timeout)
84{
85 int err;
86 void *buf = NULL;
87
88 if (size > 0) {
89 buf = kmemdup(data, size, GFP_KERNEL);
90 if (!buf)
91 return -ENOMEM;
92 }
93 err = usb_control_msg(dev, pipe, request, requesttype,
94 value, index, buf, size, timeout);
95 if (size > 0) {
96 memcpy(data, buf, size);
97 kfree(buf);
98 }
99 return err;
100}
101
102unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
103 struct usb_host_interface *alts)
104{
105 if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH &&
106 get_endpoint(alts, 0)->bInterval >= 1 &&
107 get_endpoint(alts, 0)->bInterval <= 4)
108 return get_endpoint(alts, 0)->bInterval - 1;
109 else
110 return 0;
111}
112
diff --git a/sound/usb/helper.h b/sound/usb/helper.h
new file mode 100644
index 000000000000..a6b0e51b3a9a
--- /dev/null
+++ b/sound/usb/helper.h
@@ -0,0 +1,32 @@
1#ifndef __USBAUDIO_HELPER_H
2#define __USBAUDIO_HELPER_H
3
4unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size);
5
6void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype);
7void *snd_usb_find_csint_desc(void *descstart, int desclen, void *after, u8 dsubtype);
8
9int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe,
10 __u8 request, __u8 requesttype, __u16 value, __u16 index,
11 void *data, __u16 size, int timeout);
12
13unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
14 struct usb_host_interface *alts);
15
16/*
17 * retrieve usb_interface descriptor from the host interface
18 * (conditional for compatibility with the older API)
19 */
20#ifndef get_iface_desc
21#define get_iface_desc(iface) (&(iface)->desc)
22#define get_endpoint(alt,ep) (&(alt)->endpoint[ep].desc)
23#define get_ep_desc(ep) (&(ep)->desc)
24#define get_cfg_desc(cfg) (&(cfg)->desc)
25#endif
26
27#ifndef snd_usb_get_speed
28#define snd_usb_get_speed(dev) ((dev)->speed)
29#endif
30
31
32#endif /* __USBAUDIO_HELPER_H */
diff --git a/sound/usb/usbmidi.c b/sound/usb/midi.c
index 5915a04cdb9b..c6ee4a18e513 100644
--- a/sound/usb/usbmidi.c
+++ b/sound/usb/midi.c
@@ -53,8 +53,8 @@
53#include <sound/rawmidi.h> 53#include <sound/rawmidi.h>
54#include <sound/asequencer.h> 54#include <sound/asequencer.h>
55#include "usbaudio.h" 55#include "usbaudio.h"
56#include "usbmidi.h" 56#include "midi.h"
57 57#include "helper.h"
58 58
59/* 59/*
60 * define this to log all USB packets 60 * define this to log all USB packets
diff --git a/sound/usb/usbmidi.h b/sound/usb/midi.h
index 2089ec987c66..2089ec987c66 100644
--- a/sound/usb/usbmidi.h
+++ b/sound/usb/midi.h
diff --git a/sound/usb/misc/ua101.c b/sound/usb/misc/ua101.c
index b4a4cb46a178..796d8b25ee89 100644
--- a/sound/usb/misc/ua101.c
+++ b/sound/usb/misc/ua101.c
@@ -24,7 +24,7 @@
24#include <sound/pcm.h> 24#include <sound/pcm.h>
25#include <sound/pcm_params.h> 25#include <sound/pcm_params.h>
26#include "../usbaudio.h" 26#include "../usbaudio.h"
27#include "../usbmidi.h" 27#include "../midi.h"
28 28
29MODULE_DESCRIPTION("Edirol UA-101/1000 driver"); 29MODULE_DESCRIPTION("Edirol UA-101/1000 driver");
30MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); 30MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
new file mode 100644
index 000000000000..87863ccf9068
--- /dev/null
+++ b/sound/usb/pcm.c
@@ -0,0 +1,845 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17#include <linux/init.h>
18#include <linux/usb.h>
19#include <linux/usb/audio.h>
20
21#include <sound/core.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24
25#include "usbaudio.h"
26#include "card.h"
27#include "quirks.h"
28#include "debug.h"
29#include "urb.h"
30#include "helper.h"
31#include "pcm.h"
32
33/*
34 * return the current pcm pointer. just based on the hwptr_done value.
35 */
36static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
37{
38 struct snd_usb_substream *subs;
39 unsigned int hwptr_done;
40
41 subs = (struct snd_usb_substream *)substream->runtime->private_data;
42 spin_lock(&subs->lock);
43 hwptr_done = subs->hwptr_done;
44 spin_unlock(&subs->lock);
45 return hwptr_done / (substream->runtime->frame_bits >> 3);
46}
47
48/*
49 * find a matching audio format
50 */
51static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
52 unsigned int rate, unsigned int channels)
53{
54 struct list_head *p;
55 struct audioformat *found = NULL;
56 int cur_attr = 0, attr;
57
58 list_for_each(p, &subs->fmt_list) {
59 struct audioformat *fp;
60 fp = list_entry(p, struct audioformat, list);
61 if (fp->format != format || fp->channels != channels)
62 continue;
63 if (rate < fp->rate_min || rate > fp->rate_max)
64 continue;
65 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
66 unsigned int i;
67 for (i = 0; i < fp->nr_rates; i++)
68 if (fp->rate_table[i] == rate)
69 break;
70 if (i >= fp->nr_rates)
71 continue;
72 }
73 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
74 if (! found) {
75 found = fp;
76 cur_attr = attr;
77 continue;
78 }
79 /* avoid async out and adaptive in if the other method
80 * supports the same format.
81 * this is a workaround for the case like
82 * M-audio audiophile USB.
83 */
84 if (attr != cur_attr) {
85 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
86 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
87 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
88 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
89 continue;
90 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
91 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
92 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
93 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
94 found = fp;
95 cur_attr = attr;
96 continue;
97 }
98 }
99 /* find the format with the largest max. packet size */
100 if (fp->maxpacksize > found->maxpacksize) {
101 found = fp;
102 cur_attr = attr;
103 }
104 }
105 return found;
106}
107
108
109/*
110 * initialize the picth control and sample rate
111 */
112int snd_usb_init_pitch(struct usb_device *dev, int iface,
113 struct usb_host_interface *alts,
114 struct audioformat *fmt)
115{
116 unsigned int ep;
117 unsigned char data[1];
118 int err;
119
120 ep = get_endpoint(alts, 0)->bEndpointAddress;
121 /* if endpoint has pitch control, enable it */
122 if (fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL) {
123 data[0] = 1;
124 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
125 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
126 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
127 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
128 dev->devnum, iface, ep);
129 return err;
130 }
131 }
132 return 0;
133}
134
135int snd_usb_init_sample_rate(struct usb_device *dev, int iface,
136 struct usb_host_interface *alts,
137 struct audioformat *fmt, int rate)
138{
139 unsigned int ep;
140 unsigned char data[3];
141 int err;
142
143 ep = get_endpoint(alts, 0)->bEndpointAddress;
144 /* if endpoint has sampling rate control, set it */
145 if (fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE) {
146 int crate;
147 data[0] = rate;
148 data[1] = rate >> 8;
149 data[2] = rate >> 16;
150 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
151 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
152 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) {
153 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
154 dev->devnum, iface, fmt->altsetting, rate, ep);
155 return err;
156 }
157 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
158 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
159 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) {
160 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
161 dev->devnum, iface, fmt->altsetting, ep);
162 return 0; /* some devices don't support reading */
163 }
164 crate = data[0] | (data[1] << 8) | (data[2] << 16);
165 if (crate != rate) {
166 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
167 // runtime->rate = crate;
168 }
169 }
170 return 0;
171}
172
173/*
174 * find a matching format and set up the interface
175 */
176static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
177{
178 struct usb_device *dev = subs->dev;
179 struct usb_host_interface *alts;
180 struct usb_interface_descriptor *altsd;
181 struct usb_interface *iface;
182 unsigned int ep, attr;
183 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
184 int err;
185
186 iface = usb_ifnum_to_if(dev, fmt->iface);
187 if (WARN_ON(!iface))
188 return -EINVAL;
189 alts = &iface->altsetting[fmt->altset_idx];
190 altsd = get_iface_desc(alts);
191 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
192 return -EINVAL;
193
194 if (fmt == subs->cur_audiofmt)
195 return 0;
196
197 /* close the old interface */
198 if (subs->interface >= 0 && subs->interface != fmt->iface) {
199 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
200 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
201 dev->devnum, fmt->iface, fmt->altsetting);
202 return -EIO;
203 }
204 subs->interface = -1;
205 subs->format = 0;
206 }
207
208 /* set interface */
209 if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
210 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
211 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
212 dev->devnum, fmt->iface, fmt->altsetting);
213 return -EIO;
214 }
215 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
216 subs->interface = fmt->iface;
217 subs->format = fmt->altset_idx;
218 }
219
220 /* create a data pipe */
221 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
222 if (is_playback)
223 subs->datapipe = usb_sndisocpipe(dev, ep);
224 else
225 subs->datapipe = usb_rcvisocpipe(dev, ep);
226 subs->datainterval = fmt->datainterval;
227 subs->syncpipe = subs->syncinterval = 0;
228 subs->maxpacksize = fmt->maxpacksize;
229 subs->fill_max = 0;
230
231 /* we need a sync pipe in async OUT or adaptive IN mode */
232 /* check the number of EP, since some devices have broken
233 * descriptors which fool us. if it has only one EP,
234 * assume it as adaptive-out or sync-in.
235 */
236 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
237 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
238 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
239 altsd->bNumEndpoints >= 2) {
240 /* check sync-pipe endpoint */
241 /* ... and check descriptor size before accessing bSynchAddress
242 because there is a version of the SB Audigy 2 NX firmware lacking
243 the audio fields in the endpoint descriptors */
244 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
245 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
246 get_endpoint(alts, 1)->bSynchAddress != 0)) {
247 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
248 dev->devnum, fmt->iface, fmt->altsetting);
249 return -EINVAL;
250 }
251 ep = get_endpoint(alts, 1)->bEndpointAddress;
252 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
253 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
254 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
255 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
256 dev->devnum, fmt->iface, fmt->altsetting);
257 return -EINVAL;
258 }
259 ep &= USB_ENDPOINT_NUMBER_MASK;
260 if (is_playback)
261 subs->syncpipe = usb_rcvisocpipe(dev, ep);
262 else
263 subs->syncpipe = usb_sndisocpipe(dev, ep);
264 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
265 get_endpoint(alts, 1)->bRefresh >= 1 &&
266 get_endpoint(alts, 1)->bRefresh <= 9)
267 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
268 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
269 subs->syncinterval = 1;
270 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
271 get_endpoint(alts, 1)->bInterval <= 16)
272 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
273 else
274 subs->syncinterval = 3;
275 }
276
277 /* always fill max packet size */
278 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
279 subs->fill_max = 1;
280
281 if ((err = snd_usb_init_pitch(dev, subs->interface, alts, fmt)) < 0)
282 return err;
283
284 subs->cur_audiofmt = fmt;
285
286 snd_usb_set_format_quirk(subs, fmt);
287
288#if 0
289 printk(KERN_DEBUG
290 "setting done: format = %d, rate = %d..%d, channels = %d\n",
291 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
292 printk(KERN_DEBUG
293 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
294 subs->datapipe, subs->syncpipe);
295#endif
296
297 return 0;
298}
299
300/*
301 * hw_params callback
302 *
303 * allocate a buffer and set the given audio format.
304 *
305 * so far we use a physically linear buffer although packetize transfer
306 * doesn't need a continuous area.
307 * if sg buffer is supported on the later version of alsa, we'll follow
308 * that.
309 */
310static int snd_usb_hw_params(struct snd_pcm_substream *substream,
311 struct snd_pcm_hw_params *hw_params)
312{
313 struct snd_usb_substream *subs = substream->runtime->private_data;
314 struct audioformat *fmt;
315 unsigned int channels, rate, format;
316 int ret, changed;
317
318 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
319 params_buffer_bytes(hw_params));
320 if (ret < 0)
321 return ret;
322
323 format = params_format(hw_params);
324 rate = params_rate(hw_params);
325 channels = params_channels(hw_params);
326 fmt = find_format(subs, format, rate, channels);
327 if (!fmt) {
328 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
329 format, rate, channels);
330 return -EINVAL;
331 }
332
333 changed = subs->cur_audiofmt != fmt ||
334 subs->period_bytes != params_period_bytes(hw_params) ||
335 subs->cur_rate != rate;
336 if ((ret = set_format(subs, fmt)) < 0)
337 return ret;
338
339 if (subs->cur_rate != rate) {
340 struct usb_host_interface *alts;
341 struct usb_interface *iface;
342 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
343 alts = &iface->altsetting[fmt->altset_idx];
344 ret = snd_usb_init_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
345 if (ret < 0)
346 return ret;
347 subs->cur_rate = rate;
348 }
349
350 if (changed) {
351 /* format changed */
352 snd_usb_release_substream_urbs(subs, 0);
353 /* influenced: period_bytes, channels, rate, format, */
354 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
355 params_rate(hw_params),
356 snd_pcm_format_physical_width(params_format(hw_params)) *
357 params_channels(hw_params));
358 }
359
360 return ret;
361}
362
363/*
364 * hw_free callback
365 *
366 * reset the audio format and release the buffer
367 */
368static int snd_usb_hw_free(struct snd_pcm_substream *substream)
369{
370 struct snd_usb_substream *subs = substream->runtime->private_data;
371
372 subs->cur_audiofmt = NULL;
373 subs->cur_rate = 0;
374 subs->period_bytes = 0;
375 if (!subs->stream->chip->shutdown)
376 snd_usb_release_substream_urbs(subs, 0);
377 return snd_pcm_lib_free_vmalloc_buffer(substream);
378}
379
380/*
381 * prepare callback
382 *
383 * only a few subtle things...
384 */
385static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
386{
387 struct snd_pcm_runtime *runtime = substream->runtime;
388 struct snd_usb_substream *subs = runtime->private_data;
389
390 if (! subs->cur_audiofmt) {
391 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
392 return -ENXIO;
393 }
394
395 /* some unit conversions in runtime */
396 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
397 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
398
399 /* reset the pointer */
400 subs->hwptr_done = 0;
401 subs->transfer_done = 0;
402 subs->phase = 0;
403 runtime->delay = 0;
404
405 return snd_usb_substream_prepare(subs, runtime);
406}
407
408static struct snd_pcm_hardware snd_usb_hardware =
409{
410 .info = SNDRV_PCM_INFO_MMAP |
411 SNDRV_PCM_INFO_MMAP_VALID |
412 SNDRV_PCM_INFO_BATCH |
413 SNDRV_PCM_INFO_INTERLEAVED |
414 SNDRV_PCM_INFO_BLOCK_TRANSFER |
415 SNDRV_PCM_INFO_PAUSE,
416 .buffer_bytes_max = 1024 * 1024,
417 .period_bytes_min = 64,
418 .period_bytes_max = 512 * 1024,
419 .periods_min = 2,
420 .periods_max = 1024,
421};
422
423static int hw_check_valid_format(struct snd_usb_substream *subs,
424 struct snd_pcm_hw_params *params,
425 struct audioformat *fp)
426{
427 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
428 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
429 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
430 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
431 unsigned int ptime;
432
433 /* check the format */
434 if (!snd_mask_test(fmts, fp->format)) {
435 hwc_debug(" > check: no supported format %d\n", fp->format);
436 return 0;
437 }
438 /* check the channels */
439 if (fp->channels < ct->min || fp->channels > ct->max) {
440 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
441 return 0;
442 }
443 /* check the rate is within the range */
444 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
445 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
446 return 0;
447 }
448 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
449 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
450 return 0;
451 }
452 /* check whether the period time is >= the data packet interval */
453 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
454 ptime = 125 * (1 << fp->datainterval);
455 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
456 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
457 return 0;
458 }
459 }
460 return 1;
461}
462
463static int hw_rule_rate(struct snd_pcm_hw_params *params,
464 struct snd_pcm_hw_rule *rule)
465{
466 struct snd_usb_substream *subs = rule->private;
467 struct list_head *p;
468 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
469 unsigned int rmin, rmax;
470 int changed;
471
472 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
473 changed = 0;
474 rmin = rmax = 0;
475 list_for_each(p, &subs->fmt_list) {
476 struct audioformat *fp;
477 fp = list_entry(p, struct audioformat, list);
478 if (!hw_check_valid_format(subs, params, fp))
479 continue;
480 if (changed++) {
481 if (rmin > fp->rate_min)
482 rmin = fp->rate_min;
483 if (rmax < fp->rate_max)
484 rmax = fp->rate_max;
485 } else {
486 rmin = fp->rate_min;
487 rmax = fp->rate_max;
488 }
489 }
490
491 if (!changed) {
492 hwc_debug(" --> get empty\n");
493 it->empty = 1;
494 return -EINVAL;
495 }
496
497 changed = 0;
498 if (it->min < rmin) {
499 it->min = rmin;
500 it->openmin = 0;
501 changed = 1;
502 }
503 if (it->max > rmax) {
504 it->max = rmax;
505 it->openmax = 0;
506 changed = 1;
507 }
508 if (snd_interval_checkempty(it)) {
509 it->empty = 1;
510 return -EINVAL;
511 }
512 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
513 return changed;
514}
515
516
517static int hw_rule_channels(struct snd_pcm_hw_params *params,
518 struct snd_pcm_hw_rule *rule)
519{
520 struct snd_usb_substream *subs = rule->private;
521 struct list_head *p;
522 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
523 unsigned int rmin, rmax;
524 int changed;
525
526 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
527 changed = 0;
528 rmin = rmax = 0;
529 list_for_each(p, &subs->fmt_list) {
530 struct audioformat *fp;
531 fp = list_entry(p, struct audioformat, list);
532 if (!hw_check_valid_format(subs, params, fp))
533 continue;
534 if (changed++) {
535 if (rmin > fp->channels)
536 rmin = fp->channels;
537 if (rmax < fp->channels)
538 rmax = fp->channels;
539 } else {
540 rmin = fp->channels;
541 rmax = fp->channels;
542 }
543 }
544
545 if (!changed) {
546 hwc_debug(" --> get empty\n");
547 it->empty = 1;
548 return -EINVAL;
549 }
550
551 changed = 0;
552 if (it->min < rmin) {
553 it->min = rmin;
554 it->openmin = 0;
555 changed = 1;
556 }
557 if (it->max > rmax) {
558 it->max = rmax;
559 it->openmax = 0;
560 changed = 1;
561 }
562 if (snd_interval_checkempty(it)) {
563 it->empty = 1;
564 return -EINVAL;
565 }
566 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
567 return changed;
568}
569
570static int hw_rule_format(struct snd_pcm_hw_params *params,
571 struct snd_pcm_hw_rule *rule)
572{
573 struct snd_usb_substream *subs = rule->private;
574 struct list_head *p;
575 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
576 u64 fbits;
577 u32 oldbits[2];
578 int changed;
579
580 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
581 fbits = 0;
582 list_for_each(p, &subs->fmt_list) {
583 struct audioformat *fp;
584 fp = list_entry(p, struct audioformat, list);
585 if (!hw_check_valid_format(subs, params, fp))
586 continue;
587 fbits |= (1ULL << fp->format);
588 }
589
590 oldbits[0] = fmt->bits[0];
591 oldbits[1] = fmt->bits[1];
592 fmt->bits[0] &= (u32)fbits;
593 fmt->bits[1] &= (u32)(fbits >> 32);
594 if (!fmt->bits[0] && !fmt->bits[1]) {
595 hwc_debug(" --> get empty\n");
596 return -EINVAL;
597 }
598 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
599 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
600 return changed;
601}
602
603static int hw_rule_period_time(struct snd_pcm_hw_params *params,
604 struct snd_pcm_hw_rule *rule)
605{
606 struct snd_usb_substream *subs = rule->private;
607 struct audioformat *fp;
608 struct snd_interval *it;
609 unsigned char min_datainterval;
610 unsigned int pmin;
611 int changed;
612
613 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
614 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
615 min_datainterval = 0xff;
616 list_for_each_entry(fp, &subs->fmt_list, list) {
617 if (!hw_check_valid_format(subs, params, fp))
618 continue;
619 min_datainterval = min(min_datainterval, fp->datainterval);
620 }
621 if (min_datainterval == 0xff) {
622 hwc_debug(" --> get emtpy\n");
623 it->empty = 1;
624 return -EINVAL;
625 }
626 pmin = 125 * (1 << min_datainterval);
627 changed = 0;
628 if (it->min < pmin) {
629 it->min = pmin;
630 it->openmin = 0;
631 changed = 1;
632 }
633 if (snd_interval_checkempty(it)) {
634 it->empty = 1;
635 return -EINVAL;
636 }
637 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
638 return changed;
639}
640
641/*
642 * If the device supports unusual bit rates, does the request meet these?
643 */
644static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
645 struct snd_usb_substream *subs)
646{
647 struct audioformat *fp;
648 int count = 0, needs_knot = 0;
649 int err;
650
651 list_for_each_entry(fp, &subs->fmt_list, list) {
652 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
653 return 0;
654 count += fp->nr_rates;
655 if (fp->rates & SNDRV_PCM_RATE_KNOT)
656 needs_knot = 1;
657 }
658 if (!needs_knot)
659 return 0;
660
661 subs->rate_list.count = count;
662 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
663 subs->rate_list.mask = 0;
664 count = 0;
665 list_for_each_entry(fp, &subs->fmt_list, list) {
666 int i;
667 for (i = 0; i < fp->nr_rates; i++)
668 subs->rate_list.list[count++] = fp->rate_table[i];
669 }
670 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
671 &subs->rate_list);
672 if (err < 0)
673 return err;
674
675 return 0;
676}
677
678
679/*
680 * set up the runtime hardware information.
681 */
682
683static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
684{
685 struct list_head *p;
686 unsigned int pt, ptmin;
687 int param_period_time_if_needed;
688 int err;
689
690 runtime->hw.formats = subs->formats;
691
692 runtime->hw.rate_min = 0x7fffffff;
693 runtime->hw.rate_max = 0;
694 runtime->hw.channels_min = 256;
695 runtime->hw.channels_max = 0;
696 runtime->hw.rates = 0;
697 ptmin = UINT_MAX;
698 /* check min/max rates and channels */
699 list_for_each(p, &subs->fmt_list) {
700 struct audioformat *fp;
701 fp = list_entry(p, struct audioformat, list);
702 runtime->hw.rates |= fp->rates;
703 if (runtime->hw.rate_min > fp->rate_min)
704 runtime->hw.rate_min = fp->rate_min;
705 if (runtime->hw.rate_max < fp->rate_max)
706 runtime->hw.rate_max = fp->rate_max;
707 if (runtime->hw.channels_min > fp->channels)
708 runtime->hw.channels_min = fp->channels;
709 if (runtime->hw.channels_max < fp->channels)
710 runtime->hw.channels_max = fp->channels;
711 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
712 /* FIXME: there might be more than one audio formats... */
713 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
714 fp->frame_size;
715 }
716 pt = 125 * (1 << fp->datainterval);
717 ptmin = min(ptmin, pt);
718 }
719
720 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
721 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
722 /* full speed devices have fixed data packet interval */
723 ptmin = 1000;
724 if (ptmin == 1000)
725 /* if period time doesn't go below 1 ms, no rules needed */
726 param_period_time_if_needed = -1;
727 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
728 ptmin, UINT_MAX);
729
730 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
731 hw_rule_rate, subs,
732 SNDRV_PCM_HW_PARAM_FORMAT,
733 SNDRV_PCM_HW_PARAM_CHANNELS,
734 param_period_time_if_needed,
735 -1)) < 0)
736 return err;
737 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
738 hw_rule_channels, subs,
739 SNDRV_PCM_HW_PARAM_FORMAT,
740 SNDRV_PCM_HW_PARAM_RATE,
741 param_period_time_if_needed,
742 -1)) < 0)
743 return err;
744 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
745 hw_rule_format, subs,
746 SNDRV_PCM_HW_PARAM_RATE,
747 SNDRV_PCM_HW_PARAM_CHANNELS,
748 param_period_time_if_needed,
749 -1)) < 0)
750 return err;
751 if (param_period_time_if_needed >= 0) {
752 err = snd_pcm_hw_rule_add(runtime, 0,
753 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
754 hw_rule_period_time, subs,
755 SNDRV_PCM_HW_PARAM_FORMAT,
756 SNDRV_PCM_HW_PARAM_CHANNELS,
757 SNDRV_PCM_HW_PARAM_RATE,
758 -1);
759 if (err < 0)
760 return err;
761 }
762 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
763 return err;
764 return 0;
765}
766
767static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
768{
769 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
770 struct snd_pcm_runtime *runtime = substream->runtime;
771 struct snd_usb_substream *subs = &as->substream[direction];
772
773 subs->interface = -1;
774 subs->format = 0;
775 runtime->hw = snd_usb_hardware;
776 runtime->private_data = subs;
777 subs->pcm_substream = substream;
778 return setup_hw_info(runtime, subs);
779}
780
781static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
782{
783 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
784 struct snd_usb_substream *subs = &as->substream[direction];
785
786 if (!as->chip->shutdown && subs->interface >= 0) {
787 usb_set_interface(subs->dev, subs->interface, 0);
788 subs->interface = -1;
789 }
790 subs->pcm_substream = NULL;
791 return 0;
792}
793
794static int snd_usb_playback_open(struct snd_pcm_substream *substream)
795{
796 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
797}
798
799static int snd_usb_playback_close(struct snd_pcm_substream *substream)
800{
801 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
802}
803
804static int snd_usb_capture_open(struct snd_pcm_substream *substream)
805{
806 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
807}
808
809static int snd_usb_capture_close(struct snd_pcm_substream *substream)
810{
811 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
812}
813
814static struct snd_pcm_ops snd_usb_playback_ops = {
815 .open = snd_usb_playback_open,
816 .close = snd_usb_playback_close,
817 .ioctl = snd_pcm_lib_ioctl,
818 .hw_params = snd_usb_hw_params,
819 .hw_free = snd_usb_hw_free,
820 .prepare = snd_usb_pcm_prepare,
821 .trigger = snd_usb_substream_playback_trigger,
822 .pointer = snd_usb_pcm_pointer,
823 .page = snd_pcm_lib_get_vmalloc_page,
824 .mmap = snd_pcm_lib_mmap_vmalloc,
825};
826
827static struct snd_pcm_ops snd_usb_capture_ops = {
828 .open = snd_usb_capture_open,
829 .close = snd_usb_capture_close,
830 .ioctl = snd_pcm_lib_ioctl,
831 .hw_params = snd_usb_hw_params,
832 .hw_free = snd_usb_hw_free,
833 .prepare = snd_usb_pcm_prepare,
834 .trigger = snd_usb_substream_capture_trigger,
835 .pointer = snd_usb_pcm_pointer,
836 .page = snd_pcm_lib_get_vmalloc_page,
837 .mmap = snd_pcm_lib_mmap_vmalloc,
838};
839
840void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
841{
842 snd_pcm_set_ops(pcm, stream,
843 stream == SNDRV_PCM_STREAM_PLAYBACK ?
844 &snd_usb_playback_ops : &snd_usb_capture_ops);
845}
diff --git a/sound/usb/pcm.h b/sound/usb/pcm.h
new file mode 100644
index 000000000000..85856016e056
--- /dev/null
+++ b/sound/usb/pcm.h
@@ -0,0 +1,14 @@
1#ifndef __USBAUDIO_PCM_H
2#define __USBAUDIO_PCM_H
3
4void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream);
5
6int snd_usb_init_pitch(struct usb_device *dev, int iface,
7 struct usb_host_interface *alts,
8 struct audioformat *fmt);
9
10int snd_usb_init_sample_rate(struct usb_device *dev, int iface,
11 struct usb_host_interface *alts,
12 struct audioformat *fmt, int rate);
13
14#endif /* __USBAUDIO_PCM_H */
diff --git a/sound/usb/proc.c b/sound/usb/proc.c
new file mode 100644
index 000000000000..be3065ea1afa
--- /dev/null
+++ b/sound/usb/proc.c
@@ -0,0 +1,163 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 */
17
18#include <linux/init.h>
19#include <linux/usb.h>
20
21#include <sound/core.h>
22#include <sound/info.h>
23#include <sound/pcm.h>
24
25#include "usbaudio.h"
26#include "helper.h"
27#include "card.h"
28#include "proc.h"
29
30/* convert our full speed USB rate into sampling rate in Hz */
31static inline unsigned get_full_speed_hz(unsigned int usb_rate)
32{
33 return (usb_rate * 125 + (1 << 12)) >> 13;
34}
35
36/* convert our high speed USB rate into sampling rate in Hz */
37static inline unsigned get_high_speed_hz(unsigned int usb_rate)
38{
39 return (usb_rate * 125 + (1 << 9)) >> 10;
40}
41
42/*
43 * common proc files to show the usb device info
44 */
45static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
46{
47 struct snd_usb_audio *chip = entry->private_data;
48 if (!chip->shutdown)
49 snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
50}
51
52static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
53{
54 struct snd_usb_audio *chip = entry->private_data;
55 if (!chip->shutdown)
56 snd_iprintf(buffer, "%04x:%04x\n",
57 USB_ID_VENDOR(chip->usb_id),
58 USB_ID_PRODUCT(chip->usb_id));
59}
60
61void snd_usb_audio_create_proc(struct snd_usb_audio *chip)
62{
63 struct snd_info_entry *entry;
64 if (!snd_card_proc_new(chip->card, "usbbus", &entry))
65 snd_info_set_text_ops(entry, chip, proc_audio_usbbus_read);
66 if (!snd_card_proc_new(chip->card, "usbid", &entry))
67 snd_info_set_text_ops(entry, chip, proc_audio_usbid_read);
68}
69
70/*
71 * proc interface for list the supported pcm formats
72 */
73static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
74{
75 struct list_head *p;
76 static char *sync_types[4] = {
77 "NONE", "ASYNC", "ADAPTIVE", "SYNC"
78 };
79
80 list_for_each(p, &subs->fmt_list) {
81 struct audioformat *fp;
82 fp = list_entry(p, struct audioformat, list);
83 snd_iprintf(buffer, " Interface %d\n", fp->iface);
84 snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
85 snd_iprintf(buffer, " Format: %s\n",
86 snd_pcm_format_name(fp->format));
87 snd_iprintf(buffer, " Channels: %d\n", fp->channels);
88 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
89 fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
90 fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
91 sync_types[(fp->ep_attr & USB_ENDPOINT_SYNCTYPE) >> 2]);
92 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
93 snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
94 fp->rate_min, fp->rate_max);
95 } else {
96 unsigned int i;
97 snd_iprintf(buffer, " Rates: ");
98 for (i = 0; i < fp->nr_rates; i++) {
99 if (i > 0)
100 snd_iprintf(buffer, ", ");
101 snd_iprintf(buffer, "%d", fp->rate_table[i]);
102 }
103 snd_iprintf(buffer, "\n");
104 }
105 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
106 snd_iprintf(buffer, " Data packet interval: %d us\n",
107 125 * (1 << fp->datainterval));
108 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
109 // snd_iprintf(buffer, " EP Attribute = %#x\n", fp->attributes);
110 }
111}
112
113static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
114{
115 if (subs->running) {
116 unsigned int i;
117 snd_iprintf(buffer, " Status: Running\n");
118 snd_iprintf(buffer, " Interface = %d\n", subs->interface);
119 snd_iprintf(buffer, " Altset = %d\n", subs->format);
120 snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
121 for (i = 0; i < subs->nurbs; i++)
122 snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
123 snd_iprintf(buffer, "]\n");
124 snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
125 snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
126 snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
127 ? get_full_speed_hz(subs->freqm)
128 : get_high_speed_hz(subs->freqm),
129 subs->freqm >> 16, subs->freqm & 0xffff);
130 } else {
131 snd_iprintf(buffer, " Status: Stop\n");
132 }
133}
134
135static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
136{
137 struct snd_usb_stream *stream = entry->private_data;
138
139 snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
140
141 if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
142 snd_iprintf(buffer, "\nPlayback:\n");
143 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
144 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
145 }
146 if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
147 snd_iprintf(buffer, "\nCapture:\n");
148 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
149 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
150 }
151}
152
153void snd_usb_proc_pcm_format_add(struct snd_usb_stream *stream)
154{
155 struct snd_info_entry *entry;
156 char name[32];
157 struct snd_card *card = stream->chip->card;
158
159 sprintf(name, "stream%d", stream->pcm_index);
160 if (!snd_card_proc_new(card, name, &entry))
161 snd_info_set_text_ops(entry, stream, proc_pcm_format_read);
162}
163
diff --git a/sound/usb/proc.h b/sound/usb/proc.h
new file mode 100644
index 000000000000..a45b765e4cf1
--- /dev/null
+++ b/sound/usb/proc.h
@@ -0,0 +1,8 @@
1#ifndef __USBAUDIO_PROC_H
2#define __USBAUDIO_PROC_H
3
4void snd_usb_audio_create_proc(struct snd_usb_audio *chip);
5void snd_usb_proc_pcm_format_add(struct snd_usb_stream *stream);
6
7#endif /* __USBAUDIO_PROC_H */
8
diff --git a/sound/usb/usbquirks.h b/sound/usb/quirks-table.h
index 2b426c1fd0e8..2b426c1fd0e8 100644
--- a/sound/usb/usbquirks.h
+++ b/sound/usb/quirks-table.h
diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c
new file mode 100644
index 000000000000..4c16920844ea
--- /dev/null
+++ b/sound/usb/quirks.c
@@ -0,0 +1,592 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17#include <linux/init.h>
18#include <linux/usb.h>
19#include <linux/usb/audio.h>
20
21#include <sound/core.h>
22#include <sound/info.h>
23#include <sound/pcm.h>
24
25#include "usbaudio.h"
26#include "card.h"
27#include "usbmixer.h"
28#include "midi.h"
29#include "quirks.h"
30#include "helper.h"
31#include "endpoint.h"
32#include "pcm.h"
33
34/*
35 * handle the quirks for the contained interfaces
36 */
37static int create_composite_quirk(struct snd_usb_audio *chip,
38 struct usb_interface *iface,
39 struct usb_driver *driver,
40 const struct snd_usb_audio_quirk *quirk)
41{
42 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
43 int err;
44
45 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
46 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
47 if (!iface)
48 continue;
49 if (quirk->ifnum != probed_ifnum &&
50 usb_interface_claimed(iface))
51 continue;
52 err = snd_usb_create_quirk(chip, iface, driver, quirk);
53 if (err < 0)
54 return err;
55 if (quirk->ifnum != probed_ifnum)
56 usb_driver_claim_interface(driver, iface, (void *)-1L);
57 }
58 return 0;
59}
60
61static int ignore_interface_quirk(struct snd_usb_audio *chip,
62 struct usb_interface *iface,
63 struct usb_driver *driver,
64 const struct snd_usb_audio_quirk *quirk)
65{
66 return 0;
67}
68
69
70/*
71 * Allow alignment on audio sub-slot (channel samples) rather than
72 * on audio slots (audio frames)
73 */
74static int create_align_transfer_quirk(struct snd_usb_audio *chip,
75 struct usb_interface *iface,
76 struct usb_driver *driver,
77 const struct snd_usb_audio_quirk *quirk)
78{
79 chip->txfr_quirk = 1;
80 return 1; /* Continue with creating streams and mixer */
81}
82
83static int create_any_midi_quirk(struct snd_usb_audio *chip,
84 struct usb_interface *intf,
85 struct usb_driver *driver,
86 const struct snd_usb_audio_quirk *quirk)
87{
88 return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
89}
90
91/*
92 * create a stream for an interface with proper descriptors
93 */
94static int create_standard_audio_quirk(struct snd_usb_audio *chip,
95 struct usb_interface *iface,
96 struct usb_driver *driver,
97 const struct snd_usb_audio_quirk *quirk)
98{
99 struct usb_host_interface *alts;
100 struct usb_interface_descriptor *altsd;
101 int err;
102
103 alts = &iface->altsetting[0];
104 altsd = get_iface_desc(alts);
105 err = snd_usb_parse_audio_endpoints(chip, altsd->bInterfaceNumber);
106 if (err < 0) {
107 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
108 altsd->bInterfaceNumber, err);
109 return err;
110 }
111 /* reset the current interface */
112 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
113 return 0;
114}
115
116/*
117 * create a stream for an endpoint/altsetting without proper descriptors
118 */
119static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
120 struct usb_interface *iface,
121 struct usb_driver *driver,
122 const struct snd_usb_audio_quirk *quirk)
123{
124 struct audioformat *fp;
125 struct usb_host_interface *alts;
126 int stream, err;
127 unsigned *rate_table = NULL;
128
129 fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
130 if (! fp) {
131 snd_printk(KERN_ERR "cannot memdup\n");
132 return -ENOMEM;
133 }
134 if (fp->nr_rates > 0) {
135 rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
136 if (!rate_table) {
137 kfree(fp);
138 return -ENOMEM;
139 }
140 memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
141 fp->rate_table = rate_table;
142 }
143
144 stream = (fp->endpoint & USB_DIR_IN)
145 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
146 err = snd_usb_add_audio_endpoint(chip, stream, fp);
147 if (err < 0) {
148 kfree(fp);
149 kfree(rate_table);
150 return err;
151 }
152 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
153 fp->altset_idx >= iface->num_altsetting) {
154 kfree(fp);
155 kfree(rate_table);
156 return -EINVAL;
157 }
158 alts = &iface->altsetting[fp->altset_idx];
159 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
160 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
161 usb_set_interface(chip->dev, fp->iface, 0);
162 snd_usb_init_pitch(chip->dev, fp->iface, alts, fp);
163 snd_usb_init_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
164 return 0;
165}
166
167/*
168 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
169 * The only way to detect the sample rate is by looking at wMaxPacketSize.
170 */
171static int create_uaxx_quirk(struct snd_usb_audio *chip,
172 struct usb_interface *iface,
173 struct usb_driver *driver,
174 const struct snd_usb_audio_quirk *quirk)
175{
176 static const struct audioformat ua_format = {
177 .format = SNDRV_PCM_FORMAT_S24_3LE,
178 .channels = 2,
179 .fmt_type = UAC_FORMAT_TYPE_I,
180 .altsetting = 1,
181 .altset_idx = 1,
182 .rates = SNDRV_PCM_RATE_CONTINUOUS,
183 };
184 struct usb_host_interface *alts;
185 struct usb_interface_descriptor *altsd;
186 struct audioformat *fp;
187 int stream, err;
188
189 /* both PCM and MIDI interfaces have 2 or more altsettings */
190 if (iface->num_altsetting < 2)
191 return -ENXIO;
192 alts = &iface->altsetting[1];
193 altsd = get_iface_desc(alts);
194
195 if (altsd->bNumEndpoints == 2) {
196 static const struct snd_usb_midi_endpoint_info ua700_ep = {
197 .out_cables = 0x0003,
198 .in_cables = 0x0003
199 };
200 static const struct snd_usb_audio_quirk ua700_quirk = {
201 .type = QUIRK_MIDI_FIXED_ENDPOINT,
202 .data = &ua700_ep
203 };
204 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
205 .out_cables = 0x0001,
206 .in_cables = 0x0001
207 };
208 static const struct snd_usb_audio_quirk uaxx_quirk = {
209 .type = QUIRK_MIDI_FIXED_ENDPOINT,
210 .data = &uaxx_ep
211 };
212 const struct snd_usb_audio_quirk *quirk =
213 chip->usb_id == USB_ID(0x0582, 0x002b)
214 ? &ua700_quirk : &uaxx_quirk;
215 return snd_usbmidi_create(chip->card, iface,
216 &chip->midi_list, quirk);
217 }
218
219 if (altsd->bNumEndpoints != 1)
220 return -ENXIO;
221
222 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
223 if (!fp)
224 return -ENOMEM;
225 memcpy(fp, &ua_format, sizeof(*fp));
226
227 fp->iface = altsd->bInterfaceNumber;
228 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
229 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
230 fp->datainterval = 0;
231 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
232
233 switch (fp->maxpacksize) {
234 case 0x120:
235 fp->rate_max = fp->rate_min = 44100;
236 break;
237 case 0x138:
238 case 0x140:
239 fp->rate_max = fp->rate_min = 48000;
240 break;
241 case 0x258:
242 case 0x260:
243 fp->rate_max = fp->rate_min = 96000;
244 break;
245 default:
246 snd_printk(KERN_ERR "unknown sample rate\n");
247 kfree(fp);
248 return -ENXIO;
249 }
250
251 stream = (fp->endpoint & USB_DIR_IN)
252 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
253 err = snd_usb_add_audio_endpoint(chip, stream, fp);
254 if (err < 0) {
255 kfree(fp);
256 return err;
257 }
258 usb_set_interface(chip->dev, fp->iface, 0);
259 return 0;
260}
261
262/*
263 * audio-interface quirks
264 *
265 * returns zero if no standard audio/MIDI parsing is needed.
266 * returns a postive value if standard audio/midi interfaces are parsed
267 * after this.
268 * returns a negative value at error.
269 */
270int snd_usb_create_quirk(struct snd_usb_audio *chip,
271 struct usb_interface *iface,
272 struct usb_driver *driver,
273 const struct snd_usb_audio_quirk *quirk)
274{
275 typedef int (*quirk_func_t)(struct snd_usb_audio *,
276 struct usb_interface *,
277 struct usb_driver *,
278 const struct snd_usb_audio_quirk *);
279 static const quirk_func_t quirk_funcs[] = {
280 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
281 [QUIRK_COMPOSITE] = create_composite_quirk,
282 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
283 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
284 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
285 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
286 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
287 [QUIRK_MIDI_FASTLANE] = create_any_midi_quirk,
288 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
289 [QUIRK_MIDI_CME] = create_any_midi_quirk,
290 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
291 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
292 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
293 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk
294 };
295
296 if (quirk->type < QUIRK_TYPE_COUNT) {
297 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
298 } else {
299 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
300 return -ENXIO;
301 }
302}
303
304/*
305 * boot quirks
306 */
307
308#define EXTIGY_FIRMWARE_SIZE_OLD 794
309#define EXTIGY_FIRMWARE_SIZE_NEW 483
310
311static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
312{
313 struct usb_host_config *config = dev->actconfig;
314 int err;
315
316 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
317 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
318 snd_printdd("sending Extigy boot sequence...\n");
319 /* Send message to force it to reconnect with full interface. */
320 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
321 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
322 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
323 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
324 &dev->descriptor, sizeof(dev->descriptor));
325 config = dev->actconfig;
326 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
327 err = usb_reset_configuration(dev);
328 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
329 snd_printdd("extigy_boot: new boot length = %d\n",
330 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
331 return -ENODEV; /* quit this anyway */
332 }
333 return 0;
334}
335
336static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
337{
338 u8 buf = 1;
339
340 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
341 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
342 0, 0, &buf, 1, 1000);
343 if (buf == 0) {
344 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
345 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
346 1, 2000, NULL, 0, 1000);
347 return -ENODEV;
348 }
349 return 0;
350}
351
352/*
353 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
354 * documented in the device's data sheet.
355 */
356static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
357{
358 u8 buf[4];
359 buf[0] = 0x20;
360 buf[1] = value & 0xff;
361 buf[2] = (value >> 8) & 0xff;
362 buf[3] = reg;
363 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
364 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
365 0, 0, &buf, 4, 1000);
366}
367
368static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
369{
370 /*
371 * Enable line-out driver mode, set headphone source to front
372 * channels, enable stereo mic.
373 */
374 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
375}
376
377/*
378 * C-Media CM6206 is based on CM106 with two additional
379 * registers that are not documented in the data sheet.
380 * Values here are chosen based on sniffing USB traffic
381 * under Windows.
382 */
383static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
384{
385 int err, reg;
386 int val[] = {0x200c, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
387
388 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
389 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
390 if (err < 0)
391 return err;
392 }
393
394 return err;
395}
396
397/*
398 * This call will put the synth in "USB send" mode, i.e it will send MIDI
399 * messages through USB (this is disabled at startup). The synth will
400 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
401 * sign on its LCD. Values here are chosen based on sniffing USB traffic
402 * under Windows.
403 */
404static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
405{
406 int err, actual_length;
407
408 /* "midi send" enable */
409 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
410
411 void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
412 if (!buf)
413 return -ENOMEM;
414 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
415 ARRAY_SIZE(seq), &actual_length, 1000);
416 kfree(buf);
417 if (err < 0)
418 return err;
419
420 return 0;
421}
422
423/*
424 * Setup quirks
425 */
426#define AUDIOPHILE_SET 0x01 /* if set, parse device_setup */
427#define AUDIOPHILE_SET_DTS 0x02 /* if set, enable DTS Digital Output */
428#define AUDIOPHILE_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
429#define AUDIOPHILE_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
430#define AUDIOPHILE_SET_DI 0x10 /* if set, enable Digital Input */
431#define AUDIOPHILE_SET_MASK 0x1F /* bit mask for setup value */
432#define AUDIOPHILE_SET_24B_48K_DI 0x19 /* value for 24bits+48KHz+Digital Input */
433#define AUDIOPHILE_SET_24B_48K_NOTDI 0x09 /* value for 24bits+48KHz+No Digital Input */
434#define AUDIOPHILE_SET_16B_48K_DI 0x11 /* value for 16bits+48KHz+Digital Input */
435#define AUDIOPHILE_SET_16B_48K_NOTDI 0x01 /* value for 16bits+48KHz+No Digital Input */
436
437static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
438 int iface,
439 int altno)
440{
441 /* Reset ALL ifaces to 0 altsetting.
442 * Call it for every possible altsetting of every interface.
443 */
444 usb_set_interface(chip->dev, iface, 0);
445
446 if (chip->setup & AUDIOPHILE_SET) {
447 if ((chip->setup & AUDIOPHILE_SET_DTS)
448 && altno != 6)
449 return 1; /* skip this altsetting */
450 if ((chip->setup & AUDIOPHILE_SET_96K)
451 && altno != 1)
452 return 1; /* skip this altsetting */
453 if ((chip->setup & AUDIOPHILE_SET_MASK) ==
454 AUDIOPHILE_SET_24B_48K_DI && altno != 2)
455 return 1; /* skip this altsetting */
456 if ((chip->setup & AUDIOPHILE_SET_MASK) ==
457 AUDIOPHILE_SET_24B_48K_NOTDI && altno != 3)
458 return 1; /* skip this altsetting */
459 if ((chip->setup & AUDIOPHILE_SET_MASK) ==
460 AUDIOPHILE_SET_16B_48K_DI && altno != 4)
461 return 1; /* skip this altsetting */
462 if ((chip->setup & AUDIOPHILE_SET_MASK) ==
463 AUDIOPHILE_SET_16B_48K_NOTDI && altno != 5)
464 return 1; /* skip this altsetting */
465 }
466
467 return 0; /* keep this altsetting */
468}
469
470int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
471 int iface,
472 int altno)
473{
474 /* audiophile usb: skip altsets incompatible with device_setup */
475 if (chip->usb_id == USB_ID(0x0763, 0x2003))
476 return audiophile_skip_setting_quirk(chip, iface, altno);
477
478 return 0;
479}
480
481int snd_usb_apply_boot_quirk(struct usb_device *dev,
482 struct usb_interface *intf,
483 const struct snd_usb_audio_quirk *quirk)
484{
485 u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
486 le16_to_cpu(dev->descriptor.idProduct));
487
488 /* SB Extigy needs special boot-up sequence */
489 /* if more models come, this will go to the quirk list. */
490 if (id == USB_ID(0x041e, 0x3000))
491 return snd_usb_extigy_boot_quirk(dev, intf);
492
493 /* SB Audigy 2 NX needs its own boot-up magic, too */
494 if (id == USB_ID(0x041e, 0x3020))
495 return snd_usb_audigy2nx_boot_quirk(dev);
496
497 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
498 if (id == USB_ID(0x10f5, 0x0200))
499 return snd_usb_cm106_boot_quirk(dev);
500
501 /* C-Media CM6206 / CM106-Like Sound Device */
502 if (id == USB_ID(0x0d8c, 0x0102))
503 return snd_usb_cm6206_boot_quirk(dev);
504
505 /* Access Music VirusTI Desktop */
506 if (id == USB_ID(0x133e, 0x0815))
507 return snd_usb_accessmusic_boot_quirk(dev);
508
509 return 0;
510}
511
512/*
513 * check if the device uses big-endian samples
514 */
515int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
516{
517 switch (chip->usb_id) {
518 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
519 if (fp->endpoint & USB_DIR_IN)
520 return 1;
521 break;
522 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
523 if (chip->setup == 0x00 ||
524 fp->altsetting==1 || fp->altsetting==2 || fp->altsetting==3)
525 return 1;
526 }
527 return 0;
528}
529
530/*
531 * For E-Mu 0404USB/0202USB/TrackerPre sample rate should be set for device,
532 * not for interface.
533 */
534
535enum {
536 EMU_QUIRK_SR_44100HZ = 0,
537 EMU_QUIRK_SR_48000HZ,
538 EMU_QUIRK_SR_88200HZ,
539 EMU_QUIRK_SR_96000HZ,
540 EMU_QUIRK_SR_176400HZ,
541 EMU_QUIRK_SR_192000HZ
542};
543
544static void set_format_emu_quirk(struct snd_usb_substream *subs,
545 struct audioformat *fmt)
546{
547 unsigned char emu_samplerate_id = 0;
548
549 /* When capture is active
550 * sample rate shouldn't be changed
551 * by playback substream
552 */
553 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
554 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
555 return;
556 }
557
558 switch (fmt->rate_min) {
559 case 48000:
560 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
561 break;
562 case 88200:
563 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
564 break;
565 case 96000:
566 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
567 break;
568 case 176400:
569 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
570 break;
571 case 192000:
572 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
573 break;
574 default:
575 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
576 break;
577 }
578 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
579}
580
581void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
582 struct audioformat *fmt)
583{
584 switch (subs->stream->chip->usb_id) {
585 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
586 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
587 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
588 set_format_emu_quirk(subs, fmt);
589 break;
590 }
591}
592
diff --git a/sound/usb/quirks.h b/sound/usb/quirks.h
new file mode 100644
index 000000000000..03e5e94098cd
--- /dev/null
+++ b/sound/usb/quirks.h
@@ -0,0 +1,23 @@
1#ifndef __USBAUDIO_QUIRKS_H
2#define __USBAUDIO_QUIRKS_H
3
4int snd_usb_create_quirk(struct snd_usb_audio *chip,
5 struct usb_interface *iface,
6 struct usb_driver *driver,
7 const struct snd_usb_audio_quirk *quirk);
8
9int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
10 int iface,
11 int altno);
12
13int snd_usb_apply_boot_quirk(struct usb_device *dev,
14 struct usb_interface *intf,
15 const struct snd_usb_audio_quirk *quirk);
16
17void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
18 struct audioformat *fmt);
19
20int snd_usb_is_big_endian_format(struct snd_usb_audio *chip,
21 struct audioformat *fp);
22
23#endif /* __USBAUDIO_QUIRKS_H */
diff --git a/sound/usb/urb.c b/sound/usb/urb.c
new file mode 100644
index 000000000000..e9c339f75861
--- /dev/null
+++ b/sound/usb/urb.c
@@ -0,0 +1,989 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 */
17
18#include <linux/init.h>
19#include <linux/usb.h>
20#include <linux/usb/audio.h>
21
22#include <sound/core.h>
23#include <sound/pcm.h>
24
25#include "usbaudio.h"
26#include "helper.h"
27#include "card.h"
28#include "urb.h"
29#include "pcm.h"
30
31/*
32 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
33 * this will overflow at approx 524 kHz
34 */
35static inline unsigned get_usb_full_speed_rate(unsigned int rate)
36{
37 return ((rate << 13) + 62) / 125;
38}
39
40/*
41 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
42 * this will overflow at approx 4 MHz
43 */
44static inline unsigned get_usb_high_speed_rate(unsigned int rate)
45{
46 return ((rate << 10) + 62) / 125;
47}
48
49/*
50 * unlink active urbs.
51 */
52static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
53{
54 struct snd_usb_audio *chip = subs->stream->chip;
55 unsigned int i;
56 int async;
57
58 subs->running = 0;
59
60 if (!force && subs->stream->chip->shutdown) /* to be sure... */
61 return -EBADFD;
62
63 async = !can_sleep && chip->async_unlink;
64
65 if (!async && in_interrupt())
66 return 0;
67
68 for (i = 0; i < subs->nurbs; i++) {
69 if (test_bit(i, &subs->active_mask)) {
70 if (!test_and_set_bit(i, &subs->unlink_mask)) {
71 struct urb *u = subs->dataurb[i].urb;
72 if (async)
73 usb_unlink_urb(u);
74 else
75 usb_kill_urb(u);
76 }
77 }
78 }
79 if (subs->syncpipe) {
80 for (i = 0; i < SYNC_URBS; i++) {
81 if (test_bit(i+16, &subs->active_mask)) {
82 if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
83 struct urb *u = subs->syncurb[i].urb;
84 if (async)
85 usb_unlink_urb(u);
86 else
87 usb_kill_urb(u);
88 }
89 }
90 }
91 }
92 return 0;
93}
94
95
96/*
97 * release a urb data
98 */
99static void release_urb_ctx(struct snd_urb_ctx *u)
100{
101 if (u->urb) {
102 if (u->buffer_size)
103 usb_buffer_free(u->subs->dev, u->buffer_size,
104 u->urb->transfer_buffer,
105 u->urb->transfer_dma);
106 usb_free_urb(u->urb);
107 u->urb = NULL;
108 }
109}
110
111/*
112 * wait until all urbs are processed.
113 */
114static int wait_clear_urbs(struct snd_usb_substream *subs)
115{
116 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
117 unsigned int i;
118 int alive;
119
120 do {
121 alive = 0;
122 for (i = 0; i < subs->nurbs; i++) {
123 if (test_bit(i, &subs->active_mask))
124 alive++;
125 }
126 if (subs->syncpipe) {
127 for (i = 0; i < SYNC_URBS; i++) {
128 if (test_bit(i + 16, &subs->active_mask))
129 alive++;
130 }
131 }
132 if (! alive)
133 break;
134 schedule_timeout_uninterruptible(1);
135 } while (time_before(jiffies, end_time));
136 if (alive)
137 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
138 return 0;
139}
140
141/*
142 * release a substream
143 */
144void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force)
145{
146 int i;
147
148 /* stop urbs (to be sure) */
149 deactivate_urbs(subs, force, 1);
150 wait_clear_urbs(subs);
151
152 for (i = 0; i < MAX_URBS; i++)
153 release_urb_ctx(&subs->dataurb[i]);
154 for (i = 0; i < SYNC_URBS; i++)
155 release_urb_ctx(&subs->syncurb[i]);
156 usb_buffer_free(subs->dev, SYNC_URBS * 4,
157 subs->syncbuf, subs->sync_dma);
158 subs->syncbuf = NULL;
159 subs->nurbs = 0;
160}
161
162/*
163 * complete callback from data urb
164 */
165static void snd_complete_urb(struct urb *urb)
166{
167 struct snd_urb_ctx *ctx = urb->context;
168 struct snd_usb_substream *subs = ctx->subs;
169 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
170 int err = 0;
171
172 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
173 !subs->running || /* can be stopped during retire callback */
174 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
175 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
176 clear_bit(ctx->index, &subs->active_mask);
177 if (err < 0) {
178 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
179 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
180 }
181 }
182}
183
184
185/*
186 * complete callback from sync urb
187 */
188static void snd_complete_sync_urb(struct urb *urb)
189{
190 struct snd_urb_ctx *ctx = urb->context;
191 struct snd_usb_substream *subs = ctx->subs;
192 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
193 int err = 0;
194
195 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
196 !subs->running || /* can be stopped during retire callback */
197 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
198 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
199 clear_bit(ctx->index + 16, &subs->active_mask);
200 if (err < 0) {
201 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
202 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
203 }
204 }
205}
206
207
208/*
209 * initialize a substream for plaback/capture
210 */
211int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
212 unsigned int period_bytes,
213 unsigned int rate,
214 unsigned int frame_bits)
215{
216 unsigned int maxsize, i;
217 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
218 unsigned int urb_packs, total_packs, packs_per_ms;
219 struct snd_usb_audio *chip = subs->stream->chip;
220
221 /* calculate the frequency in 16.16 format */
222 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
223 subs->freqn = get_usb_full_speed_rate(rate);
224 else
225 subs->freqn = get_usb_high_speed_rate(rate);
226 subs->freqm = subs->freqn;
227 /* calculate max. frequency */
228 if (subs->maxpacksize) {
229 /* whatever fits into a max. size packet */
230 maxsize = subs->maxpacksize;
231 subs->freqmax = (maxsize / (frame_bits >> 3))
232 << (16 - subs->datainterval);
233 } else {
234 /* no max. packet size: just take 25% higher than nominal */
235 subs->freqmax = subs->freqn + (subs->freqn >> 2);
236 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
237 >> (16 - subs->datainterval);
238 }
239 subs->phase = 0;
240
241 if (subs->fill_max)
242 subs->curpacksize = subs->maxpacksize;
243 else
244 subs->curpacksize = maxsize;
245
246 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
247 packs_per_ms = 8 >> subs->datainterval;
248 else
249 packs_per_ms = 1;
250
251 if (is_playback) {
252 urb_packs = max(chip->nrpacks, 1);
253 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
254 } else
255 urb_packs = 1;
256 urb_packs *= packs_per_ms;
257 if (subs->syncpipe)
258 urb_packs = min(urb_packs, 1U << subs->syncinterval);
259
260 /* decide how many packets to be used */
261 if (is_playback) {
262 unsigned int minsize, maxpacks;
263 /* determine how small a packet can be */
264 minsize = (subs->freqn >> (16 - subs->datainterval))
265 * (frame_bits >> 3);
266 /* with sync from device, assume it can be 12% lower */
267 if (subs->syncpipe)
268 minsize -= minsize >> 3;
269 minsize = max(minsize, 1u);
270 total_packs = (period_bytes + minsize - 1) / minsize;
271 /* we need at least two URBs for queueing */
272 if (total_packs < 2) {
273 total_packs = 2;
274 } else {
275 /* and we don't want too long a queue either */
276 maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
277 total_packs = min(total_packs, maxpacks);
278 }
279 } else {
280 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
281 urb_packs >>= 1;
282 total_packs = MAX_URBS * urb_packs;
283 }
284 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
285 if (subs->nurbs > MAX_URBS) {
286 /* too much... */
287 subs->nurbs = MAX_URBS;
288 total_packs = MAX_URBS * urb_packs;
289 } else if (subs->nurbs < 2) {
290 /* too little - we need at least two packets
291 * to ensure contiguous playback/capture
292 */
293 subs->nurbs = 2;
294 }
295
296 /* allocate and initialize data urbs */
297 for (i = 0; i < subs->nurbs; i++) {
298 struct snd_urb_ctx *u = &subs->dataurb[i];
299 u->index = i;
300 u->subs = subs;
301 u->packets = (i + 1) * total_packs / subs->nurbs
302 - i * total_packs / subs->nurbs;
303 u->buffer_size = maxsize * u->packets;
304 if (subs->fmt_type == UAC_FORMAT_TYPE_II)
305 u->packets++; /* for transfer delimiter */
306 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
307 if (!u->urb)
308 goto out_of_memory;
309 u->urb->transfer_buffer =
310 usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL,
311 &u->urb->transfer_dma);
312 if (!u->urb->transfer_buffer)
313 goto out_of_memory;
314 u->urb->pipe = subs->datapipe;
315 u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
316 u->urb->interval = 1 << subs->datainterval;
317 u->urb->context = u;
318 u->urb->complete = snd_complete_urb;
319 }
320
321 if (subs->syncpipe) {
322 /* allocate and initialize sync urbs */
323 subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4,
324 GFP_KERNEL, &subs->sync_dma);
325 if (!subs->syncbuf)
326 goto out_of_memory;
327 for (i = 0; i < SYNC_URBS; i++) {
328 struct snd_urb_ctx *u = &subs->syncurb[i];
329 u->index = i;
330 u->subs = subs;
331 u->packets = 1;
332 u->urb = usb_alloc_urb(1, GFP_KERNEL);
333 if (!u->urb)
334 goto out_of_memory;
335 u->urb->transfer_buffer = subs->syncbuf + i * 4;
336 u->urb->transfer_dma = subs->sync_dma + i * 4;
337 u->urb->transfer_buffer_length = 4;
338 u->urb->pipe = subs->syncpipe;
339 u->urb->transfer_flags = URB_ISO_ASAP |
340 URB_NO_TRANSFER_DMA_MAP;
341 u->urb->number_of_packets = 1;
342 u->urb->interval = 1 << subs->syncinterval;
343 u->urb->context = u;
344 u->urb->complete = snd_complete_sync_urb;
345 }
346 }
347 return 0;
348
349out_of_memory:
350 snd_usb_release_substream_urbs(subs, 0);
351 return -ENOMEM;
352}
353
354/*
355 * prepare urb for full speed capture sync pipe
356 *
357 * fill the length and offset of each urb descriptor.
358 * the fixed 10.14 frequency is passed through the pipe.
359 */
360static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
361 struct snd_pcm_runtime *runtime,
362 struct urb *urb)
363{
364 unsigned char *cp = urb->transfer_buffer;
365 struct snd_urb_ctx *ctx = urb->context;
366
367 urb->dev = ctx->subs->dev; /* we need to set this at each time */
368 urb->iso_frame_desc[0].length = 3;
369 urb->iso_frame_desc[0].offset = 0;
370 cp[0] = subs->freqn >> 2;
371 cp[1] = subs->freqn >> 10;
372 cp[2] = subs->freqn >> 18;
373 return 0;
374}
375
376/*
377 * prepare urb for high speed capture sync pipe
378 *
379 * fill the length and offset of each urb descriptor.
380 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
381 */
382static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
383 struct snd_pcm_runtime *runtime,
384 struct urb *urb)
385{
386 unsigned char *cp = urb->transfer_buffer;
387 struct snd_urb_ctx *ctx = urb->context;
388
389 urb->dev = ctx->subs->dev; /* we need to set this at each time */
390 urb->iso_frame_desc[0].length = 4;
391 urb->iso_frame_desc[0].offset = 0;
392 cp[0] = subs->freqn;
393 cp[1] = subs->freqn >> 8;
394 cp[2] = subs->freqn >> 16;
395 cp[3] = subs->freqn >> 24;
396 return 0;
397}
398
399/*
400 * process after capture sync complete
401 * - nothing to do
402 */
403static int retire_capture_sync_urb(struct snd_usb_substream *subs,
404 struct snd_pcm_runtime *runtime,
405 struct urb *urb)
406{
407 return 0;
408}
409
410/*
411 * prepare urb for capture data pipe
412 *
413 * fill the offset and length of each descriptor.
414 *
415 * we use a temporary buffer to write the captured data.
416 * since the length of written data is determined by host, we cannot
417 * write onto the pcm buffer directly... the data is thus copied
418 * later at complete callback to the global buffer.
419 */
420static int prepare_capture_urb(struct snd_usb_substream *subs,
421 struct snd_pcm_runtime *runtime,
422 struct urb *urb)
423{
424 int i, offs;
425 struct snd_urb_ctx *ctx = urb->context;
426
427 offs = 0;
428 urb->dev = ctx->subs->dev; /* we need to set this at each time */
429 for (i = 0; i < ctx->packets; i++) {
430 urb->iso_frame_desc[i].offset = offs;
431 urb->iso_frame_desc[i].length = subs->curpacksize;
432 offs += subs->curpacksize;
433 }
434 urb->transfer_buffer_length = offs;
435 urb->number_of_packets = ctx->packets;
436 return 0;
437}
438
439/*
440 * process after capture complete
441 *
442 * copy the data from each desctiptor to the pcm buffer, and
443 * update the current position.
444 */
445static int retire_capture_urb(struct snd_usb_substream *subs,
446 struct snd_pcm_runtime *runtime,
447 struct urb *urb)
448{
449 unsigned long flags;
450 unsigned char *cp;
451 int i;
452 unsigned int stride, frames, bytes, oldptr;
453 int period_elapsed = 0;
454
455 stride = runtime->frame_bits >> 3;
456
457 for (i = 0; i < urb->number_of_packets; i++) {
458 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
459 if (urb->iso_frame_desc[i].status) {
460 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
461 // continue;
462 }
463 bytes = urb->iso_frame_desc[i].actual_length;
464 frames = bytes / stride;
465 if (!subs->txfr_quirk)
466 bytes = frames * stride;
467 if (bytes % (runtime->sample_bits >> 3) != 0) {
468#ifdef CONFIG_SND_DEBUG_VERBOSE
469 int oldbytes = bytes;
470#endif
471 bytes = frames * stride;
472 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
473 oldbytes, bytes);
474 }
475 /* update the current pointer */
476 spin_lock_irqsave(&subs->lock, flags);
477 oldptr = subs->hwptr_done;
478 subs->hwptr_done += bytes;
479 if (subs->hwptr_done >= runtime->buffer_size * stride)
480 subs->hwptr_done -= runtime->buffer_size * stride;
481 frames = (bytes + (oldptr % stride)) / stride;
482 subs->transfer_done += frames;
483 if (subs->transfer_done >= runtime->period_size) {
484 subs->transfer_done -= runtime->period_size;
485 period_elapsed = 1;
486 }
487 spin_unlock_irqrestore(&subs->lock, flags);
488 /* copy a data chunk */
489 if (oldptr + bytes > runtime->buffer_size * stride) {
490 unsigned int bytes1 =
491 runtime->buffer_size * stride - oldptr;
492 memcpy(runtime->dma_area + oldptr, cp, bytes1);
493 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
494 } else {
495 memcpy(runtime->dma_area + oldptr, cp, bytes);
496 }
497 }
498 if (period_elapsed)
499 snd_pcm_period_elapsed(subs->pcm_substream);
500 return 0;
501}
502
503/*
504 * Process after capture complete when paused. Nothing to do.
505 */
506static int retire_paused_capture_urb(struct snd_usb_substream *subs,
507 struct snd_pcm_runtime *runtime,
508 struct urb *urb)
509{
510 return 0;
511}
512
513
514/*
515 * prepare urb for full speed playback sync pipe
516 *
517 * set up the offset and length to receive the current frequency.
518 */
519
520static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
521 struct snd_pcm_runtime *runtime,
522 struct urb *urb)
523{
524 struct snd_urb_ctx *ctx = urb->context;
525
526 urb->dev = ctx->subs->dev; /* we need to set this at each time */
527 urb->iso_frame_desc[0].length = 3;
528 urb->iso_frame_desc[0].offset = 0;
529 return 0;
530}
531
532/*
533 * prepare urb for high speed playback sync pipe
534 *
535 * set up the offset and length to receive the current frequency.
536 */
537
538static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
539 struct snd_pcm_runtime *runtime,
540 struct urb *urb)
541{
542 struct snd_urb_ctx *ctx = urb->context;
543
544 urb->dev = ctx->subs->dev; /* we need to set this at each time */
545 urb->iso_frame_desc[0].length = 4;
546 urb->iso_frame_desc[0].offset = 0;
547 return 0;
548}
549
550/*
551 * process after full speed playback sync complete
552 *
553 * retrieve the current 10.14 frequency from pipe, and set it.
554 * the value is referred in prepare_playback_urb().
555 */
556static int retire_playback_sync_urb(struct snd_usb_substream *subs,
557 struct snd_pcm_runtime *runtime,
558 struct urb *urb)
559{
560 unsigned int f;
561 unsigned long flags;
562
563 if (urb->iso_frame_desc[0].status == 0 &&
564 urb->iso_frame_desc[0].actual_length == 3) {
565 f = combine_triple((u8*)urb->transfer_buffer) << 2;
566 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
567 spin_lock_irqsave(&subs->lock, flags);
568 subs->freqm = f;
569 spin_unlock_irqrestore(&subs->lock, flags);
570 }
571 }
572
573 return 0;
574}
575
576/*
577 * process after high speed playback sync complete
578 *
579 * retrieve the current 12.13 frequency from pipe, and set it.
580 * the value is referred in prepare_playback_urb().
581 */
582static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
583 struct snd_pcm_runtime *runtime,
584 struct urb *urb)
585{
586 unsigned int f;
587 unsigned long flags;
588
589 if (urb->iso_frame_desc[0].status == 0 &&
590 urb->iso_frame_desc[0].actual_length == 4) {
591 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
592 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
593 spin_lock_irqsave(&subs->lock, flags);
594 subs->freqm = f;
595 spin_unlock_irqrestore(&subs->lock, flags);
596 }
597 }
598
599 return 0;
600}
601
602/*
603 * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete
604 *
605 * These devices return the number of samples per packet instead of the number
606 * of samples per microframe.
607 */
608static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs,
609 struct snd_pcm_runtime *runtime,
610 struct urb *urb)
611{
612 unsigned int f;
613 unsigned long flags;
614
615 if (urb->iso_frame_desc[0].status == 0 &&
616 urb->iso_frame_desc[0].actual_length == 4) {
617 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
618 f >>= subs->datainterval;
619 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
620 spin_lock_irqsave(&subs->lock, flags);
621 subs->freqm = f;
622 spin_unlock_irqrestore(&subs->lock, flags);
623 }
624 }
625
626 return 0;
627}
628
629/* determine the number of frames in the next packet */
630static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
631{
632 if (subs->fill_max)
633 return subs->maxframesize;
634 else {
635 subs->phase = (subs->phase & 0xffff)
636 + (subs->freqm << subs->datainterval);
637 return min(subs->phase >> 16, subs->maxframesize);
638 }
639}
640
641/*
642 * Prepare urb for streaming before playback starts or when paused.
643 *
644 * We don't have any data, so we send silence.
645 */
646static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
647 struct snd_pcm_runtime *runtime,
648 struct urb *urb)
649{
650 unsigned int i, offs, counts;
651 struct snd_urb_ctx *ctx = urb->context;
652 int stride = runtime->frame_bits >> 3;
653
654 offs = 0;
655 urb->dev = ctx->subs->dev;
656 for (i = 0; i < ctx->packets; ++i) {
657 counts = snd_usb_audio_next_packet_size(subs);
658 urb->iso_frame_desc[i].offset = offs * stride;
659 urb->iso_frame_desc[i].length = counts * stride;
660 offs += counts;
661 }
662 urb->number_of_packets = ctx->packets;
663 urb->transfer_buffer_length = offs * stride;
664 memset(urb->transfer_buffer,
665 subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
666 offs * stride);
667 return 0;
668}
669
670/*
671 * prepare urb for playback data pipe
672 *
673 * Since a URB can handle only a single linear buffer, we must use double
674 * buffering when the data to be transferred overflows the buffer boundary.
675 * To avoid inconsistencies when updating hwptr_done, we use double buffering
676 * for all URBs.
677 */
678static int prepare_playback_urb(struct snd_usb_substream *subs,
679 struct snd_pcm_runtime *runtime,
680 struct urb *urb)
681{
682 int i, stride;
683 unsigned int counts, frames, bytes;
684 unsigned long flags;
685 int period_elapsed = 0;
686 struct snd_urb_ctx *ctx = urb->context;
687
688 stride = runtime->frame_bits >> 3;
689
690 frames = 0;
691 urb->dev = ctx->subs->dev; /* we need to set this at each time */
692 urb->number_of_packets = 0;
693 spin_lock_irqsave(&subs->lock, flags);
694 for (i = 0; i < ctx->packets; i++) {
695 counts = snd_usb_audio_next_packet_size(subs);
696 /* set up descriptor */
697 urb->iso_frame_desc[i].offset = frames * stride;
698 urb->iso_frame_desc[i].length = counts * stride;
699 frames += counts;
700 urb->number_of_packets++;
701 subs->transfer_done += counts;
702 if (subs->transfer_done >= runtime->period_size) {
703 subs->transfer_done -= runtime->period_size;
704 period_elapsed = 1;
705 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
706 if (subs->transfer_done > 0) {
707 /* FIXME: fill-max mode is not
708 * supported yet */
709 frames -= subs->transfer_done;
710 counts -= subs->transfer_done;
711 urb->iso_frame_desc[i].length =
712 counts * stride;
713 subs->transfer_done = 0;
714 }
715 i++;
716 if (i < ctx->packets) {
717 /* add a transfer delimiter */
718 urb->iso_frame_desc[i].offset =
719 frames * stride;
720 urb->iso_frame_desc[i].length = 0;
721 urb->number_of_packets++;
722 }
723 break;
724 }
725 }
726 if (period_elapsed) /* finish at the period boundary */
727 break;
728 }
729 bytes = frames * stride;
730 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
731 /* err, the transferred area goes over buffer boundary. */
732 unsigned int bytes1 =
733 runtime->buffer_size * stride - subs->hwptr_done;
734 memcpy(urb->transfer_buffer,
735 runtime->dma_area + subs->hwptr_done, bytes1);
736 memcpy(urb->transfer_buffer + bytes1,
737 runtime->dma_area, bytes - bytes1);
738 } else {
739 memcpy(urb->transfer_buffer,
740 runtime->dma_area + subs->hwptr_done, bytes);
741 }
742 subs->hwptr_done += bytes;
743 if (subs->hwptr_done >= runtime->buffer_size * stride)
744 subs->hwptr_done -= runtime->buffer_size * stride;
745 runtime->delay += frames;
746 spin_unlock_irqrestore(&subs->lock, flags);
747 urb->transfer_buffer_length = bytes;
748 if (period_elapsed)
749 snd_pcm_period_elapsed(subs->pcm_substream);
750 return 0;
751}
752
753/*
754 * process after playback data complete
755 * - decrease the delay count again
756 */
757static int retire_playback_urb(struct snd_usb_substream *subs,
758 struct snd_pcm_runtime *runtime,
759 struct urb *urb)
760{
761 unsigned long flags;
762 int stride = runtime->frame_bits >> 3;
763 int processed = urb->transfer_buffer_length / stride;
764
765 spin_lock_irqsave(&subs->lock, flags);
766 if (processed > runtime->delay)
767 runtime->delay = 0;
768 else
769 runtime->delay -= processed;
770 spin_unlock_irqrestore(&subs->lock, flags);
771 return 0;
772}
773
774static const char *usb_error_string(int err)
775{
776 switch (err) {
777 case -ENODEV:
778 return "no device";
779 case -ENOENT:
780 return "endpoint not enabled";
781 case -EPIPE:
782 return "endpoint stalled";
783 case -ENOSPC:
784 return "not enough bandwidth";
785 case -ESHUTDOWN:
786 return "device disabled";
787 case -EHOSTUNREACH:
788 return "device suspended";
789 case -EINVAL:
790 case -EAGAIN:
791 case -EFBIG:
792 case -EMSGSIZE:
793 return "internal error";
794 default:
795 return "unknown error";
796 }
797}
798
799/*
800 * set up and start data/sync urbs
801 */
802static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
803{
804 unsigned int i;
805 int err;
806
807 if (subs->stream->chip->shutdown)
808 return -EBADFD;
809
810 for (i = 0; i < subs->nurbs; i++) {
811 if (snd_BUG_ON(!subs->dataurb[i].urb))
812 return -EINVAL;
813 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
814 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
815 goto __error;
816 }
817 }
818 if (subs->syncpipe) {
819 for (i = 0; i < SYNC_URBS; i++) {
820 if (snd_BUG_ON(!subs->syncurb[i].urb))
821 return -EINVAL;
822 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
823 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
824 goto __error;
825 }
826 }
827 }
828
829 subs->active_mask = 0;
830 subs->unlink_mask = 0;
831 subs->running = 1;
832 for (i = 0; i < subs->nurbs; i++) {
833 err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
834 if (err < 0) {
835 snd_printk(KERN_ERR "cannot submit datapipe "
836 "for urb %d, error %d: %s\n",
837 i, err, usb_error_string(err));
838 goto __error;
839 }
840 set_bit(i, &subs->active_mask);
841 }
842 if (subs->syncpipe) {
843 for (i = 0; i < SYNC_URBS; i++) {
844 err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
845 if (err < 0) {
846 snd_printk(KERN_ERR "cannot submit syncpipe "
847 "for urb %d, error %d: %s\n",
848 i, err, usb_error_string(err));
849 goto __error;
850 }
851 set_bit(i + 16, &subs->active_mask);
852 }
853 }
854 return 0;
855
856 __error:
857 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
858 deactivate_urbs(subs, 0, 0);
859 return -EPIPE;
860}
861
862
863/*
864 */
865static struct snd_urb_ops audio_urb_ops[2] = {
866 {
867 .prepare = prepare_nodata_playback_urb,
868 .retire = retire_playback_urb,
869 .prepare_sync = prepare_playback_sync_urb,
870 .retire_sync = retire_playback_sync_urb,
871 },
872 {
873 .prepare = prepare_capture_urb,
874 .retire = retire_capture_urb,
875 .prepare_sync = prepare_capture_sync_urb,
876 .retire_sync = retire_capture_sync_urb,
877 },
878};
879
880static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
881 {
882 .prepare = prepare_nodata_playback_urb,
883 .retire = retire_playback_urb,
884 .prepare_sync = prepare_playback_sync_urb_hs,
885 .retire_sync = retire_playback_sync_urb_hs,
886 },
887 {
888 .prepare = prepare_capture_urb,
889 .retire = retire_capture_urb,
890 .prepare_sync = prepare_capture_sync_urb_hs,
891 .retire_sync = retire_capture_sync_urb,
892 },
893};
894
895/*
896 * initialize the substream instance.
897 */
898
899void snd_usb_init_substream(struct snd_usb_stream *as,
900 int stream, struct audioformat *fp)
901{
902 struct snd_usb_substream *subs = &as->substream[stream];
903
904 INIT_LIST_HEAD(&subs->fmt_list);
905 spin_lock_init(&subs->lock);
906
907 subs->stream = as;
908 subs->direction = stream;
909 subs->dev = as->chip->dev;
910 subs->txfr_quirk = as->chip->txfr_quirk;
911 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) {
912 subs->ops = audio_urb_ops[stream];
913 } else {
914 subs->ops = audio_urb_ops_high_speed[stream];
915 switch (as->chip->usb_id) {
916 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
917 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
918 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
919 subs->ops.retire_sync = retire_playback_sync_urb_hs_emu;
920 break;
921 }
922 }
923
924 snd_usb_set_pcm_ops(as->pcm, stream);
925
926 list_add_tail(&fp->list, &subs->fmt_list);
927 subs->formats |= 1ULL << fp->format;
928 subs->endpoint = fp->endpoint;
929 subs->num_formats++;
930 subs->fmt_type = fp->fmt_type;
931}
932
933int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd)
934{
935 struct snd_usb_substream *subs = substream->runtime->private_data;
936
937 switch (cmd) {
938 case SNDRV_PCM_TRIGGER_START:
939 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
940 subs->ops.prepare = prepare_playback_urb;
941 return 0;
942 case SNDRV_PCM_TRIGGER_STOP:
943 return deactivate_urbs(subs, 0, 0);
944 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
945 subs->ops.prepare = prepare_nodata_playback_urb;
946 return 0;
947 }
948
949 return -EINVAL;
950}
951
952int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)
953{
954 struct snd_usb_substream *subs = substream->runtime->private_data;
955
956 switch (cmd) {
957 case SNDRV_PCM_TRIGGER_START:
958 subs->ops.retire = retire_capture_urb;
959 return start_urbs(subs, substream->runtime);
960 case SNDRV_PCM_TRIGGER_STOP:
961 return deactivate_urbs(subs, 0, 0);
962 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
963 subs->ops.retire = retire_paused_capture_urb;
964 return 0;
965 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
966 subs->ops.retire = retire_capture_urb;
967 return 0;
968 }
969
970 return -EINVAL;
971}
972
973int snd_usb_substream_prepare(struct snd_usb_substream *subs,
974 struct snd_pcm_runtime *runtime)
975{
976 /* clear urbs (to be sure) */
977 deactivate_urbs(subs, 0, 1);
978 wait_clear_urbs(subs);
979
980 /* for playback, submit the URBs now; otherwise, the first hwptr_done
981 * updates for all URBs would happen at the same time when starting */
982 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
983 subs->ops.prepare = prepare_nodata_playback_urb;
984 return start_urbs(subs, runtime);
985 }
986
987 return 0;
988}
989
diff --git a/sound/usb/urb.h b/sound/usb/urb.h
new file mode 100644
index 000000000000..888da38079cf
--- /dev/null
+++ b/sound/usb/urb.h
@@ -0,0 +1,21 @@
1#ifndef __USBAUDIO_URB_H
2#define __USBAUDIO_URB_H
3
4void snd_usb_init_substream(struct snd_usb_stream *as,
5 int stream,
6 struct audioformat *fp);
7
8int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
9 unsigned int period_bytes,
10 unsigned int rate,
11 unsigned int frame_bits);
12
13void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force);
14
15int snd_usb_substream_prepare(struct snd_usb_substream *subs,
16 struct snd_pcm_runtime *runtime);
17
18int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd);
19int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd);
20
21#endif /* __USBAUDIO_URB_H */
diff --git a/sound/usb/usbaudio.c b/sound/usb/usbaudio.c
deleted file mode 100644
index 5b91aa02b40b..000000000000
--- a/sound/usb/usbaudio.c
+++ /dev/null
@@ -1,4051 +0,0 @@
1/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Main and PCM part
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 *
28 * NOTES:
29 *
30 * - async unlink should be used for avoiding the sleep inside lock.
31 * 2.4.22 usb-uhci seems buggy for async unlinking and results in
32 * oops. in such a cse, pass async_unlink=0 option.
33 * - the linked URBs would be preferred but not used so far because of
34 * the instability of unlinking.
35 * - type II is not supported properly. there is no device which supports
36 * this type *correctly*. SB extigy looks as if it supports, but it's
37 * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
38 */
39
40
41#include <linux/bitops.h>
42#include <linux/init.h>
43#include <linux/list.h>
44#include <linux/slab.h>
45#include <linux/string.h>
46#include <linux/usb.h>
47#include <linux/moduleparam.h>
48#include <linux/mutex.h>
49#include <linux/usb/audio.h>
50#include <linux/usb/ch9.h>
51
52#include <sound/core.h>
53#include <sound/info.h>
54#include <sound/pcm.h>
55#include <sound/pcm_params.h>
56#include <sound/initval.h>
57
58#include "usbaudio.h"
59#include "usbmidi.h"
60#include "usbmixer.h"
61
62MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
63MODULE_DESCRIPTION("USB Audio");
64MODULE_LICENSE("GPL");
65MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
66
67
68static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
69static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
70static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
71/* Vendor/product IDs for this card */
72static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
73static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
74static int nrpacks = 8; /* max. number of packets per urb */
75static int async_unlink = 1;
76static int device_setup[SNDRV_CARDS]; /* device parameter for this card*/
77static int ignore_ctl_error;
78
79module_param_array(index, int, NULL, 0444);
80MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
81module_param_array(id, charp, NULL, 0444);
82MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
83module_param_array(enable, bool, NULL, 0444);
84MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
85module_param_array(vid, int, NULL, 0444);
86MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
87module_param_array(pid, int, NULL, 0444);
88MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
89module_param(nrpacks, int, 0644);
90MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
91module_param(async_unlink, bool, 0444);
92MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
93module_param_array(device_setup, int, NULL, 0444);
94MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
95module_param(ignore_ctl_error, bool, 0444);
96MODULE_PARM_DESC(ignore_ctl_error,
97 "Ignore errors from USB controller for mixer interfaces.");
98
99/*
100 * debug the h/w constraints
101 */
102/* #define HW_CONST_DEBUG */
103
104
105/*
106 *
107 */
108
109#define MAX_PACKS 20
110#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
111#define MAX_URBS 8
112#define SYNC_URBS 4 /* always four urbs for sync */
113#define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */
114
115struct audioformat {
116 struct list_head list;
117 snd_pcm_format_t format; /* format type */
118 unsigned int channels; /* # channels */
119 unsigned int fmt_type; /* USB audio format type (1-3) */
120 unsigned int frame_size; /* samples per frame for non-audio */
121 int iface; /* interface number */
122 unsigned char altsetting; /* corresponding alternate setting */
123 unsigned char altset_idx; /* array index of altenate setting */
124 unsigned char attributes; /* corresponding attributes of cs endpoint */
125 unsigned char endpoint; /* endpoint */
126 unsigned char ep_attr; /* endpoint attributes */
127 unsigned char datainterval; /* log_2 of data packet interval */
128 unsigned int maxpacksize; /* max. packet size */
129 unsigned int rates; /* rate bitmasks */
130 unsigned int rate_min, rate_max; /* min/max rates */
131 unsigned int nr_rates; /* number of rate table entries */
132 unsigned int *rate_table; /* rate table */
133};
134
135struct snd_usb_substream;
136
137struct snd_urb_ctx {
138 struct urb *urb;
139 unsigned int buffer_size; /* size of data buffer, if data URB */
140 struct snd_usb_substream *subs;
141 int index; /* index for urb array */
142 int packets; /* number of packets per urb */
143};
144
145struct snd_urb_ops {
146 int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
147 int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
148 int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
149 int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
150};
151
152struct snd_usb_substream {
153 struct snd_usb_stream *stream;
154 struct usb_device *dev;
155 struct snd_pcm_substream *pcm_substream;
156 int direction; /* playback or capture */
157 int interface; /* current interface */
158 int endpoint; /* assigned endpoint */
159 struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
160 unsigned int cur_rate; /* current rate (for hw_params callback) */
161 unsigned int period_bytes; /* current period bytes (for hw_params callback) */
162 unsigned int format; /* USB data format */
163 unsigned int datapipe; /* the data i/o pipe */
164 unsigned int syncpipe; /* 1 - async out or adaptive in */
165 unsigned int datainterval; /* log_2 of data packet interval */
166 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
167 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
168 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
169 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
170 unsigned int phase; /* phase accumulator */
171 unsigned int maxpacksize; /* max packet size in bytes */
172 unsigned int maxframesize; /* max packet size in frames */
173 unsigned int curpacksize; /* current packet size in bytes (for capture) */
174 unsigned int curframesize; /* current packet size in frames (for capture) */
175 unsigned int fill_max: 1; /* fill max packet size always */
176 unsigned int txfr_quirk:1; /* allow sub-frame alignment */
177 unsigned int fmt_type; /* USB audio format type (1-3) */
178
179 unsigned int running: 1; /* running status */
180
181 unsigned int hwptr_done; /* processed byte position in the buffer */
182 unsigned int transfer_done; /* processed frames since last period update */
183 unsigned long active_mask; /* bitmask of active urbs */
184 unsigned long unlink_mask; /* bitmask of unlinked urbs */
185
186 unsigned int nurbs; /* # urbs */
187 struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */
188 struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */
189 char *syncbuf; /* sync buffer for all sync URBs */
190 dma_addr_t sync_dma; /* DMA address of syncbuf */
191
192 u64 formats; /* format bitmasks (all or'ed) */
193 unsigned int num_formats; /* number of supported audio formats (list) */
194 struct list_head fmt_list; /* format list */
195 struct snd_pcm_hw_constraint_list rate_list; /* limited rates */
196 spinlock_t lock;
197
198 struct snd_urb_ops ops; /* callbacks (must be filled at init) */
199};
200
201
202struct snd_usb_stream {
203 struct snd_usb_audio *chip;
204 struct snd_pcm *pcm;
205 int pcm_index;
206 unsigned int fmt_type; /* USB audio format type (1-3) */
207 struct snd_usb_substream substream[2];
208 struct list_head list;
209};
210
211
212/*
213 * we keep the snd_usb_audio_t instances by ourselves for merging
214 * the all interfaces on the same card as one sound device.
215 */
216
217static DEFINE_MUTEX(register_mutex);
218static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
219
220
221/*
222 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
223 * this will overflow at approx 524 kHz
224 */
225static inline unsigned get_usb_full_speed_rate(unsigned int rate)
226{
227 return ((rate << 13) + 62) / 125;
228}
229
230/*
231 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
232 * this will overflow at approx 4 MHz
233 */
234static inline unsigned get_usb_high_speed_rate(unsigned int rate)
235{
236 return ((rate << 10) + 62) / 125;
237}
238
239/* convert our full speed USB rate into sampling rate in Hz */
240static inline unsigned get_full_speed_hz(unsigned int usb_rate)
241{
242 return (usb_rate * 125 + (1 << 12)) >> 13;
243}
244
245/* convert our high speed USB rate into sampling rate in Hz */
246static inline unsigned get_high_speed_hz(unsigned int usb_rate)
247{
248 return (usb_rate * 125 + (1 << 9)) >> 10;
249}
250
251
252/*
253 * prepare urb for full speed capture sync pipe
254 *
255 * fill the length and offset of each urb descriptor.
256 * the fixed 10.14 frequency is passed through the pipe.
257 */
258static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
259 struct snd_pcm_runtime *runtime,
260 struct urb *urb)
261{
262 unsigned char *cp = urb->transfer_buffer;
263 struct snd_urb_ctx *ctx = urb->context;
264
265 urb->dev = ctx->subs->dev; /* we need to set this at each time */
266 urb->iso_frame_desc[0].length = 3;
267 urb->iso_frame_desc[0].offset = 0;
268 cp[0] = subs->freqn >> 2;
269 cp[1] = subs->freqn >> 10;
270 cp[2] = subs->freqn >> 18;
271 return 0;
272}
273
274/*
275 * prepare urb for high speed capture sync pipe
276 *
277 * fill the length and offset of each urb descriptor.
278 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
279 */
280static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
281 struct snd_pcm_runtime *runtime,
282 struct urb *urb)
283{
284 unsigned char *cp = urb->transfer_buffer;
285 struct snd_urb_ctx *ctx = urb->context;
286
287 urb->dev = ctx->subs->dev; /* we need to set this at each time */
288 urb->iso_frame_desc[0].length = 4;
289 urb->iso_frame_desc[0].offset = 0;
290 cp[0] = subs->freqn;
291 cp[1] = subs->freqn >> 8;
292 cp[2] = subs->freqn >> 16;
293 cp[3] = subs->freqn >> 24;
294 return 0;
295}
296
297/*
298 * process after capture sync complete
299 * - nothing to do
300 */
301static int retire_capture_sync_urb(struct snd_usb_substream *subs,
302 struct snd_pcm_runtime *runtime,
303 struct urb *urb)
304{
305 return 0;
306}
307
308/*
309 * prepare urb for capture data pipe
310 *
311 * fill the offset and length of each descriptor.
312 *
313 * we use a temporary buffer to write the captured data.
314 * since the length of written data is determined by host, we cannot
315 * write onto the pcm buffer directly... the data is thus copied
316 * later at complete callback to the global buffer.
317 */
318static int prepare_capture_urb(struct snd_usb_substream *subs,
319 struct snd_pcm_runtime *runtime,
320 struct urb *urb)
321{
322 int i, offs;
323 struct snd_urb_ctx *ctx = urb->context;
324
325 offs = 0;
326 urb->dev = ctx->subs->dev; /* we need to set this at each time */
327 for (i = 0; i < ctx->packets; i++) {
328 urb->iso_frame_desc[i].offset = offs;
329 urb->iso_frame_desc[i].length = subs->curpacksize;
330 offs += subs->curpacksize;
331 }
332 urb->transfer_buffer_length = offs;
333 urb->number_of_packets = ctx->packets;
334 return 0;
335}
336
337/*
338 * process after capture complete
339 *
340 * copy the data from each desctiptor to the pcm buffer, and
341 * update the current position.
342 */
343static int retire_capture_urb(struct snd_usb_substream *subs,
344 struct snd_pcm_runtime *runtime,
345 struct urb *urb)
346{
347 unsigned long flags;
348 unsigned char *cp;
349 int i;
350 unsigned int stride, frames, bytes, oldptr;
351 int period_elapsed = 0;
352
353 stride = runtime->frame_bits >> 3;
354
355 for (i = 0; i < urb->number_of_packets; i++) {
356 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
357 if (urb->iso_frame_desc[i].status) {
358 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
359 // continue;
360 }
361 bytes = urb->iso_frame_desc[i].actual_length;
362 frames = bytes / stride;
363 if (!subs->txfr_quirk)
364 bytes = frames * stride;
365 if (bytes % (runtime->sample_bits >> 3) != 0) {
366#ifdef CONFIG_SND_DEBUG_VERBOSE
367 int oldbytes = bytes;
368#endif
369 bytes = frames * stride;
370 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
371 oldbytes, bytes);
372 }
373 /* update the current pointer */
374 spin_lock_irqsave(&subs->lock, flags);
375 oldptr = subs->hwptr_done;
376 subs->hwptr_done += bytes;
377 if (subs->hwptr_done >= runtime->buffer_size * stride)
378 subs->hwptr_done -= runtime->buffer_size * stride;
379 frames = (bytes + (oldptr % stride)) / stride;
380 subs->transfer_done += frames;
381 if (subs->transfer_done >= runtime->period_size) {
382 subs->transfer_done -= runtime->period_size;
383 period_elapsed = 1;
384 }
385 spin_unlock_irqrestore(&subs->lock, flags);
386 /* copy a data chunk */
387 if (oldptr + bytes > runtime->buffer_size * stride) {
388 unsigned int bytes1 =
389 runtime->buffer_size * stride - oldptr;
390 memcpy(runtime->dma_area + oldptr, cp, bytes1);
391 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
392 } else {
393 memcpy(runtime->dma_area + oldptr, cp, bytes);
394 }
395 }
396 if (period_elapsed)
397 snd_pcm_period_elapsed(subs->pcm_substream);
398 return 0;
399}
400
401/*
402 * Process after capture complete when paused. Nothing to do.
403 */
404static int retire_paused_capture_urb(struct snd_usb_substream *subs,
405 struct snd_pcm_runtime *runtime,
406 struct urb *urb)
407{
408 return 0;
409}
410
411
412/*
413 * prepare urb for full speed playback sync pipe
414 *
415 * set up the offset and length to receive the current frequency.
416 */
417
418static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
419 struct snd_pcm_runtime *runtime,
420 struct urb *urb)
421{
422 struct snd_urb_ctx *ctx = urb->context;
423
424 urb->dev = ctx->subs->dev; /* we need to set this at each time */
425 urb->iso_frame_desc[0].length = 3;
426 urb->iso_frame_desc[0].offset = 0;
427 return 0;
428}
429
430/*
431 * prepare urb for high speed playback sync pipe
432 *
433 * set up the offset and length to receive the current frequency.
434 */
435
436static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
437 struct snd_pcm_runtime *runtime,
438 struct urb *urb)
439{
440 struct snd_urb_ctx *ctx = urb->context;
441
442 urb->dev = ctx->subs->dev; /* we need to set this at each time */
443 urb->iso_frame_desc[0].length = 4;
444 urb->iso_frame_desc[0].offset = 0;
445 return 0;
446}
447
448/*
449 * process after full speed playback sync complete
450 *
451 * retrieve the current 10.14 frequency from pipe, and set it.
452 * the value is referred in prepare_playback_urb().
453 */
454static int retire_playback_sync_urb(struct snd_usb_substream *subs,
455 struct snd_pcm_runtime *runtime,
456 struct urb *urb)
457{
458 unsigned int f;
459 unsigned long flags;
460
461 if (urb->iso_frame_desc[0].status == 0 &&
462 urb->iso_frame_desc[0].actual_length == 3) {
463 f = combine_triple((u8*)urb->transfer_buffer) << 2;
464 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
465 spin_lock_irqsave(&subs->lock, flags);
466 subs->freqm = f;
467 spin_unlock_irqrestore(&subs->lock, flags);
468 }
469 }
470
471 return 0;
472}
473
474/*
475 * process after high speed playback sync complete
476 *
477 * retrieve the current 12.13 frequency from pipe, and set it.
478 * the value is referred in prepare_playback_urb().
479 */
480static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
481 struct snd_pcm_runtime *runtime,
482 struct urb *urb)
483{
484 unsigned int f;
485 unsigned long flags;
486
487 if (urb->iso_frame_desc[0].status == 0 &&
488 urb->iso_frame_desc[0].actual_length == 4) {
489 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
490 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
491 spin_lock_irqsave(&subs->lock, flags);
492 subs->freqm = f;
493 spin_unlock_irqrestore(&subs->lock, flags);
494 }
495 }
496
497 return 0;
498}
499
500/*
501 * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete
502 *
503 * These devices return the number of samples per packet instead of the number
504 * of samples per microframe.
505 */
506static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs,
507 struct snd_pcm_runtime *runtime,
508 struct urb *urb)
509{
510 unsigned int f;
511 unsigned long flags;
512
513 if (urb->iso_frame_desc[0].status == 0 &&
514 urb->iso_frame_desc[0].actual_length == 4) {
515 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
516 f >>= subs->datainterval;
517 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
518 spin_lock_irqsave(&subs->lock, flags);
519 subs->freqm = f;
520 spin_unlock_irqrestore(&subs->lock, flags);
521 }
522 }
523
524 return 0;
525}
526
527/* determine the number of frames in the next packet */
528static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
529{
530 if (subs->fill_max)
531 return subs->maxframesize;
532 else {
533 subs->phase = (subs->phase & 0xffff)
534 + (subs->freqm << subs->datainterval);
535 return min(subs->phase >> 16, subs->maxframesize);
536 }
537}
538
539/*
540 * Prepare urb for streaming before playback starts or when paused.
541 *
542 * We don't have any data, so we send silence.
543 */
544static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
545 struct snd_pcm_runtime *runtime,
546 struct urb *urb)
547{
548 unsigned int i, offs, counts;
549 struct snd_urb_ctx *ctx = urb->context;
550 int stride = runtime->frame_bits >> 3;
551
552 offs = 0;
553 urb->dev = ctx->subs->dev;
554 for (i = 0; i < ctx->packets; ++i) {
555 counts = snd_usb_audio_next_packet_size(subs);
556 urb->iso_frame_desc[i].offset = offs * stride;
557 urb->iso_frame_desc[i].length = counts * stride;
558 offs += counts;
559 }
560 urb->number_of_packets = ctx->packets;
561 urb->transfer_buffer_length = offs * stride;
562 memset(urb->transfer_buffer,
563 subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
564 offs * stride);
565 return 0;
566}
567
568/*
569 * prepare urb for playback data pipe
570 *
571 * Since a URB can handle only a single linear buffer, we must use double
572 * buffering when the data to be transferred overflows the buffer boundary.
573 * To avoid inconsistencies when updating hwptr_done, we use double buffering
574 * for all URBs.
575 */
576static int prepare_playback_urb(struct snd_usb_substream *subs,
577 struct snd_pcm_runtime *runtime,
578 struct urb *urb)
579{
580 int i, stride;
581 unsigned int counts, frames, bytes;
582 unsigned long flags;
583 int period_elapsed = 0;
584 struct snd_urb_ctx *ctx = urb->context;
585
586 stride = runtime->frame_bits >> 3;
587
588 frames = 0;
589 urb->dev = ctx->subs->dev; /* we need to set this at each time */
590 urb->number_of_packets = 0;
591 spin_lock_irqsave(&subs->lock, flags);
592 for (i = 0; i < ctx->packets; i++) {
593 counts = snd_usb_audio_next_packet_size(subs);
594 /* set up descriptor */
595 urb->iso_frame_desc[i].offset = frames * stride;
596 urb->iso_frame_desc[i].length = counts * stride;
597 frames += counts;
598 urb->number_of_packets++;
599 subs->transfer_done += counts;
600 if (subs->transfer_done >= runtime->period_size) {
601 subs->transfer_done -= runtime->period_size;
602 period_elapsed = 1;
603 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
604 if (subs->transfer_done > 0) {
605 /* FIXME: fill-max mode is not
606 * supported yet */
607 frames -= subs->transfer_done;
608 counts -= subs->transfer_done;
609 urb->iso_frame_desc[i].length =
610 counts * stride;
611 subs->transfer_done = 0;
612 }
613 i++;
614 if (i < ctx->packets) {
615 /* add a transfer delimiter */
616 urb->iso_frame_desc[i].offset =
617 frames * stride;
618 urb->iso_frame_desc[i].length = 0;
619 urb->number_of_packets++;
620 }
621 break;
622 }
623 }
624 if (period_elapsed) /* finish at the period boundary */
625 break;
626 }
627 bytes = frames * stride;
628 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
629 /* err, the transferred area goes over buffer boundary. */
630 unsigned int bytes1 =
631 runtime->buffer_size * stride - subs->hwptr_done;
632 memcpy(urb->transfer_buffer,
633 runtime->dma_area + subs->hwptr_done, bytes1);
634 memcpy(urb->transfer_buffer + bytes1,
635 runtime->dma_area, bytes - bytes1);
636 } else {
637 memcpy(urb->transfer_buffer,
638 runtime->dma_area + subs->hwptr_done, bytes);
639 }
640 subs->hwptr_done += bytes;
641 if (subs->hwptr_done >= runtime->buffer_size * stride)
642 subs->hwptr_done -= runtime->buffer_size * stride;
643 runtime->delay += frames;
644 spin_unlock_irqrestore(&subs->lock, flags);
645 urb->transfer_buffer_length = bytes;
646 if (period_elapsed)
647 snd_pcm_period_elapsed(subs->pcm_substream);
648 return 0;
649}
650
651/*
652 * process after playback data complete
653 * - decrease the delay count again
654 */
655static int retire_playback_urb(struct snd_usb_substream *subs,
656 struct snd_pcm_runtime *runtime,
657 struct urb *urb)
658{
659 unsigned long flags;
660 int stride = runtime->frame_bits >> 3;
661 int processed = urb->transfer_buffer_length / stride;
662
663 spin_lock_irqsave(&subs->lock, flags);
664 if (processed > runtime->delay)
665 runtime->delay = 0;
666 else
667 runtime->delay -= processed;
668 spin_unlock_irqrestore(&subs->lock, flags);
669 return 0;
670}
671
672
673/*
674 */
675static struct snd_urb_ops audio_urb_ops[2] = {
676 {
677 .prepare = prepare_nodata_playback_urb,
678 .retire = retire_playback_urb,
679 .prepare_sync = prepare_playback_sync_urb,
680 .retire_sync = retire_playback_sync_urb,
681 },
682 {
683 .prepare = prepare_capture_urb,
684 .retire = retire_capture_urb,
685 .prepare_sync = prepare_capture_sync_urb,
686 .retire_sync = retire_capture_sync_urb,
687 },
688};
689
690static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
691 {
692 .prepare = prepare_nodata_playback_urb,
693 .retire = retire_playback_urb,
694 .prepare_sync = prepare_playback_sync_urb_hs,
695 .retire_sync = retire_playback_sync_urb_hs,
696 },
697 {
698 .prepare = prepare_capture_urb,
699 .retire = retire_capture_urb,
700 .prepare_sync = prepare_capture_sync_urb_hs,
701 .retire_sync = retire_capture_sync_urb,
702 },
703};
704
705/*
706 * complete callback from data urb
707 */
708static void snd_complete_urb(struct urb *urb)
709{
710 struct snd_urb_ctx *ctx = urb->context;
711 struct snd_usb_substream *subs = ctx->subs;
712 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
713 int err = 0;
714
715 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
716 !subs->running || /* can be stopped during retire callback */
717 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
718 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
719 clear_bit(ctx->index, &subs->active_mask);
720 if (err < 0) {
721 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
722 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
723 }
724 }
725}
726
727
728/*
729 * complete callback from sync urb
730 */
731static void snd_complete_sync_urb(struct urb *urb)
732{
733 struct snd_urb_ctx *ctx = urb->context;
734 struct snd_usb_substream *subs = ctx->subs;
735 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
736 int err = 0;
737
738 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
739 !subs->running || /* can be stopped during retire callback */
740 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
741 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
742 clear_bit(ctx->index + 16, &subs->active_mask);
743 if (err < 0) {
744 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
745 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
746 }
747 }
748}
749
750
751/*
752 * unlink active urbs.
753 */
754static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
755{
756 unsigned int i;
757 int async;
758
759 subs->running = 0;
760
761 if (!force && subs->stream->chip->shutdown) /* to be sure... */
762 return -EBADFD;
763
764 async = !can_sleep && async_unlink;
765
766 if (!async && in_interrupt())
767 return 0;
768
769 for (i = 0; i < subs->nurbs; i++) {
770 if (test_bit(i, &subs->active_mask)) {
771 if (!test_and_set_bit(i, &subs->unlink_mask)) {
772 struct urb *u = subs->dataurb[i].urb;
773 if (async)
774 usb_unlink_urb(u);
775 else
776 usb_kill_urb(u);
777 }
778 }
779 }
780 if (subs->syncpipe) {
781 for (i = 0; i < SYNC_URBS; i++) {
782 if (test_bit(i+16, &subs->active_mask)) {
783 if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
784 struct urb *u = subs->syncurb[i].urb;
785 if (async)
786 usb_unlink_urb(u);
787 else
788 usb_kill_urb(u);
789 }
790 }
791 }
792 }
793 return 0;
794}
795
796
797static const char *usb_error_string(int err)
798{
799 switch (err) {
800 case -ENODEV:
801 return "no device";
802 case -ENOENT:
803 return "endpoint not enabled";
804 case -EPIPE:
805 return "endpoint stalled";
806 case -ENOSPC:
807 return "not enough bandwidth";
808 case -ESHUTDOWN:
809 return "device disabled";
810 case -EHOSTUNREACH:
811 return "device suspended";
812 case -EINVAL:
813 case -EAGAIN:
814 case -EFBIG:
815 case -EMSGSIZE:
816 return "internal error";
817 default:
818 return "unknown error";
819 }
820}
821
822/*
823 * set up and start data/sync urbs
824 */
825static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
826{
827 unsigned int i;
828 int err;
829
830 if (subs->stream->chip->shutdown)
831 return -EBADFD;
832
833 for (i = 0; i < subs->nurbs; i++) {
834 if (snd_BUG_ON(!subs->dataurb[i].urb))
835 return -EINVAL;
836 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
837 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
838 goto __error;
839 }
840 }
841 if (subs->syncpipe) {
842 for (i = 0; i < SYNC_URBS; i++) {
843 if (snd_BUG_ON(!subs->syncurb[i].urb))
844 return -EINVAL;
845 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
846 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
847 goto __error;
848 }
849 }
850 }
851
852 subs->active_mask = 0;
853 subs->unlink_mask = 0;
854 subs->running = 1;
855 for (i = 0; i < subs->nurbs; i++) {
856 err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
857 if (err < 0) {
858 snd_printk(KERN_ERR "cannot submit datapipe "
859 "for urb %d, error %d: %s\n",
860 i, err, usb_error_string(err));
861 goto __error;
862 }
863 set_bit(i, &subs->active_mask);
864 }
865 if (subs->syncpipe) {
866 for (i = 0; i < SYNC_URBS; i++) {
867 err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
868 if (err < 0) {
869 snd_printk(KERN_ERR "cannot submit syncpipe "
870 "for urb %d, error %d: %s\n",
871 i, err, usb_error_string(err));
872 goto __error;
873 }
874 set_bit(i + 16, &subs->active_mask);
875 }
876 }
877 return 0;
878
879 __error:
880 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
881 deactivate_urbs(subs, 0, 0);
882 return -EPIPE;
883}
884
885
886/*
887 * wait until all urbs are processed.
888 */
889static int wait_clear_urbs(struct snd_usb_substream *subs)
890{
891 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
892 unsigned int i;
893 int alive;
894
895 do {
896 alive = 0;
897 for (i = 0; i < subs->nurbs; i++) {
898 if (test_bit(i, &subs->active_mask))
899 alive++;
900 }
901 if (subs->syncpipe) {
902 for (i = 0; i < SYNC_URBS; i++) {
903 if (test_bit(i + 16, &subs->active_mask))
904 alive++;
905 }
906 }
907 if (! alive)
908 break;
909 schedule_timeout_uninterruptible(1);
910 } while (time_before(jiffies, end_time));
911 if (alive)
912 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
913 return 0;
914}
915
916
917/*
918 * return the current pcm pointer. just based on the hwptr_done value.
919 */
920static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
921{
922 struct snd_usb_substream *subs;
923 unsigned int hwptr_done;
924
925 subs = (struct snd_usb_substream *)substream->runtime->private_data;
926 spin_lock(&subs->lock);
927 hwptr_done = subs->hwptr_done;
928 spin_unlock(&subs->lock);
929 return hwptr_done / (substream->runtime->frame_bits >> 3);
930}
931
932
933/*
934 * start/stop playback substream
935 */
936static int snd_usb_pcm_playback_trigger(struct snd_pcm_substream *substream,
937 int cmd)
938{
939 struct snd_usb_substream *subs = substream->runtime->private_data;
940
941 switch (cmd) {
942 case SNDRV_PCM_TRIGGER_START:
943 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
944 subs->ops.prepare = prepare_playback_urb;
945 return 0;
946 case SNDRV_PCM_TRIGGER_STOP:
947 return deactivate_urbs(subs, 0, 0);
948 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
949 subs->ops.prepare = prepare_nodata_playback_urb;
950 return 0;
951 default:
952 return -EINVAL;
953 }
954}
955
956/*
957 * start/stop capture substream
958 */
959static int snd_usb_pcm_capture_trigger(struct snd_pcm_substream *substream,
960 int cmd)
961{
962 struct snd_usb_substream *subs = substream->runtime->private_data;
963
964 switch (cmd) {
965 case SNDRV_PCM_TRIGGER_START:
966 subs->ops.retire = retire_capture_urb;
967 return start_urbs(subs, substream->runtime);
968 case SNDRV_PCM_TRIGGER_STOP:
969 return deactivate_urbs(subs, 0, 0);
970 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
971 subs->ops.retire = retire_paused_capture_urb;
972 return 0;
973 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
974 subs->ops.retire = retire_capture_urb;
975 return 0;
976 default:
977 return -EINVAL;
978 }
979}
980
981
982/*
983 * release a urb data
984 */
985static void release_urb_ctx(struct snd_urb_ctx *u)
986{
987 if (u->urb) {
988 if (u->buffer_size)
989 usb_buffer_free(u->subs->dev, u->buffer_size,
990 u->urb->transfer_buffer,
991 u->urb->transfer_dma);
992 usb_free_urb(u->urb);
993 u->urb = NULL;
994 }
995}
996
997/*
998 * release a substream
999 */
1000static void release_substream_urbs(struct snd_usb_substream *subs, int force)
1001{
1002 int i;
1003
1004 /* stop urbs (to be sure) */
1005 deactivate_urbs(subs, force, 1);
1006 wait_clear_urbs(subs);
1007
1008 for (i = 0; i < MAX_URBS; i++)
1009 release_urb_ctx(&subs->dataurb[i]);
1010 for (i = 0; i < SYNC_URBS; i++)
1011 release_urb_ctx(&subs->syncurb[i]);
1012 usb_buffer_free(subs->dev, SYNC_URBS * 4,
1013 subs->syncbuf, subs->sync_dma);
1014 subs->syncbuf = NULL;
1015 subs->nurbs = 0;
1016}
1017
1018/*
1019 * initialize a substream for plaback/capture
1020 */
1021static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes,
1022 unsigned int rate, unsigned int frame_bits)
1023{
1024 unsigned int maxsize, i;
1025 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1026 unsigned int urb_packs, total_packs, packs_per_ms;
1027
1028 /* calculate the frequency in 16.16 format */
1029 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
1030 subs->freqn = get_usb_full_speed_rate(rate);
1031 else
1032 subs->freqn = get_usb_high_speed_rate(rate);
1033 subs->freqm = subs->freqn;
1034 /* calculate max. frequency */
1035 if (subs->maxpacksize) {
1036 /* whatever fits into a max. size packet */
1037 maxsize = subs->maxpacksize;
1038 subs->freqmax = (maxsize / (frame_bits >> 3))
1039 << (16 - subs->datainterval);
1040 } else {
1041 /* no max. packet size: just take 25% higher than nominal */
1042 subs->freqmax = subs->freqn + (subs->freqn >> 2);
1043 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
1044 >> (16 - subs->datainterval);
1045 }
1046 subs->phase = 0;
1047
1048 if (subs->fill_max)
1049 subs->curpacksize = subs->maxpacksize;
1050 else
1051 subs->curpacksize = maxsize;
1052
1053 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
1054 packs_per_ms = 8 >> subs->datainterval;
1055 else
1056 packs_per_ms = 1;
1057
1058 if (is_playback) {
1059 urb_packs = max(nrpacks, 1);
1060 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
1061 } else
1062 urb_packs = 1;
1063 urb_packs *= packs_per_ms;
1064 if (subs->syncpipe)
1065 urb_packs = min(urb_packs, 1U << subs->syncinterval);
1066
1067 /* decide how many packets to be used */
1068 if (is_playback) {
1069 unsigned int minsize, maxpacks;
1070 /* determine how small a packet can be */
1071 minsize = (subs->freqn >> (16 - subs->datainterval))
1072 * (frame_bits >> 3);
1073 /* with sync from device, assume it can be 12% lower */
1074 if (subs->syncpipe)
1075 minsize -= minsize >> 3;
1076 minsize = max(minsize, 1u);
1077 total_packs = (period_bytes + minsize - 1) / minsize;
1078 /* we need at least two URBs for queueing */
1079 if (total_packs < 2) {
1080 total_packs = 2;
1081 } else {
1082 /* and we don't want too long a queue either */
1083 maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
1084 total_packs = min(total_packs, maxpacks);
1085 }
1086 } else {
1087 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
1088 urb_packs >>= 1;
1089 total_packs = MAX_URBS * urb_packs;
1090 }
1091 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
1092 if (subs->nurbs > MAX_URBS) {
1093 /* too much... */
1094 subs->nurbs = MAX_URBS;
1095 total_packs = MAX_URBS * urb_packs;
1096 } else if (subs->nurbs < 2) {
1097 /* too little - we need at least two packets
1098 * to ensure contiguous playback/capture
1099 */
1100 subs->nurbs = 2;
1101 }
1102
1103 /* allocate and initialize data urbs */
1104 for (i = 0; i < subs->nurbs; i++) {
1105 struct snd_urb_ctx *u = &subs->dataurb[i];
1106 u->index = i;
1107 u->subs = subs;
1108 u->packets = (i + 1) * total_packs / subs->nurbs
1109 - i * total_packs / subs->nurbs;
1110 u->buffer_size = maxsize * u->packets;
1111 if (subs->fmt_type == UAC_FORMAT_TYPE_II)
1112 u->packets++; /* for transfer delimiter */
1113 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1114 if (!u->urb)
1115 goto out_of_memory;
1116 u->urb->transfer_buffer =
1117 usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL,
1118 &u->urb->transfer_dma);
1119 if (!u->urb->transfer_buffer)
1120 goto out_of_memory;
1121 u->urb->pipe = subs->datapipe;
1122 u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1123 u->urb->interval = 1 << subs->datainterval;
1124 u->urb->context = u;
1125 u->urb->complete = snd_complete_urb;
1126 }
1127
1128 if (subs->syncpipe) {
1129 /* allocate and initialize sync urbs */
1130 subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4,
1131 GFP_KERNEL, &subs->sync_dma);
1132 if (!subs->syncbuf)
1133 goto out_of_memory;
1134 for (i = 0; i < SYNC_URBS; i++) {
1135 struct snd_urb_ctx *u = &subs->syncurb[i];
1136 u->index = i;
1137 u->subs = subs;
1138 u->packets = 1;
1139 u->urb = usb_alloc_urb(1, GFP_KERNEL);
1140 if (!u->urb)
1141 goto out_of_memory;
1142 u->urb->transfer_buffer = subs->syncbuf + i * 4;
1143 u->urb->transfer_dma = subs->sync_dma + i * 4;
1144 u->urb->transfer_buffer_length = 4;
1145 u->urb->pipe = subs->syncpipe;
1146 u->urb->transfer_flags = URB_ISO_ASAP |
1147 URB_NO_TRANSFER_DMA_MAP;
1148 u->urb->number_of_packets = 1;
1149 u->urb->interval = 1 << subs->syncinterval;
1150 u->urb->context = u;
1151 u->urb->complete = snd_complete_sync_urb;
1152 }
1153 }
1154 return 0;
1155
1156out_of_memory:
1157 release_substream_urbs(subs, 0);
1158 return -ENOMEM;
1159}
1160
1161
1162/*
1163 * find a matching audio format
1164 */
1165static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
1166 unsigned int rate, unsigned int channels)
1167{
1168 struct list_head *p;
1169 struct audioformat *found = NULL;
1170 int cur_attr = 0, attr;
1171
1172 list_for_each(p, &subs->fmt_list) {
1173 struct audioformat *fp;
1174 fp = list_entry(p, struct audioformat, list);
1175 if (fp->format != format || fp->channels != channels)
1176 continue;
1177 if (rate < fp->rate_min || rate > fp->rate_max)
1178 continue;
1179 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
1180 unsigned int i;
1181 for (i = 0; i < fp->nr_rates; i++)
1182 if (fp->rate_table[i] == rate)
1183 break;
1184 if (i >= fp->nr_rates)
1185 continue;
1186 }
1187 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
1188 if (! found) {
1189 found = fp;
1190 cur_attr = attr;
1191 continue;
1192 }
1193 /* avoid async out and adaptive in if the other method
1194 * supports the same format.
1195 * this is a workaround for the case like
1196 * M-audio audiophile USB.
1197 */
1198 if (attr != cur_attr) {
1199 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
1200 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1201 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
1202 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
1203 continue;
1204 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
1205 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1206 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
1207 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
1208 found = fp;
1209 cur_attr = attr;
1210 continue;
1211 }
1212 }
1213 /* find the format with the largest max. packet size */
1214 if (fp->maxpacksize > found->maxpacksize) {
1215 found = fp;
1216 cur_attr = attr;
1217 }
1218 }
1219 return found;
1220}
1221
1222
1223/*
1224 * initialize the picth control and sample rate
1225 */
1226static int init_usb_pitch(struct usb_device *dev, int iface,
1227 struct usb_host_interface *alts,
1228 struct audioformat *fmt)
1229{
1230 unsigned int ep;
1231 unsigned char data[1];
1232 int err;
1233
1234 ep = get_endpoint(alts, 0)->bEndpointAddress;
1235 /* if endpoint has pitch control, enable it */
1236 if (fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL) {
1237 data[0] = 1;
1238 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
1239 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1240 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1241 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
1242 dev->devnum, iface, ep);
1243 return err;
1244 }
1245 }
1246 return 0;
1247}
1248
1249static int init_usb_sample_rate(struct usb_device *dev, int iface,
1250 struct usb_host_interface *alts,
1251 struct audioformat *fmt, int rate)
1252{
1253 unsigned int ep;
1254 unsigned char data[3];
1255 int err;
1256
1257 ep = get_endpoint(alts, 0)->bEndpointAddress;
1258 /* if endpoint has sampling rate control, set it */
1259 if (fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE) {
1260 int crate;
1261 data[0] = rate;
1262 data[1] = rate >> 8;
1263 data[2] = rate >> 16;
1264 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
1265 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1266 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) {
1267 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
1268 dev->devnum, iface, fmt->altsetting, rate, ep);
1269 return err;
1270 }
1271 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
1272 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1273 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) {
1274 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
1275 dev->devnum, iface, fmt->altsetting, ep);
1276 return 0; /* some devices don't support reading */
1277 }
1278 crate = data[0] | (data[1] << 8) | (data[2] << 16);
1279 if (crate != rate) {
1280 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
1281 // runtime->rate = crate;
1282 }
1283 }
1284 return 0;
1285}
1286
1287/*
1288 * For E-Mu 0404USB/0202USB/TrackerPre sample rate should be set for device,
1289 * not for interface.
1290 */
1291static void set_format_emu_quirk(struct snd_usb_substream *subs,
1292 struct audioformat *fmt)
1293{
1294 unsigned char emu_samplerate_id = 0;
1295
1296 /* When capture is active
1297 * sample rate shouldn't be changed
1298 * by playback substream
1299 */
1300 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1301 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
1302 return;
1303 }
1304
1305 switch (fmt->rate_min) {
1306 case 48000:
1307 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1308 break;
1309 case 88200:
1310 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1311 break;
1312 case 96000:
1313 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1314 break;
1315 case 176400:
1316 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1317 break;
1318 case 192000:
1319 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1320 break;
1321 default:
1322 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1323 break;
1324 }
1325 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
1326}
1327
1328/*
1329 * find a matching format and set up the interface
1330 */
1331static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
1332{
1333 struct usb_device *dev = subs->dev;
1334 struct usb_host_interface *alts;
1335 struct usb_interface_descriptor *altsd;
1336 struct usb_interface *iface;
1337 unsigned int ep, attr;
1338 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1339 int err;
1340
1341 iface = usb_ifnum_to_if(dev, fmt->iface);
1342 if (WARN_ON(!iface))
1343 return -EINVAL;
1344 alts = &iface->altsetting[fmt->altset_idx];
1345 altsd = get_iface_desc(alts);
1346 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
1347 return -EINVAL;
1348
1349 if (fmt == subs->cur_audiofmt)
1350 return 0;
1351
1352 /* close the old interface */
1353 if (subs->interface >= 0 && subs->interface != fmt->iface) {
1354 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
1355 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
1356 dev->devnum, fmt->iface, fmt->altsetting);
1357 return -EIO;
1358 }
1359 subs->interface = -1;
1360 subs->format = 0;
1361 }
1362
1363 /* set interface */
1364 if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
1365 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
1366 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
1367 dev->devnum, fmt->iface, fmt->altsetting);
1368 return -EIO;
1369 }
1370 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
1371 subs->interface = fmt->iface;
1372 subs->format = fmt->altset_idx;
1373 }
1374
1375 /* create a data pipe */
1376 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
1377 if (is_playback)
1378 subs->datapipe = usb_sndisocpipe(dev, ep);
1379 else
1380 subs->datapipe = usb_rcvisocpipe(dev, ep);
1381 subs->datainterval = fmt->datainterval;
1382 subs->syncpipe = subs->syncinterval = 0;
1383 subs->maxpacksize = fmt->maxpacksize;
1384 subs->fill_max = 0;
1385
1386 /* we need a sync pipe in async OUT or adaptive IN mode */
1387 /* check the number of EP, since some devices have broken
1388 * descriptors which fool us. if it has only one EP,
1389 * assume it as adaptive-out or sync-in.
1390 */
1391 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
1392 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
1393 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
1394 altsd->bNumEndpoints >= 2) {
1395 /* check sync-pipe endpoint */
1396 /* ... and check descriptor size before accessing bSynchAddress
1397 because there is a version of the SB Audigy 2 NX firmware lacking
1398 the audio fields in the endpoint descriptors */
1399 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
1400 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1401 get_endpoint(alts, 1)->bSynchAddress != 0)) {
1402 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1403 dev->devnum, fmt->iface, fmt->altsetting);
1404 return -EINVAL;
1405 }
1406 ep = get_endpoint(alts, 1)->bEndpointAddress;
1407 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1408 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
1409 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
1410 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1411 dev->devnum, fmt->iface, fmt->altsetting);
1412 return -EINVAL;
1413 }
1414 ep &= USB_ENDPOINT_NUMBER_MASK;
1415 if (is_playback)
1416 subs->syncpipe = usb_rcvisocpipe(dev, ep);
1417 else
1418 subs->syncpipe = usb_sndisocpipe(dev, ep);
1419 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1420 get_endpoint(alts, 1)->bRefresh >= 1 &&
1421 get_endpoint(alts, 1)->bRefresh <= 9)
1422 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
1423 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
1424 subs->syncinterval = 1;
1425 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
1426 get_endpoint(alts, 1)->bInterval <= 16)
1427 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
1428 else
1429 subs->syncinterval = 3;
1430 }
1431
1432 /* always fill max packet size */
1433 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
1434 subs->fill_max = 1;
1435
1436 if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
1437 return err;
1438
1439 subs->cur_audiofmt = fmt;
1440
1441 switch (subs->stream->chip->usb_id) {
1442 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1443 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1444 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
1445 set_format_emu_quirk(subs, fmt);
1446 break;
1447 }
1448
1449#if 0
1450 printk(KERN_DEBUG
1451 "setting done: format = %d, rate = %d..%d, channels = %d\n",
1452 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
1453 printk(KERN_DEBUG
1454 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
1455 subs->datapipe, subs->syncpipe);
1456#endif
1457
1458 return 0;
1459}
1460
1461/*
1462 * hw_params callback
1463 *
1464 * allocate a buffer and set the given audio format.
1465 *
1466 * so far we use a physically linear buffer although packetize transfer
1467 * doesn't need a continuous area.
1468 * if sg buffer is supported on the later version of alsa, we'll follow
1469 * that.
1470 */
1471static int snd_usb_hw_params(struct snd_pcm_substream *substream,
1472 struct snd_pcm_hw_params *hw_params)
1473{
1474 struct snd_usb_substream *subs = substream->runtime->private_data;
1475 struct audioformat *fmt;
1476 unsigned int channels, rate, format;
1477 int ret, changed;
1478
1479 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
1480 params_buffer_bytes(hw_params));
1481 if (ret < 0)
1482 return ret;
1483
1484 format = params_format(hw_params);
1485 rate = params_rate(hw_params);
1486 channels = params_channels(hw_params);
1487 fmt = find_format(subs, format, rate, channels);
1488 if (!fmt) {
1489 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
1490 format, rate, channels);
1491 return -EINVAL;
1492 }
1493
1494 changed = subs->cur_audiofmt != fmt ||
1495 subs->period_bytes != params_period_bytes(hw_params) ||
1496 subs->cur_rate != rate;
1497 if ((ret = set_format(subs, fmt)) < 0)
1498 return ret;
1499
1500 if (subs->cur_rate != rate) {
1501 struct usb_host_interface *alts;
1502 struct usb_interface *iface;
1503 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
1504 alts = &iface->altsetting[fmt->altset_idx];
1505 ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
1506 if (ret < 0)
1507 return ret;
1508 subs->cur_rate = rate;
1509 }
1510
1511 if (changed) {
1512 /* format changed */
1513 release_substream_urbs(subs, 0);
1514 /* influenced: period_bytes, channels, rate, format, */
1515 ret = init_substream_urbs(subs, params_period_bytes(hw_params),
1516 params_rate(hw_params),
1517 snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
1518 }
1519
1520 return ret;
1521}
1522
1523/*
1524 * hw_free callback
1525 *
1526 * reset the audio format and release the buffer
1527 */
1528static int snd_usb_hw_free(struct snd_pcm_substream *substream)
1529{
1530 struct snd_usb_substream *subs = substream->runtime->private_data;
1531
1532 subs->cur_audiofmt = NULL;
1533 subs->cur_rate = 0;
1534 subs->period_bytes = 0;
1535 if (!subs->stream->chip->shutdown)
1536 release_substream_urbs(subs, 0);
1537 return snd_pcm_lib_free_vmalloc_buffer(substream);
1538}
1539
1540/*
1541 * prepare callback
1542 *
1543 * only a few subtle things...
1544 */
1545static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
1546{
1547 struct snd_pcm_runtime *runtime = substream->runtime;
1548 struct snd_usb_substream *subs = runtime->private_data;
1549
1550 if (! subs->cur_audiofmt) {
1551 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
1552 return -ENXIO;
1553 }
1554
1555 /* some unit conversions in runtime */
1556 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
1557 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
1558
1559 /* reset the pointer */
1560 subs->hwptr_done = 0;
1561 subs->transfer_done = 0;
1562 subs->phase = 0;
1563 runtime->delay = 0;
1564
1565 /* clear urbs (to be sure) */
1566 deactivate_urbs(subs, 0, 1);
1567 wait_clear_urbs(subs);
1568
1569 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1570 * updates for all URBs would happen at the same time when starting */
1571 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1572 subs->ops.prepare = prepare_nodata_playback_urb;
1573 return start_urbs(subs, runtime);
1574 } else
1575 return 0;
1576}
1577
1578static struct snd_pcm_hardware snd_usb_hardware =
1579{
1580 .info = SNDRV_PCM_INFO_MMAP |
1581 SNDRV_PCM_INFO_MMAP_VALID |
1582 SNDRV_PCM_INFO_BATCH |
1583 SNDRV_PCM_INFO_INTERLEAVED |
1584 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1585 SNDRV_PCM_INFO_PAUSE,
1586 .buffer_bytes_max = 1024 * 1024,
1587 .period_bytes_min = 64,
1588 .period_bytes_max = 512 * 1024,
1589 .periods_min = 2,
1590 .periods_max = 1024,
1591};
1592
1593/*
1594 * h/w constraints
1595 */
1596
1597#ifdef HW_CONST_DEBUG
1598#define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
1599#else
1600#define hwc_debug(fmt, args...) /**/
1601#endif
1602
1603static int hw_check_valid_format(struct snd_usb_substream *subs,
1604 struct snd_pcm_hw_params *params,
1605 struct audioformat *fp)
1606{
1607 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1608 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1609 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1610 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1611 unsigned int ptime;
1612
1613 /* check the format */
1614 if (!snd_mask_test(fmts, fp->format)) {
1615 hwc_debug(" > check: no supported format %d\n", fp->format);
1616 return 0;
1617 }
1618 /* check the channels */
1619 if (fp->channels < ct->min || fp->channels > ct->max) {
1620 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1621 return 0;
1622 }
1623 /* check the rate is within the range */
1624 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1625 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1626 return 0;
1627 }
1628 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1629 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1630 return 0;
1631 }
1632 /* check whether the period time is >= the data packet interval */
1633 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
1634 ptime = 125 * (1 << fp->datainterval);
1635 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
1636 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
1637 return 0;
1638 }
1639 }
1640 return 1;
1641}
1642
1643static int hw_rule_rate(struct snd_pcm_hw_params *params,
1644 struct snd_pcm_hw_rule *rule)
1645{
1646 struct snd_usb_substream *subs = rule->private;
1647 struct list_head *p;
1648 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1649 unsigned int rmin, rmax;
1650 int changed;
1651
1652 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1653 changed = 0;
1654 rmin = rmax = 0;
1655 list_for_each(p, &subs->fmt_list) {
1656 struct audioformat *fp;
1657 fp = list_entry(p, struct audioformat, list);
1658 if (!hw_check_valid_format(subs, params, fp))
1659 continue;
1660 if (changed++) {
1661 if (rmin > fp->rate_min)
1662 rmin = fp->rate_min;
1663 if (rmax < fp->rate_max)
1664 rmax = fp->rate_max;
1665 } else {
1666 rmin = fp->rate_min;
1667 rmax = fp->rate_max;
1668 }
1669 }
1670
1671 if (!changed) {
1672 hwc_debug(" --> get empty\n");
1673 it->empty = 1;
1674 return -EINVAL;
1675 }
1676
1677 changed = 0;
1678 if (it->min < rmin) {
1679 it->min = rmin;
1680 it->openmin = 0;
1681 changed = 1;
1682 }
1683 if (it->max > rmax) {
1684 it->max = rmax;
1685 it->openmax = 0;
1686 changed = 1;
1687 }
1688 if (snd_interval_checkempty(it)) {
1689 it->empty = 1;
1690 return -EINVAL;
1691 }
1692 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1693 return changed;
1694}
1695
1696
1697static int hw_rule_channels(struct snd_pcm_hw_params *params,
1698 struct snd_pcm_hw_rule *rule)
1699{
1700 struct snd_usb_substream *subs = rule->private;
1701 struct list_head *p;
1702 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1703 unsigned int rmin, rmax;
1704 int changed;
1705
1706 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1707 changed = 0;
1708 rmin = rmax = 0;
1709 list_for_each(p, &subs->fmt_list) {
1710 struct audioformat *fp;
1711 fp = list_entry(p, struct audioformat, list);
1712 if (!hw_check_valid_format(subs, params, fp))
1713 continue;
1714 if (changed++) {
1715 if (rmin > fp->channels)
1716 rmin = fp->channels;
1717 if (rmax < fp->channels)
1718 rmax = fp->channels;
1719 } else {
1720 rmin = fp->channels;
1721 rmax = fp->channels;
1722 }
1723 }
1724
1725 if (!changed) {
1726 hwc_debug(" --> get empty\n");
1727 it->empty = 1;
1728 return -EINVAL;
1729 }
1730
1731 changed = 0;
1732 if (it->min < rmin) {
1733 it->min = rmin;
1734 it->openmin = 0;
1735 changed = 1;
1736 }
1737 if (it->max > rmax) {
1738 it->max = rmax;
1739 it->openmax = 0;
1740 changed = 1;
1741 }
1742 if (snd_interval_checkempty(it)) {
1743 it->empty = 1;
1744 return -EINVAL;
1745 }
1746 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1747 return changed;
1748}
1749
1750static int hw_rule_format(struct snd_pcm_hw_params *params,
1751 struct snd_pcm_hw_rule *rule)
1752{
1753 struct snd_usb_substream *subs = rule->private;
1754 struct list_head *p;
1755 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1756 u64 fbits;
1757 u32 oldbits[2];
1758 int changed;
1759
1760 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1761 fbits = 0;
1762 list_for_each(p, &subs->fmt_list) {
1763 struct audioformat *fp;
1764 fp = list_entry(p, struct audioformat, list);
1765 if (!hw_check_valid_format(subs, params, fp))
1766 continue;
1767 fbits |= (1ULL << fp->format);
1768 }
1769
1770 oldbits[0] = fmt->bits[0];
1771 oldbits[1] = fmt->bits[1];
1772 fmt->bits[0] &= (u32)fbits;
1773 fmt->bits[1] &= (u32)(fbits >> 32);
1774 if (!fmt->bits[0] && !fmt->bits[1]) {
1775 hwc_debug(" --> get empty\n");
1776 return -EINVAL;
1777 }
1778 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1779 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1780 return changed;
1781}
1782
1783static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1784 struct snd_pcm_hw_rule *rule)
1785{
1786 struct snd_usb_substream *subs = rule->private;
1787 struct audioformat *fp;
1788 struct snd_interval *it;
1789 unsigned char min_datainterval;
1790 unsigned int pmin;
1791 int changed;
1792
1793 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1794 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1795 min_datainterval = 0xff;
1796 list_for_each_entry(fp, &subs->fmt_list, list) {
1797 if (!hw_check_valid_format(subs, params, fp))
1798 continue;
1799 min_datainterval = min(min_datainterval, fp->datainterval);
1800 }
1801 if (min_datainterval == 0xff) {
1802 hwc_debug(" --> get emtpy\n");
1803 it->empty = 1;
1804 return -EINVAL;
1805 }
1806 pmin = 125 * (1 << min_datainterval);
1807 changed = 0;
1808 if (it->min < pmin) {
1809 it->min = pmin;
1810 it->openmin = 0;
1811 changed = 1;
1812 }
1813 if (snd_interval_checkempty(it)) {
1814 it->empty = 1;
1815 return -EINVAL;
1816 }
1817 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1818 return changed;
1819}
1820
1821/*
1822 * If the device supports unusual bit rates, does the request meet these?
1823 */
1824static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1825 struct snd_usb_substream *subs)
1826{
1827 struct audioformat *fp;
1828 int count = 0, needs_knot = 0;
1829 int err;
1830
1831 list_for_each_entry(fp, &subs->fmt_list, list) {
1832 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1833 return 0;
1834 count += fp->nr_rates;
1835 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1836 needs_knot = 1;
1837 }
1838 if (!needs_knot)
1839 return 0;
1840
1841 subs->rate_list.count = count;
1842 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
1843 subs->rate_list.mask = 0;
1844 count = 0;
1845 list_for_each_entry(fp, &subs->fmt_list, list) {
1846 int i;
1847 for (i = 0; i < fp->nr_rates; i++)
1848 subs->rate_list.list[count++] = fp->rate_table[i];
1849 }
1850 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1851 &subs->rate_list);
1852 if (err < 0)
1853 return err;
1854
1855 return 0;
1856}
1857
1858
1859/*
1860 * set up the runtime hardware information.
1861 */
1862
1863static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1864{
1865 struct list_head *p;
1866 unsigned int pt, ptmin;
1867 int param_period_time_if_needed;
1868 int err;
1869
1870 runtime->hw.formats = subs->formats;
1871
1872 runtime->hw.rate_min = 0x7fffffff;
1873 runtime->hw.rate_max = 0;
1874 runtime->hw.channels_min = 256;
1875 runtime->hw.channels_max = 0;
1876 runtime->hw.rates = 0;
1877 ptmin = UINT_MAX;
1878 /* check min/max rates and channels */
1879 list_for_each(p, &subs->fmt_list) {
1880 struct audioformat *fp;
1881 fp = list_entry(p, struct audioformat, list);
1882 runtime->hw.rates |= fp->rates;
1883 if (runtime->hw.rate_min > fp->rate_min)
1884 runtime->hw.rate_min = fp->rate_min;
1885 if (runtime->hw.rate_max < fp->rate_max)
1886 runtime->hw.rate_max = fp->rate_max;
1887 if (runtime->hw.channels_min > fp->channels)
1888 runtime->hw.channels_min = fp->channels;
1889 if (runtime->hw.channels_max < fp->channels)
1890 runtime->hw.channels_max = fp->channels;
1891 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1892 /* FIXME: there might be more than one audio formats... */
1893 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1894 fp->frame_size;
1895 }
1896 pt = 125 * (1 << fp->datainterval);
1897 ptmin = min(ptmin, pt);
1898 }
1899
1900 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1901 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
1902 /* full speed devices have fixed data packet interval */
1903 ptmin = 1000;
1904 if (ptmin == 1000)
1905 /* if period time doesn't go below 1 ms, no rules needed */
1906 param_period_time_if_needed = -1;
1907 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1908 ptmin, UINT_MAX);
1909
1910 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1911 hw_rule_rate, subs,
1912 SNDRV_PCM_HW_PARAM_FORMAT,
1913 SNDRV_PCM_HW_PARAM_CHANNELS,
1914 param_period_time_if_needed,
1915 -1)) < 0)
1916 return err;
1917 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1918 hw_rule_channels, subs,
1919 SNDRV_PCM_HW_PARAM_FORMAT,
1920 SNDRV_PCM_HW_PARAM_RATE,
1921 param_period_time_if_needed,
1922 -1)) < 0)
1923 return err;
1924 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1925 hw_rule_format, subs,
1926 SNDRV_PCM_HW_PARAM_RATE,
1927 SNDRV_PCM_HW_PARAM_CHANNELS,
1928 param_period_time_if_needed,
1929 -1)) < 0)
1930 return err;
1931 if (param_period_time_if_needed >= 0) {
1932 err = snd_pcm_hw_rule_add(runtime, 0,
1933 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1934 hw_rule_period_time, subs,
1935 SNDRV_PCM_HW_PARAM_FORMAT,
1936 SNDRV_PCM_HW_PARAM_CHANNELS,
1937 SNDRV_PCM_HW_PARAM_RATE,
1938 -1);
1939 if (err < 0)
1940 return err;
1941 }
1942 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
1943 return err;
1944 return 0;
1945}
1946
1947static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1948{
1949 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1950 struct snd_pcm_runtime *runtime = substream->runtime;
1951 struct snd_usb_substream *subs = &as->substream[direction];
1952
1953 subs->interface = -1;
1954 subs->format = 0;
1955 runtime->hw = snd_usb_hardware;
1956 runtime->private_data = subs;
1957 subs->pcm_substream = substream;
1958 return setup_hw_info(runtime, subs);
1959}
1960
1961static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1962{
1963 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1964 struct snd_usb_substream *subs = &as->substream[direction];
1965
1966 if (!as->chip->shutdown && subs->interface >= 0) {
1967 usb_set_interface(subs->dev, subs->interface, 0);
1968 subs->interface = -1;
1969 }
1970 subs->pcm_substream = NULL;
1971 return 0;
1972}
1973
1974static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1975{
1976 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1977}
1978
1979static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1980{
1981 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1982}
1983
1984static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1985{
1986 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1987}
1988
1989static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1990{
1991 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1992}
1993
1994static struct snd_pcm_ops snd_usb_playback_ops = {
1995 .open = snd_usb_playback_open,
1996 .close = snd_usb_playback_close,
1997 .ioctl = snd_pcm_lib_ioctl,
1998 .hw_params = snd_usb_hw_params,
1999 .hw_free = snd_usb_hw_free,
2000 .prepare = snd_usb_pcm_prepare,
2001 .trigger = snd_usb_pcm_playback_trigger,
2002 .pointer = snd_usb_pcm_pointer,
2003 .page = snd_pcm_lib_get_vmalloc_page,
2004 .mmap = snd_pcm_lib_mmap_vmalloc,
2005};
2006
2007static struct snd_pcm_ops snd_usb_capture_ops = {
2008 .open = snd_usb_capture_open,
2009 .close = snd_usb_capture_close,
2010 .ioctl = snd_pcm_lib_ioctl,
2011 .hw_params = snd_usb_hw_params,
2012 .hw_free = snd_usb_hw_free,
2013 .prepare = snd_usb_pcm_prepare,
2014 .trigger = snd_usb_pcm_capture_trigger,
2015 .pointer = snd_usb_pcm_pointer,
2016 .page = snd_pcm_lib_get_vmalloc_page,
2017 .mmap = snd_pcm_lib_mmap_vmalloc,
2018};
2019
2020
2021
2022/*
2023 * helper functions
2024 */
2025
2026/*
2027 * combine bytes and get an integer value
2028 */
2029unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
2030{
2031 switch (size) {
2032 case 1: return *bytes;
2033 case 2: return combine_word(bytes);
2034 case 3: return combine_triple(bytes);
2035 case 4: return combine_quad(bytes);
2036 default: return 0;
2037 }
2038}
2039
2040/*
2041 * parse descriptor buffer and return the pointer starting the given
2042 * descriptor type.
2043 */
2044void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
2045{
2046 u8 *p, *end, *next;
2047
2048 p = descstart;
2049 end = p + desclen;
2050 for (; p < end;) {
2051 if (p[0] < 2)
2052 return NULL;
2053 next = p + p[0];
2054 if (next > end)
2055 return NULL;
2056 if (p[1] == dtype && (!after || (void *)p > after)) {
2057 return p;
2058 }
2059 p = next;
2060 }
2061 return NULL;
2062}
2063
2064/*
2065 * find a class-specified interface descriptor with the given subtype.
2066 */
2067void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
2068{
2069 unsigned char *p = after;
2070
2071 while ((p = snd_usb_find_desc(buffer, buflen, p,
2072 USB_DT_CS_INTERFACE)) != NULL) {
2073 if (p[0] >= 3 && p[2] == dsubtype)
2074 return p;
2075 }
2076 return NULL;
2077}
2078
2079/*
2080 * Wrapper for usb_control_msg().
2081 * Allocates a temp buffer to prevent dmaing from/to the stack.
2082 */
2083int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
2084 __u8 requesttype, __u16 value, __u16 index, void *data,
2085 __u16 size, int timeout)
2086{
2087 int err;
2088 void *buf = NULL;
2089
2090 if (size > 0) {
2091 buf = kmemdup(data, size, GFP_KERNEL);
2092 if (!buf)
2093 return -ENOMEM;
2094 }
2095 err = usb_control_msg(dev, pipe, request, requesttype,
2096 value, index, buf, size, timeout);
2097 if (size > 0) {
2098 memcpy(data, buf, size);
2099 kfree(buf);
2100 }
2101 return err;
2102}
2103
2104
2105/*
2106 * entry point for linux usb interface
2107 */
2108
2109static int usb_audio_probe(struct usb_interface *intf,
2110 const struct usb_device_id *id);
2111static void usb_audio_disconnect(struct usb_interface *intf);
2112
2113#ifdef CONFIG_PM
2114static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message);
2115static int usb_audio_resume(struct usb_interface *intf);
2116#else
2117#define usb_audio_suspend NULL
2118#define usb_audio_resume NULL
2119#endif
2120
2121static struct usb_device_id usb_audio_ids [] = {
2122#include "usbquirks.h"
2123 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
2124 .bInterfaceClass = USB_CLASS_AUDIO,
2125 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
2126 { } /* Terminating entry */
2127};
2128
2129MODULE_DEVICE_TABLE (usb, usb_audio_ids);
2130
2131static struct usb_driver usb_audio_driver = {
2132 .name = "snd-usb-audio",
2133 .probe = usb_audio_probe,
2134 .disconnect = usb_audio_disconnect,
2135 .suspend = usb_audio_suspend,
2136 .resume = usb_audio_resume,
2137 .id_table = usb_audio_ids,
2138};
2139
2140
2141#if defined(CONFIG_PROC_FS) && defined(CONFIG_SND_VERBOSE_PROCFS)
2142
2143/*
2144 * proc interface for list the supported pcm formats
2145 */
2146static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
2147{
2148 struct list_head *p;
2149 static char *sync_types[4] = {
2150 "NONE", "ASYNC", "ADAPTIVE", "SYNC"
2151 };
2152
2153 list_for_each(p, &subs->fmt_list) {
2154 struct audioformat *fp;
2155 fp = list_entry(p, struct audioformat, list);
2156 snd_iprintf(buffer, " Interface %d\n", fp->iface);
2157 snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
2158 snd_iprintf(buffer, " Format: %s\n",
2159 snd_pcm_format_name(fp->format));
2160 snd_iprintf(buffer, " Channels: %d\n", fp->channels);
2161 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
2162 fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
2163 fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
2164 sync_types[(fp->ep_attr & USB_ENDPOINT_SYNCTYPE) >> 2]);
2165 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
2166 snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
2167 fp->rate_min, fp->rate_max);
2168 } else {
2169 unsigned int i;
2170 snd_iprintf(buffer, " Rates: ");
2171 for (i = 0; i < fp->nr_rates; i++) {
2172 if (i > 0)
2173 snd_iprintf(buffer, ", ");
2174 snd_iprintf(buffer, "%d", fp->rate_table[i]);
2175 }
2176 snd_iprintf(buffer, "\n");
2177 }
2178 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
2179 snd_iprintf(buffer, " Data packet interval: %d us\n",
2180 125 * (1 << fp->datainterval));
2181 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
2182 // snd_iprintf(buffer, " EP Attribute = %#x\n", fp->attributes);
2183 }
2184}
2185
2186static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
2187{
2188 if (subs->running) {
2189 unsigned int i;
2190 snd_iprintf(buffer, " Status: Running\n");
2191 snd_iprintf(buffer, " Interface = %d\n", subs->interface);
2192 snd_iprintf(buffer, " Altset = %d\n", subs->format);
2193 snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
2194 for (i = 0; i < subs->nurbs; i++)
2195 snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
2196 snd_iprintf(buffer, "]\n");
2197 snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
2198 snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
2199 snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
2200 ? get_full_speed_hz(subs->freqm)
2201 : get_high_speed_hz(subs->freqm),
2202 subs->freqm >> 16, subs->freqm & 0xffff);
2203 } else {
2204 snd_iprintf(buffer, " Status: Stop\n");
2205 }
2206}
2207
2208static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
2209{
2210 struct snd_usb_stream *stream = entry->private_data;
2211
2212 snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
2213
2214 if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
2215 snd_iprintf(buffer, "\nPlayback:\n");
2216 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2217 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2218 }
2219 if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
2220 snd_iprintf(buffer, "\nCapture:\n");
2221 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2222 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2223 }
2224}
2225
2226static void proc_pcm_format_add(struct snd_usb_stream *stream)
2227{
2228 struct snd_info_entry *entry;
2229 char name[32];
2230 struct snd_card *card = stream->chip->card;
2231
2232 sprintf(name, "stream%d", stream->pcm_index);
2233 if (!snd_card_proc_new(card, name, &entry))
2234 snd_info_set_text_ops(entry, stream, proc_pcm_format_read);
2235}
2236
2237#else
2238
2239static inline void proc_pcm_format_add(struct snd_usb_stream *stream)
2240{
2241}
2242
2243#endif
2244
2245/*
2246 * initialize the substream instance.
2247 */
2248
2249static void init_substream(struct snd_usb_stream *as, int stream, struct audioformat *fp)
2250{
2251 struct snd_usb_substream *subs = &as->substream[stream];
2252
2253 INIT_LIST_HEAD(&subs->fmt_list);
2254 spin_lock_init(&subs->lock);
2255
2256 subs->stream = as;
2257 subs->direction = stream;
2258 subs->dev = as->chip->dev;
2259 subs->txfr_quirk = as->chip->txfr_quirk;
2260 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) {
2261 subs->ops = audio_urb_ops[stream];
2262 } else {
2263 subs->ops = audio_urb_ops_high_speed[stream];
2264 switch (as->chip->usb_id) {
2265 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
2266 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
2267 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
2268 subs->ops.retire_sync = retire_playback_sync_urb_hs_emu;
2269 break;
2270 }
2271 }
2272 snd_pcm_set_ops(as->pcm, stream,
2273 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2274 &snd_usb_playback_ops : &snd_usb_capture_ops);
2275
2276 list_add_tail(&fp->list, &subs->fmt_list);
2277 subs->formats |= 1ULL << fp->format;
2278 subs->endpoint = fp->endpoint;
2279 subs->num_formats++;
2280 subs->fmt_type = fp->fmt_type;
2281}
2282
2283
2284/*
2285 * free a substream
2286 */
2287static void free_substream(struct snd_usb_substream *subs)
2288{
2289 struct list_head *p, *n;
2290
2291 if (!subs->num_formats)
2292 return; /* not initialized */
2293 list_for_each_safe(p, n, &subs->fmt_list) {
2294 struct audioformat *fp = list_entry(p, struct audioformat, list);
2295 kfree(fp->rate_table);
2296 kfree(fp);
2297 }
2298 kfree(subs->rate_list.list);
2299}
2300
2301
2302/*
2303 * free a usb stream instance
2304 */
2305static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
2306{
2307 free_substream(&stream->substream[0]);
2308 free_substream(&stream->substream[1]);
2309 list_del(&stream->list);
2310 kfree(stream);
2311}
2312
2313static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
2314{
2315 struct snd_usb_stream *stream = pcm->private_data;
2316 if (stream) {
2317 stream->pcm = NULL;
2318 snd_usb_audio_stream_free(stream);
2319 }
2320}
2321
2322
2323/*
2324 * add this endpoint to the chip instance.
2325 * if a stream with the same endpoint already exists, append to it.
2326 * if not, create a new pcm stream.
2327 */
2328static int add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)
2329{
2330 struct list_head *p;
2331 struct snd_usb_stream *as;
2332 struct snd_usb_substream *subs;
2333 struct snd_pcm *pcm;
2334 int err;
2335
2336 list_for_each(p, &chip->pcm_list) {
2337 as = list_entry(p, struct snd_usb_stream, list);
2338 if (as->fmt_type != fp->fmt_type)
2339 continue;
2340 subs = &as->substream[stream];
2341 if (!subs->endpoint)
2342 continue;
2343 if (subs->endpoint == fp->endpoint) {
2344 list_add_tail(&fp->list, &subs->fmt_list);
2345 subs->num_formats++;
2346 subs->formats |= 1ULL << fp->format;
2347 return 0;
2348 }
2349 }
2350 /* look for an empty stream */
2351 list_for_each(p, &chip->pcm_list) {
2352 as = list_entry(p, struct snd_usb_stream, list);
2353 if (as->fmt_type != fp->fmt_type)
2354 continue;
2355 subs = &as->substream[stream];
2356 if (subs->endpoint)
2357 continue;
2358 err = snd_pcm_new_stream(as->pcm, stream, 1);
2359 if (err < 0)
2360 return err;
2361 init_substream(as, stream, fp);
2362 return 0;
2363 }
2364
2365 /* create a new pcm */
2366 as = kzalloc(sizeof(*as), GFP_KERNEL);
2367 if (!as)
2368 return -ENOMEM;
2369 as->pcm_index = chip->pcm_devs;
2370 as->chip = chip;
2371 as->fmt_type = fp->fmt_type;
2372 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
2373 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
2374 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
2375 &pcm);
2376 if (err < 0) {
2377 kfree(as);
2378 return err;
2379 }
2380 as->pcm = pcm;
2381 pcm->private_data = as;
2382 pcm->private_free = snd_usb_audio_pcm_free;
2383 pcm->info_flags = 0;
2384 if (chip->pcm_devs > 0)
2385 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
2386 else
2387 strcpy(pcm->name, "USB Audio");
2388
2389 init_substream(as, stream, fp);
2390
2391 list_add(&as->list, &chip->pcm_list);
2392 chip->pcm_devs++;
2393
2394 proc_pcm_format_add(as);
2395
2396 return 0;
2397}
2398
2399
2400/*
2401 * check if the device uses big-endian samples
2402 */
2403static int is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
2404{
2405 switch (chip->usb_id) {
2406 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
2407 if (fp->endpoint & USB_DIR_IN)
2408 return 1;
2409 break;
2410 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2411 if (device_setup[chip->index] == 0x00 ||
2412 fp->altsetting==1 || fp->altsetting==2 || fp->altsetting==3)
2413 return 1;
2414 }
2415 return 0;
2416}
2417
2418/*
2419 * parse the audio format type I descriptor
2420 * and returns the corresponding pcm format
2421 *
2422 * @dev: usb device
2423 * @fp: audioformat record
2424 * @format: the format tag (wFormatTag)
2425 * @fmt: the format type descriptor
2426 */
2427static int parse_audio_format_i_type(struct snd_usb_audio *chip,
2428 struct audioformat *fp,
2429 int format, void *_fmt,
2430 int protocol)
2431{
2432 int pcm_format, i;
2433 int sample_width, sample_bytes;
2434
2435 switch (protocol) {
2436 case UAC_VERSION_1: {
2437 struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
2438 sample_width = fmt->bBitResolution;
2439 sample_bytes = fmt->bSubframeSize;
2440 break;
2441 }
2442
2443 case UAC_VERSION_2: {
2444 struct uac_format_type_i_ext_descriptor *fmt = _fmt;
2445 sample_width = fmt->bBitResolution;
2446 sample_bytes = fmt->bSubslotSize;
2447
2448 /*
2449 * FIXME
2450 * USB audio class v2 devices specify a bitmap of possible
2451 * audio formats rather than one fix value. For now, we just
2452 * pick one of them and report that as the only possible
2453 * value for this setting.
2454 * The bit allocation map is in fact compatible to the
2455 * wFormatTag of the v1 AS streaming descriptors, which is why
2456 * we can simply map the matrix.
2457 */
2458
2459 for (i = 0; i < 5; i++)
2460 if (format & (1UL << i)) {
2461 format = i + 1;
2462 break;
2463 }
2464
2465 break;
2466 }
2467
2468 default:
2469 return -EINVAL;
2470 }
2471
2472 /* FIXME: correct endianess and sign? */
2473 pcm_format = -1;
2474
2475 switch (format) {
2476 case UAC_FORMAT_TYPE_I_UNDEFINED: /* some devices don't define this correctly... */
2477 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
2478 chip->dev->devnum, fp->iface, fp->altsetting);
2479 /* fall-through */
2480 case UAC_FORMAT_TYPE_I_PCM:
2481 if (sample_width > sample_bytes * 8) {
2482 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
2483 chip->dev->devnum, fp->iface, fp->altsetting,
2484 sample_width, sample_bytes);
2485 }
2486 /* check the format byte size */
2487 switch (sample_bytes) {
2488 case 1:
2489 pcm_format = SNDRV_PCM_FORMAT_S8;
2490 break;
2491 case 2:
2492 if (is_big_endian_format(chip, fp))
2493 pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
2494 else
2495 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2496 break;
2497 case 3:
2498 if (is_big_endian_format(chip, fp))
2499 pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
2500 else
2501 pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
2502 break;
2503 case 4:
2504 pcm_format = SNDRV_PCM_FORMAT_S32_LE;
2505 break;
2506 default:
2507 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
2508 chip->dev->devnum, fp->iface, fp->altsetting,
2509 sample_width, sample_bytes);
2510 break;
2511 }
2512 break;
2513 case UAC_FORMAT_TYPE_I_PCM8:
2514 pcm_format = SNDRV_PCM_FORMAT_U8;
2515
2516 /* Dallas DS4201 workaround: it advertises U8 format, but really
2517 supports S8. */
2518 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
2519 pcm_format = SNDRV_PCM_FORMAT_S8;
2520 break;
2521 case UAC_FORMAT_TYPE_I_IEEE_FLOAT:
2522 pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
2523 break;
2524 case UAC_FORMAT_TYPE_I_ALAW:
2525 pcm_format = SNDRV_PCM_FORMAT_A_LAW;
2526 break;
2527 case UAC_FORMAT_TYPE_I_MULAW:
2528 pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
2529 break;
2530 default:
2531 snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
2532 chip->dev->devnum, fp->iface, fp->altsetting, format);
2533 break;
2534 }
2535 return pcm_format;
2536}
2537
2538
2539/*
2540 * parse the format descriptor and stores the possible sample rates
2541 * on the audioformat table (audio class v1).
2542 *
2543 * @dev: usb device
2544 * @fp: audioformat record
2545 * @fmt: the format descriptor
2546 * @offset: the start offset of descriptor pointing the rate type
2547 * (7 for type I and II, 8 for type II)
2548 */
2549static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
2550 unsigned char *fmt, int offset)
2551{
2552 int nr_rates = fmt[offset];
2553
2554 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
2555 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
2556 chip->dev->devnum, fp->iface, fp->altsetting);
2557 return -1;
2558 }
2559
2560 if (nr_rates) {
2561 /*
2562 * build the rate table and bitmap flags
2563 */
2564 int r, idx;
2565
2566 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2567 if (fp->rate_table == NULL) {
2568 snd_printk(KERN_ERR "cannot malloc\n");
2569 return -1;
2570 }
2571
2572 fp->nr_rates = 0;
2573 fp->rate_min = fp->rate_max = 0;
2574 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
2575 unsigned int rate = combine_triple(&fmt[idx]);
2576 if (!rate)
2577 continue;
2578 /* C-Media CM6501 mislabels its 96 kHz altsetting */
2579 if (rate == 48000 && nr_rates == 1 &&
2580 (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
2581 chip->usb_id == USB_ID(0x0d8c, 0x0102)) &&
2582 fp->altsetting == 5 && fp->maxpacksize == 392)
2583 rate = 96000;
2584 /* Creative VF0470 Live Cam reports 16 kHz instead of 8kHz */
2585 if (rate == 16000 && chip->usb_id == USB_ID(0x041e, 0x4068))
2586 rate = 8000;
2587 fp->rate_table[fp->nr_rates] = rate;
2588 if (!fp->rate_min || rate < fp->rate_min)
2589 fp->rate_min = rate;
2590 if (!fp->rate_max || rate > fp->rate_max)
2591 fp->rate_max = rate;
2592 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
2593 fp->nr_rates++;
2594 }
2595 if (!fp->nr_rates) {
2596 hwc_debug("All rates were zero. Skipping format!\n");
2597 return -1;
2598 }
2599 } else {
2600 /* continuous rates */
2601 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
2602 fp->rate_min = combine_triple(&fmt[offset + 1]);
2603 fp->rate_max = combine_triple(&fmt[offset + 4]);
2604 }
2605 return 0;
2606}
2607
2608/*
2609 * parse the format descriptor and stores the possible sample rates
2610 * on the audioformat table (audio class v2).
2611 */
2612static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,
2613 struct audioformat *fp,
2614 struct usb_host_interface *iface)
2615{
2616 struct usb_device *dev = chip->dev;
2617 unsigned char tmp[2], *data;
2618 int i, nr_rates, data_size, ret = 0;
2619
2620 /* get the number of sample rates first by only fetching 2 bytes */
2621 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
2622 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
2623 0x0100, chip->clock_id << 8, tmp, sizeof(tmp), 1000);
2624
2625 if (ret < 0) {
2626 snd_printk(KERN_ERR "unable to retrieve number of sample rates\n");
2627 goto err;
2628 }
2629
2630 nr_rates = (tmp[1] << 8) | tmp[0];
2631 data_size = 2 + 12 * nr_rates;
2632 data = kzalloc(data_size, GFP_KERNEL);
2633 if (!data) {
2634 ret = -ENOMEM;
2635 goto err;
2636 }
2637
2638 /* now get the full information */
2639 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
2640 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
2641 0x0100, chip->clock_id << 8, data, data_size, 1000);
2642
2643 if (ret < 0) {
2644 snd_printk(KERN_ERR "unable to retrieve sample rate range\n");
2645 ret = -EINVAL;
2646 goto err_free;
2647 }
2648
2649 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2650 if (!fp->rate_table) {
2651 ret = -ENOMEM;
2652 goto err_free;
2653 }
2654
2655 fp->nr_rates = 0;
2656 fp->rate_min = fp->rate_max = 0;
2657
2658 for (i = 0; i < nr_rates; i++) {
2659 int rate = combine_quad(&data[2 + 12 * i]);
2660
2661 fp->rate_table[fp->nr_rates] = rate;
2662 if (!fp->rate_min || rate < fp->rate_min)
2663 fp->rate_min = rate;
2664 if (!fp->rate_max || rate > fp->rate_max)
2665 fp->rate_max = rate;
2666 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
2667 fp->nr_rates++;
2668 }
2669
2670err_free:
2671 kfree(data);
2672err:
2673 return ret;
2674}
2675
2676/*
2677 * parse the format type I and III descriptors
2678 */
2679static int parse_audio_format_i(struct snd_usb_audio *chip,
2680 struct audioformat *fp,
2681 int format, void *_fmt,
2682 struct usb_host_interface *iface)
2683{
2684 struct usb_interface_descriptor *altsd = get_iface_desc(iface);
2685 struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
2686 int protocol = altsd->bInterfaceProtocol;
2687 int pcm_format, ret;
2688
2689 if (fmt->bFormatType == UAC_FORMAT_TYPE_III) {
2690 /* FIXME: the format type is really IECxxx
2691 * but we give normal PCM format to get the existing
2692 * apps working...
2693 */
2694 switch (chip->usb_id) {
2695
2696 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2697 if (device_setup[chip->index] == 0x00 &&
2698 fp->altsetting == 6)
2699 pcm_format = SNDRV_PCM_FORMAT_S16_BE;
2700 else
2701 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2702 break;
2703 default:
2704 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2705 }
2706 } else {
2707 pcm_format = parse_audio_format_i_type(chip, fp, format, fmt, protocol);
2708 if (pcm_format < 0)
2709 return -1;
2710 }
2711
2712 fp->format = pcm_format;
2713
2714 /* gather possible sample rates */
2715 /* audio class v1 reports possible sample rates as part of the
2716 * proprietary class specific descriptor.
2717 * audio class v2 uses class specific EP0 range requests for that.
2718 */
2719 switch (protocol) {
2720 case UAC_VERSION_1:
2721 fp->channels = fmt->bNrChannels;
2722 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 7);
2723 break;
2724 case UAC_VERSION_2:
2725 /* fp->channels is already set in this case */
2726 ret = parse_audio_format_rates_v2(chip, fp, iface);
2727 break;
2728 }
2729
2730 if (fp->channels < 1) {
2731 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
2732 chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
2733 return -1;
2734 }
2735
2736 return ret;
2737}
2738
2739/*
2740 * parse the format type II descriptor
2741 */
2742static int parse_audio_format_ii(struct snd_usb_audio *chip,
2743 struct audioformat *fp,
2744 int format, void *_fmt,
2745 struct usb_host_interface *iface)
2746{
2747 int brate, framesize, ret;
2748 struct usb_interface_descriptor *altsd = get_iface_desc(iface);
2749 int protocol = altsd->bInterfaceProtocol;
2750
2751 switch (format) {
2752 case UAC_FORMAT_TYPE_II_AC3:
2753 /* FIXME: there is no AC3 format defined yet */
2754 // fp->format = SNDRV_PCM_FORMAT_AC3;
2755 fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
2756 break;
2757 case UAC_FORMAT_TYPE_II_MPEG:
2758 fp->format = SNDRV_PCM_FORMAT_MPEG;
2759 break;
2760 default:
2761 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",
2762 chip->dev->devnum, fp->iface, fp->altsetting, format);
2763 fp->format = SNDRV_PCM_FORMAT_MPEG;
2764 break;
2765 }
2766
2767 fp->channels = 1;
2768
2769 switch (protocol) {
2770 case UAC_VERSION_1: {
2771 struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
2772 brate = le16_to_cpu(fmt->wMaxBitRate);
2773 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
2774 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
2775 fp->frame_size = framesize;
2776 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
2777 break;
2778 }
2779 case UAC_VERSION_2: {
2780 struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
2781 brate = le16_to_cpu(fmt->wMaxBitRate);
2782 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
2783 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
2784 fp->frame_size = framesize;
2785 ret = parse_audio_format_rates_v2(chip, fp, iface);
2786 break;
2787 }
2788 }
2789
2790 return ret;
2791}
2792
2793static int parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,
2794 int format, unsigned char *fmt, int stream,
2795 struct usb_host_interface *iface)
2796{
2797 int err;
2798
2799 switch (fmt[3]) {
2800 case UAC_FORMAT_TYPE_I:
2801 case UAC_FORMAT_TYPE_III:
2802 err = parse_audio_format_i(chip, fp, format, fmt, iface);
2803 break;
2804 case UAC_FORMAT_TYPE_II:
2805 err = parse_audio_format_ii(chip, fp, format, fmt, iface);
2806 break;
2807 default:
2808 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
2809 chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);
2810 return -1;
2811 }
2812 fp->fmt_type = fmt[3];
2813 if (err < 0)
2814 return err;
2815#if 1
2816 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
2817 /* extigy apparently supports sample rates other than 48k
2818 * but not in ordinary way. so we enable only 48k atm.
2819 */
2820 if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
2821 chip->usb_id == USB_ID(0x041e, 0x3020) ||
2822 chip->usb_id == USB_ID(0x041e, 0x3061)) {
2823 if (fmt[3] == UAC_FORMAT_TYPE_I &&
2824 fp->rates != SNDRV_PCM_RATE_48000 &&
2825 fp->rates != SNDRV_PCM_RATE_96000)
2826 return -1;
2827 }
2828#endif
2829 return 0;
2830}
2831
2832static unsigned char parse_datainterval(struct snd_usb_audio *chip,
2833 struct usb_host_interface *alts)
2834{
2835 if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH &&
2836 get_endpoint(alts, 0)->bInterval >= 1 &&
2837 get_endpoint(alts, 0)->bInterval <= 4)
2838 return get_endpoint(alts, 0)->bInterval - 1;
2839 else
2840 return 0;
2841}
2842
2843static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
2844 int iface, int altno);
2845static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
2846{
2847 struct usb_device *dev;
2848 struct usb_interface *iface;
2849 struct usb_host_interface *alts;
2850 struct usb_interface_descriptor *altsd;
2851 int i, altno, err, stream;
2852 int format = 0, num_channels = 0;
2853 struct audioformat *fp = NULL;
2854 unsigned char *fmt, *csep;
2855 int num, protocol;
2856
2857 dev = chip->dev;
2858
2859 /* parse the interface's altsettings */
2860 iface = usb_ifnum_to_if(dev, iface_no);
2861
2862 num = iface->num_altsetting;
2863
2864 /*
2865 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
2866 * one misses syncpipe, and does not produce any sound.
2867 */
2868 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
2869 num = 4;
2870
2871 for (i = 0; i < num; i++) {
2872 alts = &iface->altsetting[i];
2873 altsd = get_iface_desc(alts);
2874 protocol = altsd->bInterfaceProtocol;
2875 /* skip invalid one */
2876 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2877 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2878 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
2879 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
2880 altsd->bNumEndpoints < 1 ||
2881 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
2882 continue;
2883 /* must be isochronous */
2884 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2885 USB_ENDPOINT_XFER_ISOC)
2886 continue;
2887 /* check direction */
2888 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
2889 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2890 altno = altsd->bAlternateSetting;
2891
2892 /* audiophile usb: skip altsets incompatible with device_setup
2893 */
2894 if (chip->usb_id == USB_ID(0x0763, 0x2003) &&
2895 audiophile_skip_setting_quirk(chip, iface_no, altno))
2896 continue;
2897
2898 /* get audio formats */
2899 switch (protocol) {
2900 case UAC_VERSION_1: {
2901 struct uac_as_header_descriptor_v1 *as =
2902 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
2903
2904 if (!as) {
2905 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
2906 dev->devnum, iface_no, altno);
2907 continue;
2908 }
2909
2910 if (as->bLength < sizeof(*as)) {
2911 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
2912 dev->devnum, iface_no, altno);
2913 continue;
2914 }
2915
2916 format = le16_to_cpu(as->wFormatTag); /* remember the format value */
2917 break;
2918 }
2919
2920 case UAC_VERSION_2: {
2921 struct uac_as_header_descriptor_v2 *as =
2922 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
2923
2924 if (!as) {
2925 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
2926 dev->devnum, iface_no, altno);
2927 continue;
2928 }
2929
2930 if (as->bLength < sizeof(*as)) {
2931 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
2932 dev->devnum, iface_no, altno);
2933 continue;
2934 }
2935
2936 num_channels = as->bNrChannels;
2937 format = le32_to_cpu(as->bmFormats);
2938
2939 break;
2940 }
2941
2942 default:
2943 snd_printk(KERN_ERR "%d:%u:%d : unknown interface protocol %04x\n",
2944 dev->devnum, iface_no, altno, protocol);
2945 continue;
2946 }
2947
2948 /* get format type */
2949 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
2950 if (!fmt) {
2951 snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
2952 dev->devnum, iface_no, altno);
2953 continue;
2954 }
2955 if (((protocol == UAC_VERSION_1) && (fmt[0] < 8)) ||
2956 ((protocol == UAC_VERSION_2) && (fmt[0] != 6))) {
2957 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
2958 dev->devnum, iface_no, altno);
2959 continue;
2960 }
2961
2962 /*
2963 * Blue Microphones workaround: The last altsetting is identical
2964 * with the previous one, except for a larger packet size, but
2965 * is actually a mislabeled two-channel setting; ignore it.
2966 */
2967 if (fmt[4] == 1 && fmt[5] == 2 && altno == 2 && num == 3 &&
2968 fp && fp->altsetting == 1 && fp->channels == 1 &&
2969 fp->format == SNDRV_PCM_FORMAT_S16_LE &&
2970 protocol == UAC_VERSION_1 &&
2971 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
2972 fp->maxpacksize * 2)
2973 continue;
2974
2975 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
2976 /* Creamware Noah has this descriptor after the 2nd endpoint */
2977 if (!csep && altsd->bNumEndpoints >= 2)
2978 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
2979 if (!csep || csep[0] < 7 || csep[2] != UAC_EP_GENERAL) {
2980 snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
2981 " class specific endpoint descriptor\n",
2982 dev->devnum, iface_no, altno);
2983 csep = NULL;
2984 }
2985
2986 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
2987 if (! fp) {
2988 snd_printk(KERN_ERR "cannot malloc\n");
2989 return -ENOMEM;
2990 }
2991
2992 fp->iface = iface_no;
2993 fp->altsetting = altno;
2994 fp->altset_idx = i;
2995 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2996 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2997 fp->datainterval = parse_datainterval(chip, alts);
2998 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2999 /* num_channels is only set for v2 interfaces */
3000 fp->channels = num_channels;
3001 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
3002 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
3003 * (fp->maxpacksize & 0x7ff);
3004 fp->attributes = csep ? csep[3] : 0;
3005
3006 /* some quirks for attributes here */
3007
3008 switch (chip->usb_id) {
3009 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
3010 /* Optoplay sets the sample rate attribute although
3011 * it seems not supporting it in fact.
3012 */
3013 fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
3014 break;
3015 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
3016 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
3017 /* doesn't set the sample rate attribute, but supports it */
3018 fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
3019 break;
3020 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
3021 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
3022 an older model 77d:223) */
3023 /*
3024 * plantronics headset and Griffin iMic have set adaptive-in
3025 * although it's really not...
3026 */
3027 fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
3028 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3029 fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
3030 else
3031 fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
3032 break;
3033 }
3034
3035 /* ok, let's parse further... */
3036 if (parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {
3037 kfree(fp->rate_table);
3038 kfree(fp);
3039 continue;
3040 }
3041
3042 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
3043 err = add_audio_endpoint(chip, stream, fp);
3044 if (err < 0) {
3045 kfree(fp->rate_table);
3046 kfree(fp);
3047 return err;
3048 }
3049 /* try to set the interface... */
3050 usb_set_interface(chip->dev, iface_no, altno);
3051 init_usb_pitch(chip->dev, iface_no, alts, fp);
3052 init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
3053 }
3054 return 0;
3055}
3056
3057
3058/*
3059 * disconnect streams
3060 * called from snd_usb_audio_disconnect()
3061 */
3062static void snd_usb_stream_disconnect(struct list_head *head)
3063{
3064 int idx;
3065 struct snd_usb_stream *as;
3066 struct snd_usb_substream *subs;
3067
3068 as = list_entry(head, struct snd_usb_stream, list);
3069 for (idx = 0; idx < 2; idx++) {
3070 subs = &as->substream[idx];
3071 if (!subs->num_formats)
3072 return;
3073 release_substream_urbs(subs, 1);
3074 subs->interface = -1;
3075 }
3076}
3077
3078static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
3079{
3080 struct usb_device *dev = chip->dev;
3081 struct usb_host_interface *alts;
3082 struct usb_interface_descriptor *altsd;
3083 struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
3084
3085 if (!iface) {
3086 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
3087 dev->devnum, ctrlif, interface);
3088 return -EINVAL;
3089 }
3090
3091 if (usb_interface_claimed(iface)) {
3092 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",
3093 dev->devnum, ctrlif, interface);
3094 return -EINVAL;
3095 }
3096
3097 alts = &iface->altsetting[0];
3098 altsd = get_iface_desc(alts);
3099 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
3100 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
3101 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
3102 int err = snd_usbmidi_create(chip->card, iface,
3103 &chip->midi_list, NULL);
3104 if (err < 0) {
3105 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",
3106 dev->devnum, ctrlif, interface);
3107 return -EINVAL;
3108 }
3109 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
3110
3111 return 0;
3112 }
3113
3114 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
3115 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
3116 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
3117 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",
3118 dev->devnum, ctrlif, interface, altsd->bInterfaceClass);
3119 /* skip non-supported classes */
3120 return -EINVAL;
3121 }
3122
3123 if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
3124 snd_printk(KERN_ERR "low speed audio streaming not supported\n");
3125 return -EINVAL;
3126 }
3127
3128 if (! parse_audio_endpoints(chip, interface)) {
3129 usb_set_interface(dev, interface, 0); /* reset the current interface */
3130 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
3131 return -EINVAL;
3132 }
3133
3134 return 0;
3135}
3136
3137/*
3138 * parse audio control descriptor and create pcm/midi streams
3139 */
3140static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
3141{
3142 struct usb_device *dev = chip->dev;
3143 struct usb_host_interface *host_iface;
3144 struct usb_interface_descriptor *altsd;
3145 void *control_header;
3146 int i, protocol;
3147
3148 /* find audiocontrol interface */
3149 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
3150 control_header = snd_usb_find_csint_desc(host_iface->extra,
3151 host_iface->extralen,
3152 NULL, UAC_HEADER);
3153 altsd = get_iface_desc(host_iface);
3154 protocol = altsd->bInterfaceProtocol;
3155
3156 if (!control_header) {
3157 snd_printk(KERN_ERR "cannot find UAC_HEADER\n");
3158 return -EINVAL;
3159 }
3160
3161 switch (protocol) {
3162 case UAC_VERSION_1: {
3163 struct uac_ac_header_descriptor_v1 *h1 = control_header;
3164
3165 if (!h1->bInCollection) {
3166 snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");
3167 return -EINVAL;
3168 }
3169
3170 if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
3171 snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");
3172 return -EINVAL;
3173 }
3174
3175 for (i = 0; i < h1->bInCollection; i++)
3176 snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
3177
3178 break;
3179 }
3180
3181 case UAC_VERSION_2: {
3182 struct uac_clock_source_descriptor *cs;
3183 struct usb_interface_assoc_descriptor *assoc =
3184 usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
3185
3186 if (!assoc) {
3187 snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
3188 return -EINVAL;
3189 }
3190
3191 /* FIXME: for now, we expect there is at least one clock source
3192 * descriptor and we always take the first one.
3193 * We should properly support devices with multiple clock sources,
3194 * clock selectors and sample rate conversion units. */
3195
3196 cs = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen,
3197 NULL, UAC_CLOCK_SOURCE);
3198
3199 if (!cs) {
3200 snd_printk(KERN_ERR "CLOCK_SOURCE descriptor not found\n");
3201 return -EINVAL;
3202 }
3203
3204 chip->clock_id = cs->bClockID;
3205
3206 for (i = 0; i < assoc->bInterfaceCount; i++) {
3207 int intf = assoc->bFirstInterface + i;
3208
3209 if (intf != ctrlif)
3210 snd_usb_create_stream(chip, ctrlif, intf);
3211 }
3212
3213 break;
3214 }
3215
3216 default:
3217 snd_printk(KERN_ERR "unknown protocol version 0x%02x\n", protocol);
3218 return -EINVAL;
3219 }
3220
3221 return 0;
3222}
3223
3224/*
3225 * create a stream for an endpoint/altsetting without proper descriptors
3226 */
3227static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
3228 struct usb_interface *iface,
3229 const struct snd_usb_audio_quirk *quirk)
3230{
3231 struct audioformat *fp;
3232 struct usb_host_interface *alts;
3233 int stream, err;
3234 unsigned *rate_table = NULL;
3235
3236 fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
3237 if (! fp) {
3238 snd_printk(KERN_ERR "cannot memdup\n");
3239 return -ENOMEM;
3240 }
3241 if (fp->nr_rates > 0) {
3242 rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
3243 if (!rate_table) {
3244 kfree(fp);
3245 return -ENOMEM;
3246 }
3247 memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
3248 fp->rate_table = rate_table;
3249 }
3250
3251 stream = (fp->endpoint & USB_DIR_IN)
3252 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
3253 err = add_audio_endpoint(chip, stream, fp);
3254 if (err < 0) {
3255 kfree(fp);
3256 kfree(rate_table);
3257 return err;
3258 }
3259 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
3260 fp->altset_idx >= iface->num_altsetting) {
3261 kfree(fp);
3262 kfree(rate_table);
3263 return -EINVAL;
3264 }
3265 alts = &iface->altsetting[fp->altset_idx];
3266 fp->datainterval = parse_datainterval(chip, alts);
3267 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
3268 usb_set_interface(chip->dev, fp->iface, 0);
3269 init_usb_pitch(chip->dev, fp->iface, alts, fp);
3270 init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
3271 return 0;
3272}
3273
3274/*
3275 * create a stream for an interface with proper descriptors
3276 */
3277static int create_standard_audio_quirk(struct snd_usb_audio *chip,
3278 struct usb_interface *iface,
3279 const struct snd_usb_audio_quirk *quirk)
3280{
3281 struct usb_host_interface *alts;
3282 struct usb_interface_descriptor *altsd;
3283 int err;
3284
3285 alts = &iface->altsetting[0];
3286 altsd = get_iface_desc(alts);
3287 err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
3288 if (err < 0) {
3289 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
3290 altsd->bInterfaceNumber, err);
3291 return err;
3292 }
3293 /* reset the current interface */
3294 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
3295 return 0;
3296}
3297
3298/*
3299 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
3300 * The only way to detect the sample rate is by looking at wMaxPacketSize.
3301 */
3302static int create_uaxx_quirk(struct snd_usb_audio *chip,
3303 struct usb_interface *iface,
3304 const struct snd_usb_audio_quirk *quirk)
3305{
3306 static const struct audioformat ua_format = {
3307 .format = SNDRV_PCM_FORMAT_S24_3LE,
3308 .channels = 2,
3309 .fmt_type = UAC_FORMAT_TYPE_I,
3310 .altsetting = 1,
3311 .altset_idx = 1,
3312 .rates = SNDRV_PCM_RATE_CONTINUOUS,
3313 };
3314 struct usb_host_interface *alts;
3315 struct usb_interface_descriptor *altsd;
3316 struct audioformat *fp;
3317 int stream, err;
3318
3319 /* both PCM and MIDI interfaces have 2 or more altsettings */
3320 if (iface->num_altsetting < 2)
3321 return -ENXIO;
3322 alts = &iface->altsetting[1];
3323 altsd = get_iface_desc(alts);
3324
3325 if (altsd->bNumEndpoints == 2) {
3326 static const struct snd_usb_midi_endpoint_info ua700_ep = {
3327 .out_cables = 0x0003,
3328 .in_cables = 0x0003
3329 };
3330 static const struct snd_usb_audio_quirk ua700_quirk = {
3331 .type = QUIRK_MIDI_FIXED_ENDPOINT,
3332 .data = &ua700_ep
3333 };
3334 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
3335 .out_cables = 0x0001,
3336 .in_cables = 0x0001
3337 };
3338 static const struct snd_usb_audio_quirk uaxx_quirk = {
3339 .type = QUIRK_MIDI_FIXED_ENDPOINT,
3340 .data = &uaxx_ep
3341 };
3342 const struct snd_usb_audio_quirk *quirk =
3343 chip->usb_id == USB_ID(0x0582, 0x002b)
3344 ? &ua700_quirk : &uaxx_quirk;
3345 return snd_usbmidi_create(chip->card, iface,
3346 &chip->midi_list, quirk);
3347 }
3348
3349 if (altsd->bNumEndpoints != 1)
3350 return -ENXIO;
3351
3352 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
3353 if (!fp)
3354 return -ENOMEM;
3355 memcpy(fp, &ua_format, sizeof(*fp));
3356
3357 fp->iface = altsd->bInterfaceNumber;
3358 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
3359 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
3360 fp->datainterval = 0;
3361 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
3362
3363 switch (fp->maxpacksize) {
3364 case 0x120:
3365 fp->rate_max = fp->rate_min = 44100;
3366 break;
3367 case 0x138:
3368 case 0x140:
3369 fp->rate_max = fp->rate_min = 48000;
3370 break;
3371 case 0x258:
3372 case 0x260:
3373 fp->rate_max = fp->rate_min = 96000;
3374 break;
3375 default:
3376 snd_printk(KERN_ERR "unknown sample rate\n");
3377 kfree(fp);
3378 return -ENXIO;
3379 }
3380
3381 stream = (fp->endpoint & USB_DIR_IN)
3382 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
3383 err = add_audio_endpoint(chip, stream, fp);
3384 if (err < 0) {
3385 kfree(fp);
3386 return err;
3387 }
3388 usb_set_interface(chip->dev, fp->iface, 0);
3389 return 0;
3390}
3391
3392static int snd_usb_create_quirk(struct snd_usb_audio *chip,
3393 struct usb_interface *iface,
3394 const struct snd_usb_audio_quirk *quirk);
3395
3396/*
3397 * handle the quirks for the contained interfaces
3398 */
3399static int create_composite_quirk(struct snd_usb_audio *chip,
3400 struct usb_interface *iface,
3401 const struct snd_usb_audio_quirk *quirk)
3402{
3403 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
3404 int err;
3405
3406 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
3407 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
3408 if (!iface)
3409 continue;
3410 if (quirk->ifnum != probed_ifnum &&
3411 usb_interface_claimed(iface))
3412 continue;
3413 err = snd_usb_create_quirk(chip, iface, quirk);
3414 if (err < 0)
3415 return err;
3416 if (quirk->ifnum != probed_ifnum)
3417 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
3418 }
3419 return 0;
3420}
3421
3422static int ignore_interface_quirk(struct snd_usb_audio *chip,
3423 struct usb_interface *iface,
3424 const struct snd_usb_audio_quirk *quirk)
3425{
3426 return 0;
3427}
3428
3429/*
3430 * Allow alignment on audio sub-slot (channel samples) rather than
3431 * on audio slots (audio frames)
3432 */
3433static int create_align_transfer_quirk(struct snd_usb_audio *chip,
3434 struct usb_interface *iface,
3435 const struct snd_usb_audio_quirk *quirk)
3436{
3437 chip->txfr_quirk = 1;
3438 return 1; /* Continue with creating streams and mixer */
3439}
3440
3441
3442/*
3443 * boot quirks
3444 */
3445
3446#define EXTIGY_FIRMWARE_SIZE_OLD 794
3447#define EXTIGY_FIRMWARE_SIZE_NEW 483
3448
3449static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
3450{
3451 struct usb_host_config *config = dev->actconfig;
3452 int err;
3453
3454 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
3455 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
3456 snd_printdd("sending Extigy boot sequence...\n");
3457 /* Send message to force it to reconnect with full interface. */
3458 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
3459 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
3460 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
3461 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
3462 &dev->descriptor, sizeof(dev->descriptor));
3463 config = dev->actconfig;
3464 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
3465 err = usb_reset_configuration(dev);
3466 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
3467 snd_printdd("extigy_boot: new boot length = %d\n",
3468 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
3469 return -ENODEV; /* quit this anyway */
3470 }
3471 return 0;
3472}
3473
3474static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
3475{
3476 u8 buf = 1;
3477
3478 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
3479 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3480 0, 0, &buf, 1, 1000);
3481 if (buf == 0) {
3482 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
3483 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3484 1, 2000, NULL, 0, 1000);
3485 return -ENODEV;
3486 }
3487 return 0;
3488}
3489
3490/*
3491 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
3492 * documented in the device's data sheet.
3493 */
3494static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
3495{
3496 u8 buf[4];
3497 buf[0] = 0x20;
3498 buf[1] = value & 0xff;
3499 buf[2] = (value >> 8) & 0xff;
3500 buf[3] = reg;
3501 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
3502 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
3503 0, 0, &buf, 4, 1000);
3504}
3505
3506static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
3507{
3508 /*
3509 * Enable line-out driver mode, set headphone source to front
3510 * channels, enable stereo mic.
3511 */
3512 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
3513}
3514
3515/*
3516 * C-Media CM6206 is based on CM106 with two additional
3517 * registers that are not documented in the data sheet.
3518 * Values here are chosen based on sniffing USB traffic
3519 * under Windows.
3520 */
3521static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
3522{
3523 int err, reg;
3524 int val[] = {0x200c, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
3525
3526 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
3527 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
3528 if (err < 0)
3529 return err;
3530 }
3531
3532 return err;
3533}
3534
3535/*
3536 * This call will put the synth in "USB send" mode, i.e it will send MIDI
3537 * messages through USB (this is disabled at startup). The synth will
3538 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
3539 * sign on its LCD. Values here are chosen based on sniffing USB traffic
3540 * under Windows.
3541 */
3542static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
3543{
3544 int err, actual_length;
3545
3546 /* "midi send" enable */
3547 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
3548
3549 void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
3550 if (!buf)
3551 return -ENOMEM;
3552 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
3553 ARRAY_SIZE(seq), &actual_length, 1000);
3554 kfree(buf);
3555 if (err < 0)
3556 return err;
3557
3558 return 0;
3559}
3560
3561/*
3562 * Setup quirks
3563 */
3564#define AUDIOPHILE_SET 0x01 /* if set, parse device_setup */
3565#define AUDIOPHILE_SET_DTS 0x02 /* if set, enable DTS Digital Output */
3566#define AUDIOPHILE_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
3567#define AUDIOPHILE_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
3568#define AUDIOPHILE_SET_DI 0x10 /* if set, enable Digital Input */
3569#define AUDIOPHILE_SET_MASK 0x1F /* bit mask for setup value */
3570#define AUDIOPHILE_SET_24B_48K_DI 0x19 /* value for 24bits+48KHz+Digital Input */
3571#define AUDIOPHILE_SET_24B_48K_NOTDI 0x09 /* value for 24bits+48KHz+No Digital Input */
3572#define AUDIOPHILE_SET_16B_48K_DI 0x11 /* value for 16bits+48KHz+Digital Input */
3573#define AUDIOPHILE_SET_16B_48K_NOTDI 0x01 /* value for 16bits+48KHz+No Digital Input */
3574
3575static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
3576 int iface, int altno)
3577{
3578 /* Reset ALL ifaces to 0 altsetting.
3579 * Call it for every possible altsetting of every interface.
3580 */
3581 usb_set_interface(chip->dev, iface, 0);
3582
3583 if (device_setup[chip->index] & AUDIOPHILE_SET) {
3584 if ((device_setup[chip->index] & AUDIOPHILE_SET_DTS)
3585 && altno != 6)
3586 return 1; /* skip this altsetting */
3587 if ((device_setup[chip->index] & AUDIOPHILE_SET_96K)
3588 && altno != 1)
3589 return 1; /* skip this altsetting */
3590 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3591 AUDIOPHILE_SET_24B_48K_DI && altno != 2)
3592 return 1; /* skip this altsetting */
3593 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3594 AUDIOPHILE_SET_24B_48K_NOTDI && altno != 3)
3595 return 1; /* skip this altsetting */
3596 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3597 AUDIOPHILE_SET_16B_48K_DI && altno != 4)
3598 return 1; /* skip this altsetting */
3599 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3600 AUDIOPHILE_SET_16B_48K_NOTDI && altno != 5)
3601 return 1; /* skip this altsetting */
3602 }
3603 return 0; /* keep this altsetting */
3604}
3605
3606static int create_any_midi_quirk(struct snd_usb_audio *chip,
3607 struct usb_interface *intf,
3608 const struct snd_usb_audio_quirk *quirk)
3609{
3610 return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
3611}
3612
3613/*
3614 * audio-interface quirks
3615 *
3616 * returns zero if no standard audio/MIDI parsing is needed.
3617 * returns a postive value if standard audio/midi interfaces are parsed
3618 * after this.
3619 * returns a negative value at error.
3620 */
3621static int snd_usb_create_quirk(struct snd_usb_audio *chip,
3622 struct usb_interface *iface,
3623 const struct snd_usb_audio_quirk *quirk)
3624{
3625 typedef int (*quirk_func_t)(struct snd_usb_audio *, struct usb_interface *,
3626 const struct snd_usb_audio_quirk *);
3627 static const quirk_func_t quirk_funcs[] = {
3628 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
3629 [QUIRK_COMPOSITE] = create_composite_quirk,
3630 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
3631 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
3632 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
3633 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
3634 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
3635 [QUIRK_MIDI_FASTLANE] = create_any_midi_quirk,
3636 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
3637 [QUIRK_MIDI_CME] = create_any_midi_quirk,
3638 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
3639 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
3640 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
3641 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk
3642 };
3643
3644 if (quirk->type < QUIRK_TYPE_COUNT) {
3645 return quirk_funcs[quirk->type](chip, iface, quirk);
3646 } else {
3647 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
3648 return -ENXIO;
3649 }
3650}
3651
3652
3653/*
3654 * common proc files to show the usb device info
3655 */
3656static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
3657{
3658 struct snd_usb_audio *chip = entry->private_data;
3659 if (!chip->shutdown)
3660 snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
3661}
3662
3663static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
3664{
3665 struct snd_usb_audio *chip = entry->private_data;
3666 if (!chip->shutdown)
3667 snd_iprintf(buffer, "%04x:%04x\n",
3668 USB_ID_VENDOR(chip->usb_id),
3669 USB_ID_PRODUCT(chip->usb_id));
3670}
3671
3672static void snd_usb_audio_create_proc(struct snd_usb_audio *chip)
3673{
3674 struct snd_info_entry *entry;
3675 if (!snd_card_proc_new(chip->card, "usbbus", &entry))
3676 snd_info_set_text_ops(entry, chip, proc_audio_usbbus_read);
3677 if (!snd_card_proc_new(chip->card, "usbid", &entry))
3678 snd_info_set_text_ops(entry, chip, proc_audio_usbid_read);
3679}
3680
3681/*
3682 * free the chip instance
3683 *
3684 * here we have to do not much, since pcm and controls are already freed
3685 *
3686 */
3687
3688static int snd_usb_audio_free(struct snd_usb_audio *chip)
3689{
3690 kfree(chip);
3691 return 0;
3692}
3693
3694static int snd_usb_audio_dev_free(struct snd_device *device)
3695{
3696 struct snd_usb_audio *chip = device->device_data;
3697 return snd_usb_audio_free(chip);
3698}
3699
3700
3701/*
3702 * create a chip instance and set its names.
3703 */
3704static int snd_usb_audio_create(struct usb_device *dev, int idx,
3705 const struct snd_usb_audio_quirk *quirk,
3706 struct snd_usb_audio **rchip)
3707{
3708 struct snd_card *card;
3709 struct snd_usb_audio *chip;
3710 int err, len;
3711 char component[14];
3712 static struct snd_device_ops ops = {
3713 .dev_free = snd_usb_audio_dev_free,
3714 };
3715
3716 *rchip = NULL;
3717
3718 if (snd_usb_get_speed(dev) != USB_SPEED_LOW &&
3719 snd_usb_get_speed(dev) != USB_SPEED_FULL &&
3720 snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
3721 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
3722 return -ENXIO;
3723 }
3724
3725 err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
3726 if (err < 0) {
3727 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
3728 return err;
3729 }
3730
3731 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
3732 if (! chip) {
3733 snd_card_free(card);
3734 return -ENOMEM;
3735 }
3736
3737 chip->index = idx;
3738 chip->dev = dev;
3739 chip->card = card;
3740 chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3741 le16_to_cpu(dev->descriptor.idProduct));
3742 INIT_LIST_HEAD(&chip->pcm_list);
3743 INIT_LIST_HEAD(&chip->midi_list);
3744 INIT_LIST_HEAD(&chip->mixer_list);
3745
3746 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
3747 snd_usb_audio_free(chip);
3748 snd_card_free(card);
3749 return err;
3750 }
3751
3752 strcpy(card->driver, "USB-Audio");
3753 sprintf(component, "USB%04x:%04x",
3754 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
3755 snd_component_add(card, component);
3756
3757 /* retrieve the device string as shortname */
3758 if (quirk && quirk->product_name) {
3759 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
3760 } else {
3761 if (!dev->descriptor.iProduct ||
3762 usb_string(dev, dev->descriptor.iProduct,
3763 card->shortname, sizeof(card->shortname)) <= 0) {
3764 /* no name available from anywhere, so use ID */
3765 sprintf(card->shortname, "USB Device %#04x:%#04x",
3766 USB_ID_VENDOR(chip->usb_id),
3767 USB_ID_PRODUCT(chip->usb_id));
3768 }
3769 }
3770
3771 /* retrieve the vendor and device strings as longname */
3772 if (quirk && quirk->vendor_name) {
3773 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
3774 } else {
3775 if (dev->descriptor.iManufacturer)
3776 len = usb_string(dev, dev->descriptor.iManufacturer,
3777 card->longname, sizeof(card->longname));
3778 else
3779 len = 0;
3780 /* we don't really care if there isn't any vendor string */
3781 }
3782 if (len > 0)
3783 strlcat(card->longname, " ", sizeof(card->longname));
3784
3785 strlcat(card->longname, card->shortname, sizeof(card->longname));
3786
3787 len = strlcat(card->longname, " at ", sizeof(card->longname));
3788
3789 if (len < sizeof(card->longname))
3790 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
3791
3792 strlcat(card->longname,
3793 snd_usb_get_speed(dev) == USB_SPEED_LOW ? ", low speed" :
3794 snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" :
3795 ", high speed",
3796 sizeof(card->longname));
3797
3798 snd_usb_audio_create_proc(chip);
3799
3800 *rchip = chip;
3801 return 0;
3802}
3803
3804
3805/*
3806 * probe the active usb device
3807 *
3808 * note that this can be called multiple times per a device, when it
3809 * includes multiple audio control interfaces.
3810 *
3811 * thus we check the usb device pointer and creates the card instance
3812 * only at the first time. the successive calls of this function will
3813 * append the pcm interface to the corresponding card.
3814 */
3815static void *snd_usb_audio_probe(struct usb_device *dev,
3816 struct usb_interface *intf,
3817 const struct usb_device_id *usb_id)
3818{
3819 const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
3820 int i, err;
3821 struct snd_usb_audio *chip;
3822 struct usb_host_interface *alts;
3823 int ifnum;
3824 u32 id;
3825
3826 alts = &intf->altsetting[0];
3827 ifnum = get_iface_desc(alts)->bInterfaceNumber;
3828 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3829 le16_to_cpu(dev->descriptor.idProduct));
3830 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
3831 goto __err_val;
3832
3833 /* SB Extigy needs special boot-up sequence */
3834 /* if more models come, this will go to the quirk list. */
3835 if (id == USB_ID(0x041e, 0x3000)) {
3836 if (snd_usb_extigy_boot_quirk(dev, intf) < 0)
3837 goto __err_val;
3838 }
3839 /* SB Audigy 2 NX needs its own boot-up magic, too */
3840 if (id == USB_ID(0x041e, 0x3020)) {
3841 if (snd_usb_audigy2nx_boot_quirk(dev) < 0)
3842 goto __err_val;
3843 }
3844
3845 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
3846 if (id == USB_ID(0x10f5, 0x0200)) {
3847 if (snd_usb_cm106_boot_quirk(dev) < 0)
3848 goto __err_val;
3849 }
3850
3851 /* C-Media CM6206 / CM106-Like Sound Device */
3852 if (id == USB_ID(0x0d8c, 0x0102)) {
3853 if (snd_usb_cm6206_boot_quirk(dev) < 0)
3854 goto __err_val;
3855 }
3856
3857 /* Access Music VirusTI Desktop */
3858 if (id == USB_ID(0x133e, 0x0815)) {
3859 if (snd_usb_accessmusic_boot_quirk(dev) < 0)
3860 goto __err_val;
3861 }
3862
3863 /*
3864 * found a config. now register to ALSA
3865 */
3866
3867 /* check whether it's already registered */
3868 chip = NULL;
3869 mutex_lock(&register_mutex);
3870 for (i = 0; i < SNDRV_CARDS; i++) {
3871 if (usb_chip[i] && usb_chip[i]->dev == dev) {
3872 if (usb_chip[i]->shutdown) {
3873 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
3874 goto __error;
3875 }
3876 chip = usb_chip[i];
3877 break;
3878 }
3879 }
3880 if (! chip) {
3881 /* it's a fresh one.
3882 * now look for an empty slot and create a new card instance
3883 */
3884 for (i = 0; i < SNDRV_CARDS; i++)
3885 if (enable[i] && ! usb_chip[i] &&
3886 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
3887 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
3888 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
3889 goto __error;
3890 }
3891 snd_card_set_dev(chip->card, &intf->dev);
3892 break;
3893 }
3894 if (!chip) {
3895 printk(KERN_ERR "no available usb audio device\n");
3896 goto __error;
3897 }
3898 }
3899
3900 chip->txfr_quirk = 0;
3901 err = 1; /* continue */
3902 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
3903 /* need some special handlings */
3904 if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)
3905 goto __error;
3906 }
3907
3908 if (err > 0) {
3909 /* create normal USB audio interfaces */
3910 if (snd_usb_create_streams(chip, ifnum) < 0 ||
3911 snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
3912 goto __error;
3913 }
3914 }
3915
3916 /* we are allowed to call snd_card_register() many times */
3917 if (snd_card_register(chip->card) < 0) {
3918 goto __error;
3919 }
3920
3921 usb_chip[chip->index] = chip;
3922 chip->num_interfaces++;
3923 mutex_unlock(&register_mutex);
3924 return chip;
3925
3926 __error:
3927 if (chip && !chip->num_interfaces)
3928 snd_card_free(chip->card);
3929 mutex_unlock(&register_mutex);
3930 __err_val:
3931 return NULL;
3932}
3933
3934/*
3935 * we need to take care of counter, since disconnection can be called also
3936 * many times as well as usb_audio_probe().
3937 */
3938static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
3939{
3940 struct snd_usb_audio *chip;
3941 struct snd_card *card;
3942 struct list_head *p;
3943
3944 if (ptr == (void *)-1L)
3945 return;
3946
3947 chip = ptr;
3948 card = chip->card;
3949 mutex_lock(&register_mutex);
3950 chip->shutdown = 1;
3951 chip->num_interfaces--;
3952 if (chip->num_interfaces <= 0) {
3953 snd_card_disconnect(card);
3954 /* release the pcm resources */
3955 list_for_each(p, &chip->pcm_list) {
3956 snd_usb_stream_disconnect(p);
3957 }
3958 /* release the midi resources */
3959 list_for_each(p, &chip->midi_list) {
3960 snd_usbmidi_disconnect(p);
3961 }
3962 /* release mixer resources */
3963 list_for_each(p, &chip->mixer_list) {
3964 snd_usb_mixer_disconnect(p);
3965 }
3966 usb_chip[chip->index] = NULL;
3967 mutex_unlock(&register_mutex);
3968 snd_card_free_when_closed(card);
3969 } else {
3970 mutex_unlock(&register_mutex);
3971 }
3972}
3973
3974/*
3975 * new 2.5 USB kernel API
3976 */
3977static int usb_audio_probe(struct usb_interface *intf,
3978 const struct usb_device_id *id)
3979{
3980 void *chip;
3981 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
3982 if (chip) {
3983 usb_set_intfdata(intf, chip);
3984 return 0;
3985 } else
3986 return -EIO;
3987}
3988
3989static void usb_audio_disconnect(struct usb_interface *intf)
3990{
3991 snd_usb_audio_disconnect(interface_to_usbdev(intf),
3992 usb_get_intfdata(intf));
3993}
3994
3995#ifdef CONFIG_PM
3996static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
3997{
3998 struct snd_usb_audio *chip = usb_get_intfdata(intf);
3999 struct list_head *p;
4000 struct snd_usb_stream *as;
4001
4002 if (chip == (void *)-1L)
4003 return 0;
4004
4005 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
4006 if (!chip->num_suspended_intf++) {
4007 list_for_each(p, &chip->pcm_list) {
4008 as = list_entry(p, struct snd_usb_stream, list);
4009 snd_pcm_suspend_all(as->pcm);
4010 }
4011 }
4012
4013 return 0;
4014}
4015
4016static int usb_audio_resume(struct usb_interface *intf)
4017{
4018 struct snd_usb_audio *chip = usb_get_intfdata(intf);
4019
4020 if (chip == (void *)-1L)
4021 return 0;
4022 if (--chip->num_suspended_intf)
4023 return 0;
4024 /*
4025 * ALSA leaves material resumption to user space
4026 * we just notify
4027 */
4028
4029 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
4030
4031 return 0;
4032}
4033#endif /* CONFIG_PM */
4034
4035static int __init snd_usb_audio_init(void)
4036{
4037 if (nrpacks < 1 || nrpacks > MAX_PACKS) {
4038 printk(KERN_WARNING "invalid nrpacks value.\n");
4039 return -EINVAL;
4040 }
4041 return usb_register(&usb_audio_driver);
4042}
4043
4044
4045static void __exit snd_usb_audio_cleanup(void)
4046{
4047 usb_deregister(&usb_audio_driver);
4048}
4049
4050module_init(snd_usb_audio_init);
4051module_exit(snd_usb_audio_cleanup);
diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h
index 49a691a0b281..d679e72a3e5c 100644
--- a/sound/usb/usbaudio.h
+++ b/sound/usb/usbaudio.h
@@ -27,6 +27,7 @@
27#define USB_ID_PRODUCT(id) ((u16)(id)) 27#define USB_ID_PRODUCT(id) ((u16)(id))
28 28
29/* 29/*
30 *
30 */ 31 */
31 32
32struct snd_usb_audio { 33struct snd_usb_audio {
@@ -48,6 +49,10 @@ struct snd_usb_audio {
48 struct list_head midi_list; /* list of midi interfaces */ 49 struct list_head midi_list; /* list of midi interfaces */
49 50
50 struct list_head mixer_list; /* list of mixer interfaces */ 51 struct list_head mixer_list; /* list of mixer interfaces */
52
53 int setup; /* from the 'device_setup' module param */
54 int nrpacks; /* from the 'nrpacks' module param */
55 int async_unlink; /* from the 'async_unlink' module param */
51}; 56};
52 57
53/* 58/*
@@ -86,45 +91,8 @@ struct snd_usb_audio_quirk {
86 const void *data; 91 const void *data;
87}; 92};
88 93
89/*
90 */
91
92/*E-mu USB samplerate control quirk*/
93enum {
94 EMU_QUIRK_SR_44100HZ = 0,
95 EMU_QUIRK_SR_48000HZ,
96 EMU_QUIRK_SR_88200HZ,
97 EMU_QUIRK_SR_96000HZ,
98 EMU_QUIRK_SR_176400HZ,
99 EMU_QUIRK_SR_192000HZ
100};
101
102#define combine_word(s) ((*(s)) | ((unsigned int)(s)[1] << 8)) 94#define combine_word(s) ((*(s)) | ((unsigned int)(s)[1] << 8))
103#define combine_triple(s) (combine_word(s) | ((unsigned int)(s)[2] << 16)) 95#define combine_triple(s) (combine_word(s) | ((unsigned int)(s)[2] << 16))
104#define combine_quad(s) (combine_triple(s) | ((unsigned int)(s)[3] << 24)) 96#define combine_quad(s) (combine_triple(s) | ((unsigned int)(s)[3] << 24))
105 97
106unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size);
107
108void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype);
109void *snd_usb_find_csint_desc(void *descstart, int desclen, void *after, u8 dsubtype);
110
111int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe,
112 __u8 request, __u8 requesttype, __u16 value, __u16 index,
113 void *data, __u16 size, int timeout);
114
115/*
116 * retrieve usb_interface descriptor from the host interface
117 * (conditional for compatibility with the older API)
118 */
119#ifndef get_iface_desc
120#define get_iface_desc(iface) (&(iface)->desc)
121#define get_endpoint(alt,ep) (&(alt)->endpoint[ep].desc)
122#define get_ep_desc(ep) (&(ep)->desc)
123#define get_cfg_desc(cfg) (&(cfg)->desc)
124#endif
125
126#ifndef snd_usb_get_speed
127#define snd_usb_get_speed(dev) ((dev)->speed)
128#endif
129
130#endif /* __USBAUDIO_H */ 98#endif /* __USBAUDIO_H */
diff --git a/sound/usb/usbmixer.c b/sound/usb/usbmixer.c
index 43d53a362494..5c0568375941 100644
--- a/sound/usb/usbmixer.c
+++ b/sound/usb/usbmixer.c
@@ -42,6 +42,7 @@
42 42
43#include "usbaudio.h" 43#include "usbaudio.h"
44#include "usbmixer.h" 44#include "usbmixer.h"
45#include "helper.h"
45 46
46/* 47/*
47 */ 48 */
diff --git a/sound/usb/usx2y/us122l.c b/sound/usb/usx2y/us122l.c
index 4f6518c9b057..5f7b942ff577 100644
--- a/sound/usb/usx2y/us122l.c
+++ b/sound/usb/usx2y/us122l.c
@@ -25,7 +25,7 @@
25#define MODNAME "US122L" 25#define MODNAME "US122L"
26#include "usb_stream.c" 26#include "usb_stream.c"
27#include "../usbaudio.h" 27#include "../usbaudio.h"
28#include "../usbmidi.h" 28#include "../midi.h"
29#include "us122l.h" 29#include "us122l.h"
30 30
31MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>"); 31MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
diff --git a/sound/usb/usx2y/usbusx2y.h b/sound/usb/usx2y/usbusx2y.h
index 9ab97b40a357..e43c0a86441a 100644
--- a/sound/usb/usx2y/usbusx2y.h
+++ b/sound/usb/usx2y/usbusx2y.h
@@ -1,7 +1,7 @@
1#ifndef USBUSX2Y_H 1#ifndef USBUSX2Y_H
2#define USBUSX2Y_H 2#define USBUSX2Y_H
3#include "../usbaudio.h" 3#include "../usbaudio.h"
4#include "../usbmidi.h" 4#include "../midi.h"
5#include "usbus428ctldefs.h" 5#include "usbus428ctldefs.h"
6 6
7#define NRURBS 2 7#define NRURBS 2