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