blob: 4132522ac90f8c88da831b68f9cbdf777131de8e [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"
Daniel Macke5779992010-03-04 19:46:13 +010035
36/*
37 * return the current pcm pointer. just based on the hwptr_done value.
38 */
39static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
40{
41 struct snd_usb_substream *subs;
42 unsigned int hwptr_done;
43
44 subs = (struct snd_usb_substream *)substream->runtime->private_data;
45 spin_lock(&subs->lock);
46 hwptr_done = subs->hwptr_done;
47 spin_unlock(&subs->lock);
48 return hwptr_done / (substream->runtime->frame_bits >> 3);
49}
50
51/*
52 * find a matching audio format
53 */
54static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
55 unsigned int rate, unsigned int channels)
56{
57 struct list_head *p;
58 struct audioformat *found = NULL;
59 int cur_attr = 0, attr;
60
61 list_for_each(p, &subs->fmt_list) {
62 struct audioformat *fp;
63 fp = list_entry(p, struct audioformat, list);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +010064 if (!(fp->formats & (1uLL << format)))
65 continue;
66 if (fp->channels != channels)
Daniel Macke5779992010-03-04 19:46:13 +010067 continue;
68 if (rate < fp->rate_min || rate > fp->rate_max)
69 continue;
70 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
71 unsigned int i;
72 for (i = 0; i < fp->nr_rates; i++)
73 if (fp->rate_table[i] == rate)
74 break;
75 if (i >= fp->nr_rates)
76 continue;
77 }
78 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
79 if (! found) {
80 found = fp;
81 cur_attr = attr;
82 continue;
83 }
84 /* avoid async out and adaptive in if the other method
85 * supports the same format.
86 * this is a workaround for the case like
87 * M-audio audiophile USB.
88 */
89 if (attr != cur_attr) {
90 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
91 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
92 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
93 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
94 continue;
95 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
96 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
97 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
98 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
99 found = fp;
100 cur_attr = attr;
101 continue;
102 }
103 }
104 /* find the format with the largest max. packet size */
105 if (fp->maxpacksize > found->maxpacksize) {
106 found = fp;
107 cur_attr = attr;
108 }
109 }
110 return found;
111}
112
Daniel Mack767d75a2010-03-04 19:46:17 +0100113static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
114 struct usb_host_interface *alts,
115 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100116{
Daniel Mack767d75a2010-03-04 19:46:17 +0100117 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100118 unsigned int ep;
119 unsigned char data[1];
120 int err;
121
122 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100123
Daniel Mack767d75a2010-03-04 19:46:17 +0100124 data[0] = 1;
125 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
126 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
127 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
128 data, sizeof(data), 1000)) < 0) {
129 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
130 dev->devnum, iface, ep);
131 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100132 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100133
Daniel Macke5779992010-03-04 19:46:13 +0100134 return 0;
135}
136
Daniel Mack92c25612010-05-26 18:11:39 +0200137static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
138 struct usb_host_interface *alts,
139 struct audioformat *fmt)
140{
141 struct usb_device *dev = chip->dev;
142 unsigned char data[1];
143 unsigned int ep;
144 int err;
145
146 ep = get_endpoint(alts, 0)->bEndpointAddress;
147
148 data[0] = 1;
149 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
150 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
151 UAC2_EP_CS_PITCH << 8, 0,
152 data, sizeof(data), 1000)) < 0) {
153 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
154 dev->devnum, iface, fmt->altsetting);
155 return err;
156 }
157
158 return 0;
159}
160
Daniel Mack767d75a2010-03-04 19:46:17 +0100161/*
Daniel Mack92c25612010-05-26 18:11:39 +0200162 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100163 */
164int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
165 struct usb_host_interface *alts,
166 struct audioformat *fmt)
167{
168 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
169
Daniel Mack92c25612010-05-26 18:11:39 +0200170 /* if endpoint doesn't have pitch control, bail out */
171 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
172 return 0;
173
Daniel Mack767d75a2010-03-04 19:46:17 +0100174 switch (altsd->bInterfaceProtocol) {
175 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200176 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100177 return init_pitch_v1(chip, iface, alts, fmt);
178
179 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200180 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100181 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100182}
183
Daniel Macke5779992010-03-04 19:46:13 +0100184/*
185 * find a matching format and set up the interface
186 */
187static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
188{
189 struct usb_device *dev = subs->dev;
190 struct usb_host_interface *alts;
191 struct usb_interface_descriptor *altsd;
192 struct usb_interface *iface;
193 unsigned int ep, attr;
194 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
195 int err;
196
197 iface = usb_ifnum_to_if(dev, fmt->iface);
198 if (WARN_ON(!iface))
199 return -EINVAL;
200 alts = &iface->altsetting[fmt->altset_idx];
201 altsd = get_iface_desc(alts);
202 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
203 return -EINVAL;
204
205 if (fmt == subs->cur_audiofmt)
206 return 0;
207
208 /* close the old interface */
209 if (subs->interface >= 0 && subs->interface != fmt->iface) {
210 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
211 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
212 dev->devnum, fmt->iface, fmt->altsetting);
213 return -EIO;
214 }
215 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +0100216 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100217 }
218
219 /* set interface */
Clemens Ladische11b4e02010-03-04 19:46:14 +0100220 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
Daniel Macke5779992010-03-04 19:46:13 +0100221 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
222 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
223 dev->devnum, fmt->iface, fmt->altsetting);
224 return -EIO;
225 }
226 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
227 subs->interface = fmt->iface;
Clemens Ladische11b4e02010-03-04 19:46:14 +0100228 subs->altset_idx = fmt->altset_idx;
Daniel Macke5779992010-03-04 19:46:13 +0100229 }
230
231 /* create a data pipe */
232 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
233 if (is_playback)
234 subs->datapipe = usb_sndisocpipe(dev, ep);
235 else
236 subs->datapipe = usb_rcvisocpipe(dev, ep);
237 subs->datainterval = fmt->datainterval;
238 subs->syncpipe = subs->syncinterval = 0;
239 subs->maxpacksize = fmt->maxpacksize;
Clemens Ladisch89e1e662010-10-26 17:14:41 +0200240 subs->syncmaxsize = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100241 subs->fill_max = 0;
242
243 /* we need a sync pipe in async OUT or adaptive IN mode */
244 /* check the number of EP, since some devices have broken
245 * descriptors which fool us. if it has only one EP,
246 * assume it as adaptive-out or sync-in.
247 */
248 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
249 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
250 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
251 altsd->bNumEndpoints >= 2) {
252 /* check sync-pipe endpoint */
253 /* ... and check descriptor size before accessing bSynchAddress
254 because there is a version of the SB Audigy 2 NX firmware lacking
255 the audio fields in the endpoint descriptors */
256 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
257 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
258 get_endpoint(alts, 1)->bSynchAddress != 0)) {
259 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
260 dev->devnum, fmt->iface, fmt->altsetting);
261 return -EINVAL;
262 }
263 ep = get_endpoint(alts, 1)->bEndpointAddress;
264 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
265 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
266 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
267 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
268 dev->devnum, fmt->iface, fmt->altsetting);
269 return -EINVAL;
270 }
271 ep &= USB_ENDPOINT_NUMBER_MASK;
272 if (is_playback)
273 subs->syncpipe = usb_rcvisocpipe(dev, ep);
274 else
275 subs->syncpipe = usb_sndisocpipe(dev, ep);
276 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
277 get_endpoint(alts, 1)->bRefresh >= 1 &&
278 get_endpoint(alts, 1)->bRefresh <= 9)
279 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
280 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
281 subs->syncinterval = 1;
282 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
283 get_endpoint(alts, 1)->bInterval <= 16)
284 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
285 else
286 subs->syncinterval = 3;
Clemens Ladisch89e1e662010-10-26 17:14:41 +0200287 subs->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
Daniel Macke5779992010-03-04 19:46:13 +0100288 }
289
290 /* always fill max packet size */
291 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
292 subs->fill_max = 1;
293
Daniel Mack767d75a2010-03-04 19:46:17 +0100294 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100295 return err;
296
297 subs->cur_audiofmt = fmt;
298
299 snd_usb_set_format_quirk(subs, fmt);
300
301#if 0
302 printk(KERN_DEBUG
303 "setting done: format = %d, rate = %d..%d, channels = %d\n",
304 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
305 printk(KERN_DEBUG
306 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
307 subs->datapipe, subs->syncpipe);
308#endif
309
310 return 0;
311}
312
313/*
314 * hw_params callback
315 *
316 * allocate a buffer and set the given audio format.
317 *
318 * so far we use a physically linear buffer although packetize transfer
319 * doesn't need a continuous area.
320 * if sg buffer is supported on the later version of alsa, we'll follow
321 * that.
322 */
323static int snd_usb_hw_params(struct snd_pcm_substream *substream,
324 struct snd_pcm_hw_params *hw_params)
325{
326 struct snd_usb_substream *subs = substream->runtime->private_data;
327 struct audioformat *fmt;
328 unsigned int channels, rate, format;
329 int ret, changed;
330
331 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
332 params_buffer_bytes(hw_params));
333 if (ret < 0)
334 return ret;
335
336 format = params_format(hw_params);
337 rate = params_rate(hw_params);
338 channels = params_channels(hw_params);
339 fmt = find_format(subs, format, rate, channels);
340 if (!fmt) {
341 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
342 format, rate, channels);
343 return -EINVAL;
344 }
345
346 changed = subs->cur_audiofmt != fmt ||
347 subs->period_bytes != params_period_bytes(hw_params) ||
348 subs->cur_rate != rate;
349 if ((ret = set_format(subs, fmt)) < 0)
350 return ret;
351
352 if (subs->cur_rate != rate) {
353 struct usb_host_interface *alts;
354 struct usb_interface *iface;
355 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
356 alts = &iface->altsetting[fmt->altset_idx];
Daniel Mack767d75a2010-03-04 19:46:17 +0100357 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
Daniel Macke5779992010-03-04 19:46:13 +0100358 if (ret < 0)
359 return ret;
360 subs->cur_rate = rate;
361 }
362
363 if (changed) {
364 /* format changed */
365 snd_usb_release_substream_urbs(subs, 0);
366 /* influenced: period_bytes, channels, rate, format, */
367 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
368 params_rate(hw_params),
369 snd_pcm_format_physical_width(params_format(hw_params)) *
370 params_channels(hw_params));
371 }
372
373 return ret;
374}
375
376/*
377 * hw_free callback
378 *
379 * reset the audio format and release the buffer
380 */
381static int snd_usb_hw_free(struct snd_pcm_substream *substream)
382{
383 struct snd_usb_substream *subs = substream->runtime->private_data;
384
385 subs->cur_audiofmt = NULL;
386 subs->cur_rate = 0;
387 subs->period_bytes = 0;
388 if (!subs->stream->chip->shutdown)
389 snd_usb_release_substream_urbs(subs, 0);
390 return snd_pcm_lib_free_vmalloc_buffer(substream);
391}
392
393/*
394 * prepare callback
395 *
396 * only a few subtle things...
397 */
398static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
399{
400 struct snd_pcm_runtime *runtime = substream->runtime;
401 struct snd_usb_substream *subs = runtime->private_data;
402
403 if (! subs->cur_audiofmt) {
404 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
405 return -ENXIO;
406 }
407
408 /* some unit conversions in runtime */
409 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
410 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
411
412 /* reset the pointer */
413 subs->hwptr_done = 0;
414 subs->transfer_done = 0;
415 subs->phase = 0;
416 runtime->delay = 0;
417
418 return snd_usb_substream_prepare(subs, runtime);
419}
420
421static struct snd_pcm_hardware snd_usb_hardware =
422{
423 .info = SNDRV_PCM_INFO_MMAP |
424 SNDRV_PCM_INFO_MMAP_VALID |
425 SNDRV_PCM_INFO_BATCH |
426 SNDRV_PCM_INFO_INTERLEAVED |
427 SNDRV_PCM_INFO_BLOCK_TRANSFER |
428 SNDRV_PCM_INFO_PAUSE,
429 .buffer_bytes_max = 1024 * 1024,
430 .period_bytes_min = 64,
431 .period_bytes_max = 512 * 1024,
432 .periods_min = 2,
433 .periods_max = 1024,
434};
435
436static int hw_check_valid_format(struct snd_usb_substream *subs,
437 struct snd_pcm_hw_params *params,
438 struct audioformat *fp)
439{
440 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
441 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
442 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
443 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100444 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100445 unsigned int ptime;
446
447 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100448 snd_mask_none(&check_fmts);
449 check_fmts.bits[0] = (u32)fp->formats;
450 check_fmts.bits[1] = (u32)(fp->formats >> 32);
451 snd_mask_intersect(&check_fmts, fmts);
452 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100453 hwc_debug(" > check: no supported format %d\n", fp->format);
454 return 0;
455 }
456 /* check the channels */
457 if (fp->channels < ct->min || fp->channels > ct->max) {
458 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
459 return 0;
460 }
461 /* check the rate is within the range */
462 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
463 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
464 return 0;
465 }
466 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
467 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
468 return 0;
469 }
470 /* check whether the period time is >= the data packet interval */
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700471 if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100472 ptime = 125 * (1 << fp->datainterval);
473 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
474 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
475 return 0;
476 }
477 }
478 return 1;
479}
480
481static int hw_rule_rate(struct snd_pcm_hw_params *params,
482 struct snd_pcm_hw_rule *rule)
483{
484 struct snd_usb_substream *subs = rule->private;
485 struct list_head *p;
486 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
487 unsigned int rmin, rmax;
488 int changed;
489
490 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
491 changed = 0;
492 rmin = rmax = 0;
493 list_for_each(p, &subs->fmt_list) {
494 struct audioformat *fp;
495 fp = list_entry(p, struct audioformat, list);
496 if (!hw_check_valid_format(subs, params, fp))
497 continue;
498 if (changed++) {
499 if (rmin > fp->rate_min)
500 rmin = fp->rate_min;
501 if (rmax < fp->rate_max)
502 rmax = fp->rate_max;
503 } else {
504 rmin = fp->rate_min;
505 rmax = fp->rate_max;
506 }
507 }
508
509 if (!changed) {
510 hwc_debug(" --> get empty\n");
511 it->empty = 1;
512 return -EINVAL;
513 }
514
515 changed = 0;
516 if (it->min < rmin) {
517 it->min = rmin;
518 it->openmin = 0;
519 changed = 1;
520 }
521 if (it->max > rmax) {
522 it->max = rmax;
523 it->openmax = 0;
524 changed = 1;
525 }
526 if (snd_interval_checkempty(it)) {
527 it->empty = 1;
528 return -EINVAL;
529 }
530 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
531 return changed;
532}
533
534
535static int hw_rule_channels(struct snd_pcm_hw_params *params,
536 struct snd_pcm_hw_rule *rule)
537{
538 struct snd_usb_substream *subs = rule->private;
539 struct list_head *p;
540 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
541 unsigned int rmin, rmax;
542 int changed;
543
544 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
545 changed = 0;
546 rmin = rmax = 0;
547 list_for_each(p, &subs->fmt_list) {
548 struct audioformat *fp;
549 fp = list_entry(p, struct audioformat, list);
550 if (!hw_check_valid_format(subs, params, fp))
551 continue;
552 if (changed++) {
553 if (rmin > fp->channels)
554 rmin = fp->channels;
555 if (rmax < fp->channels)
556 rmax = fp->channels;
557 } else {
558 rmin = fp->channels;
559 rmax = fp->channels;
560 }
561 }
562
563 if (!changed) {
564 hwc_debug(" --> get empty\n");
565 it->empty = 1;
566 return -EINVAL;
567 }
568
569 changed = 0;
570 if (it->min < rmin) {
571 it->min = rmin;
572 it->openmin = 0;
573 changed = 1;
574 }
575 if (it->max > rmax) {
576 it->max = rmax;
577 it->openmax = 0;
578 changed = 1;
579 }
580 if (snd_interval_checkempty(it)) {
581 it->empty = 1;
582 return -EINVAL;
583 }
584 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
585 return changed;
586}
587
588static int hw_rule_format(struct snd_pcm_hw_params *params,
589 struct snd_pcm_hw_rule *rule)
590{
591 struct snd_usb_substream *subs = rule->private;
592 struct list_head *p;
593 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
594 u64 fbits;
595 u32 oldbits[2];
596 int changed;
597
598 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
599 fbits = 0;
600 list_for_each(p, &subs->fmt_list) {
601 struct audioformat *fp;
602 fp = list_entry(p, struct audioformat, list);
603 if (!hw_check_valid_format(subs, params, fp))
604 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100605 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100606 }
607
608 oldbits[0] = fmt->bits[0];
609 oldbits[1] = fmt->bits[1];
610 fmt->bits[0] &= (u32)fbits;
611 fmt->bits[1] &= (u32)(fbits >> 32);
612 if (!fmt->bits[0] && !fmt->bits[1]) {
613 hwc_debug(" --> get empty\n");
614 return -EINVAL;
615 }
616 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
617 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
618 return changed;
619}
620
621static int hw_rule_period_time(struct snd_pcm_hw_params *params,
622 struct snd_pcm_hw_rule *rule)
623{
624 struct snd_usb_substream *subs = rule->private;
625 struct audioformat *fp;
626 struct snd_interval *it;
627 unsigned char min_datainterval;
628 unsigned int pmin;
629 int changed;
630
631 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
632 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
633 min_datainterval = 0xff;
634 list_for_each_entry(fp, &subs->fmt_list, list) {
635 if (!hw_check_valid_format(subs, params, fp))
636 continue;
637 min_datainterval = min(min_datainterval, fp->datainterval);
638 }
639 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +0200640 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +0100641 it->empty = 1;
642 return -EINVAL;
643 }
644 pmin = 125 * (1 << min_datainterval);
645 changed = 0;
646 if (it->min < pmin) {
647 it->min = pmin;
648 it->openmin = 0;
649 changed = 1;
650 }
651 if (snd_interval_checkempty(it)) {
652 it->empty = 1;
653 return -EINVAL;
654 }
655 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
656 return changed;
657}
658
659/*
660 * If the device supports unusual bit rates, does the request meet these?
661 */
662static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
663 struct snd_usb_substream *subs)
664{
665 struct audioformat *fp;
666 int count = 0, needs_knot = 0;
667 int err;
668
669 list_for_each_entry(fp, &subs->fmt_list, list) {
670 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
671 return 0;
672 count += fp->nr_rates;
673 if (fp->rates & SNDRV_PCM_RATE_KNOT)
674 needs_knot = 1;
675 }
676 if (!needs_knot)
677 return 0;
678
Daniel Macke5779992010-03-04 19:46:13 +0100679 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +0200680 if (!subs->rate_list.list)
681 return -ENOMEM;
682 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +0100683 subs->rate_list.mask = 0;
684 count = 0;
685 list_for_each_entry(fp, &subs->fmt_list, list) {
686 int i;
687 for (i = 0; i < fp->nr_rates; i++)
688 subs->rate_list.list[count++] = fp->rate_table[i];
689 }
690 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
691 &subs->rate_list);
692 if (err < 0)
693 return err;
694
695 return 0;
696}
697
698
699/*
700 * set up the runtime hardware information.
701 */
702
703static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
704{
705 struct list_head *p;
706 unsigned int pt, ptmin;
707 int param_period_time_if_needed;
708 int err;
709
710 runtime->hw.formats = subs->formats;
711
712 runtime->hw.rate_min = 0x7fffffff;
713 runtime->hw.rate_max = 0;
714 runtime->hw.channels_min = 256;
715 runtime->hw.channels_max = 0;
716 runtime->hw.rates = 0;
717 ptmin = UINT_MAX;
718 /* check min/max rates and channels */
719 list_for_each(p, &subs->fmt_list) {
720 struct audioformat *fp;
721 fp = list_entry(p, struct audioformat, list);
722 runtime->hw.rates |= fp->rates;
723 if (runtime->hw.rate_min > fp->rate_min)
724 runtime->hw.rate_min = fp->rate_min;
725 if (runtime->hw.rate_max < fp->rate_max)
726 runtime->hw.rate_max = fp->rate_max;
727 if (runtime->hw.channels_min > fp->channels)
728 runtime->hw.channels_min = fp->channels;
729 if (runtime->hw.channels_max < fp->channels)
730 runtime->hw.channels_max = fp->channels;
731 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
732 /* FIXME: there might be more than one audio formats... */
733 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
734 fp->frame_size;
735 }
736 pt = 125 * (1 << fp->datainterval);
737 ptmin = min(ptmin, pt);
738 }
739
740 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700741 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +0100742 /* full speed devices have fixed data packet interval */
743 ptmin = 1000;
744 if (ptmin == 1000)
745 /* if period time doesn't go below 1 ms, no rules needed */
746 param_period_time_if_needed = -1;
747 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
748 ptmin, UINT_MAX);
749
750 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
751 hw_rule_rate, subs,
752 SNDRV_PCM_HW_PARAM_FORMAT,
753 SNDRV_PCM_HW_PARAM_CHANNELS,
754 param_period_time_if_needed,
755 -1)) < 0)
756 return err;
757 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
758 hw_rule_channels, subs,
759 SNDRV_PCM_HW_PARAM_FORMAT,
760 SNDRV_PCM_HW_PARAM_RATE,
761 param_period_time_if_needed,
762 -1)) < 0)
763 return err;
764 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
765 hw_rule_format, subs,
766 SNDRV_PCM_HW_PARAM_RATE,
767 SNDRV_PCM_HW_PARAM_CHANNELS,
768 param_period_time_if_needed,
769 -1)) < 0)
770 return err;
771 if (param_period_time_if_needed >= 0) {
772 err = snd_pcm_hw_rule_add(runtime, 0,
773 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
774 hw_rule_period_time, subs,
775 SNDRV_PCM_HW_PARAM_FORMAT,
776 SNDRV_PCM_HW_PARAM_CHANNELS,
777 SNDRV_PCM_HW_PARAM_RATE,
778 -1);
779 if (err < 0)
780 return err;
781 }
782 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
783 return err;
784 return 0;
785}
786
787static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
788{
789 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
790 struct snd_pcm_runtime *runtime = substream->runtime;
791 struct snd_usb_substream *subs = &as->substream[direction];
792
793 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +0100794 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100795 runtime->hw = snd_usb_hardware;
796 runtime->private_data = subs;
797 subs->pcm_substream = substream;
798 return setup_hw_info(runtime, subs);
799}
800
801static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
802{
803 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
804 struct snd_usb_substream *subs = &as->substream[direction];
805
806 if (!as->chip->shutdown && subs->interface >= 0) {
807 usb_set_interface(subs->dev, subs->interface, 0);
808 subs->interface = -1;
809 }
810 subs->pcm_substream = NULL;
811 return 0;
812}
813
814static int snd_usb_playback_open(struct snd_pcm_substream *substream)
815{
816 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
817}
818
819static int snd_usb_playback_close(struct snd_pcm_substream *substream)
820{
821 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
822}
823
824static int snd_usb_capture_open(struct snd_pcm_substream *substream)
825{
826 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
827}
828
829static int snd_usb_capture_close(struct snd_pcm_substream *substream)
830{
831 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
832}
833
834static struct snd_pcm_ops snd_usb_playback_ops = {
835 .open = snd_usb_playback_open,
836 .close = snd_usb_playback_close,
837 .ioctl = snd_pcm_lib_ioctl,
838 .hw_params = snd_usb_hw_params,
839 .hw_free = snd_usb_hw_free,
840 .prepare = snd_usb_pcm_prepare,
841 .trigger = snd_usb_substream_playback_trigger,
842 .pointer = snd_usb_pcm_pointer,
843 .page = snd_pcm_lib_get_vmalloc_page,
844 .mmap = snd_pcm_lib_mmap_vmalloc,
845};
846
847static struct snd_pcm_ops snd_usb_capture_ops = {
848 .open = snd_usb_capture_open,
849 .close = snd_usb_capture_close,
850 .ioctl = snd_pcm_lib_ioctl,
851 .hw_params = snd_usb_hw_params,
852 .hw_free = snd_usb_hw_free,
853 .prepare = snd_usb_pcm_prepare,
854 .trigger = snd_usb_substream_capture_trigger,
855 .pointer = snd_usb_pcm_pointer,
856 .page = snd_pcm_lib_get_vmalloc_page,
857 .mmap = snd_pcm_lib_mmap_vmalloc,
858};
859
860void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
861{
862 snd_pcm_set_ops(pcm, stream,
863 stream == SNDRV_PCM_STREAM_PLAYBACK ?
864 &snd_usb_playback_ops : &snd_usb_capture_ops);
865}