[PATCH] umem: check pci_set_dma_mask return value correctly
[linux-2.6/x86.git] / sound / usb / usbaudio.c
blobd5013383fad71dec63557b5207cc38f08ba0578c
1 /*
2 * (Tentative) USB Audio Driver for ALSA
4 * Main and PCM part
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
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.
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.
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
28 * NOTES:
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).
41 #include <sound/driver.h>
42 #include <linux/bitops.h>
43 #include <linux/init.h>
44 #include <linux/list.h>
45 #include <linux/slab.h>
46 #include <linux/string.h>
47 #include <linux/usb.h>
48 #include <linux/vmalloc.h>
49 #include <linux/moduleparam.h>
50 #include <sound/core.h>
51 #include <sound/info.h>
52 #include <sound/pcm.h>
53 #include <sound/pcm_params.h>
54 #include <sound/initval.h>
56 #include "usbaudio.h"
59 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
60 MODULE_DESCRIPTION("USB Audio");
61 MODULE_LICENSE("GPL");
62 MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
65 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
66 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
67 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
68 static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Vendor ID for this card */
69 static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Product ID for this card */
70 static int nrpacks = 4; /* max. number of packets per urb */
71 static int async_unlink = 1;
73 module_param_array(index, int, NULL, 0444);
74 MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
75 module_param_array(id, charp, NULL, 0444);
76 MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
77 module_param_array(enable, bool, NULL, 0444);
78 MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
79 module_param_array(vid, int, NULL, 0444);
80 MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
81 module_param_array(pid, int, NULL, 0444);
82 MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
83 module_param(nrpacks, int, 0644);
84 MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
85 module_param(async_unlink, bool, 0444);
86 MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
90 * debug the h/w constraints
92 /* #define HW_CONST_DEBUG */
99 #define MAX_PACKS 10
100 #define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
101 #define MAX_URBS 8
102 #define SYNC_URBS 4 /* always four urbs for sync */
103 #define MIN_PACKS_URB 1 /* minimum 1 packet per urb */
105 struct audioformat {
106 struct list_head list;
107 snd_pcm_format_t format; /* format type */
108 unsigned int channels; /* # channels */
109 unsigned int fmt_type; /* USB audio format type (1-3) */
110 unsigned int frame_size; /* samples per frame for non-audio */
111 int iface; /* interface number */
112 unsigned char altsetting; /* corresponding alternate setting */
113 unsigned char altset_idx; /* array index of altenate setting */
114 unsigned char attributes; /* corresponding attributes of cs endpoint */
115 unsigned char endpoint; /* endpoint */
116 unsigned char ep_attr; /* endpoint attributes */
117 unsigned int maxpacksize; /* max. packet size */
118 unsigned int rates; /* rate bitmasks */
119 unsigned int rate_min, rate_max; /* min/max rates */
120 unsigned int nr_rates; /* number of rate table entries */
121 unsigned int *rate_table; /* rate table */
124 struct snd_usb_substream;
126 struct snd_urb_ctx {
127 struct urb *urb;
128 unsigned int buffer_size; /* size of data buffer, if data URB */
129 struct snd_usb_substream *subs;
130 int index; /* index for urb array */
131 int packets; /* number of packets per urb */
134 struct snd_urb_ops {
135 int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
136 int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
137 int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
138 int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
141 struct snd_usb_substream {
142 struct snd_usb_stream *stream;
143 struct usb_device *dev;
144 struct snd_pcm_substream *pcm_substream;
145 int direction; /* playback or capture */
146 int interface; /* current interface */
147 int endpoint; /* assigned endpoint */
148 struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
149 unsigned int cur_rate; /* current rate (for hw_params callback) */
150 unsigned int period_bytes; /* current period bytes (for hw_params callback) */
151 unsigned int format; /* USB data format */
152 unsigned int datapipe; /* the data i/o pipe */
153 unsigned int syncpipe; /* 1 - async out or adaptive in */
154 unsigned int datainterval; /* log_2 of data packet interval */
155 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
156 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
157 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
158 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
159 unsigned int phase; /* phase accumulator */
160 unsigned int maxpacksize; /* max packet size in bytes */
161 unsigned int maxframesize; /* max packet size in frames */
162 unsigned int curpacksize; /* current packet size in bytes (for capture) */
163 unsigned int curframesize; /* current packet size in frames (for capture) */
164 unsigned int fill_max: 1; /* fill max packet size always */
165 unsigned int fmt_type; /* USB audio format type (1-3) */
166 unsigned int packs_per_ms; /* packets per millisecond (for playback) */
168 unsigned int running: 1; /* running status */
170 unsigned int hwptr_done; /* processed frame position in the buffer */
171 unsigned int transfer_done; /* processed frames since last period update */
172 unsigned long active_mask; /* bitmask of active urbs */
173 unsigned long unlink_mask; /* bitmask of unlinked urbs */
175 unsigned int nurbs; /* # urbs */
176 struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */
177 struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */
178 char *syncbuf; /* sync buffer for all sync URBs */
179 dma_addr_t sync_dma; /* DMA address of syncbuf */
181 u64 formats; /* format bitmasks (all or'ed) */
182 unsigned int num_formats; /* number of supported audio formats (list) */
183 struct list_head fmt_list; /* format list */
184 spinlock_t lock;
186 struct snd_urb_ops ops; /* callbacks (must be filled at init) */
190 struct snd_usb_stream {
191 struct snd_usb_audio *chip;
192 struct snd_pcm *pcm;
193 int pcm_index;
194 unsigned int fmt_type; /* USB audio format type (1-3) */
195 struct snd_usb_substream substream[2];
196 struct list_head list;
201 * we keep the snd_usb_audio_t instances by ourselves for merging
202 * the all interfaces on the same card as one sound device.
205 static DECLARE_MUTEX(register_mutex);
206 static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
210 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
211 * this will overflow at approx 524 kHz
213 static inline unsigned get_usb_full_speed_rate(unsigned int rate)
215 return ((rate << 13) + 62) / 125;
219 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
220 * this will overflow at approx 4 MHz
222 static inline unsigned get_usb_high_speed_rate(unsigned int rate)
224 return ((rate << 10) + 62) / 125;
227 /* convert our full speed USB rate into sampling rate in Hz */
228 static inline unsigned get_full_speed_hz(unsigned int usb_rate)
230 return (usb_rate * 125 + (1 << 12)) >> 13;
233 /* convert our high speed USB rate into sampling rate in Hz */
234 static inline unsigned get_high_speed_hz(unsigned int usb_rate)
236 return (usb_rate * 125 + (1 << 9)) >> 10;
241 * prepare urb for full speed capture sync pipe
243 * fill the length and offset of each urb descriptor.
244 * the fixed 10.14 frequency is passed through the pipe.
246 static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
247 struct snd_pcm_runtime *runtime,
248 struct urb *urb)
250 unsigned char *cp = urb->transfer_buffer;
251 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
253 urb->dev = ctx->subs->dev; /* we need to set this at each time */
254 urb->iso_frame_desc[0].length = 3;
255 urb->iso_frame_desc[0].offset = 0;
256 cp[0] = subs->freqn >> 2;
257 cp[1] = subs->freqn >> 10;
258 cp[2] = subs->freqn >> 18;
259 return 0;
263 * prepare urb for high speed capture sync pipe
265 * fill the length and offset of each urb descriptor.
266 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
268 static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
269 struct snd_pcm_runtime *runtime,
270 struct urb *urb)
272 unsigned char *cp = urb->transfer_buffer;
273 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
275 urb->dev = ctx->subs->dev; /* we need to set this at each time */
276 urb->iso_frame_desc[0].length = 4;
277 urb->iso_frame_desc[0].offset = 0;
278 cp[0] = subs->freqn;
279 cp[1] = subs->freqn >> 8;
280 cp[2] = subs->freqn >> 16;
281 cp[3] = subs->freqn >> 24;
282 return 0;
286 * process after capture sync complete
287 * - nothing to do
289 static int retire_capture_sync_urb(struct snd_usb_substream *subs,
290 struct snd_pcm_runtime *runtime,
291 struct urb *urb)
293 return 0;
297 * prepare urb for capture data pipe
299 * fill the offset and length of each descriptor.
301 * we use a temporary buffer to write the captured data.
302 * since the length of written data is determined by host, we cannot
303 * write onto the pcm buffer directly... the data is thus copied
304 * later at complete callback to the global buffer.
306 static int prepare_capture_urb(struct snd_usb_substream *subs,
307 struct snd_pcm_runtime *runtime,
308 struct urb *urb)
310 int i, offs;
311 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
313 offs = 0;
314 urb->dev = ctx->subs->dev; /* we need to set this at each time */
315 for (i = 0; i < ctx->packets; i++) {
316 urb->iso_frame_desc[i].offset = offs;
317 urb->iso_frame_desc[i].length = subs->curpacksize;
318 offs += subs->curpacksize;
320 urb->transfer_buffer_length = offs;
321 urb->number_of_packets = ctx->packets;
322 #if 0 // for check
323 if (! urb->bandwidth) {
324 int bustime;
325 bustime = usb_check_bandwidth(urb->dev, urb);
326 if (bustime < 0)
327 return bustime;
328 printk("urb %d: bandwidth = %d (packets = %d)\n", ctx->index, bustime, urb->number_of_packets);
329 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
331 #endif // for check
332 return 0;
336 * process after capture complete
338 * copy the data from each desctiptor to the pcm buffer, and
339 * update the current position.
341 static int retire_capture_urb(struct snd_usb_substream *subs,
342 struct snd_pcm_runtime *runtime,
343 struct urb *urb)
345 unsigned long flags;
346 unsigned char *cp;
347 int i;
348 unsigned int stride, len, oldptr;
349 int period_elapsed = 0;
351 stride = runtime->frame_bits >> 3;
353 for (i = 0; i < urb->number_of_packets; i++) {
354 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
355 if (urb->iso_frame_desc[i].status) {
356 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
357 // continue;
359 len = urb->iso_frame_desc[i].actual_length / stride;
360 if (! len)
361 continue;
362 /* update the current pointer */
363 spin_lock_irqsave(&subs->lock, flags);
364 oldptr = subs->hwptr_done;
365 subs->hwptr_done += len;
366 if (subs->hwptr_done >= runtime->buffer_size)
367 subs->hwptr_done -= runtime->buffer_size;
368 subs->transfer_done += len;
369 if (subs->transfer_done >= runtime->period_size) {
370 subs->transfer_done -= runtime->period_size;
371 period_elapsed = 1;
373 spin_unlock_irqrestore(&subs->lock, flags);
374 /* copy a data chunk */
375 if (oldptr + len > runtime->buffer_size) {
376 unsigned int cnt = runtime->buffer_size - oldptr;
377 unsigned int blen = cnt * stride;
378 memcpy(runtime->dma_area + oldptr * stride, cp, blen);
379 memcpy(runtime->dma_area, cp + blen, len * stride - blen);
380 } else {
381 memcpy(runtime->dma_area + oldptr * stride, cp, len * stride);
384 if (period_elapsed)
385 snd_pcm_period_elapsed(subs->pcm_substream);
386 return 0;
391 * prepare urb for full speed playback sync pipe
393 * set up the offset and length to receive the current frequency.
396 static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
397 struct snd_pcm_runtime *runtime,
398 struct urb *urb)
400 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
402 urb->dev = ctx->subs->dev; /* we need to set this at each time */
403 urb->iso_frame_desc[0].length = 3;
404 urb->iso_frame_desc[0].offset = 0;
405 return 0;
409 * prepare urb for high speed playback sync pipe
411 * set up the offset and length to receive the current frequency.
414 static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
415 struct snd_pcm_runtime *runtime,
416 struct urb *urb)
418 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
420 urb->dev = ctx->subs->dev; /* we need to set this at each time */
421 urb->iso_frame_desc[0].length = 4;
422 urb->iso_frame_desc[0].offset = 0;
423 return 0;
427 * process after full speed playback sync complete
429 * retrieve the current 10.14 frequency from pipe, and set it.
430 * the value is referred in prepare_playback_urb().
432 static int retire_playback_sync_urb(struct snd_usb_substream *subs,
433 struct snd_pcm_runtime *runtime,
434 struct urb *urb)
436 unsigned int f;
437 unsigned long flags;
439 if (urb->iso_frame_desc[0].status == 0 &&
440 urb->iso_frame_desc[0].actual_length == 3) {
441 f = combine_triple((u8*)urb->transfer_buffer) << 2;
442 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
443 spin_lock_irqsave(&subs->lock, flags);
444 subs->freqm = f;
445 spin_unlock_irqrestore(&subs->lock, flags);
449 return 0;
453 * process after high speed playback sync complete
455 * retrieve the current 12.13 frequency from pipe, and set it.
456 * the value is referred in prepare_playback_urb().
458 static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
459 struct snd_pcm_runtime *runtime,
460 struct urb *urb)
462 unsigned int f;
463 unsigned long flags;
465 if (urb->iso_frame_desc[0].status == 0 &&
466 urb->iso_frame_desc[0].actual_length == 4) {
467 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
468 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
469 spin_lock_irqsave(&subs->lock, flags);
470 subs->freqm = f;
471 spin_unlock_irqrestore(&subs->lock, flags);
475 return 0;
479 * Prepare urb for streaming before playback starts.
481 * We don't yet have data, so we send a frame of silence.
483 static int prepare_startup_playback_urb(struct snd_usb_substream *subs,
484 struct snd_pcm_runtime *runtime,
485 struct urb *urb)
487 unsigned int i, offs, counts;
488 struct snd_urb_ctx *ctx = urb->context;
489 int stride = runtime->frame_bits >> 3;
491 offs = 0;
492 urb->dev = ctx->subs->dev;
493 urb->number_of_packets = subs->packs_per_ms;
494 for (i = 0; i < subs->packs_per_ms; ++i) {
495 /* calculate the size of a packet */
496 if (subs->fill_max)
497 counts = subs->maxframesize; /* fixed */
498 else {
499 subs->phase = (subs->phase & 0xffff)
500 + (subs->freqm << subs->datainterval);
501 counts = subs->phase >> 16;
502 if (counts > subs->maxframesize)
503 counts = subs->maxframesize;
505 urb->iso_frame_desc[i].offset = offs * stride;
506 urb->iso_frame_desc[i].length = counts * stride;
507 offs += counts;
509 urb->transfer_buffer_length = offs * stride;
510 memset(urb->transfer_buffer,
511 subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
512 offs * stride);
513 return 0;
517 * prepare urb for playback data pipe
519 * Since a URB can handle only a single linear buffer, we must use double
520 * buffering when the data to be transferred overflows the buffer boundary.
521 * To avoid inconsistencies when updating hwptr_done, we use double buffering
522 * for all URBs.
524 static int prepare_playback_urb(struct snd_usb_substream *subs,
525 struct snd_pcm_runtime *runtime,
526 struct urb *urb)
528 int i, stride, offs;
529 unsigned int counts;
530 unsigned long flags;
531 int period_elapsed = 0;
532 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
534 stride = runtime->frame_bits >> 3;
536 offs = 0;
537 urb->dev = ctx->subs->dev; /* we need to set this at each time */
538 urb->number_of_packets = 0;
539 spin_lock_irqsave(&subs->lock, flags);
540 for (i = 0; i < ctx->packets; i++) {
541 /* calculate the size of a packet */
542 if (subs->fill_max)
543 counts = subs->maxframesize; /* fixed */
544 else {
545 subs->phase = (subs->phase & 0xffff)
546 + (subs->freqm << subs->datainterval);
547 counts = subs->phase >> 16;
548 if (counts > subs->maxframesize)
549 counts = subs->maxframesize;
551 /* set up descriptor */
552 urb->iso_frame_desc[i].offset = offs * stride;
553 urb->iso_frame_desc[i].length = counts * stride;
554 offs += counts;
555 urb->number_of_packets++;
556 subs->transfer_done += counts;
557 if (subs->transfer_done >= runtime->period_size) {
558 subs->transfer_done -= runtime->period_size;
559 period_elapsed = 1;
560 if (subs->fmt_type == USB_FORMAT_TYPE_II) {
561 if (subs->transfer_done > 0) {
562 /* FIXME: fill-max mode is not
563 * supported yet */
564 offs -= subs->transfer_done;
565 counts -= subs->transfer_done;
566 urb->iso_frame_desc[i].length =
567 counts * stride;
568 subs->transfer_done = 0;
570 i++;
571 if (i < ctx->packets) {
572 /* add a transfer delimiter */
573 urb->iso_frame_desc[i].offset =
574 offs * stride;
575 urb->iso_frame_desc[i].length = 0;
576 urb->number_of_packets++;
578 break;
581 /* finish at the frame boundary at/after the period boundary */
582 if (period_elapsed &&
583 (i & (subs->packs_per_ms - 1)) == subs->packs_per_ms - 1)
584 break;
586 if (subs->hwptr_done + offs > runtime->buffer_size) {
587 /* err, the transferred area goes over buffer boundary. */
588 unsigned int len = runtime->buffer_size - subs->hwptr_done;
589 memcpy(urb->transfer_buffer,
590 runtime->dma_area + subs->hwptr_done * stride,
591 len * stride);
592 memcpy(urb->transfer_buffer + len * stride,
593 runtime->dma_area,
594 (offs - len) * stride);
595 } else {
596 memcpy(urb->transfer_buffer,
597 runtime->dma_area + subs->hwptr_done * stride,
598 offs * stride);
600 subs->hwptr_done += offs;
601 if (subs->hwptr_done >= runtime->buffer_size)
602 subs->hwptr_done -= runtime->buffer_size;
603 spin_unlock_irqrestore(&subs->lock, flags);
604 urb->transfer_buffer_length = offs * stride;
605 if (period_elapsed)
606 snd_pcm_period_elapsed(subs->pcm_substream);
607 return 0;
611 * process after playback data complete
612 * - nothing to do
614 static int retire_playback_urb(struct snd_usb_substream *subs,
615 struct snd_pcm_runtime *runtime,
616 struct urb *urb)
618 return 0;
624 static struct snd_urb_ops audio_urb_ops[2] = {
626 .prepare = prepare_startup_playback_urb,
627 .retire = retire_playback_urb,
628 .prepare_sync = prepare_playback_sync_urb,
629 .retire_sync = retire_playback_sync_urb,
632 .prepare = prepare_capture_urb,
633 .retire = retire_capture_urb,
634 .prepare_sync = prepare_capture_sync_urb,
635 .retire_sync = retire_capture_sync_urb,
639 static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
641 .prepare = prepare_startup_playback_urb,
642 .retire = retire_playback_urb,
643 .prepare_sync = prepare_playback_sync_urb_hs,
644 .retire_sync = retire_playback_sync_urb_hs,
647 .prepare = prepare_capture_urb,
648 .retire = retire_capture_urb,
649 .prepare_sync = prepare_capture_sync_urb_hs,
650 .retire_sync = retire_capture_sync_urb,
655 * complete callback from data urb
657 static void snd_complete_urb(struct urb *urb, struct pt_regs *regs)
659 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
660 struct snd_usb_substream *subs = ctx->subs;
661 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
662 int err = 0;
664 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
665 ! subs->running || /* can be stopped during retire callback */
666 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
667 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
668 clear_bit(ctx->index, &subs->active_mask);
669 if (err < 0) {
670 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
671 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
678 * complete callback from sync urb
680 static void snd_complete_sync_urb(struct urb *urb, struct pt_regs *regs)
682 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
683 struct snd_usb_substream *subs = ctx->subs;
684 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
685 int err = 0;
687 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
688 ! subs->running || /* can be stopped during retire callback */
689 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
690 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
691 clear_bit(ctx->index + 16, &subs->active_mask);
692 if (err < 0) {
693 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
694 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
700 /* get the physical page pointer at the given offset */
701 static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
702 unsigned long offset)
704 void *pageptr = subs->runtime->dma_area + offset;
705 return vmalloc_to_page(pageptr);
708 /* allocate virtual buffer; may be called more than once */
709 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, size_t size)
711 struct snd_pcm_runtime *runtime = subs->runtime;
712 if (runtime->dma_area) {
713 if (runtime->dma_bytes >= size)
714 return 0; /* already large enough */
715 vfree(runtime->dma_area);
717 runtime->dma_area = vmalloc(size);
718 if (! runtime->dma_area)
719 return -ENOMEM;
720 runtime->dma_bytes = size;
721 return 0;
724 /* free virtual buffer; may be called more than once */
725 static int snd_pcm_free_vmalloc_buffer(struct snd_pcm_substream *subs)
727 struct snd_pcm_runtime *runtime = subs->runtime;
728 if (runtime->dma_area) {
729 vfree(runtime->dma_area);
730 runtime->dma_area = NULL;
732 return 0;
737 * unlink active urbs.
739 static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
741 unsigned int i;
742 int async;
744 subs->running = 0;
746 if (!force && subs->stream->chip->shutdown) /* to be sure... */
747 return -EBADFD;
749 async = !can_sleep && async_unlink;
751 if (! async && in_interrupt())
752 return 0;
754 for (i = 0; i < subs->nurbs; i++) {
755 if (test_bit(i, &subs->active_mask)) {
756 if (! test_and_set_bit(i, &subs->unlink_mask)) {
757 struct urb *u = subs->dataurb[i].urb;
758 if (async)
759 usb_unlink_urb(u);
760 else
761 usb_kill_urb(u);
765 if (subs->syncpipe) {
766 for (i = 0; i < SYNC_URBS; i++) {
767 if (test_bit(i+16, &subs->active_mask)) {
768 if (! test_and_set_bit(i+16, &subs->unlink_mask)) {
769 struct urb *u = subs->syncurb[i].urb;
770 if (async)
771 usb_unlink_urb(u);
772 else
773 usb_kill_urb(u);
778 return 0;
783 * set up and start data/sync urbs
785 static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
787 unsigned int i;
788 int err;
790 if (subs->stream->chip->shutdown)
791 return -EBADFD;
793 for (i = 0; i < subs->nurbs; i++) {
794 snd_assert(subs->dataurb[i].urb, return -EINVAL);
795 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
796 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
797 goto __error;
800 if (subs->syncpipe) {
801 for (i = 0; i < SYNC_URBS; i++) {
802 snd_assert(subs->syncurb[i].urb, return -EINVAL);
803 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
804 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
805 goto __error;
810 subs->active_mask = 0;
811 subs->unlink_mask = 0;
812 subs->running = 1;
813 for (i = 0; i < subs->nurbs; i++) {
814 if ((err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC)) < 0) {
815 snd_printk(KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
816 goto __error;
818 set_bit(i, &subs->active_mask);
820 if (subs->syncpipe) {
821 for (i = 0; i < SYNC_URBS; i++) {
822 if ((err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC)) < 0) {
823 snd_printk(KERN_ERR "cannot submit syncpipe for urb %d, err = %d\n", i, err);
824 goto __error;
826 set_bit(i + 16, &subs->active_mask);
829 return 0;
831 __error:
832 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
833 deactivate_urbs(subs, 0, 0);
834 return -EPIPE;
839 * wait until all urbs are processed.
841 static int wait_clear_urbs(struct snd_usb_substream *subs)
843 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
844 unsigned int i;
845 int alive;
847 do {
848 alive = 0;
849 for (i = 0; i < subs->nurbs; i++) {
850 if (test_bit(i, &subs->active_mask))
851 alive++;
853 if (subs->syncpipe) {
854 for (i = 0; i < SYNC_URBS; i++) {
855 if (test_bit(i + 16, &subs->active_mask))
856 alive++;
859 if (! alive)
860 break;
861 schedule_timeout_uninterruptible(1);
862 } while (time_before(jiffies, end_time));
863 if (alive)
864 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
865 return 0;
870 * return the current pcm pointer. just return the hwptr_done value.
872 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
874 struct snd_usb_substream *subs;
875 snd_pcm_uframes_t hwptr_done;
877 subs = (struct snd_usb_substream *)substream->runtime->private_data;
878 spin_lock(&subs->lock);
879 hwptr_done = subs->hwptr_done;
880 spin_unlock(&subs->lock);
881 return hwptr_done;
886 * start/stop playback substream
888 static int snd_usb_pcm_playback_trigger(struct snd_pcm_substream *substream,
889 int cmd)
891 struct snd_usb_substream *subs = substream->runtime->private_data;
893 switch (cmd) {
894 case SNDRV_PCM_TRIGGER_START:
895 subs->ops.prepare = prepare_playback_urb;
896 return 0;
897 case SNDRV_PCM_TRIGGER_STOP:
898 return deactivate_urbs(subs, 0, 0);
899 default:
900 return -EINVAL;
905 * start/stop capture substream
907 static int snd_usb_pcm_capture_trigger(struct snd_pcm_substream *substream,
908 int cmd)
910 struct snd_usb_substream *subs = substream->runtime->private_data;
912 switch (cmd) {
913 case SNDRV_PCM_TRIGGER_START:
914 return start_urbs(subs, substream->runtime);
915 case SNDRV_PCM_TRIGGER_STOP:
916 return deactivate_urbs(subs, 0, 0);
917 default:
918 return -EINVAL;
924 * release a urb data
926 static void release_urb_ctx(struct snd_urb_ctx *u)
928 if (u->urb) {
929 if (u->buffer_size)
930 usb_buffer_free(u->subs->dev, u->buffer_size,
931 u->urb->transfer_buffer,
932 u->urb->transfer_dma);
933 usb_free_urb(u->urb);
934 u->urb = NULL;
939 * release a substream
941 static void release_substream_urbs(struct snd_usb_substream *subs, int force)
943 int i;
945 /* stop urbs (to be sure) */
946 deactivate_urbs(subs, force, 1);
947 wait_clear_urbs(subs);
949 for (i = 0; i < MAX_URBS; i++)
950 release_urb_ctx(&subs->dataurb[i]);
951 for (i = 0; i < SYNC_URBS; i++)
952 release_urb_ctx(&subs->syncurb[i]);
953 usb_buffer_free(subs->dev, SYNC_URBS * 4,
954 subs->syncbuf, subs->sync_dma);
955 subs->syncbuf = NULL;
956 subs->nurbs = 0;
960 * initialize a substream for plaback/capture
962 static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes,
963 unsigned int rate, unsigned int frame_bits)
965 unsigned int maxsize, n, i;
966 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
967 unsigned int npacks[MAX_URBS], urb_packs, total_packs, packs_per_ms;
969 /* calculate the frequency in 16.16 format */
970 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
971 subs->freqn = get_usb_full_speed_rate(rate);
972 else
973 subs->freqn = get_usb_high_speed_rate(rate);
974 subs->freqm = subs->freqn;
975 /* calculate max. frequency */
976 if (subs->maxpacksize) {
977 /* whatever fits into a max. size packet */
978 maxsize = subs->maxpacksize;
979 subs->freqmax = (maxsize / (frame_bits >> 3))
980 << (16 - subs->datainterval);
981 } else {
982 /* no max. packet size: just take 25% higher than nominal */
983 subs->freqmax = subs->freqn + (subs->freqn >> 2);
984 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
985 >> (16 - subs->datainterval);
987 subs->phase = 0;
989 if (subs->fill_max)
990 subs->curpacksize = subs->maxpacksize;
991 else
992 subs->curpacksize = maxsize;
994 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
995 packs_per_ms = 8 >> subs->datainterval;
996 else
997 packs_per_ms = 1;
998 subs->packs_per_ms = packs_per_ms;
1000 if (is_playback) {
1001 urb_packs = nrpacks;
1002 urb_packs = max(urb_packs, (unsigned int)MIN_PACKS_URB);
1003 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
1004 } else
1005 urb_packs = 1;
1006 urb_packs *= packs_per_ms;
1008 /* decide how many packets to be used */
1009 if (is_playback) {
1010 unsigned int minsize;
1011 /* determine how small a packet can be */
1012 minsize = (subs->freqn >> (16 - subs->datainterval))
1013 * (frame_bits >> 3);
1014 /* with sync from device, assume it can be 12% lower */
1015 if (subs->syncpipe)
1016 minsize -= minsize >> 3;
1017 minsize = max(minsize, 1u);
1018 total_packs = (period_bytes + minsize - 1) / minsize;
1019 /* round up to multiple of packs_per_ms */
1020 total_packs = (total_packs + packs_per_ms - 1)
1021 & ~(packs_per_ms - 1);
1022 /* we need at least two URBs for queueing */
1023 if (total_packs < 2 * MIN_PACKS_URB * packs_per_ms)
1024 total_packs = 2 * MIN_PACKS_URB * packs_per_ms;
1025 } else {
1026 total_packs = MAX_URBS * urb_packs;
1028 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
1029 if (subs->nurbs > MAX_URBS) {
1030 /* too much... */
1031 subs->nurbs = MAX_URBS;
1032 total_packs = MAX_URBS * urb_packs;
1034 n = total_packs;
1035 for (i = 0; i < subs->nurbs; i++) {
1036 npacks[i] = n > urb_packs ? urb_packs : n;
1037 n -= urb_packs;
1039 if (subs->nurbs <= 1) {
1040 /* too little - we need at least two packets
1041 * to ensure contiguous playback/capture
1043 subs->nurbs = 2;
1044 npacks[0] = (total_packs + 1) / 2;
1045 npacks[1] = total_packs - npacks[0];
1046 } else if (npacks[subs->nurbs-1] < MIN_PACKS_URB * packs_per_ms) {
1047 /* the last packet is too small.. */
1048 if (subs->nurbs > 2) {
1049 /* merge to the first one */
1050 npacks[0] += npacks[subs->nurbs - 1];
1051 subs->nurbs--;
1052 } else {
1053 /* divide to two */
1054 subs->nurbs = 2;
1055 npacks[0] = (total_packs + 1) / 2;
1056 npacks[1] = total_packs - npacks[0];
1060 /* allocate and initialize data urbs */
1061 for (i = 0; i < subs->nurbs; i++) {
1062 struct snd_urb_ctx *u = &subs->dataurb[i];
1063 u->index = i;
1064 u->subs = subs;
1065 u->packets = npacks[i];
1066 u->buffer_size = maxsize * u->packets;
1067 if (subs->fmt_type == USB_FORMAT_TYPE_II)
1068 u->packets++; /* for transfer delimiter */
1069 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1070 if (! u->urb)
1071 goto out_of_memory;
1072 u->urb->transfer_buffer =
1073 usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL,
1074 &u->urb->transfer_dma);
1075 if (! u->urb->transfer_buffer)
1076 goto out_of_memory;
1077 u->urb->pipe = subs->datapipe;
1078 u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1079 u->urb->interval = 1 << subs->datainterval;
1080 u->urb->context = u;
1081 u->urb->complete = snd_complete_urb;
1084 if (subs->syncpipe) {
1085 /* allocate and initialize sync urbs */
1086 subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4,
1087 GFP_KERNEL, &subs->sync_dma);
1088 if (! subs->syncbuf)
1089 goto out_of_memory;
1090 for (i = 0; i < SYNC_URBS; i++) {
1091 struct snd_urb_ctx *u = &subs->syncurb[i];
1092 u->index = i;
1093 u->subs = subs;
1094 u->packets = 1;
1095 u->urb = usb_alloc_urb(1, GFP_KERNEL);
1096 if (! u->urb)
1097 goto out_of_memory;
1098 u->urb->transfer_buffer = subs->syncbuf + i * 4;
1099 u->urb->transfer_dma = subs->sync_dma + i * 4;
1100 u->urb->transfer_buffer_length = 4;
1101 u->urb->pipe = subs->syncpipe;
1102 u->urb->transfer_flags = URB_ISO_ASAP |
1103 URB_NO_TRANSFER_DMA_MAP;
1104 u->urb->number_of_packets = 1;
1105 u->urb->interval = 1 << subs->syncinterval;
1106 u->urb->context = u;
1107 u->urb->complete = snd_complete_sync_urb;
1110 return 0;
1112 out_of_memory:
1113 release_substream_urbs(subs, 0);
1114 return -ENOMEM;
1119 * find a matching audio format
1121 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
1122 unsigned int rate, unsigned int channels)
1124 struct list_head *p;
1125 struct audioformat *found = NULL;
1126 int cur_attr = 0, attr;
1128 list_for_each(p, &subs->fmt_list) {
1129 struct audioformat *fp;
1130 fp = list_entry(p, struct audioformat, list);
1131 if (fp->format != format || fp->channels != channels)
1132 continue;
1133 if (rate < fp->rate_min || rate > fp->rate_max)
1134 continue;
1135 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
1136 unsigned int i;
1137 for (i = 0; i < fp->nr_rates; i++)
1138 if (fp->rate_table[i] == rate)
1139 break;
1140 if (i >= fp->nr_rates)
1141 continue;
1143 attr = fp->ep_attr & EP_ATTR_MASK;
1144 if (! found) {
1145 found = fp;
1146 cur_attr = attr;
1147 continue;
1149 /* avoid async out and adaptive in if the other method
1150 * supports the same format.
1151 * this is a workaround for the case like
1152 * M-audio audiophile USB.
1154 if (attr != cur_attr) {
1155 if ((attr == EP_ATTR_ASYNC &&
1156 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1157 (attr == EP_ATTR_ADAPTIVE &&
1158 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
1159 continue;
1160 if ((cur_attr == EP_ATTR_ASYNC &&
1161 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1162 (cur_attr == EP_ATTR_ADAPTIVE &&
1163 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
1164 found = fp;
1165 cur_attr = attr;
1166 continue;
1169 /* find the format with the largest max. packet size */
1170 if (fp->maxpacksize > found->maxpacksize) {
1171 found = fp;
1172 cur_attr = attr;
1175 return found;
1180 * initialize the picth control and sample rate
1182 static int init_usb_pitch(struct usb_device *dev, int iface,
1183 struct usb_host_interface *alts,
1184 struct audioformat *fmt)
1186 unsigned int ep;
1187 unsigned char data[1];
1188 int err;
1190 ep = get_endpoint(alts, 0)->bEndpointAddress;
1191 /* if endpoint has pitch control, enable it */
1192 if (fmt->attributes & EP_CS_ATTR_PITCH_CONTROL) {
1193 data[0] = 1;
1194 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1195 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1196 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1197 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
1198 dev->devnum, iface, ep);
1199 return err;
1202 return 0;
1205 static int init_usb_sample_rate(struct usb_device *dev, int iface,
1206 struct usb_host_interface *alts,
1207 struct audioformat *fmt, int rate)
1209 unsigned int ep;
1210 unsigned char data[3];
1211 int err;
1213 ep = get_endpoint(alts, 0)->bEndpointAddress;
1214 /* if endpoint has sampling rate control, set it */
1215 if (fmt->attributes & EP_CS_ATTR_SAMPLE_RATE) {
1216 int crate;
1217 data[0] = rate;
1218 data[1] = rate >> 8;
1219 data[2] = rate >> 16;
1220 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1221 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1222 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1223 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep 0x%x\n",
1224 dev->devnum, iface, fmt->altsetting, rate, ep);
1225 return err;
1227 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR,
1228 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1229 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1230 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep 0x%x\n",
1231 dev->devnum, iface, fmt->altsetting, ep);
1232 return 0; /* some devices don't support reading */
1234 crate = data[0] | (data[1] << 8) | (data[2] << 16);
1235 if (crate != rate) {
1236 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
1237 // runtime->rate = crate;
1240 return 0;
1244 * find a matching format and set up the interface
1246 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
1248 struct usb_device *dev = subs->dev;
1249 struct usb_host_interface *alts;
1250 struct usb_interface_descriptor *altsd;
1251 struct usb_interface *iface;
1252 unsigned int ep, attr;
1253 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1254 int err;
1256 iface = usb_ifnum_to_if(dev, fmt->iface);
1257 snd_assert(iface, return -EINVAL);
1258 alts = &iface->altsetting[fmt->altset_idx];
1259 altsd = get_iface_desc(alts);
1260 snd_assert(altsd->bAlternateSetting == fmt->altsetting, return -EINVAL);
1262 if (fmt == subs->cur_audiofmt)
1263 return 0;
1265 /* close the old interface */
1266 if (subs->interface >= 0 && subs->interface != fmt->iface) {
1267 usb_set_interface(subs->dev, subs->interface, 0);
1268 subs->interface = -1;
1269 subs->format = 0;
1272 /* set interface */
1273 if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
1274 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
1275 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
1276 dev->devnum, fmt->iface, fmt->altsetting);
1277 return -EIO;
1279 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
1280 subs->interface = fmt->iface;
1281 subs->format = fmt->altset_idx;
1284 /* create a data pipe */
1285 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
1286 if (is_playback)
1287 subs->datapipe = usb_sndisocpipe(dev, ep);
1288 else
1289 subs->datapipe = usb_rcvisocpipe(dev, ep);
1290 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH &&
1291 get_endpoint(alts, 0)->bInterval >= 1 &&
1292 get_endpoint(alts, 0)->bInterval <= 4)
1293 subs->datainterval = get_endpoint(alts, 0)->bInterval - 1;
1294 else
1295 subs->datainterval = 0;
1296 subs->syncpipe = subs->syncinterval = 0;
1297 subs->maxpacksize = fmt->maxpacksize;
1298 subs->fill_max = 0;
1300 /* we need a sync pipe in async OUT or adaptive IN mode */
1301 /* check the number of EP, since some devices have broken
1302 * descriptors which fool us. if it has only one EP,
1303 * assume it as adaptive-out or sync-in.
1305 attr = fmt->ep_attr & EP_ATTR_MASK;
1306 if (((is_playback && attr == EP_ATTR_ASYNC) ||
1307 (! is_playback && attr == EP_ATTR_ADAPTIVE)) &&
1308 altsd->bNumEndpoints >= 2) {
1309 /* check sync-pipe endpoint */
1310 /* ... and check descriptor size before accessing bSynchAddress
1311 because there is a version of the SB Audigy 2 NX firmware lacking
1312 the audio fields in the endpoint descriptors */
1313 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
1314 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1315 get_endpoint(alts, 1)->bSynchAddress != 0)) {
1316 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1317 dev->devnum, fmt->iface, fmt->altsetting);
1318 return -EINVAL;
1320 ep = get_endpoint(alts, 1)->bEndpointAddress;
1321 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1322 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
1323 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
1324 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1325 dev->devnum, fmt->iface, fmt->altsetting);
1326 return -EINVAL;
1328 ep &= USB_ENDPOINT_NUMBER_MASK;
1329 if (is_playback)
1330 subs->syncpipe = usb_rcvisocpipe(dev, ep);
1331 else
1332 subs->syncpipe = usb_sndisocpipe(dev, ep);
1333 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1334 get_endpoint(alts, 1)->bRefresh >= 1 &&
1335 get_endpoint(alts, 1)->bRefresh <= 9)
1336 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
1337 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
1338 subs->syncinterval = 1;
1339 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
1340 get_endpoint(alts, 1)->bInterval <= 16)
1341 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
1342 else
1343 subs->syncinterval = 3;
1346 /* always fill max packet size */
1347 if (fmt->attributes & EP_CS_ATTR_FILL_MAX)
1348 subs->fill_max = 1;
1350 if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
1351 return err;
1353 subs->cur_audiofmt = fmt;
1355 #if 0
1356 printk("setting done: format = %d, rate = %d, channels = %d\n",
1357 fmt->format, fmt->rate, fmt->channels);
1358 printk(" datapipe = 0x%0x, syncpipe = 0x%0x\n",
1359 subs->datapipe, subs->syncpipe);
1360 #endif
1362 return 0;
1366 * hw_params callback
1368 * allocate a buffer and set the given audio format.
1370 * so far we use a physically linear buffer although packetize transfer
1371 * doesn't need a continuous area.
1372 * if sg buffer is supported on the later version of alsa, we'll follow
1373 * that.
1375 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
1376 struct snd_pcm_hw_params *hw_params)
1378 struct snd_usb_substream *subs = (struct snd_usb_substream *)substream->runtime->private_data;
1379 struct audioformat *fmt;
1380 unsigned int channels, rate, format;
1381 int ret, changed;
1383 ret = snd_pcm_alloc_vmalloc_buffer(substream,
1384 params_buffer_bytes(hw_params));
1385 if (ret < 0)
1386 return ret;
1388 format = params_format(hw_params);
1389 rate = params_rate(hw_params);
1390 channels = params_channels(hw_params);
1391 fmt = find_format(subs, format, rate, channels);
1392 if (! fmt) {
1393 snd_printd(KERN_DEBUG "cannot set format: format = %s, rate = %d, channels = %d\n",
1394 snd_pcm_format_name(format), rate, channels);
1395 return -EINVAL;
1398 changed = subs->cur_audiofmt != fmt ||
1399 subs->period_bytes != params_period_bytes(hw_params) ||
1400 subs->cur_rate != rate;
1401 if ((ret = set_format(subs, fmt)) < 0)
1402 return ret;
1404 if (subs->cur_rate != rate) {
1405 struct usb_host_interface *alts;
1406 struct usb_interface *iface;
1407 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
1408 alts = &iface->altsetting[fmt->altset_idx];
1409 ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
1410 if (ret < 0)
1411 return ret;
1412 subs->cur_rate = rate;
1415 if (changed) {
1416 /* format changed */
1417 release_substream_urbs(subs, 0);
1418 /* influenced: period_bytes, channels, rate, format, */
1419 ret = init_substream_urbs(subs, params_period_bytes(hw_params),
1420 params_rate(hw_params),
1421 snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
1424 return ret;
1428 * hw_free callback
1430 * reset the audio format and release the buffer
1432 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
1434 struct snd_usb_substream *subs = (struct snd_usb_substream *)substream->runtime->private_data;
1436 subs->cur_audiofmt = NULL;
1437 subs->cur_rate = 0;
1438 subs->period_bytes = 0;
1439 release_substream_urbs(subs, 0);
1440 return snd_pcm_free_vmalloc_buffer(substream);
1444 * prepare callback
1446 * only a few subtle things...
1448 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
1450 struct snd_pcm_runtime *runtime = substream->runtime;
1451 struct snd_usb_substream *subs = runtime->private_data;
1453 if (! subs->cur_audiofmt) {
1454 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
1455 return -ENXIO;
1458 /* some unit conversions in runtime */
1459 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
1460 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
1462 /* reset the pointer */
1463 subs->hwptr_done = 0;
1464 subs->transfer_done = 0;
1465 subs->phase = 0;
1467 /* clear urbs (to be sure) */
1468 deactivate_urbs(subs, 0, 1);
1469 wait_clear_urbs(subs);
1471 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1472 * updates for all URBs would happen at the same time when starting */
1473 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1474 subs->ops.prepare = prepare_startup_playback_urb;
1475 return start_urbs(subs, runtime);
1476 } else
1477 return 0;
1480 static struct snd_pcm_hardware snd_usb_playback =
1482 .info = SNDRV_PCM_INFO_MMAP |
1483 SNDRV_PCM_INFO_MMAP_VALID |
1484 SNDRV_PCM_INFO_BATCH |
1485 SNDRV_PCM_INFO_INTERLEAVED |
1486 SNDRV_PCM_INFO_BLOCK_TRANSFER,
1487 .buffer_bytes_max = 1024 * 1024,
1488 .period_bytes_min = 64,
1489 .period_bytes_max = 512 * 1024,
1490 .periods_min = 2,
1491 .periods_max = 1024,
1494 static struct snd_pcm_hardware snd_usb_capture =
1496 .info = SNDRV_PCM_INFO_MMAP |
1497 SNDRV_PCM_INFO_MMAP_VALID |
1498 SNDRV_PCM_INFO_BATCH |
1499 SNDRV_PCM_INFO_INTERLEAVED |
1500 SNDRV_PCM_INFO_BLOCK_TRANSFER,
1501 .buffer_bytes_max = 1024 * 1024,
1502 .period_bytes_min = 64,
1503 .period_bytes_max = 512 * 1024,
1504 .periods_min = 2,
1505 .periods_max = 1024,
1509 * h/w constraints
1512 #ifdef HW_CONST_DEBUG
1513 #define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
1514 #else
1515 #define hwc_debug(fmt, args...) /**/
1516 #endif
1518 static int hw_check_valid_format(struct snd_pcm_hw_params *params, struct audioformat *fp)
1520 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1521 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1522 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1524 /* check the format */
1525 if (! snd_mask_test(fmts, fp->format)) {
1526 hwc_debug(" > check: no supported format %d\n", fp->format);
1527 return 0;
1529 /* check the channels */
1530 if (fp->channels < ct->min || fp->channels > ct->max) {
1531 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1532 return 0;
1534 /* check the rate is within the range */
1535 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1536 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1537 return 0;
1539 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1540 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1541 return 0;
1543 return 1;
1546 static int hw_rule_rate(struct snd_pcm_hw_params *params,
1547 struct snd_pcm_hw_rule *rule)
1549 struct snd_usb_substream *subs = rule->private;
1550 struct list_head *p;
1551 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1552 unsigned int rmin, rmax;
1553 int changed;
1555 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1556 changed = 0;
1557 rmin = rmax = 0;
1558 list_for_each(p, &subs->fmt_list) {
1559 struct audioformat *fp;
1560 fp = list_entry(p, struct audioformat, list);
1561 if (! hw_check_valid_format(params, fp))
1562 continue;
1563 if (changed++) {
1564 if (rmin > fp->rate_min)
1565 rmin = fp->rate_min;
1566 if (rmax < fp->rate_max)
1567 rmax = fp->rate_max;
1568 } else {
1569 rmin = fp->rate_min;
1570 rmax = fp->rate_max;
1574 if (! changed) {
1575 hwc_debug(" --> get empty\n");
1576 it->empty = 1;
1577 return -EINVAL;
1580 changed = 0;
1581 if (it->min < rmin) {
1582 it->min = rmin;
1583 it->openmin = 0;
1584 changed = 1;
1586 if (it->max > rmax) {
1587 it->max = rmax;
1588 it->openmax = 0;
1589 changed = 1;
1591 if (snd_interval_checkempty(it)) {
1592 it->empty = 1;
1593 return -EINVAL;
1595 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1596 return changed;
1600 static int hw_rule_channels(struct snd_pcm_hw_params *params,
1601 struct snd_pcm_hw_rule *rule)
1603 struct snd_usb_substream *subs = rule->private;
1604 struct list_head *p;
1605 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1606 unsigned int rmin, rmax;
1607 int changed;
1609 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1610 changed = 0;
1611 rmin = rmax = 0;
1612 list_for_each(p, &subs->fmt_list) {
1613 struct audioformat *fp;
1614 fp = list_entry(p, struct audioformat, list);
1615 if (! hw_check_valid_format(params, fp))
1616 continue;
1617 if (changed++) {
1618 if (rmin > fp->channels)
1619 rmin = fp->channels;
1620 if (rmax < fp->channels)
1621 rmax = fp->channels;
1622 } else {
1623 rmin = fp->channels;
1624 rmax = fp->channels;
1628 if (! changed) {
1629 hwc_debug(" --> get empty\n");
1630 it->empty = 1;
1631 return -EINVAL;
1634 changed = 0;
1635 if (it->min < rmin) {
1636 it->min = rmin;
1637 it->openmin = 0;
1638 changed = 1;
1640 if (it->max > rmax) {
1641 it->max = rmax;
1642 it->openmax = 0;
1643 changed = 1;
1645 if (snd_interval_checkempty(it)) {
1646 it->empty = 1;
1647 return -EINVAL;
1649 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1650 return changed;
1653 static int hw_rule_format(struct snd_pcm_hw_params *params,
1654 struct snd_pcm_hw_rule *rule)
1656 struct snd_usb_substream *subs = rule->private;
1657 struct list_head *p;
1658 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1659 u64 fbits;
1660 u32 oldbits[2];
1661 int changed;
1663 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1664 fbits = 0;
1665 list_for_each(p, &subs->fmt_list) {
1666 struct audioformat *fp;
1667 fp = list_entry(p, struct audioformat, list);
1668 if (! hw_check_valid_format(params, fp))
1669 continue;
1670 fbits |= (1ULL << fp->format);
1673 oldbits[0] = fmt->bits[0];
1674 oldbits[1] = fmt->bits[1];
1675 fmt->bits[0] &= (u32)fbits;
1676 fmt->bits[1] &= (u32)(fbits >> 32);
1677 if (! fmt->bits[0] && ! fmt->bits[1]) {
1678 hwc_debug(" --> get empty\n");
1679 return -EINVAL;
1681 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1682 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1683 return changed;
1686 #define MAX_MASK 64
1689 * check whether the registered audio formats need special hw-constraints
1691 static int check_hw_params_convention(struct snd_usb_substream *subs)
1693 int i;
1694 u32 *channels;
1695 u32 *rates;
1696 u32 cmaster, rmaster;
1697 u32 rate_min = 0, rate_max = 0;
1698 struct list_head *p;
1699 int err = 1;
1701 channels = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1702 rates = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1704 list_for_each(p, &subs->fmt_list) {
1705 struct audioformat *f;
1706 f = list_entry(p, struct audioformat, list);
1707 /* unconventional channels? */
1708 if (f->channels > 32)
1709 goto __out;
1710 /* continuous rate min/max matches? */
1711 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1712 if (rate_min && f->rate_min != rate_min)
1713 goto __out;
1714 if (rate_max && f->rate_max != rate_max)
1715 goto __out;
1716 rate_min = f->rate_min;
1717 rate_max = f->rate_max;
1719 /* combination of continuous rates and fixed rates? */
1720 if (rates[f->format] & SNDRV_PCM_RATE_CONTINUOUS) {
1721 if (f->rates != rates[f->format])
1722 goto __out;
1724 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1725 if (rates[f->format] && rates[f->format] != f->rates)
1726 goto __out;
1728 channels[f->format] |= (1 << f->channels);
1729 rates[f->format] |= f->rates;
1731 /* check whether channels and rates match for all formats */
1732 cmaster = rmaster = 0;
1733 for (i = 0; i < MAX_MASK; i++) {
1734 if (cmaster != channels[i] && cmaster && channels[i])
1735 goto __out;
1736 if (rmaster != rates[i] && rmaster && rates[i])
1737 goto __out;
1738 if (channels[i])
1739 cmaster = channels[i];
1740 if (rates[i])
1741 rmaster = rates[i];
1743 /* check whether channels match for all distinct rates */
1744 memset(channels, 0, MAX_MASK * sizeof(u32));
1745 list_for_each(p, &subs->fmt_list) {
1746 struct audioformat *f;
1747 f = list_entry(p, struct audioformat, list);
1748 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS)
1749 continue;
1750 for (i = 0; i < 32; i++) {
1751 if (f->rates & (1 << i))
1752 channels[i] |= (1 << f->channels);
1755 cmaster = 0;
1756 for (i = 0; i < 32; i++) {
1757 if (cmaster != channels[i] && cmaster && channels[i])
1758 goto __out;
1759 if (channels[i])
1760 cmaster = channels[i];
1762 err = 0;
1764 __out:
1765 kfree(channels);
1766 kfree(rates);
1767 return err;
1772 * set up the runtime hardware information.
1775 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1777 struct list_head *p;
1778 int err;
1780 runtime->hw.formats = subs->formats;
1782 runtime->hw.rate_min = 0x7fffffff;
1783 runtime->hw.rate_max = 0;
1784 runtime->hw.channels_min = 256;
1785 runtime->hw.channels_max = 0;
1786 runtime->hw.rates = 0;
1787 /* check min/max rates and channels */
1788 list_for_each(p, &subs->fmt_list) {
1789 struct audioformat *fp;
1790 fp = list_entry(p, struct audioformat, list);
1791 runtime->hw.rates |= fp->rates;
1792 if (runtime->hw.rate_min > fp->rate_min)
1793 runtime->hw.rate_min = fp->rate_min;
1794 if (runtime->hw.rate_max < fp->rate_max)
1795 runtime->hw.rate_max = fp->rate_max;
1796 if (runtime->hw.channels_min > fp->channels)
1797 runtime->hw.channels_min = fp->channels;
1798 if (runtime->hw.channels_max < fp->channels)
1799 runtime->hw.channels_max = fp->channels;
1800 if (fp->fmt_type == USB_FORMAT_TYPE_II && fp->frame_size > 0) {
1801 /* FIXME: there might be more than one audio formats... */
1802 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1803 fp->frame_size;
1807 /* set the period time minimum 1ms */
1808 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1809 1000 * MIN_PACKS_URB,
1810 /*(nrpacks * MAX_URBS) * 1000*/ UINT_MAX);
1812 if (check_hw_params_convention(subs)) {
1813 hwc_debug("setting extra hw constraints...\n");
1814 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1815 hw_rule_rate, subs,
1816 SNDRV_PCM_HW_PARAM_FORMAT,
1817 SNDRV_PCM_HW_PARAM_CHANNELS,
1818 -1)) < 0)
1819 return err;
1820 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1821 hw_rule_channels, subs,
1822 SNDRV_PCM_HW_PARAM_FORMAT,
1823 SNDRV_PCM_HW_PARAM_RATE,
1824 -1)) < 0)
1825 return err;
1826 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1827 hw_rule_format, subs,
1828 SNDRV_PCM_HW_PARAM_RATE,
1829 SNDRV_PCM_HW_PARAM_CHANNELS,
1830 -1)) < 0)
1831 return err;
1833 return 0;
1836 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction,
1837 struct snd_pcm_hardware *hw)
1839 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1840 struct snd_pcm_runtime *runtime = substream->runtime;
1841 struct snd_usb_substream *subs = &as->substream[direction];
1843 subs->interface = -1;
1844 subs->format = 0;
1845 runtime->hw = *hw;
1846 runtime->private_data = subs;
1847 subs->pcm_substream = substream;
1848 return setup_hw_info(runtime, subs);
1851 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1853 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1854 struct snd_usb_substream *subs = &as->substream[direction];
1856 if (subs->interface >= 0) {
1857 usb_set_interface(subs->dev, subs->interface, 0);
1858 subs->interface = -1;
1860 subs->pcm_substream = NULL;
1861 return 0;
1864 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1866 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK, &snd_usb_playback);
1869 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1871 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1874 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1876 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE, &snd_usb_capture);
1879 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1881 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1884 static struct snd_pcm_ops snd_usb_playback_ops = {
1885 .open = snd_usb_playback_open,
1886 .close = snd_usb_playback_close,
1887 .ioctl = snd_pcm_lib_ioctl,
1888 .hw_params = snd_usb_hw_params,
1889 .hw_free = snd_usb_hw_free,
1890 .prepare = snd_usb_pcm_prepare,
1891 .trigger = snd_usb_pcm_playback_trigger,
1892 .pointer = snd_usb_pcm_pointer,
1893 .page = snd_pcm_get_vmalloc_page,
1896 static struct snd_pcm_ops snd_usb_capture_ops = {
1897 .open = snd_usb_capture_open,
1898 .close = snd_usb_capture_close,
1899 .ioctl = snd_pcm_lib_ioctl,
1900 .hw_params = snd_usb_hw_params,
1901 .hw_free = snd_usb_hw_free,
1902 .prepare = snd_usb_pcm_prepare,
1903 .trigger = snd_usb_pcm_capture_trigger,
1904 .pointer = snd_usb_pcm_pointer,
1905 .page = snd_pcm_get_vmalloc_page,
1911 * helper functions
1915 * combine bytes and get an integer value
1917 unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
1919 switch (size) {
1920 case 1: return *bytes;
1921 case 2: return combine_word(bytes);
1922 case 3: return combine_triple(bytes);
1923 case 4: return combine_quad(bytes);
1924 default: return 0;
1929 * parse descriptor buffer and return the pointer starting the given
1930 * descriptor type.
1932 void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
1934 u8 *p, *end, *next;
1936 p = descstart;
1937 end = p + desclen;
1938 for (; p < end;) {
1939 if (p[0] < 2)
1940 return NULL;
1941 next = p + p[0];
1942 if (next > end)
1943 return NULL;
1944 if (p[1] == dtype && (!after || (void *)p > after)) {
1945 return p;
1947 p = next;
1949 return NULL;
1953 * find a class-specified interface descriptor with the given subtype.
1955 void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
1957 unsigned char *p = after;
1959 while ((p = snd_usb_find_desc(buffer, buflen, p,
1960 USB_DT_CS_INTERFACE)) != NULL) {
1961 if (p[0] >= 3 && p[2] == dsubtype)
1962 return p;
1964 return NULL;
1968 * Wrapper for usb_control_msg().
1969 * Allocates a temp buffer to prevent dmaing from/to the stack.
1971 int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
1972 __u8 requesttype, __u16 value, __u16 index, void *data,
1973 __u16 size, int timeout)
1975 int err;
1976 void *buf = NULL;
1978 if (size > 0) {
1979 buf = kmalloc(size, GFP_KERNEL);
1980 if (!buf)
1981 return -ENOMEM;
1982 memcpy(buf, data, size);
1984 err = usb_control_msg(dev, pipe, request, requesttype,
1985 value, index, buf, size, timeout);
1986 if (size > 0) {
1987 memcpy(data, buf, size);
1988 kfree(buf);
1990 return err;
1995 * entry point for linux usb interface
1998 static int usb_audio_probe(struct usb_interface *intf,
1999 const struct usb_device_id *id);
2000 static void usb_audio_disconnect(struct usb_interface *intf);
2002 static struct usb_device_id usb_audio_ids [] = {
2003 #include "usbquirks.h"
2004 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
2005 .bInterfaceClass = USB_CLASS_AUDIO,
2006 .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL },
2007 { } /* Terminating entry */
2010 MODULE_DEVICE_TABLE (usb, usb_audio_ids);
2012 static struct usb_driver usb_audio_driver = {
2013 .name = "snd-usb-audio",
2014 .probe = usb_audio_probe,
2015 .disconnect = usb_audio_disconnect,
2016 .id_table = usb_audio_ids,
2021 * proc interface for list the supported pcm formats
2023 static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
2025 struct list_head *p;
2026 static char *sync_types[4] = {
2027 "NONE", "ASYNC", "ADAPTIVE", "SYNC"
2030 list_for_each(p, &subs->fmt_list) {
2031 struct audioformat *fp;
2032 fp = list_entry(p, struct audioformat, list);
2033 snd_iprintf(buffer, " Interface %d\n", fp->iface);
2034 snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
2035 snd_iprintf(buffer, " Format: %s\n", snd_pcm_format_name(fp->format));
2036 snd_iprintf(buffer, " Channels: %d\n", fp->channels);
2037 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
2038 fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
2039 fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
2040 sync_types[(fp->ep_attr & EP_ATTR_MASK) >> 2]);
2041 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
2042 snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
2043 fp->rate_min, fp->rate_max);
2044 } else {
2045 unsigned int i;
2046 snd_iprintf(buffer, " Rates: ");
2047 for (i = 0; i < fp->nr_rates; i++) {
2048 if (i > 0)
2049 snd_iprintf(buffer, ", ");
2050 snd_iprintf(buffer, "%d", fp->rate_table[i]);
2052 snd_iprintf(buffer, "\n");
2054 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
2055 // snd_iprintf(buffer, " EP Attribute = 0x%x\n", fp->attributes);
2059 static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
2061 if (subs->running) {
2062 unsigned int i;
2063 snd_iprintf(buffer, " Status: Running\n");
2064 snd_iprintf(buffer, " Interface = %d\n", subs->interface);
2065 snd_iprintf(buffer, " Altset = %d\n", subs->format);
2066 snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
2067 for (i = 0; i < subs->nurbs; i++)
2068 snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
2069 snd_iprintf(buffer, "]\n");
2070 snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
2071 snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
2072 snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
2073 ? get_full_speed_hz(subs->freqm)
2074 : get_high_speed_hz(subs->freqm),
2075 subs->freqm >> 16, subs->freqm & 0xffff);
2076 } else {
2077 snd_iprintf(buffer, " Status: Stop\n");
2081 static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
2083 struct snd_usb_stream *stream = entry->private_data;
2085 snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
2087 if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
2088 snd_iprintf(buffer, "\nPlayback:\n");
2089 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2090 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2092 if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
2093 snd_iprintf(buffer, "\nCapture:\n");
2094 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2095 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2099 static void proc_pcm_format_add(struct snd_usb_stream *stream)
2101 struct snd_info_entry *entry;
2102 char name[32];
2103 struct snd_card *card = stream->chip->card;
2105 sprintf(name, "stream%d", stream->pcm_index);
2106 if (! snd_card_proc_new(card, name, &entry))
2107 snd_info_set_text_ops(entry, stream, 1024, proc_pcm_format_read);
2112 * initialize the substream instance.
2115 static void init_substream(struct snd_usb_stream *as, int stream, struct audioformat *fp)
2117 struct snd_usb_substream *subs = &as->substream[stream];
2119 INIT_LIST_HEAD(&subs->fmt_list);
2120 spin_lock_init(&subs->lock);
2122 subs->stream = as;
2123 subs->direction = stream;
2124 subs->dev = as->chip->dev;
2125 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
2126 subs->ops = audio_urb_ops[stream];
2127 else
2128 subs->ops = audio_urb_ops_high_speed[stream];
2129 snd_pcm_set_ops(as->pcm, stream,
2130 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2131 &snd_usb_playback_ops : &snd_usb_capture_ops);
2133 list_add_tail(&fp->list, &subs->fmt_list);
2134 subs->formats |= 1ULL << fp->format;
2135 subs->endpoint = fp->endpoint;
2136 subs->num_formats++;
2137 subs->fmt_type = fp->fmt_type;
2142 * free a substream
2144 static void free_substream(struct snd_usb_substream *subs)
2146 struct list_head *p, *n;
2148 if (! subs->num_formats)
2149 return; /* not initialized */
2150 list_for_each_safe(p, n, &subs->fmt_list) {
2151 struct audioformat *fp = list_entry(p, struct audioformat, list);
2152 kfree(fp->rate_table);
2153 kfree(fp);
2159 * free a usb stream instance
2161 static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
2163 free_substream(&stream->substream[0]);
2164 free_substream(&stream->substream[1]);
2165 list_del(&stream->list);
2166 kfree(stream);
2169 static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
2171 struct snd_usb_stream *stream = pcm->private_data;
2172 if (stream) {
2173 stream->pcm = NULL;
2174 snd_usb_audio_stream_free(stream);
2180 * add this endpoint to the chip instance.
2181 * if a stream with the same endpoint already exists, append to it.
2182 * if not, create a new pcm stream.
2184 static int add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)
2186 struct list_head *p;
2187 struct snd_usb_stream *as;
2188 struct snd_usb_substream *subs;
2189 struct snd_pcm *pcm;
2190 int err;
2192 list_for_each(p, &chip->pcm_list) {
2193 as = list_entry(p, struct snd_usb_stream, list);
2194 if (as->fmt_type != fp->fmt_type)
2195 continue;
2196 subs = &as->substream[stream];
2197 if (! subs->endpoint)
2198 continue;
2199 if (subs->endpoint == fp->endpoint) {
2200 list_add_tail(&fp->list, &subs->fmt_list);
2201 subs->num_formats++;
2202 subs->formats |= 1ULL << fp->format;
2203 return 0;
2206 /* look for an empty stream */
2207 list_for_each(p, &chip->pcm_list) {
2208 as = list_entry(p, struct snd_usb_stream, list);
2209 if (as->fmt_type != fp->fmt_type)
2210 continue;
2211 subs = &as->substream[stream];
2212 if (subs->endpoint)
2213 continue;
2214 err = snd_pcm_new_stream(as->pcm, stream, 1);
2215 if (err < 0)
2216 return err;
2217 init_substream(as, stream, fp);
2218 return 0;
2221 /* create a new pcm */
2222 as = kmalloc(sizeof(*as), GFP_KERNEL);
2223 if (! as)
2224 return -ENOMEM;
2225 memset(as, 0, sizeof(*as));
2226 as->pcm_index = chip->pcm_devs;
2227 as->chip = chip;
2228 as->fmt_type = fp->fmt_type;
2229 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
2230 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
2231 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
2232 &pcm);
2233 if (err < 0) {
2234 kfree(as);
2235 return err;
2237 as->pcm = pcm;
2238 pcm->private_data = as;
2239 pcm->private_free = snd_usb_audio_pcm_free;
2240 pcm->info_flags = 0;
2241 if (chip->pcm_devs > 0)
2242 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
2243 else
2244 strcpy(pcm->name, "USB Audio");
2246 init_substream(as, stream, fp);
2248 list_add(&as->list, &chip->pcm_list);
2249 chip->pcm_devs++;
2251 proc_pcm_format_add(as);
2253 return 0;
2258 * check if the device uses big-endian samples
2260 static int is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
2262 switch (chip->usb_id) {
2263 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
2264 if (fp->endpoint & USB_DIR_IN)
2265 return 1;
2266 break;
2267 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2268 return 1;
2270 return 0;
2274 * parse the audio format type I descriptor
2275 * and returns the corresponding pcm format
2277 * @dev: usb device
2278 * @fp: audioformat record
2279 * @format: the format tag (wFormatTag)
2280 * @fmt: the format type descriptor
2282 static int parse_audio_format_i_type(struct snd_usb_audio *chip, struct audioformat *fp,
2283 int format, unsigned char *fmt)
2285 int pcm_format;
2286 int sample_width, sample_bytes;
2288 /* FIXME: correct endianess and sign? */
2289 pcm_format = -1;
2290 sample_width = fmt[6];
2291 sample_bytes = fmt[5];
2292 switch (format) {
2293 case 0: /* some devices don't define this correctly... */
2294 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
2295 chip->dev->devnum, fp->iface, fp->altsetting);
2296 /* fall-through */
2297 case USB_AUDIO_FORMAT_PCM:
2298 if (sample_width > sample_bytes * 8) {
2299 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
2300 chip->dev->devnum, fp->iface, fp->altsetting,
2301 sample_width, sample_bytes);
2303 /* check the format byte size */
2304 switch (fmt[5]) {
2305 case 1:
2306 pcm_format = SNDRV_PCM_FORMAT_S8;
2307 break;
2308 case 2:
2309 if (is_big_endian_format(chip, fp))
2310 pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
2311 else
2312 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2313 break;
2314 case 3:
2315 if (is_big_endian_format(chip, fp))
2316 pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
2317 else
2318 pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
2319 break;
2320 case 4:
2321 pcm_format = SNDRV_PCM_FORMAT_S32_LE;
2322 break;
2323 default:
2324 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
2325 chip->dev->devnum, fp->iface,
2326 fp->altsetting, sample_width, sample_bytes);
2327 break;
2329 break;
2330 case USB_AUDIO_FORMAT_PCM8:
2331 /* Dallas DS4201 workaround */
2332 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
2333 pcm_format = SNDRV_PCM_FORMAT_S8;
2334 else
2335 pcm_format = SNDRV_PCM_FORMAT_U8;
2336 break;
2337 case USB_AUDIO_FORMAT_IEEE_FLOAT:
2338 pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
2339 break;
2340 case USB_AUDIO_FORMAT_ALAW:
2341 pcm_format = SNDRV_PCM_FORMAT_A_LAW;
2342 break;
2343 case USB_AUDIO_FORMAT_MU_LAW:
2344 pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
2345 break;
2346 default:
2347 snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
2348 chip->dev->devnum, fp->iface, fp->altsetting, format);
2349 break;
2351 return pcm_format;
2356 * parse the format descriptor and stores the possible sample rates
2357 * on the audioformat table.
2359 * @dev: usb device
2360 * @fp: audioformat record
2361 * @fmt: the format descriptor
2362 * @offset: the start offset of descriptor pointing the rate type
2363 * (7 for type I and II, 8 for type II)
2365 static int parse_audio_format_rates(struct snd_usb_audio *chip, struct audioformat *fp,
2366 unsigned char *fmt, int offset)
2368 int nr_rates = fmt[offset];
2369 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
2370 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
2371 chip->dev->devnum, fp->iface, fp->altsetting);
2372 return -1;
2375 if (nr_rates) {
2377 * build the rate table and bitmap flags
2379 int r, idx, c;
2380 /* this table corresponds to the SNDRV_PCM_RATE_XXX bit */
2381 static unsigned int conv_rates[] = {
2382 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
2383 64000, 88200, 96000, 176400, 192000
2385 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2386 if (fp->rate_table == NULL) {
2387 snd_printk(KERN_ERR "cannot malloc\n");
2388 return -1;
2391 fp->nr_rates = nr_rates;
2392 fp->rate_min = fp->rate_max = combine_triple(&fmt[8]);
2393 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
2394 unsigned int rate = fp->rate_table[r] = combine_triple(&fmt[idx]);
2395 if (rate < fp->rate_min)
2396 fp->rate_min = rate;
2397 else if (rate > fp->rate_max)
2398 fp->rate_max = rate;
2399 for (c = 0; c < (int)ARRAY_SIZE(conv_rates); c++) {
2400 if (rate == conv_rates[c]) {
2401 fp->rates |= (1 << c);
2402 break;
2406 } else {
2407 /* continuous rates */
2408 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
2409 fp->rate_min = combine_triple(&fmt[offset + 1]);
2410 fp->rate_max = combine_triple(&fmt[offset + 4]);
2412 return 0;
2416 * parse the format type I and III descriptors
2418 static int parse_audio_format_i(struct snd_usb_audio *chip, struct audioformat *fp,
2419 int format, unsigned char *fmt)
2421 int pcm_format;
2423 if (fmt[3] == USB_FORMAT_TYPE_III) {
2424 /* FIXME: the format type is really IECxxx
2425 * but we give normal PCM format to get the existing
2426 * apps working...
2428 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2429 } else {
2430 pcm_format = parse_audio_format_i_type(chip, fp, format, fmt);
2431 if (pcm_format < 0)
2432 return -1;
2434 fp->format = pcm_format;
2435 fp->channels = fmt[4];
2436 if (fp->channels < 1) {
2437 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
2438 chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
2439 return -1;
2441 return parse_audio_format_rates(chip, fp, fmt, 7);
2445 * prase the format type II descriptor
2447 static int parse_audio_format_ii(struct snd_usb_audio *chip, struct audioformat *fp,
2448 int format, unsigned char *fmt)
2450 int brate, framesize;
2451 switch (format) {
2452 case USB_AUDIO_FORMAT_AC3:
2453 /* FIXME: there is no AC3 format defined yet */
2454 // fp->format = SNDRV_PCM_FORMAT_AC3;
2455 fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
2456 break;
2457 case USB_AUDIO_FORMAT_MPEG:
2458 fp->format = SNDRV_PCM_FORMAT_MPEG;
2459 break;
2460 default:
2461 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag 0x%x is detected. processed as MPEG.\n",
2462 chip->dev->devnum, fp->iface, fp->altsetting, format);
2463 fp->format = SNDRV_PCM_FORMAT_MPEG;
2464 break;
2466 fp->channels = 1;
2467 brate = combine_word(&fmt[4]); /* fmt[4,5] : wMaxBitRate (in kbps) */
2468 framesize = combine_word(&fmt[6]); /* fmt[6,7]: wSamplesPerFrame */
2469 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
2470 fp->frame_size = framesize;
2471 return parse_audio_format_rates(chip, fp, fmt, 8); /* fmt[8..] sample rates */
2474 static int parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,
2475 int format, unsigned char *fmt, int stream)
2477 int err;
2479 switch (fmt[3]) {
2480 case USB_FORMAT_TYPE_I:
2481 case USB_FORMAT_TYPE_III:
2482 err = parse_audio_format_i(chip, fp, format, fmt);
2483 break;
2484 case USB_FORMAT_TYPE_II:
2485 err = parse_audio_format_ii(chip, fp, format, fmt);
2486 break;
2487 default:
2488 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
2489 chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);
2490 return -1;
2492 fp->fmt_type = fmt[3];
2493 if (err < 0)
2494 return err;
2495 #if 1
2496 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
2497 /* extigy apparently supports sample rates other than 48k
2498 * but not in ordinary way. so we enable only 48k atm.
2500 if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
2501 chip->usb_id == USB_ID(0x041e, 0x3020) ||
2502 chip->usb_id == USB_ID(0x041e, 0x3061)) {
2503 if (fmt[3] == USB_FORMAT_TYPE_I &&
2504 fp->rates != SNDRV_PCM_RATE_48000 &&
2505 fp->rates != SNDRV_PCM_RATE_96000)
2506 return -1;
2508 #endif
2509 return 0;
2512 static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
2514 struct usb_device *dev;
2515 struct usb_interface *iface;
2516 struct usb_host_interface *alts;
2517 struct usb_interface_descriptor *altsd;
2518 int i, altno, err, stream;
2519 int format;
2520 struct audioformat *fp;
2521 unsigned char *fmt, *csep;
2523 dev = chip->dev;
2525 /* parse the interface's altsettings */
2526 iface = usb_ifnum_to_if(dev, iface_no);
2527 for (i = 0; i < iface->num_altsetting; i++) {
2528 alts = &iface->altsetting[i];
2529 altsd = get_iface_desc(alts);
2530 /* skip invalid one */
2531 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2532 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2533 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING &&
2534 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
2535 altsd->bNumEndpoints < 1 ||
2536 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
2537 continue;
2538 /* must be isochronous */
2539 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2540 USB_ENDPOINT_XFER_ISOC)
2541 continue;
2542 /* check direction */
2543 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
2544 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2545 altno = altsd->bAlternateSetting;
2547 /* get audio formats */
2548 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, AS_GENERAL);
2549 if (!fmt) {
2550 snd_printk(KERN_ERR "%d:%u:%d : AS_GENERAL descriptor not found\n",
2551 dev->devnum, iface_no, altno);
2552 continue;
2555 if (fmt[0] < 7) {
2556 snd_printk(KERN_ERR "%d:%u:%d : invalid AS_GENERAL desc\n",
2557 dev->devnum, iface_no, altno);
2558 continue;
2561 format = (fmt[6] << 8) | fmt[5]; /* remember the format value */
2563 /* get format type */
2564 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, FORMAT_TYPE);
2565 if (!fmt) {
2566 snd_printk(KERN_ERR "%d:%u:%d : no FORMAT_TYPE desc\n",
2567 dev->devnum, iface_no, altno);
2568 continue;
2570 if (fmt[0] < 8) {
2571 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
2572 dev->devnum, iface_no, altno);
2573 continue;
2576 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
2577 /* Creamware Noah has this descriptor after the 2nd endpoint */
2578 if (!csep && altsd->bNumEndpoints >= 2)
2579 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
2580 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2581 snd_printk(KERN_ERR "%d:%u:%d : no or invalid class specific endpoint descriptor\n",
2582 dev->devnum, iface_no, altno);
2583 continue;
2586 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2587 if (! fp) {
2588 snd_printk(KERN_ERR "cannot malloc\n");
2589 return -ENOMEM;
2592 memset(fp, 0, sizeof(*fp));
2593 fp->iface = iface_no;
2594 fp->altsetting = altno;
2595 fp->altset_idx = i;
2596 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2597 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2598 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2599 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
2600 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
2601 * (fp->maxpacksize & 0x7ff);
2602 fp->attributes = csep[3];
2604 /* some quirks for attributes here */
2606 switch (chip->usb_id) {
2607 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
2608 /* Optoplay sets the sample rate attribute although
2609 * it seems not supporting it in fact.
2611 fp->attributes &= ~EP_CS_ATTR_SAMPLE_RATE;
2612 break;
2613 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
2614 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2615 /* doesn't set the sample rate attribute, but supports it */
2616 fp->attributes |= EP_CS_ATTR_SAMPLE_RATE;
2617 break;
2618 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
2619 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
2620 an older model 77d:223) */
2622 * plantronics headset and Griffin iMic have set adaptive-in
2623 * although it's really not...
2625 fp->ep_attr &= ~EP_ATTR_MASK;
2626 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2627 fp->ep_attr |= EP_ATTR_ADAPTIVE;
2628 else
2629 fp->ep_attr |= EP_ATTR_SYNC;
2630 break;
2633 /* ok, let's parse further... */
2634 if (parse_audio_format(chip, fp, format, fmt, stream) < 0) {
2635 kfree(fp->rate_table);
2636 kfree(fp);
2637 continue;
2640 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint 0x%x\n", dev->devnum, iface_no, i, fp->endpoint);
2641 err = add_audio_endpoint(chip, stream, fp);
2642 if (err < 0) {
2643 kfree(fp->rate_table);
2644 kfree(fp);
2645 return err;
2647 /* try to set the interface... */
2648 usb_set_interface(chip->dev, iface_no, altno);
2649 init_usb_pitch(chip->dev, iface_no, alts, fp);
2650 init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
2652 return 0;
2657 * disconnect streams
2658 * called from snd_usb_audio_disconnect()
2660 static void snd_usb_stream_disconnect(struct list_head *head)
2662 int idx;
2663 struct snd_usb_stream *as;
2664 struct snd_usb_substream *subs;
2666 as = list_entry(head, struct snd_usb_stream, list);
2667 for (idx = 0; idx < 2; idx++) {
2668 subs = &as->substream[idx];
2669 if (!subs->num_formats)
2670 return;
2671 release_substream_urbs(subs, 1);
2672 subs->interface = -1;
2677 * parse audio control descriptor and create pcm/midi streams
2679 static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
2681 struct usb_device *dev = chip->dev;
2682 struct usb_host_interface *host_iface;
2683 struct usb_interface *iface;
2684 unsigned char *p1;
2685 int i, j;
2687 /* find audiocontrol interface */
2688 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
2689 if (!(p1 = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen, NULL, HEADER))) {
2690 snd_printk(KERN_ERR "cannot find HEADER\n");
2691 return -EINVAL;
2693 if (! p1[7] || p1[0] < 8 + p1[7]) {
2694 snd_printk(KERN_ERR "invalid HEADER\n");
2695 return -EINVAL;
2699 * parse all USB audio streaming interfaces
2701 for (i = 0; i < p1[7]; i++) {
2702 struct usb_host_interface *alts;
2703 struct usb_interface_descriptor *altsd;
2704 j = p1[8 + i];
2705 iface = usb_ifnum_to_if(dev, j);
2706 if (!iface) {
2707 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
2708 dev->devnum, ctrlif, j);
2709 continue;
2711 if (usb_interface_claimed(iface)) {
2712 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n", dev->devnum, ctrlif, j);
2713 continue;
2715 alts = &iface->altsetting[0];
2716 altsd = get_iface_desc(alts);
2717 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
2718 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
2719 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDI_STREAMING) {
2720 if (snd_usb_create_midi_interface(chip, iface, NULL) < 0) {
2721 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n", dev->devnum, ctrlif, j);
2722 continue;
2724 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2725 continue;
2727 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2728 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2729 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING) {
2730 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n", dev->devnum, ctrlif, j, altsd->bInterfaceClass);
2731 /* skip non-supported classes */
2732 continue;
2734 if (! parse_audio_endpoints(chip, j)) {
2735 usb_set_interface(dev, j, 0); /* reset the current interface */
2736 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2740 return 0;
2744 * create a stream for an endpoint/altsetting without proper descriptors
2746 static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
2747 struct usb_interface *iface,
2748 const struct snd_usb_audio_quirk *quirk)
2750 struct audioformat *fp;
2751 struct usb_host_interface *alts;
2752 int stream, err;
2753 int *rate_table = NULL;
2755 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2756 if (! fp) {
2757 snd_printk(KERN_ERR "cannot malloc\n");
2758 return -ENOMEM;
2760 memcpy(fp, quirk->data, sizeof(*fp));
2761 if (fp->nr_rates > 0) {
2762 rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
2763 if (!rate_table) {
2764 kfree(fp);
2765 return -ENOMEM;
2767 memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
2768 fp->rate_table = rate_table;
2771 stream = (fp->endpoint & USB_DIR_IN)
2772 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2773 err = add_audio_endpoint(chip, stream, fp);
2774 if (err < 0) {
2775 kfree(fp);
2776 kfree(rate_table);
2777 return err;
2779 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
2780 fp->altset_idx >= iface->num_altsetting) {
2781 kfree(fp);
2782 kfree(rate_table);
2783 return -EINVAL;
2785 alts = &iface->altsetting[fp->altset_idx];
2786 usb_set_interface(chip->dev, fp->iface, 0);
2787 init_usb_pitch(chip->dev, fp->iface, alts, fp);
2788 init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
2789 return 0;
2793 * create a stream for an interface with proper descriptors
2795 static int create_standard_audio_quirk(struct snd_usb_audio *chip,
2796 struct usb_interface *iface,
2797 const struct snd_usb_audio_quirk *quirk)
2799 struct usb_host_interface *alts;
2800 struct usb_interface_descriptor *altsd;
2801 int err;
2803 alts = &iface->altsetting[0];
2804 altsd = get_iface_desc(alts);
2805 err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
2806 if (err < 0) {
2807 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
2808 altsd->bInterfaceNumber, err);
2809 return err;
2811 /* reset the current interface */
2812 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
2813 return 0;
2817 * Create a stream for an Edirol UA-700/UA-25 interface. The only way
2818 * to detect the sample rate is by looking at wMaxPacketSize.
2820 static int create_ua700_ua25_quirk(struct snd_usb_audio *chip,
2821 struct usb_interface *iface,
2822 const struct snd_usb_audio_quirk *quirk)
2824 static const struct audioformat ua_format = {
2825 .format = SNDRV_PCM_FORMAT_S24_3LE,
2826 .channels = 2,
2827 .fmt_type = USB_FORMAT_TYPE_I,
2828 .altsetting = 1,
2829 .altset_idx = 1,
2830 .rates = SNDRV_PCM_RATE_CONTINUOUS,
2832 struct usb_host_interface *alts;
2833 struct usb_interface_descriptor *altsd;
2834 struct audioformat *fp;
2835 int stream, err;
2837 /* both PCM and MIDI interfaces have 2 altsettings */
2838 if (iface->num_altsetting != 2)
2839 return -ENXIO;
2840 alts = &iface->altsetting[1];
2841 altsd = get_iface_desc(alts);
2843 if (altsd->bNumEndpoints == 2) {
2844 static const struct snd_usb_midi_endpoint_info ua700_ep = {
2845 .out_cables = 0x0003,
2846 .in_cables = 0x0003
2848 static const struct snd_usb_audio_quirk ua700_quirk = {
2849 .type = QUIRK_MIDI_FIXED_ENDPOINT,
2850 .data = &ua700_ep
2852 static const struct snd_usb_midi_endpoint_info ua25_ep = {
2853 .out_cables = 0x0001,
2854 .in_cables = 0x0001
2856 static const struct snd_usb_audio_quirk ua25_quirk = {
2857 .type = QUIRK_MIDI_FIXED_ENDPOINT,
2858 .data = &ua25_ep
2860 if (chip->usb_id == USB_ID(0x0582, 0x002b))
2861 return snd_usb_create_midi_interface(chip, iface,
2862 &ua700_quirk);
2863 else
2864 return snd_usb_create_midi_interface(chip, iface,
2865 &ua25_quirk);
2868 if (altsd->bNumEndpoints != 1)
2869 return -ENXIO;
2871 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2872 if (!fp)
2873 return -ENOMEM;
2874 memcpy(fp, &ua_format, sizeof(*fp));
2876 fp->iface = altsd->bInterfaceNumber;
2877 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2878 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2879 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2881 switch (fp->maxpacksize) {
2882 case 0x120:
2883 fp->rate_max = fp->rate_min = 44100;
2884 break;
2885 case 0x138:
2886 case 0x140:
2887 fp->rate_max = fp->rate_min = 48000;
2888 break;
2889 case 0x258:
2890 case 0x260:
2891 fp->rate_max = fp->rate_min = 96000;
2892 break;
2893 default:
2894 snd_printk(KERN_ERR "unknown sample rate\n");
2895 kfree(fp);
2896 return -ENXIO;
2899 stream = (fp->endpoint & USB_DIR_IN)
2900 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2901 err = add_audio_endpoint(chip, stream, fp);
2902 if (err < 0) {
2903 kfree(fp);
2904 return err;
2906 usb_set_interface(chip->dev, fp->iface, 0);
2907 return 0;
2911 * Create a stream for an Edirol UA-1000 interface.
2913 static int create_ua1000_quirk(struct snd_usb_audio *chip,
2914 struct usb_interface *iface,
2915 const struct snd_usb_audio_quirk *quirk)
2917 static const struct audioformat ua1000_format = {
2918 .format = SNDRV_PCM_FORMAT_S32_LE,
2919 .fmt_type = USB_FORMAT_TYPE_I,
2920 .altsetting = 1,
2921 .altset_idx = 1,
2922 .attributes = 0,
2923 .rates = SNDRV_PCM_RATE_CONTINUOUS,
2925 struct usb_host_interface *alts;
2926 struct usb_interface_descriptor *altsd;
2927 struct audioformat *fp;
2928 int stream, err;
2930 if (iface->num_altsetting != 2)
2931 return -ENXIO;
2932 alts = &iface->altsetting[1];
2933 altsd = get_iface_desc(alts);
2934 if (alts->extralen != 11 || alts->extra[1] != CS_AUDIO_INTERFACE ||
2935 altsd->bNumEndpoints != 1)
2936 return -ENXIO;
2938 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2939 if (!fp)
2940 return -ENOMEM;
2941 memcpy(fp, &ua1000_format, sizeof(*fp));
2943 fp->channels = alts->extra[4];
2944 fp->iface = altsd->bInterfaceNumber;
2945 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2946 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2947 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2948 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]);
2950 stream = (fp->endpoint & USB_DIR_IN)
2951 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2952 err = add_audio_endpoint(chip, stream, fp);
2953 if (err < 0) {
2954 kfree(fp);
2955 return err;
2957 /* FIXME: playback must be synchronized to capture */
2958 usb_set_interface(chip->dev, fp->iface, 0);
2959 return 0;
2962 static int snd_usb_create_quirk(struct snd_usb_audio *chip,
2963 struct usb_interface *iface,
2964 const struct snd_usb_audio_quirk *quirk);
2967 * handle the quirks for the contained interfaces
2969 static int create_composite_quirk(struct snd_usb_audio *chip,
2970 struct usb_interface *iface,
2971 const struct snd_usb_audio_quirk *quirk)
2973 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
2974 int err;
2976 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
2977 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
2978 if (!iface)
2979 continue;
2980 if (quirk->ifnum != probed_ifnum &&
2981 usb_interface_claimed(iface))
2982 continue;
2983 err = snd_usb_create_quirk(chip, iface, quirk);
2984 if (err < 0)
2985 return err;
2986 if (quirk->ifnum != probed_ifnum)
2987 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2989 return 0;
2992 static int ignore_interface_quirk(struct snd_usb_audio *chip,
2993 struct usb_interface *iface,
2994 const struct snd_usb_audio_quirk *quirk)
2996 return 0;
3001 * boot quirks
3004 #define EXTIGY_FIRMWARE_SIZE_OLD 794
3005 #define EXTIGY_FIRMWARE_SIZE_NEW 483
3007 static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
3009 struct usb_host_config *config = dev->actconfig;
3010 int err;
3012 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
3013 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
3014 snd_printdd("sending Extigy boot sequence...\n");
3015 /* Send message to force it to reconnect with full interface. */
3016 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
3017 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
3018 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
3019 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
3020 &dev->descriptor, sizeof(dev->descriptor));
3021 config = dev->actconfig;
3022 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
3023 err = usb_reset_configuration(dev);
3024 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
3025 snd_printdd("extigy_boot: new boot length = %d\n",
3026 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
3027 return -ENODEV; /* quit this anyway */
3029 return 0;
3032 static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
3034 u8 buf = 1;
3036 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
3037 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3038 0, 0, &buf, 1, 1000);
3039 if (buf == 0) {
3040 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
3041 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3042 1, 2000, NULL, 0, 1000);
3043 return -ENODEV;
3045 return 0;
3050 * audio-interface quirks
3052 * returns zero if no standard audio/MIDI parsing is needed.
3053 * returns a postive value if standard audio/midi interfaces are parsed
3054 * after this.
3055 * returns a negative value at error.
3057 static int snd_usb_create_quirk(struct snd_usb_audio *chip,
3058 struct usb_interface *iface,
3059 const struct snd_usb_audio_quirk *quirk)
3061 typedef int (*quirk_func_t)(struct snd_usb_audio *, struct usb_interface *,
3062 const struct snd_usb_audio_quirk *);
3063 static const quirk_func_t quirk_funcs[] = {
3064 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
3065 [QUIRK_COMPOSITE] = create_composite_quirk,
3066 [QUIRK_MIDI_STANDARD_INTERFACE] = snd_usb_create_midi_interface,
3067 [QUIRK_MIDI_FIXED_ENDPOINT] = snd_usb_create_midi_interface,
3068 [QUIRK_MIDI_YAMAHA] = snd_usb_create_midi_interface,
3069 [QUIRK_MIDI_MIDIMAN] = snd_usb_create_midi_interface,
3070 [QUIRK_MIDI_NOVATION] = snd_usb_create_midi_interface,
3071 [QUIRK_MIDI_RAW] = snd_usb_create_midi_interface,
3072 [QUIRK_MIDI_EMAGIC] = snd_usb_create_midi_interface,
3073 [QUIRK_MIDI_MIDITECH] = snd_usb_create_midi_interface,
3074 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
3075 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
3076 [QUIRK_AUDIO_EDIROL_UA700_UA25] = create_ua700_ua25_quirk,
3077 [QUIRK_AUDIO_EDIROL_UA1000] = create_ua1000_quirk,
3080 if (quirk->type < QUIRK_TYPE_COUNT) {
3081 return quirk_funcs[quirk->type](chip, iface, quirk);
3082 } else {
3083 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
3084 return -ENXIO;
3090 * common proc files to show the usb device info
3092 static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
3094 struct snd_usb_audio *chip = entry->private_data;
3095 if (! chip->shutdown)
3096 snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
3099 static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
3101 struct snd_usb_audio *chip = entry->private_data;
3102 if (! chip->shutdown)
3103 snd_iprintf(buffer, "%04x:%04x\n",
3104 USB_ID_VENDOR(chip->usb_id),
3105 USB_ID_PRODUCT(chip->usb_id));
3108 static void snd_usb_audio_create_proc(struct snd_usb_audio *chip)
3110 struct snd_info_entry *entry;
3111 if (! snd_card_proc_new(chip->card, "usbbus", &entry))
3112 snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbbus_read);
3113 if (! snd_card_proc_new(chip->card, "usbid", &entry))
3114 snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbid_read);
3118 * free the chip instance
3120 * here we have to do not much, since pcm and controls are already freed
3124 static int snd_usb_audio_free(struct snd_usb_audio *chip)
3126 kfree(chip);
3127 return 0;
3130 static int snd_usb_audio_dev_free(struct snd_device *device)
3132 struct snd_usb_audio *chip = device->device_data;
3133 return snd_usb_audio_free(chip);
3138 * create a chip instance and set its names.
3140 static int snd_usb_audio_create(struct usb_device *dev, int idx,
3141 const struct snd_usb_audio_quirk *quirk,
3142 struct snd_usb_audio **rchip)
3144 struct snd_card *card;
3145 struct snd_usb_audio *chip;
3146 int err, len;
3147 char component[14];
3148 static struct snd_device_ops ops = {
3149 .dev_free = snd_usb_audio_dev_free,
3152 *rchip = NULL;
3154 if (snd_usb_get_speed(dev) != USB_SPEED_FULL &&
3155 snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
3156 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
3157 return -ENXIO;
3160 card = snd_card_new(index[idx], id[idx], THIS_MODULE, 0);
3161 if (card == NULL) {
3162 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
3163 return -ENOMEM;
3166 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
3167 if (! chip) {
3168 snd_card_free(card);
3169 return -ENOMEM;
3172 chip->index = idx;
3173 chip->dev = dev;
3174 chip->card = card;
3175 chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3176 le16_to_cpu(dev->descriptor.idProduct));
3177 INIT_LIST_HEAD(&chip->pcm_list);
3178 INIT_LIST_HEAD(&chip->midi_list);
3179 INIT_LIST_HEAD(&chip->mixer_list);
3181 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
3182 snd_usb_audio_free(chip);
3183 snd_card_free(card);
3184 return err;
3187 strcpy(card->driver, "USB-Audio");
3188 sprintf(component, "USB%04x:%04x",
3189 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
3190 snd_component_add(card, component);
3192 /* retrieve the device string as shortname */
3193 if (quirk && quirk->product_name) {
3194 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
3195 } else {
3196 if (!dev->descriptor.iProduct ||
3197 usb_string(dev, dev->descriptor.iProduct,
3198 card->shortname, sizeof(card->shortname)) <= 0) {
3199 /* no name available from anywhere, so use ID */
3200 sprintf(card->shortname, "USB Device %#04x:%#04x",
3201 USB_ID_VENDOR(chip->usb_id),
3202 USB_ID_PRODUCT(chip->usb_id));
3206 /* retrieve the vendor and device strings as longname */
3207 if (quirk && quirk->vendor_name) {
3208 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
3209 } else {
3210 if (dev->descriptor.iManufacturer)
3211 len = usb_string(dev, dev->descriptor.iManufacturer,
3212 card->longname, sizeof(card->longname));
3213 else
3214 len = 0;
3215 /* we don't really care if there isn't any vendor string */
3217 if (len > 0)
3218 strlcat(card->longname, " ", sizeof(card->longname));
3220 strlcat(card->longname, card->shortname, sizeof(card->longname));
3222 len = strlcat(card->longname, " at ", sizeof(card->longname));
3224 if (len < sizeof(card->longname))
3225 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
3227 strlcat(card->longname,
3228 snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" : ", high speed",
3229 sizeof(card->longname));
3231 snd_usb_audio_create_proc(chip);
3233 *rchip = chip;
3234 return 0;
3239 * probe the active usb device
3241 * note that this can be called multiple times per a device, when it
3242 * includes multiple audio control interfaces.
3244 * thus we check the usb device pointer and creates the card instance
3245 * only at the first time. the successive calls of this function will
3246 * append the pcm interface to the corresponding card.
3248 static void *snd_usb_audio_probe(struct usb_device *dev,
3249 struct usb_interface *intf,
3250 const struct usb_device_id *usb_id)
3252 const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
3253 int i, err;
3254 struct snd_usb_audio *chip;
3255 struct usb_host_interface *alts;
3256 int ifnum;
3257 u32 id;
3259 alts = &intf->altsetting[0];
3260 ifnum = get_iface_desc(alts)->bInterfaceNumber;
3261 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3262 le16_to_cpu(dev->descriptor.idProduct));
3264 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
3265 goto __err_val;
3267 /* SB Extigy needs special boot-up sequence */
3268 /* if more models come, this will go to the quirk list. */
3269 if (id == USB_ID(0x041e, 0x3000)) {
3270 if (snd_usb_extigy_boot_quirk(dev, intf) < 0)
3271 goto __err_val;
3273 /* SB Audigy 2 NX needs its own boot-up magic, too */
3274 if (id == USB_ID(0x041e, 0x3020)) {
3275 if (snd_usb_audigy2nx_boot_quirk(dev) < 0)
3276 goto __err_val;
3280 * found a config. now register to ALSA
3283 /* check whether it's already registered */
3284 chip = NULL;
3285 down(&register_mutex);
3286 for (i = 0; i < SNDRV_CARDS; i++) {
3287 if (usb_chip[i] && usb_chip[i]->dev == dev) {
3288 if (usb_chip[i]->shutdown) {
3289 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
3290 goto __error;
3292 chip = usb_chip[i];
3293 break;
3296 if (! chip) {
3297 /* it's a fresh one.
3298 * now look for an empty slot and create a new card instance
3300 for (i = 0; i < SNDRV_CARDS; i++)
3301 if (enable[i] && ! usb_chip[i] &&
3302 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
3303 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
3304 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
3305 goto __error;
3307 snd_card_set_dev(chip->card, &intf->dev);
3308 break;
3310 if (! chip) {
3311 snd_printk(KERN_ERR "no available usb audio device\n");
3312 goto __error;
3316 err = 1; /* continue */
3317 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
3318 /* need some special handlings */
3319 if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)
3320 goto __error;
3323 if (err > 0) {
3324 /* create normal USB audio interfaces */
3325 if (snd_usb_create_streams(chip, ifnum) < 0 ||
3326 snd_usb_create_mixer(chip, ifnum) < 0) {
3327 goto __error;
3331 /* we are allowed to call snd_card_register() many times */
3332 if (snd_card_register(chip->card) < 0) {
3333 goto __error;
3336 usb_chip[chip->index] = chip;
3337 chip->num_interfaces++;
3338 up(&register_mutex);
3339 return chip;
3341 __error:
3342 if (chip && !chip->num_interfaces)
3343 snd_card_free(chip->card);
3344 up(&register_mutex);
3345 __err_val:
3346 return NULL;
3350 * we need to take care of counter, since disconnection can be called also
3351 * many times as well as usb_audio_probe().
3353 static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
3355 struct snd_usb_audio *chip;
3356 struct snd_card *card;
3357 struct list_head *p;
3359 if (ptr == (void *)-1L)
3360 return;
3362 chip = ptr;
3363 card = chip->card;
3364 down(&register_mutex);
3365 chip->shutdown = 1;
3366 chip->num_interfaces--;
3367 if (chip->num_interfaces <= 0) {
3368 snd_card_disconnect(card);
3369 /* release the pcm resources */
3370 list_for_each(p, &chip->pcm_list) {
3371 snd_usb_stream_disconnect(p);
3373 /* release the midi resources */
3374 list_for_each(p, &chip->midi_list) {
3375 snd_usbmidi_disconnect(p);
3377 /* release mixer resources */
3378 list_for_each(p, &chip->mixer_list) {
3379 snd_usb_mixer_disconnect(p);
3381 usb_chip[chip->index] = NULL;
3382 up(&register_mutex);
3383 snd_card_free(card);
3384 } else {
3385 up(&register_mutex);
3390 * new 2.5 USB kernel API
3392 static int usb_audio_probe(struct usb_interface *intf,
3393 const struct usb_device_id *id)
3395 void *chip;
3396 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
3397 if (chip) {
3398 dev_set_drvdata(&intf->dev, chip);
3399 return 0;
3400 } else
3401 return -EIO;
3404 static void usb_audio_disconnect(struct usb_interface *intf)
3406 snd_usb_audio_disconnect(interface_to_usbdev(intf),
3407 dev_get_drvdata(&intf->dev));
3411 static int __init snd_usb_audio_init(void)
3413 if (nrpacks < MIN_PACKS_URB || nrpacks > MAX_PACKS) {
3414 printk(KERN_WARNING "invalid nrpacks value.\n");
3415 return -EINVAL;
3417 usb_register(&usb_audio_driver);
3418 return 0;
3422 static void __exit snd_usb_audio_cleanup(void)
3424 usb_deregister(&usb_audio_driver);
3427 module_init(snd_usb_audio_init);
3428 module_exit(snd_usb_audio_cleanup);