2 * (Tentative) USB Audio Driver for ALSA
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
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 <linux/bitops.h>
42 #include <linux/init.h>
43 #include <linux/list.h>
44 #include <linux/slab.h>
45 #include <linux/string.h>
46 #include <linux/usb.h>
47 #include <linux/moduleparam.h>
48 #include <linux/mutex.h>
49 #include <linux/usb/audio.h>
50 #include <linux/usb/ch9.h>
52 #include <sound/core.h>
53 #include <sound/info.h>
54 #include <sound/pcm.h>
55 #include <sound/pcm_params.h>
56 #include <sound/initval.h>
62 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
63 MODULE_DESCRIPTION("USB Audio");
64 MODULE_LICENSE("GPL");
65 MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
68 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
69 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
70 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
71 /* Vendor/product IDs for this card */
72 static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
73 static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
74 static int nrpacks = 8; /* max. number of packets per urb */
75 static int async_unlink = 1;
76 static int device_setup[SNDRV_CARDS]; /* device parameter for this card*/
77 static int ignore_ctl_error;
79 module_param_array(index, int, NULL, 0444);
80 MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
81 module_param_array(id, charp, NULL, 0444);
82 MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
83 module_param_array(enable, bool, NULL, 0444);
84 MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
85 module_param_array(vid, int, NULL, 0444);
86 MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
87 module_param_array(pid, int, NULL, 0444);
88 MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
89 module_param(nrpacks, int, 0644);
90 MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
91 module_param(async_unlink, bool, 0444);
92 MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
93 module_param_array(device_setup, int, NULL, 0444);
94 MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
95 module_param(ignore_ctl_error, bool, 0444);
96 MODULE_PARM_DESC(ignore_ctl_error,
97 "Ignore errors from USB controller for mixer interfaces.");
100 * debug the h/w constraints
102 /* #define HW_CONST_DEBUG */
110 #define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
112 #define SYNC_URBS 4 /* always four urbs for sync */
113 #define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */
116 struct list_head list;
117 snd_pcm_format_t format; /* format type */
118 unsigned int channels; /* # channels */
119 unsigned int fmt_type; /* USB audio format type (1-3) */
120 unsigned int frame_size; /* samples per frame for non-audio */
121 int iface; /* interface number */
122 unsigned char altsetting; /* corresponding alternate setting */
123 unsigned char altset_idx; /* array index of altenate setting */
124 unsigned char attributes; /* corresponding attributes of cs endpoint */
125 unsigned char endpoint; /* endpoint */
126 unsigned char ep_attr; /* endpoint attributes */
127 unsigned char datainterval; /* log_2 of data packet interval */
128 unsigned int maxpacksize; /* max. packet size */
129 unsigned int rates; /* rate bitmasks */
130 unsigned int rate_min, rate_max; /* min/max rates */
131 unsigned int nr_rates; /* number of rate table entries */
132 unsigned int *rate_table; /* rate table */
135 struct snd_usb_substream;
139 unsigned int buffer_size; /* size of data buffer, if data URB */
140 struct snd_usb_substream *subs;
141 int index; /* index for urb array */
142 int packets; /* number of packets per urb */
146 int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
147 int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
148 int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
149 int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
152 struct snd_usb_substream {
153 struct snd_usb_stream *stream;
154 struct usb_device *dev;
155 struct snd_pcm_substream *pcm_substream;
156 int direction; /* playback or capture */
157 int interface; /* current interface */
158 int endpoint; /* assigned endpoint */
159 struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
160 unsigned int cur_rate; /* current rate (for hw_params callback) */
161 unsigned int period_bytes; /* current period bytes (for hw_params callback) */
162 unsigned int format; /* USB data format */
163 unsigned int datapipe; /* the data i/o pipe */
164 unsigned int syncpipe; /* 1 - async out or adaptive in */
165 unsigned int datainterval; /* log_2 of data packet interval */
166 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
167 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
168 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
169 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
170 unsigned int phase; /* phase accumulator */
171 unsigned int maxpacksize; /* max packet size in bytes */
172 unsigned int maxframesize; /* max packet size in frames */
173 unsigned int curpacksize; /* current packet size in bytes (for capture) */
174 unsigned int curframesize; /* current packet size in frames (for capture) */
175 unsigned int fill_max: 1; /* fill max packet size always */
176 unsigned int txfr_quirk:1; /* allow sub-frame alignment */
177 unsigned int fmt_type; /* USB audio format type (1-3) */
179 unsigned int running: 1; /* running status */
181 unsigned int hwptr_done; /* processed byte position in the buffer */
182 unsigned int transfer_done; /* processed frames since last period update */
183 unsigned long active_mask; /* bitmask of active urbs */
184 unsigned long unlink_mask; /* bitmask of unlinked urbs */
186 unsigned int nurbs; /* # urbs */
187 struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */
188 struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */
189 char *syncbuf; /* sync buffer for all sync URBs */
190 dma_addr_t sync_dma; /* DMA address of syncbuf */
192 u64 formats; /* format bitmasks (all or'ed) */
193 unsigned int num_formats; /* number of supported audio formats (list) */
194 struct list_head fmt_list; /* format list */
195 struct snd_pcm_hw_constraint_list rate_list; /* limited rates */
198 struct snd_urb_ops ops; /* callbacks (must be filled at init) */
202 struct snd_usb_stream {
203 struct snd_usb_audio *chip;
206 unsigned int fmt_type; /* USB audio format type (1-3) */
207 struct snd_usb_substream substream[2];
208 struct list_head list;
213 * we keep the snd_usb_audio_t instances by ourselves for merging
214 * the all interfaces on the same card as one sound device.
217 static DEFINE_MUTEX(register_mutex);
218 static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
222 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
223 * this will overflow at approx 524 kHz
225 static inline unsigned get_usb_full_speed_rate(unsigned int rate)
227 return ((rate << 13) + 62) / 125;
231 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
232 * this will overflow at approx 4 MHz
234 static inline unsigned get_usb_high_speed_rate(unsigned int rate)
236 return ((rate << 10) + 62) / 125;
239 /* convert our full speed USB rate into sampling rate in Hz */
240 static inline unsigned get_full_speed_hz(unsigned int usb_rate)
242 return (usb_rate * 125 + (1 << 12)) >> 13;
245 /* convert our high speed USB rate into sampling rate in Hz */
246 static inline unsigned get_high_speed_hz(unsigned int usb_rate)
248 return (usb_rate * 125 + (1 << 9)) >> 10;
253 * prepare urb for full speed capture sync pipe
255 * fill the length and offset of each urb descriptor.
256 * the fixed 10.14 frequency is passed through the pipe.
258 static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
259 struct snd_pcm_runtime *runtime,
262 unsigned char *cp = urb->transfer_buffer;
263 struct snd_urb_ctx *ctx = urb->context;
265 urb->dev = ctx->subs->dev; /* we need to set this at each time */
266 urb->iso_frame_desc[0].length = 3;
267 urb->iso_frame_desc[0].offset = 0;
268 cp[0] = subs->freqn >> 2;
269 cp[1] = subs->freqn >> 10;
270 cp[2] = subs->freqn >> 18;
275 * prepare urb for high speed capture sync pipe
277 * fill the length and offset of each urb descriptor.
278 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
280 static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
281 struct snd_pcm_runtime *runtime,
284 unsigned char *cp = urb->transfer_buffer;
285 struct snd_urb_ctx *ctx = urb->context;
287 urb->dev = ctx->subs->dev; /* we need to set this at each time */
288 urb->iso_frame_desc[0].length = 4;
289 urb->iso_frame_desc[0].offset = 0;
291 cp[1] = subs->freqn >> 8;
292 cp[2] = subs->freqn >> 16;
293 cp[3] = subs->freqn >> 24;
298 * process after capture sync complete
301 static int retire_capture_sync_urb(struct snd_usb_substream *subs,
302 struct snd_pcm_runtime *runtime,
309 * prepare urb for capture data pipe
311 * fill the offset and length of each descriptor.
313 * we use a temporary buffer to write the captured data.
314 * since the length of written data is determined by host, we cannot
315 * write onto the pcm buffer directly... the data is thus copied
316 * later at complete callback to the global buffer.
318 static int prepare_capture_urb(struct snd_usb_substream *subs,
319 struct snd_pcm_runtime *runtime,
323 struct snd_urb_ctx *ctx = urb->context;
326 urb->dev = ctx->subs->dev; /* we need to set this at each time */
327 for (i = 0; i < ctx->packets; i++) {
328 urb->iso_frame_desc[i].offset = offs;
329 urb->iso_frame_desc[i].length = subs->curpacksize;
330 offs += subs->curpacksize;
332 urb->transfer_buffer_length = offs;
333 urb->number_of_packets = ctx->packets;
338 * process after capture complete
340 * copy the data from each desctiptor to the pcm buffer, and
341 * update the current position.
343 static int retire_capture_urb(struct snd_usb_substream *subs,
344 struct snd_pcm_runtime *runtime,
350 unsigned int stride, frames, bytes, oldptr;
351 int period_elapsed = 0;
353 stride = runtime->frame_bits >> 3;
355 for (i = 0; i < urb->number_of_packets; i++) {
356 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
357 if (urb->iso_frame_desc[i].status) {
358 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
361 bytes = urb->iso_frame_desc[i].actual_length;
362 frames = bytes / stride;
363 if (!subs->txfr_quirk)
364 bytes = frames * stride;
365 if (bytes % (runtime->sample_bits >> 3) != 0) {
366 #ifdef CONFIG_SND_DEBUG_VERBOSE
367 int oldbytes = bytes;
369 bytes = frames * stride;
370 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
373 /* update the current pointer */
374 spin_lock_irqsave(&subs->lock, flags);
375 oldptr = subs->hwptr_done;
376 subs->hwptr_done += bytes;
377 if (subs->hwptr_done >= runtime->buffer_size * stride)
378 subs->hwptr_done -= runtime->buffer_size * stride;
379 frames = (bytes + (oldptr % stride)) / stride;
380 subs->transfer_done += frames;
381 if (subs->transfer_done >= runtime->period_size) {
382 subs->transfer_done -= runtime->period_size;
385 spin_unlock_irqrestore(&subs->lock, flags);
386 /* copy a data chunk */
387 if (oldptr + bytes > runtime->buffer_size * stride) {
388 unsigned int bytes1 =
389 runtime->buffer_size * stride - oldptr;
390 memcpy(runtime->dma_area + oldptr, cp, bytes1);
391 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
393 memcpy(runtime->dma_area + oldptr, cp, bytes);
397 snd_pcm_period_elapsed(subs->pcm_substream);
402 * Process after capture complete when paused. Nothing to do.
404 static int retire_paused_capture_urb(struct snd_usb_substream *subs,
405 struct snd_pcm_runtime *runtime,
413 * prepare urb for full speed playback sync pipe
415 * set up the offset and length to receive the current frequency.
418 static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
419 struct snd_pcm_runtime *runtime,
422 struct snd_urb_ctx *ctx = urb->context;
424 urb->dev = ctx->subs->dev; /* we need to set this at each time */
425 urb->iso_frame_desc[0].length = 3;
426 urb->iso_frame_desc[0].offset = 0;
431 * prepare urb for high speed playback sync pipe
433 * set up the offset and length to receive the current frequency.
436 static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
437 struct snd_pcm_runtime *runtime,
440 struct snd_urb_ctx *ctx = urb->context;
442 urb->dev = ctx->subs->dev; /* we need to set this at each time */
443 urb->iso_frame_desc[0].length = 4;
444 urb->iso_frame_desc[0].offset = 0;
449 * process after full speed playback sync complete
451 * retrieve the current 10.14 frequency from pipe, and set it.
452 * the value is referred in prepare_playback_urb().
454 static int retire_playback_sync_urb(struct snd_usb_substream *subs,
455 struct snd_pcm_runtime *runtime,
461 if (urb->iso_frame_desc[0].status == 0 &&
462 urb->iso_frame_desc[0].actual_length == 3) {
463 f = combine_triple((u8*)urb->transfer_buffer) << 2;
464 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
465 spin_lock_irqsave(&subs->lock, flags);
467 spin_unlock_irqrestore(&subs->lock, flags);
475 * process after high speed playback sync complete
477 * retrieve the current 12.13 frequency from pipe, and set it.
478 * the value is referred in prepare_playback_urb().
480 static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
481 struct snd_pcm_runtime *runtime,
487 if (urb->iso_frame_desc[0].status == 0 &&
488 urb->iso_frame_desc[0].actual_length == 4) {
489 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
490 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
491 spin_lock_irqsave(&subs->lock, flags);
493 spin_unlock_irqrestore(&subs->lock, flags);
501 * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete
503 * These devices return the number of samples per packet instead of the number
504 * of samples per microframe.
506 static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs,
507 struct snd_pcm_runtime *runtime,
513 if (urb->iso_frame_desc[0].status == 0 &&
514 urb->iso_frame_desc[0].actual_length == 4) {
515 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
516 f >>= subs->datainterval;
517 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
518 spin_lock_irqsave(&subs->lock, flags);
520 spin_unlock_irqrestore(&subs->lock, flags);
527 /* determine the number of frames in the next packet */
528 static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
531 return subs->maxframesize;
533 subs->phase = (subs->phase & 0xffff)
534 + (subs->freqm << subs->datainterval);
535 return min(subs->phase >> 16, subs->maxframesize);
540 * Prepare urb for streaming before playback starts or when paused.
542 * We don't have any data, so we send silence.
544 static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
545 struct snd_pcm_runtime *runtime,
548 unsigned int i, offs, counts;
549 struct snd_urb_ctx *ctx = urb->context;
550 int stride = runtime->frame_bits >> 3;
553 urb->dev = ctx->subs->dev;
554 for (i = 0; i < ctx->packets; ++i) {
555 counts = snd_usb_audio_next_packet_size(subs);
556 urb->iso_frame_desc[i].offset = offs * stride;
557 urb->iso_frame_desc[i].length = counts * stride;
560 urb->number_of_packets = ctx->packets;
561 urb->transfer_buffer_length = offs * stride;
562 memset(urb->transfer_buffer,
563 subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
569 * prepare urb for playback data pipe
571 * Since a URB can handle only a single linear buffer, we must use double
572 * buffering when the data to be transferred overflows the buffer boundary.
573 * To avoid inconsistencies when updating hwptr_done, we use double buffering
576 static int prepare_playback_urb(struct snd_usb_substream *subs,
577 struct snd_pcm_runtime *runtime,
581 unsigned int counts, frames, bytes;
583 int period_elapsed = 0;
584 struct snd_urb_ctx *ctx = urb->context;
586 stride = runtime->frame_bits >> 3;
589 urb->dev = ctx->subs->dev; /* we need to set this at each time */
590 urb->number_of_packets = 0;
591 spin_lock_irqsave(&subs->lock, flags);
592 for (i = 0; i < ctx->packets; i++) {
593 counts = snd_usb_audio_next_packet_size(subs);
594 /* set up descriptor */
595 urb->iso_frame_desc[i].offset = frames * stride;
596 urb->iso_frame_desc[i].length = counts * stride;
598 urb->number_of_packets++;
599 subs->transfer_done += counts;
600 if (subs->transfer_done >= runtime->period_size) {
601 subs->transfer_done -= runtime->period_size;
603 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
604 if (subs->transfer_done > 0) {
605 /* FIXME: fill-max mode is not
607 frames -= subs->transfer_done;
608 counts -= subs->transfer_done;
609 urb->iso_frame_desc[i].length =
611 subs->transfer_done = 0;
614 if (i < ctx->packets) {
615 /* add a transfer delimiter */
616 urb->iso_frame_desc[i].offset =
618 urb->iso_frame_desc[i].length = 0;
619 urb->number_of_packets++;
624 if (period_elapsed) /* finish at the period boundary */
627 bytes = frames * stride;
628 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
629 /* err, the transferred area goes over buffer boundary. */
630 unsigned int bytes1 =
631 runtime->buffer_size * stride - subs->hwptr_done;
632 memcpy(urb->transfer_buffer,
633 runtime->dma_area + subs->hwptr_done, bytes1);
634 memcpy(urb->transfer_buffer + bytes1,
635 runtime->dma_area, bytes - bytes1);
637 memcpy(urb->transfer_buffer,
638 runtime->dma_area + subs->hwptr_done, bytes);
640 subs->hwptr_done += bytes;
641 if (subs->hwptr_done >= runtime->buffer_size * stride)
642 subs->hwptr_done -= runtime->buffer_size * stride;
643 runtime->delay += frames;
644 spin_unlock_irqrestore(&subs->lock, flags);
645 urb->transfer_buffer_length = bytes;
647 snd_pcm_period_elapsed(subs->pcm_substream);
652 * process after playback data complete
653 * - decrease the delay count again
655 static int retire_playback_urb(struct snd_usb_substream *subs,
656 struct snd_pcm_runtime *runtime,
660 int stride = runtime->frame_bits >> 3;
661 int processed = urb->transfer_buffer_length / stride;
663 spin_lock_irqsave(&subs->lock, flags);
664 if (processed > runtime->delay)
667 runtime->delay -= processed;
668 spin_unlock_irqrestore(&subs->lock, flags);
675 static struct snd_urb_ops audio_urb_ops[2] = {
677 .prepare = prepare_nodata_playback_urb,
678 .retire = retire_playback_urb,
679 .prepare_sync = prepare_playback_sync_urb,
680 .retire_sync = retire_playback_sync_urb,
683 .prepare = prepare_capture_urb,
684 .retire = retire_capture_urb,
685 .prepare_sync = prepare_capture_sync_urb,
686 .retire_sync = retire_capture_sync_urb,
690 static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
692 .prepare = prepare_nodata_playback_urb,
693 .retire = retire_playback_urb,
694 .prepare_sync = prepare_playback_sync_urb_hs,
695 .retire_sync = retire_playback_sync_urb_hs,
698 .prepare = prepare_capture_urb,
699 .retire = retire_capture_urb,
700 .prepare_sync = prepare_capture_sync_urb_hs,
701 .retire_sync = retire_capture_sync_urb,
706 * complete callback from data urb
708 static void snd_complete_urb(struct urb *urb)
710 struct snd_urb_ctx *ctx = urb->context;
711 struct snd_usb_substream *subs = ctx->subs;
712 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
715 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
716 !subs->running || /* can be stopped during retire callback */
717 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
718 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
719 clear_bit(ctx->index, &subs->active_mask);
721 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
722 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
729 * complete callback from sync urb
731 static void snd_complete_sync_urb(struct urb *urb)
733 struct snd_urb_ctx *ctx = urb->context;
734 struct snd_usb_substream *subs = ctx->subs;
735 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
738 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
739 !subs->running || /* can be stopped during retire callback */
740 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
741 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
742 clear_bit(ctx->index + 16, &subs->active_mask);
744 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
745 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
752 * unlink active urbs.
754 static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
761 if (!force && subs->stream->chip->shutdown) /* to be sure... */
764 async = !can_sleep && async_unlink;
766 if (!async && in_interrupt())
769 for (i = 0; i < subs->nurbs; i++) {
770 if (test_bit(i, &subs->active_mask)) {
771 if (!test_and_set_bit(i, &subs->unlink_mask)) {
772 struct urb *u = subs->dataurb[i].urb;
780 if (subs->syncpipe) {
781 for (i = 0; i < SYNC_URBS; i++) {
782 if (test_bit(i+16, &subs->active_mask)) {
783 if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
784 struct urb *u = subs->syncurb[i].urb;
797 static const char *usb_error_string(int err)
803 return "endpoint not enabled";
805 return "endpoint stalled";
807 return "not enough bandwidth";
809 return "device disabled";
811 return "device suspended";
816 return "internal error";
818 return "unknown error";
823 * set up and start data/sync urbs
825 static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
830 if (subs->stream->chip->shutdown)
833 for (i = 0; i < subs->nurbs; i++) {
834 if (snd_BUG_ON(!subs->dataurb[i].urb))
836 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
837 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
841 if (subs->syncpipe) {
842 for (i = 0; i < SYNC_URBS; i++) {
843 if (snd_BUG_ON(!subs->syncurb[i].urb))
845 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
846 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
852 subs->active_mask = 0;
853 subs->unlink_mask = 0;
855 for (i = 0; i < subs->nurbs; i++) {
856 err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
858 snd_printk(KERN_ERR "cannot submit datapipe "
859 "for urb %d, error %d: %s\n",
860 i, err, usb_error_string(err));
863 set_bit(i, &subs->active_mask);
865 if (subs->syncpipe) {
866 for (i = 0; i < SYNC_URBS; i++) {
867 err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
869 snd_printk(KERN_ERR "cannot submit syncpipe "
870 "for urb %d, error %d: %s\n",
871 i, err, usb_error_string(err));
874 set_bit(i + 16, &subs->active_mask);
880 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
881 deactivate_urbs(subs, 0, 0);
887 * wait until all urbs are processed.
889 static int wait_clear_urbs(struct snd_usb_substream *subs)
891 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
897 for (i = 0; i < subs->nurbs; i++) {
898 if (test_bit(i, &subs->active_mask))
901 if (subs->syncpipe) {
902 for (i = 0; i < SYNC_URBS; i++) {
903 if (test_bit(i + 16, &subs->active_mask))
909 schedule_timeout_uninterruptible(1);
910 } while (time_before(jiffies, end_time));
912 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
918 * return the current pcm pointer. just based on the hwptr_done value.
920 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
922 struct snd_usb_substream *subs;
923 unsigned int hwptr_done;
925 subs = (struct snd_usb_substream *)substream->runtime->private_data;
926 spin_lock(&subs->lock);
927 hwptr_done = subs->hwptr_done;
928 spin_unlock(&subs->lock);
929 return hwptr_done / (substream->runtime->frame_bits >> 3);
934 * start/stop playback substream
936 static int snd_usb_pcm_playback_trigger(struct snd_pcm_substream *substream,
939 struct snd_usb_substream *subs = substream->runtime->private_data;
942 case SNDRV_PCM_TRIGGER_START:
943 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
944 subs->ops.prepare = prepare_playback_urb;
946 case SNDRV_PCM_TRIGGER_STOP:
947 return deactivate_urbs(subs, 0, 0);
948 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
949 subs->ops.prepare = prepare_nodata_playback_urb;
957 * start/stop capture substream
959 static int snd_usb_pcm_capture_trigger(struct snd_pcm_substream *substream,
962 struct snd_usb_substream *subs = substream->runtime->private_data;
965 case SNDRV_PCM_TRIGGER_START:
966 subs->ops.retire = retire_capture_urb;
967 return start_urbs(subs, substream->runtime);
968 case SNDRV_PCM_TRIGGER_STOP:
969 return deactivate_urbs(subs, 0, 0);
970 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
971 subs->ops.retire = retire_paused_capture_urb;
973 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
974 subs->ops.retire = retire_capture_urb;
985 static void release_urb_ctx(struct snd_urb_ctx *u)
989 usb_buffer_free(u->subs->dev, u->buffer_size,
990 u->urb->transfer_buffer,
991 u->urb->transfer_dma);
992 usb_free_urb(u->urb);
998 * release a substream
1000 static void release_substream_urbs(struct snd_usb_substream *subs, int force)
1004 /* stop urbs (to be sure) */
1005 deactivate_urbs(subs, force, 1);
1006 wait_clear_urbs(subs);
1008 for (i = 0; i < MAX_URBS; i++)
1009 release_urb_ctx(&subs->dataurb[i]);
1010 for (i = 0; i < SYNC_URBS; i++)
1011 release_urb_ctx(&subs->syncurb[i]);
1012 usb_buffer_free(subs->dev, SYNC_URBS * 4,
1013 subs->syncbuf, subs->sync_dma);
1014 subs->syncbuf = NULL;
1019 * initialize a substream for plaback/capture
1021 static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes,
1022 unsigned int rate, unsigned int frame_bits)
1024 unsigned int maxsize, i;
1025 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1026 unsigned int urb_packs, total_packs, packs_per_ms;
1028 /* calculate the frequency in 16.16 format */
1029 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
1030 subs->freqn = get_usb_full_speed_rate(rate);
1032 subs->freqn = get_usb_high_speed_rate(rate);
1033 subs->freqm = subs->freqn;
1034 /* calculate max. frequency */
1035 if (subs->maxpacksize) {
1036 /* whatever fits into a max. size packet */
1037 maxsize = subs->maxpacksize;
1038 subs->freqmax = (maxsize / (frame_bits >> 3))
1039 << (16 - subs->datainterval);
1041 /* no max. packet size: just take 25% higher than nominal */
1042 subs->freqmax = subs->freqn + (subs->freqn >> 2);
1043 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
1044 >> (16 - subs->datainterval);
1049 subs->curpacksize = subs->maxpacksize;
1051 subs->curpacksize = maxsize;
1053 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
1054 packs_per_ms = 8 >> subs->datainterval;
1059 urb_packs = max(nrpacks, 1);
1060 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
1063 urb_packs *= packs_per_ms;
1065 urb_packs = min(urb_packs, 1U << subs->syncinterval);
1067 /* decide how many packets to be used */
1069 unsigned int minsize, maxpacks;
1070 /* determine how small a packet can be */
1071 minsize = (subs->freqn >> (16 - subs->datainterval))
1072 * (frame_bits >> 3);
1073 /* with sync from device, assume it can be 12% lower */
1075 minsize -= minsize >> 3;
1076 minsize = max(minsize, 1u);
1077 total_packs = (period_bytes + minsize - 1) / minsize;
1078 /* we need at least two URBs for queueing */
1079 if (total_packs < 2) {
1082 /* and we don't want too long a queue either */
1083 maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
1084 total_packs = min(total_packs, maxpacks);
1087 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
1089 total_packs = MAX_URBS * urb_packs;
1091 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
1092 if (subs->nurbs > MAX_URBS) {
1094 subs->nurbs = MAX_URBS;
1095 total_packs = MAX_URBS * urb_packs;
1096 } else if (subs->nurbs < 2) {
1097 /* too little - we need at least two packets
1098 * to ensure contiguous playback/capture
1103 /* allocate and initialize data urbs */
1104 for (i = 0; i < subs->nurbs; i++) {
1105 struct snd_urb_ctx *u = &subs->dataurb[i];
1108 u->packets = (i + 1) * total_packs / subs->nurbs
1109 - i * total_packs / subs->nurbs;
1110 u->buffer_size = maxsize * u->packets;
1111 if (subs->fmt_type == UAC_FORMAT_TYPE_II)
1112 u->packets++; /* for transfer delimiter */
1113 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1116 u->urb->transfer_buffer =
1117 usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL,
1118 &u->urb->transfer_dma);
1119 if (!u->urb->transfer_buffer)
1121 u->urb->pipe = subs->datapipe;
1122 u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1123 u->urb->interval = 1 << subs->datainterval;
1124 u->urb->context = u;
1125 u->urb->complete = snd_complete_urb;
1128 if (subs->syncpipe) {
1129 /* allocate and initialize sync urbs */
1130 subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4,
1131 GFP_KERNEL, &subs->sync_dma);
1134 for (i = 0; i < SYNC_URBS; i++) {
1135 struct snd_urb_ctx *u = &subs->syncurb[i];
1139 u->urb = usb_alloc_urb(1, GFP_KERNEL);
1142 u->urb->transfer_buffer = subs->syncbuf + i * 4;
1143 u->urb->transfer_dma = subs->sync_dma + i * 4;
1144 u->urb->transfer_buffer_length = 4;
1145 u->urb->pipe = subs->syncpipe;
1146 u->urb->transfer_flags = URB_ISO_ASAP |
1147 URB_NO_TRANSFER_DMA_MAP;
1148 u->urb->number_of_packets = 1;
1149 u->urb->interval = 1 << subs->syncinterval;
1150 u->urb->context = u;
1151 u->urb->complete = snd_complete_sync_urb;
1157 release_substream_urbs(subs, 0);
1163 * find a matching audio format
1165 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
1166 unsigned int rate, unsigned int channels)
1168 struct list_head *p;
1169 struct audioformat *found = NULL;
1170 int cur_attr = 0, attr;
1172 list_for_each(p, &subs->fmt_list) {
1173 struct audioformat *fp;
1174 fp = list_entry(p, struct audioformat, list);
1175 if (fp->format != format || fp->channels != channels)
1177 if (rate < fp->rate_min || rate > fp->rate_max)
1179 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
1181 for (i = 0; i < fp->nr_rates; i++)
1182 if (fp->rate_table[i] == rate)
1184 if (i >= fp->nr_rates)
1187 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
1193 /* avoid async out and adaptive in if the other method
1194 * supports the same format.
1195 * this is a workaround for the case like
1196 * M-audio audiophile USB.
1198 if (attr != cur_attr) {
1199 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
1200 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1201 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
1202 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
1204 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
1205 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1206 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
1207 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
1213 /* find the format with the largest max. packet size */
1214 if (fp->maxpacksize > found->maxpacksize) {
1224 * initialize the picth control and sample rate
1226 static int init_usb_pitch(struct usb_device *dev, int iface,
1227 struct usb_host_interface *alts,
1228 struct audioformat *fmt)
1231 unsigned char data[1];
1234 ep = get_endpoint(alts, 0)->bEndpointAddress;
1235 /* if endpoint has pitch control, enable it */
1236 if (fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL) {
1238 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
1239 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1240 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1241 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
1242 dev->devnum, iface, ep);
1249 static int init_usb_sample_rate(struct usb_device *dev, int iface,
1250 struct usb_host_interface *alts,
1251 struct audioformat *fmt, int rate)
1254 unsigned char data[3];
1257 ep = get_endpoint(alts, 0)->bEndpointAddress;
1258 /* if endpoint has sampling rate control, set it */
1259 if (fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE) {
1262 data[1] = rate >> 8;
1263 data[2] = rate >> 16;
1264 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
1265 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1266 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) {
1267 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
1268 dev->devnum, iface, fmt->altsetting, rate, ep);
1271 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
1272 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1273 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) {
1274 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
1275 dev->devnum, iface, fmt->altsetting, ep);
1276 return 0; /* some devices don't support reading */
1278 crate = data[0] | (data[1] << 8) | (data[2] << 16);
1279 if (crate != rate) {
1280 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
1281 // runtime->rate = crate;
1288 * For E-Mu 0404USB/0202USB/TrackerPre sample rate should be set for device,
1289 * not for interface.
1291 static void set_format_emu_quirk(struct snd_usb_substream *subs,
1292 struct audioformat *fmt)
1294 unsigned char emu_samplerate_id = 0;
1296 /* When capture is active
1297 * sample rate shouldn't be changed
1298 * by playback substream
1300 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1301 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
1305 switch (fmt->rate_min) {
1307 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1310 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1313 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1316 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1319 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1322 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1325 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
1329 * find a matching format and set up the interface
1331 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
1333 struct usb_device *dev = subs->dev;
1334 struct usb_host_interface *alts;
1335 struct usb_interface_descriptor *altsd;
1336 struct usb_interface *iface;
1337 unsigned int ep, attr;
1338 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1341 iface = usb_ifnum_to_if(dev, fmt->iface);
1342 if (WARN_ON(!iface))
1344 alts = &iface->altsetting[fmt->altset_idx];
1345 altsd = get_iface_desc(alts);
1346 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
1349 if (fmt == subs->cur_audiofmt)
1352 /* close the old interface */
1353 if (subs->interface >= 0 && subs->interface != fmt->iface) {
1354 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
1355 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
1356 dev->devnum, fmt->iface, fmt->altsetting);
1359 subs->interface = -1;
1364 if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
1365 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
1366 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
1367 dev->devnum, fmt->iface, fmt->altsetting);
1370 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
1371 subs->interface = fmt->iface;
1372 subs->format = fmt->altset_idx;
1375 /* create a data pipe */
1376 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
1378 subs->datapipe = usb_sndisocpipe(dev, ep);
1380 subs->datapipe = usb_rcvisocpipe(dev, ep);
1381 subs->datainterval = fmt->datainterval;
1382 subs->syncpipe = subs->syncinterval = 0;
1383 subs->maxpacksize = fmt->maxpacksize;
1386 /* we need a sync pipe in async OUT or adaptive IN mode */
1387 /* check the number of EP, since some devices have broken
1388 * descriptors which fool us. if it has only one EP,
1389 * assume it as adaptive-out or sync-in.
1391 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
1392 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
1393 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
1394 altsd->bNumEndpoints >= 2) {
1395 /* check sync-pipe endpoint */
1396 /* ... and check descriptor size before accessing bSynchAddress
1397 because there is a version of the SB Audigy 2 NX firmware lacking
1398 the audio fields in the endpoint descriptors */
1399 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
1400 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1401 get_endpoint(alts, 1)->bSynchAddress != 0)) {
1402 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1403 dev->devnum, fmt->iface, fmt->altsetting);
1406 ep = get_endpoint(alts, 1)->bEndpointAddress;
1407 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1408 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
1409 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
1410 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1411 dev->devnum, fmt->iface, fmt->altsetting);
1414 ep &= USB_ENDPOINT_NUMBER_MASK;
1416 subs->syncpipe = usb_rcvisocpipe(dev, ep);
1418 subs->syncpipe = usb_sndisocpipe(dev, ep);
1419 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1420 get_endpoint(alts, 1)->bRefresh >= 1 &&
1421 get_endpoint(alts, 1)->bRefresh <= 9)
1422 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
1423 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
1424 subs->syncinterval = 1;
1425 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
1426 get_endpoint(alts, 1)->bInterval <= 16)
1427 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
1429 subs->syncinterval = 3;
1432 /* always fill max packet size */
1433 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
1436 if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
1439 subs->cur_audiofmt = fmt;
1441 switch (subs->stream->chip->usb_id) {
1442 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1443 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1444 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
1445 set_format_emu_quirk(subs, fmt);
1451 "setting done: format = %d, rate = %d..%d, channels = %d\n",
1452 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
1454 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
1455 subs->datapipe, subs->syncpipe);
1462 * hw_params callback
1464 * allocate a buffer and set the given audio format.
1466 * so far we use a physically linear buffer although packetize transfer
1467 * doesn't need a continuous area.
1468 * if sg buffer is supported on the later version of alsa, we'll follow
1471 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
1472 struct snd_pcm_hw_params *hw_params)
1474 struct snd_usb_substream *subs = substream->runtime->private_data;
1475 struct audioformat *fmt;
1476 unsigned int channels, rate, format;
1479 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
1480 params_buffer_bytes(hw_params));
1484 format = params_format(hw_params);
1485 rate = params_rate(hw_params);
1486 channels = params_channels(hw_params);
1487 fmt = find_format(subs, format, rate, channels);
1489 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
1490 format, rate, channels);
1494 changed = subs->cur_audiofmt != fmt ||
1495 subs->period_bytes != params_period_bytes(hw_params) ||
1496 subs->cur_rate != rate;
1497 if ((ret = set_format(subs, fmt)) < 0)
1500 if (subs->cur_rate != rate) {
1501 struct usb_host_interface *alts;
1502 struct usb_interface *iface;
1503 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
1504 alts = &iface->altsetting[fmt->altset_idx];
1505 ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
1508 subs->cur_rate = rate;
1512 /* format changed */
1513 release_substream_urbs(subs, 0);
1514 /* influenced: period_bytes, channels, rate, format, */
1515 ret = init_substream_urbs(subs, params_period_bytes(hw_params),
1516 params_rate(hw_params),
1517 snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
1526 * reset the audio format and release the buffer
1528 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
1530 struct snd_usb_substream *subs = substream->runtime->private_data;
1532 subs->cur_audiofmt = NULL;
1534 subs->period_bytes = 0;
1535 if (!subs->stream->chip->shutdown)
1536 release_substream_urbs(subs, 0);
1537 return snd_pcm_lib_free_vmalloc_buffer(substream);
1543 * only a few subtle things...
1545 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
1547 struct snd_pcm_runtime *runtime = substream->runtime;
1548 struct snd_usb_substream *subs = runtime->private_data;
1550 if (! subs->cur_audiofmt) {
1551 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
1555 /* some unit conversions in runtime */
1556 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
1557 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
1559 /* reset the pointer */
1560 subs->hwptr_done = 0;
1561 subs->transfer_done = 0;
1565 /* clear urbs (to be sure) */
1566 deactivate_urbs(subs, 0, 1);
1567 wait_clear_urbs(subs);
1569 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1570 * updates for all URBs would happen at the same time when starting */
1571 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1572 subs->ops.prepare = prepare_nodata_playback_urb;
1573 return start_urbs(subs, runtime);
1578 static struct snd_pcm_hardware snd_usb_hardware =
1580 .info = SNDRV_PCM_INFO_MMAP |
1581 SNDRV_PCM_INFO_MMAP_VALID |
1582 SNDRV_PCM_INFO_BATCH |
1583 SNDRV_PCM_INFO_INTERLEAVED |
1584 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1585 SNDRV_PCM_INFO_PAUSE,
1586 .buffer_bytes_max = 1024 * 1024,
1587 .period_bytes_min = 64,
1588 .period_bytes_max = 512 * 1024,
1590 .periods_max = 1024,
1597 #ifdef HW_CONST_DEBUG
1598 #define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
1600 #define hwc_debug(fmt, args...) /**/
1603 static int hw_check_valid_format(struct snd_usb_substream *subs,
1604 struct snd_pcm_hw_params *params,
1605 struct audioformat *fp)
1607 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1608 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1609 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1610 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1613 /* check the format */
1614 if (!snd_mask_test(fmts, fp->format)) {
1615 hwc_debug(" > check: no supported format %d\n", fp->format);
1618 /* check the channels */
1619 if (fp->channels < ct->min || fp->channels > ct->max) {
1620 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1623 /* check the rate is within the range */
1624 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1625 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1628 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1629 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1632 /* check whether the period time is >= the data packet interval */
1633 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
1634 ptime = 125 * (1 << fp->datainterval);
1635 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
1636 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
1643 static int hw_rule_rate(struct snd_pcm_hw_params *params,
1644 struct snd_pcm_hw_rule *rule)
1646 struct snd_usb_substream *subs = rule->private;
1647 struct list_head *p;
1648 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1649 unsigned int rmin, rmax;
1652 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1655 list_for_each(p, &subs->fmt_list) {
1656 struct audioformat *fp;
1657 fp = list_entry(p, struct audioformat, list);
1658 if (!hw_check_valid_format(subs, params, fp))
1661 if (rmin > fp->rate_min)
1662 rmin = fp->rate_min;
1663 if (rmax < fp->rate_max)
1664 rmax = fp->rate_max;
1666 rmin = fp->rate_min;
1667 rmax = fp->rate_max;
1672 hwc_debug(" --> get empty\n");
1678 if (it->min < rmin) {
1683 if (it->max > rmax) {
1688 if (snd_interval_checkempty(it)) {
1692 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1697 static int hw_rule_channels(struct snd_pcm_hw_params *params,
1698 struct snd_pcm_hw_rule *rule)
1700 struct snd_usb_substream *subs = rule->private;
1701 struct list_head *p;
1702 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1703 unsigned int rmin, rmax;
1706 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1709 list_for_each(p, &subs->fmt_list) {
1710 struct audioformat *fp;
1711 fp = list_entry(p, struct audioformat, list);
1712 if (!hw_check_valid_format(subs, params, fp))
1715 if (rmin > fp->channels)
1716 rmin = fp->channels;
1717 if (rmax < fp->channels)
1718 rmax = fp->channels;
1720 rmin = fp->channels;
1721 rmax = fp->channels;
1726 hwc_debug(" --> get empty\n");
1732 if (it->min < rmin) {
1737 if (it->max > rmax) {
1742 if (snd_interval_checkempty(it)) {
1746 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1750 static int hw_rule_format(struct snd_pcm_hw_params *params,
1751 struct snd_pcm_hw_rule *rule)
1753 struct snd_usb_substream *subs = rule->private;
1754 struct list_head *p;
1755 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1760 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1762 list_for_each(p, &subs->fmt_list) {
1763 struct audioformat *fp;
1764 fp = list_entry(p, struct audioformat, list);
1765 if (!hw_check_valid_format(subs, params, fp))
1767 fbits |= (1ULL << fp->format);
1770 oldbits[0] = fmt->bits[0];
1771 oldbits[1] = fmt->bits[1];
1772 fmt->bits[0] &= (u32)fbits;
1773 fmt->bits[1] &= (u32)(fbits >> 32);
1774 if (!fmt->bits[0] && !fmt->bits[1]) {
1775 hwc_debug(" --> get empty\n");
1778 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1779 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1783 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1784 struct snd_pcm_hw_rule *rule)
1786 struct snd_usb_substream *subs = rule->private;
1787 struct audioformat *fp;
1788 struct snd_interval *it;
1789 unsigned char min_datainterval;
1793 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1794 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1795 min_datainterval = 0xff;
1796 list_for_each_entry(fp, &subs->fmt_list, list) {
1797 if (!hw_check_valid_format(subs, params, fp))
1799 min_datainterval = min(min_datainterval, fp->datainterval);
1801 if (min_datainterval == 0xff) {
1802 hwc_debug(" --> get emtpy\n");
1806 pmin = 125 * (1 << min_datainterval);
1808 if (it->min < pmin) {
1813 if (snd_interval_checkempty(it)) {
1817 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1822 * If the device supports unusual bit rates, does the request meet these?
1824 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1825 struct snd_usb_substream *subs)
1827 struct audioformat *fp;
1828 int count = 0, needs_knot = 0;
1831 list_for_each_entry(fp, &subs->fmt_list, list) {
1832 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1834 count += fp->nr_rates;
1835 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1841 subs->rate_list.count = count;
1842 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
1843 subs->rate_list.mask = 0;
1845 list_for_each_entry(fp, &subs->fmt_list, list) {
1847 for (i = 0; i < fp->nr_rates; i++)
1848 subs->rate_list.list[count++] = fp->rate_table[i];
1850 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1860 * set up the runtime hardware information.
1863 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1865 struct list_head *p;
1866 unsigned int pt, ptmin;
1867 int param_period_time_if_needed;
1870 runtime->hw.formats = subs->formats;
1872 runtime->hw.rate_min = 0x7fffffff;
1873 runtime->hw.rate_max = 0;
1874 runtime->hw.channels_min = 256;
1875 runtime->hw.channels_max = 0;
1876 runtime->hw.rates = 0;
1878 /* check min/max rates and channels */
1879 list_for_each(p, &subs->fmt_list) {
1880 struct audioformat *fp;
1881 fp = list_entry(p, struct audioformat, list);
1882 runtime->hw.rates |= fp->rates;
1883 if (runtime->hw.rate_min > fp->rate_min)
1884 runtime->hw.rate_min = fp->rate_min;
1885 if (runtime->hw.rate_max < fp->rate_max)
1886 runtime->hw.rate_max = fp->rate_max;
1887 if (runtime->hw.channels_min > fp->channels)
1888 runtime->hw.channels_min = fp->channels;
1889 if (runtime->hw.channels_max < fp->channels)
1890 runtime->hw.channels_max = fp->channels;
1891 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1892 /* FIXME: there might be more than one audio formats... */
1893 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1896 pt = 125 * (1 << fp->datainterval);
1897 ptmin = min(ptmin, pt);
1900 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1901 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
1902 /* full speed devices have fixed data packet interval */
1905 /* if period time doesn't go below 1 ms, no rules needed */
1906 param_period_time_if_needed = -1;
1907 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1910 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1912 SNDRV_PCM_HW_PARAM_FORMAT,
1913 SNDRV_PCM_HW_PARAM_CHANNELS,
1914 param_period_time_if_needed,
1917 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1918 hw_rule_channels, subs,
1919 SNDRV_PCM_HW_PARAM_FORMAT,
1920 SNDRV_PCM_HW_PARAM_RATE,
1921 param_period_time_if_needed,
1924 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1925 hw_rule_format, subs,
1926 SNDRV_PCM_HW_PARAM_RATE,
1927 SNDRV_PCM_HW_PARAM_CHANNELS,
1928 param_period_time_if_needed,
1931 if (param_period_time_if_needed >= 0) {
1932 err = snd_pcm_hw_rule_add(runtime, 0,
1933 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1934 hw_rule_period_time, subs,
1935 SNDRV_PCM_HW_PARAM_FORMAT,
1936 SNDRV_PCM_HW_PARAM_CHANNELS,
1937 SNDRV_PCM_HW_PARAM_RATE,
1942 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
1947 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1949 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1950 struct snd_pcm_runtime *runtime = substream->runtime;
1951 struct snd_usb_substream *subs = &as->substream[direction];
1953 subs->interface = -1;
1955 runtime->hw = snd_usb_hardware;
1956 runtime->private_data = subs;
1957 subs->pcm_substream = substream;
1958 return setup_hw_info(runtime, subs);
1961 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1963 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1964 struct snd_usb_substream *subs = &as->substream[direction];
1966 if (!as->chip->shutdown && subs->interface >= 0) {
1967 usb_set_interface(subs->dev, subs->interface, 0);
1968 subs->interface = -1;
1970 subs->pcm_substream = NULL;
1974 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1976 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1979 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1981 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1984 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1986 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1989 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1991 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1994 static struct snd_pcm_ops snd_usb_playback_ops = {
1995 .open = snd_usb_playback_open,
1996 .close = snd_usb_playback_close,
1997 .ioctl = snd_pcm_lib_ioctl,
1998 .hw_params = snd_usb_hw_params,
1999 .hw_free = snd_usb_hw_free,
2000 .prepare = snd_usb_pcm_prepare,
2001 .trigger = snd_usb_pcm_playback_trigger,
2002 .pointer = snd_usb_pcm_pointer,
2003 .page = snd_pcm_lib_get_vmalloc_page,
2004 .mmap = snd_pcm_lib_mmap_vmalloc,
2007 static struct snd_pcm_ops snd_usb_capture_ops = {
2008 .open = snd_usb_capture_open,
2009 .close = snd_usb_capture_close,
2010 .ioctl = snd_pcm_lib_ioctl,
2011 .hw_params = snd_usb_hw_params,
2012 .hw_free = snd_usb_hw_free,
2013 .prepare = snd_usb_pcm_prepare,
2014 .trigger = snd_usb_pcm_capture_trigger,
2015 .pointer = snd_usb_pcm_pointer,
2016 .page = snd_pcm_lib_get_vmalloc_page,
2017 .mmap = snd_pcm_lib_mmap_vmalloc,
2027 * combine bytes and get an integer value
2029 unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
2032 case 1: return *bytes;
2033 case 2: return combine_word(bytes);
2034 case 3: return combine_triple(bytes);
2035 case 4: return combine_quad(bytes);
2041 * parse descriptor buffer and return the pointer starting the given
2044 void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
2056 if (p[1] == dtype && (!after || (void *)p > after)) {
2065 * find a class-specified interface descriptor with the given subtype.
2067 void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
2069 unsigned char *p = after;
2071 while ((p = snd_usb_find_desc(buffer, buflen, p,
2072 USB_DT_CS_INTERFACE)) != NULL) {
2073 if (p[0] >= 3 && p[2] == dsubtype)
2080 * Wrapper for usb_control_msg().
2081 * Allocates a temp buffer to prevent dmaing from/to the stack.
2083 int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
2084 __u8 requesttype, __u16 value, __u16 index, void *data,
2085 __u16 size, int timeout)
2091 buf = kmemdup(data, size, GFP_KERNEL);
2095 err = usb_control_msg(dev, pipe, request, requesttype,
2096 value, index, buf, size, timeout);
2098 memcpy(data, buf, size);
2106 * entry point for linux usb interface
2109 static int usb_audio_probe(struct usb_interface *intf,
2110 const struct usb_device_id *id);
2111 static void usb_audio_disconnect(struct usb_interface *intf);
2114 static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message);
2115 static int usb_audio_resume(struct usb_interface *intf);
2117 #define usb_audio_suspend NULL
2118 #define usb_audio_resume NULL
2121 static struct usb_device_id usb_audio_ids [] = {
2122 #include "usbquirks.h"
2123 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
2124 .bInterfaceClass = USB_CLASS_AUDIO,
2125 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
2126 { } /* Terminating entry */
2129 MODULE_DEVICE_TABLE (usb, usb_audio_ids);
2131 static struct usb_driver usb_audio_driver = {
2132 .name = "snd-usb-audio",
2133 .probe = usb_audio_probe,
2134 .disconnect = usb_audio_disconnect,
2135 .suspend = usb_audio_suspend,
2136 .resume = usb_audio_resume,
2137 .id_table = usb_audio_ids,
2141 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SND_VERBOSE_PROCFS)
2144 * proc interface for list the supported pcm formats
2146 static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
2148 struct list_head *p;
2149 static char *sync_types[4] = {
2150 "NONE", "ASYNC", "ADAPTIVE", "SYNC"
2153 list_for_each(p, &subs->fmt_list) {
2154 struct audioformat *fp;
2155 fp = list_entry(p, struct audioformat, list);
2156 snd_iprintf(buffer, " Interface %d\n", fp->iface);
2157 snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
2158 snd_iprintf(buffer, " Format: %s\n",
2159 snd_pcm_format_name(fp->format));
2160 snd_iprintf(buffer, " Channels: %d\n", fp->channels);
2161 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
2162 fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
2163 fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
2164 sync_types[(fp->ep_attr & USB_ENDPOINT_SYNCTYPE) >> 2]);
2165 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
2166 snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
2167 fp->rate_min, fp->rate_max);
2170 snd_iprintf(buffer, " Rates: ");
2171 for (i = 0; i < fp->nr_rates; i++) {
2173 snd_iprintf(buffer, ", ");
2174 snd_iprintf(buffer, "%d", fp->rate_table[i]);
2176 snd_iprintf(buffer, "\n");
2178 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
2179 snd_iprintf(buffer, " Data packet interval: %d us\n",
2180 125 * (1 << fp->datainterval));
2181 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
2182 // snd_iprintf(buffer, " EP Attribute = %#x\n", fp->attributes);
2186 static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
2188 if (subs->running) {
2190 snd_iprintf(buffer, " Status: Running\n");
2191 snd_iprintf(buffer, " Interface = %d\n", subs->interface);
2192 snd_iprintf(buffer, " Altset = %d\n", subs->format);
2193 snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
2194 for (i = 0; i < subs->nurbs; i++)
2195 snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
2196 snd_iprintf(buffer, "]\n");
2197 snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
2198 snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
2199 snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
2200 ? get_full_speed_hz(subs->freqm)
2201 : get_high_speed_hz(subs->freqm),
2202 subs->freqm >> 16, subs->freqm & 0xffff);
2204 snd_iprintf(buffer, " Status: Stop\n");
2208 static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
2210 struct snd_usb_stream *stream = entry->private_data;
2212 snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
2214 if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
2215 snd_iprintf(buffer, "\nPlayback:\n");
2216 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2217 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2219 if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
2220 snd_iprintf(buffer, "\nCapture:\n");
2221 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2222 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2226 static void proc_pcm_format_add(struct snd_usb_stream *stream)
2228 struct snd_info_entry *entry;
2230 struct snd_card *card = stream->chip->card;
2232 sprintf(name, "stream%d", stream->pcm_index);
2233 if (!snd_card_proc_new(card, name, &entry))
2234 snd_info_set_text_ops(entry, stream, proc_pcm_format_read);
2239 static inline void proc_pcm_format_add(struct snd_usb_stream *stream)
2246 * initialize the substream instance.
2249 static void init_substream(struct snd_usb_stream *as, int stream, struct audioformat *fp)
2251 struct snd_usb_substream *subs = &as->substream[stream];
2253 INIT_LIST_HEAD(&subs->fmt_list);
2254 spin_lock_init(&subs->lock);
2257 subs->direction = stream;
2258 subs->dev = as->chip->dev;
2259 subs->txfr_quirk = as->chip->txfr_quirk;
2260 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) {
2261 subs->ops = audio_urb_ops[stream];
2263 subs->ops = audio_urb_ops_high_speed[stream];
2264 switch (as->chip->usb_id) {
2265 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
2266 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
2267 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
2268 subs->ops.retire_sync = retire_playback_sync_urb_hs_emu;
2272 snd_pcm_set_ops(as->pcm, stream,
2273 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2274 &snd_usb_playback_ops : &snd_usb_capture_ops);
2276 list_add_tail(&fp->list, &subs->fmt_list);
2277 subs->formats |= 1ULL << fp->format;
2278 subs->endpoint = fp->endpoint;
2279 subs->num_formats++;
2280 subs->fmt_type = fp->fmt_type;
2287 static void free_substream(struct snd_usb_substream *subs)
2289 struct list_head *p, *n;
2291 if (!subs->num_formats)
2292 return; /* not initialized */
2293 list_for_each_safe(p, n, &subs->fmt_list) {
2294 struct audioformat *fp = list_entry(p, struct audioformat, list);
2295 kfree(fp->rate_table);
2298 kfree(subs->rate_list.list);
2303 * free a usb stream instance
2305 static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
2307 free_substream(&stream->substream[0]);
2308 free_substream(&stream->substream[1]);
2309 list_del(&stream->list);
2313 static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
2315 struct snd_usb_stream *stream = pcm->private_data;
2318 snd_usb_audio_stream_free(stream);
2324 * add this endpoint to the chip instance.
2325 * if a stream with the same endpoint already exists, append to it.
2326 * if not, create a new pcm stream.
2328 static int add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)
2330 struct list_head *p;
2331 struct snd_usb_stream *as;
2332 struct snd_usb_substream *subs;
2333 struct snd_pcm *pcm;
2336 list_for_each(p, &chip->pcm_list) {
2337 as = list_entry(p, struct snd_usb_stream, list);
2338 if (as->fmt_type != fp->fmt_type)
2340 subs = &as->substream[stream];
2341 if (!subs->endpoint)
2343 if (subs->endpoint == fp->endpoint) {
2344 list_add_tail(&fp->list, &subs->fmt_list);
2345 subs->num_formats++;
2346 subs->formats |= 1ULL << fp->format;
2350 /* look for an empty stream */
2351 list_for_each(p, &chip->pcm_list) {
2352 as = list_entry(p, struct snd_usb_stream, list);
2353 if (as->fmt_type != fp->fmt_type)
2355 subs = &as->substream[stream];
2358 err = snd_pcm_new_stream(as->pcm, stream, 1);
2361 init_substream(as, stream, fp);
2365 /* create a new pcm */
2366 as = kzalloc(sizeof(*as), GFP_KERNEL);
2369 as->pcm_index = chip->pcm_devs;
2371 as->fmt_type = fp->fmt_type;
2372 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
2373 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
2374 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
2381 pcm->private_data = as;
2382 pcm->private_free = snd_usb_audio_pcm_free;
2383 pcm->info_flags = 0;
2384 if (chip->pcm_devs > 0)
2385 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
2387 strcpy(pcm->name, "USB Audio");
2389 init_substream(as, stream, fp);
2391 list_add(&as->list, &chip->pcm_list);
2394 proc_pcm_format_add(as);
2401 * check if the device uses big-endian samples
2403 static int is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
2405 switch (chip->usb_id) {
2406 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
2407 if (fp->endpoint & USB_DIR_IN)
2410 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2411 if (device_setup[chip->index] == 0x00 ||
2412 fp->altsetting==1 || fp->altsetting==2 || fp->altsetting==3)
2419 * parse the audio format type I descriptor
2420 * and returns the corresponding pcm format
2423 * @fp: audioformat record
2424 * @format: the format tag (wFormatTag)
2425 * @fmt: the format type descriptor
2427 static int parse_audio_format_i_type(struct snd_usb_audio *chip,
2428 struct audioformat *fp,
2429 int format, void *_fmt,
2433 int sample_width, sample_bytes;
2436 case UAC_VERSION_1: {
2437 struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
2438 sample_width = fmt->bBitResolution;
2439 sample_bytes = fmt->bSubframeSize;
2443 case UAC_VERSION_2: {
2444 struct uac_format_type_i_ext_descriptor *fmt = _fmt;
2445 sample_width = fmt->bBitResolution;
2446 sample_bytes = fmt->bSubslotSize;
2450 * USB audio class v2 devices specify a bitmap of possible
2451 * audio formats rather than one fix value. For now, we just
2452 * pick one of them and report that as the only possible
2453 * value for this setting.
2454 * The bit allocation map is in fact compatible to the
2455 * wFormatTag of the v1 AS streaming descriptors, which is why
2456 * we can simply map the matrix.
2459 for (i = 0; i < 5; i++)
2460 if (format & (1UL << i)) {
2472 /* FIXME: correct endianess and sign? */
2476 case UAC_FORMAT_TYPE_I_UNDEFINED: /* some devices don't define this correctly... */
2477 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
2478 chip->dev->devnum, fp->iface, fp->altsetting);
2480 case UAC_FORMAT_TYPE_I_PCM:
2481 if (sample_width > sample_bytes * 8) {
2482 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
2483 chip->dev->devnum, fp->iface, fp->altsetting,
2484 sample_width, sample_bytes);
2486 /* check the format byte size */
2487 switch (sample_bytes) {
2489 pcm_format = SNDRV_PCM_FORMAT_S8;
2492 if (is_big_endian_format(chip, fp))
2493 pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
2495 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2498 if (is_big_endian_format(chip, fp))
2499 pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
2501 pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
2504 pcm_format = SNDRV_PCM_FORMAT_S32_LE;
2507 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
2508 chip->dev->devnum, fp->iface, fp->altsetting,
2509 sample_width, sample_bytes);
2513 case UAC_FORMAT_TYPE_I_PCM8:
2514 pcm_format = SNDRV_PCM_FORMAT_U8;
2516 /* Dallas DS4201 workaround: it advertises U8 format, but really
2518 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
2519 pcm_format = SNDRV_PCM_FORMAT_S8;
2521 case UAC_FORMAT_TYPE_I_IEEE_FLOAT:
2522 pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
2524 case UAC_FORMAT_TYPE_I_ALAW:
2525 pcm_format = SNDRV_PCM_FORMAT_A_LAW;
2527 case UAC_FORMAT_TYPE_I_MULAW:
2528 pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
2531 snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
2532 chip->dev->devnum, fp->iface, fp->altsetting, format);
2540 * parse the format descriptor and stores the possible sample rates
2541 * on the audioformat table (audio class v1).
2544 * @fp: audioformat record
2545 * @fmt: the format descriptor
2546 * @offset: the start offset of descriptor pointing the rate type
2547 * (7 for type I and II, 8 for type II)
2549 static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
2550 unsigned char *fmt, int offset)
2552 int nr_rates = fmt[offset];
2554 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
2555 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
2556 chip->dev->devnum, fp->iface, fp->altsetting);
2562 * build the rate table and bitmap flags
2566 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2567 if (fp->rate_table == NULL) {
2568 snd_printk(KERN_ERR "cannot malloc\n");
2573 fp->rate_min = fp->rate_max = 0;
2574 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
2575 unsigned int rate = combine_triple(&fmt[idx]);
2578 /* C-Media CM6501 mislabels its 96 kHz altsetting */
2579 if (rate == 48000 && nr_rates == 1 &&
2580 (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
2581 chip->usb_id == USB_ID(0x0d8c, 0x0102)) &&
2582 fp->altsetting == 5 && fp->maxpacksize == 392)
2584 /* Creative VF0470 Live Cam reports 16 kHz instead of 8kHz */
2585 if (rate == 16000 && chip->usb_id == USB_ID(0x041e, 0x4068))
2587 fp->rate_table[fp->nr_rates] = rate;
2588 if (!fp->rate_min || rate < fp->rate_min)
2589 fp->rate_min = rate;
2590 if (!fp->rate_max || rate > fp->rate_max)
2591 fp->rate_max = rate;
2592 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
2595 if (!fp->nr_rates) {
2596 hwc_debug("All rates were zero. Skipping format!\n");
2600 /* continuous rates */
2601 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
2602 fp->rate_min = combine_triple(&fmt[offset + 1]);
2603 fp->rate_max = combine_triple(&fmt[offset + 4]);
2609 * parse the format descriptor and stores the possible sample rates
2610 * on the audioformat table (audio class v2).
2612 static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,
2613 struct audioformat *fp,
2614 struct usb_host_interface *iface)
2616 struct usb_device *dev = chip->dev;
2617 unsigned char tmp[2], *data;
2618 int i, nr_rates, data_size, ret = 0;
2620 /* get the number of sample rates first by only fetching 2 bytes */
2621 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
2622 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
2623 0x0100, chip->clock_id << 8, tmp, sizeof(tmp), 1000);
2626 snd_printk(KERN_ERR "unable to retrieve number of sample rates\n");
2630 nr_rates = (tmp[1] << 8) | tmp[0];
2631 data_size = 2 + 12 * nr_rates;
2632 data = kzalloc(data_size, GFP_KERNEL);
2638 /* now get the full information */
2639 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
2640 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
2641 0x0100, chip->clock_id << 8, data, data_size, 1000);
2644 snd_printk(KERN_ERR "unable to retrieve sample rate range\n");
2649 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2650 if (!fp->rate_table) {
2656 fp->rate_min = fp->rate_max = 0;
2658 for (i = 0; i < nr_rates; i++) {
2659 int rate = combine_quad(&data[2 + 12 * i]);
2661 fp->rate_table[fp->nr_rates] = rate;
2662 if (!fp->rate_min || rate < fp->rate_min)
2663 fp->rate_min = rate;
2664 if (!fp->rate_max || rate > fp->rate_max)
2665 fp->rate_max = rate;
2666 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
2677 * parse the format type I and III descriptors
2679 static int parse_audio_format_i(struct snd_usb_audio *chip,
2680 struct audioformat *fp,
2681 int format, void *_fmt,
2682 struct usb_host_interface *iface)
2684 struct usb_interface_descriptor *altsd = get_iface_desc(iface);
2685 struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
2686 int protocol = altsd->bInterfaceProtocol;
2687 int pcm_format, ret;
2689 if (fmt->bFormatType == UAC_FORMAT_TYPE_III) {
2690 /* FIXME: the format type is really IECxxx
2691 * but we give normal PCM format to get the existing
2694 switch (chip->usb_id) {
2696 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2697 if (device_setup[chip->index] == 0x00 &&
2698 fp->altsetting == 6)
2699 pcm_format = SNDRV_PCM_FORMAT_S16_BE;
2701 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2704 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2707 pcm_format = parse_audio_format_i_type(chip, fp, format, fmt, protocol);
2712 fp->format = pcm_format;
2714 /* gather possible sample rates */
2715 /* audio class v1 reports possible sample rates as part of the
2716 * proprietary class specific descriptor.
2717 * audio class v2 uses class specific EP0 range requests for that.
2721 fp->channels = fmt->bNrChannels;
2722 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 7);
2725 /* fp->channels is already set in this case */
2726 ret = parse_audio_format_rates_v2(chip, fp, iface);
2730 if (fp->channels < 1) {
2731 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
2732 chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
2740 * parse the format type II descriptor
2742 static int parse_audio_format_ii(struct snd_usb_audio *chip,
2743 struct audioformat *fp,
2744 int format, void *_fmt,
2745 struct usb_host_interface *iface)
2747 int brate, framesize, ret;
2748 struct usb_interface_descriptor *altsd = get_iface_desc(iface);
2749 int protocol = altsd->bInterfaceProtocol;
2752 case UAC_FORMAT_TYPE_II_AC3:
2753 /* FIXME: there is no AC3 format defined yet */
2754 // fp->format = SNDRV_PCM_FORMAT_AC3;
2755 fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
2757 case UAC_FORMAT_TYPE_II_MPEG:
2758 fp->format = SNDRV_PCM_FORMAT_MPEG;
2761 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",
2762 chip->dev->devnum, fp->iface, fp->altsetting, format);
2763 fp->format = SNDRV_PCM_FORMAT_MPEG;
2770 case UAC_VERSION_1: {
2771 struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
2772 brate = le16_to_cpu(fmt->wMaxBitRate);
2773 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
2774 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
2775 fp->frame_size = framesize;
2776 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
2779 case UAC_VERSION_2: {
2780 struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
2781 brate = le16_to_cpu(fmt->wMaxBitRate);
2782 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
2783 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
2784 fp->frame_size = framesize;
2785 ret = parse_audio_format_rates_v2(chip, fp, iface);
2793 static int parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,
2794 int format, unsigned char *fmt, int stream,
2795 struct usb_host_interface *iface)
2800 case UAC_FORMAT_TYPE_I:
2801 case UAC_FORMAT_TYPE_III:
2802 err = parse_audio_format_i(chip, fp, format, fmt, iface);
2804 case UAC_FORMAT_TYPE_II:
2805 err = parse_audio_format_ii(chip, fp, format, fmt, iface);
2808 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
2809 chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);
2812 fp->fmt_type = fmt[3];
2816 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
2817 /* extigy apparently supports sample rates other than 48k
2818 * but not in ordinary way. so we enable only 48k atm.
2820 if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
2821 chip->usb_id == USB_ID(0x041e, 0x3020) ||
2822 chip->usb_id == USB_ID(0x041e, 0x3061)) {
2823 if (fmt[3] == UAC_FORMAT_TYPE_I &&
2824 fp->rates != SNDRV_PCM_RATE_48000 &&
2825 fp->rates != SNDRV_PCM_RATE_96000)
2832 static unsigned char parse_datainterval(struct snd_usb_audio *chip,
2833 struct usb_host_interface *alts)
2835 if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH &&
2836 get_endpoint(alts, 0)->bInterval >= 1 &&
2837 get_endpoint(alts, 0)->bInterval <= 4)
2838 return get_endpoint(alts, 0)->bInterval - 1;
2843 static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
2844 int iface, int altno);
2845 static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
2847 struct usb_device *dev;
2848 struct usb_interface *iface;
2849 struct usb_host_interface *alts;
2850 struct usb_interface_descriptor *altsd;
2851 int i, altno, err, stream;
2852 int format = 0, num_channels = 0;
2853 struct audioformat *fp = NULL;
2854 unsigned char *fmt, *csep;
2859 /* parse the interface's altsettings */
2860 iface = usb_ifnum_to_if(dev, iface_no);
2862 num = iface->num_altsetting;
2865 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
2866 * one misses syncpipe, and does not produce any sound.
2868 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
2871 for (i = 0; i < num; i++) {
2872 alts = &iface->altsetting[i];
2873 altsd = get_iface_desc(alts);
2874 protocol = altsd->bInterfaceProtocol;
2875 /* skip invalid one */
2876 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2877 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2878 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
2879 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
2880 altsd->bNumEndpoints < 1 ||
2881 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
2883 /* must be isochronous */
2884 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2885 USB_ENDPOINT_XFER_ISOC)
2887 /* check direction */
2888 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
2889 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2890 altno = altsd->bAlternateSetting;
2892 /* audiophile usb: skip altsets incompatible with device_setup
2894 if (chip->usb_id == USB_ID(0x0763, 0x2003) &&
2895 audiophile_skip_setting_quirk(chip, iface_no, altno))
2898 /* get audio formats */
2900 case UAC_VERSION_1: {
2901 struct uac_as_header_descriptor_v1 *as =
2902 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
2905 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
2906 dev->devnum, iface_no, altno);
2910 if (as->bLength < sizeof(*as)) {
2911 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
2912 dev->devnum, iface_no, altno);
2916 format = le16_to_cpu(as->wFormatTag); /* remember the format value */
2920 case UAC_VERSION_2: {
2921 struct uac_as_header_descriptor_v2 *as =
2922 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
2925 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
2926 dev->devnum, iface_no, altno);
2930 if (as->bLength < sizeof(*as)) {
2931 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
2932 dev->devnum, iface_no, altno);
2936 num_channels = as->bNrChannels;
2937 format = le32_to_cpu(as->bmFormats);
2943 snd_printk(KERN_ERR "%d:%u:%d : unknown interface protocol %04x\n",
2944 dev->devnum, iface_no, altno, protocol);
2948 /* get format type */
2949 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
2951 snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
2952 dev->devnum, iface_no, altno);
2955 if (((protocol == UAC_VERSION_1) && (fmt[0] < 8)) ||
2956 ((protocol == UAC_VERSION_2) && (fmt[0] != 6))) {
2957 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
2958 dev->devnum, iface_no, altno);
2963 * Blue Microphones workaround: The last altsetting is identical
2964 * with the previous one, except for a larger packet size, but
2965 * is actually a mislabeled two-channel setting; ignore it.
2967 if (fmt[4] == 1 && fmt[5] == 2 && altno == 2 && num == 3 &&
2968 fp && fp->altsetting == 1 && fp->channels == 1 &&
2969 fp->format == SNDRV_PCM_FORMAT_S16_LE &&
2970 protocol == UAC_VERSION_1 &&
2971 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
2972 fp->maxpacksize * 2)
2975 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
2976 /* Creamware Noah has this descriptor after the 2nd endpoint */
2977 if (!csep && altsd->bNumEndpoints >= 2)
2978 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
2979 if (!csep || csep[0] < 7 || csep[2] != UAC_EP_GENERAL) {
2980 snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
2981 " class specific endpoint descriptor\n",
2982 dev->devnum, iface_no, altno);
2986 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
2988 snd_printk(KERN_ERR "cannot malloc\n");
2992 fp->iface = iface_no;
2993 fp->altsetting = altno;
2995 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2996 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2997 fp->datainterval = parse_datainterval(chip, alts);
2998 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2999 /* num_channels is only set for v2 interfaces */
3000 fp->channels = num_channels;
3001 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
3002 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
3003 * (fp->maxpacksize & 0x7ff);
3004 fp->attributes = csep ? csep[3] : 0;
3006 /* some quirks for attributes here */
3008 switch (chip->usb_id) {
3009 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
3010 /* Optoplay sets the sample rate attribute although
3011 * it seems not supporting it in fact.
3013 fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
3015 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
3016 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
3017 /* doesn't set the sample rate attribute, but supports it */
3018 fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
3020 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
3021 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
3022 an older model 77d:223) */
3024 * plantronics headset and Griffin iMic have set adaptive-in
3025 * although it's really not...
3027 fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
3028 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3029 fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
3031 fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
3035 /* ok, let's parse further... */
3036 if (parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {
3037 kfree(fp->rate_table);
3042 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
3043 err = add_audio_endpoint(chip, stream, fp);
3045 kfree(fp->rate_table);
3049 /* try to set the interface... */
3050 usb_set_interface(chip->dev, iface_no, altno);
3051 init_usb_pitch(chip->dev, iface_no, alts, fp);
3052 init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
3059 * disconnect streams
3060 * called from snd_usb_audio_disconnect()
3062 static void snd_usb_stream_disconnect(struct list_head *head)
3065 struct snd_usb_stream *as;
3066 struct snd_usb_substream *subs;
3068 as = list_entry(head, struct snd_usb_stream, list);
3069 for (idx = 0; idx < 2; idx++) {
3070 subs = &as->substream[idx];
3071 if (!subs->num_formats)
3073 release_substream_urbs(subs, 1);
3074 subs->interface = -1;
3078 static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
3080 struct usb_device *dev = chip->dev;
3081 struct usb_host_interface *alts;
3082 struct usb_interface_descriptor *altsd;
3083 struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
3086 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
3087 dev->devnum, ctrlif, interface);
3091 if (usb_interface_claimed(iface)) {
3092 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",
3093 dev->devnum, ctrlif, interface);
3097 alts = &iface->altsetting[0];
3098 altsd = get_iface_desc(alts);
3099 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
3100 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
3101 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
3102 int err = snd_usbmidi_create(chip->card, iface,
3103 &chip->midi_list, NULL);
3105 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",
3106 dev->devnum, ctrlif, interface);
3109 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
3114 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
3115 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
3116 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
3117 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",
3118 dev->devnum, ctrlif, interface, altsd->bInterfaceClass);
3119 /* skip non-supported classes */
3123 if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
3124 snd_printk(KERN_ERR "low speed audio streaming not supported\n");
3128 if (! parse_audio_endpoints(chip, interface)) {
3129 usb_set_interface(dev, interface, 0); /* reset the current interface */
3130 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
3138 * parse audio control descriptor and create pcm/midi streams
3140 static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
3142 struct usb_device *dev = chip->dev;
3143 struct usb_host_interface *host_iface;
3144 struct usb_interface_descriptor *altsd;
3145 void *control_header;
3148 /* find audiocontrol interface */
3149 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
3150 control_header = snd_usb_find_csint_desc(host_iface->extra,
3151 host_iface->extralen,
3153 altsd = get_iface_desc(host_iface);
3154 protocol = altsd->bInterfaceProtocol;
3156 if (!control_header) {
3157 snd_printk(KERN_ERR "cannot find UAC_HEADER\n");
3162 case UAC_VERSION_1: {
3163 struct uac_ac_header_descriptor_v1 *h1 = control_header;
3165 if (!h1->bInCollection) {
3166 snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");
3170 if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
3171 snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");
3175 for (i = 0; i < h1->bInCollection; i++)
3176 snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
3181 case UAC_VERSION_2: {
3182 struct uac_clock_source_descriptor *cs;
3183 struct usb_interface_assoc_descriptor *assoc =
3184 usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
3187 snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
3191 /* FIXME: for now, we expect there is at least one clock source
3192 * descriptor and we always take the first one.
3193 * We should properly support devices with multiple clock sources,
3194 * clock selectors and sample rate conversion units. */
3196 cs = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen,
3197 NULL, UAC_CLOCK_SOURCE);
3200 snd_printk(KERN_ERR "CLOCK_SOURCE descriptor not found\n");
3204 chip->clock_id = cs->bClockID;
3206 for (i = 0; i < assoc->bInterfaceCount; i++) {
3207 int intf = assoc->bFirstInterface + i;
3210 snd_usb_create_stream(chip, ctrlif, intf);
3217 snd_printk(KERN_ERR "unknown protocol version 0x%02x\n", protocol);
3225 * create a stream for an endpoint/altsetting without proper descriptors
3227 static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
3228 struct usb_interface *iface,
3229 const struct snd_usb_audio_quirk *quirk)
3231 struct audioformat *fp;
3232 struct usb_host_interface *alts;
3234 unsigned *rate_table = NULL;
3236 fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
3238 snd_printk(KERN_ERR "cannot memdup\n");
3241 if (fp->nr_rates > 0) {
3242 rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
3247 memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
3248 fp->rate_table = rate_table;
3251 stream = (fp->endpoint & USB_DIR_IN)
3252 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
3253 err = add_audio_endpoint(chip, stream, fp);
3259 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
3260 fp->altset_idx >= iface->num_altsetting) {
3265 alts = &iface->altsetting[fp->altset_idx];
3266 fp->datainterval = parse_datainterval(chip, alts);
3267 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
3268 usb_set_interface(chip->dev, fp->iface, 0);
3269 init_usb_pitch(chip->dev, fp->iface, alts, fp);
3270 init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
3275 * create a stream for an interface with proper descriptors
3277 static int create_standard_audio_quirk(struct snd_usb_audio *chip,
3278 struct usb_interface *iface,
3279 const struct snd_usb_audio_quirk *quirk)
3281 struct usb_host_interface *alts;
3282 struct usb_interface_descriptor *altsd;
3285 alts = &iface->altsetting[0];
3286 altsd = get_iface_desc(alts);
3287 err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
3289 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
3290 altsd->bInterfaceNumber, err);
3293 /* reset the current interface */
3294 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
3299 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
3300 * The only way to detect the sample rate is by looking at wMaxPacketSize.
3302 static int create_uaxx_quirk(struct snd_usb_audio *chip,
3303 struct usb_interface *iface,
3304 const struct snd_usb_audio_quirk *quirk)
3306 static const struct audioformat ua_format = {
3307 .format = SNDRV_PCM_FORMAT_S24_3LE,
3309 .fmt_type = UAC_FORMAT_TYPE_I,
3312 .rates = SNDRV_PCM_RATE_CONTINUOUS,
3314 struct usb_host_interface *alts;
3315 struct usb_interface_descriptor *altsd;
3316 struct audioformat *fp;
3319 /* both PCM and MIDI interfaces have 2 or more altsettings */
3320 if (iface->num_altsetting < 2)
3322 alts = &iface->altsetting[1];
3323 altsd = get_iface_desc(alts);
3325 if (altsd->bNumEndpoints == 2) {
3326 static const struct snd_usb_midi_endpoint_info ua700_ep = {
3327 .out_cables = 0x0003,
3330 static const struct snd_usb_audio_quirk ua700_quirk = {
3331 .type = QUIRK_MIDI_FIXED_ENDPOINT,
3334 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
3335 .out_cables = 0x0001,
3338 static const struct snd_usb_audio_quirk uaxx_quirk = {
3339 .type = QUIRK_MIDI_FIXED_ENDPOINT,
3342 const struct snd_usb_audio_quirk *quirk =
3343 chip->usb_id == USB_ID(0x0582, 0x002b)
3344 ? &ua700_quirk : &uaxx_quirk;
3345 return snd_usbmidi_create(chip->card, iface,
3346 &chip->midi_list, quirk);
3349 if (altsd->bNumEndpoints != 1)
3352 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
3355 memcpy(fp, &ua_format, sizeof(*fp));
3357 fp->iface = altsd->bInterfaceNumber;
3358 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
3359 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
3360 fp->datainterval = 0;
3361 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
3363 switch (fp->maxpacksize) {
3365 fp->rate_max = fp->rate_min = 44100;
3369 fp->rate_max = fp->rate_min = 48000;
3373 fp->rate_max = fp->rate_min = 96000;
3376 snd_printk(KERN_ERR "unknown sample rate\n");
3381 stream = (fp->endpoint & USB_DIR_IN)
3382 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
3383 err = add_audio_endpoint(chip, stream, fp);
3388 usb_set_interface(chip->dev, fp->iface, 0);
3392 static int snd_usb_create_quirk(struct snd_usb_audio *chip,
3393 struct usb_interface *iface,
3394 const struct snd_usb_audio_quirk *quirk);
3397 * handle the quirks for the contained interfaces
3399 static int create_composite_quirk(struct snd_usb_audio *chip,
3400 struct usb_interface *iface,
3401 const struct snd_usb_audio_quirk *quirk)
3403 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
3406 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
3407 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
3410 if (quirk->ifnum != probed_ifnum &&
3411 usb_interface_claimed(iface))
3413 err = snd_usb_create_quirk(chip, iface, quirk);
3416 if (quirk->ifnum != probed_ifnum)
3417 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
3422 static int ignore_interface_quirk(struct snd_usb_audio *chip,
3423 struct usb_interface *iface,
3424 const struct snd_usb_audio_quirk *quirk)
3430 * Allow alignment on audio sub-slot (channel samples) rather than
3431 * on audio slots (audio frames)
3433 static int create_align_transfer_quirk(struct snd_usb_audio *chip,
3434 struct usb_interface *iface,
3435 const struct snd_usb_audio_quirk *quirk)
3437 chip->txfr_quirk = 1;
3438 return 1; /* Continue with creating streams and mixer */
3446 #define EXTIGY_FIRMWARE_SIZE_OLD 794
3447 #define EXTIGY_FIRMWARE_SIZE_NEW 483
3449 static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
3451 struct usb_host_config *config = dev->actconfig;
3454 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
3455 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
3456 snd_printdd("sending Extigy boot sequence...\n");
3457 /* Send message to force it to reconnect with full interface. */
3458 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
3459 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
3460 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
3461 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
3462 &dev->descriptor, sizeof(dev->descriptor));
3463 config = dev->actconfig;
3464 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
3465 err = usb_reset_configuration(dev);
3466 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
3467 snd_printdd("extigy_boot: new boot length = %d\n",
3468 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
3469 return -ENODEV; /* quit this anyway */
3474 static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
3478 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
3479 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3480 0, 0, &buf, 1, 1000);
3482 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
3483 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3484 1, 2000, NULL, 0, 1000);
3491 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
3492 * documented in the device's data sheet.
3494 static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
3498 buf[1] = value & 0xff;
3499 buf[2] = (value >> 8) & 0xff;
3501 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
3502 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
3503 0, 0, &buf, 4, 1000);
3506 static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
3509 * Enable line-out driver mode, set headphone source to front
3510 * channels, enable stereo mic.
3512 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
3516 * C-Media CM6206 is based on CM106 with two additional
3517 * registers that are not documented in the data sheet.
3518 * Values here are chosen based on sniffing USB traffic
3521 static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
3524 int val[] = {0x200c, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
3526 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
3527 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
3536 * This call will put the synth in "USB send" mode, i.e it will send MIDI
3537 * messages through USB (this is disabled at startup). The synth will
3538 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
3539 * sign on its LCD. Values here are chosen based on sniffing USB traffic
3542 static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
3544 int err, actual_length;
3546 /* "midi send" enable */
3547 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
3549 void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
3552 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
3553 ARRAY_SIZE(seq), &actual_length, 1000);
3564 #define AUDIOPHILE_SET 0x01 /* if set, parse device_setup */
3565 #define AUDIOPHILE_SET_DTS 0x02 /* if set, enable DTS Digital Output */
3566 #define AUDIOPHILE_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
3567 #define AUDIOPHILE_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
3568 #define AUDIOPHILE_SET_DI 0x10 /* if set, enable Digital Input */
3569 #define AUDIOPHILE_SET_MASK 0x1F /* bit mask for setup value */
3570 #define AUDIOPHILE_SET_24B_48K_DI 0x19 /* value for 24bits+48KHz+Digital Input */
3571 #define AUDIOPHILE_SET_24B_48K_NOTDI 0x09 /* value for 24bits+48KHz+No Digital Input */
3572 #define AUDIOPHILE_SET_16B_48K_DI 0x11 /* value for 16bits+48KHz+Digital Input */
3573 #define AUDIOPHILE_SET_16B_48K_NOTDI 0x01 /* value for 16bits+48KHz+No Digital Input */
3575 static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
3576 int iface, int altno)
3578 /* Reset ALL ifaces to 0 altsetting.
3579 * Call it for every possible altsetting of every interface.
3581 usb_set_interface(chip->dev, iface, 0);
3583 if (device_setup[chip->index] & AUDIOPHILE_SET) {
3584 if ((device_setup[chip->index] & AUDIOPHILE_SET_DTS)
3586 return 1; /* skip this altsetting */
3587 if ((device_setup[chip->index] & AUDIOPHILE_SET_96K)
3589 return 1; /* skip this altsetting */
3590 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3591 AUDIOPHILE_SET_24B_48K_DI && altno != 2)
3592 return 1; /* skip this altsetting */
3593 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3594 AUDIOPHILE_SET_24B_48K_NOTDI && altno != 3)
3595 return 1; /* skip this altsetting */
3596 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3597 AUDIOPHILE_SET_16B_48K_DI && altno != 4)
3598 return 1; /* skip this altsetting */
3599 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3600 AUDIOPHILE_SET_16B_48K_NOTDI && altno != 5)
3601 return 1; /* skip this altsetting */
3603 return 0; /* keep this altsetting */
3606 static int create_any_midi_quirk(struct snd_usb_audio *chip,
3607 struct usb_interface *intf,
3608 const struct snd_usb_audio_quirk *quirk)
3610 return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
3614 * audio-interface quirks
3616 * returns zero if no standard audio/MIDI parsing is needed.
3617 * returns a postive value if standard audio/midi interfaces are parsed
3619 * returns a negative value at error.
3621 static int snd_usb_create_quirk(struct snd_usb_audio *chip,
3622 struct usb_interface *iface,
3623 const struct snd_usb_audio_quirk *quirk)
3625 typedef int (*quirk_func_t)(struct snd_usb_audio *, struct usb_interface *,
3626 const struct snd_usb_audio_quirk *);
3627 static const quirk_func_t quirk_funcs[] = {
3628 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
3629 [QUIRK_COMPOSITE] = create_composite_quirk,
3630 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
3631 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
3632 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
3633 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
3634 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
3635 [QUIRK_MIDI_FASTLANE] = create_any_midi_quirk,
3636 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
3637 [QUIRK_MIDI_CME] = create_any_midi_quirk,
3638 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
3639 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
3640 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
3641 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk
3644 if (quirk->type < QUIRK_TYPE_COUNT) {
3645 return quirk_funcs[quirk->type](chip, iface, quirk);
3647 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
3654 * common proc files to show the usb device info
3656 static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
3658 struct snd_usb_audio *chip = entry->private_data;
3659 if (!chip->shutdown)
3660 snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
3663 static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
3665 struct snd_usb_audio *chip = entry->private_data;
3666 if (!chip->shutdown)
3667 snd_iprintf(buffer, "%04x:%04x\n",
3668 USB_ID_VENDOR(chip->usb_id),
3669 USB_ID_PRODUCT(chip->usb_id));
3672 static void snd_usb_audio_create_proc(struct snd_usb_audio *chip)
3674 struct snd_info_entry *entry;
3675 if (!snd_card_proc_new(chip->card, "usbbus", &entry))
3676 snd_info_set_text_ops(entry, chip, proc_audio_usbbus_read);
3677 if (!snd_card_proc_new(chip->card, "usbid", &entry))
3678 snd_info_set_text_ops(entry, chip, proc_audio_usbid_read);
3682 * free the chip instance
3684 * here we have to do not much, since pcm and controls are already freed
3688 static int snd_usb_audio_free(struct snd_usb_audio *chip)
3694 static int snd_usb_audio_dev_free(struct snd_device *device)
3696 struct snd_usb_audio *chip = device->device_data;
3697 return snd_usb_audio_free(chip);
3702 * create a chip instance and set its names.
3704 static int snd_usb_audio_create(struct usb_device *dev, int idx,
3705 const struct snd_usb_audio_quirk *quirk,
3706 struct snd_usb_audio **rchip)
3708 struct snd_card *card;
3709 struct snd_usb_audio *chip;
3712 static struct snd_device_ops ops = {
3713 .dev_free = snd_usb_audio_dev_free,
3718 if (snd_usb_get_speed(dev) != USB_SPEED_LOW &&
3719 snd_usb_get_speed(dev) != USB_SPEED_FULL &&
3720 snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
3721 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
3725 err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
3727 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
3731 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
3733 snd_card_free(card);
3740 chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3741 le16_to_cpu(dev->descriptor.idProduct));
3742 INIT_LIST_HEAD(&chip->pcm_list);
3743 INIT_LIST_HEAD(&chip->midi_list);
3744 INIT_LIST_HEAD(&chip->mixer_list);
3746 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
3747 snd_usb_audio_free(chip);
3748 snd_card_free(card);
3752 strcpy(card->driver, "USB-Audio");
3753 sprintf(component, "USB%04x:%04x",
3754 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
3755 snd_component_add(card, component);
3757 /* retrieve the device string as shortname */
3758 if (quirk && quirk->product_name) {
3759 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
3761 if (!dev->descriptor.iProduct ||
3762 usb_string(dev, dev->descriptor.iProduct,
3763 card->shortname, sizeof(card->shortname)) <= 0) {
3764 /* no name available from anywhere, so use ID */
3765 sprintf(card->shortname, "USB Device %#04x:%#04x",
3766 USB_ID_VENDOR(chip->usb_id),
3767 USB_ID_PRODUCT(chip->usb_id));
3771 /* retrieve the vendor and device strings as longname */
3772 if (quirk && quirk->vendor_name) {
3773 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
3775 if (dev->descriptor.iManufacturer)
3776 len = usb_string(dev, dev->descriptor.iManufacturer,
3777 card->longname, sizeof(card->longname));
3780 /* we don't really care if there isn't any vendor string */
3783 strlcat(card->longname, " ", sizeof(card->longname));
3785 strlcat(card->longname, card->shortname, sizeof(card->longname));
3787 len = strlcat(card->longname, " at ", sizeof(card->longname));
3789 if (len < sizeof(card->longname))
3790 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
3792 strlcat(card->longname,
3793 snd_usb_get_speed(dev) == USB_SPEED_LOW ? ", low speed" :
3794 snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" :
3796 sizeof(card->longname));
3798 snd_usb_audio_create_proc(chip);
3806 * probe the active usb device
3808 * note that this can be called multiple times per a device, when it
3809 * includes multiple audio control interfaces.
3811 * thus we check the usb device pointer and creates the card instance
3812 * only at the first time. the successive calls of this function will
3813 * append the pcm interface to the corresponding card.
3815 static void *snd_usb_audio_probe(struct usb_device *dev,
3816 struct usb_interface *intf,
3817 const struct usb_device_id *usb_id)
3819 const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
3821 struct snd_usb_audio *chip;
3822 struct usb_host_interface *alts;
3826 alts = &intf->altsetting[0];
3827 ifnum = get_iface_desc(alts)->bInterfaceNumber;
3828 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3829 le16_to_cpu(dev->descriptor.idProduct));
3830 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
3833 /* SB Extigy needs special boot-up sequence */
3834 /* if more models come, this will go to the quirk list. */
3835 if (id == USB_ID(0x041e, 0x3000)) {
3836 if (snd_usb_extigy_boot_quirk(dev, intf) < 0)
3839 /* SB Audigy 2 NX needs its own boot-up magic, too */
3840 if (id == USB_ID(0x041e, 0x3020)) {
3841 if (snd_usb_audigy2nx_boot_quirk(dev) < 0)
3845 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
3846 if (id == USB_ID(0x10f5, 0x0200)) {
3847 if (snd_usb_cm106_boot_quirk(dev) < 0)
3851 /* C-Media CM6206 / CM106-Like Sound Device */
3852 if (id == USB_ID(0x0d8c, 0x0102)) {
3853 if (snd_usb_cm6206_boot_quirk(dev) < 0)
3857 /* Access Music VirusTI Desktop */
3858 if (id == USB_ID(0x133e, 0x0815)) {
3859 if (snd_usb_accessmusic_boot_quirk(dev) < 0)
3864 * found a config. now register to ALSA
3867 /* check whether it's already registered */
3869 mutex_lock(®ister_mutex);
3870 for (i = 0; i < SNDRV_CARDS; i++) {
3871 if (usb_chip[i] && usb_chip[i]->dev == dev) {
3872 if (usb_chip[i]->shutdown) {
3873 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
3881 /* it's a fresh one.
3882 * now look for an empty slot and create a new card instance
3884 for (i = 0; i < SNDRV_CARDS; i++)
3885 if (enable[i] && ! usb_chip[i] &&
3886 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
3887 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
3888 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
3891 snd_card_set_dev(chip->card, &intf->dev);
3895 printk(KERN_ERR "no available usb audio device\n");
3900 chip->txfr_quirk = 0;
3901 err = 1; /* continue */
3902 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
3903 /* need some special handlings */
3904 if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)
3909 /* create normal USB audio interfaces */
3910 if (snd_usb_create_streams(chip, ifnum) < 0 ||
3911 snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
3916 /* we are allowed to call snd_card_register() many times */
3917 if (snd_card_register(chip->card) < 0) {
3921 usb_chip[chip->index] = chip;
3922 chip->num_interfaces++;
3923 mutex_unlock(®ister_mutex);
3927 if (chip && !chip->num_interfaces)
3928 snd_card_free(chip->card);
3929 mutex_unlock(®ister_mutex);
3935 * we need to take care of counter, since disconnection can be called also
3936 * many times as well as usb_audio_probe().
3938 static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
3940 struct snd_usb_audio *chip;
3941 struct snd_card *card;
3942 struct list_head *p;
3944 if (ptr == (void *)-1L)
3949 mutex_lock(®ister_mutex);
3951 chip->num_interfaces--;
3952 if (chip->num_interfaces <= 0) {
3953 snd_card_disconnect(card);
3954 /* release the pcm resources */
3955 list_for_each(p, &chip->pcm_list) {
3956 snd_usb_stream_disconnect(p);
3958 /* release the midi resources */
3959 list_for_each(p, &chip->midi_list) {
3960 snd_usbmidi_disconnect(p);
3962 /* release mixer resources */
3963 list_for_each(p, &chip->mixer_list) {
3964 snd_usb_mixer_disconnect(p);
3966 usb_chip[chip->index] = NULL;
3967 mutex_unlock(®ister_mutex);
3968 snd_card_free_when_closed(card);
3970 mutex_unlock(®ister_mutex);
3975 * new 2.5 USB kernel API
3977 static int usb_audio_probe(struct usb_interface *intf,
3978 const struct usb_device_id *id)
3981 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
3983 usb_set_intfdata(intf, chip);
3989 static void usb_audio_disconnect(struct usb_interface *intf)
3991 snd_usb_audio_disconnect(interface_to_usbdev(intf),
3992 usb_get_intfdata(intf));
3996 static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
3998 struct snd_usb_audio *chip = usb_get_intfdata(intf);
3999 struct list_head *p;
4000 struct snd_usb_stream *as;
4002 if (chip == (void *)-1L)
4005 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
4006 if (!chip->num_suspended_intf++) {
4007 list_for_each(p, &chip->pcm_list) {
4008 as = list_entry(p, struct snd_usb_stream, list);
4009 snd_pcm_suspend_all(as->pcm);
4016 static int usb_audio_resume(struct usb_interface *intf)
4018 struct snd_usb_audio *chip = usb_get_intfdata(intf);
4020 if (chip == (void *)-1L)
4022 if (--chip->num_suspended_intf)
4025 * ALSA leaves material resumption to user space
4029 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
4033 #endif /* CONFIG_PM */
4035 static int __init snd_usb_audio_init(void)
4037 if (nrpacks < 1 || nrpacks > MAX_PACKS) {
4038 printk(KERN_WARNING "invalid nrpacks value.\n");
4041 return usb_register(&usb_audio_driver);
4045 static void __exit snd_usb_audio_cleanup(void)
4047 usb_deregister(&usb_audio_driver);
4050 module_init(snd_usb_audio_init);
4051 module_exit(snd_usb_audio_cleanup);