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.
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.
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
17 #include <linux/init.h>
18 #include <linux/usb.h>
19 #include <linux/usb/audio.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
34 * return the current pcm pointer. just based on the hwptr_done value.
36 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
38 struct snd_usb_substream *subs;
39 unsigned int hwptr_done;
41 subs = (struct snd_usb_substream *)substream->runtime->private_data;
42 spin_lock(&subs->lock);
43 hwptr_done = subs->hwptr_done;
44 spin_unlock(&subs->lock);
45 return hwptr_done / (substream->runtime->frame_bits >> 3);
49 * find a matching audio format
51 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
52 unsigned int rate, unsigned int channels)
55 struct audioformat *found = NULL;
56 int cur_attr = 0, attr;
58 list_for_each(p, &subs->fmt_list) {
59 struct audioformat *fp;
60 fp = list_entry(p, struct audioformat, list);
61 if (!(fp->formats & (1uLL << format)))
63 if (fp->channels != channels)
65 if (rate < fp->rate_min || rate > fp->rate_max)
67 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
69 for (i = 0; i < fp->nr_rates; i++)
70 if (fp->rate_table[i] == rate)
72 if (i >= fp->nr_rates)
75 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
81 /* avoid async out and adaptive in if the other method
82 * supports the same format.
83 * this is a workaround for the case like
84 * M-audio audiophile USB.
86 if (attr != cur_attr) {
87 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
88 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
89 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
90 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
92 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
93 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
94 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
95 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
101 /* find the format with the largest max. packet size */
102 if (fp->maxpacksize > found->maxpacksize) {
110 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
111 struct usb_host_interface *alts,
112 struct audioformat *fmt)
114 struct usb_device *dev = chip->dev;
116 unsigned char data[1];
119 ep = get_endpoint(alts, 0)->bEndpointAddress;
121 /* if endpoint doesn't have pitch control, bail out */
122 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
126 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
127 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
128 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
129 data, sizeof(data), 1000)) < 0) {
130 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
131 dev->devnum, iface, ep);
139 * initialize the picth control and sample rate
141 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
142 struct usb_host_interface *alts,
143 struct audioformat *fmt)
145 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
147 switch (altsd->bInterfaceProtocol) {
149 return init_pitch_v1(chip, iface, alts, fmt);
152 /* not implemented yet */
159 static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
160 struct usb_host_interface *alts,
161 struct audioformat *fmt, int rate)
163 struct usb_device *dev = chip->dev;
165 unsigned char data[3];
168 ep = get_endpoint(alts, 0)->bEndpointAddress;
169 /* if endpoint doesn't have sampling rate control, bail out */
170 if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE)) {
171 snd_printk(KERN_WARNING "%d:%d:%d: endpoint lacks sample rate attribute bit, cannot set.\n",
172 dev->devnum, iface, fmt->altsetting);
178 data[2] = rate >> 16;
179 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
180 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
181 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
182 data, sizeof(data), 1000)) < 0) {
183 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
184 dev->devnum, iface, fmt->altsetting, rate, ep);
187 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
188 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
189 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
190 data, sizeof(data), 1000)) < 0) {
191 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
192 dev->devnum, iface, fmt->altsetting, ep);
193 return 0; /* some devices don't support reading */
195 crate = data[0] | (data[1] << 8) | (data[2] << 16);
197 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
198 // runtime->rate = crate;
204 static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
205 struct usb_host_interface *alts,
206 struct audioformat *fmt, int rate)
208 struct usb_device *dev = chip->dev;
209 unsigned char data[4];
214 data[2] = rate >> 16;
215 data[3] = rate >> 24;
216 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
217 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
218 0x0100, chip->clock_id << 8,
219 data, sizeof(data), 1000)) < 0) {
220 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2)\n",
221 dev->devnum, iface, fmt->altsetting, rate);
224 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
225 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
226 0x0100, chip->clock_id << 8,
227 data, sizeof(data), 1000)) < 0) {
228 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2)\n",
229 dev->devnum, iface, fmt->altsetting);
232 crate = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
234 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
239 int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
240 struct usb_host_interface *alts,
241 struct audioformat *fmt, int rate)
243 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
245 switch (altsd->bInterfaceProtocol) {
247 return set_sample_rate_v1(chip, iface, alts, fmt, rate);
250 return set_sample_rate_v2(chip, iface, alts, fmt, rate);
257 * find a matching format and set up the interface
259 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
261 struct usb_device *dev = subs->dev;
262 struct usb_host_interface *alts;
263 struct usb_interface_descriptor *altsd;
264 struct usb_interface *iface;
265 unsigned int ep, attr;
266 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
269 iface = usb_ifnum_to_if(dev, fmt->iface);
272 alts = &iface->altsetting[fmt->altset_idx];
273 altsd = get_iface_desc(alts);
274 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
277 if (fmt == subs->cur_audiofmt)
280 /* close the old interface */
281 if (subs->interface >= 0 && subs->interface != fmt->iface) {
282 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
283 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
284 dev->devnum, fmt->iface, fmt->altsetting);
287 subs->interface = -1;
288 subs->altset_idx = 0;
292 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
293 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
294 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
295 dev->devnum, fmt->iface, fmt->altsetting);
298 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
299 subs->interface = fmt->iface;
300 subs->altset_idx = fmt->altset_idx;
303 /* create a data pipe */
304 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
306 subs->datapipe = usb_sndisocpipe(dev, ep);
308 subs->datapipe = usb_rcvisocpipe(dev, ep);
309 subs->datainterval = fmt->datainterval;
310 subs->syncpipe = subs->syncinterval = 0;
311 subs->maxpacksize = fmt->maxpacksize;
314 /* we need a sync pipe in async OUT or adaptive IN mode */
315 /* check the number of EP, since some devices have broken
316 * descriptors which fool us. if it has only one EP,
317 * assume it as adaptive-out or sync-in.
319 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
320 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
321 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
322 altsd->bNumEndpoints >= 2) {
323 /* check sync-pipe endpoint */
324 /* ... and check descriptor size before accessing bSynchAddress
325 because there is a version of the SB Audigy 2 NX firmware lacking
326 the audio fields in the endpoint descriptors */
327 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
328 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
329 get_endpoint(alts, 1)->bSynchAddress != 0)) {
330 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
331 dev->devnum, fmt->iface, fmt->altsetting);
334 ep = get_endpoint(alts, 1)->bEndpointAddress;
335 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
336 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
337 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
338 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
339 dev->devnum, fmt->iface, fmt->altsetting);
342 ep &= USB_ENDPOINT_NUMBER_MASK;
344 subs->syncpipe = usb_rcvisocpipe(dev, ep);
346 subs->syncpipe = usb_sndisocpipe(dev, ep);
347 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
348 get_endpoint(alts, 1)->bRefresh >= 1 &&
349 get_endpoint(alts, 1)->bRefresh <= 9)
350 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
351 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
352 subs->syncinterval = 1;
353 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
354 get_endpoint(alts, 1)->bInterval <= 16)
355 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
357 subs->syncinterval = 3;
360 /* always fill max packet size */
361 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
364 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
367 subs->cur_audiofmt = fmt;
369 snd_usb_set_format_quirk(subs, fmt);
373 "setting done: format = %d, rate = %d..%d, channels = %d\n",
374 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
376 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
377 subs->datapipe, subs->syncpipe);
386 * allocate a buffer and set the given audio format.
388 * so far we use a physically linear buffer although packetize transfer
389 * doesn't need a continuous area.
390 * if sg buffer is supported on the later version of alsa, we'll follow
393 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
394 struct snd_pcm_hw_params *hw_params)
396 struct snd_usb_substream *subs = substream->runtime->private_data;
397 struct audioformat *fmt;
398 unsigned int channels, rate, format;
401 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
402 params_buffer_bytes(hw_params));
406 format = params_format(hw_params);
407 rate = params_rate(hw_params);
408 channels = params_channels(hw_params);
409 fmt = find_format(subs, format, rate, channels);
411 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
412 format, rate, channels);
416 changed = subs->cur_audiofmt != fmt ||
417 subs->period_bytes != params_period_bytes(hw_params) ||
418 subs->cur_rate != rate;
419 if ((ret = set_format(subs, fmt)) < 0)
422 if (subs->cur_rate != rate) {
423 struct usb_host_interface *alts;
424 struct usb_interface *iface;
425 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
426 alts = &iface->altsetting[fmt->altset_idx];
427 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
430 subs->cur_rate = rate;
435 snd_usb_release_substream_urbs(subs, 0);
436 /* influenced: period_bytes, channels, rate, format, */
437 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
438 params_rate(hw_params),
439 snd_pcm_format_physical_width(params_format(hw_params)) *
440 params_channels(hw_params));
449 * reset the audio format and release the buffer
451 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
453 struct snd_usb_substream *subs = substream->runtime->private_data;
455 subs->cur_audiofmt = NULL;
457 subs->period_bytes = 0;
458 if (!subs->stream->chip->shutdown)
459 snd_usb_release_substream_urbs(subs, 0);
460 return snd_pcm_lib_free_vmalloc_buffer(substream);
466 * only a few subtle things...
468 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
470 struct snd_pcm_runtime *runtime = substream->runtime;
471 struct snd_usb_substream *subs = runtime->private_data;
473 if (! subs->cur_audiofmt) {
474 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
478 /* some unit conversions in runtime */
479 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
480 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
482 /* reset the pointer */
483 subs->hwptr_done = 0;
484 subs->transfer_done = 0;
488 return snd_usb_substream_prepare(subs, runtime);
491 static struct snd_pcm_hardware snd_usb_hardware =
493 .info = SNDRV_PCM_INFO_MMAP |
494 SNDRV_PCM_INFO_MMAP_VALID |
495 SNDRV_PCM_INFO_BATCH |
496 SNDRV_PCM_INFO_INTERLEAVED |
497 SNDRV_PCM_INFO_BLOCK_TRANSFER |
498 SNDRV_PCM_INFO_PAUSE,
499 .buffer_bytes_max = 1024 * 1024,
500 .period_bytes_min = 64,
501 .period_bytes_max = 512 * 1024,
506 static int hw_check_valid_format(struct snd_usb_substream *subs,
507 struct snd_pcm_hw_params *params,
508 struct audioformat *fp)
510 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
511 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
512 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
513 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
514 struct snd_mask check_fmts;
517 /* check the format */
518 snd_mask_none(&check_fmts);
519 check_fmts.bits[0] = (u32)fp->formats;
520 check_fmts.bits[1] = (u32)(fp->formats >> 32);
521 snd_mask_intersect(&check_fmts, fmts);
522 if (snd_mask_empty(&check_fmts)) {
523 hwc_debug(" > check: no supported format %d\n", fp->format);
526 /* check the channels */
527 if (fp->channels < ct->min || fp->channels > ct->max) {
528 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
531 /* check the rate is within the range */
532 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
533 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
536 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
537 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
540 /* check whether the period time is >= the data packet interval */
541 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
542 ptime = 125 * (1 << fp->datainterval);
543 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
544 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
551 static int hw_rule_rate(struct snd_pcm_hw_params *params,
552 struct snd_pcm_hw_rule *rule)
554 struct snd_usb_substream *subs = rule->private;
556 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
557 unsigned int rmin, rmax;
560 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
563 list_for_each(p, &subs->fmt_list) {
564 struct audioformat *fp;
565 fp = list_entry(p, struct audioformat, list);
566 if (!hw_check_valid_format(subs, params, fp))
569 if (rmin > fp->rate_min)
571 if (rmax < fp->rate_max)
580 hwc_debug(" --> get empty\n");
586 if (it->min < rmin) {
591 if (it->max > rmax) {
596 if (snd_interval_checkempty(it)) {
600 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
605 static int hw_rule_channels(struct snd_pcm_hw_params *params,
606 struct snd_pcm_hw_rule *rule)
608 struct snd_usb_substream *subs = rule->private;
610 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
611 unsigned int rmin, rmax;
614 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
617 list_for_each(p, &subs->fmt_list) {
618 struct audioformat *fp;
619 fp = list_entry(p, struct audioformat, list);
620 if (!hw_check_valid_format(subs, params, fp))
623 if (rmin > fp->channels)
625 if (rmax < fp->channels)
634 hwc_debug(" --> get empty\n");
640 if (it->min < rmin) {
645 if (it->max > rmax) {
650 if (snd_interval_checkempty(it)) {
654 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
658 static int hw_rule_format(struct snd_pcm_hw_params *params,
659 struct snd_pcm_hw_rule *rule)
661 struct snd_usb_substream *subs = rule->private;
663 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
668 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
670 list_for_each(p, &subs->fmt_list) {
671 struct audioformat *fp;
672 fp = list_entry(p, struct audioformat, list);
673 if (!hw_check_valid_format(subs, params, fp))
675 fbits |= fp->formats;
678 oldbits[0] = fmt->bits[0];
679 oldbits[1] = fmt->bits[1];
680 fmt->bits[0] &= (u32)fbits;
681 fmt->bits[1] &= (u32)(fbits >> 32);
682 if (!fmt->bits[0] && !fmt->bits[1]) {
683 hwc_debug(" --> get empty\n");
686 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
687 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
691 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
692 struct snd_pcm_hw_rule *rule)
694 struct snd_usb_substream *subs = rule->private;
695 struct audioformat *fp;
696 struct snd_interval *it;
697 unsigned char min_datainterval;
701 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
702 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
703 min_datainterval = 0xff;
704 list_for_each_entry(fp, &subs->fmt_list, list) {
705 if (!hw_check_valid_format(subs, params, fp))
707 min_datainterval = min(min_datainterval, fp->datainterval);
709 if (min_datainterval == 0xff) {
710 hwc_debug(" --> get emtpy\n");
714 pmin = 125 * (1 << min_datainterval);
716 if (it->min < pmin) {
721 if (snd_interval_checkempty(it)) {
725 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
730 * If the device supports unusual bit rates, does the request meet these?
732 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
733 struct snd_usb_substream *subs)
735 struct audioformat *fp;
736 int count = 0, needs_knot = 0;
739 list_for_each_entry(fp, &subs->fmt_list, list) {
740 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
742 count += fp->nr_rates;
743 if (fp->rates & SNDRV_PCM_RATE_KNOT)
749 subs->rate_list.count = count;
750 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
751 subs->rate_list.mask = 0;
753 list_for_each_entry(fp, &subs->fmt_list, list) {
755 for (i = 0; i < fp->nr_rates; i++)
756 subs->rate_list.list[count++] = fp->rate_table[i];
758 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
768 * set up the runtime hardware information.
771 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
774 unsigned int pt, ptmin;
775 int param_period_time_if_needed;
778 runtime->hw.formats = subs->formats;
780 runtime->hw.rate_min = 0x7fffffff;
781 runtime->hw.rate_max = 0;
782 runtime->hw.channels_min = 256;
783 runtime->hw.channels_max = 0;
784 runtime->hw.rates = 0;
786 /* check min/max rates and channels */
787 list_for_each(p, &subs->fmt_list) {
788 struct audioformat *fp;
789 fp = list_entry(p, struct audioformat, list);
790 runtime->hw.rates |= fp->rates;
791 if (runtime->hw.rate_min > fp->rate_min)
792 runtime->hw.rate_min = fp->rate_min;
793 if (runtime->hw.rate_max < fp->rate_max)
794 runtime->hw.rate_max = fp->rate_max;
795 if (runtime->hw.channels_min > fp->channels)
796 runtime->hw.channels_min = fp->channels;
797 if (runtime->hw.channels_max < fp->channels)
798 runtime->hw.channels_max = fp->channels;
799 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
800 /* FIXME: there might be more than one audio formats... */
801 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
804 pt = 125 * (1 << fp->datainterval);
805 ptmin = min(ptmin, pt);
808 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
809 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
810 /* full speed devices have fixed data packet interval */
813 /* if period time doesn't go below 1 ms, no rules needed */
814 param_period_time_if_needed = -1;
815 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
818 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
820 SNDRV_PCM_HW_PARAM_FORMAT,
821 SNDRV_PCM_HW_PARAM_CHANNELS,
822 param_period_time_if_needed,
825 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
826 hw_rule_channels, subs,
827 SNDRV_PCM_HW_PARAM_FORMAT,
828 SNDRV_PCM_HW_PARAM_RATE,
829 param_period_time_if_needed,
832 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
833 hw_rule_format, subs,
834 SNDRV_PCM_HW_PARAM_RATE,
835 SNDRV_PCM_HW_PARAM_CHANNELS,
836 param_period_time_if_needed,
839 if (param_period_time_if_needed >= 0) {
840 err = snd_pcm_hw_rule_add(runtime, 0,
841 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
842 hw_rule_period_time, subs,
843 SNDRV_PCM_HW_PARAM_FORMAT,
844 SNDRV_PCM_HW_PARAM_CHANNELS,
845 SNDRV_PCM_HW_PARAM_RATE,
850 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
855 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
857 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
858 struct snd_pcm_runtime *runtime = substream->runtime;
859 struct snd_usb_substream *subs = &as->substream[direction];
861 subs->interface = -1;
862 subs->altset_idx = 0;
863 runtime->hw = snd_usb_hardware;
864 runtime->private_data = subs;
865 subs->pcm_substream = substream;
866 return setup_hw_info(runtime, subs);
869 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
871 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
872 struct snd_usb_substream *subs = &as->substream[direction];
874 if (!as->chip->shutdown && subs->interface >= 0) {
875 usb_set_interface(subs->dev, subs->interface, 0);
876 subs->interface = -1;
878 subs->pcm_substream = NULL;
882 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
884 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
887 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
889 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
892 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
894 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
897 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
899 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
902 static struct snd_pcm_ops snd_usb_playback_ops = {
903 .open = snd_usb_playback_open,
904 .close = snd_usb_playback_close,
905 .ioctl = snd_pcm_lib_ioctl,
906 .hw_params = snd_usb_hw_params,
907 .hw_free = snd_usb_hw_free,
908 .prepare = snd_usb_pcm_prepare,
909 .trigger = snd_usb_substream_playback_trigger,
910 .pointer = snd_usb_pcm_pointer,
911 .page = snd_pcm_lib_get_vmalloc_page,
912 .mmap = snd_pcm_lib_mmap_vmalloc,
915 static struct snd_pcm_ops snd_usb_capture_ops = {
916 .open = snd_usb_capture_open,
917 .close = snd_usb_capture_close,
918 .ioctl = snd_pcm_lib_ioctl,
919 .hw_params = snd_usb_hw_params,
920 .hw_free = snd_usb_hw_free,
921 .prepare = snd_usb_pcm_prepare,
922 .trigger = snd_usb_substream_capture_trigger,
923 .pointer = snd_usb_pcm_pointer,
924 .page = snd_pcm_lib_get_vmalloc_page,
925 .mmap = snd_pcm_lib_mmap_vmalloc,
928 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
930 snd_pcm_set_ops(pcm, stream,
931 stream == SNDRV_PCM_STREAM_PLAYBACK ?
932 &snd_usb_playback_ops : &snd_usb_capture_ops);