jfs: fix a couple races
[linux-2.6.git] / sound / usb / pcm.c
blobc6593101c049b88ca6e5661f8765b3f3df0b4d33
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.
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/slab.h>
19 #include <linux/ratelimit.h>
20 #include <linux/usb.h>
21 #include <linux/usb/audio.h>
22 #include <linux/usb/audio-v2.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
28 #include "usbaudio.h"
29 #include "card.h"
30 #include "quirks.h"
31 #include "debug.h"
32 #include "endpoint.h"
33 #include "helper.h"
34 #include "pcm.h"
35 #include "clock.h"
36 #include "power.h"
38 #define SUBSTREAM_FLAG_DATA_EP_STARTED 0
39 #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
41 /* return the estimated delay based on USB frame counters */
42 snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
43 unsigned int rate)
45 int current_frame_number;
46 int frame_diff;
47 int est_delay;
49 if (!subs->last_delay)
50 return 0; /* short path */
52 current_frame_number = usb_get_current_frame_number(subs->dev);
54 * HCD implementations use different widths, use lower 8 bits.
55 * The delay will be managed up to 256ms, which is more than
56 * enough
58 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
60 /* Approximation based on number of samples per USB frame (ms),
61 some truncation for 44.1 but the estimate is good enough */
62 est_delay = subs->last_delay - (frame_diff * rate / 1000);
63 if (est_delay < 0)
64 est_delay = 0;
65 return est_delay;
69 * return the current pcm pointer. just based on the hwptr_done value.
71 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
73 struct snd_usb_substream *subs;
74 unsigned int hwptr_done;
76 subs = (struct snd_usb_substream *)substream->runtime->private_data;
77 if (subs->stream->chip->shutdown)
78 return SNDRV_PCM_POS_XRUN;
79 spin_lock(&subs->lock);
80 hwptr_done = subs->hwptr_done;
81 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
82 substream->runtime->delay = snd_usb_pcm_delay(subs,
83 substream->runtime->rate);
84 spin_unlock(&subs->lock);
85 return hwptr_done / (substream->runtime->frame_bits >> 3);
89 * find a matching audio format
91 static struct audioformat *find_format(struct snd_usb_substream *subs)
93 struct list_head *p;
94 struct audioformat *found = NULL;
95 int cur_attr = 0, attr;
97 list_for_each(p, &subs->fmt_list) {
98 struct audioformat *fp;
99 fp = list_entry(p, struct audioformat, list);
100 if (!(fp->formats & (1uLL << subs->pcm_format)))
101 continue;
102 if (fp->channels != subs->channels)
103 continue;
104 if (subs->cur_rate < fp->rate_min ||
105 subs->cur_rate > fp->rate_max)
106 continue;
107 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
108 unsigned int i;
109 for (i = 0; i < fp->nr_rates; i++)
110 if (fp->rate_table[i] == subs->cur_rate)
111 break;
112 if (i >= fp->nr_rates)
113 continue;
115 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
116 if (! found) {
117 found = fp;
118 cur_attr = attr;
119 continue;
121 /* avoid async out and adaptive in if the other method
122 * supports the same format.
123 * this is a workaround for the case like
124 * M-audio audiophile USB.
126 if (attr != cur_attr) {
127 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
128 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
129 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
130 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
131 continue;
132 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
133 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
134 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
135 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
136 found = fp;
137 cur_attr = attr;
138 continue;
141 /* find the format with the largest max. packet size */
142 if (fp->maxpacksize > found->maxpacksize) {
143 found = fp;
144 cur_attr = attr;
147 return found;
150 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
151 struct usb_host_interface *alts,
152 struct audioformat *fmt)
154 struct usb_device *dev = chip->dev;
155 unsigned int ep;
156 unsigned char data[1];
157 int err;
159 ep = get_endpoint(alts, 0)->bEndpointAddress;
161 data[0] = 1;
162 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
163 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
164 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
165 data, sizeof(data))) < 0) {
166 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
167 dev->devnum, iface, ep);
168 return err;
171 return 0;
174 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
175 struct usb_host_interface *alts,
176 struct audioformat *fmt)
178 struct usb_device *dev = chip->dev;
179 unsigned char data[1];
180 int err;
182 data[0] = 1;
183 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
184 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
185 UAC2_EP_CS_PITCH << 8, 0,
186 data, sizeof(data))) < 0) {
187 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
188 dev->devnum, iface, fmt->altsetting);
189 return err;
192 return 0;
196 * initialize the pitch control and sample rate
198 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
199 struct usb_host_interface *alts,
200 struct audioformat *fmt)
202 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
204 /* if endpoint doesn't have pitch control, bail out */
205 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
206 return 0;
208 switch (altsd->bInterfaceProtocol) {
209 case UAC_VERSION_1:
210 default:
211 return init_pitch_v1(chip, iface, alts, fmt);
213 case UAC_VERSION_2:
214 return init_pitch_v2(chip, iface, alts, fmt);
218 static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
220 int err;
222 if (!subs->data_endpoint)
223 return -EINVAL;
225 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
226 struct snd_usb_endpoint *ep = subs->data_endpoint;
228 snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
230 ep->data_subs = subs;
231 err = snd_usb_endpoint_start(ep, can_sleep);
232 if (err < 0) {
233 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
234 return err;
238 if (subs->sync_endpoint &&
239 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
240 struct snd_usb_endpoint *ep = subs->sync_endpoint;
242 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
243 subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
244 err = usb_set_interface(subs->dev,
245 subs->sync_endpoint->iface,
246 subs->sync_endpoint->alt_idx);
247 if (err < 0) {
248 snd_printk(KERN_ERR
249 "%d:%d:%d: cannot set interface (%d)\n",
250 subs->dev->devnum,
251 subs->sync_endpoint->iface,
252 subs->sync_endpoint->alt_idx, err);
253 return -EIO;
257 snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
259 ep->sync_slave = subs->data_endpoint;
260 err = snd_usb_endpoint_start(ep, can_sleep);
261 if (err < 0) {
262 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
263 return err;
267 return 0;
270 static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
272 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
273 snd_usb_endpoint_stop(subs->sync_endpoint);
275 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
276 snd_usb_endpoint_stop(subs->data_endpoint);
278 if (wait) {
279 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
280 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
284 static int deactivate_endpoints(struct snd_usb_substream *subs)
286 int reta, retb;
288 reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
289 retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
291 if (reta < 0)
292 return reta;
294 if (retb < 0)
295 return retb;
297 return 0;
301 * find a matching format and set up the interface
303 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
305 struct usb_device *dev = subs->dev;
306 struct usb_host_interface *alts;
307 struct usb_interface_descriptor *altsd;
308 struct usb_interface *iface;
309 unsigned int ep, attr;
310 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
311 int err, implicit_fb = 0;
313 iface = usb_ifnum_to_if(dev, fmt->iface);
314 if (WARN_ON(!iface))
315 return -EINVAL;
316 alts = &iface->altsetting[fmt->altset_idx];
317 altsd = get_iface_desc(alts);
318 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
319 return -EINVAL;
321 if (fmt == subs->cur_audiofmt)
322 return 0;
324 /* close the old interface */
325 if (subs->interface >= 0 && subs->interface != fmt->iface) {
326 err = usb_set_interface(subs->dev, subs->interface, 0);
327 if (err < 0) {
328 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
329 dev->devnum, fmt->iface, fmt->altsetting, err);
330 return -EIO;
332 subs->interface = -1;
333 subs->altset_idx = 0;
336 /* set interface */
337 if (subs->interface != fmt->iface ||
338 subs->altset_idx != fmt->altset_idx) {
339 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
340 if (err < 0) {
341 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
342 dev->devnum, fmt->iface, fmt->altsetting, err);
343 return -EIO;
345 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
346 fmt->iface, fmt->altsetting);
347 subs->interface = fmt->iface;
348 subs->altset_idx = fmt->altset_idx;
351 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
352 alts, fmt->endpoint, subs->direction,
353 SND_USB_ENDPOINT_TYPE_DATA);
354 if (!subs->data_endpoint)
355 return -EINVAL;
357 /* we need a sync pipe in async OUT or adaptive IN mode */
358 /* check the number of EP, since some devices have broken
359 * descriptors which fool us. if it has only one EP,
360 * assume it as adaptive-out or sync-in.
362 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
364 switch (subs->stream->chip->usb_id) {
365 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
366 if (is_playback) {
367 implicit_fb = 1;
368 ep = 0x81;
369 iface = usb_ifnum_to_if(dev, 3);
371 if (!iface || iface->num_altsetting == 0)
372 return -EINVAL;
374 alts = &iface->altsetting[1];
375 goto add_sync_ep;
377 break;
378 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
379 case USB_ID(0x0763, 0x2081):
380 if (is_playback) {
381 implicit_fb = 1;
382 ep = 0x81;
383 iface = usb_ifnum_to_if(dev, 2);
385 if (!iface || iface->num_altsetting == 0)
386 return -EINVAL;
388 alts = &iface->altsetting[1];
389 goto add_sync_ep;
393 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
394 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
395 altsd->bNumEndpoints >= 2) {
396 /* check sync-pipe endpoint */
397 /* ... and check descriptor size before accessing bSynchAddress
398 because there is a version of the SB Audigy 2 NX firmware lacking
399 the audio fields in the endpoint descriptors */
400 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
401 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
402 get_endpoint(alts, 1)->bSynchAddress != 0 &&
403 !implicit_fb)) {
404 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
405 dev->devnum, fmt->iface, fmt->altsetting,
406 get_endpoint(alts, 1)->bmAttributes,
407 get_endpoint(alts, 1)->bLength,
408 get_endpoint(alts, 1)->bSynchAddress);
409 return -EINVAL;
411 ep = get_endpoint(alts, 1)->bEndpointAddress;
412 if (!implicit_fb &&
413 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
414 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
415 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
416 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
417 dev->devnum, fmt->iface, fmt->altsetting,
418 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
419 return -EINVAL;
422 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
423 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
425 add_sync_ep:
426 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
427 alts, ep, !subs->direction,
428 implicit_fb ?
429 SND_USB_ENDPOINT_TYPE_DATA :
430 SND_USB_ENDPOINT_TYPE_SYNC);
431 if (!subs->sync_endpoint)
432 return -EINVAL;
434 subs->data_endpoint->sync_master = subs->sync_endpoint;
437 if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
438 return err;
440 subs->cur_audiofmt = fmt;
442 snd_usb_set_format_quirk(subs, fmt);
444 #if 0
445 printk(KERN_DEBUG
446 "setting done: format = %d, rate = %d..%d, channels = %d\n",
447 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
448 printk(KERN_DEBUG
449 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
450 subs->datapipe, subs->syncpipe);
451 #endif
453 return 0;
457 * Return the score of matching two audioformats.
458 * Veto the audioformat if:
459 * - It has no channels for some reason.
460 * - Requested PCM format is not supported.
461 * - Requested sample rate is not supported.
463 static int match_endpoint_audioformats(struct audioformat *fp,
464 struct audioformat *match, int rate,
465 snd_pcm_format_t pcm_format)
467 int i;
468 int score = 0;
470 if (fp->channels < 1) {
471 snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
472 return 0;
475 if (!(fp->formats & (1ULL << pcm_format))) {
476 snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
477 fp, pcm_format);
478 return 0;
481 for (i = 0; i < fp->nr_rates; i++) {
482 if (fp->rate_table[i] == rate) {
483 score++;
484 break;
487 if (!score) {
488 snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
489 fp, rate);
490 return 0;
493 if (fp->channels == match->channels)
494 score++;
496 snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
498 return score;
502 * Configure the sync ep using the rate and pcm format of the data ep.
504 static int configure_sync_endpoint(struct snd_usb_substream *subs)
506 int ret;
507 struct audioformat *fp;
508 struct audioformat *sync_fp = NULL;
509 int cur_score = 0;
510 int sync_period_bytes = subs->period_bytes;
511 struct snd_usb_substream *sync_subs =
512 &subs->stream->substream[subs->direction ^ 1];
514 /* Try to find the best matching audioformat. */
515 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
516 int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
517 subs->cur_rate, subs->pcm_format);
519 if (score > cur_score) {
520 sync_fp = fp;
521 cur_score = score;
525 if (unlikely(sync_fp == NULL)) {
526 snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
527 __func__, sync_subs->ep_num);
528 return -EINVAL;
532 * Recalculate the period bytes if channel number differ between
533 * data and sync ep audioformat.
535 if (sync_fp->channels != subs->channels) {
536 sync_period_bytes = (subs->period_bytes / subs->channels) *
537 sync_fp->channels;
538 snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
539 __func__, subs->period_bytes, sync_period_bytes);
542 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
543 subs->pcm_format,
544 sync_fp->channels,
545 sync_period_bytes,
546 subs->cur_rate,
547 sync_fp,
548 NULL);
550 return ret;
554 * configure endpoint params
556 * called during initial setup and upon resume
558 static int configure_endpoint(struct snd_usb_substream *subs)
560 int ret;
562 /* format changed */
563 stop_endpoints(subs, true);
564 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
565 subs->pcm_format,
566 subs->channels,
567 subs->period_bytes,
568 subs->cur_rate,
569 subs->cur_audiofmt,
570 subs->sync_endpoint);
571 if (ret < 0)
572 return ret;
574 if (subs->sync_endpoint)
575 ret = configure_sync_endpoint(subs);
577 return ret;
581 * hw_params callback
583 * allocate a buffer and set the given audio format.
585 * so far we use a physically linear buffer although packetize transfer
586 * doesn't need a continuous area.
587 * if sg buffer is supported on the later version of alsa, we'll follow
588 * that.
590 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
591 struct snd_pcm_hw_params *hw_params)
593 struct snd_usb_substream *subs = substream->runtime->private_data;
594 struct audioformat *fmt;
595 int ret;
597 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
598 params_buffer_bytes(hw_params));
599 if (ret < 0)
600 return ret;
602 subs->pcm_format = params_format(hw_params);
603 subs->period_bytes = params_period_bytes(hw_params);
604 subs->channels = params_channels(hw_params);
605 subs->cur_rate = params_rate(hw_params);
607 fmt = find_format(subs);
608 if (!fmt) {
609 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
610 subs->pcm_format, subs->cur_rate, subs->channels);
611 return -EINVAL;
614 down_read(&subs->stream->chip->shutdown_rwsem);
615 if (subs->stream->chip->shutdown)
616 ret = -ENODEV;
617 else
618 ret = set_format(subs, fmt);
619 up_read(&subs->stream->chip->shutdown_rwsem);
620 if (ret < 0)
621 return ret;
623 subs->interface = fmt->iface;
624 subs->altset_idx = fmt->altset_idx;
625 subs->need_setup_ep = true;
627 return 0;
631 * hw_free callback
633 * reset the audio format and release the buffer
635 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
637 struct snd_usb_substream *subs = substream->runtime->private_data;
639 subs->cur_audiofmt = NULL;
640 subs->cur_rate = 0;
641 subs->period_bytes = 0;
642 down_read(&subs->stream->chip->shutdown_rwsem);
643 if (!subs->stream->chip->shutdown) {
644 stop_endpoints(subs, true);
645 deactivate_endpoints(subs);
647 up_read(&subs->stream->chip->shutdown_rwsem);
648 return snd_pcm_lib_free_vmalloc_buffer(substream);
652 * prepare callback
654 * only a few subtle things...
656 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
658 struct snd_pcm_runtime *runtime = substream->runtime;
659 struct snd_usb_substream *subs = runtime->private_data;
660 struct usb_host_interface *alts;
661 struct usb_interface *iface;
662 int ret;
664 if (! subs->cur_audiofmt) {
665 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
666 return -ENXIO;
669 down_read(&subs->stream->chip->shutdown_rwsem);
670 if (subs->stream->chip->shutdown) {
671 ret = -ENODEV;
672 goto unlock;
674 if (snd_BUG_ON(!subs->data_endpoint)) {
675 ret = -EIO;
676 goto unlock;
679 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
680 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
682 ret = set_format(subs, subs->cur_audiofmt);
683 if (ret < 0)
684 goto unlock;
686 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
687 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
688 ret = snd_usb_init_sample_rate(subs->stream->chip,
689 subs->cur_audiofmt->iface,
690 alts,
691 subs->cur_audiofmt,
692 subs->cur_rate);
693 if (ret < 0)
694 goto unlock;
696 if (subs->need_setup_ep) {
697 ret = configure_endpoint(subs);
698 if (ret < 0)
699 goto unlock;
700 subs->need_setup_ep = false;
703 /* some unit conversions in runtime */
704 subs->data_endpoint->maxframesize =
705 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
706 subs->data_endpoint->curframesize =
707 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
709 /* reset the pointer */
710 subs->hwptr_done = 0;
711 subs->transfer_done = 0;
712 subs->last_delay = 0;
713 subs->last_frame_number = 0;
714 runtime->delay = 0;
716 /* for playback, submit the URBs now; otherwise, the first hwptr_done
717 * updates for all URBs would happen at the same time when starting */
718 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
719 ret = start_endpoints(subs, true);
721 unlock:
722 up_read(&subs->stream->chip->shutdown_rwsem);
723 return ret;
726 static struct snd_pcm_hardware snd_usb_hardware =
728 .info = SNDRV_PCM_INFO_MMAP |
729 SNDRV_PCM_INFO_MMAP_VALID |
730 SNDRV_PCM_INFO_BATCH |
731 SNDRV_PCM_INFO_INTERLEAVED |
732 SNDRV_PCM_INFO_BLOCK_TRANSFER |
733 SNDRV_PCM_INFO_PAUSE,
734 .buffer_bytes_max = 1024 * 1024,
735 .period_bytes_min = 64,
736 .period_bytes_max = 512 * 1024,
737 .periods_min = 2,
738 .periods_max = 1024,
741 static int hw_check_valid_format(struct snd_usb_substream *subs,
742 struct snd_pcm_hw_params *params,
743 struct audioformat *fp)
745 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
746 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
747 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
748 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
749 struct snd_mask check_fmts;
750 unsigned int ptime;
752 /* check the format */
753 snd_mask_none(&check_fmts);
754 check_fmts.bits[0] = (u32)fp->formats;
755 check_fmts.bits[1] = (u32)(fp->formats >> 32);
756 snd_mask_intersect(&check_fmts, fmts);
757 if (snd_mask_empty(&check_fmts)) {
758 hwc_debug(" > check: no supported format %d\n", fp->format);
759 return 0;
761 /* check the channels */
762 if (fp->channels < ct->min || fp->channels > ct->max) {
763 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
764 return 0;
766 /* check the rate is within the range */
767 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
768 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
769 return 0;
771 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
772 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
773 return 0;
775 /* check whether the period time is >= the data packet interval */
776 if (subs->speed != USB_SPEED_FULL) {
777 ptime = 125 * (1 << fp->datainterval);
778 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
779 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
780 return 0;
783 return 1;
786 static int hw_rule_rate(struct snd_pcm_hw_params *params,
787 struct snd_pcm_hw_rule *rule)
789 struct snd_usb_substream *subs = rule->private;
790 struct list_head *p;
791 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
792 unsigned int rmin, rmax;
793 int changed;
795 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
796 changed = 0;
797 rmin = rmax = 0;
798 list_for_each(p, &subs->fmt_list) {
799 struct audioformat *fp;
800 fp = list_entry(p, struct audioformat, list);
801 if (!hw_check_valid_format(subs, params, fp))
802 continue;
803 if (changed++) {
804 if (rmin > fp->rate_min)
805 rmin = fp->rate_min;
806 if (rmax < fp->rate_max)
807 rmax = fp->rate_max;
808 } else {
809 rmin = fp->rate_min;
810 rmax = fp->rate_max;
814 if (!changed) {
815 hwc_debug(" --> get empty\n");
816 it->empty = 1;
817 return -EINVAL;
820 changed = 0;
821 if (it->min < rmin) {
822 it->min = rmin;
823 it->openmin = 0;
824 changed = 1;
826 if (it->max > rmax) {
827 it->max = rmax;
828 it->openmax = 0;
829 changed = 1;
831 if (snd_interval_checkempty(it)) {
832 it->empty = 1;
833 return -EINVAL;
835 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
836 return changed;
840 static int hw_rule_channels(struct snd_pcm_hw_params *params,
841 struct snd_pcm_hw_rule *rule)
843 struct snd_usb_substream *subs = rule->private;
844 struct list_head *p;
845 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
846 unsigned int rmin, rmax;
847 int changed;
849 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
850 changed = 0;
851 rmin = rmax = 0;
852 list_for_each(p, &subs->fmt_list) {
853 struct audioformat *fp;
854 fp = list_entry(p, struct audioformat, list);
855 if (!hw_check_valid_format(subs, params, fp))
856 continue;
857 if (changed++) {
858 if (rmin > fp->channels)
859 rmin = fp->channels;
860 if (rmax < fp->channels)
861 rmax = fp->channels;
862 } else {
863 rmin = fp->channels;
864 rmax = fp->channels;
868 if (!changed) {
869 hwc_debug(" --> get empty\n");
870 it->empty = 1;
871 return -EINVAL;
874 changed = 0;
875 if (it->min < rmin) {
876 it->min = rmin;
877 it->openmin = 0;
878 changed = 1;
880 if (it->max > rmax) {
881 it->max = rmax;
882 it->openmax = 0;
883 changed = 1;
885 if (snd_interval_checkempty(it)) {
886 it->empty = 1;
887 return -EINVAL;
889 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
890 return changed;
893 static int hw_rule_format(struct snd_pcm_hw_params *params,
894 struct snd_pcm_hw_rule *rule)
896 struct snd_usb_substream *subs = rule->private;
897 struct list_head *p;
898 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
899 u64 fbits;
900 u32 oldbits[2];
901 int changed;
903 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
904 fbits = 0;
905 list_for_each(p, &subs->fmt_list) {
906 struct audioformat *fp;
907 fp = list_entry(p, struct audioformat, list);
908 if (!hw_check_valid_format(subs, params, fp))
909 continue;
910 fbits |= fp->formats;
913 oldbits[0] = fmt->bits[0];
914 oldbits[1] = fmt->bits[1];
915 fmt->bits[0] &= (u32)fbits;
916 fmt->bits[1] &= (u32)(fbits >> 32);
917 if (!fmt->bits[0] && !fmt->bits[1]) {
918 hwc_debug(" --> get empty\n");
919 return -EINVAL;
921 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
922 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
923 return changed;
926 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
927 struct snd_pcm_hw_rule *rule)
929 struct snd_usb_substream *subs = rule->private;
930 struct audioformat *fp;
931 struct snd_interval *it;
932 unsigned char min_datainterval;
933 unsigned int pmin;
934 int changed;
936 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
937 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
938 min_datainterval = 0xff;
939 list_for_each_entry(fp, &subs->fmt_list, list) {
940 if (!hw_check_valid_format(subs, params, fp))
941 continue;
942 min_datainterval = min(min_datainterval, fp->datainterval);
944 if (min_datainterval == 0xff) {
945 hwc_debug(" --> get empty\n");
946 it->empty = 1;
947 return -EINVAL;
949 pmin = 125 * (1 << min_datainterval);
950 changed = 0;
951 if (it->min < pmin) {
952 it->min = pmin;
953 it->openmin = 0;
954 changed = 1;
956 if (snd_interval_checkempty(it)) {
957 it->empty = 1;
958 return -EINVAL;
960 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
961 return changed;
965 * If the device supports unusual bit rates, does the request meet these?
967 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
968 struct snd_usb_substream *subs)
970 struct audioformat *fp;
971 int *rate_list;
972 int count = 0, needs_knot = 0;
973 int err;
975 kfree(subs->rate_list.list);
976 subs->rate_list.list = NULL;
978 list_for_each_entry(fp, &subs->fmt_list, list) {
979 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
980 return 0;
981 count += fp->nr_rates;
982 if (fp->rates & SNDRV_PCM_RATE_KNOT)
983 needs_knot = 1;
985 if (!needs_knot)
986 return 0;
988 subs->rate_list.list = rate_list =
989 kmalloc(sizeof(int) * count, GFP_KERNEL);
990 if (!subs->rate_list.list)
991 return -ENOMEM;
992 subs->rate_list.count = count;
993 subs->rate_list.mask = 0;
994 count = 0;
995 list_for_each_entry(fp, &subs->fmt_list, list) {
996 int i;
997 for (i = 0; i < fp->nr_rates; i++)
998 rate_list[count++] = fp->rate_table[i];
1000 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1001 &subs->rate_list);
1002 if (err < 0)
1003 return err;
1005 return 0;
1010 * set up the runtime hardware information.
1013 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1015 struct list_head *p;
1016 unsigned int pt, ptmin;
1017 int param_period_time_if_needed;
1018 int err;
1020 runtime->hw.formats = subs->formats;
1022 runtime->hw.rate_min = 0x7fffffff;
1023 runtime->hw.rate_max = 0;
1024 runtime->hw.channels_min = 256;
1025 runtime->hw.channels_max = 0;
1026 runtime->hw.rates = 0;
1027 ptmin = UINT_MAX;
1028 /* check min/max rates and channels */
1029 list_for_each(p, &subs->fmt_list) {
1030 struct audioformat *fp;
1031 fp = list_entry(p, struct audioformat, list);
1032 runtime->hw.rates |= fp->rates;
1033 if (runtime->hw.rate_min > fp->rate_min)
1034 runtime->hw.rate_min = fp->rate_min;
1035 if (runtime->hw.rate_max < fp->rate_max)
1036 runtime->hw.rate_max = fp->rate_max;
1037 if (runtime->hw.channels_min > fp->channels)
1038 runtime->hw.channels_min = fp->channels;
1039 if (runtime->hw.channels_max < fp->channels)
1040 runtime->hw.channels_max = fp->channels;
1041 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1042 /* FIXME: there might be more than one audio formats... */
1043 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1044 fp->frame_size;
1046 pt = 125 * (1 << fp->datainterval);
1047 ptmin = min(ptmin, pt);
1049 err = snd_usb_autoresume(subs->stream->chip);
1050 if (err < 0)
1051 return err;
1053 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1054 if (subs->speed == USB_SPEED_FULL)
1055 /* full speed devices have fixed data packet interval */
1056 ptmin = 1000;
1057 if (ptmin == 1000)
1058 /* if period time doesn't go below 1 ms, no rules needed */
1059 param_period_time_if_needed = -1;
1060 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1061 ptmin, UINT_MAX);
1063 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1064 hw_rule_rate, subs,
1065 SNDRV_PCM_HW_PARAM_FORMAT,
1066 SNDRV_PCM_HW_PARAM_CHANNELS,
1067 param_period_time_if_needed,
1068 -1)) < 0)
1069 goto rep_err;
1070 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1071 hw_rule_channels, subs,
1072 SNDRV_PCM_HW_PARAM_FORMAT,
1073 SNDRV_PCM_HW_PARAM_RATE,
1074 param_period_time_if_needed,
1075 -1)) < 0)
1076 goto rep_err;
1077 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1078 hw_rule_format, subs,
1079 SNDRV_PCM_HW_PARAM_RATE,
1080 SNDRV_PCM_HW_PARAM_CHANNELS,
1081 param_period_time_if_needed,
1082 -1)) < 0)
1083 goto rep_err;
1084 if (param_period_time_if_needed >= 0) {
1085 err = snd_pcm_hw_rule_add(runtime, 0,
1086 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1087 hw_rule_period_time, subs,
1088 SNDRV_PCM_HW_PARAM_FORMAT,
1089 SNDRV_PCM_HW_PARAM_CHANNELS,
1090 SNDRV_PCM_HW_PARAM_RATE,
1091 -1);
1092 if (err < 0)
1093 goto rep_err;
1095 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
1096 goto rep_err;
1097 return 0;
1099 rep_err:
1100 snd_usb_autosuspend(subs->stream->chip);
1101 return err;
1104 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1106 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1107 struct snd_pcm_runtime *runtime = substream->runtime;
1108 struct snd_usb_substream *subs = &as->substream[direction];
1110 subs->interface = -1;
1111 subs->altset_idx = 0;
1112 runtime->hw = snd_usb_hardware;
1113 runtime->private_data = subs;
1114 subs->pcm_substream = substream;
1115 /* runtime PM is also done there */
1116 return setup_hw_info(runtime, subs);
1119 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1121 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1122 struct snd_usb_substream *subs = &as->substream[direction];
1124 stop_endpoints(subs, true);
1126 if (!as->chip->shutdown && subs->interface >= 0) {
1127 usb_set_interface(subs->dev, subs->interface, 0);
1128 subs->interface = -1;
1131 subs->pcm_substream = NULL;
1132 snd_usb_autosuspend(subs->stream->chip);
1134 return 0;
1137 /* Since a URB can handle only a single linear buffer, we must use double
1138 * buffering when the data to be transferred overflows the buffer boundary.
1139 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1140 * for all URBs.
1142 static void retire_capture_urb(struct snd_usb_substream *subs,
1143 struct urb *urb)
1145 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1146 unsigned int stride, frames, bytes, oldptr;
1147 int i, period_elapsed = 0;
1148 unsigned long flags;
1149 unsigned char *cp;
1151 stride = runtime->frame_bits >> 3;
1153 for (i = 0; i < urb->number_of_packets; i++) {
1154 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1155 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1156 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1157 // continue;
1159 bytes = urb->iso_frame_desc[i].actual_length;
1160 frames = bytes / stride;
1161 if (!subs->txfr_quirk)
1162 bytes = frames * stride;
1163 if (bytes % (runtime->sample_bits >> 3) != 0) {
1164 #ifdef CONFIG_SND_DEBUG_VERBOSE
1165 int oldbytes = bytes;
1166 #endif
1167 bytes = frames * stride;
1168 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1169 oldbytes, bytes);
1171 /* update the current pointer */
1172 spin_lock_irqsave(&subs->lock, flags);
1173 oldptr = subs->hwptr_done;
1174 subs->hwptr_done += bytes;
1175 if (subs->hwptr_done >= runtime->buffer_size * stride)
1176 subs->hwptr_done -= runtime->buffer_size * stride;
1177 frames = (bytes + (oldptr % stride)) / stride;
1178 subs->transfer_done += frames;
1179 if (subs->transfer_done >= runtime->period_size) {
1180 subs->transfer_done -= runtime->period_size;
1181 period_elapsed = 1;
1183 spin_unlock_irqrestore(&subs->lock, flags);
1184 /* copy a data chunk */
1185 if (oldptr + bytes > runtime->buffer_size * stride) {
1186 unsigned int bytes1 =
1187 runtime->buffer_size * stride - oldptr;
1188 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1189 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1190 } else {
1191 memcpy(runtime->dma_area + oldptr, cp, bytes);
1195 if (period_elapsed)
1196 snd_pcm_period_elapsed(subs->pcm_substream);
1199 static void prepare_playback_urb(struct snd_usb_substream *subs,
1200 struct urb *urb)
1202 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1203 struct snd_usb_endpoint *ep = subs->data_endpoint;
1204 struct snd_urb_ctx *ctx = urb->context;
1205 unsigned int counts, frames, bytes;
1206 int i, stride, period_elapsed = 0;
1207 unsigned long flags;
1209 stride = runtime->frame_bits >> 3;
1211 frames = 0;
1212 urb->number_of_packets = 0;
1213 spin_lock_irqsave(&subs->lock, flags);
1214 for (i = 0; i < ctx->packets; i++) {
1215 if (ctx->packet_size[i])
1216 counts = ctx->packet_size[i];
1217 else
1218 counts = snd_usb_endpoint_next_packet_size(ep);
1220 /* set up descriptor */
1221 urb->iso_frame_desc[i].offset = frames * stride;
1222 urb->iso_frame_desc[i].length = counts * stride;
1223 frames += counts;
1224 urb->number_of_packets++;
1225 subs->transfer_done += counts;
1226 if (subs->transfer_done >= runtime->period_size) {
1227 subs->transfer_done -= runtime->period_size;
1228 period_elapsed = 1;
1229 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1230 if (subs->transfer_done > 0) {
1231 /* FIXME: fill-max mode is not
1232 * supported yet */
1233 frames -= subs->transfer_done;
1234 counts -= subs->transfer_done;
1235 urb->iso_frame_desc[i].length =
1236 counts * stride;
1237 subs->transfer_done = 0;
1239 i++;
1240 if (i < ctx->packets) {
1241 /* add a transfer delimiter */
1242 urb->iso_frame_desc[i].offset =
1243 frames * stride;
1244 urb->iso_frame_desc[i].length = 0;
1245 urb->number_of_packets++;
1247 break;
1250 if (period_elapsed &&
1251 !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1252 break;
1254 bytes = frames * stride;
1255 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1256 /* err, the transferred area goes over buffer boundary. */
1257 unsigned int bytes1 =
1258 runtime->buffer_size * stride - subs->hwptr_done;
1259 memcpy(urb->transfer_buffer,
1260 runtime->dma_area + subs->hwptr_done, bytes1);
1261 memcpy(urb->transfer_buffer + bytes1,
1262 runtime->dma_area, bytes - bytes1);
1263 } else {
1264 memcpy(urb->transfer_buffer,
1265 runtime->dma_area + subs->hwptr_done, bytes);
1267 subs->hwptr_done += bytes;
1268 if (subs->hwptr_done >= runtime->buffer_size * stride)
1269 subs->hwptr_done -= runtime->buffer_size * stride;
1271 /* update delay with exact number of samples queued */
1272 runtime->delay = subs->last_delay;
1273 runtime->delay += frames;
1274 subs->last_delay = runtime->delay;
1276 /* realign last_frame_number */
1277 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1278 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1280 spin_unlock_irqrestore(&subs->lock, flags);
1281 urb->transfer_buffer_length = bytes;
1282 if (period_elapsed)
1283 snd_pcm_period_elapsed(subs->pcm_substream);
1287 * process after playback data complete
1288 * - decrease the delay count again
1290 static void retire_playback_urb(struct snd_usb_substream *subs,
1291 struct urb *urb)
1293 unsigned long flags;
1294 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1295 int stride = runtime->frame_bits >> 3;
1296 int processed = urb->transfer_buffer_length / stride;
1297 int est_delay;
1299 /* ignore the delay accounting when procssed=0 is given, i.e.
1300 * silent payloads are procssed before handling the actual data
1302 if (!processed)
1303 return;
1305 spin_lock_irqsave(&subs->lock, flags);
1306 if (!subs->last_delay)
1307 goto out; /* short path */
1309 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1310 /* update delay with exact number of samples played */
1311 if (processed > subs->last_delay)
1312 subs->last_delay = 0;
1313 else
1314 subs->last_delay -= processed;
1315 runtime->delay = subs->last_delay;
1318 * Report when delay estimate is off by more than 2ms.
1319 * The error should be lower than 2ms since the estimate relies
1320 * on two reads of a counter updated every ms.
1322 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1323 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1324 est_delay, subs->last_delay);
1326 if (!subs->running) {
1327 /* update last_frame_number for delay counting here since
1328 * prepare_playback_urb won't be called during pause
1330 subs->last_frame_number =
1331 usb_get_current_frame_number(subs->dev) & 0xff;
1334 out:
1335 spin_unlock_irqrestore(&subs->lock, flags);
1338 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1340 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1343 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1345 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1348 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1350 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1353 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1355 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1358 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1359 int cmd)
1361 struct snd_usb_substream *subs = substream->runtime->private_data;
1363 switch (cmd) {
1364 case SNDRV_PCM_TRIGGER_START:
1365 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1366 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1367 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1368 subs->running = 1;
1369 return 0;
1370 case SNDRV_PCM_TRIGGER_STOP:
1371 stop_endpoints(subs, false);
1372 subs->running = 0;
1373 return 0;
1374 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1375 subs->data_endpoint->prepare_data_urb = NULL;
1376 /* keep retire_data_urb for delay calculation */
1377 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1378 subs->running = 0;
1379 return 0;
1382 return -EINVAL;
1385 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1386 int cmd)
1388 int err;
1389 struct snd_usb_substream *subs = substream->runtime->private_data;
1391 switch (cmd) {
1392 case SNDRV_PCM_TRIGGER_START:
1393 err = start_endpoints(subs, false);
1394 if (err < 0)
1395 return err;
1397 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1398 subs->running = 1;
1399 return 0;
1400 case SNDRV_PCM_TRIGGER_STOP:
1401 stop_endpoints(subs, false);
1402 subs->running = 0;
1403 return 0;
1404 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1405 subs->data_endpoint->retire_data_urb = NULL;
1406 subs->running = 0;
1407 return 0;
1408 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1409 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1410 subs->running = 1;
1411 return 0;
1414 return -EINVAL;
1417 static struct snd_pcm_ops snd_usb_playback_ops = {
1418 .open = snd_usb_playback_open,
1419 .close = snd_usb_playback_close,
1420 .ioctl = snd_pcm_lib_ioctl,
1421 .hw_params = snd_usb_hw_params,
1422 .hw_free = snd_usb_hw_free,
1423 .prepare = snd_usb_pcm_prepare,
1424 .trigger = snd_usb_substream_playback_trigger,
1425 .pointer = snd_usb_pcm_pointer,
1426 .page = snd_pcm_lib_get_vmalloc_page,
1427 .mmap = snd_pcm_lib_mmap_vmalloc,
1430 static struct snd_pcm_ops snd_usb_capture_ops = {
1431 .open = snd_usb_capture_open,
1432 .close = snd_usb_capture_close,
1433 .ioctl = snd_pcm_lib_ioctl,
1434 .hw_params = snd_usb_hw_params,
1435 .hw_free = snd_usb_hw_free,
1436 .prepare = snd_usb_pcm_prepare,
1437 .trigger = snd_usb_substream_capture_trigger,
1438 .pointer = snd_usb_pcm_pointer,
1439 .page = snd_pcm_lib_get_vmalloc_page,
1440 .mmap = snd_pcm_lib_mmap_vmalloc,
1443 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1445 snd_pcm_set_ops(pcm, stream,
1446 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1447 &snd_usb_playback_ops : &snd_usb_capture_ops);