blob: 95729730bcdc2edd2bccde5868108695f020cd1d [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>
Clemens Ladisch6207e512005-08-15 08:35:25 +020048#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/moduleparam.h>
50#include <sound/core.h>
51#include <sound/info.h>
52#include <sound/pcm.h>
53#include <sound/pcm_params.h>
54#include <sound/initval.h>
55
56#include "usbaudio.h"
57
58
59MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
60MODULE_DESCRIPTION("USB Audio");
61MODULE_LICENSE("GPL");
62MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
63
64
65static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
66static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
67static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
68static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Vendor ID for this card */
69static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Product ID for this card */
70static int nrpacks = 4; /* max. number of packets per urb */
71static int async_unlink = 1;
72
73module_param_array(index, int, NULL, 0444);
74MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
75module_param_array(id, charp, NULL, 0444);
76MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
77module_param_array(enable, bool, NULL, 0444);
78MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
79module_param_array(vid, int, NULL, 0444);
80MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
81module_param_array(pid, int, NULL, 0444);
82MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
Clemens Ladisch71d848c2005-08-12 15:18:00 +020083module_param(nrpacks, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
85module_param(async_unlink, bool, 0444);
86MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
87
88
89/*
90 * debug the h/w constraints
91 */
92/* #define HW_CONST_DEBUG */
93
94
95/*
96 *
97 */
98
99#define MAX_PACKS 10
100#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
Clemens Ladisch15a24c02005-08-12 08:25:26 +0200101#define MAX_URBS 8
Clemens Ladisch604cf492005-05-17 09:15:27 +0200102#define SYNC_URBS 4 /* always four urbs for sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#define MIN_PACKS_URB 1 /* minimum 1 packet per urb */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105struct audioformat {
106 struct list_head list;
107 snd_pcm_format_t format; /* format type */
108 unsigned int channels; /* # channels */
109 unsigned int fmt_type; /* USB audio format type (1-3) */
110 unsigned int frame_size; /* samples per frame for non-audio */
111 int iface; /* interface number */
112 unsigned char altsetting; /* corresponding alternate setting */
113 unsigned char altset_idx; /* array index of altenate setting */
114 unsigned char attributes; /* corresponding attributes of cs endpoint */
115 unsigned char endpoint; /* endpoint */
116 unsigned char ep_attr; /* endpoint attributes */
117 unsigned int maxpacksize; /* max. packet size */
118 unsigned int rates; /* rate bitmasks */
119 unsigned int rate_min, rate_max; /* min/max rates */
120 unsigned int nr_rates; /* number of rate table entries */
121 unsigned int *rate_table; /* rate table */
122};
123
Takashi Iwai86e07d32005-11-17 15:08:02 +0100124struct snd_usb_substream;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126struct snd_urb_ctx {
127 struct urb *urb;
Clemens Ladisch55851f72005-08-15 08:34:16 +0200128 unsigned int buffer_size; /* size of data buffer, if data URB */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100129 struct snd_usb_substream *subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 int index; /* index for urb array */
131 int packets; /* number of packets per urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
134struct snd_urb_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100135 int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
136 int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
137 int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
138 int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
141struct snd_usb_substream {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100142 struct snd_usb_stream *stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct usb_device *dev;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100144 struct snd_pcm_substream *pcm_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int direction; /* playback or capture */
146 int interface; /* current interface */
147 int endpoint; /* assigned endpoint */
148 struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
149 unsigned int cur_rate; /* current rate (for hw_params callback) */
150 unsigned int period_bytes; /* current period bytes (for hw_params callback) */
151 unsigned int format; /* USB data format */
152 unsigned int datapipe; /* the data i/o pipe */
153 unsigned int syncpipe; /* 1 - async out or adaptive in */
Clemens Ladisch573567e2005-06-27 08:17:30 +0200154 unsigned int datainterval; /* log_2 of data packet interval */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
156 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
157 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
158 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
159 unsigned int phase; /* phase accumulator */
160 unsigned int maxpacksize; /* max packet size in bytes */
161 unsigned int maxframesize; /* max packet size in frames */
162 unsigned int curpacksize; /* current packet size in bytes (for capture) */
163 unsigned int curframesize; /* current packet size in frames (for capture) */
164 unsigned int fill_max: 1; /* fill max packet size always */
165 unsigned int fmt_type; /* USB audio format type (1-3) */
Clemens Ladisch9624ea82005-08-15 08:25:24 +0200166 unsigned int packs_per_ms; /* packets per millisecond (for playback) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 unsigned int running: 1; /* running status */
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned int hwptr_done; /* processed frame position in the buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 unsigned int transfer_done; /* processed frames since last period update */
172 unsigned long active_mask; /* bitmask of active urbs */
173 unsigned long unlink_mask; /* bitmask of unlinked urbs */
174
175 unsigned int nurbs; /* # urbs */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100176 struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */
177 struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */
Clemens Ladisch55851f72005-08-15 08:34:16 +0200178 char *syncbuf; /* sync buffer for all sync URBs */
179 dma_addr_t sync_dma; /* DMA address of syncbuf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 u64 formats; /* format bitmasks (all or'ed) */
182 unsigned int num_formats; /* number of supported audio formats (list) */
183 struct list_head fmt_list; /* format list */
184 spinlock_t lock;
185
186 struct snd_urb_ops ops; /* callbacks (must be filled at init) */
187};
188
189
190struct snd_usb_stream {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100191 struct snd_usb_audio *chip;
192 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int pcm_index;
194 unsigned int fmt_type; /* USB audio format type (1-3) */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100195 struct snd_usb_substream substream[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct list_head list;
197};
198
199
200/*
201 * we keep the snd_usb_audio_t instances by ourselves for merging
202 * the all interfaces on the same card as one sound device.
203 */
204
205static DECLARE_MUTEX(register_mutex);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100206static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208
209/*
210 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
211 * this will overflow at approx 524 kHz
212 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700213static inline unsigned get_usb_full_speed_rate(unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 return ((rate << 13) + 62) / 125;
216}
217
218/*
219 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
220 * this will overflow at approx 4 MHz
221 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700222static inline unsigned get_usb_high_speed_rate(unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 return ((rate << 10) + 62) / 125;
225}
226
227/* convert our full speed USB rate into sampling rate in Hz */
Jesper Juhl77933d72005-07-27 11:46:09 -0700228static inline unsigned get_full_speed_hz(unsigned int usb_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 return (usb_rate * 125 + (1 << 12)) >> 13;
231}
232
233/* convert our high speed USB rate into sampling rate in Hz */
Jesper Juhl77933d72005-07-27 11:46:09 -0700234static inline unsigned get_high_speed_hz(unsigned int usb_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 return (usb_rate * 125 + (1 << 9)) >> 10;
237}
238
239
240/*
241 * prepare urb for full speed capture sync pipe
242 *
243 * fill the length and offset of each urb descriptor.
244 * the fixed 10.14 frequency is passed through the pipe.
245 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100246static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
247 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct urb *urb)
249{
250 unsigned char *cp = urb->transfer_buffer;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100251 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200254 urb->iso_frame_desc[0].length = 3;
255 urb->iso_frame_desc[0].offset = 0;
256 cp[0] = subs->freqn >> 2;
257 cp[1] = subs->freqn >> 10;
258 cp[2] = subs->freqn >> 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return 0;
260}
261
262/*
263 * prepare urb for high speed capture sync pipe
264 *
265 * fill the length and offset of each urb descriptor.
266 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
267 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100268static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
269 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct urb *urb)
271{
272 unsigned char *cp = urb->transfer_buffer;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100273 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200276 urb->iso_frame_desc[0].length = 4;
277 urb->iso_frame_desc[0].offset = 0;
278 cp[0] = subs->freqn;
279 cp[1] = subs->freqn >> 8;
280 cp[2] = subs->freqn >> 16;
281 cp[3] = subs->freqn >> 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return 0;
283}
284
285/*
286 * process after capture sync complete
287 * - nothing to do
288 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100289static int retire_capture_sync_urb(struct snd_usb_substream *subs,
290 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 struct urb *urb)
292{
293 return 0;
294}
295
296/*
297 * prepare urb for capture data pipe
298 *
299 * fill the offset and length of each descriptor.
300 *
301 * we use a temporary buffer to write the captured data.
302 * since the length of written data is determined by host, we cannot
303 * write onto the pcm buffer directly... the data is thus copied
304 * later at complete callback to the global buffer.
305 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100306static int prepare_capture_urb(struct snd_usb_substream *subs,
307 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct urb *urb)
309{
310 int i, offs;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100311 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 offs = 0;
314 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 for (i = 0; i < ctx->packets; i++) {
316 urb->iso_frame_desc[i].offset = offs;
317 urb->iso_frame_desc[i].length = subs->curpacksize;
318 offs += subs->curpacksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 urb->transfer_buffer_length = offs;
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200321 urb->number_of_packets = ctx->packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322#if 0 // for check
323 if (! urb->bandwidth) {
324 int bustime;
325 bustime = usb_check_bandwidth(urb->dev, urb);
326 if (bustime < 0)
327 return bustime;
328 printk("urb %d: bandwidth = %d (packets = %d)\n", ctx->index, bustime, urb->number_of_packets);
329 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
330 }
331#endif // for check
332 return 0;
333}
334
335/*
336 * process after capture complete
337 *
338 * copy the data from each desctiptor to the pcm buffer, and
339 * update the current position.
340 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100341static int retire_capture_urb(struct snd_usb_substream *subs,
342 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct urb *urb)
344{
345 unsigned long flags;
346 unsigned char *cp;
347 int i;
348 unsigned int stride, len, oldptr;
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200349 int period_elapsed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 stride = runtime->frame_bits >> 3;
352
353 for (i = 0; i < urb->number_of_packets; i++) {
354 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
355 if (urb->iso_frame_desc[i].status) {
356 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
357 // continue;
358 }
359 len = urb->iso_frame_desc[i].actual_length / stride;
360 if (! len)
361 continue;
362 /* update the current pointer */
363 spin_lock_irqsave(&subs->lock, flags);
364 oldptr = subs->hwptr_done;
365 subs->hwptr_done += len;
366 if (subs->hwptr_done >= runtime->buffer_size)
367 subs->hwptr_done -= runtime->buffer_size;
368 subs->transfer_done += len;
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200369 if (subs->transfer_done >= runtime->period_size) {
370 subs->transfer_done -= runtime->period_size;
371 period_elapsed = 1;
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 spin_unlock_irqrestore(&subs->lock, flags);
374 /* copy a data chunk */
375 if (oldptr + len > runtime->buffer_size) {
376 unsigned int cnt = runtime->buffer_size - oldptr;
377 unsigned int blen = cnt * stride;
378 memcpy(runtime->dma_area + oldptr * stride, cp, blen);
379 memcpy(runtime->dma_area, cp + blen, len * stride - blen);
380 } else {
381 memcpy(runtime->dma_area + oldptr * stride, cp, len * stride);
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200384 if (period_elapsed)
385 snd_pcm_period_elapsed(subs->pcm_substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
387}
388
389
390/*
391 * prepare urb for full speed playback sync pipe
392 *
393 * set up the offset and length to receive the current frequency.
394 */
395
Takashi Iwai86e07d32005-11-17 15:08:02 +0100396static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
397 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct urb *urb)
399{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100400 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200403 urb->iso_frame_desc[0].length = 3;
404 urb->iso_frame_desc[0].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 0;
406}
407
408/*
409 * prepare urb for high speed playback sync pipe
410 *
411 * set up the offset and length to receive the current frequency.
412 */
413
Takashi Iwai86e07d32005-11-17 15:08:02 +0100414static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
415 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct urb *urb)
417{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100418 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200421 urb->iso_frame_desc[0].length = 4;
422 urb->iso_frame_desc[0].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
424}
425
426/*
427 * process after full speed playback sync complete
428 *
429 * retrieve the current 10.14 frequency from pipe, and set it.
430 * the value is referred in prepare_playback_urb().
431 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100432static int retire_playback_sync_urb(struct snd_usb_substream *subs,
433 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct urb *urb)
435{
Clemens Ladischca810902005-05-02 08:55:54 +0200436 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 unsigned long flags;
438
Clemens Ladischca810902005-05-02 08:55:54 +0200439 if (urb->iso_frame_desc[0].status == 0 &&
440 urb->iso_frame_desc[0].actual_length == 3) {
441 f = combine_triple((u8*)urb->transfer_buffer) << 2;
Clemens Ladisch50cdbf12005-05-13 07:44:13 +0200442 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
443 spin_lock_irqsave(&subs->lock, flags);
444 subs->freqm = f;
445 spin_unlock_irqrestore(&subs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
449 return 0;
450}
451
452/*
453 * process after high speed playback sync complete
454 *
455 * retrieve the current 12.13 frequency from pipe, and set it.
456 * the value is referred in prepare_playback_urb().
457 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100458static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
459 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct urb *urb)
461{
Clemens Ladischca810902005-05-02 08:55:54 +0200462 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 unsigned long flags;
464
Clemens Ladischca810902005-05-02 08:55:54 +0200465 if (urb->iso_frame_desc[0].status == 0 &&
466 urb->iso_frame_desc[0].actual_length == 4) {
467 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
Clemens Ladisch50cdbf12005-05-13 07:44:13 +0200468 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
469 spin_lock_irqsave(&subs->lock, flags);
470 subs->freqm = f;
471 spin_unlock_irqrestore(&subs->lock, flags);
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
475 return 0;
476}
477
478/*
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100479 * Prepare urb for streaming before playback starts.
480 *
Clemens Ladisch4b284922006-01-12 08:17:49 +0100481 * We don't yet have data, so we send a frame of silence.
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100482 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100483static int prepare_startup_playback_urb(struct snd_usb_substream *subs,
484 struct snd_pcm_runtime *runtime,
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100485 struct urb *urb)
486{
Clemens Ladisch4b284922006-01-12 08:17:49 +0100487 unsigned int i, offs, counts;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100488 struct snd_urb_ctx *ctx = urb->context;
Clemens Ladisch4b284922006-01-12 08:17:49 +0100489 int stride = runtime->frame_bits >> 3;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100490
Clemens Ladisch4b284922006-01-12 08:17:49 +0100491 offs = 0;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100492 urb->dev = ctx->subs->dev;
493 urb->number_of_packets = subs->packs_per_ms;
494 for (i = 0; i < subs->packs_per_ms; ++i) {
Clemens Ladisch4b284922006-01-12 08:17:49 +0100495 /* calculate the size of a packet */
496 if (subs->fill_max)
497 counts = subs->maxframesize; /* fixed */
498 else {
499 subs->phase = (subs->phase & 0xffff)
500 + (subs->freqm << subs->datainterval);
501 counts = subs->phase >> 16;
502 if (counts > subs->maxframesize)
503 counts = subs->maxframesize;
504 }
505 urb->iso_frame_desc[i].offset = offs * stride;
506 urb->iso_frame_desc[i].length = counts * stride;
507 offs += counts;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100508 }
Clemens Ladisch4b284922006-01-12 08:17:49 +0100509 urb->transfer_buffer_length = offs * stride;
510 memset(urb->transfer_buffer,
511 subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
512 offs * stride);
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100513 return 0;
514}
515
516/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 * prepare urb for playback data pipe
518 *
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200519 * Since a URB can handle only a single linear buffer, we must use double
520 * buffering when the data to be transferred overflows the buffer boundary.
521 * To avoid inconsistencies when updating hwptr_done, we use double buffering
522 * for all URBs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100524static int prepare_playback_urb(struct snd_usb_substream *subs,
525 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 struct urb *urb)
527{
528 int i, stride, offs;
529 unsigned int counts;
530 unsigned long flags;
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200531 int period_elapsed = 0;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100532 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 stride = runtime->frame_bits >> 3;
535
536 offs = 0;
537 urb->dev = ctx->subs->dev; /* we need to set this at each time */
538 urb->number_of_packets = 0;
539 spin_lock_irqsave(&subs->lock, flags);
540 for (i = 0; i < ctx->packets; i++) {
541 /* calculate the size of a packet */
542 if (subs->fill_max)
543 counts = subs->maxframesize; /* fixed */
544 else {
Clemens Ladisch573567e2005-06-27 08:17:30 +0200545 subs->phase = (subs->phase & 0xffff)
546 + (subs->freqm << subs->datainterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 counts = subs->phase >> 16;
548 if (counts > subs->maxframesize)
549 counts = subs->maxframesize;
550 }
551 /* set up descriptor */
552 urb->iso_frame_desc[i].offset = offs * stride;
553 urb->iso_frame_desc[i].length = counts * stride;
554 offs += counts;
555 urb->number_of_packets++;
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200556 subs->transfer_done += counts;
557 if (subs->transfer_done >= runtime->period_size) {
558 subs->transfer_done -= runtime->period_size;
559 period_elapsed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (subs->fmt_type == USB_FORMAT_TYPE_II) {
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200561 if (subs->transfer_done > 0) {
562 /* FIXME: fill-max mode is not
563 * supported yet */
564 offs -= subs->transfer_done;
565 counts -= subs->transfer_done;
566 urb->iso_frame_desc[i].length =
567 counts * stride;
568 subs->transfer_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 i++;
571 if (i < ctx->packets) {
572 /* add a transfer delimiter */
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200573 urb->iso_frame_desc[i].offset =
574 offs * stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 urb->iso_frame_desc[i].length = 0;
576 urb->number_of_packets++;
577 }
Clemens Ladisch9624ea82005-08-15 08:25:24 +0200578 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Clemens Ladisch9624ea82005-08-15 08:25:24 +0200581 /* finish at the frame boundary at/after the period boundary */
582 if (period_elapsed &&
583 (i & (subs->packs_per_ms - 1)) == subs->packs_per_ms - 1)
584 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200586 if (subs->hwptr_done + offs > runtime->buffer_size) {
587 /* err, the transferred area goes over buffer boundary. */
588 unsigned int len = runtime->buffer_size - subs->hwptr_done;
589 memcpy(urb->transfer_buffer,
590 runtime->dma_area + subs->hwptr_done * stride,
591 len * stride);
592 memcpy(urb->transfer_buffer + len * stride,
593 runtime->dma_area,
594 (offs - len) * stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 } else {
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200596 memcpy(urb->transfer_buffer,
597 runtime->dma_area + subs->hwptr_done * stride,
598 offs * stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200600 subs->hwptr_done += offs;
601 if (subs->hwptr_done >= runtime->buffer_size)
602 subs->hwptr_done -= runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 spin_unlock_irqrestore(&subs->lock, flags);
604 urb->transfer_buffer_length = offs * stride;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100605 if (period_elapsed)
606 snd_pcm_period_elapsed(subs->pcm_substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return 0;
608}
609
610/*
611 * process after playback data complete
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200612 * - nothing to do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100614static int retire_playback_urb(struct snd_usb_substream *subs,
615 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 struct urb *urb)
617{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 0;
619}
620
621
622/*
623 */
624static struct snd_urb_ops audio_urb_ops[2] = {
625 {
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100626 .prepare = prepare_startup_playback_urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 .retire = retire_playback_urb,
628 .prepare_sync = prepare_playback_sync_urb,
629 .retire_sync = retire_playback_sync_urb,
630 },
631 {
632 .prepare = prepare_capture_urb,
633 .retire = retire_capture_urb,
634 .prepare_sync = prepare_capture_sync_urb,
635 .retire_sync = retire_capture_sync_urb,
636 },
637};
638
639static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
640 {
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100641 .prepare = prepare_startup_playback_urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 .retire = retire_playback_urb,
643 .prepare_sync = prepare_playback_sync_urb_hs,
644 .retire_sync = retire_playback_sync_urb_hs,
645 },
646 {
647 .prepare = prepare_capture_urb,
648 .retire = retire_capture_urb,
649 .prepare_sync = prepare_capture_sync_urb_hs,
650 .retire_sync = retire_capture_sync_urb,
651 },
652};
653
654/*
655 * complete callback from data urb
656 */
657static void snd_complete_urb(struct urb *urb, struct pt_regs *regs)
658{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100659 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
660 struct snd_usb_substream *subs = ctx->subs;
661 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 int err = 0;
663
664 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
665 ! subs->running || /* can be stopped during retire callback */
666 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
667 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
668 clear_bit(ctx->index, &subs->active_mask);
669 if (err < 0) {
670 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
671 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
672 }
673 }
674}
675
676
677/*
678 * complete callback from sync urb
679 */
680static void snd_complete_sync_urb(struct urb *urb, struct pt_regs *regs)
681{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100682 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
683 struct snd_usb_substream *subs = ctx->subs;
684 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int err = 0;
686
687 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
688 ! subs->running || /* can be stopped during retire callback */
689 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
690 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
691 clear_bit(ctx->index + 16, &subs->active_mask);
692 if (err < 0) {
693 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
694 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
695 }
696 }
697}
698
699
Clemens Ladisch6207e512005-08-15 08:35:25 +0200700/* get the physical page pointer at the given offset */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100701static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
Clemens Ladisch6207e512005-08-15 08:35:25 +0200702 unsigned long offset)
703{
704 void *pageptr = subs->runtime->dma_area + offset;
705 return vmalloc_to_page(pageptr);
706}
707
708/* allocate virtual buffer; may be called more than once */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100709static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, size_t size)
Clemens Ladisch6207e512005-08-15 08:35:25 +0200710{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100711 struct snd_pcm_runtime *runtime = subs->runtime;
Clemens Ladisch6207e512005-08-15 08:35:25 +0200712 if (runtime->dma_area) {
713 if (runtime->dma_bytes >= size)
714 return 0; /* already large enough */
Takashi Iwaib1d57762005-10-10 11:56:31 +0200715 vfree(runtime->dma_area);
Clemens Ladisch6207e512005-08-15 08:35:25 +0200716 }
Takashi Iwaib1d57762005-10-10 11:56:31 +0200717 runtime->dma_area = vmalloc(size);
Clemens Ladisch6207e512005-08-15 08:35:25 +0200718 if (! runtime->dma_area)
719 return -ENOMEM;
720 runtime->dma_bytes = size;
721 return 0;
722}
723
724/* free virtual buffer; may be called more than once */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100725static int snd_pcm_free_vmalloc_buffer(struct snd_pcm_substream *subs)
Clemens Ladisch6207e512005-08-15 08:35:25 +0200726{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100727 struct snd_pcm_runtime *runtime = subs->runtime;
Clemens Ladisch6207e512005-08-15 08:35:25 +0200728 if (runtime->dma_area) {
Takashi Iwaib1d57762005-10-10 11:56:31 +0200729 vfree(runtime->dma_area);
Clemens Ladisch6207e512005-08-15 08:35:25 +0200730 runtime->dma_area = NULL;
731 }
732 return 0;
733}
734
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/*
737 * unlink active urbs.
738 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100739static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 unsigned int i;
742 int async;
743
744 subs->running = 0;
745
746 if (!force && subs->stream->chip->shutdown) /* to be sure... */
747 return -EBADFD;
748
749 async = !can_sleep && async_unlink;
750
751 if (! async && in_interrupt())
752 return 0;
753
754 for (i = 0; i < subs->nurbs; i++) {
755 if (test_bit(i, &subs->active_mask)) {
756 if (! test_and_set_bit(i, &subs->unlink_mask)) {
757 struct urb *u = subs->dataurb[i].urb;
Alan Sternb375a042005-07-29 16:11:07 -0400758 if (async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 usb_unlink_urb(u);
Alan Sternb375a042005-07-29 16:11:07 -0400760 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 usb_kill_urb(u);
762 }
763 }
764 }
765 if (subs->syncpipe) {
766 for (i = 0; i < SYNC_URBS; i++) {
767 if (test_bit(i+16, &subs->active_mask)) {
768 if (! test_and_set_bit(i+16, &subs->unlink_mask)) {
769 struct urb *u = subs->syncurb[i].urb;
Alan Sternb375a042005-07-29 16:11:07 -0400770 if (async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 usb_unlink_urb(u);
Alan Sternb375a042005-07-29 16:11:07 -0400772 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 usb_kill_urb(u);
774 }
775 }
776 }
777 }
778 return 0;
779}
780
781
782/*
783 * set up and start data/sync urbs
784 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100785static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 unsigned int i;
788 int err;
789
790 if (subs->stream->chip->shutdown)
791 return -EBADFD;
792
793 for (i = 0; i < subs->nurbs; i++) {
794 snd_assert(subs->dataurb[i].urb, return -EINVAL);
795 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
796 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
797 goto __error;
798 }
799 }
800 if (subs->syncpipe) {
801 for (i = 0; i < SYNC_URBS; i++) {
802 snd_assert(subs->syncurb[i].urb, return -EINVAL);
803 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
804 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
805 goto __error;
806 }
807 }
808 }
809
810 subs->active_mask = 0;
811 subs->unlink_mask = 0;
812 subs->running = 1;
813 for (i = 0; i < subs->nurbs; i++) {
814 if ((err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC)) < 0) {
815 snd_printk(KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
816 goto __error;
817 }
818 set_bit(i, &subs->active_mask);
819 }
820 if (subs->syncpipe) {
821 for (i = 0; i < SYNC_URBS; i++) {
822 if ((err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC)) < 0) {
823 snd_printk(KERN_ERR "cannot submit syncpipe for urb %d, err = %d\n", i, err);
824 goto __error;
825 }
826 set_bit(i + 16, &subs->active_mask);
827 }
828 }
829 return 0;
830
831 __error:
832 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
833 deactivate_urbs(subs, 0, 0);
834 return -EPIPE;
835}
836
837
838/*
839 * wait until all urbs are processed.
840 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100841static int wait_clear_urbs(struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Nishanth Aravamudanb27c1872005-07-09 10:54:37 +0200843 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 unsigned int i;
845 int alive;
846
847 do {
848 alive = 0;
849 for (i = 0; i < subs->nurbs; i++) {
850 if (test_bit(i, &subs->active_mask))
851 alive++;
852 }
853 if (subs->syncpipe) {
854 for (i = 0; i < SYNC_URBS; i++) {
855 if (test_bit(i + 16, &subs->active_mask))
856 alive++;
857 }
858 }
859 if (! alive)
860 break;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200861 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanb27c1872005-07-09 10:54:37 +0200862 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (alive)
864 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
865 return 0;
866}
867
868
869/*
870 * return the current pcm pointer. just return the hwptr_done value.
871 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100872static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100874 struct snd_usb_substream *subs;
Clemens Ladischdaa150e2005-08-15 08:25:50 +0200875 snd_pcm_uframes_t hwptr_done;
876
Takashi Iwai86e07d32005-11-17 15:08:02 +0100877 subs = (struct snd_usb_substream *)substream->runtime->private_data;
Clemens Ladischdaa150e2005-08-15 08:25:50 +0200878 spin_lock(&subs->lock);
879 hwptr_done = subs->hwptr_done;
880 spin_unlock(&subs->lock);
881 return hwptr_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884
885/*
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100886 * start/stop playback substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100888static int snd_usb_pcm_playback_trigger(struct snd_pcm_substream *substream,
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100889 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100891 struct snd_usb_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 switch (cmd) {
894 case SNDRV_PCM_TRIGGER_START:
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100895 subs->ops.prepare = prepare_playback_urb;
896 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 case SNDRV_PCM_TRIGGER_STOP:
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100898 return deactivate_urbs(subs, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 default:
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100900 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100902}
903
904/*
905 * start/stop capture substream
906 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100907static int snd_usb_pcm_capture_trigger(struct snd_pcm_substream *substream,
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100908 int cmd)
909{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100910 struct snd_usb_substream *subs = substream->runtime->private_data;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100911
912 switch (cmd) {
913 case SNDRV_PCM_TRIGGER_START:
914 return start_urbs(subs, substream->runtime);
915 case SNDRV_PCM_TRIGGER_STOP:
916 return deactivate_urbs(subs, 0, 0);
917 default:
918 return -EINVAL;
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922
923/*
924 * release a urb data
925 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100926static void release_urb_ctx(struct snd_urb_ctx *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 if (u->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200929 if (u->buffer_size)
930 usb_buffer_free(u->subs->dev, u->buffer_size,
931 u->urb->transfer_buffer,
932 u->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 usb_free_urb(u->urb);
934 u->urb = NULL;
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938/*
939 * release a substream
940 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100941static void release_substream_urbs(struct snd_usb_substream *subs, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 int i;
944
945 /* stop urbs (to be sure) */
946 deactivate_urbs(subs, force, 1);
947 wait_clear_urbs(subs);
948
949 for (i = 0; i < MAX_URBS; i++)
950 release_urb_ctx(&subs->dataurb[i]);
951 for (i = 0; i < SYNC_URBS; i++)
952 release_urb_ctx(&subs->syncurb[i]);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200953 usb_buffer_free(subs->dev, SYNC_URBS * 4,
954 subs->syncbuf, subs->sync_dma);
955 subs->syncbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 subs->nurbs = 0;
957}
958
959/*
960 * initialize a substream for plaback/capture
961 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100962static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 unsigned int rate, unsigned int frame_bits)
964{
965 unsigned int maxsize, n, i;
966 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
Clemens Ladischa93bf992005-08-12 15:19:39 +0200967 unsigned int npacks[MAX_URBS], urb_packs, total_packs, packs_per_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /* calculate the frequency in 16.16 format */
970 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
971 subs->freqn = get_usb_full_speed_rate(rate);
972 else
973 subs->freqn = get_usb_high_speed_rate(rate);
974 subs->freqm = subs->freqn;
Clemens Ladisch573567e2005-06-27 08:17:30 +0200975 /* calculate max. frequency */
976 if (subs->maxpacksize) {
977 /* whatever fits into a max. size packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 maxsize = subs->maxpacksize;
Clemens Ladisch573567e2005-06-27 08:17:30 +0200979 subs->freqmax = (maxsize / (frame_bits >> 3))
980 << (16 - subs->datainterval);
981 } else {
982 /* no max. packet size: just take 25% higher than nominal */
983 subs->freqmax = subs->freqn + (subs->freqn >> 2);
984 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
985 >> (16 - subs->datainterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
Clemens Ladisch573567e2005-06-27 08:17:30 +0200987 subs->phase = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 if (subs->fill_max)
990 subs->curpacksize = subs->maxpacksize;
991 else
992 subs->curpacksize = maxsize;
993
Clemens Ladischa93bf992005-08-12 15:19:39 +0200994 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
995 packs_per_ms = 8 >> subs->datainterval;
996 else
997 packs_per_ms = 1;
Clemens Ladisch9624ea82005-08-15 08:25:24 +0200998 subs->packs_per_ms = packs_per_ms;
Clemens Ladischa93bf992005-08-12 15:19:39 +0200999
Clemens Ladisch71d848c2005-08-12 15:18:00 +02001000 if (is_playback) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 urb_packs = nrpacks;
Clemens Ladisch71d848c2005-08-12 15:18:00 +02001002 urb_packs = max(urb_packs, (unsigned int)MIN_PACKS_URB);
1003 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
1004 } else
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001005 urb_packs = 1;
Clemens Ladischa93bf992005-08-12 15:19:39 +02001006 urb_packs *= packs_per_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* decide how many packets to be used */
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001009 if (is_playback) {
Clemens Ladischd6db3922005-08-12 08:28:27 +02001010 unsigned int minsize;
1011 /* determine how small a packet can be */
1012 minsize = (subs->freqn >> (16 - subs->datainterval))
1013 * (frame_bits >> 3);
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +02001014 /* with sync from device, assume it can be 12% lower */
Clemens Ladischd6db3922005-08-12 08:28:27 +02001015 if (subs->syncpipe)
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +02001016 minsize -= minsize >> 3;
Clemens Ladischd6db3922005-08-12 08:28:27 +02001017 minsize = max(minsize, 1u);
1018 total_packs = (period_bytes + minsize - 1) / minsize;
Clemens Ladischa93bf992005-08-12 15:19:39 +02001019 /* round up to multiple of packs_per_ms */
1020 total_packs = (total_packs + packs_per_ms - 1)
1021 & ~(packs_per_ms - 1);
1022 /* we need at least two URBs for queueing */
1023 if (total_packs < 2 * MIN_PACKS_URB * packs_per_ms)
1024 total_packs = 2 * MIN_PACKS_URB * packs_per_ms;
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001025 } else {
1026 total_packs = MAX_URBS * urb_packs;
1027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
1029 if (subs->nurbs > MAX_URBS) {
1030 /* too much... */
1031 subs->nurbs = MAX_URBS;
1032 total_packs = MAX_URBS * urb_packs;
1033 }
1034 n = total_packs;
1035 for (i = 0; i < subs->nurbs; i++) {
1036 npacks[i] = n > urb_packs ? urb_packs : n;
1037 n -= urb_packs;
1038 }
1039 if (subs->nurbs <= 1) {
1040 /* too little - we need at least two packets
1041 * to ensure contiguous playback/capture
1042 */
1043 subs->nurbs = 2;
1044 npacks[0] = (total_packs + 1) / 2;
1045 npacks[1] = total_packs - npacks[0];
Clemens Ladischa93bf992005-08-12 15:19:39 +02001046 } else if (npacks[subs->nurbs-1] < MIN_PACKS_URB * packs_per_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /* the last packet is too small.. */
1048 if (subs->nurbs > 2) {
1049 /* merge to the first one */
1050 npacks[0] += npacks[subs->nurbs - 1];
1051 subs->nurbs--;
1052 } else {
1053 /* divide to two */
1054 subs->nurbs = 2;
1055 npacks[0] = (total_packs + 1) / 2;
1056 npacks[1] = total_packs - npacks[0];
1057 }
1058 }
1059
1060 /* allocate and initialize data urbs */
1061 for (i = 0; i < subs->nurbs; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001062 struct snd_urb_ctx *u = &subs->dataurb[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 u->index = i;
1064 u->subs = subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 u->packets = npacks[i];
Clemens Ladisch55851f72005-08-15 08:34:16 +02001066 u->buffer_size = maxsize * u->packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (subs->fmt_type == USB_FORMAT_TYPE_II)
1068 u->packets++; /* for transfer delimiter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
Clemens Ladisch55851f72005-08-15 08:34:16 +02001070 if (! u->urb)
1071 goto out_of_memory;
1072 u->urb->transfer_buffer =
1073 usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL,
1074 &u->urb->transfer_dma);
1075 if (! u->urb->transfer_buffer)
1076 goto out_of_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 u->urb->pipe = subs->datapipe;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001078 u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Clemens Ladisch573567e2005-06-27 08:17:30 +02001079 u->urb->interval = 1 << subs->datainterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 u->urb->context = u;
Clemens Ladisch3527a002005-09-26 10:03:09 +02001081 u->urb->complete = snd_complete_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
1084 if (subs->syncpipe) {
1085 /* allocate and initialize sync urbs */
Clemens Ladisch55851f72005-08-15 08:34:16 +02001086 subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4,
1087 GFP_KERNEL, &subs->sync_dma);
1088 if (! subs->syncbuf)
1089 goto out_of_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 for (i = 0; i < SYNC_URBS; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001091 struct snd_urb_ctx *u = &subs->syncurb[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 u->index = i;
1093 u->subs = subs;
Clemens Ladischca810902005-05-02 08:55:54 +02001094 u->packets = 1;
1095 u->urb = usb_alloc_urb(1, GFP_KERNEL);
Clemens Ladisch55851f72005-08-15 08:34:16 +02001096 if (! u->urb)
1097 goto out_of_memory;
Clemens Ladischca810902005-05-02 08:55:54 +02001098 u->urb->transfer_buffer = subs->syncbuf + i * 4;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001099 u->urb->transfer_dma = subs->sync_dma + i * 4;
Clemens Ladischca810902005-05-02 08:55:54 +02001100 u->urb->transfer_buffer_length = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 u->urb->pipe = subs->syncpipe;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001102 u->urb->transfer_flags = URB_ISO_ASAP |
1103 URB_NO_TRANSFER_DMA_MAP;
Clemens Ladischca810902005-05-02 08:55:54 +02001104 u->urb->number_of_packets = 1;
Clemens Ladisch1149a642005-05-02 08:53:46 +02001105 u->urb->interval = 1 << subs->syncinterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 u->urb->context = u;
Clemens Ladisch3527a002005-09-26 10:03:09 +02001107 u->urb->complete = snd_complete_sync_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109 }
1110 return 0;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001111
1112out_of_memory:
1113 release_substream_urbs(subs, 0);
1114 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117
1118/*
1119 * find a matching audio format
1120 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001121static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 unsigned int rate, unsigned int channels)
1123{
1124 struct list_head *p;
1125 struct audioformat *found = NULL;
1126 int cur_attr = 0, attr;
1127
1128 list_for_each(p, &subs->fmt_list) {
1129 struct audioformat *fp;
1130 fp = list_entry(p, struct audioformat, list);
1131 if (fp->format != format || fp->channels != channels)
1132 continue;
1133 if (rate < fp->rate_min || rate > fp->rate_max)
1134 continue;
1135 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
1136 unsigned int i;
1137 for (i = 0; i < fp->nr_rates; i++)
1138 if (fp->rate_table[i] == rate)
1139 break;
1140 if (i >= fp->nr_rates)
1141 continue;
1142 }
1143 attr = fp->ep_attr & EP_ATTR_MASK;
1144 if (! found) {
1145 found = fp;
1146 cur_attr = attr;
1147 continue;
1148 }
1149 /* avoid async out and adaptive in if the other method
1150 * supports the same format.
1151 * this is a workaround for the case like
1152 * M-audio audiophile USB.
1153 */
1154 if (attr != cur_attr) {
1155 if ((attr == EP_ATTR_ASYNC &&
1156 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1157 (attr == EP_ATTR_ADAPTIVE &&
1158 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
1159 continue;
1160 if ((cur_attr == EP_ATTR_ASYNC &&
1161 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1162 (cur_attr == EP_ATTR_ADAPTIVE &&
1163 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
1164 found = fp;
1165 cur_attr = attr;
1166 continue;
1167 }
1168 }
1169 /* find the format with the largest max. packet size */
1170 if (fp->maxpacksize > found->maxpacksize) {
1171 found = fp;
1172 cur_attr = attr;
1173 }
1174 }
1175 return found;
1176}
1177
1178
1179/*
1180 * initialize the picth control and sample rate
1181 */
1182static int init_usb_pitch(struct usb_device *dev, int iface,
1183 struct usb_host_interface *alts,
1184 struct audioformat *fmt)
1185{
1186 unsigned int ep;
1187 unsigned char data[1];
1188 int err;
1189
1190 ep = get_endpoint(alts, 0)->bEndpointAddress;
1191 /* if endpoint has pitch control, enable it */
1192 if (fmt->attributes & EP_CS_ATTR_PITCH_CONTROL) {
1193 data[0] = 1;
1194 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1195 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1196 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1197 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
1198 dev->devnum, iface, ep);
1199 return err;
1200 }
1201 }
1202 return 0;
1203}
1204
1205static int init_usb_sample_rate(struct usb_device *dev, int iface,
1206 struct usb_host_interface *alts,
1207 struct audioformat *fmt, int rate)
1208{
1209 unsigned int ep;
1210 unsigned char data[3];
1211 int err;
1212
1213 ep = get_endpoint(alts, 0)->bEndpointAddress;
1214 /* if endpoint has sampling rate control, set it */
1215 if (fmt->attributes & EP_CS_ATTR_SAMPLE_RATE) {
1216 int crate;
1217 data[0] = rate;
1218 data[1] = rate >> 8;
1219 data[2] = rate >> 16;
1220 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1221 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1222 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1223 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep 0x%x\n",
1224 dev->devnum, iface, fmt->altsetting, rate, ep);
1225 return err;
1226 }
1227 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR,
1228 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1229 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1230 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep 0x%x\n",
1231 dev->devnum, iface, fmt->altsetting, ep);
1232 return 0; /* some devices don't support reading */
1233 }
1234 crate = data[0] | (data[1] << 8) | (data[2] << 16);
1235 if (crate != rate) {
1236 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
1237 // runtime->rate = crate;
1238 }
1239 }
1240 return 0;
1241}
1242
1243/*
1244 * find a matching format and set up the interface
1245 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001246static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 struct usb_device *dev = subs->dev;
1249 struct usb_host_interface *alts;
1250 struct usb_interface_descriptor *altsd;
1251 struct usb_interface *iface;
1252 unsigned int ep, attr;
1253 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1254 int err;
1255
1256 iface = usb_ifnum_to_if(dev, fmt->iface);
1257 snd_assert(iface, return -EINVAL);
1258 alts = &iface->altsetting[fmt->altset_idx];
1259 altsd = get_iface_desc(alts);
1260 snd_assert(altsd->bAlternateSetting == fmt->altsetting, return -EINVAL);
1261
1262 if (fmt == subs->cur_audiofmt)
1263 return 0;
1264
1265 /* close the old interface */
1266 if (subs->interface >= 0 && subs->interface != fmt->iface) {
1267 usb_set_interface(subs->dev, subs->interface, 0);
1268 subs->interface = -1;
1269 subs->format = 0;
1270 }
1271
1272 /* set interface */
1273 if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
1274 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
1275 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
1276 dev->devnum, fmt->iface, fmt->altsetting);
1277 return -EIO;
1278 }
1279 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
1280 subs->interface = fmt->iface;
1281 subs->format = fmt->altset_idx;
1282 }
1283
1284 /* create a data pipe */
1285 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
1286 if (is_playback)
1287 subs->datapipe = usb_sndisocpipe(dev, ep);
1288 else
1289 subs->datapipe = usb_rcvisocpipe(dev, ep);
Clemens Ladisch573567e2005-06-27 08:17:30 +02001290 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH &&
1291 get_endpoint(alts, 0)->bInterval >= 1 &&
1292 get_endpoint(alts, 0)->bInterval <= 4)
1293 subs->datainterval = get_endpoint(alts, 0)->bInterval - 1;
1294 else
1295 subs->datainterval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 subs->syncpipe = subs->syncinterval = 0;
1297 subs->maxpacksize = fmt->maxpacksize;
1298 subs->fill_max = 0;
1299
1300 /* we need a sync pipe in async OUT or adaptive IN mode */
1301 /* check the number of EP, since some devices have broken
1302 * descriptors which fool us. if it has only one EP,
1303 * assume it as adaptive-out or sync-in.
1304 */
1305 attr = fmt->ep_attr & EP_ATTR_MASK;
1306 if (((is_playback && attr == EP_ATTR_ASYNC) ||
1307 (! is_playback && attr == EP_ATTR_ADAPTIVE)) &&
1308 altsd->bNumEndpoints >= 2) {
1309 /* check sync-pipe endpoint */
1310 /* ... and check descriptor size before accessing bSynchAddress
1311 because there is a version of the SB Audigy 2 NX firmware lacking
1312 the audio fields in the endpoint descriptors */
1313 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
1314 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1315 get_endpoint(alts, 1)->bSynchAddress != 0)) {
1316 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1317 dev->devnum, fmt->iface, fmt->altsetting);
1318 return -EINVAL;
1319 }
1320 ep = get_endpoint(alts, 1)->bEndpointAddress;
1321 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1322 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
1323 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
1324 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1325 dev->devnum, fmt->iface, fmt->altsetting);
1326 return -EINVAL;
1327 }
1328 ep &= USB_ENDPOINT_NUMBER_MASK;
1329 if (is_playback)
1330 subs->syncpipe = usb_rcvisocpipe(dev, ep);
1331 else
1332 subs->syncpipe = usb_sndisocpipe(dev, ep);
Clemens Ladisch1149a642005-05-02 08:53:46 +02001333 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1334 get_endpoint(alts, 1)->bRefresh >= 1 &&
1335 get_endpoint(alts, 1)->bRefresh <= 9)
1336 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
Clemens Ladisch604cf492005-05-17 09:15:27 +02001337 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
Clemens Ladisch1149a642005-05-02 08:53:46 +02001338 subs->syncinterval = 1;
Clemens Ladisch604cf492005-05-17 09:15:27 +02001339 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
1340 get_endpoint(alts, 1)->bInterval <= 16)
1341 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
1342 else
1343 subs->syncinterval = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345
1346 /* always fill max packet size */
1347 if (fmt->attributes & EP_CS_ATTR_FILL_MAX)
1348 subs->fill_max = 1;
1349
1350 if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
1351 return err;
1352
1353 subs->cur_audiofmt = fmt;
1354
1355#if 0
1356 printk("setting done: format = %d, rate = %d, channels = %d\n",
1357 fmt->format, fmt->rate, fmt->channels);
1358 printk(" datapipe = 0x%0x, syncpipe = 0x%0x\n",
1359 subs->datapipe, subs->syncpipe);
1360#endif
1361
1362 return 0;
1363}
1364
1365/*
1366 * hw_params callback
1367 *
1368 * allocate a buffer and set the given audio format.
1369 *
1370 * so far we use a physically linear buffer although packetize transfer
1371 * doesn't need a continuous area.
1372 * if sg buffer is supported on the later version of alsa, we'll follow
1373 * that.
1374 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001375static int snd_usb_hw_params(struct snd_pcm_substream *substream,
1376 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001378 struct snd_usb_substream *subs = (struct snd_usb_substream *)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 struct audioformat *fmt;
1380 unsigned int channels, rate, format;
1381 int ret, changed;
1382
Clemens Ladisch6207e512005-08-15 08:35:25 +02001383 ret = snd_pcm_alloc_vmalloc_buffer(substream,
1384 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (ret < 0)
1386 return ret;
1387
1388 format = params_format(hw_params);
1389 rate = params_rate(hw_params);
1390 channels = params_channels(hw_params);
1391 fmt = find_format(subs, format, rate, channels);
1392 if (! fmt) {
1393 snd_printd(KERN_DEBUG "cannot set format: format = %s, rate = %d, channels = %d\n",
1394 snd_pcm_format_name(format), rate, channels);
1395 return -EINVAL;
1396 }
1397
1398 changed = subs->cur_audiofmt != fmt ||
1399 subs->period_bytes != params_period_bytes(hw_params) ||
1400 subs->cur_rate != rate;
1401 if ((ret = set_format(subs, fmt)) < 0)
1402 return ret;
1403
1404 if (subs->cur_rate != rate) {
1405 struct usb_host_interface *alts;
1406 struct usb_interface *iface;
1407 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
1408 alts = &iface->altsetting[fmt->altset_idx];
1409 ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
1410 if (ret < 0)
1411 return ret;
1412 subs->cur_rate = rate;
1413 }
1414
1415 if (changed) {
1416 /* format changed */
1417 release_substream_urbs(subs, 0);
1418 /* influenced: period_bytes, channels, rate, format, */
1419 ret = init_substream_urbs(subs, params_period_bytes(hw_params),
1420 params_rate(hw_params),
1421 snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
1422 }
1423
1424 return ret;
1425}
1426
1427/*
1428 * hw_free callback
1429 *
1430 * reset the audio format and release the buffer
1431 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001432static int snd_usb_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001434 struct snd_usb_substream *subs = (struct snd_usb_substream *)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 subs->cur_audiofmt = NULL;
1437 subs->cur_rate = 0;
1438 subs->period_bytes = 0;
1439 release_substream_urbs(subs, 0);
Clemens Ladisch6207e512005-08-15 08:35:25 +02001440 return snd_pcm_free_vmalloc_buffer(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
1443/*
1444 * prepare callback
1445 *
1446 * only a few subtle things...
1447 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001448static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001450 struct snd_pcm_runtime *runtime = substream->runtime;
1451 struct snd_usb_substream *subs = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 if (! subs->cur_audiofmt) {
1454 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
1455 return -ENXIO;
1456 }
1457
1458 /* some unit conversions in runtime */
1459 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
1460 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
1461
1462 /* reset the pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 subs->hwptr_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 subs->transfer_done = 0;
1465 subs->phase = 0;
1466
1467 /* clear urbs (to be sure) */
1468 deactivate_urbs(subs, 0, 1);
1469 wait_clear_urbs(subs);
1470
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001471 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1472 * updates for all URBs would happen at the same time when starting */
1473 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1474 subs->ops.prepare = prepare_startup_playback_urb;
1475 return start_urbs(subs, runtime);
1476 } else
1477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
Takashi Iwai86e07d32005-11-17 15:08:02 +01001480static struct snd_pcm_hardware snd_usb_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Clemens Ladisch49045d32005-09-05 10:31:05 +02001482 .info = SNDRV_PCM_INFO_MMAP |
1483 SNDRV_PCM_INFO_MMAP_VALID |
1484 SNDRV_PCM_INFO_BATCH |
1485 SNDRV_PCM_INFO_INTERLEAVED |
1486 SNDRV_PCM_INFO_BLOCK_TRANSFER,
Clemens Ladischd31cbbf2005-09-26 09:59:57 +02001487 .buffer_bytes_max = 1024 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 .period_bytes_min = 64,
Clemens Ladischd31cbbf2005-09-26 09:59:57 +02001489 .period_bytes_max = 512 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 .periods_min = 2,
1491 .periods_max = 1024,
1492};
1493
Takashi Iwai86e07d32005-11-17 15:08:02 +01001494static struct snd_pcm_hardware snd_usb_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Clemens Ladisch49045d32005-09-05 10:31:05 +02001496 .info = SNDRV_PCM_INFO_MMAP |
1497 SNDRV_PCM_INFO_MMAP_VALID |
1498 SNDRV_PCM_INFO_BATCH |
1499 SNDRV_PCM_INFO_INTERLEAVED |
1500 SNDRV_PCM_INFO_BLOCK_TRANSFER,
Clemens Ladischd31cbbf2005-09-26 09:59:57 +02001501 .buffer_bytes_max = 1024 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 .period_bytes_min = 64,
Clemens Ladischd31cbbf2005-09-26 09:59:57 +02001503 .period_bytes_max = 512 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 .periods_min = 2,
1505 .periods_max = 1024,
1506};
1507
1508/*
1509 * h/w constraints
1510 */
1511
1512#ifdef HW_CONST_DEBUG
1513#define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
1514#else
1515#define hwc_debug(fmt, args...) /**/
1516#endif
1517
Takashi Iwai86e07d32005-11-17 15:08:02 +01001518static int hw_check_valid_format(struct snd_pcm_hw_params *params, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001520 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1521 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1522 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 /* check the format */
1525 if (! snd_mask_test(fmts, fp->format)) {
1526 hwc_debug(" > check: no supported format %d\n", fp->format);
1527 return 0;
1528 }
1529 /* check the channels */
1530 if (fp->channels < ct->min || fp->channels > ct->max) {
1531 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1532 return 0;
1533 }
1534 /* check the rate is within the range */
1535 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1536 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1537 return 0;
1538 }
1539 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1540 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1541 return 0;
1542 }
1543 return 1;
1544}
1545
Takashi Iwai86e07d32005-11-17 15:08:02 +01001546static int hw_rule_rate(struct snd_pcm_hw_params *params,
1547 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001549 struct snd_usb_substream *subs = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001551 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 unsigned int rmin, rmax;
1553 int changed;
1554
1555 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1556 changed = 0;
1557 rmin = rmax = 0;
1558 list_for_each(p, &subs->fmt_list) {
1559 struct audioformat *fp;
1560 fp = list_entry(p, struct audioformat, list);
1561 if (! hw_check_valid_format(params, fp))
1562 continue;
1563 if (changed++) {
1564 if (rmin > fp->rate_min)
1565 rmin = fp->rate_min;
1566 if (rmax < fp->rate_max)
1567 rmax = fp->rate_max;
1568 } else {
1569 rmin = fp->rate_min;
1570 rmax = fp->rate_max;
1571 }
1572 }
1573
1574 if (! changed) {
1575 hwc_debug(" --> get empty\n");
1576 it->empty = 1;
1577 return -EINVAL;
1578 }
1579
1580 changed = 0;
1581 if (it->min < rmin) {
1582 it->min = rmin;
1583 it->openmin = 0;
1584 changed = 1;
1585 }
1586 if (it->max > rmax) {
1587 it->max = rmax;
1588 it->openmax = 0;
1589 changed = 1;
1590 }
1591 if (snd_interval_checkempty(it)) {
1592 it->empty = 1;
1593 return -EINVAL;
1594 }
1595 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1596 return changed;
1597}
1598
1599
Takashi Iwai86e07d32005-11-17 15:08:02 +01001600static int hw_rule_channels(struct snd_pcm_hw_params *params,
1601 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001603 struct snd_usb_substream *subs = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001605 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 unsigned int rmin, rmax;
1607 int changed;
1608
1609 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1610 changed = 0;
1611 rmin = rmax = 0;
1612 list_for_each(p, &subs->fmt_list) {
1613 struct audioformat *fp;
1614 fp = list_entry(p, struct audioformat, list);
1615 if (! hw_check_valid_format(params, fp))
1616 continue;
1617 if (changed++) {
1618 if (rmin > fp->channels)
1619 rmin = fp->channels;
1620 if (rmax < fp->channels)
1621 rmax = fp->channels;
1622 } else {
1623 rmin = fp->channels;
1624 rmax = fp->channels;
1625 }
1626 }
1627
1628 if (! changed) {
1629 hwc_debug(" --> get empty\n");
1630 it->empty = 1;
1631 return -EINVAL;
1632 }
1633
1634 changed = 0;
1635 if (it->min < rmin) {
1636 it->min = rmin;
1637 it->openmin = 0;
1638 changed = 1;
1639 }
1640 if (it->max > rmax) {
1641 it->max = rmax;
1642 it->openmax = 0;
1643 changed = 1;
1644 }
1645 if (snd_interval_checkempty(it)) {
1646 it->empty = 1;
1647 return -EINVAL;
1648 }
1649 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1650 return changed;
1651}
1652
Takashi Iwai86e07d32005-11-17 15:08:02 +01001653static int hw_rule_format(struct snd_pcm_hw_params *params,
1654 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001656 struct snd_usb_substream *subs = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001658 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 u64 fbits;
1660 u32 oldbits[2];
1661 int changed;
1662
1663 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1664 fbits = 0;
1665 list_for_each(p, &subs->fmt_list) {
1666 struct audioformat *fp;
1667 fp = list_entry(p, struct audioformat, list);
1668 if (! hw_check_valid_format(params, fp))
1669 continue;
1670 fbits |= (1ULL << fp->format);
1671 }
1672
1673 oldbits[0] = fmt->bits[0];
1674 oldbits[1] = fmt->bits[1];
1675 fmt->bits[0] &= (u32)fbits;
1676 fmt->bits[1] &= (u32)(fbits >> 32);
1677 if (! fmt->bits[0] && ! fmt->bits[1]) {
1678 hwc_debug(" --> get empty\n");
1679 return -EINVAL;
1680 }
1681 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1682 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1683 return changed;
1684}
1685
1686#define MAX_MASK 64
1687
1688/*
1689 * check whether the registered audio formats need special hw-constraints
1690 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001691static int check_hw_params_convention(struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 int i;
1694 u32 *channels;
1695 u32 *rates;
1696 u32 cmaster, rmaster;
1697 u32 rate_min = 0, rate_max = 0;
1698 struct list_head *p;
1699 int err = 1;
1700
1701 channels = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1702 rates = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1703
1704 list_for_each(p, &subs->fmt_list) {
1705 struct audioformat *f;
1706 f = list_entry(p, struct audioformat, list);
1707 /* unconventional channels? */
1708 if (f->channels > 32)
1709 goto __out;
1710 /* continuous rate min/max matches? */
1711 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1712 if (rate_min && f->rate_min != rate_min)
1713 goto __out;
1714 if (rate_max && f->rate_max != rate_max)
1715 goto __out;
1716 rate_min = f->rate_min;
1717 rate_max = f->rate_max;
1718 }
1719 /* combination of continuous rates and fixed rates? */
1720 if (rates[f->format] & SNDRV_PCM_RATE_CONTINUOUS) {
1721 if (f->rates != rates[f->format])
1722 goto __out;
1723 }
1724 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1725 if (rates[f->format] && rates[f->format] != f->rates)
1726 goto __out;
1727 }
1728 channels[f->format] |= (1 << f->channels);
1729 rates[f->format] |= f->rates;
1730 }
1731 /* check whether channels and rates match for all formats */
1732 cmaster = rmaster = 0;
1733 for (i = 0; i < MAX_MASK; i++) {
1734 if (cmaster != channels[i] && cmaster && channels[i])
1735 goto __out;
1736 if (rmaster != rates[i] && rmaster && rates[i])
1737 goto __out;
1738 if (channels[i])
1739 cmaster = channels[i];
1740 if (rates[i])
1741 rmaster = rates[i];
1742 }
1743 /* check whether channels match for all distinct rates */
1744 memset(channels, 0, MAX_MASK * sizeof(u32));
1745 list_for_each(p, &subs->fmt_list) {
1746 struct audioformat *f;
1747 f = list_entry(p, struct audioformat, list);
1748 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS)
1749 continue;
1750 for (i = 0; i < 32; i++) {
1751 if (f->rates & (1 << i))
1752 channels[i] |= (1 << f->channels);
1753 }
1754 }
1755 cmaster = 0;
1756 for (i = 0; i < 32; i++) {
1757 if (cmaster != channels[i] && cmaster && channels[i])
1758 goto __out;
1759 if (channels[i])
1760 cmaster = channels[i];
1761 }
1762 err = 0;
1763
1764 __out:
1765 kfree(channels);
1766 kfree(rates);
1767 return err;
1768}
1769
1770
1771/*
1772 * set up the runtime hardware information.
1773 */
1774
Takashi Iwai86e07d32005-11-17 15:08:02 +01001775static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct list_head *p;
1778 int err;
1779
1780 runtime->hw.formats = subs->formats;
1781
1782 runtime->hw.rate_min = 0x7fffffff;
1783 runtime->hw.rate_max = 0;
1784 runtime->hw.channels_min = 256;
1785 runtime->hw.channels_max = 0;
1786 runtime->hw.rates = 0;
1787 /* check min/max rates and channels */
1788 list_for_each(p, &subs->fmt_list) {
1789 struct audioformat *fp;
1790 fp = list_entry(p, struct audioformat, list);
1791 runtime->hw.rates |= fp->rates;
1792 if (runtime->hw.rate_min > fp->rate_min)
1793 runtime->hw.rate_min = fp->rate_min;
1794 if (runtime->hw.rate_max < fp->rate_max)
1795 runtime->hw.rate_max = fp->rate_max;
1796 if (runtime->hw.channels_min > fp->channels)
1797 runtime->hw.channels_min = fp->channels;
1798 if (runtime->hw.channels_max < fp->channels)
1799 runtime->hw.channels_max = fp->channels;
1800 if (fp->fmt_type == USB_FORMAT_TYPE_II && fp->frame_size > 0) {
1801 /* FIXME: there might be more than one audio formats... */
1802 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1803 fp->frame_size;
1804 }
1805 }
1806
1807 /* set the period time minimum 1ms */
1808 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1809 1000 * MIN_PACKS_URB,
1810 /*(nrpacks * MAX_URBS) * 1000*/ UINT_MAX);
1811
1812 if (check_hw_params_convention(subs)) {
1813 hwc_debug("setting extra hw constraints...\n");
1814 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1815 hw_rule_rate, subs,
1816 SNDRV_PCM_HW_PARAM_FORMAT,
1817 SNDRV_PCM_HW_PARAM_CHANNELS,
1818 -1)) < 0)
1819 return err;
1820 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1821 hw_rule_channels, subs,
1822 SNDRV_PCM_HW_PARAM_FORMAT,
1823 SNDRV_PCM_HW_PARAM_RATE,
1824 -1)) < 0)
1825 return err;
1826 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1827 hw_rule_format, subs,
1828 SNDRV_PCM_HW_PARAM_RATE,
1829 SNDRV_PCM_HW_PARAM_CHANNELS,
1830 -1)) < 0)
1831 return err;
1832 }
1833 return 0;
1834}
1835
Takashi Iwai86e07d32005-11-17 15:08:02 +01001836static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction,
1837 struct snd_pcm_hardware *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001839 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1840 struct snd_pcm_runtime *runtime = substream->runtime;
1841 struct snd_usb_substream *subs = &as->substream[direction];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 subs->interface = -1;
1844 subs->format = 0;
1845 runtime->hw = *hw;
1846 runtime->private_data = subs;
1847 subs->pcm_substream = substream;
1848 return setup_hw_info(runtime, subs);
1849}
1850
Takashi Iwai86e07d32005-11-17 15:08:02 +01001851static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001853 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1854 struct snd_usb_substream *subs = &as->substream[direction];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 if (subs->interface >= 0) {
1857 usb_set_interface(subs->dev, subs->interface, 0);
1858 subs->interface = -1;
1859 }
1860 subs->pcm_substream = NULL;
1861 return 0;
1862}
1863
Takashi Iwai86e07d32005-11-17 15:08:02 +01001864static int snd_usb_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK, &snd_usb_playback);
1867}
1868
Takashi Iwai86e07d32005-11-17 15:08:02 +01001869static int snd_usb_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
1871 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1872}
1873
Takashi Iwai86e07d32005-11-17 15:08:02 +01001874static int snd_usb_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE, &snd_usb_capture);
1877}
1878
Takashi Iwai86e07d32005-11-17 15:08:02 +01001879static int snd_usb_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1882}
1883
Takashi Iwai86e07d32005-11-17 15:08:02 +01001884static struct snd_pcm_ops snd_usb_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 .open = snd_usb_playback_open,
1886 .close = snd_usb_playback_close,
1887 .ioctl = snd_pcm_lib_ioctl,
1888 .hw_params = snd_usb_hw_params,
1889 .hw_free = snd_usb_hw_free,
1890 .prepare = snd_usb_pcm_prepare,
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001891 .trigger = snd_usb_pcm_playback_trigger,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 .pointer = snd_usb_pcm_pointer,
Clemens Ladisch6207e512005-08-15 08:35:25 +02001893 .page = snd_pcm_get_vmalloc_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894};
1895
Takashi Iwai86e07d32005-11-17 15:08:02 +01001896static struct snd_pcm_ops snd_usb_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 .open = snd_usb_capture_open,
1898 .close = snd_usb_capture_close,
1899 .ioctl = snd_pcm_lib_ioctl,
1900 .hw_params = snd_usb_hw_params,
1901 .hw_free = snd_usb_hw_free,
1902 .prepare = snd_usb_pcm_prepare,
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001903 .trigger = snd_usb_pcm_capture_trigger,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 .pointer = snd_usb_pcm_pointer,
Clemens Ladisch6207e512005-08-15 08:35:25 +02001905 .page = snd_pcm_get_vmalloc_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906};
1907
1908
1909
1910/*
1911 * helper functions
1912 */
1913
1914/*
1915 * combine bytes and get an integer value
1916 */
1917unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
1918{
1919 switch (size) {
1920 case 1: return *bytes;
1921 case 2: return combine_word(bytes);
1922 case 3: return combine_triple(bytes);
1923 case 4: return combine_quad(bytes);
1924 default: return 0;
1925 }
1926}
1927
1928/*
1929 * parse descriptor buffer and return the pointer starting the given
1930 * descriptor type.
1931 */
1932void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
1933{
1934 u8 *p, *end, *next;
1935
1936 p = descstart;
1937 end = p + desclen;
1938 for (; p < end;) {
1939 if (p[0] < 2)
1940 return NULL;
1941 next = p + p[0];
1942 if (next > end)
1943 return NULL;
1944 if (p[1] == dtype && (!after || (void *)p > after)) {
1945 return p;
1946 }
1947 p = next;
1948 }
1949 return NULL;
1950}
1951
1952/*
1953 * find a class-specified interface descriptor with the given subtype.
1954 */
1955void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
1956{
1957 unsigned char *p = after;
1958
1959 while ((p = snd_usb_find_desc(buffer, buflen, p,
1960 USB_DT_CS_INTERFACE)) != NULL) {
1961 if (p[0] >= 3 && p[2] == dsubtype)
1962 return p;
1963 }
1964 return NULL;
1965}
1966
1967/*
1968 * Wrapper for usb_control_msg().
1969 * Allocates a temp buffer to prevent dmaing from/to the stack.
1970 */
1971int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
1972 __u8 requesttype, __u16 value, __u16 index, void *data,
1973 __u16 size, int timeout)
1974{
1975 int err;
1976 void *buf = NULL;
1977
1978 if (size > 0) {
1979 buf = kmalloc(size, GFP_KERNEL);
1980 if (!buf)
1981 return -ENOMEM;
1982 memcpy(buf, data, size);
1983 }
1984 err = usb_control_msg(dev, pipe, request, requesttype,
1985 value, index, buf, size, timeout);
1986 if (size > 0) {
1987 memcpy(data, buf, size);
1988 kfree(buf);
1989 }
1990 return err;
1991}
1992
1993
1994/*
1995 * entry point for linux usb interface
1996 */
1997
1998static int usb_audio_probe(struct usb_interface *intf,
1999 const struct usb_device_id *id);
2000static void usb_audio_disconnect(struct usb_interface *intf);
2001
2002static struct usb_device_id usb_audio_ids [] = {
2003#include "usbquirks.h"
2004 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
2005 .bInterfaceClass = USB_CLASS_AUDIO,
2006 .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL },
2007 { } /* Terminating entry */
2008};
2009
2010MODULE_DEVICE_TABLE (usb, usb_audio_ids);
2011
2012static struct usb_driver usb_audio_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 .name = "snd-usb-audio",
2014 .probe = usb_audio_probe,
2015 .disconnect = usb_audio_disconnect,
2016 .id_table = usb_audio_ids,
2017};
2018
2019
2020/*
2021 * proc interface for list the supported pcm formats
2022 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002023static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 struct list_head *p;
2026 static char *sync_types[4] = {
2027 "NONE", "ASYNC", "ADAPTIVE", "SYNC"
2028 };
2029
2030 list_for_each(p, &subs->fmt_list) {
2031 struct audioformat *fp;
2032 fp = list_entry(p, struct audioformat, list);
2033 snd_iprintf(buffer, " Interface %d\n", fp->iface);
2034 snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
2035 snd_iprintf(buffer, " Format: %s\n", snd_pcm_format_name(fp->format));
2036 snd_iprintf(buffer, " Channels: %d\n", fp->channels);
2037 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
2038 fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
2039 fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
2040 sync_types[(fp->ep_attr & EP_ATTR_MASK) >> 2]);
2041 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
2042 snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
2043 fp->rate_min, fp->rate_max);
2044 } else {
2045 unsigned int i;
2046 snd_iprintf(buffer, " Rates: ");
2047 for (i = 0; i < fp->nr_rates; i++) {
2048 if (i > 0)
2049 snd_iprintf(buffer, ", ");
2050 snd_iprintf(buffer, "%d", fp->rate_table[i]);
2051 }
2052 snd_iprintf(buffer, "\n");
2053 }
2054 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
2055 // snd_iprintf(buffer, " EP Attribute = 0x%x\n", fp->attributes);
2056 }
2057}
2058
Takashi Iwai86e07d32005-11-17 15:08:02 +01002059static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 if (subs->running) {
2062 unsigned int i;
2063 snd_iprintf(buffer, " Status: Running\n");
2064 snd_iprintf(buffer, " Interface = %d\n", subs->interface);
2065 snd_iprintf(buffer, " Altset = %d\n", subs->format);
2066 snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
2067 for (i = 0; i < subs->nurbs; i++)
2068 snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
2069 snd_iprintf(buffer, "]\n");
2070 snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
Clemens Ladisch08fe1582005-04-22 15:33:01 +02002071 snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
2073 ? get_full_speed_hz(subs->freqm)
Clemens Ladisch08fe1582005-04-22 15:33:01 +02002074 : get_high_speed_hz(subs->freqm),
2075 subs->freqm >> 16, subs->freqm & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 } else {
2077 snd_iprintf(buffer, " Status: Stop\n");
2078 }
2079}
2080
Takashi Iwai86e07d32005-11-17 15:08:02 +01002081static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002083 struct snd_usb_stream *stream = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
2086
2087 if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
2088 snd_iprintf(buffer, "\nPlayback:\n");
2089 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2090 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2091 }
2092 if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
2093 snd_iprintf(buffer, "\nCapture:\n");
2094 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2095 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2096 }
2097}
2098
Takashi Iwai86e07d32005-11-17 15:08:02 +01002099static void proc_pcm_format_add(struct snd_usb_stream *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002101 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 char name[32];
Takashi Iwai86e07d32005-11-17 15:08:02 +01002103 struct snd_card *card = stream->chip->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 sprintf(name, "stream%d", stream->pcm_index);
2106 if (! snd_card_proc_new(card, name, &entry))
2107 snd_info_set_text_ops(entry, stream, 1024, proc_pcm_format_read);
2108}
2109
2110
2111/*
2112 * initialize the substream instance.
2113 */
2114
Takashi Iwai86e07d32005-11-17 15:08:02 +01002115static void init_substream(struct snd_usb_stream *as, int stream, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002117 struct snd_usb_substream *subs = &as->substream[stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 INIT_LIST_HEAD(&subs->fmt_list);
2120 spin_lock_init(&subs->lock);
2121
2122 subs->stream = as;
2123 subs->direction = stream;
2124 subs->dev = as->chip->dev;
2125 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
2126 subs->ops = audio_urb_ops[stream];
2127 else
2128 subs->ops = audio_urb_ops_high_speed[stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 snd_pcm_set_ops(as->pcm, stream,
2130 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2131 &snd_usb_playback_ops : &snd_usb_capture_ops);
2132
2133 list_add_tail(&fp->list, &subs->fmt_list);
2134 subs->formats |= 1ULL << fp->format;
2135 subs->endpoint = fp->endpoint;
2136 subs->num_formats++;
2137 subs->fmt_type = fp->fmt_type;
2138}
2139
2140
2141/*
2142 * free a substream
2143 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002144static void free_substream(struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 struct list_head *p, *n;
2147
2148 if (! subs->num_formats)
2149 return; /* not initialized */
2150 list_for_each_safe(p, n, &subs->fmt_list) {
2151 struct audioformat *fp = list_entry(p, struct audioformat, list);
2152 kfree(fp->rate_table);
2153 kfree(fp);
2154 }
2155}
2156
2157
2158/*
2159 * free a usb stream instance
2160 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002161static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 free_substream(&stream->substream[0]);
2164 free_substream(&stream->substream[1]);
2165 list_del(&stream->list);
2166 kfree(stream);
2167}
2168
Takashi Iwai86e07d32005-11-17 15:08:02 +01002169static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002171 struct snd_usb_stream *stream = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (stream) {
2173 stream->pcm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 snd_usb_audio_stream_free(stream);
2175 }
2176}
2177
2178
2179/*
2180 * add this endpoint to the chip instance.
2181 * if a stream with the same endpoint already exists, append to it.
2182 * if not, create a new pcm stream.
2183 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002184static int add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002187 struct snd_usb_stream *as;
2188 struct snd_usb_substream *subs;
2189 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 int err;
2191
2192 list_for_each(p, &chip->pcm_list) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002193 as = list_entry(p, struct snd_usb_stream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (as->fmt_type != fp->fmt_type)
2195 continue;
2196 subs = &as->substream[stream];
2197 if (! subs->endpoint)
2198 continue;
2199 if (subs->endpoint == fp->endpoint) {
2200 list_add_tail(&fp->list, &subs->fmt_list);
2201 subs->num_formats++;
2202 subs->formats |= 1ULL << fp->format;
2203 return 0;
2204 }
2205 }
2206 /* look for an empty stream */
2207 list_for_each(p, &chip->pcm_list) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002208 as = list_entry(p, struct snd_usb_stream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (as->fmt_type != fp->fmt_type)
2210 continue;
2211 subs = &as->substream[stream];
2212 if (subs->endpoint)
2213 continue;
2214 err = snd_pcm_new_stream(as->pcm, stream, 1);
2215 if (err < 0)
2216 return err;
2217 init_substream(as, stream, fp);
2218 return 0;
2219 }
2220
2221 /* create a new pcm */
2222 as = kmalloc(sizeof(*as), GFP_KERNEL);
2223 if (! as)
2224 return -ENOMEM;
2225 memset(as, 0, sizeof(*as));
2226 as->pcm_index = chip->pcm_devs;
2227 as->chip = chip;
2228 as->fmt_type = fp->fmt_type;
2229 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
2230 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
2231 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
2232 &pcm);
2233 if (err < 0) {
2234 kfree(as);
2235 return err;
2236 }
2237 as->pcm = pcm;
2238 pcm->private_data = as;
2239 pcm->private_free = snd_usb_audio_pcm_free;
2240 pcm->info_flags = 0;
2241 if (chip->pcm_devs > 0)
2242 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
2243 else
2244 strcpy(pcm->name, "USB Audio");
2245
2246 init_substream(as, stream, fp);
2247
2248 list_add(&as->list, &chip->pcm_list);
2249 chip->pcm_devs++;
2250
2251 proc_pcm_format_add(as);
2252
2253 return 0;
2254}
2255
2256
2257/*
2258 * check if the device uses big-endian samples
2259 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002260static int is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002262 switch (chip->usb_id) {
2263 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
2264 if (fp->endpoint & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return 1;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002266 break;
2267 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2268 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
2270 return 0;
2271}
2272
2273/*
2274 * parse the audio format type I descriptor
2275 * and returns the corresponding pcm format
2276 *
2277 * @dev: usb device
2278 * @fp: audioformat record
2279 * @format: the format tag (wFormatTag)
2280 * @fmt: the format type descriptor
2281 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002282static int parse_audio_format_i_type(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 int format, unsigned char *fmt)
2284{
2285 int pcm_format;
2286 int sample_width, sample_bytes;
2287
2288 /* FIXME: correct endianess and sign? */
2289 pcm_format = -1;
2290 sample_width = fmt[6];
2291 sample_bytes = fmt[5];
2292 switch (format) {
2293 case 0: /* some devices don't define this correctly... */
2294 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002295 chip->dev->devnum, fp->iface, fp->altsetting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /* fall-through */
2297 case USB_AUDIO_FORMAT_PCM:
2298 if (sample_width > sample_bytes * 8) {
2299 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002300 chip->dev->devnum, fp->iface, fp->altsetting,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 sample_width, sample_bytes);
2302 }
2303 /* check the format byte size */
2304 switch (fmt[5]) {
2305 case 1:
2306 pcm_format = SNDRV_PCM_FORMAT_S8;
2307 break;
2308 case 2:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002309 if (is_big_endian_format(chip, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
2311 else
2312 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2313 break;
2314 case 3:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002315 if (is_big_endian_format(chip, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
2317 else
2318 pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
2319 break;
2320 case 4:
2321 pcm_format = SNDRV_PCM_FORMAT_S32_LE;
2322 break;
2323 default:
2324 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002325 chip->dev->devnum, fp->iface,
2326 fp->altsetting, sample_width, sample_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 break;
2328 }
2329 break;
2330 case USB_AUDIO_FORMAT_PCM8:
2331 /* Dallas DS4201 workaround */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002332 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 pcm_format = SNDRV_PCM_FORMAT_S8;
2334 else
2335 pcm_format = SNDRV_PCM_FORMAT_U8;
2336 break;
2337 case USB_AUDIO_FORMAT_IEEE_FLOAT:
2338 pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
2339 break;
2340 case USB_AUDIO_FORMAT_ALAW:
2341 pcm_format = SNDRV_PCM_FORMAT_A_LAW;
2342 break;
2343 case USB_AUDIO_FORMAT_MU_LAW:
2344 pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
2345 break;
2346 default:
2347 snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002348 chip->dev->devnum, fp->iface, fp->altsetting, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 break;
2350 }
2351 return pcm_format;
2352}
2353
2354
2355/*
2356 * parse the format descriptor and stores the possible sample rates
2357 * on the audioformat table.
2358 *
2359 * @dev: usb device
2360 * @fp: audioformat record
2361 * @fmt: the format descriptor
2362 * @offset: the start offset of descriptor pointing the rate type
2363 * (7 for type I and II, 8 for type II)
2364 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002365static int parse_audio_format_rates(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 unsigned char *fmt, int offset)
2367{
2368 int nr_rates = fmt[offset];
2369 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
2370 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002371 chip->dev->devnum, fp->iface, fp->altsetting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 return -1;
2373 }
2374
2375 if (nr_rates) {
2376 /*
2377 * build the rate table and bitmap flags
2378 */
2379 int r, idx, c;
2380 /* this table corresponds to the SNDRV_PCM_RATE_XXX bit */
2381 static unsigned int conv_rates[] = {
2382 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
2383 64000, 88200, 96000, 176400, 192000
2384 };
2385 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2386 if (fp->rate_table == NULL) {
2387 snd_printk(KERN_ERR "cannot malloc\n");
2388 return -1;
2389 }
2390
2391 fp->nr_rates = nr_rates;
2392 fp->rate_min = fp->rate_max = combine_triple(&fmt[8]);
2393 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
2394 unsigned int rate = fp->rate_table[r] = combine_triple(&fmt[idx]);
2395 if (rate < fp->rate_min)
2396 fp->rate_min = rate;
2397 else if (rate > fp->rate_max)
2398 fp->rate_max = rate;
2399 for (c = 0; c < (int)ARRAY_SIZE(conv_rates); c++) {
2400 if (rate == conv_rates[c]) {
2401 fp->rates |= (1 << c);
2402 break;
2403 }
2404 }
2405 }
2406 } else {
2407 /* continuous rates */
2408 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
2409 fp->rate_min = combine_triple(&fmt[offset + 1]);
2410 fp->rate_max = combine_triple(&fmt[offset + 4]);
2411 }
2412 return 0;
2413}
2414
2415/*
2416 * parse the format type I and III descriptors
2417 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002418static int parse_audio_format_i(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 int format, unsigned char *fmt)
2420{
2421 int pcm_format;
2422
2423 if (fmt[3] == USB_FORMAT_TYPE_III) {
2424 /* FIXME: the format type is really IECxxx
2425 * but we give normal PCM format to get the existing
2426 * apps working...
2427 */
2428 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2429 } else {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002430 pcm_format = parse_audio_format_i_type(chip, fp, format, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (pcm_format < 0)
2432 return -1;
2433 }
2434 fp->format = pcm_format;
2435 fp->channels = fmt[4];
2436 if (fp->channels < 1) {
2437 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002438 chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return -1;
2440 }
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002441 return parse_audio_format_rates(chip, fp, fmt, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
2444/*
2445 * prase the format type II descriptor
2446 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002447static int parse_audio_format_ii(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 int format, unsigned char *fmt)
2449{
2450 int brate, framesize;
2451 switch (format) {
2452 case USB_AUDIO_FORMAT_AC3:
2453 /* FIXME: there is no AC3 format defined yet */
2454 // fp->format = SNDRV_PCM_FORMAT_AC3;
2455 fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
2456 break;
2457 case USB_AUDIO_FORMAT_MPEG:
2458 fp->format = SNDRV_PCM_FORMAT_MPEG;
2459 break;
2460 default:
2461 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag 0x%x is detected. processed as MPEG.\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002462 chip->dev->devnum, fp->iface, fp->altsetting, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 fp->format = SNDRV_PCM_FORMAT_MPEG;
2464 break;
2465 }
2466 fp->channels = 1;
2467 brate = combine_word(&fmt[4]); /* fmt[4,5] : wMaxBitRate (in kbps) */
2468 framesize = combine_word(&fmt[6]); /* fmt[6,7]: wSamplesPerFrame */
2469 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
2470 fp->frame_size = framesize;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002471 return parse_audio_format_rates(chip, fp, fmt, 8); /* fmt[8..] sample rates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
Takashi Iwai86e07d32005-11-17 15:08:02 +01002474static int parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 int format, unsigned char *fmt, int stream)
2476{
2477 int err;
2478
2479 switch (fmt[3]) {
2480 case USB_FORMAT_TYPE_I:
2481 case USB_FORMAT_TYPE_III:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002482 err = parse_audio_format_i(chip, fp, format, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 break;
2484 case USB_FORMAT_TYPE_II:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002485 err = parse_audio_format_ii(chip, fp, format, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 break;
2487 default:
2488 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002489 chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return -1;
2491 }
2492 fp->fmt_type = fmt[3];
2493 if (err < 0)
2494 return err;
2495#if 1
Clemens Ladisch8c1872d2005-04-28 09:31:53 +02002496 /* FIXME: temporary hack for extigy/audigy 2 nx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 /* extigy apparently supports sample rates other than 48k
2498 * but not in ordinary way. so we enable only 48k atm.
2499 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002500 if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
2501 chip->usb_id == USB_ID(0x041e, 0x3020)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (fmt[3] == USB_FORMAT_TYPE_I &&
Clemens Ladisch8c1872d2005-04-28 09:31:53 +02002503 fp->rates != SNDRV_PCM_RATE_48000 &&
2504 fp->rates != SNDRV_PCM_RATE_96000)
Clemens Ladischb4d3f9d2005-06-27 08:18:27 +02002505 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507#endif
2508 return 0;
2509}
2510
Takashi Iwai86e07d32005-11-17 15:08:02 +01002511static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 struct usb_device *dev;
2514 struct usb_interface *iface;
2515 struct usb_host_interface *alts;
2516 struct usb_interface_descriptor *altsd;
2517 int i, altno, err, stream;
2518 int format;
2519 struct audioformat *fp;
2520 unsigned char *fmt, *csep;
2521
2522 dev = chip->dev;
2523
2524 /* parse the interface's altsettings */
2525 iface = usb_ifnum_to_if(dev, iface_no);
2526 for (i = 0; i < iface->num_altsetting; i++) {
2527 alts = &iface->altsetting[i];
2528 altsd = get_iface_desc(alts);
2529 /* skip invalid one */
2530 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2531 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2532 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING &&
2533 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
2534 altsd->bNumEndpoints < 1 ||
2535 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
2536 continue;
2537 /* must be isochronous */
2538 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2539 USB_ENDPOINT_XFER_ISOC)
2540 continue;
2541 /* check direction */
2542 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
2543 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2544 altno = altsd->bAlternateSetting;
2545
2546 /* get audio formats */
2547 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, AS_GENERAL);
2548 if (!fmt) {
2549 snd_printk(KERN_ERR "%d:%u:%d : AS_GENERAL descriptor not found\n",
2550 dev->devnum, iface_no, altno);
2551 continue;
2552 }
2553
2554 if (fmt[0] < 7) {
2555 snd_printk(KERN_ERR "%d:%u:%d : invalid AS_GENERAL desc\n",
2556 dev->devnum, iface_no, altno);
2557 continue;
2558 }
2559
2560 format = (fmt[6] << 8) | fmt[5]; /* remember the format value */
2561
2562 /* get format type */
2563 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, FORMAT_TYPE);
2564 if (!fmt) {
2565 snd_printk(KERN_ERR "%d:%u:%d : no FORMAT_TYPE desc\n",
2566 dev->devnum, iface_no, altno);
2567 continue;
2568 }
2569 if (fmt[0] < 8) {
2570 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
2571 dev->devnum, iface_no, altno);
2572 continue;
2573 }
2574
2575 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
2576 /* Creamware Noah has this descriptor after the 2nd endpoint */
2577 if (!csep && altsd->bNumEndpoints >= 2)
2578 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
2579 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2580 snd_printk(KERN_ERR "%d:%u:%d : no or invalid class specific endpoint descriptor\n",
2581 dev->devnum, iface_no, altno);
2582 continue;
2583 }
2584
2585 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2586 if (! fp) {
2587 snd_printk(KERN_ERR "cannot malloc\n");
2588 return -ENOMEM;
2589 }
2590
2591 memset(fp, 0, sizeof(*fp));
2592 fp->iface = iface_no;
2593 fp->altsetting = altno;
2594 fp->altset_idx = i;
2595 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2596 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Clemens Ladisch573567e2005-06-27 08:17:30 +02002598 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
2599 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
2600 * (fp->maxpacksize & 0x7ff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 fp->attributes = csep[3];
2602
2603 /* some quirks for attributes here */
2604
Clemens Ladischc3f93292005-05-04 14:56:04 +02002605 switch (chip->usb_id) {
2606 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 /* Optoplay sets the sample rate attribute although
2608 * it seems not supporting it in fact.
2609 */
2610 fp->attributes &= ~EP_CS_ATTR_SAMPLE_RATE;
Clemens Ladischc3f93292005-05-04 14:56:04 +02002611 break;
2612 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
2613 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 /* doesn't set the sample rate attribute, but supports it */
2615 fp->attributes |= EP_CS_ATTR_SAMPLE_RATE;
Clemens Ladischc3f93292005-05-04 14:56:04 +02002616 break;
2617 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
2618 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
2619 an older model 77d:223) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 /*
2621 * plantronics headset and Griffin iMic have set adaptive-in
2622 * although it's really not...
2623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 fp->ep_attr &= ~EP_ATTR_MASK;
2625 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2626 fp->ep_attr |= EP_ATTR_ADAPTIVE;
2627 else
2628 fp->ep_attr |= EP_ATTR_SYNC;
Clemens Ladischc3f93292005-05-04 14:56:04 +02002629 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
2631
2632 /* ok, let's parse further... */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002633 if (parse_audio_format(chip, fp, format, fmt, stream) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 kfree(fp->rate_table);
2635 kfree(fp);
2636 continue;
2637 }
2638
2639 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint 0x%x\n", dev->devnum, iface_no, i, fp->endpoint);
2640 err = add_audio_endpoint(chip, stream, fp);
2641 if (err < 0) {
2642 kfree(fp->rate_table);
2643 kfree(fp);
2644 return err;
2645 }
2646 /* try to set the interface... */
2647 usb_set_interface(chip->dev, iface_no, altno);
2648 init_usb_pitch(chip->dev, iface_no, alts, fp);
2649 init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
2650 }
2651 return 0;
2652}
2653
2654
2655/*
2656 * disconnect streams
2657 * called from snd_usb_audio_disconnect()
2658 */
Clemens Ladischee733392005-04-25 10:34:13 +02002659static void snd_usb_stream_disconnect(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
2661 int idx;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002662 struct snd_usb_stream *as;
2663 struct snd_usb_substream *subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Takashi Iwai86e07d32005-11-17 15:08:02 +01002665 as = list_entry(head, struct snd_usb_stream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 for (idx = 0; idx < 2; idx++) {
2667 subs = &as->substream[idx];
2668 if (!subs->num_formats)
2669 return;
2670 release_substream_urbs(subs, 1);
2671 subs->interface = -1;
2672 }
2673}
2674
2675/*
2676 * parse audio control descriptor and create pcm/midi streams
2677 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002678static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
2680 struct usb_device *dev = chip->dev;
2681 struct usb_host_interface *host_iface;
2682 struct usb_interface *iface;
2683 unsigned char *p1;
2684 int i, j;
2685
2686 /* find audiocontrol interface */
2687 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
2688 if (!(p1 = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen, NULL, HEADER))) {
2689 snd_printk(KERN_ERR "cannot find HEADER\n");
2690 return -EINVAL;
2691 }
2692 if (! p1[7] || p1[0] < 8 + p1[7]) {
2693 snd_printk(KERN_ERR "invalid HEADER\n");
2694 return -EINVAL;
2695 }
2696
2697 /*
2698 * parse all USB audio streaming interfaces
2699 */
2700 for (i = 0; i < p1[7]; i++) {
2701 struct usb_host_interface *alts;
2702 struct usb_interface_descriptor *altsd;
2703 j = p1[8 + i];
2704 iface = usb_ifnum_to_if(dev, j);
2705 if (!iface) {
2706 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
2707 dev->devnum, ctrlif, j);
2708 continue;
2709 }
2710 if (usb_interface_claimed(iface)) {
2711 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n", dev->devnum, ctrlif, j);
2712 continue;
2713 }
2714 alts = &iface->altsetting[0];
2715 altsd = get_iface_desc(alts);
2716 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
2717 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
2718 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDI_STREAMING) {
2719 if (snd_usb_create_midi_interface(chip, iface, NULL) < 0) {
2720 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n", dev->devnum, ctrlif, j);
2721 continue;
2722 }
2723 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2724 continue;
2725 }
2726 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2727 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2728 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING) {
2729 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n", dev->devnum, ctrlif, j, altsd->bInterfaceClass);
2730 /* skip non-supported classes */
2731 continue;
2732 }
2733 if (! parse_audio_endpoints(chip, j)) {
2734 usb_set_interface(dev, j, 0); /* reset the current interface */
2735 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2736 }
2737 }
2738
2739 return 0;
2740}
2741
2742/*
2743 * create a stream for an endpoint/altsetting without proper descriptors
2744 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002745static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002747 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 struct audioformat *fp;
2750 struct usb_host_interface *alts;
2751 int stream, err;
2752 int *rate_table = NULL;
2753
2754 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2755 if (! fp) {
2756 snd_printk(KERN_ERR "cannot malloc\n");
2757 return -ENOMEM;
2758 }
2759 memcpy(fp, quirk->data, sizeof(*fp));
2760 if (fp->nr_rates > 0) {
2761 rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
2762 if (!rate_table) {
2763 kfree(fp);
2764 return -ENOMEM;
2765 }
2766 memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
2767 fp->rate_table = rate_table;
2768 }
2769
2770 stream = (fp->endpoint & USB_DIR_IN)
2771 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2772 err = add_audio_endpoint(chip, stream, fp);
2773 if (err < 0) {
2774 kfree(fp);
2775 kfree(rate_table);
2776 return err;
2777 }
2778 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
2779 fp->altset_idx >= iface->num_altsetting) {
2780 kfree(fp);
2781 kfree(rate_table);
2782 return -EINVAL;
2783 }
2784 alts = &iface->altsetting[fp->altset_idx];
2785 usb_set_interface(chip->dev, fp->iface, 0);
2786 init_usb_pitch(chip->dev, fp->iface, alts, fp);
2787 init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
2788 return 0;
2789}
2790
2791/*
2792 * create a stream for an interface with proper descriptors
2793 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002794static int create_standard_audio_quirk(struct snd_usb_audio *chip,
Clemens Ladischd1bda042005-09-14 08:36:03 +02002795 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002796 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797{
2798 struct usb_host_interface *alts;
2799 struct usb_interface_descriptor *altsd;
2800 int err;
2801
2802 alts = &iface->altsetting[0];
2803 altsd = get_iface_desc(alts);
Clemens Ladischd1bda042005-09-14 08:36:03 +02002804 err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (err < 0) {
2806 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
2807 altsd->bInterfaceNumber, err);
2808 return err;
2809 }
Clemens Ladischd1bda042005-09-14 08:36:03 +02002810 /* reset the current interface */
2811 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 return 0;
2813}
2814
2815/*
2816 * Create a stream for an Edirol UA-700/UA-25 interface. The only way
2817 * to detect the sample rate is by looking at wMaxPacketSize.
2818 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002819static int create_ua700_ua25_quirk(struct snd_usb_audio *chip,
Clemens Ladisch854af952005-07-25 16:19:10 +02002820 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002821 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
2823 static const struct audioformat ua_format = {
2824 .format = SNDRV_PCM_FORMAT_S24_3LE,
2825 .channels = 2,
2826 .fmt_type = USB_FORMAT_TYPE_I,
2827 .altsetting = 1,
2828 .altset_idx = 1,
2829 .rates = SNDRV_PCM_RATE_CONTINUOUS,
2830 };
2831 struct usb_host_interface *alts;
2832 struct usb_interface_descriptor *altsd;
2833 struct audioformat *fp;
2834 int stream, err;
2835
2836 /* both PCM and MIDI interfaces have 2 altsettings */
2837 if (iface->num_altsetting != 2)
2838 return -ENXIO;
2839 alts = &iface->altsetting[1];
2840 altsd = get_iface_desc(alts);
2841
2842 if (altsd->bNumEndpoints == 2) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002843 static const struct snd_usb_midi_endpoint_info ua700_ep = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 .out_cables = 0x0003,
2845 .in_cables = 0x0003
2846 };
Takashi Iwai86e07d32005-11-17 15:08:02 +01002847 static const struct snd_usb_audio_quirk ua700_quirk = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 .type = QUIRK_MIDI_FIXED_ENDPOINT,
2849 .data = &ua700_ep
2850 };
Takashi Iwai86e07d32005-11-17 15:08:02 +01002851 static const struct snd_usb_midi_endpoint_info ua25_ep = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 .out_cables = 0x0001,
2853 .in_cables = 0x0001
2854 };
Takashi Iwai86e07d32005-11-17 15:08:02 +01002855 static const struct snd_usb_audio_quirk ua25_quirk = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 .type = QUIRK_MIDI_FIXED_ENDPOINT,
2857 .data = &ua25_ep
2858 };
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002859 if (chip->usb_id == USB_ID(0x0582, 0x002b))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return snd_usb_create_midi_interface(chip, iface,
2861 &ua700_quirk);
2862 else
2863 return snd_usb_create_midi_interface(chip, iface,
2864 &ua25_quirk);
2865 }
2866
2867 if (altsd->bNumEndpoints != 1)
2868 return -ENXIO;
2869
2870 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2871 if (!fp)
2872 return -ENOMEM;
2873 memcpy(fp, &ua_format, sizeof(*fp));
2874
2875 fp->iface = altsd->bInterfaceNumber;
2876 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2877 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2878 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2879
2880 switch (fp->maxpacksize) {
2881 case 0x120:
2882 fp->rate_max = fp->rate_min = 44100;
2883 break;
2884 case 0x138:
2885 case 0x140:
2886 fp->rate_max = fp->rate_min = 48000;
2887 break;
2888 case 0x258:
2889 case 0x260:
2890 fp->rate_max = fp->rate_min = 96000;
2891 break;
2892 default:
2893 snd_printk(KERN_ERR "unknown sample rate\n");
2894 kfree(fp);
2895 return -ENXIO;
2896 }
2897
2898 stream = (fp->endpoint & USB_DIR_IN)
2899 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2900 err = add_audio_endpoint(chip, stream, fp);
2901 if (err < 0) {
2902 kfree(fp);
2903 return err;
2904 }
2905 usb_set_interface(chip->dev, fp->iface, 0);
2906 return 0;
2907}
2908
2909/*
2910 * Create a stream for an Edirol UA-1000 interface.
2911 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002912static int create_ua1000_quirk(struct snd_usb_audio *chip,
Clemens Ladisch854af952005-07-25 16:19:10 +02002913 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002914 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
2916 static const struct audioformat ua1000_format = {
2917 .format = SNDRV_PCM_FORMAT_S32_LE,
2918 .fmt_type = USB_FORMAT_TYPE_I,
2919 .altsetting = 1,
2920 .altset_idx = 1,
2921 .attributes = 0,
2922 .rates = SNDRV_PCM_RATE_CONTINUOUS,
2923 };
2924 struct usb_host_interface *alts;
2925 struct usb_interface_descriptor *altsd;
2926 struct audioformat *fp;
2927 int stream, err;
2928
2929 if (iface->num_altsetting != 2)
2930 return -ENXIO;
2931 alts = &iface->altsetting[1];
2932 altsd = get_iface_desc(alts);
2933 if (alts->extralen != 11 || alts->extra[1] != CS_AUDIO_INTERFACE ||
2934 altsd->bNumEndpoints != 1)
2935 return -ENXIO;
2936
2937 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2938 if (!fp)
2939 return -ENOMEM;
2940 memcpy(fp, &ua1000_format, sizeof(*fp));
2941
2942 fp->channels = alts->extra[4];
2943 fp->iface = altsd->bInterfaceNumber;
2944 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2945 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2946 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2947 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]);
2948
2949 stream = (fp->endpoint & USB_DIR_IN)
2950 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2951 err = add_audio_endpoint(chip, stream, fp);
2952 if (err < 0) {
2953 kfree(fp);
2954 return err;
2955 }
2956 /* FIXME: playback must be synchronized to capture */
2957 usb_set_interface(chip->dev, fp->iface, 0);
2958 return 0;
2959}
2960
Takashi Iwai86e07d32005-11-17 15:08:02 +01002961static int snd_usb_create_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002963 const struct snd_usb_audio_quirk *quirk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
2965/*
2966 * handle the quirks for the contained interfaces
2967 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002968static int create_composite_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002970 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
2972 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
2973 int err;
2974
2975 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
2976 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
2977 if (!iface)
2978 continue;
2979 if (quirk->ifnum != probed_ifnum &&
2980 usb_interface_claimed(iface))
2981 continue;
2982 err = snd_usb_create_quirk(chip, iface, quirk);
2983 if (err < 0)
2984 return err;
2985 if (quirk->ifnum != probed_ifnum)
2986 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2987 }
2988 return 0;
2989}
2990
Takashi Iwai86e07d32005-11-17 15:08:02 +01002991static int ignore_interface_quirk(struct snd_usb_audio *chip,
Clemens Ladisch854af952005-07-25 16:19:10 +02002992 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002993 const struct snd_usb_audio_quirk *quirk)
Clemens Ladisch854af952005-07-25 16:19:10 +02002994{
2995 return 0;
2996}
2997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999/*
3000 * boot quirks
3001 */
3002
3003#define EXTIGY_FIRMWARE_SIZE_OLD 794
3004#define EXTIGY_FIRMWARE_SIZE_NEW 483
3005
3006static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
3007{
3008 struct usb_host_config *config = dev->actconfig;
3009 int err;
3010
3011 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
3012 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
3013 snd_printdd("sending Extigy boot sequence...\n");
3014 /* Send message to force it to reconnect with full interface. */
3015 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
3016 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
3017 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
3018 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
3019 &dev->descriptor, sizeof(dev->descriptor));
3020 config = dev->actconfig;
3021 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
3022 err = usb_reset_configuration(dev);
3023 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
3024 snd_printdd("extigy_boot: new boot length = %d\n",
3025 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
3026 return -ENODEV; /* quit this anyway */
3027 }
3028 return 0;
3029}
3030
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003031static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
3032{
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003033 u8 buf = 1;
3034
3035 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
3036 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3037 0, 0, &buf, 1, 1000);
3038 if (buf == 0) {
3039 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
3040 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3041 1, 2000, NULL, 0, 1000);
3042 return -ENODEV;
3043 }
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003044 return 0;
3045}
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
3048/*
3049 * audio-interface quirks
3050 *
3051 * returns zero if no standard audio/MIDI parsing is needed.
3052 * returns a postive value if standard audio/midi interfaces are parsed
3053 * after this.
3054 * returns a negative value at error.
3055 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003056static int snd_usb_create_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003058 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003060 typedef int (*quirk_func_t)(struct snd_usb_audio *, struct usb_interface *,
3061 const struct snd_usb_audio_quirk *);
Clemens Ladisch854af952005-07-25 16:19:10 +02003062 static const quirk_func_t quirk_funcs[] = {
3063 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
3064 [QUIRK_COMPOSITE] = create_composite_quirk,
3065 [QUIRK_MIDI_STANDARD_INTERFACE] = snd_usb_create_midi_interface,
3066 [QUIRK_MIDI_FIXED_ENDPOINT] = snd_usb_create_midi_interface,
3067 [QUIRK_MIDI_YAMAHA] = snd_usb_create_midi_interface,
3068 [QUIRK_MIDI_MIDIMAN] = snd_usb_create_midi_interface,
3069 [QUIRK_MIDI_NOVATION] = snd_usb_create_midi_interface,
3070 [QUIRK_MIDI_RAW] = snd_usb_create_midi_interface,
3071 [QUIRK_MIDI_EMAGIC] = snd_usb_create_midi_interface,
3072 [QUIRK_MIDI_MIDITECH] = snd_usb_create_midi_interface,
Clemens Ladischd1bda042005-09-14 08:36:03 +02003073 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
Clemens Ladisch854af952005-07-25 16:19:10 +02003074 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
3075 [QUIRK_AUDIO_EDIROL_UA700_UA25] = create_ua700_ua25_quirk,
3076 [QUIRK_AUDIO_EDIROL_UA1000] = create_ua1000_quirk,
3077 };
3078
3079 if (quirk->type < QUIRK_TYPE_COUNT) {
3080 return quirk_funcs[quirk->type](chip, iface, quirk);
3081 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
3083 return -ENXIO;
3084 }
3085}
3086
3087
3088/*
3089 * common proc files to show the usb device info
3090 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003091static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003093 struct snd_usb_audio *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (! chip->shutdown)
3095 snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
3096}
3097
Takashi Iwai86e07d32005-11-17 15:08:02 +01003098static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003100 struct snd_usb_audio *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 if (! chip->shutdown)
3102 snd_iprintf(buffer, "%04x:%04x\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003103 USB_ID_VENDOR(chip->usb_id),
3104 USB_ID_PRODUCT(chip->usb_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105}
3106
Takashi Iwai86e07d32005-11-17 15:08:02 +01003107static void snd_usb_audio_create_proc(struct snd_usb_audio *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003109 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (! snd_card_proc_new(chip->card, "usbbus", &entry))
3111 snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbbus_read);
3112 if (! snd_card_proc_new(chip->card, "usbid", &entry))
3113 snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbid_read);
3114}
3115
3116/*
3117 * free the chip instance
3118 *
3119 * here we have to do not much, since pcm and controls are already freed
3120 *
3121 */
3122
Takashi Iwai86e07d32005-11-17 15:08:02 +01003123static int snd_usb_audio_free(struct snd_usb_audio *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
3125 kfree(chip);
3126 return 0;
3127}
3128
Takashi Iwai86e07d32005-11-17 15:08:02 +01003129static int snd_usb_audio_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003131 struct snd_usb_audio *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 return snd_usb_audio_free(chip);
3133}
3134
3135
3136/*
3137 * create a chip instance and set its names.
3138 */
3139static int snd_usb_audio_create(struct usb_device *dev, int idx,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003140 const struct snd_usb_audio_quirk *quirk,
3141 struct snd_usb_audio **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003143 struct snd_card *card;
3144 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 int err, len;
3146 char component[14];
Takashi Iwai86e07d32005-11-17 15:08:02 +01003147 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 .dev_free = snd_usb_audio_dev_free,
3149 };
3150
3151 *rchip = NULL;
3152
3153 if (snd_usb_get_speed(dev) != USB_SPEED_FULL &&
3154 snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
3155 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
3156 return -ENXIO;
3157 }
3158
3159 card = snd_card_new(index[idx], id[idx], THIS_MODULE, 0);
3160 if (card == NULL) {
3161 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
3162 return -ENOMEM;
3163 }
3164
Takashi Iwai561b2202005-09-09 14:22:34 +02003165 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 if (! chip) {
3167 snd_card_free(card);
3168 return -ENOMEM;
3169 }
3170
3171 chip->index = idx;
3172 chip->dev = dev;
3173 chip->card = card;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003174 chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3175 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 INIT_LIST_HEAD(&chip->pcm_list);
3177 INIT_LIST_HEAD(&chip->midi_list);
Clemens Ladisch84957a82005-04-29 16:23:13 +02003178 INIT_LIST_HEAD(&chip->mixer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
3180 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
3181 snd_usb_audio_free(chip);
3182 snd_card_free(card);
3183 return err;
3184 }
3185
3186 strcpy(card->driver, "USB-Audio");
3187 sprintf(component, "USB%04x:%04x",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003188 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 snd_component_add(card, component);
3190
3191 /* retrieve the device string as shortname */
3192 if (quirk && quirk->product_name) {
3193 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
3194 } else {
3195 if (!dev->descriptor.iProduct ||
3196 usb_string(dev, dev->descriptor.iProduct,
3197 card->shortname, sizeof(card->shortname)) <= 0) {
3198 /* no name available from anywhere, so use ID */
3199 sprintf(card->shortname, "USB Device %#04x:%#04x",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003200 USB_ID_VENDOR(chip->usb_id),
3201 USB_ID_PRODUCT(chip->usb_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 }
3203 }
3204
3205 /* retrieve the vendor and device strings as longname */
3206 if (quirk && quirk->vendor_name) {
3207 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
3208 } else {
3209 if (dev->descriptor.iManufacturer)
3210 len = usb_string(dev, dev->descriptor.iManufacturer,
3211 card->longname, sizeof(card->longname));
3212 else
3213 len = 0;
3214 /* we don't really care if there isn't any vendor string */
3215 }
3216 if (len > 0)
3217 strlcat(card->longname, " ", sizeof(card->longname));
3218
3219 strlcat(card->longname, card->shortname, sizeof(card->longname));
3220
3221 len = strlcat(card->longname, " at ", sizeof(card->longname));
3222
3223 if (len < sizeof(card->longname))
3224 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
3225
3226 strlcat(card->longname,
3227 snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" : ", high speed",
3228 sizeof(card->longname));
3229
3230 snd_usb_audio_create_proc(chip);
3231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 *rchip = chip;
3233 return 0;
3234}
3235
3236
3237/*
3238 * probe the active usb device
3239 *
3240 * note that this can be called multiple times per a device, when it
3241 * includes multiple audio control interfaces.
3242 *
3243 * thus we check the usb device pointer and creates the card instance
3244 * only at the first time. the successive calls of this function will
3245 * append the pcm interface to the corresponding card.
3246 */
3247static void *snd_usb_audio_probe(struct usb_device *dev,
3248 struct usb_interface *intf,
3249 const struct usb_device_id *usb_id)
3250{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003251 const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 int i, err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01003253 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 struct usb_host_interface *alts;
3255 int ifnum;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003256 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
3258 alts = &intf->altsetting[0];
3259 ifnum = get_iface_desc(alts)->bInterfaceNumber;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003260 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3261 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
3264 goto __err_val;
3265
3266 /* SB Extigy needs special boot-up sequence */
3267 /* if more models come, this will go to the quirk list. */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003268 if (id == USB_ID(0x041e, 0x3000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 if (snd_usb_extigy_boot_quirk(dev, intf) < 0)
3270 goto __err_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003272 /* SB Audigy 2 NX needs its own boot-up magic, too */
3273 if (id == USB_ID(0x041e, 0x3020)) {
3274 if (snd_usb_audigy2nx_boot_quirk(dev) < 0)
3275 goto __err_val;
3276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
3278 /*
3279 * found a config. now register to ALSA
3280 */
3281
3282 /* check whether it's already registered */
3283 chip = NULL;
3284 down(&register_mutex);
3285 for (i = 0; i < SNDRV_CARDS; i++) {
3286 if (usb_chip[i] && usb_chip[i]->dev == dev) {
3287 if (usb_chip[i]->shutdown) {
3288 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
3289 goto __error;
3290 }
3291 chip = usb_chip[i];
3292 break;
3293 }
3294 }
3295 if (! chip) {
3296 /* it's a fresh one.
3297 * now look for an empty slot and create a new card instance
3298 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 for (i = 0; i < SNDRV_CARDS; i++)
3300 if (enable[i] && ! usb_chip[i] &&
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003301 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
3302 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
3304 goto __error;
3305 }
Clemens Ladisch1dcd3ec2005-05-10 14:51:40 +02003306 snd_card_set_dev(chip->card, &intf->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 break;
3308 }
3309 if (! chip) {
3310 snd_printk(KERN_ERR "no available usb audio device\n");
3311 goto __error;
3312 }
3313 }
3314
3315 err = 1; /* continue */
3316 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
3317 /* need some special handlings */
3318 if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)
3319 goto __error;
3320 }
3321
3322 if (err > 0) {
3323 /* create normal USB audio interfaces */
3324 if (snd_usb_create_streams(chip, ifnum) < 0 ||
3325 snd_usb_create_mixer(chip, ifnum) < 0) {
3326 goto __error;
3327 }
3328 }
3329
3330 /* we are allowed to call snd_card_register() many times */
3331 if (snd_card_register(chip->card) < 0) {
3332 goto __error;
3333 }
3334
3335 usb_chip[chip->index] = chip;
3336 chip->num_interfaces++;
3337 up(&register_mutex);
3338 return chip;
3339
3340 __error:
3341 if (chip && !chip->num_interfaces)
3342 snd_card_free(chip->card);
3343 up(&register_mutex);
3344 __err_val:
3345 return NULL;
3346}
3347
3348/*
3349 * we need to take care of counter, since disconnection can be called also
3350 * many times as well as usb_audio_probe().
3351 */
3352static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
3353{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003354 struct snd_usb_audio *chip;
3355 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 struct list_head *p;
3357
3358 if (ptr == (void *)-1L)
3359 return;
3360
3361 chip = ptr;
3362 card = chip->card;
3363 down(&register_mutex);
3364 chip->shutdown = 1;
3365 chip->num_interfaces--;
3366 if (chip->num_interfaces <= 0) {
3367 snd_card_disconnect(card);
3368 /* release the pcm resources */
3369 list_for_each(p, &chip->pcm_list) {
Clemens Ladischee733392005-04-25 10:34:13 +02003370 snd_usb_stream_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
3372 /* release the midi resources */
3373 list_for_each(p, &chip->midi_list) {
Clemens Ladischee733392005-04-25 10:34:13 +02003374 snd_usbmidi_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02003376 /* release mixer resources */
3377 list_for_each(p, &chip->mixer_list) {
3378 snd_usb_mixer_disconnect(p);
3379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 usb_chip[chip->index] = NULL;
3381 up(&register_mutex);
Karsten Wiese230cd5e2005-04-20 10:12:35 +02003382 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 } else {
3384 up(&register_mutex);
3385 }
3386}
3387
3388/*
3389 * new 2.5 USB kernel API
3390 */
3391static int usb_audio_probe(struct usb_interface *intf,
3392 const struct usb_device_id *id)
3393{
3394 void *chip;
3395 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
3396 if (chip) {
3397 dev_set_drvdata(&intf->dev, chip);
3398 return 0;
3399 } else
3400 return -EIO;
3401}
3402
3403static void usb_audio_disconnect(struct usb_interface *intf)
3404{
3405 snd_usb_audio_disconnect(interface_to_usbdev(intf),
3406 dev_get_drvdata(&intf->dev));
3407}
3408
3409
3410static int __init snd_usb_audio_init(void)
3411{
3412 if (nrpacks < MIN_PACKS_URB || nrpacks > MAX_PACKS) {
3413 printk(KERN_WARNING "invalid nrpacks value.\n");
3414 return -EINVAL;
3415 }
3416 usb_register(&usb_audio_driver);
3417 return 0;
3418}
3419
3420
3421static void __exit snd_usb_audio_cleanup(void)
3422{
3423 usb_deregister(&usb_audio_driver);
3424}
3425
3426module_init(snd_usb_audio_init);
3427module_exit(snd_usb_audio_cleanup);