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
= substream
->runtime
->private_data
;
80 unsigned int hwptr_done
;
82 if (atomic_read(&subs
->stream
->chip
->shutdown
))
83 return SNDRV_PCM_POS_XRUN
;
84 spin_lock(&subs
->lock
);
85 hwptr_done
= subs
->hwptr_done
;
86 substream
->runtime
->delay
= snd_usb_pcm_delay(subs
,
87 substream
->runtime
->rate
);
88 spin_unlock(&subs
->lock
);
89 return hwptr_done
/ (substream
->runtime
->frame_bits
>> 3);
93 * find a matching audio format
95 static struct audioformat
*find_format(struct snd_usb_substream
*subs
)
97 struct audioformat
*fp
;
98 struct audioformat
*found
= NULL
;
99 int cur_attr
= 0, attr
;
101 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
102 if (!(fp
->formats
& pcm_format_to_bits(subs
->pcm_format
)))
104 if (fp
->channels
!= subs
->channels
)
106 if (subs
->cur_rate
< fp
->rate_min
||
107 subs
->cur_rate
> fp
->rate_max
)
109 if (! (fp
->rates
& SNDRV_PCM_RATE_CONTINUOUS
)) {
111 for (i
= 0; i
< fp
->nr_rates
; i
++)
112 if (fp
->rate_table
[i
] == subs
->cur_rate
)
114 if (i
>= fp
->nr_rates
)
117 attr
= fp
->ep_attr
& USB_ENDPOINT_SYNCTYPE
;
123 /* avoid async out and adaptive in if the other method
124 * supports the same format.
125 * this is a workaround for the case like
126 * M-audio audiophile USB.
128 if (attr
!= cur_attr
) {
129 if ((attr
== USB_ENDPOINT_SYNC_ASYNC
&&
130 subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
) ||
131 (attr
== USB_ENDPOINT_SYNC_ADAPTIVE
&&
132 subs
->direction
== SNDRV_PCM_STREAM_CAPTURE
))
134 if ((cur_attr
== USB_ENDPOINT_SYNC_ASYNC
&&
135 subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
) ||
136 (cur_attr
== USB_ENDPOINT_SYNC_ADAPTIVE
&&
137 subs
->direction
== SNDRV_PCM_STREAM_CAPTURE
)) {
143 /* find the format with the largest max. packet size */
144 if (fp
->maxpacksize
> found
->maxpacksize
) {
152 static int init_pitch_v1(struct snd_usb_audio
*chip
, int iface
,
153 struct usb_host_interface
*alts
,
154 struct audioformat
*fmt
)
156 struct usb_device
*dev
= chip
->dev
;
158 unsigned char data
[1];
161 if (get_iface_desc(alts
)->bNumEndpoints
< 1)
163 ep
= get_endpoint(alts
, 0)->bEndpointAddress
;
166 err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC_SET_CUR
,
167 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
168 UAC_EP_CS_ATTR_PITCH_CONTROL
<< 8, ep
,
171 usb_audio_err(chip
, "%d:%d: cannot set enable PITCH\n",
179 static int init_pitch_v2(struct snd_usb_audio
*chip
, int iface
,
180 struct usb_host_interface
*alts
,
181 struct audioformat
*fmt
)
183 struct usb_device
*dev
= chip
->dev
;
184 unsigned char data
[1];
188 err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC2_CS_CUR
,
189 USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
| USB_DIR_OUT
,
190 UAC2_EP_CS_PITCH
<< 8, 0,
193 usb_audio_err(chip
, "%d:%d: cannot set enable PITCH (v2)\n",
194 iface
, fmt
->altsetting
);
202 * initialize the pitch control and sample rate
204 int snd_usb_init_pitch(struct snd_usb_audio
*chip
, int iface
,
205 struct usb_host_interface
*alts
,
206 struct audioformat
*fmt
)
208 /* if endpoint doesn't have pitch control, bail out */
209 if (!(fmt
->attributes
& UAC_EP_CS_ATTR_PITCH_CONTROL
))
212 switch (fmt
->protocol
) {
215 return init_pitch_v1(chip
, iface
, alts
, fmt
);
218 return init_pitch_v2(chip
, iface
, alts
, fmt
);
222 static int start_endpoints(struct snd_usb_substream
*subs
)
226 if (!subs
->data_endpoint
)
229 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED
, &subs
->flags
)) {
230 struct snd_usb_endpoint
*ep
= subs
->data_endpoint
;
232 dev_dbg(&subs
->dev
->dev
, "Starting data EP @%p\n", ep
);
234 ep
->data_subs
= subs
;
235 err
= snd_usb_endpoint_start(ep
);
237 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED
, &subs
->flags
);
242 if (subs
->sync_endpoint
&&
243 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED
, &subs
->flags
)) {
244 struct snd_usb_endpoint
*ep
= subs
->sync_endpoint
;
246 if (subs
->data_endpoint
->iface
!= subs
->sync_endpoint
->iface
||
247 subs
->data_endpoint
->altsetting
!= subs
->sync_endpoint
->altsetting
) {
248 err
= usb_set_interface(subs
->dev
,
249 subs
->sync_endpoint
->iface
,
250 subs
->sync_endpoint
->altsetting
);
252 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED
, &subs
->flags
);
253 dev_err(&subs
->dev
->dev
,
254 "%d:%d: cannot set interface (%d)\n",
255 subs
->sync_endpoint
->iface
,
256 subs
->sync_endpoint
->altsetting
, err
);
261 dev_dbg(&subs
->dev
->dev
, "Starting sync EP @%p\n", ep
);
263 ep
->sync_slave
= subs
->data_endpoint
;
264 err
= snd_usb_endpoint_start(ep
);
266 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED
, &subs
->flags
);
274 static void stop_endpoints(struct snd_usb_substream
*subs
, bool wait
)
276 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED
, &subs
->flags
))
277 snd_usb_endpoint_stop(subs
->sync_endpoint
);
279 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED
, &subs
->flags
))
280 snd_usb_endpoint_stop(subs
->data_endpoint
);
283 snd_usb_endpoint_sync_pending_stop(subs
->sync_endpoint
);
284 snd_usb_endpoint_sync_pending_stop(subs
->data_endpoint
);
288 static int search_roland_implicit_fb(struct usb_device
*dev
, int ifnum
,
289 unsigned int altsetting
,
290 struct usb_host_interface
**alts
,
293 struct usb_interface
*iface
;
294 struct usb_interface_descriptor
*altsd
;
295 struct usb_endpoint_descriptor
*epd
;
297 iface
= usb_ifnum_to_if(dev
, ifnum
);
298 if (!iface
|| iface
->num_altsetting
< altsetting
+ 1)
300 *alts
= &iface
->altsetting
[altsetting
];
301 altsd
= get_iface_desc(*alts
);
302 if (altsd
->bAlternateSetting
!= altsetting
||
303 altsd
->bInterfaceClass
!= USB_CLASS_VENDOR_SPEC
||
304 (altsd
->bInterfaceSubClass
!= 2 &&
305 altsd
->bInterfaceProtocol
!= 2 ) ||
306 altsd
->bNumEndpoints
< 1)
308 epd
= get_endpoint(*alts
, 0);
309 if (!usb_endpoint_is_isoc_in(epd
) ||
310 (epd
->bmAttributes
& USB_ENDPOINT_USAGE_MASK
) !=
311 USB_ENDPOINT_USAGE_IMPLICIT_FB
)
313 *ep
= epd
->bEndpointAddress
;
317 /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
318 * applies. Returns 1 if a quirk was found.
320 static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream
*subs
,
321 struct usb_device
*dev
,
322 struct usb_interface_descriptor
*altsd
,
325 struct usb_host_interface
*alts
;
326 struct usb_interface
*iface
;
330 /* Implicit feedback sync EPs consumers are always playback EPs */
331 if (subs
->direction
!= SNDRV_PCM_STREAM_PLAYBACK
)
334 switch (subs
->stream
->chip
->usb_id
) {
335 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
336 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
339 goto add_sync_ep_from_ifnum
;
340 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
341 case USB_ID(0x0763, 0x2081):
344 goto add_sync_ep_from_ifnum
;
345 case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */
348 goto add_sync_ep_from_ifnum
;
349 case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */
352 goto add_sync_ep_from_ifnum
;
353 case USB_ID(0x1397, 0x0001): /* Behringer UFX1604 */
354 case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */
357 goto add_sync_ep_from_ifnum
;
358 case USB_ID(0x0582, 0x01d8): /* BOSS Katana */
359 /* BOSS Katana amplifiers do not need quirks */
363 if (attr
== USB_ENDPOINT_SYNC_ASYNC
&&
364 altsd
->bInterfaceClass
== USB_CLASS_VENDOR_SPEC
&&
365 altsd
->bInterfaceProtocol
== 2 &&
366 altsd
->bNumEndpoints
== 1 &&
367 USB_ID_VENDOR(subs
->stream
->chip
->usb_id
) == 0x0582 /* Roland */ &&
368 search_roland_implicit_fb(dev
, altsd
->bInterfaceNumber
+ 1,
369 altsd
->bAlternateSetting
,
377 add_sync_ep_from_ifnum
:
378 iface
= usb_ifnum_to_if(dev
, ifnum
);
380 if (!iface
|| iface
->num_altsetting
< 2)
383 alts
= &iface
->altsetting
[1];
386 subs
->sync_endpoint
= snd_usb_add_endpoint(subs
->stream
->chip
,
387 alts
, ep
, !subs
->direction
,
388 SND_USB_ENDPOINT_TYPE_DATA
);
389 if (!subs
->sync_endpoint
)
392 subs
->data_endpoint
->sync_master
= subs
->sync_endpoint
;
397 static int set_sync_endpoint(struct snd_usb_substream
*subs
,
398 struct audioformat
*fmt
,
399 struct usb_device
*dev
,
400 struct usb_host_interface
*alts
,
401 struct usb_interface_descriptor
*altsd
)
403 int is_playback
= subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
;
404 unsigned int ep
, attr
;
408 /* we need a sync pipe in async OUT or adaptive IN mode */
409 /* check the number of EP, since some devices have broken
410 * descriptors which fool us. if it has only one EP,
411 * assume it as adaptive-out or sync-in.
413 attr
= fmt
->ep_attr
& USB_ENDPOINT_SYNCTYPE
;
415 if ((is_playback
&& (attr
!= USB_ENDPOINT_SYNC_ASYNC
)) ||
416 (!is_playback
&& (attr
!= USB_ENDPOINT_SYNC_ADAPTIVE
))) {
419 * In these modes the notion of sync_endpoint is irrelevant.
420 * Reset pointers to avoid using stale data from previously
421 * used settings, e.g. when configuration and endpoints were
425 subs
->sync_endpoint
= NULL
;
426 subs
->data_endpoint
->sync_master
= NULL
;
429 err
= set_sync_ep_implicit_fb_quirk(subs
, dev
, altsd
, attr
);
433 /* endpoint set by quirk */
437 if (altsd
->bNumEndpoints
< 2)
440 if ((is_playback
&& (attr
== USB_ENDPOINT_SYNC_SYNC
||
441 attr
== USB_ENDPOINT_SYNC_ADAPTIVE
)) ||
442 (!is_playback
&& attr
!= USB_ENDPOINT_SYNC_ADAPTIVE
))
446 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
447 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
448 * error fall back to SYNC mode and don't create sync endpoint
451 /* check sync-pipe endpoint */
452 /* ... and check descriptor size before accessing bSynchAddress
453 because there is a version of the SB Audigy 2 NX firmware lacking
454 the audio fields in the endpoint descriptors */
455 if ((get_endpoint(alts
, 1)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_ISOC
||
456 (get_endpoint(alts
, 1)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
457 get_endpoint(alts
, 1)->bSynchAddress
!= 0)) {
459 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
460 fmt
->iface
, fmt
->altsetting
,
461 get_endpoint(alts
, 1)->bmAttributes
,
462 get_endpoint(alts
, 1)->bLength
,
463 get_endpoint(alts
, 1)->bSynchAddress
);
464 if (is_playback
&& attr
== USB_ENDPOINT_SYNC_NONE
)
468 ep
= get_endpoint(alts
, 1)->bEndpointAddress
;
469 if (get_endpoint(alts
, 0)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
470 get_endpoint(alts
, 0)->bSynchAddress
!= 0 &&
471 ((is_playback
&& ep
!= (unsigned int)(get_endpoint(alts
, 0)->bSynchAddress
| USB_DIR_IN
)) ||
472 (!is_playback
&& ep
!= (unsigned int)(get_endpoint(alts
, 0)->bSynchAddress
& ~USB_DIR_IN
)))) {
474 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
475 fmt
->iface
, fmt
->altsetting
,
476 is_playback
, ep
, get_endpoint(alts
, 0)->bSynchAddress
);
477 if (is_playback
&& attr
== USB_ENDPOINT_SYNC_NONE
)
482 implicit_fb
= (get_endpoint(alts
, 1)->bmAttributes
& USB_ENDPOINT_USAGE_MASK
)
483 == USB_ENDPOINT_USAGE_IMPLICIT_FB
;
485 subs
->sync_endpoint
= snd_usb_add_endpoint(subs
->stream
->chip
,
486 alts
, ep
, !subs
->direction
,
488 SND_USB_ENDPOINT_TYPE_DATA
:
489 SND_USB_ENDPOINT_TYPE_SYNC
);
490 if (!subs
->sync_endpoint
) {
491 if (is_playback
&& attr
== USB_ENDPOINT_SYNC_NONE
)
496 subs
->data_endpoint
->sync_master
= subs
->sync_endpoint
;
502 * find a matching format and set up the interface
504 static int set_format(struct snd_usb_substream
*subs
, struct audioformat
*fmt
)
506 struct usb_device
*dev
= subs
->dev
;
507 struct usb_host_interface
*alts
;
508 struct usb_interface_descriptor
*altsd
;
509 struct usb_interface
*iface
;
512 iface
= usb_ifnum_to_if(dev
, fmt
->iface
);
515 alts
= usb_altnum_to_altsetting(iface
, fmt
->altsetting
);
518 altsd
= get_iface_desc(alts
);
520 if (fmt
== subs
->cur_audiofmt
&& !subs
->need_setup_fmt
)
523 /* close the old interface */
524 if (subs
->interface
>= 0 && (subs
->interface
!= fmt
->iface
|| subs
->need_setup_fmt
)) {
525 if (!subs
->stream
->chip
->keep_iface
) {
526 err
= usb_set_interface(subs
->dev
, subs
->interface
, 0);
529 "%d:%d: return to setting 0 failed (%d)\n",
530 fmt
->iface
, fmt
->altsetting
, err
);
534 subs
->interface
= -1;
535 subs
->altset_idx
= 0;
538 if (subs
->need_setup_fmt
)
539 subs
->need_setup_fmt
= false;
542 if (iface
->cur_altsetting
!= alts
) {
543 err
= snd_usb_select_mode_quirk(subs
, fmt
);
547 err
= usb_set_interface(dev
, fmt
->iface
, fmt
->altsetting
);
550 "%d:%d: usb_set_interface failed (%d)\n",
551 fmt
->iface
, fmt
->altsetting
, err
);
554 dev_dbg(&dev
->dev
, "setting usb interface %d:%d\n",
555 fmt
->iface
, fmt
->altsetting
);
556 snd_usb_set_interface_quirk(dev
);
559 subs
->interface
= fmt
->iface
;
560 subs
->altset_idx
= fmt
->altset_idx
;
561 subs
->data_endpoint
= snd_usb_add_endpoint(subs
->stream
->chip
,
562 alts
, fmt
->endpoint
, subs
->direction
,
563 SND_USB_ENDPOINT_TYPE_DATA
);
565 if (!subs
->data_endpoint
)
568 err
= set_sync_endpoint(subs
, fmt
, dev
, alts
, altsd
);
572 err
= snd_usb_init_pitch(subs
->stream
->chip
, fmt
->iface
, alts
, fmt
);
576 subs
->cur_audiofmt
= fmt
;
578 snd_usb_set_format_quirk(subs
, fmt
);
584 * Return the score of matching two audioformats.
585 * Veto the audioformat if:
586 * - It has no channels for some reason.
587 * - Requested PCM format is not supported.
588 * - Requested sample rate is not supported.
590 static int match_endpoint_audioformats(struct snd_usb_substream
*subs
,
591 struct audioformat
*fp
,
592 struct audioformat
*match
, int rate
,
593 snd_pcm_format_t pcm_format
)
598 if (fp
->channels
< 1) {
599 dev_dbg(&subs
->dev
->dev
,
600 "%s: (fmt @%p) no channels\n", __func__
, fp
);
604 if (!(fp
->formats
& pcm_format_to_bits(pcm_format
))) {
605 dev_dbg(&subs
->dev
->dev
,
606 "%s: (fmt @%p) no match for format %d\n", __func__
,
611 for (i
= 0; i
< fp
->nr_rates
; i
++) {
612 if (fp
->rate_table
[i
] == rate
) {
618 dev_dbg(&subs
->dev
->dev
,
619 "%s: (fmt @%p) no match for rate %d\n", __func__
,
624 if (fp
->channels
== match
->channels
)
627 dev_dbg(&subs
->dev
->dev
,
628 "%s: (fmt @%p) score %d\n", __func__
, fp
, score
);
634 * Configure the sync ep using the rate and pcm format of the data ep.
636 static int configure_sync_endpoint(struct snd_usb_substream
*subs
)
639 struct audioformat
*fp
;
640 struct audioformat
*sync_fp
= NULL
;
642 int sync_period_bytes
= subs
->period_bytes
;
643 struct snd_usb_substream
*sync_subs
=
644 &subs
->stream
->substream
[subs
->direction
^ 1];
646 if (subs
->sync_endpoint
->type
!= SND_USB_ENDPOINT_TYPE_DATA
||
648 return snd_usb_endpoint_set_params(subs
->sync_endpoint
,
657 /* Try to find the best matching audioformat. */
658 list_for_each_entry(fp
, &sync_subs
->fmt_list
, list
) {
659 int score
= match_endpoint_audioformats(subs
,
660 fp
, subs
->cur_audiofmt
,
661 subs
->cur_rate
, subs
->pcm_format
);
663 if (score
> cur_score
) {
669 if (unlikely(sync_fp
== NULL
)) {
670 dev_err(&subs
->dev
->dev
,
671 "%s: no valid audioformat for sync ep %x found\n",
672 __func__
, sync_subs
->ep_num
);
677 * Recalculate the period bytes if channel number differ between
678 * data and sync ep audioformat.
680 if (sync_fp
->channels
!= subs
->channels
) {
681 sync_period_bytes
= (subs
->period_bytes
/ subs
->channels
) *
683 dev_dbg(&subs
->dev
->dev
,
684 "%s: adjusted sync ep period bytes (%d -> %d)\n",
685 __func__
, subs
->period_bytes
, sync_period_bytes
);
688 ret
= snd_usb_endpoint_set_params(subs
->sync_endpoint
,
701 * configure endpoint params
703 * called during initial setup and upon resume
705 static int configure_endpoint(struct snd_usb_substream
*subs
)
710 stop_endpoints(subs
, true);
711 ret
= snd_usb_endpoint_set_params(subs
->data_endpoint
,
716 subs
->buffer_periods
,
719 subs
->sync_endpoint
);
723 if (subs
->sync_endpoint
)
724 ret
= configure_sync_endpoint(subs
);
729 static int snd_usb_pcm_change_state(struct snd_usb_substream
*subs
, int state
)
736 ret
= snd_usb_power_domain_set(subs
->stream
->chip
, subs
->str_pd
, state
);
738 dev_err(&subs
->dev
->dev
,
739 "Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
740 subs
->str_pd
->pd_id
, state
, ret
);
747 int snd_usb_pcm_suspend(struct snd_usb_stream
*as
)
751 ret
= snd_usb_pcm_change_state(&as
->substream
[0], UAC3_PD_STATE_D2
);
755 ret
= snd_usb_pcm_change_state(&as
->substream
[1], UAC3_PD_STATE_D2
);
762 int snd_usb_pcm_resume(struct snd_usb_stream
*as
)
766 ret
= snd_usb_pcm_change_state(&as
->substream
[0], UAC3_PD_STATE_D1
);
770 ret
= snd_usb_pcm_change_state(&as
->substream
[1], UAC3_PD_STATE_D1
);
780 * allocate a buffer and set the given audio format.
782 * so far we use a physically linear buffer although packetize transfer
783 * doesn't need a continuous area.
784 * if sg buffer is supported on the later version of alsa, we'll follow
787 static int snd_usb_hw_params(struct snd_pcm_substream
*substream
,
788 struct snd_pcm_hw_params
*hw_params
)
790 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
791 struct audioformat
*fmt
;
794 if (snd_usb_use_vmalloc
)
795 ret
= snd_pcm_lib_alloc_vmalloc_buffer(substream
,
796 params_buffer_bytes(hw_params
));
798 ret
= snd_pcm_lib_malloc_pages(substream
,
799 params_buffer_bytes(hw_params
));
803 subs
->pcm_format
= params_format(hw_params
);
804 subs
->period_bytes
= params_period_bytes(hw_params
);
805 subs
->period_frames
= params_period_size(hw_params
);
806 subs
->buffer_periods
= params_periods(hw_params
);
807 subs
->channels
= params_channels(hw_params
);
808 subs
->cur_rate
= params_rate(hw_params
);
810 fmt
= find_format(subs
);
812 dev_dbg(&subs
->dev
->dev
,
813 "cannot set format: format = %#x, rate = %d, channels = %d\n",
814 subs
->pcm_format
, subs
->cur_rate
, subs
->channels
);
818 ret
= snd_usb_lock_shutdown(subs
->stream
->chip
);
822 ret
= snd_usb_pcm_change_state(subs
, UAC3_PD_STATE_D0
);
826 ret
= set_format(subs
, fmt
);
830 subs
->interface
= fmt
->iface
;
831 subs
->altset_idx
= fmt
->altset_idx
;
832 subs
->need_setup_ep
= true;
835 snd_usb_unlock_shutdown(subs
->stream
->chip
);
842 * reset the audio format and release the buffer
844 static int snd_usb_hw_free(struct snd_pcm_substream
*substream
)
846 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
848 subs
->cur_audiofmt
= NULL
;
850 subs
->period_bytes
= 0;
851 if (!snd_usb_lock_shutdown(subs
->stream
->chip
)) {
852 stop_endpoints(subs
, true);
853 snd_usb_endpoint_deactivate(subs
->sync_endpoint
);
854 snd_usb_endpoint_deactivate(subs
->data_endpoint
);
855 snd_usb_unlock_shutdown(subs
->stream
->chip
);
858 if (snd_usb_use_vmalloc
)
859 return snd_pcm_lib_free_vmalloc_buffer(substream
);
861 return snd_pcm_lib_free_pages(substream
);
867 * only a few subtle things...
869 static int snd_usb_pcm_prepare(struct snd_pcm_substream
*substream
)
871 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
872 struct snd_usb_substream
*subs
= runtime
->private_data
;
873 struct usb_host_interface
*alts
;
874 struct usb_interface
*iface
;
877 if (! subs
->cur_audiofmt
) {
878 dev_err(&subs
->dev
->dev
, "no format is specified!\n");
882 ret
= snd_usb_lock_shutdown(subs
->stream
->chip
);
885 if (snd_BUG_ON(!subs
->data_endpoint
)) {
890 snd_usb_endpoint_sync_pending_stop(subs
->sync_endpoint
);
891 snd_usb_endpoint_sync_pending_stop(subs
->data_endpoint
);
893 ret
= snd_usb_pcm_change_state(subs
, UAC3_PD_STATE_D0
);
897 ret
= set_format(subs
, subs
->cur_audiofmt
);
901 if (subs
->need_setup_ep
) {
903 iface
= usb_ifnum_to_if(subs
->dev
, subs
->cur_audiofmt
->iface
);
904 alts
= &iface
->altsetting
[subs
->cur_audiofmt
->altset_idx
];
905 ret
= snd_usb_init_sample_rate(subs
->stream
->chip
,
906 subs
->cur_audiofmt
->iface
,
913 ret
= configure_endpoint(subs
);
916 subs
->need_setup_ep
= false;
919 /* some unit conversions in runtime */
920 subs
->data_endpoint
->maxframesize
=
921 bytes_to_frames(runtime
, subs
->data_endpoint
->maxpacksize
);
922 subs
->data_endpoint
->curframesize
=
923 bytes_to_frames(runtime
, subs
->data_endpoint
->curpacksize
);
925 /* reset the pointer */
926 subs
->hwptr_done
= 0;
927 subs
->transfer_done
= 0;
928 subs
->last_delay
= 0;
929 subs
->last_frame_number
= 0;
932 /* for playback, submit the URBs now; otherwise, the first hwptr_done
933 * updates for all URBs would happen at the same time when starting */
934 if (subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
)
935 ret
= start_endpoints(subs
);
938 snd_usb_unlock_shutdown(subs
->stream
->chip
);
942 static const struct snd_pcm_hardware snd_usb_hardware
=
944 .info
= SNDRV_PCM_INFO_MMAP
|
945 SNDRV_PCM_INFO_MMAP_VALID
|
946 SNDRV_PCM_INFO_BATCH
|
947 SNDRV_PCM_INFO_INTERLEAVED
|
948 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
949 SNDRV_PCM_INFO_PAUSE
,
950 .buffer_bytes_max
= 1024 * 1024,
951 .period_bytes_min
= 64,
952 .period_bytes_max
= 512 * 1024,
957 static int hw_check_valid_format(struct snd_usb_substream
*subs
,
958 struct snd_pcm_hw_params
*params
,
959 struct audioformat
*fp
)
961 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
962 struct snd_interval
*ct
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
963 struct snd_mask
*fmts
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
964 struct snd_interval
*pt
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
);
965 struct snd_mask check_fmts
;
968 /* check the format */
969 snd_mask_none(&check_fmts
);
970 check_fmts
.bits
[0] = (u32
)fp
->formats
;
971 check_fmts
.bits
[1] = (u32
)(fp
->formats
>> 32);
972 snd_mask_intersect(&check_fmts
, fmts
);
973 if (snd_mask_empty(&check_fmts
)) {
974 hwc_debug(" > check: no supported format %d\n", fp
->format
);
977 /* check the channels */
978 if (fp
->channels
< ct
->min
|| fp
->channels
> ct
->max
) {
979 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp
->channels
, ct
->min
, ct
->max
);
982 /* check the rate is within the range */
983 if (fp
->rate_min
> it
->max
|| (fp
->rate_min
== it
->max
&& it
->openmax
)) {
984 hwc_debug(" > check: rate_min %d > max %d\n", fp
->rate_min
, it
->max
);
987 if (fp
->rate_max
< it
->min
|| (fp
->rate_max
== it
->min
&& it
->openmin
)) {
988 hwc_debug(" > check: rate_max %d < min %d\n", fp
->rate_max
, it
->min
);
991 /* check whether the period time is >= the data packet interval */
992 if (subs
->speed
!= USB_SPEED_FULL
) {
993 ptime
= 125 * (1 << fp
->datainterval
);
994 if (ptime
> pt
->max
|| (ptime
== pt
->max
&& pt
->openmax
)) {
995 hwc_debug(" > check: ptime %u > max %u\n", ptime
, pt
->max
);
1002 static int hw_rule_rate(struct snd_pcm_hw_params
*params
,
1003 struct snd_pcm_hw_rule
*rule
)
1005 struct snd_usb_substream
*subs
= rule
->private;
1006 struct audioformat
*fp
;
1007 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
1008 unsigned int rmin
, rmax
;
1011 hwc_debug("hw_rule_rate: (%d,%d)\n", it
->min
, it
->max
);
1014 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1015 if (!hw_check_valid_format(subs
, params
, fp
))
1018 if (rmin
> fp
->rate_min
)
1019 rmin
= fp
->rate_min
;
1020 if (rmax
< fp
->rate_max
)
1021 rmax
= fp
->rate_max
;
1023 rmin
= fp
->rate_min
;
1024 rmax
= fp
->rate_max
;
1029 hwc_debug(" --> get empty\n");
1035 if (it
->min
< rmin
) {
1040 if (it
->max
> rmax
) {
1045 if (snd_interval_checkempty(it
)) {
1049 hwc_debug(" --> (%d, %d) (changed = %d)\n", it
->min
, it
->max
, changed
);
1054 static int hw_rule_channels(struct snd_pcm_hw_params
*params
,
1055 struct snd_pcm_hw_rule
*rule
)
1057 struct snd_usb_substream
*subs
= rule
->private;
1058 struct audioformat
*fp
;
1059 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
1060 unsigned int rmin
, rmax
;
1063 hwc_debug("hw_rule_channels: (%d,%d)\n", it
->min
, it
->max
);
1066 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1067 if (!hw_check_valid_format(subs
, params
, fp
))
1070 if (rmin
> fp
->channels
)
1071 rmin
= fp
->channels
;
1072 if (rmax
< fp
->channels
)
1073 rmax
= fp
->channels
;
1075 rmin
= fp
->channels
;
1076 rmax
= fp
->channels
;
1081 hwc_debug(" --> get empty\n");
1087 if (it
->min
< rmin
) {
1092 if (it
->max
> rmax
) {
1097 if (snd_interval_checkempty(it
)) {
1101 hwc_debug(" --> (%d, %d) (changed = %d)\n", it
->min
, it
->max
, changed
);
1105 static int hw_rule_format(struct snd_pcm_hw_params
*params
,
1106 struct snd_pcm_hw_rule
*rule
)
1108 struct snd_usb_substream
*subs
= rule
->private;
1109 struct audioformat
*fp
;
1110 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1115 hwc_debug("hw_rule_format: %x:%x\n", fmt
->bits
[0], fmt
->bits
[1]);
1117 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1118 if (!hw_check_valid_format(subs
, params
, fp
))
1120 fbits
|= fp
->formats
;
1123 oldbits
[0] = fmt
->bits
[0];
1124 oldbits
[1] = fmt
->bits
[1];
1125 fmt
->bits
[0] &= (u32
)fbits
;
1126 fmt
->bits
[1] &= (u32
)(fbits
>> 32);
1127 if (!fmt
->bits
[0] && !fmt
->bits
[1]) {
1128 hwc_debug(" --> get empty\n");
1131 changed
= (oldbits
[0] != fmt
->bits
[0] || oldbits
[1] != fmt
->bits
[1]);
1132 hwc_debug(" --> %x:%x (changed = %d)\n", fmt
->bits
[0], fmt
->bits
[1], changed
);
1136 static int hw_rule_period_time(struct snd_pcm_hw_params
*params
,
1137 struct snd_pcm_hw_rule
*rule
)
1139 struct snd_usb_substream
*subs
= rule
->private;
1140 struct audioformat
*fp
;
1141 struct snd_interval
*it
;
1142 unsigned char min_datainterval
;
1146 it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
);
1147 hwc_debug("hw_rule_period_time: (%u,%u)\n", it
->min
, it
->max
);
1148 min_datainterval
= 0xff;
1149 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1150 if (!hw_check_valid_format(subs
, params
, fp
))
1152 min_datainterval
= min(min_datainterval
, fp
->datainterval
);
1154 if (min_datainterval
== 0xff) {
1155 hwc_debug(" --> get empty\n");
1159 pmin
= 125 * (1 << min_datainterval
);
1161 if (it
->min
< pmin
) {
1166 if (snd_interval_checkempty(it
)) {
1170 hwc_debug(" --> (%u,%u) (changed = %d)\n", it
->min
, it
->max
, changed
);
1175 * If the device supports unusual bit rates, does the request meet these?
1177 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime
*runtime
,
1178 struct snd_usb_substream
*subs
)
1180 struct audioformat
*fp
;
1182 int count
= 0, needs_knot
= 0;
1185 kfree(subs
->rate_list
.list
);
1186 subs
->rate_list
.list
= NULL
;
1188 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1189 if (fp
->rates
& SNDRV_PCM_RATE_CONTINUOUS
)
1191 count
+= fp
->nr_rates
;
1192 if (fp
->rates
& SNDRV_PCM_RATE_KNOT
)
1198 subs
->rate_list
.list
= rate_list
=
1199 kmalloc_array(count
, sizeof(int), GFP_KERNEL
);
1200 if (!subs
->rate_list
.list
)
1202 subs
->rate_list
.count
= count
;
1203 subs
->rate_list
.mask
= 0;
1205 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1207 for (i
= 0; i
< fp
->nr_rates
; i
++)
1208 rate_list
[count
++] = fp
->rate_table
[i
];
1210 err
= snd_pcm_hw_constraint_list(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1220 * set up the runtime hardware information.
1223 static int setup_hw_info(struct snd_pcm_runtime
*runtime
, struct snd_usb_substream
*subs
)
1225 struct audioformat
*fp
;
1226 unsigned int pt
, ptmin
;
1227 int param_period_time_if_needed
;
1230 runtime
->hw
.formats
= subs
->formats
;
1232 runtime
->hw
.rate_min
= 0x7fffffff;
1233 runtime
->hw
.rate_max
= 0;
1234 runtime
->hw
.channels_min
= 256;
1235 runtime
->hw
.channels_max
= 0;
1236 runtime
->hw
.rates
= 0;
1238 /* check min/max rates and channels */
1239 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1240 runtime
->hw
.rates
|= fp
->rates
;
1241 if (runtime
->hw
.rate_min
> fp
->rate_min
)
1242 runtime
->hw
.rate_min
= fp
->rate_min
;
1243 if (runtime
->hw
.rate_max
< fp
->rate_max
)
1244 runtime
->hw
.rate_max
= fp
->rate_max
;
1245 if (runtime
->hw
.channels_min
> fp
->channels
)
1246 runtime
->hw
.channels_min
= fp
->channels
;
1247 if (runtime
->hw
.channels_max
< fp
->channels
)
1248 runtime
->hw
.channels_max
= fp
->channels
;
1249 if (fp
->fmt_type
== UAC_FORMAT_TYPE_II
&& fp
->frame_size
> 0) {
1250 /* FIXME: there might be more than one audio formats... */
1251 runtime
->hw
.period_bytes_min
= runtime
->hw
.period_bytes_max
=
1254 pt
= 125 * (1 << fp
->datainterval
);
1255 ptmin
= min(ptmin
, pt
);
1258 param_period_time_if_needed
= SNDRV_PCM_HW_PARAM_PERIOD_TIME
;
1259 if (subs
->speed
== USB_SPEED_FULL
)
1260 /* full speed devices have fixed data packet interval */
1263 /* if period time doesn't go below 1 ms, no rules needed */
1264 param_period_time_if_needed
= -1;
1266 err
= snd_pcm_hw_constraint_minmax(runtime
,
1267 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1272 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1274 SNDRV_PCM_HW_PARAM_FORMAT
,
1275 SNDRV_PCM_HW_PARAM_CHANNELS
,
1276 param_period_time_if_needed
,
1280 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1281 hw_rule_channels
, subs
,
1282 SNDRV_PCM_HW_PARAM_FORMAT
,
1283 SNDRV_PCM_HW_PARAM_RATE
,
1284 param_period_time_if_needed
,
1288 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1289 hw_rule_format
, subs
,
1290 SNDRV_PCM_HW_PARAM_RATE
,
1291 SNDRV_PCM_HW_PARAM_CHANNELS
,
1292 param_period_time_if_needed
,
1296 if (param_period_time_if_needed
>= 0) {
1297 err
= snd_pcm_hw_rule_add(runtime
, 0,
1298 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1299 hw_rule_period_time
, subs
,
1300 SNDRV_PCM_HW_PARAM_FORMAT
,
1301 SNDRV_PCM_HW_PARAM_CHANNELS
,
1302 SNDRV_PCM_HW_PARAM_RATE
,
1307 err
= snd_usb_pcm_check_knot(runtime
, subs
);
1311 return snd_usb_autoresume(subs
->stream
->chip
);
1314 static int snd_usb_pcm_open(struct snd_pcm_substream
*substream
)
1316 int direction
= substream
->stream
;
1317 struct snd_usb_stream
*as
= snd_pcm_substream_chip(substream
);
1318 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1319 struct snd_usb_substream
*subs
= &as
->substream
[direction
];
1321 subs
->interface
= -1;
1322 subs
->altset_idx
= 0;
1323 runtime
->hw
= snd_usb_hardware
;
1324 runtime
->private_data
= subs
;
1325 subs
->pcm_substream
= substream
;
1326 /* runtime PM is also done there */
1328 /* initialize DSD/DOP context */
1329 subs
->dsd_dop
.byte_idx
= 0;
1330 subs
->dsd_dop
.channel
= 0;
1331 subs
->dsd_dop
.marker
= 1;
1333 return setup_hw_info(runtime
, subs
);
1336 static int snd_usb_pcm_close(struct snd_pcm_substream
*substream
)
1338 int direction
= substream
->stream
;
1339 struct snd_usb_stream
*as
= snd_pcm_substream_chip(substream
);
1340 struct snd_usb_substream
*subs
= &as
->substream
[direction
];
1343 stop_endpoints(subs
, true);
1345 if (!as
->chip
->keep_iface
&&
1346 subs
->interface
>= 0 &&
1347 !snd_usb_lock_shutdown(subs
->stream
->chip
)) {
1348 usb_set_interface(subs
->dev
, subs
->interface
, 0);
1349 subs
->interface
= -1;
1350 ret
= snd_usb_pcm_change_state(subs
, UAC3_PD_STATE_D1
);
1351 snd_usb_unlock_shutdown(subs
->stream
->chip
);
1356 subs
->pcm_substream
= NULL
;
1357 snd_usb_autosuspend(subs
->stream
->chip
);
1362 /* Since a URB can handle only a single linear buffer, we must use double
1363 * buffering when the data to be transferred overflows the buffer boundary.
1364 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1367 static void retire_capture_urb(struct snd_usb_substream
*subs
,
1370 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1371 unsigned int stride
, frames
, bytes
, oldptr
;
1372 int i
, period_elapsed
= 0;
1373 unsigned long flags
;
1375 int current_frame_number
;
1377 /* read frame number here, update pointer in critical section */
1378 current_frame_number
= usb_get_current_frame_number(subs
->dev
);
1380 stride
= runtime
->frame_bits
>> 3;
1382 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1383 cp
= (unsigned char *)urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
+ subs
->pkt_offset_adj
;
1384 if (urb
->iso_frame_desc
[i
].status
&& printk_ratelimit()) {
1385 dev_dbg(&subs
->dev
->dev
, "frame %d active: %d\n",
1386 i
, urb
->iso_frame_desc
[i
].status
);
1389 bytes
= urb
->iso_frame_desc
[i
].actual_length
;
1390 frames
= bytes
/ stride
;
1391 if (!subs
->txfr_quirk
)
1392 bytes
= frames
* stride
;
1393 if (bytes
% (runtime
->sample_bits
>> 3) != 0) {
1394 int oldbytes
= bytes
;
1395 bytes
= frames
* stride
;
1396 dev_warn_ratelimited(&subs
->dev
->dev
,
1397 "Corrected urb data len. %d->%d\n",
1400 /* update the current pointer */
1401 spin_lock_irqsave(&subs
->lock
, flags
);
1402 oldptr
= subs
->hwptr_done
;
1403 subs
->hwptr_done
+= bytes
;
1404 if (subs
->hwptr_done
>= runtime
->buffer_size
* stride
)
1405 subs
->hwptr_done
-= runtime
->buffer_size
* stride
;
1406 frames
= (bytes
+ (oldptr
% stride
)) / stride
;
1407 subs
->transfer_done
+= frames
;
1408 if (subs
->transfer_done
>= runtime
->period_size
) {
1409 subs
->transfer_done
-= runtime
->period_size
;
1412 /* capture delay is by construction limited to one URB,
1415 runtime
->delay
= subs
->last_delay
= 0;
1417 /* realign last_frame_number */
1418 subs
->last_frame_number
= current_frame_number
;
1419 subs
->last_frame_number
&= 0xFF; /* keep 8 LSBs */
1421 spin_unlock_irqrestore(&subs
->lock
, flags
);
1422 /* copy a data chunk */
1423 if (oldptr
+ bytes
> runtime
->buffer_size
* stride
) {
1424 unsigned int bytes1
=
1425 runtime
->buffer_size
* stride
- oldptr
;
1426 memcpy(runtime
->dma_area
+ oldptr
, cp
, bytes1
);
1427 memcpy(runtime
->dma_area
, cp
+ bytes1
, bytes
- bytes1
);
1429 memcpy(runtime
->dma_area
+ oldptr
, cp
, bytes
);
1434 snd_pcm_period_elapsed(subs
->pcm_substream
);
1437 static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream
*subs
,
1438 struct urb
*urb
, unsigned int bytes
)
1440 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1441 unsigned int stride
= runtime
->frame_bits
>> 3;
1442 unsigned int dst_idx
= 0;
1443 unsigned int src_idx
= subs
->hwptr_done
;
1444 unsigned int wrap
= runtime
->buffer_size
* stride
;
1445 u8
*dst
= urb
->transfer_buffer
;
1446 u8
*src
= runtime
->dma_area
;
1447 u8 marker
[] = { 0x05, 0xfa };
1450 * The DSP DOP format defines a way to transport DSD samples over
1451 * normal PCM data endpoints. It requires stuffing of marker bytes
1452 * (0x05 and 0xfa, alternating per sample frame), and then expects
1453 * 2 additional bytes of actual payload. The whole frame is stored
1456 * Hence, for a stereo transport, the buffer layout looks like this,
1457 * where L refers to left channel samples and R to right.
1459 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1460 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1466 if (++subs
->dsd_dop
.byte_idx
== 3) {
1467 /* frame boundary? */
1468 dst
[dst_idx
++] = marker
[subs
->dsd_dop
.marker
];
1470 subs
->dsd_dop
.byte_idx
= 0;
1472 if (++subs
->dsd_dop
.channel
% runtime
->channels
== 0) {
1473 /* alternate the marker */
1474 subs
->dsd_dop
.marker
++;
1475 subs
->dsd_dop
.marker
%= ARRAY_SIZE(marker
);
1476 subs
->dsd_dop
.channel
= 0;
1479 /* stuff the DSD payload */
1480 int idx
= (src_idx
+ subs
->dsd_dop
.byte_idx
- 1) % wrap
;
1482 if (subs
->cur_audiofmt
->dsd_bitrev
)
1483 dst
[dst_idx
++] = bitrev8(src
[idx
]);
1485 dst
[dst_idx
++] = src
[idx
];
1490 if (subs
->hwptr_done
>= runtime
->buffer_size
* stride
)
1491 subs
->hwptr_done
-= runtime
->buffer_size
* stride
;
1494 static void copy_to_urb(struct snd_usb_substream
*subs
, struct urb
*urb
,
1495 int offset
, int stride
, unsigned int bytes
)
1497 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1499 if (subs
->hwptr_done
+ bytes
> runtime
->buffer_size
* stride
) {
1500 /* err, the transferred area goes over buffer boundary. */
1501 unsigned int bytes1
=
1502 runtime
->buffer_size
* stride
- subs
->hwptr_done
;
1503 memcpy(urb
->transfer_buffer
+ offset
,
1504 runtime
->dma_area
+ subs
->hwptr_done
, bytes1
);
1505 memcpy(urb
->transfer_buffer
+ offset
+ bytes1
,
1506 runtime
->dma_area
, bytes
- bytes1
);
1508 memcpy(urb
->transfer_buffer
+ offset
,
1509 runtime
->dma_area
+ subs
->hwptr_done
, bytes
);
1511 subs
->hwptr_done
+= bytes
;
1512 if (subs
->hwptr_done
>= runtime
->buffer_size
* stride
)
1513 subs
->hwptr_done
-= runtime
->buffer_size
* stride
;
1516 static unsigned int copy_to_urb_quirk(struct snd_usb_substream
*subs
,
1517 struct urb
*urb
, int stride
,
1520 __le32 packet_length
;
1523 /* Put __le32 length descriptor at start of each packet. */
1524 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1525 unsigned int length
= urb
->iso_frame_desc
[i
].length
;
1526 unsigned int offset
= urb
->iso_frame_desc
[i
].offset
;
1528 packet_length
= cpu_to_le32(length
);
1529 offset
+= i
* sizeof(packet_length
);
1530 urb
->iso_frame_desc
[i
].offset
= offset
;
1531 urb
->iso_frame_desc
[i
].length
+= sizeof(packet_length
);
1532 memcpy(urb
->transfer_buffer
+ offset
,
1533 &packet_length
, sizeof(packet_length
));
1534 copy_to_urb(subs
, urb
, offset
+ sizeof(packet_length
),
1537 /* Adjust transfer size accordingly. */
1538 bytes
+= urb
->number_of_packets
* sizeof(packet_length
);
1542 static void prepare_playback_urb(struct snd_usb_substream
*subs
,
1545 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1546 struct snd_usb_endpoint
*ep
= subs
->data_endpoint
;
1547 struct snd_urb_ctx
*ctx
= urb
->context
;
1548 unsigned int counts
, frames
, bytes
;
1549 int i
, stride
, period_elapsed
= 0;
1550 unsigned long flags
;
1552 stride
= runtime
->frame_bits
>> 3;
1555 urb
->number_of_packets
= 0;
1556 spin_lock_irqsave(&subs
->lock
, flags
);
1557 subs
->frame_limit
+= ep
->max_urb_frames
;
1558 for (i
= 0; i
< ctx
->packets
; i
++) {
1559 if (ctx
->packet_size
[i
])
1560 counts
= ctx
->packet_size
[i
];
1562 counts
= snd_usb_endpoint_next_packet_size(ep
);
1564 /* set up descriptor */
1565 urb
->iso_frame_desc
[i
].offset
= frames
* ep
->stride
;
1566 urb
->iso_frame_desc
[i
].length
= counts
* ep
->stride
;
1568 urb
->number_of_packets
++;
1569 subs
->transfer_done
+= counts
;
1570 if (subs
->transfer_done
>= runtime
->period_size
) {
1571 subs
->transfer_done
-= runtime
->period_size
;
1572 subs
->frame_limit
= 0;
1574 if (subs
->fmt_type
== UAC_FORMAT_TYPE_II
) {
1575 if (subs
->transfer_done
> 0) {
1576 /* FIXME: fill-max mode is not
1578 frames
-= subs
->transfer_done
;
1579 counts
-= subs
->transfer_done
;
1580 urb
->iso_frame_desc
[i
].length
=
1581 counts
* ep
->stride
;
1582 subs
->transfer_done
= 0;
1585 if (i
< ctx
->packets
) {
1586 /* add a transfer delimiter */
1587 urb
->iso_frame_desc
[i
].offset
=
1588 frames
* ep
->stride
;
1589 urb
->iso_frame_desc
[i
].length
= 0;
1590 urb
->number_of_packets
++;
1595 /* finish at the period boundary or after enough frames */
1596 if ((period_elapsed
||
1597 subs
->transfer_done
>= subs
->frame_limit
) &&
1598 !snd_usb_endpoint_implicit_feedback_sink(ep
))
1601 bytes
= frames
* ep
->stride
;
1603 if (unlikely(subs
->pcm_format
== SNDRV_PCM_FORMAT_DSD_U16_LE
&&
1604 subs
->cur_audiofmt
->dsd_dop
)) {
1605 fill_playback_urb_dsd_dop(subs
, urb
, bytes
);
1606 } else if (unlikely(subs
->pcm_format
== SNDRV_PCM_FORMAT_DSD_U8
&&
1607 subs
->cur_audiofmt
->dsd_bitrev
)) {
1608 /* bit-reverse the bytes */
1609 u8
*buf
= urb
->transfer_buffer
;
1610 for (i
= 0; i
< bytes
; i
++) {
1611 int idx
= (subs
->hwptr_done
+ i
)
1612 % (runtime
->buffer_size
* stride
);
1613 buf
[i
] = bitrev8(runtime
->dma_area
[idx
]);
1616 subs
->hwptr_done
+= bytes
;
1617 if (subs
->hwptr_done
>= runtime
->buffer_size
* stride
)
1618 subs
->hwptr_done
-= runtime
->buffer_size
* stride
;
1621 if (!subs
->tx_length_quirk
)
1622 copy_to_urb(subs
, urb
, 0, stride
, bytes
);
1624 bytes
= copy_to_urb_quirk(subs
, urb
, stride
, bytes
);
1625 /* bytes is now amount of outgoing data */
1628 /* update delay with exact number of samples queued */
1629 runtime
->delay
= subs
->last_delay
;
1630 runtime
->delay
+= frames
;
1631 subs
->last_delay
= runtime
->delay
;
1633 /* realign last_frame_number */
1634 subs
->last_frame_number
= usb_get_current_frame_number(subs
->dev
);
1635 subs
->last_frame_number
&= 0xFF; /* keep 8 LSBs */
1637 if (subs
->trigger_tstamp_pending_update
) {
1638 /* this is the first actual URB submitted,
1639 * update trigger timestamp to reflect actual start time
1641 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
1642 subs
->trigger_tstamp_pending_update
= false;
1645 spin_unlock_irqrestore(&subs
->lock
, flags
);
1646 urb
->transfer_buffer_length
= bytes
;
1648 snd_pcm_period_elapsed(subs
->pcm_substream
);
1652 * process after playback data complete
1653 * - decrease the delay count again
1655 static void retire_playback_urb(struct snd_usb_substream
*subs
,
1658 unsigned long flags
;
1659 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1660 struct snd_usb_endpoint
*ep
= subs
->data_endpoint
;
1661 int processed
= urb
->transfer_buffer_length
/ ep
->stride
;
1664 /* ignore the delay accounting when procssed=0 is given, i.e.
1665 * silent payloads are procssed before handling the actual data
1670 spin_lock_irqsave(&subs
->lock
, flags
);
1671 if (!subs
->last_delay
)
1672 goto out
; /* short path */
1674 est_delay
= snd_usb_pcm_delay(subs
, runtime
->rate
);
1675 /* update delay with exact number of samples played */
1676 if (processed
> subs
->last_delay
)
1677 subs
->last_delay
= 0;
1679 subs
->last_delay
-= processed
;
1680 runtime
->delay
= subs
->last_delay
;
1683 * Report when delay estimate is off by more than 2ms.
1684 * The error should be lower than 2ms since the estimate relies
1685 * on two reads of a counter updated every ms.
1687 if (abs(est_delay
- subs
->last_delay
) * 1000 > runtime
->rate
* 2)
1688 dev_dbg_ratelimited(&subs
->dev
->dev
,
1689 "delay: estimated %d, actual %d\n",
1690 est_delay
, subs
->last_delay
);
1692 if (!subs
->running
) {
1693 /* update last_frame_number for delay counting here since
1694 * prepare_playback_urb won't be called during pause
1696 subs
->last_frame_number
=
1697 usb_get_current_frame_number(subs
->dev
) & 0xff;
1701 spin_unlock_irqrestore(&subs
->lock
, flags
);
1704 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream
*substream
,
1707 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
1710 case SNDRV_PCM_TRIGGER_START
:
1711 subs
->trigger_tstamp_pending_update
= true;
1713 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1714 subs
->data_endpoint
->prepare_data_urb
= prepare_playback_urb
;
1715 subs
->data_endpoint
->retire_data_urb
= retire_playback_urb
;
1718 case SNDRV_PCM_TRIGGER_STOP
:
1719 stop_endpoints(subs
, false);
1722 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1723 subs
->data_endpoint
->prepare_data_urb
= NULL
;
1724 /* keep retire_data_urb for delay calculation */
1725 subs
->data_endpoint
->retire_data_urb
= retire_playback_urb
;
1728 case SNDRV_PCM_TRIGGER_SUSPEND
:
1729 if (subs
->stream
->chip
->setup_fmt_after_resume_quirk
) {
1730 stop_endpoints(subs
, true);
1731 subs
->need_setup_fmt
= true;
1740 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream
*substream
,
1744 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
1747 case SNDRV_PCM_TRIGGER_START
:
1748 err
= start_endpoints(subs
);
1752 subs
->data_endpoint
->retire_data_urb
= retire_capture_urb
;
1755 case SNDRV_PCM_TRIGGER_STOP
:
1756 stop_endpoints(subs
, false);
1759 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1760 subs
->data_endpoint
->retire_data_urb
= NULL
;
1763 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1764 subs
->data_endpoint
->retire_data_urb
= retire_capture_urb
;
1767 case SNDRV_PCM_TRIGGER_SUSPEND
:
1768 if (subs
->stream
->chip
->setup_fmt_after_resume_quirk
) {
1769 stop_endpoints(subs
, true);
1770 subs
->need_setup_fmt
= true;
1779 static const struct snd_pcm_ops snd_usb_playback_ops
= {
1780 .open
= snd_usb_pcm_open
,
1781 .close
= snd_usb_pcm_close
,
1782 .ioctl
= snd_pcm_lib_ioctl
,
1783 .hw_params
= snd_usb_hw_params
,
1784 .hw_free
= snd_usb_hw_free
,
1785 .prepare
= snd_usb_pcm_prepare
,
1786 .trigger
= snd_usb_substream_playback_trigger
,
1787 .pointer
= snd_usb_pcm_pointer
,
1788 .page
= snd_pcm_lib_get_vmalloc_page
,
1791 static const struct snd_pcm_ops snd_usb_capture_ops
= {
1792 .open
= snd_usb_pcm_open
,
1793 .close
= snd_usb_pcm_close
,
1794 .ioctl
= snd_pcm_lib_ioctl
,
1795 .hw_params
= snd_usb_hw_params
,
1796 .hw_free
= snd_usb_hw_free
,
1797 .prepare
= snd_usb_pcm_prepare
,
1798 .trigger
= snd_usb_substream_capture_trigger
,
1799 .pointer
= snd_usb_pcm_pointer
,
1800 .page
= snd_pcm_lib_get_vmalloc_page
,
1803 static const struct snd_pcm_ops snd_usb_playback_dev_ops
= {
1804 .open
= snd_usb_pcm_open
,
1805 .close
= snd_usb_pcm_close
,
1806 .ioctl
= snd_pcm_lib_ioctl
,
1807 .hw_params
= snd_usb_hw_params
,
1808 .hw_free
= snd_usb_hw_free
,
1809 .prepare
= snd_usb_pcm_prepare
,
1810 .trigger
= snd_usb_substream_playback_trigger
,
1811 .pointer
= snd_usb_pcm_pointer
,
1812 .page
= snd_pcm_sgbuf_ops_page
,
1815 static const struct snd_pcm_ops snd_usb_capture_dev_ops
= {
1816 .open
= snd_usb_pcm_open
,
1817 .close
= snd_usb_pcm_close
,
1818 .ioctl
= snd_pcm_lib_ioctl
,
1819 .hw_params
= snd_usb_hw_params
,
1820 .hw_free
= snd_usb_hw_free
,
1821 .prepare
= snd_usb_pcm_prepare
,
1822 .trigger
= snd_usb_substream_capture_trigger
,
1823 .pointer
= snd_usb_pcm_pointer
,
1824 .page
= snd_pcm_sgbuf_ops_page
,
1827 void snd_usb_set_pcm_ops(struct snd_pcm
*pcm
, int stream
)
1829 const struct snd_pcm_ops
*ops
;
1831 if (snd_usb_use_vmalloc
)
1832 ops
= stream
== SNDRV_PCM_STREAM_PLAYBACK
?
1833 &snd_usb_playback_ops
: &snd_usb_capture_ops
;
1835 ops
= stream
== SNDRV_PCM_STREAM_PLAYBACK
?
1836 &snd_usb_playback_dev_ops
: &snd_usb_capture_dev_ops
;
1837 snd_pcm_set_ops(pcm
, stream
, ops
);
1840 void snd_usb_preallocate_buffer(struct snd_usb_substream
*subs
)
1842 struct snd_pcm
*pcm
= subs
->stream
->pcm
;
1843 struct snd_pcm_substream
*s
= pcm
->streams
[subs
->direction
].substream
;
1844 struct device
*dev
= subs
->dev
->bus
->controller
;
1846 if (!snd_usb_use_vmalloc
)
1847 snd_pcm_lib_preallocate_pages(s
, SNDRV_DMA_TYPE_DEV_SG
,
1848 dev
, 64*1024, 512*1024);