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