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>
20 #include <linux/usb/audio-v2.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
35 * return the current pcm pointer. just based on the hwptr_done value.
37 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
39 struct snd_usb_substream *subs;
40 unsigned int hwptr_done;
42 subs = (struct snd_usb_substream *)substream->runtime->private_data;
43 spin_lock(&subs->lock);
44 hwptr_done = subs->hwptr_done;
45 spin_unlock(&subs->lock);
46 return hwptr_done / (substream->runtime->frame_bits >> 3);
50 * find a matching audio format
52 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
53 unsigned int rate, unsigned int channels)
56 struct audioformat *found = NULL;
57 int cur_attr = 0, attr;
59 list_for_each(p, &subs->fmt_list) {
60 struct audioformat *fp;
61 fp = list_entry(p, struct audioformat, list);
62 if (!(fp->formats & (1uLL << format)))
64 if (fp->channels != channels)
66 if (rate < fp->rate_min || rate > fp->rate_max)
68 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
70 for (i = 0; i < fp->nr_rates; i++)
71 if (fp->rate_table[i] == rate)
73 if (i >= fp->nr_rates)
76 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
82 /* avoid async out and adaptive in if the other method
83 * supports the same format.
84 * this is a workaround for the case like
85 * M-audio audiophile USB.
87 if (attr != cur_attr) {
88 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
89 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
90 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
91 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
93 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
94 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
95 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
96 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
102 /* find the format with the largest max. packet size */
103 if (fp->maxpacksize > found->maxpacksize) {
111 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
112 struct usb_host_interface *alts,
113 struct audioformat *fmt)
115 struct usb_device *dev = chip->dev;
117 unsigned char data[1];
120 ep = get_endpoint(alts, 0)->bEndpointAddress;
122 /* if endpoint doesn't have pitch control, bail out */
123 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
127 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
128 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
129 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
130 data, sizeof(data), 1000)) < 0) {
131 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
132 dev->devnum, iface, ep);
140 * initialize the picth control and sample rate
142 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
143 struct usb_host_interface *alts,
144 struct audioformat *fmt)
146 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
148 switch (altsd->bInterfaceProtocol) {
150 return init_pitch_v1(chip, iface, alts, fmt);
153 /* not implemented yet */
160 static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
161 struct usb_host_interface *alts,
162 struct audioformat *fmt, int rate)
164 struct usb_device *dev = chip->dev;
166 unsigned char data[3];
169 ep = get_endpoint(alts, 0)->bEndpointAddress;
170 /* if endpoint doesn't have sampling rate control, bail out */
171 if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE)) {
172 snd_printk(KERN_WARNING "%d:%d:%d: endpoint lacks sample rate attribute bit, cannot set.\n",
173 dev->devnum, iface, fmt->altsetting);
179 data[2] = rate >> 16;
180 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
181 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
182 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
183 data, sizeof(data), 1000)) < 0) {
184 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
185 dev->devnum, iface, fmt->altsetting, rate, ep);
188 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
189 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
190 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
191 data, sizeof(data), 1000)) < 0) {
192 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
193 dev->devnum, iface, fmt->altsetting, ep);
194 return 0; /* some devices don't support reading */
196 crate = data[0] | (data[1] << 8) | (data[2] << 16);
198 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
199 // runtime->rate = crate;
205 static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
206 struct usb_host_interface *alts,
207 struct audioformat *fmt, int rate)
209 struct usb_device *dev = chip->dev;
210 unsigned char data[4];
215 data[2] = rate >> 16;
216 data[3] = rate >> 24;
217 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
218 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
219 UAC2_CS_CONTROL_SAM_FREQ << 8, chip->clock_id << 8,
220 data, sizeof(data), 1000)) < 0) {
221 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2)\n",
222 dev->devnum, iface, fmt->altsetting, rate);
225 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
226 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
227 UAC2_CS_CONTROL_SAM_FREQ << 8, chip->clock_id << 8,
228 data, sizeof(data), 1000)) < 0) {
229 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2)\n",
230 dev->devnum, iface, fmt->altsetting);
233 crate = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
235 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
240 int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
241 struct usb_host_interface *alts,
242 struct audioformat *fmt, int rate)
244 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
246 switch (altsd->bInterfaceProtocol) {
248 return set_sample_rate_v1(chip, iface, alts, fmt, rate);
251 return set_sample_rate_v2(chip, iface, alts, fmt, rate);
258 * find a matching format and set up the interface
260 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
262 struct usb_device *dev = subs->dev;
263 struct usb_host_interface *alts;
264 struct usb_interface_descriptor *altsd;
265 struct usb_interface *iface;
266 unsigned int ep, attr;
267 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
270 iface = usb_ifnum_to_if(dev, fmt->iface);
273 alts = &iface->altsetting[fmt->altset_idx];
274 altsd = get_iface_desc(alts);
275 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
278 if (fmt == subs->cur_audiofmt)
281 /* close the old interface */
282 if (subs->interface >= 0 && subs->interface != fmt->iface) {
283 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
284 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
285 dev->devnum, fmt->iface, fmt->altsetting);
288 subs->interface = -1;
289 subs->altset_idx = 0;
293 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
294 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
295 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
296 dev->devnum, fmt->iface, fmt->altsetting);
299 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
300 subs->interface = fmt->iface;
301 subs->altset_idx = fmt->altset_idx;
304 /* create a data pipe */
305 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
307 subs->datapipe = usb_sndisocpipe(dev, ep);
309 subs->datapipe = usb_rcvisocpipe(dev, ep);
310 subs->datainterval = fmt->datainterval;
311 subs->syncpipe = subs->syncinterval = 0;
312 subs->maxpacksize = fmt->maxpacksize;
315 /* we need a sync pipe in async OUT or adaptive IN mode */
316 /* check the number of EP, since some devices have broken
317 * descriptors which fool us. if it has only one EP,
318 * assume it as adaptive-out or sync-in.
320 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
321 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
322 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
323 altsd->bNumEndpoints >= 2) {
324 /* check sync-pipe endpoint */
325 /* ... and check descriptor size before accessing bSynchAddress
326 because there is a version of the SB Audigy 2 NX firmware lacking
327 the audio fields in the endpoint descriptors */
328 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
329 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
330 get_endpoint(alts, 1)->bSynchAddress != 0)) {
331 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
332 dev->devnum, fmt->iface, fmt->altsetting);
335 ep = get_endpoint(alts, 1)->bEndpointAddress;
336 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
337 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
338 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
339 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
340 dev->devnum, fmt->iface, fmt->altsetting);
343 ep &= USB_ENDPOINT_NUMBER_MASK;
345 subs->syncpipe = usb_rcvisocpipe(dev, ep);
347 subs->syncpipe = usb_sndisocpipe(dev, ep);
348 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
349 get_endpoint(alts, 1)->bRefresh >= 1 &&
350 get_endpoint(alts, 1)->bRefresh <= 9)
351 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
352 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
353 subs->syncinterval = 1;
354 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
355 get_endpoint(alts, 1)->bInterval <= 16)
356 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
358 subs->syncinterval = 3;
361 /* always fill max packet size */
362 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
365 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
368 subs->cur_audiofmt = fmt;
370 snd_usb_set_format_quirk(subs, fmt);
374 "setting done: format = %d, rate = %d..%d, channels = %d\n",
375 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
377 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
378 subs->datapipe, subs->syncpipe);
387 * allocate a buffer and set the given audio format.
389 * so far we use a physically linear buffer although packetize transfer
390 * doesn't need a continuous area.
391 * if sg buffer is supported on the later version of alsa, we'll follow
394 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
395 struct snd_pcm_hw_params *hw_params)
397 struct snd_usb_substream *subs = substream->runtime->private_data;
398 struct audioformat *fmt;
399 unsigned int channels, rate, format;
402 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
403 params_buffer_bytes(hw_params));
407 format = params_format(hw_params);
408 rate = params_rate(hw_params);
409 channels = params_channels(hw_params);
410 fmt = find_format(subs, format, rate, channels);
412 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
413 format, rate, channels);
417 changed = subs->cur_audiofmt != fmt ||
418 subs->period_bytes != params_period_bytes(hw_params) ||
419 subs->cur_rate != rate;
420 if ((ret = set_format(subs, fmt)) < 0)
423 if (subs->cur_rate != rate) {
424 struct usb_host_interface *alts;
425 struct usb_interface *iface;
426 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
427 alts = &iface->altsetting[fmt->altset_idx];
428 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
431 subs->cur_rate = rate;
436 snd_usb_release_substream_urbs(subs, 0);
437 /* influenced: period_bytes, channels, rate, format, */
438 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
439 params_rate(hw_params),
440 snd_pcm_format_physical_width(params_format(hw_params)) *
441 params_channels(hw_params));
450 * reset the audio format and release the buffer
452 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
454 struct snd_usb_substream *subs = substream->runtime->private_data;
456 subs->cur_audiofmt = NULL;
458 subs->period_bytes = 0;
459 if (!subs->stream->chip->shutdown)
460 snd_usb_release_substream_urbs(subs, 0);
461 return snd_pcm_lib_free_vmalloc_buffer(substream);
467 * only a few subtle things...
469 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
471 struct snd_pcm_runtime *runtime = substream->runtime;
472 struct snd_usb_substream *subs = runtime->private_data;
474 if (! subs->cur_audiofmt) {
475 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
479 /* some unit conversions in runtime */
480 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
481 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
483 /* reset the pointer */
484 subs->hwptr_done = 0;
485 subs->transfer_done = 0;
489 return snd_usb_substream_prepare(subs, runtime);
492 static struct snd_pcm_hardware snd_usb_hardware =
494 .info = SNDRV_PCM_INFO_MMAP |
495 SNDRV_PCM_INFO_MMAP_VALID |
496 SNDRV_PCM_INFO_BATCH |
497 SNDRV_PCM_INFO_INTERLEAVED |
498 SNDRV_PCM_INFO_BLOCK_TRANSFER |
499 SNDRV_PCM_INFO_PAUSE,
500 .buffer_bytes_max = 1024 * 1024,
501 .period_bytes_min = 64,
502 .period_bytes_max = 512 * 1024,
507 static int hw_check_valid_format(struct snd_usb_substream *subs,
508 struct snd_pcm_hw_params *params,
509 struct audioformat *fp)
511 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
512 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
513 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
514 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
515 struct snd_mask check_fmts;
518 /* check the format */
519 snd_mask_none(&check_fmts);
520 check_fmts.bits[0] = (u32)fp->formats;
521 check_fmts.bits[1] = (u32)(fp->formats >> 32);
522 snd_mask_intersect(&check_fmts, fmts);
523 if (snd_mask_empty(&check_fmts)) {
524 hwc_debug(" > check: no supported format %d\n", fp->format);
527 /* check the channels */
528 if (fp->channels < ct->min || fp->channels > ct->max) {
529 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
532 /* check the rate is within the range */
533 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
534 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
537 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
538 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
541 /* check whether the period time is >= the data packet interval */
542 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
543 ptime = 125 * (1 << fp->datainterval);
544 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
545 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
552 static int hw_rule_rate(struct snd_pcm_hw_params *params,
553 struct snd_pcm_hw_rule *rule)
555 struct snd_usb_substream *subs = rule->private;
557 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
558 unsigned int rmin, rmax;
561 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
564 list_for_each(p, &subs->fmt_list) {
565 struct audioformat *fp;
566 fp = list_entry(p, struct audioformat, list);
567 if (!hw_check_valid_format(subs, params, fp))
570 if (rmin > fp->rate_min)
572 if (rmax < fp->rate_max)
581 hwc_debug(" --> get empty\n");
587 if (it->min < rmin) {
592 if (it->max > rmax) {
597 if (snd_interval_checkempty(it)) {
601 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
606 static int hw_rule_channels(struct snd_pcm_hw_params *params,
607 struct snd_pcm_hw_rule *rule)
609 struct snd_usb_substream *subs = rule->private;
611 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
612 unsigned int rmin, rmax;
615 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
618 list_for_each(p, &subs->fmt_list) {
619 struct audioformat *fp;
620 fp = list_entry(p, struct audioformat, list);
621 if (!hw_check_valid_format(subs, params, fp))
624 if (rmin > fp->channels)
626 if (rmax < fp->channels)
635 hwc_debug(" --> get empty\n");
641 if (it->min < rmin) {
646 if (it->max > rmax) {
651 if (snd_interval_checkempty(it)) {
655 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
659 static int hw_rule_format(struct snd_pcm_hw_params *params,
660 struct snd_pcm_hw_rule *rule)
662 struct snd_usb_substream *subs = rule->private;
664 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
669 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
671 list_for_each(p, &subs->fmt_list) {
672 struct audioformat *fp;
673 fp = list_entry(p, struct audioformat, list);
674 if (!hw_check_valid_format(subs, params, fp))
676 fbits |= fp->formats;
679 oldbits[0] = fmt->bits[0];
680 oldbits[1] = fmt->bits[1];
681 fmt->bits[0] &= (u32)fbits;
682 fmt->bits[1] &= (u32)(fbits >> 32);
683 if (!fmt->bits[0] && !fmt->bits[1]) {
684 hwc_debug(" --> get empty\n");
687 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
688 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
692 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
693 struct snd_pcm_hw_rule *rule)
695 struct snd_usb_substream *subs = rule->private;
696 struct audioformat *fp;
697 struct snd_interval *it;
698 unsigned char min_datainterval;
702 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
703 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
704 min_datainterval = 0xff;
705 list_for_each_entry(fp, &subs->fmt_list, list) {
706 if (!hw_check_valid_format(subs, params, fp))
708 min_datainterval = min(min_datainterval, fp->datainterval);
710 if (min_datainterval == 0xff) {
711 hwc_debug(" --> get emtpy\n");
715 pmin = 125 * (1 << min_datainterval);
717 if (it->min < pmin) {
722 if (snd_interval_checkempty(it)) {
726 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
731 * If the device supports unusual bit rates, does the request meet these?
733 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
734 struct snd_usb_substream *subs)
736 struct audioformat *fp;
737 int count = 0, needs_knot = 0;
740 list_for_each_entry(fp, &subs->fmt_list, list) {
741 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
743 count += fp->nr_rates;
744 if (fp->rates & SNDRV_PCM_RATE_KNOT)
750 subs->rate_list.count = count;
751 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
752 subs->rate_list.mask = 0;
754 list_for_each_entry(fp, &subs->fmt_list, list) {
756 for (i = 0; i < fp->nr_rates; i++)
757 subs->rate_list.list[count++] = fp->rate_table[i];
759 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
769 * set up the runtime hardware information.
772 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
775 unsigned int pt, ptmin;
776 int param_period_time_if_needed;
779 runtime->hw.formats = subs->formats;
781 runtime->hw.rate_min = 0x7fffffff;
782 runtime->hw.rate_max = 0;
783 runtime->hw.channels_min = 256;
784 runtime->hw.channels_max = 0;
785 runtime->hw.rates = 0;
787 /* check min/max rates and channels */
788 list_for_each(p, &subs->fmt_list) {
789 struct audioformat *fp;
790 fp = list_entry(p, struct audioformat, list);
791 runtime->hw.rates |= fp->rates;
792 if (runtime->hw.rate_min > fp->rate_min)
793 runtime->hw.rate_min = fp->rate_min;
794 if (runtime->hw.rate_max < fp->rate_max)
795 runtime->hw.rate_max = fp->rate_max;
796 if (runtime->hw.channels_min > fp->channels)
797 runtime->hw.channels_min = fp->channels;
798 if (runtime->hw.channels_max < fp->channels)
799 runtime->hw.channels_max = fp->channels;
800 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
801 /* FIXME: there might be more than one audio formats... */
802 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
805 pt = 125 * (1 << fp->datainterval);
806 ptmin = min(ptmin, pt);
809 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
810 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
811 /* full speed devices have fixed data packet interval */
814 /* if period time doesn't go below 1 ms, no rules needed */
815 param_period_time_if_needed = -1;
816 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
819 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
821 SNDRV_PCM_HW_PARAM_FORMAT,
822 SNDRV_PCM_HW_PARAM_CHANNELS,
823 param_period_time_if_needed,
826 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
827 hw_rule_channels, subs,
828 SNDRV_PCM_HW_PARAM_FORMAT,
829 SNDRV_PCM_HW_PARAM_RATE,
830 param_period_time_if_needed,
833 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
834 hw_rule_format, subs,
835 SNDRV_PCM_HW_PARAM_RATE,
836 SNDRV_PCM_HW_PARAM_CHANNELS,
837 param_period_time_if_needed,
840 if (param_period_time_if_needed >= 0) {
841 err = snd_pcm_hw_rule_add(runtime, 0,
842 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
843 hw_rule_period_time, subs,
844 SNDRV_PCM_HW_PARAM_FORMAT,
845 SNDRV_PCM_HW_PARAM_CHANNELS,
846 SNDRV_PCM_HW_PARAM_RATE,
851 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
856 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
858 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
859 struct snd_pcm_runtime *runtime = substream->runtime;
860 struct snd_usb_substream *subs = &as->substream[direction];
862 subs->interface = -1;
863 subs->altset_idx = 0;
864 runtime->hw = snd_usb_hardware;
865 runtime->private_data = subs;
866 subs->pcm_substream = substream;
867 return setup_hw_info(runtime, subs);
870 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
872 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
873 struct snd_usb_substream *subs = &as->substream[direction];
875 if (!as->chip->shutdown && subs->interface >= 0) {
876 usb_set_interface(subs->dev, subs->interface, 0);
877 subs->interface = -1;
879 subs->pcm_substream = NULL;
883 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
885 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
888 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
890 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
893 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
895 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
898 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
900 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
903 static struct snd_pcm_ops snd_usb_playback_ops = {
904 .open = snd_usb_playback_open,
905 .close = snd_usb_playback_close,
906 .ioctl = snd_pcm_lib_ioctl,
907 .hw_params = snd_usb_hw_params,
908 .hw_free = snd_usb_hw_free,
909 .prepare = snd_usb_pcm_prepare,
910 .trigger = snd_usb_substream_playback_trigger,
911 .pointer = snd_usb_pcm_pointer,
912 .page = snd_pcm_lib_get_vmalloc_page,
913 .mmap = snd_pcm_lib_mmap_vmalloc,
916 static struct snd_pcm_ops snd_usb_capture_ops = {
917 .open = snd_usb_capture_open,
918 .close = snd_usb_capture_close,
919 .ioctl = snd_pcm_lib_ioctl,
920 .hw_params = snd_usb_hw_params,
921 .hw_free = snd_usb_hw_free,
922 .prepare = snd_usb_pcm_prepare,
923 .trigger = snd_usb_substream_capture_trigger,
924 .pointer = snd_usb_pcm_pointer,
925 .page = snd_pcm_lib_get_vmalloc_page,
926 .mmap = snd_pcm_lib_mmap_vmalloc,
929 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
931 snd_pcm_set_ops(pcm, stream,
932 stream == SNDRV_PCM_STREAM_PLAYBACK ?
933 &snd_usb_playback_ops : &snd_usb_capture_ops);