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/bitrev.h>
20 #include <linux/ratelimit.h>
21 #include <linux/usb.h>
22 #include <linux/usb/audio.h>
23 #include <linux/usb/audio-v2.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
39 #define SUBSTREAM_FLAG_DATA_EP_STARTED 0
40 #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
42 /* return the estimated delay based on USB frame counters */
43 snd_pcm_uframes_t
snd_usb_pcm_delay(struct snd_usb_substream
*subs
,
46 int current_frame_number
;
50 if (!subs
->last_delay
)
51 return 0; /* short path */
53 current_frame_number
= usb_get_current_frame_number(subs
->dev
);
55 * HCD implementations use different widths, use lower 8 bits.
56 * The delay will be managed up to 256ms, which is more than
59 frame_diff
= (current_frame_number
- subs
->last_frame_number
) & 0xff;
61 /* Approximation based on number of samples per USB frame (ms),
62 some truncation for 44.1 but the estimate is good enough */
63 est_delay
= frame_diff
* rate
/ 1000;
64 if (subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
)
65 est_delay
= subs
->last_delay
- est_delay
;
67 est_delay
= subs
->last_delay
+ est_delay
;
75 * return the current pcm pointer. just based on the hwptr_done value.
77 static snd_pcm_uframes_t
snd_usb_pcm_pointer(struct snd_pcm_substream
*substream
)
79 struct snd_usb_substream
*subs
;
80 unsigned int hwptr_done
;
82 subs
= (struct snd_usb_substream
*)substream
->runtime
->private_data
;
83 if (subs
->stream
->chip
->shutdown
)
84 return SNDRV_PCM_POS_XRUN
;
85 spin_lock(&subs
->lock
);
86 hwptr_done
= subs
->hwptr_done
;
87 substream
->runtime
->delay
= snd_usb_pcm_delay(subs
,
88 substream
->runtime
->rate
);
89 spin_unlock(&subs
->lock
);
90 return hwptr_done
/ (substream
->runtime
->frame_bits
>> 3);
94 * find a matching audio format
96 static struct audioformat
*find_format(struct snd_usb_substream
*subs
)
98 struct audioformat
*fp
;
99 struct audioformat
*found
= NULL
;
100 int cur_attr
= 0, attr
;
102 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
103 if (!(fp
->formats
& pcm_format_to_bits(subs
->pcm_format
)))
105 if (fp
->channels
!= subs
->channels
)
107 if (subs
->cur_rate
< fp
->rate_min
||
108 subs
->cur_rate
> fp
->rate_max
)
110 if (! (fp
->rates
& SNDRV_PCM_RATE_CONTINUOUS
)) {
112 for (i
= 0; i
< fp
->nr_rates
; i
++)
113 if (fp
->rate_table
[i
] == subs
->cur_rate
)
115 if (i
>= fp
->nr_rates
)
118 attr
= fp
->ep_attr
& USB_ENDPOINT_SYNCTYPE
;
124 /* avoid async out and adaptive in if the other method
125 * supports the same format.
126 * this is a workaround for the case like
127 * M-audio audiophile USB.
129 if (attr
!= cur_attr
) {
130 if ((attr
== USB_ENDPOINT_SYNC_ASYNC
&&
131 subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
) ||
132 (attr
== USB_ENDPOINT_SYNC_ADAPTIVE
&&
133 subs
->direction
== SNDRV_PCM_STREAM_CAPTURE
))
135 if ((cur_attr
== USB_ENDPOINT_SYNC_ASYNC
&&
136 subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
) ||
137 (cur_attr
== USB_ENDPOINT_SYNC_ADAPTIVE
&&
138 subs
->direction
== SNDRV_PCM_STREAM_CAPTURE
)) {
144 /* find the format with the largest max. packet size */
145 if (fp
->maxpacksize
> found
->maxpacksize
) {
153 static int init_pitch_v1(struct snd_usb_audio
*chip
, int iface
,
154 struct usb_host_interface
*alts
,
155 struct audioformat
*fmt
)
157 struct usb_device
*dev
= chip
->dev
;
159 unsigned char data
[1];
162 ep
= get_endpoint(alts
, 0)->bEndpointAddress
;
165 if ((err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC_SET_CUR
,
166 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
167 UAC_EP_CS_ATTR_PITCH_CONTROL
<< 8, ep
,
168 data
, sizeof(data
))) < 0) {
169 snd_printk(KERN_ERR
"%d:%d:%d: cannot set enable PITCH\n",
170 dev
->devnum
, iface
, ep
);
177 static int init_pitch_v2(struct snd_usb_audio
*chip
, int iface
,
178 struct usb_host_interface
*alts
,
179 struct audioformat
*fmt
)
181 struct usb_device
*dev
= chip
->dev
;
182 unsigned char data
[1];
186 if ((err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC2_CS_CUR
,
187 USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
| USB_DIR_OUT
,
188 UAC2_EP_CS_PITCH
<< 8, 0,
189 data
, sizeof(data
))) < 0) {
190 snd_printk(KERN_ERR
"%d:%d:%d: cannot set enable PITCH (v2)\n",
191 dev
->devnum
, iface
, fmt
->altsetting
);
199 * initialize the pitch control and sample rate
201 int snd_usb_init_pitch(struct snd_usb_audio
*chip
, int iface
,
202 struct usb_host_interface
*alts
,
203 struct audioformat
*fmt
)
205 /* if endpoint doesn't have pitch control, bail out */
206 if (!(fmt
->attributes
& UAC_EP_CS_ATTR_PITCH_CONTROL
))
209 switch (fmt
->protocol
) {
212 return init_pitch_v1(chip
, iface
, alts
, fmt
);
215 return init_pitch_v2(chip
, iface
, alts
, fmt
);
219 static int start_endpoints(struct snd_usb_substream
*subs
, bool can_sleep
)
223 if (!subs
->data_endpoint
)
226 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED
, &subs
->flags
)) {
227 struct snd_usb_endpoint
*ep
= subs
->data_endpoint
;
229 snd_printdd(KERN_DEBUG
"Starting data EP @%p\n", ep
);
231 ep
->data_subs
= subs
;
232 err
= snd_usb_endpoint_start(ep
, can_sleep
);
234 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED
, &subs
->flags
);
239 if (subs
->sync_endpoint
&&
240 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED
, &subs
->flags
)) {
241 struct snd_usb_endpoint
*ep
= subs
->sync_endpoint
;
243 if (subs
->data_endpoint
->iface
!= subs
->sync_endpoint
->iface
||
244 subs
->data_endpoint
->altsetting
!= subs
->sync_endpoint
->altsetting
) {
245 err
= usb_set_interface(subs
->dev
,
246 subs
->sync_endpoint
->iface
,
247 subs
->sync_endpoint
->altsetting
);
249 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED
, &subs
->flags
);
251 "%d:%d:%d: cannot set interface (%d)\n",
253 subs
->sync_endpoint
->iface
,
254 subs
->sync_endpoint
->altsetting
, err
);
259 snd_printdd(KERN_DEBUG
"Starting sync EP @%p\n", ep
);
261 ep
->sync_slave
= subs
->data_endpoint
;
262 err
= snd_usb_endpoint_start(ep
, can_sleep
);
264 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED
, &subs
->flags
);
272 static void stop_endpoints(struct snd_usb_substream
*subs
, bool wait
)
274 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED
, &subs
->flags
))
275 snd_usb_endpoint_stop(subs
->sync_endpoint
);
277 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED
, &subs
->flags
))
278 snd_usb_endpoint_stop(subs
->data_endpoint
);
281 snd_usb_endpoint_sync_pending_stop(subs
->sync_endpoint
);
282 snd_usb_endpoint_sync_pending_stop(subs
->data_endpoint
);
286 static int search_roland_implicit_fb(struct usb_device
*dev
, int ifnum
,
287 unsigned int altsetting
,
288 struct usb_host_interface
**alts
,
291 struct usb_interface
*iface
;
292 struct usb_interface_descriptor
*altsd
;
293 struct usb_endpoint_descriptor
*epd
;
295 iface
= usb_ifnum_to_if(dev
, ifnum
);
296 if (!iface
|| iface
->num_altsetting
< altsetting
+ 1)
298 *alts
= &iface
->altsetting
[altsetting
];
299 altsd
= get_iface_desc(*alts
);
300 if (altsd
->bAlternateSetting
!= altsetting
||
301 altsd
->bInterfaceClass
!= USB_CLASS_VENDOR_SPEC
||
302 (altsd
->bInterfaceSubClass
!= 2 &&
303 altsd
->bInterfaceProtocol
!= 2 ) ||
304 altsd
->bNumEndpoints
< 1)
306 epd
= get_endpoint(*alts
, 0);
307 if (!usb_endpoint_is_isoc_in(epd
) ||
308 (epd
->bmAttributes
& USB_ENDPOINT_USAGE_MASK
) !=
309 USB_ENDPOINT_USAGE_IMPLICIT_FB
)
311 *ep
= epd
->bEndpointAddress
;
315 static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream
*subs
,
316 struct usb_device
*dev
,
317 struct usb_interface_descriptor
*altsd
,
320 struct usb_host_interface
*alts
;
321 struct usb_interface
*iface
;
324 /* Implicit feedback sync EPs consumers are always playback EPs */
325 if (subs
->direction
!= SNDRV_PCM_STREAM_PLAYBACK
)
328 switch (subs
->stream
->chip
->usb_id
) {
329 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
330 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
332 iface
= usb_ifnum_to_if(dev
, 3);
334 if (!iface
|| iface
->num_altsetting
== 0)
337 alts
= &iface
->altsetting
[1];
340 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
341 case USB_ID(0x0763, 0x2081):
343 iface
= usb_ifnum_to_if(dev
, 2);
345 if (!iface
|| iface
->num_altsetting
== 0)
348 alts
= &iface
->altsetting
[1];
351 if (attr
== USB_ENDPOINT_SYNC_ASYNC
&&
352 altsd
->bInterfaceClass
== USB_CLASS_VENDOR_SPEC
&&
353 altsd
->bInterfaceProtocol
== 2 &&
354 altsd
->bNumEndpoints
== 1 &&
355 USB_ID_VENDOR(subs
->stream
->chip
->usb_id
) == 0x0582 /* Roland */ &&
356 search_roland_implicit_fb(dev
, altsd
->bInterfaceNumber
+ 1,
357 altsd
->bAlternateSetting
,
366 subs
->sync_endpoint
= snd_usb_add_endpoint(subs
->stream
->chip
,
367 alts
, ep
, !subs
->direction
,
368 SND_USB_ENDPOINT_TYPE_DATA
);
369 if (!subs
->sync_endpoint
)
372 subs
->data_endpoint
->sync_master
= subs
->sync_endpoint
;
377 static int set_sync_endpoint(struct snd_usb_substream
*subs
,
378 struct audioformat
*fmt
,
379 struct usb_device
*dev
,
380 struct usb_host_interface
*alts
,
381 struct usb_interface_descriptor
*altsd
)
383 int is_playback
= subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
;
384 unsigned int ep
, attr
;
388 /* we need a sync pipe in async OUT or adaptive IN mode */
389 /* check the number of EP, since some devices have broken
390 * descriptors which fool us. if it has only one EP,
391 * assume it as adaptive-out or sync-in.
393 attr
= fmt
->ep_attr
& USB_ENDPOINT_SYNCTYPE
;
395 err
= set_sync_ep_implicit_fb_quirk(subs
, dev
, altsd
, attr
);
399 if (altsd
->bNumEndpoints
< 2)
402 if ((is_playback
&& attr
!= USB_ENDPOINT_SYNC_ASYNC
) ||
403 (!is_playback
&& attr
!= USB_ENDPOINT_SYNC_ADAPTIVE
))
406 /* check sync-pipe endpoint */
407 /* ... and check descriptor size before accessing bSynchAddress
408 because there is a version of the SB Audigy 2 NX firmware lacking
409 the audio fields in the endpoint descriptors */
410 if ((get_endpoint(alts
, 1)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_ISOC
||
411 (get_endpoint(alts
, 1)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
412 get_endpoint(alts
, 1)->bSynchAddress
!= 0)) {
413 snd_printk(KERN_ERR
"%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
414 dev
->devnum
, fmt
->iface
, fmt
->altsetting
,
415 get_endpoint(alts
, 1)->bmAttributes
,
416 get_endpoint(alts
, 1)->bLength
,
417 get_endpoint(alts
, 1)->bSynchAddress
);
420 ep
= get_endpoint(alts
, 1)->bEndpointAddress
;
421 if (get_endpoint(alts
, 0)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
422 ((is_playback
&& ep
!= (unsigned int)(get_endpoint(alts
, 0)->bSynchAddress
| USB_DIR_IN
)) ||
423 (!is_playback
&& ep
!= (unsigned int)(get_endpoint(alts
, 0)->bSynchAddress
& ~USB_DIR_IN
)))) {
424 snd_printk(KERN_ERR
"%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
425 dev
->devnum
, fmt
->iface
, fmt
->altsetting
,
426 is_playback
, ep
, get_endpoint(alts
, 0)->bSynchAddress
);
430 implicit_fb
= (get_endpoint(alts
, 1)->bmAttributes
& USB_ENDPOINT_USAGE_MASK
)
431 == USB_ENDPOINT_USAGE_IMPLICIT_FB
;
433 subs
->sync_endpoint
= snd_usb_add_endpoint(subs
->stream
->chip
,
434 alts
, ep
, !subs
->direction
,
436 SND_USB_ENDPOINT_TYPE_DATA
:
437 SND_USB_ENDPOINT_TYPE_SYNC
);
438 if (!subs
->sync_endpoint
)
441 subs
->data_endpoint
->sync_master
= subs
->sync_endpoint
;
447 * find a matching format and set up the interface
449 static int set_format(struct snd_usb_substream
*subs
, struct audioformat
*fmt
)
451 struct usb_device
*dev
= subs
->dev
;
452 struct usb_host_interface
*alts
;
453 struct usb_interface_descriptor
*altsd
;
454 struct usb_interface
*iface
;
457 iface
= usb_ifnum_to_if(dev
, fmt
->iface
);
460 alts
= &iface
->altsetting
[fmt
->altset_idx
];
461 altsd
= get_iface_desc(alts
);
462 if (WARN_ON(altsd
->bAlternateSetting
!= fmt
->altsetting
))
465 if (fmt
== subs
->cur_audiofmt
)
468 /* close the old interface */
469 if (subs
->interface
>= 0 && subs
->interface
!= fmt
->iface
) {
470 err
= usb_set_interface(subs
->dev
, subs
->interface
, 0);
472 snd_printk(KERN_ERR
"%d:%d:%d: return to setting 0 failed (%d)\n",
473 dev
->devnum
, fmt
->iface
, fmt
->altsetting
, err
);
476 subs
->interface
= -1;
477 subs
->altset_idx
= 0;
481 if (subs
->interface
!= fmt
->iface
||
482 subs
->altset_idx
!= fmt
->altset_idx
) {
483 err
= usb_set_interface(dev
, fmt
->iface
, fmt
->altsetting
);
485 snd_printk(KERN_ERR
"%d:%d:%d: usb_set_interface failed (%d)\n",
486 dev
->devnum
, fmt
->iface
, fmt
->altsetting
, err
);
489 snd_printdd(KERN_INFO
"setting usb interface %d:%d\n",
490 fmt
->iface
, fmt
->altsetting
);
491 subs
->interface
= fmt
->iface
;
492 subs
->altset_idx
= fmt
->altset_idx
;
494 snd_usb_set_interface_quirk(dev
);
497 subs
->data_endpoint
= snd_usb_add_endpoint(subs
->stream
->chip
,
498 alts
, fmt
->endpoint
, subs
->direction
,
499 SND_USB_ENDPOINT_TYPE_DATA
);
501 if (!subs
->data_endpoint
)
504 err
= set_sync_endpoint(subs
, fmt
, dev
, alts
, altsd
);
508 err
= snd_usb_init_pitch(subs
->stream
->chip
, fmt
->iface
, alts
, fmt
);
512 subs
->cur_audiofmt
= fmt
;
514 snd_usb_set_format_quirk(subs
, fmt
);
520 * Return the score of matching two audioformats.
521 * Veto the audioformat if:
522 * - It has no channels for some reason.
523 * - Requested PCM format is not supported.
524 * - Requested sample rate is not supported.
526 static int match_endpoint_audioformats(struct audioformat
*fp
,
527 struct audioformat
*match
, int rate
,
528 snd_pcm_format_t pcm_format
)
533 if (fp
->channels
< 1) {
534 snd_printdd("%s: (fmt @%p) no channels\n", __func__
, fp
);
538 if (!(fp
->formats
& pcm_format_to_bits(pcm_format
))) {
539 snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__
,
544 for (i
= 0; i
< fp
->nr_rates
; i
++) {
545 if (fp
->rate_table
[i
] == rate
) {
551 snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__
,
556 if (fp
->channels
== match
->channels
)
559 snd_printdd("%s: (fmt @%p) score %d\n", __func__
, fp
, score
);
565 * Configure the sync ep using the rate and pcm format of the data ep.
567 static int configure_sync_endpoint(struct snd_usb_substream
*subs
)
570 struct audioformat
*fp
;
571 struct audioformat
*sync_fp
= NULL
;
573 int sync_period_bytes
= subs
->period_bytes
;
574 struct snd_usb_substream
*sync_subs
=
575 &subs
->stream
->substream
[subs
->direction
^ 1];
577 if (subs
->sync_endpoint
->type
!= SND_USB_ENDPOINT_TYPE_DATA
||
579 return snd_usb_endpoint_set_params(subs
->sync_endpoint
,
588 /* Try to find the best matching audioformat. */
589 list_for_each_entry(fp
, &sync_subs
->fmt_list
, list
) {
590 int score
= match_endpoint_audioformats(fp
, subs
->cur_audiofmt
,
591 subs
->cur_rate
, subs
->pcm_format
);
593 if (score
> cur_score
) {
599 if (unlikely(sync_fp
== NULL
)) {
600 snd_printk(KERN_ERR
"%s: no valid audioformat for sync ep %x found\n",
601 __func__
, sync_subs
->ep_num
);
606 * Recalculate the period bytes if channel number differ between
607 * data and sync ep audioformat.
609 if (sync_fp
->channels
!= subs
->channels
) {
610 sync_period_bytes
= (subs
->period_bytes
/ subs
->channels
) *
612 snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
613 __func__
, subs
->period_bytes
, sync_period_bytes
);
616 ret
= snd_usb_endpoint_set_params(subs
->sync_endpoint
,
629 * configure endpoint params
631 * called during initial setup and upon resume
633 static int configure_endpoint(struct snd_usb_substream
*subs
)
638 stop_endpoints(subs
, true);
639 ret
= snd_usb_endpoint_set_params(subs
->data_endpoint
,
644 subs
->buffer_periods
,
647 subs
->sync_endpoint
);
651 if (subs
->sync_endpoint
)
652 ret
= configure_sync_endpoint(subs
);
660 * allocate a buffer and set the given audio format.
662 * so far we use a physically linear buffer although packetize transfer
663 * doesn't need a continuous area.
664 * if sg buffer is supported on the later version of alsa, we'll follow
667 static int snd_usb_hw_params(struct snd_pcm_substream
*substream
,
668 struct snd_pcm_hw_params
*hw_params
)
670 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
671 struct audioformat
*fmt
;
674 ret
= snd_pcm_lib_alloc_vmalloc_buffer(substream
,
675 params_buffer_bytes(hw_params
));
679 subs
->pcm_format
= params_format(hw_params
);
680 subs
->period_bytes
= params_period_bytes(hw_params
);
681 subs
->period_frames
= params_period_size(hw_params
);
682 subs
->buffer_periods
= params_periods(hw_params
);
683 subs
->channels
= params_channels(hw_params
);
684 subs
->cur_rate
= params_rate(hw_params
);
686 fmt
= find_format(subs
);
688 snd_printd(KERN_DEBUG
"cannot set format: format = %#x, rate = %d, channels = %d\n",
689 subs
->pcm_format
, subs
->cur_rate
, subs
->channels
);
693 down_read(&subs
->stream
->chip
->shutdown_rwsem
);
694 if (subs
->stream
->chip
->shutdown
)
697 ret
= set_format(subs
, fmt
);
698 up_read(&subs
->stream
->chip
->shutdown_rwsem
);
702 subs
->interface
= fmt
->iface
;
703 subs
->altset_idx
= fmt
->altset_idx
;
704 subs
->need_setup_ep
= true;
712 * reset the audio format and release the buffer
714 static int snd_usb_hw_free(struct snd_pcm_substream
*substream
)
716 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
718 subs
->cur_audiofmt
= NULL
;
720 subs
->period_bytes
= 0;
721 down_read(&subs
->stream
->chip
->shutdown_rwsem
);
722 if (!subs
->stream
->chip
->shutdown
) {
723 stop_endpoints(subs
, true);
724 snd_usb_endpoint_deactivate(subs
->sync_endpoint
);
725 snd_usb_endpoint_deactivate(subs
->data_endpoint
);
727 up_read(&subs
->stream
->chip
->shutdown_rwsem
);
728 return snd_pcm_lib_free_vmalloc_buffer(substream
);
734 * only a few subtle things...
736 static int snd_usb_pcm_prepare(struct snd_pcm_substream
*substream
)
738 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
739 struct snd_usb_substream
*subs
= runtime
->private_data
;
740 struct usb_host_interface
*alts
;
741 struct usb_interface
*iface
;
744 if (! subs
->cur_audiofmt
) {
745 snd_printk(KERN_ERR
"usbaudio: no format is specified!\n");
749 down_read(&subs
->stream
->chip
->shutdown_rwsem
);
750 if (subs
->stream
->chip
->shutdown
) {
754 if (snd_BUG_ON(!subs
->data_endpoint
)) {
759 snd_usb_endpoint_sync_pending_stop(subs
->sync_endpoint
);
760 snd_usb_endpoint_sync_pending_stop(subs
->data_endpoint
);
762 ret
= set_format(subs
, subs
->cur_audiofmt
);
766 iface
= usb_ifnum_to_if(subs
->dev
, subs
->cur_audiofmt
->iface
);
767 alts
= &iface
->altsetting
[subs
->cur_audiofmt
->altset_idx
];
768 ret
= snd_usb_init_sample_rate(subs
->stream
->chip
,
769 subs
->cur_audiofmt
->iface
,
776 if (subs
->need_setup_ep
) {
777 ret
= configure_endpoint(subs
);
780 subs
->need_setup_ep
= false;
783 /* some unit conversions in runtime */
784 subs
->data_endpoint
->maxframesize
=
785 bytes_to_frames(runtime
, subs
->data_endpoint
->maxpacksize
);
786 subs
->data_endpoint
->curframesize
=
787 bytes_to_frames(runtime
, subs
->data_endpoint
->curpacksize
);
789 /* reset the pointer */
790 subs
->hwptr_done
= 0;
791 subs
->transfer_done
= 0;
792 subs
->last_delay
= 0;
793 subs
->last_frame_number
= 0;
796 /* for playback, submit the URBs now; otherwise, the first hwptr_done
797 * updates for all URBs would happen at the same time when starting */
798 if (subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
)
799 ret
= start_endpoints(subs
, true);
802 up_read(&subs
->stream
->chip
->shutdown_rwsem
);
806 static struct snd_pcm_hardware snd_usb_hardware
=
808 .info
= SNDRV_PCM_INFO_MMAP
|
809 SNDRV_PCM_INFO_MMAP_VALID
|
810 SNDRV_PCM_INFO_BATCH
|
811 SNDRV_PCM_INFO_INTERLEAVED
|
812 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
813 SNDRV_PCM_INFO_PAUSE
,
814 .buffer_bytes_max
= 1024 * 1024,
815 .period_bytes_min
= 64,
816 .period_bytes_max
= 512 * 1024,
821 static int hw_check_valid_format(struct snd_usb_substream
*subs
,
822 struct snd_pcm_hw_params
*params
,
823 struct audioformat
*fp
)
825 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
826 struct snd_interval
*ct
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
827 struct snd_mask
*fmts
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
828 struct snd_interval
*pt
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
);
829 struct snd_mask check_fmts
;
832 /* check the format */
833 snd_mask_none(&check_fmts
);
834 check_fmts
.bits
[0] = (u32
)fp
->formats
;
835 check_fmts
.bits
[1] = (u32
)(fp
->formats
>> 32);
836 snd_mask_intersect(&check_fmts
, fmts
);
837 if (snd_mask_empty(&check_fmts
)) {
838 hwc_debug(" > check: no supported format %d\n", fp
->format
);
841 /* check the channels */
842 if (fp
->channels
< ct
->min
|| fp
->channels
> ct
->max
) {
843 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp
->channels
, ct
->min
, ct
->max
);
846 /* check the rate is within the range */
847 if (fp
->rate_min
> it
->max
|| (fp
->rate_min
== it
->max
&& it
->openmax
)) {
848 hwc_debug(" > check: rate_min %d > max %d\n", fp
->rate_min
, it
->max
);
851 if (fp
->rate_max
< it
->min
|| (fp
->rate_max
== it
->min
&& it
->openmin
)) {
852 hwc_debug(" > check: rate_max %d < min %d\n", fp
->rate_max
, it
->min
);
855 /* check whether the period time is >= the data packet interval */
856 if (subs
->speed
!= USB_SPEED_FULL
) {
857 ptime
= 125 * (1 << fp
->datainterval
);
858 if (ptime
> pt
->max
|| (ptime
== pt
->max
&& pt
->openmax
)) {
859 hwc_debug(" > check: ptime %u > max %u\n", ptime
, pt
->max
);
866 static int hw_rule_rate(struct snd_pcm_hw_params
*params
,
867 struct snd_pcm_hw_rule
*rule
)
869 struct snd_usb_substream
*subs
= rule
->private;
870 struct audioformat
*fp
;
871 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
872 unsigned int rmin
, rmax
;
875 hwc_debug("hw_rule_rate: (%d,%d)\n", it
->min
, it
->max
);
878 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
879 if (!hw_check_valid_format(subs
, params
, fp
))
882 if (rmin
> fp
->rate_min
)
884 if (rmax
< fp
->rate_max
)
893 hwc_debug(" --> get empty\n");
899 if (it
->min
< rmin
) {
904 if (it
->max
> rmax
) {
909 if (snd_interval_checkempty(it
)) {
913 hwc_debug(" --> (%d, %d) (changed = %d)\n", it
->min
, it
->max
, changed
);
918 static int hw_rule_channels(struct snd_pcm_hw_params
*params
,
919 struct snd_pcm_hw_rule
*rule
)
921 struct snd_usb_substream
*subs
= rule
->private;
922 struct audioformat
*fp
;
923 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
924 unsigned int rmin
, rmax
;
927 hwc_debug("hw_rule_channels: (%d,%d)\n", it
->min
, it
->max
);
930 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
931 if (!hw_check_valid_format(subs
, params
, fp
))
934 if (rmin
> fp
->channels
)
936 if (rmax
< fp
->channels
)
945 hwc_debug(" --> get empty\n");
951 if (it
->min
< rmin
) {
956 if (it
->max
> rmax
) {
961 if (snd_interval_checkempty(it
)) {
965 hwc_debug(" --> (%d, %d) (changed = %d)\n", it
->min
, it
->max
, changed
);
969 static int hw_rule_format(struct snd_pcm_hw_params
*params
,
970 struct snd_pcm_hw_rule
*rule
)
972 struct snd_usb_substream
*subs
= rule
->private;
973 struct audioformat
*fp
;
974 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
979 hwc_debug("hw_rule_format: %x:%x\n", fmt
->bits
[0], fmt
->bits
[1]);
981 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
982 if (!hw_check_valid_format(subs
, params
, fp
))
984 fbits
|= fp
->formats
;
987 oldbits
[0] = fmt
->bits
[0];
988 oldbits
[1] = fmt
->bits
[1];
989 fmt
->bits
[0] &= (u32
)fbits
;
990 fmt
->bits
[1] &= (u32
)(fbits
>> 32);
991 if (!fmt
->bits
[0] && !fmt
->bits
[1]) {
992 hwc_debug(" --> get empty\n");
995 changed
= (oldbits
[0] != fmt
->bits
[0] || oldbits
[1] != fmt
->bits
[1]);
996 hwc_debug(" --> %x:%x (changed = %d)\n", fmt
->bits
[0], fmt
->bits
[1], changed
);
1000 static int hw_rule_period_time(struct snd_pcm_hw_params
*params
,
1001 struct snd_pcm_hw_rule
*rule
)
1003 struct snd_usb_substream
*subs
= rule
->private;
1004 struct audioformat
*fp
;
1005 struct snd_interval
*it
;
1006 unsigned char min_datainterval
;
1010 it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
);
1011 hwc_debug("hw_rule_period_time: (%u,%u)\n", it
->min
, it
->max
);
1012 min_datainterval
= 0xff;
1013 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1014 if (!hw_check_valid_format(subs
, params
, fp
))
1016 min_datainterval
= min(min_datainterval
, fp
->datainterval
);
1018 if (min_datainterval
== 0xff) {
1019 hwc_debug(" --> get empty\n");
1023 pmin
= 125 * (1 << min_datainterval
);
1025 if (it
->min
< pmin
) {
1030 if (snd_interval_checkempty(it
)) {
1034 hwc_debug(" --> (%u,%u) (changed = %d)\n", it
->min
, it
->max
, changed
);
1039 * If the device supports unusual bit rates, does the request meet these?
1041 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime
*runtime
,
1042 struct snd_usb_substream
*subs
)
1044 struct audioformat
*fp
;
1046 int count
= 0, needs_knot
= 0;
1049 kfree(subs
->rate_list
.list
);
1050 subs
->rate_list
.list
= NULL
;
1052 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1053 if (fp
->rates
& SNDRV_PCM_RATE_CONTINUOUS
)
1055 count
+= fp
->nr_rates
;
1056 if (fp
->rates
& SNDRV_PCM_RATE_KNOT
)
1062 subs
->rate_list
.list
= rate_list
=
1063 kmalloc(sizeof(int) * count
, GFP_KERNEL
);
1064 if (!subs
->rate_list
.list
)
1066 subs
->rate_list
.count
= count
;
1067 subs
->rate_list
.mask
= 0;
1069 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1071 for (i
= 0; i
< fp
->nr_rates
; i
++)
1072 rate_list
[count
++] = fp
->rate_table
[i
];
1074 err
= snd_pcm_hw_constraint_list(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1084 * set up the runtime hardware information.
1087 static int setup_hw_info(struct snd_pcm_runtime
*runtime
, struct snd_usb_substream
*subs
)
1089 struct audioformat
*fp
;
1090 unsigned int pt
, ptmin
;
1091 int param_period_time_if_needed
;
1094 runtime
->hw
.formats
= subs
->formats
;
1096 runtime
->hw
.rate_min
= 0x7fffffff;
1097 runtime
->hw
.rate_max
= 0;
1098 runtime
->hw
.channels_min
= 256;
1099 runtime
->hw
.channels_max
= 0;
1100 runtime
->hw
.rates
= 0;
1102 /* check min/max rates and channels */
1103 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1104 runtime
->hw
.rates
|= fp
->rates
;
1105 if (runtime
->hw
.rate_min
> fp
->rate_min
)
1106 runtime
->hw
.rate_min
= fp
->rate_min
;
1107 if (runtime
->hw
.rate_max
< fp
->rate_max
)
1108 runtime
->hw
.rate_max
= fp
->rate_max
;
1109 if (runtime
->hw
.channels_min
> fp
->channels
)
1110 runtime
->hw
.channels_min
= fp
->channels
;
1111 if (runtime
->hw
.channels_max
< fp
->channels
)
1112 runtime
->hw
.channels_max
= fp
->channels
;
1113 if (fp
->fmt_type
== UAC_FORMAT_TYPE_II
&& fp
->frame_size
> 0) {
1114 /* FIXME: there might be more than one audio formats... */
1115 runtime
->hw
.period_bytes_min
= runtime
->hw
.period_bytes_max
=
1118 pt
= 125 * (1 << fp
->datainterval
);
1119 ptmin
= min(ptmin
, pt
);
1121 err
= snd_usb_autoresume(subs
->stream
->chip
);
1125 param_period_time_if_needed
= SNDRV_PCM_HW_PARAM_PERIOD_TIME
;
1126 if (subs
->speed
== USB_SPEED_FULL
)
1127 /* full speed devices have fixed data packet interval */
1130 /* if period time doesn't go below 1 ms, no rules needed */
1131 param_period_time_if_needed
= -1;
1132 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1135 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1137 SNDRV_PCM_HW_PARAM_FORMAT
,
1138 SNDRV_PCM_HW_PARAM_CHANNELS
,
1139 param_period_time_if_needed
,
1142 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1143 hw_rule_channels
, subs
,
1144 SNDRV_PCM_HW_PARAM_FORMAT
,
1145 SNDRV_PCM_HW_PARAM_RATE
,
1146 param_period_time_if_needed
,
1149 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1150 hw_rule_format
, subs
,
1151 SNDRV_PCM_HW_PARAM_RATE
,
1152 SNDRV_PCM_HW_PARAM_CHANNELS
,
1153 param_period_time_if_needed
,
1156 if (param_period_time_if_needed
>= 0) {
1157 err
= snd_pcm_hw_rule_add(runtime
, 0,
1158 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1159 hw_rule_period_time
, subs
,
1160 SNDRV_PCM_HW_PARAM_FORMAT
,
1161 SNDRV_PCM_HW_PARAM_CHANNELS
,
1162 SNDRV_PCM_HW_PARAM_RATE
,
1167 if ((err
= snd_usb_pcm_check_knot(runtime
, subs
)) < 0)
1172 snd_usb_autosuspend(subs
->stream
->chip
);
1176 static int snd_usb_pcm_open(struct snd_pcm_substream
*substream
, int direction
)
1178 struct snd_usb_stream
*as
= snd_pcm_substream_chip(substream
);
1179 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1180 struct snd_usb_substream
*subs
= &as
->substream
[direction
];
1182 subs
->interface
= -1;
1183 subs
->altset_idx
= 0;
1184 runtime
->hw
= snd_usb_hardware
;
1185 runtime
->private_data
= subs
;
1186 subs
->pcm_substream
= substream
;
1187 /* runtime PM is also done there */
1189 /* initialize DSD/DOP context */
1190 subs
->dsd_dop
.byte_idx
= 0;
1191 subs
->dsd_dop
.channel
= 0;
1192 subs
->dsd_dop
.marker
= 1;
1194 return setup_hw_info(runtime
, subs
);
1197 static int snd_usb_pcm_close(struct snd_pcm_substream
*substream
, int direction
)
1199 struct snd_usb_stream
*as
= snd_pcm_substream_chip(substream
);
1200 struct snd_usb_substream
*subs
= &as
->substream
[direction
];
1202 stop_endpoints(subs
, true);
1204 if (!as
->chip
->shutdown
&& subs
->interface
>= 0) {
1205 usb_set_interface(subs
->dev
, subs
->interface
, 0);
1206 subs
->interface
= -1;
1209 subs
->pcm_substream
= NULL
;
1210 snd_usb_autosuspend(subs
->stream
->chip
);
1215 /* Since a URB can handle only a single linear buffer, we must use double
1216 * buffering when the data to be transferred overflows the buffer boundary.
1217 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1220 static void retire_capture_urb(struct snd_usb_substream
*subs
,
1223 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1224 unsigned int stride
, frames
, bytes
, oldptr
;
1225 int i
, period_elapsed
= 0;
1226 unsigned long flags
;
1228 int current_frame_number
;
1230 /* read frame number here, update pointer in critical section */
1231 current_frame_number
= usb_get_current_frame_number(subs
->dev
);
1233 stride
= runtime
->frame_bits
>> 3;
1235 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1236 cp
= (unsigned char *)urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
+ subs
->pkt_offset_adj
;
1237 if (urb
->iso_frame_desc
[i
].status
&& printk_ratelimit()) {
1238 snd_printdd(KERN_ERR
"frame %d active: %d\n", i
, urb
->iso_frame_desc
[i
].status
);
1241 bytes
= urb
->iso_frame_desc
[i
].actual_length
;
1242 frames
= bytes
/ stride
;
1243 if (!subs
->txfr_quirk
)
1244 bytes
= frames
* stride
;
1245 if (bytes
% (runtime
->sample_bits
>> 3) != 0) {
1246 int oldbytes
= bytes
;
1247 bytes
= frames
* stride
;
1248 snd_printdd(KERN_ERR
"Corrected urb data len. %d->%d\n",
1251 /* update the current pointer */
1252 spin_lock_irqsave(&subs
->lock
, flags
);
1253 oldptr
= subs
->hwptr_done
;
1254 subs
->hwptr_done
+= bytes
;
1255 if (subs
->hwptr_done
>= runtime
->buffer_size
* stride
)
1256 subs
->hwptr_done
-= runtime
->buffer_size
* stride
;
1257 frames
= (bytes
+ (oldptr
% stride
)) / stride
;
1258 subs
->transfer_done
+= frames
;
1259 if (subs
->transfer_done
>= runtime
->period_size
) {
1260 subs
->transfer_done
-= runtime
->period_size
;
1263 /* capture delay is by construction limited to one URB,
1266 runtime
->delay
= subs
->last_delay
= 0;
1268 /* realign last_frame_number */
1269 subs
->last_frame_number
= current_frame_number
;
1270 subs
->last_frame_number
&= 0xFF; /* keep 8 LSBs */
1272 spin_unlock_irqrestore(&subs
->lock
, flags
);
1273 /* copy a data chunk */
1274 if (oldptr
+ bytes
> runtime
->buffer_size
* stride
) {
1275 unsigned int bytes1
=
1276 runtime
->buffer_size
* stride
- oldptr
;
1277 memcpy(runtime
->dma_area
+ oldptr
, cp
, bytes1
);
1278 memcpy(runtime
->dma_area
, cp
+ bytes1
, bytes
- bytes1
);
1280 memcpy(runtime
->dma_area
+ oldptr
, cp
, bytes
);
1285 snd_pcm_period_elapsed(subs
->pcm_substream
);
1288 static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream
*subs
,
1289 struct urb
*urb
, unsigned int bytes
)
1291 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1292 unsigned int stride
= runtime
->frame_bits
>> 3;
1293 unsigned int dst_idx
= 0;
1294 unsigned int src_idx
= subs
->hwptr_done
;
1295 unsigned int wrap
= runtime
->buffer_size
* stride
;
1296 u8
*dst
= urb
->transfer_buffer
;
1297 u8
*src
= runtime
->dma_area
;
1298 u8 marker
[] = { 0x05, 0xfa };
1301 * The DSP DOP format defines a way to transport DSD samples over
1302 * normal PCM data endpoints. It requires stuffing of marker bytes
1303 * (0x05 and 0xfa, alternating per sample frame), and then expects
1304 * 2 additional bytes of actual payload. The whole frame is stored
1307 * Hence, for a stereo transport, the buffer layout looks like this,
1308 * where L refers to left channel samples and R to right.
1310 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1311 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1317 if (++subs
->dsd_dop
.byte_idx
== 3) {
1318 /* frame boundary? */
1319 dst
[dst_idx
++] = marker
[subs
->dsd_dop
.marker
];
1321 subs
->dsd_dop
.byte_idx
= 0;
1323 if (++subs
->dsd_dop
.channel
% runtime
->channels
== 0) {
1324 /* alternate the marker */
1325 subs
->dsd_dop
.marker
++;
1326 subs
->dsd_dop
.marker
%= ARRAY_SIZE(marker
);
1327 subs
->dsd_dop
.channel
= 0;
1330 /* stuff the DSD payload */
1331 int idx
= (src_idx
+ subs
->dsd_dop
.byte_idx
- 1) % wrap
;
1333 if (subs
->cur_audiofmt
->dsd_bitrev
)
1334 dst
[dst_idx
++] = bitrev8(src
[idx
]);
1336 dst
[dst_idx
++] = src
[idx
];
1343 static void prepare_playback_urb(struct snd_usb_substream
*subs
,
1346 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1347 struct snd_usb_endpoint
*ep
= subs
->data_endpoint
;
1348 struct snd_urb_ctx
*ctx
= urb
->context
;
1349 unsigned int counts
, frames
, bytes
;
1350 int i
, stride
, period_elapsed
= 0;
1351 unsigned long flags
;
1353 stride
= runtime
->frame_bits
>> 3;
1356 urb
->number_of_packets
= 0;
1357 spin_lock_irqsave(&subs
->lock
, flags
);
1358 subs
->frame_limit
+= ep
->max_urb_frames
;
1359 for (i
= 0; i
< ctx
->packets
; i
++) {
1360 if (ctx
->packet_size
[i
])
1361 counts
= ctx
->packet_size
[i
];
1363 counts
= snd_usb_endpoint_next_packet_size(ep
);
1365 /* set up descriptor */
1366 urb
->iso_frame_desc
[i
].offset
= frames
* ep
->stride
;
1367 urb
->iso_frame_desc
[i
].length
= counts
* ep
->stride
;
1369 urb
->number_of_packets
++;
1370 subs
->transfer_done
+= counts
;
1371 if (subs
->transfer_done
>= runtime
->period_size
) {
1372 subs
->transfer_done
-= runtime
->period_size
;
1373 subs
->frame_limit
= 0;
1375 if (subs
->fmt_type
== UAC_FORMAT_TYPE_II
) {
1376 if (subs
->transfer_done
> 0) {
1377 /* FIXME: fill-max mode is not
1379 frames
-= subs
->transfer_done
;
1380 counts
-= subs
->transfer_done
;
1381 urb
->iso_frame_desc
[i
].length
=
1382 counts
* ep
->stride
;
1383 subs
->transfer_done
= 0;
1386 if (i
< ctx
->packets
) {
1387 /* add a transfer delimiter */
1388 urb
->iso_frame_desc
[i
].offset
=
1389 frames
* ep
->stride
;
1390 urb
->iso_frame_desc
[i
].length
= 0;
1391 urb
->number_of_packets
++;
1396 /* finish at the period boundary or after enough frames */
1397 if ((period_elapsed
||
1398 subs
->transfer_done
>= subs
->frame_limit
) &&
1399 !snd_usb_endpoint_implicit_feedback_sink(ep
))
1402 bytes
= frames
* ep
->stride
;
1404 if (unlikely(subs
->pcm_format
== SNDRV_PCM_FORMAT_DSD_U16_LE
&&
1405 subs
->cur_audiofmt
->dsd_dop
)) {
1406 fill_playback_urb_dsd_dop(subs
, urb
, bytes
);
1407 } else if (unlikely(subs
->pcm_format
== SNDRV_PCM_FORMAT_DSD_U8
&&
1408 subs
->cur_audiofmt
->dsd_bitrev
)) {
1409 /* bit-reverse the bytes */
1410 u8
*buf
= urb
->transfer_buffer
;
1411 for (i
= 0; i
< bytes
; i
++) {
1412 int idx
= (subs
->hwptr_done
+ i
)
1413 % (runtime
->buffer_size
* stride
);
1414 buf
[i
] = bitrev8(runtime
->dma_area
[idx
]);
1417 subs
->hwptr_done
+= bytes
;
1420 if (subs
->hwptr_done
+ bytes
> runtime
->buffer_size
* stride
) {
1421 /* err, the transferred area goes over buffer boundary. */
1422 unsigned int bytes1
=
1423 runtime
->buffer_size
* stride
- subs
->hwptr_done
;
1424 memcpy(urb
->transfer_buffer
,
1425 runtime
->dma_area
+ subs
->hwptr_done
, bytes1
);
1426 memcpy(urb
->transfer_buffer
+ bytes1
,
1427 runtime
->dma_area
, bytes
- bytes1
);
1429 memcpy(urb
->transfer_buffer
,
1430 runtime
->dma_area
+ subs
->hwptr_done
, bytes
);
1433 subs
->hwptr_done
+= bytes
;
1436 if (subs
->hwptr_done
>= runtime
->buffer_size
* stride
)
1437 subs
->hwptr_done
-= runtime
->buffer_size
* stride
;
1439 /* update delay with exact number of samples queued */
1440 runtime
->delay
= subs
->last_delay
;
1441 runtime
->delay
+= frames
;
1442 subs
->last_delay
= runtime
->delay
;
1444 /* realign last_frame_number */
1445 subs
->last_frame_number
= usb_get_current_frame_number(subs
->dev
);
1446 subs
->last_frame_number
&= 0xFF; /* keep 8 LSBs */
1448 spin_unlock_irqrestore(&subs
->lock
, flags
);
1449 urb
->transfer_buffer_length
= bytes
;
1451 snd_pcm_period_elapsed(subs
->pcm_substream
);
1455 * process after playback data complete
1456 * - decrease the delay count again
1458 static void retire_playback_urb(struct snd_usb_substream
*subs
,
1461 unsigned long flags
;
1462 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1463 struct snd_usb_endpoint
*ep
= subs
->data_endpoint
;
1464 int processed
= urb
->transfer_buffer_length
/ ep
->stride
;
1467 /* ignore the delay accounting when procssed=0 is given, i.e.
1468 * silent payloads are procssed before handling the actual data
1473 spin_lock_irqsave(&subs
->lock
, flags
);
1474 if (!subs
->last_delay
)
1475 goto out
; /* short path */
1477 est_delay
= snd_usb_pcm_delay(subs
, runtime
->rate
);
1478 /* update delay with exact number of samples played */
1479 if (processed
> subs
->last_delay
)
1480 subs
->last_delay
= 0;
1482 subs
->last_delay
-= processed
;
1483 runtime
->delay
= subs
->last_delay
;
1486 * Report when delay estimate is off by more than 2ms.
1487 * The error should be lower than 2ms since the estimate relies
1488 * on two reads of a counter updated every ms.
1490 if (abs(est_delay
- subs
->last_delay
) * 1000 > runtime
->rate
* 2)
1491 snd_printk(KERN_DEBUG
"delay: estimated %d, actual %d\n",
1492 est_delay
, subs
->last_delay
);
1494 if (!subs
->running
) {
1495 /* update last_frame_number for delay counting here since
1496 * prepare_playback_urb won't be called during pause
1498 subs
->last_frame_number
=
1499 usb_get_current_frame_number(subs
->dev
) & 0xff;
1503 spin_unlock_irqrestore(&subs
->lock
, flags
);
1506 static int snd_usb_playback_open(struct snd_pcm_substream
*substream
)
1508 return snd_usb_pcm_open(substream
, SNDRV_PCM_STREAM_PLAYBACK
);
1511 static int snd_usb_playback_close(struct snd_pcm_substream
*substream
)
1513 return snd_usb_pcm_close(substream
, SNDRV_PCM_STREAM_PLAYBACK
);
1516 static int snd_usb_capture_open(struct snd_pcm_substream
*substream
)
1518 return snd_usb_pcm_open(substream
, SNDRV_PCM_STREAM_CAPTURE
);
1521 static int snd_usb_capture_close(struct snd_pcm_substream
*substream
)
1523 return snd_usb_pcm_close(substream
, SNDRV_PCM_STREAM_CAPTURE
);
1526 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream
*substream
,
1529 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
1532 case SNDRV_PCM_TRIGGER_START
:
1533 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1534 subs
->data_endpoint
->prepare_data_urb
= prepare_playback_urb
;
1535 subs
->data_endpoint
->retire_data_urb
= retire_playback_urb
;
1538 case SNDRV_PCM_TRIGGER_STOP
:
1539 stop_endpoints(subs
, false);
1542 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1543 subs
->data_endpoint
->prepare_data_urb
= NULL
;
1544 /* keep retire_data_urb for delay calculation */
1545 subs
->data_endpoint
->retire_data_urb
= retire_playback_urb
;
1553 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream
*substream
,
1557 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
1560 case SNDRV_PCM_TRIGGER_START
:
1561 err
= start_endpoints(subs
, false);
1565 subs
->data_endpoint
->retire_data_urb
= retire_capture_urb
;
1568 case SNDRV_PCM_TRIGGER_STOP
:
1569 stop_endpoints(subs
, false);
1572 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1573 subs
->data_endpoint
->retire_data_urb
= NULL
;
1576 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1577 subs
->data_endpoint
->retire_data_urb
= retire_capture_urb
;
1585 static struct snd_pcm_ops snd_usb_playback_ops
= {
1586 .open
= snd_usb_playback_open
,
1587 .close
= snd_usb_playback_close
,
1588 .ioctl
= snd_pcm_lib_ioctl
,
1589 .hw_params
= snd_usb_hw_params
,
1590 .hw_free
= snd_usb_hw_free
,
1591 .prepare
= snd_usb_pcm_prepare
,
1592 .trigger
= snd_usb_substream_playback_trigger
,
1593 .pointer
= snd_usb_pcm_pointer
,
1594 .page
= snd_pcm_lib_get_vmalloc_page
,
1595 .mmap
= snd_pcm_lib_mmap_vmalloc
,
1598 static struct snd_pcm_ops snd_usb_capture_ops
= {
1599 .open
= snd_usb_capture_open
,
1600 .close
= snd_usb_capture_close
,
1601 .ioctl
= snd_pcm_lib_ioctl
,
1602 .hw_params
= snd_usb_hw_params
,
1603 .hw_free
= snd_usb_hw_free
,
1604 .prepare
= snd_usb_pcm_prepare
,
1605 .trigger
= snd_usb_substream_capture_trigger
,
1606 .pointer
= snd_usb_pcm_pointer
,
1607 .page
= snd_pcm_lib_get_vmalloc_page
,
1608 .mmap
= snd_pcm_lib_mmap_vmalloc
,
1611 void snd_usb_set_pcm_ops(struct snd_pcm
*pcm
, int stream
)
1613 snd_pcm_set_ops(pcm
, stream
,
1614 stream
== SNDRV_PCM_STREAM_PLAYBACK
?
1615 &snd_usb_playback_ops
: &snd_usb_capture_ops
);