2 * (Tentative) USB Audio Driver for ALSA
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * - async unlink should be used for avoiding the sleep inside lock.
31 * 2.4.22 usb-uhci seems buggy for async unlinking and results in
32 * oops. in such a cse, pass async_unlink=0 option.
33 * - the linked URBs would be preferred but not used so far because of
34 * the instability of unlinking.
35 * - type II is not supported properly. there is no device which supports
36 * this type *correctly*. SB extigy looks as if it supports, but it's
37 * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
41 #include <linux/bitops.h>
42 #include <linux/init.h>
43 #include <linux/list.h>
44 #include <linux/slab.h>
45 #include <linux/string.h>
46 #include <linux/usb.h>
47 #include <linux/vmalloc.h>
48 #include <linux/moduleparam.h>
49 #include <linux/mutex.h>
50 #include <sound/core.h>
51 #include <sound/info.h>
52 #include <sound/pcm.h>
53 #include <sound/pcm_params.h>
54 #include <sound/initval.h>
59 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
60 MODULE_DESCRIPTION("USB Audio");
61 MODULE_LICENSE("GPL");
62 MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
65 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-MAX */
66 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* ID for this card */
67 static int enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
;/* Enable this card */
68 /* Vendor/product IDs for this card */
69 static int vid
[SNDRV_CARDS
] = { [0 ... (SNDRV_CARDS
-1)] = -1 };
70 static int pid
[SNDRV_CARDS
] = { [0 ... (SNDRV_CARDS
-1)] = -1 };
71 static int nrpacks
= 8; /* max. number of packets per urb */
72 static int async_unlink
= 1;
73 static int device_setup
[SNDRV_CARDS
]; /* device parameter for this card*/
75 module_param_array(index
, int, NULL
, 0444);
76 MODULE_PARM_DESC(index
, "Index value for the USB audio adapter.");
77 module_param_array(id
, charp
, NULL
, 0444);
78 MODULE_PARM_DESC(id
, "ID string for the USB audio adapter.");
79 module_param_array(enable
, bool, NULL
, 0444);
80 MODULE_PARM_DESC(enable
, "Enable USB audio adapter.");
81 module_param_array(vid
, int, NULL
, 0444);
82 MODULE_PARM_DESC(vid
, "Vendor ID for the USB audio device.");
83 module_param_array(pid
, int, NULL
, 0444);
84 MODULE_PARM_DESC(pid
, "Product ID for the USB audio device.");
85 module_param(nrpacks
, int, 0644);
86 MODULE_PARM_DESC(nrpacks
, "Max. number of packets per URB.");
87 module_param(async_unlink
, bool, 0444);
88 MODULE_PARM_DESC(async_unlink
, "Use async unlink mode.");
89 module_param_array(device_setup
, int, NULL
, 0444);
90 MODULE_PARM_DESC(device_setup
, "Specific device setup (if needed).");
94 * debug the h/w constraints
96 /* #define HW_CONST_DEBUG */
104 #define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
106 #define SYNC_URBS 4 /* always four urbs for sync */
107 #define MIN_PACKS_URB 1 /* minimum 1 packet per urb */
110 struct list_head list
;
111 snd_pcm_format_t format
; /* format type */
112 unsigned int channels
; /* # channels */
113 unsigned int fmt_type
; /* USB audio format type (1-3) */
114 unsigned int frame_size
; /* samples per frame for non-audio */
115 int iface
; /* interface number */
116 unsigned char altsetting
; /* corresponding alternate setting */
117 unsigned char altset_idx
; /* array index of altenate setting */
118 unsigned char attributes
; /* corresponding attributes of cs endpoint */
119 unsigned char endpoint
; /* endpoint */
120 unsigned char ep_attr
; /* endpoint attributes */
121 unsigned int maxpacksize
; /* max. packet size */
122 unsigned int rates
; /* rate bitmasks */
123 unsigned int rate_min
, rate_max
; /* min/max rates */
124 unsigned int nr_rates
; /* number of rate table entries */
125 unsigned int *rate_table
; /* rate table */
128 struct snd_usb_substream
;
132 unsigned int buffer_size
; /* size of data buffer, if data URB */
133 struct snd_usb_substream
*subs
;
134 int index
; /* index for urb array */
135 int packets
; /* number of packets per urb */
139 int (*prepare
)(struct snd_usb_substream
*subs
, struct snd_pcm_runtime
*runtime
, struct urb
*u
);
140 int (*retire
)(struct snd_usb_substream
*subs
, struct snd_pcm_runtime
*runtime
, struct urb
*u
);
141 int (*prepare_sync
)(struct snd_usb_substream
*subs
, struct snd_pcm_runtime
*runtime
, struct urb
*u
);
142 int (*retire_sync
)(struct snd_usb_substream
*subs
, struct snd_pcm_runtime
*runtime
, struct urb
*u
);
145 struct snd_usb_substream
{
146 struct snd_usb_stream
*stream
;
147 struct usb_device
*dev
;
148 struct snd_pcm_substream
*pcm_substream
;
149 int direction
; /* playback or capture */
150 int interface
; /* current interface */
151 int endpoint
; /* assigned endpoint */
152 struct audioformat
*cur_audiofmt
; /* current audioformat pointer (for hw_params callback) */
153 unsigned int cur_rate
; /* current rate (for hw_params callback) */
154 unsigned int period_bytes
; /* current period bytes (for hw_params callback) */
155 unsigned int format
; /* USB data format */
156 unsigned int datapipe
; /* the data i/o pipe */
157 unsigned int syncpipe
; /* 1 - async out or adaptive in */
158 unsigned int datainterval
; /* log_2 of data packet interval */
159 unsigned int syncinterval
; /* P for adaptive mode, 0 otherwise */
160 unsigned int freqn
; /* nominal sampling rate in fs/fps in Q16.16 format */
161 unsigned int freqm
; /* momentary sampling rate in fs/fps in Q16.16 format */
162 unsigned int freqmax
; /* maximum sampling rate, used for buffer management */
163 unsigned int phase
; /* phase accumulator */
164 unsigned int maxpacksize
; /* max packet size in bytes */
165 unsigned int maxframesize
; /* max packet size in frames */
166 unsigned int curpacksize
; /* current packet size in bytes (for capture) */
167 unsigned int curframesize
; /* current packet size in frames (for capture) */
168 unsigned int fill_max
: 1; /* fill max packet size always */
169 unsigned int fmt_type
; /* USB audio format type (1-3) */
170 unsigned int packs_per_ms
; /* packets per millisecond (for playback) */
172 unsigned int running
: 1; /* running status */
174 unsigned int hwptr_done
; /* processed frame position in the buffer */
175 unsigned int transfer_done
; /* processed frames since last period update */
176 unsigned long active_mask
; /* bitmask of active urbs */
177 unsigned long unlink_mask
; /* bitmask of unlinked urbs */
179 unsigned int nurbs
; /* # urbs */
180 struct snd_urb_ctx dataurb
[MAX_URBS
]; /* data urb table */
181 struct snd_urb_ctx syncurb
[SYNC_URBS
]; /* sync urb table */
182 char *syncbuf
; /* sync buffer for all sync URBs */
183 dma_addr_t sync_dma
; /* DMA address of syncbuf */
185 u64 formats
; /* format bitmasks (all or'ed) */
186 unsigned int num_formats
; /* number of supported audio formats (list) */
187 struct list_head fmt_list
; /* format list */
188 struct snd_pcm_hw_constraint_list rate_list
; /* limited rates */
191 struct snd_urb_ops ops
; /* callbacks (must be filled at init) */
195 struct snd_usb_stream
{
196 struct snd_usb_audio
*chip
;
199 unsigned int fmt_type
; /* USB audio format type (1-3) */
200 struct snd_usb_substream substream
[2];
201 struct list_head list
;
206 * we keep the snd_usb_audio_t instances by ourselves for merging
207 * the all interfaces on the same card as one sound device.
210 static DEFINE_MUTEX(register_mutex
);
211 static struct snd_usb_audio
*usb_chip
[SNDRV_CARDS
];
215 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
216 * this will overflow at approx 524 kHz
218 static inline unsigned get_usb_full_speed_rate(unsigned int rate
)
220 return ((rate
<< 13) + 62) / 125;
224 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
225 * this will overflow at approx 4 MHz
227 static inline unsigned get_usb_high_speed_rate(unsigned int rate
)
229 return ((rate
<< 10) + 62) / 125;
232 /* convert our full speed USB rate into sampling rate in Hz */
233 static inline unsigned get_full_speed_hz(unsigned int usb_rate
)
235 return (usb_rate
* 125 + (1 << 12)) >> 13;
238 /* convert our high speed USB rate into sampling rate in Hz */
239 static inline unsigned get_high_speed_hz(unsigned int usb_rate
)
241 return (usb_rate
* 125 + (1 << 9)) >> 10;
246 * prepare urb for full speed capture sync pipe
248 * fill the length and offset of each urb descriptor.
249 * the fixed 10.14 frequency is passed through the pipe.
251 static int prepare_capture_sync_urb(struct snd_usb_substream
*subs
,
252 struct snd_pcm_runtime
*runtime
,
255 unsigned char *cp
= urb
->transfer_buffer
;
256 struct snd_urb_ctx
*ctx
= urb
->context
;
258 urb
->dev
= ctx
->subs
->dev
; /* we need to set this at each time */
259 urb
->iso_frame_desc
[0].length
= 3;
260 urb
->iso_frame_desc
[0].offset
= 0;
261 cp
[0] = subs
->freqn
>> 2;
262 cp
[1] = subs
->freqn
>> 10;
263 cp
[2] = subs
->freqn
>> 18;
268 * prepare urb for high speed capture sync pipe
270 * fill the length and offset of each urb descriptor.
271 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
273 static int prepare_capture_sync_urb_hs(struct snd_usb_substream
*subs
,
274 struct snd_pcm_runtime
*runtime
,
277 unsigned char *cp
= urb
->transfer_buffer
;
278 struct snd_urb_ctx
*ctx
= urb
->context
;
280 urb
->dev
= ctx
->subs
->dev
; /* we need to set this at each time */
281 urb
->iso_frame_desc
[0].length
= 4;
282 urb
->iso_frame_desc
[0].offset
= 0;
284 cp
[1] = subs
->freqn
>> 8;
285 cp
[2] = subs
->freqn
>> 16;
286 cp
[3] = subs
->freqn
>> 24;
291 * process after capture sync complete
294 static int retire_capture_sync_urb(struct snd_usb_substream
*subs
,
295 struct snd_pcm_runtime
*runtime
,
302 * prepare urb for capture data pipe
304 * fill the offset and length of each descriptor.
306 * we use a temporary buffer to write the captured data.
307 * since the length of written data is determined by host, we cannot
308 * write onto the pcm buffer directly... the data is thus copied
309 * later at complete callback to the global buffer.
311 static int prepare_capture_urb(struct snd_usb_substream
*subs
,
312 struct snd_pcm_runtime
*runtime
,
316 struct snd_urb_ctx
*ctx
= urb
->context
;
319 urb
->dev
= ctx
->subs
->dev
; /* we need to set this at each time */
320 for (i
= 0; i
< ctx
->packets
; i
++) {
321 urb
->iso_frame_desc
[i
].offset
= offs
;
322 urb
->iso_frame_desc
[i
].length
= subs
->curpacksize
;
323 offs
+= subs
->curpacksize
;
325 urb
->transfer_buffer_length
= offs
;
326 urb
->number_of_packets
= ctx
->packets
;
331 * process after capture complete
333 * copy the data from each desctiptor to the pcm buffer, and
334 * update the current position.
336 static int retire_capture_urb(struct snd_usb_substream
*subs
,
337 struct snd_pcm_runtime
*runtime
,
343 unsigned int stride
, len
, oldptr
;
344 int period_elapsed
= 0;
346 stride
= runtime
->frame_bits
>> 3;
348 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
349 cp
= (unsigned char *)urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
350 if (urb
->iso_frame_desc
[i
].status
) {
351 snd_printd(KERN_ERR
"frame %d active: %d\n", i
, urb
->iso_frame_desc
[i
].status
);
354 len
= urb
->iso_frame_desc
[i
].actual_length
/ stride
;
357 /* update the current pointer */
358 spin_lock_irqsave(&subs
->lock
, flags
);
359 oldptr
= subs
->hwptr_done
;
360 subs
->hwptr_done
+= len
;
361 if (subs
->hwptr_done
>= runtime
->buffer_size
)
362 subs
->hwptr_done
-= runtime
->buffer_size
;
363 subs
->transfer_done
+= len
;
364 if (subs
->transfer_done
>= runtime
->period_size
) {
365 subs
->transfer_done
-= runtime
->period_size
;
368 spin_unlock_irqrestore(&subs
->lock
, flags
);
369 /* copy a data chunk */
370 if (oldptr
+ len
> runtime
->buffer_size
) {
371 unsigned int cnt
= runtime
->buffer_size
- oldptr
;
372 unsigned int blen
= cnt
* stride
;
373 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
, blen
);
374 memcpy(runtime
->dma_area
, cp
+ blen
, len
* stride
- blen
);
376 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
, len
* stride
);
380 snd_pcm_period_elapsed(subs
->pcm_substream
);
385 * Process after capture complete when paused. Nothing to do.
387 static int retire_paused_capture_urb(struct snd_usb_substream
*subs
,
388 struct snd_pcm_runtime
*runtime
,
396 * prepare urb for full speed playback sync pipe
398 * set up the offset and length to receive the current frequency.
401 static int prepare_playback_sync_urb(struct snd_usb_substream
*subs
,
402 struct snd_pcm_runtime
*runtime
,
405 struct snd_urb_ctx
*ctx
= urb
->context
;
407 urb
->dev
= ctx
->subs
->dev
; /* we need to set this at each time */
408 urb
->iso_frame_desc
[0].length
= 3;
409 urb
->iso_frame_desc
[0].offset
= 0;
414 * prepare urb for high speed playback sync pipe
416 * set up the offset and length to receive the current frequency.
419 static int prepare_playback_sync_urb_hs(struct snd_usb_substream
*subs
,
420 struct snd_pcm_runtime
*runtime
,
423 struct snd_urb_ctx
*ctx
= urb
->context
;
425 urb
->dev
= ctx
->subs
->dev
; /* we need to set this at each time */
426 urb
->iso_frame_desc
[0].length
= 4;
427 urb
->iso_frame_desc
[0].offset
= 0;
432 * process after full speed playback sync complete
434 * retrieve the current 10.14 frequency from pipe, and set it.
435 * the value is referred in prepare_playback_urb().
437 static int retire_playback_sync_urb(struct snd_usb_substream
*subs
,
438 struct snd_pcm_runtime
*runtime
,
444 if (urb
->iso_frame_desc
[0].status
== 0 &&
445 urb
->iso_frame_desc
[0].actual_length
== 3) {
446 f
= combine_triple((u8
*)urb
->transfer_buffer
) << 2;
447 if (f
>= subs
->freqn
- subs
->freqn
/ 8 && f
<= subs
->freqmax
) {
448 spin_lock_irqsave(&subs
->lock
, flags
);
450 spin_unlock_irqrestore(&subs
->lock
, flags
);
458 * process after high speed playback sync complete
460 * retrieve the current 12.13 frequency from pipe, and set it.
461 * the value is referred in prepare_playback_urb().
463 static int retire_playback_sync_urb_hs(struct snd_usb_substream
*subs
,
464 struct snd_pcm_runtime
*runtime
,
470 if (urb
->iso_frame_desc
[0].status
== 0 &&
471 urb
->iso_frame_desc
[0].actual_length
== 4) {
472 f
= combine_quad((u8
*)urb
->transfer_buffer
) & 0x0fffffff;
473 if (f
>= subs
->freqn
- subs
->freqn
/ 8 && f
<= subs
->freqmax
) {
474 spin_lock_irqsave(&subs
->lock
, flags
);
476 spin_unlock_irqrestore(&subs
->lock
, flags
);
484 * process after E-Mu 0202/0404 high speed playback sync complete
486 * These devices return the number of samples per packet instead of the number
487 * of samples per microframe.
489 static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream
*subs
,
490 struct snd_pcm_runtime
*runtime
,
496 if (urb
->iso_frame_desc
[0].status
== 0 &&
497 urb
->iso_frame_desc
[0].actual_length
== 4) {
498 f
= combine_quad((u8
*)urb
->transfer_buffer
) & 0x0fffffff;
499 f
>>= subs
->datainterval
;
500 if (f
>= subs
->freqn
- subs
->freqn
/ 8 && f
<= subs
->freqmax
) {
501 spin_lock_irqsave(&subs
->lock
, flags
);
503 spin_unlock_irqrestore(&subs
->lock
, flags
);
510 /* determine the number of frames in the next packet */
511 static int snd_usb_audio_next_packet_size(struct snd_usb_substream
*subs
)
514 return subs
->maxframesize
;
516 subs
->phase
= (subs
->phase
& 0xffff)
517 + (subs
->freqm
<< subs
->datainterval
);
518 return min(subs
->phase
>> 16, subs
->maxframesize
);
523 * Prepare urb for streaming before playback starts or when paused.
525 * We don't have any data, so we send a frame of silence.
527 static int prepare_nodata_playback_urb(struct snd_usb_substream
*subs
,
528 struct snd_pcm_runtime
*runtime
,
531 unsigned int i
, offs
, counts
;
532 struct snd_urb_ctx
*ctx
= urb
->context
;
533 int stride
= runtime
->frame_bits
>> 3;
536 urb
->dev
= ctx
->subs
->dev
;
537 urb
->number_of_packets
= subs
->packs_per_ms
;
538 for (i
= 0; i
< subs
->packs_per_ms
; ++i
) {
539 counts
= snd_usb_audio_next_packet_size(subs
);
540 urb
->iso_frame_desc
[i
].offset
= offs
* stride
;
541 urb
->iso_frame_desc
[i
].length
= counts
* stride
;
544 urb
->transfer_buffer_length
= offs
* stride
;
545 memset(urb
->transfer_buffer
,
546 subs
->cur_audiofmt
->format
== SNDRV_PCM_FORMAT_U8
? 0x80 : 0,
552 * prepare urb for playback data pipe
554 * Since a URB can handle only a single linear buffer, we must use double
555 * buffering when the data to be transferred overflows the buffer boundary.
556 * To avoid inconsistencies when updating hwptr_done, we use double buffering
559 static int prepare_playback_urb(struct snd_usb_substream
*subs
,
560 struct snd_pcm_runtime
*runtime
,
566 int period_elapsed
= 0;
567 struct snd_urb_ctx
*ctx
= urb
->context
;
569 stride
= runtime
->frame_bits
>> 3;
572 urb
->dev
= ctx
->subs
->dev
; /* we need to set this at each time */
573 urb
->number_of_packets
= 0;
574 spin_lock_irqsave(&subs
->lock
, flags
);
575 for (i
= 0; i
< ctx
->packets
; i
++) {
576 counts
= snd_usb_audio_next_packet_size(subs
);
577 /* set up descriptor */
578 urb
->iso_frame_desc
[i
].offset
= offs
* stride
;
579 urb
->iso_frame_desc
[i
].length
= counts
* stride
;
581 urb
->number_of_packets
++;
582 subs
->transfer_done
+= counts
;
583 if (subs
->transfer_done
>= runtime
->period_size
) {
584 subs
->transfer_done
-= runtime
->period_size
;
586 if (subs
->fmt_type
== USB_FORMAT_TYPE_II
) {
587 if (subs
->transfer_done
> 0) {
588 /* FIXME: fill-max mode is not
590 offs
-= subs
->transfer_done
;
591 counts
-= subs
->transfer_done
;
592 urb
->iso_frame_desc
[i
].length
=
594 subs
->transfer_done
= 0;
597 if (i
< ctx
->packets
) {
598 /* add a transfer delimiter */
599 urb
->iso_frame_desc
[i
].offset
=
601 urb
->iso_frame_desc
[i
].length
= 0;
602 urb
->number_of_packets
++;
607 /* finish at the frame boundary at/after the period boundary */
608 if (period_elapsed
&&
609 (i
& (subs
->packs_per_ms
- 1)) == subs
->packs_per_ms
- 1)
612 if (subs
->hwptr_done
+ offs
> runtime
->buffer_size
) {
613 /* err, the transferred area goes over buffer boundary. */
614 unsigned int len
= runtime
->buffer_size
- subs
->hwptr_done
;
615 memcpy(urb
->transfer_buffer
,
616 runtime
->dma_area
+ subs
->hwptr_done
* stride
,
618 memcpy(urb
->transfer_buffer
+ len
* stride
,
620 (offs
- len
) * stride
);
622 memcpy(urb
->transfer_buffer
,
623 runtime
->dma_area
+ subs
->hwptr_done
* stride
,
626 subs
->hwptr_done
+= offs
;
627 if (subs
->hwptr_done
>= runtime
->buffer_size
)
628 subs
->hwptr_done
-= runtime
->buffer_size
;
629 spin_unlock_irqrestore(&subs
->lock
, flags
);
630 urb
->transfer_buffer_length
= offs
* stride
;
632 snd_pcm_period_elapsed(subs
->pcm_substream
);
637 * process after playback data complete
640 static int retire_playback_urb(struct snd_usb_substream
*subs
,
641 struct snd_pcm_runtime
*runtime
,
650 static struct snd_urb_ops audio_urb_ops
[2] = {
652 .prepare
= prepare_nodata_playback_urb
,
653 .retire
= retire_playback_urb
,
654 .prepare_sync
= prepare_playback_sync_urb
,
655 .retire_sync
= retire_playback_sync_urb
,
658 .prepare
= prepare_capture_urb
,
659 .retire
= retire_capture_urb
,
660 .prepare_sync
= prepare_capture_sync_urb
,
661 .retire_sync
= retire_capture_sync_urb
,
665 static struct snd_urb_ops audio_urb_ops_high_speed
[2] = {
667 .prepare
= prepare_nodata_playback_urb
,
668 .retire
= retire_playback_urb
,
669 .prepare_sync
= prepare_playback_sync_urb_hs
,
670 .retire_sync
= retire_playback_sync_urb_hs
,
673 .prepare
= prepare_capture_urb
,
674 .retire
= retire_capture_urb
,
675 .prepare_sync
= prepare_capture_sync_urb_hs
,
676 .retire_sync
= retire_capture_sync_urb
,
681 * complete callback from data urb
683 static void snd_complete_urb(struct urb
*urb
)
685 struct snd_urb_ctx
*ctx
= urb
->context
;
686 struct snd_usb_substream
*subs
= ctx
->subs
;
687 struct snd_pcm_substream
*substream
= ctx
->subs
->pcm_substream
;
690 if ((subs
->running
&& subs
->ops
.retire(subs
, substream
->runtime
, urb
)) ||
691 !subs
->running
|| /* can be stopped during retire callback */
692 (err
= subs
->ops
.prepare(subs
, substream
->runtime
, urb
)) < 0 ||
693 (err
= usb_submit_urb(urb
, GFP_ATOMIC
)) < 0) {
694 clear_bit(ctx
->index
, &subs
->active_mask
);
696 snd_printd(KERN_ERR
"cannot submit urb (err = %d)\n", err
);
697 snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
704 * complete callback from sync urb
706 static void snd_complete_sync_urb(struct urb
*urb
)
708 struct snd_urb_ctx
*ctx
= urb
->context
;
709 struct snd_usb_substream
*subs
= ctx
->subs
;
710 struct snd_pcm_substream
*substream
= ctx
->subs
->pcm_substream
;
713 if ((subs
->running
&& subs
->ops
.retire_sync(subs
, substream
->runtime
, urb
)) ||
714 !subs
->running
|| /* can be stopped during retire callback */
715 (err
= subs
->ops
.prepare_sync(subs
, substream
->runtime
, urb
)) < 0 ||
716 (err
= usb_submit_urb(urb
, GFP_ATOMIC
)) < 0) {
717 clear_bit(ctx
->index
+ 16, &subs
->active_mask
);
719 snd_printd(KERN_ERR
"cannot submit sync urb (err = %d)\n", err
);
720 snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
726 /* get the physical page pointer at the given offset */
727 static struct page
*snd_pcm_get_vmalloc_page(struct snd_pcm_substream
*subs
,
728 unsigned long offset
)
730 void *pageptr
= subs
->runtime
->dma_area
+ offset
;
731 return vmalloc_to_page(pageptr
);
734 /* allocate virtual buffer; may be called more than once */
735 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream
*subs
, size_t size
)
737 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
738 if (runtime
->dma_area
) {
739 if (runtime
->dma_bytes
>= size
)
740 return 0; /* already large enough */
741 vfree(runtime
->dma_area
);
743 runtime
->dma_area
= vmalloc(size
);
744 if (!runtime
->dma_area
)
746 runtime
->dma_bytes
= size
;
750 /* free virtual buffer; may be called more than once */
751 static int snd_pcm_free_vmalloc_buffer(struct snd_pcm_substream
*subs
)
753 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
755 vfree(runtime
->dma_area
);
756 runtime
->dma_area
= NULL
;
762 * unlink active urbs.
764 static int deactivate_urbs(struct snd_usb_substream
*subs
, int force
, int can_sleep
)
771 if (!force
&& subs
->stream
->chip
->shutdown
) /* to be sure... */
774 async
= !can_sleep
&& async_unlink
;
776 if (!async
&& in_interrupt())
779 for (i
= 0; i
< subs
->nurbs
; i
++) {
780 if (test_bit(i
, &subs
->active_mask
)) {
781 if (!test_and_set_bit(i
, &subs
->unlink_mask
)) {
782 struct urb
*u
= subs
->dataurb
[i
].urb
;
790 if (subs
->syncpipe
) {
791 for (i
= 0; i
< SYNC_URBS
; i
++) {
792 if (test_bit(i
+16, &subs
->active_mask
)) {
793 if (!test_and_set_bit(i
+16, &subs
->unlink_mask
)) {
794 struct urb
*u
= subs
->syncurb
[i
].urb
;
807 static const char *usb_error_string(int err
)
813 return "endpoint not enabled";
815 return "endpoint stalled";
817 return "not enough bandwidth";
819 return "device disabled";
821 return "device suspended";
826 return "internal error";
828 return "unknown error";
833 * set up and start data/sync urbs
835 static int start_urbs(struct snd_usb_substream
*subs
, struct snd_pcm_runtime
*runtime
)
840 if (subs
->stream
->chip
->shutdown
)
843 for (i
= 0; i
< subs
->nurbs
; i
++) {
844 snd_assert(subs
->dataurb
[i
].urb
, return -EINVAL
);
845 if (subs
->ops
.prepare(subs
, runtime
, subs
->dataurb
[i
].urb
) < 0) {
846 snd_printk(KERN_ERR
"cannot prepare datapipe for urb %d\n", i
);
850 if (subs
->syncpipe
) {
851 for (i
= 0; i
< SYNC_URBS
; i
++) {
852 snd_assert(subs
->syncurb
[i
].urb
, return -EINVAL
);
853 if (subs
->ops
.prepare_sync(subs
, runtime
, subs
->syncurb
[i
].urb
) < 0) {
854 snd_printk(KERN_ERR
"cannot prepare syncpipe for urb %d\n", i
);
860 subs
->active_mask
= 0;
861 subs
->unlink_mask
= 0;
863 for (i
= 0; i
< subs
->nurbs
; i
++) {
864 err
= usb_submit_urb(subs
->dataurb
[i
].urb
, GFP_ATOMIC
);
866 snd_printk(KERN_ERR
"cannot submit datapipe "
867 "for urb %d, error %d: %s\n",
868 i
, err
, usb_error_string(err
));
871 set_bit(i
, &subs
->active_mask
);
873 if (subs
->syncpipe
) {
874 for (i
= 0; i
< SYNC_URBS
; i
++) {
875 err
= usb_submit_urb(subs
->syncurb
[i
].urb
, GFP_ATOMIC
);
877 snd_printk(KERN_ERR
"cannot submit syncpipe "
878 "for urb %d, error %d: %s\n",
879 i
, err
, usb_error_string(err
));
882 set_bit(i
+ 16, &subs
->active_mask
);
888 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
889 deactivate_urbs(subs
, 0, 0);
895 * wait until all urbs are processed.
897 static int wait_clear_urbs(struct snd_usb_substream
*subs
)
899 unsigned long end_time
= jiffies
+ msecs_to_jiffies(1000);
905 for (i
= 0; i
< subs
->nurbs
; i
++) {
906 if (test_bit(i
, &subs
->active_mask
))
909 if (subs
->syncpipe
) {
910 for (i
= 0; i
< SYNC_URBS
; i
++) {
911 if (test_bit(i
+ 16, &subs
->active_mask
))
917 schedule_timeout_uninterruptible(1);
918 } while (time_before(jiffies
, end_time
));
920 snd_printk(KERN_ERR
"timeout: still %d active urbs..\n", alive
);
926 * return the current pcm pointer. just return the hwptr_done value.
928 static snd_pcm_uframes_t
snd_usb_pcm_pointer(struct snd_pcm_substream
*substream
)
930 struct snd_usb_substream
*subs
;
931 snd_pcm_uframes_t hwptr_done
;
933 subs
= (struct snd_usb_substream
*)substream
->runtime
->private_data
;
934 spin_lock(&subs
->lock
);
935 hwptr_done
= subs
->hwptr_done
;
936 spin_unlock(&subs
->lock
);
942 * start/stop playback substream
944 static int snd_usb_pcm_playback_trigger(struct snd_pcm_substream
*substream
,
947 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
950 case SNDRV_PCM_TRIGGER_START
:
951 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
952 subs
->ops
.prepare
= prepare_playback_urb
;
954 case SNDRV_PCM_TRIGGER_STOP
:
955 return deactivate_urbs(subs
, 0, 0);
956 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
957 subs
->ops
.prepare
= prepare_nodata_playback_urb
;
965 * start/stop capture substream
967 static int snd_usb_pcm_capture_trigger(struct snd_pcm_substream
*substream
,
970 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
973 case SNDRV_PCM_TRIGGER_START
:
974 subs
->ops
.retire
= retire_capture_urb
;
975 return start_urbs(subs
, substream
->runtime
);
976 case SNDRV_PCM_TRIGGER_STOP
:
977 return deactivate_urbs(subs
, 0, 0);
978 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
979 subs
->ops
.retire
= retire_paused_capture_urb
;
981 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
982 subs
->ops
.retire
= retire_capture_urb
;
993 static void release_urb_ctx(struct snd_urb_ctx
*u
)
997 usb_buffer_free(u
->subs
->dev
, u
->buffer_size
,
998 u
->urb
->transfer_buffer
,
999 u
->urb
->transfer_dma
);
1000 usb_free_urb(u
->urb
);
1006 * release a substream
1008 static void release_substream_urbs(struct snd_usb_substream
*subs
, int force
)
1012 /* stop urbs (to be sure) */
1013 deactivate_urbs(subs
, force
, 1);
1014 wait_clear_urbs(subs
);
1016 for (i
= 0; i
< MAX_URBS
; i
++)
1017 release_urb_ctx(&subs
->dataurb
[i
]);
1018 for (i
= 0; i
< SYNC_URBS
; i
++)
1019 release_urb_ctx(&subs
->syncurb
[i
]);
1020 usb_buffer_free(subs
->dev
, SYNC_URBS
* 4,
1021 subs
->syncbuf
, subs
->sync_dma
);
1022 subs
->syncbuf
= NULL
;
1027 * initialize a substream for plaback/capture
1029 static int init_substream_urbs(struct snd_usb_substream
*subs
, unsigned int period_bytes
,
1030 unsigned int rate
, unsigned int frame_bits
)
1032 unsigned int maxsize
, n
, i
;
1033 int is_playback
= subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
;
1034 unsigned int npacks
[MAX_URBS
], urb_packs
, total_packs
, packs_per_ms
;
1036 /* calculate the frequency in 16.16 format */
1037 if (snd_usb_get_speed(subs
->dev
) == USB_SPEED_FULL
)
1038 subs
->freqn
= get_usb_full_speed_rate(rate
);
1040 subs
->freqn
= get_usb_high_speed_rate(rate
);
1041 subs
->freqm
= subs
->freqn
;
1042 /* calculate max. frequency */
1043 if (subs
->maxpacksize
) {
1044 /* whatever fits into a max. size packet */
1045 maxsize
= subs
->maxpacksize
;
1046 subs
->freqmax
= (maxsize
/ (frame_bits
>> 3))
1047 << (16 - subs
->datainterval
);
1049 /* no max. packet size: just take 25% higher than nominal */
1050 subs
->freqmax
= subs
->freqn
+ (subs
->freqn
>> 2);
1051 maxsize
= ((subs
->freqmax
+ 0xffff) * (frame_bits
>> 3))
1052 >> (16 - subs
->datainterval
);
1057 subs
->curpacksize
= subs
->maxpacksize
;
1059 subs
->curpacksize
= maxsize
;
1061 if (snd_usb_get_speed(subs
->dev
) == USB_SPEED_HIGH
)
1062 packs_per_ms
= 8 >> subs
->datainterval
;
1065 subs
->packs_per_ms
= packs_per_ms
;
1068 urb_packs
= nrpacks
;
1069 urb_packs
= max(urb_packs
, (unsigned int)MIN_PACKS_URB
);
1070 urb_packs
= min(urb_packs
, (unsigned int)MAX_PACKS
);
1073 urb_packs
*= packs_per_ms
;
1075 /* decide how many packets to be used */
1077 unsigned int minsize
;
1078 /* determine how small a packet can be */
1079 minsize
= (subs
->freqn
>> (16 - subs
->datainterval
))
1080 * (frame_bits
>> 3);
1081 /* with sync from device, assume it can be 12% lower */
1083 minsize
-= minsize
>> 3;
1084 minsize
= max(minsize
, 1u);
1085 total_packs
= (period_bytes
+ minsize
- 1) / minsize
;
1086 /* round up to multiple of packs_per_ms */
1087 total_packs
= (total_packs
+ packs_per_ms
- 1)
1088 & ~(packs_per_ms
- 1);
1089 /* we need at least two URBs for queueing */
1090 if (total_packs
< 2 * MIN_PACKS_URB
* packs_per_ms
)
1091 total_packs
= 2 * MIN_PACKS_URB
* packs_per_ms
;
1093 total_packs
= MAX_URBS
* urb_packs
;
1095 subs
->nurbs
= (total_packs
+ urb_packs
- 1) / urb_packs
;
1096 if (subs
->nurbs
> MAX_URBS
) {
1098 subs
->nurbs
= MAX_URBS
;
1099 total_packs
= MAX_URBS
* urb_packs
;
1102 for (i
= 0; i
< subs
->nurbs
; i
++) {
1103 npacks
[i
] = n
> urb_packs
? urb_packs
: n
;
1106 if (subs
->nurbs
<= 1) {
1107 /* too little - we need at least two packets
1108 * to ensure contiguous playback/capture
1111 npacks
[0] = (total_packs
+ 1) / 2;
1112 npacks
[1] = total_packs
- npacks
[0];
1113 } else if (npacks
[subs
->nurbs
-1] < MIN_PACKS_URB
* packs_per_ms
) {
1114 /* the last packet is too small.. */
1115 if (subs
->nurbs
> 2) {
1116 /* merge to the first one */
1117 npacks
[0] += npacks
[subs
->nurbs
- 1];
1122 npacks
[0] = (total_packs
+ 1) / 2;
1123 npacks
[1] = total_packs
- npacks
[0];
1127 /* allocate and initialize data urbs */
1128 for (i
= 0; i
< subs
->nurbs
; i
++) {
1129 struct snd_urb_ctx
*u
= &subs
->dataurb
[i
];
1132 u
->packets
= npacks
[i
];
1133 u
->buffer_size
= maxsize
* u
->packets
;
1134 if (subs
->fmt_type
== USB_FORMAT_TYPE_II
)
1135 u
->packets
++; /* for transfer delimiter */
1136 u
->urb
= usb_alloc_urb(u
->packets
, GFP_KERNEL
);
1139 u
->urb
->transfer_buffer
=
1140 usb_buffer_alloc(subs
->dev
, u
->buffer_size
, GFP_KERNEL
,
1141 &u
->urb
->transfer_dma
);
1142 if (!u
->urb
->transfer_buffer
)
1144 u
->urb
->pipe
= subs
->datapipe
;
1145 u
->urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
1146 u
->urb
->interval
= 1 << subs
->datainterval
;
1147 u
->urb
->context
= u
;
1148 u
->urb
->complete
= snd_complete_urb
;
1151 if (subs
->syncpipe
) {
1152 /* allocate and initialize sync urbs */
1153 subs
->syncbuf
= usb_buffer_alloc(subs
->dev
, SYNC_URBS
* 4,
1154 GFP_KERNEL
, &subs
->sync_dma
);
1157 for (i
= 0; i
< SYNC_URBS
; i
++) {
1158 struct snd_urb_ctx
*u
= &subs
->syncurb
[i
];
1162 u
->urb
= usb_alloc_urb(1, GFP_KERNEL
);
1165 u
->urb
->transfer_buffer
= subs
->syncbuf
+ i
* 4;
1166 u
->urb
->transfer_dma
= subs
->sync_dma
+ i
* 4;
1167 u
->urb
->transfer_buffer_length
= 4;
1168 u
->urb
->pipe
= subs
->syncpipe
;
1169 u
->urb
->transfer_flags
= URB_ISO_ASAP
|
1170 URB_NO_TRANSFER_DMA_MAP
;
1171 u
->urb
->number_of_packets
= 1;
1172 u
->urb
->interval
= 1 << subs
->syncinterval
;
1173 u
->urb
->context
= u
;
1174 u
->urb
->complete
= snd_complete_sync_urb
;
1180 release_substream_urbs(subs
, 0);
1186 * find a matching audio format
1188 static struct audioformat
*find_format(struct snd_usb_substream
*subs
, unsigned int format
,
1189 unsigned int rate
, unsigned int channels
)
1191 struct list_head
*p
;
1192 struct audioformat
*found
= NULL
;
1193 int cur_attr
= 0, attr
;
1195 list_for_each(p
, &subs
->fmt_list
) {
1196 struct audioformat
*fp
;
1197 fp
= list_entry(p
, struct audioformat
, list
);
1198 if (fp
->format
!= format
|| fp
->channels
!= channels
)
1200 if (rate
< fp
->rate_min
|| rate
> fp
->rate_max
)
1202 if (! (fp
->rates
& SNDRV_PCM_RATE_CONTINUOUS
)) {
1204 for (i
= 0; i
< fp
->nr_rates
; i
++)
1205 if (fp
->rate_table
[i
] == rate
)
1207 if (i
>= fp
->nr_rates
)
1210 attr
= fp
->ep_attr
& EP_ATTR_MASK
;
1216 /* avoid async out and adaptive in if the other method
1217 * supports the same format.
1218 * this is a workaround for the case like
1219 * M-audio audiophile USB.
1221 if (attr
!= cur_attr
) {
1222 if ((attr
== EP_ATTR_ASYNC
&&
1223 subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
) ||
1224 (attr
== EP_ATTR_ADAPTIVE
&&
1225 subs
->direction
== SNDRV_PCM_STREAM_CAPTURE
))
1227 if ((cur_attr
== EP_ATTR_ASYNC
&&
1228 subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
) ||
1229 (cur_attr
== EP_ATTR_ADAPTIVE
&&
1230 subs
->direction
== SNDRV_PCM_STREAM_CAPTURE
)) {
1236 /* find the format with the largest max. packet size */
1237 if (fp
->maxpacksize
> found
->maxpacksize
) {
1247 * initialize the picth control and sample rate
1249 static int init_usb_pitch(struct usb_device
*dev
, int iface
,
1250 struct usb_host_interface
*alts
,
1251 struct audioformat
*fmt
)
1254 unsigned char data
[1];
1257 ep
= get_endpoint(alts
, 0)->bEndpointAddress
;
1258 /* if endpoint has pitch control, enable it */
1259 if (fmt
->attributes
& EP_CS_ATTR_PITCH_CONTROL
) {
1261 if ((err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
,
1262 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
1263 PITCH_CONTROL
<< 8, ep
, data
, 1, 1000)) < 0) {
1264 snd_printk(KERN_ERR
"%d:%d:%d: cannot set enable PITCH\n",
1265 dev
->devnum
, iface
, ep
);
1272 static int init_usb_sample_rate(struct usb_device
*dev
, int iface
,
1273 struct usb_host_interface
*alts
,
1274 struct audioformat
*fmt
, int rate
)
1277 unsigned char data
[3];
1280 ep
= get_endpoint(alts
, 0)->bEndpointAddress
;
1281 /* if endpoint has sampling rate control, set it */
1282 if (fmt
->attributes
& EP_CS_ATTR_SAMPLE_RATE
) {
1285 data
[1] = rate
>> 8;
1286 data
[2] = rate
>> 16;
1287 if ((err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
,
1288 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
1289 SAMPLING_FREQ_CONTROL
<< 8, ep
, data
, 3, 1000)) < 0) {
1290 snd_printk(KERN_ERR
"%d:%d:%d: cannot set freq %d to ep 0x%x\n",
1291 dev
->devnum
, iface
, fmt
->altsetting
, rate
, ep
);
1294 if ((err
= snd_usb_ctl_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_CUR
,
1295 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_IN
,
1296 SAMPLING_FREQ_CONTROL
<< 8, ep
, data
, 3, 1000)) < 0) {
1297 snd_printk(KERN_WARNING
"%d:%d:%d: cannot get freq at ep 0x%x\n",
1298 dev
->devnum
, iface
, fmt
->altsetting
, ep
);
1299 return 0; /* some devices don't support reading */
1301 crate
= data
[0] | (data
[1] << 8) | (data
[2] << 16);
1302 if (crate
!= rate
) {
1303 snd_printd(KERN_WARNING
"current rate %d is different from the runtime rate %d\n", crate
, rate
);
1304 // runtime->rate = crate;
1311 * find a matching format and set up the interface
1313 static int set_format(struct snd_usb_substream
*subs
, struct audioformat
*fmt
)
1315 struct usb_device
*dev
= subs
->dev
;
1316 struct usb_host_interface
*alts
;
1317 struct usb_interface_descriptor
*altsd
;
1318 struct usb_interface
*iface
;
1319 unsigned int ep
, attr
;
1320 int is_playback
= subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
;
1323 iface
= usb_ifnum_to_if(dev
, fmt
->iface
);
1324 snd_assert(iface
, return -EINVAL
);
1325 alts
= &iface
->altsetting
[fmt
->altset_idx
];
1326 altsd
= get_iface_desc(alts
);
1327 snd_assert(altsd
->bAlternateSetting
== fmt
->altsetting
, return -EINVAL
);
1329 if (fmt
== subs
->cur_audiofmt
)
1332 /* close the old interface */
1333 if (subs
->interface
>= 0 && subs
->interface
!= fmt
->iface
) {
1334 if (usb_set_interface(subs
->dev
, subs
->interface
, 0) < 0) {
1335 snd_printk(KERN_ERR
"%d:%d:%d: return to setting 0 failed\n",
1336 dev
->devnum
, fmt
->iface
, fmt
->altsetting
);
1339 subs
->interface
= -1;
1344 if (subs
->interface
!= fmt
->iface
|| subs
->format
!= fmt
->altset_idx
) {
1345 if (usb_set_interface(dev
, fmt
->iface
, fmt
->altsetting
) < 0) {
1346 snd_printk(KERN_ERR
"%d:%d:%d: usb_set_interface failed\n",
1347 dev
->devnum
, fmt
->iface
, fmt
->altsetting
);
1350 snd_printdd(KERN_INFO
"setting usb interface %d:%d\n", fmt
->iface
, fmt
->altsetting
);
1351 subs
->interface
= fmt
->iface
;
1352 subs
->format
= fmt
->altset_idx
;
1355 /* create a data pipe */
1356 ep
= fmt
->endpoint
& USB_ENDPOINT_NUMBER_MASK
;
1358 subs
->datapipe
= usb_sndisocpipe(dev
, ep
);
1360 subs
->datapipe
= usb_rcvisocpipe(dev
, ep
);
1361 if (snd_usb_get_speed(subs
->dev
) == USB_SPEED_HIGH
&&
1362 get_endpoint(alts
, 0)->bInterval
>= 1 &&
1363 get_endpoint(alts
, 0)->bInterval
<= 4)
1364 subs
->datainterval
= get_endpoint(alts
, 0)->bInterval
- 1;
1366 subs
->datainterval
= 0;
1367 subs
->syncpipe
= subs
->syncinterval
= 0;
1368 subs
->maxpacksize
= fmt
->maxpacksize
;
1371 /* we need a sync pipe in async OUT or adaptive IN mode */
1372 /* check the number of EP, since some devices have broken
1373 * descriptors which fool us. if it has only one EP,
1374 * assume it as adaptive-out or sync-in.
1376 attr
= fmt
->ep_attr
& EP_ATTR_MASK
;
1377 if (((is_playback
&& attr
== EP_ATTR_ASYNC
) ||
1378 (! is_playback
&& attr
== EP_ATTR_ADAPTIVE
)) &&
1379 altsd
->bNumEndpoints
>= 2) {
1380 /* check sync-pipe endpoint */
1381 /* ... and check descriptor size before accessing bSynchAddress
1382 because there is a version of the SB Audigy 2 NX firmware lacking
1383 the audio fields in the endpoint descriptors */
1384 if ((get_endpoint(alts
, 1)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != 0x01 ||
1385 (get_endpoint(alts
, 1)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
1386 get_endpoint(alts
, 1)->bSynchAddress
!= 0)) {
1387 snd_printk(KERN_ERR
"%d:%d:%d : invalid synch pipe\n",
1388 dev
->devnum
, fmt
->iface
, fmt
->altsetting
);
1391 ep
= get_endpoint(alts
, 1)->bEndpointAddress
;
1392 if (get_endpoint(alts
, 0)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
1393 (( is_playback
&& ep
!= (unsigned int)(get_endpoint(alts
, 0)->bSynchAddress
| USB_DIR_IN
)) ||
1394 (!is_playback
&& ep
!= (unsigned int)(get_endpoint(alts
, 0)->bSynchAddress
& ~USB_DIR_IN
)))) {
1395 snd_printk(KERN_ERR
"%d:%d:%d : invalid synch pipe\n",
1396 dev
->devnum
, fmt
->iface
, fmt
->altsetting
);
1399 ep
&= USB_ENDPOINT_NUMBER_MASK
;
1401 subs
->syncpipe
= usb_rcvisocpipe(dev
, ep
);
1403 subs
->syncpipe
= usb_sndisocpipe(dev
, ep
);
1404 if (get_endpoint(alts
, 1)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
1405 get_endpoint(alts
, 1)->bRefresh
>= 1 &&
1406 get_endpoint(alts
, 1)->bRefresh
<= 9)
1407 subs
->syncinterval
= get_endpoint(alts
, 1)->bRefresh
;
1408 else if (snd_usb_get_speed(subs
->dev
) == USB_SPEED_FULL
)
1409 subs
->syncinterval
= 1;
1410 else if (get_endpoint(alts
, 1)->bInterval
>= 1 &&
1411 get_endpoint(alts
, 1)->bInterval
<= 16)
1412 subs
->syncinterval
= get_endpoint(alts
, 1)->bInterval
- 1;
1414 subs
->syncinterval
= 3;
1417 /* always fill max packet size */
1418 if (fmt
->attributes
& EP_CS_ATTR_FILL_MAX
)
1421 if ((err
= init_usb_pitch(dev
, subs
->interface
, alts
, fmt
)) < 0)
1424 subs
->cur_audiofmt
= fmt
;
1427 printk("setting done: format = %d, rate = %d..%d, channels = %d\n",
1428 fmt
->format
, fmt
->rate_min
, fmt
->rate_max
, fmt
->channels
);
1429 printk(" datapipe = 0x%0x, syncpipe = 0x%0x\n",
1430 subs
->datapipe
, subs
->syncpipe
);
1437 * hw_params callback
1439 * allocate a buffer and set the given audio format.
1441 * so far we use a physically linear buffer although packetize transfer
1442 * doesn't need a continuous area.
1443 * if sg buffer is supported on the later version of alsa, we'll follow
1446 static int snd_usb_hw_params(struct snd_pcm_substream
*substream
,
1447 struct snd_pcm_hw_params
*hw_params
)
1449 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
1450 struct audioformat
*fmt
;
1451 unsigned int channels
, rate
, format
;
1454 ret
= snd_pcm_alloc_vmalloc_buffer(substream
,
1455 params_buffer_bytes(hw_params
));
1459 format
= params_format(hw_params
);
1460 rate
= params_rate(hw_params
);
1461 channels
= params_channels(hw_params
);
1462 fmt
= find_format(subs
, format
, rate
, channels
);
1464 snd_printd(KERN_DEBUG
"cannot set format: format = 0x%x, rate = %d, channels = %d\n",
1465 format
, rate
, channels
);
1469 changed
= subs
->cur_audiofmt
!= fmt
||
1470 subs
->period_bytes
!= params_period_bytes(hw_params
) ||
1471 subs
->cur_rate
!= rate
;
1472 if ((ret
= set_format(subs
, fmt
)) < 0)
1475 if (subs
->cur_rate
!= rate
) {
1476 struct usb_host_interface
*alts
;
1477 struct usb_interface
*iface
;
1478 iface
= usb_ifnum_to_if(subs
->dev
, fmt
->iface
);
1479 alts
= &iface
->altsetting
[fmt
->altset_idx
];
1480 ret
= init_usb_sample_rate(subs
->dev
, subs
->interface
, alts
, fmt
, rate
);
1483 subs
->cur_rate
= rate
;
1487 /* format changed */
1488 release_substream_urbs(subs
, 0);
1489 /* influenced: period_bytes, channels, rate, format, */
1490 ret
= init_substream_urbs(subs
, params_period_bytes(hw_params
),
1491 params_rate(hw_params
),
1492 snd_pcm_format_physical_width(params_format(hw_params
)) * params_channels(hw_params
));
1501 * reset the audio format and release the buffer
1503 static int snd_usb_hw_free(struct snd_pcm_substream
*substream
)
1505 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
1507 subs
->cur_audiofmt
= NULL
;
1509 subs
->period_bytes
= 0;
1510 if (!subs
->stream
->chip
->shutdown
)
1511 release_substream_urbs(subs
, 0);
1512 return snd_pcm_free_vmalloc_buffer(substream
);
1518 * only a few subtle things...
1520 static int snd_usb_pcm_prepare(struct snd_pcm_substream
*substream
)
1522 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1523 struct snd_usb_substream
*subs
= runtime
->private_data
;
1525 if (! subs
->cur_audiofmt
) {
1526 snd_printk(KERN_ERR
"usbaudio: no format is specified!\n");
1530 /* some unit conversions in runtime */
1531 subs
->maxframesize
= bytes_to_frames(runtime
, subs
->maxpacksize
);
1532 subs
->curframesize
= bytes_to_frames(runtime
, subs
->curpacksize
);
1534 /* reset the pointer */
1535 subs
->hwptr_done
= 0;
1536 subs
->transfer_done
= 0;
1539 /* clear urbs (to be sure) */
1540 deactivate_urbs(subs
, 0, 1);
1541 wait_clear_urbs(subs
);
1543 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1544 * updates for all URBs would happen at the same time when starting */
1545 if (subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
) {
1546 subs
->ops
.prepare
= prepare_nodata_playback_urb
;
1547 return start_urbs(subs
, runtime
);
1552 static struct snd_pcm_hardware snd_usb_hardware
=
1554 .info
= SNDRV_PCM_INFO_MMAP
|
1555 SNDRV_PCM_INFO_MMAP_VALID
|
1556 SNDRV_PCM_INFO_BATCH
|
1557 SNDRV_PCM_INFO_INTERLEAVED
|
1558 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
1559 SNDRV_PCM_INFO_PAUSE
,
1560 .buffer_bytes_max
= 1024 * 1024,
1561 .period_bytes_min
= 64,
1562 .period_bytes_max
= 512 * 1024,
1564 .periods_max
= 1024,
1571 #ifdef HW_CONST_DEBUG
1572 #define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
1574 #define hwc_debug(fmt, args...) /**/
1577 static int hw_check_valid_format(struct snd_pcm_hw_params
*params
, struct audioformat
*fp
)
1579 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
1580 struct snd_interval
*ct
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
1581 struct snd_mask
*fmts
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1583 /* check the format */
1584 if (!snd_mask_test(fmts
, fp
->format
)) {
1585 hwc_debug(" > check: no supported format %d\n", fp
->format
);
1588 /* check the channels */
1589 if (fp
->channels
< ct
->min
|| fp
->channels
> ct
->max
) {
1590 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp
->channels
, ct
->min
, ct
->max
);
1593 /* check the rate is within the range */
1594 if (fp
->rate_min
> it
->max
|| (fp
->rate_min
== it
->max
&& it
->openmax
)) {
1595 hwc_debug(" > check: rate_min %d > max %d\n", fp
->rate_min
, it
->max
);
1598 if (fp
->rate_max
< it
->min
|| (fp
->rate_max
== it
->min
&& it
->openmin
)) {
1599 hwc_debug(" > check: rate_max %d < min %d\n", fp
->rate_max
, it
->min
);
1605 static int hw_rule_rate(struct snd_pcm_hw_params
*params
,
1606 struct snd_pcm_hw_rule
*rule
)
1608 struct snd_usb_substream
*subs
= rule
->private;
1609 struct list_head
*p
;
1610 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
1611 unsigned int rmin
, rmax
;
1614 hwc_debug("hw_rule_rate: (%d,%d)\n", it
->min
, it
->max
);
1617 list_for_each(p
, &subs
->fmt_list
) {
1618 struct audioformat
*fp
;
1619 fp
= list_entry(p
, struct audioformat
, list
);
1620 if (!hw_check_valid_format(params
, fp
))
1623 if (rmin
> fp
->rate_min
)
1624 rmin
= fp
->rate_min
;
1625 if (rmax
< fp
->rate_max
)
1626 rmax
= fp
->rate_max
;
1628 rmin
= fp
->rate_min
;
1629 rmax
= fp
->rate_max
;
1634 hwc_debug(" --> get empty\n");
1640 if (it
->min
< rmin
) {
1645 if (it
->max
> rmax
) {
1650 if (snd_interval_checkempty(it
)) {
1654 hwc_debug(" --> (%d, %d) (changed = %d)\n", it
->min
, it
->max
, changed
);
1659 static int hw_rule_channels(struct snd_pcm_hw_params
*params
,
1660 struct snd_pcm_hw_rule
*rule
)
1662 struct snd_usb_substream
*subs
= rule
->private;
1663 struct list_head
*p
;
1664 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
1665 unsigned int rmin
, rmax
;
1668 hwc_debug("hw_rule_channels: (%d,%d)\n", it
->min
, it
->max
);
1671 list_for_each(p
, &subs
->fmt_list
) {
1672 struct audioformat
*fp
;
1673 fp
= list_entry(p
, struct audioformat
, list
);
1674 if (!hw_check_valid_format(params
, fp
))
1677 if (rmin
> fp
->channels
)
1678 rmin
= fp
->channels
;
1679 if (rmax
< fp
->channels
)
1680 rmax
= fp
->channels
;
1682 rmin
= fp
->channels
;
1683 rmax
= fp
->channels
;
1688 hwc_debug(" --> get empty\n");
1694 if (it
->min
< rmin
) {
1699 if (it
->max
> rmax
) {
1704 if (snd_interval_checkempty(it
)) {
1708 hwc_debug(" --> (%d, %d) (changed = %d)\n", it
->min
, it
->max
, changed
);
1712 static int hw_rule_format(struct snd_pcm_hw_params
*params
,
1713 struct snd_pcm_hw_rule
*rule
)
1715 struct snd_usb_substream
*subs
= rule
->private;
1716 struct list_head
*p
;
1717 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1722 hwc_debug("hw_rule_format: %x:%x\n", fmt
->bits
[0], fmt
->bits
[1]);
1724 list_for_each(p
, &subs
->fmt_list
) {
1725 struct audioformat
*fp
;
1726 fp
= list_entry(p
, struct audioformat
, list
);
1727 if (!hw_check_valid_format(params
, fp
))
1729 fbits
|= (1ULL << fp
->format
);
1732 oldbits
[0] = fmt
->bits
[0];
1733 oldbits
[1] = fmt
->bits
[1];
1734 fmt
->bits
[0] &= (u32
)fbits
;
1735 fmt
->bits
[1] &= (u32
)(fbits
>> 32);
1736 if (!fmt
->bits
[0] && !fmt
->bits
[1]) {
1737 hwc_debug(" --> get empty\n");
1740 changed
= (oldbits
[0] != fmt
->bits
[0] || oldbits
[1] != fmt
->bits
[1]);
1741 hwc_debug(" --> %x:%x (changed = %d)\n", fmt
->bits
[0], fmt
->bits
[1], changed
);
1748 * check whether the registered audio formats need special hw-constraints
1750 static int check_hw_params_convention(struct snd_usb_substream
*subs
)
1755 u32 cmaster
, rmaster
;
1756 u32 rate_min
= 0, rate_max
= 0;
1757 struct list_head
*p
;
1760 channels
= kcalloc(MAX_MASK
, sizeof(u32
), GFP_KERNEL
);
1761 rates
= kcalloc(MAX_MASK
, sizeof(u32
), GFP_KERNEL
);
1762 if (!channels
|| !rates
) {
1767 list_for_each(p
, &subs
->fmt_list
) {
1768 struct audioformat
*f
;
1769 f
= list_entry(p
, struct audioformat
, list
);
1770 /* unconventional channels? */
1771 if (f
->channels
> 32)
1773 /* continuous rate min/max matches? */
1774 if (f
->rates
& SNDRV_PCM_RATE_CONTINUOUS
) {
1775 if (rate_min
&& f
->rate_min
!= rate_min
)
1777 if (rate_max
&& f
->rate_max
!= rate_max
)
1779 rate_min
= f
->rate_min
;
1780 rate_max
= f
->rate_max
;
1782 /* combination of continuous rates and fixed rates? */
1783 if (rates
[f
->format
] & SNDRV_PCM_RATE_CONTINUOUS
) {
1784 if (f
->rates
!= rates
[f
->format
])
1787 if (f
->rates
& SNDRV_PCM_RATE_CONTINUOUS
) {
1788 if (rates
[f
->format
] && rates
[f
->format
] != f
->rates
)
1791 channels
[f
->format
] |= (1 << f
->channels
);
1792 rates
[f
->format
] |= f
->rates
;
1794 if (f
->rates
& SNDRV_PCM_RATE_KNOT
)
1797 /* check whether channels and rates match for all formats */
1798 cmaster
= rmaster
= 0;
1799 for (i
= 0; i
< MAX_MASK
; i
++) {
1800 if (cmaster
!= channels
[i
] && cmaster
&& channels
[i
])
1802 if (rmaster
!= rates
[i
] && rmaster
&& rates
[i
])
1805 cmaster
= channels
[i
];
1809 /* check whether channels match for all distinct rates */
1810 memset(channels
, 0, MAX_MASK
* sizeof(u32
));
1811 list_for_each(p
, &subs
->fmt_list
) {
1812 struct audioformat
*f
;
1813 f
= list_entry(p
, struct audioformat
, list
);
1814 if (f
->rates
& SNDRV_PCM_RATE_CONTINUOUS
)
1816 for (i
= 0; i
< 32; i
++) {
1817 if (f
->rates
& (1 << i
))
1818 channels
[i
] |= (1 << f
->channels
);
1822 for (i
= 0; i
< 32; i
++) {
1823 if (cmaster
!= channels
[i
] && cmaster
&& channels
[i
])
1826 cmaster
= channels
[i
];
1837 * If the device supports unusual bit rates, does the request meet these?
1839 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime
*runtime
,
1840 struct snd_usb_substream
*subs
)
1842 struct audioformat
*fp
;
1843 int count
= 0, needs_knot
= 0;
1846 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1847 if (fp
->rates
& SNDRV_PCM_RATE_CONTINUOUS
)
1849 count
+= fp
->nr_rates
;
1850 if (fp
->rates
& SNDRV_PCM_RATE_KNOT
)
1856 subs
->rate_list
.count
= count
;
1857 subs
->rate_list
.list
= kmalloc(sizeof(int) * count
, GFP_KERNEL
);
1858 subs
->rate_list
.mask
= 0;
1860 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1862 for (i
= 0; i
< fp
->nr_rates
; i
++)
1863 subs
->rate_list
.list
[count
++] = fp
->rate_table
[i
];
1865 err
= snd_pcm_hw_constraint_list(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1875 * set up the runtime hardware information.
1878 static int setup_hw_info(struct snd_pcm_runtime
*runtime
, struct snd_usb_substream
*subs
)
1880 struct list_head
*p
;
1883 runtime
->hw
.formats
= subs
->formats
;
1885 runtime
->hw
.rate_min
= 0x7fffffff;
1886 runtime
->hw
.rate_max
= 0;
1887 runtime
->hw
.channels_min
= 256;
1888 runtime
->hw
.channels_max
= 0;
1889 runtime
->hw
.rates
= 0;
1890 /* check min/max rates and channels */
1891 list_for_each(p
, &subs
->fmt_list
) {
1892 struct audioformat
*fp
;
1893 fp
= list_entry(p
, struct audioformat
, list
);
1894 runtime
->hw
.rates
|= fp
->rates
;
1895 if (runtime
->hw
.rate_min
> fp
->rate_min
)
1896 runtime
->hw
.rate_min
= fp
->rate_min
;
1897 if (runtime
->hw
.rate_max
< fp
->rate_max
)
1898 runtime
->hw
.rate_max
= fp
->rate_max
;
1899 if (runtime
->hw
.channels_min
> fp
->channels
)
1900 runtime
->hw
.channels_min
= fp
->channels
;
1901 if (runtime
->hw
.channels_max
< fp
->channels
)
1902 runtime
->hw
.channels_max
= fp
->channels
;
1903 if (fp
->fmt_type
== USB_FORMAT_TYPE_II
&& fp
->frame_size
> 0) {
1904 /* FIXME: there might be more than one audio formats... */
1905 runtime
->hw
.period_bytes_min
= runtime
->hw
.period_bytes_max
=
1910 /* set the period time minimum 1ms */
1911 /* FIXME: high-speed mode allows 125us minimum period, but many parts
1912 * in the current code assume the 1ms period.
1914 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1915 1000 * MIN_PACKS_URB
,
1916 /*(nrpacks * MAX_URBS) * 1000*/ UINT_MAX
);
1918 err
= check_hw_params_convention(subs
);
1922 hwc_debug("setting extra hw constraints...\n");
1923 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1925 SNDRV_PCM_HW_PARAM_FORMAT
,
1926 SNDRV_PCM_HW_PARAM_CHANNELS
,
1929 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1930 hw_rule_channels
, subs
,
1931 SNDRV_PCM_HW_PARAM_FORMAT
,
1932 SNDRV_PCM_HW_PARAM_RATE
,
1935 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1936 hw_rule_format
, subs
,
1937 SNDRV_PCM_HW_PARAM_RATE
,
1938 SNDRV_PCM_HW_PARAM_CHANNELS
,
1941 if ((err
= snd_usb_pcm_check_knot(runtime
, subs
)) < 0)
1947 static int snd_usb_pcm_open(struct snd_pcm_substream
*substream
, int direction
)
1949 struct snd_usb_stream
*as
= snd_pcm_substream_chip(substream
);
1950 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1951 struct snd_usb_substream
*subs
= &as
->substream
[direction
];
1953 subs
->interface
= -1;
1955 runtime
->hw
= snd_usb_hardware
;
1956 runtime
->private_data
= subs
;
1957 subs
->pcm_substream
= substream
;
1958 return setup_hw_info(runtime
, subs
);
1961 static int snd_usb_pcm_close(struct snd_pcm_substream
*substream
, int direction
)
1963 struct snd_usb_stream
*as
= snd_pcm_substream_chip(substream
);
1964 struct snd_usb_substream
*subs
= &as
->substream
[direction
];
1966 if (subs
->interface
>= 0) {
1967 usb_set_interface(subs
->dev
, subs
->interface
, 0);
1968 subs
->interface
= -1;
1970 subs
->pcm_substream
= NULL
;
1974 static int snd_usb_playback_open(struct snd_pcm_substream
*substream
)
1976 return snd_usb_pcm_open(substream
, SNDRV_PCM_STREAM_PLAYBACK
);
1979 static int snd_usb_playback_close(struct snd_pcm_substream
*substream
)
1981 return snd_usb_pcm_close(substream
, SNDRV_PCM_STREAM_PLAYBACK
);
1984 static int snd_usb_capture_open(struct snd_pcm_substream
*substream
)
1986 return snd_usb_pcm_open(substream
, SNDRV_PCM_STREAM_CAPTURE
);
1989 static int snd_usb_capture_close(struct snd_pcm_substream
*substream
)
1991 return snd_usb_pcm_close(substream
, SNDRV_PCM_STREAM_CAPTURE
);
1994 static struct snd_pcm_ops snd_usb_playback_ops
= {
1995 .open
= snd_usb_playback_open
,
1996 .close
= snd_usb_playback_close
,
1997 .ioctl
= snd_pcm_lib_ioctl
,
1998 .hw_params
= snd_usb_hw_params
,
1999 .hw_free
= snd_usb_hw_free
,
2000 .prepare
= snd_usb_pcm_prepare
,
2001 .trigger
= snd_usb_pcm_playback_trigger
,
2002 .pointer
= snd_usb_pcm_pointer
,
2003 .page
= snd_pcm_get_vmalloc_page
,
2006 static struct snd_pcm_ops snd_usb_capture_ops
= {
2007 .open
= snd_usb_capture_open
,
2008 .close
= snd_usb_capture_close
,
2009 .ioctl
= snd_pcm_lib_ioctl
,
2010 .hw_params
= snd_usb_hw_params
,
2011 .hw_free
= snd_usb_hw_free
,
2012 .prepare
= snd_usb_pcm_prepare
,
2013 .trigger
= snd_usb_pcm_capture_trigger
,
2014 .pointer
= snd_usb_pcm_pointer
,
2015 .page
= snd_pcm_get_vmalloc_page
,
2025 * combine bytes and get an integer value
2027 unsigned int snd_usb_combine_bytes(unsigned char *bytes
, int size
)
2030 case 1: return *bytes
;
2031 case 2: return combine_word(bytes
);
2032 case 3: return combine_triple(bytes
);
2033 case 4: return combine_quad(bytes
);
2039 * parse descriptor buffer and return the pointer starting the given
2042 void *snd_usb_find_desc(void *descstart
, int desclen
, void *after
, u8 dtype
)
2054 if (p
[1] == dtype
&& (!after
|| (void *)p
> after
)) {
2063 * find a class-specified interface descriptor with the given subtype.
2065 void *snd_usb_find_csint_desc(void *buffer
, int buflen
, void *after
, u8 dsubtype
)
2067 unsigned char *p
= after
;
2069 while ((p
= snd_usb_find_desc(buffer
, buflen
, p
,
2070 USB_DT_CS_INTERFACE
)) != NULL
) {
2071 if (p
[0] >= 3 && p
[2] == dsubtype
)
2078 * Wrapper for usb_control_msg().
2079 * Allocates a temp buffer to prevent dmaing from/to the stack.
2081 int snd_usb_ctl_msg(struct usb_device
*dev
, unsigned int pipe
, __u8 request
,
2082 __u8 requesttype
, __u16 value
, __u16 index
, void *data
,
2083 __u16 size
, int timeout
)
2089 buf
= kmemdup(data
, size
, GFP_KERNEL
);
2093 err
= usb_control_msg(dev
, pipe
, request
, requesttype
,
2094 value
, index
, buf
, size
, timeout
);
2096 memcpy(data
, buf
, size
);
2104 * entry point for linux usb interface
2107 static int usb_audio_probe(struct usb_interface
*intf
,
2108 const struct usb_device_id
*id
);
2109 static void usb_audio_disconnect(struct usb_interface
*intf
);
2112 static int usb_audio_suspend(struct usb_interface
*intf
, pm_message_t message
);
2113 static int usb_audio_resume(struct usb_interface
*intf
);
2115 #define usb_audio_suspend NULL
2116 #define usb_audio_resume NULL
2119 static struct usb_device_id usb_audio_ids
[] = {
2120 #include "usbquirks.h"
2121 { .match_flags
= (USB_DEVICE_ID_MATCH_INT_CLASS
| USB_DEVICE_ID_MATCH_INT_SUBCLASS
),
2122 .bInterfaceClass
= USB_CLASS_AUDIO
,
2123 .bInterfaceSubClass
= USB_SUBCLASS_AUDIO_CONTROL
},
2124 { } /* Terminating entry */
2127 MODULE_DEVICE_TABLE (usb
, usb_audio_ids
);
2129 static struct usb_driver usb_audio_driver
= {
2130 .name
= "snd-usb-audio",
2131 .probe
= usb_audio_probe
,
2132 .disconnect
= usb_audio_disconnect
,
2133 .suspend
= usb_audio_suspend
,
2134 .resume
= usb_audio_resume
,
2135 .id_table
= usb_audio_ids
,
2139 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SND_VERBOSE_PROCFS)
2142 * proc interface for list the supported pcm formats
2144 static void proc_dump_substream_formats(struct snd_usb_substream
*subs
, struct snd_info_buffer
*buffer
)
2146 struct list_head
*p
;
2147 static char *sync_types
[4] = {
2148 "NONE", "ASYNC", "ADAPTIVE", "SYNC"
2151 list_for_each(p
, &subs
->fmt_list
) {
2152 struct audioformat
*fp
;
2153 fp
= list_entry(p
, struct audioformat
, list
);
2154 snd_iprintf(buffer
, " Interface %d\n", fp
->iface
);
2155 snd_iprintf(buffer
, " Altset %d\n", fp
->altsetting
);
2156 snd_iprintf(buffer
, " Format: 0x%x\n", fp
->format
);
2157 snd_iprintf(buffer
, " Channels: %d\n", fp
->channels
);
2158 snd_iprintf(buffer
, " Endpoint: %d %s (%s)\n",
2159 fp
->endpoint
& USB_ENDPOINT_NUMBER_MASK
,
2160 fp
->endpoint
& USB_DIR_IN
? "IN" : "OUT",
2161 sync_types
[(fp
->ep_attr
& EP_ATTR_MASK
) >> 2]);
2162 if (fp
->rates
& SNDRV_PCM_RATE_CONTINUOUS
) {
2163 snd_iprintf(buffer
, " Rates: %d - %d (continuous)\n",
2164 fp
->rate_min
, fp
->rate_max
);
2167 snd_iprintf(buffer
, " Rates: ");
2168 for (i
= 0; i
< fp
->nr_rates
; i
++) {
2170 snd_iprintf(buffer
, ", ");
2171 snd_iprintf(buffer
, "%d", fp
->rate_table
[i
]);
2173 snd_iprintf(buffer
, "\n");
2175 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
2176 // snd_iprintf(buffer, " EP Attribute = 0x%x\n", fp->attributes);
2180 static void proc_dump_substream_status(struct snd_usb_substream
*subs
, struct snd_info_buffer
*buffer
)
2182 if (subs
->running
) {
2184 snd_iprintf(buffer
, " Status: Running\n");
2185 snd_iprintf(buffer
, " Interface = %d\n", subs
->interface
);
2186 snd_iprintf(buffer
, " Altset = %d\n", subs
->format
);
2187 snd_iprintf(buffer
, " URBs = %d [ ", subs
->nurbs
);
2188 for (i
= 0; i
< subs
->nurbs
; i
++)
2189 snd_iprintf(buffer
, "%d ", subs
->dataurb
[i
].packets
);
2190 snd_iprintf(buffer
, "]\n");
2191 snd_iprintf(buffer
, " Packet Size = %d\n", subs
->curpacksize
);
2192 snd_iprintf(buffer
, " Momentary freq = %u Hz (%#x.%04x)\n",
2193 snd_usb_get_speed(subs
->dev
) == USB_SPEED_FULL
2194 ? get_full_speed_hz(subs
->freqm
)
2195 : get_high_speed_hz(subs
->freqm
),
2196 subs
->freqm
>> 16, subs
->freqm
& 0xffff);
2198 snd_iprintf(buffer
, " Status: Stop\n");
2202 static void proc_pcm_format_read(struct snd_info_entry
*entry
, struct snd_info_buffer
*buffer
)
2204 struct snd_usb_stream
*stream
= entry
->private_data
;
2206 snd_iprintf(buffer
, "%s : %s\n", stream
->chip
->card
->longname
, stream
->pcm
->name
);
2208 if (stream
->substream
[SNDRV_PCM_STREAM_PLAYBACK
].num_formats
) {
2209 snd_iprintf(buffer
, "\nPlayback:\n");
2210 proc_dump_substream_status(&stream
->substream
[SNDRV_PCM_STREAM_PLAYBACK
], buffer
);
2211 proc_dump_substream_formats(&stream
->substream
[SNDRV_PCM_STREAM_PLAYBACK
], buffer
);
2213 if (stream
->substream
[SNDRV_PCM_STREAM_CAPTURE
].num_formats
) {
2214 snd_iprintf(buffer
, "\nCapture:\n");
2215 proc_dump_substream_status(&stream
->substream
[SNDRV_PCM_STREAM_CAPTURE
], buffer
);
2216 proc_dump_substream_formats(&stream
->substream
[SNDRV_PCM_STREAM_CAPTURE
], buffer
);
2220 static void proc_pcm_format_add(struct snd_usb_stream
*stream
)
2222 struct snd_info_entry
*entry
;
2224 struct snd_card
*card
= stream
->chip
->card
;
2226 sprintf(name
, "stream%d", stream
->pcm_index
);
2227 if (!snd_card_proc_new(card
, name
, &entry
))
2228 snd_info_set_text_ops(entry
, stream
, proc_pcm_format_read
);
2233 static inline void proc_pcm_format_add(struct snd_usb_stream
*stream
)
2240 * initialize the substream instance.
2243 static void init_substream(struct snd_usb_stream
*as
, int stream
, struct audioformat
*fp
)
2245 struct snd_usb_substream
*subs
= &as
->substream
[stream
];
2247 INIT_LIST_HEAD(&subs
->fmt_list
);
2248 spin_lock_init(&subs
->lock
);
2251 subs
->direction
= stream
;
2252 subs
->dev
= as
->chip
->dev
;
2253 if (snd_usb_get_speed(subs
->dev
) == USB_SPEED_FULL
) {
2254 subs
->ops
= audio_urb_ops
[stream
];
2256 subs
->ops
= audio_urb_ops_high_speed
[stream
];
2257 switch (as
->chip
->usb_id
) {
2258 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
2259 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
2260 subs
->ops
.retire_sync
= retire_playback_sync_urb_hs_emu
;
2264 snd_pcm_set_ops(as
->pcm
, stream
,
2265 stream
== SNDRV_PCM_STREAM_PLAYBACK
?
2266 &snd_usb_playback_ops
: &snd_usb_capture_ops
);
2268 list_add_tail(&fp
->list
, &subs
->fmt_list
);
2269 subs
->formats
|= 1ULL << fp
->format
;
2270 subs
->endpoint
= fp
->endpoint
;
2271 subs
->num_formats
++;
2272 subs
->fmt_type
= fp
->fmt_type
;
2279 static void free_substream(struct snd_usb_substream
*subs
)
2281 struct list_head
*p
, *n
;
2283 if (!subs
->num_formats
)
2284 return; /* not initialized */
2285 list_for_each_safe(p
, n
, &subs
->fmt_list
) {
2286 struct audioformat
*fp
= list_entry(p
, struct audioformat
, list
);
2287 kfree(fp
->rate_table
);
2290 kfree(subs
->rate_list
.list
);
2295 * free a usb stream instance
2297 static void snd_usb_audio_stream_free(struct snd_usb_stream
*stream
)
2299 free_substream(&stream
->substream
[0]);
2300 free_substream(&stream
->substream
[1]);
2301 list_del(&stream
->list
);
2305 static void snd_usb_audio_pcm_free(struct snd_pcm
*pcm
)
2307 struct snd_usb_stream
*stream
= pcm
->private_data
;
2310 snd_usb_audio_stream_free(stream
);
2316 * add this endpoint to the chip instance.
2317 * if a stream with the same endpoint already exists, append to it.
2318 * if not, create a new pcm stream.
2320 static int add_audio_endpoint(struct snd_usb_audio
*chip
, int stream
, struct audioformat
*fp
)
2322 struct list_head
*p
;
2323 struct snd_usb_stream
*as
;
2324 struct snd_usb_substream
*subs
;
2325 struct snd_pcm
*pcm
;
2328 list_for_each(p
, &chip
->pcm_list
) {
2329 as
= list_entry(p
, struct snd_usb_stream
, list
);
2330 if (as
->fmt_type
!= fp
->fmt_type
)
2332 subs
= &as
->substream
[stream
];
2333 if (!subs
->endpoint
)
2335 if (subs
->endpoint
== fp
->endpoint
) {
2336 list_add_tail(&fp
->list
, &subs
->fmt_list
);
2337 subs
->num_formats
++;
2338 subs
->formats
|= 1ULL << fp
->format
;
2342 /* look for an empty stream */
2343 list_for_each(p
, &chip
->pcm_list
) {
2344 as
= list_entry(p
, struct snd_usb_stream
, list
);
2345 if (as
->fmt_type
!= fp
->fmt_type
)
2347 subs
= &as
->substream
[stream
];
2350 err
= snd_pcm_new_stream(as
->pcm
, stream
, 1);
2353 init_substream(as
, stream
, fp
);
2357 /* create a new pcm */
2358 as
= kzalloc(sizeof(*as
), GFP_KERNEL
);
2361 as
->pcm_index
= chip
->pcm_devs
;
2363 as
->fmt_type
= fp
->fmt_type
;
2364 err
= snd_pcm_new(chip
->card
, "USB Audio", chip
->pcm_devs
,
2365 stream
== SNDRV_PCM_STREAM_PLAYBACK
? 1 : 0,
2366 stream
== SNDRV_PCM_STREAM_PLAYBACK
? 0 : 1,
2373 pcm
->private_data
= as
;
2374 pcm
->private_free
= snd_usb_audio_pcm_free
;
2375 pcm
->info_flags
= 0;
2376 if (chip
->pcm_devs
> 0)
2377 sprintf(pcm
->name
, "USB Audio #%d", chip
->pcm_devs
);
2379 strcpy(pcm
->name
, "USB Audio");
2381 init_substream(as
, stream
, fp
);
2383 list_add(&as
->list
, &chip
->pcm_list
);
2386 proc_pcm_format_add(as
);
2393 * check if the device uses big-endian samples
2395 static int is_big_endian_format(struct snd_usb_audio
*chip
, struct audioformat
*fp
)
2397 switch (chip
->usb_id
) {
2398 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
2399 if (fp
->endpoint
& USB_DIR_IN
)
2402 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2403 if (device_setup
[chip
->index
] == 0x00 ||
2404 fp
->altsetting
==1 || fp
->altsetting
==2 || fp
->altsetting
==3)
2411 * parse the audio format type I descriptor
2412 * and returns the corresponding pcm format
2415 * @fp: audioformat record
2416 * @format: the format tag (wFormatTag)
2417 * @fmt: the format type descriptor
2419 static int parse_audio_format_i_type(struct snd_usb_audio
*chip
, struct audioformat
*fp
,
2420 int format
, unsigned char *fmt
)
2423 int sample_width
, sample_bytes
;
2425 /* FIXME: correct endianess and sign? */
2427 sample_width
= fmt
[6];
2428 sample_bytes
= fmt
[5];
2430 case 0: /* some devices don't define this correctly... */
2431 snd_printdd(KERN_INFO
"%d:%u:%d : format type 0 is detected, processed as PCM\n",
2432 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
);
2434 case USB_AUDIO_FORMAT_PCM
:
2435 if (sample_width
> sample_bytes
* 8) {
2436 snd_printk(KERN_INFO
"%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
2437 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
,
2438 sample_width
, sample_bytes
);
2440 /* check the format byte size */
2443 pcm_format
= SNDRV_PCM_FORMAT_S8
;
2446 if (is_big_endian_format(chip
, fp
))
2447 pcm_format
= SNDRV_PCM_FORMAT_S16_BE
; /* grrr, big endian!! */
2449 pcm_format
= SNDRV_PCM_FORMAT_S16_LE
;
2452 if (is_big_endian_format(chip
, fp
))
2453 pcm_format
= SNDRV_PCM_FORMAT_S24_3BE
; /* grrr, big endian!! */
2455 pcm_format
= SNDRV_PCM_FORMAT_S24_3LE
;
2458 pcm_format
= SNDRV_PCM_FORMAT_S32_LE
;
2461 snd_printk(KERN_INFO
"%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
2462 chip
->dev
->devnum
, fp
->iface
,
2463 fp
->altsetting
, sample_width
, sample_bytes
);
2467 case USB_AUDIO_FORMAT_PCM8
:
2468 pcm_format
= SNDRV_PCM_FORMAT_U8
;
2470 /* Dallas DS4201 workaround: it advertises U8 format, but really
2472 if (chip
->usb_id
== USB_ID(0x04fa, 0x4201))
2473 pcm_format
= SNDRV_PCM_FORMAT_S8
;
2475 case USB_AUDIO_FORMAT_IEEE_FLOAT
:
2476 pcm_format
= SNDRV_PCM_FORMAT_FLOAT_LE
;
2478 case USB_AUDIO_FORMAT_ALAW
:
2479 pcm_format
= SNDRV_PCM_FORMAT_A_LAW
;
2481 case USB_AUDIO_FORMAT_MU_LAW
:
2482 pcm_format
= SNDRV_PCM_FORMAT_MU_LAW
;
2485 snd_printk(KERN_INFO
"%d:%u:%d : unsupported format type %d\n",
2486 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
, format
);
2494 * parse the format descriptor and stores the possible sample rates
2495 * on the audioformat table.
2498 * @fp: audioformat record
2499 * @fmt: the format descriptor
2500 * @offset: the start offset of descriptor pointing the rate type
2501 * (7 for type I and II, 8 for type II)
2503 static int parse_audio_format_rates(struct snd_usb_audio
*chip
, struct audioformat
*fp
,
2504 unsigned char *fmt
, int offset
)
2506 int nr_rates
= fmt
[offset
];
2508 if (fmt
[0] < offset
+ 1 + 3 * (nr_rates
? nr_rates
: 2)) {
2509 snd_printk(KERN_ERR
"%d:%u:%d : invalid FORMAT_TYPE desc\n",
2510 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
);
2516 * build the rate table and bitmap flags
2520 fp
->rate_table
= kmalloc(sizeof(int) * nr_rates
, GFP_KERNEL
);
2521 if (fp
->rate_table
== NULL
) {
2522 snd_printk(KERN_ERR
"cannot malloc\n");
2527 fp
->rate_min
= fp
->rate_max
= 0;
2528 for (r
= 0, idx
= offset
+ 1; r
< nr_rates
; r
++, idx
+= 3) {
2529 unsigned int rate
= combine_triple(&fmt
[idx
]);
2532 /* C-Media CM6501 mislabels its 96 kHz altsetting */
2533 if (rate
== 48000 && nr_rates
== 1 &&
2534 (chip
->usb_id
== USB_ID(0x0d8c, 0x0201) ||
2535 chip
->usb_id
== USB_ID(0x0d8c, 0x0102)) &&
2536 fp
->altsetting
== 5 && fp
->maxpacksize
== 392)
2538 fp
->rate_table
[fp
->nr_rates
] = rate
;
2539 if (!fp
->rate_min
|| rate
< fp
->rate_min
)
2540 fp
->rate_min
= rate
;
2541 if (!fp
->rate_max
|| rate
> fp
->rate_max
)
2542 fp
->rate_max
= rate
;
2543 fp
->rates
|= snd_pcm_rate_to_rate_bit(rate
);
2546 if (!fp
->nr_rates
) {
2547 hwc_debug("All rates were zero. Skipping format!\n");
2551 /* continuous rates */
2552 fp
->rates
= SNDRV_PCM_RATE_CONTINUOUS
;
2553 fp
->rate_min
= combine_triple(&fmt
[offset
+ 1]);
2554 fp
->rate_max
= combine_triple(&fmt
[offset
+ 4]);
2560 * parse the format type I and III descriptors
2562 static int parse_audio_format_i(struct snd_usb_audio
*chip
, struct audioformat
*fp
,
2563 int format
, unsigned char *fmt
)
2567 if (fmt
[3] == USB_FORMAT_TYPE_III
) {
2568 /* FIXME: the format type is really IECxxx
2569 * but we give normal PCM format to get the existing
2572 switch (chip
->usb_id
) {
2574 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2575 if (device_setup
[chip
->index
] == 0x00 &&
2576 fp
->altsetting
== 6)
2577 pcm_format
= SNDRV_PCM_FORMAT_S16_BE
;
2579 pcm_format
= SNDRV_PCM_FORMAT_S16_LE
;
2582 pcm_format
= SNDRV_PCM_FORMAT_S16_LE
;
2585 pcm_format
= parse_audio_format_i_type(chip
, fp
, format
, fmt
);
2589 fp
->format
= pcm_format
;
2590 fp
->channels
= fmt
[4];
2591 if (fp
->channels
< 1) {
2592 snd_printk(KERN_ERR
"%d:%u:%d : invalid channels %d\n",
2593 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
, fp
->channels
);
2596 return parse_audio_format_rates(chip
, fp
, fmt
, 7);
2600 * prase the format type II descriptor
2602 static int parse_audio_format_ii(struct snd_usb_audio
*chip
, struct audioformat
*fp
,
2603 int format
, unsigned char *fmt
)
2605 int brate
, framesize
;
2607 case USB_AUDIO_FORMAT_AC3
:
2608 /* FIXME: there is no AC3 format defined yet */
2609 // fp->format = SNDRV_PCM_FORMAT_AC3;
2610 fp
->format
= SNDRV_PCM_FORMAT_U8
; /* temporarily hack to receive byte streams */
2612 case USB_AUDIO_FORMAT_MPEG
:
2613 fp
->format
= SNDRV_PCM_FORMAT_MPEG
;
2616 snd_printd(KERN_INFO
"%d:%u:%d : unknown format tag 0x%x is detected. processed as MPEG.\n",
2617 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
, format
);
2618 fp
->format
= SNDRV_PCM_FORMAT_MPEG
;
2622 brate
= combine_word(&fmt
[4]); /* fmt[4,5] : wMaxBitRate (in kbps) */
2623 framesize
= combine_word(&fmt
[6]); /* fmt[6,7]: wSamplesPerFrame */
2624 snd_printd(KERN_INFO
"found format II with max.bitrate = %d, frame size=%d\n", brate
, framesize
);
2625 fp
->frame_size
= framesize
;
2626 return parse_audio_format_rates(chip
, fp
, fmt
, 8); /* fmt[8..] sample rates */
2629 static int parse_audio_format(struct snd_usb_audio
*chip
, struct audioformat
*fp
,
2630 int format
, unsigned char *fmt
, int stream
)
2635 case USB_FORMAT_TYPE_I
:
2636 case USB_FORMAT_TYPE_III
:
2637 err
= parse_audio_format_i(chip
, fp
, format
, fmt
);
2639 case USB_FORMAT_TYPE_II
:
2640 err
= parse_audio_format_ii(chip
, fp
, format
, fmt
);
2643 snd_printd(KERN_INFO
"%d:%u:%d : format type %d is not supported yet\n",
2644 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
, fmt
[3]);
2647 fp
->fmt_type
= fmt
[3];
2651 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
2652 /* extigy apparently supports sample rates other than 48k
2653 * but not in ordinary way. so we enable only 48k atm.
2655 if (chip
->usb_id
== USB_ID(0x041e, 0x3000) ||
2656 chip
->usb_id
== USB_ID(0x041e, 0x3020) ||
2657 chip
->usb_id
== USB_ID(0x041e, 0x3061)) {
2658 if (fmt
[3] == USB_FORMAT_TYPE_I
&&
2659 fp
->rates
!= SNDRV_PCM_RATE_48000
&&
2660 fp
->rates
!= SNDRV_PCM_RATE_96000
)
2667 static int audiophile_skip_setting_quirk(struct snd_usb_audio
*chip
,
2668 int iface
, int altno
);
2669 static int parse_audio_endpoints(struct snd_usb_audio
*chip
, int iface_no
)
2671 struct usb_device
*dev
;
2672 struct usb_interface
*iface
;
2673 struct usb_host_interface
*alts
;
2674 struct usb_interface_descriptor
*altsd
;
2675 int i
, altno
, err
, stream
;
2677 struct audioformat
*fp
= NULL
;
2678 unsigned char *fmt
, *csep
;
2683 /* parse the interface's altsettings */
2684 iface
= usb_ifnum_to_if(dev
, iface_no
);
2686 num
= iface
->num_altsetting
;
2689 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
2690 * one misses syncpipe, and does not produce any sound.
2692 if (chip
->usb_id
== USB_ID(0x04fa, 0x4201))
2695 for (i
= 0; i
< num
; i
++) {
2696 alts
= &iface
->altsetting
[i
];
2697 altsd
= get_iface_desc(alts
);
2698 /* skip invalid one */
2699 if ((altsd
->bInterfaceClass
!= USB_CLASS_AUDIO
&&
2700 altsd
->bInterfaceClass
!= USB_CLASS_VENDOR_SPEC
) ||
2701 (altsd
->bInterfaceSubClass
!= USB_SUBCLASS_AUDIO_STREAMING
&&
2702 altsd
->bInterfaceSubClass
!= USB_SUBCLASS_VENDOR_SPEC
) ||
2703 altsd
->bNumEndpoints
< 1 ||
2704 le16_to_cpu(get_endpoint(alts
, 0)->wMaxPacketSize
) == 0)
2706 /* must be isochronous */
2707 if ((get_endpoint(alts
, 0)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) !=
2708 USB_ENDPOINT_XFER_ISOC
)
2710 /* check direction */
2711 stream
= (get_endpoint(alts
, 0)->bEndpointAddress
& USB_DIR_IN
) ?
2712 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
;
2713 altno
= altsd
->bAlternateSetting
;
2715 /* audiophile usb: skip altsets incompatible with device_setup
2717 if (chip
->usb_id
== USB_ID(0x0763, 0x2003) &&
2718 audiophile_skip_setting_quirk(chip
, iface_no
, altno
))
2721 /* get audio formats */
2722 fmt
= snd_usb_find_csint_desc(alts
->extra
, alts
->extralen
, NULL
, AS_GENERAL
);
2724 snd_printk(KERN_ERR
"%d:%u:%d : AS_GENERAL descriptor not found\n",
2725 dev
->devnum
, iface_no
, altno
);
2730 snd_printk(KERN_ERR
"%d:%u:%d : invalid AS_GENERAL desc\n",
2731 dev
->devnum
, iface_no
, altno
);
2735 format
= (fmt
[6] << 8) | fmt
[5]; /* remember the format value */
2737 /* get format type */
2738 fmt
= snd_usb_find_csint_desc(alts
->extra
, alts
->extralen
, NULL
, FORMAT_TYPE
);
2740 snd_printk(KERN_ERR
"%d:%u:%d : no FORMAT_TYPE desc\n",
2741 dev
->devnum
, iface_no
, altno
);
2745 snd_printk(KERN_ERR
"%d:%u:%d : invalid FORMAT_TYPE desc\n",
2746 dev
->devnum
, iface_no
, altno
);
2751 * Blue Microphones workaround: The last altsetting is identical
2752 * with the previous one, except for a larger packet size, but
2753 * is actually a mislabeled two-channel setting; ignore it.
2755 if (fmt
[4] == 1 && fmt
[5] == 2 && altno
== 2 && num
== 3 &&
2756 fp
&& fp
->altsetting
== 1 && fp
->channels
== 1 &&
2757 fp
->format
== SNDRV_PCM_FORMAT_S16_LE
&&
2758 le16_to_cpu(get_endpoint(alts
, 0)->wMaxPacketSize
) ==
2759 fp
->maxpacksize
* 2)
2762 csep
= snd_usb_find_desc(alts
->endpoint
[0].extra
, alts
->endpoint
[0].extralen
, NULL
, USB_DT_CS_ENDPOINT
);
2763 /* Creamware Noah has this descriptor after the 2nd endpoint */
2764 if (!csep
&& altsd
->bNumEndpoints
>= 2)
2765 csep
= snd_usb_find_desc(alts
->endpoint
[1].extra
, alts
->endpoint
[1].extralen
, NULL
, USB_DT_CS_ENDPOINT
);
2766 if (!csep
|| csep
[0] < 7 || csep
[2] != EP_GENERAL
) {
2767 snd_printk(KERN_WARNING
"%d:%u:%d : no or invalid"
2768 " class specific endpoint descriptor\n",
2769 dev
->devnum
, iface_no
, altno
);
2773 fp
= kzalloc(sizeof(*fp
), GFP_KERNEL
);
2775 snd_printk(KERN_ERR
"cannot malloc\n");
2779 fp
->iface
= iface_no
;
2780 fp
->altsetting
= altno
;
2782 fp
->endpoint
= get_endpoint(alts
, 0)->bEndpointAddress
;
2783 fp
->ep_attr
= get_endpoint(alts
, 0)->bmAttributes
;
2784 fp
->maxpacksize
= le16_to_cpu(get_endpoint(alts
, 0)->wMaxPacketSize
);
2785 if (snd_usb_get_speed(dev
) == USB_SPEED_HIGH
)
2786 fp
->maxpacksize
= (((fp
->maxpacksize
>> 11) & 3) + 1)
2787 * (fp
->maxpacksize
& 0x7ff);
2788 fp
->attributes
= csep
? csep
[3] : 0;
2790 /* some quirks for attributes here */
2792 switch (chip
->usb_id
) {
2793 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
2794 /* Optoplay sets the sample rate attribute although
2795 * it seems not supporting it in fact.
2797 fp
->attributes
&= ~EP_CS_ATTR_SAMPLE_RATE
;
2799 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
2800 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2801 /* doesn't set the sample rate attribute, but supports it */
2802 fp
->attributes
|= EP_CS_ATTR_SAMPLE_RATE
;
2804 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
2805 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
2806 an older model 77d:223) */
2808 * plantronics headset and Griffin iMic have set adaptive-in
2809 * although it's really not...
2811 fp
->ep_attr
&= ~EP_ATTR_MASK
;
2812 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2813 fp
->ep_attr
|= EP_ATTR_ADAPTIVE
;
2815 fp
->ep_attr
|= EP_ATTR_SYNC
;
2819 /* ok, let's parse further... */
2820 if (parse_audio_format(chip
, fp
, format
, fmt
, stream
) < 0) {
2821 kfree(fp
->rate_table
);
2826 snd_printdd(KERN_INFO
"%d:%u:%d: add audio endpoint 0x%x\n", dev
->devnum
, iface_no
, altno
, fp
->endpoint
);
2827 err
= add_audio_endpoint(chip
, stream
, fp
);
2829 kfree(fp
->rate_table
);
2833 /* try to set the interface... */
2834 usb_set_interface(chip
->dev
, iface_no
, altno
);
2835 init_usb_pitch(chip
->dev
, iface_no
, alts
, fp
);
2836 init_usb_sample_rate(chip
->dev
, iface_no
, alts
, fp
, fp
->rate_max
);
2843 * disconnect streams
2844 * called from snd_usb_audio_disconnect()
2846 static void snd_usb_stream_disconnect(struct list_head
*head
)
2849 struct snd_usb_stream
*as
;
2850 struct snd_usb_substream
*subs
;
2852 as
= list_entry(head
, struct snd_usb_stream
, list
);
2853 for (idx
= 0; idx
< 2; idx
++) {
2854 subs
= &as
->substream
[idx
];
2855 if (!subs
->num_formats
)
2857 release_substream_urbs(subs
, 1);
2858 subs
->interface
= -1;
2863 * parse audio control descriptor and create pcm/midi streams
2865 static int snd_usb_create_streams(struct snd_usb_audio
*chip
, int ctrlif
)
2867 struct usb_device
*dev
= chip
->dev
;
2868 struct usb_host_interface
*host_iface
;
2869 struct usb_interface
*iface
;
2873 /* find audiocontrol interface */
2874 host_iface
= &usb_ifnum_to_if(dev
, ctrlif
)->altsetting
[0];
2875 if (!(p1
= snd_usb_find_csint_desc(host_iface
->extra
, host_iface
->extralen
, NULL
, HEADER
))) {
2876 snd_printk(KERN_ERR
"cannot find HEADER\n");
2879 if (! p1
[7] || p1
[0] < 8 + p1
[7]) {
2880 snd_printk(KERN_ERR
"invalid HEADER\n");
2885 * parse all USB audio streaming interfaces
2887 for (i
= 0; i
< p1
[7]; i
++) {
2888 struct usb_host_interface
*alts
;
2889 struct usb_interface_descriptor
*altsd
;
2891 iface
= usb_ifnum_to_if(dev
, j
);
2893 snd_printk(KERN_ERR
"%d:%u:%d : does not exist\n",
2894 dev
->devnum
, ctrlif
, j
);
2897 if (usb_interface_claimed(iface
)) {
2898 snd_printdd(KERN_INFO
"%d:%d:%d: skipping, already claimed\n", dev
->devnum
, ctrlif
, j
);
2901 alts
= &iface
->altsetting
[0];
2902 altsd
= get_iface_desc(alts
);
2903 if ((altsd
->bInterfaceClass
== USB_CLASS_AUDIO
||
2904 altsd
->bInterfaceClass
== USB_CLASS_VENDOR_SPEC
) &&
2905 altsd
->bInterfaceSubClass
== USB_SUBCLASS_MIDI_STREAMING
) {
2906 if (snd_usb_create_midi_interface(chip
, iface
, NULL
) < 0) {
2907 snd_printk(KERN_ERR
"%d:%u:%d: cannot create sequencer device\n", dev
->devnum
, ctrlif
, j
);
2910 usb_driver_claim_interface(&usb_audio_driver
, iface
, (void *)-1L);
2913 if ((altsd
->bInterfaceClass
!= USB_CLASS_AUDIO
&&
2914 altsd
->bInterfaceClass
!= USB_CLASS_VENDOR_SPEC
) ||
2915 altsd
->bInterfaceSubClass
!= USB_SUBCLASS_AUDIO_STREAMING
) {
2916 snd_printdd(KERN_ERR
"%d:%u:%d: skipping non-supported interface %d\n", dev
->devnum
, ctrlif
, j
, altsd
->bInterfaceClass
);
2917 /* skip non-supported classes */
2920 if (snd_usb_get_speed(dev
) == USB_SPEED_LOW
) {
2921 snd_printk(KERN_ERR
"low speed audio streaming not supported\n");
2924 if (! parse_audio_endpoints(chip
, j
)) {
2925 usb_set_interface(dev
, j
, 0); /* reset the current interface */
2926 usb_driver_claim_interface(&usb_audio_driver
, iface
, (void *)-1L);
2934 * create a stream for an endpoint/altsetting without proper descriptors
2936 static int create_fixed_stream_quirk(struct snd_usb_audio
*chip
,
2937 struct usb_interface
*iface
,
2938 const struct snd_usb_audio_quirk
*quirk
)
2940 struct audioformat
*fp
;
2941 struct usb_host_interface
*alts
;
2943 unsigned *rate_table
= NULL
;
2945 fp
= kmemdup(quirk
->data
, sizeof(*fp
), GFP_KERNEL
);
2947 snd_printk(KERN_ERR
"cannot memdup\n");
2950 if (fp
->nr_rates
> 0) {
2951 rate_table
= kmalloc(sizeof(int) * fp
->nr_rates
, GFP_KERNEL
);
2956 memcpy(rate_table
, fp
->rate_table
, sizeof(int) * fp
->nr_rates
);
2957 fp
->rate_table
= rate_table
;
2960 stream
= (fp
->endpoint
& USB_DIR_IN
)
2961 ? SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
;
2962 err
= add_audio_endpoint(chip
, stream
, fp
);
2968 if (fp
->iface
!= get_iface_desc(&iface
->altsetting
[0])->bInterfaceNumber
||
2969 fp
->altset_idx
>= iface
->num_altsetting
) {
2974 alts
= &iface
->altsetting
[fp
->altset_idx
];
2975 fp
->maxpacksize
= le16_to_cpu(get_endpoint(alts
, 0)->wMaxPacketSize
);
2976 usb_set_interface(chip
->dev
, fp
->iface
, 0);
2977 init_usb_pitch(chip
->dev
, fp
->iface
, alts
, fp
);
2978 init_usb_sample_rate(chip
->dev
, fp
->iface
, alts
, fp
, fp
->rate_max
);
2983 * create a stream for an interface with proper descriptors
2985 static int create_standard_audio_quirk(struct snd_usb_audio
*chip
,
2986 struct usb_interface
*iface
,
2987 const struct snd_usb_audio_quirk
*quirk
)
2989 struct usb_host_interface
*alts
;
2990 struct usb_interface_descriptor
*altsd
;
2993 alts
= &iface
->altsetting
[0];
2994 altsd
= get_iface_desc(alts
);
2995 err
= parse_audio_endpoints(chip
, altsd
->bInterfaceNumber
);
2997 snd_printk(KERN_ERR
"cannot setup if %d: error %d\n",
2998 altsd
->bInterfaceNumber
, err
);
3001 /* reset the current interface */
3002 usb_set_interface(chip
->dev
, altsd
->bInterfaceNumber
, 0);
3007 * Create a stream for an Edirol UA-700/UA-25 interface. The only way
3008 * to detect the sample rate is by looking at wMaxPacketSize.
3010 static int create_ua700_ua25_quirk(struct snd_usb_audio
*chip
,
3011 struct usb_interface
*iface
,
3012 const struct snd_usb_audio_quirk
*quirk
)
3014 static const struct audioformat ua_format
= {
3015 .format
= SNDRV_PCM_FORMAT_S24_3LE
,
3017 .fmt_type
= USB_FORMAT_TYPE_I
,
3020 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
3022 struct usb_host_interface
*alts
;
3023 struct usb_interface_descriptor
*altsd
;
3024 struct audioformat
*fp
;
3027 /* both PCM and MIDI interfaces have 2 altsettings */
3028 if (iface
->num_altsetting
!= 2)
3030 alts
= &iface
->altsetting
[1];
3031 altsd
= get_iface_desc(alts
);
3033 if (altsd
->bNumEndpoints
== 2) {
3034 static const struct snd_usb_midi_endpoint_info ua700_ep
= {
3035 .out_cables
= 0x0003,
3038 static const struct snd_usb_audio_quirk ua700_quirk
= {
3039 .type
= QUIRK_MIDI_FIXED_ENDPOINT
,
3042 static const struct snd_usb_midi_endpoint_info ua25_ep
= {
3043 .out_cables
= 0x0001,
3046 static const struct snd_usb_audio_quirk ua25_quirk
= {
3047 .type
= QUIRK_MIDI_FIXED_ENDPOINT
,
3050 if (chip
->usb_id
== USB_ID(0x0582, 0x002b))
3051 return snd_usb_create_midi_interface(chip
, iface
,
3054 return snd_usb_create_midi_interface(chip
, iface
,
3058 if (altsd
->bNumEndpoints
!= 1)
3061 fp
= kmalloc(sizeof(*fp
), GFP_KERNEL
);
3064 memcpy(fp
, &ua_format
, sizeof(*fp
));
3066 fp
->iface
= altsd
->bInterfaceNumber
;
3067 fp
->endpoint
= get_endpoint(alts
, 0)->bEndpointAddress
;
3068 fp
->ep_attr
= get_endpoint(alts
, 0)->bmAttributes
;
3069 fp
->maxpacksize
= le16_to_cpu(get_endpoint(alts
, 0)->wMaxPacketSize
);
3071 switch (fp
->maxpacksize
) {
3073 fp
->rate_max
= fp
->rate_min
= 44100;
3077 fp
->rate_max
= fp
->rate_min
= 48000;
3081 fp
->rate_max
= fp
->rate_min
= 96000;
3084 snd_printk(KERN_ERR
"unknown sample rate\n");
3089 stream
= (fp
->endpoint
& USB_DIR_IN
)
3090 ? SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
;
3091 err
= add_audio_endpoint(chip
, stream
, fp
);
3096 usb_set_interface(chip
->dev
, fp
->iface
, 0);
3101 * Create a stream for an Edirol UA-1000 interface.
3103 static int create_ua1000_quirk(struct snd_usb_audio
*chip
,
3104 struct usb_interface
*iface
,
3105 const struct snd_usb_audio_quirk
*quirk
)
3107 static const struct audioformat ua1000_format
= {
3108 .format
= SNDRV_PCM_FORMAT_S32_LE
,
3109 .fmt_type
= USB_FORMAT_TYPE_I
,
3113 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
3115 struct usb_host_interface
*alts
;
3116 struct usb_interface_descriptor
*altsd
;
3117 struct audioformat
*fp
;
3120 if (iface
->num_altsetting
!= 2)
3122 alts
= &iface
->altsetting
[1];
3123 altsd
= get_iface_desc(alts
);
3124 if (alts
->extralen
!= 11 || alts
->extra
[1] != USB_DT_CS_INTERFACE
||
3125 altsd
->bNumEndpoints
!= 1)
3128 fp
= kmemdup(&ua1000_format
, sizeof(*fp
), GFP_KERNEL
);
3132 fp
->channels
= alts
->extra
[4];
3133 fp
->iface
= altsd
->bInterfaceNumber
;
3134 fp
->endpoint
= get_endpoint(alts
, 0)->bEndpointAddress
;
3135 fp
->ep_attr
= get_endpoint(alts
, 0)->bmAttributes
;
3136 fp
->maxpacksize
= le16_to_cpu(get_endpoint(alts
, 0)->wMaxPacketSize
);
3137 fp
->rate_max
= fp
->rate_min
= combine_triple(&alts
->extra
[8]);
3139 stream
= (fp
->endpoint
& USB_DIR_IN
)
3140 ? SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
;
3141 err
= add_audio_endpoint(chip
, stream
, fp
);
3146 /* FIXME: playback must be synchronized to capture */
3147 usb_set_interface(chip
->dev
, fp
->iface
, 0);
3152 * Create a stream for an Edirol UA-101 interface.
3153 * Copy, paste and modify from Edirol UA-1000
3155 static int create_ua101_quirk(struct snd_usb_audio
*chip
,
3156 struct usb_interface
*iface
,
3157 const struct snd_usb_audio_quirk
*quirk
)
3159 static const struct audioformat ua101_format
= {
3160 .format
= SNDRV_PCM_FORMAT_S32_LE
,
3161 .fmt_type
= USB_FORMAT_TYPE_I
,
3165 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
3167 struct usb_host_interface
*alts
;
3168 struct usb_interface_descriptor
*altsd
;
3169 struct audioformat
*fp
;
3172 if (iface
->num_altsetting
!= 2)
3174 alts
= &iface
->altsetting
[1];
3175 altsd
= get_iface_desc(alts
);
3176 if (alts
->extralen
!= 18 || alts
->extra
[1] != USB_DT_CS_INTERFACE
||
3177 altsd
->bNumEndpoints
!= 1)
3180 fp
= kmemdup(&ua101_format
, sizeof(*fp
), GFP_KERNEL
);
3184 fp
->channels
= alts
->extra
[11];
3185 fp
->iface
= altsd
->bInterfaceNumber
;
3186 fp
->endpoint
= get_endpoint(alts
, 0)->bEndpointAddress
;
3187 fp
->ep_attr
= get_endpoint(alts
, 0)->bmAttributes
;
3188 fp
->maxpacksize
= le16_to_cpu(get_endpoint(alts
, 0)->wMaxPacketSize
);
3189 fp
->rate_max
= fp
->rate_min
= combine_triple(&alts
->extra
[15]);
3191 stream
= (fp
->endpoint
& USB_DIR_IN
)
3192 ? SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
;
3193 err
= add_audio_endpoint(chip
, stream
, fp
);
3198 /* FIXME: playback must be synchronized to capture */
3199 usb_set_interface(chip
->dev
, fp
->iface
, 0);
3203 static int snd_usb_create_quirk(struct snd_usb_audio
*chip
,
3204 struct usb_interface
*iface
,
3205 const struct snd_usb_audio_quirk
*quirk
);
3208 * handle the quirks for the contained interfaces
3210 static int create_composite_quirk(struct snd_usb_audio
*chip
,
3211 struct usb_interface
*iface
,
3212 const struct snd_usb_audio_quirk
*quirk
)
3214 int probed_ifnum
= get_iface_desc(iface
->altsetting
)->bInterfaceNumber
;
3217 for (quirk
= quirk
->data
; quirk
->ifnum
>= 0; ++quirk
) {
3218 iface
= usb_ifnum_to_if(chip
->dev
, quirk
->ifnum
);
3221 if (quirk
->ifnum
!= probed_ifnum
&&
3222 usb_interface_claimed(iface
))
3224 err
= snd_usb_create_quirk(chip
, iface
, quirk
);
3227 if (quirk
->ifnum
!= probed_ifnum
)
3228 usb_driver_claim_interface(&usb_audio_driver
, iface
, (void *)-1L);
3233 static int ignore_interface_quirk(struct snd_usb_audio
*chip
,
3234 struct usb_interface
*iface
,
3235 const struct snd_usb_audio_quirk
*quirk
)
3245 #define EXTIGY_FIRMWARE_SIZE_OLD 794
3246 #define EXTIGY_FIRMWARE_SIZE_NEW 483
3248 static int snd_usb_extigy_boot_quirk(struct usb_device
*dev
, struct usb_interface
*intf
)
3250 struct usb_host_config
*config
= dev
->actconfig
;
3253 if (le16_to_cpu(get_cfg_desc(config
)->wTotalLength
) == EXTIGY_FIRMWARE_SIZE_OLD
||
3254 le16_to_cpu(get_cfg_desc(config
)->wTotalLength
) == EXTIGY_FIRMWARE_SIZE_NEW
) {
3255 snd_printdd("sending Extigy boot sequence...\n");
3256 /* Send message to force it to reconnect with full interface. */
3257 err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
,0),
3258 0x10, 0x43, 0x0001, 0x000a, NULL
, 0, 1000);
3259 if (err
< 0) snd_printdd("error sending boot message: %d\n", err
);
3260 err
= usb_get_descriptor(dev
, USB_DT_DEVICE
, 0,
3261 &dev
->descriptor
, sizeof(dev
->descriptor
));
3262 config
= dev
->actconfig
;
3263 if (err
< 0) snd_printdd("error usb_get_descriptor: %d\n", err
);
3264 err
= usb_reset_configuration(dev
);
3265 if (err
< 0) snd_printdd("error usb_reset_configuration: %d\n", err
);
3266 snd_printdd("extigy_boot: new boot length = %d\n",
3267 le16_to_cpu(get_cfg_desc(config
)->wTotalLength
));
3268 return -ENODEV
; /* quit this anyway */
3273 static int snd_usb_audigy2nx_boot_quirk(struct usb_device
*dev
)
3277 snd_usb_ctl_msg(dev
, usb_rcvctrlpipe(dev
, 0), 0x2a,
3278 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_OTHER
,
3279 0, 0, &buf
, 1, 1000);
3281 snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), 0x29,
3282 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_OTHER
,
3283 1, 2000, NULL
, 0, 1000);
3290 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
3291 * documented in the device's data sheet.
3293 static int snd_usb_cm106_write_int_reg(struct usb_device
*dev
, int reg
, u16 value
)
3297 buf
[1] = value
& 0xff;
3298 buf
[2] = (value
>> 8) & 0xff;
3300 return snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), USB_REQ_SET_CONFIGURATION
,
3301 USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
3302 0, 0, &buf
, 4, 1000);
3305 static int snd_usb_cm106_boot_quirk(struct usb_device
*dev
)
3308 * Enable line-out driver mode, set headphone source to front
3309 * channels, enable stereo mic.
3311 return snd_usb_cm106_write_int_reg(dev
, 2, 0x8004);
3318 #define AUDIOPHILE_SET 0x01 /* if set, parse device_setup */
3319 #define AUDIOPHILE_SET_DTS 0x02 /* if set, enable DTS Digital Output */
3320 #define AUDIOPHILE_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
3321 #define AUDIOPHILE_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
3322 #define AUDIOPHILE_SET_DI 0x10 /* if set, enable Digital Input */
3323 #define AUDIOPHILE_SET_MASK 0x1F /* bit mask for setup value */
3324 #define AUDIOPHILE_SET_24B_48K_DI 0x19 /* value for 24bits+48KHz+Digital Input */
3325 #define AUDIOPHILE_SET_24B_48K_NOTDI 0x09 /* value for 24bits+48KHz+No Digital Input */
3326 #define AUDIOPHILE_SET_16B_48K_DI 0x11 /* value for 16bits+48KHz+Digital Input */
3327 #define AUDIOPHILE_SET_16B_48K_NOTDI 0x01 /* value for 16bits+48KHz+No Digital Input */
3329 static int audiophile_skip_setting_quirk(struct snd_usb_audio
*chip
,
3330 int iface
, int altno
)
3332 /* Reset ALL ifaces to 0 altsetting.
3333 * Call it for every possible altsetting of every interface.
3335 usb_set_interface(chip
->dev
, iface
, 0);
3337 if (device_setup
[chip
->index
] & AUDIOPHILE_SET
) {
3338 if ((device_setup
[chip
->index
] & AUDIOPHILE_SET_DTS
)
3340 return 1; /* skip this altsetting */
3341 if ((device_setup
[chip
->index
] & AUDIOPHILE_SET_96K
)
3343 return 1; /* skip this altsetting */
3344 if ((device_setup
[chip
->index
] & AUDIOPHILE_SET_MASK
) ==
3345 AUDIOPHILE_SET_24B_48K_DI
&& altno
!= 2)
3346 return 1; /* skip this altsetting */
3347 if ((device_setup
[chip
->index
] & AUDIOPHILE_SET_MASK
) ==
3348 AUDIOPHILE_SET_24B_48K_NOTDI
&& altno
!= 3)
3349 return 1; /* skip this altsetting */
3350 if ((device_setup
[chip
->index
] & AUDIOPHILE_SET_MASK
) ==
3351 AUDIOPHILE_SET_16B_48K_DI
&& altno
!= 4)
3352 return 1; /* skip this altsetting */
3353 if ((device_setup
[chip
->index
] & AUDIOPHILE_SET_MASK
) ==
3354 AUDIOPHILE_SET_16B_48K_NOTDI
&& altno
!= 5)
3355 return 1; /* skip this altsetting */
3357 return 0; /* keep this altsetting */
3361 * audio-interface quirks
3363 * returns zero if no standard audio/MIDI parsing is needed.
3364 * returns a postive value if standard audio/midi interfaces are parsed
3366 * returns a negative value at error.
3368 static int snd_usb_create_quirk(struct snd_usb_audio
*chip
,
3369 struct usb_interface
*iface
,
3370 const struct snd_usb_audio_quirk
*quirk
)
3372 typedef int (*quirk_func_t
)(struct snd_usb_audio
*, struct usb_interface
*,
3373 const struct snd_usb_audio_quirk
*);
3374 static const quirk_func_t quirk_funcs
[] = {
3375 [QUIRK_IGNORE_INTERFACE
] = ignore_interface_quirk
,
3376 [QUIRK_COMPOSITE
] = create_composite_quirk
,
3377 [QUIRK_MIDI_STANDARD_INTERFACE
] = snd_usb_create_midi_interface
,
3378 [QUIRK_MIDI_FIXED_ENDPOINT
] = snd_usb_create_midi_interface
,
3379 [QUIRK_MIDI_YAMAHA
] = snd_usb_create_midi_interface
,
3380 [QUIRK_MIDI_MIDIMAN
] = snd_usb_create_midi_interface
,
3381 [QUIRK_MIDI_NOVATION
] = snd_usb_create_midi_interface
,
3382 [QUIRK_MIDI_FASTLANE
] = snd_usb_create_midi_interface
,
3383 [QUIRK_MIDI_EMAGIC
] = snd_usb_create_midi_interface
,
3384 [QUIRK_MIDI_CME
] = snd_usb_create_midi_interface
,
3385 [QUIRK_AUDIO_STANDARD_INTERFACE
] = create_standard_audio_quirk
,
3386 [QUIRK_AUDIO_FIXED_ENDPOINT
] = create_fixed_stream_quirk
,
3387 [QUIRK_AUDIO_EDIROL_UA700_UA25
] = create_ua700_ua25_quirk
,
3388 [QUIRK_AUDIO_EDIROL_UA1000
] = create_ua1000_quirk
,
3389 [QUIRK_AUDIO_EDIROL_UA101
] = create_ua101_quirk
,
3392 if (quirk
->type
< QUIRK_TYPE_COUNT
) {
3393 return quirk_funcs
[quirk
->type
](chip
, iface
, quirk
);
3395 snd_printd(KERN_ERR
"invalid quirk type %d\n", quirk
->type
);
3402 * common proc files to show the usb device info
3404 static void proc_audio_usbbus_read(struct snd_info_entry
*entry
, struct snd_info_buffer
*buffer
)
3406 struct snd_usb_audio
*chip
= entry
->private_data
;
3407 if (!chip
->shutdown
)
3408 snd_iprintf(buffer
, "%03d/%03d\n", chip
->dev
->bus
->busnum
, chip
->dev
->devnum
);
3411 static void proc_audio_usbid_read(struct snd_info_entry
*entry
, struct snd_info_buffer
*buffer
)
3413 struct snd_usb_audio
*chip
= entry
->private_data
;
3414 if (!chip
->shutdown
)
3415 snd_iprintf(buffer
, "%04x:%04x\n",
3416 USB_ID_VENDOR(chip
->usb_id
),
3417 USB_ID_PRODUCT(chip
->usb_id
));
3420 static void snd_usb_audio_create_proc(struct snd_usb_audio
*chip
)
3422 struct snd_info_entry
*entry
;
3423 if (!snd_card_proc_new(chip
->card
, "usbbus", &entry
))
3424 snd_info_set_text_ops(entry
, chip
, proc_audio_usbbus_read
);
3425 if (!snd_card_proc_new(chip
->card
, "usbid", &entry
))
3426 snd_info_set_text_ops(entry
, chip
, proc_audio_usbid_read
);
3430 * free the chip instance
3432 * here we have to do not much, since pcm and controls are already freed
3436 static int snd_usb_audio_free(struct snd_usb_audio
*chip
)
3442 static int snd_usb_audio_dev_free(struct snd_device
*device
)
3444 struct snd_usb_audio
*chip
= device
->device_data
;
3445 return snd_usb_audio_free(chip
);
3450 * create a chip instance and set its names.
3452 static int snd_usb_audio_create(struct usb_device
*dev
, int idx
,
3453 const struct snd_usb_audio_quirk
*quirk
,
3454 struct snd_usb_audio
**rchip
)
3456 struct snd_card
*card
;
3457 struct snd_usb_audio
*chip
;
3460 static struct snd_device_ops ops
= {
3461 .dev_free
= snd_usb_audio_dev_free
,
3466 if (snd_usb_get_speed(dev
) != USB_SPEED_LOW
&&
3467 snd_usb_get_speed(dev
) != USB_SPEED_FULL
&&
3468 snd_usb_get_speed(dev
) != USB_SPEED_HIGH
) {
3469 snd_printk(KERN_ERR
"unknown device speed %d\n", snd_usb_get_speed(dev
));
3473 card
= snd_card_new(index
[idx
], id
[idx
], THIS_MODULE
, 0);
3475 snd_printk(KERN_ERR
"cannot create card instance %d\n", idx
);
3479 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
3481 snd_card_free(card
);
3488 chip
->usb_id
= USB_ID(le16_to_cpu(dev
->descriptor
.idVendor
),
3489 le16_to_cpu(dev
->descriptor
.idProduct
));
3490 INIT_LIST_HEAD(&chip
->pcm_list
);
3491 INIT_LIST_HEAD(&chip
->midi_list
);
3492 INIT_LIST_HEAD(&chip
->mixer_list
);
3494 if ((err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
)) < 0) {
3495 snd_usb_audio_free(chip
);
3496 snd_card_free(card
);
3500 strcpy(card
->driver
, "USB-Audio");
3501 sprintf(component
, "USB%04x:%04x",
3502 USB_ID_VENDOR(chip
->usb_id
), USB_ID_PRODUCT(chip
->usb_id
));
3503 snd_component_add(card
, component
);
3505 /* retrieve the device string as shortname */
3506 if (quirk
&& quirk
->product_name
) {
3507 strlcpy(card
->shortname
, quirk
->product_name
, sizeof(card
->shortname
));
3509 if (!dev
->descriptor
.iProduct
||
3510 usb_string(dev
, dev
->descriptor
.iProduct
,
3511 card
->shortname
, sizeof(card
->shortname
)) <= 0) {
3512 /* no name available from anywhere, so use ID */
3513 sprintf(card
->shortname
, "USB Device %#04x:%#04x",
3514 USB_ID_VENDOR(chip
->usb_id
),
3515 USB_ID_PRODUCT(chip
->usb_id
));
3519 /* retrieve the vendor and device strings as longname */
3520 if (quirk
&& quirk
->vendor_name
) {
3521 len
= strlcpy(card
->longname
, quirk
->vendor_name
, sizeof(card
->longname
));
3523 if (dev
->descriptor
.iManufacturer
)
3524 len
= usb_string(dev
, dev
->descriptor
.iManufacturer
,
3525 card
->longname
, sizeof(card
->longname
));
3528 /* we don't really care if there isn't any vendor string */
3531 strlcat(card
->longname
, " ", sizeof(card
->longname
));
3533 strlcat(card
->longname
, card
->shortname
, sizeof(card
->longname
));
3535 len
= strlcat(card
->longname
, " at ", sizeof(card
->longname
));
3537 if (len
< sizeof(card
->longname
))
3538 usb_make_path(dev
, card
->longname
+ len
, sizeof(card
->longname
) - len
);
3540 strlcat(card
->longname
,
3541 snd_usb_get_speed(dev
) == USB_SPEED_LOW
? ", low speed" :
3542 snd_usb_get_speed(dev
) == USB_SPEED_FULL
? ", full speed" :
3544 sizeof(card
->longname
));
3546 snd_usb_audio_create_proc(chip
);
3554 * probe the active usb device
3556 * note that this can be called multiple times per a device, when it
3557 * includes multiple audio control interfaces.
3559 * thus we check the usb device pointer and creates the card instance
3560 * only at the first time. the successive calls of this function will
3561 * append the pcm interface to the corresponding card.
3563 static void *snd_usb_audio_probe(struct usb_device
*dev
,
3564 struct usb_interface
*intf
,
3565 const struct usb_device_id
*usb_id
)
3567 const struct snd_usb_audio_quirk
*quirk
= (const struct snd_usb_audio_quirk
*)usb_id
->driver_info
;
3569 struct snd_usb_audio
*chip
;
3570 struct usb_host_interface
*alts
;
3574 alts
= &intf
->altsetting
[0];
3575 ifnum
= get_iface_desc(alts
)->bInterfaceNumber
;
3576 id
= USB_ID(le16_to_cpu(dev
->descriptor
.idVendor
),
3577 le16_to_cpu(dev
->descriptor
.idProduct
));
3579 if (quirk
&& quirk
->ifnum
>= 0 && ifnum
!= quirk
->ifnum
)
3582 /* SB Extigy needs special boot-up sequence */
3583 /* if more models come, this will go to the quirk list. */
3584 if (id
== USB_ID(0x041e, 0x3000)) {
3585 if (snd_usb_extigy_boot_quirk(dev
, intf
) < 0)
3588 /* SB Audigy 2 NX needs its own boot-up magic, too */
3589 if (id
== USB_ID(0x041e, 0x3020)) {
3590 if (snd_usb_audigy2nx_boot_quirk(dev
) < 0)
3594 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
3595 if (id
== USB_ID(0x10f5, 0x0200)) {
3596 if (snd_usb_cm106_boot_quirk(dev
) < 0)
3601 * found a config. now register to ALSA
3604 /* check whether it's already registered */
3606 mutex_lock(®ister_mutex
);
3607 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
3608 if (usb_chip
[i
] && usb_chip
[i
]->dev
== dev
) {
3609 if (usb_chip
[i
]->shutdown
) {
3610 snd_printk(KERN_ERR
"USB device is in the shutdown state, cannot create a card instance\n");
3618 /* it's a fresh one.
3619 * now look for an empty slot and create a new card instance
3621 for (i
= 0; i
< SNDRV_CARDS
; i
++)
3622 if (enable
[i
] && ! usb_chip
[i
] &&
3623 (vid
[i
] == -1 || vid
[i
] == USB_ID_VENDOR(id
)) &&
3624 (pid
[i
] == -1 || pid
[i
] == USB_ID_PRODUCT(id
))) {
3625 if (snd_usb_audio_create(dev
, i
, quirk
, &chip
) < 0) {
3628 snd_card_set_dev(chip
->card
, &intf
->dev
);
3632 printk(KERN_ERR
"no available usb audio device\n");
3637 err
= 1; /* continue */
3638 if (quirk
&& quirk
->ifnum
!= QUIRK_NO_INTERFACE
) {
3639 /* need some special handlings */
3640 if ((err
= snd_usb_create_quirk(chip
, intf
, quirk
)) < 0)
3645 /* create normal USB audio interfaces */
3646 if (snd_usb_create_streams(chip
, ifnum
) < 0 ||
3647 snd_usb_create_mixer(chip
, ifnum
) < 0) {
3652 /* we are allowed to call snd_card_register() many times */
3653 if (snd_card_register(chip
->card
) < 0) {
3657 usb_chip
[chip
->index
] = chip
;
3658 chip
->num_interfaces
++;
3659 mutex_unlock(®ister_mutex
);
3663 if (chip
&& !chip
->num_interfaces
)
3664 snd_card_free(chip
->card
);
3665 mutex_unlock(®ister_mutex
);
3671 * we need to take care of counter, since disconnection can be called also
3672 * many times as well as usb_audio_probe().
3674 static void snd_usb_audio_disconnect(struct usb_device
*dev
, void *ptr
)
3676 struct snd_usb_audio
*chip
;
3677 struct snd_card
*card
;
3678 struct list_head
*p
;
3680 if (ptr
== (void *)-1L)
3685 mutex_lock(®ister_mutex
);
3687 chip
->num_interfaces
--;
3688 if (chip
->num_interfaces
<= 0) {
3689 snd_card_disconnect(card
);
3690 /* release the pcm resources */
3691 list_for_each(p
, &chip
->pcm_list
) {
3692 snd_usb_stream_disconnect(p
);
3694 /* release the midi resources */
3695 list_for_each(p
, &chip
->midi_list
) {
3696 snd_usbmidi_disconnect(p
);
3698 /* release mixer resources */
3699 list_for_each(p
, &chip
->mixer_list
) {
3700 snd_usb_mixer_disconnect(p
);
3702 usb_chip
[chip
->index
] = NULL
;
3703 mutex_unlock(®ister_mutex
);
3704 snd_card_free_when_closed(card
);
3706 mutex_unlock(®ister_mutex
);
3711 * new 2.5 USB kernel API
3713 static int usb_audio_probe(struct usb_interface
*intf
,
3714 const struct usb_device_id
*id
)
3717 chip
= snd_usb_audio_probe(interface_to_usbdev(intf
), intf
, id
);
3719 dev_set_drvdata(&intf
->dev
, chip
);
3725 static void usb_audio_disconnect(struct usb_interface
*intf
)
3727 snd_usb_audio_disconnect(interface_to_usbdev(intf
),
3728 dev_get_drvdata(&intf
->dev
));
3732 static int usb_audio_suspend(struct usb_interface
*intf
, pm_message_t message
)
3734 struct snd_usb_audio
*chip
= dev_get_drvdata(&intf
->dev
);
3735 struct list_head
*p
;
3736 struct snd_usb_stream
*as
;
3738 if (chip
== (void *)-1L)
3741 snd_power_change_state(chip
->card
, SNDRV_CTL_POWER_D3hot
);
3742 if (!chip
->num_suspended_intf
++) {
3743 list_for_each(p
, &chip
->pcm_list
) {
3744 as
= list_entry(p
, struct snd_usb_stream
, list
);
3745 snd_pcm_suspend_all(as
->pcm
);
3752 static int usb_audio_resume(struct usb_interface
*intf
)
3754 struct snd_usb_audio
*chip
= dev_get_drvdata(&intf
->dev
);
3756 if (chip
== (void *)-1L)
3758 if (--chip
->num_suspended_intf
)
3761 * ALSA leaves material resumption to user space
3765 snd_power_change_state(chip
->card
, SNDRV_CTL_POWER_D0
);
3769 #endif /* CONFIG_PM */
3771 static int __init
snd_usb_audio_init(void)
3773 if (nrpacks
< MIN_PACKS_URB
|| nrpacks
> MAX_PACKS
) {
3774 printk(KERN_WARNING
"invalid nrpacks value.\n");
3777 return usb_register(&usb_audio_driver
);
3781 static void __exit
snd_usb_audio_cleanup(void)
3783 usb_deregister(&usb_audio_driver
);
3786 module_init(snd_usb_audio_init
);
3787 module_exit(snd_usb_audio_cleanup
);