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