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