Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
1da177e4 LT |
41 | #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> | |
12aa7579 | 48 | #include <linux/mutex.h> |
28e1b773 DM |
49 | #include <linux/usb/audio.h> |
50 | ||
1da177e4 LT |
51 | #include <sound/core.h> |
52 | #include <sound/info.h> | |
53 | #include <sound/pcm.h> | |
54 | #include <sound/pcm_params.h> | |
55 | #include <sound/initval.h> | |
56 | ||
57 | #include "usbaudio.h" | |
58 | ||
59 | ||
60 | MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); | |
61 | MODULE_DESCRIPTION("USB Audio"); | |
62 | MODULE_LICENSE("GPL"); | |
63 | MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}"); | |
64 | ||
65 | ||
66 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ | |
67 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ | |
07f51a72 PM |
68 | static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */ |
69 | /* Vendor/product IDs for this card */ | |
70 | static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; | |
71 | static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; | |
92b9ac78 | 72 | static int nrpacks = 8; /* max. number of packets per urb */ |
1da177e4 | 73 | static int async_unlink = 1; |
e311334e | 74 | static int device_setup[SNDRV_CARDS]; /* device parameter for this card*/ |
7a9b8063 | 75 | static int ignore_ctl_error; |
1da177e4 LT |
76 | |
77 | module_param_array(index, int, NULL, 0444); | |
78 | MODULE_PARM_DESC(index, "Index value for the USB audio adapter."); | |
79 | module_param_array(id, charp, NULL, 0444); | |
80 | MODULE_PARM_DESC(id, "ID string for the USB audio adapter."); | |
81 | module_param_array(enable, bool, NULL, 0444); | |
82 | MODULE_PARM_DESC(enable, "Enable USB audio adapter."); | |
83 | module_param_array(vid, int, NULL, 0444); | |
84 | MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device."); | |
85 | module_param_array(pid, int, NULL, 0444); | |
86 | MODULE_PARM_DESC(pid, "Product ID for the USB audio device."); | |
71d848ca | 87 | module_param(nrpacks, int, 0644); |
1da177e4 LT |
88 | MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB."); |
89 | module_param(async_unlink, bool, 0444); | |
90 | MODULE_PARM_DESC(async_unlink, "Use async unlink mode."); | |
e311334e TLM |
91 | module_param_array(device_setup, int, NULL, 0444); |
92 | MODULE_PARM_DESC(device_setup, "Specific device setup (if needed)."); | |
7a9b8063 TI |
93 | module_param(ignore_ctl_error, bool, 0444); |
94 | MODULE_PARM_DESC(ignore_ctl_error, | |
95 | "Ignore errors from USB controller for mixer interfaces."); | |
1da177e4 LT |
96 | |
97 | /* | |
98 | * debug the h/w constraints | |
99 | */ | |
100 | /* #define HW_CONST_DEBUG */ | |
101 | ||
102 | ||
103 | /* | |
104 | * | |
105 | */ | |
106 | ||
92b9ac78 | 107 | #define MAX_PACKS 20 |
1da177e4 | 108 | #define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */ |
15a24c07 | 109 | #define MAX_URBS 8 |
604cf499 | 110 | #define SYNC_URBS 4 /* always four urbs for sync */ |
4d788e04 | 111 | #define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */ |
1da177e4 | 112 | |
1da177e4 LT |
113 | struct audioformat { |
114 | struct list_head list; | |
115 | snd_pcm_format_t format; /* format type */ | |
116 | unsigned int channels; /* # channels */ | |
117 | unsigned int fmt_type; /* USB audio format type (1-3) */ | |
118 | unsigned int frame_size; /* samples per frame for non-audio */ | |
119 | int iface; /* interface number */ | |
120 | unsigned char altsetting; /* corresponding alternate setting */ | |
121 | unsigned char altset_idx; /* array index of altenate setting */ | |
122 | unsigned char attributes; /* corresponding attributes of cs endpoint */ | |
123 | unsigned char endpoint; /* endpoint */ | |
124 | unsigned char ep_attr; /* endpoint attributes */ | |
744b89e5 | 125 | unsigned char datainterval; /* log_2 of data packet interval */ |
1da177e4 LT |
126 | unsigned int maxpacksize; /* max. packet size */ |
127 | unsigned int rates; /* rate bitmasks */ | |
128 | unsigned int rate_min, rate_max; /* min/max rates */ | |
129 | unsigned int nr_rates; /* number of rate table entries */ | |
130 | unsigned int *rate_table; /* rate table */ | |
131 | }; | |
132 | ||
86e07d34 TI |
133 | struct snd_usb_substream; |
134 | ||
1da177e4 LT |
135 | struct snd_urb_ctx { |
136 | struct urb *urb; | |
55851f73 | 137 | unsigned int buffer_size; /* size of data buffer, if data URB */ |
86e07d34 | 138 | struct snd_usb_substream *subs; |
1da177e4 LT |
139 | int index; /* index for urb array */ |
140 | int packets; /* number of packets per urb */ | |
1da177e4 LT |
141 | }; |
142 | ||
143 | struct snd_urb_ops { | |
86e07d34 TI |
144 | int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); |
145 | int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); | |
146 | int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); | |
147 | int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); | |
1da177e4 LT |
148 | }; |
149 | ||
150 | struct snd_usb_substream { | |
86e07d34 | 151 | struct snd_usb_stream *stream; |
1da177e4 | 152 | struct usb_device *dev; |
86e07d34 | 153 | struct snd_pcm_substream *pcm_substream; |
1da177e4 LT |
154 | int direction; /* playback or capture */ |
155 | int interface; /* current interface */ | |
156 | int endpoint; /* assigned endpoint */ | |
157 | struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */ | |
158 | unsigned int cur_rate; /* current rate (for hw_params callback) */ | |
159 | unsigned int period_bytes; /* current period bytes (for hw_params callback) */ | |
160 | unsigned int format; /* USB data format */ | |
161 | unsigned int datapipe; /* the data i/o pipe */ | |
162 | unsigned int syncpipe; /* 1 - async out or adaptive in */ | |
573567e0 | 163 | unsigned int datainterval; /* log_2 of data packet interval */ |
1da177e4 LT |
164 | unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */ |
165 | unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */ | |
166 | unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */ | |
167 | unsigned int freqmax; /* maximum sampling rate, used for buffer management */ | |
168 | unsigned int phase; /* phase accumulator */ | |
169 | unsigned int maxpacksize; /* max packet size in bytes */ | |
170 | unsigned int maxframesize; /* max packet size in frames */ | |
171 | unsigned int curpacksize; /* current packet size in bytes (for capture) */ | |
172 | unsigned int curframesize; /* current packet size in frames (for capture) */ | |
173 | unsigned int fill_max: 1; /* fill max packet size always */ | |
98e89f60 | 174 | unsigned int txfr_quirk:1; /* allow sub-frame alignment */ |
1da177e4 LT |
175 | unsigned int fmt_type; /* USB audio format type (1-3) */ |
176 | ||
177 | unsigned int running: 1; /* running status */ | |
178 | ||
adc8d313 | 179 | unsigned int hwptr_done; /* processed byte position in the buffer */ |
1da177e4 LT |
180 | unsigned int transfer_done; /* processed frames since last period update */ |
181 | unsigned long active_mask; /* bitmask of active urbs */ | |
182 | unsigned long unlink_mask; /* bitmask of unlinked urbs */ | |
183 | ||
184 | unsigned int nurbs; /* # urbs */ | |
86e07d34 TI |
185 | struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */ |
186 | struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */ | |
55851f73 CL |
187 | char *syncbuf; /* sync buffer for all sync URBs */ |
188 | dma_addr_t sync_dma; /* DMA address of syncbuf */ | |
1da177e4 LT |
189 | |
190 | u64 formats; /* format bitmasks (all or'ed) */ | |
191 | unsigned int num_formats; /* number of supported audio formats (list) */ | |
192 | struct list_head fmt_list; /* format list */ | |
8fec560d | 193 | struct snd_pcm_hw_constraint_list rate_list; /* limited rates */ |
1da177e4 LT |
194 | spinlock_t lock; |
195 | ||
196 | struct snd_urb_ops ops; /* callbacks (must be filled at init) */ | |
197 | }; | |
198 | ||
199 | ||
200 | struct snd_usb_stream { | |
86e07d34 TI |
201 | struct snd_usb_audio *chip; |
202 | struct snd_pcm *pcm; | |
1da177e4 LT |
203 | int pcm_index; |
204 | unsigned int fmt_type; /* USB audio format type (1-3) */ | |
86e07d34 | 205 | struct snd_usb_substream substream[2]; |
1da177e4 LT |
206 | struct list_head list; |
207 | }; | |
208 | ||
209 | ||
210 | /* | |
211 | * we keep the snd_usb_audio_t instances by ourselves for merging | |
212 | * the all interfaces on the same card as one sound device. | |
213 | */ | |
214 | ||
12aa7579 | 215 | static DEFINE_MUTEX(register_mutex); |
86e07d34 | 216 | static struct snd_usb_audio *usb_chip[SNDRV_CARDS]; |
1da177e4 LT |
217 | |
218 | ||
219 | /* | |
220 | * convert a sampling rate into our full speed format (fs/1000 in Q16.16) | |
221 | * this will overflow at approx 524 kHz | |
222 | */ | |
77933d72 | 223 | static inline unsigned get_usb_full_speed_rate(unsigned int rate) |
1da177e4 LT |
224 | { |
225 | return ((rate << 13) + 62) / 125; | |
226 | } | |
227 | ||
228 | /* | |
229 | * convert a sampling rate into USB high speed format (fs/8000 in Q16.16) | |
230 | * this will overflow at approx 4 MHz | |
231 | */ | |
77933d72 | 232 | static inline unsigned get_usb_high_speed_rate(unsigned int rate) |
1da177e4 LT |
233 | { |
234 | return ((rate << 10) + 62) / 125; | |
235 | } | |
236 | ||
237 | /* convert our full speed USB rate into sampling rate in Hz */ | |
77933d72 | 238 | static inline unsigned get_full_speed_hz(unsigned int usb_rate) |
1da177e4 LT |
239 | { |
240 | return (usb_rate * 125 + (1 << 12)) >> 13; | |
241 | } | |
242 | ||
243 | /* convert our high speed USB rate into sampling rate in Hz */ | |
77933d72 | 244 | static inline unsigned get_high_speed_hz(unsigned int usb_rate) |
1da177e4 LT |
245 | { |
246 | return (usb_rate * 125 + (1 << 9)) >> 10; | |
247 | } | |
248 | ||
249 | ||
250 | /* | |
251 | * prepare urb for full speed capture sync pipe | |
252 | * | |
253 | * fill the length and offset of each urb descriptor. | |
254 | * the fixed 10.14 frequency is passed through the pipe. | |
255 | */ | |
86e07d34 TI |
256 | static int prepare_capture_sync_urb(struct snd_usb_substream *subs, |
257 | struct snd_pcm_runtime *runtime, | |
1da177e4 LT |
258 | struct urb *urb) |
259 | { | |
260 | unsigned char *cp = urb->transfer_buffer; | |
88518275 | 261 | struct snd_urb_ctx *ctx = urb->context; |
1da177e4 | 262 | |
1da177e4 | 263 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
ca81090a CL |
264 | urb->iso_frame_desc[0].length = 3; |
265 | urb->iso_frame_desc[0].offset = 0; | |
266 | cp[0] = subs->freqn >> 2; | |
267 | cp[1] = subs->freqn >> 10; | |
268 | cp[2] = subs->freqn >> 18; | |
1da177e4 LT |
269 | return 0; |
270 | } | |
271 | ||
272 | /* | |
273 | * prepare urb for high speed capture sync pipe | |
274 | * | |
275 | * fill the length and offset of each urb descriptor. | |
276 | * the fixed 12.13 frequency is passed as 16.16 through the pipe. | |
277 | */ | |
86e07d34 TI |
278 | static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs, |
279 | struct snd_pcm_runtime *runtime, | |
1da177e4 LT |
280 | struct urb *urb) |
281 | { | |
282 | unsigned char *cp = urb->transfer_buffer; | |
88518275 | 283 | struct snd_urb_ctx *ctx = urb->context; |
1da177e4 | 284 | |
1da177e4 | 285 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
ca81090a CL |
286 | urb->iso_frame_desc[0].length = 4; |
287 | urb->iso_frame_desc[0].offset = 0; | |
288 | cp[0] = subs->freqn; | |
289 | cp[1] = subs->freqn >> 8; | |
290 | cp[2] = subs->freqn >> 16; | |
291 | cp[3] = subs->freqn >> 24; | |
1da177e4 LT |
292 | return 0; |
293 | } | |
294 | ||
295 | /* | |
296 | * process after capture sync complete | |
297 | * - nothing to do | |
298 | */ | |
86e07d34 TI |
299 | static int retire_capture_sync_urb(struct snd_usb_substream *subs, |
300 | struct snd_pcm_runtime *runtime, | |
1da177e4 LT |
301 | struct urb *urb) |
302 | { | |
303 | return 0; | |
304 | } | |
305 | ||
306 | /* | |
307 | * prepare urb for capture data pipe | |
308 | * | |
309 | * fill the offset and length of each descriptor. | |
310 | * | |
311 | * we use a temporary buffer to write the captured data. | |
312 | * since the length of written data is determined by host, we cannot | |
313 | * write onto the pcm buffer directly... the data is thus copied | |
314 | * later at complete callback to the global buffer. | |
315 | */ | |
86e07d34 TI |
316 | static int prepare_capture_urb(struct snd_usb_substream *subs, |
317 | struct snd_pcm_runtime *runtime, | |
1da177e4 LT |
318 | struct urb *urb) |
319 | { | |
320 | int i, offs; | |
88518275 | 321 | struct snd_urb_ctx *ctx = urb->context; |
1da177e4 LT |
322 | |
323 | offs = 0; | |
324 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ | |
1da177e4 LT |
325 | for (i = 0; i < ctx->packets; i++) { |
326 | urb->iso_frame_desc[i].offset = offs; | |
327 | urb->iso_frame_desc[i].length = subs->curpacksize; | |
328 | offs += subs->curpacksize; | |
1da177e4 | 329 | } |
1da177e4 | 330 | urb->transfer_buffer_length = offs; |
b263a9bd | 331 | urb->number_of_packets = ctx->packets; |
1da177e4 LT |
332 | return 0; |
333 | } | |
334 | ||
335 | /* | |
336 | * process after capture complete | |
337 | * | |
338 | * copy the data from each desctiptor to the pcm buffer, and | |
339 | * update the current position. | |
340 | */ | |
86e07d34 TI |
341 | static int retire_capture_urb(struct snd_usb_substream *subs, |
342 | struct snd_pcm_runtime *runtime, | |
1da177e4 LT |
343 | struct urb *urb) |
344 | { | |
345 | unsigned long flags; | |
346 | unsigned char *cp; | |
347 | int i; | |
adc8d313 | 348 | unsigned int stride, frames, bytes, oldptr; |
b263a9bd | 349 | int period_elapsed = 0; |
1da177e4 LT |
350 | |
351 | stride = runtime->frame_bits >> 3; | |
352 | ||
353 | for (i = 0; i < urb->number_of_packets; i++) { | |
354 | cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset; | |
355 | if (urb->iso_frame_desc[i].status) { | |
356 | snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status); | |
357 | // continue; | |
358 | } | |
98e89f60 JG |
359 | bytes = urb->iso_frame_desc[i].actual_length; |
360 | frames = bytes / stride; | |
361 | if (!subs->txfr_quirk) | |
362 | bytes = frames * stride; | |
363 | if (bytes % (runtime->sample_bits >> 3) != 0) { | |
364 | #ifdef CONFIG_SND_DEBUG_VERBOSE | |
365 | int oldbytes = bytes; | |
366 | #endif | |
367 | bytes = frames * stride; | |
368 | snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n", | |
369 | oldbytes, bytes); | |
370 | } | |
1da177e4 LT |
371 | /* update the current pointer */ |
372 | spin_lock_irqsave(&subs->lock, flags); | |
373 | oldptr = subs->hwptr_done; | |
adc8d313 CL |
374 | subs->hwptr_done += bytes; |
375 | if (subs->hwptr_done >= runtime->buffer_size * stride) | |
376 | subs->hwptr_done -= runtime->buffer_size * stride; | |
98e89f60 | 377 | frames = (bytes + (oldptr % stride)) / stride; |
adc8d313 | 378 | subs->transfer_done += frames; |
b263a9bd CL |
379 | if (subs->transfer_done >= runtime->period_size) { |
380 | subs->transfer_done -= runtime->period_size; | |
381 | period_elapsed = 1; | |
382 | } | |
1da177e4 LT |
383 | spin_unlock_irqrestore(&subs->lock, flags); |
384 | /* copy a data chunk */ | |
adc8d313 CL |
385 | if (oldptr + bytes > runtime->buffer_size * stride) { |
386 | unsigned int bytes1 = | |
387 | runtime->buffer_size * stride - oldptr; | |
388 | memcpy(runtime->dma_area + oldptr, cp, bytes1); | |
389 | memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1); | |
1da177e4 | 390 | } else { |
adc8d313 | 391 | memcpy(runtime->dma_area + oldptr, cp, bytes); |
1da177e4 | 392 | } |
1da177e4 | 393 | } |
b263a9bd CL |
394 | if (period_elapsed) |
395 | snd_pcm_period_elapsed(subs->pcm_substream); | |
1da177e4 LT |
396 | return 0; |
397 | } | |
398 | ||
e4f8e656 CL |
399 | /* |
400 | * Process after capture complete when paused. Nothing to do. | |
401 | */ | |
402 | static int retire_paused_capture_urb(struct snd_usb_substream *subs, | |
403 | struct snd_pcm_runtime *runtime, | |
404 | struct urb *urb) | |
405 | { | |
406 | return 0; | |
407 | } | |
408 | ||
1da177e4 LT |
409 | |
410 | /* | |
411 | * prepare urb for full speed playback sync pipe | |
412 | * | |
413 | * set up the offset and length to receive the current frequency. | |
414 | */ | |
415 | ||
86e07d34 TI |
416 | static int prepare_playback_sync_urb(struct snd_usb_substream *subs, |
417 | struct snd_pcm_runtime *runtime, | |
1da177e4 LT |
418 | struct urb *urb) |
419 | { | |
88518275 | 420 | struct snd_urb_ctx *ctx = urb->context; |
1da177e4 | 421 | |
1da177e4 | 422 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
ca81090a CL |
423 | urb->iso_frame_desc[0].length = 3; |
424 | urb->iso_frame_desc[0].offset = 0; | |
1da177e4 LT |
425 | return 0; |
426 | } | |
427 | ||
428 | /* | |
429 | * prepare urb for high speed playback sync pipe | |
430 | * | |
431 | * set up the offset and length to receive the current frequency. | |
432 | */ | |
433 | ||
86e07d34 TI |
434 | static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs, |
435 | struct snd_pcm_runtime *runtime, | |
1da177e4 LT |
436 | struct urb *urb) |
437 | { | |
88518275 | 438 | struct snd_urb_ctx *ctx = urb->context; |
1da177e4 | 439 | |
1da177e4 | 440 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
ca81090a CL |
441 | urb->iso_frame_desc[0].length = 4; |
442 | urb->iso_frame_desc[0].offset = 0; | |
1da177e4 LT |
443 | return 0; |
444 | } | |
445 | ||
446 | /* | |
447 | * process after full speed playback sync complete | |
448 | * | |
449 | * retrieve the current 10.14 frequency from pipe, and set it. | |
450 | * the value is referred in prepare_playback_urb(). | |
451 | */ | |
86e07d34 TI |
452 | static int retire_playback_sync_urb(struct snd_usb_substream *subs, |
453 | struct snd_pcm_runtime *runtime, | |
1da177e4 LT |
454 | struct urb *urb) |
455 | { | |
ca81090a | 456 | unsigned int f; |
1da177e4 LT |
457 | unsigned long flags; |
458 | ||
ca81090a CL |
459 | if (urb->iso_frame_desc[0].status == 0 && |
460 | urb->iso_frame_desc[0].actual_length == 3) { | |
461 | f = combine_triple((u8*)urb->transfer_buffer) << 2; | |
50cdbf15 CL |
462 | if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) { |
463 | spin_lock_irqsave(&subs->lock, flags); | |
464 | subs->freqm = f; | |
465 | spin_unlock_irqrestore(&subs->lock, flags); | |
1da177e4 | 466 | } |
1da177e4 LT |
467 | } |
468 | ||
469 | return 0; | |
470 | } | |
471 | ||
472 | /* | |
473 | * process after high speed playback sync complete | |
474 | * | |
475 | * retrieve the current 12.13 frequency from pipe, and set it. | |
476 | * the value is referred in prepare_playback_urb(). | |
477 | */ | |
86e07d34 TI |
478 | static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs, |
479 | struct snd_pcm_runtime *runtime, | |
1da177e4 LT |
480 | struct urb *urb) |
481 | { | |
ca81090a | 482 | unsigned int f; |
1da177e4 LT |
483 | unsigned long flags; |
484 | ||
ca81090a CL |
485 | if (urb->iso_frame_desc[0].status == 0 && |
486 | urb->iso_frame_desc[0].actual_length == 4) { | |
487 | f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff; | |
50cdbf15 CL |
488 | if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) { |
489 | spin_lock_irqsave(&subs->lock, flags); | |
490 | subs->freqm = f; | |
491 | spin_unlock_irqrestore(&subs->lock, flags); | |
492 | } | |
1da177e4 LT |
493 | } |
494 | ||
495 | return 0; | |
496 | } | |
497 | ||
d513202e | 498 | /* |
97c889a7 | 499 | * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete |
d513202e CL |
500 | * |
501 | * These devices return the number of samples per packet instead of the number | |
502 | * of samples per microframe. | |
503 | */ | |
504 | static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs, | |
505 | struct snd_pcm_runtime *runtime, | |
506 | struct urb *urb) | |
507 | { | |
508 | unsigned int f; | |
509 | unsigned long flags; | |
510 | ||
511 | if (urb->iso_frame_desc[0].status == 0 && | |
512 | urb->iso_frame_desc[0].actual_length == 4) { | |
513 | f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff; | |
514 | f >>= subs->datainterval; | |
515 | if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) { | |
516 | spin_lock_irqsave(&subs->lock, flags); | |
517 | subs->freqm = f; | |
518 | spin_unlock_irqrestore(&subs->lock, flags); | |
519 | } | |
520 | } | |
521 | ||
522 | return 0; | |
523 | } | |
524 | ||
9568f461 CL |
525 | /* determine the number of frames in the next packet */ |
526 | static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs) | |
527 | { | |
528 | if (subs->fill_max) | |
529 | return subs->maxframesize; | |
530 | else { | |
531 | subs->phase = (subs->phase & 0xffff) | |
532 | + (subs->freqm << subs->datainterval); | |
533 | return min(subs->phase >> 16, subs->maxframesize); | |
534 | } | |
535 | } | |
536 | ||
b55bbf06 | 537 | /* |
e4f8e656 | 538 | * Prepare urb for streaming before playback starts or when paused. |
b55bbf06 | 539 | * |
b7eb4a06 | 540 | * We don't have any data, so we send silence. |
b55bbf06 | 541 | */ |
e4f8e656 CL |
542 | static int prepare_nodata_playback_urb(struct snd_usb_substream *subs, |
543 | struct snd_pcm_runtime *runtime, | |
544 | struct urb *urb) | |
b55bbf06 | 545 | { |
4b284928 | 546 | unsigned int i, offs, counts; |
86e07d34 | 547 | struct snd_urb_ctx *ctx = urb->context; |
4b284928 | 548 | int stride = runtime->frame_bits >> 3; |
b55bbf06 | 549 | |
4b284928 | 550 | offs = 0; |
b55bbf06 | 551 | urb->dev = ctx->subs->dev; |
b7eb4a06 | 552 | for (i = 0; i < ctx->packets; ++i) { |
9568f461 | 553 | counts = snd_usb_audio_next_packet_size(subs); |
4b284928 CL |
554 | urb->iso_frame_desc[i].offset = offs * stride; |
555 | urb->iso_frame_desc[i].length = counts * stride; | |
556 | offs += counts; | |
b55bbf06 | 557 | } |
b7eb4a06 | 558 | urb->number_of_packets = ctx->packets; |
4b284928 CL |
559 | urb->transfer_buffer_length = offs * stride; |
560 | memset(urb->transfer_buffer, | |
561 | subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0, | |
562 | offs * stride); | |
b55bbf06 CL |
563 | return 0; |
564 | } | |
565 | ||
1da177e4 LT |
566 | /* |
567 | * prepare urb for playback data pipe | |
568 | * | |
7efd8bc8 CL |
569 | * Since a URB can handle only a single linear buffer, we must use double |
570 | * buffering when the data to be transferred overflows the buffer boundary. | |
571 | * To avoid inconsistencies when updating hwptr_done, we use double buffering | |
572 | * for all URBs. | |
1da177e4 | 573 | */ |
86e07d34 TI |
574 | static int prepare_playback_urb(struct snd_usb_substream *subs, |
575 | struct snd_pcm_runtime *runtime, | |
1da177e4 LT |
576 | struct urb *urb) |
577 | { | |
adc8d313 CL |
578 | int i, stride; |
579 | unsigned int counts, frames, bytes; | |
1da177e4 | 580 | unsigned long flags; |
7efd8bc8 | 581 | int period_elapsed = 0; |
88518275 | 582 | struct snd_urb_ctx *ctx = urb->context; |
1da177e4 LT |
583 | |
584 | stride = runtime->frame_bits >> 3; | |
585 | ||
adc8d313 | 586 | frames = 0; |
1da177e4 LT |
587 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
588 | urb->number_of_packets = 0; | |
589 | spin_lock_irqsave(&subs->lock, flags); | |
590 | for (i = 0; i < ctx->packets; i++) { | |
9568f461 | 591 | counts = snd_usb_audio_next_packet_size(subs); |
1da177e4 | 592 | /* set up descriptor */ |
adc8d313 | 593 | urb->iso_frame_desc[i].offset = frames * stride; |
1da177e4 | 594 | urb->iso_frame_desc[i].length = counts * stride; |
adc8d313 | 595 | frames += counts; |
1da177e4 | 596 | urb->number_of_packets++; |
7efd8bc8 CL |
597 | subs->transfer_done += counts; |
598 | if (subs->transfer_done >= runtime->period_size) { | |
599 | subs->transfer_done -= runtime->period_size; | |
600 | period_elapsed = 1; | |
1da177e4 | 601 | if (subs->fmt_type == USB_FORMAT_TYPE_II) { |
7efd8bc8 CL |
602 | if (subs->transfer_done > 0) { |
603 | /* FIXME: fill-max mode is not | |
604 | * supported yet */ | |
adc8d313 | 605 | frames -= subs->transfer_done; |
7efd8bc8 CL |
606 | counts -= subs->transfer_done; |
607 | urb->iso_frame_desc[i].length = | |
608 | counts * stride; | |
609 | subs->transfer_done = 0; | |
1da177e4 LT |
610 | } |
611 | i++; | |
612 | if (i < ctx->packets) { | |
613 | /* add a transfer delimiter */ | |
7efd8bc8 | 614 | urb->iso_frame_desc[i].offset = |
adc8d313 | 615 | frames * stride; |
1da177e4 LT |
616 | urb->iso_frame_desc[i].length = 0; |
617 | urb->number_of_packets++; | |
618 | } | |
9624ea81 | 619 | break; |
1da177e4 | 620 | } |
1da177e4 | 621 | } |
a7d9c099 | 622 | if (period_elapsed) /* finish at the period boundary */ |
9624ea81 | 623 | break; |
1da177e4 | 624 | } |
adc8d313 CL |
625 | bytes = frames * stride; |
626 | if (subs->hwptr_done + bytes > runtime->buffer_size * stride) { | |
7efd8bc8 | 627 | /* err, the transferred area goes over buffer boundary. */ |
adc8d313 CL |
628 | unsigned int bytes1 = |
629 | runtime->buffer_size * stride - subs->hwptr_done; | |
7efd8bc8 | 630 | memcpy(urb->transfer_buffer, |
adc8d313 CL |
631 | runtime->dma_area + subs->hwptr_done, bytes1); |
632 | memcpy(urb->transfer_buffer + bytes1, | |
633 | runtime->dma_area, bytes - bytes1); | |
1da177e4 | 634 | } else { |
7efd8bc8 | 635 | memcpy(urb->transfer_buffer, |
adc8d313 | 636 | runtime->dma_area + subs->hwptr_done, bytes); |
1da177e4 | 637 | } |
adc8d313 CL |
638 | subs->hwptr_done += bytes; |
639 | if (subs->hwptr_done >= runtime->buffer_size * stride) | |
640 | subs->hwptr_done -= runtime->buffer_size * stride; | |
641 | runtime->delay += frames; | |
1da177e4 | 642 | spin_unlock_irqrestore(&subs->lock, flags); |
adc8d313 | 643 | urb->transfer_buffer_length = bytes; |
b55bbf06 CL |
644 | if (period_elapsed) |
645 | snd_pcm_period_elapsed(subs->pcm_substream); | |
1da177e4 LT |
646 | return 0; |
647 | } | |
648 | ||
649 | /* | |
650 | * process after playback data complete | |
ae1ec5e1 | 651 | * - decrease the delay count again |
1da177e4 | 652 | */ |
86e07d34 TI |
653 | static int retire_playback_urb(struct snd_usb_substream *subs, |
654 | struct snd_pcm_runtime *runtime, | |
1da177e4 LT |
655 | struct urb *urb) |
656 | { | |
ae1ec5e1 TI |
657 | unsigned long flags; |
658 | int stride = runtime->frame_bits >> 3; | |
659 | int processed = urb->transfer_buffer_length / stride; | |
660 | ||
661 | spin_lock_irqsave(&subs->lock, flags); | |
662 | if (processed > runtime->delay) | |
663 | runtime->delay = 0; | |
664 | else | |
665 | runtime->delay -= processed; | |
666 | spin_unlock_irqrestore(&subs->lock, flags); | |
1da177e4 LT |
667 | return 0; |
668 | } | |
669 | ||
670 | ||
671 | /* | |
672 | */ | |
673 | static struct snd_urb_ops audio_urb_ops[2] = { | |
674 | { | |
e4f8e656 | 675 | .prepare = prepare_nodata_playback_urb, |
1da177e4 LT |
676 | .retire = retire_playback_urb, |
677 | .prepare_sync = prepare_playback_sync_urb, | |
678 | .retire_sync = retire_playback_sync_urb, | |
679 | }, | |
680 | { | |
681 | .prepare = prepare_capture_urb, | |
682 | .retire = retire_capture_urb, | |
683 | .prepare_sync = prepare_capture_sync_urb, | |
684 | .retire_sync = retire_capture_sync_urb, | |
685 | }, | |
686 | }; | |
687 | ||
688 | static struct snd_urb_ops audio_urb_ops_high_speed[2] = { | |
689 | { | |
e4f8e656 | 690 | .prepare = prepare_nodata_playback_urb, |
1da177e4 LT |
691 | .retire = retire_playback_urb, |
692 | .prepare_sync = prepare_playback_sync_urb_hs, | |
693 | .retire_sync = retire_playback_sync_urb_hs, | |
694 | }, | |
695 | { | |
696 | .prepare = prepare_capture_urb, | |
697 | .retire = retire_capture_urb, | |
698 | .prepare_sync = prepare_capture_sync_urb_hs, | |
699 | .retire_sync = retire_capture_sync_urb, | |
700 | }, | |
701 | }; | |
702 | ||
703 | /* | |
704 | * complete callback from data urb | |
705 | */ | |
7d12e780 | 706 | static void snd_complete_urb(struct urb *urb) |
1da177e4 | 707 | { |
88518275 | 708 | struct snd_urb_ctx *ctx = urb->context; |
86e07d34 TI |
709 | struct snd_usb_substream *subs = ctx->subs; |
710 | struct snd_pcm_substream *substream = ctx->subs->pcm_substream; | |
1da177e4 LT |
711 | int err = 0; |
712 | ||
713 | if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) || | |
07f51a72 | 714 | !subs->running || /* can be stopped during retire callback */ |
1da177e4 LT |
715 | (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 || |
716 | (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { | |
717 | clear_bit(ctx->index, &subs->active_mask); | |
718 | if (err < 0) { | |
719 | snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err); | |
720 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); | |
721 | } | |
722 | } | |
723 | } | |
724 | ||
725 | ||
726 | /* | |
727 | * complete callback from sync urb | |
728 | */ | |
7d12e780 | 729 | static void snd_complete_sync_urb(struct urb *urb) |
1da177e4 | 730 | { |
88518275 | 731 | struct snd_urb_ctx *ctx = urb->context; |
86e07d34 TI |
732 | struct snd_usb_substream *subs = ctx->subs; |
733 | struct snd_pcm_substream *substream = ctx->subs->pcm_substream; | |
1da177e4 LT |
734 | int err = 0; |
735 | ||
736 | if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) || | |
07f51a72 | 737 | !subs->running || /* can be stopped during retire callback */ |
1da177e4 LT |
738 | (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 || |
739 | (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { | |
740 | clear_bit(ctx->index + 16, &subs->active_mask); | |
741 | if (err < 0) { | |
742 | snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err); | |
743 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); | |
744 | } | |
745 | } | |
746 | } | |
747 | ||
748 | ||
749 | /* | |
750 | * unlink active urbs. | |
751 | */ | |
86e07d34 | 752 | static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep) |
1da177e4 LT |
753 | { |
754 | unsigned int i; | |
755 | int async; | |
756 | ||
757 | subs->running = 0; | |
758 | ||
759 | if (!force && subs->stream->chip->shutdown) /* to be sure... */ | |
760 | return -EBADFD; | |
761 | ||
762 | async = !can_sleep && async_unlink; | |
763 | ||
07f51a72 | 764 | if (!async && in_interrupt()) |
1da177e4 LT |
765 | return 0; |
766 | ||
767 | for (i = 0; i < subs->nurbs; i++) { | |
768 | if (test_bit(i, &subs->active_mask)) { | |
07f51a72 | 769 | if (!test_and_set_bit(i, &subs->unlink_mask)) { |
1da177e4 | 770 | struct urb *u = subs->dataurb[i].urb; |
b375a049 | 771 | if (async) |
1da177e4 | 772 | usb_unlink_urb(u); |
b375a049 | 773 | else |
1da177e4 LT |
774 | usb_kill_urb(u); |
775 | } | |
776 | } | |
777 | } | |
778 | if (subs->syncpipe) { | |
779 | for (i = 0; i < SYNC_URBS; i++) { | |
780 | if (test_bit(i+16, &subs->active_mask)) { | |
07f51a72 | 781 | if (!test_and_set_bit(i+16, &subs->unlink_mask)) { |
1da177e4 | 782 | struct urb *u = subs->syncurb[i].urb; |
b375a049 | 783 | if (async) |
1da177e4 | 784 | usb_unlink_urb(u); |
b375a049 | 785 | else |
1da177e4 LT |
786 | usb_kill_urb(u); |
787 | } | |
788 | } | |
789 | } | |
790 | } | |
791 | return 0; | |
792 | } | |
793 | ||
794 | ||
32e19e88 CL |
795 | static const char *usb_error_string(int err) |
796 | { | |
797 | switch (err) { | |
798 | case -ENODEV: | |
799 | return "no device"; | |
800 | case -ENOENT: | |
801 | return "endpoint not enabled"; | |
802 | case -EPIPE: | |
803 | return "endpoint stalled"; | |
804 | case -ENOSPC: | |
805 | return "not enough bandwidth"; | |
806 | case -ESHUTDOWN: | |
807 | return "device disabled"; | |
808 | case -EHOSTUNREACH: | |
809 | return "device suspended"; | |
810 | case -EINVAL: | |
811 | case -EAGAIN: | |
812 | case -EFBIG: | |
813 | case -EMSGSIZE: | |
814 | return "internal error"; | |
815 | default: | |
816 | return "unknown error"; | |
817 | } | |
818 | } | |
819 | ||
1da177e4 LT |
820 | /* |
821 | * set up and start data/sync urbs | |
822 | */ | |
86e07d34 | 823 | static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime) |
1da177e4 LT |
824 | { |
825 | unsigned int i; | |
826 | int err; | |
827 | ||
828 | if (subs->stream->chip->shutdown) | |
829 | return -EBADFD; | |
830 | ||
831 | for (i = 0; i < subs->nurbs; i++) { | |
5e246b85 TI |
832 | if (snd_BUG_ON(!subs->dataurb[i].urb)) |
833 | return -EINVAL; | |
1da177e4 LT |
834 | if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) { |
835 | snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i); | |
836 | goto __error; | |
837 | } | |
838 | } | |
839 | if (subs->syncpipe) { | |
840 | for (i = 0; i < SYNC_URBS; i++) { | |
5e246b85 TI |
841 | if (snd_BUG_ON(!subs->syncurb[i].urb)) |
842 | return -EINVAL; | |
1da177e4 LT |
843 | if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) { |
844 | snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i); | |
845 | goto __error; | |
846 | } | |
847 | } | |
848 | } | |
849 | ||
850 | subs->active_mask = 0; | |
851 | subs->unlink_mask = 0; | |
852 | subs->running = 1; | |
853 | for (i = 0; i < subs->nurbs; i++) { | |
32e19e88 CL |
854 | err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC); |
855 | if (err < 0) { | |
856 | snd_printk(KERN_ERR "cannot submit datapipe " | |
857 | "for urb %d, error %d: %s\n", | |
858 | i, err, usb_error_string(err)); | |
1da177e4 LT |
859 | goto __error; |
860 | } | |
861 | set_bit(i, &subs->active_mask); | |
862 | } | |
863 | if (subs->syncpipe) { | |
864 | for (i = 0; i < SYNC_URBS; i++) { | |
32e19e88 CL |
865 | err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC); |
866 | if (err < 0) { | |
867 | snd_printk(KERN_ERR "cannot submit syncpipe " | |
868 | "for urb %d, error %d: %s\n", | |
869 | i, err, usb_error_string(err)); | |
1da177e4 LT |
870 | goto __error; |
871 | } | |
872 | set_bit(i + 16, &subs->active_mask); | |
873 | } | |
874 | } | |
875 | return 0; | |
876 | ||
877 | __error: | |
878 | // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN); | |
879 | deactivate_urbs(subs, 0, 0); | |
880 | return -EPIPE; | |
881 | } | |
882 | ||
883 | ||
884 | /* | |
885 | * wait until all urbs are processed. | |
886 | */ | |
86e07d34 | 887 | static int wait_clear_urbs(struct snd_usb_substream *subs) |
1da177e4 | 888 | { |
b27c187f | 889 | unsigned long end_time = jiffies + msecs_to_jiffies(1000); |
1da177e4 LT |
890 | unsigned int i; |
891 | int alive; | |
892 | ||
893 | do { | |
894 | alive = 0; | |
895 | for (i = 0; i < subs->nurbs; i++) { | |
896 | if (test_bit(i, &subs->active_mask)) | |
897 | alive++; | |
898 | } | |
899 | if (subs->syncpipe) { | |
900 | for (i = 0; i < SYNC_URBS; i++) { | |
901 | if (test_bit(i + 16, &subs->active_mask)) | |
902 | alive++; | |
903 | } | |
904 | } | |
905 | if (! alive) | |
906 | break; | |
8433a509 | 907 | schedule_timeout_uninterruptible(1); |
b27c187f | 908 | } while (time_before(jiffies, end_time)); |
1da177e4 LT |
909 | if (alive) |
910 | snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive); | |
911 | return 0; | |
912 | } | |
913 | ||
914 | ||
915 | /* | |
adc8d313 | 916 | * return the current pcm pointer. just based on the hwptr_done value. |
1da177e4 | 917 | */ |
86e07d34 | 918 | static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream) |
1da177e4 | 919 | { |
86e07d34 | 920 | struct snd_usb_substream *subs; |
adc8d313 | 921 | unsigned int hwptr_done; |
daa150ef | 922 | |
86e07d34 | 923 | subs = (struct snd_usb_substream *)substream->runtime->private_data; |
daa150ef CL |
924 | spin_lock(&subs->lock); |
925 | hwptr_done = subs->hwptr_done; | |
926 | spin_unlock(&subs->lock); | |
adc8d313 | 927 | return hwptr_done / (substream->runtime->frame_bits >> 3); |
1da177e4 LT |
928 | } |
929 | ||
930 | ||
931 | /* | |
b55bbf06 | 932 | * start/stop playback substream |
1da177e4 | 933 | */ |
86e07d34 | 934 | static int snd_usb_pcm_playback_trigger(struct snd_pcm_substream *substream, |
b55bbf06 | 935 | int cmd) |
1da177e4 | 936 | { |
86e07d34 | 937 | struct snd_usb_substream *subs = substream->runtime->private_data; |
b55bbf06 CL |
938 | |
939 | switch (cmd) { | |
940 | case SNDRV_PCM_TRIGGER_START: | |
e4f8e656 | 941 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
b55bbf06 CL |
942 | subs->ops.prepare = prepare_playback_urb; |
943 | return 0; | |
944 | case SNDRV_PCM_TRIGGER_STOP: | |
945 | return deactivate_urbs(subs, 0, 0); | |
e4f8e656 CL |
946 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
947 | subs->ops.prepare = prepare_nodata_playback_urb; | |
948 | return 0; | |
b55bbf06 CL |
949 | default: |
950 | return -EINVAL; | |
951 | } | |
952 | } | |
953 | ||
954 | /* | |
955 | * start/stop capture substream | |
956 | */ | |
86e07d34 | 957 | static int snd_usb_pcm_capture_trigger(struct snd_pcm_substream *substream, |
b55bbf06 CL |
958 | int cmd) |
959 | { | |
86e07d34 | 960 | struct snd_usb_substream *subs = substream->runtime->private_data; |
1da177e4 LT |
961 | |
962 | switch (cmd) { | |
963 | case SNDRV_PCM_TRIGGER_START: | |
e4f8e656 | 964 | subs->ops.retire = retire_capture_urb; |
b55bbf06 | 965 | return start_urbs(subs, substream->runtime); |
1da177e4 | 966 | case SNDRV_PCM_TRIGGER_STOP: |
b55bbf06 | 967 | return deactivate_urbs(subs, 0, 0); |
e4f8e656 CL |
968 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
969 | subs->ops.retire = retire_paused_capture_urb; | |
970 | return 0; | |
971 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
972 | subs->ops.retire = retire_capture_urb; | |
973 | return 0; | |
1da177e4 | 974 | default: |
b55bbf06 | 975 | return -EINVAL; |
1da177e4 | 976 | } |
1da177e4 LT |
977 | } |
978 | ||
979 | ||
980 | /* | |
981 | * release a urb data | |
982 | */ | |
86e07d34 | 983 | static void release_urb_ctx(struct snd_urb_ctx *u) |
1da177e4 LT |
984 | { |
985 | if (u->urb) { | |
55851f73 CL |
986 | if (u->buffer_size) |
987 | usb_buffer_free(u->subs->dev, u->buffer_size, | |
988 | u->urb->transfer_buffer, | |
989 | u->urb->transfer_dma); | |
1da177e4 LT |
990 | usb_free_urb(u->urb); |
991 | u->urb = NULL; | |
992 | } | |
1da177e4 LT |
993 | } |
994 | ||
995 | /* | |
996 | * release a substream | |
997 | */ | |
86e07d34 | 998 | static void release_substream_urbs(struct snd_usb_substream *subs, int force) |
1da177e4 LT |
999 | { |
1000 | int i; | |
1001 | ||
1002 | /* stop urbs (to be sure) */ | |
1003 | deactivate_urbs(subs, force, 1); | |
1004 | wait_clear_urbs(subs); | |
1005 | ||
1006 | for (i = 0; i < MAX_URBS; i++) | |
1007 | release_urb_ctx(&subs->dataurb[i]); | |
1008 | for (i = 0; i < SYNC_URBS; i++) | |
1009 | release_urb_ctx(&subs->syncurb[i]); | |
55851f73 CL |
1010 | usb_buffer_free(subs->dev, SYNC_URBS * 4, |
1011 | subs->syncbuf, subs->sync_dma); | |
1012 | subs->syncbuf = NULL; | |
1da177e4 LT |
1013 | subs->nurbs = 0; |
1014 | } | |
1015 | ||
1016 | /* | |
1017 | * initialize a substream for plaback/capture | |
1018 | */ | |
86e07d34 | 1019 | static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes, |
1da177e4 LT |
1020 | unsigned int rate, unsigned int frame_bits) |
1021 | { | |
160389c8 | 1022 | unsigned int maxsize, i; |
1da177e4 | 1023 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; |
160389c8 | 1024 | unsigned int urb_packs, total_packs, packs_per_ms; |
1da177e4 LT |
1025 | |
1026 | /* calculate the frequency in 16.16 format */ | |
1027 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) | |
1028 | subs->freqn = get_usb_full_speed_rate(rate); | |
1029 | else | |
1030 | subs->freqn = get_usb_high_speed_rate(rate); | |
1031 | subs->freqm = subs->freqn; | |
573567e0 CL |
1032 | /* calculate max. frequency */ |
1033 | if (subs->maxpacksize) { | |
1034 | /* whatever fits into a max. size packet */ | |
1da177e4 | 1035 | maxsize = subs->maxpacksize; |
573567e0 CL |
1036 | subs->freqmax = (maxsize / (frame_bits >> 3)) |
1037 | << (16 - subs->datainterval); | |
1038 | } else { | |
1039 | /* no max. packet size: just take 25% higher than nominal */ | |
1040 | subs->freqmax = subs->freqn + (subs->freqn >> 2); | |
1041 | maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3)) | |
1042 | >> (16 - subs->datainterval); | |
1da177e4 | 1043 | } |
573567e0 | 1044 | subs->phase = 0; |
1da177e4 LT |
1045 | |
1046 | if (subs->fill_max) | |
1047 | subs->curpacksize = subs->maxpacksize; | |
1048 | else | |
1049 | subs->curpacksize = maxsize; | |
1050 | ||
a93bf990 CL |
1051 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) |
1052 | packs_per_ms = 8 >> subs->datainterval; | |
1053 | else | |
1054 | packs_per_ms = 1; | |
1055 | ||
71d848ca | 1056 | if (is_playback) { |
f3990e61 | 1057 | urb_packs = max(nrpacks, 1); |
71d848ca CL |
1058 | urb_packs = min(urb_packs, (unsigned int)MAX_PACKS); |
1059 | } else | |
15a24c07 | 1060 | urb_packs = 1; |
a93bf990 | 1061 | urb_packs *= packs_per_ms; |
765e8db0 | 1062 | if (subs->syncpipe) |
f1e6d3c5 | 1063 | urb_packs = min(urb_packs, 1U << subs->syncinterval); |
1da177e4 | 1064 | |
1da177e4 | 1065 | /* decide how many packets to be used */ |
15a24c07 | 1066 | if (is_playback) { |
4d788e04 | 1067 | unsigned int minsize, maxpacks; |
d6db392e CL |
1068 | /* determine how small a packet can be */ |
1069 | minsize = (subs->freqn >> (16 - subs->datainterval)) | |
1070 | * (frame_bits >> 3); | |
7efd8bc8 | 1071 | /* with sync from device, assume it can be 12% lower */ |
d6db392e | 1072 | if (subs->syncpipe) |
7efd8bc8 | 1073 | minsize -= minsize >> 3; |
d6db392e CL |
1074 | minsize = max(minsize, 1u); |
1075 | total_packs = (period_bytes + minsize - 1) / minsize; | |
a93bf990 | 1076 | /* we need at least two URBs for queueing */ |
a7d9c099 CL |
1077 | if (total_packs < 2) { |
1078 | total_packs = 2; | |
f3990e61 | 1079 | } else { |
4d788e04 | 1080 | /* and we don't want too long a queue either */ |
b1c86bb8 CL |
1081 | maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2); |
1082 | total_packs = min(total_packs, maxpacks); | |
4d788e04 | 1083 | } |
15a24c07 | 1084 | } else { |
a7d9c099 CL |
1085 | while (urb_packs > 1 && urb_packs * maxsize >= period_bytes) |
1086 | urb_packs >>= 1; | |
15a24c07 CL |
1087 | total_packs = MAX_URBS * urb_packs; |
1088 | } | |
1da177e4 LT |
1089 | subs->nurbs = (total_packs + urb_packs - 1) / urb_packs; |
1090 | if (subs->nurbs > MAX_URBS) { | |
1091 | /* too much... */ | |
1092 | subs->nurbs = MAX_URBS; | |
1093 | total_packs = MAX_URBS * urb_packs; | |
160389c8 | 1094 | } else if (subs->nurbs < 2) { |
1da177e4 LT |
1095 | /* too little - we need at least two packets |
1096 | * to ensure contiguous playback/capture | |
1097 | */ | |
1098 | subs->nurbs = 2; | |
1da177e4 LT |
1099 | } |
1100 | ||
1101 | /* allocate and initialize data urbs */ | |
1102 | for (i = 0; i < subs->nurbs; i++) { | |
86e07d34 | 1103 | struct snd_urb_ctx *u = &subs->dataurb[i]; |
1da177e4 LT |
1104 | u->index = i; |
1105 | u->subs = subs; | |
160389c8 CL |
1106 | u->packets = (i + 1) * total_packs / subs->nurbs |
1107 | - i * total_packs / subs->nurbs; | |
55851f73 | 1108 | u->buffer_size = maxsize * u->packets; |
1da177e4 LT |
1109 | if (subs->fmt_type == USB_FORMAT_TYPE_II) |
1110 | u->packets++; /* for transfer delimiter */ | |
1da177e4 | 1111 | u->urb = usb_alloc_urb(u->packets, GFP_KERNEL); |
07f51a72 | 1112 | if (!u->urb) |
55851f73 CL |
1113 | goto out_of_memory; |
1114 | u->urb->transfer_buffer = | |
1115 | usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL, | |
1116 | &u->urb->transfer_dma); | |
07f51a72 | 1117 | if (!u->urb->transfer_buffer) |
55851f73 | 1118 | goto out_of_memory; |
1da177e4 | 1119 | u->urb->pipe = subs->datapipe; |
55851f73 | 1120 | u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
573567e0 | 1121 | u->urb->interval = 1 << subs->datainterval; |
1da177e4 | 1122 | u->urb->context = u; |
3527a008 | 1123 | u->urb->complete = snd_complete_urb; |
1da177e4 LT |
1124 | } |
1125 | ||
1126 | if (subs->syncpipe) { | |
1127 | /* allocate and initialize sync urbs */ | |
55851f73 CL |
1128 | subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4, |
1129 | GFP_KERNEL, &subs->sync_dma); | |
07f51a72 | 1130 | if (!subs->syncbuf) |
55851f73 | 1131 | goto out_of_memory; |
1da177e4 | 1132 | for (i = 0; i < SYNC_URBS; i++) { |
86e07d34 | 1133 | struct snd_urb_ctx *u = &subs->syncurb[i]; |
1da177e4 LT |
1134 | u->index = i; |
1135 | u->subs = subs; | |
ca81090a CL |
1136 | u->packets = 1; |
1137 | u->urb = usb_alloc_urb(1, GFP_KERNEL); | |
07f51a72 | 1138 | if (!u->urb) |
55851f73 | 1139 | goto out_of_memory; |
ca81090a | 1140 | u->urb->transfer_buffer = subs->syncbuf + i * 4; |
55851f73 | 1141 | u->urb->transfer_dma = subs->sync_dma + i * 4; |
ca81090a | 1142 | u->urb->transfer_buffer_length = 4; |
1da177e4 | 1143 | u->urb->pipe = subs->syncpipe; |
55851f73 CL |
1144 | u->urb->transfer_flags = URB_ISO_ASAP | |
1145 | URB_NO_TRANSFER_DMA_MAP; | |
ca81090a | 1146 | u->urb->number_of_packets = 1; |
1149a64f | 1147 | u->urb->interval = 1 << subs->syncinterval; |
1da177e4 | 1148 | u->urb->context = u; |
3527a008 | 1149 | u->urb->complete = snd_complete_sync_urb; |
1da177e4 LT |
1150 | } |
1151 | } | |
1152 | return 0; | |
55851f73 CL |
1153 | |
1154 | out_of_memory: | |
1155 | release_substream_urbs(subs, 0); | |
1156 | return -ENOMEM; | |
1da177e4 LT |
1157 | } |
1158 | ||
1159 | ||
1160 | /* | |
1161 | * find a matching audio format | |
1162 | */ | |
86e07d34 | 1163 | static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format, |
1da177e4 LT |
1164 | unsigned int rate, unsigned int channels) |
1165 | { | |
1166 | struct list_head *p; | |
1167 | struct audioformat *found = NULL; | |
1168 | int cur_attr = 0, attr; | |
1169 | ||
1170 | list_for_each(p, &subs->fmt_list) { | |
1171 | struct audioformat *fp; | |
1172 | fp = list_entry(p, struct audioformat, list); | |
1173 | if (fp->format != format || fp->channels != channels) | |
1174 | continue; | |
1175 | if (rate < fp->rate_min || rate > fp->rate_max) | |
1176 | continue; | |
1177 | if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) { | |
1178 | unsigned int i; | |
1179 | for (i = 0; i < fp->nr_rates; i++) | |
1180 | if (fp->rate_table[i] == rate) | |
1181 | break; | |
1182 | if (i >= fp->nr_rates) | |
1183 | continue; | |
1184 | } | |
1185 | attr = fp->ep_attr & EP_ATTR_MASK; | |
1186 | if (! found) { | |
1187 | found = fp; | |
1188 | cur_attr = attr; | |
1189 | continue; | |
1190 | } | |
1191 | /* avoid async out and adaptive in if the other method | |
1192 | * supports the same format. | |
1193 | * this is a workaround for the case like | |
1194 | * M-audio audiophile USB. | |
1195 | */ | |
1196 | if (attr != cur_attr) { | |
1197 | if ((attr == EP_ATTR_ASYNC && | |
1198 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || | |
1199 | (attr == EP_ATTR_ADAPTIVE && | |
1200 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) | |
1201 | continue; | |
1202 | if ((cur_attr == EP_ATTR_ASYNC && | |
1203 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || | |
1204 | (cur_attr == EP_ATTR_ADAPTIVE && | |
1205 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) { | |
1206 | found = fp; | |
1207 | cur_attr = attr; | |
1208 | continue; | |
1209 | } | |
1210 | } | |
1211 | /* find the format with the largest max. packet size */ | |
1212 | if (fp->maxpacksize > found->maxpacksize) { | |
1213 | found = fp; | |
1214 | cur_attr = attr; | |
1215 | } | |
1216 | } | |
1217 | return found; | |
1218 | } | |
1219 | ||
1220 | ||
1221 | /* | |
1222 | * initialize the picth control and sample rate | |
1223 | */ | |
1224 | static int init_usb_pitch(struct usb_device *dev, int iface, | |
1225 | struct usb_host_interface *alts, | |
1226 | struct audioformat *fmt) | |
1227 | { | |
1228 | unsigned int ep; | |
1229 | unsigned char data[1]; | |
1230 | int err; | |
1231 | ||
1232 | ep = get_endpoint(alts, 0)->bEndpointAddress; | |
1233 | /* if endpoint has pitch control, enable it */ | |
1234 | if (fmt->attributes & EP_CS_ATTR_PITCH_CONTROL) { | |
1235 | data[0] = 1; | |
1236 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, | |
1237 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, | |
1238 | PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) { | |
1239 | snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n", | |
1240 | dev->devnum, iface, ep); | |
1241 | return err; | |
1242 | } | |
1243 | } | |
1244 | return 0; | |
1245 | } | |
1246 | ||
1247 | static int init_usb_sample_rate(struct usb_device *dev, int iface, | |
1248 | struct usb_host_interface *alts, | |
1249 | struct audioformat *fmt, int rate) | |
1250 | { | |
1251 | unsigned int ep; | |
1252 | unsigned char data[3]; | |
1253 | int err; | |
1254 | ||
1255 | ep = get_endpoint(alts, 0)->bEndpointAddress; | |
1256 | /* if endpoint has sampling rate control, set it */ | |
1257 | if (fmt->attributes & EP_CS_ATTR_SAMPLE_RATE) { | |
1258 | int crate; | |
1259 | data[0] = rate; | |
1260 | data[1] = rate >> 8; | |
1261 | data[2] = rate >> 16; | |
1262 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, | |
1263 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, | |
1264 | SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) { | |
b9d710b3 | 1265 | snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n", |
1da177e4 LT |
1266 | dev->devnum, iface, fmt->altsetting, rate, ep); |
1267 | return err; | |
1268 | } | |
1269 | if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, | |
1270 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN, | |
1271 | SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) { | |
b9d710b3 | 1272 | snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n", |
1da177e4 LT |
1273 | dev->devnum, iface, fmt->altsetting, ep); |
1274 | return 0; /* some devices don't support reading */ | |
1275 | } | |
1276 | crate = data[0] | (data[1] << 8) | (data[2] << 16); | |
1277 | if (crate != rate) { | |
1278 | snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate); | |
1279 | // runtime->rate = crate; | |
1280 | } | |
1281 | } | |
1282 | return 0; | |
1283 | } | |
1284 | ||
7d2b451e SK |
1285 | /* |
1286 | * For E-Mu 0404USB/0202USB/TrackerPre sample rate should be set for device, | |
1287 | * not for interface. | |
1288 | */ | |
1289 | static void set_format_emu_quirk(struct snd_usb_substream *subs, | |
1290 | struct audioformat *fmt) | |
1291 | { | |
1292 | unsigned char emu_samplerate_id = 0; | |
1293 | ||
1294 | /* When capture is active | |
1295 | * sample rate shouldn't be changed | |
1296 | * by playback substream | |
1297 | */ | |
1298 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) { | |
1299 | if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1) | |
1300 | return; | |
1301 | } | |
1302 | ||
1303 | switch (fmt->rate_min) { | |
1304 | case 48000: | |
1305 | emu_samplerate_id = EMU_QUIRK_SR_48000HZ; | |
1306 | break; | |
1307 | case 88200: | |
1308 | emu_samplerate_id = EMU_QUIRK_SR_88200HZ; | |
1309 | break; | |
1310 | case 96000: | |
1311 | emu_samplerate_id = EMU_QUIRK_SR_96000HZ; | |
1312 | break; | |
1313 | case 176400: | |
1314 | emu_samplerate_id = EMU_QUIRK_SR_176400HZ; | |
1315 | break; | |
1316 | case 192000: | |
1317 | emu_samplerate_id = EMU_QUIRK_SR_192000HZ; | |
1318 | break; | |
1319 | default: | |
1320 | emu_samplerate_id = EMU_QUIRK_SR_44100HZ; | |
1321 | break; | |
1322 | } | |
1323 | snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id); | |
1324 | } | |
1325 | ||
1da177e4 LT |
1326 | /* |
1327 | * find a matching format and set up the interface | |
1328 | */ | |
86e07d34 | 1329 | static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt) |
1da177e4 LT |
1330 | { |
1331 | struct usb_device *dev = subs->dev; | |
1332 | struct usb_host_interface *alts; | |
1333 | struct usb_interface_descriptor *altsd; | |
1334 | struct usb_interface *iface; | |
1335 | unsigned int ep, attr; | |
1336 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; | |
1337 | int err; | |
1338 | ||
1339 | iface = usb_ifnum_to_if(dev, fmt->iface); | |
5e246b85 TI |
1340 | if (WARN_ON(!iface)) |
1341 | return -EINVAL; | |
1da177e4 LT |
1342 | alts = &iface->altsetting[fmt->altset_idx]; |
1343 | altsd = get_iface_desc(alts); | |
5e246b85 TI |
1344 | if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting)) |
1345 | return -EINVAL; | |
1da177e4 LT |
1346 | |
1347 | if (fmt == subs->cur_audiofmt) | |
1348 | return 0; | |
1349 | ||
1350 | /* close the old interface */ | |
1351 | if (subs->interface >= 0 && subs->interface != fmt->iface) { | |
5149fe2c ON |
1352 | if (usb_set_interface(subs->dev, subs->interface, 0) < 0) { |
1353 | snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n", | |
1354 | dev->devnum, fmt->iface, fmt->altsetting); | |
1355 | return -EIO; | |
1356 | } | |
1da177e4 LT |
1357 | subs->interface = -1; |
1358 | subs->format = 0; | |
1359 | } | |
1360 | ||
1361 | /* set interface */ | |
1362 | if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) { | |
1363 | if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) { | |
1364 | snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n", | |
1365 | dev->devnum, fmt->iface, fmt->altsetting); | |
1366 | return -EIO; | |
1367 | } | |
1368 | snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting); | |
1369 | subs->interface = fmt->iface; | |
1370 | subs->format = fmt->altset_idx; | |
1371 | } | |
1372 | ||
1373 | /* create a data pipe */ | |
1374 | ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK; | |
1375 | if (is_playback) | |
1376 | subs->datapipe = usb_sndisocpipe(dev, ep); | |
1377 | else | |
1378 | subs->datapipe = usb_rcvisocpipe(dev, ep); | |
744b89e5 | 1379 | subs->datainterval = fmt->datainterval; |
1da177e4 LT |
1380 | subs->syncpipe = subs->syncinterval = 0; |
1381 | subs->maxpacksize = fmt->maxpacksize; | |
1382 | subs->fill_max = 0; | |
1383 | ||
1384 | /* we need a sync pipe in async OUT or adaptive IN mode */ | |
1385 | /* check the number of EP, since some devices have broken | |
1386 | * descriptors which fool us. if it has only one EP, | |
1387 | * assume it as adaptive-out or sync-in. | |
1388 | */ | |
1389 | attr = fmt->ep_attr & EP_ATTR_MASK; | |
1390 | if (((is_playback && attr == EP_ATTR_ASYNC) || | |
1391 | (! is_playback && attr == EP_ATTR_ADAPTIVE)) && | |
1392 | altsd->bNumEndpoints >= 2) { | |
1393 | /* check sync-pipe endpoint */ | |
1394 | /* ... and check descriptor size before accessing bSynchAddress | |
1395 | because there is a version of the SB Audigy 2 NX firmware lacking | |
1396 | the audio fields in the endpoint descriptors */ | |
1397 | if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 || | |
1398 | (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | |
1399 | get_endpoint(alts, 1)->bSynchAddress != 0)) { | |
1400 | snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n", | |
1401 | dev->devnum, fmt->iface, fmt->altsetting); | |
1402 | return -EINVAL; | |
1403 | } | |
1404 | ep = get_endpoint(alts, 1)->bEndpointAddress; | |
1405 | if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | |
1406 | (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) || | |
1407 | (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) { | |
1408 | snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n", | |
1409 | dev->devnum, fmt->iface, fmt->altsetting); | |
1410 | return -EINVAL; | |
1411 | } | |
1412 | ep &= USB_ENDPOINT_NUMBER_MASK; | |
1413 | if (is_playback) | |
1414 | subs->syncpipe = usb_rcvisocpipe(dev, ep); | |
1415 | else | |
1416 | subs->syncpipe = usb_sndisocpipe(dev, ep); | |
1149a64f CL |
1417 | if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
1418 | get_endpoint(alts, 1)->bRefresh >= 1 && | |
1419 | get_endpoint(alts, 1)->bRefresh <= 9) | |
1420 | subs->syncinterval = get_endpoint(alts, 1)->bRefresh; | |
604cf499 | 1421 | else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) |
1149a64f | 1422 | subs->syncinterval = 1; |
604cf499 CL |
1423 | else if (get_endpoint(alts, 1)->bInterval >= 1 && |
1424 | get_endpoint(alts, 1)->bInterval <= 16) | |
1425 | subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1; | |
1426 | else | |
1427 | subs->syncinterval = 3; | |
1da177e4 LT |
1428 | } |
1429 | ||
1430 | /* always fill max packet size */ | |
1431 | if (fmt->attributes & EP_CS_ATTR_FILL_MAX) | |
1432 | subs->fill_max = 1; | |
1433 | ||
1434 | if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0) | |
1435 | return err; | |
1436 | ||
1437 | subs->cur_audiofmt = fmt; | |
1438 | ||
7d2b451e SK |
1439 | switch (subs->stream->chip->usb_id) { |
1440 | case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */ | |
1441 | case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */ | |
1442 | case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */ | |
1443 | set_format_emu_quirk(subs, fmt); | |
1444 | break; | |
1445 | } | |
1446 | ||
1da177e4 | 1447 | #if 0 |
54530bde TI |
1448 | printk(KERN_DEBUG |
1449 | "setting done: format = %d, rate = %d..%d, channels = %d\n", | |
2a56f51b | 1450 | fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels); |
54530bde TI |
1451 | printk(KERN_DEBUG |
1452 | " datapipe = 0x%0x, syncpipe = 0x%0x\n", | |
1da177e4 LT |
1453 | subs->datapipe, subs->syncpipe); |
1454 | #endif | |
1455 | ||
1456 | return 0; | |
1457 | } | |
1458 | ||
1459 | /* | |
1460 | * hw_params callback | |
1461 | * | |
1462 | * allocate a buffer and set the given audio format. | |
1463 | * | |
1464 | * so far we use a physically linear buffer although packetize transfer | |
1465 | * doesn't need a continuous area. | |
1466 | * if sg buffer is supported on the later version of alsa, we'll follow | |
1467 | * that. | |
1468 | */ | |
86e07d34 TI |
1469 | static int snd_usb_hw_params(struct snd_pcm_substream *substream, |
1470 | struct snd_pcm_hw_params *hw_params) | |
1da177e4 | 1471 | { |
88518275 | 1472 | struct snd_usb_substream *subs = substream->runtime->private_data; |
1da177e4 LT |
1473 | struct audioformat *fmt; |
1474 | unsigned int channels, rate, format; | |
1475 | int ret, changed; | |
1476 | ||
c55675e3 CL |
1477 | ret = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
1478 | params_buffer_bytes(hw_params)); | |
1da177e4 LT |
1479 | if (ret < 0) |
1480 | return ret; | |
1481 | ||
1482 | format = params_format(hw_params); | |
1483 | rate = params_rate(hw_params); | |
1484 | channels = params_channels(hw_params); | |
1485 | fmt = find_format(subs, format, rate, channels); | |
07f51a72 | 1486 | if (!fmt) { |
b9d710b3 | 1487 | snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n", |
21a3479a | 1488 | format, rate, channels); |
1da177e4 LT |
1489 | return -EINVAL; |
1490 | } | |
1491 | ||
1492 | changed = subs->cur_audiofmt != fmt || | |
1493 | subs->period_bytes != params_period_bytes(hw_params) || | |
1494 | subs->cur_rate != rate; | |
1495 | if ((ret = set_format(subs, fmt)) < 0) | |
1496 | return ret; | |
1497 | ||
1498 | if (subs->cur_rate != rate) { | |
1499 | struct usb_host_interface *alts; | |
1500 | struct usb_interface *iface; | |
1501 | iface = usb_ifnum_to_if(subs->dev, fmt->iface); | |
1502 | alts = &iface->altsetting[fmt->altset_idx]; | |
1503 | ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate); | |
1504 | if (ret < 0) | |
1505 | return ret; | |
1506 | subs->cur_rate = rate; | |
1507 | } | |
1508 | ||
1509 | if (changed) { | |
1510 | /* format changed */ | |
1511 | release_substream_urbs(subs, 0); | |
1512 | /* influenced: period_bytes, channels, rate, format, */ | |
1513 | ret = init_substream_urbs(subs, params_period_bytes(hw_params), | |
1514 | params_rate(hw_params), | |
1515 | snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params)); | |
1516 | } | |
1517 | ||
1518 | return ret; | |
1519 | } | |
1520 | ||
1521 | /* | |
1522 | * hw_free callback | |
1523 | * | |
1524 | * reset the audio format and release the buffer | |
1525 | */ | |
86e07d34 | 1526 | static int snd_usb_hw_free(struct snd_pcm_substream *substream) |
1da177e4 | 1527 | { |
88518275 | 1528 | struct snd_usb_substream *subs = substream->runtime->private_data; |
1da177e4 LT |
1529 | |
1530 | subs->cur_audiofmt = NULL; | |
1531 | subs->cur_rate = 0; | |
1532 | subs->period_bytes = 0; | |
de1b8b93 TI |
1533 | if (!subs->stream->chip->shutdown) |
1534 | release_substream_urbs(subs, 0); | |
c55675e3 | 1535 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
1da177e4 LT |
1536 | } |
1537 | ||
1538 | /* | |
1539 | * prepare callback | |
1540 | * | |
1541 | * only a few subtle things... | |
1542 | */ | |
86e07d34 | 1543 | static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) |
1da177e4 | 1544 | { |
86e07d34 TI |
1545 | struct snd_pcm_runtime *runtime = substream->runtime; |
1546 | struct snd_usb_substream *subs = runtime->private_data; | |
1da177e4 LT |
1547 | |
1548 | if (! subs->cur_audiofmt) { | |
1549 | snd_printk(KERN_ERR "usbaudio: no format is specified!\n"); | |
1550 | return -ENXIO; | |
1551 | } | |
1552 | ||
1553 | /* some unit conversions in runtime */ | |
1554 | subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize); | |
1555 | subs->curframesize = bytes_to_frames(runtime, subs->curpacksize); | |
1556 | ||
1557 | /* reset the pointer */ | |
1da177e4 | 1558 | subs->hwptr_done = 0; |
1da177e4 LT |
1559 | subs->transfer_done = 0; |
1560 | subs->phase = 0; | |
ae1ec5e1 | 1561 | runtime->delay = 0; |
1da177e4 LT |
1562 | |
1563 | /* clear urbs (to be sure) */ | |
1564 | deactivate_urbs(subs, 0, 1); | |
1565 | wait_clear_urbs(subs); | |
1566 | ||
b55bbf06 CL |
1567 | /* for playback, submit the URBs now; otherwise, the first hwptr_done |
1568 | * updates for all URBs would happen at the same time when starting */ | |
1569 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) { | |
e4f8e656 | 1570 | subs->ops.prepare = prepare_nodata_playback_urb; |
b55bbf06 CL |
1571 | return start_urbs(subs, runtime); |
1572 | } else | |
1573 | return 0; | |
1da177e4 LT |
1574 | } |
1575 | ||
1700f308 | 1576 | static struct snd_pcm_hardware snd_usb_hardware = |
1da177e4 | 1577 | { |
49045d3d CL |
1578 | .info = SNDRV_PCM_INFO_MMAP | |
1579 | SNDRV_PCM_INFO_MMAP_VALID | | |
1580 | SNDRV_PCM_INFO_BATCH | | |
1581 | SNDRV_PCM_INFO_INTERLEAVED | | |
e4f8e656 CL |
1582 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
1583 | SNDRV_PCM_INFO_PAUSE, | |
d31cbbfd | 1584 | .buffer_bytes_max = 1024 * 1024, |
1da177e4 | 1585 | .period_bytes_min = 64, |
d31cbbfd | 1586 | .period_bytes_max = 512 * 1024, |
1da177e4 LT |
1587 | .periods_min = 2, |
1588 | .periods_max = 1024, | |
1589 | }; | |
1590 | ||
1591 | /* | |
1592 | * h/w constraints | |
1593 | */ | |
1594 | ||
1595 | #ifdef HW_CONST_DEBUG | |
1596 | #define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args) | |
1597 | #else | |
1598 | #define hwc_debug(fmt, args...) /**/ | |
1599 | #endif | |
1600 | ||
a7d9c099 CL |
1601 | static int hw_check_valid_format(struct snd_usb_substream *subs, |
1602 | struct snd_pcm_hw_params *params, | |
1603 | struct audioformat *fp) | |
1da177e4 | 1604 | { |
86e07d34 TI |
1605 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
1606 | struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
1607 | struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
a7d9c099 CL |
1608 | struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); |
1609 | unsigned int ptime; | |
1da177e4 LT |
1610 | |
1611 | /* check the format */ | |
07f51a72 | 1612 | if (!snd_mask_test(fmts, fp->format)) { |
1da177e4 LT |
1613 | hwc_debug(" > check: no supported format %d\n", fp->format); |
1614 | return 0; | |
1615 | } | |
1616 | /* check the channels */ | |
1617 | if (fp->channels < ct->min || fp->channels > ct->max) { | |
1618 | hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max); | |
1619 | return 0; | |
1620 | } | |
1621 | /* check the rate is within the range */ | |
1622 | if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) { | |
1623 | hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max); | |
1624 | return 0; | |
1625 | } | |
1626 | if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) { | |
1627 | hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min); | |
1628 | return 0; | |
1629 | } | |
a7d9c099 CL |
1630 | /* check whether the period time is >= the data packet interval */ |
1631 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) { | |
1632 | ptime = 125 * (1 << fp->datainterval); | |
1633 | if (ptime > pt->max || (ptime == pt->max && pt->openmax)) { | |
1634 | hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max); | |
1635 | return 0; | |
1636 | } | |
1637 | } | |
1da177e4 LT |
1638 | return 1; |
1639 | } | |
1640 | ||
86e07d34 TI |
1641 | static int hw_rule_rate(struct snd_pcm_hw_params *params, |
1642 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 1643 | { |
86e07d34 | 1644 | struct snd_usb_substream *subs = rule->private; |
1da177e4 | 1645 | struct list_head *p; |
86e07d34 | 1646 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
1da177e4 LT |
1647 | unsigned int rmin, rmax; |
1648 | int changed; | |
1649 | ||
1650 | hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max); | |
1651 | changed = 0; | |
1652 | rmin = rmax = 0; | |
1653 | list_for_each(p, &subs->fmt_list) { | |
1654 | struct audioformat *fp; | |
1655 | fp = list_entry(p, struct audioformat, list); | |
a7d9c099 | 1656 | if (!hw_check_valid_format(subs, params, fp)) |
1da177e4 LT |
1657 | continue; |
1658 | if (changed++) { | |
1659 | if (rmin > fp->rate_min) | |
1660 | rmin = fp->rate_min; | |
1661 | if (rmax < fp->rate_max) | |
1662 | rmax = fp->rate_max; | |
1663 | } else { | |
1664 | rmin = fp->rate_min; | |
1665 | rmax = fp->rate_max; | |
1666 | } | |
1667 | } | |
1668 | ||
07f51a72 | 1669 | if (!changed) { |
1da177e4 LT |
1670 | hwc_debug(" --> get empty\n"); |
1671 | it->empty = 1; | |
1672 | return -EINVAL; | |
1673 | } | |
1674 | ||
1675 | changed = 0; | |
1676 | if (it->min < rmin) { | |
1677 | it->min = rmin; | |
1678 | it->openmin = 0; | |
1679 | changed = 1; | |
1680 | } | |
1681 | if (it->max > rmax) { | |
1682 | it->max = rmax; | |
1683 | it->openmax = 0; | |
1684 | changed = 1; | |
1685 | } | |
1686 | if (snd_interval_checkempty(it)) { | |
1687 | it->empty = 1; | |
1688 | return -EINVAL; | |
1689 | } | |
1690 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); | |
1691 | return changed; | |
1692 | } | |
1693 | ||
1694 | ||
86e07d34 TI |
1695 | static int hw_rule_channels(struct snd_pcm_hw_params *params, |
1696 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 1697 | { |
86e07d34 | 1698 | struct snd_usb_substream *subs = rule->private; |
1da177e4 | 1699 | struct list_head *p; |
86e07d34 | 1700 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
1da177e4 LT |
1701 | unsigned int rmin, rmax; |
1702 | int changed; | |
1703 | ||
1704 | hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max); | |
1705 | changed = 0; | |
1706 | rmin = rmax = 0; | |
1707 | list_for_each(p, &subs->fmt_list) { | |
1708 | struct audioformat *fp; | |
1709 | fp = list_entry(p, struct audioformat, list); | |
a7d9c099 | 1710 | if (!hw_check_valid_format(subs, params, fp)) |
1da177e4 LT |
1711 | continue; |
1712 | if (changed++) { | |
1713 | if (rmin > fp->channels) | |
1714 | rmin = fp->channels; | |
1715 | if (rmax < fp->channels) | |
1716 | rmax = fp->channels; | |
1717 | } else { | |
1718 | rmin = fp->channels; | |
1719 | rmax = fp->channels; | |
1720 | } | |
1721 | } | |
1722 | ||
07f51a72 | 1723 | if (!changed) { |
1da177e4 LT |
1724 | hwc_debug(" --> get empty\n"); |
1725 | it->empty = 1; | |
1726 | return -EINVAL; | |
1727 | } | |
1728 | ||
1729 | changed = 0; | |
1730 | if (it->min < rmin) { | |
1731 | it->min = rmin; | |
1732 | it->openmin = 0; | |
1733 | changed = 1; | |
1734 | } | |
1735 | if (it->max > rmax) { | |
1736 | it->max = rmax; | |
1737 | it->openmax = 0; | |
1738 | changed = 1; | |
1739 | } | |
1740 | if (snd_interval_checkempty(it)) { | |
1741 | it->empty = 1; | |
1742 | return -EINVAL; | |
1743 | } | |
1744 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); | |
1745 | return changed; | |
1746 | } | |
1747 | ||
86e07d34 TI |
1748 | static int hw_rule_format(struct snd_pcm_hw_params *params, |
1749 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 1750 | { |
86e07d34 | 1751 | struct snd_usb_substream *subs = rule->private; |
1da177e4 | 1752 | struct list_head *p; |
86e07d34 | 1753 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
1da177e4 LT |
1754 | u64 fbits; |
1755 | u32 oldbits[2]; | |
1756 | int changed; | |
1757 | ||
1758 | hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]); | |
1759 | fbits = 0; | |
1760 | list_for_each(p, &subs->fmt_list) { | |
1761 | struct audioformat *fp; | |
1762 | fp = list_entry(p, struct audioformat, list); | |
a7d9c099 | 1763 | if (!hw_check_valid_format(subs, params, fp)) |
1da177e4 LT |
1764 | continue; |
1765 | fbits |= (1ULL << fp->format); | |
1766 | } | |
1767 | ||
1768 | oldbits[0] = fmt->bits[0]; | |
1769 | oldbits[1] = fmt->bits[1]; | |
1770 | fmt->bits[0] &= (u32)fbits; | |
1771 | fmt->bits[1] &= (u32)(fbits >> 32); | |
07f51a72 | 1772 | if (!fmt->bits[0] && !fmt->bits[1]) { |
1da177e4 LT |
1773 | hwc_debug(" --> get empty\n"); |
1774 | return -EINVAL; | |
1775 | } | |
1776 | changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]); | |
1777 | hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed); | |
1778 | return changed; | |
1779 | } | |
1780 | ||
a7d9c099 CL |
1781 | static int hw_rule_period_time(struct snd_pcm_hw_params *params, |
1782 | struct snd_pcm_hw_rule *rule) | |
1783 | { | |
1784 | struct snd_usb_substream *subs = rule->private; | |
1785 | struct audioformat *fp; | |
1786 | struct snd_interval *it; | |
1787 | unsigned char min_datainterval; | |
1788 | unsigned int pmin; | |
1789 | int changed; | |
1790 | ||
1791 | it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); | |
1792 | hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max); | |
1793 | min_datainterval = 0xff; | |
1794 | list_for_each_entry(fp, &subs->fmt_list, list) { | |
1795 | if (!hw_check_valid_format(subs, params, fp)) | |
1796 | continue; | |
1797 | min_datainterval = min(min_datainterval, fp->datainterval); | |
1798 | } | |
1799 | if (min_datainterval == 0xff) { | |
1800 | hwc_debug(" --> get emtpy\n"); | |
1801 | it->empty = 1; | |
1802 | return -EINVAL; | |
1803 | } | |
1804 | pmin = 125 * (1 << min_datainterval); | |
1805 | changed = 0; | |
1806 | if (it->min < pmin) { | |
1807 | it->min = pmin; | |
1808 | it->openmin = 0; | |
1809 | changed = 1; | |
1810 | } | |
1811 | if (snd_interval_checkempty(it)) { | |
1812 | it->empty = 1; | |
1813 | return -EINVAL; | |
1814 | } | |
1815 | hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed); | |
1816 | return changed; | |
1817 | } | |
1818 | ||
a79eee8d LR |
1819 | /* |
1820 | * If the device supports unusual bit rates, does the request meet these? | |
1821 | */ | |
1822 | static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime, | |
1823 | struct snd_usb_substream *subs) | |
1824 | { | |
8fec560d TI |
1825 | struct audioformat *fp; |
1826 | int count = 0, needs_knot = 0; | |
a79eee8d LR |
1827 | int err; |
1828 | ||
8fec560d TI |
1829 | list_for_each_entry(fp, &subs->fmt_list, list) { |
1830 | if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) | |
1831 | return 0; | |
1832 | count += fp->nr_rates; | |
918f3a0e | 1833 | if (fp->rates & SNDRV_PCM_RATE_KNOT) |
8fec560d | 1834 | needs_knot = 1; |
a79eee8d | 1835 | } |
8fec560d TI |
1836 | if (!needs_knot) |
1837 | return 0; | |
1838 | ||
1839 | subs->rate_list.count = count; | |
1840 | subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL); | |
1841 | subs->rate_list.mask = 0; | |
1842 | count = 0; | |
1843 | list_for_each_entry(fp, &subs->fmt_list, list) { | |
1844 | int i; | |
1845 | for (i = 0; i < fp->nr_rates; i++) | |
1846 | subs->rate_list.list[count++] = fp->rate_table[i]; | |
1847 | } | |
1848 | err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
1849 | &subs->rate_list); | |
1850 | if (err < 0) | |
1851 | return err; | |
a79eee8d LR |
1852 | |
1853 | return 0; | |
1854 | } | |
1855 | ||
1da177e4 LT |
1856 | |
1857 | /* | |
1858 | * set up the runtime hardware information. | |
1859 | */ | |
1860 | ||
86e07d34 | 1861 | static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs) |
1da177e4 LT |
1862 | { |
1863 | struct list_head *p; | |
a7d9c099 CL |
1864 | unsigned int pt, ptmin; |
1865 | int param_period_time_if_needed; | |
1da177e4 LT |
1866 | int err; |
1867 | ||
1868 | runtime->hw.formats = subs->formats; | |
1869 | ||
1870 | runtime->hw.rate_min = 0x7fffffff; | |
1871 | runtime->hw.rate_max = 0; | |
1872 | runtime->hw.channels_min = 256; | |
1873 | runtime->hw.channels_max = 0; | |
1874 | runtime->hw.rates = 0; | |
a7d9c099 | 1875 | ptmin = UINT_MAX; |
1da177e4 LT |
1876 | /* check min/max rates and channels */ |
1877 | list_for_each(p, &subs->fmt_list) { | |
1878 | struct audioformat *fp; | |
1879 | fp = list_entry(p, struct audioformat, list); | |
1880 | runtime->hw.rates |= fp->rates; | |
1881 | if (runtime->hw.rate_min > fp->rate_min) | |
1882 | runtime->hw.rate_min = fp->rate_min; | |
1883 | if (runtime->hw.rate_max < fp->rate_max) | |
1884 | runtime->hw.rate_max = fp->rate_max; | |
1885 | if (runtime->hw.channels_min > fp->channels) | |
1886 | runtime->hw.channels_min = fp->channels; | |
1887 | if (runtime->hw.channels_max < fp->channels) | |
1888 | runtime->hw.channels_max = fp->channels; | |
1889 | if (fp->fmt_type == USB_FORMAT_TYPE_II && fp->frame_size > 0) { | |
1890 | /* FIXME: there might be more than one audio formats... */ | |
1891 | runtime->hw.period_bytes_min = runtime->hw.period_bytes_max = | |
1892 | fp->frame_size; | |
1893 | } | |
a7d9c099 CL |
1894 | pt = 125 * (1 << fp->datainterval); |
1895 | ptmin = min(ptmin, pt); | |
1da177e4 LT |
1896 | } |
1897 | ||
a7d9c099 CL |
1898 | param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME; |
1899 | if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH) | |
1900 | /* full speed devices have fixed data packet interval */ | |
1901 | ptmin = 1000; | |
1902 | if (ptmin == 1000) | |
1903 | /* if period time doesn't go below 1 ms, no rules needed */ | |
1904 | param_period_time_if_needed = -1; | |
1da177e4 | 1905 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
a7d9c099 | 1906 | ptmin, UINT_MAX); |
1da177e4 | 1907 | |
4608eb08 CL |
1908 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
1909 | hw_rule_rate, subs, | |
1910 | SNDRV_PCM_HW_PARAM_FORMAT, | |
1911 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
a7d9c099 | 1912 | param_period_time_if_needed, |
4608eb08 CL |
1913 | -1)) < 0) |
1914 | return err; | |
1915 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, | |
1916 | hw_rule_channels, subs, | |
1917 | SNDRV_PCM_HW_PARAM_FORMAT, | |
1918 | SNDRV_PCM_HW_PARAM_RATE, | |
a7d9c099 | 1919 | param_period_time_if_needed, |
4608eb08 CL |
1920 | -1)) < 0) |
1921 | return err; | |
1922 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, | |
1923 | hw_rule_format, subs, | |
1924 | SNDRV_PCM_HW_PARAM_RATE, | |
1925 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
a7d9c099 | 1926 | param_period_time_if_needed, |
4608eb08 CL |
1927 | -1)) < 0) |
1928 | return err; | |
a7d9c099 CL |
1929 | if (param_period_time_if_needed >= 0) { |
1930 | err = snd_pcm_hw_rule_add(runtime, 0, | |
1931 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
1932 | hw_rule_period_time, subs, | |
1933 | SNDRV_PCM_HW_PARAM_FORMAT, | |
1934 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
1935 | SNDRV_PCM_HW_PARAM_RATE, | |
1936 | -1); | |
1937 | if (err < 0) | |
1938 | return err; | |
1939 | } | |
4608eb08 | 1940 | if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0) |
5a220c86 | 1941 | return err; |
1da177e4 LT |
1942 | return 0; |
1943 | } | |
1944 | ||
1700f308 | 1945 | static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction) |
1da177e4 | 1946 | { |
86e07d34 TI |
1947 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); |
1948 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1949 | struct snd_usb_substream *subs = &as->substream[direction]; | |
1da177e4 LT |
1950 | |
1951 | subs->interface = -1; | |
1952 | subs->format = 0; | |
1700f308 | 1953 | runtime->hw = snd_usb_hardware; |
1da177e4 LT |
1954 | runtime->private_data = subs; |
1955 | subs->pcm_substream = substream; | |
1956 | return setup_hw_info(runtime, subs); | |
1957 | } | |
1958 | ||
86e07d34 | 1959 | static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction) |
1da177e4 | 1960 | { |
86e07d34 TI |
1961 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); |
1962 | struct snd_usb_substream *subs = &as->substream[direction]; | |
1da177e4 | 1963 | |
78b8d5d2 | 1964 | if (!as->chip->shutdown && subs->interface >= 0) { |
1da177e4 LT |
1965 | usb_set_interface(subs->dev, subs->interface, 0); |
1966 | subs->interface = -1; | |
1967 | } | |
1968 | subs->pcm_substream = NULL; | |
1969 | return 0; | |
1970 | } | |
1971 | ||
86e07d34 | 1972 | static int snd_usb_playback_open(struct snd_pcm_substream *substream) |
1da177e4 | 1973 | { |
1700f308 | 1974 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK); |
1da177e4 LT |
1975 | } |
1976 | ||
86e07d34 | 1977 | static int snd_usb_playback_close(struct snd_pcm_substream *substream) |
1da177e4 LT |
1978 | { |
1979 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK); | |
1980 | } | |
1981 | ||
86e07d34 | 1982 | static int snd_usb_capture_open(struct snd_pcm_substream *substream) |
1da177e4 | 1983 | { |
1700f308 | 1984 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE); |
1da177e4 LT |
1985 | } |
1986 | ||
86e07d34 | 1987 | static int snd_usb_capture_close(struct snd_pcm_substream *substream) |
1da177e4 LT |
1988 | { |
1989 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE); | |
1990 | } | |
1991 | ||
86e07d34 | 1992 | static struct snd_pcm_ops snd_usb_playback_ops = { |
1da177e4 LT |
1993 | .open = snd_usb_playback_open, |
1994 | .close = snd_usb_playback_close, | |
1995 | .ioctl = snd_pcm_lib_ioctl, | |
1996 | .hw_params = snd_usb_hw_params, | |
1997 | .hw_free = snd_usb_hw_free, | |
1998 | .prepare = snd_usb_pcm_prepare, | |
b55bbf06 | 1999 | .trigger = snd_usb_pcm_playback_trigger, |
1da177e4 | 2000 | .pointer = snd_usb_pcm_pointer, |
c55675e3 | 2001 | .page = snd_pcm_lib_get_vmalloc_page, |
c32d977b | 2002 | .mmap = snd_pcm_lib_mmap_vmalloc, |
1da177e4 LT |
2003 | }; |
2004 | ||
86e07d34 | 2005 | static struct snd_pcm_ops snd_usb_capture_ops = { |
1da177e4 LT |
2006 | .open = snd_usb_capture_open, |
2007 | .close = snd_usb_capture_close, | |
2008 | .ioctl = snd_pcm_lib_ioctl, | |
2009 | .hw_params = snd_usb_hw_params, | |
2010 | .hw_free = snd_usb_hw_free, | |
2011 | .prepare = snd_usb_pcm_prepare, | |
b55bbf06 | 2012 | .trigger = snd_usb_pcm_capture_trigger, |
1da177e4 | 2013 | .pointer = snd_usb_pcm_pointer, |
c55675e3 | 2014 | .page = snd_pcm_lib_get_vmalloc_page, |
c32d977b | 2015 | .mmap = snd_pcm_lib_mmap_vmalloc, |
1da177e4 LT |
2016 | }; |
2017 | ||
2018 | ||
2019 | ||
2020 | /* | |
2021 | * helper functions | |
2022 | */ | |
2023 | ||
2024 | /* | |
2025 | * combine bytes and get an integer value | |
2026 | */ | |
2027 | unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size) | |
2028 | { | |
2029 | switch (size) { | |
2030 | case 1: return *bytes; | |
2031 | case 2: return combine_word(bytes); | |
2032 | case 3: return combine_triple(bytes); | |
2033 | case 4: return combine_quad(bytes); | |
2034 | default: return 0; | |
2035 | } | |
2036 | } | |
2037 | ||
2038 | /* | |
2039 | * parse descriptor buffer and return the pointer starting the given | |
2040 | * descriptor type. | |
2041 | */ | |
2042 | void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype) | |
2043 | { | |
2044 | u8 *p, *end, *next; | |
2045 | ||
2046 | p = descstart; | |
2047 | end = p + desclen; | |
2048 | for (; p < end;) { | |
2049 | if (p[0] < 2) | |
2050 | return NULL; | |
2051 | next = p + p[0]; | |
2052 | if (next > end) | |
2053 | return NULL; | |
2054 | if (p[1] == dtype && (!after || (void *)p > after)) { | |
2055 | return p; | |
2056 | } | |
2057 | p = next; | |
2058 | } | |
2059 | return NULL; | |
2060 | } | |
2061 | ||
2062 | /* | |
2063 | * find a class-specified interface descriptor with the given subtype. | |
2064 | */ | |
2065 | void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype) | |
2066 | { | |
2067 | unsigned char *p = after; | |
2068 | ||
2069 | while ((p = snd_usb_find_desc(buffer, buflen, p, | |
2070 | USB_DT_CS_INTERFACE)) != NULL) { | |
2071 | if (p[0] >= 3 && p[2] == dsubtype) | |
2072 | return p; | |
2073 | } | |
2074 | return NULL; | |
2075 | } | |
2076 | ||
2077 | /* | |
2078 | * Wrapper for usb_control_msg(). | |
2079 | * Allocates a temp buffer to prevent dmaing from/to the stack. | |
2080 | */ | |
2081 | int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request, | |
2082 | __u8 requesttype, __u16 value, __u16 index, void *data, | |
2083 | __u16 size, int timeout) | |
2084 | { | |
2085 | int err; | |
2086 | void *buf = NULL; | |
2087 | ||
2088 | if (size > 0) { | |
52978be6 | 2089 | buf = kmemdup(data, size, GFP_KERNEL); |
1da177e4 LT |
2090 | if (!buf) |
2091 | return -ENOMEM; | |
1da177e4 LT |
2092 | } |
2093 | err = usb_control_msg(dev, pipe, request, requesttype, | |
2094 | value, index, buf, size, timeout); | |
2095 | if (size > 0) { | |
2096 | memcpy(data, buf, size); | |
2097 | kfree(buf); | |
2098 | } | |
2099 | return err; | |
2100 | } | |
2101 | ||
2102 | ||
2103 | /* | |
2104 | * entry point for linux usb interface | |
2105 | */ | |
2106 | ||
2107 | static int usb_audio_probe(struct usb_interface *intf, | |
2108 | const struct usb_device_id *id); | |
2109 | static void usb_audio_disconnect(struct usb_interface *intf); | |
93521d27 AM |
2110 | |
2111 | #ifdef CONFIG_PM | |
f85bf29c ON |
2112 | static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message); |
2113 | static int usb_audio_resume(struct usb_interface *intf); | |
93521d27 AM |
2114 | #else |
2115 | #define usb_audio_suspend NULL | |
2116 | #define usb_audio_resume NULL | |
2117 | #endif | |
1da177e4 LT |
2118 | |
2119 | static struct usb_device_id usb_audio_ids [] = { | |
2120 | #include "usbquirks.h" | |
2121 | { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS), | |
2122 | .bInterfaceClass = USB_CLASS_AUDIO, | |
2123 | .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL }, | |
2124 | { } /* Terminating entry */ | |
2125 | }; | |
2126 | ||
2127 | MODULE_DEVICE_TABLE (usb, usb_audio_ids); | |
2128 | ||
2129 | static struct usb_driver usb_audio_driver = { | |
1da177e4 LT |
2130 | .name = "snd-usb-audio", |
2131 | .probe = usb_audio_probe, | |
2132 | .disconnect = usb_audio_disconnect, | |
f85bf29c ON |
2133 | .suspend = usb_audio_suspend, |
2134 | .resume = usb_audio_resume, | |
1da177e4 LT |
2135 | .id_table = usb_audio_ids, |
2136 | }; | |
2137 | ||
2138 | ||
727f317a | 2139 | #if defined(CONFIG_PROC_FS) && defined(CONFIG_SND_VERBOSE_PROCFS) |
21a3479a | 2140 | |
1da177e4 LT |
2141 | /* |
2142 | * proc interface for list the supported pcm formats | |
2143 | */ | |
86e07d34 | 2144 | static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer) |
1da177e4 LT |
2145 | { |
2146 | struct list_head *p; | |
2147 | static char *sync_types[4] = { | |
2148 | "NONE", "ASYNC", "ADAPTIVE", "SYNC" | |
2149 | }; | |
2150 | ||
2151 | list_for_each(p, &subs->fmt_list) { | |
2152 | struct audioformat *fp; | |
2153 | fp = list_entry(p, struct audioformat, list); | |
2154 | snd_iprintf(buffer, " Interface %d\n", fp->iface); | |
2155 | snd_iprintf(buffer, " Altset %d\n", fp->altsetting); | |
6e5265ec TI |
2156 | snd_iprintf(buffer, " Format: %s\n", |
2157 | snd_pcm_format_name(fp->format)); | |
1da177e4 LT |
2158 | snd_iprintf(buffer, " Channels: %d\n", fp->channels); |
2159 | snd_iprintf(buffer, " Endpoint: %d %s (%s)\n", | |
2160 | fp->endpoint & USB_ENDPOINT_NUMBER_MASK, | |
2161 | fp->endpoint & USB_DIR_IN ? "IN" : "OUT", | |
2162 | sync_types[(fp->ep_attr & EP_ATTR_MASK) >> 2]); | |
2163 | if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) { | |
2164 | snd_iprintf(buffer, " Rates: %d - %d (continuous)\n", | |
2165 | fp->rate_min, fp->rate_max); | |
2166 | } else { | |
2167 | unsigned int i; | |
2168 | snd_iprintf(buffer, " Rates: "); | |
2169 | for (i = 0; i < fp->nr_rates; i++) { | |
2170 | if (i > 0) | |
2171 | snd_iprintf(buffer, ", "); | |
2172 | snd_iprintf(buffer, "%d", fp->rate_table[i]); | |
2173 | } | |
2174 | snd_iprintf(buffer, "\n"); | |
2175 | } | |
744b89e5 CL |
2176 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) |
2177 | snd_iprintf(buffer, " Data packet interval: %d us\n", | |
2178 | 125 * (1 << fp->datainterval)); | |
1da177e4 | 2179 | // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize); |
b9d710b3 | 2180 | // snd_iprintf(buffer, " EP Attribute = %#x\n", fp->attributes); |
1da177e4 LT |
2181 | } |
2182 | } | |
2183 | ||
86e07d34 | 2184 | static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer) |
1da177e4 LT |
2185 | { |
2186 | if (subs->running) { | |
2187 | unsigned int i; | |
2188 | snd_iprintf(buffer, " Status: Running\n"); | |
2189 | snd_iprintf(buffer, " Interface = %d\n", subs->interface); | |
2190 | snd_iprintf(buffer, " Altset = %d\n", subs->format); | |
2191 | snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs); | |
2192 | for (i = 0; i < subs->nurbs; i++) | |
2193 | snd_iprintf(buffer, "%d ", subs->dataurb[i].packets); | |
2194 | snd_iprintf(buffer, "]\n"); | |
2195 | snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize); | |
08fe1589 | 2196 | snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n", |
1da177e4 LT |
2197 | snd_usb_get_speed(subs->dev) == USB_SPEED_FULL |
2198 | ? get_full_speed_hz(subs->freqm) | |
08fe1589 CL |
2199 | : get_high_speed_hz(subs->freqm), |
2200 | subs->freqm >> 16, subs->freqm & 0xffff); | |
1da177e4 LT |
2201 | } else { |
2202 | snd_iprintf(buffer, " Status: Stop\n"); | |
2203 | } | |
2204 | } | |
2205 | ||
86e07d34 | 2206 | static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) |
1da177e4 | 2207 | { |
86e07d34 | 2208 | struct snd_usb_stream *stream = entry->private_data; |
1da177e4 LT |
2209 | |
2210 | snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name); | |
2211 | ||
2212 | if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) { | |
2213 | snd_iprintf(buffer, "\nPlayback:\n"); | |
2214 | proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer); | |
2215 | proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer); | |
2216 | } | |
2217 | if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) { | |
2218 | snd_iprintf(buffer, "\nCapture:\n"); | |
2219 | proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer); | |
2220 | proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer); | |
2221 | } | |
2222 | } | |
2223 | ||
86e07d34 | 2224 | static void proc_pcm_format_add(struct snd_usb_stream *stream) |
1da177e4 | 2225 | { |
86e07d34 | 2226 | struct snd_info_entry *entry; |
1da177e4 | 2227 | char name[32]; |
86e07d34 | 2228 | struct snd_card *card = stream->chip->card; |
1da177e4 LT |
2229 | |
2230 | sprintf(name, "stream%d", stream->pcm_index); | |
07f51a72 | 2231 | if (!snd_card_proc_new(card, name, &entry)) |
bf850204 | 2232 | snd_info_set_text_ops(entry, stream, proc_pcm_format_read); |
1da177e4 LT |
2233 | } |
2234 | ||
21a3479a JK |
2235 | #else |
2236 | ||
2237 | static inline void proc_pcm_format_add(struct snd_usb_stream *stream) | |
2238 | { | |
2239 | } | |
2240 | ||
2241 | #endif | |
1da177e4 LT |
2242 | |
2243 | /* | |
2244 | * initialize the substream instance. | |
2245 | */ | |
2246 | ||
86e07d34 | 2247 | static void init_substream(struct snd_usb_stream *as, int stream, struct audioformat *fp) |
1da177e4 | 2248 | { |
86e07d34 | 2249 | struct snd_usb_substream *subs = &as->substream[stream]; |
1da177e4 LT |
2250 | |
2251 | INIT_LIST_HEAD(&subs->fmt_list); | |
2252 | spin_lock_init(&subs->lock); | |
2253 | ||
2254 | subs->stream = as; | |
2255 | subs->direction = stream; | |
2256 | subs->dev = as->chip->dev; | |
98e89f60 | 2257 | subs->txfr_quirk = as->chip->txfr_quirk; |
d513202e | 2258 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) { |
1da177e4 | 2259 | subs->ops = audio_urb_ops[stream]; |
d513202e | 2260 | } else { |
1da177e4 | 2261 | subs->ops = audio_urb_ops_high_speed[stream]; |
d513202e CL |
2262 | switch (as->chip->usb_id) { |
2263 | case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */ | |
2264 | case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */ | |
97c889a7 | 2265 | case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */ |
d513202e CL |
2266 | subs->ops.retire_sync = retire_playback_sync_urb_hs_emu; |
2267 | break; | |
2268 | } | |
2269 | } | |
1da177e4 LT |
2270 | snd_pcm_set_ops(as->pcm, stream, |
2271 | stream == SNDRV_PCM_STREAM_PLAYBACK ? | |
2272 | &snd_usb_playback_ops : &snd_usb_capture_ops); | |
2273 | ||
2274 | list_add_tail(&fp->list, &subs->fmt_list); | |
2275 | subs->formats |= 1ULL << fp->format; | |
2276 | subs->endpoint = fp->endpoint; | |
2277 | subs->num_formats++; | |
2278 | subs->fmt_type = fp->fmt_type; | |
2279 | } | |
2280 | ||
2281 | ||
2282 | /* | |
2283 | * free a substream | |
2284 | */ | |
86e07d34 | 2285 | static void free_substream(struct snd_usb_substream *subs) |
1da177e4 LT |
2286 | { |
2287 | struct list_head *p, *n; | |
2288 | ||
07f51a72 | 2289 | if (!subs->num_formats) |
1da177e4 LT |
2290 | return; /* not initialized */ |
2291 | list_for_each_safe(p, n, &subs->fmt_list) { | |
2292 | struct audioformat *fp = list_entry(p, struct audioformat, list); | |
2293 | kfree(fp->rate_table); | |
2294 | kfree(fp); | |
2295 | } | |
8fec560d | 2296 | kfree(subs->rate_list.list); |
1da177e4 LT |
2297 | } |
2298 | ||
2299 | ||
2300 | /* | |
2301 | * free a usb stream instance | |
2302 | */ | |
86e07d34 | 2303 | static void snd_usb_audio_stream_free(struct snd_usb_stream *stream) |
1da177e4 LT |
2304 | { |
2305 | free_substream(&stream->substream[0]); | |
2306 | free_substream(&stream->substream[1]); | |
2307 | list_del(&stream->list); | |
2308 | kfree(stream); | |
2309 | } | |
2310 | ||
86e07d34 | 2311 | static void snd_usb_audio_pcm_free(struct snd_pcm *pcm) |
1da177e4 | 2312 | { |
86e07d34 | 2313 | struct snd_usb_stream *stream = pcm->private_data; |
1da177e4 LT |
2314 | if (stream) { |
2315 | stream->pcm = NULL; | |
1da177e4 LT |
2316 | snd_usb_audio_stream_free(stream); |
2317 | } | |
2318 | } | |
2319 | ||
2320 | ||
2321 | /* | |
2322 | * add this endpoint to the chip instance. | |
2323 | * if a stream with the same endpoint already exists, append to it. | |
2324 | * if not, create a new pcm stream. | |
2325 | */ | |
86e07d34 | 2326 | static int add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp) |
1da177e4 LT |
2327 | { |
2328 | struct list_head *p; | |
86e07d34 TI |
2329 | struct snd_usb_stream *as; |
2330 | struct snd_usb_substream *subs; | |
2331 | struct snd_pcm *pcm; | |
1da177e4 LT |
2332 | int err; |
2333 | ||
2334 | list_for_each(p, &chip->pcm_list) { | |
86e07d34 | 2335 | as = list_entry(p, struct snd_usb_stream, list); |
1da177e4 LT |
2336 | if (as->fmt_type != fp->fmt_type) |
2337 | continue; | |
2338 | subs = &as->substream[stream]; | |
07f51a72 | 2339 | if (!subs->endpoint) |
1da177e4 LT |
2340 | continue; |
2341 | if (subs->endpoint == fp->endpoint) { | |
2342 | list_add_tail(&fp->list, &subs->fmt_list); | |
2343 | subs->num_formats++; | |
2344 | subs->formats |= 1ULL << fp->format; | |
2345 | return 0; | |
2346 | } | |
2347 | } | |
2348 | /* look for an empty stream */ | |
2349 | list_for_each(p, &chip->pcm_list) { | |
86e07d34 | 2350 | as = list_entry(p, struct snd_usb_stream, list); |
1da177e4 LT |
2351 | if (as->fmt_type != fp->fmt_type) |
2352 | continue; | |
2353 | subs = &as->substream[stream]; | |
2354 | if (subs->endpoint) | |
2355 | continue; | |
2356 | err = snd_pcm_new_stream(as->pcm, stream, 1); | |
2357 | if (err < 0) | |
2358 | return err; | |
2359 | init_substream(as, stream, fp); | |
2360 | return 0; | |
2361 | } | |
2362 | ||
2363 | /* create a new pcm */ | |
59feddb2 | 2364 | as = kzalloc(sizeof(*as), GFP_KERNEL); |
07f51a72 | 2365 | if (!as) |
1da177e4 | 2366 | return -ENOMEM; |
1da177e4 LT |
2367 | as->pcm_index = chip->pcm_devs; |
2368 | as->chip = chip; | |
2369 | as->fmt_type = fp->fmt_type; | |
2370 | err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs, | |
2371 | stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0, | |
2372 | stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1, | |
2373 | &pcm); | |
2374 | if (err < 0) { | |
2375 | kfree(as); | |
2376 | return err; | |
2377 | } | |
2378 | as->pcm = pcm; | |
2379 | pcm->private_data = as; | |
2380 | pcm->private_free = snd_usb_audio_pcm_free; | |
2381 | pcm->info_flags = 0; | |
2382 | if (chip->pcm_devs > 0) | |
2383 | sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs); | |
2384 | else | |
2385 | strcpy(pcm->name, "USB Audio"); | |
2386 | ||
2387 | init_substream(as, stream, fp); | |
2388 | ||
2389 | list_add(&as->list, &chip->pcm_list); | |
2390 | chip->pcm_devs++; | |
2391 | ||
2392 | proc_pcm_format_add(as); | |
2393 | ||
2394 | return 0; | |
2395 | } | |
2396 | ||
2397 | ||
2398 | /* | |
2399 | * check if the device uses big-endian samples | |
2400 | */ | |
86e07d34 | 2401 | static int is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp) |
1da177e4 | 2402 | { |
27d10f56 CL |
2403 | switch (chip->usb_id) { |
2404 | case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */ | |
2405 | if (fp->endpoint & USB_DIR_IN) | |
1da177e4 | 2406 | return 1; |
27d10f56 CL |
2407 | break; |
2408 | case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ | |
f8c78b82 TLM |
2409 | if (device_setup[chip->index] == 0x00 || |
2410 | fp->altsetting==1 || fp->altsetting==2 || fp->altsetting==3) | |
2411 | return 1; | |
1da177e4 LT |
2412 | } |
2413 | return 0; | |
2414 | } | |
2415 | ||
2416 | /* | |
2417 | * parse the audio format type I descriptor | |
2418 | * and returns the corresponding pcm format | |
2419 | * | |
2420 | * @dev: usb device | |
2421 | * @fp: audioformat record | |
2422 | * @format: the format tag (wFormatTag) | |
2423 | * @fmt: the format type descriptor | |
2424 | */ | |
86e07d34 | 2425 | static int parse_audio_format_i_type(struct snd_usb_audio *chip, struct audioformat *fp, |
28e1b773 | 2426 | int format, void *fmt_raw) |
1da177e4 LT |
2427 | { |
2428 | int pcm_format; | |
2429 | int sample_width, sample_bytes; | |
28e1b773 | 2430 | struct uac_format_type_i_discrete_descriptor *fmt = fmt_raw; |
1da177e4 LT |
2431 | |
2432 | /* FIXME: correct endianess and sign? */ | |
2433 | pcm_format = -1; | |
28e1b773 DM |
2434 | sample_width = fmt->bBitResolution; |
2435 | sample_bytes = fmt->bSubframeSize; | |
2436 | ||
1da177e4 LT |
2437 | switch (format) { |
2438 | case 0: /* some devices don't define this correctly... */ | |
2439 | snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n", | |
27d10f56 | 2440 | chip->dev->devnum, fp->iface, fp->altsetting); |
1da177e4 LT |
2441 | /* fall-through */ |
2442 | case USB_AUDIO_FORMAT_PCM: | |
2443 | if (sample_width > sample_bytes * 8) { | |
2444 | snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n", | |
27d10f56 | 2445 | chip->dev->devnum, fp->iface, fp->altsetting, |
1da177e4 LT |
2446 | sample_width, sample_bytes); |
2447 | } | |
2448 | /* check the format byte size */ | |
28e1b773 | 2449 | switch (sample_bytes) { |
1da177e4 LT |
2450 | case 1: |
2451 | pcm_format = SNDRV_PCM_FORMAT_S8; | |
2452 | break; | |
2453 | case 2: | |
27d10f56 | 2454 | if (is_big_endian_format(chip, fp)) |
1da177e4 LT |
2455 | pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */ |
2456 | else | |
2457 | pcm_format = SNDRV_PCM_FORMAT_S16_LE; | |
2458 | break; | |
2459 | case 3: | |
27d10f56 | 2460 | if (is_big_endian_format(chip, fp)) |
1da177e4 LT |
2461 | pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */ |
2462 | else | |
2463 | pcm_format = SNDRV_PCM_FORMAT_S24_3LE; | |
2464 | break; | |
2465 | case 4: | |
2466 | pcm_format = SNDRV_PCM_FORMAT_S32_LE; | |
2467 | break; | |
2468 | default: | |
2469 | snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n", | |
28e1b773 DM |
2470 | chip->dev->devnum, fp->iface, fp->altsetting, |
2471 | sample_width, sample_bytes); | |
1da177e4 LT |
2472 | break; |
2473 | } | |
2474 | break; | |
2475 | case USB_AUDIO_FORMAT_PCM8: | |
2a56f51b PM |
2476 | pcm_format = SNDRV_PCM_FORMAT_U8; |
2477 | ||
2478 | /* Dallas DS4201 workaround: it advertises U8 format, but really | |
2479 | supports S8. */ | |
27d10f56 | 2480 | if (chip->usb_id == USB_ID(0x04fa, 0x4201)) |
1da177e4 | 2481 | pcm_format = SNDRV_PCM_FORMAT_S8; |
1da177e4 LT |
2482 | break; |
2483 | case USB_AUDIO_FORMAT_IEEE_FLOAT: | |
2484 | pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE; | |
2485 | break; | |
2486 | case USB_AUDIO_FORMAT_ALAW: | |
2487 | pcm_format = SNDRV_PCM_FORMAT_A_LAW; | |
2488 | break; | |
2489 | case USB_AUDIO_FORMAT_MU_LAW: | |
2490 | pcm_format = SNDRV_PCM_FORMAT_MU_LAW; | |
2491 | break; | |
2492 | default: | |
2493 | snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n", | |
27d10f56 | 2494 | chip->dev->devnum, fp->iface, fp->altsetting, format); |
1da177e4 LT |
2495 | break; |
2496 | } | |
2497 | return pcm_format; | |
2498 | } | |
2499 | ||
2500 | ||
2501 | /* | |
2502 | * parse the format descriptor and stores the possible sample rates | |
2503 | * on the audioformat table. | |
2504 | * | |
2505 | * @dev: usb device | |
2506 | * @fp: audioformat record | |
2507 | * @fmt: the format descriptor | |
2508 | * @offset: the start offset of descriptor pointing the rate type | |
2509 | * (7 for type I and II, 8 for type II) | |
2510 | */ | |
86e07d34 | 2511 | static int parse_audio_format_rates(struct snd_usb_audio *chip, struct audioformat *fp, |
1da177e4 LT |
2512 | unsigned char *fmt, int offset) |
2513 | { | |
2514 | int nr_rates = fmt[offset]; | |
918f3a0e | 2515 | |
1da177e4 LT |
2516 | if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) { |
2517 | snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n", | |
27d10f56 | 2518 | chip->dev->devnum, fp->iface, fp->altsetting); |
1da177e4 LT |
2519 | return -1; |
2520 | } | |
2521 | ||
2522 | if (nr_rates) { | |
2523 | /* | |
2524 | * build the rate table and bitmap flags | |
2525 | */ | |
918f3a0e | 2526 | int r, idx; |
918f3a0e | 2527 | |
1da177e4 LT |
2528 | fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL); |
2529 | if (fp->rate_table == NULL) { | |
2530 | snd_printk(KERN_ERR "cannot malloc\n"); | |
2531 | return -1; | |
2532 | } | |
2533 | ||
0412558c TI |
2534 | fp->nr_rates = 0; |
2535 | fp->rate_min = fp->rate_max = 0; | |
1da177e4 | 2536 | for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) { |
987411b7 | 2537 | unsigned int rate = combine_triple(&fmt[idx]); |
0412558c TI |
2538 | if (!rate) |
2539 | continue; | |
987411b7 CL |
2540 | /* C-Media CM6501 mislabels its 96 kHz altsetting */ |
2541 | if (rate == 48000 && nr_rates == 1 && | |
3b03cc5b JR |
2542 | (chip->usb_id == USB_ID(0x0d8c, 0x0201) || |
2543 | chip->usb_id == USB_ID(0x0d8c, 0x0102)) && | |
987411b7 CL |
2544 | fp->altsetting == 5 && fp->maxpacksize == 392) |
2545 | rate = 96000; | |
0412558c TI |
2546 | fp->rate_table[fp->nr_rates] = rate; |
2547 | if (!fp->rate_min || rate < fp->rate_min) | |
1da177e4 | 2548 | fp->rate_min = rate; |
0412558c | 2549 | if (!fp->rate_max || rate > fp->rate_max) |
1da177e4 | 2550 | fp->rate_max = rate; |
918f3a0e | 2551 | fp->rates |= snd_pcm_rate_to_rate_bit(rate); |
0412558c | 2552 | fp->nr_rates++; |
1da177e4 | 2553 | } |
0412558c | 2554 | if (!fp->nr_rates) { |
beb60119 GJ |
2555 | hwc_debug("All rates were zero. Skipping format!\n"); |
2556 | return -1; | |
2557 | } | |
1da177e4 LT |
2558 | } else { |
2559 | /* continuous rates */ | |
2560 | fp->rates = SNDRV_PCM_RATE_CONTINUOUS; | |
2561 | fp->rate_min = combine_triple(&fmt[offset + 1]); | |
2562 | fp->rate_max = combine_triple(&fmt[offset + 4]); | |
2563 | } | |
2564 | return 0; | |
2565 | } | |
2566 | ||
2567 | /* | |
2568 | * parse the format type I and III descriptors | |
2569 | */ | |
86e07d34 | 2570 | static int parse_audio_format_i(struct snd_usb_audio *chip, struct audioformat *fp, |
28e1b773 | 2571 | int format, void *fmt_raw) |
1da177e4 LT |
2572 | { |
2573 | int pcm_format; | |
28e1b773 | 2574 | struct uac_format_type_i_discrete_descriptor *fmt = fmt_raw; |
1da177e4 | 2575 | |
28e1b773 | 2576 | if (fmt->bFormatType == USB_FORMAT_TYPE_III) { |
1da177e4 LT |
2577 | /* FIXME: the format type is really IECxxx |
2578 | * but we give normal PCM format to get the existing | |
2579 | * apps working... | |
2580 | */ | |
cac19c3b TLM |
2581 | switch (chip->usb_id) { |
2582 | ||
2583 | case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ | |
2584 | if (device_setup[chip->index] == 0x00 && | |
2585 | fp->altsetting == 6) | |
2586 | pcm_format = SNDRV_PCM_FORMAT_S16_BE; | |
2587 | else | |
2588 | pcm_format = SNDRV_PCM_FORMAT_S16_LE; | |
2589 | break; | |
2590 | default: | |
2591 | pcm_format = SNDRV_PCM_FORMAT_S16_LE; | |
2592 | } | |
1da177e4 | 2593 | } else { |
27d10f56 | 2594 | pcm_format = parse_audio_format_i_type(chip, fp, format, fmt); |
1da177e4 LT |
2595 | if (pcm_format < 0) |
2596 | return -1; | |
2597 | } | |
28e1b773 | 2598 | |
1da177e4 | 2599 | fp->format = pcm_format; |
28e1b773 DM |
2600 | fp->channels = fmt->bNrChannels; |
2601 | ||
1da177e4 LT |
2602 | if (fp->channels < 1) { |
2603 | snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n", | |
27d10f56 | 2604 | chip->dev->devnum, fp->iface, fp->altsetting, fp->channels); |
1da177e4 LT |
2605 | return -1; |
2606 | } | |
28e1b773 | 2607 | return parse_audio_format_rates(chip, fp, fmt_raw, 7); |
1da177e4 LT |
2608 | } |
2609 | ||
2610 | /* | |
28e1b773 | 2611 | * parse the format type II descriptor |
1da177e4 | 2612 | */ |
86e07d34 | 2613 | static int parse_audio_format_ii(struct snd_usb_audio *chip, struct audioformat *fp, |
28e1b773 | 2614 | int format, void *fmt_raw) |
1da177e4 LT |
2615 | { |
2616 | int brate, framesize; | |
28e1b773 DM |
2617 | struct uac_format_type_ii_discrete_descriptor *fmt = fmt_raw; |
2618 | ||
1da177e4 LT |
2619 | switch (format) { |
2620 | case USB_AUDIO_FORMAT_AC3: | |
2621 | /* FIXME: there is no AC3 format defined yet */ | |
2622 | // fp->format = SNDRV_PCM_FORMAT_AC3; | |
2623 | fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */ | |
2624 | break; | |
2625 | case USB_AUDIO_FORMAT_MPEG: | |
2626 | fp->format = SNDRV_PCM_FORMAT_MPEG; | |
2627 | break; | |
2628 | default: | |
b9d710b3 | 2629 | snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n", |
27d10f56 | 2630 | chip->dev->devnum, fp->iface, fp->altsetting, format); |
1da177e4 LT |
2631 | fp->format = SNDRV_PCM_FORMAT_MPEG; |
2632 | break; | |
2633 | } | |
28e1b773 | 2634 | |
1da177e4 | 2635 | fp->channels = 1; |
28e1b773 DM |
2636 | |
2637 | brate = le16_to_cpu(fmt->wMaxBitRate); | |
2638 | framesize = le16_to_cpu(fmt->wSamplesPerFrame); | |
1da177e4 LT |
2639 | snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); |
2640 | fp->frame_size = framesize; | |
28e1b773 | 2641 | return parse_audio_format_rates(chip, fp, fmt_raw, 8); /* fmt[8..] sample rates */ |
1da177e4 LT |
2642 | } |
2643 | ||
86e07d34 | 2644 | static int parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp, |
28e1b773 | 2645 | int format, void *fmt_raw, int stream) |
1da177e4 LT |
2646 | { |
2647 | int err; | |
28e1b773 DM |
2648 | /* we only parse the common header of all format types here, |
2649 | * so it is safe to take a type_i struct */ | |
2650 | struct uac_format_type_i_discrete_descriptor *fmt = fmt_raw; | |
1da177e4 | 2651 | |
28e1b773 | 2652 | switch (fmt->bFormatType) { |
1da177e4 LT |
2653 | case USB_FORMAT_TYPE_I: |
2654 | case USB_FORMAT_TYPE_III: | |
27d10f56 | 2655 | err = parse_audio_format_i(chip, fp, format, fmt); |
1da177e4 LT |
2656 | break; |
2657 | case USB_FORMAT_TYPE_II: | |
27d10f56 | 2658 | err = parse_audio_format_ii(chip, fp, format, fmt); |
1da177e4 LT |
2659 | break; |
2660 | default: | |
2661 | snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n", | |
28e1b773 | 2662 | chip->dev->devnum, fp->iface, fp->altsetting, fmt->bFormatType); |
1da177e4 LT |
2663 | return -1; |
2664 | } | |
28e1b773 | 2665 | fp->fmt_type = fmt->bFormatType; |
1da177e4 LT |
2666 | if (err < 0) |
2667 | return err; | |
2668 | #if 1 | |
33159378 | 2669 | /* FIXME: temporary hack for extigy/audigy 2 nx/zs */ |
1da177e4 LT |
2670 | /* extigy apparently supports sample rates other than 48k |
2671 | * but not in ordinary way. so we enable only 48k atm. | |
2672 | */ | |
27d10f56 | 2673 | if (chip->usb_id == USB_ID(0x041e, 0x3000) || |
33159378 CL |
2674 | chip->usb_id == USB_ID(0x041e, 0x3020) || |
2675 | chip->usb_id == USB_ID(0x041e, 0x3061)) { | |
28e1b773 | 2676 | if (fmt->bFormatType == USB_FORMAT_TYPE_I && |
8c1872dc CL |
2677 | fp->rates != SNDRV_PCM_RATE_48000 && |
2678 | fp->rates != SNDRV_PCM_RATE_96000) | |
b4d3f9d4 | 2679 | return -1; |
1da177e4 LT |
2680 | } |
2681 | #endif | |
2682 | return 0; | |
2683 | } | |
2684 | ||
744b89e5 CL |
2685 | static unsigned char parse_datainterval(struct snd_usb_audio *chip, |
2686 | struct usb_host_interface *alts) | |
2687 | { | |
2688 | if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH && | |
2689 | get_endpoint(alts, 0)->bInterval >= 1 && | |
2690 | get_endpoint(alts, 0)->bInterval <= 4) | |
2691 | return get_endpoint(alts, 0)->bInterval - 1; | |
2692 | else | |
2693 | return 0; | |
2694 | } | |
2695 | ||
e311334e TLM |
2696 | static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip, |
2697 | int iface, int altno); | |
86e07d34 | 2698 | static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no) |
1da177e4 LT |
2699 | { |
2700 | struct usb_device *dev; | |
2701 | struct usb_interface *iface; | |
2702 | struct usb_host_interface *alts; | |
2703 | struct usb_interface_descriptor *altsd; | |
2704 | int i, altno, err, stream; | |
2705 | int format; | |
8886f33f | 2706 | struct audioformat *fp = NULL; |
1da177e4 | 2707 | unsigned char *fmt, *csep; |
b9d43bcd | 2708 | int num; |
1da177e4 LT |
2709 | |
2710 | dev = chip->dev; | |
2711 | ||
2712 | /* parse the interface's altsettings */ | |
2713 | iface = usb_ifnum_to_if(dev, iface_no); | |
b9d43bcd PM |
2714 | |
2715 | num = iface->num_altsetting; | |
2716 | ||
2717 | /* | |
2718 | * Dallas DS4201 workaround: It presents 5 altsettings, but the last | |
2719 | * one misses syncpipe, and does not produce any sound. | |
2720 | */ | |
2721 | if (chip->usb_id == USB_ID(0x04fa, 0x4201)) | |
2722 | num = 4; | |
2723 | ||
2724 | for (i = 0; i < num; i++) { | |
28e1b773 DM |
2725 | struct uac_as_header_descriptor_v1 *as; |
2726 | ||
1da177e4 LT |
2727 | alts = &iface->altsetting[i]; |
2728 | altsd = get_iface_desc(alts); | |
2729 | /* skip invalid one */ | |
2730 | if ((altsd->bInterfaceClass != USB_CLASS_AUDIO && | |
2731 | altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || | |
2732 | (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING && | |
2733 | altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) || | |
2734 | altsd->bNumEndpoints < 1 || | |
2735 | le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0) | |
2736 | continue; | |
2737 | /* must be isochronous */ | |
2738 | if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != | |
2739 | USB_ENDPOINT_XFER_ISOC) | |
2740 | continue; | |
2741 | /* check direction */ | |
2742 | stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ? | |
2743 | SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; | |
2744 | altno = altsd->bAlternateSetting; | |
28e1b773 | 2745 | |
e311334e TLM |
2746 | /* audiophile usb: skip altsets incompatible with device_setup |
2747 | */ | |
2748 | if (chip->usb_id == USB_ID(0x0763, 0x2003) && | |
2749 | audiophile_skip_setting_quirk(chip, iface_no, altno)) | |
2750 | continue; | |
1da177e4 LT |
2751 | |
2752 | /* get audio formats */ | |
28e1b773 DM |
2753 | as = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, AS_GENERAL); |
2754 | ||
2755 | if (!as) { | |
1da177e4 LT |
2756 | snd_printk(KERN_ERR "%d:%u:%d : AS_GENERAL descriptor not found\n", |
2757 | dev->devnum, iface_no, altno); | |
2758 | continue; | |
2759 | } | |
2760 | ||
28e1b773 | 2761 | if (as->bLength < sizeof(*as)) { |
1da177e4 LT |
2762 | snd_printk(KERN_ERR "%d:%u:%d : invalid AS_GENERAL desc\n", |
2763 | dev->devnum, iface_no, altno); | |
2764 | continue; | |
2765 | } | |
2766 | ||
28e1b773 | 2767 | format = le16_to_cpu(as->wFormatTag); /* remember the format value */ |
1da177e4 LT |
2768 | |
2769 | /* get format type */ | |
2770 | fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, FORMAT_TYPE); | |
2771 | if (!fmt) { | |
2772 | snd_printk(KERN_ERR "%d:%u:%d : no FORMAT_TYPE desc\n", | |
2773 | dev->devnum, iface_no, altno); | |
2774 | continue; | |
2775 | } | |
2776 | if (fmt[0] < 8) { | |
2777 | snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n", | |
2778 | dev->devnum, iface_no, altno); | |
2779 | continue; | |
2780 | } | |
2781 | ||
8886f33f CL |
2782 | /* |
2783 | * Blue Microphones workaround: The last altsetting is identical | |
2784 | * with the previous one, except for a larger packet size, but | |
2785 | * is actually a mislabeled two-channel setting; ignore it. | |
2786 | */ | |
2787 | if (fmt[4] == 1 && fmt[5] == 2 && altno == 2 && num == 3 && | |
2788 | fp && fp->altsetting == 1 && fp->channels == 1 && | |
2789 | fp->format == SNDRV_PCM_FORMAT_S16_LE && | |
2790 | le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == | |
2791 | fp->maxpacksize * 2) | |
2792 | continue; | |
2793 | ||
1da177e4 LT |
2794 | csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT); |
2795 | /* Creamware Noah has this descriptor after the 2nd endpoint */ | |
2796 | if (!csep && altsd->bNumEndpoints >= 2) | |
2797 | csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT); | |
2798 | if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) { | |
f8c75790 | 2799 | snd_printk(KERN_WARNING "%d:%u:%d : no or invalid" |
faf8d117 | 2800 | " class specific endpoint descriptor\n", |
1da177e4 | 2801 | dev->devnum, iface_no, altno); |
faf8d117 | 2802 | csep = NULL; |
1da177e4 LT |
2803 | } |
2804 | ||
59feddb2 | 2805 | fp = kzalloc(sizeof(*fp), GFP_KERNEL); |
1da177e4 LT |
2806 | if (! fp) { |
2807 | snd_printk(KERN_ERR "cannot malloc\n"); | |
2808 | return -ENOMEM; | |
2809 | } | |
2810 | ||
1da177e4 LT |
2811 | fp->iface = iface_no; |
2812 | fp->altsetting = altno; | |
2813 | fp->altset_idx = i; | |
2814 | fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; | |
2815 | fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; | |
744b89e5 | 2816 | fp->datainterval = parse_datainterval(chip, alts); |
1da177e4 | 2817 | fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); |
573567e0 CL |
2818 | if (snd_usb_get_speed(dev) == USB_SPEED_HIGH) |
2819 | fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1) | |
2820 | * (fp->maxpacksize & 0x7ff); | |
faf8d117 | 2821 | fp->attributes = csep ? csep[3] : 0; |
1da177e4 LT |
2822 | |
2823 | /* some quirks for attributes here */ | |
2824 | ||
c3f93297 CL |
2825 | switch (chip->usb_id) { |
2826 | case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */ | |
1da177e4 LT |
2827 | /* Optoplay sets the sample rate attribute although |
2828 | * it seems not supporting it in fact. | |
2829 | */ | |
2830 | fp->attributes &= ~EP_CS_ATTR_SAMPLE_RATE; | |
c3f93297 CL |
2831 | break; |
2832 | case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */ | |
2833 | case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ | |
1da177e4 LT |
2834 | /* doesn't set the sample rate attribute, but supports it */ |
2835 | fp->attributes |= EP_CS_ATTR_SAMPLE_RATE; | |
c3f93297 CL |
2836 | break; |
2837 | case USB_ID(0x047f, 0x0ca1): /* plantronics headset */ | |
2838 | case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is | |
2839 | an older model 77d:223) */ | |
1da177e4 LT |
2840 | /* |
2841 | * plantronics headset and Griffin iMic have set adaptive-in | |
2842 | * although it's really not... | |
2843 | */ | |
1da177e4 LT |
2844 | fp->ep_attr &= ~EP_ATTR_MASK; |
2845 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) | |
2846 | fp->ep_attr |= EP_ATTR_ADAPTIVE; | |
2847 | else | |
2848 | fp->ep_attr |= EP_ATTR_SYNC; | |
c3f93297 | 2849 | break; |
1da177e4 LT |
2850 | } |
2851 | ||
2852 | /* ok, let's parse further... */ | |
27d10f56 | 2853 | if (parse_audio_format(chip, fp, format, fmt, stream) < 0) { |
1da177e4 LT |
2854 | kfree(fp->rate_table); |
2855 | kfree(fp); | |
2856 | continue; | |
2857 | } | |
2858 | ||
b9d710b3 | 2859 | snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint); |
1da177e4 LT |
2860 | err = add_audio_endpoint(chip, stream, fp); |
2861 | if (err < 0) { | |
2862 | kfree(fp->rate_table); | |
2863 | kfree(fp); | |
2864 | return err; | |
2865 | } | |
2866 | /* try to set the interface... */ | |
2867 | usb_set_interface(chip->dev, iface_no, altno); | |
2868 | init_usb_pitch(chip->dev, iface_no, alts, fp); | |
2869 | init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max); | |
2870 | } | |
2871 | return 0; | |
2872 | } | |
2873 | ||
2874 | ||
2875 | /* | |
2876 | * disconnect streams | |
2877 | * called from snd_usb_audio_disconnect() | |
2878 | */ | |
ee733397 | 2879 | static void snd_usb_stream_disconnect(struct list_head *head) |
1da177e4 LT |
2880 | { |
2881 | int idx; | |
86e07d34 TI |
2882 | struct snd_usb_stream *as; |
2883 | struct snd_usb_substream *subs; | |
1da177e4 | 2884 | |
86e07d34 | 2885 | as = list_entry(head, struct snd_usb_stream, list); |
1da177e4 LT |
2886 | for (idx = 0; idx < 2; idx++) { |
2887 | subs = &as->substream[idx]; | |
2888 | if (!subs->num_formats) | |
2889 | return; | |
2890 | release_substream_urbs(subs, 1); | |
2891 | subs->interface = -1; | |
2892 | } | |
2893 | } | |
2894 | ||
28e1b773 DM |
2895 | static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface) |
2896 | { | |
2897 | struct usb_device *dev = chip->dev; | |
2898 | struct usb_host_interface *alts; | |
2899 | struct usb_interface_descriptor *altsd; | |
2900 | struct usb_interface *iface = usb_ifnum_to_if(dev, interface); | |
2901 | ||
2902 | if (!iface) { | |
2903 | snd_printk(KERN_ERR "%d:%u:%d : does not exist\n", | |
2904 | dev->devnum, ctrlif, interface); | |
2905 | return -EINVAL; | |
2906 | } | |
2907 | ||
2908 | if (usb_interface_claimed(iface)) { | |
2909 | snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n", | |
2910 | dev->devnum, ctrlif, interface); | |
2911 | return -EINVAL; | |
2912 | } | |
2913 | ||
2914 | alts = &iface->altsetting[0]; | |
2915 | altsd = get_iface_desc(alts); | |
2916 | if ((altsd->bInterfaceClass == USB_CLASS_AUDIO || | |
2917 | altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) && | |
2918 | altsd->bInterfaceSubClass == USB_SUBCLASS_MIDI_STREAMING) { | |
2919 | int err = snd_usbmidi_create(chip->card, iface, | |
2920 | &chip->midi_list, NULL); | |
2921 | if (err < 0) { | |
2922 | snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n", | |
2923 | dev->devnum, ctrlif, interface); | |
2924 | return -EINVAL; | |
2925 | } | |
2926 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); | |
2927 | ||
2928 | return 0; | |
2929 | } | |
2930 | ||
2931 | if ((altsd->bInterfaceClass != USB_CLASS_AUDIO && | |
2932 | altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || | |
2933 | altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING) { | |
2934 | snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n", | |
2935 | dev->devnum, ctrlif, interface, altsd->bInterfaceClass); | |
2936 | /* skip non-supported classes */ | |
2937 | return -EINVAL; | |
2938 | } | |
2939 | ||
2940 | if (snd_usb_get_speed(dev) == USB_SPEED_LOW) { | |
2941 | snd_printk(KERN_ERR "low speed audio streaming not supported\n"); | |
2942 | return -EINVAL; | |
2943 | } | |
2944 | ||
2945 | if (! parse_audio_endpoints(chip, interface)) { | |
2946 | usb_set_interface(dev, interface, 0); /* reset the current interface */ | |
2947 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); | |
2948 | return -EINVAL; | |
2949 | } | |
2950 | ||
2951 | return 0; | |
2952 | } | |
2953 | ||
1da177e4 LT |
2954 | /* |
2955 | * parse audio control descriptor and create pcm/midi streams | |
2956 | */ | |
86e07d34 | 2957 | static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif) |
1da177e4 LT |
2958 | { |
2959 | struct usb_device *dev = chip->dev; | |
2960 | struct usb_host_interface *host_iface; | |
28e1b773 DM |
2961 | struct uac_ac_header_descriptor_v1 *h1; |
2962 | void *control_header; | |
2963 | int i; | |
1da177e4 LT |
2964 | |
2965 | /* find audiocontrol interface */ | |
2966 | host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0]; | |
28e1b773 DM |
2967 | control_header = snd_usb_find_csint_desc(host_iface->extra, |
2968 | host_iface->extralen, | |
2969 | NULL, HEADER); | |
2970 | ||
2971 | if (!control_header) { | |
1da177e4 LT |
2972 | snd_printk(KERN_ERR "cannot find HEADER\n"); |
2973 | return -EINVAL; | |
2974 | } | |
28e1b773 DM |
2975 | |
2976 | h1 = control_header; | |
2977 | ||
2978 | if (!h1->bInCollection) { | |
2979 | snd_printk(KERN_INFO "skipping empty audio interface (v1)\n"); | |
1da177e4 LT |
2980 | return -EINVAL; |
2981 | } | |
2982 | ||
28e1b773 DM |
2983 | if (h1->bLength < sizeof(*h1) + h1->bInCollection) { |
2984 | snd_printk(KERN_ERR "invalid HEADER (v1)\n"); | |
2985 | return -EINVAL; | |
1da177e4 LT |
2986 | } |
2987 | ||
28e1b773 DM |
2988 | for (i = 0; i < h1->bInCollection; i++) |
2989 | snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]); | |
2990 | ||
1da177e4 LT |
2991 | return 0; |
2992 | } | |
2993 | ||
2994 | /* | |
2995 | * create a stream for an endpoint/altsetting without proper descriptors | |
2996 | */ | |
86e07d34 | 2997 | static int create_fixed_stream_quirk(struct snd_usb_audio *chip, |
1da177e4 | 2998 | struct usb_interface *iface, |
86e07d34 | 2999 | const struct snd_usb_audio_quirk *quirk) |
1da177e4 LT |
3000 | { |
3001 | struct audioformat *fp; | |
3002 | struct usb_host_interface *alts; | |
3003 | int stream, err; | |
b4482a4b | 3004 | unsigned *rate_table = NULL; |
1da177e4 | 3005 | |
52978be6 | 3006 | fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL); |
1da177e4 | 3007 | if (! fp) { |
52978be6 | 3008 | snd_printk(KERN_ERR "cannot memdup\n"); |
1da177e4 LT |
3009 | return -ENOMEM; |
3010 | } | |
1da177e4 LT |
3011 | if (fp->nr_rates > 0) { |
3012 | rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL); | |
3013 | if (!rate_table) { | |
3014 | kfree(fp); | |
3015 | return -ENOMEM; | |
3016 | } | |
3017 | memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates); | |
3018 | fp->rate_table = rate_table; | |
3019 | } | |
3020 | ||
3021 | stream = (fp->endpoint & USB_DIR_IN) | |
3022 | ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; | |
3023 | err = add_audio_endpoint(chip, stream, fp); | |
3024 | if (err < 0) { | |
3025 | kfree(fp); | |
3026 | kfree(rate_table); | |
3027 | return err; | |
3028 | } | |
3029 | if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber || | |
3030 | fp->altset_idx >= iface->num_altsetting) { | |
3031 | kfree(fp); | |
3032 | kfree(rate_table); | |
3033 | return -EINVAL; | |
3034 | } | |
3035 | alts = &iface->altsetting[fp->altset_idx]; | |
744b89e5 | 3036 | fp->datainterval = parse_datainterval(chip, alts); |
894dcd78 | 3037 | fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); |
1da177e4 LT |
3038 | usb_set_interface(chip->dev, fp->iface, 0); |
3039 | init_usb_pitch(chip->dev, fp->iface, alts, fp); | |
3040 | init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max); | |
3041 | return 0; | |
3042 | } | |
3043 | ||
3044 | /* | |
3045 | * create a stream for an interface with proper descriptors | |
3046 | */ | |
86e07d34 | 3047 | static int create_standard_audio_quirk(struct snd_usb_audio *chip, |
d1bda045 | 3048 | struct usb_interface *iface, |
86e07d34 | 3049 | const struct snd_usb_audio_quirk *quirk) |
1da177e4 LT |
3050 | { |
3051 | struct usb_host_interface *alts; | |
3052 | struct usb_interface_descriptor *altsd; | |
3053 | int err; | |
3054 | ||
3055 | alts = &iface->altsetting[0]; | |
3056 | altsd = get_iface_desc(alts); | |
d1bda045 | 3057 | err = parse_audio_endpoints(chip, altsd->bInterfaceNumber); |
1da177e4 LT |
3058 | if (err < 0) { |
3059 | snd_printk(KERN_ERR "cannot setup if %d: error %d\n", | |
3060 | altsd->bInterfaceNumber, err); | |
3061 | return err; | |
3062 | } | |
d1bda045 CL |
3063 | /* reset the current interface */ |
3064 | usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0); | |
1da177e4 LT |
3065 | return 0; |
3066 | } | |
3067 | ||
3068 | /* | |
310e0dc0 PLC |
3069 | * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface. |
3070 | * The only way to detect the sample rate is by looking at wMaxPacketSize. | |
1da177e4 | 3071 | */ |
310e0dc0 PLC |
3072 | static int create_uaxx_quirk(struct snd_usb_audio *chip, |
3073 | struct usb_interface *iface, | |
3074 | const struct snd_usb_audio_quirk *quirk) | |
1da177e4 LT |
3075 | { |
3076 | static const struct audioformat ua_format = { | |
3077 | .format = SNDRV_PCM_FORMAT_S24_3LE, | |
3078 | .channels = 2, | |
3079 | .fmt_type = USB_FORMAT_TYPE_I, | |
3080 | .altsetting = 1, | |
3081 | .altset_idx = 1, | |
3082 | .rates = SNDRV_PCM_RATE_CONTINUOUS, | |
3083 | }; | |
3084 | struct usb_host_interface *alts; | |
3085 | struct usb_interface_descriptor *altsd; | |
3086 | struct audioformat *fp; | |
3087 | int stream, err; | |
3088 | ||
310e0dc0 PLC |
3089 | /* both PCM and MIDI interfaces have 2 or more altsettings */ |
3090 | if (iface->num_altsetting < 2) | |
1da177e4 LT |
3091 | return -ENXIO; |
3092 | alts = &iface->altsetting[1]; | |
3093 | altsd = get_iface_desc(alts); | |
3094 | ||
59b3db6c PLC |
3095 | if (altsd->bNumEndpoints == 2) { |
3096 | static const struct snd_usb_midi_endpoint_info ua700_ep = { | |
3097 | .out_cables = 0x0003, | |
3098 | .in_cables = 0x0003 | |
3099 | }; | |
3100 | static const struct snd_usb_audio_quirk ua700_quirk = { | |
3101 | .type = QUIRK_MIDI_FIXED_ENDPOINT, | |
3102 | .data = &ua700_ep | |
3103 | }; | |
3104 | static const struct snd_usb_midi_endpoint_info uaxx_ep = { | |
3105 | .out_cables = 0x0001, | |
3106 | .in_cables = 0x0001 | |
3107 | }; | |
3108 | static const struct snd_usb_audio_quirk uaxx_quirk = { | |
3109 | .type = QUIRK_MIDI_FIXED_ENDPOINT, | |
3110 | .data = &uaxx_ep | |
3111 | }; | |
d82af9f9 CL |
3112 | const struct snd_usb_audio_quirk *quirk = |
3113 | chip->usb_id == USB_ID(0x0582, 0x002b) | |
3114 | ? &ua700_quirk : &uaxx_quirk; | |
3115 | return snd_usbmidi_create(chip->card, iface, | |
3116 | &chip->midi_list, quirk); | |
59b3db6c PLC |
3117 | } |
3118 | ||
1da177e4 LT |
3119 | if (altsd->bNumEndpoints != 1) |
3120 | return -ENXIO; | |
3121 | ||
3122 | fp = kmalloc(sizeof(*fp), GFP_KERNEL); | |
3123 | if (!fp) | |
3124 | return -ENOMEM; | |
3125 | memcpy(fp, &ua_format, sizeof(*fp)); | |
3126 | ||
3127 | fp->iface = altsd->bInterfaceNumber; | |
3128 | fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; | |
3129 | fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; | |
744b89e5 | 3130 | fp->datainterval = 0; |
1da177e4 LT |
3131 | fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); |
3132 | ||
3133 | switch (fp->maxpacksize) { | |
3134 | case 0x120: | |
3135 | fp->rate_max = fp->rate_min = 44100; | |
3136 | break; | |
3137 | case 0x138: | |
3138 | case 0x140: | |
3139 | fp->rate_max = fp->rate_min = 48000; | |
3140 | break; | |
3141 | case 0x258: | |
3142 | case 0x260: | |
3143 | fp->rate_max = fp->rate_min = 96000; | |
3144 | break; | |
3145 | default: | |
3146 | snd_printk(KERN_ERR "unknown sample rate\n"); | |
3147 | kfree(fp); | |
3148 | return -ENXIO; | |
3149 | } | |
3150 | ||
3151 | stream = (fp->endpoint & USB_DIR_IN) | |
3152 | ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; | |
3153 | err = add_audio_endpoint(chip, stream, fp); | |
3154 | if (err < 0) { | |
3155 | kfree(fp); | |
3156 | return err; | |
3157 | } | |
3158 | usb_set_interface(chip->dev, fp->iface, 0); | |
3159 | return 0; | |
3160 | } | |
3161 | ||
3162 | /* | |
3163 | * Create a stream for an Edirol UA-1000 interface. | |
3164 | */ | |
86e07d34 | 3165 | static int create_ua1000_quirk(struct snd_usb_audio *chip, |
854af957 | 3166 | struct usb_interface *iface, |
86e07d34 | 3167 | const struct snd_usb_audio_quirk *quirk) |
1da177e4 LT |
3168 | { |
3169 | static const struct audioformat ua1000_format = { | |
3170 | .format = SNDRV_PCM_FORMAT_S32_LE, | |
3171 | .fmt_type = USB_FORMAT_TYPE_I, | |
3172 | .altsetting = 1, | |
3173 | .altset_idx = 1, | |
3174 | .attributes = 0, | |
3175 | .rates = SNDRV_PCM_RATE_CONTINUOUS, | |
3176 | }; | |
3177 | struct usb_host_interface *alts; | |
3178 | struct usb_interface_descriptor *altsd; | |
3179 | struct audioformat *fp; | |
3180 | int stream, err; | |
3181 | ||
3182 | if (iface->num_altsetting != 2) | |
3183 | return -ENXIO; | |
3184 | alts = &iface->altsetting[1]; | |
3185 | altsd = get_iface_desc(alts); | |
c4a87ef4 | 3186 | if (alts->extralen != 11 || alts->extra[1] != USB_DT_CS_INTERFACE || |
1da177e4 LT |
3187 | altsd->bNumEndpoints != 1) |
3188 | return -ENXIO; | |
3189 | ||
52978be6 | 3190 | fp = kmemdup(&ua1000_format, sizeof(*fp), GFP_KERNEL); |
1da177e4 LT |
3191 | if (!fp) |
3192 | return -ENOMEM; | |
1da177e4 LT |
3193 | |
3194 | fp->channels = alts->extra[4]; | |
3195 | fp->iface = altsd->bInterfaceNumber; | |
3196 | fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; | |
3197 | fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; | |
744b89e5 | 3198 | fp->datainterval = parse_datainterval(chip, alts); |
1da177e4 LT |
3199 | fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); |
3200 | fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]); | |
3201 | ||
3202 | stream = (fp->endpoint & USB_DIR_IN) | |
3203 | ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; | |
3204 | err = add_audio_endpoint(chip, stream, fp); | |
3205 | if (err < 0) { | |
3206 | kfree(fp); | |
3207 | return err; | |
3208 | } | |
3209 | /* FIXME: playback must be synchronized to capture */ | |
3210 | usb_set_interface(chip->dev, fp->iface, 0); | |
3211 | return 0; | |
3212 | } | |
3213 | ||
86e07d34 | 3214 | static int snd_usb_create_quirk(struct snd_usb_audio *chip, |
1da177e4 | 3215 | struct usb_interface *iface, |
86e07d34 | 3216 | const struct snd_usb_audio_quirk *quirk); |
1da177e4 LT |
3217 | |
3218 | /* | |
3219 | * handle the quirks for the contained interfaces | |
3220 | */ | |
86e07d34 | 3221 | static int create_composite_quirk(struct snd_usb_audio *chip, |
1da177e4 | 3222 | struct usb_interface *iface, |
86e07d34 | 3223 | const struct snd_usb_audio_quirk *quirk) |
1da177e4 LT |
3224 | { |
3225 | int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber; | |
3226 | int err; | |
3227 | ||
3228 | for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) { | |
3229 | iface = usb_ifnum_to_if(chip->dev, quirk->ifnum); | |
3230 | if (!iface) | |
3231 | continue; | |
3232 | if (quirk->ifnum != probed_ifnum && | |
3233 | usb_interface_claimed(iface)) | |
3234 | continue; | |
3235 | err = snd_usb_create_quirk(chip, iface, quirk); | |
3236 | if (err < 0) | |
3237 | return err; | |
3238 | if (quirk->ifnum != probed_ifnum) | |
3239 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); | |
3240 | } | |
3241 | return 0; | |
3242 | } | |
3243 | ||
86e07d34 | 3244 | static int ignore_interface_quirk(struct snd_usb_audio *chip, |
854af957 | 3245 | struct usb_interface *iface, |
86e07d34 | 3246 | const struct snd_usb_audio_quirk *quirk) |
854af957 CL |
3247 | { |
3248 | return 0; | |
3249 | } | |
3250 | ||
52a7a583 JG |
3251 | /* |
3252 | * Allow alignment on audio sub-slot (channel samples) rather than | |
3253 | * on audio slots (audio frames) | |
3254 | */ | |
3255 | static int create_align_transfer_quirk(struct snd_usb_audio *chip, | |
3256 | struct usb_interface *iface, | |
3257 | const struct snd_usb_audio_quirk *quirk) | |
3258 | { | |
3259 | chip->txfr_quirk = 1; | |
3260 | return 1; /* Continue with creating streams and mixer */ | |
3261 | } | |
3262 | ||
1da177e4 LT |
3263 | |
3264 | /* | |
3265 | * boot quirks | |
3266 | */ | |
3267 | ||
3268 | #define EXTIGY_FIRMWARE_SIZE_OLD 794 | |
3269 | #define EXTIGY_FIRMWARE_SIZE_NEW 483 | |
3270 | ||
3271 | static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf) | |
3272 | { | |
3273 | struct usb_host_config *config = dev->actconfig; | |
3274 | int err; | |
3275 | ||
3276 | if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD || | |
3277 | le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) { | |
3278 | snd_printdd("sending Extigy boot sequence...\n"); | |
3279 | /* Send message to force it to reconnect with full interface. */ | |
3280 | err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0), | |
3281 | 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000); | |
3282 | if (err < 0) snd_printdd("error sending boot message: %d\n", err); | |
3283 | err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, | |
3284 | &dev->descriptor, sizeof(dev->descriptor)); | |
3285 | config = dev->actconfig; | |
3286 | if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err); | |
3287 | err = usb_reset_configuration(dev); | |
3288 | if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err); | |
3289 | snd_printdd("extigy_boot: new boot length = %d\n", | |
3290 | le16_to_cpu(get_cfg_desc(config)->wTotalLength)); | |
3291 | return -ENODEV; /* quit this anyway */ | |
3292 | } | |
3293 | return 0; | |
3294 | } | |
3295 | ||
3a2f0856 CL |
3296 | static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev) |
3297 | { | |
3a2f0856 CL |
3298 | u8 buf = 1; |
3299 | ||
3300 | snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a, | |
3301 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER, | |
3302 | 0, 0, &buf, 1, 1000); | |
3303 | if (buf == 0) { | |
3304 | snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29, | |
3305 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER, | |
3306 | 1, 2000, NULL, 0, 1000); | |
3307 | return -ENODEV; | |
3308 | } | |
3a2f0856 CL |
3309 | return 0; |
3310 | } | |
3311 | ||
e217e30c SR |
3312 | /* |
3313 | * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely | |
3314 | * documented in the device's data sheet. | |
3315 | */ | |
3316 | static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value) | |
3317 | { | |
3318 | u8 buf[4]; | |
3319 | buf[0] = 0x20; | |
3320 | buf[1] = value & 0xff; | |
3321 | buf[2] = (value >> 8) & 0xff; | |
3322 | buf[3] = reg; | |
3323 | return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION, | |
3324 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT, | |
3325 | 0, 0, &buf, 4, 1000); | |
3326 | } | |
3327 | ||
3328 | static int snd_usb_cm106_boot_quirk(struct usb_device *dev) | |
3329 | { | |
3330 | /* | |
3331 | * Enable line-out driver mode, set headphone source to front | |
3332 | * channels, enable stereo mic. | |
3333 | */ | |
3334 | return snd_usb_cm106_write_int_reg(dev, 2, 0x8004); | |
3335 | } | |
3336 | ||
92a43793 DA |
3337 | /* |
3338 | * C-Media CM6206 is based on CM106 with two additional | |
3339 | * registers that are not documented in the data sheet. | |
3340 | * Values here are chosen based on sniffing USB traffic | |
3341 | * under Windows. | |
3342 | */ | |
3343 | static int snd_usb_cm6206_boot_quirk(struct usb_device *dev) | |
3344 | { | |
3345 | int err, reg; | |
3346 | int val[] = {0x200c, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000}; | |
3347 | ||
3348 | for (reg = 0; reg < ARRAY_SIZE(val); reg++) { | |
3349 | err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]); | |
3350 | if (err < 0) | |
3351 | return err; | |
3352 | } | |
3353 | ||
3354 | return err; | |
3355 | } | |
e217e30c | 3356 | |
d39e82db SA |
3357 | /* |
3358 | * This call will put the synth in "USB send" mode, i.e it will send MIDI | |
3359 | * messages through USB (this is disabled at startup). The synth will | |
3360 | * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB | |
3361 | * sign on its LCD. Values here are chosen based on sniffing USB traffic | |
3362 | * under Windows. | |
3363 | */ | |
3364 | static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev) | |
3365 | { | |
3366 | int err, actual_length; | |
3367 | ||
3368 | /* "midi send" enable */ | |
3369 | static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 }; | |
3370 | ||
3371 | void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL); | |
3372 | if (!buf) | |
3373 | return -ENOMEM; | |
3374 | err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf, | |
3375 | ARRAY_SIZE(seq), &actual_length, 1000); | |
3376 | kfree(buf); | |
3377 | if (err < 0) | |
3378 | return err; | |
3379 | ||
3380 | return 0; | |
3381 | } | |
3382 | ||
e311334e TLM |
3383 | /* |
3384 | * Setup quirks | |
3385 | */ | |
3386 | #define AUDIOPHILE_SET 0x01 /* if set, parse device_setup */ | |
3387 | #define AUDIOPHILE_SET_DTS 0x02 /* if set, enable DTS Digital Output */ | |
3388 | #define AUDIOPHILE_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */ | |
3389 | #define AUDIOPHILE_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */ | |
3390 | #define AUDIOPHILE_SET_DI 0x10 /* if set, enable Digital Input */ | |
3391 | #define AUDIOPHILE_SET_MASK 0x1F /* bit mask for setup value */ | |
3392 | #define AUDIOPHILE_SET_24B_48K_DI 0x19 /* value for 24bits+48KHz+Digital Input */ | |
3393 | #define AUDIOPHILE_SET_24B_48K_NOTDI 0x09 /* value for 24bits+48KHz+No Digital Input */ | |
3394 | #define AUDIOPHILE_SET_16B_48K_DI 0x11 /* value for 16bits+48KHz+Digital Input */ | |
3395 | #define AUDIOPHILE_SET_16B_48K_NOTDI 0x01 /* value for 16bits+48KHz+No Digital Input */ | |
3396 | ||
3397 | static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip, | |
3398 | int iface, int altno) | |
3399 | { | |
f8c78b82 TLM |
3400 | /* Reset ALL ifaces to 0 altsetting. |
3401 | * Call it for every possible altsetting of every interface. | |
3402 | */ | |
3403 | usb_set_interface(chip->dev, iface, 0); | |
3404 | ||
e311334e TLM |
3405 | if (device_setup[chip->index] & AUDIOPHILE_SET) { |
3406 | if ((device_setup[chip->index] & AUDIOPHILE_SET_DTS) | |
3407 | && altno != 6) | |
3408 | return 1; /* skip this altsetting */ | |
3409 | if ((device_setup[chip->index] & AUDIOPHILE_SET_96K) | |
3410 | && altno != 1) | |
3411 | return 1; /* skip this altsetting */ | |
3412 | if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) == | |
3413 | AUDIOPHILE_SET_24B_48K_DI && altno != 2) | |
3414 | return 1; /* skip this altsetting */ | |
3415 | if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) == | |
3416 | AUDIOPHILE_SET_24B_48K_NOTDI && altno != 3) | |
3417 | return 1; /* skip this altsetting */ | |
3418 | if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) == | |
3419 | AUDIOPHILE_SET_16B_48K_DI && altno != 4) | |
3420 | return 1; /* skip this altsetting */ | |
3421 | if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) == | |
3422 | AUDIOPHILE_SET_16B_48K_NOTDI && altno != 5) | |
3423 | return 1; /* skip this altsetting */ | |
3424 | } | |
3425 | return 0; /* keep this altsetting */ | |
3426 | } | |
1da177e4 | 3427 | |
d82af9f9 CL |
3428 | static int create_any_midi_quirk(struct snd_usb_audio *chip, |
3429 | struct usb_interface *intf, | |
3430 | const struct snd_usb_audio_quirk *quirk) | |
3431 | { | |
3432 | return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk); | |
3433 | } | |
3434 | ||
1da177e4 LT |
3435 | /* |
3436 | * audio-interface quirks | |
3437 | * | |
3438 | * returns zero if no standard audio/MIDI parsing is needed. | |
3439 | * returns a postive value if standard audio/midi interfaces are parsed | |
3440 | * after this. | |
3441 | * returns a negative value at error. | |
3442 | */ | |
86e07d34 | 3443 | static int snd_usb_create_quirk(struct snd_usb_audio *chip, |
1da177e4 | 3444 | struct usb_interface *iface, |
86e07d34 | 3445 | const struct snd_usb_audio_quirk *quirk) |
1da177e4 | 3446 | { |
86e07d34 TI |
3447 | typedef int (*quirk_func_t)(struct snd_usb_audio *, struct usb_interface *, |
3448 | const struct snd_usb_audio_quirk *); | |
854af957 CL |
3449 | static const quirk_func_t quirk_funcs[] = { |
3450 | [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk, | |
3451 | [QUIRK_COMPOSITE] = create_composite_quirk, | |
d82af9f9 CL |
3452 | [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk, |
3453 | [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk, | |
3454 | [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk, | |
3455 | [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk, | |
3456 | [QUIRK_MIDI_NOVATION] = create_any_midi_quirk, | |
3457 | [QUIRK_MIDI_FASTLANE] = create_any_midi_quirk, | |
3458 | [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk, | |
3459 | [QUIRK_MIDI_CME] = create_any_midi_quirk, | |
d1bda045 | 3460 | [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk, |
854af957 | 3461 | [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk, |
854af957 | 3462 | [QUIRK_AUDIO_EDIROL_UA1000] = create_ua1000_quirk, |
52a7a583 JG |
3463 | [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk, |
3464 | [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk | |
854af957 CL |
3465 | }; |
3466 | ||
3467 | if (quirk->type < QUIRK_TYPE_COUNT) { | |
3468 | return quirk_funcs[quirk->type](chip, iface, quirk); | |
3469 | } else { | |
1da177e4 LT |
3470 | snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type); |
3471 | return -ENXIO; | |
3472 | } | |
3473 | } | |
3474 | ||
3475 | ||
3476 | /* | |
3477 | * common proc files to show the usb device info | |
3478 | */ | |
86e07d34 | 3479 | static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) |
1da177e4 | 3480 | { |
86e07d34 | 3481 | struct snd_usb_audio *chip = entry->private_data; |
07f51a72 | 3482 | if (!chip->shutdown) |
1da177e4 LT |
3483 | snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum); |
3484 | } | |
3485 | ||
86e07d34 | 3486 | static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) |
1da177e4 | 3487 | { |
86e07d34 | 3488 | struct snd_usb_audio *chip = entry->private_data; |
07f51a72 | 3489 | if (!chip->shutdown) |
1da177e4 | 3490 | snd_iprintf(buffer, "%04x:%04x\n", |
27d10f56 CL |
3491 | USB_ID_VENDOR(chip->usb_id), |
3492 | USB_ID_PRODUCT(chip->usb_id)); | |
1da177e4 LT |
3493 | } |
3494 | ||
86e07d34 | 3495 | static void snd_usb_audio_create_proc(struct snd_usb_audio *chip) |
1da177e4 | 3496 | { |
86e07d34 | 3497 | struct snd_info_entry *entry; |
07f51a72 | 3498 | if (!snd_card_proc_new(chip->card, "usbbus", &entry)) |
bf850204 | 3499 | snd_info_set_text_ops(entry, chip, proc_audio_usbbus_read); |
07f51a72 | 3500 | if (!snd_card_proc_new(chip->card, "usbid", &entry)) |
bf850204 | 3501 | snd_info_set_text_ops(entry, chip, proc_audio_usbid_read); |
1da177e4 LT |
3502 | } |
3503 | ||
3504 | /* | |
3505 | * free the chip instance | |
3506 | * | |
3507 | * here we have to do not much, since pcm and controls are already freed | |
3508 | * | |
3509 | */ | |
3510 | ||
86e07d34 | 3511 | static int snd_usb_audio_free(struct snd_usb_audio *chip) |
1da177e4 LT |
3512 | { |
3513 | kfree(chip); | |
3514 | return 0; | |
3515 | } | |
3516 | ||
86e07d34 | 3517 | static int snd_usb_audio_dev_free(struct snd_device *device) |
1da177e4 | 3518 | { |
86e07d34 | 3519 | struct snd_usb_audio *chip = device->device_data; |
1da177e4 LT |
3520 | return snd_usb_audio_free(chip); |
3521 | } | |
3522 | ||
3523 | ||
3524 | /* | |
3525 | * create a chip instance and set its names. | |
3526 | */ | |
3527 | static int snd_usb_audio_create(struct usb_device *dev, int idx, | |
86e07d34 TI |
3528 | const struct snd_usb_audio_quirk *quirk, |
3529 | struct snd_usb_audio **rchip) | |
1da177e4 | 3530 | { |
86e07d34 TI |
3531 | struct snd_card *card; |
3532 | struct snd_usb_audio *chip; | |
1da177e4 LT |
3533 | int err, len; |
3534 | char component[14]; | |
86e07d34 | 3535 | static struct snd_device_ops ops = { |
1da177e4 LT |
3536 | .dev_free = snd_usb_audio_dev_free, |
3537 | }; | |
3538 | ||
3539 | *rchip = NULL; | |
3540 | ||
076639f6 CL |
3541 | if (snd_usb_get_speed(dev) != USB_SPEED_LOW && |
3542 | snd_usb_get_speed(dev) != USB_SPEED_FULL && | |
1da177e4 LT |
3543 | snd_usb_get_speed(dev) != USB_SPEED_HIGH) { |
3544 | snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev)); | |
3545 | return -ENXIO; | |
3546 | } | |
3547 | ||
bd7dd77c TI |
3548 | err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card); |
3549 | if (err < 0) { | |
1da177e4 | 3550 | snd_printk(KERN_ERR "cannot create card instance %d\n", idx); |
bd7dd77c | 3551 | return err; |
1da177e4 LT |
3552 | } |
3553 | ||
561b220a | 3554 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
1da177e4 LT |
3555 | if (! chip) { |
3556 | snd_card_free(card); | |
3557 | return -ENOMEM; | |
3558 | } | |
3559 | ||
3560 | chip->index = idx; | |
3561 | chip->dev = dev; | |
3562 | chip->card = card; | |
27d10f56 CL |
3563 | chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), |
3564 | le16_to_cpu(dev->descriptor.idProduct)); | |
1da177e4 LT |
3565 | INIT_LIST_HEAD(&chip->pcm_list); |
3566 | INIT_LIST_HEAD(&chip->midi_list); | |
84957a8a | 3567 | INIT_LIST_HEAD(&chip->mixer_list); |
1da177e4 LT |
3568 | |
3569 | if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { | |
3570 | snd_usb_audio_free(chip); | |
3571 | snd_card_free(card); | |
3572 | return err; | |
3573 | } | |
3574 | ||
3575 | strcpy(card->driver, "USB-Audio"); | |
3576 | sprintf(component, "USB%04x:%04x", | |
27d10f56 | 3577 | USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id)); |
1da177e4 LT |
3578 | snd_component_add(card, component); |
3579 | ||
3580 | /* retrieve the device string as shortname */ | |
3581 | if (quirk && quirk->product_name) { | |
3582 | strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname)); | |
3583 | } else { | |
3584 | if (!dev->descriptor.iProduct || | |
3585 | usb_string(dev, dev->descriptor.iProduct, | |
3586 | card->shortname, sizeof(card->shortname)) <= 0) { | |
3587 | /* no name available from anywhere, so use ID */ | |
3588 | sprintf(card->shortname, "USB Device %#04x:%#04x", | |
27d10f56 CL |
3589 | USB_ID_VENDOR(chip->usb_id), |
3590 | USB_ID_PRODUCT(chip->usb_id)); | |
1da177e4 LT |
3591 | } |
3592 | } | |
3593 | ||
3594 | /* retrieve the vendor and device strings as longname */ | |
3595 | if (quirk && quirk->vendor_name) { | |
3596 | len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname)); | |
3597 | } else { | |
3598 | if (dev->descriptor.iManufacturer) | |
3599 | len = usb_string(dev, dev->descriptor.iManufacturer, | |
3600 | card->longname, sizeof(card->longname)); | |
3601 | else | |
3602 | len = 0; | |
3603 | /* we don't really care if there isn't any vendor string */ | |
3604 | } | |
3605 | if (len > 0) | |
3606 | strlcat(card->longname, " ", sizeof(card->longname)); | |
3607 | ||
3608 | strlcat(card->longname, card->shortname, sizeof(card->longname)); | |
3609 | ||
3610 | len = strlcat(card->longname, " at ", sizeof(card->longname)); | |
3611 | ||
3612 | if (len < sizeof(card->longname)) | |
3613 | usb_make_path(dev, card->longname + len, sizeof(card->longname) - len); | |
3614 | ||
3615 | strlcat(card->longname, | |
076639f6 CL |
3616 | snd_usb_get_speed(dev) == USB_SPEED_LOW ? ", low speed" : |
3617 | snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" : | |
3618 | ", high speed", | |
1da177e4 LT |
3619 | sizeof(card->longname)); |
3620 | ||
3621 | snd_usb_audio_create_proc(chip); | |
3622 | ||
1da177e4 LT |
3623 | *rchip = chip; |
3624 | return 0; | |
3625 | } | |
3626 | ||
3627 | ||
3628 | /* | |
3629 | * probe the active usb device | |
3630 | * | |
3631 | * note that this can be called multiple times per a device, when it | |
3632 | * includes multiple audio control interfaces. | |
3633 | * | |
3634 | * thus we check the usb device pointer and creates the card instance | |
3635 | * only at the first time. the successive calls of this function will | |
3636 | * append the pcm interface to the corresponding card. | |
3637 | */ | |
3638 | static void *snd_usb_audio_probe(struct usb_device *dev, | |
3639 | struct usb_interface *intf, | |
3640 | const struct usb_device_id *usb_id) | |
3641 | { | |
86e07d34 | 3642 | const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info; |
1da177e4 | 3643 | int i, err; |
86e07d34 | 3644 | struct snd_usb_audio *chip; |
1da177e4 LT |
3645 | struct usb_host_interface *alts; |
3646 | int ifnum; | |
27d10f56 | 3647 | u32 id; |
1da177e4 LT |
3648 | |
3649 | alts = &intf->altsetting[0]; | |
3650 | ifnum = get_iface_desc(alts)->bInterfaceNumber; | |
27d10f56 CL |
3651 | id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), |
3652 | le16_to_cpu(dev->descriptor.idProduct)); | |
1da177e4 LT |
3653 | if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum) |
3654 | goto __err_val; | |
3655 | ||
3656 | /* SB Extigy needs special boot-up sequence */ | |
3657 | /* if more models come, this will go to the quirk list. */ | |
27d10f56 | 3658 | if (id == USB_ID(0x041e, 0x3000)) { |
1da177e4 LT |
3659 | if (snd_usb_extigy_boot_quirk(dev, intf) < 0) |
3660 | goto __err_val; | |
1da177e4 | 3661 | } |
3a2f0856 CL |
3662 | /* SB Audigy 2 NX needs its own boot-up magic, too */ |
3663 | if (id == USB_ID(0x041e, 0x3020)) { | |
3664 | if (snd_usb_audigy2nx_boot_quirk(dev) < 0) | |
3665 | goto __err_val; | |
3666 | } | |
1da177e4 | 3667 | |
e217e30c SR |
3668 | /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */ |
3669 | if (id == USB_ID(0x10f5, 0x0200)) { | |
3670 | if (snd_usb_cm106_boot_quirk(dev) < 0) | |
3671 | goto __err_val; | |
3672 | } | |
3673 | ||
92a43793 DA |
3674 | /* C-Media CM6206 / CM106-Like Sound Device */ |
3675 | if (id == USB_ID(0x0d8c, 0x0102)) { | |
3676 | if (snd_usb_cm6206_boot_quirk(dev) < 0) | |
3677 | goto __err_val; | |
3678 | } | |
3679 | ||
d39e82db SA |
3680 | /* Access Music VirusTI Desktop */ |
3681 | if (id == USB_ID(0x133e, 0x0815)) { | |
3682 | if (snd_usb_accessmusic_boot_quirk(dev) < 0) | |
3683 | goto __err_val; | |
3684 | } | |
3685 | ||
1da177e4 LT |
3686 | /* |
3687 | * found a config. now register to ALSA | |
3688 | */ | |
3689 | ||
3690 | /* check whether it's already registered */ | |
3691 | chip = NULL; | |
12aa7579 | 3692 | mutex_lock(®ister_mutex); |
1da177e4 LT |
3693 | for (i = 0; i < SNDRV_CARDS; i++) { |
3694 | if (usb_chip[i] && usb_chip[i]->dev == dev) { | |
3695 | if (usb_chip[i]->shutdown) { | |
3696 | snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n"); | |
3697 | goto __error; | |
3698 | } | |
3699 | chip = usb_chip[i]; | |
3700 | break; | |
3701 | } | |
3702 | } | |
3703 | if (! chip) { | |
3704 | /* it's a fresh one. | |
3705 | * now look for an empty slot and create a new card instance | |
3706 | */ | |
1da177e4 LT |
3707 | for (i = 0; i < SNDRV_CARDS; i++) |
3708 | if (enable[i] && ! usb_chip[i] && | |
27d10f56 CL |
3709 | (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) && |
3710 | (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) { | |
1da177e4 LT |
3711 | if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) { |
3712 | goto __error; | |
3713 | } | |
1dcd3ec4 | 3714 | snd_card_set_dev(chip->card, &intf->dev); |
1da177e4 LT |
3715 | break; |
3716 | } | |
07f51a72 PM |
3717 | if (!chip) { |
3718 | printk(KERN_ERR "no available usb audio device\n"); | |
1da177e4 LT |
3719 | goto __error; |
3720 | } | |
3721 | } | |
3722 | ||
52a7a583 | 3723 | chip->txfr_quirk = 0; |
1da177e4 LT |
3724 | err = 1; /* continue */ |
3725 | if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) { | |
3726 | /* need some special handlings */ | |
3727 | if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0) | |
3728 | goto __error; | |
3729 | } | |
3730 | ||
3731 | if (err > 0) { | |
3732 | /* create normal USB audio interfaces */ | |
3733 | if (snd_usb_create_streams(chip, ifnum) < 0 || | |
7a9b8063 | 3734 | snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) { |
1da177e4 LT |
3735 | goto __error; |
3736 | } | |
3737 | } | |
3738 | ||
3739 | /* we are allowed to call snd_card_register() many times */ | |
3740 | if (snd_card_register(chip->card) < 0) { | |
3741 | goto __error; | |
3742 | } | |
3743 | ||
3744 | usb_chip[chip->index] = chip; | |
3745 | chip->num_interfaces++; | |
12aa7579 | 3746 | mutex_unlock(®ister_mutex); |
1da177e4 LT |
3747 | return chip; |
3748 | ||
3749 | __error: | |
3750 | if (chip && !chip->num_interfaces) | |
3751 | snd_card_free(chip->card); | |
12aa7579 | 3752 | mutex_unlock(®ister_mutex); |
1da177e4 LT |
3753 | __err_val: |
3754 | return NULL; | |
3755 | } | |
3756 | ||
3757 | /* | |
3758 | * we need to take care of counter, since disconnection can be called also | |
3759 | * many times as well as usb_audio_probe(). | |
3760 | */ | |
3761 | static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr) | |
3762 | { | |
86e07d34 TI |
3763 | struct snd_usb_audio *chip; |
3764 | struct snd_card *card; | |
1da177e4 LT |
3765 | struct list_head *p; |
3766 | ||
3767 | if (ptr == (void *)-1L) | |
3768 | return; | |
3769 | ||
3770 | chip = ptr; | |
3771 | card = chip->card; | |
12aa7579 | 3772 | mutex_lock(®ister_mutex); |
1da177e4 LT |
3773 | chip->shutdown = 1; |
3774 | chip->num_interfaces--; | |
3775 | if (chip->num_interfaces <= 0) { | |
3776 | snd_card_disconnect(card); | |
3777 | /* release the pcm resources */ | |
3778 | list_for_each(p, &chip->pcm_list) { | |
ee733397 | 3779 | snd_usb_stream_disconnect(p); |
1da177e4 LT |
3780 | } |
3781 | /* release the midi resources */ | |
3782 | list_for_each(p, &chip->midi_list) { | |
ee733397 | 3783 | snd_usbmidi_disconnect(p); |
1da177e4 | 3784 | } |
84957a8a CL |
3785 | /* release mixer resources */ |
3786 | list_for_each(p, &chip->mixer_list) { | |
3787 | snd_usb_mixer_disconnect(p); | |
3788 | } | |
9eb70e68 | 3789 | usb_chip[chip->index] = NULL; |
12aa7579 | 3790 | mutex_unlock(®ister_mutex); |
c461482c | 3791 | snd_card_free_when_closed(card); |
1da177e4 | 3792 | } else { |
12aa7579 | 3793 | mutex_unlock(®ister_mutex); |
1da177e4 LT |
3794 | } |
3795 | } | |
3796 | ||
3797 | /* | |
3798 | * new 2.5 USB kernel API | |
3799 | */ | |
3800 | static int usb_audio_probe(struct usb_interface *intf, | |
3801 | const struct usb_device_id *id) | |
3802 | { | |
3803 | void *chip; | |
3804 | chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id); | |
3805 | if (chip) { | |
f4e9749f | 3806 | usb_set_intfdata(intf, chip); |
1da177e4 LT |
3807 | return 0; |
3808 | } else | |
3809 | return -EIO; | |
3810 | } | |
3811 | ||
3812 | static void usb_audio_disconnect(struct usb_interface *intf) | |
3813 | { | |
3814 | snd_usb_audio_disconnect(interface_to_usbdev(intf), | |
f4e9749f | 3815 | usb_get_intfdata(intf)); |
1da177e4 LT |
3816 | } |
3817 | ||
93521d27 | 3818 | #ifdef CONFIG_PM |
f85bf29c ON |
3819 | static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message) |
3820 | { | |
f4e9749f | 3821 | struct snd_usb_audio *chip = usb_get_intfdata(intf); |
f85bf29c ON |
3822 | struct list_head *p; |
3823 | struct snd_usb_stream *as; | |
3824 | ||
3825 | if (chip == (void *)-1L) | |
3826 | return 0; | |
3827 | ||
3828 | snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot); | |
3829 | if (!chip->num_suspended_intf++) { | |
3830 | list_for_each(p, &chip->pcm_list) { | |
3831 | as = list_entry(p, struct snd_usb_stream, list); | |
3832 | snd_pcm_suspend_all(as->pcm); | |
3833 | } | |
3834 | } | |
3835 | ||
3836 | return 0; | |
3837 | } | |
3838 | ||
3839 | static int usb_audio_resume(struct usb_interface *intf) | |
3840 | { | |
f4e9749f | 3841 | struct snd_usb_audio *chip = usb_get_intfdata(intf); |
f85bf29c ON |
3842 | |
3843 | if (chip == (void *)-1L) | |
3844 | return 0; | |
3845 | if (--chip->num_suspended_intf) | |
3846 | return 0; | |
3847 | /* | |
3848 | * ALSA leaves material resumption to user space | |
3849 | * we just notify | |
3850 | */ | |
3851 | ||
3852 | snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0); | |
3853 | ||
3854 | return 0; | |
3855 | } | |
93521d27 | 3856 | #endif /* CONFIG_PM */ |
1da177e4 LT |
3857 | |
3858 | static int __init snd_usb_audio_init(void) | |
3859 | { | |
f3990e61 | 3860 | if (nrpacks < 1 || nrpacks > MAX_PACKS) { |
1da177e4 LT |
3861 | printk(KERN_WARNING "invalid nrpacks value.\n"); |
3862 | return -EINVAL; | |
3863 | } | |
cf78bbc4 | 3864 | return usb_register(&usb_audio_driver); |
1da177e4 LT |
3865 | } |
3866 | ||
3867 | ||
3868 | static void __exit snd_usb_audio_cleanup(void) | |
3869 | { | |
3870 | usb_deregister(&usb_audio_driver); | |
3871 | } | |
3872 | ||
3873 | module_init(snd_usb_audio_init); | |
3874 | module_exit(snd_usb_audio_cleanup); |