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