blob: 6fad2c40c77cf432a01469ec3f422e6be3b7509c [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>
Ingo Molnar12aa7572006-01-16 16:36:05 +010050#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <sound/core.h>
52#include <sound/info.h>
53#include <sound/pcm.h>
54#include <sound/pcm_params.h>
55#include <sound/initval.h>
56
57#include "usbaudio.h"
58
59
60MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
61MODULE_DESCRIPTION("USB Audio");
62MODULE_LICENSE("GPL");
63MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
64
65
66static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
67static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
68static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
69static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Vendor ID for this card */
70static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Product ID for this card */
71static int nrpacks = 4; /* max. number of packets per urb */
72static int async_unlink = 1;
73
74module_param_array(index, int, NULL, 0444);
75MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
76module_param_array(id, charp, NULL, 0444);
77MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
78module_param_array(enable, bool, NULL, 0444);
79MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
80module_param_array(vid, int, NULL, 0444);
81MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
82module_param_array(pid, int, NULL, 0444);
83MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
Clemens Ladisch71d848c2005-08-12 15:18:00 +020084module_param(nrpacks, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
86module_param(async_unlink, bool, 0444);
87MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
88
89
90/*
91 * debug the h/w constraints
92 */
93/* #define HW_CONST_DEBUG */
94
95
96/*
97 *
98 */
99
100#define MAX_PACKS 10
101#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
Clemens Ladisch15a24c02005-08-12 08:25:26 +0200102#define MAX_URBS 8
Clemens Ladisch604cf492005-05-17 09:15:27 +0200103#define SYNC_URBS 4 /* always four urbs for sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#define MIN_PACKS_URB 1 /* minimum 1 packet per urb */
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106struct audioformat {
107 struct list_head list;
108 snd_pcm_format_t format; /* format type */
109 unsigned int channels; /* # channels */
110 unsigned int fmt_type; /* USB audio format type (1-3) */
111 unsigned int frame_size; /* samples per frame for non-audio */
112 int iface; /* interface number */
113 unsigned char altsetting; /* corresponding alternate setting */
114 unsigned char altset_idx; /* array index of altenate setting */
115 unsigned char attributes; /* corresponding attributes of cs endpoint */
116 unsigned char endpoint; /* endpoint */
117 unsigned char ep_attr; /* endpoint attributes */
118 unsigned int maxpacksize; /* max. packet size */
119 unsigned int rates; /* rate bitmasks */
120 unsigned int rate_min, rate_max; /* min/max rates */
121 unsigned int nr_rates; /* number of rate table entries */
122 unsigned int *rate_table; /* rate table */
123};
124
Takashi Iwai86e07d32005-11-17 15:08:02 +0100125struct snd_usb_substream;
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127struct snd_urb_ctx {
128 struct urb *urb;
Clemens Ladisch55851f72005-08-15 08:34:16 +0200129 unsigned int buffer_size; /* size of data buffer, if data URB */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100130 struct snd_usb_substream *subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int index; /* index for urb array */
132 int packets; /* number of packets per urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135struct snd_urb_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100136 int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
137 int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
138 int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
139 int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
142struct snd_usb_substream {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100143 struct snd_usb_stream *stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct usb_device *dev;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100145 struct snd_pcm_substream *pcm_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int direction; /* playback or capture */
147 int interface; /* current interface */
148 int endpoint; /* assigned endpoint */
149 struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
150 unsigned int cur_rate; /* current rate (for hw_params callback) */
151 unsigned int period_bytes; /* current period bytes (for hw_params callback) */
152 unsigned int format; /* USB data format */
153 unsigned int datapipe; /* the data i/o pipe */
154 unsigned int syncpipe; /* 1 - async out or adaptive in */
Clemens Ladisch573567e2005-06-27 08:17:30 +0200155 unsigned int datainterval; /* log_2 of data packet interval */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
157 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
158 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
159 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
160 unsigned int phase; /* phase accumulator */
161 unsigned int maxpacksize; /* max packet size in bytes */
162 unsigned int maxframesize; /* max packet size in frames */
163 unsigned int curpacksize; /* current packet size in bytes (for capture) */
164 unsigned int curframesize; /* current packet size in frames (for capture) */
165 unsigned int fill_max: 1; /* fill max packet size always */
166 unsigned int fmt_type; /* USB audio format type (1-3) */
Clemens Ladisch9624ea82005-08-15 08:25:24 +0200167 unsigned int packs_per_ms; /* packets per millisecond (for playback) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 unsigned int running: 1; /* running status */
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 unsigned int hwptr_done; /* processed frame position in the buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned int transfer_done; /* processed frames since last period update */
173 unsigned long active_mask; /* bitmask of active urbs */
174 unsigned long unlink_mask; /* bitmask of unlinked urbs */
175
176 unsigned int nurbs; /* # urbs */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100177 struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */
178 struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */
Clemens Ladisch55851f72005-08-15 08:34:16 +0200179 char *syncbuf; /* sync buffer for all sync URBs */
180 dma_addr_t sync_dma; /* DMA address of syncbuf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 u64 formats; /* format bitmasks (all or'ed) */
183 unsigned int num_formats; /* number of supported audio formats (list) */
184 struct list_head fmt_list; /* format list */
185 spinlock_t lock;
186
187 struct snd_urb_ops ops; /* callbacks (must be filled at init) */
188};
189
190
191struct snd_usb_stream {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100192 struct snd_usb_audio *chip;
193 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int pcm_index;
195 unsigned int fmt_type; /* USB audio format type (1-3) */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100196 struct snd_usb_substream substream[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct list_head list;
198};
199
200
201/*
202 * we keep the snd_usb_audio_t instances by ourselves for merging
203 * the all interfaces on the same card as one sound device.
204 */
205
Ingo Molnar12aa7572006-01-16 16:36:05 +0100206static DEFINE_MUTEX(register_mutex);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100207static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209
210/*
211 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
212 * this will overflow at approx 524 kHz
213 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700214static inline unsigned get_usb_full_speed_rate(unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 return ((rate << 13) + 62) / 125;
217}
218
219/*
220 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
221 * this will overflow at approx 4 MHz
222 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700223static inline unsigned get_usb_high_speed_rate(unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 return ((rate << 10) + 62) / 125;
226}
227
228/* convert our full speed USB rate into sampling rate in Hz */
Jesper Juhl77933d72005-07-27 11:46:09 -0700229static inline unsigned get_full_speed_hz(unsigned int usb_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 return (usb_rate * 125 + (1 << 12)) >> 13;
232}
233
234/* convert our high speed USB rate into sampling rate in Hz */
Jesper Juhl77933d72005-07-27 11:46:09 -0700235static inline unsigned get_high_speed_hz(unsigned int usb_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 return (usb_rate * 125 + (1 << 9)) >> 10;
238}
239
240
241/*
242 * prepare urb for full speed capture sync pipe
243 *
244 * fill the length and offset of each urb descriptor.
245 * the fixed 10.14 frequency is passed through the pipe.
246 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100247static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
248 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct urb *urb)
250{
251 unsigned char *cp = urb->transfer_buffer;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100252 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200255 urb->iso_frame_desc[0].length = 3;
256 urb->iso_frame_desc[0].offset = 0;
257 cp[0] = subs->freqn >> 2;
258 cp[1] = subs->freqn >> 10;
259 cp[2] = subs->freqn >> 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
261}
262
263/*
264 * prepare urb for high speed capture sync pipe
265 *
266 * fill the length and offset of each urb descriptor.
267 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
268 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100269static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
270 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct urb *urb)
272{
273 unsigned char *cp = urb->transfer_buffer;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100274 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200277 urb->iso_frame_desc[0].length = 4;
278 urb->iso_frame_desc[0].offset = 0;
279 cp[0] = subs->freqn;
280 cp[1] = subs->freqn >> 8;
281 cp[2] = subs->freqn >> 16;
282 cp[3] = subs->freqn >> 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284}
285
286/*
287 * process after capture sync complete
288 * - nothing to do
289 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100290static int retire_capture_sync_urb(struct snd_usb_substream *subs,
291 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct urb *urb)
293{
294 return 0;
295}
296
297/*
298 * prepare urb for capture data pipe
299 *
300 * fill the offset and length of each descriptor.
301 *
302 * we use a temporary buffer to write the captured data.
303 * since the length of written data is determined by host, we cannot
304 * write onto the pcm buffer directly... the data is thus copied
305 * later at complete callback to the global buffer.
306 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100307static int prepare_capture_urb(struct snd_usb_substream *subs,
308 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 struct urb *urb)
310{
311 int i, offs;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100312 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 offs = 0;
315 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 for (i = 0; i < ctx->packets; i++) {
317 urb->iso_frame_desc[i].offset = offs;
318 urb->iso_frame_desc[i].length = subs->curpacksize;
319 offs += subs->curpacksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 urb->transfer_buffer_length = offs;
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200322 urb->number_of_packets = ctx->packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#if 0 // for check
324 if (! urb->bandwidth) {
325 int bustime;
326 bustime = usb_check_bandwidth(urb->dev, urb);
327 if (bustime < 0)
328 return bustime;
329 printk("urb %d: bandwidth = %d (packets = %d)\n", ctx->index, bustime, urb->number_of_packets);
330 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
331 }
332#endif // for check
333 return 0;
334}
335
336/*
337 * process after capture complete
338 *
339 * copy the data from each desctiptor to the pcm buffer, and
340 * update the current position.
341 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100342static int retire_capture_urb(struct snd_usb_substream *subs,
343 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct urb *urb)
345{
346 unsigned long flags;
347 unsigned char *cp;
348 int i;
349 unsigned int stride, len, oldptr;
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200350 int period_elapsed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 stride = runtime->frame_bits >> 3;
353
354 for (i = 0; i < urb->number_of_packets; i++) {
355 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
356 if (urb->iso_frame_desc[i].status) {
357 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
358 // continue;
359 }
360 len = urb->iso_frame_desc[i].actual_length / stride;
361 if (! len)
362 continue;
363 /* update the current pointer */
364 spin_lock_irqsave(&subs->lock, flags);
365 oldptr = subs->hwptr_done;
366 subs->hwptr_done += len;
367 if (subs->hwptr_done >= runtime->buffer_size)
368 subs->hwptr_done -= runtime->buffer_size;
369 subs->transfer_done += len;
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200370 if (subs->transfer_done >= runtime->period_size) {
371 subs->transfer_done -= runtime->period_size;
372 period_elapsed = 1;
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 spin_unlock_irqrestore(&subs->lock, flags);
375 /* copy a data chunk */
376 if (oldptr + len > runtime->buffer_size) {
377 unsigned int cnt = runtime->buffer_size - oldptr;
378 unsigned int blen = cnt * stride;
379 memcpy(runtime->dma_area + oldptr * stride, cp, blen);
380 memcpy(runtime->dma_area, cp + blen, len * stride - blen);
381 } else {
382 memcpy(runtime->dma_area + oldptr * stride, cp, len * stride);
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200385 if (period_elapsed)
386 snd_pcm_period_elapsed(subs->pcm_substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 0;
388}
389
390
391/*
392 * prepare urb for full speed playback sync pipe
393 *
394 * set up the offset and length to receive the current frequency.
395 */
396
Takashi Iwai86e07d32005-11-17 15:08:02 +0100397static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
398 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct urb *urb)
400{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100401 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200404 urb->iso_frame_desc[0].length = 3;
405 urb->iso_frame_desc[0].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
407}
408
409/*
410 * prepare urb for high speed playback sync pipe
411 *
412 * set up the offset and length to receive the current frequency.
413 */
414
Takashi Iwai86e07d32005-11-17 15:08:02 +0100415static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
416 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct urb *urb)
418{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100419 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200422 urb->iso_frame_desc[0].length = 4;
423 urb->iso_frame_desc[0].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 0;
425}
426
427/*
428 * process after full speed playback sync complete
429 *
430 * retrieve the current 10.14 frequency from pipe, and set it.
431 * the value is referred in prepare_playback_urb().
432 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100433static int retire_playback_sync_urb(struct snd_usb_substream *subs,
434 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct urb *urb)
436{
Clemens Ladischca810902005-05-02 08:55:54 +0200437 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 unsigned long flags;
439
Clemens Ladischca810902005-05-02 08:55:54 +0200440 if (urb->iso_frame_desc[0].status == 0 &&
441 urb->iso_frame_desc[0].actual_length == 3) {
442 f = combine_triple((u8*)urb->transfer_buffer) << 2;
Clemens Ladisch50cdbf12005-05-13 07:44:13 +0200443 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
444 spin_lock_irqsave(&subs->lock, flags);
445 subs->freqm = f;
446 spin_unlock_irqrestore(&subs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
450 return 0;
451}
452
453/*
454 * process after high speed playback sync complete
455 *
456 * retrieve the current 12.13 frequency from pipe, and set it.
457 * the value is referred in prepare_playback_urb().
458 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100459static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
460 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct urb *urb)
462{
Clemens Ladischca810902005-05-02 08:55:54 +0200463 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 unsigned long flags;
465
Clemens Ladischca810902005-05-02 08:55:54 +0200466 if (urb->iso_frame_desc[0].status == 0 &&
467 urb->iso_frame_desc[0].actual_length == 4) {
468 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
Clemens Ladisch50cdbf12005-05-13 07:44:13 +0200469 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
470 spin_lock_irqsave(&subs->lock, flags);
471 subs->freqm = f;
472 spin_unlock_irqrestore(&subs->lock, flags);
473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
476 return 0;
477}
478
Clemens Ladisch9568f462006-01-12 08:19:21 +0100479/* determine the number of frames in the next packet */
480static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
481{
482 if (subs->fill_max)
483 return subs->maxframesize;
484 else {
485 subs->phase = (subs->phase & 0xffff)
486 + (subs->freqm << subs->datainterval);
487 return min(subs->phase >> 16, subs->maxframesize);
488 }
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/*
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100492 * Prepare urb for streaming before playback starts.
493 *
Clemens Ladisch4b284922006-01-12 08:17:49 +0100494 * We don't yet have data, so we send a frame of silence.
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100495 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100496static int prepare_startup_playback_urb(struct snd_usb_substream *subs,
497 struct snd_pcm_runtime *runtime,
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100498 struct urb *urb)
499{
Clemens Ladisch4b284922006-01-12 08:17:49 +0100500 unsigned int i, offs, counts;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100501 struct snd_urb_ctx *ctx = urb->context;
Clemens Ladisch4b284922006-01-12 08:17:49 +0100502 int stride = runtime->frame_bits >> 3;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100503
Clemens Ladisch4b284922006-01-12 08:17:49 +0100504 offs = 0;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100505 urb->dev = ctx->subs->dev;
506 urb->number_of_packets = subs->packs_per_ms;
507 for (i = 0; i < subs->packs_per_ms; ++i) {
Clemens Ladisch9568f462006-01-12 08:19:21 +0100508 counts = snd_usb_audio_next_packet_size(subs);
Clemens Ladisch4b284922006-01-12 08:17:49 +0100509 urb->iso_frame_desc[i].offset = offs * stride;
510 urb->iso_frame_desc[i].length = counts * stride;
511 offs += counts;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100512 }
Clemens Ladisch4b284922006-01-12 08:17:49 +0100513 urb->transfer_buffer_length = offs * stride;
514 memset(urb->transfer_buffer,
515 subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
516 offs * stride);
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100517 return 0;
518}
519
520/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * prepare urb for playback data pipe
522 *
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200523 * Since a URB can handle only a single linear buffer, we must use double
524 * buffering when the data to be transferred overflows the buffer boundary.
525 * To avoid inconsistencies when updating hwptr_done, we use double buffering
526 * for all URBs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100528static int prepare_playback_urb(struct snd_usb_substream *subs,
529 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct urb *urb)
531{
532 int i, stride, offs;
533 unsigned int counts;
534 unsigned long flags;
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200535 int period_elapsed = 0;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100536 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 stride = runtime->frame_bits >> 3;
539
540 offs = 0;
541 urb->dev = ctx->subs->dev; /* we need to set this at each time */
542 urb->number_of_packets = 0;
543 spin_lock_irqsave(&subs->lock, flags);
544 for (i = 0; i < ctx->packets; i++) {
Clemens Ladisch9568f462006-01-12 08:19:21 +0100545 counts = snd_usb_audio_next_packet_size(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* set up descriptor */
547 urb->iso_frame_desc[i].offset = offs * stride;
548 urb->iso_frame_desc[i].length = counts * stride;
549 offs += counts;
550 urb->number_of_packets++;
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200551 subs->transfer_done += counts;
552 if (subs->transfer_done >= runtime->period_size) {
553 subs->transfer_done -= runtime->period_size;
554 period_elapsed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (subs->fmt_type == USB_FORMAT_TYPE_II) {
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200556 if (subs->transfer_done > 0) {
557 /* FIXME: fill-max mode is not
558 * supported yet */
559 offs -= subs->transfer_done;
560 counts -= subs->transfer_done;
561 urb->iso_frame_desc[i].length =
562 counts * stride;
563 subs->transfer_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 i++;
566 if (i < ctx->packets) {
567 /* add a transfer delimiter */
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200568 urb->iso_frame_desc[i].offset =
569 offs * stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 urb->iso_frame_desc[i].length = 0;
571 urb->number_of_packets++;
572 }
Clemens Ladisch9624ea82005-08-15 08:25:24 +0200573 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Clemens Ladisch9624ea82005-08-15 08:25:24 +0200576 /* finish at the frame boundary at/after the period boundary */
577 if (period_elapsed &&
578 (i & (subs->packs_per_ms - 1)) == subs->packs_per_ms - 1)
579 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200581 if (subs->hwptr_done + offs > runtime->buffer_size) {
582 /* err, the transferred area goes over buffer boundary. */
583 unsigned int len = runtime->buffer_size - subs->hwptr_done;
584 memcpy(urb->transfer_buffer,
585 runtime->dma_area + subs->hwptr_done * stride,
586 len * stride);
587 memcpy(urb->transfer_buffer + len * stride,
588 runtime->dma_area,
589 (offs - len) * stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 } else {
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200591 memcpy(urb->transfer_buffer,
592 runtime->dma_area + subs->hwptr_done * stride,
593 offs * stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200595 subs->hwptr_done += offs;
596 if (subs->hwptr_done >= runtime->buffer_size)
597 subs->hwptr_done -= runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 spin_unlock_irqrestore(&subs->lock, flags);
599 urb->transfer_buffer_length = offs * stride;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100600 if (period_elapsed)
601 snd_pcm_period_elapsed(subs->pcm_substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
603}
604
605/*
606 * process after playback data complete
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200607 * - nothing to do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100609static int retire_playback_urb(struct snd_usb_substream *subs,
610 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct urb *urb)
612{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return 0;
614}
615
616
617/*
618 */
619static struct snd_urb_ops audio_urb_ops[2] = {
620 {
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100621 .prepare = prepare_startup_playback_urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 .retire = retire_playback_urb,
623 .prepare_sync = prepare_playback_sync_urb,
624 .retire_sync = retire_playback_sync_urb,
625 },
626 {
627 .prepare = prepare_capture_urb,
628 .retire = retire_capture_urb,
629 .prepare_sync = prepare_capture_sync_urb,
630 .retire_sync = retire_capture_sync_urb,
631 },
632};
633
634static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
635 {
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100636 .prepare = prepare_startup_playback_urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 .retire = retire_playback_urb,
638 .prepare_sync = prepare_playback_sync_urb_hs,
639 .retire_sync = retire_playback_sync_urb_hs,
640 },
641 {
642 .prepare = prepare_capture_urb,
643 .retire = retire_capture_urb,
644 .prepare_sync = prepare_capture_sync_urb_hs,
645 .retire_sync = retire_capture_sync_urb,
646 },
647};
648
649/*
650 * complete callback from data urb
651 */
652static void snd_complete_urb(struct urb *urb, struct pt_regs *regs)
653{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100654 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
655 struct snd_usb_substream *subs = ctx->subs;
656 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int err = 0;
658
659 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
660 ! subs->running || /* can be stopped during retire callback */
661 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
662 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
663 clear_bit(ctx->index, &subs->active_mask);
664 if (err < 0) {
665 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
666 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
667 }
668 }
669}
670
671
672/*
673 * complete callback from sync urb
674 */
675static void snd_complete_sync_urb(struct urb *urb, struct pt_regs *regs)
676{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100677 struct snd_urb_ctx *ctx = (struct snd_urb_ctx *)urb->context;
678 struct snd_usb_substream *subs = ctx->subs;
679 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int err = 0;
681
682 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
683 ! subs->running || /* can be stopped during retire callback */
684 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
685 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
686 clear_bit(ctx->index + 16, &subs->active_mask);
687 if (err < 0) {
688 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
689 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
690 }
691 }
692}
693
694
Clemens Ladisch6207e512005-08-15 08:35:25 +0200695/* get the physical page pointer at the given offset */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100696static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
Clemens Ladisch6207e512005-08-15 08:35:25 +0200697 unsigned long offset)
698{
699 void *pageptr = subs->runtime->dma_area + offset;
700 return vmalloc_to_page(pageptr);
701}
702
703/* allocate virtual buffer; may be called more than once */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100704static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, size_t size)
Clemens Ladisch6207e512005-08-15 08:35:25 +0200705{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100706 struct snd_pcm_runtime *runtime = subs->runtime;
Clemens Ladisch6207e512005-08-15 08:35:25 +0200707 if (runtime->dma_area) {
708 if (runtime->dma_bytes >= size)
709 return 0; /* already large enough */
Takashi Iwaib1d57762005-10-10 11:56:31 +0200710 vfree(runtime->dma_area);
Clemens Ladisch6207e512005-08-15 08:35:25 +0200711 }
Takashi Iwaib1d57762005-10-10 11:56:31 +0200712 runtime->dma_area = vmalloc(size);
Clemens Ladisch6207e512005-08-15 08:35:25 +0200713 if (! runtime->dma_area)
714 return -ENOMEM;
715 runtime->dma_bytes = size;
716 return 0;
717}
718
719/* free virtual buffer; may be called more than once */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100720static int snd_pcm_free_vmalloc_buffer(struct snd_pcm_substream *subs)
Clemens Ladisch6207e512005-08-15 08:35:25 +0200721{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100722 struct snd_pcm_runtime *runtime = subs->runtime;
Jesper Juhl9c4be3d2006-02-09 20:04:16 +0100723
724 vfree(runtime->dma_area);
725 runtime->dma_area = NULL;
Clemens Ladisch6207e512005-08-15 08:35:25 +0200726 return 0;
727}
728
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730/*
731 * unlink active urbs.
732 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100733static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 unsigned int i;
736 int async;
737
738 subs->running = 0;
739
740 if (!force && subs->stream->chip->shutdown) /* to be sure... */
741 return -EBADFD;
742
743 async = !can_sleep && async_unlink;
744
745 if (! async && in_interrupt())
746 return 0;
747
748 for (i = 0; i < subs->nurbs; i++) {
749 if (test_bit(i, &subs->active_mask)) {
750 if (! test_and_set_bit(i, &subs->unlink_mask)) {
751 struct urb *u = subs->dataurb[i].urb;
Alan Sternb375a042005-07-29 16:11:07 -0400752 if (async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 usb_unlink_urb(u);
Alan Sternb375a042005-07-29 16:11:07 -0400754 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 usb_kill_urb(u);
756 }
757 }
758 }
759 if (subs->syncpipe) {
760 for (i = 0; i < SYNC_URBS; i++) {
761 if (test_bit(i+16, &subs->active_mask)) {
762 if (! test_and_set_bit(i+16, &subs->unlink_mask)) {
763 struct urb *u = subs->syncurb[i].urb;
Alan Sternb375a042005-07-29 16:11:07 -0400764 if (async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 usb_unlink_urb(u);
Alan Sternb375a042005-07-29 16:11:07 -0400766 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 usb_kill_urb(u);
768 }
769 }
770 }
771 }
772 return 0;
773}
774
775
Clemens Ladisch32e19e82006-03-09 07:58:39 +0100776static const char *usb_error_string(int err)
777{
778 switch (err) {
779 case -ENODEV:
780 return "no device";
781 case -ENOENT:
782 return "endpoint not enabled";
783 case -EPIPE:
784 return "endpoint stalled";
785 case -ENOSPC:
786 return "not enough bandwidth";
787 case -ESHUTDOWN:
788 return "device disabled";
789 case -EHOSTUNREACH:
790 return "device suspended";
Clemens Ladisch3e964432006-03-14 08:06:12 +0100791#ifndef CONFIG_USB_EHCI_SPLIT_ISO
792 case -ENOSYS:
793 return "enable CONFIG_USB_EHCI_SPLIT_ISO to play through a hub";
794#endif
Clemens Ladisch32e19e82006-03-09 07:58:39 +0100795 case -EINVAL:
796 case -EAGAIN:
797 case -EFBIG:
798 case -EMSGSIZE:
799 return "internal error";
800 default:
801 return "unknown error";
802 }
803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/*
806 * set up and start data/sync urbs
807 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100808static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 unsigned int i;
811 int err;
812
813 if (subs->stream->chip->shutdown)
814 return -EBADFD;
815
816 for (i = 0; i < subs->nurbs; i++) {
817 snd_assert(subs->dataurb[i].urb, return -EINVAL);
818 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
819 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
820 goto __error;
821 }
822 }
823 if (subs->syncpipe) {
824 for (i = 0; i < SYNC_URBS; i++) {
825 snd_assert(subs->syncurb[i].urb, return -EINVAL);
826 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
827 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
828 goto __error;
829 }
830 }
831 }
832
833 subs->active_mask = 0;
834 subs->unlink_mask = 0;
835 subs->running = 1;
836 for (i = 0; i < subs->nurbs; i++) {
Clemens Ladisch32e19e82006-03-09 07:58:39 +0100837 err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
838 if (err < 0) {
839 snd_printk(KERN_ERR "cannot submit datapipe "
840 "for urb %d, error %d: %s\n",
841 i, err, usb_error_string(err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto __error;
843 }
844 set_bit(i, &subs->active_mask);
845 }
846 if (subs->syncpipe) {
847 for (i = 0; i < SYNC_URBS; i++) {
Clemens Ladisch32e19e82006-03-09 07:58:39 +0100848 err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
849 if (err < 0) {
850 snd_printk(KERN_ERR "cannot submit syncpipe "
851 "for urb %d, error %d: %s\n",
852 i, err, usb_error_string(err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 goto __error;
854 }
855 set_bit(i + 16, &subs->active_mask);
856 }
857 }
858 return 0;
859
860 __error:
861 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
862 deactivate_urbs(subs, 0, 0);
863 return -EPIPE;
864}
865
866
867/*
868 * wait until all urbs are processed.
869 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100870static int wait_clear_urbs(struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Nishanth Aravamudanb27c1872005-07-09 10:54:37 +0200872 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 unsigned int i;
874 int alive;
875
876 do {
877 alive = 0;
878 for (i = 0; i < subs->nurbs; i++) {
879 if (test_bit(i, &subs->active_mask))
880 alive++;
881 }
882 if (subs->syncpipe) {
883 for (i = 0; i < SYNC_URBS; i++) {
884 if (test_bit(i + 16, &subs->active_mask))
885 alive++;
886 }
887 }
888 if (! alive)
889 break;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200890 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanb27c1872005-07-09 10:54:37 +0200891 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (alive)
893 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
894 return 0;
895}
896
897
898/*
899 * return the current pcm pointer. just return the hwptr_done value.
900 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100901static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100903 struct snd_usb_substream *subs;
Clemens Ladischdaa150e2005-08-15 08:25:50 +0200904 snd_pcm_uframes_t hwptr_done;
905
Takashi Iwai86e07d32005-11-17 15:08:02 +0100906 subs = (struct snd_usb_substream *)substream->runtime->private_data;
Clemens Ladischdaa150e2005-08-15 08:25:50 +0200907 spin_lock(&subs->lock);
908 hwptr_done = subs->hwptr_done;
909 spin_unlock(&subs->lock);
910 return hwptr_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913
914/*
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100915 * start/stop playback substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100917static int snd_usb_pcm_playback_trigger(struct snd_pcm_substream *substream,
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100918 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100920 struct snd_usb_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 switch (cmd) {
923 case SNDRV_PCM_TRIGGER_START:
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100924 subs->ops.prepare = prepare_playback_urb;
925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 case SNDRV_PCM_TRIGGER_STOP:
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100927 return deactivate_urbs(subs, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 default:
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100929 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100931}
932
933/*
934 * start/stop capture substream
935 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100936static int snd_usb_pcm_capture_trigger(struct snd_pcm_substream *substream,
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100937 int cmd)
938{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100939 struct snd_usb_substream *subs = substream->runtime->private_data;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100940
941 switch (cmd) {
942 case SNDRV_PCM_TRIGGER_START:
943 return start_urbs(subs, substream->runtime);
944 case SNDRV_PCM_TRIGGER_STOP:
945 return deactivate_urbs(subs, 0, 0);
946 default:
947 return -EINVAL;
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951
952/*
953 * release a urb data
954 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100955static void release_urb_ctx(struct snd_urb_ctx *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 if (u->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200958 if (u->buffer_size)
959 usb_buffer_free(u->subs->dev, u->buffer_size,
960 u->urb->transfer_buffer,
961 u->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 usb_free_urb(u->urb);
963 u->urb = NULL;
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967/*
968 * release a substream
969 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100970static void release_substream_urbs(struct snd_usb_substream *subs, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 int i;
973
974 /* stop urbs (to be sure) */
975 deactivate_urbs(subs, force, 1);
976 wait_clear_urbs(subs);
977
978 for (i = 0; i < MAX_URBS; i++)
979 release_urb_ctx(&subs->dataurb[i]);
980 for (i = 0; i < SYNC_URBS; i++)
981 release_urb_ctx(&subs->syncurb[i]);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200982 usb_buffer_free(subs->dev, SYNC_URBS * 4,
983 subs->syncbuf, subs->sync_dma);
984 subs->syncbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 subs->nurbs = 0;
986}
987
988/*
989 * initialize a substream for plaback/capture
990 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100991static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 unsigned int rate, unsigned int frame_bits)
993{
994 unsigned int maxsize, n, i;
995 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
Clemens Ladischa93bf992005-08-12 15:19:39 +0200996 unsigned int npacks[MAX_URBS], urb_packs, total_packs, packs_per_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 /* calculate the frequency in 16.16 format */
999 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
1000 subs->freqn = get_usb_full_speed_rate(rate);
1001 else
1002 subs->freqn = get_usb_high_speed_rate(rate);
1003 subs->freqm = subs->freqn;
Clemens Ladisch573567e2005-06-27 08:17:30 +02001004 /* calculate max. frequency */
1005 if (subs->maxpacksize) {
1006 /* whatever fits into a max. size packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 maxsize = subs->maxpacksize;
Clemens Ladisch573567e2005-06-27 08:17:30 +02001008 subs->freqmax = (maxsize / (frame_bits >> 3))
1009 << (16 - subs->datainterval);
1010 } else {
1011 /* no max. packet size: just take 25% higher than nominal */
1012 subs->freqmax = subs->freqn + (subs->freqn >> 2);
1013 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
1014 >> (16 - subs->datainterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
Clemens Ladisch573567e2005-06-27 08:17:30 +02001016 subs->phase = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 if (subs->fill_max)
1019 subs->curpacksize = subs->maxpacksize;
1020 else
1021 subs->curpacksize = maxsize;
1022
Clemens Ladischa93bf992005-08-12 15:19:39 +02001023 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
1024 packs_per_ms = 8 >> subs->datainterval;
1025 else
1026 packs_per_ms = 1;
Clemens Ladisch9624ea82005-08-15 08:25:24 +02001027 subs->packs_per_ms = packs_per_ms;
Clemens Ladischa93bf992005-08-12 15:19:39 +02001028
Clemens Ladisch71d848c2005-08-12 15:18:00 +02001029 if (is_playback) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 urb_packs = nrpacks;
Clemens Ladisch71d848c2005-08-12 15:18:00 +02001031 urb_packs = max(urb_packs, (unsigned int)MIN_PACKS_URB);
1032 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
1033 } else
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001034 urb_packs = 1;
Clemens Ladischa93bf992005-08-12 15:19:39 +02001035 urb_packs *= packs_per_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /* decide how many packets to be used */
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001038 if (is_playback) {
Clemens Ladischd6db3922005-08-12 08:28:27 +02001039 unsigned int minsize;
1040 /* determine how small a packet can be */
1041 minsize = (subs->freqn >> (16 - subs->datainterval))
1042 * (frame_bits >> 3);
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +02001043 /* with sync from device, assume it can be 12% lower */
Clemens Ladischd6db3922005-08-12 08:28:27 +02001044 if (subs->syncpipe)
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +02001045 minsize -= minsize >> 3;
Clemens Ladischd6db3922005-08-12 08:28:27 +02001046 minsize = max(minsize, 1u);
1047 total_packs = (period_bytes + minsize - 1) / minsize;
Clemens Ladischa93bf992005-08-12 15:19:39 +02001048 /* round up to multiple of packs_per_ms */
1049 total_packs = (total_packs + packs_per_ms - 1)
1050 & ~(packs_per_ms - 1);
1051 /* we need at least two URBs for queueing */
1052 if (total_packs < 2 * MIN_PACKS_URB * packs_per_ms)
1053 total_packs = 2 * MIN_PACKS_URB * packs_per_ms;
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001054 } else {
1055 total_packs = MAX_URBS * urb_packs;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
1058 if (subs->nurbs > MAX_URBS) {
1059 /* too much... */
1060 subs->nurbs = MAX_URBS;
1061 total_packs = MAX_URBS * urb_packs;
1062 }
1063 n = total_packs;
1064 for (i = 0; i < subs->nurbs; i++) {
1065 npacks[i] = n > urb_packs ? urb_packs : n;
1066 n -= urb_packs;
1067 }
1068 if (subs->nurbs <= 1) {
1069 /* too little - we need at least two packets
1070 * to ensure contiguous playback/capture
1071 */
1072 subs->nurbs = 2;
1073 npacks[0] = (total_packs + 1) / 2;
1074 npacks[1] = total_packs - npacks[0];
Clemens Ladischa93bf992005-08-12 15:19:39 +02001075 } else if (npacks[subs->nurbs-1] < MIN_PACKS_URB * packs_per_ms) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /* the last packet is too small.. */
1077 if (subs->nurbs > 2) {
1078 /* merge to the first one */
1079 npacks[0] += npacks[subs->nurbs - 1];
1080 subs->nurbs--;
1081 } else {
1082 /* divide to two */
1083 subs->nurbs = 2;
1084 npacks[0] = (total_packs + 1) / 2;
1085 npacks[1] = total_packs - npacks[0];
1086 }
1087 }
1088
1089 /* allocate and initialize data urbs */
1090 for (i = 0; i < subs->nurbs; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001091 struct snd_urb_ctx *u = &subs->dataurb[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 u->index = i;
1093 u->subs = subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 u->packets = npacks[i];
Clemens Ladisch55851f72005-08-15 08:34:16 +02001095 u->buffer_size = maxsize * u->packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (subs->fmt_type == USB_FORMAT_TYPE_II)
1097 u->packets++; /* for transfer delimiter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
Clemens Ladisch55851f72005-08-15 08:34:16 +02001099 if (! u->urb)
1100 goto out_of_memory;
1101 u->urb->transfer_buffer =
1102 usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL,
1103 &u->urb->transfer_dma);
1104 if (! u->urb->transfer_buffer)
1105 goto out_of_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 u->urb->pipe = subs->datapipe;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001107 u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Clemens Ladisch573567e2005-06-27 08:17:30 +02001108 u->urb->interval = 1 << subs->datainterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 u->urb->context = u;
Clemens Ladisch3527a002005-09-26 10:03:09 +02001110 u->urb->complete = snd_complete_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112
1113 if (subs->syncpipe) {
1114 /* allocate and initialize sync urbs */
Clemens Ladisch55851f72005-08-15 08:34:16 +02001115 subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4,
1116 GFP_KERNEL, &subs->sync_dma);
1117 if (! subs->syncbuf)
1118 goto out_of_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 for (i = 0; i < SYNC_URBS; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001120 struct snd_urb_ctx *u = &subs->syncurb[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 u->index = i;
1122 u->subs = subs;
Clemens Ladischca810902005-05-02 08:55:54 +02001123 u->packets = 1;
1124 u->urb = usb_alloc_urb(1, GFP_KERNEL);
Clemens Ladisch55851f72005-08-15 08:34:16 +02001125 if (! u->urb)
1126 goto out_of_memory;
Clemens Ladischca810902005-05-02 08:55:54 +02001127 u->urb->transfer_buffer = subs->syncbuf + i * 4;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001128 u->urb->transfer_dma = subs->sync_dma + i * 4;
Clemens Ladischca810902005-05-02 08:55:54 +02001129 u->urb->transfer_buffer_length = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 u->urb->pipe = subs->syncpipe;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001131 u->urb->transfer_flags = URB_ISO_ASAP |
1132 URB_NO_TRANSFER_DMA_MAP;
Clemens Ladischca810902005-05-02 08:55:54 +02001133 u->urb->number_of_packets = 1;
Clemens Ladisch1149a642005-05-02 08:53:46 +02001134 u->urb->interval = 1 << subs->syncinterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 u->urb->context = u;
Clemens Ladisch3527a002005-09-26 10:03:09 +02001136 u->urb->complete = snd_complete_sync_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 }
1139 return 0;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001140
1141out_of_memory:
1142 release_substream_urbs(subs, 0);
1143 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146
1147/*
1148 * find a matching audio format
1149 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001150static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 unsigned int rate, unsigned int channels)
1152{
1153 struct list_head *p;
1154 struct audioformat *found = NULL;
1155 int cur_attr = 0, attr;
1156
1157 list_for_each(p, &subs->fmt_list) {
1158 struct audioformat *fp;
1159 fp = list_entry(p, struct audioformat, list);
1160 if (fp->format != format || fp->channels != channels)
1161 continue;
1162 if (rate < fp->rate_min || rate > fp->rate_max)
1163 continue;
1164 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
1165 unsigned int i;
1166 for (i = 0; i < fp->nr_rates; i++)
1167 if (fp->rate_table[i] == rate)
1168 break;
1169 if (i >= fp->nr_rates)
1170 continue;
1171 }
1172 attr = fp->ep_attr & EP_ATTR_MASK;
1173 if (! found) {
1174 found = fp;
1175 cur_attr = attr;
1176 continue;
1177 }
1178 /* avoid async out and adaptive in if the other method
1179 * supports the same format.
1180 * this is a workaround for the case like
1181 * M-audio audiophile USB.
1182 */
1183 if (attr != cur_attr) {
1184 if ((attr == EP_ATTR_ASYNC &&
1185 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1186 (attr == EP_ATTR_ADAPTIVE &&
1187 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
1188 continue;
1189 if ((cur_attr == EP_ATTR_ASYNC &&
1190 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1191 (cur_attr == EP_ATTR_ADAPTIVE &&
1192 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
1193 found = fp;
1194 cur_attr = attr;
1195 continue;
1196 }
1197 }
1198 /* find the format with the largest max. packet size */
1199 if (fp->maxpacksize > found->maxpacksize) {
1200 found = fp;
1201 cur_attr = attr;
1202 }
1203 }
1204 return found;
1205}
1206
1207
1208/*
1209 * initialize the picth control and sample rate
1210 */
1211static int init_usb_pitch(struct usb_device *dev, int iface,
1212 struct usb_host_interface *alts,
1213 struct audioformat *fmt)
1214{
1215 unsigned int ep;
1216 unsigned char data[1];
1217 int err;
1218
1219 ep = get_endpoint(alts, 0)->bEndpointAddress;
1220 /* if endpoint has pitch control, enable it */
1221 if (fmt->attributes & EP_CS_ATTR_PITCH_CONTROL) {
1222 data[0] = 1;
1223 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1224 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1225 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1226 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
1227 dev->devnum, iface, ep);
1228 return err;
1229 }
1230 }
1231 return 0;
1232}
1233
1234static int init_usb_sample_rate(struct usb_device *dev, int iface,
1235 struct usb_host_interface *alts,
1236 struct audioformat *fmt, int rate)
1237{
1238 unsigned int ep;
1239 unsigned char data[3];
1240 int err;
1241
1242 ep = get_endpoint(alts, 0)->bEndpointAddress;
1243 /* if endpoint has sampling rate control, set it */
1244 if (fmt->attributes & EP_CS_ATTR_SAMPLE_RATE) {
1245 int crate;
1246 data[0] = rate;
1247 data[1] = rate >> 8;
1248 data[2] = rate >> 16;
1249 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1250 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1251 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1252 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep 0x%x\n",
1253 dev->devnum, iface, fmt->altsetting, rate, ep);
1254 return err;
1255 }
1256 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR,
1257 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1258 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1259 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep 0x%x\n",
1260 dev->devnum, iface, fmt->altsetting, ep);
1261 return 0; /* some devices don't support reading */
1262 }
1263 crate = data[0] | (data[1] << 8) | (data[2] << 16);
1264 if (crate != rate) {
1265 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
1266 // runtime->rate = crate;
1267 }
1268 }
1269 return 0;
1270}
1271
1272/*
1273 * find a matching format and set up the interface
1274 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001275static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct usb_device *dev = subs->dev;
1278 struct usb_host_interface *alts;
1279 struct usb_interface_descriptor *altsd;
1280 struct usb_interface *iface;
1281 unsigned int ep, attr;
1282 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1283 int err;
1284
1285 iface = usb_ifnum_to_if(dev, fmt->iface);
1286 snd_assert(iface, return -EINVAL);
1287 alts = &iface->altsetting[fmt->altset_idx];
1288 altsd = get_iface_desc(alts);
1289 snd_assert(altsd->bAlternateSetting == fmt->altsetting, return -EINVAL);
1290
1291 if (fmt == subs->cur_audiofmt)
1292 return 0;
1293
1294 /* close the old interface */
1295 if (subs->interface >= 0 && subs->interface != fmt->iface) {
1296 usb_set_interface(subs->dev, subs->interface, 0);
1297 subs->interface = -1;
1298 subs->format = 0;
1299 }
1300
1301 /* set interface */
1302 if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
1303 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
1304 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
1305 dev->devnum, fmt->iface, fmt->altsetting);
1306 return -EIO;
1307 }
1308 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
1309 subs->interface = fmt->iface;
1310 subs->format = fmt->altset_idx;
1311 }
1312
1313 /* create a data pipe */
1314 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
1315 if (is_playback)
1316 subs->datapipe = usb_sndisocpipe(dev, ep);
1317 else
1318 subs->datapipe = usb_rcvisocpipe(dev, ep);
Clemens Ladisch573567e2005-06-27 08:17:30 +02001319 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH &&
1320 get_endpoint(alts, 0)->bInterval >= 1 &&
1321 get_endpoint(alts, 0)->bInterval <= 4)
1322 subs->datainterval = get_endpoint(alts, 0)->bInterval - 1;
1323 else
1324 subs->datainterval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 subs->syncpipe = subs->syncinterval = 0;
1326 subs->maxpacksize = fmt->maxpacksize;
1327 subs->fill_max = 0;
1328
1329 /* we need a sync pipe in async OUT or adaptive IN mode */
1330 /* check the number of EP, since some devices have broken
1331 * descriptors which fool us. if it has only one EP,
1332 * assume it as adaptive-out or sync-in.
1333 */
1334 attr = fmt->ep_attr & EP_ATTR_MASK;
1335 if (((is_playback && attr == EP_ATTR_ASYNC) ||
1336 (! is_playback && attr == EP_ATTR_ADAPTIVE)) &&
1337 altsd->bNumEndpoints >= 2) {
1338 /* check sync-pipe endpoint */
1339 /* ... and check descriptor size before accessing bSynchAddress
1340 because there is a version of the SB Audigy 2 NX firmware lacking
1341 the audio fields in the endpoint descriptors */
1342 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
1343 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1344 get_endpoint(alts, 1)->bSynchAddress != 0)) {
1345 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1346 dev->devnum, fmt->iface, fmt->altsetting);
1347 return -EINVAL;
1348 }
1349 ep = get_endpoint(alts, 1)->bEndpointAddress;
1350 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1351 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
1352 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
1353 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1354 dev->devnum, fmt->iface, fmt->altsetting);
1355 return -EINVAL;
1356 }
1357 ep &= USB_ENDPOINT_NUMBER_MASK;
1358 if (is_playback)
1359 subs->syncpipe = usb_rcvisocpipe(dev, ep);
1360 else
1361 subs->syncpipe = usb_sndisocpipe(dev, ep);
Clemens Ladisch1149a642005-05-02 08:53:46 +02001362 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1363 get_endpoint(alts, 1)->bRefresh >= 1 &&
1364 get_endpoint(alts, 1)->bRefresh <= 9)
1365 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
Clemens Ladisch604cf492005-05-17 09:15:27 +02001366 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
Clemens Ladisch1149a642005-05-02 08:53:46 +02001367 subs->syncinterval = 1;
Clemens Ladisch604cf492005-05-17 09:15:27 +02001368 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
1369 get_endpoint(alts, 1)->bInterval <= 16)
1370 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
1371 else
1372 subs->syncinterval = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374
1375 /* always fill max packet size */
1376 if (fmt->attributes & EP_CS_ATTR_FILL_MAX)
1377 subs->fill_max = 1;
1378
1379 if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
1380 return err;
1381
1382 subs->cur_audiofmt = fmt;
1383
1384#if 0
1385 printk("setting done: format = %d, rate = %d, channels = %d\n",
1386 fmt->format, fmt->rate, fmt->channels);
1387 printk(" datapipe = 0x%0x, syncpipe = 0x%0x\n",
1388 subs->datapipe, subs->syncpipe);
1389#endif
1390
1391 return 0;
1392}
1393
1394/*
1395 * hw_params callback
1396 *
1397 * allocate a buffer and set the given audio format.
1398 *
1399 * so far we use a physically linear buffer although packetize transfer
1400 * doesn't need a continuous area.
1401 * if sg buffer is supported on the later version of alsa, we'll follow
1402 * that.
1403 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001404static int snd_usb_hw_params(struct snd_pcm_substream *substream,
1405 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001407 struct snd_usb_substream *subs = (struct snd_usb_substream *)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 struct audioformat *fmt;
1409 unsigned int channels, rate, format;
1410 int ret, changed;
1411
Clemens Ladisch6207e512005-08-15 08:35:25 +02001412 ret = snd_pcm_alloc_vmalloc_buffer(substream,
1413 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (ret < 0)
1415 return ret;
1416
1417 format = params_format(hw_params);
1418 rate = params_rate(hw_params);
1419 channels = params_channels(hw_params);
1420 fmt = find_format(subs, format, rate, channels);
1421 if (! fmt) {
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001422 snd_printd(KERN_DEBUG "cannot set format: format = 0x%x, rate = %d, channels = %d\n",
1423 format, rate, channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return -EINVAL;
1425 }
1426
1427 changed = subs->cur_audiofmt != fmt ||
1428 subs->period_bytes != params_period_bytes(hw_params) ||
1429 subs->cur_rate != rate;
1430 if ((ret = set_format(subs, fmt)) < 0)
1431 return ret;
1432
1433 if (subs->cur_rate != rate) {
1434 struct usb_host_interface *alts;
1435 struct usb_interface *iface;
1436 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
1437 alts = &iface->altsetting[fmt->altset_idx];
1438 ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
1439 if (ret < 0)
1440 return ret;
1441 subs->cur_rate = rate;
1442 }
1443
1444 if (changed) {
1445 /* format changed */
1446 release_substream_urbs(subs, 0);
1447 /* influenced: period_bytes, channels, rate, format, */
1448 ret = init_substream_urbs(subs, params_period_bytes(hw_params),
1449 params_rate(hw_params),
1450 snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
1451 }
1452
1453 return ret;
1454}
1455
1456/*
1457 * hw_free callback
1458 *
1459 * reset the audio format and release the buffer
1460 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001461static int snd_usb_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001463 struct snd_usb_substream *subs = (struct snd_usb_substream *)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 subs->cur_audiofmt = NULL;
1466 subs->cur_rate = 0;
1467 subs->period_bytes = 0;
1468 release_substream_urbs(subs, 0);
Clemens Ladisch6207e512005-08-15 08:35:25 +02001469 return snd_pcm_free_vmalloc_buffer(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
1472/*
1473 * prepare callback
1474 *
1475 * only a few subtle things...
1476 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001477static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001479 struct snd_pcm_runtime *runtime = substream->runtime;
1480 struct snd_usb_substream *subs = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 if (! subs->cur_audiofmt) {
1483 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
1484 return -ENXIO;
1485 }
1486
1487 /* some unit conversions in runtime */
1488 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
1489 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
1490
1491 /* reset the pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 subs->hwptr_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 subs->transfer_done = 0;
1494 subs->phase = 0;
1495
1496 /* clear urbs (to be sure) */
1497 deactivate_urbs(subs, 0, 1);
1498 wait_clear_urbs(subs);
1499
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001500 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1501 * updates for all URBs would happen at the same time when starting */
1502 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1503 subs->ops.prepare = prepare_startup_playback_urb;
1504 return start_urbs(subs, runtime);
1505 } else
1506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Takashi Iwai86e07d32005-11-17 15:08:02 +01001509static struct snd_pcm_hardware snd_usb_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Clemens Ladisch49045d32005-09-05 10:31:05 +02001511 .info = SNDRV_PCM_INFO_MMAP |
1512 SNDRV_PCM_INFO_MMAP_VALID |
1513 SNDRV_PCM_INFO_BATCH |
1514 SNDRV_PCM_INFO_INTERLEAVED |
1515 SNDRV_PCM_INFO_BLOCK_TRANSFER,
Clemens Ladischd31cbbf2005-09-26 09:59:57 +02001516 .buffer_bytes_max = 1024 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 .period_bytes_min = 64,
Clemens Ladischd31cbbf2005-09-26 09:59:57 +02001518 .period_bytes_max = 512 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 .periods_min = 2,
1520 .periods_max = 1024,
1521};
1522
Takashi Iwai86e07d32005-11-17 15:08:02 +01001523static struct snd_pcm_hardware snd_usb_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Clemens Ladisch49045d32005-09-05 10:31:05 +02001525 .info = SNDRV_PCM_INFO_MMAP |
1526 SNDRV_PCM_INFO_MMAP_VALID |
1527 SNDRV_PCM_INFO_BATCH |
1528 SNDRV_PCM_INFO_INTERLEAVED |
1529 SNDRV_PCM_INFO_BLOCK_TRANSFER,
Clemens Ladischd31cbbf2005-09-26 09:59:57 +02001530 .buffer_bytes_max = 1024 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 .period_bytes_min = 64,
Clemens Ladischd31cbbf2005-09-26 09:59:57 +02001532 .period_bytes_max = 512 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 .periods_min = 2,
1534 .periods_max = 1024,
1535};
1536
1537/*
1538 * h/w constraints
1539 */
1540
1541#ifdef HW_CONST_DEBUG
1542#define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
1543#else
1544#define hwc_debug(fmt, args...) /**/
1545#endif
1546
Takashi Iwai86e07d32005-11-17 15:08:02 +01001547static int hw_check_valid_format(struct snd_pcm_hw_params *params, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001549 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1550 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1551 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 /* check the format */
1554 if (! snd_mask_test(fmts, fp->format)) {
1555 hwc_debug(" > check: no supported format %d\n", fp->format);
1556 return 0;
1557 }
1558 /* check the channels */
1559 if (fp->channels < ct->min || fp->channels > ct->max) {
1560 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1561 return 0;
1562 }
1563 /* check the rate is within the range */
1564 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1565 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1566 return 0;
1567 }
1568 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1569 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1570 return 0;
1571 }
1572 return 1;
1573}
1574
Takashi Iwai86e07d32005-11-17 15:08:02 +01001575static int hw_rule_rate(struct snd_pcm_hw_params *params,
1576 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001578 struct snd_usb_substream *subs = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001580 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 unsigned int rmin, rmax;
1582 int changed;
1583
1584 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1585 changed = 0;
1586 rmin = rmax = 0;
1587 list_for_each(p, &subs->fmt_list) {
1588 struct audioformat *fp;
1589 fp = list_entry(p, struct audioformat, list);
1590 if (! hw_check_valid_format(params, fp))
1591 continue;
1592 if (changed++) {
1593 if (rmin > fp->rate_min)
1594 rmin = fp->rate_min;
1595 if (rmax < fp->rate_max)
1596 rmax = fp->rate_max;
1597 } else {
1598 rmin = fp->rate_min;
1599 rmax = fp->rate_max;
1600 }
1601 }
1602
1603 if (! changed) {
1604 hwc_debug(" --> get empty\n");
1605 it->empty = 1;
1606 return -EINVAL;
1607 }
1608
1609 changed = 0;
1610 if (it->min < rmin) {
1611 it->min = rmin;
1612 it->openmin = 0;
1613 changed = 1;
1614 }
1615 if (it->max > rmax) {
1616 it->max = rmax;
1617 it->openmax = 0;
1618 changed = 1;
1619 }
1620 if (snd_interval_checkempty(it)) {
1621 it->empty = 1;
1622 return -EINVAL;
1623 }
1624 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1625 return changed;
1626}
1627
1628
Takashi Iwai86e07d32005-11-17 15:08:02 +01001629static int hw_rule_channels(struct snd_pcm_hw_params *params,
1630 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001632 struct snd_usb_substream *subs = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001634 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 unsigned int rmin, rmax;
1636 int changed;
1637
1638 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1639 changed = 0;
1640 rmin = rmax = 0;
1641 list_for_each(p, &subs->fmt_list) {
1642 struct audioformat *fp;
1643 fp = list_entry(p, struct audioformat, list);
1644 if (! hw_check_valid_format(params, fp))
1645 continue;
1646 if (changed++) {
1647 if (rmin > fp->channels)
1648 rmin = fp->channels;
1649 if (rmax < fp->channels)
1650 rmax = fp->channels;
1651 } else {
1652 rmin = fp->channels;
1653 rmax = fp->channels;
1654 }
1655 }
1656
1657 if (! changed) {
1658 hwc_debug(" --> get empty\n");
1659 it->empty = 1;
1660 return -EINVAL;
1661 }
1662
1663 changed = 0;
1664 if (it->min < rmin) {
1665 it->min = rmin;
1666 it->openmin = 0;
1667 changed = 1;
1668 }
1669 if (it->max > rmax) {
1670 it->max = rmax;
1671 it->openmax = 0;
1672 changed = 1;
1673 }
1674 if (snd_interval_checkempty(it)) {
1675 it->empty = 1;
1676 return -EINVAL;
1677 }
1678 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1679 return changed;
1680}
1681
Takashi Iwai86e07d32005-11-17 15:08:02 +01001682static int hw_rule_format(struct snd_pcm_hw_params *params,
1683 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001685 struct snd_usb_substream *subs = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001687 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 u64 fbits;
1689 u32 oldbits[2];
1690 int changed;
1691
1692 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1693 fbits = 0;
1694 list_for_each(p, &subs->fmt_list) {
1695 struct audioformat *fp;
1696 fp = list_entry(p, struct audioformat, list);
1697 if (! hw_check_valid_format(params, fp))
1698 continue;
1699 fbits |= (1ULL << fp->format);
1700 }
1701
1702 oldbits[0] = fmt->bits[0];
1703 oldbits[1] = fmt->bits[1];
1704 fmt->bits[0] &= (u32)fbits;
1705 fmt->bits[1] &= (u32)(fbits >> 32);
1706 if (! fmt->bits[0] && ! fmt->bits[1]) {
1707 hwc_debug(" --> get empty\n");
1708 return -EINVAL;
1709 }
1710 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1711 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1712 return changed;
1713}
1714
1715#define MAX_MASK 64
1716
1717/*
1718 * check whether the registered audio formats need special hw-constraints
1719 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001720static int check_hw_params_convention(struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 int i;
1723 u32 *channels;
1724 u32 *rates;
1725 u32 cmaster, rmaster;
1726 u32 rate_min = 0, rate_max = 0;
1727 struct list_head *p;
1728 int err = 1;
1729
1730 channels = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1731 rates = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1732
1733 list_for_each(p, &subs->fmt_list) {
1734 struct audioformat *f;
1735 f = list_entry(p, struct audioformat, list);
1736 /* unconventional channels? */
1737 if (f->channels > 32)
1738 goto __out;
1739 /* continuous rate min/max matches? */
1740 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1741 if (rate_min && f->rate_min != rate_min)
1742 goto __out;
1743 if (rate_max && f->rate_max != rate_max)
1744 goto __out;
1745 rate_min = f->rate_min;
1746 rate_max = f->rate_max;
1747 }
1748 /* combination of continuous rates and fixed rates? */
1749 if (rates[f->format] & SNDRV_PCM_RATE_CONTINUOUS) {
1750 if (f->rates != rates[f->format])
1751 goto __out;
1752 }
1753 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1754 if (rates[f->format] && rates[f->format] != f->rates)
1755 goto __out;
1756 }
1757 channels[f->format] |= (1 << f->channels);
1758 rates[f->format] |= f->rates;
1759 }
1760 /* check whether channels and rates match for all formats */
1761 cmaster = rmaster = 0;
1762 for (i = 0; i < MAX_MASK; i++) {
1763 if (cmaster != channels[i] && cmaster && channels[i])
1764 goto __out;
1765 if (rmaster != rates[i] && rmaster && rates[i])
1766 goto __out;
1767 if (channels[i])
1768 cmaster = channels[i];
1769 if (rates[i])
1770 rmaster = rates[i];
1771 }
1772 /* check whether channels match for all distinct rates */
1773 memset(channels, 0, MAX_MASK * sizeof(u32));
1774 list_for_each(p, &subs->fmt_list) {
1775 struct audioformat *f;
1776 f = list_entry(p, struct audioformat, list);
1777 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS)
1778 continue;
1779 for (i = 0; i < 32; i++) {
1780 if (f->rates & (1 << i))
1781 channels[i] |= (1 << f->channels);
1782 }
1783 }
1784 cmaster = 0;
1785 for (i = 0; i < 32; i++) {
1786 if (cmaster != channels[i] && cmaster && channels[i])
1787 goto __out;
1788 if (channels[i])
1789 cmaster = channels[i];
1790 }
1791 err = 0;
1792
1793 __out:
1794 kfree(channels);
1795 kfree(rates);
1796 return err;
1797}
1798
1799
1800/*
1801 * set up the runtime hardware information.
1802 */
1803
Takashi Iwai86e07d32005-11-17 15:08:02 +01001804static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 struct list_head *p;
1807 int err;
1808
1809 runtime->hw.formats = subs->formats;
1810
1811 runtime->hw.rate_min = 0x7fffffff;
1812 runtime->hw.rate_max = 0;
1813 runtime->hw.channels_min = 256;
1814 runtime->hw.channels_max = 0;
1815 runtime->hw.rates = 0;
1816 /* check min/max rates and channels */
1817 list_for_each(p, &subs->fmt_list) {
1818 struct audioformat *fp;
1819 fp = list_entry(p, struct audioformat, list);
1820 runtime->hw.rates |= fp->rates;
1821 if (runtime->hw.rate_min > fp->rate_min)
1822 runtime->hw.rate_min = fp->rate_min;
1823 if (runtime->hw.rate_max < fp->rate_max)
1824 runtime->hw.rate_max = fp->rate_max;
1825 if (runtime->hw.channels_min > fp->channels)
1826 runtime->hw.channels_min = fp->channels;
1827 if (runtime->hw.channels_max < fp->channels)
1828 runtime->hw.channels_max = fp->channels;
1829 if (fp->fmt_type == USB_FORMAT_TYPE_II && fp->frame_size > 0) {
1830 /* FIXME: there might be more than one audio formats... */
1831 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1832 fp->frame_size;
1833 }
1834 }
1835
1836 /* set the period time minimum 1ms */
1837 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1838 1000 * MIN_PACKS_URB,
1839 /*(nrpacks * MAX_URBS) * 1000*/ UINT_MAX);
1840
1841 if (check_hw_params_convention(subs)) {
1842 hwc_debug("setting extra hw constraints...\n");
1843 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1844 hw_rule_rate, subs,
1845 SNDRV_PCM_HW_PARAM_FORMAT,
1846 SNDRV_PCM_HW_PARAM_CHANNELS,
1847 -1)) < 0)
1848 return err;
1849 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1850 hw_rule_channels, subs,
1851 SNDRV_PCM_HW_PARAM_FORMAT,
1852 SNDRV_PCM_HW_PARAM_RATE,
1853 -1)) < 0)
1854 return err;
1855 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1856 hw_rule_format, subs,
1857 SNDRV_PCM_HW_PARAM_RATE,
1858 SNDRV_PCM_HW_PARAM_CHANNELS,
1859 -1)) < 0)
1860 return err;
1861 }
1862 return 0;
1863}
1864
Takashi Iwai86e07d32005-11-17 15:08:02 +01001865static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction,
1866 struct snd_pcm_hardware *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001868 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1869 struct snd_pcm_runtime *runtime = substream->runtime;
1870 struct snd_usb_substream *subs = &as->substream[direction];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 subs->interface = -1;
1873 subs->format = 0;
1874 runtime->hw = *hw;
1875 runtime->private_data = subs;
1876 subs->pcm_substream = substream;
1877 return setup_hw_info(runtime, subs);
1878}
1879
Takashi Iwai86e07d32005-11-17 15:08:02 +01001880static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001882 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1883 struct snd_usb_substream *subs = &as->substream[direction];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 if (subs->interface >= 0) {
1886 usb_set_interface(subs->dev, subs->interface, 0);
1887 subs->interface = -1;
1888 }
1889 subs->pcm_substream = NULL;
1890 return 0;
1891}
1892
Takashi Iwai86e07d32005-11-17 15:08:02 +01001893static int snd_usb_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK, &snd_usb_playback);
1896}
1897
Takashi Iwai86e07d32005-11-17 15:08:02 +01001898static int snd_usb_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
1900 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1901}
1902
Takashi Iwai86e07d32005-11-17 15:08:02 +01001903static int snd_usb_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE, &snd_usb_capture);
1906}
1907
Takashi Iwai86e07d32005-11-17 15:08:02 +01001908static int snd_usb_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1911}
1912
Takashi Iwai86e07d32005-11-17 15:08:02 +01001913static struct snd_pcm_ops snd_usb_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 .open = snd_usb_playback_open,
1915 .close = snd_usb_playback_close,
1916 .ioctl = snd_pcm_lib_ioctl,
1917 .hw_params = snd_usb_hw_params,
1918 .hw_free = snd_usb_hw_free,
1919 .prepare = snd_usb_pcm_prepare,
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001920 .trigger = snd_usb_pcm_playback_trigger,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 .pointer = snd_usb_pcm_pointer,
Clemens Ladisch6207e512005-08-15 08:35:25 +02001922 .page = snd_pcm_get_vmalloc_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923};
1924
Takashi Iwai86e07d32005-11-17 15:08:02 +01001925static struct snd_pcm_ops snd_usb_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 .open = snd_usb_capture_open,
1927 .close = snd_usb_capture_close,
1928 .ioctl = snd_pcm_lib_ioctl,
1929 .hw_params = snd_usb_hw_params,
1930 .hw_free = snd_usb_hw_free,
1931 .prepare = snd_usb_pcm_prepare,
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001932 .trigger = snd_usb_pcm_capture_trigger,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 .pointer = snd_usb_pcm_pointer,
Clemens Ladisch6207e512005-08-15 08:35:25 +02001934 .page = snd_pcm_get_vmalloc_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935};
1936
1937
1938
1939/*
1940 * helper functions
1941 */
1942
1943/*
1944 * combine bytes and get an integer value
1945 */
1946unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
1947{
1948 switch (size) {
1949 case 1: return *bytes;
1950 case 2: return combine_word(bytes);
1951 case 3: return combine_triple(bytes);
1952 case 4: return combine_quad(bytes);
1953 default: return 0;
1954 }
1955}
1956
1957/*
1958 * parse descriptor buffer and return the pointer starting the given
1959 * descriptor type.
1960 */
1961void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
1962{
1963 u8 *p, *end, *next;
1964
1965 p = descstart;
1966 end = p + desclen;
1967 for (; p < end;) {
1968 if (p[0] < 2)
1969 return NULL;
1970 next = p + p[0];
1971 if (next > end)
1972 return NULL;
1973 if (p[1] == dtype && (!after || (void *)p > after)) {
1974 return p;
1975 }
1976 p = next;
1977 }
1978 return NULL;
1979}
1980
1981/*
1982 * find a class-specified interface descriptor with the given subtype.
1983 */
1984void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
1985{
1986 unsigned char *p = after;
1987
1988 while ((p = snd_usb_find_desc(buffer, buflen, p,
1989 USB_DT_CS_INTERFACE)) != NULL) {
1990 if (p[0] >= 3 && p[2] == dsubtype)
1991 return p;
1992 }
1993 return NULL;
1994}
1995
1996/*
1997 * Wrapper for usb_control_msg().
1998 * Allocates a temp buffer to prevent dmaing from/to the stack.
1999 */
2000int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
2001 __u8 requesttype, __u16 value, __u16 index, void *data,
2002 __u16 size, int timeout)
2003{
2004 int err;
2005 void *buf = NULL;
2006
2007 if (size > 0) {
2008 buf = kmalloc(size, GFP_KERNEL);
2009 if (!buf)
2010 return -ENOMEM;
2011 memcpy(buf, data, size);
2012 }
2013 err = usb_control_msg(dev, pipe, request, requesttype,
2014 value, index, buf, size, timeout);
2015 if (size > 0) {
2016 memcpy(data, buf, size);
2017 kfree(buf);
2018 }
2019 return err;
2020}
2021
2022
2023/*
2024 * entry point for linux usb interface
2025 */
2026
2027static int usb_audio_probe(struct usb_interface *intf,
2028 const struct usb_device_id *id);
2029static void usb_audio_disconnect(struct usb_interface *intf);
2030
2031static struct usb_device_id usb_audio_ids [] = {
2032#include "usbquirks.h"
2033 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
2034 .bInterfaceClass = USB_CLASS_AUDIO,
2035 .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL },
2036 { } /* Terminating entry */
2037};
2038
2039MODULE_DEVICE_TABLE (usb, usb_audio_ids);
2040
2041static struct usb_driver usb_audio_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 .name = "snd-usb-audio",
2043 .probe = usb_audio_probe,
2044 .disconnect = usb_audio_disconnect,
2045 .id_table = usb_audio_ids,
2046};
2047
2048
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002049#if defined(CONFIG_PROCFS) && defined(CONFIG_SND_VERBOSE_PROCFS)
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051/*
2052 * proc interface for list the supported pcm formats
2053 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002054static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 struct list_head *p;
2057 static char *sync_types[4] = {
2058 "NONE", "ASYNC", "ADAPTIVE", "SYNC"
2059 };
2060
2061 list_for_each(p, &subs->fmt_list) {
2062 struct audioformat *fp;
2063 fp = list_entry(p, struct audioformat, list);
2064 snd_iprintf(buffer, " Interface %d\n", fp->iface);
2065 snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
Jaroslav Kysela3f72a302006-01-18 11:50:40 +01002066 snd_iprintf(buffer, " Format: 0x%x\n", fp->format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 snd_iprintf(buffer, " Channels: %d\n", fp->channels);
2068 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
2069 fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
2070 fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
2071 sync_types[(fp->ep_attr & EP_ATTR_MASK) >> 2]);
2072 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
2073 snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
2074 fp->rate_min, fp->rate_max);
2075 } else {
2076 unsigned int i;
2077 snd_iprintf(buffer, " Rates: ");
2078 for (i = 0; i < fp->nr_rates; i++) {
2079 if (i > 0)
2080 snd_iprintf(buffer, ", ");
2081 snd_iprintf(buffer, "%d", fp->rate_table[i]);
2082 }
2083 snd_iprintf(buffer, "\n");
2084 }
2085 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
2086 // snd_iprintf(buffer, " EP Attribute = 0x%x\n", fp->attributes);
2087 }
2088}
2089
Takashi Iwai86e07d32005-11-17 15:08:02 +01002090static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 if (subs->running) {
2093 unsigned int i;
2094 snd_iprintf(buffer, " Status: Running\n");
2095 snd_iprintf(buffer, " Interface = %d\n", subs->interface);
2096 snd_iprintf(buffer, " Altset = %d\n", subs->format);
2097 snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
2098 for (i = 0; i < subs->nurbs; i++)
2099 snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
2100 snd_iprintf(buffer, "]\n");
2101 snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
Clemens Ladisch08fe1582005-04-22 15:33:01 +02002102 snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
2104 ? get_full_speed_hz(subs->freqm)
Clemens Ladisch08fe1582005-04-22 15:33:01 +02002105 : get_high_speed_hz(subs->freqm),
2106 subs->freqm >> 16, subs->freqm & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 } else {
2108 snd_iprintf(buffer, " Status: Stop\n");
2109 }
2110}
2111
Takashi Iwai86e07d32005-11-17 15:08:02 +01002112static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002114 struct snd_usb_stream *stream = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
2117
2118 if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
2119 snd_iprintf(buffer, "\nPlayback:\n");
2120 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2121 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2122 }
2123 if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
2124 snd_iprintf(buffer, "\nCapture:\n");
2125 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2126 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2127 }
2128}
2129
Takashi Iwai86e07d32005-11-17 15:08:02 +01002130static void proc_pcm_format_add(struct snd_usb_stream *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002132 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 char name[32];
Takashi Iwai86e07d32005-11-17 15:08:02 +01002134 struct snd_card *card = stream->chip->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 sprintf(name, "stream%d", stream->pcm_index);
2137 if (! snd_card_proc_new(card, name, &entry))
2138 snd_info_set_text_ops(entry, stream, 1024, proc_pcm_format_read);
2139}
2140
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002141#else
2142
2143static inline void proc_pcm_format_add(struct snd_usb_stream *stream)
2144{
2145}
2146
2147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149/*
2150 * initialize the substream instance.
2151 */
2152
Takashi Iwai86e07d32005-11-17 15:08:02 +01002153static void init_substream(struct snd_usb_stream *as, int stream, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002155 struct snd_usb_substream *subs = &as->substream[stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 INIT_LIST_HEAD(&subs->fmt_list);
2158 spin_lock_init(&subs->lock);
2159
2160 subs->stream = as;
2161 subs->direction = stream;
2162 subs->dev = as->chip->dev;
2163 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
2164 subs->ops = audio_urb_ops[stream];
2165 else
2166 subs->ops = audio_urb_ops_high_speed[stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 snd_pcm_set_ops(as->pcm, stream,
2168 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2169 &snd_usb_playback_ops : &snd_usb_capture_ops);
2170
2171 list_add_tail(&fp->list, &subs->fmt_list);
2172 subs->formats |= 1ULL << fp->format;
2173 subs->endpoint = fp->endpoint;
2174 subs->num_formats++;
2175 subs->fmt_type = fp->fmt_type;
2176}
2177
2178
2179/*
2180 * free a substream
2181 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002182static void free_substream(struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 struct list_head *p, *n;
2185
2186 if (! subs->num_formats)
2187 return; /* not initialized */
2188 list_for_each_safe(p, n, &subs->fmt_list) {
2189 struct audioformat *fp = list_entry(p, struct audioformat, list);
2190 kfree(fp->rate_table);
2191 kfree(fp);
2192 }
2193}
2194
2195
2196/*
2197 * free a usb stream instance
2198 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002199static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
2201 free_substream(&stream->substream[0]);
2202 free_substream(&stream->substream[1]);
2203 list_del(&stream->list);
2204 kfree(stream);
2205}
2206
Takashi Iwai86e07d32005-11-17 15:08:02 +01002207static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002209 struct snd_usb_stream *stream = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (stream) {
2211 stream->pcm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 snd_usb_audio_stream_free(stream);
2213 }
2214}
2215
2216
2217/*
2218 * add this endpoint to the chip instance.
2219 * if a stream with the same endpoint already exists, append to it.
2220 * if not, create a new pcm stream.
2221 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002222static int add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002225 struct snd_usb_stream *as;
2226 struct snd_usb_substream *subs;
2227 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 int err;
2229
2230 list_for_each(p, &chip->pcm_list) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002231 as = list_entry(p, struct snd_usb_stream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (as->fmt_type != fp->fmt_type)
2233 continue;
2234 subs = &as->substream[stream];
2235 if (! subs->endpoint)
2236 continue;
2237 if (subs->endpoint == fp->endpoint) {
2238 list_add_tail(&fp->list, &subs->fmt_list);
2239 subs->num_formats++;
2240 subs->formats |= 1ULL << fp->format;
2241 return 0;
2242 }
2243 }
2244 /* look for an empty stream */
2245 list_for_each(p, &chip->pcm_list) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002246 as = list_entry(p, struct snd_usb_stream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (as->fmt_type != fp->fmt_type)
2248 continue;
2249 subs = &as->substream[stream];
2250 if (subs->endpoint)
2251 continue;
2252 err = snd_pcm_new_stream(as->pcm, stream, 1);
2253 if (err < 0)
2254 return err;
2255 init_substream(as, stream, fp);
2256 return 0;
2257 }
2258
2259 /* create a new pcm */
2260 as = kmalloc(sizeof(*as), GFP_KERNEL);
2261 if (! as)
2262 return -ENOMEM;
2263 memset(as, 0, sizeof(*as));
2264 as->pcm_index = chip->pcm_devs;
2265 as->chip = chip;
2266 as->fmt_type = fp->fmt_type;
2267 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
2268 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
2269 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
2270 &pcm);
2271 if (err < 0) {
2272 kfree(as);
2273 return err;
2274 }
2275 as->pcm = pcm;
2276 pcm->private_data = as;
2277 pcm->private_free = snd_usb_audio_pcm_free;
2278 pcm->info_flags = 0;
2279 if (chip->pcm_devs > 0)
2280 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
2281 else
2282 strcpy(pcm->name, "USB Audio");
2283
2284 init_substream(as, stream, fp);
2285
2286 list_add(&as->list, &chip->pcm_list);
2287 chip->pcm_devs++;
2288
2289 proc_pcm_format_add(as);
2290
2291 return 0;
2292}
2293
2294
2295/*
2296 * check if the device uses big-endian samples
2297 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002298static int is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002300 switch (chip->usb_id) {
2301 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
2302 if (fp->endpoint & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return 1;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002304 break;
2305 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2306 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
2308 return 0;
2309}
2310
2311/*
2312 * parse the audio format type I descriptor
2313 * and returns the corresponding pcm format
2314 *
2315 * @dev: usb device
2316 * @fp: audioformat record
2317 * @format: the format tag (wFormatTag)
2318 * @fmt: the format type descriptor
2319 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002320static int parse_audio_format_i_type(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 int format, unsigned char *fmt)
2322{
2323 int pcm_format;
2324 int sample_width, sample_bytes;
2325
2326 /* FIXME: correct endianess and sign? */
2327 pcm_format = -1;
2328 sample_width = fmt[6];
2329 sample_bytes = fmt[5];
2330 switch (format) {
2331 case 0: /* some devices don't define this correctly... */
2332 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002333 chip->dev->devnum, fp->iface, fp->altsetting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 /* fall-through */
2335 case USB_AUDIO_FORMAT_PCM:
2336 if (sample_width > sample_bytes * 8) {
2337 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002338 chip->dev->devnum, fp->iface, fp->altsetting,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 sample_width, sample_bytes);
2340 }
2341 /* check the format byte size */
2342 switch (fmt[5]) {
2343 case 1:
2344 pcm_format = SNDRV_PCM_FORMAT_S8;
2345 break;
2346 case 2:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002347 if (is_big_endian_format(chip, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
2349 else
2350 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2351 break;
2352 case 3:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002353 if (is_big_endian_format(chip, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
2355 else
2356 pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
2357 break;
2358 case 4:
2359 pcm_format = SNDRV_PCM_FORMAT_S32_LE;
2360 break;
2361 default:
2362 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002363 chip->dev->devnum, fp->iface,
2364 fp->altsetting, sample_width, sample_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 break;
2366 }
2367 break;
2368 case USB_AUDIO_FORMAT_PCM8:
2369 /* Dallas DS4201 workaround */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002370 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 pcm_format = SNDRV_PCM_FORMAT_S8;
2372 else
2373 pcm_format = SNDRV_PCM_FORMAT_U8;
2374 break;
2375 case USB_AUDIO_FORMAT_IEEE_FLOAT:
2376 pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
2377 break;
2378 case USB_AUDIO_FORMAT_ALAW:
2379 pcm_format = SNDRV_PCM_FORMAT_A_LAW;
2380 break;
2381 case USB_AUDIO_FORMAT_MU_LAW:
2382 pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
2383 break;
2384 default:
2385 snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002386 chip->dev->devnum, fp->iface, fp->altsetting, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 break;
2388 }
2389 return pcm_format;
2390}
2391
2392
2393/*
2394 * parse the format descriptor and stores the possible sample rates
2395 * on the audioformat table.
2396 *
2397 * @dev: usb device
2398 * @fp: audioformat record
2399 * @fmt: the format descriptor
2400 * @offset: the start offset of descriptor pointing the rate type
2401 * (7 for type I and II, 8 for type II)
2402 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002403static int parse_audio_format_rates(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 unsigned char *fmt, int offset)
2405{
2406 int nr_rates = fmt[offset];
2407 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
2408 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002409 chip->dev->devnum, fp->iface, fp->altsetting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return -1;
2411 }
2412
2413 if (nr_rates) {
2414 /*
2415 * build the rate table and bitmap flags
2416 */
2417 int r, idx, c;
2418 /* this table corresponds to the SNDRV_PCM_RATE_XXX bit */
2419 static unsigned int conv_rates[] = {
2420 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
2421 64000, 88200, 96000, 176400, 192000
2422 };
2423 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2424 if (fp->rate_table == NULL) {
2425 snd_printk(KERN_ERR "cannot malloc\n");
2426 return -1;
2427 }
2428
2429 fp->nr_rates = nr_rates;
2430 fp->rate_min = fp->rate_max = combine_triple(&fmt[8]);
2431 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
2432 unsigned int rate = fp->rate_table[r] = combine_triple(&fmt[idx]);
2433 if (rate < fp->rate_min)
2434 fp->rate_min = rate;
2435 else if (rate > fp->rate_max)
2436 fp->rate_max = rate;
2437 for (c = 0; c < (int)ARRAY_SIZE(conv_rates); c++) {
2438 if (rate == conv_rates[c]) {
2439 fp->rates |= (1 << c);
2440 break;
2441 }
2442 }
2443 }
2444 } else {
2445 /* continuous rates */
2446 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
2447 fp->rate_min = combine_triple(&fmt[offset + 1]);
2448 fp->rate_max = combine_triple(&fmt[offset + 4]);
2449 }
2450 return 0;
2451}
2452
2453/*
2454 * parse the format type I and III descriptors
2455 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002456static int parse_audio_format_i(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 int format, unsigned char *fmt)
2458{
2459 int pcm_format;
2460
2461 if (fmt[3] == USB_FORMAT_TYPE_III) {
2462 /* FIXME: the format type is really IECxxx
2463 * but we give normal PCM format to get the existing
2464 * apps working...
2465 */
2466 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2467 } else {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002468 pcm_format = parse_audio_format_i_type(chip, fp, format, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (pcm_format < 0)
2470 return -1;
2471 }
2472 fp->format = pcm_format;
2473 fp->channels = fmt[4];
2474 if (fp->channels < 1) {
2475 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002476 chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return -1;
2478 }
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002479 return parse_audio_format_rates(chip, fp, fmt, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
2482/*
2483 * prase the format type II descriptor
2484 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002485static int parse_audio_format_ii(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 int format, unsigned char *fmt)
2487{
2488 int brate, framesize;
2489 switch (format) {
2490 case USB_AUDIO_FORMAT_AC3:
2491 /* FIXME: there is no AC3 format defined yet */
2492 // fp->format = SNDRV_PCM_FORMAT_AC3;
2493 fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
2494 break;
2495 case USB_AUDIO_FORMAT_MPEG:
2496 fp->format = SNDRV_PCM_FORMAT_MPEG;
2497 break;
2498 default:
2499 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 +02002500 chip->dev->devnum, fp->iface, fp->altsetting, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 fp->format = SNDRV_PCM_FORMAT_MPEG;
2502 break;
2503 }
2504 fp->channels = 1;
2505 brate = combine_word(&fmt[4]); /* fmt[4,5] : wMaxBitRate (in kbps) */
2506 framesize = combine_word(&fmt[6]); /* fmt[6,7]: wSamplesPerFrame */
2507 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
2508 fp->frame_size = framesize;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002509 return parse_audio_format_rates(chip, fp, fmt, 8); /* fmt[8..] sample rates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
Takashi Iwai86e07d32005-11-17 15:08:02 +01002512static int parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 int format, unsigned char *fmt, int stream)
2514{
2515 int err;
2516
2517 switch (fmt[3]) {
2518 case USB_FORMAT_TYPE_I:
2519 case USB_FORMAT_TYPE_III:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002520 err = parse_audio_format_i(chip, fp, format, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 break;
2522 case USB_FORMAT_TYPE_II:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002523 err = parse_audio_format_ii(chip, fp, format, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 break;
2525 default:
2526 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002527 chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return -1;
2529 }
2530 fp->fmt_type = fmt[3];
2531 if (err < 0)
2532 return err;
2533#if 1
Clemens Ladisch33159372006-01-13 08:11:22 +01002534 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 /* extigy apparently supports sample rates other than 48k
2536 * but not in ordinary way. so we enable only 48k atm.
2537 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002538 if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
Clemens Ladisch33159372006-01-13 08:11:22 +01002539 chip->usb_id == USB_ID(0x041e, 0x3020) ||
2540 chip->usb_id == USB_ID(0x041e, 0x3061)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 if (fmt[3] == USB_FORMAT_TYPE_I &&
Clemens Ladisch8c1872d2005-04-28 09:31:53 +02002542 fp->rates != SNDRV_PCM_RATE_48000 &&
2543 fp->rates != SNDRV_PCM_RATE_96000)
Clemens Ladischb4d3f9d2005-06-27 08:18:27 +02002544 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
2546#endif
2547 return 0;
2548}
2549
Takashi Iwai86e07d32005-11-17 15:08:02 +01002550static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
2552 struct usb_device *dev;
2553 struct usb_interface *iface;
2554 struct usb_host_interface *alts;
2555 struct usb_interface_descriptor *altsd;
2556 int i, altno, err, stream;
2557 int format;
2558 struct audioformat *fp;
2559 unsigned char *fmt, *csep;
2560
2561 dev = chip->dev;
2562
2563 /* parse the interface's altsettings */
2564 iface = usb_ifnum_to_if(dev, iface_no);
2565 for (i = 0; i < iface->num_altsetting; i++) {
2566 alts = &iface->altsetting[i];
2567 altsd = get_iface_desc(alts);
2568 /* skip invalid one */
2569 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2570 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2571 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING &&
2572 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
2573 altsd->bNumEndpoints < 1 ||
2574 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
2575 continue;
2576 /* must be isochronous */
2577 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2578 USB_ENDPOINT_XFER_ISOC)
2579 continue;
2580 /* check direction */
2581 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
2582 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2583 altno = altsd->bAlternateSetting;
2584
2585 /* get audio formats */
2586 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, AS_GENERAL);
2587 if (!fmt) {
2588 snd_printk(KERN_ERR "%d:%u:%d : AS_GENERAL descriptor not found\n",
2589 dev->devnum, iface_no, altno);
2590 continue;
2591 }
2592
2593 if (fmt[0] < 7) {
2594 snd_printk(KERN_ERR "%d:%u:%d : invalid AS_GENERAL desc\n",
2595 dev->devnum, iface_no, altno);
2596 continue;
2597 }
2598
2599 format = (fmt[6] << 8) | fmt[5]; /* remember the format value */
2600
2601 /* get format type */
2602 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, FORMAT_TYPE);
2603 if (!fmt) {
2604 snd_printk(KERN_ERR "%d:%u:%d : no FORMAT_TYPE desc\n",
2605 dev->devnum, iface_no, altno);
2606 continue;
2607 }
2608 if (fmt[0] < 8) {
2609 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
2610 dev->devnum, iface_no, altno);
2611 continue;
2612 }
2613
2614 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
2615 /* Creamware Noah has this descriptor after the 2nd endpoint */
2616 if (!csep && altsd->bNumEndpoints >= 2)
2617 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
2618 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2619 snd_printk(KERN_ERR "%d:%u:%d : no or invalid class specific endpoint descriptor\n",
2620 dev->devnum, iface_no, altno);
2621 continue;
2622 }
2623
2624 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2625 if (! fp) {
2626 snd_printk(KERN_ERR "cannot malloc\n");
2627 return -ENOMEM;
2628 }
2629
2630 memset(fp, 0, sizeof(*fp));
2631 fp->iface = iface_no;
2632 fp->altsetting = altno;
2633 fp->altset_idx = i;
2634 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2635 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Clemens Ladisch573567e2005-06-27 08:17:30 +02002637 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
2638 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
2639 * (fp->maxpacksize & 0x7ff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 fp->attributes = csep[3];
2641
2642 /* some quirks for attributes here */
2643
Clemens Ladischc3f93292005-05-04 14:56:04 +02002644 switch (chip->usb_id) {
2645 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 /* Optoplay sets the sample rate attribute although
2647 * it seems not supporting it in fact.
2648 */
2649 fp->attributes &= ~EP_CS_ATTR_SAMPLE_RATE;
Clemens Ladischc3f93292005-05-04 14:56:04 +02002650 break;
2651 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
2652 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 /* doesn't set the sample rate attribute, but supports it */
2654 fp->attributes |= EP_CS_ATTR_SAMPLE_RATE;
Clemens Ladischc3f93292005-05-04 14:56:04 +02002655 break;
2656 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
2657 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
2658 an older model 77d:223) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 /*
2660 * plantronics headset and Griffin iMic have set adaptive-in
2661 * although it's really not...
2662 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 fp->ep_attr &= ~EP_ATTR_MASK;
2664 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2665 fp->ep_attr |= EP_ATTR_ADAPTIVE;
2666 else
2667 fp->ep_attr |= EP_ATTR_SYNC;
Clemens Ladischc3f93292005-05-04 14:56:04 +02002668 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670
2671 /* ok, let's parse further... */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002672 if (parse_audio_format(chip, fp, format, fmt, stream) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 kfree(fp->rate_table);
2674 kfree(fp);
2675 continue;
2676 }
2677
2678 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint 0x%x\n", dev->devnum, iface_no, i, fp->endpoint);
2679 err = add_audio_endpoint(chip, stream, fp);
2680 if (err < 0) {
2681 kfree(fp->rate_table);
2682 kfree(fp);
2683 return err;
2684 }
2685 /* try to set the interface... */
2686 usb_set_interface(chip->dev, iface_no, altno);
2687 init_usb_pitch(chip->dev, iface_no, alts, fp);
2688 init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
2689 }
2690 return 0;
2691}
2692
2693
2694/*
2695 * disconnect streams
2696 * called from snd_usb_audio_disconnect()
2697 */
Clemens Ladischee733392005-04-25 10:34:13 +02002698static void snd_usb_stream_disconnect(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 int idx;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002701 struct snd_usb_stream *as;
2702 struct snd_usb_substream *subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Takashi Iwai86e07d32005-11-17 15:08:02 +01002704 as = list_entry(head, struct snd_usb_stream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 for (idx = 0; idx < 2; idx++) {
2706 subs = &as->substream[idx];
2707 if (!subs->num_formats)
2708 return;
2709 release_substream_urbs(subs, 1);
2710 subs->interface = -1;
2711 }
2712}
2713
2714/*
2715 * parse audio control descriptor and create pcm/midi streams
2716 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002717static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
2719 struct usb_device *dev = chip->dev;
2720 struct usb_host_interface *host_iface;
2721 struct usb_interface *iface;
2722 unsigned char *p1;
2723 int i, j;
2724
2725 /* find audiocontrol interface */
2726 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
2727 if (!(p1 = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen, NULL, HEADER))) {
2728 snd_printk(KERN_ERR "cannot find HEADER\n");
2729 return -EINVAL;
2730 }
2731 if (! p1[7] || p1[0] < 8 + p1[7]) {
2732 snd_printk(KERN_ERR "invalid HEADER\n");
2733 return -EINVAL;
2734 }
2735
2736 /*
2737 * parse all USB audio streaming interfaces
2738 */
2739 for (i = 0; i < p1[7]; i++) {
2740 struct usb_host_interface *alts;
2741 struct usb_interface_descriptor *altsd;
2742 j = p1[8 + i];
2743 iface = usb_ifnum_to_if(dev, j);
2744 if (!iface) {
2745 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
2746 dev->devnum, ctrlif, j);
2747 continue;
2748 }
2749 if (usb_interface_claimed(iface)) {
2750 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n", dev->devnum, ctrlif, j);
2751 continue;
2752 }
2753 alts = &iface->altsetting[0];
2754 altsd = get_iface_desc(alts);
2755 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
2756 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
2757 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDI_STREAMING) {
2758 if (snd_usb_create_midi_interface(chip, iface, NULL) < 0) {
2759 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n", dev->devnum, ctrlif, j);
2760 continue;
2761 }
2762 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2763 continue;
2764 }
2765 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2766 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2767 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING) {
2768 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n", dev->devnum, ctrlif, j, altsd->bInterfaceClass);
2769 /* skip non-supported classes */
2770 continue;
2771 }
2772 if (! parse_audio_endpoints(chip, j)) {
2773 usb_set_interface(dev, j, 0); /* reset the current interface */
2774 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2775 }
2776 }
2777
2778 return 0;
2779}
2780
2781/*
2782 * create a stream for an endpoint/altsetting without proper descriptors
2783 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002784static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002786 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
2788 struct audioformat *fp;
2789 struct usb_host_interface *alts;
2790 int stream, err;
2791 int *rate_table = NULL;
2792
2793 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2794 if (! fp) {
2795 snd_printk(KERN_ERR "cannot malloc\n");
2796 return -ENOMEM;
2797 }
2798 memcpy(fp, quirk->data, sizeof(*fp));
2799 if (fp->nr_rates > 0) {
2800 rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
2801 if (!rate_table) {
2802 kfree(fp);
2803 return -ENOMEM;
2804 }
2805 memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
2806 fp->rate_table = rate_table;
2807 }
2808
2809 stream = (fp->endpoint & USB_DIR_IN)
2810 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2811 err = add_audio_endpoint(chip, stream, fp);
2812 if (err < 0) {
2813 kfree(fp);
2814 kfree(rate_table);
2815 return err;
2816 }
2817 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
2818 fp->altset_idx >= iface->num_altsetting) {
2819 kfree(fp);
2820 kfree(rate_table);
2821 return -EINVAL;
2822 }
2823 alts = &iface->altsetting[fp->altset_idx];
2824 usb_set_interface(chip->dev, fp->iface, 0);
2825 init_usb_pitch(chip->dev, fp->iface, alts, fp);
2826 init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
2827 return 0;
2828}
2829
2830/*
2831 * create a stream for an interface with proper descriptors
2832 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002833static int create_standard_audio_quirk(struct snd_usb_audio *chip,
Clemens Ladischd1bda042005-09-14 08:36:03 +02002834 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002835 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
2837 struct usb_host_interface *alts;
2838 struct usb_interface_descriptor *altsd;
2839 int err;
2840
2841 alts = &iface->altsetting[0];
2842 altsd = get_iface_desc(alts);
Clemens Ladischd1bda042005-09-14 08:36:03 +02002843 err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (err < 0) {
2845 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
2846 altsd->bInterfaceNumber, err);
2847 return err;
2848 }
Clemens Ladischd1bda042005-09-14 08:36:03 +02002849 /* reset the current interface */
2850 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return 0;
2852}
2853
2854/*
2855 * Create a stream for an Edirol UA-700/UA-25 interface. The only way
2856 * to detect the sample rate is by looking at wMaxPacketSize.
2857 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002858static int create_ua700_ua25_quirk(struct snd_usb_audio *chip,
Clemens Ladisch854af952005-07-25 16:19:10 +02002859 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002860 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
2862 static const struct audioformat ua_format = {
2863 .format = SNDRV_PCM_FORMAT_S24_3LE,
2864 .channels = 2,
2865 .fmt_type = USB_FORMAT_TYPE_I,
2866 .altsetting = 1,
2867 .altset_idx = 1,
2868 .rates = SNDRV_PCM_RATE_CONTINUOUS,
2869 };
2870 struct usb_host_interface *alts;
2871 struct usb_interface_descriptor *altsd;
2872 struct audioformat *fp;
2873 int stream, err;
2874
2875 /* both PCM and MIDI interfaces have 2 altsettings */
2876 if (iface->num_altsetting != 2)
2877 return -ENXIO;
2878 alts = &iface->altsetting[1];
2879 altsd = get_iface_desc(alts);
2880
2881 if (altsd->bNumEndpoints == 2) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002882 static const struct snd_usb_midi_endpoint_info ua700_ep = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 .out_cables = 0x0003,
2884 .in_cables = 0x0003
2885 };
Takashi Iwai86e07d32005-11-17 15:08:02 +01002886 static const struct snd_usb_audio_quirk ua700_quirk = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 .type = QUIRK_MIDI_FIXED_ENDPOINT,
2888 .data = &ua700_ep
2889 };
Takashi Iwai86e07d32005-11-17 15:08:02 +01002890 static const struct snd_usb_midi_endpoint_info ua25_ep = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 .out_cables = 0x0001,
2892 .in_cables = 0x0001
2893 };
Takashi Iwai86e07d32005-11-17 15:08:02 +01002894 static const struct snd_usb_audio_quirk ua25_quirk = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 .type = QUIRK_MIDI_FIXED_ENDPOINT,
2896 .data = &ua25_ep
2897 };
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002898 if (chip->usb_id == USB_ID(0x0582, 0x002b))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 return snd_usb_create_midi_interface(chip, iface,
2900 &ua700_quirk);
2901 else
2902 return snd_usb_create_midi_interface(chip, iface,
2903 &ua25_quirk);
2904 }
2905
2906 if (altsd->bNumEndpoints != 1)
2907 return -ENXIO;
2908
2909 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2910 if (!fp)
2911 return -ENOMEM;
2912 memcpy(fp, &ua_format, sizeof(*fp));
2913
2914 fp->iface = altsd->bInterfaceNumber;
2915 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2916 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2917 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2918
2919 switch (fp->maxpacksize) {
2920 case 0x120:
2921 fp->rate_max = fp->rate_min = 44100;
2922 break;
2923 case 0x138:
2924 case 0x140:
2925 fp->rate_max = fp->rate_min = 48000;
2926 break;
2927 case 0x258:
2928 case 0x260:
2929 fp->rate_max = fp->rate_min = 96000;
2930 break;
2931 default:
2932 snd_printk(KERN_ERR "unknown sample rate\n");
2933 kfree(fp);
2934 return -ENXIO;
2935 }
2936
2937 stream = (fp->endpoint & USB_DIR_IN)
2938 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2939 err = add_audio_endpoint(chip, stream, fp);
2940 if (err < 0) {
2941 kfree(fp);
2942 return err;
2943 }
2944 usb_set_interface(chip->dev, fp->iface, 0);
2945 return 0;
2946}
2947
2948/*
2949 * Create a stream for an Edirol UA-1000 interface.
2950 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002951static int create_ua1000_quirk(struct snd_usb_audio *chip,
Clemens Ladisch854af952005-07-25 16:19:10 +02002952 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002953 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 static const struct audioformat ua1000_format = {
2956 .format = SNDRV_PCM_FORMAT_S32_LE,
2957 .fmt_type = USB_FORMAT_TYPE_I,
2958 .altsetting = 1,
2959 .altset_idx = 1,
2960 .attributes = 0,
2961 .rates = SNDRV_PCM_RATE_CONTINUOUS,
2962 };
2963 struct usb_host_interface *alts;
2964 struct usb_interface_descriptor *altsd;
2965 struct audioformat *fp;
2966 int stream, err;
2967
2968 if (iface->num_altsetting != 2)
2969 return -ENXIO;
2970 alts = &iface->altsetting[1];
2971 altsd = get_iface_desc(alts);
2972 if (alts->extralen != 11 || alts->extra[1] != CS_AUDIO_INTERFACE ||
2973 altsd->bNumEndpoints != 1)
2974 return -ENXIO;
2975
2976 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2977 if (!fp)
2978 return -ENOMEM;
2979 memcpy(fp, &ua1000_format, sizeof(*fp));
2980
2981 fp->channels = alts->extra[4];
2982 fp->iface = altsd->bInterfaceNumber;
2983 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2984 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2985 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2986 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]);
2987
2988 stream = (fp->endpoint & USB_DIR_IN)
2989 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2990 err = add_audio_endpoint(chip, stream, fp);
2991 if (err < 0) {
2992 kfree(fp);
2993 return err;
2994 }
2995 /* FIXME: playback must be synchronized to capture */
2996 usb_set_interface(chip->dev, fp->iface, 0);
2997 return 0;
2998}
2999
Takashi Iwai86e07d32005-11-17 15:08:02 +01003000static int snd_usb_create_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003002 const struct snd_usb_audio_quirk *quirk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004/*
3005 * handle the quirks for the contained interfaces
3006 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003007static int create_composite_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003009 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
3011 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
3012 int err;
3013
3014 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
3015 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
3016 if (!iface)
3017 continue;
3018 if (quirk->ifnum != probed_ifnum &&
3019 usb_interface_claimed(iface))
3020 continue;
3021 err = snd_usb_create_quirk(chip, iface, quirk);
3022 if (err < 0)
3023 return err;
3024 if (quirk->ifnum != probed_ifnum)
3025 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
3026 }
3027 return 0;
3028}
3029
Takashi Iwai86e07d32005-11-17 15:08:02 +01003030static int ignore_interface_quirk(struct snd_usb_audio *chip,
Clemens Ladisch854af952005-07-25 16:19:10 +02003031 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003032 const struct snd_usb_audio_quirk *quirk)
Clemens Ladisch854af952005-07-25 16:19:10 +02003033{
3034 return 0;
3035}
3036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038/*
3039 * boot quirks
3040 */
3041
3042#define EXTIGY_FIRMWARE_SIZE_OLD 794
3043#define EXTIGY_FIRMWARE_SIZE_NEW 483
3044
3045static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
3046{
3047 struct usb_host_config *config = dev->actconfig;
3048 int err;
3049
3050 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
3051 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
3052 snd_printdd("sending Extigy boot sequence...\n");
3053 /* Send message to force it to reconnect with full interface. */
3054 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
3055 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
3056 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
3057 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
3058 &dev->descriptor, sizeof(dev->descriptor));
3059 config = dev->actconfig;
3060 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
3061 err = usb_reset_configuration(dev);
3062 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
3063 snd_printdd("extigy_boot: new boot length = %d\n",
3064 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
3065 return -ENODEV; /* quit this anyway */
3066 }
3067 return 0;
3068}
3069
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003070static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
3071{
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003072 u8 buf = 1;
3073
3074 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
3075 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3076 0, 0, &buf, 1, 1000);
3077 if (buf == 0) {
3078 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
3079 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3080 1, 2000, NULL, 0, 1000);
3081 return -ENODEV;
3082 }
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003083 return 0;
3084}
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
3087/*
3088 * audio-interface quirks
3089 *
3090 * returns zero if no standard audio/MIDI parsing is needed.
3091 * returns a postive value if standard audio/midi interfaces are parsed
3092 * after this.
3093 * returns a negative value at error.
3094 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003095static int snd_usb_create_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003097 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003099 typedef int (*quirk_func_t)(struct snd_usb_audio *, struct usb_interface *,
3100 const struct snd_usb_audio_quirk *);
Clemens Ladisch854af952005-07-25 16:19:10 +02003101 static const quirk_func_t quirk_funcs[] = {
3102 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
3103 [QUIRK_COMPOSITE] = create_composite_quirk,
3104 [QUIRK_MIDI_STANDARD_INTERFACE] = snd_usb_create_midi_interface,
3105 [QUIRK_MIDI_FIXED_ENDPOINT] = snd_usb_create_midi_interface,
3106 [QUIRK_MIDI_YAMAHA] = snd_usb_create_midi_interface,
3107 [QUIRK_MIDI_MIDIMAN] = snd_usb_create_midi_interface,
3108 [QUIRK_MIDI_NOVATION] = snd_usb_create_midi_interface,
3109 [QUIRK_MIDI_RAW] = snd_usb_create_midi_interface,
3110 [QUIRK_MIDI_EMAGIC] = snd_usb_create_midi_interface,
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01003111 [QUIRK_MIDI_CME] = snd_usb_create_midi_interface,
Clemens Ladischd1bda042005-09-14 08:36:03 +02003112 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
Clemens Ladisch854af952005-07-25 16:19:10 +02003113 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
3114 [QUIRK_AUDIO_EDIROL_UA700_UA25] = create_ua700_ua25_quirk,
3115 [QUIRK_AUDIO_EDIROL_UA1000] = create_ua1000_quirk,
3116 };
3117
3118 if (quirk->type < QUIRK_TYPE_COUNT) {
3119 return quirk_funcs[quirk->type](chip, iface, quirk);
3120 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
3122 return -ENXIO;
3123 }
3124}
3125
3126
3127/*
3128 * common proc files to show the usb device info
3129 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003130static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003132 struct snd_usb_audio *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (! chip->shutdown)
3134 snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
3135}
3136
Takashi Iwai86e07d32005-11-17 15:08:02 +01003137static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003139 struct snd_usb_audio *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (! chip->shutdown)
3141 snd_iprintf(buffer, "%04x:%04x\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003142 USB_ID_VENDOR(chip->usb_id),
3143 USB_ID_PRODUCT(chip->usb_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
Takashi Iwai86e07d32005-11-17 15:08:02 +01003146static void snd_usb_audio_create_proc(struct snd_usb_audio *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003148 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 if (! snd_card_proc_new(chip->card, "usbbus", &entry))
3150 snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbbus_read);
3151 if (! snd_card_proc_new(chip->card, "usbid", &entry))
3152 snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbid_read);
3153}
3154
3155/*
3156 * free the chip instance
3157 *
3158 * here we have to do not much, since pcm and controls are already freed
3159 *
3160 */
3161
Takashi Iwai86e07d32005-11-17 15:08:02 +01003162static int snd_usb_audio_free(struct snd_usb_audio *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
3164 kfree(chip);
3165 return 0;
3166}
3167
Takashi Iwai86e07d32005-11-17 15:08:02 +01003168static int snd_usb_audio_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003170 struct snd_usb_audio *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return snd_usb_audio_free(chip);
3172}
3173
3174
3175/*
3176 * create a chip instance and set its names.
3177 */
3178static int snd_usb_audio_create(struct usb_device *dev, int idx,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003179 const struct snd_usb_audio_quirk *quirk,
3180 struct snd_usb_audio **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003182 struct snd_card *card;
3183 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 int err, len;
3185 char component[14];
Takashi Iwai86e07d32005-11-17 15:08:02 +01003186 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 .dev_free = snd_usb_audio_dev_free,
3188 };
3189
3190 *rchip = NULL;
3191
3192 if (snd_usb_get_speed(dev) != USB_SPEED_FULL &&
3193 snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
3194 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
3195 return -ENXIO;
3196 }
3197
3198 card = snd_card_new(index[idx], id[idx], THIS_MODULE, 0);
3199 if (card == NULL) {
3200 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
3201 return -ENOMEM;
3202 }
3203
Takashi Iwai561b2202005-09-09 14:22:34 +02003204 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 if (! chip) {
3206 snd_card_free(card);
3207 return -ENOMEM;
3208 }
3209
3210 chip->index = idx;
3211 chip->dev = dev;
3212 chip->card = card;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003213 chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3214 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 INIT_LIST_HEAD(&chip->pcm_list);
3216 INIT_LIST_HEAD(&chip->midi_list);
Clemens Ladisch84957a82005-04-29 16:23:13 +02003217 INIT_LIST_HEAD(&chip->mixer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
3219 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
3220 snd_usb_audio_free(chip);
3221 snd_card_free(card);
3222 return err;
3223 }
3224
3225 strcpy(card->driver, "USB-Audio");
3226 sprintf(component, "USB%04x:%04x",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003227 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 snd_component_add(card, component);
3229
3230 /* retrieve the device string as shortname */
3231 if (quirk && quirk->product_name) {
3232 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
3233 } else {
3234 if (!dev->descriptor.iProduct ||
3235 usb_string(dev, dev->descriptor.iProduct,
3236 card->shortname, sizeof(card->shortname)) <= 0) {
3237 /* no name available from anywhere, so use ID */
3238 sprintf(card->shortname, "USB Device %#04x:%#04x",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003239 USB_ID_VENDOR(chip->usb_id),
3240 USB_ID_PRODUCT(chip->usb_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
3242 }
3243
3244 /* retrieve the vendor and device strings as longname */
3245 if (quirk && quirk->vendor_name) {
3246 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
3247 } else {
3248 if (dev->descriptor.iManufacturer)
3249 len = usb_string(dev, dev->descriptor.iManufacturer,
3250 card->longname, sizeof(card->longname));
3251 else
3252 len = 0;
3253 /* we don't really care if there isn't any vendor string */
3254 }
3255 if (len > 0)
3256 strlcat(card->longname, " ", sizeof(card->longname));
3257
3258 strlcat(card->longname, card->shortname, sizeof(card->longname));
3259
3260 len = strlcat(card->longname, " at ", sizeof(card->longname));
3261
3262 if (len < sizeof(card->longname))
3263 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
3264
3265 strlcat(card->longname,
3266 snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" : ", high speed",
3267 sizeof(card->longname));
3268
3269 snd_usb_audio_create_proc(chip);
3270
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 *rchip = chip;
3272 return 0;
3273}
3274
3275
3276/*
3277 * probe the active usb device
3278 *
3279 * note that this can be called multiple times per a device, when it
3280 * includes multiple audio control interfaces.
3281 *
3282 * thus we check the usb device pointer and creates the card instance
3283 * only at the first time. the successive calls of this function will
3284 * append the pcm interface to the corresponding card.
3285 */
3286static void *snd_usb_audio_probe(struct usb_device *dev,
3287 struct usb_interface *intf,
3288 const struct usb_device_id *usb_id)
3289{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003290 const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 int i, err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01003292 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 struct usb_host_interface *alts;
3294 int ifnum;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003295 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
3297 alts = &intf->altsetting[0];
3298 ifnum = get_iface_desc(alts)->bInterfaceNumber;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003299 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3300 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
3302 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
3303 goto __err_val;
3304
3305 /* SB Extigy needs special boot-up sequence */
3306 /* if more models come, this will go to the quirk list. */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003307 if (id == USB_ID(0x041e, 0x3000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 if (snd_usb_extigy_boot_quirk(dev, intf) < 0)
3309 goto __err_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 }
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003311 /* SB Audigy 2 NX needs its own boot-up magic, too */
3312 if (id == USB_ID(0x041e, 0x3020)) {
3313 if (snd_usb_audigy2nx_boot_quirk(dev) < 0)
3314 goto __err_val;
3315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317 /*
3318 * found a config. now register to ALSA
3319 */
3320
3321 /* check whether it's already registered */
3322 chip = NULL;
Ingo Molnar12aa7572006-01-16 16:36:05 +01003323 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 for (i = 0; i < SNDRV_CARDS; i++) {
3325 if (usb_chip[i] && usb_chip[i]->dev == dev) {
3326 if (usb_chip[i]->shutdown) {
3327 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
3328 goto __error;
3329 }
3330 chip = usb_chip[i];
3331 break;
3332 }
3333 }
3334 if (! chip) {
3335 /* it's a fresh one.
3336 * now look for an empty slot and create a new card instance
3337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 for (i = 0; i < SNDRV_CARDS; i++)
3339 if (enable[i] && ! usb_chip[i] &&
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003340 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
3341 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
3343 goto __error;
3344 }
Clemens Ladisch1dcd3ec2005-05-10 14:51:40 +02003345 snd_card_set_dev(chip->card, &intf->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 break;
3347 }
3348 if (! chip) {
3349 snd_printk(KERN_ERR "no available usb audio device\n");
3350 goto __error;
3351 }
3352 }
3353
3354 err = 1; /* continue */
3355 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
3356 /* need some special handlings */
3357 if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)
3358 goto __error;
3359 }
3360
3361 if (err > 0) {
3362 /* create normal USB audio interfaces */
3363 if (snd_usb_create_streams(chip, ifnum) < 0 ||
3364 snd_usb_create_mixer(chip, ifnum) < 0) {
3365 goto __error;
3366 }
3367 }
3368
3369 /* we are allowed to call snd_card_register() many times */
3370 if (snd_card_register(chip->card) < 0) {
3371 goto __error;
3372 }
3373
3374 usb_chip[chip->index] = chip;
3375 chip->num_interfaces++;
Ingo Molnar12aa7572006-01-16 16:36:05 +01003376 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 return chip;
3378
3379 __error:
3380 if (chip && !chip->num_interfaces)
3381 snd_card_free(chip->card);
Ingo Molnar12aa7572006-01-16 16:36:05 +01003382 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 __err_val:
3384 return NULL;
3385}
3386
3387/*
3388 * we need to take care of counter, since disconnection can be called also
3389 * many times as well as usb_audio_probe().
3390 */
3391static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
3392{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003393 struct snd_usb_audio *chip;
3394 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 struct list_head *p;
3396
3397 if (ptr == (void *)-1L)
3398 return;
3399
3400 chip = ptr;
3401 card = chip->card;
Ingo Molnar12aa7572006-01-16 16:36:05 +01003402 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 chip->shutdown = 1;
3404 chip->num_interfaces--;
3405 if (chip->num_interfaces <= 0) {
3406 snd_card_disconnect(card);
3407 /* release the pcm resources */
3408 list_for_each(p, &chip->pcm_list) {
Clemens Ladischee733392005-04-25 10:34:13 +02003409 snd_usb_stream_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 }
3411 /* release the midi resources */
3412 list_for_each(p, &chip->midi_list) {
Clemens Ladischee733392005-04-25 10:34:13 +02003413 snd_usbmidi_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02003415 /* release mixer resources */
3416 list_for_each(p, &chip->mixer_list) {
3417 snd_usb_mixer_disconnect(p);
3418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 usb_chip[chip->index] = NULL;
Ingo Molnar12aa7572006-01-16 16:36:05 +01003420 mutex_unlock(&register_mutex);
Karsten Wiese230cd5e2005-04-20 10:12:35 +02003421 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 } else {
Ingo Molnar12aa7572006-01-16 16:36:05 +01003423 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 }
3425}
3426
3427/*
3428 * new 2.5 USB kernel API
3429 */
3430static int usb_audio_probe(struct usb_interface *intf,
3431 const struct usb_device_id *id)
3432{
3433 void *chip;
3434 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
3435 if (chip) {
3436 dev_set_drvdata(&intf->dev, chip);
3437 return 0;
3438 } else
3439 return -EIO;
3440}
3441
3442static void usb_audio_disconnect(struct usb_interface *intf)
3443{
3444 snd_usb_audio_disconnect(interface_to_usbdev(intf),
3445 dev_get_drvdata(&intf->dev));
3446}
3447
3448
3449static int __init snd_usb_audio_init(void)
3450{
3451 if (nrpacks < MIN_PACKS_URB || nrpacks > MAX_PACKS) {
3452 printk(KERN_WARNING "invalid nrpacks value.\n");
3453 return -EINVAL;
3454 }
3455 usb_register(&usb_audio_driver);
3456 return 0;
3457}
3458
3459
3460static void __exit snd_usb_audio_cleanup(void)
3461{
3462 usb_deregister(&usb_audio_driver);
3463}
3464
3465module_init(snd_usb_audio_init);
3466module_exit(snd_usb_audio_cleanup);