blob: 0b699ca1957ef8849cf6e87771f6890a3bbdad0a [file] [log] [blame]
Daniel Macke5779992010-03-04 19:46:13 +01001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17#include <linux/init.h>
Stephen Rothwell9966dda2010-03-29 19:01:48 +110018#include <linux/slab.h>
Daniel Macke5779992010-03-04 19:46:13 +010019#include <linux/usb.h>
20#include <linux/usb/audio.h>
Daniel Mack7e847892010-03-11 21:13:20 +010021#include <linux/usb/audio-v2.h>
Daniel Macke5779992010-03-04 19:46:13 +010022
23#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26
27#include "usbaudio.h"
28#include "card.h"
29#include "quirks.h"
30#include "debug.h"
31#include "urb.h"
32#include "helper.h"
33#include "pcm.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020034#include "clock.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010035#include "power.h"
Daniel Macke5779992010-03-04 19:46:13 +010036
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050037/* return the estimated delay based on USB frame counters */
38snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
39 unsigned int rate)
40{
41 int current_frame_number;
42 int frame_diff;
43 int est_delay;
44
45 current_frame_number = usb_get_current_frame_number(subs->dev);
46 /*
47 * HCD implementations use different widths, use lower 8 bits.
48 * The delay will be managed up to 256ms, which is more than
49 * enough
50 */
51 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
52
53 /* Approximation based on number of samples per USB frame (ms),
54 some truncation for 44.1 but the estimate is good enough */
55 est_delay = subs->last_delay - (frame_diff * rate / 1000);
56 if (est_delay < 0)
57 est_delay = 0;
58 return est_delay;
59}
60
Daniel Macke5779992010-03-04 19:46:13 +010061/*
62 * return the current pcm pointer. just based on the hwptr_done value.
63 */
64static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
65{
66 struct snd_usb_substream *subs;
67 unsigned int hwptr_done;
68
69 subs = (struct snd_usb_substream *)substream->runtime->private_data;
70 spin_lock(&subs->lock);
71 hwptr_done = subs->hwptr_done;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050072 substream->runtime->delay = snd_usb_pcm_delay(subs,
73 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010074 spin_unlock(&subs->lock);
75 return hwptr_done / (substream->runtime->frame_bits >> 3);
76}
77
78/*
79 * find a matching audio format
80 */
81static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
82 unsigned int rate, unsigned int channels)
83{
84 struct list_head *p;
85 struct audioformat *found = NULL;
86 int cur_attr = 0, attr;
87
88 list_for_each(p, &subs->fmt_list) {
89 struct audioformat *fp;
90 fp = list_entry(p, struct audioformat, list);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +010091 if (!(fp->formats & (1uLL << format)))
92 continue;
93 if (fp->channels != channels)
Daniel Macke5779992010-03-04 19:46:13 +010094 continue;
95 if (rate < fp->rate_min || rate > fp->rate_max)
96 continue;
97 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
98 unsigned int i;
99 for (i = 0; i < fp->nr_rates; i++)
100 if (fp->rate_table[i] == rate)
101 break;
102 if (i >= fp->nr_rates)
103 continue;
104 }
105 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
106 if (! found) {
107 found = fp;
108 cur_attr = attr;
109 continue;
110 }
111 /* avoid async out and adaptive in if the other method
112 * supports the same format.
113 * this is a workaround for the case like
114 * M-audio audiophile USB.
115 */
116 if (attr != cur_attr) {
117 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
118 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
119 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
120 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
121 continue;
122 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
123 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
124 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
125 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
126 found = fp;
127 cur_attr = attr;
128 continue;
129 }
130 }
131 /* find the format with the largest max. packet size */
132 if (fp->maxpacksize > found->maxpacksize) {
133 found = fp;
134 cur_attr = attr;
135 }
136 }
137 return found;
138}
139
Daniel Mack767d75a2010-03-04 19:46:17 +0100140static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
141 struct usb_host_interface *alts,
142 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100143{
Daniel Mack767d75a2010-03-04 19:46:17 +0100144 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100145 unsigned int ep;
146 unsigned char data[1];
147 int err;
148
149 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100150
Daniel Mack767d75a2010-03-04 19:46:17 +0100151 data[0] = 1;
152 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
153 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
154 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
155 data, sizeof(data), 1000)) < 0) {
156 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
157 dev->devnum, iface, ep);
158 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100159 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100160
Daniel Macke5779992010-03-04 19:46:13 +0100161 return 0;
162}
163
Daniel Mack92c25612010-05-26 18:11:39 +0200164static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
165 struct usb_host_interface *alts,
166 struct audioformat *fmt)
167{
168 struct usb_device *dev = chip->dev;
169 unsigned char data[1];
170 unsigned int ep;
171 int err;
172
173 ep = get_endpoint(alts, 0)->bEndpointAddress;
174
175 data[0] = 1;
176 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
177 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
178 UAC2_EP_CS_PITCH << 8, 0,
179 data, sizeof(data), 1000)) < 0) {
180 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
181 dev->devnum, iface, fmt->altsetting);
182 return err;
183 }
184
185 return 0;
186}
187
Daniel Mack767d75a2010-03-04 19:46:17 +0100188/*
Daniel Mack92c25612010-05-26 18:11:39 +0200189 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100190 */
191int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
192 struct usb_host_interface *alts,
193 struct audioformat *fmt)
194{
195 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
196
Daniel Mack92c25612010-05-26 18:11:39 +0200197 /* if endpoint doesn't have pitch control, bail out */
198 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
199 return 0;
200
Daniel Mack767d75a2010-03-04 19:46:17 +0100201 switch (altsd->bInterfaceProtocol) {
202 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200203 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100204 return init_pitch_v1(chip, iface, alts, fmt);
205
206 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200207 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100208 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100209}
210
Daniel Macke5779992010-03-04 19:46:13 +0100211/*
212 * find a matching format and set up the interface
213 */
214static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
215{
216 struct usb_device *dev = subs->dev;
217 struct usb_host_interface *alts;
218 struct usb_interface_descriptor *altsd;
219 struct usb_interface *iface;
220 unsigned int ep, attr;
221 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
222 int err;
223
224 iface = usb_ifnum_to_if(dev, fmt->iface);
225 if (WARN_ON(!iface))
226 return -EINVAL;
227 alts = &iface->altsetting[fmt->altset_idx];
228 altsd = get_iface_desc(alts);
229 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
230 return -EINVAL;
231
232 if (fmt == subs->cur_audiofmt)
233 return 0;
234
235 /* close the old interface */
236 if (subs->interface >= 0 && subs->interface != fmt->iface) {
237 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
238 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
239 dev->devnum, fmt->iface, fmt->altsetting);
240 return -EIO;
241 }
242 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +0100243 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100244 }
245
246 /* set interface */
Clemens Ladische11b4e02010-03-04 19:46:14 +0100247 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
Daniel Macke5779992010-03-04 19:46:13 +0100248 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
249 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
250 dev->devnum, fmt->iface, fmt->altsetting);
251 return -EIO;
252 }
253 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
254 subs->interface = fmt->iface;
Clemens Ladische11b4e02010-03-04 19:46:14 +0100255 subs->altset_idx = fmt->altset_idx;
Daniel Macke5779992010-03-04 19:46:13 +0100256 }
257
258 /* create a data pipe */
259 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
260 if (is_playback)
261 subs->datapipe = usb_sndisocpipe(dev, ep);
262 else
263 subs->datapipe = usb_rcvisocpipe(dev, ep);
264 subs->datainterval = fmt->datainterval;
265 subs->syncpipe = subs->syncinterval = 0;
266 subs->maxpacksize = fmt->maxpacksize;
Clemens Ladisch89e1e662010-10-26 17:14:41 +0200267 subs->syncmaxsize = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100268 subs->fill_max = 0;
269
270 /* we need a sync pipe in async OUT or adaptive IN mode */
271 /* check the number of EP, since some devices have broken
272 * descriptors which fool us. if it has only one EP,
273 * assume it as adaptive-out or sync-in.
274 */
275 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
276 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
277 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
278 altsd->bNumEndpoints >= 2) {
279 /* check sync-pipe endpoint */
280 /* ... and check descriptor size before accessing bSynchAddress
281 because there is a version of the SB Audigy 2 NX firmware lacking
282 the audio fields in the endpoint descriptors */
283 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
284 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
285 get_endpoint(alts, 1)->bSynchAddress != 0)) {
286 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
287 dev->devnum, fmt->iface, fmt->altsetting);
288 return -EINVAL;
289 }
290 ep = get_endpoint(alts, 1)->bEndpointAddress;
291 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
292 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
293 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
294 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
295 dev->devnum, fmt->iface, fmt->altsetting);
296 return -EINVAL;
297 }
298 ep &= USB_ENDPOINT_NUMBER_MASK;
299 if (is_playback)
300 subs->syncpipe = usb_rcvisocpipe(dev, ep);
301 else
302 subs->syncpipe = usb_sndisocpipe(dev, ep);
303 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
304 get_endpoint(alts, 1)->bRefresh >= 1 &&
305 get_endpoint(alts, 1)->bRefresh <= 9)
306 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
307 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
308 subs->syncinterval = 1;
309 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
310 get_endpoint(alts, 1)->bInterval <= 16)
311 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
312 else
313 subs->syncinterval = 3;
Clemens Ladisch89e1e662010-10-26 17:14:41 +0200314 subs->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
Daniel Macke5779992010-03-04 19:46:13 +0100315 }
316
317 /* always fill max packet size */
318 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
319 subs->fill_max = 1;
320
Daniel Mack767d75a2010-03-04 19:46:17 +0100321 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100322 return err;
323
324 subs->cur_audiofmt = fmt;
325
326 snd_usb_set_format_quirk(subs, fmt);
327
328#if 0
329 printk(KERN_DEBUG
330 "setting done: format = %d, rate = %d..%d, channels = %d\n",
331 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
332 printk(KERN_DEBUG
333 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
334 subs->datapipe, subs->syncpipe);
335#endif
336
337 return 0;
338}
339
340/*
341 * hw_params callback
342 *
343 * allocate a buffer and set the given audio format.
344 *
345 * so far we use a physically linear buffer although packetize transfer
346 * doesn't need a continuous area.
347 * if sg buffer is supported on the later version of alsa, we'll follow
348 * that.
349 */
350static int snd_usb_hw_params(struct snd_pcm_substream *substream,
351 struct snd_pcm_hw_params *hw_params)
352{
353 struct snd_usb_substream *subs = substream->runtime->private_data;
354 struct audioformat *fmt;
355 unsigned int channels, rate, format;
356 int ret, changed;
357
358 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
359 params_buffer_bytes(hw_params));
360 if (ret < 0)
361 return ret;
362
363 format = params_format(hw_params);
364 rate = params_rate(hw_params);
365 channels = params_channels(hw_params);
366 fmt = find_format(subs, format, rate, channels);
367 if (!fmt) {
368 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
369 format, rate, channels);
370 return -EINVAL;
371 }
372
373 changed = subs->cur_audiofmt != fmt ||
374 subs->period_bytes != params_period_bytes(hw_params) ||
375 subs->cur_rate != rate;
376 if ((ret = set_format(subs, fmt)) < 0)
377 return ret;
378
379 if (subs->cur_rate != rate) {
380 struct usb_host_interface *alts;
381 struct usb_interface *iface;
382 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
383 alts = &iface->altsetting[fmt->altset_idx];
Daniel Mack767d75a2010-03-04 19:46:17 +0100384 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
Daniel Macke5779992010-03-04 19:46:13 +0100385 if (ret < 0)
386 return ret;
387 subs->cur_rate = rate;
388 }
389
390 if (changed) {
Takashi Iwai382225e2011-02-22 10:21:18 +0100391 mutex_lock(&subs->stream->chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100392 /* format changed */
393 snd_usb_release_substream_urbs(subs, 0);
394 /* influenced: period_bytes, channels, rate, format, */
395 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
396 params_rate(hw_params),
397 snd_pcm_format_physical_width(params_format(hw_params)) *
398 params_channels(hw_params));
Takashi Iwai382225e2011-02-22 10:21:18 +0100399 mutex_unlock(&subs->stream->chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100400 }
401
402 return ret;
403}
404
405/*
406 * hw_free callback
407 *
408 * reset the audio format and release the buffer
409 */
410static int snd_usb_hw_free(struct snd_pcm_substream *substream)
411{
412 struct snd_usb_substream *subs = substream->runtime->private_data;
413
414 subs->cur_audiofmt = NULL;
415 subs->cur_rate = 0;
416 subs->period_bytes = 0;
Takashi Iwai382225e2011-02-22 10:21:18 +0100417 mutex_lock(&subs->stream->chip->shutdown_mutex);
418 snd_usb_release_substream_urbs(subs, 0);
419 mutex_unlock(&subs->stream->chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100420 return snd_pcm_lib_free_vmalloc_buffer(substream);
421}
422
423/*
424 * prepare callback
425 *
426 * only a few subtle things...
427 */
428static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
429{
430 struct snd_pcm_runtime *runtime = substream->runtime;
431 struct snd_usb_substream *subs = runtime->private_data;
432
433 if (! subs->cur_audiofmt) {
434 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
435 return -ENXIO;
436 }
437
438 /* some unit conversions in runtime */
439 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
440 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
441
442 /* reset the pointer */
443 subs->hwptr_done = 0;
444 subs->transfer_done = 0;
445 subs->phase = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500446 subs->last_delay = 0;
447 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100448 runtime->delay = 0;
449
450 return snd_usb_substream_prepare(subs, runtime);
451}
452
453static struct snd_pcm_hardware snd_usb_hardware =
454{
455 .info = SNDRV_PCM_INFO_MMAP |
456 SNDRV_PCM_INFO_MMAP_VALID |
457 SNDRV_PCM_INFO_BATCH |
458 SNDRV_PCM_INFO_INTERLEAVED |
459 SNDRV_PCM_INFO_BLOCK_TRANSFER |
460 SNDRV_PCM_INFO_PAUSE,
461 .buffer_bytes_max = 1024 * 1024,
462 .period_bytes_min = 64,
463 .period_bytes_max = 512 * 1024,
464 .periods_min = 2,
465 .periods_max = 1024,
466};
467
468static int hw_check_valid_format(struct snd_usb_substream *subs,
469 struct snd_pcm_hw_params *params,
470 struct audioformat *fp)
471{
472 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
473 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
474 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
475 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100476 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100477 unsigned int ptime;
478
479 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100480 snd_mask_none(&check_fmts);
481 check_fmts.bits[0] = (u32)fp->formats;
482 check_fmts.bits[1] = (u32)(fp->formats >> 32);
483 snd_mask_intersect(&check_fmts, fmts);
484 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100485 hwc_debug(" > check: no supported format %d\n", fp->format);
486 return 0;
487 }
488 /* check the channels */
489 if (fp->channels < ct->min || fp->channels > ct->max) {
490 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
491 return 0;
492 }
493 /* check the rate is within the range */
494 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
495 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
496 return 0;
497 }
498 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
499 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
500 return 0;
501 }
502 /* check whether the period time is >= the data packet interval */
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700503 if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100504 ptime = 125 * (1 << fp->datainterval);
505 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
506 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
507 return 0;
508 }
509 }
510 return 1;
511}
512
513static int hw_rule_rate(struct snd_pcm_hw_params *params,
514 struct snd_pcm_hw_rule *rule)
515{
516 struct snd_usb_substream *subs = rule->private;
517 struct list_head *p;
518 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
519 unsigned int rmin, rmax;
520 int changed;
521
522 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
523 changed = 0;
524 rmin = rmax = 0;
525 list_for_each(p, &subs->fmt_list) {
526 struct audioformat *fp;
527 fp = list_entry(p, struct audioformat, list);
528 if (!hw_check_valid_format(subs, params, fp))
529 continue;
530 if (changed++) {
531 if (rmin > fp->rate_min)
532 rmin = fp->rate_min;
533 if (rmax < fp->rate_max)
534 rmax = fp->rate_max;
535 } else {
536 rmin = fp->rate_min;
537 rmax = fp->rate_max;
538 }
539 }
540
541 if (!changed) {
542 hwc_debug(" --> get empty\n");
543 it->empty = 1;
544 return -EINVAL;
545 }
546
547 changed = 0;
548 if (it->min < rmin) {
549 it->min = rmin;
550 it->openmin = 0;
551 changed = 1;
552 }
553 if (it->max > rmax) {
554 it->max = rmax;
555 it->openmax = 0;
556 changed = 1;
557 }
558 if (snd_interval_checkempty(it)) {
559 it->empty = 1;
560 return -EINVAL;
561 }
562 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
563 return changed;
564}
565
566
567static int hw_rule_channels(struct snd_pcm_hw_params *params,
568 struct snd_pcm_hw_rule *rule)
569{
570 struct snd_usb_substream *subs = rule->private;
571 struct list_head *p;
572 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
573 unsigned int rmin, rmax;
574 int changed;
575
576 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
577 changed = 0;
578 rmin = rmax = 0;
579 list_for_each(p, &subs->fmt_list) {
580 struct audioformat *fp;
581 fp = list_entry(p, struct audioformat, list);
582 if (!hw_check_valid_format(subs, params, fp))
583 continue;
584 if (changed++) {
585 if (rmin > fp->channels)
586 rmin = fp->channels;
587 if (rmax < fp->channels)
588 rmax = fp->channels;
589 } else {
590 rmin = fp->channels;
591 rmax = fp->channels;
592 }
593 }
594
595 if (!changed) {
596 hwc_debug(" --> get empty\n");
597 it->empty = 1;
598 return -EINVAL;
599 }
600
601 changed = 0;
602 if (it->min < rmin) {
603 it->min = rmin;
604 it->openmin = 0;
605 changed = 1;
606 }
607 if (it->max > rmax) {
608 it->max = rmax;
609 it->openmax = 0;
610 changed = 1;
611 }
612 if (snd_interval_checkempty(it)) {
613 it->empty = 1;
614 return -EINVAL;
615 }
616 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
617 return changed;
618}
619
620static int hw_rule_format(struct snd_pcm_hw_params *params,
621 struct snd_pcm_hw_rule *rule)
622{
623 struct snd_usb_substream *subs = rule->private;
624 struct list_head *p;
625 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
626 u64 fbits;
627 u32 oldbits[2];
628 int changed;
629
630 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
631 fbits = 0;
632 list_for_each(p, &subs->fmt_list) {
633 struct audioformat *fp;
634 fp = list_entry(p, struct audioformat, list);
635 if (!hw_check_valid_format(subs, params, fp))
636 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100637 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100638 }
639
640 oldbits[0] = fmt->bits[0];
641 oldbits[1] = fmt->bits[1];
642 fmt->bits[0] &= (u32)fbits;
643 fmt->bits[1] &= (u32)(fbits >> 32);
644 if (!fmt->bits[0] && !fmt->bits[1]) {
645 hwc_debug(" --> get empty\n");
646 return -EINVAL;
647 }
648 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
649 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
650 return changed;
651}
652
653static int hw_rule_period_time(struct snd_pcm_hw_params *params,
654 struct snd_pcm_hw_rule *rule)
655{
656 struct snd_usb_substream *subs = rule->private;
657 struct audioformat *fp;
658 struct snd_interval *it;
659 unsigned char min_datainterval;
660 unsigned int pmin;
661 int changed;
662
663 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
664 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
665 min_datainterval = 0xff;
666 list_for_each_entry(fp, &subs->fmt_list, list) {
667 if (!hw_check_valid_format(subs, params, fp))
668 continue;
669 min_datainterval = min(min_datainterval, fp->datainterval);
670 }
671 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +0200672 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +0100673 it->empty = 1;
674 return -EINVAL;
675 }
676 pmin = 125 * (1 << min_datainterval);
677 changed = 0;
678 if (it->min < pmin) {
679 it->min = pmin;
680 it->openmin = 0;
681 changed = 1;
682 }
683 if (snd_interval_checkempty(it)) {
684 it->empty = 1;
685 return -EINVAL;
686 }
687 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
688 return changed;
689}
690
691/*
692 * If the device supports unusual bit rates, does the request meet these?
693 */
694static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
695 struct snd_usb_substream *subs)
696{
697 struct audioformat *fp;
698 int count = 0, needs_knot = 0;
699 int err;
700
701 list_for_each_entry(fp, &subs->fmt_list, list) {
702 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
703 return 0;
704 count += fp->nr_rates;
705 if (fp->rates & SNDRV_PCM_RATE_KNOT)
706 needs_knot = 1;
707 }
708 if (!needs_knot)
709 return 0;
710
Daniel Macke5779992010-03-04 19:46:13 +0100711 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +0200712 if (!subs->rate_list.list)
713 return -ENOMEM;
714 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +0100715 subs->rate_list.mask = 0;
716 count = 0;
717 list_for_each_entry(fp, &subs->fmt_list, list) {
718 int i;
719 for (i = 0; i < fp->nr_rates; i++)
720 subs->rate_list.list[count++] = fp->rate_table[i];
721 }
722 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
723 &subs->rate_list);
724 if (err < 0)
725 return err;
726
727 return 0;
728}
729
730
731/*
732 * set up the runtime hardware information.
733 */
734
735static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
736{
737 struct list_head *p;
738 unsigned int pt, ptmin;
739 int param_period_time_if_needed;
740 int err;
741
742 runtime->hw.formats = subs->formats;
743
744 runtime->hw.rate_min = 0x7fffffff;
745 runtime->hw.rate_max = 0;
746 runtime->hw.channels_min = 256;
747 runtime->hw.channels_max = 0;
748 runtime->hw.rates = 0;
749 ptmin = UINT_MAX;
750 /* check min/max rates and channels */
751 list_for_each(p, &subs->fmt_list) {
752 struct audioformat *fp;
753 fp = list_entry(p, struct audioformat, list);
754 runtime->hw.rates |= fp->rates;
755 if (runtime->hw.rate_min > fp->rate_min)
756 runtime->hw.rate_min = fp->rate_min;
757 if (runtime->hw.rate_max < fp->rate_max)
758 runtime->hw.rate_max = fp->rate_max;
759 if (runtime->hw.channels_min > fp->channels)
760 runtime->hw.channels_min = fp->channels;
761 if (runtime->hw.channels_max < fp->channels)
762 runtime->hw.channels_max = fp->channels;
763 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
764 /* FIXME: there might be more than one audio formats... */
765 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
766 fp->frame_size;
767 }
768 pt = 125 * (1 << fp->datainterval);
769 ptmin = min(ptmin, pt);
770 }
Oliver Neukum88a85162011-03-11 14:51:12 +0100771 err = snd_usb_autoresume(subs->stream->chip);
772 if (err < 0)
773 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100774
775 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700776 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +0100777 /* full speed devices have fixed data packet interval */
778 ptmin = 1000;
779 if (ptmin == 1000)
780 /* if period time doesn't go below 1 ms, no rules needed */
781 param_period_time_if_needed = -1;
782 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
783 ptmin, UINT_MAX);
784
785 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
786 hw_rule_rate, subs,
787 SNDRV_PCM_HW_PARAM_FORMAT,
788 SNDRV_PCM_HW_PARAM_CHANNELS,
789 param_period_time_if_needed,
790 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100791 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100792 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
793 hw_rule_channels, subs,
794 SNDRV_PCM_HW_PARAM_FORMAT,
795 SNDRV_PCM_HW_PARAM_RATE,
796 param_period_time_if_needed,
797 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100798 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100799 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
800 hw_rule_format, subs,
801 SNDRV_PCM_HW_PARAM_RATE,
802 SNDRV_PCM_HW_PARAM_CHANNELS,
803 param_period_time_if_needed,
804 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100805 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100806 if (param_period_time_if_needed >= 0) {
807 err = snd_pcm_hw_rule_add(runtime, 0,
808 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
809 hw_rule_period_time, subs,
810 SNDRV_PCM_HW_PARAM_FORMAT,
811 SNDRV_PCM_HW_PARAM_CHANNELS,
812 SNDRV_PCM_HW_PARAM_RATE,
813 -1);
814 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100815 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100816 }
817 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +0100818 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +0100819 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +0100820
821rep_err:
822 snd_usb_autosuspend(subs->stream->chip);
823 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100824}
825
826static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
827{
828 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
829 struct snd_pcm_runtime *runtime = substream->runtime;
830 struct snd_usb_substream *subs = &as->substream[direction];
831
832 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +0100833 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100834 runtime->hw = snd_usb_hardware;
835 runtime->private_data = subs;
836 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +0100837 /* runtime PM is also done there */
Daniel Macke5779992010-03-04 19:46:13 +0100838 return setup_hw_info(runtime, subs);
839}
840
841static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
842{
843 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
844 struct snd_usb_substream *subs = &as->substream[direction];
845
846 if (!as->chip->shutdown && subs->interface >= 0) {
847 usb_set_interface(subs->dev, subs->interface, 0);
848 subs->interface = -1;
849 }
850 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +0100851 snd_usb_autosuspend(subs->stream->chip);
Daniel Macke5779992010-03-04 19:46:13 +0100852 return 0;
853}
854
855static int snd_usb_playback_open(struct snd_pcm_substream *substream)
856{
857 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
858}
859
860static int snd_usb_playback_close(struct snd_pcm_substream *substream)
861{
862 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
863}
864
865static int snd_usb_capture_open(struct snd_pcm_substream *substream)
866{
867 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
868}
869
870static int snd_usb_capture_close(struct snd_pcm_substream *substream)
871{
872 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
873}
874
875static struct snd_pcm_ops snd_usb_playback_ops = {
876 .open = snd_usb_playback_open,
877 .close = snd_usb_playback_close,
878 .ioctl = snd_pcm_lib_ioctl,
879 .hw_params = snd_usb_hw_params,
880 .hw_free = snd_usb_hw_free,
881 .prepare = snd_usb_pcm_prepare,
882 .trigger = snd_usb_substream_playback_trigger,
883 .pointer = snd_usb_pcm_pointer,
884 .page = snd_pcm_lib_get_vmalloc_page,
885 .mmap = snd_pcm_lib_mmap_vmalloc,
886};
887
888static struct snd_pcm_ops snd_usb_capture_ops = {
889 .open = snd_usb_capture_open,
890 .close = snd_usb_capture_close,
891 .ioctl = snd_pcm_lib_ioctl,
892 .hw_params = snd_usb_hw_params,
893 .hw_free = snd_usb_hw_free,
894 .prepare = snd_usb_pcm_prepare,
895 .trigger = snd_usb_substream_capture_trigger,
896 .pointer = snd_usb_pcm_pointer,
897 .page = snd_pcm_lib_get_vmalloc_page,
898 .mmap = snd_pcm_lib_mmap_vmalloc,
899};
900
901void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
902{
903 snd_pcm_set_ops(pcm, stream,
904 stream == SNDRV_PCM_STREAM_PLAYBACK ?
905 &snd_usb_playback_ops : &snd_usb_capture_ops);
906}