blob: 8fcb5d5a94b62254628d494e2025117f0d8745fd [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
Clemens Ladischadc8d312009-12-27 12:19:57 -0500176 unsigned int hwptr_done; /* processed byte 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;
Clemens Ladischadc8d312009-12-27 12:19:57 -0500345 unsigned int stride, frames, bytes, 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 }
Clemens Ladischadc8d312009-12-27 12:19:57 -0500356 frames = urb->iso_frame_desc[i].actual_length / stride;
357 bytes = frames * stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /* update the current pointer */
359 spin_lock_irqsave(&subs->lock, flags);
360 oldptr = subs->hwptr_done;
Clemens Ladischadc8d312009-12-27 12:19:57 -0500361 subs->hwptr_done += bytes;
362 if (subs->hwptr_done >= runtime->buffer_size * stride)
363 subs->hwptr_done -= runtime->buffer_size * stride;
364 subs->transfer_done += frames;
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200365 if (subs->transfer_done >= runtime->period_size) {
366 subs->transfer_done -= runtime->period_size;
367 period_elapsed = 1;
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_unlock_irqrestore(&subs->lock, flags);
370 /* copy a data chunk */
Clemens Ladischadc8d312009-12-27 12:19:57 -0500371 if (oldptr + bytes > runtime->buffer_size * stride) {
372 unsigned int bytes1 =
373 runtime->buffer_size * stride - oldptr;
374 memcpy(runtime->dma_area + oldptr, cp, bytes1);
375 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 } else {
Clemens Ladischadc8d312009-12-27 12:19:57 -0500377 memcpy(runtime->dma_area + oldptr, cp, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Clemens Ladischb263a9b2005-08-15 08:22:39 +0200380 if (period_elapsed)
381 snd_pcm_period_elapsed(subs->pcm_substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
383}
384
Clemens Ladische4f8e652006-10-04 13:42:57 +0200385/*
386 * Process after capture complete when paused. Nothing to do.
387 */
388static int retire_paused_capture_urb(struct snd_usb_substream *subs,
389 struct snd_pcm_runtime *runtime,
390 struct urb *urb)
391{
392 return 0;
393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396/*
397 * prepare urb for full speed playback sync pipe
398 *
399 * set up the offset and length to receive the current frequency.
400 */
401
Takashi Iwai86e07d32005-11-17 15:08:02 +0100402static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
403 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct urb *urb)
405{
John Daiker88518272006-12-28 13:55:05 +0100406 struct snd_urb_ctx *ctx = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200409 urb->iso_frame_desc[0].length = 3;
410 urb->iso_frame_desc[0].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return 0;
412}
413
414/*
415 * prepare urb for high speed playback sync pipe
416 *
417 * set up the offset and length to receive the current frequency.
418 */
419
Takashi Iwai86e07d32005-11-17 15:08:02 +0100420static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
421 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct urb *urb)
423{
John Daiker88518272006-12-28 13:55:05 +0100424 struct snd_urb_ctx *ctx = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 urb->dev = ctx->subs->dev; /* we need to set this at each time */
Clemens Ladischca810902005-05-02 08:55:54 +0200427 urb->iso_frame_desc[0].length = 4;
428 urb->iso_frame_desc[0].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
432/*
433 * process after full speed playback sync complete
434 *
435 * retrieve the current 10.14 frequency from pipe, and set it.
436 * the value is referred in prepare_playback_urb().
437 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100438static int retire_playback_sync_urb(struct snd_usb_substream *subs,
439 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct urb *urb)
441{
Clemens Ladischca810902005-05-02 08:55:54 +0200442 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 unsigned long flags;
444
Clemens Ladischca810902005-05-02 08:55:54 +0200445 if (urb->iso_frame_desc[0].status == 0 &&
446 urb->iso_frame_desc[0].actual_length == 3) {
447 f = combine_triple((u8*)urb->transfer_buffer) << 2;
Clemens Ladisch50cdbf12005-05-13 07:44:13 +0200448 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
449 spin_lock_irqsave(&subs->lock, flags);
450 subs->freqm = f;
451 spin_unlock_irqrestore(&subs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
455 return 0;
456}
457
458/*
459 * process after high speed playback sync complete
460 *
461 * retrieve the current 12.13 frequency from pipe, and set it.
462 * the value is referred in prepare_playback_urb().
463 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100464static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
465 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct urb *urb)
467{
Clemens Ladischca810902005-05-02 08:55:54 +0200468 unsigned int f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 unsigned long flags;
470
Clemens Ladischca810902005-05-02 08:55:54 +0200471 if (urb->iso_frame_desc[0].status == 0 &&
472 urb->iso_frame_desc[0].actual_length == 4) {
473 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
Clemens Ladisch50cdbf12005-05-13 07:44:13 +0200474 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
475 spin_lock_irqsave(&subs->lock, flags);
476 subs->freqm = f;
477 spin_unlock_irqrestore(&subs->lock, flags);
478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480
481 return 0;
482}
483
Clemens Ladischd5132022008-02-25 11:01:00 +0100484/*
Eran Tromer97c889a2008-09-26 01:07:03 -0400485 * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete
Clemens Ladischd5132022008-02-25 11:01:00 +0100486 *
487 * These devices return the number of samples per packet instead of the number
488 * of samples per microframe.
489 */
490static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs,
491 struct snd_pcm_runtime *runtime,
492 struct urb *urb)
493{
494 unsigned int f;
495 unsigned long flags;
496
497 if (urb->iso_frame_desc[0].status == 0 &&
498 urb->iso_frame_desc[0].actual_length == 4) {
499 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
500 f >>= subs->datainterval;
501 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
502 spin_lock_irqsave(&subs->lock, flags);
503 subs->freqm = f;
504 spin_unlock_irqrestore(&subs->lock, flags);
505 }
506 }
507
508 return 0;
509}
510
Clemens Ladisch9568f462006-01-12 08:19:21 +0100511/* determine the number of frames in the next packet */
512static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
513{
514 if (subs->fill_max)
515 return subs->maxframesize;
516 else {
517 subs->phase = (subs->phase & 0xffff)
518 + (subs->freqm << subs->datainterval);
519 return min(subs->phase >> 16, subs->maxframesize);
520 }
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/*
Clemens Ladische4f8e652006-10-04 13:42:57 +0200524 * Prepare urb for streaming before playback starts or when paused.
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100525 *
Clemens Ladischb7eb4a02009-01-26 08:08:34 +0100526 * We don't have any data, so we send silence.
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100527 */
Clemens Ladische4f8e652006-10-04 13:42:57 +0200528static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
529 struct snd_pcm_runtime *runtime,
530 struct urb *urb)
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100531{
Clemens Ladisch4b284922006-01-12 08:17:49 +0100532 unsigned int i, offs, counts;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100533 struct snd_urb_ctx *ctx = urb->context;
Clemens Ladisch4b284922006-01-12 08:17:49 +0100534 int stride = runtime->frame_bits >> 3;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100535
Clemens Ladisch4b284922006-01-12 08:17:49 +0100536 offs = 0;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100537 urb->dev = ctx->subs->dev;
Clemens Ladischb7eb4a02009-01-26 08:08:34 +0100538 for (i = 0; i < ctx->packets; ++i) {
Clemens Ladisch9568f462006-01-12 08:19:21 +0100539 counts = snd_usb_audio_next_packet_size(subs);
Clemens Ladisch4b284922006-01-12 08:17:49 +0100540 urb->iso_frame_desc[i].offset = offs * stride;
541 urb->iso_frame_desc[i].length = counts * stride;
542 offs += counts;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100543 }
Clemens Ladischb7eb4a02009-01-26 08:08:34 +0100544 urb->number_of_packets = ctx->packets;
Clemens Ladisch4b284922006-01-12 08:17:49 +0100545 urb->transfer_buffer_length = offs * stride;
546 memset(urb->transfer_buffer,
547 subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
548 offs * stride);
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100549 return 0;
550}
551
552/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * prepare urb for playback data pipe
554 *
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200555 * Since a URB can handle only a single linear buffer, we must use double
556 * buffering when the data to be transferred overflows the buffer boundary.
557 * To avoid inconsistencies when updating hwptr_done, we use double buffering
558 * for all URBs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100560static int prepare_playback_urb(struct snd_usb_substream *subs,
561 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct urb *urb)
563{
Clemens Ladischadc8d312009-12-27 12:19:57 -0500564 int i, stride;
565 unsigned int counts, frames, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 unsigned long flags;
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200567 int period_elapsed = 0;
John Daiker88518272006-12-28 13:55:05 +0100568 struct snd_urb_ctx *ctx = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 stride = runtime->frame_bits >> 3;
571
Clemens Ladischadc8d312009-12-27 12:19:57 -0500572 frames = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 urb->dev = ctx->subs->dev; /* we need to set this at each time */
574 urb->number_of_packets = 0;
575 spin_lock_irqsave(&subs->lock, flags);
576 for (i = 0; i < ctx->packets; i++) {
Clemens Ladisch9568f462006-01-12 08:19:21 +0100577 counts = snd_usb_audio_next_packet_size(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* set up descriptor */
Clemens Ladischadc8d312009-12-27 12:19:57 -0500579 urb->iso_frame_desc[i].offset = frames * stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 urb->iso_frame_desc[i].length = counts * stride;
Clemens Ladischadc8d312009-12-27 12:19:57 -0500581 frames += counts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 urb->number_of_packets++;
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200583 subs->transfer_done += counts;
584 if (subs->transfer_done >= runtime->period_size) {
585 subs->transfer_done -= runtime->period_size;
586 period_elapsed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (subs->fmt_type == USB_FORMAT_TYPE_II) {
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200588 if (subs->transfer_done > 0) {
589 /* FIXME: fill-max mode is not
590 * supported yet */
Clemens Ladischadc8d312009-12-27 12:19:57 -0500591 frames -= subs->transfer_done;
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200592 counts -= subs->transfer_done;
593 urb->iso_frame_desc[i].length =
594 counts * stride;
595 subs->transfer_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597 i++;
598 if (i < ctx->packets) {
599 /* add a transfer delimiter */
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200600 urb->iso_frame_desc[i].offset =
Clemens Ladischadc8d312009-12-27 12:19:57 -0500601 frames * stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 urb->iso_frame_desc[i].length = 0;
603 urb->number_of_packets++;
604 }
Clemens Ladisch9624ea82005-08-15 08:25:24 +0200605 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Clemens Ladischa7d9c092009-04-03 09:48:26 +0200608 if (period_elapsed) /* finish at the period boundary */
Clemens Ladisch9624ea82005-08-15 08:25:24 +0200609 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Clemens Ladischadc8d312009-12-27 12:19:57 -0500611 bytes = frames * stride;
612 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200613 /* err, the transferred area goes over buffer boundary. */
Clemens Ladischadc8d312009-12-27 12:19:57 -0500614 unsigned int bytes1 =
615 runtime->buffer_size * stride - subs->hwptr_done;
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200616 memcpy(urb->transfer_buffer,
Clemens Ladischadc8d312009-12-27 12:19:57 -0500617 runtime->dma_area + subs->hwptr_done, bytes1);
618 memcpy(urb->transfer_buffer + bytes1,
619 runtime->dma_area, bytes - bytes1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 } else {
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +0200621 memcpy(urb->transfer_buffer,
Clemens Ladischadc8d312009-12-27 12:19:57 -0500622 runtime->dma_area + subs->hwptr_done, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Clemens Ladischadc8d312009-12-27 12:19:57 -0500624 subs->hwptr_done += bytes;
625 if (subs->hwptr_done >= runtime->buffer_size * stride)
626 subs->hwptr_done -= runtime->buffer_size * stride;
627 runtime->delay += frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 spin_unlock_irqrestore(&subs->lock, flags);
Clemens Ladischadc8d312009-12-27 12:19:57 -0500629 urb->transfer_buffer_length = bytes;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100630 if (period_elapsed)
631 snd_pcm_period_elapsed(subs->pcm_substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return 0;
633}
634
635/*
636 * process after playback data complete
Takashi Iwaiae1ec5e2008-10-13 03:08:53 +0200637 * - decrease the delay count again
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100639static int retire_playback_urb(struct snd_usb_substream *subs,
640 struct snd_pcm_runtime *runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct urb *urb)
642{
Takashi Iwaiae1ec5e2008-10-13 03:08:53 +0200643 unsigned long flags;
644 int stride = runtime->frame_bits >> 3;
645 int processed = urb->transfer_buffer_length / stride;
646
647 spin_lock_irqsave(&subs->lock, flags);
648 if (processed > runtime->delay)
649 runtime->delay = 0;
650 else
651 runtime->delay -= processed;
652 spin_unlock_irqrestore(&subs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 0;
654}
655
656
657/*
658 */
659static struct snd_urb_ops audio_urb_ops[2] = {
660 {
Clemens Ladische4f8e652006-10-04 13:42:57 +0200661 .prepare = prepare_nodata_playback_urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 .retire = retire_playback_urb,
663 .prepare_sync = prepare_playback_sync_urb,
664 .retire_sync = retire_playback_sync_urb,
665 },
666 {
667 .prepare = prepare_capture_urb,
668 .retire = retire_capture_urb,
669 .prepare_sync = prepare_capture_sync_urb,
670 .retire_sync = retire_capture_sync_urb,
671 },
672};
673
674static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
675 {
Clemens Ladische4f8e652006-10-04 13:42:57 +0200676 .prepare = prepare_nodata_playback_urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 .retire = retire_playback_urb,
678 .prepare_sync = prepare_playback_sync_urb_hs,
679 .retire_sync = retire_playback_sync_urb_hs,
680 },
681 {
682 .prepare = prepare_capture_urb,
683 .retire = retire_capture_urb,
684 .prepare_sync = prepare_capture_sync_urb_hs,
685 .retire_sync = retire_capture_sync_urb,
686 },
687};
688
689/*
690 * complete callback from data urb
691 */
David Howells7d12e782006-10-05 14:55:46 +0100692static void snd_complete_urb(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
John Daiker88518272006-12-28 13:55:05 +0100694 struct snd_urb_ctx *ctx = urb->context;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100695 struct snd_usb_substream *subs = ctx->subs;
696 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 int err = 0;
698
699 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
Pavel Machek07f51a72008-04-14 13:15:56 +0200700 !subs->running || /* can be stopped during retire callback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
702 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
703 clear_bit(ctx->index, &subs->active_mask);
704 if (err < 0) {
705 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
706 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
707 }
708 }
709}
710
711
712/*
713 * complete callback from sync urb
714 */
David Howells7d12e782006-10-05 14:55:46 +0100715static void snd_complete_sync_urb(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
John Daiker88518272006-12-28 13:55:05 +0100717 struct snd_urb_ctx *ctx = urb->context;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100718 struct snd_usb_substream *subs = ctx->subs;
719 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 int err = 0;
721
722 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
Pavel Machek07f51a72008-04-14 13:15:56 +0200723 !subs->running || /* can be stopped during retire callback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
725 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
726 clear_bit(ctx->index + 16, &subs->active_mask);
727 if (err < 0) {
728 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
729 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
730 }
731 }
732}
733
734
735/*
736 * unlink active urbs.
737 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100738static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 unsigned int i;
741 int async;
742
743 subs->running = 0;
744
745 if (!force && subs->stream->chip->shutdown) /* to be sure... */
746 return -EBADFD;
747
748 async = !can_sleep && async_unlink;
749
Pavel Machek07f51a72008-04-14 13:15:56 +0200750 if (!async && in_interrupt())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return 0;
752
753 for (i = 0; i < subs->nurbs; i++) {
754 if (test_bit(i, &subs->active_mask)) {
Pavel Machek07f51a72008-04-14 13:15:56 +0200755 if (!test_and_set_bit(i, &subs->unlink_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct urb *u = subs->dataurb[i].urb;
Alan Sternb375a042005-07-29 16:11:07 -0400757 if (async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 usb_unlink_urb(u);
Alan Sternb375a042005-07-29 16:11:07 -0400759 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 usb_kill_urb(u);
761 }
762 }
763 }
764 if (subs->syncpipe) {
765 for (i = 0; i < SYNC_URBS; i++) {
766 if (test_bit(i+16, &subs->active_mask)) {
Pavel Machek07f51a72008-04-14 13:15:56 +0200767 if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct urb *u = subs->syncurb[i].urb;
Alan Sternb375a042005-07-29 16:11:07 -0400769 if (async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 usb_unlink_urb(u);
Alan Sternb375a042005-07-29 16:11:07 -0400771 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 usb_kill_urb(u);
773 }
774 }
775 }
776 }
777 return 0;
778}
779
780
Clemens Ladisch32e19e82006-03-09 07:58:39 +0100781static const char *usb_error_string(int err)
782{
783 switch (err) {
784 case -ENODEV:
785 return "no device";
786 case -ENOENT:
787 return "endpoint not enabled";
788 case -EPIPE:
789 return "endpoint stalled";
790 case -ENOSPC:
791 return "not enough bandwidth";
792 case -ESHUTDOWN:
793 return "device disabled";
794 case -EHOSTUNREACH:
795 return "device suspended";
796 case -EINVAL:
797 case -EAGAIN:
798 case -EFBIG:
799 case -EMSGSIZE:
800 return "internal error";
801 default:
802 return "unknown error";
803 }
804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806/*
807 * set up and start data/sync urbs
808 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100809static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 unsigned int i;
812 int err;
813
814 if (subs->stream->chip->shutdown)
815 return -EBADFD;
816
817 for (i = 0; i < subs->nurbs; i++) {
Takashi Iwai5e246b82008-08-08 17:12:47 +0200818 if (snd_BUG_ON(!subs->dataurb[i].urb))
819 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
821 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
822 goto __error;
823 }
824 }
825 if (subs->syncpipe) {
826 for (i = 0; i < SYNC_URBS; i++) {
Takashi Iwai5e246b82008-08-08 17:12:47 +0200827 if (snd_BUG_ON(!subs->syncurb[i].urb))
828 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
830 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
831 goto __error;
832 }
833 }
834 }
835
836 subs->active_mask = 0;
837 subs->unlink_mask = 0;
838 subs->running = 1;
839 for (i = 0; i < subs->nurbs; i++) {
Clemens Ladisch32e19e82006-03-09 07:58:39 +0100840 err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
841 if (err < 0) {
842 snd_printk(KERN_ERR "cannot submit datapipe "
843 "for urb %d, error %d: %s\n",
844 i, err, usb_error_string(err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 goto __error;
846 }
847 set_bit(i, &subs->active_mask);
848 }
849 if (subs->syncpipe) {
850 for (i = 0; i < SYNC_URBS; i++) {
Clemens Ladisch32e19e82006-03-09 07:58:39 +0100851 err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
852 if (err < 0) {
853 snd_printk(KERN_ERR "cannot submit syncpipe "
854 "for urb %d, error %d: %s\n",
855 i, err, usb_error_string(err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 goto __error;
857 }
858 set_bit(i + 16, &subs->active_mask);
859 }
860 }
861 return 0;
862
863 __error:
864 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
865 deactivate_urbs(subs, 0, 0);
866 return -EPIPE;
867}
868
869
870/*
871 * wait until all urbs are processed.
872 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100873static int wait_clear_urbs(struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Nishanth Aravamudanb27c1872005-07-09 10:54:37 +0200875 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned int i;
877 int alive;
878
879 do {
880 alive = 0;
881 for (i = 0; i < subs->nurbs; i++) {
882 if (test_bit(i, &subs->active_mask))
883 alive++;
884 }
885 if (subs->syncpipe) {
886 for (i = 0; i < SYNC_URBS; i++) {
887 if (test_bit(i + 16, &subs->active_mask))
888 alive++;
889 }
890 }
891 if (! alive)
892 break;
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200893 schedule_timeout_uninterruptible(1);
Nishanth Aravamudanb27c1872005-07-09 10:54:37 +0200894 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (alive)
896 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
897 return 0;
898}
899
900
901/*
Clemens Ladischadc8d312009-12-27 12:19:57 -0500902 * return the current pcm pointer. just based on the hwptr_done value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100904static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100906 struct snd_usb_substream *subs;
Clemens Ladischadc8d312009-12-27 12:19:57 -0500907 unsigned int hwptr_done;
Clemens Ladischdaa150e2005-08-15 08:25:50 +0200908
Takashi Iwai86e07d32005-11-17 15:08:02 +0100909 subs = (struct snd_usb_substream *)substream->runtime->private_data;
Clemens Ladischdaa150e2005-08-15 08:25:50 +0200910 spin_lock(&subs->lock);
911 hwptr_done = subs->hwptr_done;
912 spin_unlock(&subs->lock);
Clemens Ladischadc8d312009-12-27 12:19:57 -0500913 return hwptr_done / (substream->runtime->frame_bits >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916
917/*
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100918 * start/stop playback substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100920static int snd_usb_pcm_playback_trigger(struct snd_pcm_substream *substream,
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100921 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100923 struct snd_usb_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 switch (cmd) {
926 case SNDRV_PCM_TRIGGER_START:
Clemens Ladische4f8e652006-10-04 13:42:57 +0200927 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100928 subs->ops.prepare = prepare_playback_urb;
929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 case SNDRV_PCM_TRIGGER_STOP:
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100931 return deactivate_urbs(subs, 0, 0);
Clemens Ladische4f8e652006-10-04 13:42:57 +0200932 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
933 subs->ops.prepare = prepare_nodata_playback_urb;
934 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 default:
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100936 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100938}
939
940/*
941 * start/stop capture substream
942 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100943static int snd_usb_pcm_capture_trigger(struct snd_pcm_substream *substream,
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100944 int cmd)
945{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100946 struct snd_usb_substream *subs = substream->runtime->private_data;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100947
948 switch (cmd) {
949 case SNDRV_PCM_TRIGGER_START:
Clemens Ladische4f8e652006-10-04 13:42:57 +0200950 subs->ops.retire = retire_capture_urb;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100951 return start_urbs(subs, substream->runtime);
952 case SNDRV_PCM_TRIGGER_STOP:
953 return deactivate_urbs(subs, 0, 0);
Clemens Ladische4f8e652006-10-04 13:42:57 +0200954 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
955 subs->ops.retire = retire_paused_capture_urb;
956 return 0;
957 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
958 subs->ops.retire = retire_capture_urb;
959 return 0;
Clemens Ladischb55bbf02005-11-02 11:32:52 +0100960 default:
961 return -EINVAL;
962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965
966/*
967 * release a urb data
968 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100969static void release_urb_ctx(struct snd_urb_ctx *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 if (u->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200972 if (u->buffer_size)
973 usb_buffer_free(u->subs->dev, u->buffer_size,
974 u->urb->transfer_buffer,
975 u->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 usb_free_urb(u->urb);
977 u->urb = NULL;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
981/*
982 * release a substream
983 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100984static void release_substream_urbs(struct snd_usb_substream *subs, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 int i;
987
988 /* stop urbs (to be sure) */
989 deactivate_urbs(subs, force, 1);
990 wait_clear_urbs(subs);
991
992 for (i = 0; i < MAX_URBS; i++)
993 release_urb_ctx(&subs->dataurb[i]);
994 for (i = 0; i < SYNC_URBS; i++)
995 release_urb_ctx(&subs->syncurb[i]);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200996 usb_buffer_free(subs->dev, SYNC_URBS * 4,
997 subs->syncbuf, subs->sync_dma);
998 subs->syncbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 subs->nurbs = 0;
1000}
1001
1002/*
1003 * initialize a substream for plaback/capture
1004 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001005static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 unsigned int rate, unsigned int frame_bits)
1007{
Clemens Ladisch160389c2009-01-26 08:10:19 +01001008 unsigned int maxsize, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
Clemens Ladisch160389c2009-01-26 08:10:19 +01001010 unsigned int urb_packs, total_packs, packs_per_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 /* calculate the frequency in 16.16 format */
1013 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
1014 subs->freqn = get_usb_full_speed_rate(rate);
1015 else
1016 subs->freqn = get_usb_high_speed_rate(rate);
1017 subs->freqm = subs->freqn;
Clemens Ladisch573567e2005-06-27 08:17:30 +02001018 /* calculate max. frequency */
1019 if (subs->maxpacksize) {
1020 /* whatever fits into a max. size packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 maxsize = subs->maxpacksize;
Clemens Ladisch573567e2005-06-27 08:17:30 +02001022 subs->freqmax = (maxsize / (frame_bits >> 3))
1023 << (16 - subs->datainterval);
1024 } else {
1025 /* no max. packet size: just take 25% higher than nominal */
1026 subs->freqmax = subs->freqn + (subs->freqn >> 2);
1027 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
1028 >> (16 - subs->datainterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
Clemens Ladisch573567e2005-06-27 08:17:30 +02001030 subs->phase = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 if (subs->fill_max)
1033 subs->curpacksize = subs->maxpacksize;
1034 else
1035 subs->curpacksize = maxsize;
1036
Clemens Ladischa93bf992005-08-12 15:19:39 +02001037 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
1038 packs_per_ms = 8 >> subs->datainterval;
1039 else
1040 packs_per_ms = 1;
1041
Clemens Ladisch71d848c2005-08-12 15:18:00 +02001042 if (is_playback) {
Clemens Ladischf3990e62009-02-20 09:32:40 +01001043 urb_packs = max(nrpacks, 1);
Clemens Ladisch71d848c2005-08-12 15:18:00 +02001044 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
1045 } else
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001046 urb_packs = 1;
Clemens Ladischa93bf992005-08-12 15:19:39 +02001047 urb_packs *= packs_per_ms;
Clemens Ladisch765e8db2009-08-10 10:07:35 +02001048 if (subs->syncpipe)
Takashi Iwaif1e6d3c2009-08-11 08:15:04 +02001049 urb_packs = min(urb_packs, 1U << subs->syncinterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* decide how many packets to be used */
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001052 if (is_playback) {
Clemens Ladisch4d788e02009-01-26 08:09:28 +01001053 unsigned int minsize, maxpacks;
Clemens Ladischd6db3922005-08-12 08:28:27 +02001054 /* determine how small a packet can be */
1055 minsize = (subs->freqn >> (16 - subs->datainterval))
1056 * (frame_bits >> 3);
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +02001057 /* with sync from device, assume it can be 12% lower */
Clemens Ladischd6db3922005-08-12 08:28:27 +02001058 if (subs->syncpipe)
Clemens Ladisch7efd8bc2005-08-15 08:24:44 +02001059 minsize -= minsize >> 3;
Clemens Ladischd6db3922005-08-12 08:28:27 +02001060 minsize = max(minsize, 1u);
1061 total_packs = (period_bytes + minsize - 1) / minsize;
Clemens Ladischa93bf992005-08-12 15:19:39 +02001062 /* we need at least two URBs for queueing */
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001063 if (total_packs < 2) {
1064 total_packs = 2;
Clemens Ladischf3990e62009-02-20 09:32:40 +01001065 } else {
Clemens Ladisch4d788e02009-01-26 08:09:28 +01001066 /* and we don't want too long a queue either */
Clemens Ladischb1c86bb2009-03-02 12:06:28 +01001067 maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
1068 total_packs = min(total_packs, maxpacks);
Clemens Ladisch4d788e02009-01-26 08:09:28 +01001069 }
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001070 } else {
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001071 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
1072 urb_packs >>= 1;
Clemens Ladisch15a24c02005-08-12 08:25:26 +02001073 total_packs = MAX_URBS * urb_packs;
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
1076 if (subs->nurbs > MAX_URBS) {
1077 /* too much... */
1078 subs->nurbs = MAX_URBS;
1079 total_packs = MAX_URBS * urb_packs;
Clemens Ladisch160389c2009-01-26 08:10:19 +01001080 } else if (subs->nurbs < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /* too little - we need at least two packets
1082 * to ensure contiguous playback/capture
1083 */
1084 subs->nurbs = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086
1087 /* allocate and initialize data urbs */
1088 for (i = 0; i < subs->nurbs; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001089 struct snd_urb_ctx *u = &subs->dataurb[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 u->index = i;
1091 u->subs = subs;
Clemens Ladisch160389c2009-01-26 08:10:19 +01001092 u->packets = (i + 1) * total_packs / subs->nurbs
1093 - i * total_packs / subs->nurbs;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001094 u->buffer_size = maxsize * u->packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (subs->fmt_type == USB_FORMAT_TYPE_II)
1096 u->packets++; /* for transfer delimiter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
Pavel Machek07f51a72008-04-14 13:15:56 +02001098 if (!u->urb)
Clemens Ladisch55851f72005-08-15 08:34:16 +02001099 goto out_of_memory;
1100 u->urb->transfer_buffer =
1101 usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL,
1102 &u->urb->transfer_dma);
Pavel Machek07f51a72008-04-14 13:15:56 +02001103 if (!u->urb->transfer_buffer)
Clemens Ladisch55851f72005-08-15 08:34:16 +02001104 goto out_of_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 u->urb->pipe = subs->datapipe;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001106 u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Clemens Ladisch573567e2005-06-27 08:17:30 +02001107 u->urb->interval = 1 << subs->datainterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 u->urb->context = u;
Clemens Ladisch3527a002005-09-26 10:03:09 +02001109 u->urb->complete = snd_complete_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111
1112 if (subs->syncpipe) {
1113 /* allocate and initialize sync urbs */
Clemens Ladisch55851f72005-08-15 08:34:16 +02001114 subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4,
1115 GFP_KERNEL, &subs->sync_dma);
Pavel Machek07f51a72008-04-14 13:15:56 +02001116 if (!subs->syncbuf)
Clemens Ladisch55851f72005-08-15 08:34:16 +02001117 goto out_of_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 for (i = 0; i < SYNC_URBS; i++) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001119 struct snd_urb_ctx *u = &subs->syncurb[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 u->index = i;
1121 u->subs = subs;
Clemens Ladischca810902005-05-02 08:55:54 +02001122 u->packets = 1;
1123 u->urb = usb_alloc_urb(1, GFP_KERNEL);
Pavel Machek07f51a72008-04-14 13:15:56 +02001124 if (!u->urb)
Clemens Ladisch55851f72005-08-15 08:34:16 +02001125 goto out_of_memory;
Clemens Ladischca810902005-05-02 08:55:54 +02001126 u->urb->transfer_buffer = subs->syncbuf + i * 4;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001127 u->urb->transfer_dma = subs->sync_dma + i * 4;
Clemens Ladischca810902005-05-02 08:55:54 +02001128 u->urb->transfer_buffer_length = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 u->urb->pipe = subs->syncpipe;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001130 u->urb->transfer_flags = URB_ISO_ASAP |
1131 URB_NO_TRANSFER_DMA_MAP;
Clemens Ladischca810902005-05-02 08:55:54 +02001132 u->urb->number_of_packets = 1;
Clemens Ladisch1149a642005-05-02 08:53:46 +02001133 u->urb->interval = 1 << subs->syncinterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 u->urb->context = u;
Clemens Ladisch3527a002005-09-26 10:03:09 +02001135 u->urb->complete = snd_complete_sync_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137 }
1138 return 0;
Clemens Ladisch55851f72005-08-15 08:34:16 +02001139
1140out_of_memory:
1141 release_substream_urbs(subs, 0);
1142 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145
1146/*
1147 * find a matching audio format
1148 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001149static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 unsigned int rate, unsigned int channels)
1151{
1152 struct list_head *p;
1153 struct audioformat *found = NULL;
1154 int cur_attr = 0, attr;
1155
1156 list_for_each(p, &subs->fmt_list) {
1157 struct audioformat *fp;
1158 fp = list_entry(p, struct audioformat, list);
1159 if (fp->format != format || fp->channels != channels)
1160 continue;
1161 if (rate < fp->rate_min || rate > fp->rate_max)
1162 continue;
1163 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
1164 unsigned int i;
1165 for (i = 0; i < fp->nr_rates; i++)
1166 if (fp->rate_table[i] == rate)
1167 break;
1168 if (i >= fp->nr_rates)
1169 continue;
1170 }
1171 attr = fp->ep_attr & EP_ATTR_MASK;
1172 if (! found) {
1173 found = fp;
1174 cur_attr = attr;
1175 continue;
1176 }
1177 /* avoid async out and adaptive in if the other method
1178 * supports the same format.
1179 * this is a workaround for the case like
1180 * M-audio audiophile USB.
1181 */
1182 if (attr != cur_attr) {
1183 if ((attr == EP_ATTR_ASYNC &&
1184 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1185 (attr == EP_ATTR_ADAPTIVE &&
1186 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
1187 continue;
1188 if ((cur_attr == EP_ATTR_ASYNC &&
1189 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1190 (cur_attr == EP_ATTR_ADAPTIVE &&
1191 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
1192 found = fp;
1193 cur_attr = attr;
1194 continue;
1195 }
1196 }
1197 /* find the format with the largest max. packet size */
1198 if (fp->maxpacksize > found->maxpacksize) {
1199 found = fp;
1200 cur_attr = attr;
1201 }
1202 }
1203 return found;
1204}
1205
1206
1207/*
1208 * initialize the picth control and sample rate
1209 */
1210static int init_usb_pitch(struct usb_device *dev, int iface,
1211 struct usb_host_interface *alts,
1212 struct audioformat *fmt)
1213{
1214 unsigned int ep;
1215 unsigned char data[1];
1216 int err;
1217
1218 ep = get_endpoint(alts, 0)->bEndpointAddress;
1219 /* if endpoint has pitch control, enable it */
1220 if (fmt->attributes & EP_CS_ATTR_PITCH_CONTROL) {
1221 data[0] = 1;
1222 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1223 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1224 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1225 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
1226 dev->devnum, iface, ep);
1227 return err;
1228 }
1229 }
1230 return 0;
1231}
1232
1233static int init_usb_sample_rate(struct usb_device *dev, int iface,
1234 struct usb_host_interface *alts,
1235 struct audioformat *fmt, int rate)
1236{
1237 unsigned int ep;
1238 unsigned char data[3];
1239 int err;
1240
1241 ep = get_endpoint(alts, 0)->bEndpointAddress;
1242 /* if endpoint has sampling rate control, set it */
1243 if (fmt->attributes & EP_CS_ATTR_SAMPLE_RATE) {
1244 int crate;
1245 data[0] = rate;
1246 data[1] = rate >> 8;
1247 data[2] = rate >> 16;
1248 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1249 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1250 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
Andreas Bergmeierb9d710b2009-01-24 12:15:14 +01001251 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 dev->devnum, iface, fmt->altsetting, rate, ep);
1253 return err;
1254 }
1255 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR,
1256 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1257 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
Andreas Bergmeierb9d710b2009-01-24 12:15:14 +01001258 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 dev->devnum, iface, fmt->altsetting, ep);
1260 return 0; /* some devices don't support reading */
1261 }
1262 crate = data[0] | (data[1] << 8) | (data[2] << 16);
1263 if (crate != rate) {
1264 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
1265 // runtime->rate = crate;
1266 }
1267 }
1268 return 0;
1269}
1270
1271/*
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001272 * For E-Mu 0404USB/0202USB/TrackerPre sample rate should be set for device,
1273 * not for interface.
1274 */
1275static void set_format_emu_quirk(struct snd_usb_substream *subs,
1276 struct audioformat *fmt)
1277{
1278 unsigned char emu_samplerate_id = 0;
1279
1280 /* When capture is active
1281 * sample rate shouldn't be changed
1282 * by playback substream
1283 */
1284 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1285 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
1286 return;
1287 }
1288
1289 switch (fmt->rate_min) {
1290 case 48000:
1291 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1292 break;
1293 case 88200:
1294 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1295 break;
1296 case 96000:
1297 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1298 break;
1299 case 176400:
1300 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1301 break;
1302 case 192000:
1303 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1304 break;
1305 default:
1306 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1307 break;
1308 }
1309 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
1310}
1311
1312/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * find a matching format and set up the interface
1314 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001315static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
1317 struct usb_device *dev = subs->dev;
1318 struct usb_host_interface *alts;
1319 struct usb_interface_descriptor *altsd;
1320 struct usb_interface *iface;
1321 unsigned int ep, attr;
1322 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1323 int err;
1324
1325 iface = usb_ifnum_to_if(dev, fmt->iface);
Takashi Iwai5e246b82008-08-08 17:12:47 +02001326 if (WARN_ON(!iface))
1327 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 alts = &iface->altsetting[fmt->altset_idx];
1329 altsd = get_iface_desc(alts);
Takashi Iwai5e246b82008-08-08 17:12:47 +02001330 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
1331 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if (fmt == subs->cur_audiofmt)
1334 return 0;
1335
1336 /* close the old interface */
1337 if (subs->interface >= 0 && subs->interface != fmt->iface) {
Oliver Neukum5149fe22007-08-31 12:15:27 +02001338 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
1339 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
1340 dev->devnum, fmt->iface, fmt->altsetting);
1341 return -EIO;
1342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 subs->interface = -1;
1344 subs->format = 0;
1345 }
1346
1347 /* set interface */
1348 if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
1349 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
1350 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
1351 dev->devnum, fmt->iface, fmt->altsetting);
1352 return -EIO;
1353 }
1354 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
1355 subs->interface = fmt->iface;
1356 subs->format = fmt->altset_idx;
1357 }
1358
1359 /* create a data pipe */
1360 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
1361 if (is_playback)
1362 subs->datapipe = usb_sndisocpipe(dev, ep);
1363 else
1364 subs->datapipe = usb_rcvisocpipe(dev, ep);
Clemens Ladisch744b89e2009-04-03 09:45:01 +02001365 subs->datainterval = fmt->datainterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 subs->syncpipe = subs->syncinterval = 0;
1367 subs->maxpacksize = fmt->maxpacksize;
1368 subs->fill_max = 0;
1369
1370 /* we need a sync pipe in async OUT or adaptive IN mode */
1371 /* check the number of EP, since some devices have broken
1372 * descriptors which fool us. if it has only one EP,
1373 * assume it as adaptive-out or sync-in.
1374 */
1375 attr = fmt->ep_attr & EP_ATTR_MASK;
1376 if (((is_playback && attr == EP_ATTR_ASYNC) ||
1377 (! is_playback && attr == EP_ATTR_ADAPTIVE)) &&
1378 altsd->bNumEndpoints >= 2) {
1379 /* check sync-pipe endpoint */
1380 /* ... and check descriptor size before accessing bSynchAddress
1381 because there is a version of the SB Audigy 2 NX firmware lacking
1382 the audio fields in the endpoint descriptors */
1383 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
1384 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1385 get_endpoint(alts, 1)->bSynchAddress != 0)) {
1386 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1387 dev->devnum, fmt->iface, fmt->altsetting);
1388 return -EINVAL;
1389 }
1390 ep = get_endpoint(alts, 1)->bEndpointAddress;
1391 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1392 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
1393 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
1394 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1395 dev->devnum, fmt->iface, fmt->altsetting);
1396 return -EINVAL;
1397 }
1398 ep &= USB_ENDPOINT_NUMBER_MASK;
1399 if (is_playback)
1400 subs->syncpipe = usb_rcvisocpipe(dev, ep);
1401 else
1402 subs->syncpipe = usb_sndisocpipe(dev, ep);
Clemens Ladisch1149a642005-05-02 08:53:46 +02001403 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1404 get_endpoint(alts, 1)->bRefresh >= 1 &&
1405 get_endpoint(alts, 1)->bRefresh <= 9)
1406 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
Clemens Ladisch604cf492005-05-17 09:15:27 +02001407 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
Clemens Ladisch1149a642005-05-02 08:53:46 +02001408 subs->syncinterval = 1;
Clemens Ladisch604cf492005-05-17 09:15:27 +02001409 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
1410 get_endpoint(alts, 1)->bInterval <= 16)
1411 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
1412 else
1413 subs->syncinterval = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415
1416 /* always fill max packet size */
1417 if (fmt->attributes & EP_CS_ATTR_FILL_MAX)
1418 subs->fill_max = 1;
1419
1420 if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
1421 return err;
1422
1423 subs->cur_audiofmt = fmt;
1424
Sergiy Kovalchuk7d2b4512009-12-27 09:13:41 -08001425 switch (subs->stream->chip->usb_id) {
1426 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1427 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1428 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
1429 set_format_emu_quirk(subs, fmt);
1430 break;
1431 }
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433#if 0
Takashi Iwai54530bd2009-02-05 15:55:18 +01001434 printk(KERN_DEBUG
1435 "setting done: format = %d, rate = %d..%d, channels = %d\n",
Pavel Machek2a56f512008-04-14 13:14:22 +02001436 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
Takashi Iwai54530bd2009-02-05 15:55:18 +01001437 printk(KERN_DEBUG
1438 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 subs->datapipe, subs->syncpipe);
1440#endif
1441
1442 return 0;
1443}
1444
1445/*
1446 * hw_params callback
1447 *
1448 * allocate a buffer and set the given audio format.
1449 *
1450 * so far we use a physically linear buffer although packetize transfer
1451 * doesn't need a continuous area.
1452 * if sg buffer is supported on the later version of alsa, we'll follow
1453 * that.
1454 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001455static int snd_usb_hw_params(struct snd_pcm_substream *substream,
1456 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
John Daiker88518272006-12-28 13:55:05 +01001458 struct snd_usb_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 struct audioformat *fmt;
1460 unsigned int channels, rate, format;
1461 int ret, changed;
1462
Clemens Ladischc55675e2009-12-18 09:31:31 +01001463 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
1464 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (ret < 0)
1466 return ret;
1467
1468 format = params_format(hw_params);
1469 rate = params_rate(hw_params);
1470 channels = params_channels(hw_params);
1471 fmt = find_format(subs, format, rate, channels);
Pavel Machek07f51a72008-04-14 13:15:56 +02001472 if (!fmt) {
Andreas Bergmeierb9d710b2009-01-24 12:15:14 +01001473 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001474 format, rate, channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return -EINVAL;
1476 }
1477
1478 changed = subs->cur_audiofmt != fmt ||
1479 subs->period_bytes != params_period_bytes(hw_params) ||
1480 subs->cur_rate != rate;
1481 if ((ret = set_format(subs, fmt)) < 0)
1482 return ret;
1483
1484 if (subs->cur_rate != rate) {
1485 struct usb_host_interface *alts;
1486 struct usb_interface *iface;
1487 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
1488 alts = &iface->altsetting[fmt->altset_idx];
1489 ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
1490 if (ret < 0)
1491 return ret;
1492 subs->cur_rate = rate;
1493 }
1494
1495 if (changed) {
1496 /* format changed */
1497 release_substream_urbs(subs, 0);
1498 /* influenced: period_bytes, channels, rate, format, */
1499 ret = init_substream_urbs(subs, params_period_bytes(hw_params),
1500 params_rate(hw_params),
1501 snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
1502 }
1503
1504 return ret;
1505}
1506
1507/*
1508 * hw_free callback
1509 *
1510 * reset the audio format and release the buffer
1511 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001512static int snd_usb_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
John Daiker88518272006-12-28 13:55:05 +01001514 struct snd_usb_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 subs->cur_audiofmt = NULL;
1517 subs->cur_rate = 0;
1518 subs->period_bytes = 0;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001519 if (!subs->stream->chip->shutdown)
1520 release_substream_urbs(subs, 0);
Clemens Ladischc55675e2009-12-18 09:31:31 +01001521 return snd_pcm_lib_free_vmalloc_buffer(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
1524/*
1525 * prepare callback
1526 *
1527 * only a few subtle things...
1528 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001529static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001531 struct snd_pcm_runtime *runtime = substream->runtime;
1532 struct snd_usb_substream *subs = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 if (! subs->cur_audiofmt) {
1535 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
1536 return -ENXIO;
1537 }
1538
1539 /* some unit conversions in runtime */
1540 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
1541 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
1542
1543 /* reset the pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 subs->hwptr_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 subs->transfer_done = 0;
1546 subs->phase = 0;
Takashi Iwaiae1ec5e2008-10-13 03:08:53 +02001547 runtime->delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 /* clear urbs (to be sure) */
1550 deactivate_urbs(subs, 0, 1);
1551 wait_clear_urbs(subs);
1552
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001553 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1554 * updates for all URBs would happen at the same time when starting */
1555 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
Clemens Ladische4f8e652006-10-04 13:42:57 +02001556 subs->ops.prepare = prepare_nodata_playback_urb;
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001557 return start_urbs(subs, runtime);
1558 } else
1559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Clemens Ladisch1700f302006-10-04 13:41:25 +02001562static struct snd_pcm_hardware snd_usb_hardware =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Clemens Ladisch49045d32005-09-05 10:31:05 +02001564 .info = SNDRV_PCM_INFO_MMAP |
1565 SNDRV_PCM_INFO_MMAP_VALID |
1566 SNDRV_PCM_INFO_BATCH |
1567 SNDRV_PCM_INFO_INTERLEAVED |
Clemens Ladische4f8e652006-10-04 13:42:57 +02001568 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1569 SNDRV_PCM_INFO_PAUSE,
Clemens Ladischd31cbbf2005-09-26 09:59:57 +02001570 .buffer_bytes_max = 1024 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 .period_bytes_min = 64,
Clemens Ladischd31cbbf2005-09-26 09:59:57 +02001572 .period_bytes_max = 512 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 .periods_min = 2,
1574 .periods_max = 1024,
1575};
1576
1577/*
1578 * h/w constraints
1579 */
1580
1581#ifdef HW_CONST_DEBUG
1582#define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
1583#else
1584#define hwc_debug(fmt, args...) /**/
1585#endif
1586
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001587static int hw_check_valid_format(struct snd_usb_substream *subs,
1588 struct snd_pcm_hw_params *params,
1589 struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001591 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1592 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1593 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001594 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1595 unsigned int ptime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /* check the format */
Pavel Machek07f51a72008-04-14 13:15:56 +02001598 if (!snd_mask_test(fmts, fp->format)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 hwc_debug(" > check: no supported format %d\n", fp->format);
1600 return 0;
1601 }
1602 /* check the channels */
1603 if (fp->channels < ct->min || fp->channels > ct->max) {
1604 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1605 return 0;
1606 }
1607 /* check the rate is within the range */
1608 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1609 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1610 return 0;
1611 }
1612 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1613 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1614 return 0;
1615 }
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001616 /* check whether the period time is >= the data packet interval */
1617 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
1618 ptime = 125 * (1 << fp->datainterval);
1619 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
1620 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
1621 return 0;
1622 }
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return 1;
1625}
1626
Takashi Iwai86e07d32005-11-17 15:08:02 +01001627static int hw_rule_rate(struct snd_pcm_hw_params *params,
1628 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001630 struct snd_usb_substream *subs = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001632 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 unsigned int rmin, rmax;
1634 int changed;
1635
1636 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1637 changed = 0;
1638 rmin = rmax = 0;
1639 list_for_each(p, &subs->fmt_list) {
1640 struct audioformat *fp;
1641 fp = list_entry(p, struct audioformat, list);
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001642 if (!hw_check_valid_format(subs, params, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 continue;
1644 if (changed++) {
1645 if (rmin > fp->rate_min)
1646 rmin = fp->rate_min;
1647 if (rmax < fp->rate_max)
1648 rmax = fp->rate_max;
1649 } else {
1650 rmin = fp->rate_min;
1651 rmax = fp->rate_max;
1652 }
1653 }
1654
Pavel Machek07f51a72008-04-14 13:15:56 +02001655 if (!changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 hwc_debug(" --> get empty\n");
1657 it->empty = 1;
1658 return -EINVAL;
1659 }
1660
1661 changed = 0;
1662 if (it->min < rmin) {
1663 it->min = rmin;
1664 it->openmin = 0;
1665 changed = 1;
1666 }
1667 if (it->max > rmax) {
1668 it->max = rmax;
1669 it->openmax = 0;
1670 changed = 1;
1671 }
1672 if (snd_interval_checkempty(it)) {
1673 it->empty = 1;
1674 return -EINVAL;
1675 }
1676 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1677 return changed;
1678}
1679
1680
Takashi Iwai86e07d32005-11-17 15:08:02 +01001681static int hw_rule_channels(struct snd_pcm_hw_params *params,
1682 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001684 struct snd_usb_substream *subs = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001686 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 unsigned int rmin, rmax;
1688 int changed;
1689
1690 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1691 changed = 0;
1692 rmin = rmax = 0;
1693 list_for_each(p, &subs->fmt_list) {
1694 struct audioformat *fp;
1695 fp = list_entry(p, struct audioformat, list);
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001696 if (!hw_check_valid_format(subs, params, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 continue;
1698 if (changed++) {
1699 if (rmin > fp->channels)
1700 rmin = fp->channels;
1701 if (rmax < fp->channels)
1702 rmax = fp->channels;
1703 } else {
1704 rmin = fp->channels;
1705 rmax = fp->channels;
1706 }
1707 }
1708
Pavel Machek07f51a72008-04-14 13:15:56 +02001709 if (!changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 hwc_debug(" --> get empty\n");
1711 it->empty = 1;
1712 return -EINVAL;
1713 }
1714
1715 changed = 0;
1716 if (it->min < rmin) {
1717 it->min = rmin;
1718 it->openmin = 0;
1719 changed = 1;
1720 }
1721 if (it->max > rmax) {
1722 it->max = rmax;
1723 it->openmax = 0;
1724 changed = 1;
1725 }
1726 if (snd_interval_checkempty(it)) {
1727 it->empty = 1;
1728 return -EINVAL;
1729 }
1730 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1731 return changed;
1732}
1733
Takashi Iwai86e07d32005-11-17 15:08:02 +01001734static int hw_rule_format(struct snd_pcm_hw_params *params,
1735 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001737 struct snd_usb_substream *subs = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001739 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 u64 fbits;
1741 u32 oldbits[2];
1742 int changed;
1743
1744 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1745 fbits = 0;
1746 list_for_each(p, &subs->fmt_list) {
1747 struct audioformat *fp;
1748 fp = list_entry(p, struct audioformat, list);
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001749 if (!hw_check_valid_format(subs, params, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 continue;
1751 fbits |= (1ULL << fp->format);
1752 }
1753
1754 oldbits[0] = fmt->bits[0];
1755 oldbits[1] = fmt->bits[1];
1756 fmt->bits[0] &= (u32)fbits;
1757 fmt->bits[1] &= (u32)(fbits >> 32);
Pavel Machek07f51a72008-04-14 13:15:56 +02001758 if (!fmt->bits[0] && !fmt->bits[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 hwc_debug(" --> get empty\n");
1760 return -EINVAL;
1761 }
1762 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1763 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1764 return changed;
1765}
1766
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001767static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1768 struct snd_pcm_hw_rule *rule)
1769{
1770 struct snd_usb_substream *subs = rule->private;
1771 struct audioformat *fp;
1772 struct snd_interval *it;
1773 unsigned char min_datainterval;
1774 unsigned int pmin;
1775 int changed;
1776
1777 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1778 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1779 min_datainterval = 0xff;
1780 list_for_each_entry(fp, &subs->fmt_list, list) {
1781 if (!hw_check_valid_format(subs, params, fp))
1782 continue;
1783 min_datainterval = min(min_datainterval, fp->datainterval);
1784 }
1785 if (min_datainterval == 0xff) {
1786 hwc_debug(" --> get emtpy\n");
1787 it->empty = 1;
1788 return -EINVAL;
1789 }
1790 pmin = 125 * (1 << min_datainterval);
1791 changed = 0;
1792 if (it->min < pmin) {
1793 it->min = pmin;
1794 it->openmin = 0;
1795 changed = 1;
1796 }
1797 if (snd_interval_checkempty(it)) {
1798 it->empty = 1;
1799 return -EINVAL;
1800 }
1801 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1802 return changed;
1803}
1804
Luke Rossa79eee82006-08-29 10:46:32 +02001805/*
1806 * If the device supports unusual bit rates, does the request meet these?
1807 */
1808static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1809 struct snd_usb_substream *subs)
1810{
Takashi Iwai8fec5602007-02-01 11:50:56 +01001811 struct audioformat *fp;
1812 int count = 0, needs_knot = 0;
Luke Rossa79eee82006-08-29 10:46:32 +02001813 int err;
1814
Takashi Iwai8fec5602007-02-01 11:50:56 +01001815 list_for_each_entry(fp, &subs->fmt_list, list) {
1816 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1817 return 0;
1818 count += fp->nr_rates;
Clemens Ladisch918f3a02007-08-13 17:40:54 +02001819 if (fp->rates & SNDRV_PCM_RATE_KNOT)
Takashi Iwai8fec5602007-02-01 11:50:56 +01001820 needs_knot = 1;
Luke Rossa79eee82006-08-29 10:46:32 +02001821 }
Takashi Iwai8fec5602007-02-01 11:50:56 +01001822 if (!needs_knot)
1823 return 0;
1824
1825 subs->rate_list.count = count;
1826 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
1827 subs->rate_list.mask = 0;
1828 count = 0;
1829 list_for_each_entry(fp, &subs->fmt_list, list) {
1830 int i;
1831 for (i = 0; i < fp->nr_rates; i++)
1832 subs->rate_list.list[count++] = fp->rate_table[i];
1833 }
1834 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1835 &subs->rate_list);
1836 if (err < 0)
1837 return err;
Luke Rossa79eee82006-08-29 10:46:32 +02001838
1839 return 0;
1840}
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843/*
1844 * set up the runtime hardware information.
1845 */
1846
Takashi Iwai86e07d32005-11-17 15:08:02 +01001847static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 struct list_head *p;
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001850 unsigned int pt, ptmin;
1851 int param_period_time_if_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 int err;
1853
1854 runtime->hw.formats = subs->formats;
1855
1856 runtime->hw.rate_min = 0x7fffffff;
1857 runtime->hw.rate_max = 0;
1858 runtime->hw.channels_min = 256;
1859 runtime->hw.channels_max = 0;
1860 runtime->hw.rates = 0;
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001861 ptmin = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 /* check min/max rates and channels */
1863 list_for_each(p, &subs->fmt_list) {
1864 struct audioformat *fp;
1865 fp = list_entry(p, struct audioformat, list);
1866 runtime->hw.rates |= fp->rates;
1867 if (runtime->hw.rate_min > fp->rate_min)
1868 runtime->hw.rate_min = fp->rate_min;
1869 if (runtime->hw.rate_max < fp->rate_max)
1870 runtime->hw.rate_max = fp->rate_max;
1871 if (runtime->hw.channels_min > fp->channels)
1872 runtime->hw.channels_min = fp->channels;
1873 if (runtime->hw.channels_max < fp->channels)
1874 runtime->hw.channels_max = fp->channels;
1875 if (fp->fmt_type == USB_FORMAT_TYPE_II && fp->frame_size > 0) {
1876 /* FIXME: there might be more than one audio formats... */
1877 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1878 fp->frame_size;
1879 }
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001880 pt = 125 * (1 << fp->datainterval);
1881 ptmin = min(ptmin, pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001884 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1885 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
1886 /* full speed devices have fixed data packet interval */
1887 ptmin = 1000;
1888 if (ptmin == 1000)
1889 /* if period time doesn't go below 1 ms, no rules needed */
1890 param_period_time_if_needed = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001892 ptmin, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Clemens Ladisch4608eb02009-04-03 09:42:42 +02001894 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1895 hw_rule_rate, subs,
1896 SNDRV_PCM_HW_PARAM_FORMAT,
1897 SNDRV_PCM_HW_PARAM_CHANNELS,
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001898 param_period_time_if_needed,
Clemens Ladisch4608eb02009-04-03 09:42:42 +02001899 -1)) < 0)
Takashi Iwai5a220c82008-03-17 09:59:32 +01001900 return err;
Clemens Ladisch4608eb02009-04-03 09:42:42 +02001901 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1902 hw_rule_channels, subs,
1903 SNDRV_PCM_HW_PARAM_FORMAT,
1904 SNDRV_PCM_HW_PARAM_RATE,
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001905 param_period_time_if_needed,
Clemens Ladisch4608eb02009-04-03 09:42:42 +02001906 -1)) < 0)
1907 return err;
1908 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1909 hw_rule_format, subs,
1910 SNDRV_PCM_HW_PARAM_RATE,
1911 SNDRV_PCM_HW_PARAM_CHANNELS,
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001912 param_period_time_if_needed,
Clemens Ladisch4608eb02009-04-03 09:42:42 +02001913 -1)) < 0)
1914 return err;
Clemens Ladischa7d9c092009-04-03 09:48:26 +02001915 if (param_period_time_if_needed >= 0) {
1916 err = snd_pcm_hw_rule_add(runtime, 0,
1917 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1918 hw_rule_period_time, subs,
1919 SNDRV_PCM_HW_PARAM_FORMAT,
1920 SNDRV_PCM_HW_PARAM_CHANNELS,
1921 SNDRV_PCM_HW_PARAM_RATE,
1922 -1);
1923 if (err < 0)
1924 return err;
1925 }
Clemens Ladisch4608eb02009-04-03 09:42:42 +02001926 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
1927 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return 0;
1929}
1930
Clemens Ladisch1700f302006-10-04 13:41:25 +02001931static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001933 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1934 struct snd_pcm_runtime *runtime = substream->runtime;
1935 struct snd_usb_substream *subs = &as->substream[direction];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 subs->interface = -1;
1938 subs->format = 0;
Clemens Ladisch1700f302006-10-04 13:41:25 +02001939 runtime->hw = snd_usb_hardware;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 runtime->private_data = subs;
1941 subs->pcm_substream = substream;
1942 return setup_hw_info(runtime, subs);
1943}
1944
Takashi Iwai86e07d32005-11-17 15:08:02 +01001945static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001947 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1948 struct snd_usb_substream *subs = &as->substream[direction];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 if (subs->interface >= 0) {
1951 usb_set_interface(subs->dev, subs->interface, 0);
1952 subs->interface = -1;
1953 }
1954 subs->pcm_substream = NULL;
1955 return 0;
1956}
1957
Takashi Iwai86e07d32005-11-17 15:08:02 +01001958static int snd_usb_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Clemens Ladisch1700f302006-10-04 13:41:25 +02001960 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Takashi Iwai86e07d32005-11-17 15:08:02 +01001963static int snd_usb_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1966}
1967
Takashi Iwai86e07d32005-11-17 15:08:02 +01001968static int snd_usb_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Clemens Ladisch1700f302006-10-04 13:41:25 +02001970 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
Takashi Iwai86e07d32005-11-17 15:08:02 +01001973static int snd_usb_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1976}
1977
Takashi Iwai86e07d32005-11-17 15:08:02 +01001978static struct snd_pcm_ops snd_usb_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 .open = snd_usb_playback_open,
1980 .close = snd_usb_playback_close,
1981 .ioctl = snd_pcm_lib_ioctl,
1982 .hw_params = snd_usb_hw_params,
1983 .hw_free = snd_usb_hw_free,
1984 .prepare = snd_usb_pcm_prepare,
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001985 .trigger = snd_usb_pcm_playback_trigger,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 .pointer = snd_usb_pcm_pointer,
Clemens Ladischc55675e2009-12-18 09:31:31 +01001987 .page = snd_pcm_lib_get_vmalloc_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988};
1989
Takashi Iwai86e07d32005-11-17 15:08:02 +01001990static struct snd_pcm_ops snd_usb_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 .open = snd_usb_capture_open,
1992 .close = snd_usb_capture_close,
1993 .ioctl = snd_pcm_lib_ioctl,
1994 .hw_params = snd_usb_hw_params,
1995 .hw_free = snd_usb_hw_free,
1996 .prepare = snd_usb_pcm_prepare,
Clemens Ladischb55bbf02005-11-02 11:32:52 +01001997 .trigger = snd_usb_pcm_capture_trigger,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 .pointer = snd_usb_pcm_pointer,
Clemens Ladischc55675e2009-12-18 09:31:31 +01001999 .page = snd_pcm_lib_get_vmalloc_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000};
2001
2002
2003
2004/*
2005 * helper functions
2006 */
2007
2008/*
2009 * combine bytes and get an integer value
2010 */
2011unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
2012{
2013 switch (size) {
2014 case 1: return *bytes;
2015 case 2: return combine_word(bytes);
2016 case 3: return combine_triple(bytes);
2017 case 4: return combine_quad(bytes);
2018 default: return 0;
2019 }
2020}
2021
2022/*
2023 * parse descriptor buffer and return the pointer starting the given
2024 * descriptor type.
2025 */
2026void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
2027{
2028 u8 *p, *end, *next;
2029
2030 p = descstart;
2031 end = p + desclen;
2032 for (; p < end;) {
2033 if (p[0] < 2)
2034 return NULL;
2035 next = p + p[0];
2036 if (next > end)
2037 return NULL;
2038 if (p[1] == dtype && (!after || (void *)p > after)) {
2039 return p;
2040 }
2041 p = next;
2042 }
2043 return NULL;
2044}
2045
2046/*
2047 * find a class-specified interface descriptor with the given subtype.
2048 */
2049void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
2050{
2051 unsigned char *p = after;
2052
2053 while ((p = snd_usb_find_desc(buffer, buflen, p,
2054 USB_DT_CS_INTERFACE)) != NULL) {
2055 if (p[0] >= 3 && p[2] == dsubtype)
2056 return p;
2057 }
2058 return NULL;
2059}
2060
2061/*
2062 * Wrapper for usb_control_msg().
2063 * Allocates a temp buffer to prevent dmaing from/to the stack.
2064 */
2065int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
2066 __u8 requesttype, __u16 value, __u16 index, void *data,
2067 __u16 size, int timeout)
2068{
2069 int err;
2070 void *buf = NULL;
2071
2072 if (size > 0) {
Alexey Dobriyan52978be2006-09-30 23:27:21 -07002073 buf = kmemdup(data, size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (!buf)
2075 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077 err = usb_control_msg(dev, pipe, request, requesttype,
2078 value, index, buf, size, timeout);
2079 if (size > 0) {
2080 memcpy(data, buf, size);
2081 kfree(buf);
2082 }
2083 return err;
2084}
2085
2086
2087/*
2088 * entry point for linux usb interface
2089 */
2090
2091static int usb_audio_probe(struct usb_interface *intf,
2092 const struct usb_device_id *id);
2093static void usb_audio_disconnect(struct usb_interface *intf);
Andrew Morton93521d22007-12-24 14:40:56 +01002094
2095#ifdef CONFIG_PM
Oliver Neukumf85bf292007-12-14 14:42:41 +01002096static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message);
2097static int usb_audio_resume(struct usb_interface *intf);
Andrew Morton93521d22007-12-24 14:40:56 +01002098#else
2099#define usb_audio_suspend NULL
2100#define usb_audio_resume NULL
2101#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103static struct usb_device_id usb_audio_ids [] = {
2104#include "usbquirks.h"
2105 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
2106 .bInterfaceClass = USB_CLASS_AUDIO,
2107 .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL },
2108 { } /* Terminating entry */
2109};
2110
2111MODULE_DEVICE_TABLE (usb, usb_audio_ids);
2112
2113static struct usb_driver usb_audio_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 .name = "snd-usb-audio",
2115 .probe = usb_audio_probe,
2116 .disconnect = usb_audio_disconnect,
Oliver Neukumf85bf292007-12-14 14:42:41 +01002117 .suspend = usb_audio_suspend,
2118 .resume = usb_audio_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 .id_table = usb_audio_ids,
2120};
2121
2122
Takashi Iwai727f3172006-08-04 19:08:03 +02002123#if defined(CONFIG_PROC_FS) && defined(CONFIG_SND_VERBOSE_PROCFS)
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125/*
2126 * proc interface for list the supported pcm formats
2127 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002128static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 struct list_head *p;
2131 static char *sync_types[4] = {
2132 "NONE", "ASYNC", "ADAPTIVE", "SYNC"
2133 };
2134
2135 list_for_each(p, &subs->fmt_list) {
2136 struct audioformat *fp;
2137 fp = list_entry(p, struct audioformat, list);
2138 snd_iprintf(buffer, " Interface %d\n", fp->iface);
2139 snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
Takashi Iwai6e5265e2009-09-08 14:26:51 +02002140 snd_iprintf(buffer, " Format: %s\n",
2141 snd_pcm_format_name(fp->format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 snd_iprintf(buffer, " Channels: %d\n", fp->channels);
2143 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
2144 fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
2145 fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
2146 sync_types[(fp->ep_attr & EP_ATTR_MASK) >> 2]);
2147 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
2148 snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
2149 fp->rate_min, fp->rate_max);
2150 } else {
2151 unsigned int i;
2152 snd_iprintf(buffer, " Rates: ");
2153 for (i = 0; i < fp->nr_rates; i++) {
2154 if (i > 0)
2155 snd_iprintf(buffer, ", ");
2156 snd_iprintf(buffer, "%d", fp->rate_table[i]);
2157 }
2158 snd_iprintf(buffer, "\n");
2159 }
Clemens Ladisch744b89e2009-04-03 09:45:01 +02002160 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
2161 snd_iprintf(buffer, " Data packet interval: %d us\n",
2162 125 * (1 << fp->datainterval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
Andreas Bergmeierb9d710b2009-01-24 12:15:14 +01002164 // snd_iprintf(buffer, " EP Attribute = %#x\n", fp->attributes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166}
2167
Takashi Iwai86e07d32005-11-17 15:08:02 +01002168static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 if (subs->running) {
2171 unsigned int i;
2172 snd_iprintf(buffer, " Status: Running\n");
2173 snd_iprintf(buffer, " Interface = %d\n", subs->interface);
2174 snd_iprintf(buffer, " Altset = %d\n", subs->format);
2175 snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
2176 for (i = 0; i < subs->nurbs; i++)
2177 snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
2178 snd_iprintf(buffer, "]\n");
2179 snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
Clemens Ladisch08fe1582005-04-22 15:33:01 +02002180 snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
2182 ? get_full_speed_hz(subs->freqm)
Clemens Ladisch08fe1582005-04-22 15:33:01 +02002183 : get_high_speed_hz(subs->freqm),
2184 subs->freqm >> 16, subs->freqm & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 } else {
2186 snd_iprintf(buffer, " Status: Stop\n");
2187 }
2188}
2189
Takashi Iwai86e07d32005-11-17 15:08:02 +01002190static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002192 struct snd_usb_stream *stream = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
2195
2196 if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
2197 snd_iprintf(buffer, "\nPlayback:\n");
2198 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2199 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2200 }
2201 if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
2202 snd_iprintf(buffer, "\nCapture:\n");
2203 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2204 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2205 }
2206}
2207
Takashi Iwai86e07d32005-11-17 15:08:02 +01002208static void proc_pcm_format_add(struct snd_usb_stream *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002210 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 char name[32];
Takashi Iwai86e07d32005-11-17 15:08:02 +01002212 struct snd_card *card = stream->chip->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 sprintf(name, "stream%d", stream->pcm_index);
Pavel Machek07f51a72008-04-14 13:15:56 +02002215 if (!snd_card_proc_new(card, name, &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002216 snd_info_set_text_ops(entry, stream, proc_pcm_format_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002219#else
2220
2221static inline void proc_pcm_format_add(struct snd_usb_stream *stream)
2222{
2223}
2224
2225#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227/*
2228 * initialize the substream instance.
2229 */
2230
Takashi Iwai86e07d32005-11-17 15:08:02 +01002231static void init_substream(struct snd_usb_stream *as, int stream, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002233 struct snd_usb_substream *subs = &as->substream[stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 INIT_LIST_HEAD(&subs->fmt_list);
2236 spin_lock_init(&subs->lock);
2237
2238 subs->stream = as;
2239 subs->direction = stream;
2240 subs->dev = as->chip->dev;
Clemens Ladischd5132022008-02-25 11:01:00 +01002241 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 subs->ops = audio_urb_ops[stream];
Clemens Ladischd5132022008-02-25 11:01:00 +01002243 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 subs->ops = audio_urb_ops_high_speed[stream];
Clemens Ladischd5132022008-02-25 11:01:00 +01002245 switch (as->chip->usb_id) {
2246 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
2247 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
Eran Tromer97c889a2008-09-26 01:07:03 -04002248 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
Clemens Ladischd5132022008-02-25 11:01:00 +01002249 subs->ops.retire_sync = retire_playback_sync_urb_hs_emu;
2250 break;
2251 }
2252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 snd_pcm_set_ops(as->pcm, stream,
2254 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2255 &snd_usb_playback_ops : &snd_usb_capture_ops);
2256
2257 list_add_tail(&fp->list, &subs->fmt_list);
2258 subs->formats |= 1ULL << fp->format;
2259 subs->endpoint = fp->endpoint;
2260 subs->num_formats++;
2261 subs->fmt_type = fp->fmt_type;
2262}
2263
2264
2265/*
2266 * free a substream
2267 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002268static void free_substream(struct snd_usb_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 struct list_head *p, *n;
2271
Pavel Machek07f51a72008-04-14 13:15:56 +02002272 if (!subs->num_formats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return; /* not initialized */
2274 list_for_each_safe(p, n, &subs->fmt_list) {
2275 struct audioformat *fp = list_entry(p, struct audioformat, list);
2276 kfree(fp->rate_table);
2277 kfree(fp);
2278 }
Takashi Iwai8fec5602007-02-01 11:50:56 +01002279 kfree(subs->rate_list.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
2282
2283/*
2284 * free a usb stream instance
2285 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002286static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
2288 free_substream(&stream->substream[0]);
2289 free_substream(&stream->substream[1]);
2290 list_del(&stream->list);
2291 kfree(stream);
2292}
2293
Takashi Iwai86e07d32005-11-17 15:08:02 +01002294static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002296 struct snd_usb_stream *stream = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (stream) {
2298 stream->pcm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 snd_usb_audio_stream_free(stream);
2300 }
2301}
2302
2303
2304/*
2305 * add this endpoint to the chip instance.
2306 * if a stream with the same endpoint already exists, append to it.
2307 * if not, create a new pcm stream.
2308 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002309static int add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
2311 struct list_head *p;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002312 struct snd_usb_stream *as;
2313 struct snd_usb_substream *subs;
2314 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 int err;
2316
2317 list_for_each(p, &chip->pcm_list) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002318 as = list_entry(p, struct snd_usb_stream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (as->fmt_type != fp->fmt_type)
2320 continue;
2321 subs = &as->substream[stream];
Pavel Machek07f51a72008-04-14 13:15:56 +02002322 if (!subs->endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 continue;
2324 if (subs->endpoint == fp->endpoint) {
2325 list_add_tail(&fp->list, &subs->fmt_list);
2326 subs->num_formats++;
2327 subs->formats |= 1ULL << fp->format;
2328 return 0;
2329 }
2330 }
2331 /* look for an empty stream */
2332 list_for_each(p, &chip->pcm_list) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002333 as = list_entry(p, struct snd_usb_stream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 if (as->fmt_type != fp->fmt_type)
2335 continue;
2336 subs = &as->substream[stream];
2337 if (subs->endpoint)
2338 continue;
2339 err = snd_pcm_new_stream(as->pcm, stream, 1);
2340 if (err < 0)
2341 return err;
2342 init_substream(as, stream, fp);
2343 return 0;
2344 }
2345
2346 /* create a new pcm */
Panagiotis Issaris59feddb2006-07-25 15:28:03 +02002347 as = kzalloc(sizeof(*as), GFP_KERNEL);
Pavel Machek07f51a72008-04-14 13:15:56 +02002348 if (!as)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 as->pcm_index = chip->pcm_devs;
2351 as->chip = chip;
2352 as->fmt_type = fp->fmt_type;
2353 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
2354 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
2355 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
2356 &pcm);
2357 if (err < 0) {
2358 kfree(as);
2359 return err;
2360 }
2361 as->pcm = pcm;
2362 pcm->private_data = as;
2363 pcm->private_free = snd_usb_audio_pcm_free;
2364 pcm->info_flags = 0;
2365 if (chip->pcm_devs > 0)
2366 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
2367 else
2368 strcpy(pcm->name, "USB Audio");
2369
2370 init_substream(as, stream, fp);
2371
2372 list_add(&as->list, &chip->pcm_list);
2373 chip->pcm_devs++;
2374
2375 proc_pcm_format_add(as);
2376
2377 return 0;
2378}
2379
2380
2381/*
2382 * check if the device uses big-endian samples
2383 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002384static int is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002386 switch (chip->usb_id) {
2387 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
2388 if (fp->endpoint & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return 1;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002390 break;
2391 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
Thibault Le Meurf8c78b82007-07-12 11:26:35 +02002392 if (device_setup[chip->index] == 0x00 ||
2393 fp->altsetting==1 || fp->altsetting==2 || fp->altsetting==3)
2394 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396 return 0;
2397}
2398
2399/*
2400 * parse the audio format type I descriptor
2401 * and returns the corresponding pcm format
2402 *
2403 * @dev: usb device
2404 * @fp: audioformat record
2405 * @format: the format tag (wFormatTag)
2406 * @fmt: the format type descriptor
2407 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002408static int parse_audio_format_i_type(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 int format, unsigned char *fmt)
2410{
2411 int pcm_format;
2412 int sample_width, sample_bytes;
2413
2414 /* FIXME: correct endianess and sign? */
2415 pcm_format = -1;
2416 sample_width = fmt[6];
2417 sample_bytes = fmt[5];
2418 switch (format) {
2419 case 0: /* some devices don't define this correctly... */
2420 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002421 chip->dev->devnum, fp->iface, fp->altsetting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 /* fall-through */
2423 case USB_AUDIO_FORMAT_PCM:
2424 if (sample_width > sample_bytes * 8) {
2425 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002426 chip->dev->devnum, fp->iface, fp->altsetting,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 sample_width, sample_bytes);
2428 }
2429 /* check the format byte size */
2430 switch (fmt[5]) {
2431 case 1:
2432 pcm_format = SNDRV_PCM_FORMAT_S8;
2433 break;
2434 case 2:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002435 if (is_big_endian_format(chip, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
2437 else
2438 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2439 break;
2440 case 3:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002441 if (is_big_endian_format(chip, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
2443 else
2444 pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
2445 break;
2446 case 4:
2447 pcm_format = SNDRV_PCM_FORMAT_S32_LE;
2448 break;
2449 default:
2450 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002451 chip->dev->devnum, fp->iface,
2452 fp->altsetting, sample_width, sample_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 break;
2454 }
2455 break;
2456 case USB_AUDIO_FORMAT_PCM8:
Pavel Machek2a56f512008-04-14 13:14:22 +02002457 pcm_format = SNDRV_PCM_FORMAT_U8;
2458
2459 /* Dallas DS4201 workaround: it advertises U8 format, but really
2460 supports S8. */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002461 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 pcm_format = SNDRV_PCM_FORMAT_S8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 break;
2464 case USB_AUDIO_FORMAT_IEEE_FLOAT:
2465 pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
2466 break;
2467 case USB_AUDIO_FORMAT_ALAW:
2468 pcm_format = SNDRV_PCM_FORMAT_A_LAW;
2469 break;
2470 case USB_AUDIO_FORMAT_MU_LAW:
2471 pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
2472 break;
2473 default:
2474 snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002475 chip->dev->devnum, fp->iface, fp->altsetting, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 break;
2477 }
2478 return pcm_format;
2479}
2480
2481
2482/*
2483 * parse the format descriptor and stores the possible sample rates
2484 * on the audioformat table.
2485 *
2486 * @dev: usb device
2487 * @fp: audioformat record
2488 * @fmt: the format descriptor
2489 * @offset: the start offset of descriptor pointing the rate type
2490 * (7 for type I and II, 8 for type II)
2491 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002492static int parse_audio_format_rates(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 unsigned char *fmt, int offset)
2494{
2495 int nr_rates = fmt[offset];
Clemens Ladisch918f3a02007-08-13 17:40:54 +02002496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
2498 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002499 chip->dev->devnum, fp->iface, fp->altsetting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return -1;
2501 }
2502
2503 if (nr_rates) {
2504 /*
2505 * build the rate table and bitmap flags
2506 */
Clemens Ladisch918f3a02007-08-13 17:40:54 +02002507 int r, idx;
Clemens Ladisch918f3a02007-08-13 17:40:54 +02002508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2510 if (fp->rate_table == NULL) {
2511 snd_printk(KERN_ERR "cannot malloc\n");
2512 return -1;
2513 }
2514
Takashi Iwai04125582009-02-16 22:48:12 +01002515 fp->nr_rates = 0;
2516 fp->rate_min = fp->rate_max = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
Clemens Ladisch987411b2006-11-20 14:14:39 +01002518 unsigned int rate = combine_triple(&fmt[idx]);
Takashi Iwai04125582009-02-16 22:48:12 +01002519 if (!rate)
2520 continue;
Clemens Ladisch987411b2006-11-20 14:14:39 +01002521 /* C-Media CM6501 mislabels its 96 kHz altsetting */
2522 if (rate == 48000 && nr_rates == 1 &&
Joris van Rantwijk3b03cc52009-02-16 22:58:23 +01002523 (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
2524 chip->usb_id == USB_ID(0x0d8c, 0x0102)) &&
Clemens Ladisch987411b2006-11-20 14:14:39 +01002525 fp->altsetting == 5 && fp->maxpacksize == 392)
2526 rate = 96000;
Takashi Iwai04125582009-02-16 22:48:12 +01002527 fp->rate_table[fp->nr_rates] = rate;
2528 if (!fp->rate_min || rate < fp->rate_min)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 fp->rate_min = rate;
Takashi Iwai04125582009-02-16 22:48:12 +01002530 if (!fp->rate_max || rate > fp->rate_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 fp->rate_max = rate;
Clemens Ladisch918f3a02007-08-13 17:40:54 +02002532 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
Takashi Iwai04125582009-02-16 22:48:12 +01002533 fp->nr_rates++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
Takashi Iwai04125582009-02-16 22:48:12 +01002535 if (!fp->nr_rates) {
Gregor Jasnybeb60112007-01-31 12:27:39 +01002536 hwc_debug("All rates were zero. Skipping format!\n");
2537 return -1;
2538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 } else {
2540 /* continuous rates */
2541 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
2542 fp->rate_min = combine_triple(&fmt[offset + 1]);
2543 fp->rate_max = combine_triple(&fmt[offset + 4]);
2544 }
2545 return 0;
2546}
2547
2548/*
2549 * parse the format type I and III descriptors
2550 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002551static int parse_audio_format_i(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 int format, unsigned char *fmt)
2553{
2554 int pcm_format;
2555
2556 if (fmt[3] == USB_FORMAT_TYPE_III) {
2557 /* FIXME: the format type is really IECxxx
2558 * but we give normal PCM format to get the existing
2559 * apps working...
2560 */
Thibault Le Meurcac19c32007-07-13 11:50:23 +02002561 switch (chip->usb_id) {
2562
2563 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2564 if (device_setup[chip->index] == 0x00 &&
2565 fp->altsetting == 6)
2566 pcm_format = SNDRV_PCM_FORMAT_S16_BE;
2567 else
2568 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2569 break;
2570 default:
2571 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 } else {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002574 pcm_format = parse_audio_format_i_type(chip, fp, format, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 if (pcm_format < 0)
2576 return -1;
2577 }
2578 fp->format = pcm_format;
2579 fp->channels = fmt[4];
2580 if (fp->channels < 1) {
2581 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002582 chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return -1;
2584 }
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002585 return parse_audio_format_rates(chip, fp, fmt, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
2587
2588/*
2589 * prase the format type II descriptor
2590 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002591static int parse_audio_format_ii(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 int format, unsigned char *fmt)
2593{
2594 int brate, framesize;
2595 switch (format) {
2596 case USB_AUDIO_FORMAT_AC3:
2597 /* FIXME: there is no AC3 format defined yet */
2598 // fp->format = SNDRV_PCM_FORMAT_AC3;
2599 fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
2600 break;
2601 case USB_AUDIO_FORMAT_MPEG:
2602 fp->format = SNDRV_PCM_FORMAT_MPEG;
2603 break;
2604 default:
Andreas Bergmeierb9d710b2009-01-24 12:15:14 +01002605 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002606 chip->dev->devnum, fp->iface, fp->altsetting, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 fp->format = SNDRV_PCM_FORMAT_MPEG;
2608 break;
2609 }
2610 fp->channels = 1;
2611 brate = combine_word(&fmt[4]); /* fmt[4,5] : wMaxBitRate (in kbps) */
2612 framesize = combine_word(&fmt[6]); /* fmt[6,7]: wSamplesPerFrame */
2613 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
2614 fp->frame_size = framesize;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002615 return parse_audio_format_rates(chip, fp, fmt, 8); /* fmt[8..] sample rates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
Takashi Iwai86e07d32005-11-17 15:08:02 +01002618static int parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 int format, unsigned char *fmt, int stream)
2620{
2621 int err;
2622
2623 switch (fmt[3]) {
2624 case USB_FORMAT_TYPE_I:
2625 case USB_FORMAT_TYPE_III:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002626 err = parse_audio_format_i(chip, fp, format, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 break;
2628 case USB_FORMAT_TYPE_II:
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002629 err = parse_audio_format_ii(chip, fp, format, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 break;
2631 default:
2632 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002633 chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 return -1;
2635 }
2636 fp->fmt_type = fmt[3];
2637 if (err < 0)
2638 return err;
2639#if 1
Clemens Ladisch33159372006-01-13 08:11:22 +01002640 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 /* extigy apparently supports sample rates other than 48k
2642 * but not in ordinary way. so we enable only 48k atm.
2643 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002644 if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
Clemens Ladisch33159372006-01-13 08:11:22 +01002645 chip->usb_id == USB_ID(0x041e, 0x3020) ||
2646 chip->usb_id == USB_ID(0x041e, 0x3061)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (fmt[3] == USB_FORMAT_TYPE_I &&
Clemens Ladisch8c1872d2005-04-28 09:31:53 +02002648 fp->rates != SNDRV_PCM_RATE_48000 &&
2649 fp->rates != SNDRV_PCM_RATE_96000)
Clemens Ladischb4d3f9d2005-06-27 08:18:27 +02002650 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
2652#endif
2653 return 0;
2654}
2655
Clemens Ladisch744b89e2009-04-03 09:45:01 +02002656static unsigned char parse_datainterval(struct snd_usb_audio *chip,
2657 struct usb_host_interface *alts)
2658{
2659 if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH &&
2660 get_endpoint(alts, 0)->bInterval >= 1 &&
2661 get_endpoint(alts, 0)->bInterval <= 4)
2662 return get_endpoint(alts, 0)->bInterval - 1;
2663 else
2664 return 0;
2665}
2666
Thibault LE MEURe3113342006-03-14 11:44:53 +01002667static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
2668 int iface, int altno);
Takashi Iwai86e07d32005-11-17 15:08:02 +01002669static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
2671 struct usb_device *dev;
2672 struct usb_interface *iface;
2673 struct usb_host_interface *alts;
2674 struct usb_interface_descriptor *altsd;
2675 int i, altno, err, stream;
2676 int format;
Clemens Ladisch8886f332009-07-13 13:21:58 +02002677 struct audioformat *fp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 unsigned char *fmt, *csep;
Pavel Machekb9d43bc2008-04-14 13:12:47 +02002679 int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681 dev = chip->dev;
2682
2683 /* parse the interface's altsettings */
2684 iface = usb_ifnum_to_if(dev, iface_no);
Pavel Machekb9d43bc2008-04-14 13:12:47 +02002685
2686 num = iface->num_altsetting;
2687
2688 /*
2689 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
2690 * one misses syncpipe, and does not produce any sound.
2691 */
2692 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
2693 num = 4;
2694
2695 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 alts = &iface->altsetting[i];
2697 altsd = get_iface_desc(alts);
2698 /* skip invalid one */
2699 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2700 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2701 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING &&
2702 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
2703 altsd->bNumEndpoints < 1 ||
2704 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
2705 continue;
2706 /* must be isochronous */
2707 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2708 USB_ENDPOINT_XFER_ISOC)
2709 continue;
2710 /* check direction */
2711 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
2712 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2713 altno = altsd->bAlternateSetting;
Thibault LE MEURe3113342006-03-14 11:44:53 +01002714
2715 /* audiophile usb: skip altsets incompatible with device_setup
2716 */
2717 if (chip->usb_id == USB_ID(0x0763, 0x2003) &&
2718 audiophile_skip_setting_quirk(chip, iface_no, altno))
2719 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721 /* get audio formats */
2722 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, AS_GENERAL);
2723 if (!fmt) {
2724 snd_printk(KERN_ERR "%d:%u:%d : AS_GENERAL descriptor not found\n",
2725 dev->devnum, iface_no, altno);
2726 continue;
2727 }
2728
2729 if (fmt[0] < 7) {
2730 snd_printk(KERN_ERR "%d:%u:%d : invalid AS_GENERAL desc\n",
2731 dev->devnum, iface_no, altno);
2732 continue;
2733 }
2734
2735 format = (fmt[6] << 8) | fmt[5]; /* remember the format value */
2736
2737 /* get format type */
2738 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, FORMAT_TYPE);
2739 if (!fmt) {
2740 snd_printk(KERN_ERR "%d:%u:%d : no FORMAT_TYPE desc\n",
2741 dev->devnum, iface_no, altno);
2742 continue;
2743 }
2744 if (fmt[0] < 8) {
2745 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
2746 dev->devnum, iface_no, altno);
2747 continue;
2748 }
2749
Clemens Ladisch8886f332009-07-13 13:21:58 +02002750 /*
2751 * Blue Microphones workaround: The last altsetting is identical
2752 * with the previous one, except for a larger packet size, but
2753 * is actually a mislabeled two-channel setting; ignore it.
2754 */
2755 if (fmt[4] == 1 && fmt[5] == 2 && altno == 2 && num == 3 &&
2756 fp && fp->altsetting == 1 && fp->channels == 1 &&
2757 fp->format == SNDRV_PCM_FORMAT_S16_LE &&
2758 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
2759 fp->maxpacksize * 2)
2760 continue;
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
2763 /* Creamware Noah has this descriptor after the 2nd endpoint */
2764 if (!csep && altsd->bNumEndpoints >= 2)
2765 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
2766 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
Takashi Iwaif8c75792006-05-18 14:47:03 +02002767 snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
Clemens Ladischfaf8d112006-05-18 09:35:15 +02002768 " class specific endpoint descriptor\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 dev->devnum, iface_no, altno);
Clemens Ladischfaf8d112006-05-18 09:35:15 +02002770 csep = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
2772
Panagiotis Issaris59feddb2006-07-25 15:28:03 +02002773 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (! fp) {
2775 snd_printk(KERN_ERR "cannot malloc\n");
2776 return -ENOMEM;
2777 }
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 fp->iface = iface_no;
2780 fp->altsetting = altno;
2781 fp->altset_idx = i;
2782 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2783 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
Clemens Ladisch744b89e2009-04-03 09:45:01 +02002784 fp->datainterval = parse_datainterval(chip, alts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Clemens Ladisch573567e2005-06-27 08:17:30 +02002786 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
2787 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
2788 * (fp->maxpacksize & 0x7ff);
Clemens Ladischfaf8d112006-05-18 09:35:15 +02002789 fp->attributes = csep ? csep[3] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 /* some quirks for attributes here */
2792
Clemens Ladischc3f93292005-05-04 14:56:04 +02002793 switch (chip->usb_id) {
2794 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 /* Optoplay sets the sample rate attribute although
2796 * it seems not supporting it in fact.
2797 */
2798 fp->attributes &= ~EP_CS_ATTR_SAMPLE_RATE;
Clemens Ladischc3f93292005-05-04 14:56:04 +02002799 break;
2800 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
2801 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 /* doesn't set the sample rate attribute, but supports it */
2803 fp->attributes |= EP_CS_ATTR_SAMPLE_RATE;
Clemens Ladischc3f93292005-05-04 14:56:04 +02002804 break;
2805 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
2806 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
2807 an older model 77d:223) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 /*
2809 * plantronics headset and Griffin iMic have set adaptive-in
2810 * although it's really not...
2811 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 fp->ep_attr &= ~EP_ATTR_MASK;
2813 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2814 fp->ep_attr |= EP_ATTR_ADAPTIVE;
2815 else
2816 fp->ep_attr |= EP_ATTR_SYNC;
Clemens Ladischc3f93292005-05-04 14:56:04 +02002817 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
2819
2820 /* ok, let's parse further... */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02002821 if (parse_audio_format(chip, fp, format, fmt, stream) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 kfree(fp->rate_table);
2823 kfree(fp);
2824 continue;
2825 }
2826
Andreas Bergmeierb9d710b2009-01-24 12:15:14 +01002827 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 -07002828 err = add_audio_endpoint(chip, stream, fp);
2829 if (err < 0) {
2830 kfree(fp->rate_table);
2831 kfree(fp);
2832 return err;
2833 }
2834 /* try to set the interface... */
2835 usb_set_interface(chip->dev, iface_no, altno);
2836 init_usb_pitch(chip->dev, iface_no, alts, fp);
2837 init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
2838 }
2839 return 0;
2840}
2841
2842
2843/*
2844 * disconnect streams
2845 * called from snd_usb_audio_disconnect()
2846 */
Clemens Ladischee733392005-04-25 10:34:13 +02002847static void snd_usb_stream_disconnect(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849 int idx;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002850 struct snd_usb_stream *as;
2851 struct snd_usb_substream *subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Takashi Iwai86e07d32005-11-17 15:08:02 +01002853 as = list_entry(head, struct snd_usb_stream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 for (idx = 0; idx < 2; idx++) {
2855 subs = &as->substream[idx];
2856 if (!subs->num_formats)
2857 return;
2858 release_substream_urbs(subs, 1);
2859 subs->interface = -1;
2860 }
2861}
2862
2863/*
2864 * parse audio control descriptor and create pcm/midi streams
2865 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002866static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
2868 struct usb_device *dev = chip->dev;
2869 struct usb_host_interface *host_iface;
2870 struct usb_interface *iface;
2871 unsigned char *p1;
2872 int i, j;
2873
2874 /* find audiocontrol interface */
2875 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
2876 if (!(p1 = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen, NULL, HEADER))) {
2877 snd_printk(KERN_ERR "cannot find HEADER\n");
2878 return -EINVAL;
2879 }
2880 if (! p1[7] || p1[0] < 8 + p1[7]) {
2881 snd_printk(KERN_ERR "invalid HEADER\n");
2882 return -EINVAL;
2883 }
2884
2885 /*
2886 * parse all USB audio streaming interfaces
2887 */
2888 for (i = 0; i < p1[7]; i++) {
2889 struct usb_host_interface *alts;
2890 struct usb_interface_descriptor *altsd;
2891 j = p1[8 + i];
2892 iface = usb_ifnum_to_if(dev, j);
2893 if (!iface) {
2894 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
2895 dev->devnum, ctrlif, j);
2896 continue;
2897 }
2898 if (usb_interface_claimed(iface)) {
2899 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n", dev->devnum, ctrlif, j);
2900 continue;
2901 }
2902 alts = &iface->altsetting[0];
2903 altsd = get_iface_desc(alts);
2904 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
2905 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
2906 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDI_STREAMING) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002907 int err = snd_usbmidi_create(chip->card, iface,
2908 &chip->midi_list, NULL);
2909 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n", dev->devnum, ctrlif, j);
2911 continue;
2912 }
2913 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2914 continue;
2915 }
2916 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2917 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2918 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING) {
2919 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n", dev->devnum, ctrlif, j, altsd->bInterfaceClass);
2920 /* skip non-supported classes */
2921 continue;
2922 }
Clemens Ladisch076639f2007-08-21 08:56:54 +02002923 if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
2924 snd_printk(KERN_ERR "low speed audio streaming not supported\n");
2925 continue;
2926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 if (! parse_audio_endpoints(chip, j)) {
2928 usb_set_interface(dev, j, 0); /* reset the current interface */
2929 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2930 }
2931 }
2932
2933 return 0;
2934}
2935
2936/*
2937 * create a stream for an endpoint/altsetting without proper descriptors
2938 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002939static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002941 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
2943 struct audioformat *fp;
2944 struct usb_host_interface *alts;
2945 int stream, err;
Al Virob4482a42007-10-14 19:35:40 +01002946 unsigned *rate_table = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Alexey Dobriyan52978be2006-09-30 23:27:21 -07002948 fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 if (! fp) {
Alexey Dobriyan52978be2006-09-30 23:27:21 -07002950 snd_printk(KERN_ERR "cannot memdup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 return -ENOMEM;
2952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 if (fp->nr_rates > 0) {
2954 rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
2955 if (!rate_table) {
2956 kfree(fp);
2957 return -ENOMEM;
2958 }
2959 memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
2960 fp->rate_table = rate_table;
2961 }
2962
2963 stream = (fp->endpoint & USB_DIR_IN)
2964 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2965 err = add_audio_endpoint(chip, stream, fp);
2966 if (err < 0) {
2967 kfree(fp);
2968 kfree(rate_table);
2969 return err;
2970 }
2971 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
2972 fp->altset_idx >= iface->num_altsetting) {
2973 kfree(fp);
2974 kfree(rate_table);
2975 return -EINVAL;
2976 }
2977 alts = &iface->altsetting[fp->altset_idx];
Clemens Ladisch744b89e2009-04-03 09:45:01 +02002978 fp->datainterval = parse_datainterval(chip, alts);
Clemens Ladisch894dcd72009-02-06 08:13:07 +01002979 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 usb_set_interface(chip->dev, fp->iface, 0);
2981 init_usb_pitch(chip->dev, fp->iface, alts, fp);
2982 init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
2983 return 0;
2984}
2985
2986/*
2987 * create a stream for an interface with proper descriptors
2988 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01002989static int create_standard_audio_quirk(struct snd_usb_audio *chip,
Clemens Ladischd1bda042005-09-14 08:36:03 +02002990 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002991 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
2993 struct usb_host_interface *alts;
2994 struct usb_interface_descriptor *altsd;
2995 int err;
2996
2997 alts = &iface->altsetting[0];
2998 altsd = get_iface_desc(alts);
Clemens Ladischd1bda042005-09-14 08:36:03 +02002999 err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (err < 0) {
3001 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
3002 altsd->bInterfaceNumber, err);
3003 return err;
3004 }
Clemens Ladischd1bda042005-09-14 08:36:03 +02003005 /* reset the current interface */
3006 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return 0;
3008}
3009
3010/*
Pedro Lopez-Cabanillas310e0dc2008-10-04 16:27:36 +02003011 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
3012 * The only way to detect the sample rate is by looking at wMaxPacketSize.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 */
Pedro Lopez-Cabanillas310e0dc2008-10-04 16:27:36 +02003014static int create_uaxx_quirk(struct snd_usb_audio *chip,
3015 struct usb_interface *iface,
3016 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
3018 static const struct audioformat ua_format = {
3019 .format = SNDRV_PCM_FORMAT_S24_3LE,
3020 .channels = 2,
3021 .fmt_type = USB_FORMAT_TYPE_I,
3022 .altsetting = 1,
3023 .altset_idx = 1,
3024 .rates = SNDRV_PCM_RATE_CONTINUOUS,
3025 };
3026 struct usb_host_interface *alts;
3027 struct usb_interface_descriptor *altsd;
3028 struct audioformat *fp;
3029 int stream, err;
3030
Pedro Lopez-Cabanillas310e0dc2008-10-04 16:27:36 +02003031 /* both PCM and MIDI interfaces have 2 or more altsettings */
3032 if (iface->num_altsetting < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return -ENXIO;
3034 alts = &iface->altsetting[1];
3035 altsd = get_iface_desc(alts);
3036
Pedro Lopez-Cabanillas59b3db62008-10-07 20:54:18 +02003037 if (altsd->bNumEndpoints == 2) {
3038 static const struct snd_usb_midi_endpoint_info ua700_ep = {
3039 .out_cables = 0x0003,
3040 .in_cables = 0x0003
3041 };
3042 static const struct snd_usb_audio_quirk ua700_quirk = {
3043 .type = QUIRK_MIDI_FIXED_ENDPOINT,
3044 .data = &ua700_ep
3045 };
3046 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
3047 .out_cables = 0x0001,
3048 .in_cables = 0x0001
3049 };
3050 static const struct snd_usb_audio_quirk uaxx_quirk = {
3051 .type = QUIRK_MIDI_FIXED_ENDPOINT,
3052 .data = &uaxx_ep
3053 };
Clemens Ladischd82af9f2009-11-16 12:23:46 +01003054 const struct snd_usb_audio_quirk *quirk =
3055 chip->usb_id == USB_ID(0x0582, 0x002b)
3056 ? &ua700_quirk : &uaxx_quirk;
3057 return snd_usbmidi_create(chip->card, iface,
3058 &chip->midi_list, quirk);
Pedro Lopez-Cabanillas59b3db62008-10-07 20:54:18 +02003059 }
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 if (altsd->bNumEndpoints != 1)
3062 return -ENXIO;
3063
3064 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
3065 if (!fp)
3066 return -ENOMEM;
3067 memcpy(fp, &ua_format, sizeof(*fp));
3068
3069 fp->iface = altsd->bInterfaceNumber;
3070 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
3071 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
Clemens Ladisch744b89e2009-04-03 09:45:01 +02003072 fp->datainterval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
3074
3075 switch (fp->maxpacksize) {
3076 case 0x120:
3077 fp->rate_max = fp->rate_min = 44100;
3078 break;
3079 case 0x138:
3080 case 0x140:
3081 fp->rate_max = fp->rate_min = 48000;
3082 break;
3083 case 0x258:
3084 case 0x260:
3085 fp->rate_max = fp->rate_min = 96000;
3086 break;
3087 default:
3088 snd_printk(KERN_ERR "unknown sample rate\n");
3089 kfree(fp);
3090 return -ENXIO;
3091 }
3092
3093 stream = (fp->endpoint & USB_DIR_IN)
3094 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
3095 err = add_audio_endpoint(chip, stream, fp);
3096 if (err < 0) {
3097 kfree(fp);
3098 return err;
3099 }
3100 usb_set_interface(chip->dev, fp->iface, 0);
3101 return 0;
3102}
3103
3104/*
3105 * Create a stream for an Edirol UA-1000 interface.
3106 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003107static int create_ua1000_quirk(struct snd_usb_audio *chip,
Clemens Ladisch854af952005-07-25 16:19:10 +02003108 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003109 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
3111 static const struct audioformat ua1000_format = {
3112 .format = SNDRV_PCM_FORMAT_S32_LE,
3113 .fmt_type = USB_FORMAT_TYPE_I,
3114 .altsetting = 1,
3115 .altset_idx = 1,
3116 .attributes = 0,
3117 .rates = SNDRV_PCM_RATE_CONTINUOUS,
3118 };
3119 struct usb_host_interface *alts;
3120 struct usb_interface_descriptor *altsd;
3121 struct audioformat *fp;
3122 int stream, err;
3123
3124 if (iface->num_altsetting != 2)
3125 return -ENXIO;
3126 alts = &iface->altsetting[1];
3127 altsd = get_iface_desc(alts);
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02003128 if (alts->extralen != 11 || alts->extra[1] != USB_DT_CS_INTERFACE ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 altsd->bNumEndpoints != 1)
3130 return -ENXIO;
3131
Alexey Dobriyan52978be2006-09-30 23:27:21 -07003132 fp = kmemdup(&ua1000_format, sizeof(*fp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (!fp)
3134 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 fp->channels = alts->extra[4];
3137 fp->iface = altsd->bInterfaceNumber;
3138 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
3139 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
Clemens Ladisch744b89e2009-04-03 09:45:01 +02003140 fp->datainterval = parse_datainterval(chip, alts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
3142 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]);
3143
3144 stream = (fp->endpoint & USB_DIR_IN)
3145 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
3146 err = add_audio_endpoint(chip, stream, fp);
3147 if (err < 0) {
3148 kfree(fp);
3149 return err;
3150 }
3151 /* FIXME: playback must be synchronized to capture */
3152 usb_set_interface(chip->dev, fp->iface, 0);
3153 return 0;
3154}
3155
Takashi Iwai86e07d32005-11-17 15:08:02 +01003156static int snd_usb_create_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003158 const struct snd_usb_audio_quirk *quirk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160/*
3161 * handle the quirks for the contained interfaces
3162 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003163static int create_composite_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003165 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166{
3167 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
3168 int err;
3169
3170 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
3171 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
3172 if (!iface)
3173 continue;
3174 if (quirk->ifnum != probed_ifnum &&
3175 usb_interface_claimed(iface))
3176 continue;
3177 err = snd_usb_create_quirk(chip, iface, quirk);
3178 if (err < 0)
3179 return err;
3180 if (quirk->ifnum != probed_ifnum)
3181 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
3182 }
3183 return 0;
3184}
3185
Takashi Iwai86e07d32005-11-17 15:08:02 +01003186static int ignore_interface_quirk(struct snd_usb_audio *chip,
Clemens Ladisch854af952005-07-25 16:19:10 +02003187 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003188 const struct snd_usb_audio_quirk *quirk)
Clemens Ladisch854af952005-07-25 16:19:10 +02003189{
3190 return 0;
3191}
3192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
3194/*
3195 * boot quirks
3196 */
3197
3198#define EXTIGY_FIRMWARE_SIZE_OLD 794
3199#define EXTIGY_FIRMWARE_SIZE_NEW 483
3200
3201static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
3202{
3203 struct usb_host_config *config = dev->actconfig;
3204 int err;
3205
3206 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
3207 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
3208 snd_printdd("sending Extigy boot sequence...\n");
3209 /* Send message to force it to reconnect with full interface. */
3210 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
3211 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
3212 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
3213 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
3214 &dev->descriptor, sizeof(dev->descriptor));
3215 config = dev->actconfig;
3216 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
3217 err = usb_reset_configuration(dev);
3218 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
3219 snd_printdd("extigy_boot: new boot length = %d\n",
3220 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
3221 return -ENODEV; /* quit this anyway */
3222 }
3223 return 0;
3224}
3225
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003226static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
3227{
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003228 u8 buf = 1;
3229
3230 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
3231 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3232 0, 0, &buf, 1, 1000);
3233 if (buf == 0) {
3234 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
3235 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
3236 1, 2000, NULL, 0, 1000);
3237 return -ENODEV;
3238 }
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003239 return 0;
3240}
3241
Thibault LE MEURe3113342006-03-14 11:44:53 +01003242/*
Sam Revitche217e302006-06-23 15:10:18 +02003243 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
3244 * documented in the device's data sheet.
3245 */
3246static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
3247{
3248 u8 buf[4];
3249 buf[0] = 0x20;
3250 buf[1] = value & 0xff;
3251 buf[2] = (value >> 8) & 0xff;
3252 buf[3] = reg;
3253 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
3254 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
3255 0, 0, &buf, 4, 1000);
3256}
3257
3258static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
3259{
3260 /*
3261 * Enable line-out driver mode, set headphone source to front
3262 * channels, enable stereo mic.
3263 */
3264 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
3265}
3266
Dan Allongo92a43792009-06-08 11:21:52 -04003267/*
3268 * C-Media CM6206 is based on CM106 with two additional
3269 * registers that are not documented in the data sheet.
3270 * Values here are chosen based on sniffing USB traffic
3271 * under Windows.
3272 */
3273static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
3274{
3275 int err, reg;
3276 int val[] = {0x200c, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
3277
3278 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
3279 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
3280 if (err < 0)
3281 return err;
3282 }
3283
3284 return err;
3285}
Sam Revitche217e302006-06-23 15:10:18 +02003286
3287/*
Thibault LE MEURe3113342006-03-14 11:44:53 +01003288 * Setup quirks
3289 */
3290#define AUDIOPHILE_SET 0x01 /* if set, parse device_setup */
3291#define AUDIOPHILE_SET_DTS 0x02 /* if set, enable DTS Digital Output */
3292#define AUDIOPHILE_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
3293#define AUDIOPHILE_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
3294#define AUDIOPHILE_SET_DI 0x10 /* if set, enable Digital Input */
3295#define AUDIOPHILE_SET_MASK 0x1F /* bit mask for setup value */
3296#define AUDIOPHILE_SET_24B_48K_DI 0x19 /* value for 24bits+48KHz+Digital Input */
3297#define AUDIOPHILE_SET_24B_48K_NOTDI 0x09 /* value for 24bits+48KHz+No Digital Input */
3298#define AUDIOPHILE_SET_16B_48K_DI 0x11 /* value for 16bits+48KHz+Digital Input */
3299#define AUDIOPHILE_SET_16B_48K_NOTDI 0x01 /* value for 16bits+48KHz+No Digital Input */
3300
3301static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
3302 int iface, int altno)
3303{
Thibault Le Meurf8c78b82007-07-12 11:26:35 +02003304 /* Reset ALL ifaces to 0 altsetting.
3305 * Call it for every possible altsetting of every interface.
3306 */
3307 usb_set_interface(chip->dev, iface, 0);
3308
Thibault LE MEURe3113342006-03-14 11:44:53 +01003309 if (device_setup[chip->index] & AUDIOPHILE_SET) {
3310 if ((device_setup[chip->index] & AUDIOPHILE_SET_DTS)
3311 && altno != 6)
3312 return 1; /* skip this altsetting */
3313 if ((device_setup[chip->index] & AUDIOPHILE_SET_96K)
3314 && altno != 1)
3315 return 1; /* skip this altsetting */
3316 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3317 AUDIOPHILE_SET_24B_48K_DI && altno != 2)
3318 return 1; /* skip this altsetting */
3319 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3320 AUDIOPHILE_SET_24B_48K_NOTDI && altno != 3)
3321 return 1; /* skip this altsetting */
3322 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3323 AUDIOPHILE_SET_16B_48K_DI && altno != 4)
3324 return 1; /* skip this altsetting */
3325 if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
3326 AUDIOPHILE_SET_16B_48K_NOTDI && altno != 5)
3327 return 1; /* skip this altsetting */
3328 }
3329 return 0; /* keep this altsetting */
3330}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Clemens Ladischd82af9f2009-11-16 12:23:46 +01003332static int create_any_midi_quirk(struct snd_usb_audio *chip,
3333 struct usb_interface *intf,
3334 const struct snd_usb_audio_quirk *quirk)
3335{
3336 return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
3337}
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339/*
3340 * audio-interface quirks
3341 *
3342 * returns zero if no standard audio/MIDI parsing is needed.
3343 * returns a postive value if standard audio/midi interfaces are parsed
3344 * after this.
3345 * returns a negative value at error.
3346 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003347static int snd_usb_create_quirk(struct snd_usb_audio *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 struct usb_interface *iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003349 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003351 typedef int (*quirk_func_t)(struct snd_usb_audio *, struct usb_interface *,
3352 const struct snd_usb_audio_quirk *);
Clemens Ladisch854af952005-07-25 16:19:10 +02003353 static const quirk_func_t quirk_funcs[] = {
3354 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
3355 [QUIRK_COMPOSITE] = create_composite_quirk,
Clemens Ladischd82af9f2009-11-16 12:23:46 +01003356 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
3357 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
3358 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
3359 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
3360 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
3361 [QUIRK_MIDI_FASTLANE] = create_any_midi_quirk,
3362 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
3363 [QUIRK_MIDI_CME] = create_any_midi_quirk,
Clemens Ladischd1bda042005-09-14 08:36:03 +02003364 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
Clemens Ladisch854af952005-07-25 16:19:10 +02003365 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
Clemens Ladisch854af952005-07-25 16:19:10 +02003366 [QUIRK_AUDIO_EDIROL_UA1000] = create_ua1000_quirk,
Pedro Lopez-Cabanillas310e0dc2008-10-04 16:27:36 +02003367 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk
Clemens Ladisch854af952005-07-25 16:19:10 +02003368 };
3369
3370 if (quirk->type < QUIRK_TYPE_COUNT) {
3371 return quirk_funcs[quirk->type](chip, iface, quirk);
3372 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
3374 return -ENXIO;
3375 }
3376}
3377
3378
3379/*
3380 * common proc files to show the usb device info
3381 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01003382static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003384 struct snd_usb_audio *chip = entry->private_data;
Pavel Machek07f51a72008-04-14 13:15:56 +02003385 if (!chip->shutdown)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
3387}
3388
Takashi Iwai86e07d32005-11-17 15:08:02 +01003389static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003391 struct snd_usb_audio *chip = entry->private_data;
Pavel Machek07f51a72008-04-14 13:15:56 +02003392 if (!chip->shutdown)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 snd_iprintf(buffer, "%04x:%04x\n",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003394 USB_ID_VENDOR(chip->usb_id),
3395 USB_ID_PRODUCT(chip->usb_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396}
3397
Takashi Iwai86e07d32005-11-17 15:08:02 +01003398static void snd_usb_audio_create_proc(struct snd_usb_audio *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003400 struct snd_info_entry *entry;
Pavel Machek07f51a72008-04-14 13:15:56 +02003401 if (!snd_card_proc_new(chip->card, "usbbus", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02003402 snd_info_set_text_ops(entry, chip, proc_audio_usbbus_read);
Pavel Machek07f51a72008-04-14 13:15:56 +02003403 if (!snd_card_proc_new(chip->card, "usbid", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02003404 snd_info_set_text_ops(entry, chip, proc_audio_usbid_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405}
3406
3407/*
3408 * free the chip instance
3409 *
3410 * here we have to do not much, since pcm and controls are already freed
3411 *
3412 */
3413
Takashi Iwai86e07d32005-11-17 15:08:02 +01003414static int snd_usb_audio_free(struct snd_usb_audio *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415{
3416 kfree(chip);
3417 return 0;
3418}
3419
Takashi Iwai86e07d32005-11-17 15:08:02 +01003420static int snd_usb_audio_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003422 struct snd_usb_audio *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 return snd_usb_audio_free(chip);
3424}
3425
3426
3427/*
3428 * create a chip instance and set its names.
3429 */
3430static int snd_usb_audio_create(struct usb_device *dev, int idx,
Takashi Iwai86e07d32005-11-17 15:08:02 +01003431 const struct snd_usb_audio_quirk *quirk,
3432 struct snd_usb_audio **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003434 struct snd_card *card;
3435 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 int err, len;
3437 char component[14];
Takashi Iwai86e07d32005-11-17 15:08:02 +01003438 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 .dev_free = snd_usb_audio_dev_free,
3440 };
3441
3442 *rchip = NULL;
3443
Clemens Ladisch076639f2007-08-21 08:56:54 +02003444 if (snd_usb_get_speed(dev) != USB_SPEED_LOW &&
3445 snd_usb_get_speed(dev) != USB_SPEED_FULL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
3447 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
3448 return -ENXIO;
3449 }
3450
Takashi Iwaibd7dd772008-12-28 16:45:02 +01003451 err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
3452 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01003454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 }
3456
Takashi Iwai561b2202005-09-09 14:22:34 +02003457 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 if (! chip) {
3459 snd_card_free(card);
3460 return -ENOMEM;
3461 }
3462
3463 chip->index = idx;
3464 chip->dev = dev;
3465 chip->card = card;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003466 chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3467 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 INIT_LIST_HEAD(&chip->pcm_list);
3469 INIT_LIST_HEAD(&chip->midi_list);
Clemens Ladisch84957a82005-04-29 16:23:13 +02003470 INIT_LIST_HEAD(&chip->mixer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
3473 snd_usb_audio_free(chip);
3474 snd_card_free(card);
3475 return err;
3476 }
3477
3478 strcpy(card->driver, "USB-Audio");
3479 sprintf(component, "USB%04x:%04x",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003480 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 snd_component_add(card, component);
3482
3483 /* retrieve the device string as shortname */
3484 if (quirk && quirk->product_name) {
3485 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
3486 } else {
3487 if (!dev->descriptor.iProduct ||
3488 usb_string(dev, dev->descriptor.iProduct,
3489 card->shortname, sizeof(card->shortname)) <= 0) {
3490 /* no name available from anywhere, so use ID */
3491 sprintf(card->shortname, "USB Device %#04x:%#04x",
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003492 USB_ID_VENDOR(chip->usb_id),
3493 USB_ID_PRODUCT(chip->usb_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 }
3495 }
3496
3497 /* retrieve the vendor and device strings as longname */
3498 if (quirk && quirk->vendor_name) {
3499 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
3500 } else {
3501 if (dev->descriptor.iManufacturer)
3502 len = usb_string(dev, dev->descriptor.iManufacturer,
3503 card->longname, sizeof(card->longname));
3504 else
3505 len = 0;
3506 /* we don't really care if there isn't any vendor string */
3507 }
3508 if (len > 0)
3509 strlcat(card->longname, " ", sizeof(card->longname));
3510
3511 strlcat(card->longname, card->shortname, sizeof(card->longname));
3512
3513 len = strlcat(card->longname, " at ", sizeof(card->longname));
3514
3515 if (len < sizeof(card->longname))
3516 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
3517
3518 strlcat(card->longname,
Clemens Ladisch076639f2007-08-21 08:56:54 +02003519 snd_usb_get_speed(dev) == USB_SPEED_LOW ? ", low speed" :
3520 snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" :
3521 ", high speed",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 sizeof(card->longname));
3523
3524 snd_usb_audio_create_proc(chip);
3525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 *rchip = chip;
3527 return 0;
3528}
3529
3530
3531/*
3532 * probe the active usb device
3533 *
3534 * note that this can be called multiple times per a device, when it
3535 * includes multiple audio control interfaces.
3536 *
3537 * thus we check the usb device pointer and creates the card instance
3538 * only at the first time. the successive calls of this function will
3539 * append the pcm interface to the corresponding card.
3540 */
3541static void *snd_usb_audio_probe(struct usb_device *dev,
3542 struct usb_interface *intf,
3543 const struct usb_device_id *usb_id)
3544{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003545 const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 int i, err;
Takashi Iwai86e07d32005-11-17 15:08:02 +01003547 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 struct usb_host_interface *alts;
3549 int ifnum;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003550 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
3552 alts = &intf->altsetting[0];
3553 ifnum = get_iface_desc(alts)->bInterfaceNumber;
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003554 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
3555 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
3557 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
3558 goto __err_val;
3559
3560 /* SB Extigy needs special boot-up sequence */
3561 /* if more models come, this will go to the quirk list. */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003562 if (id == USB_ID(0x041e, 0x3000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 if (snd_usb_extigy_boot_quirk(dev, intf) < 0)
3564 goto __err_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 }
Clemens Ladisch3a2f0852005-05-09 09:20:31 +02003566 /* SB Audigy 2 NX needs its own boot-up magic, too */
3567 if (id == USB_ID(0x041e, 0x3020)) {
3568 if (snd_usb_audigy2nx_boot_quirk(dev) < 0)
3569 goto __err_val;
3570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Sam Revitche217e302006-06-23 15:10:18 +02003572 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
3573 if (id == USB_ID(0x10f5, 0x0200)) {
3574 if (snd_usb_cm106_boot_quirk(dev) < 0)
3575 goto __err_val;
3576 }
3577
Dan Allongo92a43792009-06-08 11:21:52 -04003578 /* C-Media CM6206 / CM106-Like Sound Device */
3579 if (id == USB_ID(0x0d8c, 0x0102)) {
3580 if (snd_usb_cm6206_boot_quirk(dev) < 0)
3581 goto __err_val;
3582 }
3583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 /*
3585 * found a config. now register to ALSA
3586 */
3587
3588 /* check whether it's already registered */
3589 chip = NULL;
Ingo Molnar12aa7572006-01-16 16:36:05 +01003590 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 for (i = 0; i < SNDRV_CARDS; i++) {
3592 if (usb_chip[i] && usb_chip[i]->dev == dev) {
3593 if (usb_chip[i]->shutdown) {
3594 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
3595 goto __error;
3596 }
3597 chip = usb_chip[i];
3598 break;
3599 }
3600 }
3601 if (! chip) {
3602 /* it's a fresh one.
3603 * now look for an empty slot and create a new card instance
3604 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 for (i = 0; i < SNDRV_CARDS; i++)
3606 if (enable[i] && ! usb_chip[i] &&
Clemens Ladisch27d10f52005-05-02 08:51:26 +02003607 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
3608 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
3610 goto __error;
3611 }
Clemens Ladisch1dcd3ec2005-05-10 14:51:40 +02003612 snd_card_set_dev(chip->card, &intf->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 break;
3614 }
Pavel Machek07f51a72008-04-14 13:15:56 +02003615 if (!chip) {
3616 printk(KERN_ERR "no available usb audio device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 goto __error;
3618 }
3619 }
3620
3621 err = 1; /* continue */
3622 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
3623 /* need some special handlings */
3624 if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)
3625 goto __error;
3626 }
3627
3628 if (err > 0) {
3629 /* create normal USB audio interfaces */
3630 if (snd_usb_create_streams(chip, ifnum) < 0 ||
Takashi Iwai7a9b8062008-08-13 15:40:53 +02003631 snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 goto __error;
3633 }
3634 }
3635
3636 /* we are allowed to call snd_card_register() many times */
3637 if (snd_card_register(chip->card) < 0) {
3638 goto __error;
3639 }
3640
3641 usb_chip[chip->index] = chip;
3642 chip->num_interfaces++;
Ingo Molnar12aa7572006-01-16 16:36:05 +01003643 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 return chip;
3645
3646 __error:
3647 if (chip && !chip->num_interfaces)
3648 snd_card_free(chip->card);
Ingo Molnar12aa7572006-01-16 16:36:05 +01003649 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 __err_val:
3651 return NULL;
3652}
3653
3654/*
3655 * we need to take care of counter, since disconnection can be called also
3656 * many times as well as usb_audio_probe().
3657 */
3658static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
3659{
Takashi Iwai86e07d32005-11-17 15:08:02 +01003660 struct snd_usb_audio *chip;
3661 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 struct list_head *p;
3663
3664 if (ptr == (void *)-1L)
3665 return;
3666
3667 chip = ptr;
3668 card = chip->card;
Ingo Molnar12aa7572006-01-16 16:36:05 +01003669 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 chip->shutdown = 1;
3671 chip->num_interfaces--;
3672 if (chip->num_interfaces <= 0) {
3673 snd_card_disconnect(card);
3674 /* release the pcm resources */
3675 list_for_each(p, &chip->pcm_list) {
Clemens Ladischee733392005-04-25 10:34:13 +02003676 snd_usb_stream_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 }
3678 /* release the midi resources */
3679 list_for_each(p, &chip->midi_list) {
Clemens Ladischee733392005-04-25 10:34:13 +02003680 snd_usbmidi_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 }
Clemens Ladisch84957a82005-04-29 16:23:13 +02003682 /* release mixer resources */
3683 list_for_each(p, &chip->mixer_list) {
3684 snd_usb_mixer_disconnect(p);
3685 }
Takashi Iwai9eb70e62008-04-17 12:53:26 +02003686 usb_chip[chip->index] = NULL;
Ingo Molnar12aa7572006-01-16 16:36:05 +01003687 mutex_unlock(&register_mutex);
Takashi Iwaic4614822006-06-23 14:38:23 +02003688 snd_card_free_when_closed(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 } else {
Ingo Molnar12aa7572006-01-16 16:36:05 +01003690 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 }
3692}
3693
3694/*
3695 * new 2.5 USB kernel API
3696 */
3697static int usb_audio_probe(struct usb_interface *intf,
3698 const struct usb_device_id *id)
3699{
3700 void *chip;
3701 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
3702 if (chip) {
Julia Lawallf4e97492009-01-01 18:14:35 +01003703 usb_set_intfdata(intf, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return 0;
3705 } else
3706 return -EIO;
3707}
3708
3709static void usb_audio_disconnect(struct usb_interface *intf)
3710{
3711 snd_usb_audio_disconnect(interface_to_usbdev(intf),
Julia Lawallf4e97492009-01-01 18:14:35 +01003712 usb_get_intfdata(intf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713}
3714
Andrew Morton93521d22007-12-24 14:40:56 +01003715#ifdef CONFIG_PM
Oliver Neukumf85bf292007-12-14 14:42:41 +01003716static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
3717{
Julia Lawallf4e97492009-01-01 18:14:35 +01003718 struct snd_usb_audio *chip = usb_get_intfdata(intf);
Oliver Neukumf85bf292007-12-14 14:42:41 +01003719 struct list_head *p;
3720 struct snd_usb_stream *as;
3721
3722 if (chip == (void *)-1L)
3723 return 0;
3724
3725 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
3726 if (!chip->num_suspended_intf++) {
3727 list_for_each(p, &chip->pcm_list) {
3728 as = list_entry(p, struct snd_usb_stream, list);
3729 snd_pcm_suspend_all(as->pcm);
3730 }
3731 }
3732
3733 return 0;
3734}
3735
3736static int usb_audio_resume(struct usb_interface *intf)
3737{
Julia Lawallf4e97492009-01-01 18:14:35 +01003738 struct snd_usb_audio *chip = usb_get_intfdata(intf);
Oliver Neukumf85bf292007-12-14 14:42:41 +01003739
3740 if (chip == (void *)-1L)
3741 return 0;
3742 if (--chip->num_suspended_intf)
3743 return 0;
3744 /*
3745 * ALSA leaves material resumption to user space
3746 * we just notify
3747 */
3748
3749 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
3750
3751 return 0;
3752}
Andrew Morton93521d22007-12-24 14:40:56 +01003753#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
3755static int __init snd_usb_audio_init(void)
3756{
Clemens Ladischf3990e62009-02-20 09:32:40 +01003757 if (nrpacks < 1 || nrpacks > MAX_PACKS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 printk(KERN_WARNING "invalid nrpacks value.\n");
3759 return -EINVAL;
3760 }
Tobias Klausercf78bbc2006-10-04 18:12:43 +02003761 return usb_register(&usb_audio_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762}
3763
3764
3765static void __exit snd_usb_audio_cleanup(void)
3766{
3767 usb_deregister(&usb_audio_driver);
3768}
3769
3770module_init(snd_usb_audio_init);
3771module_exit(snd_usb_audio_cleanup);