blob: e4b91045ca166829e0d43aa47ab9fc48c4317ed3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Main and PCM part
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 *
28 * NOTES:
29 *
30 * - async unlink should be used for avoiding the sleep inside lock.
31 * 2.4.22 usb-uhci seems buggy for async unlinking and results in
32 * oops. in such a cse, pass async_unlink=0 option.
33 * - the linked URBs would be preferred but not used so far because of
34 * the instability of unlinking.
35 * - type II is not supported properly. there is no device which supports
36 * this type *correctly*. SB extigy looks as if it supports, but it's
37 * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
38 */
39
40
41#include <sound/driver.h>
42#include <linux/bitops.h>
43#include <linux/init.h>
44#include <linux/list.h>
45#include <linux/slab.h>
46#include <linux/string.h>
47#include <linux/usb.h>
48#include <linux/moduleparam.h>
49#include <sound/core.h>
50#include <sound/info.h>
51#include <sound/pcm.h>
52#include <sound/pcm_params.h>
53#include <sound/initval.h>
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 */
66static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
67static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Vendor ID for this card */
68static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Product ID for this card */
69static int nrpacks = 4; /* max. number of packets per urb */
70static int async_unlink = 1;
71
72module_param_array(index, int, NULL, 0444);
73MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
74module_param_array(id, charp, NULL, 0444);
75MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
76module_param_array(enable, bool, NULL, 0444);
77MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
78module_param_array(vid, int, NULL, 0444);
79MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
80module_param_array(pid, int, NULL, 0444);
81MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
82module_param(nrpacks, int, 0444);
83MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
84module_param(async_unlink, bool, 0444);
85MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
86
87
88/*
89 * debug the h/w constraints
90 */
91/* #define HW_CONST_DEBUG */
92
93
94/*
95 *
96 */
97
98#define MAX_PACKS 10
99#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
100#define MAX_URBS 5 /* max. 20ms long packets */
101#define SYNC_URBS 2 /* always two urbs for sync */
102#define MIN_PACKS_URB 1 /* minimum 1 packet per urb */
103
104typedef struct snd_usb_substream snd_usb_substream_t;
105typedef struct snd_usb_stream snd_usb_stream_t;
106typedef struct snd_urb_ctx snd_urb_ctx_t;
107
108struct audioformat {
109 struct list_head list;
110 snd_pcm_format_t format; /* format type */
111 unsigned int channels; /* # channels */
112 unsigned int fmt_type; /* USB audio format type (1-3) */
113 unsigned int frame_size; /* samples per frame for non-audio */
114 int iface; /* interface number */
115 unsigned char altsetting; /* corresponding alternate setting */
116 unsigned char altset_idx; /* array index of altenate setting */
117 unsigned char attributes; /* corresponding attributes of cs endpoint */
118 unsigned char endpoint; /* endpoint */
119 unsigned char ep_attr; /* endpoint attributes */
120 unsigned int maxpacksize; /* max. packet size */
121 unsigned int rates; /* rate bitmasks */
122 unsigned int rate_min, rate_max; /* min/max rates */
123 unsigned int nr_rates; /* number of rate table entries */
124 unsigned int *rate_table; /* rate table */
125};
126
127struct snd_urb_ctx {
128 struct urb *urb;
129 snd_usb_substream_t *subs;
130 int index; /* index for urb array */
131 int packets; /* number of packets per urb */
132 int transfer; /* transferred size */
133 char *buf; /* buffer for capture */
134};
135
136struct snd_urb_ops {
137 int (*prepare)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
138 int (*retire)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
139 int (*prepare_sync)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
140 int (*retire_sync)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
141};
142
143struct snd_usb_substream {
144 snd_usb_stream_t *stream;
145 struct usb_device *dev;
146 snd_pcm_substream_t *pcm_substream;
147 int direction; /* playback or capture */
148 int interface; /* current interface */
149 int endpoint; /* assigned endpoint */
150 struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
151 unsigned int cur_rate; /* current rate (for hw_params callback) */
152 unsigned int period_bytes; /* current period bytes (for hw_params callback) */
153 unsigned int format; /* USB data format */
154 unsigned int datapipe; /* the data i/o pipe */
155 unsigned int syncpipe; /* 1 - async out or adaptive in */
156 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
157 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
158 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
159 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
160 unsigned int phase; /* phase accumulator */
161 unsigned int maxpacksize; /* max packet size in bytes */
162 unsigned int maxframesize; /* max packet size in frames */
163 unsigned int curpacksize; /* current packet size in bytes (for capture) */
164 unsigned int curframesize; /* current packet size in frames (for capture) */
165 unsigned int fill_max: 1; /* fill max packet size always */
166 unsigned int fmt_type; /* USB audio format type (1-3) */
167
168 unsigned int running: 1; /* running status */
169
170 unsigned int hwptr; /* free frame position in the buffer (only for playback) */
171 unsigned int hwptr_done; /* processed frame position in the buffer */
172 unsigned int transfer_sched; /* scheduled frames since last period (for playback) */
173 unsigned int transfer_done; /* processed frames since last period update */
174 unsigned long active_mask; /* bitmask of active urbs */
175 unsigned long unlink_mask; /* bitmask of unlinked urbs */
176
177 unsigned int nurbs; /* # urbs */
178 snd_urb_ctx_t dataurb[MAX_URBS]; /* data urb table */
179 snd_urb_ctx_t syncurb[SYNC_URBS]; /* sync urb table */
180 char syncbuf[SYNC_URBS * MAX_PACKS * 4]; /* sync buffer; it's so small - let's get static */
181 char *tmpbuf; /* temporary buffer for playback */
182
183 u64 formats; /* format bitmasks (all or'ed) */
184 unsigned int num_formats; /* number of supported audio formats (list) */
185 struct list_head fmt_list; /* format list */
186 spinlock_t lock;
187
188 struct snd_urb_ops ops; /* callbacks (must be filled at init) */
189};
190
191
192struct snd_usb_stream {
193 snd_usb_audio_t *chip;
194 snd_pcm_t *pcm;
195 int pcm_index;
196 unsigned int fmt_type; /* USB audio format type (1-3) */
197 snd_usb_substream_t substream[2];
198 struct list_head list;
199};
200
201
202/*
203 * we keep the snd_usb_audio_t instances by ourselves for merging
204 * the all interfaces on the same card as one sound device.
205 */
206
207static DECLARE_MUTEX(register_mutex);
208static snd_usb_audio_t *usb_chip[SNDRV_CARDS];
209
210
211/*
212 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
213 * this will overflow at approx 524 kHz
214 */
215inline static unsigned get_usb_full_speed_rate(unsigned int rate)
216{
217 return ((rate << 13) + 62) / 125;
218}
219
220/*
221 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
222 * this will overflow at approx 4 MHz
223 */
224inline static unsigned get_usb_high_speed_rate(unsigned int rate)
225{
226 return ((rate << 10) + 62) / 125;
227}
228
229/* convert our full speed USB rate into sampling rate in Hz */
230inline static unsigned get_full_speed_hz(unsigned int usb_rate)
231{
232 return (usb_rate * 125 + (1 << 12)) >> 13;
233}
234
235/* convert our high speed USB rate into sampling rate in Hz */
236inline static unsigned get_high_speed_hz(unsigned int usb_rate)
237{
238 return (usb_rate * 125 + (1 << 9)) >> 10;
239}
240
241
242/*
243 * prepare urb for full speed capture sync pipe
244 *
245 * fill the length and offset of each urb descriptor.
246 * the fixed 10.14 frequency is passed through the pipe.
247 */
248static int prepare_capture_sync_urb(snd_usb_substream_t *subs,
249 snd_pcm_runtime_t *runtime,
250 struct urb *urb)
251{
252 unsigned char *cp = urb->transfer_buffer;
253 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
254 int i, offs;
255
256 urb->number_of_packets = ctx->packets;
257 urb->dev = ctx->subs->dev; /* we need to set this at each time */
258 for (i = offs = 0; i < urb->number_of_packets; i++, offs += 4, cp += 4) {
259 urb->iso_frame_desc[i].length = 3;
260 urb->iso_frame_desc[i].offset = offs;
261 cp[0] = subs->freqn >> 2;
262 cp[1] = subs->freqn >> 10;
263 cp[2] = subs->freqn >> 18;
264 }
265 return 0;
266}
267
268/*
269 * prepare urb for high speed capture sync pipe
270 *
271 * fill the length and offset of each urb descriptor.
272 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
273 */
274static int prepare_capture_sync_urb_hs(snd_usb_substream_t *subs,
275 snd_pcm_runtime_t *runtime,
276 struct urb *urb)
277{
278 unsigned char *cp = urb->transfer_buffer;
279 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
280 int i, offs;
281
282 urb->number_of_packets = ctx->packets;
283 urb->dev = ctx->subs->dev; /* we need to set this at each time */
284 for (i = offs = 0; i < urb->number_of_packets; i++, offs += 4, cp += 4) {
285 urb->iso_frame_desc[i].length = 4;
286 urb->iso_frame_desc[i].offset = offs;
287 cp[0] = subs->freqn;
288 cp[1] = subs->freqn >> 8;
289 cp[2] = subs->freqn >> 16;
290 cp[3] = subs->freqn >> 24;
291 }
292 return 0;
293}
294
295/*
296 * process after capture sync complete
297 * - nothing to do
298 */
299static int retire_capture_sync_urb(snd_usb_substream_t *subs,
300 snd_pcm_runtime_t *runtime,
301 struct urb *urb)
302{
303 return 0;
304}
305
306/*
307 * prepare urb for capture data pipe
308 *
309 * fill the offset and length of each descriptor.
310 *
311 * we use a temporary buffer to write the captured data.
312 * since the length of written data is determined by host, we cannot
313 * write onto the pcm buffer directly... the data is thus copied
314 * later at complete callback to the global buffer.
315 */
316static int prepare_capture_urb(snd_usb_substream_t *subs,
317 snd_pcm_runtime_t *runtime,
318 struct urb *urb)
319{
320 int i, offs;
321 unsigned long flags;
322 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
323
324 offs = 0;
325 urb->dev = ctx->subs->dev; /* we need to set this at each time */
326 urb->number_of_packets = 0;
327 spin_lock_irqsave(&subs->lock, flags);
328 for (i = 0; i < ctx->packets; i++) {
329 urb->iso_frame_desc[i].offset = offs;
330 urb->iso_frame_desc[i].length = subs->curpacksize;
331 offs += subs->curpacksize;
332 urb->number_of_packets++;
333 subs->transfer_sched += subs->curframesize;
334 if (subs->transfer_sched >= runtime->period_size) {
335 subs->transfer_sched -= runtime->period_size;
336 break;
337 }
338 }
339 spin_unlock_irqrestore(&subs->lock, flags);
340 urb->transfer_buffer = ctx->buf;
341 urb->transfer_buffer_length = offs;
342#if 0 // for check
343 if (! urb->bandwidth) {
344 int bustime;
345 bustime = usb_check_bandwidth(urb->dev, urb);
346 if (bustime < 0)
347 return bustime;
348 printk("urb %d: bandwidth = %d (packets = %d)\n", ctx->index, bustime, urb->number_of_packets);
349 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
350 }
351#endif // for check
352 return 0;
353}
354
355/*
356 * process after capture complete
357 *
358 * copy the data from each desctiptor to the pcm buffer, and
359 * update the current position.
360 */
361static int retire_capture_urb(snd_usb_substream_t *subs,
362 snd_pcm_runtime_t *runtime,
363 struct urb *urb)
364{
365 unsigned long flags;
366 unsigned char *cp;
367 int i;
368 unsigned int stride, len, oldptr;
369
370 stride = runtime->frame_bits >> 3;
371
372 for (i = 0; i < urb->number_of_packets; i++) {
373 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
374 if (urb->iso_frame_desc[i].status) {
375 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
376 // continue;
377 }
378 len = urb->iso_frame_desc[i].actual_length / stride;
379 if (! len)
380 continue;
381 /* update the current pointer */
382 spin_lock_irqsave(&subs->lock, flags);
383 oldptr = subs->hwptr_done;
384 subs->hwptr_done += len;
385 if (subs->hwptr_done >= runtime->buffer_size)
386 subs->hwptr_done -= runtime->buffer_size;
387 subs->transfer_done += len;
388 spin_unlock_irqrestore(&subs->lock, flags);
389 /* copy a data chunk */
390 if (oldptr + len > runtime->buffer_size) {
391 unsigned int cnt = runtime->buffer_size - oldptr;
392 unsigned int blen = cnt * stride;
393 memcpy(runtime->dma_area + oldptr * stride, cp, blen);
394 memcpy(runtime->dma_area, cp + blen, len * stride - blen);
395 } else {
396 memcpy(runtime->dma_area + oldptr * stride, cp, len * stride);
397 }
398 /* update the pointer, call callback if necessary */
399 spin_lock_irqsave(&subs->lock, flags);
400 if (subs->transfer_done >= runtime->period_size) {
401 subs->transfer_done -= runtime->period_size;
402 spin_unlock_irqrestore(&subs->lock, flags);
403 snd_pcm_period_elapsed(subs->pcm_substream);
404 } else
405 spin_unlock_irqrestore(&subs->lock, flags);
406 }
407 return 0;
408}
409
410
411/*
412 * prepare urb for full speed playback sync pipe
413 *
414 * set up the offset and length to receive the current frequency.
415 */
416
417static int prepare_playback_sync_urb(snd_usb_substream_t *subs,
418 snd_pcm_runtime_t *runtime,
419 struct urb *urb)
420{
421 int i, offs;
422 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
423
424 urb->number_of_packets = ctx->packets;
425 urb->dev = ctx->subs->dev; /* we need to set this at each time */
426 for (i = offs = 0; i < urb->number_of_packets; i++, offs += 4) {
427 urb->iso_frame_desc[i].length = 3;
428 urb->iso_frame_desc[i].offset = offs;
429 }
430 return 0;
431}
432
433/*
434 * prepare urb for high speed playback sync pipe
435 *
436 * set up the offset and length to receive the current frequency.
437 */
438
439static int prepare_playback_sync_urb_hs(snd_usb_substream_t *subs,
440 snd_pcm_runtime_t *runtime,
441 struct urb *urb)
442{
443 int i, offs;
444 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
445
446 urb->number_of_packets = ctx->packets;
447 urb->dev = ctx->subs->dev; /* we need to set this at each time */
448 for (i = offs = 0; i < urb->number_of_packets; i++, offs += 4) {
449 urb->iso_frame_desc[i].length = 4;
450 urb->iso_frame_desc[i].offset = offs;
451 }
452 return 0;
453}
454
455/*
456 * process after full speed playback sync complete
457 *
458 * retrieve the current 10.14 frequency from pipe, and set it.
459 * the value is referred in prepare_playback_urb().
460 */
461static int retire_playback_sync_urb(snd_usb_substream_t *subs,
462 snd_pcm_runtime_t *runtime,
463 struct urb *urb)
464{
465 int i;
466 unsigned int f, found;
467 unsigned char *cp = urb->transfer_buffer;
468 unsigned long flags;
469
470 found = 0;
471 for (i = 0; i < urb->number_of_packets; i++, cp += 4) {
472 if (urb->iso_frame_desc[i].status ||
473 urb->iso_frame_desc[i].actual_length < 3)
474 continue;
475 f = combine_triple(cp) << 2;
476#if 0
477 if (f < subs->freqn - (subs->freqn>>3) || f > subs->freqmax) {
478 snd_printd(KERN_WARNING "requested frequency %d (%u,%03uHz) out of range (current nominal %d (%u,%03uHz))\n",
479 f, f >> 14, (f & ((1 << 14) - 1) * 1000) / ((1 << 14) - 1),
480 subs->freqn, subs->freqn >> 14, (subs->freqn & ((1 << 14) - 1) * 1000) / ((1 << 14) - 1));
481 continue;
482 }
483#endif
484 found = f;
485 }
486 if (found) {
487 spin_lock_irqsave(&subs->lock, flags);
488 subs->freqm = found;
489 spin_unlock_irqrestore(&subs->lock, flags);
490 }
491
492 return 0;
493}
494
495/*
496 * process after high speed playback sync complete
497 *
498 * retrieve the current 12.13 frequency from pipe, and set it.
499 * the value is referred in prepare_playback_urb().
500 */
501static int retire_playback_sync_urb_hs(snd_usb_substream_t *subs,
502 snd_pcm_runtime_t *runtime,
503 struct urb *urb)
504{
505 int i;
506 unsigned int found;
507 unsigned char *cp = urb->transfer_buffer;
508 unsigned long flags;
509
510 found = 0;
511 for (i = 0; i < urb->number_of_packets; i++, cp += 4) {
512 if (urb->iso_frame_desc[i].status ||
513 urb->iso_frame_desc[i].actual_length < 4)
514 continue;
515 found = combine_quad(cp) & 0x0fffffff;
516 }
517 if (found) {
518 spin_lock_irqsave(&subs->lock, flags);
519 subs->freqm = found;
520 spin_unlock_irqrestore(&subs->lock, flags);
521 }
522
523 return 0;
524}
525
526/*
527 * prepare urb for playback data pipe
528 *
529 * we copy the data directly from the pcm buffer.
530 * the current position to be copied is held in hwptr field.
531 * since a urb can handle only a single linear buffer, if the total
532 * transferred area overflows the buffer boundary, we cannot send
533 * it directly from the buffer. thus the data is once copied to
534 * a temporary buffer and urb points to that.
535 */
536static int prepare_playback_urb(snd_usb_substream_t *subs,
537 snd_pcm_runtime_t *runtime,
538 struct urb *urb)
539{
540 int i, stride, offs;
541 unsigned int counts;
542 unsigned long flags;
543 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
544
545 stride = runtime->frame_bits >> 3;
546
547 offs = 0;
548 urb->dev = ctx->subs->dev; /* we need to set this at each time */
549 urb->number_of_packets = 0;
550 spin_lock_irqsave(&subs->lock, flags);
551 for (i = 0; i < ctx->packets; i++) {
552 /* calculate the size of a packet */
553 if (subs->fill_max)
554 counts = subs->maxframesize; /* fixed */
555 else {
556 subs->phase = (subs->phase & 0xffff) + subs->freqm;
557 counts = subs->phase >> 16;
558 if (counts > subs->maxframesize)
559 counts = subs->maxframesize;
560 }
561 /* set up descriptor */
562 urb->iso_frame_desc[i].offset = offs * stride;
563 urb->iso_frame_desc[i].length = counts * stride;
564 offs += counts;
565 urb->number_of_packets++;
566 subs->transfer_sched += counts;
567 if (subs->transfer_sched >= runtime->period_size) {
568 subs->transfer_sched -= runtime->period_size;
569 if (subs->fmt_type == USB_FORMAT_TYPE_II) {
570 if (subs->transfer_sched > 0) {
571 /* FIXME: fill-max mode is not supported yet */
572 offs -= subs->transfer_sched;
573 counts -= subs->transfer_sched;
574 urb->iso_frame_desc[i].length = counts * stride;
575 subs->transfer_sched = 0;
576 }
577 i++;
578 if (i < ctx->packets) {
579 /* add a transfer delimiter */
580 urb->iso_frame_desc[i].offset = offs * stride;
581 urb->iso_frame_desc[i].length = 0;
582 urb->number_of_packets++;
583 }
584 }
585 break;
586 }
587 }
588 if (subs->hwptr + offs > runtime->buffer_size) {
589 /* err, the transferred area goes over buffer boundary.
590 * copy the data to the temp buffer.
591 */
592 int len;
593 len = runtime->buffer_size - subs->hwptr;
594 urb->transfer_buffer = subs->tmpbuf;
595 memcpy(subs->tmpbuf, runtime->dma_area + subs->hwptr * stride, len * stride);
596 memcpy(subs->tmpbuf + len * stride, runtime->dma_area, (offs - len) * stride);
597 subs->hwptr += offs;
598 subs->hwptr -= runtime->buffer_size;
599 } else {
600 /* set the buffer pointer */
601 urb->transfer_buffer = runtime->dma_area + subs->hwptr * stride;
602 subs->hwptr += offs;
603 }
604 spin_unlock_irqrestore(&subs->lock, flags);
605 urb->transfer_buffer_length = offs * stride;
606 ctx->transfer = offs;
607
608 return 0;
609}
610
611/*
612 * process after playback data complete
613 *
614 * update the current position and call callback if a period is processed.
615 */
616static int retire_playback_urb(snd_usb_substream_t *subs,
617 snd_pcm_runtime_t *runtime,
618 struct urb *urb)
619{
620 unsigned long flags;
621 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
622
623 spin_lock_irqsave(&subs->lock, flags);
624 subs->transfer_done += ctx->transfer;
625 subs->hwptr_done += ctx->transfer;
626 ctx->transfer = 0;
627 if (subs->hwptr_done >= runtime->buffer_size)
628 subs->hwptr_done -= runtime->buffer_size;
629 if (subs->transfer_done >= runtime->period_size) {
630 subs->transfer_done -= runtime->period_size;
631 spin_unlock_irqrestore(&subs->lock, flags);
632 snd_pcm_period_elapsed(subs->pcm_substream);
633 } else
634 spin_unlock_irqrestore(&subs->lock, flags);
635 return 0;
636}
637
638
639/*
640 */
641static struct snd_urb_ops audio_urb_ops[2] = {
642 {
643 .prepare = prepare_playback_urb,
644 .retire = retire_playback_urb,
645 .prepare_sync = prepare_playback_sync_urb,
646 .retire_sync = retire_playback_sync_urb,
647 },
648 {
649 .prepare = prepare_capture_urb,
650 .retire = retire_capture_urb,
651 .prepare_sync = prepare_capture_sync_urb,
652 .retire_sync = retire_capture_sync_urb,
653 },
654};
655
656static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
657 {
658 .prepare = prepare_playback_urb,
659 .retire = retire_playback_urb,
660 .prepare_sync = prepare_playback_sync_urb_hs,
661 .retire_sync = retire_playback_sync_urb_hs,
662 },
663 {
664 .prepare = prepare_capture_urb,
665 .retire = retire_capture_urb,
666 .prepare_sync = prepare_capture_sync_urb_hs,
667 .retire_sync = retire_capture_sync_urb,
668 },
669};
670
671/*
672 * complete callback from data urb
673 */
674static void snd_complete_urb(struct urb *urb, struct pt_regs *regs)
675{
676 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
677 snd_usb_substream_t *subs = ctx->subs;
678 snd_pcm_substream_t *substream = ctx->subs->pcm_substream;
679 int err = 0;
680
681 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
682 ! subs->running || /* can be stopped during retire callback */
683 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
684 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
685 clear_bit(ctx->index, &subs->active_mask);
686 if (err < 0) {
687 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
688 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
689 }
690 }
691}
692
693
694/*
695 * complete callback from sync urb
696 */
697static void snd_complete_sync_urb(struct urb *urb, struct pt_regs *regs)
698{
699 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
700 snd_usb_substream_t *subs = ctx->subs;
701 snd_pcm_substream_t *substream = ctx->subs->pcm_substream;
702 int err = 0;
703
704 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
705 ! subs->running || /* can be stopped during retire callback */
706 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
707 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
708 clear_bit(ctx->index + 16, &subs->active_mask);
709 if (err < 0) {
710 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
711 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
712 }
713 }
714}
715
716
717/*
718 * unlink active urbs.
719 */
720static int deactivate_urbs(snd_usb_substream_t *subs, int force, int can_sleep)
721{
722 unsigned int i;
723 int async;
724
725 subs->running = 0;
726
727 if (!force && subs->stream->chip->shutdown) /* to be sure... */
728 return -EBADFD;
729
730 async = !can_sleep && async_unlink;
731
732 if (! async && in_interrupt())
733 return 0;
734
735 for (i = 0; i < subs->nurbs; i++) {
736 if (test_bit(i, &subs->active_mask)) {
737 if (! test_and_set_bit(i, &subs->unlink_mask)) {
738 struct urb *u = subs->dataurb[i].urb;
739 if (async) {
740 u->transfer_flags |= URB_ASYNC_UNLINK;
741 usb_unlink_urb(u);
742 } else
743 usb_kill_urb(u);
744 }
745 }
746 }
747 if (subs->syncpipe) {
748 for (i = 0; i < SYNC_URBS; i++) {
749 if (test_bit(i+16, &subs->active_mask)) {
750 if (! test_and_set_bit(i+16, &subs->unlink_mask)) {
751 struct urb *u = subs->syncurb[i].urb;
752 if (async) {
753 u->transfer_flags |= URB_ASYNC_UNLINK;
754 usb_unlink_urb(u);
755 } else
756 usb_kill_urb(u);
757 }
758 }
759 }
760 }
761 return 0;
762}
763
764
765/*
766 * set up and start data/sync urbs
767 */
768static int start_urbs(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime)
769{
770 unsigned int i;
771 int err;
772
773 if (subs->stream->chip->shutdown)
774 return -EBADFD;
775
776 for (i = 0; i < subs->nurbs; i++) {
777 snd_assert(subs->dataurb[i].urb, return -EINVAL);
778 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
779 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
780 goto __error;
781 }
782 }
783 if (subs->syncpipe) {
784 for (i = 0; i < SYNC_URBS; i++) {
785 snd_assert(subs->syncurb[i].urb, return -EINVAL);
786 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
787 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
788 goto __error;
789 }
790 }
791 }
792
793 subs->active_mask = 0;
794 subs->unlink_mask = 0;
795 subs->running = 1;
796 for (i = 0; i < subs->nurbs; i++) {
797 if ((err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC)) < 0) {
798 snd_printk(KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
799 goto __error;
800 }
801 set_bit(i, &subs->active_mask);
802 }
803 if (subs->syncpipe) {
804 for (i = 0; i < SYNC_URBS; i++) {
805 if ((err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC)) < 0) {
806 snd_printk(KERN_ERR "cannot submit syncpipe for urb %d, err = %d\n", i, err);
807 goto __error;
808 }
809 set_bit(i + 16, &subs->active_mask);
810 }
811 }
812 return 0;
813
814 __error:
815 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
816 deactivate_urbs(subs, 0, 0);
817 return -EPIPE;
818}
819
820
821/*
822 * wait until all urbs are processed.
823 */
824static int wait_clear_urbs(snd_usb_substream_t *subs)
825{
826 int timeout = HZ;
827 unsigned int i;
828 int alive;
829
830 do {
831 alive = 0;
832 for (i = 0; i < subs->nurbs; i++) {
833 if (test_bit(i, &subs->active_mask))
834 alive++;
835 }
836 if (subs->syncpipe) {
837 for (i = 0; i < SYNC_URBS; i++) {
838 if (test_bit(i + 16, &subs->active_mask))
839 alive++;
840 }
841 }
842 if (! alive)
843 break;
844 set_current_state(TASK_UNINTERRUPTIBLE);
845 schedule_timeout(1);
846 } while (--timeout > 0);
847 if (alive)
848 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
849 return 0;
850}
851
852
853/*
854 * return the current pcm pointer. just return the hwptr_done value.
855 */
856static snd_pcm_uframes_t snd_usb_pcm_pointer(snd_pcm_substream_t *substream)
857{
858 snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
859 return subs->hwptr_done;
860}
861
862
863/*
864 * start/stop substream
865 */
866static int snd_usb_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
867{
868 snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
869 int err;
870
871 switch (cmd) {
872 case SNDRV_PCM_TRIGGER_START:
873 err = start_urbs(subs, substream->runtime);
874 break;
875 case SNDRV_PCM_TRIGGER_STOP:
876 err = deactivate_urbs(subs, 0, 0);
877 break;
878 default:
879 err = -EINVAL;
880 break;
881 }
882 return err < 0 ? err : 0;
883}
884
885
886/*
887 * release a urb data
888 */
889static void release_urb_ctx(snd_urb_ctx_t *u)
890{
891 if (u->urb) {
892 usb_free_urb(u->urb);
893 u->urb = NULL;
894 }
895 if (u->buf) {
896 kfree(u->buf);
897 u->buf = NULL;
898 }
899}
900
901/*
902 * release a substream
903 */
904static void release_substream_urbs(snd_usb_substream_t *subs, int force)
905{
906 int i;
907
908 /* stop urbs (to be sure) */
909 deactivate_urbs(subs, force, 1);
910 wait_clear_urbs(subs);
911
912 for (i = 0; i < MAX_URBS; i++)
913 release_urb_ctx(&subs->dataurb[i]);
914 for (i = 0; i < SYNC_URBS; i++)
915 release_urb_ctx(&subs->syncurb[i]);
916 if (subs->tmpbuf) {
917 kfree(subs->tmpbuf);
918 subs->tmpbuf = NULL;
919 }
920 subs->nurbs = 0;
921}
922
923/*
924 * initialize a substream for plaback/capture
925 */
926static int init_substream_urbs(snd_usb_substream_t *subs, unsigned int period_bytes,
927 unsigned int rate, unsigned int frame_bits)
928{
929 unsigned int maxsize, n, i;
930 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
931 unsigned int npacks[MAX_URBS], urb_packs, total_packs;
932
933 /* calculate the frequency in 16.16 format */
934 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
935 subs->freqn = get_usb_full_speed_rate(rate);
936 else
937 subs->freqn = get_usb_high_speed_rate(rate);
938 subs->freqm = subs->freqn;
939 subs->freqmax = subs->freqn + (subs->freqn >> 2); /* max. allowed frequency */
940 subs->phase = 0;
941
942 /* calculate the max. size of packet */
943 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3)) >> 16;
944 if (subs->maxpacksize && maxsize > subs->maxpacksize) {
945 //snd_printd(KERN_DEBUG "maxsize %d is greater than defined size %d\n",
946 // maxsize, subs->maxpacksize);
947 maxsize = subs->maxpacksize;
948 }
949
950 if (subs->fill_max)
951 subs->curpacksize = subs->maxpacksize;
952 else
953 subs->curpacksize = maxsize;
954
955 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
956 urb_packs = nrpacks;
957 else
958 urb_packs = nrpacks * 8;
959
960 /* allocate a temporary buffer for playback */
961 if (is_playback) {
962 subs->tmpbuf = kmalloc(maxsize * urb_packs, GFP_KERNEL);
963 if (! subs->tmpbuf) {
964 snd_printk(KERN_ERR "cannot malloc tmpbuf\n");
965 return -ENOMEM;
966 }
967 }
968
969 /* decide how many packets to be used */
970 total_packs = (period_bytes + maxsize - 1) / maxsize;
971 if (total_packs < 2 * MIN_PACKS_URB)
972 total_packs = 2 * MIN_PACKS_URB;
973 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
974 if (subs->nurbs > MAX_URBS) {
975 /* too much... */
976 subs->nurbs = MAX_URBS;
977 total_packs = MAX_URBS * urb_packs;
978 }
979 n = total_packs;
980 for (i = 0; i < subs->nurbs; i++) {
981 npacks[i] = n > urb_packs ? urb_packs : n;
982 n -= urb_packs;
983 }
984 if (subs->nurbs <= 1) {
985 /* too little - we need at least two packets
986 * to ensure contiguous playback/capture
987 */
988 subs->nurbs = 2;
989 npacks[0] = (total_packs + 1) / 2;
990 npacks[1] = total_packs - npacks[0];
991 } else if (npacks[subs->nurbs-1] < MIN_PACKS_URB) {
992 /* the last packet is too small.. */
993 if (subs->nurbs > 2) {
994 /* merge to the first one */
995 npacks[0] += npacks[subs->nurbs - 1];
996 subs->nurbs--;
997 } else {
998 /* divide to two */
999 subs->nurbs = 2;
1000 npacks[0] = (total_packs + 1) / 2;
1001 npacks[1] = total_packs - npacks[0];
1002 }
1003 }
1004
1005 /* allocate and initialize data urbs */
1006 for (i = 0; i < subs->nurbs; i++) {
1007 snd_urb_ctx_t *u = &subs->dataurb[i];
1008 u->index = i;
1009 u->subs = subs;
1010 u->transfer = 0;
1011 u->packets = npacks[i];
1012 if (subs->fmt_type == USB_FORMAT_TYPE_II)
1013 u->packets++; /* for transfer delimiter */
1014 if (! is_playback) {
1015 /* allocate a capture buffer per urb */
1016 u->buf = kmalloc(maxsize * u->packets, GFP_KERNEL);
1017 if (! u->buf) {
1018 release_substream_urbs(subs, 0);
1019 return -ENOMEM;
1020 }
1021 }
1022 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1023 if (! u->urb) {
1024 release_substream_urbs(subs, 0);
1025 return -ENOMEM;
1026 }
1027 u->urb->dev = subs->dev;
1028 u->urb->pipe = subs->datapipe;
1029 u->urb->transfer_flags = URB_ISO_ASAP;
1030 u->urb->number_of_packets = u->packets;
1031 u->urb->interval = 1;
1032 u->urb->context = u;
1033 u->urb->complete = snd_usb_complete_callback(snd_complete_urb);
1034 }
1035
1036 if (subs->syncpipe) {
1037 /* allocate and initialize sync urbs */
1038 for (i = 0; i < SYNC_URBS; i++) {
1039 snd_urb_ctx_t *u = &subs->syncurb[i];
1040 u->index = i;
1041 u->subs = subs;
1042 u->packets = nrpacks;
1043 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1044 if (! u->urb) {
1045 release_substream_urbs(subs, 0);
1046 return -ENOMEM;
1047 }
1048 u->urb->transfer_buffer = subs->syncbuf + i * nrpacks * 4;
1049 u->urb->transfer_buffer_length = nrpacks * 4;
1050 u->urb->dev = subs->dev;
1051 u->urb->pipe = subs->syncpipe;
1052 u->urb->transfer_flags = URB_ISO_ASAP;
1053 u->urb->number_of_packets = u->packets;
1054 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
1055 u->urb->interval = 8;
1056 else
1057 u->urb->interval = 1;
1058 u->urb->context = u;
1059 u->urb->complete = snd_usb_complete_callback(snd_complete_sync_urb);
1060 }
1061 }
1062 return 0;
1063}
1064
1065
1066/*
1067 * find a matching audio format
1068 */
1069static struct audioformat *find_format(snd_usb_substream_t *subs, unsigned int format,
1070 unsigned int rate, unsigned int channels)
1071{
1072 struct list_head *p;
1073 struct audioformat *found = NULL;
1074 int cur_attr = 0, attr;
1075
1076 list_for_each(p, &subs->fmt_list) {
1077 struct audioformat *fp;
1078 fp = list_entry(p, struct audioformat, list);
1079 if (fp->format != format || fp->channels != channels)
1080 continue;
1081 if (rate < fp->rate_min || rate > fp->rate_max)
1082 continue;
1083 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
1084 unsigned int i;
1085 for (i = 0; i < fp->nr_rates; i++)
1086 if (fp->rate_table[i] == rate)
1087 break;
1088 if (i >= fp->nr_rates)
1089 continue;
1090 }
1091 attr = fp->ep_attr & EP_ATTR_MASK;
1092 if (! found) {
1093 found = fp;
1094 cur_attr = attr;
1095 continue;
1096 }
1097 /* avoid async out and adaptive in if the other method
1098 * supports the same format.
1099 * this is a workaround for the case like
1100 * M-audio audiophile USB.
1101 */
1102 if (attr != cur_attr) {
1103 if ((attr == EP_ATTR_ASYNC &&
1104 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1105 (attr == EP_ATTR_ADAPTIVE &&
1106 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
1107 continue;
1108 if ((cur_attr == EP_ATTR_ASYNC &&
1109 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1110 (cur_attr == EP_ATTR_ADAPTIVE &&
1111 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
1112 found = fp;
1113 cur_attr = attr;
1114 continue;
1115 }
1116 }
1117 /* find the format with the largest max. packet size */
1118 if (fp->maxpacksize > found->maxpacksize) {
1119 found = fp;
1120 cur_attr = attr;
1121 }
1122 }
1123 return found;
1124}
1125
1126
1127/*
1128 * initialize the picth control and sample rate
1129 */
1130static int init_usb_pitch(struct usb_device *dev, int iface,
1131 struct usb_host_interface *alts,
1132 struct audioformat *fmt)
1133{
1134 unsigned int ep;
1135 unsigned char data[1];
1136 int err;
1137
1138 ep = get_endpoint(alts, 0)->bEndpointAddress;
1139 /* if endpoint has pitch control, enable it */
1140 if (fmt->attributes & EP_CS_ATTR_PITCH_CONTROL) {
1141 data[0] = 1;
1142 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1143 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1144 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1145 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
1146 dev->devnum, iface, ep);
1147 return err;
1148 }
1149 }
1150 return 0;
1151}
1152
1153static int init_usb_sample_rate(struct usb_device *dev, int iface,
1154 struct usb_host_interface *alts,
1155 struct audioformat *fmt, int rate)
1156{
1157 unsigned int ep;
1158 unsigned char data[3];
1159 int err;
1160
1161 ep = get_endpoint(alts, 0)->bEndpointAddress;
1162 /* if endpoint has sampling rate control, set it */
1163 if (fmt->attributes & EP_CS_ATTR_SAMPLE_RATE) {
1164 int crate;
1165 data[0] = rate;
1166 data[1] = rate >> 8;
1167 data[2] = rate >> 16;
1168 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1169 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1170 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1171 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep 0x%x\n",
1172 dev->devnum, iface, fmt->altsetting, rate, ep);
1173 return err;
1174 }
1175 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR,
1176 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1177 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1178 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep 0x%x\n",
1179 dev->devnum, iface, fmt->altsetting, ep);
1180 return 0; /* some devices don't support reading */
1181 }
1182 crate = data[0] | (data[1] << 8) | (data[2] << 16);
1183 if (crate != rate) {
1184 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
1185 // runtime->rate = crate;
1186 }
1187 }
1188 return 0;
1189}
1190
1191/*
1192 * find a matching format and set up the interface
1193 */
1194static int set_format(snd_usb_substream_t *subs, struct audioformat *fmt)
1195{
1196 struct usb_device *dev = subs->dev;
1197 struct usb_host_interface *alts;
1198 struct usb_interface_descriptor *altsd;
1199 struct usb_interface *iface;
1200 unsigned int ep, attr;
1201 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1202 int err;
1203
1204 iface = usb_ifnum_to_if(dev, fmt->iface);
1205 snd_assert(iface, return -EINVAL);
1206 alts = &iface->altsetting[fmt->altset_idx];
1207 altsd = get_iface_desc(alts);
1208 snd_assert(altsd->bAlternateSetting == fmt->altsetting, return -EINVAL);
1209
1210 if (fmt == subs->cur_audiofmt)
1211 return 0;
1212
1213 /* close the old interface */
1214 if (subs->interface >= 0 && subs->interface != fmt->iface) {
1215 usb_set_interface(subs->dev, subs->interface, 0);
1216 subs->interface = -1;
1217 subs->format = 0;
1218 }
1219
1220 /* set interface */
1221 if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
1222 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
1223 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
1224 dev->devnum, fmt->iface, fmt->altsetting);
1225 return -EIO;
1226 }
1227 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
1228 subs->interface = fmt->iface;
1229 subs->format = fmt->altset_idx;
1230 }
1231
1232 /* create a data pipe */
1233 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
1234 if (is_playback)
1235 subs->datapipe = usb_sndisocpipe(dev, ep);
1236 else
1237 subs->datapipe = usb_rcvisocpipe(dev, ep);
1238 subs->syncpipe = subs->syncinterval = 0;
1239 subs->maxpacksize = fmt->maxpacksize;
1240 subs->fill_max = 0;
1241
1242 /* we need a sync pipe in async OUT or adaptive IN mode */
1243 /* check the number of EP, since some devices have broken
1244 * descriptors which fool us. if it has only one EP,
1245 * assume it as adaptive-out or sync-in.
1246 */
1247 attr = fmt->ep_attr & EP_ATTR_MASK;
1248 if (((is_playback && attr == EP_ATTR_ASYNC) ||
1249 (! is_playback && attr == EP_ATTR_ADAPTIVE)) &&
1250 altsd->bNumEndpoints >= 2) {
1251 /* check sync-pipe endpoint */
1252 /* ... and check descriptor size before accessing bSynchAddress
1253 because there is a version of the SB Audigy 2 NX firmware lacking
1254 the audio fields in the endpoint descriptors */
1255 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
1256 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1257 get_endpoint(alts, 1)->bSynchAddress != 0)) {
1258 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1259 dev->devnum, fmt->iface, fmt->altsetting);
1260 return -EINVAL;
1261 }
1262 ep = get_endpoint(alts, 1)->bEndpointAddress;
1263 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1264 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
1265 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
1266 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1267 dev->devnum, fmt->iface, fmt->altsetting);
1268 return -EINVAL;
1269 }
1270 ep &= USB_ENDPOINT_NUMBER_MASK;
1271 if (is_playback)
1272 subs->syncpipe = usb_rcvisocpipe(dev, ep);
1273 else
1274 subs->syncpipe = usb_sndisocpipe(dev, ep);
1275 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
1276 }
1277
1278 /* always fill max packet size */
1279 if (fmt->attributes & EP_CS_ATTR_FILL_MAX)
1280 subs->fill_max = 1;
1281
1282 if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
1283 return err;
1284
1285 subs->cur_audiofmt = fmt;
1286
1287#if 0
1288 printk("setting done: format = %d, rate = %d, channels = %d\n",
1289 fmt->format, fmt->rate, fmt->channels);
1290 printk(" datapipe = 0x%0x, syncpipe = 0x%0x\n",
1291 subs->datapipe, subs->syncpipe);
1292#endif
1293
1294 return 0;
1295}
1296
1297/*
1298 * hw_params callback
1299 *
1300 * allocate a buffer and set the given audio format.
1301 *
1302 * so far we use a physically linear buffer although packetize transfer
1303 * doesn't need a continuous area.
1304 * if sg buffer is supported on the later version of alsa, we'll follow
1305 * that.
1306 */
1307static int snd_usb_hw_params(snd_pcm_substream_t *substream,
1308 snd_pcm_hw_params_t *hw_params)
1309{
1310 snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
1311 struct audioformat *fmt;
1312 unsigned int channels, rate, format;
1313 int ret, changed;
1314
1315 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1316 if (ret < 0)
1317 return ret;
1318
1319 format = params_format(hw_params);
1320 rate = params_rate(hw_params);
1321 channels = params_channels(hw_params);
1322 fmt = find_format(subs, format, rate, channels);
1323 if (! fmt) {
1324 snd_printd(KERN_DEBUG "cannot set format: format = %s, rate = %d, channels = %d\n",
1325 snd_pcm_format_name(format), rate, channels);
1326 return -EINVAL;
1327 }
1328
1329 changed = subs->cur_audiofmt != fmt ||
1330 subs->period_bytes != params_period_bytes(hw_params) ||
1331 subs->cur_rate != rate;
1332 if ((ret = set_format(subs, fmt)) < 0)
1333 return ret;
1334
1335 if (subs->cur_rate != rate) {
1336 struct usb_host_interface *alts;
1337 struct usb_interface *iface;
1338 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
1339 alts = &iface->altsetting[fmt->altset_idx];
1340 ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
1341 if (ret < 0)
1342 return ret;
1343 subs->cur_rate = rate;
1344 }
1345
1346 if (changed) {
1347 /* format changed */
1348 release_substream_urbs(subs, 0);
1349 /* influenced: period_bytes, channels, rate, format, */
1350 ret = init_substream_urbs(subs, params_period_bytes(hw_params),
1351 params_rate(hw_params),
1352 snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
1353 }
1354
1355 return ret;
1356}
1357
1358/*
1359 * hw_free callback
1360 *
1361 * reset the audio format and release the buffer
1362 */
1363static int snd_usb_hw_free(snd_pcm_substream_t *substream)
1364{
1365 snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
1366
1367 subs->cur_audiofmt = NULL;
1368 subs->cur_rate = 0;
1369 subs->period_bytes = 0;
1370 release_substream_urbs(subs, 0);
1371 return snd_pcm_lib_free_pages(substream);
1372}
1373
1374/*
1375 * prepare callback
1376 *
1377 * only a few subtle things...
1378 */
1379static int snd_usb_pcm_prepare(snd_pcm_substream_t *substream)
1380{
1381 snd_pcm_runtime_t *runtime = substream->runtime;
1382 snd_usb_substream_t *subs = (snd_usb_substream_t *)runtime->private_data;
1383
1384 if (! subs->cur_audiofmt) {
1385 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
1386 return -ENXIO;
1387 }
1388
1389 /* some unit conversions in runtime */
1390 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
1391 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
1392
1393 /* reset the pointer */
1394 subs->hwptr = 0;
1395 subs->hwptr_done = 0;
1396 subs->transfer_sched = 0;
1397 subs->transfer_done = 0;
1398 subs->phase = 0;
1399
1400 /* clear urbs (to be sure) */
1401 deactivate_urbs(subs, 0, 1);
1402 wait_clear_urbs(subs);
1403
1404 return 0;
1405}
1406
1407static snd_pcm_hardware_t snd_usb_playback =
1408{
1409 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1410 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1411 SNDRV_PCM_INFO_MMAP_VALID),
1412 .buffer_bytes_max = (128*1024),
1413 .period_bytes_min = 64,
1414 .period_bytes_max = (128*1024),
1415 .periods_min = 2,
1416 .periods_max = 1024,
1417};
1418
1419static snd_pcm_hardware_t snd_usb_capture =
1420{
1421 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1422 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1423 SNDRV_PCM_INFO_MMAP_VALID),
1424 .buffer_bytes_max = (128*1024),
1425 .period_bytes_min = 64,
1426 .period_bytes_max = (128*1024),
1427 .periods_min = 2,
1428 .periods_max = 1024,
1429};
1430
1431/*
1432 * h/w constraints
1433 */
1434
1435#ifdef HW_CONST_DEBUG
1436#define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
1437#else
1438#define hwc_debug(fmt, args...) /**/
1439#endif
1440
1441static int hw_check_valid_format(snd_pcm_hw_params_t *params, struct audioformat *fp)
1442{
1443 snd_interval_t *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1444 snd_interval_t *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1445 snd_mask_t *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1446
1447 /* check the format */
1448 if (! snd_mask_test(fmts, fp->format)) {
1449 hwc_debug(" > check: no supported format %d\n", fp->format);
1450 return 0;
1451 }
1452 /* check the channels */
1453 if (fp->channels < ct->min || fp->channels > ct->max) {
1454 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1455 return 0;
1456 }
1457 /* check the rate is within the range */
1458 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1459 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1460 return 0;
1461 }
1462 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1463 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1464 return 0;
1465 }
1466 return 1;
1467}
1468
1469static int hw_rule_rate(snd_pcm_hw_params_t *params,
1470 snd_pcm_hw_rule_t *rule)
1471{
1472 snd_usb_substream_t *subs = rule->private;
1473 struct list_head *p;
1474 snd_interval_t *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1475 unsigned int rmin, rmax;
1476 int changed;
1477
1478 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1479 changed = 0;
1480 rmin = rmax = 0;
1481 list_for_each(p, &subs->fmt_list) {
1482 struct audioformat *fp;
1483 fp = list_entry(p, struct audioformat, list);
1484 if (! hw_check_valid_format(params, fp))
1485 continue;
1486 if (changed++) {
1487 if (rmin > fp->rate_min)
1488 rmin = fp->rate_min;
1489 if (rmax < fp->rate_max)
1490 rmax = fp->rate_max;
1491 } else {
1492 rmin = fp->rate_min;
1493 rmax = fp->rate_max;
1494 }
1495 }
1496
1497 if (! changed) {
1498 hwc_debug(" --> get empty\n");
1499 it->empty = 1;
1500 return -EINVAL;
1501 }
1502
1503 changed = 0;
1504 if (it->min < rmin) {
1505 it->min = rmin;
1506 it->openmin = 0;
1507 changed = 1;
1508 }
1509 if (it->max > rmax) {
1510 it->max = rmax;
1511 it->openmax = 0;
1512 changed = 1;
1513 }
1514 if (snd_interval_checkempty(it)) {
1515 it->empty = 1;
1516 return -EINVAL;
1517 }
1518 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1519 return changed;
1520}
1521
1522
1523static int hw_rule_channels(snd_pcm_hw_params_t *params,
1524 snd_pcm_hw_rule_t *rule)
1525{
1526 snd_usb_substream_t *subs = rule->private;
1527 struct list_head *p;
1528 snd_interval_t *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1529 unsigned int rmin, rmax;
1530 int changed;
1531
1532 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1533 changed = 0;
1534 rmin = rmax = 0;
1535 list_for_each(p, &subs->fmt_list) {
1536 struct audioformat *fp;
1537 fp = list_entry(p, struct audioformat, list);
1538 if (! hw_check_valid_format(params, fp))
1539 continue;
1540 if (changed++) {
1541 if (rmin > fp->channels)
1542 rmin = fp->channels;
1543 if (rmax < fp->channels)
1544 rmax = fp->channels;
1545 } else {
1546 rmin = fp->channels;
1547 rmax = fp->channels;
1548 }
1549 }
1550
1551 if (! changed) {
1552 hwc_debug(" --> get empty\n");
1553 it->empty = 1;
1554 return -EINVAL;
1555 }
1556
1557 changed = 0;
1558 if (it->min < rmin) {
1559 it->min = rmin;
1560 it->openmin = 0;
1561 changed = 1;
1562 }
1563 if (it->max > rmax) {
1564 it->max = rmax;
1565 it->openmax = 0;
1566 changed = 1;
1567 }
1568 if (snd_interval_checkempty(it)) {
1569 it->empty = 1;
1570 return -EINVAL;
1571 }
1572 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1573 return changed;
1574}
1575
1576static int hw_rule_format(snd_pcm_hw_params_t *params,
1577 snd_pcm_hw_rule_t *rule)
1578{
1579 snd_usb_substream_t *subs = rule->private;
1580 struct list_head *p;
1581 snd_mask_t *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1582 u64 fbits;
1583 u32 oldbits[2];
1584 int changed;
1585
1586 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1587 fbits = 0;
1588 list_for_each(p, &subs->fmt_list) {
1589 struct audioformat *fp;
1590 fp = list_entry(p, struct audioformat, list);
1591 if (! hw_check_valid_format(params, fp))
1592 continue;
1593 fbits |= (1ULL << fp->format);
1594 }
1595
1596 oldbits[0] = fmt->bits[0];
1597 oldbits[1] = fmt->bits[1];
1598 fmt->bits[0] &= (u32)fbits;
1599 fmt->bits[1] &= (u32)(fbits >> 32);
1600 if (! fmt->bits[0] && ! fmt->bits[1]) {
1601 hwc_debug(" --> get empty\n");
1602 return -EINVAL;
1603 }
1604 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1605 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1606 return changed;
1607}
1608
1609#define MAX_MASK 64
1610
1611/*
1612 * check whether the registered audio formats need special hw-constraints
1613 */
1614static int check_hw_params_convention(snd_usb_substream_t *subs)
1615{
1616 int i;
1617 u32 *channels;
1618 u32 *rates;
1619 u32 cmaster, rmaster;
1620 u32 rate_min = 0, rate_max = 0;
1621 struct list_head *p;
1622 int err = 1;
1623
1624 channels = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1625 rates = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1626
1627 list_for_each(p, &subs->fmt_list) {
1628 struct audioformat *f;
1629 f = list_entry(p, struct audioformat, list);
1630 /* unconventional channels? */
1631 if (f->channels > 32)
1632 goto __out;
1633 /* continuous rate min/max matches? */
1634 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1635 if (rate_min && f->rate_min != rate_min)
1636 goto __out;
1637 if (rate_max && f->rate_max != rate_max)
1638 goto __out;
1639 rate_min = f->rate_min;
1640 rate_max = f->rate_max;
1641 }
1642 /* combination of continuous rates and fixed rates? */
1643 if (rates[f->format] & SNDRV_PCM_RATE_CONTINUOUS) {
1644 if (f->rates != rates[f->format])
1645 goto __out;
1646 }
1647 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1648 if (rates[f->format] && rates[f->format] != f->rates)
1649 goto __out;
1650 }
1651 channels[f->format] |= (1 << f->channels);
1652 rates[f->format] |= f->rates;
1653 }
1654 /* check whether channels and rates match for all formats */
1655 cmaster = rmaster = 0;
1656 for (i = 0; i < MAX_MASK; i++) {
1657 if (cmaster != channels[i] && cmaster && channels[i])
1658 goto __out;
1659 if (rmaster != rates[i] && rmaster && rates[i])
1660 goto __out;
1661 if (channels[i])
1662 cmaster = channels[i];
1663 if (rates[i])
1664 rmaster = rates[i];
1665 }
1666 /* check whether channels match for all distinct rates */
1667 memset(channels, 0, MAX_MASK * sizeof(u32));
1668 list_for_each(p, &subs->fmt_list) {
1669 struct audioformat *f;
1670 f = list_entry(p, struct audioformat, list);
1671 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS)
1672 continue;
1673 for (i = 0; i < 32; i++) {
1674 if (f->rates & (1 << i))
1675 channels[i] |= (1 << f->channels);
1676 }
1677 }
1678 cmaster = 0;
1679 for (i = 0; i < 32; i++) {
1680 if (cmaster != channels[i] && cmaster && channels[i])
1681 goto __out;
1682 if (channels[i])
1683 cmaster = channels[i];
1684 }
1685 err = 0;
1686
1687 __out:
1688 kfree(channels);
1689 kfree(rates);
1690 return err;
1691}
1692
1693
1694/*
1695 * set up the runtime hardware information.
1696 */
1697
1698static int setup_hw_info(snd_pcm_runtime_t *runtime, snd_usb_substream_t *subs)
1699{
1700 struct list_head *p;
1701 int err;
1702
1703 runtime->hw.formats = subs->formats;
1704
1705 runtime->hw.rate_min = 0x7fffffff;
1706 runtime->hw.rate_max = 0;
1707 runtime->hw.channels_min = 256;
1708 runtime->hw.channels_max = 0;
1709 runtime->hw.rates = 0;
1710 /* check min/max rates and channels */
1711 list_for_each(p, &subs->fmt_list) {
1712 struct audioformat *fp;
1713 fp = list_entry(p, struct audioformat, list);
1714 runtime->hw.rates |= fp->rates;
1715 if (runtime->hw.rate_min > fp->rate_min)
1716 runtime->hw.rate_min = fp->rate_min;
1717 if (runtime->hw.rate_max < fp->rate_max)
1718 runtime->hw.rate_max = fp->rate_max;
1719 if (runtime->hw.channels_min > fp->channels)
1720 runtime->hw.channels_min = fp->channels;
1721 if (runtime->hw.channels_max < fp->channels)
1722 runtime->hw.channels_max = fp->channels;
1723 if (fp->fmt_type == USB_FORMAT_TYPE_II && fp->frame_size > 0) {
1724 /* FIXME: there might be more than one audio formats... */
1725 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1726 fp->frame_size;
1727 }
1728 }
1729
1730 /* set the period time minimum 1ms */
1731 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1732 1000 * MIN_PACKS_URB,
1733 /*(nrpacks * MAX_URBS) * 1000*/ UINT_MAX);
1734
1735 if (check_hw_params_convention(subs)) {
1736 hwc_debug("setting extra hw constraints...\n");
1737 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1738 hw_rule_rate, subs,
1739 SNDRV_PCM_HW_PARAM_FORMAT,
1740 SNDRV_PCM_HW_PARAM_CHANNELS,
1741 -1)) < 0)
1742 return err;
1743 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1744 hw_rule_channels, subs,
1745 SNDRV_PCM_HW_PARAM_FORMAT,
1746 SNDRV_PCM_HW_PARAM_RATE,
1747 -1)) < 0)
1748 return err;
1749 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1750 hw_rule_format, subs,
1751 SNDRV_PCM_HW_PARAM_RATE,
1752 SNDRV_PCM_HW_PARAM_CHANNELS,
1753 -1)) < 0)
1754 return err;
1755 }
1756 return 0;
1757}
1758
1759static int snd_usb_pcm_open(snd_pcm_substream_t *substream, int direction,
1760 snd_pcm_hardware_t *hw)
1761{
1762 snd_usb_stream_t *as = snd_pcm_substream_chip(substream);
1763 snd_pcm_runtime_t *runtime = substream->runtime;
1764 snd_usb_substream_t *subs = &as->substream[direction];
1765
1766 subs->interface = -1;
1767 subs->format = 0;
1768 runtime->hw = *hw;
1769 runtime->private_data = subs;
1770 subs->pcm_substream = substream;
1771 return setup_hw_info(runtime, subs);
1772}
1773
1774static int snd_usb_pcm_close(snd_pcm_substream_t *substream, int direction)
1775{
1776 snd_usb_stream_t *as = snd_pcm_substream_chip(substream);
1777 snd_usb_substream_t *subs = &as->substream[direction];
1778
1779 if (subs->interface >= 0) {
1780 usb_set_interface(subs->dev, subs->interface, 0);
1781 subs->interface = -1;
1782 }
1783 subs->pcm_substream = NULL;
1784 return 0;
1785}
1786
1787static int snd_usb_playback_open(snd_pcm_substream_t *substream)
1788{
1789 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK, &snd_usb_playback);
1790}
1791
1792static int snd_usb_playback_close(snd_pcm_substream_t *substream)
1793{
1794 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1795}
1796
1797static int snd_usb_capture_open(snd_pcm_substream_t *substream)
1798{
1799 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE, &snd_usb_capture);
1800}
1801
1802static int snd_usb_capture_close(snd_pcm_substream_t *substream)
1803{
1804 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1805}
1806
1807static snd_pcm_ops_t snd_usb_playback_ops = {
1808 .open = snd_usb_playback_open,
1809 .close = snd_usb_playback_close,
1810 .ioctl = snd_pcm_lib_ioctl,
1811 .hw_params = snd_usb_hw_params,
1812 .hw_free = snd_usb_hw_free,
1813 .prepare = snd_usb_pcm_prepare,
1814 .trigger = snd_usb_pcm_trigger,
1815 .pointer = snd_usb_pcm_pointer,
1816};
1817
1818static snd_pcm_ops_t snd_usb_capture_ops = {
1819 .open = snd_usb_capture_open,
1820 .close = snd_usb_capture_close,
1821 .ioctl = snd_pcm_lib_ioctl,
1822 .hw_params = snd_usb_hw_params,
1823 .hw_free = snd_usb_hw_free,
1824 .prepare = snd_usb_pcm_prepare,
1825 .trigger = snd_usb_pcm_trigger,
1826 .pointer = snd_usb_pcm_pointer,
1827};
1828
1829
1830
1831/*
1832 * helper functions
1833 */
1834
1835/*
1836 * combine bytes and get an integer value
1837 */
1838unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
1839{
1840 switch (size) {
1841 case 1: return *bytes;
1842 case 2: return combine_word(bytes);
1843 case 3: return combine_triple(bytes);
1844 case 4: return combine_quad(bytes);
1845 default: return 0;
1846 }
1847}
1848
1849/*
1850 * parse descriptor buffer and return the pointer starting the given
1851 * descriptor type.
1852 */
1853void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
1854{
1855 u8 *p, *end, *next;
1856
1857 p = descstart;
1858 end = p + desclen;
1859 for (; p < end;) {
1860 if (p[0] < 2)
1861 return NULL;
1862 next = p + p[0];
1863 if (next > end)
1864 return NULL;
1865 if (p[1] == dtype && (!after || (void *)p > after)) {
1866 return p;
1867 }
1868 p = next;
1869 }
1870 return NULL;
1871}
1872
1873/*
1874 * find a class-specified interface descriptor with the given subtype.
1875 */
1876void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
1877{
1878 unsigned char *p = after;
1879
1880 while ((p = snd_usb_find_desc(buffer, buflen, p,
1881 USB_DT_CS_INTERFACE)) != NULL) {
1882 if (p[0] >= 3 && p[2] == dsubtype)
1883 return p;
1884 }
1885 return NULL;
1886}
1887
1888/*
1889 * Wrapper for usb_control_msg().
1890 * Allocates a temp buffer to prevent dmaing from/to the stack.
1891 */
1892int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
1893 __u8 requesttype, __u16 value, __u16 index, void *data,
1894 __u16 size, int timeout)
1895{
1896 int err;
1897 void *buf = NULL;
1898
1899 if (size > 0) {
1900 buf = kmalloc(size, GFP_KERNEL);
1901 if (!buf)
1902 return -ENOMEM;
1903 memcpy(buf, data, size);
1904 }
1905 err = usb_control_msg(dev, pipe, request, requesttype,
1906 value, index, buf, size, timeout);
1907 if (size > 0) {
1908 memcpy(data, buf, size);
1909 kfree(buf);
1910 }
1911 return err;
1912}
1913
1914
1915/*
1916 * entry point for linux usb interface
1917 */
1918
1919static int usb_audio_probe(struct usb_interface *intf,
1920 const struct usb_device_id *id);
1921static void usb_audio_disconnect(struct usb_interface *intf);
1922
1923static struct usb_device_id usb_audio_ids [] = {
1924#include "usbquirks.h"
1925 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
1926 .bInterfaceClass = USB_CLASS_AUDIO,
1927 .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL },
1928 { } /* Terminating entry */
1929};
1930
1931MODULE_DEVICE_TABLE (usb, usb_audio_ids);
1932
1933static struct usb_driver usb_audio_driver = {
1934 .owner = THIS_MODULE,
1935 .name = "snd-usb-audio",
1936 .probe = usb_audio_probe,
1937 .disconnect = usb_audio_disconnect,
1938 .id_table = usb_audio_ids,
1939};
1940
1941
1942/*
1943 * proc interface for list the supported pcm formats
1944 */
1945static void proc_dump_substream_formats(snd_usb_substream_t *subs, snd_info_buffer_t *buffer)
1946{
1947 struct list_head *p;
1948 static char *sync_types[4] = {
1949 "NONE", "ASYNC", "ADAPTIVE", "SYNC"
1950 };
1951
1952 list_for_each(p, &subs->fmt_list) {
1953 struct audioformat *fp;
1954 fp = list_entry(p, struct audioformat, list);
1955 snd_iprintf(buffer, " Interface %d\n", fp->iface);
1956 snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
1957 snd_iprintf(buffer, " Format: %s\n", snd_pcm_format_name(fp->format));
1958 snd_iprintf(buffer, " Channels: %d\n", fp->channels);
1959 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
1960 fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
1961 fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
1962 sync_types[(fp->ep_attr & EP_ATTR_MASK) >> 2]);
1963 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1964 snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
1965 fp->rate_min, fp->rate_max);
1966 } else {
1967 unsigned int i;
1968 snd_iprintf(buffer, " Rates: ");
1969 for (i = 0; i < fp->nr_rates; i++) {
1970 if (i > 0)
1971 snd_iprintf(buffer, ", ");
1972 snd_iprintf(buffer, "%d", fp->rate_table[i]);
1973 }
1974 snd_iprintf(buffer, "\n");
1975 }
1976 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
1977 // snd_iprintf(buffer, " EP Attribute = 0x%x\n", fp->attributes);
1978 }
1979}
1980
1981static void proc_dump_substream_status(snd_usb_substream_t *subs, snd_info_buffer_t *buffer)
1982{
1983 if (subs->running) {
1984 unsigned int i;
1985 snd_iprintf(buffer, " Status: Running\n");
1986 snd_iprintf(buffer, " Interface = %d\n", subs->interface);
1987 snd_iprintf(buffer, " Altset = %d\n", subs->format);
1988 snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
1989 for (i = 0; i < subs->nurbs; i++)
1990 snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
1991 snd_iprintf(buffer, "]\n");
1992 snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
Clemens Ladisch08fe1582005-04-22 15:33:01 +02001993 snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
1995 ? get_full_speed_hz(subs->freqm)
Clemens Ladisch08fe1582005-04-22 15:33:01 +02001996 : get_high_speed_hz(subs->freqm),
1997 subs->freqm >> 16, subs->freqm & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 } else {
1999 snd_iprintf(buffer, " Status: Stop\n");
2000 }
2001}
2002
2003static void proc_pcm_format_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
2004{
2005 snd_usb_stream_t *stream = entry->private_data;
2006
2007 snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
2008
2009 if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
2010 snd_iprintf(buffer, "\nPlayback:\n");
2011 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2012 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2013 }
2014 if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
2015 snd_iprintf(buffer, "\nCapture:\n");
2016 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2017 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2018 }
2019}
2020
2021static void proc_pcm_format_add(snd_usb_stream_t *stream)
2022{
2023 snd_info_entry_t *entry;
2024 char name[32];
2025 snd_card_t *card = stream->chip->card;
2026
2027 sprintf(name, "stream%d", stream->pcm_index);
2028 if (! snd_card_proc_new(card, name, &entry))
2029 snd_info_set_text_ops(entry, stream, 1024, proc_pcm_format_read);
2030}
2031
2032
2033/*
2034 * initialize the substream instance.
2035 */
2036
2037static void init_substream(snd_usb_stream_t *as, int stream, struct audioformat *fp)
2038{
2039 snd_usb_substream_t *subs = &as->substream[stream];
2040
2041 INIT_LIST_HEAD(&subs->fmt_list);
2042 spin_lock_init(&subs->lock);
2043
2044 subs->stream = as;
2045 subs->direction = stream;
2046 subs->dev = as->chip->dev;
2047 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
2048 subs->ops = audio_urb_ops[stream];
2049 else
2050 subs->ops = audio_urb_ops_high_speed[stream];
2051 snd_pcm_lib_preallocate_pages(as->pcm->streams[stream].substream,
2052 SNDRV_DMA_TYPE_CONTINUOUS,
2053 snd_dma_continuous_data(GFP_KERNEL),
2054 64 * 1024, 128 * 1024);
2055 snd_pcm_set_ops(as->pcm, stream,
2056 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2057 &snd_usb_playback_ops : &snd_usb_capture_ops);
2058
2059 list_add_tail(&fp->list, &subs->fmt_list);
2060 subs->formats |= 1ULL << fp->format;
2061 subs->endpoint = fp->endpoint;
2062 subs->num_formats++;
2063 subs->fmt_type = fp->fmt_type;
2064}
2065
2066
2067/*
2068 * free a substream
2069 */
2070static void free_substream(snd_usb_substream_t *subs)
2071{
2072 struct list_head *p, *n;
2073
2074 if (! subs->num_formats)
2075 return; /* not initialized */
2076 list_for_each_safe(p, n, &subs->fmt_list) {
2077 struct audioformat *fp = list_entry(p, struct audioformat, list);
2078 kfree(fp->rate_table);
2079 kfree(fp);
2080 }
2081}
2082
2083
2084/*
2085 * free a usb stream instance
2086 */
2087static void snd_usb_audio_stream_free(snd_usb_stream_t *stream)
2088{
2089 free_substream(&stream->substream[0]);
2090 free_substream(&stream->substream[1]);
2091 list_del(&stream->list);
2092 kfree(stream);
2093}
2094
2095static void snd_usb_audio_pcm_free(snd_pcm_t *pcm)
2096{
2097 snd_usb_stream_t *stream = pcm->private_data;
2098 if (stream) {
2099 stream->pcm = NULL;
2100 snd_pcm_lib_preallocate_free_for_all(pcm);
2101 snd_usb_audio_stream_free(stream);
2102 }
2103}
2104
2105
2106/*
2107 * add this endpoint to the chip instance.
2108 * if a stream with the same endpoint already exists, append to it.
2109 * if not, create a new pcm stream.
2110 */
2111static int add_audio_endpoint(snd_usb_audio_t *chip, int stream, struct audioformat *fp)
2112{
2113 struct list_head *p;
2114 snd_usb_stream_t *as;
2115 snd_usb_substream_t *subs;
2116 snd_pcm_t *pcm;
2117 int err;
2118
2119 list_for_each(p, &chip->pcm_list) {
2120 as = list_entry(p, snd_usb_stream_t, list);
2121 if (as->fmt_type != fp->fmt_type)
2122 continue;
2123 subs = &as->substream[stream];
2124 if (! subs->endpoint)
2125 continue;
2126 if (subs->endpoint == fp->endpoint) {
2127 list_add_tail(&fp->list, &subs->fmt_list);
2128 subs->num_formats++;
2129 subs->formats |= 1ULL << fp->format;
2130 return 0;
2131 }
2132 }
2133 /* look for an empty stream */
2134 list_for_each(p, &chip->pcm_list) {
2135 as = list_entry(p, snd_usb_stream_t, list);
2136 if (as->fmt_type != fp->fmt_type)
2137 continue;
2138 subs = &as->substream[stream];
2139 if (subs->endpoint)
2140 continue;
2141 err = snd_pcm_new_stream(as->pcm, stream, 1);
2142 if (err < 0)
2143 return err;
2144 init_substream(as, stream, fp);
2145 return 0;
2146 }
2147
2148 /* create a new pcm */
2149 as = kmalloc(sizeof(*as), GFP_KERNEL);
2150 if (! as)
2151 return -ENOMEM;
2152 memset(as, 0, sizeof(*as));
2153 as->pcm_index = chip->pcm_devs;
2154 as->chip = chip;
2155 as->fmt_type = fp->fmt_type;
2156 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
2157 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
2158 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
2159 &pcm);
2160 if (err < 0) {
2161 kfree(as);
2162 return err;
2163 }
2164 as->pcm = pcm;
2165 pcm->private_data = as;
2166 pcm->private_free = snd_usb_audio_pcm_free;
2167 pcm->info_flags = 0;
2168 if (chip->pcm_devs > 0)
2169 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
2170 else
2171 strcpy(pcm->name, "USB Audio");
2172
2173 init_substream(as, stream, fp);
2174
2175 list_add(&as->list, &chip->pcm_list);
2176 chip->pcm_devs++;
2177
2178 proc_pcm_format_add(as);
2179
2180 return 0;
2181}
2182
2183
2184/*
2185 * check if the device uses big-endian samples
2186 */
2187static int is_big_endian_format(struct usb_device *dev, struct audioformat *fp)
2188{
2189 /* M-Audio */
2190 if (le16_to_cpu(dev->descriptor.idVendor) == 0x0763) {
2191 /* Quattro: captured data only */
2192 if (le16_to_cpu(dev->descriptor.idProduct) == 0x2001 &&
2193 fp->endpoint & USB_DIR_IN)
2194 return 1;
2195 /* Audiophile USB */
2196 if (le16_to_cpu(dev->descriptor.idProduct) == 0x2003)
2197 return 1;
2198 }
2199 return 0;
2200}
2201
2202/*
2203 * parse the audio format type I descriptor
2204 * and returns the corresponding pcm format
2205 *
2206 * @dev: usb device
2207 * @fp: audioformat record
2208 * @format: the format tag (wFormatTag)
2209 * @fmt: the format type descriptor
2210 */
2211static int parse_audio_format_i_type(struct usb_device *dev, struct audioformat *fp,
2212 int format, unsigned char *fmt)
2213{
2214 int pcm_format;
2215 int sample_width, sample_bytes;
2216
2217 /* FIXME: correct endianess and sign? */
2218 pcm_format = -1;
2219 sample_width = fmt[6];
2220 sample_bytes = fmt[5];
2221 switch (format) {
2222 case 0: /* some devices don't define this correctly... */
2223 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
2224 dev->devnum, fp->iface, fp->altsetting);
2225 /* fall-through */
2226 case USB_AUDIO_FORMAT_PCM:
2227 if (sample_width > sample_bytes * 8) {
2228 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
2229 dev->devnum, fp->iface, fp->altsetting,
2230 sample_width, sample_bytes);
2231 }
2232 /* check the format byte size */
2233 switch (fmt[5]) {
2234 case 1:
2235 pcm_format = SNDRV_PCM_FORMAT_S8;
2236 break;
2237 case 2:
2238 if (is_big_endian_format(dev, fp))
2239 pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
2240 else
2241 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2242 break;
2243 case 3:
2244 if (is_big_endian_format(dev, fp))
2245 pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
2246 else
2247 pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
2248 break;
2249 case 4:
2250 pcm_format = SNDRV_PCM_FORMAT_S32_LE;
2251 break;
2252 default:
2253 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
2254 dev->devnum, fp->iface, fp->altsetting, sample_width, sample_bytes);
2255 break;
2256 }
2257 break;
2258 case USB_AUDIO_FORMAT_PCM8:
2259 /* Dallas DS4201 workaround */
2260 if (le16_to_cpu(dev->descriptor.idVendor) == 0x04fa &&
2261 le16_to_cpu(dev->descriptor.idProduct) == 0x4201)
2262 pcm_format = SNDRV_PCM_FORMAT_S8;
2263 else
2264 pcm_format = SNDRV_PCM_FORMAT_U8;
2265 break;
2266 case USB_AUDIO_FORMAT_IEEE_FLOAT:
2267 pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
2268 break;
2269 case USB_AUDIO_FORMAT_ALAW:
2270 pcm_format = SNDRV_PCM_FORMAT_A_LAW;
2271 break;
2272 case USB_AUDIO_FORMAT_MU_LAW:
2273 pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
2274 break;
2275 default:
2276 snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
2277 dev->devnum, fp->iface, fp->altsetting, format);
2278 break;
2279 }
2280 return pcm_format;
2281}
2282
2283
2284/*
2285 * parse the format descriptor and stores the possible sample rates
2286 * on the audioformat table.
2287 *
2288 * @dev: usb device
2289 * @fp: audioformat record
2290 * @fmt: the format descriptor
2291 * @offset: the start offset of descriptor pointing the rate type
2292 * (7 for type I and II, 8 for type II)
2293 */
2294static int parse_audio_format_rates(struct usb_device *dev, struct audioformat *fp,
2295 unsigned char *fmt, int offset)
2296{
2297 int nr_rates = fmt[offset];
2298 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
2299 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
2300 dev->devnum, fp->iface, fp->altsetting);
2301 return -1;
2302 }
2303
2304 if (nr_rates) {
2305 /*
2306 * build the rate table and bitmap flags
2307 */
2308 int r, idx, c;
2309 /* this table corresponds to the SNDRV_PCM_RATE_XXX bit */
2310 static unsigned int conv_rates[] = {
2311 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
2312 64000, 88200, 96000, 176400, 192000
2313 };
2314 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2315 if (fp->rate_table == NULL) {
2316 snd_printk(KERN_ERR "cannot malloc\n");
2317 return -1;
2318 }
2319
2320 fp->nr_rates = nr_rates;
2321 fp->rate_min = fp->rate_max = combine_triple(&fmt[8]);
2322 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
2323 unsigned int rate = fp->rate_table[r] = combine_triple(&fmt[idx]);
2324 if (rate < fp->rate_min)
2325 fp->rate_min = rate;
2326 else if (rate > fp->rate_max)
2327 fp->rate_max = rate;
2328 for (c = 0; c < (int)ARRAY_SIZE(conv_rates); c++) {
2329 if (rate == conv_rates[c]) {
2330 fp->rates |= (1 << c);
2331 break;
2332 }
2333 }
2334 }
2335 } else {
2336 /* continuous rates */
2337 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
2338 fp->rate_min = combine_triple(&fmt[offset + 1]);
2339 fp->rate_max = combine_triple(&fmt[offset + 4]);
2340 }
2341 return 0;
2342}
2343
2344/*
2345 * parse the format type I and III descriptors
2346 */
2347static int parse_audio_format_i(struct usb_device *dev, struct audioformat *fp,
2348 int format, unsigned char *fmt)
2349{
2350 int pcm_format;
2351
2352 if (fmt[3] == USB_FORMAT_TYPE_III) {
2353 /* FIXME: the format type is really IECxxx
2354 * but we give normal PCM format to get the existing
2355 * apps working...
2356 */
2357 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2358 } else {
2359 pcm_format = parse_audio_format_i_type(dev, fp, format, fmt);
2360 if (pcm_format < 0)
2361 return -1;
2362 }
2363 fp->format = pcm_format;
2364 fp->channels = fmt[4];
2365 if (fp->channels < 1) {
2366 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
2367 dev->devnum, fp->iface, fp->altsetting, fp->channels);
2368 return -1;
2369 }
2370 return parse_audio_format_rates(dev, fp, fmt, 7);
2371}
2372
2373/*
2374 * prase the format type II descriptor
2375 */
2376static int parse_audio_format_ii(struct usb_device *dev, struct audioformat *fp,
2377 int format, unsigned char *fmt)
2378{
2379 int brate, framesize;
2380 switch (format) {
2381 case USB_AUDIO_FORMAT_AC3:
2382 /* FIXME: there is no AC3 format defined yet */
2383 // fp->format = SNDRV_PCM_FORMAT_AC3;
2384 fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
2385 break;
2386 case USB_AUDIO_FORMAT_MPEG:
2387 fp->format = SNDRV_PCM_FORMAT_MPEG;
2388 break;
2389 default:
2390 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag 0x%x is detected. processed as MPEG.\n",
2391 dev->devnum, fp->iface, fp->altsetting, format);
2392 fp->format = SNDRV_PCM_FORMAT_MPEG;
2393 break;
2394 }
2395 fp->channels = 1;
2396 brate = combine_word(&fmt[4]); /* fmt[4,5] : wMaxBitRate (in kbps) */
2397 framesize = combine_word(&fmt[6]); /* fmt[6,7]: wSamplesPerFrame */
2398 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
2399 fp->frame_size = framesize;
2400 return parse_audio_format_rates(dev, fp, fmt, 8); /* fmt[8..] sample rates */
2401}
2402
2403static int parse_audio_format(struct usb_device *dev, struct audioformat *fp,
2404 int format, unsigned char *fmt, int stream)
2405{
2406 int err;
2407
2408 switch (fmt[3]) {
2409 case USB_FORMAT_TYPE_I:
2410 case USB_FORMAT_TYPE_III:
2411 err = parse_audio_format_i(dev, fp, format, fmt);
2412 break;
2413 case USB_FORMAT_TYPE_II:
2414 err = parse_audio_format_ii(dev, fp, format, fmt);
2415 break;
2416 default:
2417 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
2418 dev->devnum, fp->iface, fp->altsetting, fmt[3]);
2419 return -1;
2420 }
2421 fp->fmt_type = fmt[3];
2422 if (err < 0)
2423 return err;
2424#if 1
Clemens Ladisch8c1872d2005-04-28 09:31:53 +02002425 /* FIXME: temporary hack for extigy/audigy 2 nx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /* extigy apparently supports sample rates other than 48k
2427 * but not in ordinary way. so we enable only 48k atm.
2428 */
2429 if (le16_to_cpu(dev->descriptor.idVendor) == 0x041e &&
Clemens Ladisch8c1872d2005-04-28 09:31:53 +02002430 (le16_to_cpu(dev->descriptor.idProduct) == 0x3000 ||
2431 le16_to_cpu(dev->descriptor.idProduct) == 0x3020)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (fmt[3] == USB_FORMAT_TYPE_I &&
2433 stream == SNDRV_PCM_STREAM_PLAYBACK &&
Clemens Ladisch8c1872d2005-04-28 09:31:53 +02002434 fp->rates != SNDRV_PCM_RATE_48000 &&
2435 fp->rates != SNDRV_PCM_RATE_96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return -1; /* use 48k only */
2437 }
2438#endif
2439 return 0;
2440}
2441
2442static int parse_audio_endpoints(snd_usb_audio_t *chip, int iface_no)
2443{
2444 struct usb_device *dev;
2445 struct usb_interface *iface;
2446 struct usb_host_interface *alts;
2447 struct usb_interface_descriptor *altsd;
2448 int i, altno, err, stream;
2449 int format;
2450 struct audioformat *fp;
2451 unsigned char *fmt, *csep;
2452
2453 dev = chip->dev;
2454
2455 /* parse the interface's altsettings */
2456 iface = usb_ifnum_to_if(dev, iface_no);
2457 for (i = 0; i < iface->num_altsetting; i++) {
2458 alts = &iface->altsetting[i];
2459 altsd = get_iface_desc(alts);
2460 /* skip invalid one */
2461 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2462 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2463 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING &&
2464 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
2465 altsd->bNumEndpoints < 1 ||
2466 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
2467 continue;
2468 /* must be isochronous */
2469 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2470 USB_ENDPOINT_XFER_ISOC)
2471 continue;
2472 /* check direction */
2473 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
2474 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2475 altno = altsd->bAlternateSetting;
2476
2477 /* get audio formats */
2478 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, AS_GENERAL);
2479 if (!fmt) {
2480 snd_printk(KERN_ERR "%d:%u:%d : AS_GENERAL descriptor not found\n",
2481 dev->devnum, iface_no, altno);
2482 continue;
2483 }
2484
2485 if (fmt[0] < 7) {
2486 snd_printk(KERN_ERR "%d:%u:%d : invalid AS_GENERAL desc\n",
2487 dev->devnum, iface_no, altno);
2488 continue;
2489 }
2490
2491 format = (fmt[6] << 8) | fmt[5]; /* remember the format value */
2492
2493 /* get format type */
2494 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, FORMAT_TYPE);
2495 if (!fmt) {
2496 snd_printk(KERN_ERR "%d:%u:%d : no FORMAT_TYPE desc\n",
2497 dev->devnum, iface_no, altno);
2498 continue;
2499 }
2500 if (fmt[0] < 8) {
2501 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
2502 dev->devnum, iface_no, altno);
2503 continue;
2504 }
2505
2506 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
2507 /* Creamware Noah has this descriptor after the 2nd endpoint */
2508 if (!csep && altsd->bNumEndpoints >= 2)
2509 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
2510 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2511 snd_printk(KERN_ERR "%d:%u:%d : no or invalid class specific endpoint descriptor\n",
2512 dev->devnum, iface_no, altno);
2513 continue;
2514 }
2515
2516 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2517 if (! fp) {
2518 snd_printk(KERN_ERR "cannot malloc\n");
2519 return -ENOMEM;
2520 }
2521
2522 memset(fp, 0, sizeof(*fp));
2523 fp->iface = iface_no;
2524 fp->altsetting = altno;
2525 fp->altset_idx = i;
2526 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2527 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2528 /* FIXME: decode wMaxPacketSize of high bandwith endpoints */
2529 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2530 fp->attributes = csep[3];
2531
2532 /* some quirks for attributes here */
2533
2534 /* workaround for AudioTrak Optoplay */
2535 if (le16_to_cpu(dev->descriptor.idVendor) == 0x0a92 &&
2536 le16_to_cpu(dev->descriptor.idProduct) == 0x0053) {
2537 /* Optoplay sets the sample rate attribute although
2538 * it seems not supporting it in fact.
2539 */
2540 fp->attributes &= ~EP_CS_ATTR_SAMPLE_RATE;
2541 }
2542
2543 /* workaround for M-Audio Audiophile USB */
2544 if (le16_to_cpu(dev->descriptor.idVendor) == 0x0763 &&
2545 le16_to_cpu(dev->descriptor.idProduct) == 0x2003) {
2546 /* doesn't set the sample rate attribute, but supports it */
2547 fp->attributes |= EP_CS_ATTR_SAMPLE_RATE;
2548 }
2549
2550 /*
2551 * plantronics headset and Griffin iMic have set adaptive-in
2552 * although it's really not...
2553 */
2554 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x047f &&
2555 le16_to_cpu(dev->descriptor.idProduct) == 0x0ca1) ||
2556 /* Griffin iMic (note that there is an older model 77d:223) */
2557 (le16_to_cpu(dev->descriptor.idVendor) == 0x077d &&
2558 le16_to_cpu(dev->descriptor.idProduct) == 0x07af)) {
2559 fp->ep_attr &= ~EP_ATTR_MASK;
2560 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2561 fp->ep_attr |= EP_ATTR_ADAPTIVE;
2562 else
2563 fp->ep_attr |= EP_ATTR_SYNC;
2564 }
2565
2566 /* ok, let's parse further... */
2567 if (parse_audio_format(dev, fp, format, fmt, stream) < 0) {
2568 kfree(fp->rate_table);
2569 kfree(fp);
2570 continue;
2571 }
2572
2573 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint 0x%x\n", dev->devnum, iface_no, i, fp->endpoint);
2574 err = add_audio_endpoint(chip, stream, fp);
2575 if (err < 0) {
2576 kfree(fp->rate_table);
2577 kfree(fp);
2578 return err;
2579 }
2580 /* try to set the interface... */
2581 usb_set_interface(chip->dev, iface_no, altno);
2582 init_usb_pitch(chip->dev, iface_no, alts, fp);
2583 init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
2584 }
2585 return 0;
2586}
2587
2588
2589/*
2590 * disconnect streams
2591 * called from snd_usb_audio_disconnect()
2592 */
Clemens Ladischee733392005-04-25 10:34:13 +02002593static void snd_usb_stream_disconnect(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
2595 int idx;
2596 snd_usb_stream_t *as;
2597 snd_usb_substream_t *subs;
2598
2599 as = list_entry(head, snd_usb_stream_t, list);
2600 for (idx = 0; idx < 2; idx++) {
2601 subs = &as->substream[idx];
2602 if (!subs->num_formats)
2603 return;
2604 release_substream_urbs(subs, 1);
2605 subs->interface = -1;
2606 }
2607}
2608
2609/*
2610 * parse audio control descriptor and create pcm/midi streams
2611 */
2612static int snd_usb_create_streams(snd_usb_audio_t *chip, int ctrlif)
2613{
2614 struct usb_device *dev = chip->dev;
2615 struct usb_host_interface *host_iface;
2616 struct usb_interface *iface;
2617 unsigned char *p1;
2618 int i, j;
2619
2620 /* find audiocontrol interface */
2621 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
2622 if (!(p1 = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen, NULL, HEADER))) {
2623 snd_printk(KERN_ERR "cannot find HEADER\n");
2624 return -EINVAL;
2625 }
2626 if (! p1[7] || p1[0] < 8 + p1[7]) {
2627 snd_printk(KERN_ERR "invalid HEADER\n");
2628 return -EINVAL;
2629 }
2630
2631 /*
2632 * parse all USB audio streaming interfaces
2633 */
2634 for (i = 0; i < p1[7]; i++) {
2635 struct usb_host_interface *alts;
2636 struct usb_interface_descriptor *altsd;
2637 j = p1[8 + i];
2638 iface = usb_ifnum_to_if(dev, j);
2639 if (!iface) {
2640 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
2641 dev->devnum, ctrlif, j);
2642 continue;
2643 }
2644 if (usb_interface_claimed(iface)) {
2645 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n", dev->devnum, ctrlif, j);
2646 continue;
2647 }
2648 alts = &iface->altsetting[0];
2649 altsd = get_iface_desc(alts);
2650 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
2651 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
2652 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDI_STREAMING) {
2653 if (snd_usb_create_midi_interface(chip, iface, NULL) < 0) {
2654 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n", dev->devnum, ctrlif, j);
2655 continue;
2656 }
2657 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2658 continue;
2659 }
2660 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2661 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2662 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING) {
2663 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n", dev->devnum, ctrlif, j, altsd->bInterfaceClass);
2664 /* skip non-supported classes */
2665 continue;
2666 }
2667 if (! parse_audio_endpoints(chip, j)) {
2668 usb_set_interface(dev, j, 0); /* reset the current interface */
2669 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2670 }
2671 }
2672
2673 return 0;
2674}
2675
2676/*
2677 * create a stream for an endpoint/altsetting without proper descriptors
2678 */
2679static int create_fixed_stream_quirk(snd_usb_audio_t *chip,
2680 struct usb_interface *iface,
2681 const snd_usb_audio_quirk_t *quirk)
2682{
2683 struct audioformat *fp;
2684 struct usb_host_interface *alts;
2685 int stream, err;
2686 int *rate_table = NULL;
2687
2688 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2689 if (! fp) {
2690 snd_printk(KERN_ERR "cannot malloc\n");
2691 return -ENOMEM;
2692 }
2693 memcpy(fp, quirk->data, sizeof(*fp));
2694 if (fp->nr_rates > 0) {
2695 rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
2696 if (!rate_table) {
2697 kfree(fp);
2698 return -ENOMEM;
2699 }
2700 memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
2701 fp->rate_table = rate_table;
2702 }
2703
2704 stream = (fp->endpoint & USB_DIR_IN)
2705 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2706 err = add_audio_endpoint(chip, stream, fp);
2707 if (err < 0) {
2708 kfree(fp);
2709 kfree(rate_table);
2710 return err;
2711 }
2712 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
2713 fp->altset_idx >= iface->num_altsetting) {
2714 kfree(fp);
2715 kfree(rate_table);
2716 return -EINVAL;
2717 }
2718 alts = &iface->altsetting[fp->altset_idx];
2719 usb_set_interface(chip->dev, fp->iface, 0);
2720 init_usb_pitch(chip->dev, fp->iface, alts, fp);
2721 init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
2722 return 0;
2723}
2724
2725/*
2726 * create a stream for an interface with proper descriptors
2727 */
2728static int create_standard_interface_quirk(snd_usb_audio_t *chip,
2729 struct usb_interface *iface,
2730 const snd_usb_audio_quirk_t *quirk)
2731{
2732 struct usb_host_interface *alts;
2733 struct usb_interface_descriptor *altsd;
2734 int err;
2735
2736 alts = &iface->altsetting[0];
2737 altsd = get_iface_desc(alts);
2738 switch (quirk->type) {
2739 case QUIRK_AUDIO_STANDARD_INTERFACE:
2740 err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
2741 if (!err)
2742 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0); /* reset the current interface */
2743 break;
2744 case QUIRK_MIDI_STANDARD_INTERFACE:
2745 err = snd_usb_create_midi_interface(chip, iface, NULL);
2746 break;
2747 default:
2748 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2749 return -ENXIO;
2750 }
2751 if (err < 0) {
2752 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
2753 altsd->bInterfaceNumber, err);
2754 return err;
2755 }
2756 return 0;
2757}
2758
2759/*
2760 * Create a stream for an Edirol UA-700/UA-25 interface. The only way
2761 * to detect the sample rate is by looking at wMaxPacketSize.
2762 */
2763static int create_ua700_ua25_quirk(snd_usb_audio_t *chip,
2764 struct usb_interface *iface)
2765{
2766 static const struct audioformat ua_format = {
2767 .format = SNDRV_PCM_FORMAT_S24_3LE,
2768 .channels = 2,
2769 .fmt_type = USB_FORMAT_TYPE_I,
2770 .altsetting = 1,
2771 .altset_idx = 1,
2772 .rates = SNDRV_PCM_RATE_CONTINUOUS,
2773 };
2774 struct usb_host_interface *alts;
2775 struct usb_interface_descriptor *altsd;
2776 struct audioformat *fp;
2777 int stream, err;
2778
2779 /* both PCM and MIDI interfaces have 2 altsettings */
2780 if (iface->num_altsetting != 2)
2781 return -ENXIO;
2782 alts = &iface->altsetting[1];
2783 altsd = get_iface_desc(alts);
2784
2785 if (altsd->bNumEndpoints == 2) {
2786 static const snd_usb_midi_endpoint_info_t ua700_ep = {
2787 .out_cables = 0x0003,
2788 .in_cables = 0x0003
2789 };
2790 static const snd_usb_audio_quirk_t ua700_quirk = {
2791 .type = QUIRK_MIDI_FIXED_ENDPOINT,
2792 .data = &ua700_ep
2793 };
2794 static const snd_usb_midi_endpoint_info_t ua25_ep = {
2795 .out_cables = 0x0001,
2796 .in_cables = 0x0001
2797 };
2798 static const snd_usb_audio_quirk_t ua25_quirk = {
2799 .type = QUIRK_MIDI_FIXED_ENDPOINT,
2800 .data = &ua25_ep
2801 };
2802 if (le16_to_cpu(chip->dev->descriptor.idProduct) == 0x002b)
2803 return snd_usb_create_midi_interface(chip, iface,
2804 &ua700_quirk);
2805 else
2806 return snd_usb_create_midi_interface(chip, iface,
2807 &ua25_quirk);
2808 }
2809
2810 if (altsd->bNumEndpoints != 1)
2811 return -ENXIO;
2812
2813 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2814 if (!fp)
2815 return -ENOMEM;
2816 memcpy(fp, &ua_format, sizeof(*fp));
2817
2818 fp->iface = altsd->bInterfaceNumber;
2819 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2820 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2821 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2822
2823 switch (fp->maxpacksize) {
2824 case 0x120:
2825 fp->rate_max = fp->rate_min = 44100;
2826 break;
2827 case 0x138:
2828 case 0x140:
2829 fp->rate_max = fp->rate_min = 48000;
2830 break;
2831 case 0x258:
2832 case 0x260:
2833 fp->rate_max = fp->rate_min = 96000;
2834 break;
2835 default:
2836 snd_printk(KERN_ERR "unknown sample rate\n");
2837 kfree(fp);
2838 return -ENXIO;
2839 }
2840
2841 stream = (fp->endpoint & USB_DIR_IN)
2842 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2843 err = add_audio_endpoint(chip, stream, fp);
2844 if (err < 0) {
2845 kfree(fp);
2846 return err;
2847 }
2848 usb_set_interface(chip->dev, fp->iface, 0);
2849 return 0;
2850}
2851
2852/*
2853 * Create a stream for an Edirol UA-1000 interface.
2854 */
2855static int create_ua1000_quirk(snd_usb_audio_t *chip, struct usb_interface *iface)
2856{
2857 static const struct audioformat ua1000_format = {
2858 .format = SNDRV_PCM_FORMAT_S32_LE,
2859 .fmt_type = USB_FORMAT_TYPE_I,
2860 .altsetting = 1,
2861 .altset_idx = 1,
2862 .attributes = 0,
2863 .rates = SNDRV_PCM_RATE_CONTINUOUS,
2864 };
2865 struct usb_host_interface *alts;
2866 struct usb_interface_descriptor *altsd;
2867 struct audioformat *fp;
2868 int stream, err;
2869
2870 if (iface->num_altsetting != 2)
2871 return -ENXIO;
2872 alts = &iface->altsetting[1];
2873 altsd = get_iface_desc(alts);
2874 if (alts->extralen != 11 || alts->extra[1] != CS_AUDIO_INTERFACE ||
2875 altsd->bNumEndpoints != 1)
2876 return -ENXIO;
2877
2878 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2879 if (!fp)
2880 return -ENOMEM;
2881 memcpy(fp, &ua1000_format, sizeof(*fp));
2882
2883 fp->channels = alts->extra[4];
2884 fp->iface = altsd->bInterfaceNumber;
2885 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2886 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2887 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2888 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]);
2889
2890 stream = (fp->endpoint & USB_DIR_IN)
2891 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2892 err = add_audio_endpoint(chip, stream, fp);
2893 if (err < 0) {
2894 kfree(fp);
2895 return err;
2896 }
2897 /* FIXME: playback must be synchronized to capture */
2898 usb_set_interface(chip->dev, fp->iface, 0);
2899 return 0;
2900}
2901
2902static int snd_usb_create_quirk(snd_usb_audio_t *chip,
2903 struct usb_interface *iface,
2904 const snd_usb_audio_quirk_t *quirk);
2905
2906/*
2907 * handle the quirks for the contained interfaces
2908 */
2909static int create_composite_quirk(snd_usb_audio_t *chip,
2910 struct usb_interface *iface,
2911 const snd_usb_audio_quirk_t *quirk)
2912{
2913 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
2914 int err;
2915
2916 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
2917 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
2918 if (!iface)
2919 continue;
2920 if (quirk->ifnum != probed_ifnum &&
2921 usb_interface_claimed(iface))
2922 continue;
2923 err = snd_usb_create_quirk(chip, iface, quirk);
2924 if (err < 0)
2925 return err;
2926 if (quirk->ifnum != probed_ifnum)
2927 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2928 }
2929 return 0;
2930}
2931
2932
2933/*
2934 * boot quirks
2935 */
2936
2937#define EXTIGY_FIRMWARE_SIZE_OLD 794
2938#define EXTIGY_FIRMWARE_SIZE_NEW 483
2939
2940static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
2941{
2942 struct usb_host_config *config = dev->actconfig;
2943 int err;
2944
2945 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
2946 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
2947 snd_printdd("sending Extigy boot sequence...\n");
2948 /* Send message to force it to reconnect with full interface. */
2949 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
2950 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
2951 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
2952 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
2953 &dev->descriptor, sizeof(dev->descriptor));
2954 config = dev->actconfig;
2955 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
2956 err = usb_reset_configuration(dev);
2957 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
2958 snd_printdd("extigy_boot: new boot length = %d\n",
2959 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
2960 return -ENODEV; /* quit this anyway */
2961 }
2962 return 0;
2963}
2964
2965
2966/*
2967 * audio-interface quirks
2968 *
2969 * returns zero if no standard audio/MIDI parsing is needed.
2970 * returns a postive value if standard audio/midi interfaces are parsed
2971 * after this.
2972 * returns a negative value at error.
2973 */
2974static int snd_usb_create_quirk(snd_usb_audio_t *chip,
2975 struct usb_interface *iface,
2976 const snd_usb_audio_quirk_t *quirk)
2977{
2978 switch (quirk->type) {
2979 case QUIRK_MIDI_FIXED_ENDPOINT:
2980 case QUIRK_MIDI_YAMAHA:
2981 case QUIRK_MIDI_MIDIMAN:
2982 case QUIRK_MIDI_NOVATION:
2983 case QUIRK_MIDI_MOTU:
2984 case QUIRK_MIDI_EMAGIC:
2985 return snd_usb_create_midi_interface(chip, iface, quirk);
2986 case QUIRK_COMPOSITE:
2987 return create_composite_quirk(chip, iface, quirk);
2988 case QUIRK_AUDIO_FIXED_ENDPOINT:
2989 return create_fixed_stream_quirk(chip, iface, quirk);
2990 case QUIRK_AUDIO_STANDARD_INTERFACE:
2991 case QUIRK_MIDI_STANDARD_INTERFACE:
2992 return create_standard_interface_quirk(chip, iface, quirk);
2993 case QUIRK_AUDIO_EDIROL_UA700_UA25:
2994 return create_ua700_ua25_quirk(chip, iface);
2995 case QUIRK_AUDIO_EDIROL_UA1000:
2996 return create_ua1000_quirk(chip, iface);
2997 case QUIRK_IGNORE_INTERFACE:
2998 return 0;
2999 default:
3000 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
3001 return -ENXIO;
3002 }
3003}
3004
3005
3006/*
3007 * common proc files to show the usb device info
3008 */
3009static void proc_audio_usbbus_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
3010{
3011 snd_usb_audio_t *chip = entry->private_data;
3012 if (! chip->shutdown)
3013 snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
3014}
3015
3016static void proc_audio_usbid_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
3017{
3018 snd_usb_audio_t *chip = entry->private_data;
3019 if (! chip->shutdown)
3020 snd_iprintf(buffer, "%04x:%04x\n",
3021 le16_to_cpu(chip->dev->descriptor.idVendor),
3022 le16_to_cpu(chip->dev->descriptor.idProduct));
3023}
3024
3025static void snd_usb_audio_create_proc(snd_usb_audio_t *chip)
3026{
3027 snd_info_entry_t *entry;
3028 if (! snd_card_proc_new(chip->card, "usbbus", &entry))
3029 snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbbus_read);
3030 if (! snd_card_proc_new(chip->card, "usbid", &entry))
3031 snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbid_read);
3032}
3033
3034/*
3035 * free the chip instance
3036 *
3037 * here we have to do not much, since pcm and controls are already freed
3038 *
3039 */
3040
3041static int snd_usb_audio_free(snd_usb_audio_t *chip)
3042{
3043 kfree(chip);
3044 return 0;
3045}
3046
3047static int snd_usb_audio_dev_free(snd_device_t *device)
3048{
3049 snd_usb_audio_t *chip = device->device_data;
3050 return snd_usb_audio_free(chip);
3051}
3052
3053
3054/*
3055 * create a chip instance and set its names.
3056 */
3057static int snd_usb_audio_create(struct usb_device *dev, int idx,
3058 const snd_usb_audio_quirk_t *quirk,
3059 snd_usb_audio_t **rchip)
3060{
3061 snd_card_t *card;
3062 snd_usb_audio_t *chip;
3063 int err, len;
3064 char component[14];
3065 static snd_device_ops_t ops = {
3066 .dev_free = snd_usb_audio_dev_free,
3067 };
3068
3069 *rchip = NULL;
3070
3071 if (snd_usb_get_speed(dev) != USB_SPEED_FULL &&
3072 snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
3073 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
3074 return -ENXIO;
3075 }
3076
3077 card = snd_card_new(index[idx], id[idx], THIS_MODULE, 0);
3078 if (card == NULL) {
3079 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
3080 return -ENOMEM;
3081 }
3082
3083 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
3084 if (! chip) {
3085 snd_card_free(card);
3086 return -ENOMEM;
3087 }
3088
3089 chip->index = idx;
3090 chip->dev = dev;
3091 chip->card = card;
3092 INIT_LIST_HEAD(&chip->pcm_list);
3093 INIT_LIST_HEAD(&chip->midi_list);
3094
3095 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
3096 snd_usb_audio_free(chip);
3097 snd_card_free(card);
3098 return err;
3099 }
3100
3101 strcpy(card->driver, "USB-Audio");
3102 sprintf(component, "USB%04x:%04x",
3103 le16_to_cpu(dev->descriptor.idVendor),
3104 le16_to_cpu(dev->descriptor.idProduct));
3105 snd_component_add(card, component);
3106
3107 /* retrieve the device string as shortname */
3108 if (quirk && quirk->product_name) {
3109 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
3110 } else {
3111 if (!dev->descriptor.iProduct ||
3112 usb_string(dev, dev->descriptor.iProduct,
3113 card->shortname, sizeof(card->shortname)) <= 0) {
3114 /* no name available from anywhere, so use ID */
3115 sprintf(card->shortname, "USB Device %#04x:%#04x",
3116 le16_to_cpu(dev->descriptor.idVendor),
3117 le16_to_cpu(dev->descriptor.idProduct));
3118 }
3119 }
3120
3121 /* retrieve the vendor and device strings as longname */
3122 if (quirk && quirk->vendor_name) {
3123 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
3124 } else {
3125 if (dev->descriptor.iManufacturer)
3126 len = usb_string(dev, dev->descriptor.iManufacturer,
3127 card->longname, sizeof(card->longname));
3128 else
3129 len = 0;
3130 /* we don't really care if there isn't any vendor string */
3131 }
3132 if (len > 0)
3133 strlcat(card->longname, " ", sizeof(card->longname));
3134
3135 strlcat(card->longname, card->shortname, sizeof(card->longname));
3136
3137 len = strlcat(card->longname, " at ", sizeof(card->longname));
3138
3139 if (len < sizeof(card->longname))
3140 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
3141
3142 strlcat(card->longname,
3143 snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" : ", high speed",
3144 sizeof(card->longname));
3145
3146 snd_usb_audio_create_proc(chip);
3147
3148 snd_card_set_dev(card, &dev->dev);
3149
3150 *rchip = chip;
3151 return 0;
3152}
3153
3154
3155/*
3156 * probe the active usb device
3157 *
3158 * note that this can be called multiple times per a device, when it
3159 * includes multiple audio control interfaces.
3160 *
3161 * thus we check the usb device pointer and creates the card instance
3162 * only at the first time. the successive calls of this function will
3163 * append the pcm interface to the corresponding card.
3164 */
3165static void *snd_usb_audio_probe(struct usb_device *dev,
3166 struct usb_interface *intf,
3167 const struct usb_device_id *usb_id)
3168{
3169 struct usb_host_config *config = dev->actconfig;
3170 const snd_usb_audio_quirk_t *quirk = (const snd_usb_audio_quirk_t *)usb_id->driver_info;
3171 int i, err;
3172 snd_usb_audio_t *chip;
3173 struct usb_host_interface *alts;
3174 int ifnum;
3175
3176 alts = &intf->altsetting[0];
3177 ifnum = get_iface_desc(alts)->bInterfaceNumber;
3178
3179 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
3180 goto __err_val;
3181
3182 /* SB Extigy needs special boot-up sequence */
3183 /* if more models come, this will go to the quirk list. */
3184 if (le16_to_cpu(dev->descriptor.idVendor) == 0x041e &&
3185 le16_to_cpu(dev->descriptor.idProduct) == 0x3000) {
3186 if (snd_usb_extigy_boot_quirk(dev, intf) < 0)
3187 goto __err_val;
3188 config = dev->actconfig;
3189 }
3190
3191 /*
3192 * found a config. now register to ALSA
3193 */
3194
3195 /* check whether it's already registered */
3196 chip = NULL;
3197 down(&register_mutex);
3198 for (i = 0; i < SNDRV_CARDS; i++) {
3199 if (usb_chip[i] && usb_chip[i]->dev == dev) {
3200 if (usb_chip[i]->shutdown) {
3201 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
3202 goto __error;
3203 }
3204 chip = usb_chip[i];
3205 break;
3206 }
3207 }
3208 if (! chip) {
3209 /* it's a fresh one.
3210 * now look for an empty slot and create a new card instance
3211 */
3212 /* first, set the current configuration for this device */
3213 if (usb_reset_configuration(dev) < 0) {
3214 snd_printk(KERN_ERR "cannot reset configuration (value 0x%x)\n", get_cfg_desc(config)->bConfigurationValue);
3215 goto __error;
3216 }
3217 for (i = 0; i < SNDRV_CARDS; i++)
3218 if (enable[i] && ! usb_chip[i] &&
3219 (vid[i] == -1 || vid[i] == le16_to_cpu(dev->descriptor.idVendor)) &&
3220 (pid[i] == -1 || pid[i] == le16_to_cpu(dev->descriptor.idProduct))) {
3221 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
3222 goto __error;
3223 }
3224 break;
3225 }
3226 if (! chip) {
3227 snd_printk(KERN_ERR "no available usb audio device\n");
3228 goto __error;
3229 }
3230 }
3231
3232 err = 1; /* continue */
3233 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
3234 /* need some special handlings */
3235 if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)
3236 goto __error;
3237 }
3238
3239 if (err > 0) {
3240 /* create normal USB audio interfaces */
3241 if (snd_usb_create_streams(chip, ifnum) < 0 ||
3242 snd_usb_create_mixer(chip, ifnum) < 0) {
3243 goto __error;
3244 }
3245 }
3246
3247 /* we are allowed to call snd_card_register() many times */
3248 if (snd_card_register(chip->card) < 0) {
3249 goto __error;
3250 }
3251
3252 usb_chip[chip->index] = chip;
3253 chip->num_interfaces++;
3254 up(&register_mutex);
3255 return chip;
3256
3257 __error:
3258 if (chip && !chip->num_interfaces)
3259 snd_card_free(chip->card);
3260 up(&register_mutex);
3261 __err_val:
3262 return NULL;
3263}
3264
3265/*
3266 * we need to take care of counter, since disconnection can be called also
3267 * many times as well as usb_audio_probe().
3268 */
3269static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
3270{
3271 snd_usb_audio_t *chip;
3272 snd_card_t *card;
3273 struct list_head *p;
3274
3275 if (ptr == (void *)-1L)
3276 return;
3277
3278 chip = ptr;
3279 card = chip->card;
3280 down(&register_mutex);
3281 chip->shutdown = 1;
3282 chip->num_interfaces--;
3283 if (chip->num_interfaces <= 0) {
3284 snd_card_disconnect(card);
3285 /* release the pcm resources */
3286 list_for_each(p, &chip->pcm_list) {
Clemens Ladischee733392005-04-25 10:34:13 +02003287 snd_usb_stream_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 }
3289 /* release the midi resources */
3290 list_for_each(p, &chip->midi_list) {
Clemens Ladischee733392005-04-25 10:34:13 +02003291 snd_usbmidi_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 }
3293 usb_chip[chip->index] = NULL;
3294 up(&register_mutex);
Karsten Wiese230cd5e2005-04-20 10:12:35 +02003295 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 } else {
3297 up(&register_mutex);
3298 }
3299}
3300
3301/*
3302 * new 2.5 USB kernel API
3303 */
3304static int usb_audio_probe(struct usb_interface *intf,
3305 const struct usb_device_id *id)
3306{
3307 void *chip;
3308 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
3309 if (chip) {
3310 dev_set_drvdata(&intf->dev, chip);
3311 return 0;
3312 } else
3313 return -EIO;
3314}
3315
3316static void usb_audio_disconnect(struct usb_interface *intf)
3317{
3318 snd_usb_audio_disconnect(interface_to_usbdev(intf),
3319 dev_get_drvdata(&intf->dev));
3320}
3321
3322
3323static int __init snd_usb_audio_init(void)
3324{
3325 if (nrpacks < MIN_PACKS_URB || nrpacks > MAX_PACKS) {
3326 printk(KERN_WARNING "invalid nrpacks value.\n");
3327 return -EINVAL;
3328 }
3329 usb_register(&usb_audio_driver);
3330 return 0;
3331}
3332
3333
3334static void __exit snd_usb_audio_cleanup(void)
3335{
3336 usb_deregister(&usb_audio_driver);
3337}
3338
3339module_init(snd_usb_audio_init);
3340module_exit(snd_usb_audio_cleanup);