[ALSA] prevent oops & dead keyboard on usb unplugging while the device is being used
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / usb / usbaudio.c
blobaae66144d411ce579eee7185b8e3cca84e730d9e
1 /*
2 * (Tentative) USB Audio Driver for ALSA
4 * Main and PCM part
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * NOTES:
30 * - async unlink should be used for avoiding the sleep inside lock.
31 * 2.4.22 usb-uhci seems buggy for async unlinking and results in
32 * oops. in such a cse, pass async_unlink=0 option.
33 * - the linked URBs would be preferred but not used so far because of
34 * the instability of unlinking.
35 * - type II is not supported properly. there is no device which supports
36 * this type *correctly*. SB extigy looks as if it supports, but it's
37 * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
41 #include <sound/driver.h>
42 #include <linux/bitops.h>
43 #include <linux/init.h>
44 #include <linux/list.h>
45 #include <linux/slab.h>
46 #include <linux/string.h>
47 #include <linux/usb.h>
48 #include <linux/moduleparam.h>
49 #include <sound/core.h>
50 #include <sound/info.h>
51 #include <sound/pcm.h>
52 #include <sound/pcm_params.h>
53 #include <sound/initval.h>
55 #include "usbaudio.h"
58 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
59 MODULE_DESCRIPTION("USB Audio");
60 MODULE_LICENSE("GPL");
61 MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
64 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
65 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
66 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
67 static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Vendor ID for this card */
68 static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Product ID for this card */
69 static int nrpacks = 4; /* max. number of packets per urb */
70 static int async_unlink = 1;
72 module_param_array(index, int, NULL, 0444);
73 MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
74 module_param_array(id, charp, NULL, 0444);
75 MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
76 module_param_array(enable, bool, NULL, 0444);
77 MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
78 module_param_array(vid, int, NULL, 0444);
79 MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
80 module_param_array(pid, int, NULL, 0444);
81 MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
82 module_param(nrpacks, int, 0444);
83 MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
84 module_param(async_unlink, bool, 0444);
85 MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
89 * debug the h/w constraints
91 /* #define HW_CONST_DEBUG */
98 #define MAX_PACKS 10
99 #define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
100 #define MAX_URBS 5 /* max. 20ms long packets */
101 #define SYNC_URBS 2 /* always two urbs for sync */
102 #define MIN_PACKS_URB 1 /* minimum 1 packet per urb */
104 typedef struct snd_usb_substream snd_usb_substream_t;
105 typedef struct snd_usb_stream snd_usb_stream_t;
106 typedef struct snd_urb_ctx snd_urb_ctx_t;
108 struct audioformat {
109 struct list_head list;
110 snd_pcm_format_t format; /* format type */
111 unsigned int channels; /* # channels */
112 unsigned int fmt_type; /* USB audio format type (1-3) */
113 unsigned int frame_size; /* samples per frame for non-audio */
114 int iface; /* interface number */
115 unsigned char altsetting; /* corresponding alternate setting */
116 unsigned char altset_idx; /* array index of altenate setting */
117 unsigned char attributes; /* corresponding attributes of cs endpoint */
118 unsigned char endpoint; /* endpoint */
119 unsigned char ep_attr; /* endpoint attributes */
120 unsigned int maxpacksize; /* max. packet size */
121 unsigned int rates; /* rate bitmasks */
122 unsigned int rate_min, rate_max; /* min/max rates */
123 unsigned int nr_rates; /* number of rate table entries */
124 unsigned int *rate_table; /* rate table */
127 struct snd_urb_ctx {
128 struct urb *urb;
129 snd_usb_substream_t *subs;
130 int index; /* index for urb array */
131 int packets; /* number of packets per urb */
132 int transfer; /* transferred size */
133 char *buf; /* buffer for capture */
136 struct snd_urb_ops {
137 int (*prepare)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
138 int (*retire)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
139 int (*prepare_sync)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
140 int (*retire_sync)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
143 struct snd_usb_substream {
144 snd_usb_stream_t *stream;
145 struct usb_device *dev;
146 snd_pcm_substream_t *pcm_substream;
147 int direction; /* playback or capture */
148 int interface; /* current interface */
149 int endpoint; /* assigned endpoint */
150 struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
151 unsigned int cur_rate; /* current rate (for hw_params callback) */
152 unsigned int period_bytes; /* current period bytes (for hw_params callback) */
153 unsigned int format; /* USB data format */
154 unsigned int datapipe; /* the data i/o pipe */
155 unsigned int syncpipe; /* 1 - async out or adaptive in */
156 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
157 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
158 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
159 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
160 unsigned int phase; /* phase accumulator */
161 unsigned int maxpacksize; /* max packet size in bytes */
162 unsigned int maxframesize; /* max packet size in frames */
163 unsigned int curpacksize; /* current packet size in bytes (for capture) */
164 unsigned int curframesize; /* current packet size in frames (for capture) */
165 unsigned int fill_max: 1; /* fill max packet size always */
166 unsigned int fmt_type; /* USB audio format type (1-3) */
168 unsigned int running: 1; /* running status */
170 unsigned int hwptr; /* free frame position in the buffer (only for playback) */
171 unsigned int hwptr_done; /* processed frame position in the buffer */
172 unsigned int transfer_sched; /* scheduled frames since last period (for playback) */
173 unsigned int transfer_done; /* processed frames since last period update */
174 unsigned long active_mask; /* bitmask of active urbs */
175 unsigned long unlink_mask; /* bitmask of unlinked urbs */
177 unsigned int nurbs; /* # urbs */
178 snd_urb_ctx_t dataurb[MAX_URBS]; /* data urb table */
179 snd_urb_ctx_t syncurb[SYNC_URBS]; /* sync urb table */
180 char syncbuf[SYNC_URBS * MAX_PACKS * 4]; /* sync buffer; it's so small - let's get static */
181 char *tmpbuf; /* temporary buffer for playback */
183 u64 formats; /* format bitmasks (all or'ed) */
184 unsigned int num_formats; /* number of supported audio formats (list) */
185 struct list_head fmt_list; /* format list */
186 spinlock_t lock;
188 struct snd_urb_ops ops; /* callbacks (must be filled at init) */
192 struct snd_usb_stream {
193 snd_usb_audio_t *chip;
194 snd_pcm_t *pcm;
195 int pcm_index;
196 unsigned int fmt_type; /* USB audio format type (1-3) */
197 snd_usb_substream_t substream[2];
198 struct list_head list;
203 * we keep the snd_usb_audio_t instances by ourselves for merging
204 * the all interfaces on the same card as one sound device.
207 static DECLARE_MUTEX(register_mutex);
208 static snd_usb_audio_t *usb_chip[SNDRV_CARDS];
212 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
213 * this will overflow at approx 524 kHz
215 inline static unsigned get_usb_full_speed_rate(unsigned int rate)
217 return ((rate << 13) + 62) / 125;
221 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
222 * this will overflow at approx 4 MHz
224 inline static unsigned get_usb_high_speed_rate(unsigned int rate)
226 return ((rate << 10) + 62) / 125;
229 /* convert our full speed USB rate into sampling rate in Hz */
230 inline static unsigned get_full_speed_hz(unsigned int usb_rate)
232 return (usb_rate * 125 + (1 << 12)) >> 13;
235 /* convert our high speed USB rate into sampling rate in Hz */
236 inline static unsigned get_high_speed_hz(unsigned int usb_rate)
238 return (usb_rate * 125 + (1 << 9)) >> 10;
243 * prepare urb for full speed capture sync pipe
245 * fill the length and offset of each urb descriptor.
246 * the fixed 10.14 frequency is passed through the pipe.
248 static int prepare_capture_sync_urb(snd_usb_substream_t *subs,
249 snd_pcm_runtime_t *runtime,
250 struct urb *urb)
252 unsigned char *cp = urb->transfer_buffer;
253 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
254 int i, offs;
256 urb->number_of_packets = ctx->packets;
257 urb->dev = ctx->subs->dev; /* we need to set this at each time */
258 for (i = offs = 0; i < urb->number_of_packets; i++, offs += 4, cp += 4) {
259 urb->iso_frame_desc[i].length = 3;
260 urb->iso_frame_desc[i].offset = offs;
261 cp[0] = subs->freqn >> 2;
262 cp[1] = subs->freqn >> 10;
263 cp[2] = subs->freqn >> 18;
265 return 0;
269 * prepare urb for high speed capture sync pipe
271 * fill the length and offset of each urb descriptor.
272 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
274 static int prepare_capture_sync_urb_hs(snd_usb_substream_t *subs,
275 snd_pcm_runtime_t *runtime,
276 struct urb *urb)
278 unsigned char *cp = urb->transfer_buffer;
279 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
280 int i, offs;
282 urb->number_of_packets = ctx->packets;
283 urb->dev = ctx->subs->dev; /* we need to set this at each time */
284 for (i = offs = 0; i < urb->number_of_packets; i++, offs += 4, cp += 4) {
285 urb->iso_frame_desc[i].length = 4;
286 urb->iso_frame_desc[i].offset = offs;
287 cp[0] = subs->freqn;
288 cp[1] = subs->freqn >> 8;
289 cp[2] = subs->freqn >> 16;
290 cp[3] = subs->freqn >> 24;
292 return 0;
296 * process after capture sync complete
297 * - nothing to do
299 static int retire_capture_sync_urb(snd_usb_substream_t *subs,
300 snd_pcm_runtime_t *runtime,
301 struct urb *urb)
303 return 0;
307 * prepare urb for capture data pipe
309 * fill the offset and length of each descriptor.
311 * we use a temporary buffer to write the captured data.
312 * since the length of written data is determined by host, we cannot
313 * write onto the pcm buffer directly... the data is thus copied
314 * later at complete callback to the global buffer.
316 static int prepare_capture_urb(snd_usb_substream_t *subs,
317 snd_pcm_runtime_t *runtime,
318 struct urb *urb)
320 int i, offs;
321 unsigned long flags;
322 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
324 offs = 0;
325 urb->dev = ctx->subs->dev; /* we need to set this at each time */
326 urb->number_of_packets = 0;
327 spin_lock_irqsave(&subs->lock, flags);
328 for (i = 0; i < ctx->packets; i++) {
329 urb->iso_frame_desc[i].offset = offs;
330 urb->iso_frame_desc[i].length = subs->curpacksize;
331 offs += subs->curpacksize;
332 urb->number_of_packets++;
333 subs->transfer_sched += subs->curframesize;
334 if (subs->transfer_sched >= runtime->period_size) {
335 subs->transfer_sched -= runtime->period_size;
336 break;
339 spin_unlock_irqrestore(&subs->lock, flags);
340 urb->transfer_buffer = ctx->buf;
341 urb->transfer_buffer_length = offs;
342 #if 0 // for check
343 if (! urb->bandwidth) {
344 int bustime;
345 bustime = usb_check_bandwidth(urb->dev, urb);
346 if (bustime < 0)
347 return bustime;
348 printk("urb %d: bandwidth = %d (packets = %d)\n", ctx->index, bustime, urb->number_of_packets);
349 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
351 #endif // for check
352 return 0;
356 * process after capture complete
358 * copy the data from each desctiptor to the pcm buffer, and
359 * update the current position.
361 static int retire_capture_urb(snd_usb_substream_t *subs,
362 snd_pcm_runtime_t *runtime,
363 struct urb *urb)
365 unsigned long flags;
366 unsigned char *cp;
367 int i;
368 unsigned int stride, len, oldptr;
370 stride = runtime->frame_bits >> 3;
372 for (i = 0; i < urb->number_of_packets; i++) {
373 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
374 if (urb->iso_frame_desc[i].status) {
375 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
376 // continue;
378 len = urb->iso_frame_desc[i].actual_length / stride;
379 if (! len)
380 continue;
381 /* update the current pointer */
382 spin_lock_irqsave(&subs->lock, flags);
383 oldptr = subs->hwptr_done;
384 subs->hwptr_done += len;
385 if (subs->hwptr_done >= runtime->buffer_size)
386 subs->hwptr_done -= runtime->buffer_size;
387 subs->transfer_done += len;
388 spin_unlock_irqrestore(&subs->lock, flags);
389 /* copy a data chunk */
390 if (oldptr + len > runtime->buffer_size) {
391 unsigned int cnt = runtime->buffer_size - oldptr;
392 unsigned int blen = cnt * stride;
393 memcpy(runtime->dma_area + oldptr * stride, cp, blen);
394 memcpy(runtime->dma_area, cp + blen, len * stride - blen);
395 } else {
396 memcpy(runtime->dma_area + oldptr * stride, cp, len * stride);
398 /* update the pointer, call callback if necessary */
399 spin_lock_irqsave(&subs->lock, flags);
400 if (subs->transfer_done >= runtime->period_size) {
401 subs->transfer_done -= runtime->period_size;
402 spin_unlock_irqrestore(&subs->lock, flags);
403 snd_pcm_period_elapsed(subs->pcm_substream);
404 } else
405 spin_unlock_irqrestore(&subs->lock, flags);
407 return 0;
412 * prepare urb for full speed playback sync pipe
414 * set up the offset and length to receive the current frequency.
417 static int prepare_playback_sync_urb(snd_usb_substream_t *subs,
418 snd_pcm_runtime_t *runtime,
419 struct urb *urb)
421 int i, offs;
422 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
424 urb->number_of_packets = ctx->packets;
425 urb->dev = ctx->subs->dev; /* we need to set this at each time */
426 for (i = offs = 0; i < urb->number_of_packets; i++, offs += 4) {
427 urb->iso_frame_desc[i].length = 3;
428 urb->iso_frame_desc[i].offset = offs;
430 return 0;
434 * prepare urb for high speed playback sync pipe
436 * set up the offset and length to receive the current frequency.
439 static int prepare_playback_sync_urb_hs(snd_usb_substream_t *subs,
440 snd_pcm_runtime_t *runtime,
441 struct urb *urb)
443 int i, offs;
444 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
446 urb->number_of_packets = ctx->packets;
447 urb->dev = ctx->subs->dev; /* we need to set this at each time */
448 for (i = offs = 0; i < urb->number_of_packets; i++, offs += 4) {
449 urb->iso_frame_desc[i].length = 4;
450 urb->iso_frame_desc[i].offset = offs;
452 return 0;
456 * process after full speed playback sync complete
458 * retrieve the current 10.14 frequency from pipe, and set it.
459 * the value is referred in prepare_playback_urb().
461 static int retire_playback_sync_urb(snd_usb_substream_t *subs,
462 snd_pcm_runtime_t *runtime,
463 struct urb *urb)
465 int i;
466 unsigned int f, found;
467 unsigned char *cp = urb->transfer_buffer;
468 unsigned long flags;
470 found = 0;
471 for (i = 0; i < urb->number_of_packets; i++, cp += 4) {
472 if (urb->iso_frame_desc[i].status ||
473 urb->iso_frame_desc[i].actual_length < 3)
474 continue;
475 f = combine_triple(cp) << 2;
476 #if 0
477 if (f < subs->freqn - (subs->freqn>>3) || f > subs->freqmax) {
478 snd_printd(KERN_WARNING "requested frequency %d (%u,%03uHz) out of range (current nominal %d (%u,%03uHz))\n",
479 f, f >> 14, (f & ((1 << 14) - 1) * 1000) / ((1 << 14) - 1),
480 subs->freqn, subs->freqn >> 14, (subs->freqn & ((1 << 14) - 1) * 1000) / ((1 << 14) - 1));
481 continue;
483 #endif
484 found = f;
486 if (found) {
487 spin_lock_irqsave(&subs->lock, flags);
488 subs->freqm = found;
489 spin_unlock_irqrestore(&subs->lock, flags);
492 return 0;
496 * process after high speed playback sync complete
498 * retrieve the current 12.13 frequency from pipe, and set it.
499 * the value is referred in prepare_playback_urb().
501 static int retire_playback_sync_urb_hs(snd_usb_substream_t *subs,
502 snd_pcm_runtime_t *runtime,
503 struct urb *urb)
505 int i;
506 unsigned int found;
507 unsigned char *cp = urb->transfer_buffer;
508 unsigned long flags;
510 found = 0;
511 for (i = 0; i < urb->number_of_packets; i++, cp += 4) {
512 if (urb->iso_frame_desc[i].status ||
513 urb->iso_frame_desc[i].actual_length < 4)
514 continue;
515 found = combine_quad(cp) & 0x0fffffff;
517 if (found) {
518 spin_lock_irqsave(&subs->lock, flags);
519 subs->freqm = found;
520 spin_unlock_irqrestore(&subs->lock, flags);
523 return 0;
527 * prepare urb for playback data pipe
529 * we copy the data directly from the pcm buffer.
530 * the current position to be copied is held in hwptr field.
531 * since a urb can handle only a single linear buffer, if the total
532 * transferred area overflows the buffer boundary, we cannot send
533 * it directly from the buffer. thus the data is once copied to
534 * a temporary buffer and urb points to that.
536 static int prepare_playback_urb(snd_usb_substream_t *subs,
537 snd_pcm_runtime_t *runtime,
538 struct urb *urb)
540 int i, stride, offs;
541 unsigned int counts;
542 unsigned long flags;
543 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
545 stride = runtime->frame_bits >> 3;
547 offs = 0;
548 urb->dev = ctx->subs->dev; /* we need to set this at each time */
549 urb->number_of_packets = 0;
550 spin_lock_irqsave(&subs->lock, flags);
551 for (i = 0; i < ctx->packets; i++) {
552 /* calculate the size of a packet */
553 if (subs->fill_max)
554 counts = subs->maxframesize; /* fixed */
555 else {
556 subs->phase = (subs->phase & 0xffff) + subs->freqm;
557 counts = subs->phase >> 16;
558 if (counts > subs->maxframesize)
559 counts = subs->maxframesize;
561 /* set up descriptor */
562 urb->iso_frame_desc[i].offset = offs * stride;
563 urb->iso_frame_desc[i].length = counts * stride;
564 offs += counts;
565 urb->number_of_packets++;
566 subs->transfer_sched += counts;
567 if (subs->transfer_sched >= runtime->period_size) {
568 subs->transfer_sched -= runtime->period_size;
569 if (subs->fmt_type == USB_FORMAT_TYPE_II) {
570 if (subs->transfer_sched > 0) {
571 /* FIXME: fill-max mode is not supported yet */
572 offs -= subs->transfer_sched;
573 counts -= subs->transfer_sched;
574 urb->iso_frame_desc[i].length = counts * stride;
575 subs->transfer_sched = 0;
577 i++;
578 if (i < ctx->packets) {
579 /* add a transfer delimiter */
580 urb->iso_frame_desc[i].offset = offs * stride;
581 urb->iso_frame_desc[i].length = 0;
582 urb->number_of_packets++;
585 break;
588 if (subs->hwptr + offs > runtime->buffer_size) {
589 /* err, the transferred area goes over buffer boundary.
590 * copy the data to the temp buffer.
592 int len;
593 len = runtime->buffer_size - subs->hwptr;
594 urb->transfer_buffer = subs->tmpbuf;
595 memcpy(subs->tmpbuf, runtime->dma_area + subs->hwptr * stride, len * stride);
596 memcpy(subs->tmpbuf + len * stride, runtime->dma_area, (offs - len) * stride);
597 subs->hwptr += offs;
598 subs->hwptr -= runtime->buffer_size;
599 } else {
600 /* set the buffer pointer */
601 urb->transfer_buffer = runtime->dma_area + subs->hwptr * stride;
602 subs->hwptr += offs;
604 spin_unlock_irqrestore(&subs->lock, flags);
605 urb->transfer_buffer_length = offs * stride;
606 ctx->transfer = offs;
608 return 0;
612 * process after playback data complete
614 * update the current position and call callback if a period is processed.
616 static int retire_playback_urb(snd_usb_substream_t *subs,
617 snd_pcm_runtime_t *runtime,
618 struct urb *urb)
620 unsigned long flags;
621 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
623 spin_lock_irqsave(&subs->lock, flags);
624 subs->transfer_done += ctx->transfer;
625 subs->hwptr_done += ctx->transfer;
626 ctx->transfer = 0;
627 if (subs->hwptr_done >= runtime->buffer_size)
628 subs->hwptr_done -= runtime->buffer_size;
629 if (subs->transfer_done >= runtime->period_size) {
630 subs->transfer_done -= runtime->period_size;
631 spin_unlock_irqrestore(&subs->lock, flags);
632 snd_pcm_period_elapsed(subs->pcm_substream);
633 } else
634 spin_unlock_irqrestore(&subs->lock, flags);
635 return 0;
641 static struct snd_urb_ops audio_urb_ops[2] = {
643 .prepare = prepare_playback_urb,
644 .retire = retire_playback_urb,
645 .prepare_sync = prepare_playback_sync_urb,
646 .retire_sync = retire_playback_sync_urb,
649 .prepare = prepare_capture_urb,
650 .retire = retire_capture_urb,
651 .prepare_sync = prepare_capture_sync_urb,
652 .retire_sync = retire_capture_sync_urb,
656 static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
658 .prepare = prepare_playback_urb,
659 .retire = retire_playback_urb,
660 .prepare_sync = prepare_playback_sync_urb_hs,
661 .retire_sync = retire_playback_sync_urb_hs,
664 .prepare = prepare_capture_urb,
665 .retire = retire_capture_urb,
666 .prepare_sync = prepare_capture_sync_urb_hs,
667 .retire_sync = retire_capture_sync_urb,
672 * complete callback from data urb
674 static void snd_complete_urb(struct urb *urb, struct pt_regs *regs)
676 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
677 snd_usb_substream_t *subs = ctx->subs;
678 snd_pcm_substream_t *substream = ctx->subs->pcm_substream;
679 int err = 0;
681 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
682 ! subs->running || /* can be stopped during retire callback */
683 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
684 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
685 clear_bit(ctx->index, &subs->active_mask);
686 if (err < 0) {
687 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
688 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
695 * complete callback from sync urb
697 static void snd_complete_sync_urb(struct urb *urb, struct pt_regs *regs)
699 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
700 snd_usb_substream_t *subs = ctx->subs;
701 snd_pcm_substream_t *substream = ctx->subs->pcm_substream;
702 int err = 0;
704 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
705 ! subs->running || /* can be stopped during retire callback */
706 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
707 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
708 clear_bit(ctx->index + 16, &subs->active_mask);
709 if (err < 0) {
710 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
711 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
718 * unlink active urbs.
720 static int deactivate_urbs(snd_usb_substream_t *subs, int force, int can_sleep)
722 unsigned int i;
723 int async;
725 subs->running = 0;
727 if (!force && subs->stream->chip->shutdown) /* to be sure... */
728 return -EBADFD;
730 async = !can_sleep && async_unlink;
732 if (! async && in_interrupt())
733 return 0;
735 for (i = 0; i < subs->nurbs; i++) {
736 if (test_bit(i, &subs->active_mask)) {
737 if (! test_and_set_bit(i, &subs->unlink_mask)) {
738 struct urb *u = subs->dataurb[i].urb;
739 if (async) {
740 u->transfer_flags |= URB_ASYNC_UNLINK;
741 usb_unlink_urb(u);
742 } else
743 usb_kill_urb(u);
747 if (subs->syncpipe) {
748 for (i = 0; i < SYNC_URBS; i++) {
749 if (test_bit(i+16, &subs->active_mask)) {
750 if (! test_and_set_bit(i+16, &subs->unlink_mask)) {
751 struct urb *u = subs->syncurb[i].urb;
752 if (async) {
753 u->transfer_flags |= URB_ASYNC_UNLINK;
754 usb_unlink_urb(u);
755 } else
756 usb_kill_urb(u);
761 return 0;
766 * set up and start data/sync urbs
768 static int start_urbs(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime)
770 unsigned int i;
771 int err;
773 if (subs->stream->chip->shutdown)
774 return -EBADFD;
776 for (i = 0; i < subs->nurbs; i++) {
777 snd_assert(subs->dataurb[i].urb, return -EINVAL);
778 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
779 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
780 goto __error;
783 if (subs->syncpipe) {
784 for (i = 0; i < SYNC_URBS; i++) {
785 snd_assert(subs->syncurb[i].urb, return -EINVAL);
786 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
787 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
788 goto __error;
793 subs->active_mask = 0;
794 subs->unlink_mask = 0;
795 subs->running = 1;
796 for (i = 0; i < subs->nurbs; i++) {
797 if ((err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC)) < 0) {
798 snd_printk(KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
799 goto __error;
801 set_bit(i, &subs->active_mask);
803 if (subs->syncpipe) {
804 for (i = 0; i < SYNC_URBS; i++) {
805 if ((err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC)) < 0) {
806 snd_printk(KERN_ERR "cannot submit syncpipe for urb %d, err = %d\n", i, err);
807 goto __error;
809 set_bit(i + 16, &subs->active_mask);
812 return 0;
814 __error:
815 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
816 deactivate_urbs(subs, 0, 0);
817 return -EPIPE;
822 * wait until all urbs are processed.
824 static int wait_clear_urbs(snd_usb_substream_t *subs)
826 int timeout = HZ;
827 unsigned int i;
828 int alive;
830 do {
831 alive = 0;
832 for (i = 0; i < subs->nurbs; i++) {
833 if (test_bit(i, &subs->active_mask))
834 alive++;
836 if (subs->syncpipe) {
837 for (i = 0; i < SYNC_URBS; i++) {
838 if (test_bit(i + 16, &subs->active_mask))
839 alive++;
842 if (! alive)
843 break;
844 set_current_state(TASK_UNINTERRUPTIBLE);
845 schedule_timeout(1);
846 } while (--timeout > 0);
847 if (alive)
848 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
849 return 0;
854 * return the current pcm pointer. just return the hwptr_done value.
856 static snd_pcm_uframes_t snd_usb_pcm_pointer(snd_pcm_substream_t *substream)
858 snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
859 return subs->hwptr_done;
864 * start/stop substream
866 static int snd_usb_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
868 snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
869 int err;
871 switch (cmd) {
872 case SNDRV_PCM_TRIGGER_START:
873 err = start_urbs(subs, substream->runtime);
874 break;
875 case SNDRV_PCM_TRIGGER_STOP:
876 err = deactivate_urbs(subs, 0, 0);
877 break;
878 default:
879 err = -EINVAL;
880 break;
882 return err < 0 ? err : 0;
887 * release a urb data
889 static void release_urb_ctx(snd_urb_ctx_t *u)
891 if (u->urb) {
892 usb_free_urb(u->urb);
893 u->urb = NULL;
895 if (u->buf) {
896 kfree(u->buf);
897 u->buf = NULL;
902 * release a substream
904 static void release_substream_urbs(snd_usb_substream_t *subs, int force)
906 int i;
908 /* stop urbs (to be sure) */
909 deactivate_urbs(subs, force, 1);
910 wait_clear_urbs(subs);
912 for (i = 0; i < MAX_URBS; i++)
913 release_urb_ctx(&subs->dataurb[i]);
914 for (i = 0; i < SYNC_URBS; i++)
915 release_urb_ctx(&subs->syncurb[i]);
916 if (subs->tmpbuf) {
917 kfree(subs->tmpbuf);
918 subs->tmpbuf = NULL;
920 subs->nurbs = 0;
924 * initialize a substream for plaback/capture
926 static int init_substream_urbs(snd_usb_substream_t *subs, unsigned int period_bytes,
927 unsigned int rate, unsigned int frame_bits)
929 unsigned int maxsize, n, i;
930 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
931 unsigned int npacks[MAX_URBS], urb_packs, total_packs;
933 /* calculate the frequency in 16.16 format */
934 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
935 subs->freqn = get_usb_full_speed_rate(rate);
936 else
937 subs->freqn = get_usb_high_speed_rate(rate);
938 subs->freqm = subs->freqn;
939 subs->freqmax = subs->freqn + (subs->freqn >> 2); /* max. allowed frequency */
940 subs->phase = 0;
942 /* calculate the max. size of packet */
943 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3)) >> 16;
944 if (subs->maxpacksize && maxsize > subs->maxpacksize) {
945 //snd_printd(KERN_DEBUG "maxsize %d is greater than defined size %d\n",
946 // maxsize, subs->maxpacksize);
947 maxsize = subs->maxpacksize;
950 if (subs->fill_max)
951 subs->curpacksize = subs->maxpacksize;
952 else
953 subs->curpacksize = maxsize;
955 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
956 urb_packs = nrpacks;
957 else
958 urb_packs = nrpacks * 8;
960 /* allocate a temporary buffer for playback */
961 if (is_playback) {
962 subs->tmpbuf = kmalloc(maxsize * urb_packs, GFP_KERNEL);
963 if (! subs->tmpbuf) {
964 snd_printk(KERN_ERR "cannot malloc tmpbuf\n");
965 return -ENOMEM;
969 /* decide how many packets to be used */
970 total_packs = (period_bytes + maxsize - 1) / maxsize;
971 if (total_packs < 2 * MIN_PACKS_URB)
972 total_packs = 2 * MIN_PACKS_URB;
973 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
974 if (subs->nurbs > MAX_URBS) {
975 /* too much... */
976 subs->nurbs = MAX_URBS;
977 total_packs = MAX_URBS * urb_packs;
979 n = total_packs;
980 for (i = 0; i < subs->nurbs; i++) {
981 npacks[i] = n > urb_packs ? urb_packs : n;
982 n -= urb_packs;
984 if (subs->nurbs <= 1) {
985 /* too little - we need at least two packets
986 * to ensure contiguous playback/capture
988 subs->nurbs = 2;
989 npacks[0] = (total_packs + 1) / 2;
990 npacks[1] = total_packs - npacks[0];
991 } else if (npacks[subs->nurbs-1] < MIN_PACKS_URB) {
992 /* the last packet is too small.. */
993 if (subs->nurbs > 2) {
994 /* merge to the first one */
995 npacks[0] += npacks[subs->nurbs - 1];
996 subs->nurbs--;
997 } else {
998 /* divide to two */
999 subs->nurbs = 2;
1000 npacks[0] = (total_packs + 1) / 2;
1001 npacks[1] = total_packs - npacks[0];
1005 /* allocate and initialize data urbs */
1006 for (i = 0; i < subs->nurbs; i++) {
1007 snd_urb_ctx_t *u = &subs->dataurb[i];
1008 u->index = i;
1009 u->subs = subs;
1010 u->transfer = 0;
1011 u->packets = npacks[i];
1012 if (subs->fmt_type == USB_FORMAT_TYPE_II)
1013 u->packets++; /* for transfer delimiter */
1014 if (! is_playback) {
1015 /* allocate a capture buffer per urb */
1016 u->buf = kmalloc(maxsize * u->packets, GFP_KERNEL);
1017 if (! u->buf) {
1018 release_substream_urbs(subs, 0);
1019 return -ENOMEM;
1022 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1023 if (! u->urb) {
1024 release_substream_urbs(subs, 0);
1025 return -ENOMEM;
1027 u->urb->dev = subs->dev;
1028 u->urb->pipe = subs->datapipe;
1029 u->urb->transfer_flags = URB_ISO_ASAP;
1030 u->urb->number_of_packets = u->packets;
1031 u->urb->interval = 1;
1032 u->urb->context = u;
1033 u->urb->complete = snd_usb_complete_callback(snd_complete_urb);
1036 if (subs->syncpipe) {
1037 /* allocate and initialize sync urbs */
1038 for (i = 0; i < SYNC_URBS; i++) {
1039 snd_urb_ctx_t *u = &subs->syncurb[i];
1040 u->index = i;
1041 u->subs = subs;
1042 u->packets = nrpacks;
1043 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1044 if (! u->urb) {
1045 release_substream_urbs(subs, 0);
1046 return -ENOMEM;
1048 u->urb->transfer_buffer = subs->syncbuf + i * nrpacks * 4;
1049 u->urb->transfer_buffer_length = nrpacks * 4;
1050 u->urb->dev = subs->dev;
1051 u->urb->pipe = subs->syncpipe;
1052 u->urb->transfer_flags = URB_ISO_ASAP;
1053 u->urb->number_of_packets = u->packets;
1054 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
1055 u->urb->interval = 8;
1056 else
1057 u->urb->interval = 1;
1058 u->urb->context = u;
1059 u->urb->complete = snd_usb_complete_callback(snd_complete_sync_urb);
1062 return 0;
1067 * find a matching audio format
1069 static struct audioformat *find_format(snd_usb_substream_t *subs, unsigned int format,
1070 unsigned int rate, unsigned int channels)
1072 struct list_head *p;
1073 struct audioformat *found = NULL;
1074 int cur_attr = 0, attr;
1076 list_for_each(p, &subs->fmt_list) {
1077 struct audioformat *fp;
1078 fp = list_entry(p, struct audioformat, list);
1079 if (fp->format != format || fp->channels != channels)
1080 continue;
1081 if (rate < fp->rate_min || rate > fp->rate_max)
1082 continue;
1083 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
1084 unsigned int i;
1085 for (i = 0; i < fp->nr_rates; i++)
1086 if (fp->rate_table[i] == rate)
1087 break;
1088 if (i >= fp->nr_rates)
1089 continue;
1091 attr = fp->ep_attr & EP_ATTR_MASK;
1092 if (! found) {
1093 found = fp;
1094 cur_attr = attr;
1095 continue;
1097 /* avoid async out and adaptive in if the other method
1098 * supports the same format.
1099 * this is a workaround for the case like
1100 * M-audio audiophile USB.
1102 if (attr != cur_attr) {
1103 if ((attr == EP_ATTR_ASYNC &&
1104 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1105 (attr == EP_ATTR_ADAPTIVE &&
1106 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
1107 continue;
1108 if ((cur_attr == EP_ATTR_ASYNC &&
1109 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1110 (cur_attr == EP_ATTR_ADAPTIVE &&
1111 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
1112 found = fp;
1113 cur_attr = attr;
1114 continue;
1117 /* find the format with the largest max. packet size */
1118 if (fp->maxpacksize > found->maxpacksize) {
1119 found = fp;
1120 cur_attr = attr;
1123 return found;
1128 * initialize the picth control and sample rate
1130 static int init_usb_pitch(struct usb_device *dev, int iface,
1131 struct usb_host_interface *alts,
1132 struct audioformat *fmt)
1134 unsigned int ep;
1135 unsigned char data[1];
1136 int err;
1138 ep = get_endpoint(alts, 0)->bEndpointAddress;
1139 /* if endpoint has pitch control, enable it */
1140 if (fmt->attributes & EP_CS_ATTR_PITCH_CONTROL) {
1141 data[0] = 1;
1142 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1143 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1144 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1145 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
1146 dev->devnum, iface, ep);
1147 return err;
1150 return 0;
1153 static int init_usb_sample_rate(struct usb_device *dev, int iface,
1154 struct usb_host_interface *alts,
1155 struct audioformat *fmt, int rate)
1157 unsigned int ep;
1158 unsigned char data[3];
1159 int err;
1161 ep = get_endpoint(alts, 0)->bEndpointAddress;
1162 /* if endpoint has sampling rate control, set it */
1163 if (fmt->attributes & EP_CS_ATTR_SAMPLE_RATE) {
1164 int crate;
1165 data[0] = rate;
1166 data[1] = rate >> 8;
1167 data[2] = rate >> 16;
1168 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1169 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1170 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1171 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep 0x%x\n",
1172 dev->devnum, iface, fmt->altsetting, rate, ep);
1173 return err;
1175 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR,
1176 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1177 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1178 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep 0x%x\n",
1179 dev->devnum, iface, fmt->altsetting, ep);
1180 return 0; /* some devices don't support reading */
1182 crate = data[0] | (data[1] << 8) | (data[2] << 16);
1183 if (crate != rate) {
1184 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
1185 // runtime->rate = crate;
1188 return 0;
1192 * find a matching format and set up the interface
1194 static int set_format(snd_usb_substream_t *subs, struct audioformat *fmt)
1196 struct usb_device *dev = subs->dev;
1197 struct usb_host_interface *alts;
1198 struct usb_interface_descriptor *altsd;
1199 struct usb_interface *iface;
1200 unsigned int ep, attr;
1201 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1202 int err;
1204 iface = usb_ifnum_to_if(dev, fmt->iface);
1205 snd_assert(iface, return -EINVAL);
1206 alts = &iface->altsetting[fmt->altset_idx];
1207 altsd = get_iface_desc(alts);
1208 snd_assert(altsd->bAlternateSetting == fmt->altsetting, return -EINVAL);
1210 if (fmt == subs->cur_audiofmt)
1211 return 0;
1213 /* close the old interface */
1214 if (subs->interface >= 0 && subs->interface != fmt->iface) {
1215 usb_set_interface(subs->dev, subs->interface, 0);
1216 subs->interface = -1;
1217 subs->format = 0;
1220 /* set interface */
1221 if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
1222 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
1223 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
1224 dev->devnum, fmt->iface, fmt->altsetting);
1225 return -EIO;
1227 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
1228 subs->interface = fmt->iface;
1229 subs->format = fmt->altset_idx;
1232 /* create a data pipe */
1233 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
1234 if (is_playback)
1235 subs->datapipe = usb_sndisocpipe(dev, ep);
1236 else
1237 subs->datapipe = usb_rcvisocpipe(dev, ep);
1238 subs->syncpipe = subs->syncinterval = 0;
1239 subs->maxpacksize = fmt->maxpacksize;
1240 subs->fill_max = 0;
1242 /* we need a sync pipe in async OUT or adaptive IN mode */
1243 /* check the number of EP, since some devices have broken
1244 * descriptors which fool us. if it has only one EP,
1245 * assume it as adaptive-out or sync-in.
1247 attr = fmt->ep_attr & EP_ATTR_MASK;
1248 if (((is_playback && attr == EP_ATTR_ASYNC) ||
1249 (! is_playback && attr == EP_ATTR_ADAPTIVE)) &&
1250 altsd->bNumEndpoints >= 2) {
1251 /* check sync-pipe endpoint */
1252 /* ... and check descriptor size before accessing bSynchAddress
1253 because there is a version of the SB Audigy 2 NX firmware lacking
1254 the audio fields in the endpoint descriptors */
1255 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
1256 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1257 get_endpoint(alts, 1)->bSynchAddress != 0)) {
1258 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1259 dev->devnum, fmt->iface, fmt->altsetting);
1260 return -EINVAL;
1262 ep = get_endpoint(alts, 1)->bEndpointAddress;
1263 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1264 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
1265 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
1266 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1267 dev->devnum, fmt->iface, fmt->altsetting);
1268 return -EINVAL;
1270 ep &= USB_ENDPOINT_NUMBER_MASK;
1271 if (is_playback)
1272 subs->syncpipe = usb_rcvisocpipe(dev, ep);
1273 else
1274 subs->syncpipe = usb_sndisocpipe(dev, ep);
1275 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
1278 /* always fill max packet size */
1279 if (fmt->attributes & EP_CS_ATTR_FILL_MAX)
1280 subs->fill_max = 1;
1282 if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
1283 return err;
1285 subs->cur_audiofmt = fmt;
1287 #if 0
1288 printk("setting done: format = %d, rate = %d, channels = %d\n",
1289 fmt->format, fmt->rate, fmt->channels);
1290 printk(" datapipe = 0x%0x, syncpipe = 0x%0x\n",
1291 subs->datapipe, subs->syncpipe);
1292 #endif
1294 return 0;
1298 * hw_params callback
1300 * allocate a buffer and set the given audio format.
1302 * so far we use a physically linear buffer although packetize transfer
1303 * doesn't need a continuous area.
1304 * if sg buffer is supported on the later version of alsa, we'll follow
1305 * that.
1307 static int snd_usb_hw_params(snd_pcm_substream_t *substream,
1308 snd_pcm_hw_params_t *hw_params)
1310 snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
1311 struct audioformat *fmt;
1312 unsigned int channels, rate, format;
1313 int ret, changed;
1315 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1316 if (ret < 0)
1317 return ret;
1319 format = params_format(hw_params);
1320 rate = params_rate(hw_params);
1321 channels = params_channels(hw_params);
1322 fmt = find_format(subs, format, rate, channels);
1323 if (! fmt) {
1324 snd_printd(KERN_DEBUG "cannot set format: format = %s, rate = %d, channels = %d\n",
1325 snd_pcm_format_name(format), rate, channels);
1326 return -EINVAL;
1329 changed = subs->cur_audiofmt != fmt ||
1330 subs->period_bytes != params_period_bytes(hw_params) ||
1331 subs->cur_rate != rate;
1332 if ((ret = set_format(subs, fmt)) < 0)
1333 return ret;
1335 if (subs->cur_rate != rate) {
1336 struct usb_host_interface *alts;
1337 struct usb_interface *iface;
1338 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
1339 alts = &iface->altsetting[fmt->altset_idx];
1340 ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
1341 if (ret < 0)
1342 return ret;
1343 subs->cur_rate = rate;
1346 if (changed) {
1347 /* format changed */
1348 release_substream_urbs(subs, 0);
1349 /* influenced: period_bytes, channels, rate, format, */
1350 ret = init_substream_urbs(subs, params_period_bytes(hw_params),
1351 params_rate(hw_params),
1352 snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
1355 return ret;
1359 * hw_free callback
1361 * reset the audio format and release the buffer
1363 static int snd_usb_hw_free(snd_pcm_substream_t *substream)
1365 snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
1367 subs->cur_audiofmt = NULL;
1368 subs->cur_rate = 0;
1369 subs->period_bytes = 0;
1370 release_substream_urbs(subs, 0);
1371 return snd_pcm_lib_free_pages(substream);
1375 * prepare callback
1377 * only a few subtle things...
1379 static int snd_usb_pcm_prepare(snd_pcm_substream_t *substream)
1381 snd_pcm_runtime_t *runtime = substream->runtime;
1382 snd_usb_substream_t *subs = (snd_usb_substream_t *)runtime->private_data;
1384 if (! subs->cur_audiofmt) {
1385 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
1386 return -ENXIO;
1389 /* some unit conversions in runtime */
1390 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
1391 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
1393 /* reset the pointer */
1394 subs->hwptr = 0;
1395 subs->hwptr_done = 0;
1396 subs->transfer_sched = 0;
1397 subs->transfer_done = 0;
1398 subs->phase = 0;
1400 /* clear urbs (to be sure) */
1401 deactivate_urbs(subs, 0, 1);
1402 wait_clear_urbs(subs);
1404 return 0;
1407 static snd_pcm_hardware_t snd_usb_playback =
1409 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1410 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1411 SNDRV_PCM_INFO_MMAP_VALID),
1412 .buffer_bytes_max = (128*1024),
1413 .period_bytes_min = 64,
1414 .period_bytes_max = (128*1024),
1415 .periods_min = 2,
1416 .periods_max = 1024,
1419 static snd_pcm_hardware_t snd_usb_capture =
1421 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1422 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1423 SNDRV_PCM_INFO_MMAP_VALID),
1424 .buffer_bytes_max = (128*1024),
1425 .period_bytes_min = 64,
1426 .period_bytes_max = (128*1024),
1427 .periods_min = 2,
1428 .periods_max = 1024,
1432 * h/w constraints
1435 #ifdef HW_CONST_DEBUG
1436 #define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
1437 #else
1438 #define hwc_debug(fmt, args...) /**/
1439 #endif
1441 static int hw_check_valid_format(snd_pcm_hw_params_t *params, struct audioformat *fp)
1443 snd_interval_t *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1444 snd_interval_t *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1445 snd_mask_t *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1447 /* check the format */
1448 if (! snd_mask_test(fmts, fp->format)) {
1449 hwc_debug(" > check: no supported format %d\n", fp->format);
1450 return 0;
1452 /* check the channels */
1453 if (fp->channels < ct->min || fp->channels > ct->max) {
1454 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1455 return 0;
1457 /* check the rate is within the range */
1458 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1459 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1460 return 0;
1462 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1463 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1464 return 0;
1466 return 1;
1469 static int hw_rule_rate(snd_pcm_hw_params_t *params,
1470 snd_pcm_hw_rule_t *rule)
1472 snd_usb_substream_t *subs = rule->private;
1473 struct list_head *p;
1474 snd_interval_t *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1475 unsigned int rmin, rmax;
1476 int changed;
1478 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1479 changed = 0;
1480 rmin = rmax = 0;
1481 list_for_each(p, &subs->fmt_list) {
1482 struct audioformat *fp;
1483 fp = list_entry(p, struct audioformat, list);
1484 if (! hw_check_valid_format(params, fp))
1485 continue;
1486 if (changed++) {
1487 if (rmin > fp->rate_min)
1488 rmin = fp->rate_min;
1489 if (rmax < fp->rate_max)
1490 rmax = fp->rate_max;
1491 } else {
1492 rmin = fp->rate_min;
1493 rmax = fp->rate_max;
1497 if (! changed) {
1498 hwc_debug(" --> get empty\n");
1499 it->empty = 1;
1500 return -EINVAL;
1503 changed = 0;
1504 if (it->min < rmin) {
1505 it->min = rmin;
1506 it->openmin = 0;
1507 changed = 1;
1509 if (it->max > rmax) {
1510 it->max = rmax;
1511 it->openmax = 0;
1512 changed = 1;
1514 if (snd_interval_checkempty(it)) {
1515 it->empty = 1;
1516 return -EINVAL;
1518 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1519 return changed;
1523 static int hw_rule_channels(snd_pcm_hw_params_t *params,
1524 snd_pcm_hw_rule_t *rule)
1526 snd_usb_substream_t *subs = rule->private;
1527 struct list_head *p;
1528 snd_interval_t *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1529 unsigned int rmin, rmax;
1530 int changed;
1532 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1533 changed = 0;
1534 rmin = rmax = 0;
1535 list_for_each(p, &subs->fmt_list) {
1536 struct audioformat *fp;
1537 fp = list_entry(p, struct audioformat, list);
1538 if (! hw_check_valid_format(params, fp))
1539 continue;
1540 if (changed++) {
1541 if (rmin > fp->channels)
1542 rmin = fp->channels;
1543 if (rmax < fp->channels)
1544 rmax = fp->channels;
1545 } else {
1546 rmin = fp->channels;
1547 rmax = fp->channels;
1551 if (! changed) {
1552 hwc_debug(" --> get empty\n");
1553 it->empty = 1;
1554 return -EINVAL;
1557 changed = 0;
1558 if (it->min < rmin) {
1559 it->min = rmin;
1560 it->openmin = 0;
1561 changed = 1;
1563 if (it->max > rmax) {
1564 it->max = rmax;
1565 it->openmax = 0;
1566 changed = 1;
1568 if (snd_interval_checkempty(it)) {
1569 it->empty = 1;
1570 return -EINVAL;
1572 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1573 return changed;
1576 static int hw_rule_format(snd_pcm_hw_params_t *params,
1577 snd_pcm_hw_rule_t *rule)
1579 snd_usb_substream_t *subs = rule->private;
1580 struct list_head *p;
1581 snd_mask_t *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1582 u64 fbits;
1583 u32 oldbits[2];
1584 int changed;
1586 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1587 fbits = 0;
1588 list_for_each(p, &subs->fmt_list) {
1589 struct audioformat *fp;
1590 fp = list_entry(p, struct audioformat, list);
1591 if (! hw_check_valid_format(params, fp))
1592 continue;
1593 fbits |= (1ULL << fp->format);
1596 oldbits[0] = fmt->bits[0];
1597 oldbits[1] = fmt->bits[1];
1598 fmt->bits[0] &= (u32)fbits;
1599 fmt->bits[1] &= (u32)(fbits >> 32);
1600 if (! fmt->bits[0] && ! fmt->bits[1]) {
1601 hwc_debug(" --> get empty\n");
1602 return -EINVAL;
1604 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1605 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1606 return changed;
1609 #define MAX_MASK 64
1612 * check whether the registered audio formats need special hw-constraints
1614 static int check_hw_params_convention(snd_usb_substream_t *subs)
1616 int i;
1617 u32 *channels;
1618 u32 *rates;
1619 u32 cmaster, rmaster;
1620 u32 rate_min = 0, rate_max = 0;
1621 struct list_head *p;
1622 int err = 1;
1624 channels = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1625 rates = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1627 list_for_each(p, &subs->fmt_list) {
1628 struct audioformat *f;
1629 f = list_entry(p, struct audioformat, list);
1630 /* unconventional channels? */
1631 if (f->channels > 32)
1632 goto __out;
1633 /* continuous rate min/max matches? */
1634 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1635 if (rate_min && f->rate_min != rate_min)
1636 goto __out;
1637 if (rate_max && f->rate_max != rate_max)
1638 goto __out;
1639 rate_min = f->rate_min;
1640 rate_max = f->rate_max;
1642 /* combination of continuous rates and fixed rates? */
1643 if (rates[f->format] & SNDRV_PCM_RATE_CONTINUOUS) {
1644 if (f->rates != rates[f->format])
1645 goto __out;
1647 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1648 if (rates[f->format] && rates[f->format] != f->rates)
1649 goto __out;
1651 channels[f->format] |= (1 << f->channels);
1652 rates[f->format] |= f->rates;
1654 /* check whether channels and rates match for all formats */
1655 cmaster = rmaster = 0;
1656 for (i = 0; i < MAX_MASK; i++) {
1657 if (cmaster != channels[i] && cmaster && channels[i])
1658 goto __out;
1659 if (rmaster != rates[i] && rmaster && rates[i])
1660 goto __out;
1661 if (channels[i])
1662 cmaster = channels[i];
1663 if (rates[i])
1664 rmaster = rates[i];
1666 /* check whether channels match for all distinct rates */
1667 memset(channels, 0, MAX_MASK * sizeof(u32));
1668 list_for_each(p, &subs->fmt_list) {
1669 struct audioformat *f;
1670 f = list_entry(p, struct audioformat, list);
1671 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS)
1672 continue;
1673 for (i = 0; i < 32; i++) {
1674 if (f->rates & (1 << i))
1675 channels[i] |= (1 << f->channels);
1678 cmaster = 0;
1679 for (i = 0; i < 32; i++) {
1680 if (cmaster != channels[i] && cmaster && channels[i])
1681 goto __out;
1682 if (channels[i])
1683 cmaster = channels[i];
1685 err = 0;
1687 __out:
1688 kfree(channels);
1689 kfree(rates);
1690 return err;
1695 * set up the runtime hardware information.
1698 static int setup_hw_info(snd_pcm_runtime_t *runtime, snd_usb_substream_t *subs)
1700 struct list_head *p;
1701 int err;
1703 runtime->hw.formats = subs->formats;
1705 runtime->hw.rate_min = 0x7fffffff;
1706 runtime->hw.rate_max = 0;
1707 runtime->hw.channels_min = 256;
1708 runtime->hw.channels_max = 0;
1709 runtime->hw.rates = 0;
1710 /* check min/max rates and channels */
1711 list_for_each(p, &subs->fmt_list) {
1712 struct audioformat *fp;
1713 fp = list_entry(p, struct audioformat, list);
1714 runtime->hw.rates |= fp->rates;
1715 if (runtime->hw.rate_min > fp->rate_min)
1716 runtime->hw.rate_min = fp->rate_min;
1717 if (runtime->hw.rate_max < fp->rate_max)
1718 runtime->hw.rate_max = fp->rate_max;
1719 if (runtime->hw.channels_min > fp->channels)
1720 runtime->hw.channels_min = fp->channels;
1721 if (runtime->hw.channels_max < fp->channels)
1722 runtime->hw.channels_max = fp->channels;
1723 if (fp->fmt_type == USB_FORMAT_TYPE_II && fp->frame_size > 0) {
1724 /* FIXME: there might be more than one audio formats... */
1725 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1726 fp->frame_size;
1730 /* set the period time minimum 1ms */
1731 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1732 1000 * MIN_PACKS_URB,
1733 /*(nrpacks * MAX_URBS) * 1000*/ UINT_MAX);
1735 if (check_hw_params_convention(subs)) {
1736 hwc_debug("setting extra hw constraints...\n");
1737 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1738 hw_rule_rate, subs,
1739 SNDRV_PCM_HW_PARAM_FORMAT,
1740 SNDRV_PCM_HW_PARAM_CHANNELS,
1741 -1)) < 0)
1742 return err;
1743 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1744 hw_rule_channels, subs,
1745 SNDRV_PCM_HW_PARAM_FORMAT,
1746 SNDRV_PCM_HW_PARAM_RATE,
1747 -1)) < 0)
1748 return err;
1749 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1750 hw_rule_format, subs,
1751 SNDRV_PCM_HW_PARAM_RATE,
1752 SNDRV_PCM_HW_PARAM_CHANNELS,
1753 -1)) < 0)
1754 return err;
1756 return 0;
1759 static int snd_usb_pcm_open(snd_pcm_substream_t *substream, int direction,
1760 snd_pcm_hardware_t *hw)
1762 snd_usb_stream_t *as = snd_pcm_substream_chip(substream);
1763 snd_pcm_runtime_t *runtime = substream->runtime;
1764 snd_usb_substream_t *subs = &as->substream[direction];
1766 subs->interface = -1;
1767 subs->format = 0;
1768 runtime->hw = *hw;
1769 runtime->private_data = subs;
1770 subs->pcm_substream = substream;
1771 return setup_hw_info(runtime, subs);
1774 static int snd_usb_pcm_close(snd_pcm_substream_t *substream, int direction)
1776 snd_usb_stream_t *as = snd_pcm_substream_chip(substream);
1777 snd_usb_substream_t *subs = &as->substream[direction];
1779 if (subs->interface >= 0) {
1780 usb_set_interface(subs->dev, subs->interface, 0);
1781 subs->interface = -1;
1783 subs->pcm_substream = NULL;
1784 return 0;
1787 static int snd_usb_playback_open(snd_pcm_substream_t *substream)
1789 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK, &snd_usb_playback);
1792 static int snd_usb_playback_close(snd_pcm_substream_t *substream)
1794 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1797 static int snd_usb_capture_open(snd_pcm_substream_t *substream)
1799 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE, &snd_usb_capture);
1802 static int snd_usb_capture_close(snd_pcm_substream_t *substream)
1804 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1807 static snd_pcm_ops_t snd_usb_playback_ops = {
1808 .open = snd_usb_playback_open,
1809 .close = snd_usb_playback_close,
1810 .ioctl = snd_pcm_lib_ioctl,
1811 .hw_params = snd_usb_hw_params,
1812 .hw_free = snd_usb_hw_free,
1813 .prepare = snd_usb_pcm_prepare,
1814 .trigger = snd_usb_pcm_trigger,
1815 .pointer = snd_usb_pcm_pointer,
1818 static snd_pcm_ops_t snd_usb_capture_ops = {
1819 .open = snd_usb_capture_open,
1820 .close = snd_usb_capture_close,
1821 .ioctl = snd_pcm_lib_ioctl,
1822 .hw_params = snd_usb_hw_params,
1823 .hw_free = snd_usb_hw_free,
1824 .prepare = snd_usb_pcm_prepare,
1825 .trigger = snd_usb_pcm_trigger,
1826 .pointer = snd_usb_pcm_pointer,
1832 * helper functions
1836 * combine bytes and get an integer value
1838 unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
1840 switch (size) {
1841 case 1: return *bytes;
1842 case 2: return combine_word(bytes);
1843 case 3: return combine_triple(bytes);
1844 case 4: return combine_quad(bytes);
1845 default: return 0;
1850 * parse descriptor buffer and return the pointer starting the given
1851 * descriptor type.
1853 void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
1855 u8 *p, *end, *next;
1857 p = descstart;
1858 end = p + desclen;
1859 for (; p < end;) {
1860 if (p[0] < 2)
1861 return NULL;
1862 next = p + p[0];
1863 if (next > end)
1864 return NULL;
1865 if (p[1] == dtype && (!after || (void *)p > after)) {
1866 return p;
1868 p = next;
1870 return NULL;
1874 * find a class-specified interface descriptor with the given subtype.
1876 void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
1878 unsigned char *p = after;
1880 while ((p = snd_usb_find_desc(buffer, buflen, p,
1881 USB_DT_CS_INTERFACE)) != NULL) {
1882 if (p[0] >= 3 && p[2] == dsubtype)
1883 return p;
1885 return NULL;
1889 * Wrapper for usb_control_msg().
1890 * Allocates a temp buffer to prevent dmaing from/to the stack.
1892 int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
1893 __u8 requesttype, __u16 value, __u16 index, void *data,
1894 __u16 size, int timeout)
1896 int err;
1897 void *buf = NULL;
1899 if (size > 0) {
1900 buf = kmalloc(size, GFP_KERNEL);
1901 if (!buf)
1902 return -ENOMEM;
1903 memcpy(buf, data, size);
1905 err = usb_control_msg(dev, pipe, request, requesttype,
1906 value, index, buf, size, timeout);
1907 if (size > 0) {
1908 memcpy(data, buf, size);
1909 kfree(buf);
1911 return err;
1916 * entry point for linux usb interface
1919 static int usb_audio_probe(struct usb_interface *intf,
1920 const struct usb_device_id *id);
1921 static void usb_audio_disconnect(struct usb_interface *intf);
1923 static struct usb_device_id usb_audio_ids [] = {
1924 #include "usbquirks.h"
1925 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
1926 .bInterfaceClass = USB_CLASS_AUDIO,
1927 .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL },
1928 { } /* Terminating entry */
1931 MODULE_DEVICE_TABLE (usb, usb_audio_ids);
1933 static struct usb_driver usb_audio_driver = {
1934 .owner = THIS_MODULE,
1935 .name = "snd-usb-audio",
1936 .probe = usb_audio_probe,
1937 .disconnect = usb_audio_disconnect,
1938 .id_table = usb_audio_ids,
1943 * proc interface for list the supported pcm formats
1945 static void proc_dump_substream_formats(snd_usb_substream_t *subs, snd_info_buffer_t *buffer)
1947 struct list_head *p;
1948 static char *sync_types[4] = {
1949 "NONE", "ASYNC", "ADAPTIVE", "SYNC"
1952 list_for_each(p, &subs->fmt_list) {
1953 struct audioformat *fp;
1954 fp = list_entry(p, struct audioformat, list);
1955 snd_iprintf(buffer, " Interface %d\n", fp->iface);
1956 snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
1957 snd_iprintf(buffer, " Format: %s\n", snd_pcm_format_name(fp->format));
1958 snd_iprintf(buffer, " Channels: %d\n", fp->channels);
1959 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
1960 fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
1961 fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
1962 sync_types[(fp->ep_attr & EP_ATTR_MASK) >> 2]);
1963 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1964 snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
1965 fp->rate_min, fp->rate_max);
1966 } else {
1967 unsigned int i;
1968 snd_iprintf(buffer, " Rates: ");
1969 for (i = 0; i < fp->nr_rates; i++) {
1970 if (i > 0)
1971 snd_iprintf(buffer, ", ");
1972 snd_iprintf(buffer, "%d", fp->rate_table[i]);
1974 snd_iprintf(buffer, "\n");
1976 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
1977 // snd_iprintf(buffer, " EP Attribute = 0x%x\n", fp->attributes);
1981 static void proc_dump_substream_status(snd_usb_substream_t *subs, snd_info_buffer_t *buffer)
1983 if (subs->running) {
1984 unsigned int i;
1985 snd_iprintf(buffer, " Status: Running\n");
1986 snd_iprintf(buffer, " Interface = %d\n", subs->interface);
1987 snd_iprintf(buffer, " Altset = %d\n", subs->format);
1988 snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
1989 for (i = 0; i < subs->nurbs; i++)
1990 snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
1991 snd_iprintf(buffer, "]\n");
1992 snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
1993 snd_iprintf(buffer, " Momentary freq = %u Hz\n",
1994 snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
1995 ? get_full_speed_hz(subs->freqm)
1996 : get_high_speed_hz(subs->freqm));
1997 } else {
1998 snd_iprintf(buffer, " Status: Stop\n");
2002 static void proc_pcm_format_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
2004 snd_usb_stream_t *stream = entry->private_data;
2006 snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
2008 if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
2009 snd_iprintf(buffer, "\nPlayback:\n");
2010 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2011 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2013 if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
2014 snd_iprintf(buffer, "\nCapture:\n");
2015 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2016 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2020 static void proc_pcm_format_add(snd_usb_stream_t *stream)
2022 snd_info_entry_t *entry;
2023 char name[32];
2024 snd_card_t *card = stream->chip->card;
2026 sprintf(name, "stream%d", stream->pcm_index);
2027 if (! snd_card_proc_new(card, name, &entry))
2028 snd_info_set_text_ops(entry, stream, 1024, proc_pcm_format_read);
2033 * initialize the substream instance.
2036 static void init_substream(snd_usb_stream_t *as, int stream, struct audioformat *fp)
2038 snd_usb_substream_t *subs = &as->substream[stream];
2040 INIT_LIST_HEAD(&subs->fmt_list);
2041 spin_lock_init(&subs->lock);
2043 subs->stream = as;
2044 subs->direction = stream;
2045 subs->dev = as->chip->dev;
2046 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
2047 subs->ops = audio_urb_ops[stream];
2048 else
2049 subs->ops = audio_urb_ops_high_speed[stream];
2050 snd_pcm_lib_preallocate_pages(as->pcm->streams[stream].substream,
2051 SNDRV_DMA_TYPE_CONTINUOUS,
2052 snd_dma_continuous_data(GFP_KERNEL),
2053 64 * 1024, 128 * 1024);
2054 snd_pcm_set_ops(as->pcm, stream,
2055 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2056 &snd_usb_playback_ops : &snd_usb_capture_ops);
2058 list_add_tail(&fp->list, &subs->fmt_list);
2059 subs->formats |= 1ULL << fp->format;
2060 subs->endpoint = fp->endpoint;
2061 subs->num_formats++;
2062 subs->fmt_type = fp->fmt_type;
2067 * free a substream
2069 static void free_substream(snd_usb_substream_t *subs)
2071 struct list_head *p, *n;
2073 if (! subs->num_formats)
2074 return; /* not initialized */
2075 list_for_each_safe(p, n, &subs->fmt_list) {
2076 struct audioformat *fp = list_entry(p, struct audioformat, list);
2077 kfree(fp->rate_table);
2078 kfree(fp);
2084 * free a usb stream instance
2086 static void snd_usb_audio_stream_free(snd_usb_stream_t *stream)
2088 free_substream(&stream->substream[0]);
2089 free_substream(&stream->substream[1]);
2090 list_del(&stream->list);
2091 kfree(stream);
2094 static void snd_usb_audio_pcm_free(snd_pcm_t *pcm)
2096 snd_usb_stream_t *stream = pcm->private_data;
2097 if (stream) {
2098 stream->pcm = NULL;
2099 snd_pcm_lib_preallocate_free_for_all(pcm);
2100 snd_usb_audio_stream_free(stream);
2106 * add this endpoint to the chip instance.
2107 * if a stream with the same endpoint already exists, append to it.
2108 * if not, create a new pcm stream.
2110 static int add_audio_endpoint(snd_usb_audio_t *chip, int stream, struct audioformat *fp)
2112 struct list_head *p;
2113 snd_usb_stream_t *as;
2114 snd_usb_substream_t *subs;
2115 snd_pcm_t *pcm;
2116 int err;
2118 list_for_each(p, &chip->pcm_list) {
2119 as = list_entry(p, snd_usb_stream_t, list);
2120 if (as->fmt_type != fp->fmt_type)
2121 continue;
2122 subs = &as->substream[stream];
2123 if (! subs->endpoint)
2124 continue;
2125 if (subs->endpoint == fp->endpoint) {
2126 list_add_tail(&fp->list, &subs->fmt_list);
2127 subs->num_formats++;
2128 subs->formats |= 1ULL << fp->format;
2129 return 0;
2132 /* look for an empty stream */
2133 list_for_each(p, &chip->pcm_list) {
2134 as = list_entry(p, snd_usb_stream_t, list);
2135 if (as->fmt_type != fp->fmt_type)
2136 continue;
2137 subs = &as->substream[stream];
2138 if (subs->endpoint)
2139 continue;
2140 err = snd_pcm_new_stream(as->pcm, stream, 1);
2141 if (err < 0)
2142 return err;
2143 init_substream(as, stream, fp);
2144 return 0;
2147 /* create a new pcm */
2148 as = kmalloc(sizeof(*as), GFP_KERNEL);
2149 if (! as)
2150 return -ENOMEM;
2151 memset(as, 0, sizeof(*as));
2152 as->pcm_index = chip->pcm_devs;
2153 as->chip = chip;
2154 as->fmt_type = fp->fmt_type;
2155 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
2156 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
2157 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
2158 &pcm);
2159 if (err < 0) {
2160 kfree(as);
2161 return err;
2163 as->pcm = pcm;
2164 pcm->private_data = as;
2165 pcm->private_free = snd_usb_audio_pcm_free;
2166 pcm->info_flags = 0;
2167 if (chip->pcm_devs > 0)
2168 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
2169 else
2170 strcpy(pcm->name, "USB Audio");
2172 init_substream(as, stream, fp);
2174 list_add(&as->list, &chip->pcm_list);
2175 chip->pcm_devs++;
2177 proc_pcm_format_add(as);
2179 return 0;
2184 * check if the device uses big-endian samples
2186 static int is_big_endian_format(struct usb_device *dev, struct audioformat *fp)
2188 /* M-Audio */
2189 if (le16_to_cpu(dev->descriptor.idVendor) == 0x0763) {
2190 /* Quattro: captured data only */
2191 if (le16_to_cpu(dev->descriptor.idProduct) == 0x2001 &&
2192 fp->endpoint & USB_DIR_IN)
2193 return 1;
2194 /* Audiophile USB */
2195 if (le16_to_cpu(dev->descriptor.idProduct) == 0x2003)
2196 return 1;
2198 return 0;
2202 * parse the audio format type I descriptor
2203 * and returns the corresponding pcm format
2205 * @dev: usb device
2206 * @fp: audioformat record
2207 * @format: the format tag (wFormatTag)
2208 * @fmt: the format type descriptor
2210 static int parse_audio_format_i_type(struct usb_device *dev, struct audioformat *fp,
2211 int format, unsigned char *fmt)
2213 int pcm_format;
2214 int sample_width, sample_bytes;
2216 /* FIXME: correct endianess and sign? */
2217 pcm_format = -1;
2218 sample_width = fmt[6];
2219 sample_bytes = fmt[5];
2220 switch (format) {
2221 case 0: /* some devices don't define this correctly... */
2222 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
2223 dev->devnum, fp->iface, fp->altsetting);
2224 /* fall-through */
2225 case USB_AUDIO_FORMAT_PCM:
2226 if (sample_width > sample_bytes * 8) {
2227 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
2228 dev->devnum, fp->iface, fp->altsetting,
2229 sample_width, sample_bytes);
2231 /* check the format byte size */
2232 switch (fmt[5]) {
2233 case 1:
2234 pcm_format = SNDRV_PCM_FORMAT_S8;
2235 break;
2236 case 2:
2237 if (is_big_endian_format(dev, fp))
2238 pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
2239 else
2240 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2241 break;
2242 case 3:
2243 if (is_big_endian_format(dev, fp))
2244 pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
2245 else
2246 pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
2247 break;
2248 case 4:
2249 pcm_format = SNDRV_PCM_FORMAT_S32_LE;
2250 break;
2251 default:
2252 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
2253 dev->devnum, fp->iface, fp->altsetting, sample_width, sample_bytes);
2254 break;
2256 break;
2257 case USB_AUDIO_FORMAT_PCM8:
2258 /* Dallas DS4201 workaround */
2259 if (le16_to_cpu(dev->descriptor.idVendor) == 0x04fa &&
2260 le16_to_cpu(dev->descriptor.idProduct) == 0x4201)
2261 pcm_format = SNDRV_PCM_FORMAT_S8;
2262 else
2263 pcm_format = SNDRV_PCM_FORMAT_U8;
2264 break;
2265 case USB_AUDIO_FORMAT_IEEE_FLOAT:
2266 pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
2267 break;
2268 case USB_AUDIO_FORMAT_ALAW:
2269 pcm_format = SNDRV_PCM_FORMAT_A_LAW;
2270 break;
2271 case USB_AUDIO_FORMAT_MU_LAW:
2272 pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
2273 break;
2274 default:
2275 snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
2276 dev->devnum, fp->iface, fp->altsetting, format);
2277 break;
2279 return pcm_format;
2284 * parse the format descriptor and stores the possible sample rates
2285 * on the audioformat table.
2287 * @dev: usb device
2288 * @fp: audioformat record
2289 * @fmt: the format descriptor
2290 * @offset: the start offset of descriptor pointing the rate type
2291 * (7 for type I and II, 8 for type II)
2293 static int parse_audio_format_rates(struct usb_device *dev, struct audioformat *fp,
2294 unsigned char *fmt, int offset)
2296 int nr_rates = fmt[offset];
2297 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
2298 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
2299 dev->devnum, fp->iface, fp->altsetting);
2300 return -1;
2303 if (nr_rates) {
2305 * build the rate table and bitmap flags
2307 int r, idx, c;
2308 /* this table corresponds to the SNDRV_PCM_RATE_XXX bit */
2309 static unsigned int conv_rates[] = {
2310 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
2311 64000, 88200, 96000, 176400, 192000
2313 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2314 if (fp->rate_table == NULL) {
2315 snd_printk(KERN_ERR "cannot malloc\n");
2316 return -1;
2319 fp->nr_rates = nr_rates;
2320 fp->rate_min = fp->rate_max = combine_triple(&fmt[8]);
2321 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
2322 unsigned int rate = fp->rate_table[r] = combine_triple(&fmt[idx]);
2323 if (rate < fp->rate_min)
2324 fp->rate_min = rate;
2325 else if (rate > fp->rate_max)
2326 fp->rate_max = rate;
2327 for (c = 0; c < (int)ARRAY_SIZE(conv_rates); c++) {
2328 if (rate == conv_rates[c]) {
2329 fp->rates |= (1 << c);
2330 break;
2334 } else {
2335 /* continuous rates */
2336 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
2337 fp->rate_min = combine_triple(&fmt[offset + 1]);
2338 fp->rate_max = combine_triple(&fmt[offset + 4]);
2340 return 0;
2344 * parse the format type I and III descriptors
2346 static int parse_audio_format_i(struct usb_device *dev, struct audioformat *fp,
2347 int format, unsigned char *fmt)
2349 int pcm_format;
2351 if (fmt[3] == USB_FORMAT_TYPE_III) {
2352 /* FIXME: the format type is really IECxxx
2353 * but we give normal PCM format to get the existing
2354 * apps working...
2356 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2357 } else {
2358 pcm_format = parse_audio_format_i_type(dev, fp, format, fmt);
2359 if (pcm_format < 0)
2360 return -1;
2362 fp->format = pcm_format;
2363 fp->channels = fmt[4];
2364 if (fp->channels < 1) {
2365 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
2366 dev->devnum, fp->iface, fp->altsetting, fp->channels);
2367 return -1;
2369 return parse_audio_format_rates(dev, fp, fmt, 7);
2373 * prase the format type II descriptor
2375 static int parse_audio_format_ii(struct usb_device *dev, struct audioformat *fp,
2376 int format, unsigned char *fmt)
2378 int brate, framesize;
2379 switch (format) {
2380 case USB_AUDIO_FORMAT_AC3:
2381 /* FIXME: there is no AC3 format defined yet */
2382 // fp->format = SNDRV_PCM_FORMAT_AC3;
2383 fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
2384 break;
2385 case USB_AUDIO_FORMAT_MPEG:
2386 fp->format = SNDRV_PCM_FORMAT_MPEG;
2387 break;
2388 default:
2389 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag 0x%x is detected. processed as MPEG.\n",
2390 dev->devnum, fp->iface, fp->altsetting, format);
2391 fp->format = SNDRV_PCM_FORMAT_MPEG;
2392 break;
2394 fp->channels = 1;
2395 brate = combine_word(&fmt[4]); /* fmt[4,5] : wMaxBitRate (in kbps) */
2396 framesize = combine_word(&fmt[6]); /* fmt[6,7]: wSamplesPerFrame */
2397 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
2398 fp->frame_size = framesize;
2399 return parse_audio_format_rates(dev, fp, fmt, 8); /* fmt[8..] sample rates */
2402 static int parse_audio_format(struct usb_device *dev, struct audioformat *fp,
2403 int format, unsigned char *fmt, int stream)
2405 int err;
2407 switch (fmt[3]) {
2408 case USB_FORMAT_TYPE_I:
2409 case USB_FORMAT_TYPE_III:
2410 err = parse_audio_format_i(dev, fp, format, fmt);
2411 break;
2412 case USB_FORMAT_TYPE_II:
2413 err = parse_audio_format_ii(dev, fp, format, fmt);
2414 break;
2415 default:
2416 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
2417 dev->devnum, fp->iface, fp->altsetting, fmt[3]);
2418 return -1;
2420 fp->fmt_type = fmt[3];
2421 if (err < 0)
2422 return err;
2423 #if 1
2424 /* FIXME: temporary hack for extigy */
2425 /* extigy apparently supports sample rates other than 48k
2426 * but not in ordinary way. so we enable only 48k atm.
2428 if (le16_to_cpu(dev->descriptor.idVendor) == 0x041e &&
2429 le16_to_cpu(dev->descriptor.idProduct) == 0x3000) {
2430 if (fmt[3] == USB_FORMAT_TYPE_I &&
2431 stream == SNDRV_PCM_STREAM_PLAYBACK &&
2432 fp->rates != SNDRV_PCM_RATE_48000)
2433 return -1; /* use 48k only */
2435 #endif
2436 return 0;
2439 static int parse_audio_endpoints(snd_usb_audio_t *chip, int iface_no)
2441 struct usb_device *dev;
2442 struct usb_interface *iface;
2443 struct usb_host_interface *alts;
2444 struct usb_interface_descriptor *altsd;
2445 int i, altno, err, stream;
2446 int format;
2447 struct audioformat *fp;
2448 unsigned char *fmt, *csep;
2450 dev = chip->dev;
2452 /* parse the interface's altsettings */
2453 iface = usb_ifnum_to_if(dev, iface_no);
2454 for (i = 0; i < iface->num_altsetting; i++) {
2455 alts = &iface->altsetting[i];
2456 altsd = get_iface_desc(alts);
2457 /* skip invalid one */
2458 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2459 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2460 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING &&
2461 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
2462 altsd->bNumEndpoints < 1 ||
2463 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
2464 continue;
2465 /* must be isochronous */
2466 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2467 USB_ENDPOINT_XFER_ISOC)
2468 continue;
2469 /* check direction */
2470 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
2471 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2472 altno = altsd->bAlternateSetting;
2474 /* get audio formats */
2475 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, AS_GENERAL);
2476 if (!fmt) {
2477 snd_printk(KERN_ERR "%d:%u:%d : AS_GENERAL descriptor not found\n",
2478 dev->devnum, iface_no, altno);
2479 continue;
2482 if (fmt[0] < 7) {
2483 snd_printk(KERN_ERR "%d:%u:%d : invalid AS_GENERAL desc\n",
2484 dev->devnum, iface_no, altno);
2485 continue;
2488 format = (fmt[6] << 8) | fmt[5]; /* remember the format value */
2490 /* get format type */
2491 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, FORMAT_TYPE);
2492 if (!fmt) {
2493 snd_printk(KERN_ERR "%d:%u:%d : no FORMAT_TYPE desc\n",
2494 dev->devnum, iface_no, altno);
2495 continue;
2497 if (fmt[0] < 8) {
2498 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
2499 dev->devnum, iface_no, altno);
2500 continue;
2503 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
2504 /* Creamware Noah has this descriptor after the 2nd endpoint */
2505 if (!csep && altsd->bNumEndpoints >= 2)
2506 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
2507 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2508 snd_printk(KERN_ERR "%d:%u:%d : no or invalid class specific endpoint descriptor\n",
2509 dev->devnum, iface_no, altno);
2510 continue;
2513 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2514 if (! fp) {
2515 snd_printk(KERN_ERR "cannot malloc\n");
2516 return -ENOMEM;
2519 memset(fp, 0, sizeof(*fp));
2520 fp->iface = iface_no;
2521 fp->altsetting = altno;
2522 fp->altset_idx = i;
2523 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2524 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2525 /* FIXME: decode wMaxPacketSize of high bandwith endpoints */
2526 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2527 fp->attributes = csep[3];
2529 /* some quirks for attributes here */
2531 /* workaround for AudioTrak Optoplay */
2532 if (le16_to_cpu(dev->descriptor.idVendor) == 0x0a92 &&
2533 le16_to_cpu(dev->descriptor.idProduct) == 0x0053) {
2534 /* Optoplay sets the sample rate attribute although
2535 * it seems not supporting it in fact.
2537 fp->attributes &= ~EP_CS_ATTR_SAMPLE_RATE;
2540 /* workaround for M-Audio Audiophile USB */
2541 if (le16_to_cpu(dev->descriptor.idVendor) == 0x0763 &&
2542 le16_to_cpu(dev->descriptor.idProduct) == 0x2003) {
2543 /* doesn't set the sample rate attribute, but supports it */
2544 fp->attributes |= EP_CS_ATTR_SAMPLE_RATE;
2548 * plantronics headset and Griffin iMic have set adaptive-in
2549 * although it's really not...
2551 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x047f &&
2552 le16_to_cpu(dev->descriptor.idProduct) == 0x0ca1) ||
2553 /* Griffin iMic (note that there is an older model 77d:223) */
2554 (le16_to_cpu(dev->descriptor.idVendor) == 0x077d &&
2555 le16_to_cpu(dev->descriptor.idProduct) == 0x07af)) {
2556 fp->ep_attr &= ~EP_ATTR_MASK;
2557 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2558 fp->ep_attr |= EP_ATTR_ADAPTIVE;
2559 else
2560 fp->ep_attr |= EP_ATTR_SYNC;
2563 /* ok, let's parse further... */
2564 if (parse_audio_format(dev, fp, format, fmt, stream) < 0) {
2565 kfree(fp->rate_table);
2566 kfree(fp);
2567 continue;
2570 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint 0x%x\n", dev->devnum, iface_no, i, fp->endpoint);
2571 err = add_audio_endpoint(chip, stream, fp);
2572 if (err < 0) {
2573 kfree(fp->rate_table);
2574 kfree(fp);
2575 return err;
2577 /* try to set the interface... */
2578 usb_set_interface(chip->dev, iface_no, altno);
2579 init_usb_pitch(chip->dev, iface_no, alts, fp);
2580 init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
2582 return 0;
2587 * disconnect streams
2588 * called from snd_usb_audio_disconnect()
2590 static void snd_usb_stream_disconnect(struct list_head *head, struct usb_driver *driver)
2592 int idx;
2593 snd_usb_stream_t *as;
2594 snd_usb_substream_t *subs;
2596 as = list_entry(head, snd_usb_stream_t, list);
2597 for (idx = 0; idx < 2; idx++) {
2598 subs = &as->substream[idx];
2599 if (!subs->num_formats)
2600 return;
2601 release_substream_urbs(subs, 1);
2602 subs->interface = -1;
2607 * parse audio control descriptor and create pcm/midi streams
2609 static int snd_usb_create_streams(snd_usb_audio_t *chip, int ctrlif)
2611 struct usb_device *dev = chip->dev;
2612 struct usb_host_interface *host_iface;
2613 struct usb_interface *iface;
2614 unsigned char *p1;
2615 int i, j;
2617 /* find audiocontrol interface */
2618 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
2619 if (!(p1 = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen, NULL, HEADER))) {
2620 snd_printk(KERN_ERR "cannot find HEADER\n");
2621 return -EINVAL;
2623 if (! p1[7] || p1[0] < 8 + p1[7]) {
2624 snd_printk(KERN_ERR "invalid HEADER\n");
2625 return -EINVAL;
2629 * parse all USB audio streaming interfaces
2631 for (i = 0; i < p1[7]; i++) {
2632 struct usb_host_interface *alts;
2633 struct usb_interface_descriptor *altsd;
2634 j = p1[8 + i];
2635 iface = usb_ifnum_to_if(dev, j);
2636 if (!iface) {
2637 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
2638 dev->devnum, ctrlif, j);
2639 continue;
2641 if (usb_interface_claimed(iface)) {
2642 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n", dev->devnum, ctrlif, j);
2643 continue;
2645 alts = &iface->altsetting[0];
2646 altsd = get_iface_desc(alts);
2647 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
2648 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
2649 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDI_STREAMING) {
2650 if (snd_usb_create_midi_interface(chip, iface, NULL) < 0) {
2651 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n", dev->devnum, ctrlif, j);
2652 continue;
2654 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2655 continue;
2657 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2658 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2659 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING) {
2660 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n", dev->devnum, ctrlif, j, altsd->bInterfaceClass);
2661 /* skip non-supported classes */
2662 continue;
2664 if (! parse_audio_endpoints(chip, j)) {
2665 usb_set_interface(dev, j, 0); /* reset the current interface */
2666 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2670 return 0;
2674 * create a stream for an endpoint/altsetting without proper descriptors
2676 static int create_fixed_stream_quirk(snd_usb_audio_t *chip,
2677 struct usb_interface *iface,
2678 const snd_usb_audio_quirk_t *quirk)
2680 struct audioformat *fp;
2681 struct usb_host_interface *alts;
2682 int stream, err;
2683 int *rate_table = NULL;
2685 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2686 if (! fp) {
2687 snd_printk(KERN_ERR "cannot malloc\n");
2688 return -ENOMEM;
2690 memcpy(fp, quirk->data, sizeof(*fp));
2691 if (fp->nr_rates > 0) {
2692 rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
2693 if (!rate_table) {
2694 kfree(fp);
2695 return -ENOMEM;
2697 memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
2698 fp->rate_table = rate_table;
2701 stream = (fp->endpoint & USB_DIR_IN)
2702 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2703 err = add_audio_endpoint(chip, stream, fp);
2704 if (err < 0) {
2705 kfree(fp);
2706 kfree(rate_table);
2707 return err;
2709 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
2710 fp->altset_idx >= iface->num_altsetting) {
2711 kfree(fp);
2712 kfree(rate_table);
2713 return -EINVAL;
2715 alts = &iface->altsetting[fp->altset_idx];
2716 usb_set_interface(chip->dev, fp->iface, 0);
2717 init_usb_pitch(chip->dev, fp->iface, alts, fp);
2718 init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
2719 return 0;
2723 * create a stream for an interface with proper descriptors
2725 static int create_standard_interface_quirk(snd_usb_audio_t *chip,
2726 struct usb_interface *iface,
2727 const snd_usb_audio_quirk_t *quirk)
2729 struct usb_host_interface *alts;
2730 struct usb_interface_descriptor *altsd;
2731 int err;
2733 alts = &iface->altsetting[0];
2734 altsd = get_iface_desc(alts);
2735 switch (quirk->type) {
2736 case QUIRK_AUDIO_STANDARD_INTERFACE:
2737 err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
2738 if (!err)
2739 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0); /* reset the current interface */
2740 break;
2741 case QUIRK_MIDI_STANDARD_INTERFACE:
2742 err = snd_usb_create_midi_interface(chip, iface, NULL);
2743 break;
2744 default:
2745 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2746 return -ENXIO;
2748 if (err < 0) {
2749 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
2750 altsd->bInterfaceNumber, err);
2751 return err;
2753 return 0;
2757 * Create a stream for an Edirol UA-700/UA-25 interface. The only way
2758 * to detect the sample rate is by looking at wMaxPacketSize.
2760 static int create_ua700_ua25_quirk(snd_usb_audio_t *chip,
2761 struct usb_interface *iface)
2763 static const struct audioformat ua_format = {
2764 .format = SNDRV_PCM_FORMAT_S24_3LE,
2765 .channels = 2,
2766 .fmt_type = USB_FORMAT_TYPE_I,
2767 .altsetting = 1,
2768 .altset_idx = 1,
2769 .rates = SNDRV_PCM_RATE_CONTINUOUS,
2771 struct usb_host_interface *alts;
2772 struct usb_interface_descriptor *altsd;
2773 struct audioformat *fp;
2774 int stream, err;
2776 /* both PCM and MIDI interfaces have 2 altsettings */
2777 if (iface->num_altsetting != 2)
2778 return -ENXIO;
2779 alts = &iface->altsetting[1];
2780 altsd = get_iface_desc(alts);
2782 if (altsd->bNumEndpoints == 2) {
2783 static const snd_usb_midi_endpoint_info_t ua700_ep = {
2784 .out_cables = 0x0003,
2785 .in_cables = 0x0003
2787 static const snd_usb_audio_quirk_t ua700_quirk = {
2788 .type = QUIRK_MIDI_FIXED_ENDPOINT,
2789 .data = &ua700_ep
2791 static const snd_usb_midi_endpoint_info_t ua25_ep = {
2792 .out_cables = 0x0001,
2793 .in_cables = 0x0001
2795 static const snd_usb_audio_quirk_t ua25_quirk = {
2796 .type = QUIRK_MIDI_FIXED_ENDPOINT,
2797 .data = &ua25_ep
2799 if (le16_to_cpu(chip->dev->descriptor.idProduct) == 0x002b)
2800 return snd_usb_create_midi_interface(chip, iface,
2801 &ua700_quirk);
2802 else
2803 return snd_usb_create_midi_interface(chip, iface,
2804 &ua25_quirk);
2807 if (altsd->bNumEndpoints != 1)
2808 return -ENXIO;
2810 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2811 if (!fp)
2812 return -ENOMEM;
2813 memcpy(fp, &ua_format, sizeof(*fp));
2815 fp->iface = altsd->bInterfaceNumber;
2816 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2817 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2818 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2820 switch (fp->maxpacksize) {
2821 case 0x120:
2822 fp->rate_max = fp->rate_min = 44100;
2823 break;
2824 case 0x138:
2825 case 0x140:
2826 fp->rate_max = fp->rate_min = 48000;
2827 break;
2828 case 0x258:
2829 case 0x260:
2830 fp->rate_max = fp->rate_min = 96000;
2831 break;
2832 default:
2833 snd_printk(KERN_ERR "unknown sample rate\n");
2834 kfree(fp);
2835 return -ENXIO;
2838 stream = (fp->endpoint & USB_DIR_IN)
2839 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2840 err = add_audio_endpoint(chip, stream, fp);
2841 if (err < 0) {
2842 kfree(fp);
2843 return err;
2845 usb_set_interface(chip->dev, fp->iface, 0);
2846 return 0;
2850 * Create a stream for an Edirol UA-1000 interface.
2852 static int create_ua1000_quirk(snd_usb_audio_t *chip, struct usb_interface *iface)
2854 static const struct audioformat ua1000_format = {
2855 .format = SNDRV_PCM_FORMAT_S32_LE,
2856 .fmt_type = USB_FORMAT_TYPE_I,
2857 .altsetting = 1,
2858 .altset_idx = 1,
2859 .attributes = 0,
2860 .rates = SNDRV_PCM_RATE_CONTINUOUS,
2862 struct usb_host_interface *alts;
2863 struct usb_interface_descriptor *altsd;
2864 struct audioformat *fp;
2865 int stream, err;
2867 if (iface->num_altsetting != 2)
2868 return -ENXIO;
2869 alts = &iface->altsetting[1];
2870 altsd = get_iface_desc(alts);
2871 if (alts->extralen != 11 || alts->extra[1] != CS_AUDIO_INTERFACE ||
2872 altsd->bNumEndpoints != 1)
2873 return -ENXIO;
2875 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2876 if (!fp)
2877 return -ENOMEM;
2878 memcpy(fp, &ua1000_format, sizeof(*fp));
2880 fp->channels = alts->extra[4];
2881 fp->iface = altsd->bInterfaceNumber;
2882 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2883 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2884 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2885 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]);
2887 stream = (fp->endpoint & USB_DIR_IN)
2888 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2889 err = add_audio_endpoint(chip, stream, fp);
2890 if (err < 0) {
2891 kfree(fp);
2892 return err;
2894 /* FIXME: playback must be synchronized to capture */
2895 usb_set_interface(chip->dev, fp->iface, 0);
2896 return 0;
2899 static int snd_usb_create_quirk(snd_usb_audio_t *chip,
2900 struct usb_interface *iface,
2901 const snd_usb_audio_quirk_t *quirk);
2904 * handle the quirks for the contained interfaces
2906 static int create_composite_quirk(snd_usb_audio_t *chip,
2907 struct usb_interface *iface,
2908 const snd_usb_audio_quirk_t *quirk)
2910 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
2911 int err;
2913 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
2914 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
2915 if (!iface)
2916 continue;
2917 if (quirk->ifnum != probed_ifnum &&
2918 usb_interface_claimed(iface))
2919 continue;
2920 err = snd_usb_create_quirk(chip, iface, quirk);
2921 if (err < 0)
2922 return err;
2923 if (quirk->ifnum != probed_ifnum)
2924 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2926 return 0;
2931 * boot quirks
2934 #define EXTIGY_FIRMWARE_SIZE_OLD 794
2935 #define EXTIGY_FIRMWARE_SIZE_NEW 483
2937 static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
2939 struct usb_host_config *config = dev->actconfig;
2940 int err;
2942 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
2943 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
2944 snd_printdd("sending Extigy boot sequence...\n");
2945 /* Send message to force it to reconnect with full interface. */
2946 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
2947 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
2948 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
2949 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
2950 &dev->descriptor, sizeof(dev->descriptor));
2951 config = dev->actconfig;
2952 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
2953 err = usb_reset_configuration(dev);
2954 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
2955 snd_printdd("extigy_boot: new boot length = %d\n",
2956 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
2957 return -ENODEV; /* quit this anyway */
2959 return 0;
2964 * audio-interface quirks
2966 * returns zero if no standard audio/MIDI parsing is needed.
2967 * returns a postive value if standard audio/midi interfaces are parsed
2968 * after this.
2969 * returns a negative value at error.
2971 static int snd_usb_create_quirk(snd_usb_audio_t *chip,
2972 struct usb_interface *iface,
2973 const snd_usb_audio_quirk_t *quirk)
2975 switch (quirk->type) {
2976 case QUIRK_MIDI_FIXED_ENDPOINT:
2977 case QUIRK_MIDI_YAMAHA:
2978 case QUIRK_MIDI_MIDIMAN:
2979 case QUIRK_MIDI_NOVATION:
2980 case QUIRK_MIDI_MOTU:
2981 case QUIRK_MIDI_EMAGIC:
2982 return snd_usb_create_midi_interface(chip, iface, quirk);
2983 case QUIRK_COMPOSITE:
2984 return create_composite_quirk(chip, iface, quirk);
2985 case QUIRK_AUDIO_FIXED_ENDPOINT:
2986 return create_fixed_stream_quirk(chip, iface, quirk);
2987 case QUIRK_AUDIO_STANDARD_INTERFACE:
2988 case QUIRK_MIDI_STANDARD_INTERFACE:
2989 return create_standard_interface_quirk(chip, iface, quirk);
2990 case QUIRK_AUDIO_EDIROL_UA700_UA25:
2991 return create_ua700_ua25_quirk(chip, iface);
2992 case QUIRK_AUDIO_EDIROL_UA1000:
2993 return create_ua1000_quirk(chip, iface);
2994 case QUIRK_IGNORE_INTERFACE:
2995 return 0;
2996 default:
2997 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2998 return -ENXIO;
3004 * common proc files to show the usb device info
3006 static void proc_audio_usbbus_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
3008 snd_usb_audio_t *chip = entry->private_data;
3009 if (! chip->shutdown)
3010 snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
3013 static void proc_audio_usbid_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
3015 snd_usb_audio_t *chip = entry->private_data;
3016 if (! chip->shutdown)
3017 snd_iprintf(buffer, "%04x:%04x\n",
3018 le16_to_cpu(chip->dev->descriptor.idVendor),
3019 le16_to_cpu(chip->dev->descriptor.idProduct));
3022 static void snd_usb_audio_create_proc(snd_usb_audio_t *chip)
3024 snd_info_entry_t *entry;
3025 if (! snd_card_proc_new(chip->card, "usbbus", &entry))
3026 snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbbus_read);
3027 if (! snd_card_proc_new(chip->card, "usbid", &entry))
3028 snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbid_read);
3032 * free the chip instance
3034 * here we have to do not much, since pcm and controls are already freed
3038 static int snd_usb_audio_free(snd_usb_audio_t *chip)
3040 kfree(chip);
3041 return 0;
3044 static int snd_usb_audio_dev_free(snd_device_t *device)
3046 snd_usb_audio_t *chip = device->device_data;
3047 return snd_usb_audio_free(chip);
3052 * create a chip instance and set its names.
3054 static int snd_usb_audio_create(struct usb_device *dev, int idx,
3055 const snd_usb_audio_quirk_t *quirk,
3056 snd_usb_audio_t **rchip)
3058 snd_card_t *card;
3059 snd_usb_audio_t *chip;
3060 int err, len;
3061 char component[14];
3062 static snd_device_ops_t ops = {
3063 .dev_free = snd_usb_audio_dev_free,
3066 *rchip = NULL;
3068 if (snd_usb_get_speed(dev) != USB_SPEED_FULL &&
3069 snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
3070 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
3071 return -ENXIO;
3074 card = snd_card_new(index[idx], id[idx], THIS_MODULE, 0);
3075 if (card == NULL) {
3076 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
3077 return -ENOMEM;
3080 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
3081 if (! chip) {
3082 snd_card_free(card);
3083 return -ENOMEM;
3086 chip->index = idx;
3087 chip->dev = dev;
3088 chip->card = card;
3089 INIT_LIST_HEAD(&chip->pcm_list);
3090 INIT_LIST_HEAD(&chip->midi_list);
3092 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
3093 snd_usb_audio_free(chip);
3094 snd_card_free(card);
3095 return err;
3098 strcpy(card->driver, "USB-Audio");
3099 sprintf(component, "USB%04x:%04x",
3100 le16_to_cpu(dev->descriptor.idVendor),
3101 le16_to_cpu(dev->descriptor.idProduct));
3102 snd_component_add(card, component);
3104 /* retrieve the device string as shortname */
3105 if (quirk && quirk->product_name) {
3106 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
3107 } else {
3108 if (!dev->descriptor.iProduct ||
3109 usb_string(dev, dev->descriptor.iProduct,
3110 card->shortname, sizeof(card->shortname)) <= 0) {
3111 /* no name available from anywhere, so use ID */
3112 sprintf(card->shortname, "USB Device %#04x:%#04x",
3113 le16_to_cpu(dev->descriptor.idVendor),
3114 le16_to_cpu(dev->descriptor.idProduct));
3118 /* retrieve the vendor and device strings as longname */
3119 if (quirk && quirk->vendor_name) {
3120 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
3121 } else {
3122 if (dev->descriptor.iManufacturer)
3123 len = usb_string(dev, dev->descriptor.iManufacturer,
3124 card->longname, sizeof(card->longname));
3125 else
3126 len = 0;
3127 /* we don't really care if there isn't any vendor string */
3129 if (len > 0)
3130 strlcat(card->longname, " ", sizeof(card->longname));
3132 strlcat(card->longname, card->shortname, sizeof(card->longname));
3134 len = strlcat(card->longname, " at ", sizeof(card->longname));
3136 if (len < sizeof(card->longname))
3137 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
3139 strlcat(card->longname,
3140 snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" : ", high speed",
3141 sizeof(card->longname));
3143 snd_usb_audio_create_proc(chip);
3145 snd_card_set_dev(card, &dev->dev);
3147 *rchip = chip;
3148 return 0;
3153 * probe the active usb device
3155 * note that this can be called multiple times per a device, when it
3156 * includes multiple audio control interfaces.
3158 * thus we check the usb device pointer and creates the card instance
3159 * only at the first time. the successive calls of this function will
3160 * append the pcm interface to the corresponding card.
3162 static void *snd_usb_audio_probe(struct usb_device *dev,
3163 struct usb_interface *intf,
3164 const struct usb_device_id *usb_id)
3166 struct usb_host_config *config = dev->actconfig;
3167 const snd_usb_audio_quirk_t *quirk = (const snd_usb_audio_quirk_t *)usb_id->driver_info;
3168 int i, err;
3169 snd_usb_audio_t *chip;
3170 struct usb_host_interface *alts;
3171 int ifnum;
3173 alts = &intf->altsetting[0];
3174 ifnum = get_iface_desc(alts)->bInterfaceNumber;
3176 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
3177 goto __err_val;
3179 /* SB Extigy needs special boot-up sequence */
3180 /* if more models come, this will go to the quirk list. */
3181 if (le16_to_cpu(dev->descriptor.idVendor) == 0x041e &&
3182 le16_to_cpu(dev->descriptor.idProduct) == 0x3000) {
3183 if (snd_usb_extigy_boot_quirk(dev, intf) < 0)
3184 goto __err_val;
3185 config = dev->actconfig;
3189 * found a config. now register to ALSA
3192 /* check whether it's already registered */
3193 chip = NULL;
3194 down(&register_mutex);
3195 for (i = 0; i < SNDRV_CARDS; i++) {
3196 if (usb_chip[i] && usb_chip[i]->dev == dev) {
3197 if (usb_chip[i]->shutdown) {
3198 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
3199 goto __error;
3201 chip = usb_chip[i];
3202 break;
3205 if (! chip) {
3206 /* it's a fresh one.
3207 * now look for an empty slot and create a new card instance
3209 /* first, set the current configuration for this device */
3210 if (usb_reset_configuration(dev) < 0) {
3211 snd_printk(KERN_ERR "cannot reset configuration (value 0x%x)\n", get_cfg_desc(config)->bConfigurationValue);
3212 goto __error;
3214 for (i = 0; i < SNDRV_CARDS; i++)
3215 if (enable[i] && ! usb_chip[i] &&
3216 (vid[i] == -1 || vid[i] == le16_to_cpu(dev->descriptor.idVendor)) &&
3217 (pid[i] == -1 || pid[i] == le16_to_cpu(dev->descriptor.idProduct))) {
3218 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
3219 goto __error;
3221 break;
3223 if (! chip) {
3224 snd_printk(KERN_ERR "no available usb audio device\n");
3225 goto __error;
3229 err = 1; /* continue */
3230 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
3231 /* need some special handlings */
3232 if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)
3233 goto __error;
3236 if (err > 0) {
3237 /* create normal USB audio interfaces */
3238 if (snd_usb_create_streams(chip, ifnum) < 0 ||
3239 snd_usb_create_mixer(chip, ifnum) < 0) {
3240 goto __error;
3244 /* we are allowed to call snd_card_register() many times */
3245 if (snd_card_register(chip->card) < 0) {
3246 goto __error;
3249 usb_chip[chip->index] = chip;
3250 chip->num_interfaces++;
3251 up(&register_mutex);
3252 return chip;
3254 __error:
3255 if (chip && !chip->num_interfaces)
3256 snd_card_free(chip->card);
3257 up(&register_mutex);
3258 __err_val:
3259 return NULL;
3263 * we need to take care of counter, since disconnection can be called also
3264 * many times as well as usb_audio_probe().
3266 static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
3268 snd_usb_audio_t *chip;
3269 snd_card_t *card;
3270 struct list_head *p;
3272 if (ptr == (void *)-1L)
3273 return;
3275 chip = ptr;
3276 card = chip->card;
3277 down(&register_mutex);
3278 chip->shutdown = 1;
3279 chip->num_interfaces--;
3280 if (chip->num_interfaces <= 0) {
3281 snd_card_disconnect(card);
3282 /* release the pcm resources */
3283 list_for_each(p, &chip->pcm_list) {
3284 snd_usb_stream_disconnect(p, &usb_audio_driver);
3286 /* release the midi resources */
3287 list_for_each(p, &chip->midi_list) {
3288 snd_usbmidi_disconnect(p, &usb_audio_driver);
3290 usb_chip[chip->index] = NULL;
3291 up(&register_mutex);
3292 snd_card_free(card);
3293 } else {
3294 up(&register_mutex);
3299 * new 2.5 USB kernel API
3301 static int usb_audio_probe(struct usb_interface *intf,
3302 const struct usb_device_id *id)
3304 void *chip;
3305 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
3306 if (chip) {
3307 dev_set_drvdata(&intf->dev, chip);
3308 return 0;
3309 } else
3310 return -EIO;
3313 static void usb_audio_disconnect(struct usb_interface *intf)
3315 snd_usb_audio_disconnect(interface_to_usbdev(intf),
3316 dev_get_drvdata(&intf->dev));
3320 static int __init snd_usb_audio_init(void)
3322 if (nrpacks < MIN_PACKS_URB || nrpacks > MAX_PACKS) {
3323 printk(KERN_WARNING "invalid nrpacks value.\n");
3324 return -EINVAL;
3326 usb_register(&usb_audio_driver);
3327 return 0;
3331 static void __exit snd_usb_audio_cleanup(void)
3333 usb_deregister(&usb_audio_driver);
3336 module_init(snd_usb_audio_init);
3337 module_exit(snd_usb_audio_cleanup);