blob: 286fa14e48bd548d723c1aad45f6870b52678ea3 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/bitops.h>
42#include <linux/init.h>
43#include <linux/list.h>
44#include <linux/slab.h>
45#include <linux/string.h>
46#include <linux/usb.h>
47#include <linux/moduleparam.h>
Ingo Molnar12aa7572006-01-16 16:36:05 +010048#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <sound/core.h>
50#include <sound/info.h>
51#include <sound/pcm.h>
52#include <sound/pcm_params.h>
53#include <sound/initval.h>
54
55#include "usbaudio.h"
56
57
58MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
59MODULE_DESCRIPTION("USB Audio");
60MODULE_LICENSE("GPL");
61MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
62
63
64static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
65static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Pavel Machek07f51a72008-04-14 13:15:56 +020066static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
67/* Vendor/product IDs for this card */
68static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
69static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
Clemens Ladisch92b9ac72006-09-22 10:57:36 +020070static int nrpacks = 8; /* max. number of packets per urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static int async_unlink = 1;
Thibault LE MEURe3113342006-03-14 11:44:53 +010072static int device_setup[SNDRV_CARDS]; /* device parameter for this card*/
Takashi Iwai7a9b8062008-08-13 15:40:53 +020073static int ignore_ctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75module_param_array(index, int, NULL, 0444);
76MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
77module_param_array(id, charp, NULL, 0444);
78MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
79module_param_array(enable, bool, NULL, 0444);
80MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
81module_param_array(vid, int, NULL, 0444);
82MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
83module_param_array(pid, int, NULL, 0444);
84MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
Clemens Ladisch71d848c2005-08-12 15:18:00 +020085module_param(nrpacks, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
87module_param(async_unlink, bool, 0444);
88MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
Thibault LE MEURe3113342006-03-14 11:44:53 +010089module_param_array(device_setup, int, NULL, 0444);
90MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
Takashi Iwai7a9b8062008-08-13 15:40:53 +020091module_param(ignore_ctl_error, bool, 0444);
92MODULE_PARM_DESC(ignore_ctl_error,
93 "Ignore errors from USB controller for mixer interfaces.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/*
96 * debug the h/w constraints
97 */
98/* #define HW_CONST_DEBUG */
99
100
101/*
102 *
103 */
104
Clemens Ladisch92b9ac72006-09-22 10:57:36 +0200105#define MAX_PACKS 20
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
Clemens Ladisch15a24c02005-08-12 08:25:26 +0200107#define MAX_URBS 8
Clemens Ladisch604cf492005-05-17 09:15:27 +0200108#define SYNC_URBS 4 /* always four urbs for sync */
Clemens Ladisch4d788e02009-01-26 08:09:28 +0100109#define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111struct audioformat {
112 struct list_head list;
113 snd_pcm_format_t format; /* format type */
114 unsigned int channels; /* # channels */
115 unsigned int fmt_type; /* USB audio format type (1-3) */
116 unsigned int frame_size; /* samples per frame for non-audio */
117 int iface; /* interface number */
118 unsigned char altsetting; /* corresponding alternate setting */
119 unsigned char altset_idx; /* array index of altenate setting */
120 unsigned char attributes; /* corresponding attributes of cs endpoint */
121 unsigned char endpoint; /* endpoint */
122 unsigned char ep_attr; /* endpoint attributes */
Clemens Ladisch744b89e2009-04-03 09:45:01 +0200123 unsigned char datainterval; /* log_2 of data packet interval */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 unsigned int maxpacksize; /* max. packet size */
125 unsigned int rates; /* rate bitmasks */
126 unsigned int rate_min, rate_max; /* min/max rates */
127 unsigned int nr_rates; /* number of rate table entries */
128 unsigned int *rate_table; /* rate table */
129};
130
Takashi Iwai86e07d32005-11-17 15:08:02 +0100131struct snd_usb_substream;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133struct snd_urb_ctx {
134 struct urb *urb;
Clemens Ladisch55851f72005-08-15 08:34:16 +0200135 unsigned int buffer_size; /* size of data buffer, if data URB */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100136 struct snd_usb_substream *subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int index; /* index for urb array */
138 int packets; /* number of packets per urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
141struct snd_urb_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100142 int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
143 int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
144 int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
145 int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
148struct snd_usb_substream {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100149 struct snd_usb_stream *stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct usb_device *dev;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100151 struct snd_pcm_substream *pcm_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 int direction; /* playback or capture */
153 int interface; /* current interface */
154 int endpoint; /* assigned endpoint */
155 struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
156 unsigned int cur_rate; /* current rate (for hw_params callback) */
157 unsigned int period_bytes; /* current period bytes (for hw_params callback) */
158 unsigned int format; /* USB data format */
159 unsigned int datapipe; /* the data i/o pipe */
160 unsigned int syncpipe; /* 1 - async out or adaptive in */
Clemens Ladisch573567e2005-06-27 08:17:30 +0200161 unsigned int datainterval; /* log_2 of data packet interval */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
163 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
164 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
165 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
166 unsigned int phase; /* phase accumulator */
167 unsigned int maxpacksize; /* max packet size in bytes */
168 unsigned int maxframesize; /* max packet size in frames */
169 unsigned int curpacksize; /* current packet size in bytes (for capture) */
170 unsigned int curframesize; /* current packet size in frames (for capture) */
171 unsigned int fill_max: 1; /* fill max packet size always */
172 unsigned int fmt_type; /* USB audio format type (1-3) */
173
174 unsigned int running: 1; /* running status */
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 unsigned int hwptr_done; /* processed frame position in the buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int transfer_done; /* processed frames since last period update */
178 unsigned long active_mask; /* bitmask of active urbs */
179 unsigned long unlink_mask; /* bitmask of unlinked urbs */
180
181 unsigned int nurbs; /* # urbs */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100182 struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */
183 struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */
Clemens Ladisch55851f72005-08-15 08:34:16 +0200184 char *syncbuf; /* sync buffer for all sync URBs */
185 dma_addr_t sync_dma; /* DMA address of syncbuf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 u64 formats; /* format bitmasks (all or'ed) */
188 unsigned int num_formats; /* number of supported audio formats (list) */
189 struct list_head fmt_list; /* format list */
Takashi Iwai8fec5602007-02-01 11:50:56 +0100190 struct snd_pcm_hw_constraint_list rate_list; /* limited rates */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 spinlock_t lock;
192
193 struct snd_urb_ops ops; /* callbacks (must be filled at init) */
194};
195
196
197struct snd_usb_stream {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100198 struct snd_usb_audio *chip;
199 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int pcm_index;
201 unsigned int fmt_type; /* USB audio format type (1-3) */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100202 struct snd_usb_substream substream[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct list_head list;
204};
205
206
207/*
208 * we keep the snd_usb_audio_t instances by ourselves for merging
209 * the all interfaces on the same card as one sound device.
210 */
211
Ingo Molnar12aa7572006-01-16 16:36:05 +0100212static DEFINE_MUTEX(register_mutex);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100213static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215
216/*
217 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
218 * this will overflow at approx 524 kHz
219 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700220static inline unsigned get_usb_full_speed_rate(unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 return ((rate << 13) + 62) / 125;
223}
224
225/*
226 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
227 * this will overflow at approx 4 MHz
228 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700229static inline unsigned get_usb_high_speed_rate(unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 return ((rate << 10) + 62) / 125;
232}
233
234/* convert our full speed USB rate into sampling rate in Hz */
Jesper Juhl77933d72005-07-27 11:46:09 -0700235static inline unsigned get_full_speed_hz(unsigned int usb_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 return (usb_rate * 125 + (1 << 12)) >> 13;
238}
239
240/* convert our high speed USB rate into sampling rate in Hz */
Jesper Juhl77933d72005-07-27 11:46:09 -0700241static inline unsigned get_high_speed_hz(unsigned int usb_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 return (usb_rate * 125 + (1 << 9)) >> 10;
244}
245
246
247/*
248 * prepare urb for full speed capture sync pipe
249 *
250 * fill the length and offset of each urb descriptor.
251 * the fixed 10.14 frequency is passed through the pipe.
252 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100253static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
254 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct urb *urb)
256{
257 unsigned char *cp = urb->transfer_buffer;
John Daiker88518272006-12-28 13:55:05 +0100258 struct snd_urb_ctx *ctx = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200261 urb->iso_frame_desc[0].length = 3;
262 urb->iso_frame_desc[0].offset = 0;
263 cp[0] = subs->freqn >> 2;
264 cp[1] = subs->freqn >> 10;
265 cp[2] = subs->freqn >> 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 0;
267}
268
269/*
270 * prepare urb for high speed capture sync pipe
271 *
272 * fill the length and offset of each urb descriptor.
273 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
274 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100275static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
276 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct urb *urb)
278{
279 unsigned char *cp = urb->transfer_buffer;
John Daiker88518272006-12-28 13:55:05 +0100280 struct snd_urb_ctx *ctx = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200283 urb->iso_frame_desc[0].length = 4;
284 urb->iso_frame_desc[0].offset = 0;
285 cp[0] = subs->freqn;
286 cp[1] = subs->freqn >> 8;
287 cp[2] = subs->freqn >> 16;
288 cp[3] = subs->freqn >> 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return 0;
290}
291
292/*
293 * process after capture sync complete
294 * - nothing to do
295 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100296static int retire_capture_sync_urb(struct snd_usb_substream *subs,
297 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct urb *urb)
299{
300 return 0;
301}
302
303/*
304 * prepare urb for capture data pipe
305 *
306 * fill the offset and length of each descriptor.
307 *
308 * we use a temporary buffer to write the captured data.
309 * since the length of written data is determined by host, we cannot
310 * write onto the pcm buffer directly... the data is thus copied
311 * later at complete callback to the global buffer.
312 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100313static int prepare_capture_urb(struct snd_usb_substream *subs,
314 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct urb *urb)
316{
317 int i, offs;
John Daiker88518272006-12-28 13:55:05 +0100318 struct snd_urb_ctx *ctx = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 offs = 0;
321 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 for (i = 0; i < ctx->packets; i++) {
323 urb->iso_frame_desc[i].offset = offs;
324 urb->iso_frame_desc[i].length = subs->curpacksize;
325 offs += subs->curpacksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 urb->transfer_buffer_length = offs;
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200328 urb->number_of_packets = ctx->packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330}
331
332/*
333 * process after capture complete
334 *
335 * copy the data from each desctiptor to the pcm buffer, and
336 * update the current position.
337 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100338static int retire_capture_urb(struct snd_usb_substream *subs,
339 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct urb *urb)
341{
342 unsigned long flags;
343 unsigned char *cp;
344 int i;
345 unsigned int stride, len, oldptr;
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200346 int period_elapsed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 stride = runtime->frame_bits >> 3;
349
350 for (i = 0; i < urb->number_of_packets; i++) {
351 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
352 if (urb->iso_frame_desc[i].status) {
353 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
354 // continue;
355 }
356 len = urb->iso_frame_desc[i].actual_length / stride;
357 if (! len)
358 continue;
359 /* update the current pointer */
360 spin_lock_irqsave(&subs->lock, flags);
361 oldptr = subs->hwptr_done;
362 subs->hwptr_done += len;
363 if (subs->hwptr_done >= runtime->buffer_size)
364 subs->hwptr_done -= runtime->buffer_size;
365 subs->transfer_done += len;
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200366 if (subs->transfer_done >= runtime->period_size) {
367 subs->transfer_done -= runtime->period_size;
368 period_elapsed = 1;
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 spin_unlock_irqrestore(&subs->lock, flags);
371 /* copy a data chunk */
372 if (oldptr + len > runtime->buffer_size) {
373 unsigned int cnt = runtime->buffer_size - oldptr;
374 unsigned int blen = cnt * stride;
375 memcpy(runtime->dma_area + oldptr * stride, cp, blen);
376 memcpy(runtime->dma_area, cp + blen, len * stride - blen);
377 } else {
378 memcpy(runtime->dma_area + oldptr * stride, cp, len * stride);
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200381 if (period_elapsed)
382 snd_pcm_period_elapsed(subs->pcm_substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 0;
384}
385
Clemens Ladische4f8e652006-10-04 13:42:57 +0200386/*
387 * Process after capture complete when paused. Nothing to do.
388 */
389static int retire_paused_capture_urb(struct snd_usb_substream *subs,
390 struct snd_pcm_runtime *runtime,
391 struct urb *urb)
392{
393 return 0;
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397/*
398 * prepare urb for full speed playback sync pipe
399 *
400 * set up the offset and length to receive the current frequency.
401 */
402
Takashi Iwai86e07d32005-11-17 15:08:02 +0100403static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
404 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct urb *urb)
406{
John Daiker88518272006-12-28 13:55:05 +0100407 struct snd_urb_ctx *ctx = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200410 urb->iso_frame_desc[0].length = 3;
411 urb->iso_frame_desc[0].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return 0;
413}
414
415/*
416 * prepare urb for high speed playback sync pipe
417 *
418 * set up the offset and length to receive the current frequency.
419 */
420
Takashi Iwai86e07d32005-11-17 15:08:02 +0100421static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
422 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct urb *urb)
424{
John Daiker88518272006-12-28 13:55:05 +0100425 struct snd_urb_ctx *ctx = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200428 urb->iso_frame_desc[0].length = 4;
429 urb->iso_frame_desc[0].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431}
432
433/*
434 * process after full speed playback sync complete
435 *
436 * retrieve the current 10.14 frequency from pipe, and set it.
437 * the value is referred in prepare_playback_urb().
438 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100439static int retire_playback_sync_urb(struct snd_usb_substream *subs,
440 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct urb *urb)
442{
Clemens Ladischca810902005-05-02 08:55:54 +0200443 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unsigned long flags;
445
Clemens Ladischca810902005-05-02 08:55:54 +0200446 if (urb->iso_frame_desc[0].status == 0 &&
447 urb->iso_frame_desc[0].actual_length == 3) {
448 f = combine_triple((u8*)urb->transfer_buffer) << 2;
Clemens Ladisch50cdbf12005-05-13 07:44:13 +0200449 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
450 spin_lock_irqsave(&subs->lock, flags);
451 subs->freqm = f;
452 spin_unlock_irqrestore(&subs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 return 0;
457}
458
459/*
460 * process after high speed playback sync complete
461 *
462 * retrieve the current 12.13 frequency from pipe, and set it.
463 * the value is referred in prepare_playback_urb().
464 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100465static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
466 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct urb *urb)
468{
Clemens Ladischca810902005-05-02 08:55:54 +0200469 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 unsigned long flags;
471
Clemens Ladischca810902005-05-02 08:55:54 +0200472 if (urb->iso_frame_desc[0].status == 0 &&
473 urb->iso_frame_desc[0].actual_length == 4) {
474 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
Clemens Ladisch50cdbf12005-05-13 07:44:13 +0200475 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
476 spin_lock_irqsave(&subs->lock, flags);
477 subs->freqm = f;
478 spin_unlock_irqrestore(&subs->lock, flags);
479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 return 0;
483}
484
Clemens Ladischd5132022008-02-25 11:01:00 +0100485/*
Eran Tromer97c889a2008-09-26 01:07:03 -0400486 * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete
Clemens Ladischd5132022008-02-25 11:01:00 +0100487 *
488 * These devices return the number of samples per packet instead of the number
489 * of samples per microframe.
490 */
491static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs,
492 struct snd_pcm_runtime *runtime,
493 struct urb *urb)
494{
495 unsigned int f;
496 unsigned long flags;
497
498 if (urb->iso_frame_desc[0].status == 0 &&
499 urb->iso_frame_desc[0].actual_length == 4) {
500 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
501 f >>= subs->datainterval;
502 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
503 spin_lock_irqsave(&subs->lock, flags);
504 subs->freqm = f;
505 spin_unlock_irqrestore(&subs->lock, flags);
506 }
507 }
508
509 return 0;
510}
511
Clemens Ladisch9568f462006-01-12 08:19:21 +0100512/* determine the number of frames in the next packet */
513static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
514{
515 if (subs->fill_max)
516 return subs->maxframesize;
517 else {
518 subs->phase = (subs->phase & 0xffff)
519 + (subs->freqm << subs->datainterval);
520 return min(subs->phase >> 16, subs->maxframesize);
521 }
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/*
Clemens Ladische4f8e652006-10-04 13:42:57 +0200525 * Prepare urb for streaming before playback starts or when paused.
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100526 *
Clemens Ladischb7eb4a02009-01-26 08:08:34 +0100527 * We don't have any data, so we send silence.
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100528 */
Clemens Ladische4f8e652006-10-04 13:42:57 +0200529static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
530 struct snd_pcm_runtime *runtime,
531 struct urb *urb)
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100532{
Clemens Ladisch4b284922006-01-12 08:17:49 +0100533 unsigned int i, offs, counts;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100534 struct snd_urb_ctx *ctx = urb->context;
Clemens Ladisch4b284922006-01-12 08:17:49 +0100535 int stride = runtime->frame_bits >> 3;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100536
Clemens Ladisch4b284922006-01-12 08:17:49 +0100537 offs = 0;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100538 urb->dev = ctx->subs->dev;
Clemens Ladischb7eb4a02009-01-26 08:08:34 +0100539 for (i = 0; i < ctx->packets; ++i) {
Clemens Ladisch9568f462006-01-12 08:19:21 +0100540 counts = snd_usb_audio_next_packet_size(subs);
Clemens Ladisch4b284922006-01-12 08:17:49 +0100541 urb->iso_frame_desc[i].offset = offs * stride;
542 urb->iso_frame_desc[i].length = counts * stride;
543 offs += counts;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100544 }
Clemens Ladischb7eb4a02009-01-26 08:08:34 +0100545 urb->number_of_packets = ctx->packets;
Clemens Ladisch4b284922006-01-12 08:17:49 +0100546 urb->transfer_buffer_length = offs * stride;
547 memset(urb->transfer_buffer,
548 subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
549 offs * stride);
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100550 return 0;
551}
552
553/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * prepare urb for playback data pipe
555 *
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200556 * Since a URB can handle only a single linear buffer, we must use double
557 * buffering when the data to be transferred overflows the buffer boundary.
558 * To avoid inconsistencies when updating hwptr_done, we use double buffering
559 * for all URBs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100561static int prepare_playback_urb(struct snd_usb_substream *subs,
562 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct urb *urb)
564{
565 int i, stride, offs;
566 unsigned int counts;
567 unsigned long flags;
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200568 int period_elapsed = 0;
John Daiker88518272006-12-28 13:55:05 +0100569 struct snd_urb_ctx *ctx = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 stride = runtime->frame_bits >> 3;
572
573 offs = 0;
574 urb->dev = ctx->subs->dev; /* we need to set this at each time */
575 urb->number_of_packets = 0;
576 spin_lock_irqsave(&subs->lock, flags);
577 for (i = 0; i < ctx->packets; i++) {
Clemens Ladisch9568f462006-01-12 08:19:21 +0100578 counts = snd_usb_audio_next_packet_size(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* set up descriptor */
580 urb->iso_frame_desc[i].offset = offs * stride;
581 urb->iso_frame_desc[i].length = counts * stride;
582 offs += counts;
583 urb->number_of_packets++;
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200584 subs->transfer_done += counts;
585 if (subs->transfer_done >= runtime->period_size) {
586 subs->transfer_done -= runtime->period_size;
587 period_elapsed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (subs->fmt_type == USB_FORMAT_TYPE_II) {
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200589 if (subs->transfer_done > 0) {
590 /* FIXME: fill-max mode is not
591 * supported yet */
592 offs -= subs->transfer_done;
593 counts -= subs->transfer_done;
594 urb->iso_frame_desc[i].length =
595 counts * stride;
596 subs->transfer_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 i++;
599 if (i < ctx->packets) {
600 /* add a transfer delimiter */
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200601 urb->iso_frame_desc[i].offset =
602 offs * stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 urb->iso_frame_desc[i].length = 0;
604 urb->number_of_packets++;
605 }
Clemens Ladisch9624ea82005-08-15 08:25:24 +0200606 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Clemens Ladischa7d9c092009-04-03 09:48:26 +0200609 if (period_elapsed) /* finish at the period boundary */
Clemens Ladisch9624ea82005-08-15 08:25:24 +0200610 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200612 if (subs->hwptr_done + offs > runtime->buffer_size) {
613 /* err, the transferred area goes over buffer boundary. */
614 unsigned int len = runtime->buffer_size - subs->hwptr_done;
615 memcpy(urb->transfer_buffer,
616 runtime->dma_area + subs->hwptr_done * stride,
617 len * stride);
618 memcpy(urb->transfer_buffer + len * stride,
619 runtime->dma_area,
620 (offs - len) * stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 } else {
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200622 memcpy(urb->transfer_buffer,
623 runtime->dma_area + subs->hwptr_done * stride,
624 offs * stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200626 subs->hwptr_done += offs;
627 if (subs->hwptr_done >= runtime->buffer_size)
628 subs->hwptr_done -= runtime->buffer_size;
Takashi Iwaiae1ec5e2008-10-13 03:08:53 +0200629 runtime->delay += offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 spin_unlock_irqrestore(&subs->lock, flags);
631 urb->transfer_buffer_length = offs * stride;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100632 if (period_elapsed)
633 snd_pcm_period_elapsed(subs->pcm_substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return 0;
635}
636
637/*
638 * process after playback data complete
Takashi Iwaiae1ec5e2008-10-13 03:08:53 +0200639 * - decrease the delay count again
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100641static int retire_playback_urb(struct snd_usb_substream *subs,
642 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct urb *urb)
644{
Takashi Iwaiae1ec5e2008-10-13 03:08:53 +0200645 unsigned long flags;
646 int stride = runtime->frame_bits >> 3;
647 int processed = urb->transfer_buffer_length / stride;
648
649 spin_lock_irqsave(&subs->lock, flags);
650 if (processed > runtime->delay)
651 runtime->delay = 0;
652 else
653 runtime->delay -= processed;
654 spin_unlock_irqrestore(&subs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 0;
656}
657
658
659/*
660 */
661static struct snd_urb_ops audio_urb_ops[2] = {
662 {
Clemens Ladische4f8e652006-10-04 13:42:57 +0200663 .prepare = prepare_nodata_playback_urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 .retire = retire_playback_urb,
665 .prepare_sync = prepare_playback_sync_urb,
666 .retire_sync = retire_playback_sync_urb,
667 },
668 {
669 .prepare = prepare_capture_urb,
670 .retire = retire_capture_urb,
671 .prepare_sync = prepare_capture_sync_urb,
672 .retire_sync = retire_capture_sync_urb,
673 },
674};
675
676static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
677 {
Clemens Ladische4f8e652006-10-04 13:42:57 +0200678 .prepare = prepare_nodata_playback_urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 .retire = retire_playback_urb,
680 .prepare_sync = prepare_playback_sync_urb_hs,
681 .retire_sync = retire_playback_sync_urb_hs,
682 },
683 {
684 .prepare = prepare_capture_urb,
685 .retire = retire_capture_urb,
686 .prepare_sync = prepare_capture_sync_urb_hs,
687 .retire_sync = retire_capture_sync_urb,
688 },
689};
690
691/*
692 * complete callback from data urb
693 */
David Howells7d12e782006-10-05 14:55:46 +0100694static void snd_complete_urb(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
John Daiker88518272006-12-28 13:55:05 +0100696 struct snd_urb_ctx *ctx = urb->context;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100697 struct snd_usb_substream *subs = ctx->subs;
698 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int err = 0;
700
701 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
Pavel Machek07f51a72008-04-14 13:15:56 +0200702 !subs->running || /* can be stopped during retire callback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
704 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
705 clear_bit(ctx->index, &subs->active_mask);
706 if (err < 0) {
707 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
708 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
709 }
710 }
711}
712
713
714/*
715 * complete callback from sync urb
716 */
David Howells7d12e782006-10-05 14:55:46 +0100717static void snd_complete_sync_urb(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
John Daiker88518272006-12-28 13:55:05 +0100719 struct snd_urb_ctx *ctx = urb->context;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100720 struct snd_usb_substream *subs = ctx->subs;
721 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int err = 0;
723
724 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
Pavel Machek07f51a72008-04-14 13:15:56 +0200725 !subs->running || /* can be stopped during retire callback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
727 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
728 clear_bit(ctx->index + 16, &subs->active_mask);
729 if (err < 0) {
730 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
731 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
732 }
733 }
734}
735
736
737/*
738 * unlink active urbs.
739 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100740static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 unsigned int i;
743 int async;
744
745 subs->running = 0;
746
747 if (!force && subs->stream->chip->shutdown) /* to be sure... */
748 return -EBADFD;
749
750 async = !can_sleep && async_unlink;
751
Pavel Machek07f51a72008-04-14 13:15:56 +0200752 if (!async && in_interrupt())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return 0;
754
755 for (i = 0; i < subs->nurbs; i++) {
756 if (test_bit(i, &subs->active_mask)) {
Pavel Machek07f51a72008-04-14 13:15:56 +0200757 if (!test_and_set_bit(i, &subs->unlink_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct urb *u = subs->dataurb[i].urb;
Alan Sternb375a042005-07-29 16:11:07 -0400759 if (async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 usb_unlink_urb(u);
Alan Sternb375a042005-07-29 16:11:07 -0400761 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 usb_kill_urb(u);
763 }
764 }
765 }
766 if (subs->syncpipe) {
767 for (i = 0; i < SYNC_URBS; i++) {
768 if (test_bit(i+16, &subs->active_mask)) {
Pavel Machek07f51a72008-04-14 13:15:56 +0200769 if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 struct urb *u = subs->syncurb[i].urb;
Alan Sternb375a042005-07-29 16:11:07 -0400771 if (async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 usb_unlink_urb(u);
Alan Sternb375a042005-07-29 16:11:07 -0400773 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 usb_kill_urb(u);
775 }
776 }
777 }
778 }
779 return 0;
780}
781
782
Clemens Ladisch32e19e82006-03-09 07:58:39 +0100783static const char *usb_error_string(int err)
784{
785 switch (err) {
786 case -ENODEV:
787 return "no device";
788 case -ENOENT:
789 return "endpoint not enabled";
790 case -EPIPE:
791 return "endpoint stalled";
792 case -ENOSPC:
793 return "not enough bandwidth";
794 case -ESHUTDOWN:
795 return "device disabled";
796 case -EHOSTUNREACH:
797 return "device suspended";
798 case -EINVAL:
799 case -EAGAIN:
800 case -EFBIG:
801 case -EMSGSIZE:
802 return "internal error";
803 default:
804 return "unknown error";
805 }
806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
809 * set up and start data/sync urbs
810 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100811static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 unsigned int i;
814 int err;
815
816 if (subs->stream->chip->shutdown)
817 return -EBADFD;
818
819 for (i = 0; i < subs->nurbs; i++) {
Takashi Iwai5e246b82008-08-08 17:12:47 +0200820 if (snd_BUG_ON(!subs->dataurb[i].urb))
821 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
823 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
824 goto __error;
825 }
826 }
827 if (subs->syncpipe) {
828 for (i = 0; i < SYNC_URBS; i++) {
Takashi Iwai5e246b82008-08-08 17:12:47 +0200829 if (snd_BUG_ON(!subs->syncurb[i].urb))
830 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
832 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
833 goto __error;
834 }
835 }
836 }
837
838 subs->active_mask = 0;
839 subs->unlink_mask = 0;
840 subs->running = 1;
841 for (i = 0; i < subs->nurbs; i++) {
Clemens Ladisch32e19e82006-03-09 07:58:39 +0100842 err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
843 if (err < 0) {
844 snd_printk(KERN_ERR "cannot submit datapipe "
845 "for urb %d, error %d: %s\n",
846 i, err, usb_error_string(err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto __error;
848 }
849 set_bit(i, &subs->active_mask);
850 }
851 if (subs->syncpipe) {
852 for (i = 0; i < SYNC_URBS; i++) {
Clemens Ladisch32e19e82006-03-09 07:58:39 +0100853 err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
854 if (err < 0) {
855 snd_printk(KERN_ERR "cannot submit syncpipe "
856 "for urb %d, error %d: %s\n",
857 i, err, usb_error_string(err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 goto __error;
859 }
860 set_bit(i + 16, &subs->active_mask);
861 }
862 }
863 return 0;
864
865 __error:
866 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
867 deactivate_urbs(subs, 0, 0);
868 return -EPIPE;
869}
870
871
872/*
873 * wait until all urbs are processed.
874 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100875static int wait_clear_urbs(struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Nishanth Aravamudanb27c1872005-07-09 10:54:37 +0200877 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 unsigned int i;
879 int alive;
880
881 do {
882 alive = 0;
883 for (i = 0; i < subs->nurbs; i++) {
884 if (test_bit(i, &subs->active_mask))
885 alive++;
886 }
887 if (subs->syncpipe) {
888 for (i = 0; i < SYNC_URBS; i++) {
889 if (test_bit(i + 16, &subs->active_mask))
890 alive++;
891 }
892 }
893 if (! alive)
894 break;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200895 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanb27c1872005-07-09 10:54:37 +0200896 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (alive)
898 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
899 return 0;
900}
901
902
903/*
904 * return the current pcm pointer. just return the hwptr_done value.
905 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100906static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100908 struct snd_usb_substream *subs;
Clemens Ladischdaa150e2005-08-15 08:25:50 +0200909 snd_pcm_uframes_t hwptr_done;
910
Takashi Iwai86e07d32005-11-17 15:08:02 +0100911 subs = (struct snd_usb_substream *)substream->runtime->private_data;
Clemens Ladischdaa150e2005-08-15 08:25:50 +0200912 spin_lock(&subs->lock);
913 hwptr_done = subs->hwptr_done;
914 spin_unlock(&subs->lock);
915 return hwptr_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918
919/*
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100920 * start/stop playback substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100922static int snd_usb_pcm_playback_trigger(struct snd_pcm_substream *substream,
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100923 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100925 struct snd_usb_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 switch (cmd) {
928 case SNDRV_PCM_TRIGGER_START:
Clemens Ladische4f8e652006-10-04 13:42:57 +0200929 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100930 subs->ops.prepare = prepare_playback_urb;
931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 case SNDRV_PCM_TRIGGER_STOP:
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100933 return deactivate_urbs(subs, 0, 0);
Clemens Ladische4f8e652006-10-04 13:42:57 +0200934 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
935 subs->ops.prepare = prepare_nodata_playback_urb;
936 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 default:
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100938 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100940}
941
942/*
943 * start/stop capture substream
944 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100945static int snd_usb_pcm_capture_trigger(struct snd_pcm_substream *substream,
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100946 int cmd)
947{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100948 struct snd_usb_substream *subs = substream->runtime->private_data;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100949
950 switch (cmd) {
951 case SNDRV_PCM_TRIGGER_START:
Clemens Ladische4f8e652006-10-04 13:42:57 +0200952 subs->ops.retire = retire_capture_urb;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100953 return start_urbs(subs, substream->runtime);
954 case SNDRV_PCM_TRIGGER_STOP:
955 return deactivate_urbs(subs, 0, 0);
Clemens Ladische4f8e652006-10-04 13:42:57 +0200956 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
957 subs->ops.retire = retire_paused_capture_urb;
958 return 0;
959 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
960 subs->ops.retire = retire_capture_urb;
961 return 0;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100962 default:
963 return -EINVAL;
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967
968/*
969 * release a urb data
970 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100971static void release_urb_ctx(struct snd_urb_ctx *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 if (u->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200974 if (u->buffer_size)
975 usb_buffer_free(u->subs->dev, u->buffer_size,
976 u->urb->transfer_buffer,
977 u->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 usb_free_urb(u->urb);
979 u->urb = NULL;
980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983/*
984 * release a substream
985 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100986static void release_substream_urbs(struct snd_usb_substream *subs, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 int i;
989
990 /* stop urbs (to be sure) */
991 deactivate_urbs(subs, force, 1);
992 wait_clear_urbs(subs);
993
994 for (i = 0; i < MAX_URBS; i++)
995 release_urb_ctx(&subs->dataurb[i]);
996 for (i = 0; i < SYNC_URBS; i++)
997 release_urb_ctx(&subs->syncurb[i]);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200998 usb_buffer_free(subs->dev, SYNC_URBS * 4,
999 subs->syncbuf, subs->sync_dma);
1000 subs->syncbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 subs->nurbs = 0;
1002}
1003
1004/*
1005 * initialize a substream for plaback/capture
1006 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001007static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 unsigned int rate, unsigned int frame_bits)
1009{
Clemens Ladisch160389c2009-01-26 08:10:19 +01001010 unsigned int maxsize, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
Clemens Ladisch160389c2009-01-26 08:10:19 +01001012 unsigned int urb_packs, total_packs, packs_per_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /* calculate the frequency in 16.16 format */
1015 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
1016 subs->freqn = get_usb_full_speed_rate(rate);
1017 else
1018 subs->freqn = get_usb_high_speed_rate(rate);
1019 subs->freqm = subs->freqn;
Clemens Ladisch573567e2005-06-27 08:17:30 +02001020 /* calculate max. frequency */
1021 if (subs->maxpacksize) {
1022 /* whatever fits into a max. size packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 maxsize = subs->maxpacksize;
Clemens Ladisch573567e2005-06-27 08:17:30 +02001024 subs->freqmax = (maxsize / (frame_bits >> 3))
1025 << (16 - subs->datainterval);
1026 } else {
1027 /* no max. packet size: just take 25% higher than nominal */
1028 subs->freqmax = subs->freqn + (subs->freqn >> 2);
1029 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
1030 >> (16 - subs->datainterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Clemens Ladisch573567e2005-06-27 08:17:30 +02001032 subs->phase = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 if (subs->fill_max)
1035 subs->curpacksize = subs->maxpacksize;
1036 else
1037 subs->curpacksize = maxsize;
1038
Clemens Ladischa93bf992005-08-12 15:19:39 +02001039 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
1040 packs_per_ms = 8 >> subs->datainterval;
1041 else
1042 packs_per_ms = 1;
1043
Clemens Ladisch71d848c2005-08-12 15:18:00 +02001044 if (is_playback) {
Clemens Ladischf3990e62009-02-20 09:32:40 +01001045 urb_packs = max(nrpacks, 1);
Clemens Ladisch71d848c2005-08-12 15:18:00 +02001046 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
1047 } else
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001048 urb_packs = 1;
Clemens Ladischa93bf992005-08-12 15:19:39 +02001049 urb_packs *= packs_per_ms;
Clemens Ladisch765e8db2009-08-10 10:07:35 +02001050 if (subs->syncpipe)
Takashi Iwaif1e6d3c2009-08-11 08:15:04 +02001051 urb_packs = min(urb_packs, 1U << subs->syncinterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* decide how many packets to be used */
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001054 if (is_playback) {
Clemens Ladisch4d788e02009-01-26 08:09:28 +01001055 unsigned int minsize, maxpacks;
Clemens Ladischd6db3922005-08-12 08:28:27 +02001056 /* determine how small a packet can be */
1057 minsize = (subs->freqn >> (16 - subs->datainterval))
1058 * (frame_bits >> 3);
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +02001059 /* with sync from device, assume it can be 12% lower */
Clemens Ladischd6db3922005-08-12 08:28:27 +02001060 if (subs->syncpipe)
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +02001061 minsize -= minsize >> 3;
Clemens Ladischd6db3922005-08-12 08:28:27 +02001062 minsize = max(minsize, 1u);
1063 total_packs = (period_bytes + minsize - 1) / minsize;
Clemens Ladischa93bf992005-08-12 15:19:39 +02001064 /* we need at least two URBs for queueing */
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001065 if (total_packs < 2) {
1066 total_packs = 2;
Clemens Ladischf3990e62009-02-20 09:32:40 +01001067 } else {
Clemens Ladisch4d788e02009-01-26 08:09:28 +01001068 /* and we don't want too long a queue either */
Clemens Ladischb1c86bb2009-03-02 12:06:28 +01001069 maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
1070 total_packs = min(total_packs, maxpacks);
Clemens Ladisch4d788e02009-01-26 08:09:28 +01001071 }
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001072 } else {
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001073 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
1074 urb_packs >>= 1;
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001075 total_packs = MAX_URBS * urb_packs;
1076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
1078 if (subs->nurbs > MAX_URBS) {
1079 /* too much... */
1080 subs->nurbs = MAX_URBS;
1081 total_packs = MAX_URBS * urb_packs;
Clemens Ladisch160389c2009-01-26 08:10:19 +01001082 } else if (subs->nurbs < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* too little - we need at least two packets
1084 * to ensure contiguous playback/capture
1085 */
1086 subs->nurbs = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
1089 /* allocate and initialize data urbs */
1090 for (i = 0; i < subs->nurbs; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001091 struct snd_urb_ctx *u = &subs->dataurb[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 u->index = i;
1093 u->subs = subs;
Clemens Ladisch160389c2009-01-26 08:10:19 +01001094 u->packets = (i + 1) * total_packs / subs->nurbs
1095 - i * total_packs / subs->nurbs;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001096 u->buffer_size = maxsize * u->packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (subs->fmt_type == USB_FORMAT_TYPE_II)
1098 u->packets++; /* for transfer delimiter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
Pavel Machek07f51a72008-04-14 13:15:56 +02001100 if (!u->urb)
Clemens Ladisch55851f72005-08-15 08:34:16 +02001101 goto out_of_memory;
1102 u->urb->transfer_buffer =
1103 usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL,
1104 &u->urb->transfer_dma);
Pavel Machek07f51a72008-04-14 13:15:56 +02001105 if (!u->urb->transfer_buffer)
Clemens Ladisch55851f72005-08-15 08:34:16 +02001106 goto out_of_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 u->urb->pipe = subs->datapipe;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001108 u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Clemens Ladisch573567e2005-06-27 08:17:30 +02001109 u->urb->interval = 1 << subs->datainterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 u->urb->context = u;
Clemens Ladisch3527a002005-09-26 10:03:09 +02001111 u->urb->complete = snd_complete_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113
1114 if (subs->syncpipe) {
1115 /* allocate and initialize sync urbs */
Clemens Ladisch55851f72005-08-15 08:34:16 +02001116 subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4,
1117 GFP_KERNEL, &subs->sync_dma);
Pavel Machek07f51a72008-04-14 13:15:56 +02001118 if (!subs->syncbuf)
Clemens Ladisch55851f72005-08-15 08:34:16 +02001119 goto out_of_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 for (i = 0; i < SYNC_URBS; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001121 struct snd_urb_ctx *u = &subs->syncurb[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 u->index = i;
1123 u->subs = subs;
Clemens Ladischca810902005-05-02 08:55:54 +02001124 u->packets = 1;
1125 u->urb = usb_alloc_urb(1, GFP_KERNEL);
Pavel Machek07f51a72008-04-14 13:15:56 +02001126 if (!u->urb)
Clemens Ladisch55851f72005-08-15 08:34:16 +02001127 goto out_of_memory;
Clemens Ladischca810902005-05-02 08:55:54 +02001128 u->urb->transfer_buffer = subs->syncbuf + i * 4;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001129 u->urb->transfer_dma = subs->sync_dma + i * 4;
Clemens Ladischca810902005-05-02 08:55:54 +02001130 u->urb->transfer_buffer_length = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 u->urb->pipe = subs->syncpipe;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001132 u->urb->transfer_flags = URB_ISO_ASAP |
1133 URB_NO_TRANSFER_DMA_MAP;
Clemens Ladischca810902005-05-02 08:55:54 +02001134 u->urb->number_of_packets = 1;
Clemens Ladisch1149a642005-05-02 08:53:46 +02001135 u->urb->interval = 1 << subs->syncinterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 u->urb->context = u;
Clemens Ladisch3527a002005-09-26 10:03:09 +02001137 u->urb->complete = snd_complete_sync_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139 }
1140 return 0;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001141
1142out_of_memory:
1143 release_substream_urbs(subs, 0);
1144 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
1147
1148/*
1149 * find a matching audio format
1150 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001151static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 unsigned int rate, unsigned int channels)
1153{
1154 struct list_head *p;
1155 struct audioformat *found = NULL;
1156 int cur_attr = 0, attr;
1157
1158 list_for_each(p, &subs->fmt_list) {
1159 struct audioformat *fp;
1160 fp = list_entry(p, struct audioformat, list);
1161 if (fp->format != format || fp->channels != channels)
1162 continue;
1163 if (rate < fp->rate_min || rate > fp->rate_max)
1164 continue;
1165 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
1166 unsigned int i;
1167 for (i = 0; i < fp->nr_rates; i++)
1168 if (fp->rate_table[i] == rate)
1169 break;
1170 if (i >= fp->nr_rates)
1171 continue;
1172 }
1173 attr = fp->ep_attr & EP_ATTR_MASK;
1174 if (! found) {
1175 found = fp;
1176 cur_attr = attr;
1177 continue;
1178 }
1179 /* avoid async out and adaptive in if the other method
1180 * supports the same format.
1181 * this is a workaround for the case like
1182 * M-audio audiophile USB.
1183 */
1184 if (attr != cur_attr) {
1185 if ((attr == EP_ATTR_ASYNC &&
1186 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1187 (attr == EP_ATTR_ADAPTIVE &&
1188 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
1189 continue;
1190 if ((cur_attr == EP_ATTR_ASYNC &&
1191 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1192 (cur_attr == EP_ATTR_ADAPTIVE &&
1193 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
1194 found = fp;
1195 cur_attr = attr;
1196 continue;
1197 }
1198 }
1199 /* find the format with the largest max. packet size */
1200 if (fp->maxpacksize > found->maxpacksize) {
1201 found = fp;
1202 cur_attr = attr;
1203 }
1204 }
1205 return found;
1206}
1207
1208
1209/*
1210 * initialize the picth control and sample rate
1211 */
1212static int init_usb_pitch(struct usb_device *dev, int iface,
1213 struct usb_host_interface *alts,
1214 struct audioformat *fmt)
1215{
1216 unsigned int ep;
1217 unsigned char data[1];
1218 int err;
1219
1220 ep = get_endpoint(alts, 0)->bEndpointAddress;
1221 /* if endpoint has pitch control, enable it */
1222 if (fmt->attributes & EP_CS_ATTR_PITCH_CONTROL) {
1223 data[0] = 1;
1224 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1225 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1226 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1227 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
1228 dev->devnum, iface, ep);
1229 return err;
1230 }
1231 }
1232 return 0;
1233}
1234
1235static int init_usb_sample_rate(struct usb_device *dev, int iface,
1236 struct usb_host_interface *alts,
1237 struct audioformat *fmt, int rate)
1238{
1239 unsigned int ep;
1240 unsigned char data[3];
1241 int err;
1242
1243 ep = get_endpoint(alts, 0)->bEndpointAddress;
1244 /* if endpoint has sampling rate control, set it */
1245 if (fmt->attributes & EP_CS_ATTR_SAMPLE_RATE) {
1246 int crate;
1247 data[0] = rate;
1248 data[1] = rate >> 8;
1249 data[2] = rate >> 16;
1250 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1251 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1252 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
Andreas Bergmeierb9d710b2009-01-24 12:15:14 +01001253 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 dev->devnum, iface, fmt->altsetting, rate, ep);
1255 return err;
1256 }
1257 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR,
1258 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1259 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
Andreas Bergmeierb9d710b2009-01-24 12:15:14 +01001260 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 dev->devnum, iface, fmt->altsetting, ep);
1262 return 0; /* some devices don't support reading */
1263 }
1264 crate = data[0] | (data[1] << 8) | (data[2] << 16);
1265 if (crate != rate) {
1266 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
1267 // runtime->rate = crate;
1268 }
1269 }
1270 return 0;
1271}
1272
1273/*
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001274 * For E-Mu 0404USB/0202USB/TrackerPre sample rate should be set for device,
1275 * not for interface.
1276 */
1277static void set_format_emu_quirk(struct snd_usb_substream *subs,
1278 struct audioformat *fmt)
1279{
1280 unsigned char emu_samplerate_id = 0;
1281
1282 /* When capture is active
1283 * sample rate shouldn't be changed
1284 * by playback substream
1285 */
1286 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1287 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
1288 return;
1289 }
1290
1291 switch (fmt->rate_min) {
1292 case 48000:
1293 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1294 break;
1295 case 88200:
1296 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1297 break;
1298 case 96000:
1299 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1300 break;
1301 case 176400:
1302 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1303 break;
1304 case 192000:
1305 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1306 break;
1307 default:
1308 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1309 break;
1310 }
1311 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
1312}
1313
1314/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 * find a matching format and set up the interface
1316 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001317static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 struct usb_device *dev = subs->dev;
1320 struct usb_host_interface *alts;
1321 struct usb_interface_descriptor *altsd;
1322 struct usb_interface *iface;
1323 unsigned int ep, attr;
1324 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1325 int err;
1326
1327 iface = usb_ifnum_to_if(dev, fmt->iface);
Takashi Iwai5e246b82008-08-08 17:12:47 +02001328 if (WARN_ON(!iface))
1329 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 alts = &iface->altsetting[fmt->altset_idx];
1331 altsd = get_iface_desc(alts);
Takashi Iwai5e246b82008-08-08 17:12:47 +02001332 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
1333 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 if (fmt == subs->cur_audiofmt)
1336 return 0;
1337
1338 /* close the old interface */
1339 if (subs->interface >= 0 && subs->interface != fmt->iface) {
Oliver Neukum5149fe22007-08-31 12:15:27 +02001340 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
1341 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
1342 dev->devnum, fmt->iface, fmt->altsetting);
1343 return -EIO;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 subs->interface = -1;
1346 subs->format = 0;
1347 }
1348
1349 /* set interface */
1350 if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
1351 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
1352 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
1353 dev->devnum, fmt->iface, fmt->altsetting);
1354 return -EIO;
1355 }
1356 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
1357 subs->interface = fmt->iface;
1358 subs->format = fmt->altset_idx;
1359 }
1360
1361 /* create a data pipe */
1362 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
1363 if (is_playback)
1364 subs->datapipe = usb_sndisocpipe(dev, ep);
1365 else
1366 subs->datapipe = usb_rcvisocpipe(dev, ep);
Clemens Ladisch744b89e2009-04-03 09:45:01 +02001367 subs->datainterval = fmt->datainterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 subs->syncpipe = subs->syncinterval = 0;
1369 subs->maxpacksize = fmt->maxpacksize;
1370 subs->fill_max = 0;
1371
1372 /* we need a sync pipe in async OUT or adaptive IN mode */
1373 /* check the number of EP, since some devices have broken
1374 * descriptors which fool us. if it has only one EP,
1375 * assume it as adaptive-out or sync-in.
1376 */
1377 attr = fmt->ep_attr & EP_ATTR_MASK;
1378 if (((is_playback && attr == EP_ATTR_ASYNC) ||
1379 (! is_playback && attr == EP_ATTR_ADAPTIVE)) &&
1380 altsd->bNumEndpoints >= 2) {
1381 /* check sync-pipe endpoint */
1382 /* ... and check descriptor size before accessing bSynchAddress
1383 because there is a version of the SB Audigy 2 NX firmware lacking
1384 the audio fields in the endpoint descriptors */
1385 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
1386 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1387 get_endpoint(alts, 1)->bSynchAddress != 0)) {
1388 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1389 dev->devnum, fmt->iface, fmt->altsetting);
1390 return -EINVAL;
1391 }
1392 ep = get_endpoint(alts, 1)->bEndpointAddress;
1393 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1394 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
1395 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
1396 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1397 dev->devnum, fmt->iface, fmt->altsetting);
1398 return -EINVAL;
1399 }
1400 ep &= USB_ENDPOINT_NUMBER_MASK;
1401 if (is_playback)
1402 subs->syncpipe = usb_rcvisocpipe(dev, ep);
1403 else
1404 subs->syncpipe = usb_sndisocpipe(dev, ep);
Clemens Ladisch1149a642005-05-02 08:53:46 +02001405 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1406 get_endpoint(alts, 1)->bRefresh >= 1 &&
1407 get_endpoint(alts, 1)->bRefresh <= 9)
1408 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
Clemens Ladisch604cf492005-05-17 09:15:27 +02001409 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
Clemens Ladisch1149a642005-05-02 08:53:46 +02001410 subs->syncinterval = 1;
Clemens Ladisch604cf492005-05-17 09:15:27 +02001411 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
1412 get_endpoint(alts, 1)->bInterval <= 16)
1413 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
1414 else
1415 subs->syncinterval = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
1418 /* always fill max packet size */
1419 if (fmt->attributes & EP_CS_ATTR_FILL_MAX)
1420 subs->fill_max = 1;
1421
1422 if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
1423 return err;
1424
1425 subs->cur_audiofmt = fmt;
1426
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001427 switch (subs->stream->chip->usb_id) {
1428 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1429 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1430 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
1431 set_format_emu_quirk(subs, fmt);
1432 break;
1433 }
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435#if 0
Takashi Iwai54530bd2009-02-05 15:55:18 +01001436 printk(KERN_DEBUG
1437 "setting done: format = %d, rate = %d..%d, channels = %d\n",
Pavel Machek2a56f512008-04-14 13:14:22 +02001438 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
Takashi Iwai54530bd2009-02-05 15:55:18 +01001439 printk(KERN_DEBUG
1440 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 subs->datapipe, subs->syncpipe);
1442#endif
1443
1444 return 0;
1445}
1446
1447/*
1448 * hw_params callback
1449 *
1450 * allocate a buffer and set the given audio format.
1451 *
1452 * so far we use a physically linear buffer although packetize transfer
1453 * doesn't need a continuous area.
1454 * if sg buffer is supported on the later version of alsa, we'll follow
1455 * that.
1456 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001457static int snd_usb_hw_params(struct snd_pcm_substream *substream,
1458 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
John Daiker88518272006-12-28 13:55:05 +01001460 struct snd_usb_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 struct audioformat *fmt;
1462 unsigned int channels, rate, format;
1463 int ret, changed;
1464
Clemens Ladischc55675e2009-12-18 09:31:31 +01001465 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
1466 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (ret < 0)
1468 return ret;
1469
1470 format = params_format(hw_params);
1471 rate = params_rate(hw_params);
1472 channels = params_channels(hw_params);
1473 fmt = find_format(subs, format, rate, channels);
Pavel Machek07f51a72008-04-14 13:15:56 +02001474 if (!fmt) {
Andreas Bergmeierb9d710b2009-01-24 12:15:14 +01001475 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001476 format, rate, channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return -EINVAL;
1478 }
1479
1480 changed = subs->cur_audiofmt != fmt ||
1481 subs->period_bytes != params_period_bytes(hw_params) ||
1482 subs->cur_rate != rate;
1483 if ((ret = set_format(subs, fmt)) < 0)
1484 return ret;
1485
1486 if (subs->cur_rate != rate) {
1487 struct usb_host_interface *alts;
1488 struct usb_interface *iface;
1489 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
1490 alts = &iface->altsetting[fmt->altset_idx];
1491 ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
1492 if (ret < 0)
1493 return ret;
1494 subs->cur_rate = rate;
1495 }
1496
1497 if (changed) {
1498 /* format changed */
1499 release_substream_urbs(subs, 0);
1500 /* influenced: period_bytes, channels, rate, format, */
1501 ret = init_substream_urbs(subs, params_period_bytes(hw_params),
1502 params_rate(hw_params),
1503 snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
1504 }
1505
1506 return ret;
1507}
1508
1509/*
1510 * hw_free callback
1511 *
1512 * reset the audio format and release the buffer
1513 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001514static int snd_usb_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
John Daiker88518272006-12-28 13:55:05 +01001516 struct snd_usb_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 subs->cur_audiofmt = NULL;
1519 subs->cur_rate = 0;
1520 subs->period_bytes = 0;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001521 if (!subs->stream->chip->shutdown)
1522 release_substream_urbs(subs, 0);
Clemens Ladischc55675e2009-12-18 09:31:31 +01001523 return snd_pcm_lib_free_vmalloc_buffer(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526/*
1527 * prepare callback
1528 *
1529 * only a few subtle things...
1530 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001531static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001533 struct snd_pcm_runtime *runtime = substream->runtime;
1534 struct snd_usb_substream *subs = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (! subs->cur_audiofmt) {
1537 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
1538 return -ENXIO;
1539 }
1540
1541 /* some unit conversions in runtime */
1542 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
1543 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
1544
1545 /* reset the pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 subs->hwptr_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 subs->transfer_done = 0;
1548 subs->phase = 0;
Takashi Iwaiae1ec5e2008-10-13 03:08:53 +02001549 runtime->delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /* clear urbs (to be sure) */
1552 deactivate_urbs(subs, 0, 1);
1553 wait_clear_urbs(subs);
1554
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001555 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1556 * updates for all URBs would happen at the same time when starting */
1557 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
Clemens Ladische4f8e652006-10-04 13:42:57 +02001558 subs->ops.prepare = prepare_nodata_playback_urb;
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001559 return start_urbs(subs, runtime);
1560 } else
1561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Clemens Ladisch1700f302006-10-04 13:41:25 +02001564static struct snd_pcm_hardware snd_usb_hardware =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Clemens Ladisch49045d32005-09-05 10:31:05 +02001566 .info = SNDRV_PCM_INFO_MMAP |
1567 SNDRV_PCM_INFO_MMAP_VALID |
1568 SNDRV_PCM_INFO_BATCH |
1569 SNDRV_PCM_INFO_INTERLEAVED |
Clemens Ladische4f8e652006-10-04 13:42:57 +02001570 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1571 SNDRV_PCM_INFO_PAUSE,
Clemens Ladischd31cbbf2005-09-26 09:59:57 +02001572 .buffer_bytes_max = 1024 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 .period_bytes_min = 64,
Clemens Ladischd31cbbf2005-09-26 09:59:57 +02001574 .period_bytes_max = 512 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 .periods_min = 2,
1576 .periods_max = 1024,
1577};
1578
1579/*
1580 * h/w constraints
1581 */
1582
1583#ifdef HW_CONST_DEBUG
1584#define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
1585#else
1586#define hwc_debug(fmt, args...) /**/
1587#endif
1588
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001589static int hw_check_valid_format(struct snd_usb_substream *subs,
1590 struct snd_pcm_hw_params *params,
1591 struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001593 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1594 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1595 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001596 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1597 unsigned int ptime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 /* check the format */
Pavel Machek07f51a72008-04-14 13:15:56 +02001600 if (!snd_mask_test(fmts, fp->format)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 hwc_debug(" > check: no supported format %d\n", fp->format);
1602 return 0;
1603 }
1604 /* check the channels */
1605 if (fp->channels < ct->min || fp->channels > ct->max) {
1606 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1607 return 0;
1608 }
1609 /* check the rate is within the range */
1610 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1611 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1612 return 0;
1613 }
1614 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1615 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1616 return 0;
1617 }
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001618 /* check whether the period time is >= the data packet interval */
1619 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
1620 ptime = 125 * (1 << fp->datainterval);
1621 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
1622 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
1623 return 0;
1624 }
1625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 1;
1627}
1628
Takashi Iwai86e07d32005-11-17 15:08:02 +01001629static int hw_rule_rate(struct snd_pcm_hw_params *params,
1630 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001632 struct snd_usb_substream *subs = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001634 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 unsigned int rmin, rmax;
1636 int changed;
1637
1638 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1639 changed = 0;
1640 rmin = rmax = 0;
1641 list_for_each(p, &subs->fmt_list) {
1642 struct audioformat *fp;
1643 fp = list_entry(p, struct audioformat, list);
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001644 if (!hw_check_valid_format(subs, params, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 continue;
1646 if (changed++) {
1647 if (rmin > fp->rate_min)
1648 rmin = fp->rate_min;
1649 if (rmax < fp->rate_max)
1650 rmax = fp->rate_max;
1651 } else {
1652 rmin = fp->rate_min;
1653 rmax = fp->rate_max;
1654 }
1655 }
1656
Pavel Machek07f51a72008-04-14 13:15:56 +02001657 if (!changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 hwc_debug(" --> get empty\n");
1659 it->empty = 1;
1660 return -EINVAL;
1661 }
1662
1663 changed = 0;
1664 if (it->min < rmin) {
1665 it->min = rmin;
1666 it->openmin = 0;
1667 changed = 1;
1668 }
1669 if (it->max > rmax) {
1670 it->max = rmax;
1671 it->openmax = 0;
1672 changed = 1;
1673 }
1674 if (snd_interval_checkempty(it)) {
1675 it->empty = 1;
1676 return -EINVAL;
1677 }
1678 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1679 return changed;
1680}
1681
1682
Takashi Iwai86e07d32005-11-17 15:08:02 +01001683static int hw_rule_channels(struct snd_pcm_hw_params *params,
1684 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001686 struct snd_usb_substream *subs = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001688 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 unsigned int rmin, rmax;
1690 int changed;
1691
1692 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1693 changed = 0;
1694 rmin = rmax = 0;
1695 list_for_each(p, &subs->fmt_list) {
1696 struct audioformat *fp;
1697 fp = list_entry(p, struct audioformat, list);
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001698 if (!hw_check_valid_format(subs, params, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 continue;
1700 if (changed++) {
1701 if (rmin > fp->channels)
1702 rmin = fp->channels;
1703 if (rmax < fp->channels)
1704 rmax = fp->channels;
1705 } else {
1706 rmin = fp->channels;
1707 rmax = fp->channels;
1708 }
1709 }
1710
Pavel Machek07f51a72008-04-14 13:15:56 +02001711 if (!changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 hwc_debug(" --> get empty\n");
1713 it->empty = 1;
1714 return -EINVAL;
1715 }
1716
1717 changed = 0;
1718 if (it->min < rmin) {
1719 it->min = rmin;
1720 it->openmin = 0;
1721 changed = 1;
1722 }
1723 if (it->max > rmax) {
1724 it->max = rmax;
1725 it->openmax = 0;
1726 changed = 1;
1727 }
1728 if (snd_interval_checkempty(it)) {
1729 it->empty = 1;
1730 return -EINVAL;
1731 }
1732 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1733 return changed;
1734}
1735
Takashi Iwai86e07d32005-11-17 15:08:02 +01001736static int hw_rule_format(struct snd_pcm_hw_params *params,
1737 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001739 struct snd_usb_substream *subs = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001741 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 u64 fbits;
1743 u32 oldbits[2];
1744 int changed;
1745
1746 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1747 fbits = 0;
1748 list_for_each(p, &subs->fmt_list) {
1749 struct audioformat *fp;
1750 fp = list_entry(p, struct audioformat, list);
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001751 if (!hw_check_valid_format(subs, params, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 continue;
1753 fbits |= (1ULL << fp->format);
1754 }
1755
1756 oldbits[0] = fmt->bits[0];
1757 oldbits[1] = fmt->bits[1];
1758 fmt->bits[0] &= (u32)fbits;
1759 fmt->bits[1] &= (u32)(fbits >> 32);
Pavel Machek07f51a72008-04-14 13:15:56 +02001760 if (!fmt->bits[0] && !fmt->bits[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 hwc_debug(" --> get empty\n");
1762 return -EINVAL;
1763 }
1764 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1765 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1766 return changed;
1767}
1768
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001769static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1770 struct snd_pcm_hw_rule *rule)
1771{
1772 struct snd_usb_substream *subs = rule->private;
1773 struct audioformat *fp;
1774 struct snd_interval *it;
1775 unsigned char min_datainterval;
1776 unsigned int pmin;
1777 int changed;
1778
1779 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1780 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1781 min_datainterval = 0xff;
1782 list_for_each_entry(fp, &subs->fmt_list, list) {
1783 if (!hw_check_valid_format(subs, params, fp))
1784 continue;
1785 min_datainterval = min(min_datainterval, fp->datainterval);
1786 }
1787 if (min_datainterval == 0xff) {
1788 hwc_debug(" --> get emtpy\n");
1789 it->empty = 1;
1790 return -EINVAL;
1791 }
1792 pmin = 125 * (1 << min_datainterval);
1793 changed = 0;
1794 if (it->min < pmin) {
1795 it->min = pmin;
1796 it->openmin = 0;
1797 changed = 1;
1798 }
1799 if (snd_interval_checkempty(it)) {
1800 it->empty = 1;
1801 return -EINVAL;
1802 }
1803 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1804 return changed;
1805}
1806
Luke Rossa79eee82006-08-29 10:46:32 +02001807/*
1808 * If the device supports unusual bit rates, does the request meet these?
1809 */
1810static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1811 struct snd_usb_substream *subs)
1812{
Takashi Iwai8fec5602007-02-01 11:50:56 +01001813 struct audioformat *fp;
1814 int count = 0, needs_knot = 0;
Luke Rossa79eee82006-08-29 10:46:32 +02001815 int err;
1816
Takashi Iwai8fec5602007-02-01 11:50:56 +01001817 list_for_each_entry(fp, &subs->fmt_list, list) {
1818 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1819 return 0;
1820 count += fp->nr_rates;
Clemens Ladisch918f3a02007-08-13 17:40:54 +02001821 if (fp->rates & SNDRV_PCM_RATE_KNOT)
Takashi Iwai8fec5602007-02-01 11:50:56 +01001822 needs_knot = 1;
Luke Rossa79eee82006-08-29 10:46:32 +02001823 }
Takashi Iwai8fec5602007-02-01 11:50:56 +01001824 if (!needs_knot)
1825 return 0;
1826
1827 subs->rate_list.count = count;
1828 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
1829 subs->rate_list.mask = 0;
1830 count = 0;
1831 list_for_each_entry(fp, &subs->fmt_list, list) {
1832 int i;
1833 for (i = 0; i < fp->nr_rates; i++)
1834 subs->rate_list.list[count++] = fp->rate_table[i];
1835 }
1836 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1837 &subs->rate_list);
1838 if (err < 0)
1839 return err;
Luke Rossa79eee82006-08-29 10:46:32 +02001840
1841 return 0;
1842}
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845/*
1846 * set up the runtime hardware information.
1847 */
1848
Takashi Iwai86e07d32005-11-17 15:08:02 +01001849static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 struct list_head *p;
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001852 unsigned int pt, ptmin;
1853 int param_period_time_if_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 int err;
1855
1856 runtime->hw.formats = subs->formats;
1857
1858 runtime->hw.rate_min = 0x7fffffff;
1859 runtime->hw.rate_max = 0;
1860 runtime->hw.channels_min = 256;
1861 runtime->hw.channels_max = 0;
1862 runtime->hw.rates = 0;
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001863 ptmin = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /* check min/max rates and channels */
1865 list_for_each(p, &subs->fmt_list) {
1866 struct audioformat *fp;
1867 fp = list_entry(p, struct audioformat, list);
1868 runtime->hw.rates |= fp->rates;
1869 if (runtime->hw.rate_min > fp->rate_min)
1870 runtime->hw.rate_min = fp->rate_min;
1871 if (runtime->hw.rate_max < fp->rate_max)
1872 runtime->hw.rate_max = fp->rate_max;
1873 if (runtime->hw.channels_min > fp->channels)
1874 runtime->hw.channels_min = fp->channels;
1875 if (runtime->hw.channels_max < fp->channels)
1876 runtime->hw.channels_max = fp->channels;
1877 if (fp->fmt_type == USB_FORMAT_TYPE_II && fp->frame_size > 0) {
1878 /* FIXME: there might be more than one audio formats... */
1879 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1880 fp->frame_size;
1881 }
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001882 pt = 125 * (1 << fp->datainterval);
1883 ptmin = min(ptmin, pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001886 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1887 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
1888 /* full speed devices have fixed data packet interval */
1889 ptmin = 1000;
1890 if (ptmin == 1000)
1891 /* if period time doesn't go below 1 ms, no rules needed */
1892 param_period_time_if_needed = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001894 ptmin, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Clemens Ladisch4608eb02009-04-03 09:42:42 +02001896 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1897 hw_rule_rate, subs,
1898 SNDRV_PCM_HW_PARAM_FORMAT,
1899 SNDRV_PCM_HW_PARAM_CHANNELS,
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001900 param_period_time_if_needed,
Clemens Ladisch4608eb02009-04-03 09:42:42 +02001901 -1)) < 0)
Takashi Iwai5a220c82008-03-17 09:59:32 +01001902 return err;
Clemens Ladisch4608eb02009-04-03 09:42:42 +02001903 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1904 hw_rule_channels, subs,
1905 SNDRV_PCM_HW_PARAM_FORMAT,
1906 SNDRV_PCM_HW_PARAM_RATE,
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001907 param_period_time_if_needed,
Clemens Ladisch4608eb02009-04-03 09:42:42 +02001908 -1)) < 0)
1909 return err;
1910 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1911 hw_rule_format, subs,
1912 SNDRV_PCM_HW_PARAM_RATE,
1913 SNDRV_PCM_HW_PARAM_CHANNELS,
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001914 param_period_time_if_needed,
Clemens Ladisch4608eb02009-04-03 09:42:42 +02001915 -1)) < 0)
1916 return err;
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001917 if (param_period_time_if_needed >= 0) {
1918 err = snd_pcm_hw_rule_add(runtime, 0,
1919 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1920 hw_rule_period_time, subs,
1921 SNDRV_PCM_HW_PARAM_FORMAT,
1922 SNDRV_PCM_HW_PARAM_CHANNELS,
1923 SNDRV_PCM_HW_PARAM_RATE,
1924 -1);
1925 if (err < 0)
1926 return err;
1927 }
Clemens Ladisch4608eb02009-04-03 09:42:42 +02001928 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
1929 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return 0;
1931}
1932
Clemens Ladisch1700f302006-10-04 13:41:25 +02001933static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001935 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1936 struct snd_pcm_runtime *runtime = substream->runtime;
1937 struct snd_usb_substream *subs = &as->substream[direction];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 subs->interface = -1;
1940 subs->format = 0;
Clemens Ladisch1700f302006-10-04 13:41:25 +02001941 runtime->hw = snd_usb_hardware;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 runtime->private_data = subs;
1943 subs->pcm_substream = substream;
1944 return setup_hw_info(runtime, subs);
1945}
1946
Takashi Iwai86e07d32005-11-17 15:08:02 +01001947static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001949 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1950 struct snd_usb_substream *subs = &as->substream[direction];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 if (subs->interface >= 0) {
1953 usb_set_interface(subs->dev, subs->interface, 0);
1954 subs->interface = -1;
1955 }
1956 subs->pcm_substream = NULL;
1957 return 0;
1958}
1959
Takashi Iwai86e07d32005-11-17 15:08:02 +01001960static int snd_usb_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Clemens Ladisch1700f302006-10-04 13:41:25 +02001962 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
Takashi Iwai86e07d32005-11-17 15:08:02 +01001965static int snd_usb_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1968}
1969
Takashi Iwai86e07d32005-11-17 15:08:02 +01001970static int snd_usb_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Clemens Ladisch1700f302006-10-04 13:41:25 +02001972 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Takashi Iwai86e07d32005-11-17 15:08:02 +01001975static int snd_usb_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1978}
1979
Takashi Iwai86e07d32005-11-17 15:08:02 +01001980static struct snd_pcm_ops snd_usb_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 .open = snd_usb_playback_open,
1982 .close = snd_usb_playback_close,
1983 .ioctl = snd_pcm_lib_ioctl,
1984 .hw_params = snd_usb_hw_params,
1985 .hw_free = snd_usb_hw_free,
1986 .prepare = snd_usb_pcm_prepare,
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001987 .trigger = snd_usb_pcm_playback_trigger,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 .pointer = snd_usb_pcm_pointer,
Clemens Ladischc55675e2009-12-18 09:31:31 +01001989 .page = snd_pcm_lib_get_vmalloc_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990};
1991
Takashi Iwai86e07d32005-11-17 15:08:02 +01001992static struct snd_pcm_ops snd_usb_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 .open = snd_usb_capture_open,
1994 .close = snd_usb_capture_close,
1995 .ioctl = snd_pcm_lib_ioctl,
1996 .hw_params = snd_usb_hw_params,
1997 .hw_free = snd_usb_hw_free,
1998 .prepare = snd_usb_pcm_prepare,
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001999 .trigger = snd_usb_pcm_capture_trigger,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 .pointer = snd_usb_pcm_pointer,
Clemens Ladischc55675e2009-12-18 09:31:31 +01002001 .page = snd_pcm_lib_get_vmalloc_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002};
2003
2004
2005
2006/*
2007 * helper functions
2008 */
2009
2010/*
2011 * combine bytes and get an integer value
2012 */
2013unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
2014{
2015 switch (size) {
2016 case 1: return *bytes;
2017 case 2: return combine_word(bytes);
2018 case 3: return combine_triple(bytes);
2019 case 4: return combine_quad(bytes);
2020 default: return 0;
2021 }
2022}
2023
2024/*
2025 * parse descriptor buffer and return the pointer starting the given
2026 * descriptor type.
2027 */
2028void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
2029{
2030 u8 *p, *end, *next;
2031
2032 p = descstart;
2033 end = p + desclen;
2034 for (; p < end;) {
2035 if (p[0] < 2)
2036 return NULL;
2037 next = p + p[0];
2038 if (next > end)
2039 return NULL;
2040 if (p[1] == dtype && (!after || (void *)p > after)) {
2041 return p;
2042 }
2043 p = next;
2044 }
2045 return NULL;
2046}
2047
2048/*
2049 * find a class-specified interface descriptor with the given subtype.
2050 */
2051void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
2052{
2053 unsigned char *p = after;
2054
2055 while ((p = snd_usb_find_desc(buffer, buflen, p,
2056 USB_DT_CS_INTERFACE)) != NULL) {
2057 if (p[0] >= 3 && p[2] == dsubtype)
2058 return p;
2059 }
2060 return NULL;
2061}
2062
2063/*
2064 * Wrapper for usb_control_msg().
2065 * Allocates a temp buffer to prevent dmaing from/to the stack.
2066 */
2067int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
2068 __u8 requesttype, __u16 value, __u16 index, void *data,
2069 __u16 size, int timeout)
2070{
2071 int err;
2072 void *buf = NULL;
2073
2074 if (size > 0) {
Alexey Dobriyan52978be2006-09-30 23:27:21 -07002075 buf = kmemdup(data, size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (!buf)
2077 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
2079 err = usb_control_msg(dev, pipe, request, requesttype,
2080 value, index, buf, size, timeout);
2081 if (size > 0) {
2082 memcpy(data, buf, size);
2083 kfree(buf);
2084 }
2085 return err;
2086}
2087
2088
2089/*
2090 * entry point for linux usb interface
2091 */
2092
2093static int usb_audio_probe(struct usb_interface *intf,
2094 const struct usb_device_id *id);
2095static void usb_audio_disconnect(struct usb_interface *intf);
Andrew Morton93521d22007-12-24 14:40:56 +01002096
2097#ifdef CONFIG_PM
Oliver Neukumf85bf292007-12-14 14:42:41 +01002098static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message);
2099static int usb_audio_resume(struct usb_interface *intf);
Andrew Morton93521d22007-12-24 14:40:56 +01002100#else
2101#define usb_audio_suspend NULL
2102#define usb_audio_resume NULL
2103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105static struct usb_device_id usb_audio_ids [] = {
2106#include "usbquirks.h"
2107 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
2108 .bInterfaceClass = USB_CLASS_AUDIO,
2109 .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL },
2110 { } /* Terminating entry */
2111};
2112
2113MODULE_DEVICE_TABLE (usb, usb_audio_ids);
2114
2115static struct usb_driver usb_audio_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 .name = "snd-usb-audio",
2117 .probe = usb_audio_probe,
2118 .disconnect = usb_audio_disconnect,
Oliver Neukumf85bf292007-12-14 14:42:41 +01002119 .suspend = usb_audio_suspend,
2120 .resume = usb_audio_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 .id_table = usb_audio_ids,
2122};
2123
2124
Takashi Iwai727f3172006-08-04 19:08:03 +02002125#if defined(CONFIG_PROC_FS) && defined(CONFIG_SND_VERBOSE_PROCFS)
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127/*
2128 * proc interface for list the supported pcm formats
2129 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002130static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 struct list_head *p;
2133 static char *sync_types[4] = {
2134 "NONE", "ASYNC", "ADAPTIVE", "SYNC"
2135 };
2136
2137 list_for_each(p, &subs->fmt_list) {
2138 struct audioformat *fp;
2139 fp = list_entry(p, struct audioformat, list);
2140 snd_iprintf(buffer, " Interface %d\n", fp->iface);
2141 snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
Takashi Iwai6e5265e2009-09-08 14:26:51 +02002142 snd_iprintf(buffer, " Format: %s\n",
2143 snd_pcm_format_name(fp->format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 snd_iprintf(buffer, " Channels: %d\n", fp->channels);
2145 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
2146 fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
2147 fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
2148 sync_types[(fp->ep_attr & EP_ATTR_MASK) >> 2]);
2149 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
2150 snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
2151 fp->rate_min, fp->rate_max);
2152 } else {
2153 unsigned int i;
2154 snd_iprintf(buffer, " Rates: ");
2155 for (i = 0; i < fp->nr_rates; i++) {
2156 if (i > 0)
2157 snd_iprintf(buffer, ", ");
2158 snd_iprintf(buffer, "%d", fp->rate_table[i]);
2159 }
2160 snd_iprintf(buffer, "\n");
2161 }
Clemens Ladisch744b89e2009-04-03 09:45:01 +02002162 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
2163 snd_iprintf(buffer, " Data packet interval: %d us\n",
2164 125 * (1 << fp->datainterval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
Andreas Bergmeierb9d710b2009-01-24 12:15:14 +01002166 // snd_iprintf(buffer, " EP Attribute = %#x\n", fp->attributes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168}
2169
Takashi Iwai86e07d32005-11-17 15:08:02 +01002170static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
2172 if (subs->running) {
2173 unsigned int i;
2174 snd_iprintf(buffer, " Status: Running\n");
2175 snd_iprintf(buffer, " Interface = %d\n", subs->interface);
2176 snd_iprintf(buffer, " Altset = %d\n", subs->format);
2177 snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
2178 for (i = 0; i < subs->nurbs; i++)
2179 snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
2180 snd_iprintf(buffer, "]\n");
2181 snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
Clemens Ladisch08fe1582005-04-22 15:33:01 +02002182 snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
2184 ? get_full_speed_hz(subs->freqm)
Clemens Ladisch08fe1582005-04-22 15:33:01 +02002185 : get_high_speed_hz(subs->freqm),
2186 subs->freqm >> 16, subs->freqm & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 } else {
2188 snd_iprintf(buffer, " Status: Stop\n");
2189 }
2190}
2191
Takashi Iwai86e07d32005-11-17 15:08:02 +01002192static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002194 struct snd_usb_stream *stream = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
2197
2198 if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
2199 snd_iprintf(buffer, "\nPlayback:\n");
2200 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2201 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2202 }
2203 if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
2204 snd_iprintf(buffer, "\nCapture:\n");
2205 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2206 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2207 }
2208}
2209
Takashi Iwai86e07d32005-11-17 15:08:02 +01002210static void proc_pcm_format_add(struct snd_usb_stream *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002212 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 char name[32];
Takashi Iwai86e07d32005-11-17 15:08:02 +01002214 struct snd_card *card = stream->chip->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 sprintf(name, "stream%d", stream->pcm_index);
Pavel Machek07f51a72008-04-14 13:15:56 +02002217 if (!snd_card_proc_new(card, name, &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002218 snd_info_set_text_ops(entry, stream, proc_pcm_format_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219}
2220
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002221#else
2222
2223static inline void proc_pcm_format_add(struct snd_usb_stream *stream)
2224{
2225}
2226
2227#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229/*
2230 * initialize the substream instance.
2231 */
2232
Takashi Iwai86e07d32005-11-17 15:08:02 +01002233static void init_substream(struct snd_usb_stream *as, int stream, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002235 struct snd_usb_substream *subs = &as->substream[stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 INIT_LIST_HEAD(&subs->fmt_list);
2238 spin_lock_init(&subs->lock);
2239
2240 subs->stream = as;
2241 subs->direction = stream;
2242 subs->dev = as->chip->dev;
Clemens Ladischd5132022008-02-25 11:01:00 +01002243 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 subs->ops = audio_urb_ops[stream];
Clemens Ladischd5132022008-02-25 11:01:00 +01002245 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 subs->ops = audio_urb_ops_high_speed[stream];
Clemens Ladischd5132022008-02-25 11:01:00 +01002247 switch (as->chip->usb_id) {
2248 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
2249 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
Eran Tromer97c889a2008-09-26 01:07:03 -04002250 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
Clemens Ladischd5132022008-02-25 11:01:00 +01002251 subs->ops.retire_sync = retire_playback_sync_urb_hs_emu;
2252 break;
2253 }
2254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 snd_pcm_set_ops(as->pcm, stream,
2256 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2257 &snd_usb_playback_ops : &snd_usb_capture_ops);
2258
2259 list_add_tail(&fp->list, &subs->fmt_list);
2260 subs->formats |= 1ULL << fp->format;
2261 subs->endpoint = fp->endpoint;
2262 subs->num_formats++;
2263 subs->fmt_type = fp->fmt_type;
2264}
2265
2266
2267/*
2268 * free a substream
2269 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002270static void free_substream(struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
2272 struct list_head *p, *n;
2273
Pavel Machek07f51a72008-04-14 13:15:56 +02002274 if (!subs->num_formats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return; /* not initialized */
2276 list_for_each_safe(p, n, &subs->fmt_list) {
2277 struct audioformat *fp = list_entry(p, struct audioformat, list);
2278 kfree(fp->rate_table);
2279 kfree(fp);
2280 }
Takashi Iwai8fec5602007-02-01 11:50:56 +01002281 kfree(subs->rate_list.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
2284
2285/*
2286 * free a usb stream instance
2287 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002288static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 free_substream(&stream->substream[0]);
2291 free_substream(&stream->substream[1]);
2292 list_del(&stream->list);
2293 kfree(stream);
2294}
2295
Takashi Iwai86e07d32005-11-17 15:08:02 +01002296static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002298 struct snd_usb_stream *stream = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (stream) {
2300 stream->pcm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 snd_usb_audio_stream_free(stream);
2302 }
2303}
2304
2305
2306/*
2307 * add this endpoint to the chip instance.
2308 * if a stream with the same endpoint already exists, append to it.
2309 * if not, create a new pcm stream.
2310 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002311static int add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
2313 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002314 struct snd_usb_stream *as;
2315 struct snd_usb_substream *subs;
2316 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 int err;
2318
2319 list_for_each(p, &chip->pcm_list) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002320 as = list_entry(p, struct snd_usb_stream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (as->fmt_type != fp->fmt_type)
2322 continue;
2323 subs = &as->substream[stream];
Pavel Machek07f51a72008-04-14 13:15:56 +02002324 if (!subs->endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 continue;
2326 if (subs->endpoint == fp->endpoint) {
2327 list_add_tail(&fp->list, &subs->fmt_list);
2328 subs->num_formats++;
2329 subs->formats |= 1ULL << fp->format;
2330 return 0;
2331 }
2332 }
2333 /* look for an empty stream */
2334 list_for_each(p, &chip->pcm_list) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002335 as = list_entry(p, struct snd_usb_stream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (as->fmt_type != fp->fmt_type)
2337 continue;
2338 subs = &as->substream[stream];
2339 if (subs->endpoint)
2340 continue;
2341 err = snd_pcm_new_stream(as->pcm, stream, 1);
2342 if (err < 0)
2343 return err;
2344 init_substream(as, stream, fp);
2345 return 0;
2346 }
2347
2348 /* create a new pcm */
Panagiotis Issaris59feddb2006-07-25 15:28:03 +02002349 as = kzalloc(sizeof(*as), GFP_KERNEL);
Pavel Machek07f51a72008-04-14 13:15:56 +02002350 if (!as)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 as->pcm_index = chip->pcm_devs;
2353 as->chip = chip;
2354 as->fmt_type = fp->fmt_type;
2355 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
2356 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
2357 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
2358 &pcm);
2359 if (err < 0) {
2360 kfree(as);
2361 return err;
2362 }
2363 as->pcm = pcm;
2364 pcm->private_data = as;
2365 pcm->private_free = snd_usb_audio_pcm_free;
2366 pcm->info_flags = 0;
2367 if (chip->pcm_devs > 0)
2368 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
2369 else
2370 strcpy(pcm->name, "USB Audio");
2371
2372 init_substream(as, stream, fp);
2373
2374 list_add(&as->list, &chip->pcm_list);
2375 chip->pcm_devs++;
2376
2377 proc_pcm_format_add(as);
2378
2379 return 0;
2380}
2381
2382
2383/*
2384 * check if the device uses big-endian samples
2385 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002386static int is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002388 switch (chip->usb_id) {
2389 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
2390 if (fp->endpoint & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return 1;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002392 break;
2393 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
Thibault Le Meurf8c78b82007-07-12 11:26:35 +02002394 if (device_setup[chip->index] == 0x00 ||
2395 fp->altsetting==1 || fp->altsetting==2 || fp->altsetting==3)
2396 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398 return 0;
2399}
2400
2401/*
2402 * parse the audio format type I descriptor
2403 * and returns the corresponding pcm format
2404 *
2405 * @dev: usb device
2406 * @fp: audioformat record
2407 * @format: the format tag (wFormatTag)
2408 * @fmt: the format type descriptor
2409 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002410static int parse_audio_format_i_type(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 int format, unsigned char *fmt)
2412{
2413 int pcm_format;
2414 int sample_width, sample_bytes;
2415
2416 /* FIXME: correct endianess and sign? */
2417 pcm_format = -1;
2418 sample_width = fmt[6];
2419 sample_bytes = fmt[5];
2420 switch (format) {
2421 case 0: /* some devices don't define this correctly... */
2422 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002423 chip->dev->devnum, fp->iface, fp->altsetting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 /* fall-through */
2425 case USB_AUDIO_FORMAT_PCM:
2426 if (sample_width > sample_bytes * 8) {
2427 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002428 chip->dev->devnum, fp->iface, fp->altsetting,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 sample_width, sample_bytes);
2430 }
2431 /* check the format byte size */
2432 switch (fmt[5]) {
2433 case 1:
2434 pcm_format = SNDRV_PCM_FORMAT_S8;
2435 break;
2436 case 2:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002437 if (is_big_endian_format(chip, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
2439 else
2440 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2441 break;
2442 case 3:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002443 if (is_big_endian_format(chip, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
2445 else
2446 pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
2447 break;
2448 case 4:
2449 pcm_format = SNDRV_PCM_FORMAT_S32_LE;
2450 break;
2451 default:
2452 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002453 chip->dev->devnum, fp->iface,
2454 fp->altsetting, sample_width, sample_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 break;
2456 }
2457 break;
2458 case USB_AUDIO_FORMAT_PCM8:
Pavel Machek2a56f512008-04-14 13:14:22 +02002459 pcm_format = SNDRV_PCM_FORMAT_U8;
2460
2461 /* Dallas DS4201 workaround: it advertises U8 format, but really
2462 supports S8. */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002463 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 pcm_format = SNDRV_PCM_FORMAT_S8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 break;
2466 case USB_AUDIO_FORMAT_IEEE_FLOAT:
2467 pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
2468 break;
2469 case USB_AUDIO_FORMAT_ALAW:
2470 pcm_format = SNDRV_PCM_FORMAT_A_LAW;
2471 break;
2472 case USB_AUDIO_FORMAT_MU_LAW:
2473 pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
2474 break;
2475 default:
2476 snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002477 chip->dev->devnum, fp->iface, fp->altsetting, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 break;
2479 }
2480 return pcm_format;
2481}
2482
2483
2484/*
2485 * parse the format descriptor and stores the possible sample rates
2486 * on the audioformat table.
2487 *
2488 * @dev: usb device
2489 * @fp: audioformat record
2490 * @fmt: the format descriptor
2491 * @offset: the start offset of descriptor pointing the rate type
2492 * (7 for type I and II, 8 for type II)
2493 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002494static int parse_audio_format_rates(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 unsigned char *fmt, int offset)
2496{
2497 int nr_rates = fmt[offset];
Clemens Ladisch918f3a02007-08-13 17:40:54 +02002498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
2500 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002501 chip->dev->devnum, fp->iface, fp->altsetting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return -1;
2503 }
2504
2505 if (nr_rates) {
2506 /*
2507 * build the rate table and bitmap flags
2508 */
Clemens Ladisch918f3a02007-08-13 17:40:54 +02002509 int r, idx;
Clemens Ladisch918f3a02007-08-13 17:40:54 +02002510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2512 if (fp->rate_table == NULL) {
2513 snd_printk(KERN_ERR "cannot malloc\n");
2514 return -1;
2515 }
2516
Takashi Iwai04125582009-02-16 22:48:12 +01002517 fp->nr_rates = 0;
2518 fp->rate_min = fp->rate_max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
Clemens Ladisch987411b2006-11-20 14:14:39 +01002520 unsigned int rate = combine_triple(&fmt[idx]);
Takashi Iwai04125582009-02-16 22:48:12 +01002521 if (!rate)
2522 continue;
Clemens Ladisch987411b2006-11-20 14:14:39 +01002523 /* C-Media CM6501 mislabels its 96 kHz altsetting */
2524 if (rate == 48000 && nr_rates == 1 &&
Joris van Rantwijk3b03cc52009-02-16 22:58:23 +01002525 (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
2526 chip->usb_id == USB_ID(0x0d8c, 0x0102)) &&
Clemens Ladisch987411b2006-11-20 14:14:39 +01002527 fp->altsetting == 5 && fp->maxpacksize == 392)
2528 rate = 96000;
Takashi Iwai04125582009-02-16 22:48:12 +01002529 fp->rate_table[fp->nr_rates] = rate;
2530 if (!fp->rate_min || rate < fp->rate_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 fp->rate_min = rate;
Takashi Iwai04125582009-02-16 22:48:12 +01002532 if (!fp->rate_max || rate > fp->rate_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 fp->rate_max = rate;
Clemens Ladisch918f3a02007-08-13 17:40:54 +02002534 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
Takashi Iwai04125582009-02-16 22:48:12 +01002535 fp->nr_rates++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
Takashi Iwai04125582009-02-16 22:48:12 +01002537 if (!fp->nr_rates) {
Gregor Jasnybeb60112007-01-31 12:27:39 +01002538 hwc_debug("All rates were zero. Skipping format!\n");
2539 return -1;
2540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 } else {
2542 /* continuous rates */
2543 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
2544 fp->rate_min = combine_triple(&fmt[offset + 1]);
2545 fp->rate_max = combine_triple(&fmt[offset + 4]);
2546 }
2547 return 0;
2548}
2549
2550/*
2551 * parse the format type I and III descriptors
2552 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002553static int parse_audio_format_i(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 int format, unsigned char *fmt)
2555{
2556 int pcm_format;
2557
2558 if (fmt[3] == USB_FORMAT_TYPE_III) {
2559 /* FIXME: the format type is really IECxxx
2560 * but we give normal PCM format to get the existing
2561 * apps working...
2562 */
Thibault Le Meurcac19c32007-07-13 11:50:23 +02002563 switch (chip->usb_id) {
2564
2565 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2566 if (device_setup[chip->index] == 0x00 &&
2567 fp->altsetting == 6)
2568 pcm_format = SNDRV_PCM_FORMAT_S16_BE;
2569 else
2570 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2571 break;
2572 default:
2573 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 } else {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002576 pcm_format = parse_audio_format_i_type(chip, fp, format, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (pcm_format < 0)
2578 return -1;
2579 }
2580 fp->format = pcm_format;
2581 fp->channels = fmt[4];
2582 if (fp->channels < 1) {
2583 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002584 chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 return -1;
2586 }
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002587 return parse_audio_format_rates(chip, fp, fmt, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
2590/*
2591 * prase the format type II descriptor
2592 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002593static int parse_audio_format_ii(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 int format, unsigned char *fmt)
2595{
2596 int brate, framesize;
2597 switch (format) {
2598 case USB_AUDIO_FORMAT_AC3:
2599 /* FIXME: there is no AC3 format defined yet */
2600 // fp->format = SNDRV_PCM_FORMAT_AC3;
2601 fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
2602 break;
2603 case USB_AUDIO_FORMAT_MPEG:
2604 fp->format = SNDRV_PCM_FORMAT_MPEG;
2605 break;
2606 default:
Andreas Bergmeierb9d710b2009-01-24 12:15:14 +01002607 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002608 chip->dev->devnum, fp->iface, fp->altsetting, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 fp->format = SNDRV_PCM_FORMAT_MPEG;
2610 break;
2611 }
2612 fp->channels = 1;
2613 brate = combine_word(&fmt[4]); /* fmt[4,5] : wMaxBitRate (in kbps) */
2614 framesize = combine_word(&fmt[6]); /* fmt[6,7]: wSamplesPerFrame */
2615 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
2616 fp->frame_size = framesize;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002617 return parse_audio_format_rates(chip, fp, fmt, 8); /* fmt[8..] sample rates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
Takashi Iwai86e07d32005-11-17 15:08:02 +01002620static int parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 int format, unsigned char *fmt, int stream)
2622{
2623 int err;
2624
2625 switch (fmt[3]) {
2626 case USB_FORMAT_TYPE_I:
2627 case USB_FORMAT_TYPE_III:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002628 err = parse_audio_format_i(chip, fp, format, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 break;
2630 case USB_FORMAT_TYPE_II:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002631 err = parse_audio_format_ii(chip, fp, format, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 break;
2633 default:
2634 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002635 chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 return -1;
2637 }
2638 fp->fmt_type = fmt[3];
2639 if (err < 0)
2640 return err;
2641#if 1
Clemens Ladisch33159372006-01-13 08:11:22 +01002642 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /* extigy apparently supports sample rates other than 48k
2644 * but not in ordinary way. so we enable only 48k atm.
2645 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002646 if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
Clemens Ladisch33159372006-01-13 08:11:22 +01002647 chip->usb_id == USB_ID(0x041e, 0x3020) ||
2648 chip->usb_id == USB_ID(0x041e, 0x3061)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (fmt[3] == USB_FORMAT_TYPE_I &&
Clemens Ladisch8c1872d2005-04-28 09:31:53 +02002650 fp->rates != SNDRV_PCM_RATE_48000 &&
2651 fp->rates != SNDRV_PCM_RATE_96000)
Clemens Ladischb4d3f9d2005-06-27 08:18:27 +02002652 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654#endif
2655 return 0;
2656}
2657
Clemens Ladisch744b89e2009-04-03 09:45:01 +02002658static unsigned char parse_datainterval(struct snd_usb_audio *chip,
2659 struct usb_host_interface *alts)
2660{
2661 if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH &&
2662 get_endpoint(alts, 0)->bInterval >= 1 &&
2663 get_endpoint(alts, 0)->bInterval <= 4)
2664 return get_endpoint(alts, 0)->bInterval - 1;
2665 else
2666 return 0;
2667}
2668
Thibault LE MEURe3113342006-03-14 11:44:53 +01002669static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
2670 int iface, int altno);
Takashi Iwai86e07d32005-11-17 15:08:02 +01002671static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
2673 struct usb_device *dev;
2674 struct usb_interface *iface;
2675 struct usb_host_interface *alts;
2676 struct usb_interface_descriptor *altsd;
2677 int i, altno, err, stream;
2678 int format;
Clemens Ladisch8886f332009-07-13 13:21:58 +02002679 struct audioformat *fp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 unsigned char *fmt, *csep;
Pavel Machekb9d43bc2008-04-14 13:12:47 +02002681 int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
2683 dev = chip->dev;
2684
2685 /* parse the interface's altsettings */
2686 iface = usb_ifnum_to_if(dev, iface_no);
Pavel Machekb9d43bc2008-04-14 13:12:47 +02002687
2688 num = iface->num_altsetting;
2689
2690 /*
2691 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
2692 * one misses syncpipe, and does not produce any sound.
2693 */
2694 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
2695 num = 4;
2696
2697 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 alts = &iface->altsetting[i];
2699 altsd = get_iface_desc(alts);
2700 /* skip invalid one */
2701 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2702 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2703 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING &&
2704 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
2705 altsd->bNumEndpoints < 1 ||
2706 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
2707 continue;
2708 /* must be isochronous */
2709 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2710 USB_ENDPOINT_XFER_ISOC)
2711 continue;
2712 /* check direction */
2713 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
2714 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2715 altno = altsd->bAlternateSetting;
Thibault LE MEURe3113342006-03-14 11:44:53 +01002716
2717 /* audiophile usb: skip altsets incompatible with device_setup
2718 */
2719 if (chip->usb_id == USB_ID(0x0763, 0x2003) &&
2720 audiophile_skip_setting_quirk(chip, iface_no, altno))
2721 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 /* get audio formats */
2724 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, AS_GENERAL);
2725 if (!fmt) {
2726 snd_printk(KERN_ERR "%d:%u:%d : AS_GENERAL descriptor not found\n",
2727 dev->devnum, iface_no, altno);
2728 continue;
2729 }
2730
2731 if (fmt[0] < 7) {
2732 snd_printk(KERN_ERR "%d:%u:%d : invalid AS_GENERAL desc\n",
2733 dev->devnum, iface_no, altno);
2734 continue;
2735 }
2736
2737 format = (fmt[6] << 8) | fmt[5]; /* remember the format value */
2738
2739 /* get format type */
2740 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, FORMAT_TYPE);
2741 if (!fmt) {
2742 snd_printk(KERN_ERR "%d:%u:%d : no FORMAT_TYPE desc\n",
2743 dev->devnum, iface_no, altno);
2744 continue;
2745 }
2746 if (fmt[0] < 8) {
2747 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
2748 dev->devnum, iface_no, altno);
2749 continue;
2750 }
2751
Clemens Ladisch8886f332009-07-13 13:21:58 +02002752 /*
2753 * Blue Microphones workaround: The last altsetting is identical
2754 * with the previous one, except for a larger packet size, but
2755 * is actually a mislabeled two-channel setting; ignore it.
2756 */
2757 if (fmt[4] == 1 && fmt[5] == 2 && altno == 2 && num == 3 &&
2758 fp && fp->altsetting == 1 && fp->channels == 1 &&
2759 fp->format == SNDRV_PCM_FORMAT_S16_LE &&
2760 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
2761 fp->maxpacksize * 2)
2762 continue;
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
2765 /* Creamware Noah has this descriptor after the 2nd endpoint */
2766 if (!csep && altsd->bNumEndpoints >= 2)
2767 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
2768 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
Takashi Iwaif8c75792006-05-18 14:47:03 +02002769 snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
Clemens Ladischfaf8d112006-05-18 09:35:15 +02002770 " class specific endpoint descriptor\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 dev->devnum, iface_no, altno);
Clemens Ladischfaf8d112006-05-18 09:35:15 +02002772 csep = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
2774
Panagiotis Issaris59feddb2006-07-25 15:28:03 +02002775 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if (! fp) {
2777 snd_printk(KERN_ERR "cannot malloc\n");
2778 return -ENOMEM;
2779 }
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 fp->iface = iface_no;
2782 fp->altsetting = altno;
2783 fp->altset_idx = i;
2784 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2785 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
Clemens Ladisch744b89e2009-04-03 09:45:01 +02002786 fp->datainterval = parse_datainterval(chip, alts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Clemens Ladisch573567e2005-06-27 08:17:30 +02002788 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
2789 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
2790 * (fp->maxpacksize & 0x7ff);
Clemens Ladischfaf8d112006-05-18 09:35:15 +02002791 fp->attributes = csep ? csep[3] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 /* some quirks for attributes here */
2794
Clemens Ladischc3f93292005-05-04 14:56:04 +02002795 switch (chip->usb_id) {
2796 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 /* Optoplay sets the sample rate attribute although
2798 * it seems not supporting it in fact.
2799 */
2800 fp->attributes &= ~EP_CS_ATTR_SAMPLE_RATE;
Clemens Ladischc3f93292005-05-04 14:56:04 +02002801 break;
2802 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
2803 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 /* doesn't set the sample rate attribute, but supports it */
2805 fp->attributes |= EP_CS_ATTR_SAMPLE_RATE;
Clemens Ladischc3f93292005-05-04 14:56:04 +02002806 break;
2807 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
2808 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
2809 an older model 77d:223) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 /*
2811 * plantronics headset and Griffin iMic have set adaptive-in
2812 * although it's really not...
2813 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 fp->ep_attr &= ~EP_ATTR_MASK;
2815 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2816 fp->ep_attr |= EP_ATTR_ADAPTIVE;
2817 else
2818 fp->ep_attr |= EP_ATTR_SYNC;
Clemens Ladischc3f93292005-05-04 14:56:04 +02002819 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 }
2821
2822 /* ok, let's parse further... */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002823 if (parse_audio_format(chip, fp, format, fmt, stream) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 kfree(fp->rate_table);
2825 kfree(fp);
2826 continue;
2827 }
2828
Andreas Bergmeierb9d710b2009-01-24 12:15:14 +01002829 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 err = add_audio_endpoint(chip, stream, fp);
2831 if (err < 0) {
2832 kfree(fp->rate_table);
2833 kfree(fp);
2834 return err;
2835 }
2836 /* try to set the interface... */
2837 usb_set_interface(chip->dev, iface_no, altno);
2838 init_usb_pitch(chip->dev, iface_no, alts, fp);
2839 init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
2840 }
2841 return 0;
2842}
2843
2844
2845/*
2846 * disconnect streams
2847 * called from snd_usb_audio_disconnect()
2848 */
Clemens Ladischee733392005-04-25 10:34:13 +02002849static void snd_usb_stream_disconnect(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
2851 int idx;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002852 struct snd_usb_stream *as;
2853 struct snd_usb_substream *subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Takashi Iwai86e07d32005-11-17 15:08:02 +01002855 as = list_entry(head, struct snd_usb_stream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 for (idx = 0; idx < 2; idx++) {
2857 subs = &as->substream[idx];
2858 if (!subs->num_formats)
2859 return;
2860 release_substream_urbs(subs, 1);
2861 subs->interface = -1;
2862 }
2863}
2864
2865/*
2866 * parse audio control descriptor and create pcm/midi streams
2867 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002868static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
2870 struct usb_device *dev = chip->dev;
2871 struct usb_host_interface *host_iface;
2872 struct usb_interface *iface;
2873 unsigned char *p1;
2874 int i, j;
2875
2876 /* find audiocontrol interface */
2877 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
2878 if (!(p1 = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen, NULL, HEADER))) {
2879 snd_printk(KERN_ERR "cannot find HEADER\n");
2880 return -EINVAL;
2881 }
2882 if (! p1[7] || p1[0] < 8 + p1[7]) {
2883 snd_printk(KERN_ERR "invalid HEADER\n");
2884 return -EINVAL;
2885 }
2886
2887 /*
2888 * parse all USB audio streaming interfaces
2889 */
2890 for (i = 0; i < p1[7]; i++) {
2891 struct usb_host_interface *alts;
2892 struct usb_interface_descriptor *altsd;
2893 j = p1[8 + i];
2894 iface = usb_ifnum_to_if(dev, j);
2895 if (!iface) {
2896 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
2897 dev->devnum, ctrlif, j);
2898 continue;
2899 }
2900 if (usb_interface_claimed(iface)) {
2901 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n", dev->devnum, ctrlif, j);
2902 continue;
2903 }
2904 alts = &iface->altsetting[0];
2905 altsd = get_iface_desc(alts);
2906 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
2907 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
2908 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDI_STREAMING) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002909 int err = snd_usbmidi_create(chip->card, iface,
2910 &chip->midi_list, NULL);
2911 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n", dev->devnum, ctrlif, j);
2913 continue;
2914 }
2915 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2916 continue;
2917 }
2918 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2919 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2920 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING) {
2921 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n", dev->devnum, ctrlif, j, altsd->bInterfaceClass);
2922 /* skip non-supported classes */
2923 continue;
2924 }
Clemens Ladisch076639f2007-08-21 08:56:54 +02002925 if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
2926 snd_printk(KERN_ERR "low speed audio streaming not supported\n");
2927 continue;
2928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 if (! parse_audio_endpoints(chip, j)) {
2930 usb_set_interface(dev, j, 0); /* reset the current interface */
2931 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2932 }
2933 }
2934
2935 return 0;
2936}
2937
2938/*
2939 * create a stream for an endpoint/altsetting without proper descriptors
2940 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002941static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002943 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
2945 struct audioformat *fp;
2946 struct usb_host_interface *alts;
2947 int stream, err;
Al Virob4482a42007-10-14 19:35:40 +01002948 unsigned *rate_table = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Alexey Dobriyan52978be2006-09-30 23:27:21 -07002950 fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if (! fp) {
Alexey Dobriyan52978be2006-09-30 23:27:21 -07002952 snd_printk(KERN_ERR "cannot memdup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 return -ENOMEM;
2954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 if (fp->nr_rates > 0) {
2956 rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
2957 if (!rate_table) {
2958 kfree(fp);
2959 return -ENOMEM;
2960 }
2961 memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
2962 fp->rate_table = rate_table;
2963 }
2964
2965 stream = (fp->endpoint & USB_DIR_IN)
2966 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2967 err = add_audio_endpoint(chip, stream, fp);
2968 if (err < 0) {
2969 kfree(fp);
2970 kfree(rate_table);
2971 return err;
2972 }
2973 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
2974 fp->altset_idx >= iface->num_altsetting) {
2975 kfree(fp);
2976 kfree(rate_table);
2977 return -EINVAL;
2978 }
2979 alts = &iface->altsetting[fp->altset_idx];
Clemens Ladisch744b89e2009-04-03 09:45:01 +02002980 fp->datainterval = parse_datainterval(chip, alts);
Clemens Ladisch894dcd72009-02-06 08:13:07 +01002981 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 usb_set_interface(chip->dev, fp->iface, 0);
2983 init_usb_pitch(chip->dev, fp->iface, alts, fp);
2984 init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
2985 return 0;
2986}
2987
2988/*
2989 * create a stream for an interface with proper descriptors
2990 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002991static int create_standard_audio_quirk(struct snd_usb_audio *chip,
Clemens Ladischd1bda042005-09-14 08:36:03 +02002992 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002993 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
2995 struct usb_host_interface *alts;
2996 struct usb_interface_descriptor *altsd;
2997 int err;
2998
2999 alts = &iface->altsetting[0];
3000 altsd = get_iface_desc(alts);
Clemens Ladischd1bda042005-09-14 08:36:03 +02003001 err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 if (err < 0) {
3003 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
3004 altsd->bInterfaceNumber, err);
3005 return err;
3006 }
Clemens Ladischd1bda042005-09-14 08:36:03 +02003007 /* reset the current interface */
3008 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return 0;
3010}
3011
3012/*
Pedro Lopez-Cabanillas310e0dc2008-10-04 16:27:36 +02003013 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
3014 * The only way to detect the sample rate is by looking at wMaxPacketSize.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 */
Pedro Lopez-Cabanillas310e0dc2008-10-04 16:27:36 +02003016static int create_uaxx_quirk(struct snd_usb_audio *chip,
3017 struct usb_interface *iface,
3018 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020 static const struct audioformat ua_format = {
3021 .format = SNDRV_PCM_FORMAT_S24_3LE,
3022 .channels = 2,
3023 .fmt_type = USB_FORMAT_TYPE_I,
3024 .altsetting = 1,
3025 .altset_idx = 1,
3026 .rates = SNDRV_PCM_RATE_CONTINUOUS,
3027 };
3028 struct usb_host_interface *alts;
3029 struct usb_interface_descriptor *altsd;
3030 struct audioformat *fp;
3031 int stream, err;
3032
Pedro Lopez-Cabanillas310e0dc2008-10-04 16:27:36 +02003033 /* both PCM and MIDI interfaces have 2 or more altsettings */
3034 if (iface->num_altsetting < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return -ENXIO;
3036 alts = &iface->altsetting[1];
3037 altsd = get_iface_desc(alts);
3038
Pedro Lopez-Cabanillas59b3db62008-10-07 20:54:18 +02003039 if (altsd->bNumEndpoints == 2) {
3040 static const struct snd_usb_midi_endpoint_info ua700_ep = {
3041 .out_cables = 0x0003,
3042 .in_cables = 0x0003
3043 };
3044 static const struct snd_usb_audio_quirk ua700_quirk = {
3045 .type = QUIRK_MIDI_FIXED_ENDPOINT,
3046 .data = &ua700_ep
3047 };
3048 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
3049 .out_cables = 0x0001,
3050 .in_cables = 0x0001
3051 };
3052 static const struct snd_usb_audio_quirk uaxx_quirk = {
3053 .type = QUIRK_MIDI_FIXED_ENDPOINT,
3054 .data = &uaxx_ep
3055 };
Clemens Ladischd82af9f2009-11-16 12:23:46 +01003056 const struct snd_usb_audio_quirk *quirk =
3057 chip->usb_id == USB_ID(0x0582, 0x002b)
3058 ? &ua700_quirk : &uaxx_quirk;
3059 return snd_usbmidi_create(chip->card, iface,
3060 &chip->midi_list, quirk);
Pedro Lopez-Cabanillas59b3db62008-10-07 20:54:18 +02003061 }
3062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 if (altsd->bNumEndpoints != 1)
3064 return -ENXIO;
3065
3066 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
3067 if (!fp)
3068 return -ENOMEM;
3069 memcpy(fp, &ua_format, sizeof(*fp));
3070
3071 fp->iface = altsd->bInterfaceNumber;
3072 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
3073 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
Clemens Ladisch744b89e2009-04-03 09:45:01 +02003074 fp->datainterval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
3076
3077 switch (fp->maxpacksize) {
3078 case 0x120:
3079 fp->rate_max = fp->rate_min = 44100;
3080 break;
3081 case 0x138:
3082 case 0x140:
3083 fp->rate_max = fp->rate_min = 48000;
3084 break;
3085 case 0x258:
3086 case 0x260:
3087 fp->rate_max = fp->rate_min = 96000;
3088 break;
3089 default:
3090 snd_printk(KERN_ERR "unknown sample rate\n");
3091 kfree(fp);
3092 return -ENXIO;
3093 }
3094
3095 stream = (fp->endpoint & USB_DIR_IN)
3096 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
3097 err = add_audio_endpoint(chip, stream, fp);
3098 if (err < 0) {
3099 kfree(fp);
3100 return err;
3101 }
3102 usb_set_interface(chip->dev, fp->iface, 0);
3103 return 0;
3104}
3105
3106/*
3107 * Create a stream for an Edirol UA-1000 interface.
3108 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003109static int create_ua1000_quirk(struct snd_usb_audio *chip,
Clemens Ladisch854af952005-07-25 16:19:10 +02003110 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003111 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
3113 static const struct audioformat ua1000_format = {
3114 .format = SNDRV_PCM_FORMAT_S32_LE,
3115 .fmt_type = USB_FORMAT_TYPE_I,
3116 .altsetting = 1,
3117 .altset_idx = 1,
3118 .attributes = 0,
3119 .rates = SNDRV_PCM_RATE_CONTINUOUS,
3120 };
3121 struct usb_host_interface *alts;
3122 struct usb_interface_descriptor *altsd;
3123 struct audioformat *fp;
3124 int stream, err;
3125
3126 if (iface->num_altsetting != 2)
3127 return -ENXIO;
3128 alts = &iface->altsetting[1];
3129 altsd = get_iface_desc(alts);
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02003130 if (alts->extralen != 11 || alts->extra[1] != USB_DT_CS_INTERFACE ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 altsd->bNumEndpoints != 1)
3132 return -ENXIO;
3133
Alexey Dobriyan52978be2006-09-30 23:27:21 -07003134 fp = kmemdup(&ua1000_format, sizeof(*fp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 if (!fp)
3136 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
3138 fp->channels = alts->extra[4];
3139 fp->iface = altsd->bInterfaceNumber;
3140 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
3141 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
Clemens Ladisch744b89e2009-04-03 09:45:01 +02003142 fp->datainterval = parse_datainterval(chip, alts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
3144 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]);
3145
3146 stream = (fp->endpoint & USB_DIR_IN)
3147 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
3148 err = add_audio_endpoint(chip, stream, fp);
3149 if (err < 0) {
3150 kfree(fp);
3151 return err;
3152 }
3153 /* FIXME: playback must be synchronized to capture */
3154 usb_set_interface(chip->dev, fp->iface, 0);
3155 return 0;
3156}
3157
Takashi Iwai86e07d32005-11-17 15:08:02 +01003158static int snd_usb_create_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003160 const struct snd_usb_audio_quirk *quirk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
3162/*
3163 * handle the quirks for the contained interfaces
3164 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003165static int create_composite_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003167 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
3169 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
3170 int err;
3171
3172 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
3173 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
3174 if (!iface)
3175 continue;
3176 if (quirk->ifnum != probed_ifnum &&
3177 usb_interface_claimed(iface))
3178 continue;
3179 err = snd_usb_create_quirk(chip, iface, quirk);
3180 if (err < 0)
3181 return err;
3182 if (quirk->ifnum != probed_ifnum)
3183 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
3184 }
3185 return 0;
3186}
3187
Takashi Iwai86e07d32005-11-17 15:08:02 +01003188static int ignore_interface_quirk(struct snd_usb_audio *chip,
Clemens Ladisch854af952005-07-25 16:19:10 +02003189 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003190 const struct snd_usb_audio_quirk *quirk)
Clemens Ladisch854af952005-07-25 16:19:10 +02003191{
3192 return 0;
3193}
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196/*
3197 * boot quirks
3198 */
3199
3200#define EXTIGY_FIRMWARE_SIZE_OLD 794
3201#define EXTIGY_FIRMWARE_SIZE_NEW 483
3202
3203static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
3204{
3205 struct usb_host_config *config = dev->actconfig;
3206 int err;
3207
3208 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
3209 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
3210 snd_printdd("sending Extigy boot sequence...\n");
3211 /* Send message to force it to reconnect with full interface. */
3212 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
3213 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
3214 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
3215 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
3216 &dev->descriptor, sizeof(dev->descriptor));
3217 config = dev->actconfig;
3218 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
3219 err = usb_reset_configuration(dev);
3220 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
3221 snd_printdd("extigy_boot: new boot length = %d\n",
3222 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
3223 return -ENODEV; /* quit this anyway */
3224 }
3225 return 0;
3226}
3227
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003228static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
3229{
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003230 u8 buf = 1;
3231
3232 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
3233 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3234 0, 0, &buf, 1, 1000);
3235 if (buf == 0) {
3236 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
3237 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3238 1, 2000, NULL, 0, 1000);
3239 return -ENODEV;
3240 }
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003241 return 0;
3242}
3243
Thibault LE MEURe3113342006-03-14 11:44:53 +01003244/*
Sam Revitche217e302006-06-23 15:10:18 +02003245 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
3246 * documented in the device's data sheet.
3247 */
3248static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
3249{
3250 u8 buf[4];
3251 buf[0] = 0x20;
3252 buf[1] = value & 0xff;
3253 buf[2] = (value >> 8) & 0xff;
3254 buf[3] = reg;
3255 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
3256 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
3257 0, 0, &buf, 4, 1000);
3258}
3259
3260static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
3261{
3262 /*
3263 * Enable line-out driver mode, set headphone source to front
3264 * channels, enable stereo mic.
3265 */
3266 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
3267}
3268
Dan Allongo92a43792009-06-08 11:21:52 -04003269/*
3270 * C-Media CM6206 is based on CM106 with two additional
3271 * registers that are not documented in the data sheet.
3272 * Values here are chosen based on sniffing USB traffic
3273 * under Windows.
3274 */
3275static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
3276{
3277 int err, reg;
3278 int val[] = {0x200c, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
3279
3280 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
3281 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
3282 if (err < 0)
3283 return err;
3284 }
3285
3286 return err;
3287}
Sam Revitche217e302006-06-23 15:10:18 +02003288
3289/*
Thibault LE MEURe3113342006-03-14 11:44:53 +01003290 * Setup quirks
3291 */
3292#define AUDIOPHILE_SET 0x01 /* if set, parse device_setup */
3293#define AUDIOPHILE_SET_DTS 0x02 /* if set, enable DTS Digital Output */
3294#define AUDIOPHILE_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
3295#define AUDIOPHILE_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
3296#define AUDIOPHILE_SET_DI 0x10 /* if set, enable Digital Input */
3297#define AUDIOPHILE_SET_MASK 0x1F /* bit mask for setup value */
3298#define AUDIOPHILE_SET_24B_48K_DI 0x19 /* value for 24bits+48KHz+Digital Input */
3299#define AUDIOPHILE_SET_24B_48K_NOTDI 0x09 /* value for 24bits+48KHz+No Digital Input */
3300#define AUDIOPHILE_SET_16B_48K_DI 0x11 /* value for 16bits+48KHz+Digital Input */
3301#define AUDIOPHILE_SET_16B_48K_NOTDI 0x01 /* value for 16bits+48KHz+No Digital Input */
3302
3303static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
3304 int iface, int altno)
3305{
Thibault Le Meurf8c78b82007-07-12 11:26:35 +02003306 /* Reset ALL ifaces to 0 altsetting.
3307 * Call it for every possible altsetting of every interface.
3308 */
3309 usb_set_interface(chip->dev, iface, 0);
3310
Thibault LE MEURe3113342006-03-14 11:44:53 +01003311 if (device_setup[chip->index] & AUDIOPHILE_SET) {
3312 if ((device_setup[chip->index] & AUDIOPHILE_SET_DTS)
3313 && altno != 6)
3314 return 1; /* skip this altsetting */
3315 if ((device_setup[chip->index] & AUDIOPHILE_SET_96K)
3316 && altno != 1)
3317 return 1; /* skip this altsetting */
3318 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3319 AUDIOPHILE_SET_24B_48K_DI && altno != 2)
3320 return 1; /* skip this altsetting */
3321 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3322 AUDIOPHILE_SET_24B_48K_NOTDI && altno != 3)
3323 return 1; /* skip this altsetting */
3324 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3325 AUDIOPHILE_SET_16B_48K_DI && altno != 4)
3326 return 1; /* skip this altsetting */
3327 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3328 AUDIOPHILE_SET_16B_48K_NOTDI && altno != 5)
3329 return 1; /* skip this altsetting */
3330 }
3331 return 0; /* keep this altsetting */
3332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Clemens Ladischd82af9f2009-11-16 12:23:46 +01003334static int create_any_midi_quirk(struct snd_usb_audio *chip,
3335 struct usb_interface *intf,
3336 const struct snd_usb_audio_quirk *quirk)
3337{
3338 return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
3339}
3340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341/*
3342 * audio-interface quirks
3343 *
3344 * returns zero if no standard audio/MIDI parsing is needed.
3345 * returns a postive value if standard audio/midi interfaces are parsed
3346 * after this.
3347 * returns a negative value at error.
3348 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003349static int snd_usb_create_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003351 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003353 typedef int (*quirk_func_t)(struct snd_usb_audio *, struct usb_interface *,
3354 const struct snd_usb_audio_quirk *);
Clemens Ladisch854af952005-07-25 16:19:10 +02003355 static const quirk_func_t quirk_funcs[] = {
3356 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
3357 [QUIRK_COMPOSITE] = create_composite_quirk,
Clemens Ladischd82af9f2009-11-16 12:23:46 +01003358 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
3359 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
3360 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
3361 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
3362 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
3363 [QUIRK_MIDI_FASTLANE] = create_any_midi_quirk,
3364 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
3365 [QUIRK_MIDI_CME] = create_any_midi_quirk,
Clemens Ladischd1bda042005-09-14 08:36:03 +02003366 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
Clemens Ladisch854af952005-07-25 16:19:10 +02003367 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
Clemens Ladisch854af952005-07-25 16:19:10 +02003368 [QUIRK_AUDIO_EDIROL_UA1000] = create_ua1000_quirk,
Pedro Lopez-Cabanillas310e0dc2008-10-04 16:27:36 +02003369 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk
Clemens Ladisch854af952005-07-25 16:19:10 +02003370 };
3371
3372 if (quirk->type < QUIRK_TYPE_COUNT) {
3373 return quirk_funcs[quirk->type](chip, iface, quirk);
3374 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
3376 return -ENXIO;
3377 }
3378}
3379
3380
3381/*
3382 * common proc files to show the usb device info
3383 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003384static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003386 struct snd_usb_audio *chip = entry->private_data;
Pavel Machek07f51a72008-04-14 13:15:56 +02003387 if (!chip->shutdown)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
3389}
3390
Takashi Iwai86e07d32005-11-17 15:08:02 +01003391static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003393 struct snd_usb_audio *chip = entry->private_data;
Pavel Machek07f51a72008-04-14 13:15:56 +02003394 if (!chip->shutdown)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 snd_iprintf(buffer, "%04x:%04x\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003396 USB_ID_VENDOR(chip->usb_id),
3397 USB_ID_PRODUCT(chip->usb_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398}
3399
Takashi Iwai86e07d32005-11-17 15:08:02 +01003400static void snd_usb_audio_create_proc(struct snd_usb_audio *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003402 struct snd_info_entry *entry;
Pavel Machek07f51a72008-04-14 13:15:56 +02003403 if (!snd_card_proc_new(chip->card, "usbbus", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02003404 snd_info_set_text_ops(entry, chip, proc_audio_usbbus_read);
Pavel Machek07f51a72008-04-14 13:15:56 +02003405 if (!snd_card_proc_new(chip->card, "usbid", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02003406 snd_info_set_text_ops(entry, chip, proc_audio_usbid_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
3409/*
3410 * free the chip instance
3411 *
3412 * here we have to do not much, since pcm and controls are already freed
3413 *
3414 */
3415
Takashi Iwai86e07d32005-11-17 15:08:02 +01003416static int snd_usb_audio_free(struct snd_usb_audio *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
3418 kfree(chip);
3419 return 0;
3420}
3421
Takashi Iwai86e07d32005-11-17 15:08:02 +01003422static int snd_usb_audio_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003424 struct snd_usb_audio *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return snd_usb_audio_free(chip);
3426}
3427
3428
3429/*
3430 * create a chip instance and set its names.
3431 */
3432static int snd_usb_audio_create(struct usb_device *dev, int idx,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003433 const struct snd_usb_audio_quirk *quirk,
3434 struct snd_usb_audio **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003436 struct snd_card *card;
3437 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 int err, len;
3439 char component[14];
Takashi Iwai86e07d32005-11-17 15:08:02 +01003440 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 .dev_free = snd_usb_audio_dev_free,
3442 };
3443
3444 *rchip = NULL;
3445
Clemens Ladisch076639f2007-08-21 08:56:54 +02003446 if (snd_usb_get_speed(dev) != USB_SPEED_LOW &&
3447 snd_usb_get_speed(dev) != USB_SPEED_FULL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
3449 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
3450 return -ENXIO;
3451 }
3452
Takashi Iwaibd7dd772008-12-28 16:45:02 +01003453 err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
3454 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01003456 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
3458
Takashi Iwai561b2202005-09-09 14:22:34 +02003459 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 if (! chip) {
3461 snd_card_free(card);
3462 return -ENOMEM;
3463 }
3464
3465 chip->index = idx;
3466 chip->dev = dev;
3467 chip->card = card;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003468 chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3469 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 INIT_LIST_HEAD(&chip->pcm_list);
3471 INIT_LIST_HEAD(&chip->midi_list);
Clemens Ladisch84957a82005-04-29 16:23:13 +02003472 INIT_LIST_HEAD(&chip->mixer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
3474 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
3475 snd_usb_audio_free(chip);
3476 snd_card_free(card);
3477 return err;
3478 }
3479
3480 strcpy(card->driver, "USB-Audio");
3481 sprintf(component, "USB%04x:%04x",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003482 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 snd_component_add(card, component);
3484
3485 /* retrieve the device string as shortname */
3486 if (quirk && quirk->product_name) {
3487 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
3488 } else {
3489 if (!dev->descriptor.iProduct ||
3490 usb_string(dev, dev->descriptor.iProduct,
3491 card->shortname, sizeof(card->shortname)) <= 0) {
3492 /* no name available from anywhere, so use ID */
3493 sprintf(card->shortname, "USB Device %#04x:%#04x",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003494 USB_ID_VENDOR(chip->usb_id),
3495 USB_ID_PRODUCT(chip->usb_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
3497 }
3498
3499 /* retrieve the vendor and device strings as longname */
3500 if (quirk && quirk->vendor_name) {
3501 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
3502 } else {
3503 if (dev->descriptor.iManufacturer)
3504 len = usb_string(dev, dev->descriptor.iManufacturer,
3505 card->longname, sizeof(card->longname));
3506 else
3507 len = 0;
3508 /* we don't really care if there isn't any vendor string */
3509 }
3510 if (len > 0)
3511 strlcat(card->longname, " ", sizeof(card->longname));
3512
3513 strlcat(card->longname, card->shortname, sizeof(card->longname));
3514
3515 len = strlcat(card->longname, " at ", sizeof(card->longname));
3516
3517 if (len < sizeof(card->longname))
3518 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
3519
3520 strlcat(card->longname,
Clemens Ladisch076639f2007-08-21 08:56:54 +02003521 snd_usb_get_speed(dev) == USB_SPEED_LOW ? ", low speed" :
3522 snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" :
3523 ", high speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 sizeof(card->longname));
3525
3526 snd_usb_audio_create_proc(chip);
3527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 *rchip = chip;
3529 return 0;
3530}
3531
3532
3533/*
3534 * probe the active usb device
3535 *
3536 * note that this can be called multiple times per a device, when it
3537 * includes multiple audio control interfaces.
3538 *
3539 * thus we check the usb device pointer and creates the card instance
3540 * only at the first time. the successive calls of this function will
3541 * append the pcm interface to the corresponding card.
3542 */
3543static void *snd_usb_audio_probe(struct usb_device *dev,
3544 struct usb_interface *intf,
3545 const struct usb_device_id *usb_id)
3546{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003547 const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 int i, err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01003549 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 struct usb_host_interface *alts;
3551 int ifnum;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003552 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
3554 alts = &intf->altsetting[0];
3555 ifnum = get_iface_desc(alts)->bInterfaceNumber;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003556 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3557 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
3559 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
3560 goto __err_val;
3561
3562 /* SB Extigy needs special boot-up sequence */
3563 /* if more models come, this will go to the quirk list. */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003564 if (id == USB_ID(0x041e, 0x3000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 if (snd_usb_extigy_boot_quirk(dev, intf) < 0)
3566 goto __err_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 }
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003568 /* SB Audigy 2 NX needs its own boot-up magic, too */
3569 if (id == USB_ID(0x041e, 0x3020)) {
3570 if (snd_usb_audigy2nx_boot_quirk(dev) < 0)
3571 goto __err_val;
3572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Sam Revitche217e302006-06-23 15:10:18 +02003574 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
3575 if (id == USB_ID(0x10f5, 0x0200)) {
3576 if (snd_usb_cm106_boot_quirk(dev) < 0)
3577 goto __err_val;
3578 }
3579
Dan Allongo92a43792009-06-08 11:21:52 -04003580 /* C-Media CM6206 / CM106-Like Sound Device */
3581 if (id == USB_ID(0x0d8c, 0x0102)) {
3582 if (snd_usb_cm6206_boot_quirk(dev) < 0)
3583 goto __err_val;
3584 }
3585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 /*
3587 * found a config. now register to ALSA
3588 */
3589
3590 /* check whether it's already registered */
3591 chip = NULL;
Ingo Molnar12aa7572006-01-16 16:36:05 +01003592 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 for (i = 0; i < SNDRV_CARDS; i++) {
3594 if (usb_chip[i] && usb_chip[i]->dev == dev) {
3595 if (usb_chip[i]->shutdown) {
3596 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
3597 goto __error;
3598 }
3599 chip = usb_chip[i];
3600 break;
3601 }
3602 }
3603 if (! chip) {
3604 /* it's a fresh one.
3605 * now look for an empty slot and create a new card instance
3606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 for (i = 0; i < SNDRV_CARDS; i++)
3608 if (enable[i] && ! usb_chip[i] &&
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003609 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
3610 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
3612 goto __error;
3613 }
Clemens Ladisch1dcd3ec2005-05-10 14:51:40 +02003614 snd_card_set_dev(chip->card, &intf->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 break;
3616 }
Pavel Machek07f51a72008-04-14 13:15:56 +02003617 if (!chip) {
3618 printk(KERN_ERR "no available usb audio device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 goto __error;
3620 }
3621 }
3622
3623 err = 1; /* continue */
3624 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
3625 /* need some special handlings */
3626 if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)
3627 goto __error;
3628 }
3629
3630 if (err > 0) {
3631 /* create normal USB audio interfaces */
3632 if (snd_usb_create_streams(chip, ifnum) < 0 ||
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003633 snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 goto __error;
3635 }
3636 }
3637
3638 /* we are allowed to call snd_card_register() many times */
3639 if (snd_card_register(chip->card) < 0) {
3640 goto __error;
3641 }
3642
3643 usb_chip[chip->index] = chip;
3644 chip->num_interfaces++;
Ingo Molnar12aa7572006-01-16 16:36:05 +01003645 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return chip;
3647
3648 __error:
3649 if (chip && !chip->num_interfaces)
3650 snd_card_free(chip->card);
Ingo Molnar12aa7572006-01-16 16:36:05 +01003651 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 __err_val:
3653 return NULL;
3654}
3655
3656/*
3657 * we need to take care of counter, since disconnection can be called also
3658 * many times as well as usb_audio_probe().
3659 */
3660static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
3661{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003662 struct snd_usb_audio *chip;
3663 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 struct list_head *p;
3665
3666 if (ptr == (void *)-1L)
3667 return;
3668
3669 chip = ptr;
3670 card = chip->card;
Ingo Molnar12aa7572006-01-16 16:36:05 +01003671 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 chip->shutdown = 1;
3673 chip->num_interfaces--;
3674 if (chip->num_interfaces <= 0) {
3675 snd_card_disconnect(card);
3676 /* release the pcm resources */
3677 list_for_each(p, &chip->pcm_list) {
Clemens Ladischee733392005-04-25 10:34:13 +02003678 snd_usb_stream_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 }
3680 /* release the midi resources */
3681 list_for_each(p, &chip->midi_list) {
Clemens Ladischee733392005-04-25 10:34:13 +02003682 snd_usbmidi_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02003684 /* release mixer resources */
3685 list_for_each(p, &chip->mixer_list) {
3686 snd_usb_mixer_disconnect(p);
3687 }
Takashi Iwai9eb70e62008-04-17 12:53:26 +02003688 usb_chip[chip->index] = NULL;
Ingo Molnar12aa7572006-01-16 16:36:05 +01003689 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02003690 snd_card_free_when_closed(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 } else {
Ingo Molnar12aa7572006-01-16 16:36:05 +01003692 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 }
3694}
3695
3696/*
3697 * new 2.5 USB kernel API
3698 */
3699static int usb_audio_probe(struct usb_interface *intf,
3700 const struct usb_device_id *id)
3701{
3702 void *chip;
3703 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
3704 if (chip) {
Julia Lawallf4e97492009-01-01 18:14:35 +01003705 usb_set_intfdata(intf, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 return 0;
3707 } else
3708 return -EIO;
3709}
3710
3711static void usb_audio_disconnect(struct usb_interface *intf)
3712{
3713 snd_usb_audio_disconnect(interface_to_usbdev(intf),
Julia Lawallf4e97492009-01-01 18:14:35 +01003714 usb_get_intfdata(intf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715}
3716
Andrew Morton93521d22007-12-24 14:40:56 +01003717#ifdef CONFIG_PM
Oliver Neukumf85bf292007-12-14 14:42:41 +01003718static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
3719{
Julia Lawallf4e97492009-01-01 18:14:35 +01003720 struct snd_usb_audio *chip = usb_get_intfdata(intf);
Oliver Neukumf85bf292007-12-14 14:42:41 +01003721 struct list_head *p;
3722 struct snd_usb_stream *as;
3723
3724 if (chip == (void *)-1L)
3725 return 0;
3726
3727 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
3728 if (!chip->num_suspended_intf++) {
3729 list_for_each(p, &chip->pcm_list) {
3730 as = list_entry(p, struct snd_usb_stream, list);
3731 snd_pcm_suspend_all(as->pcm);
3732 }
3733 }
3734
3735 return 0;
3736}
3737
3738static int usb_audio_resume(struct usb_interface *intf)
3739{
Julia Lawallf4e97492009-01-01 18:14:35 +01003740 struct snd_usb_audio *chip = usb_get_intfdata(intf);
Oliver Neukumf85bf292007-12-14 14:42:41 +01003741
3742 if (chip == (void *)-1L)
3743 return 0;
3744 if (--chip->num_suspended_intf)
3745 return 0;
3746 /*
3747 * ALSA leaves material resumption to user space
3748 * we just notify
3749 */
3750
3751 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
3752
3753 return 0;
3754}
Andrew Morton93521d22007-12-24 14:40:56 +01003755#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
3757static int __init snd_usb_audio_init(void)
3758{
Clemens Ladischf3990e62009-02-20 09:32:40 +01003759 if (nrpacks < 1 || nrpacks > MAX_PACKS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 printk(KERN_WARNING "invalid nrpacks value.\n");
3761 return -EINVAL;
3762 }
Tobias Klausercf78bbc2006-10-04 18:12:43 +02003763 return usb_register(&usb_audio_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764}
3765
3766
3767static void __exit snd_usb_audio_cleanup(void)
3768{
3769 usb_deregister(&usb_audio_driver);
3770}
3771
3772module_init(snd_usb_audio_init);
3773module_exit(snd_usb_audio_cleanup);