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 (atomic_read(&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 if (get_iface_desc(alts
)->bNumEndpoints
< 1)
164 ep
= get_endpoint(alts
, 0)->bEndpointAddress
;
167 if ((err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC_SET_CUR
,
168 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
169 UAC_EP_CS_ATTR_PITCH_CONTROL
<< 8, ep
,
170 data
, sizeof(data
))) < 0) {
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 if ((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,
191 data
, sizeof(data
))) < 0) {
192 usb_audio_err(chip
, "%d:%d: cannot set enable PITCH (v2)\n",
193 iface
, fmt
->altsetting
);
201 * initialize the pitch control and sample rate
203 int snd_usb_init_pitch(struct snd_usb_audio
*chip
, int iface
,
204 struct usb_host_interface
*alts
,
205 struct audioformat
*fmt
)
207 /* if endpoint doesn't have pitch control, bail out */
208 if (!(fmt
->attributes
& UAC_EP_CS_ATTR_PITCH_CONTROL
))
211 switch (fmt
->protocol
) {
214 return init_pitch_v1(chip
, iface
, alts
, fmt
);
217 return init_pitch_v2(chip
, iface
, alts
, fmt
);
221 static int start_endpoints(struct snd_usb_substream
*subs
)
225 if (!subs
->data_endpoint
)
228 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED
, &subs
->flags
)) {
229 struct snd_usb_endpoint
*ep
= subs
->data_endpoint
;
231 dev_dbg(&subs
->dev
->dev
, "Starting data EP @%p\n", ep
);
233 ep
->data_subs
= subs
;
234 err
= snd_usb_endpoint_start(ep
);
236 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED
, &subs
->flags
);
241 if (subs
->sync_endpoint
&&
242 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED
, &subs
->flags
)) {
243 struct snd_usb_endpoint
*ep
= subs
->sync_endpoint
;
245 if (subs
->data_endpoint
->iface
!= subs
->sync_endpoint
->iface
||
246 subs
->data_endpoint
->altsetting
!= subs
->sync_endpoint
->altsetting
) {
247 err
= usb_set_interface(subs
->dev
,
248 subs
->sync_endpoint
->iface
,
249 subs
->sync_endpoint
->altsetting
);
251 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED
, &subs
->flags
);
252 dev_err(&subs
->dev
->dev
,
253 "%d:%d: cannot set interface (%d)\n",
254 subs
->sync_endpoint
->iface
,
255 subs
->sync_endpoint
->altsetting
, err
);
260 dev_dbg(&subs
->dev
->dev
, "Starting sync EP @%p\n", ep
);
262 ep
->sync_slave
= subs
->data_endpoint
;
263 err
= snd_usb_endpoint_start(ep
);
265 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED
, &subs
->flags
);
273 static void stop_endpoints(struct snd_usb_substream
*subs
, bool wait
)
275 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED
, &subs
->flags
))
276 snd_usb_endpoint_stop(subs
->sync_endpoint
);
278 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED
, &subs
->flags
))
279 snd_usb_endpoint_stop(subs
->data_endpoint
);
282 snd_usb_endpoint_sync_pending_stop(subs
->sync_endpoint
);
283 snd_usb_endpoint_sync_pending_stop(subs
->data_endpoint
);
287 static int search_roland_implicit_fb(struct usb_device
*dev
, int ifnum
,
288 unsigned int altsetting
,
289 struct usb_host_interface
**alts
,
292 struct usb_interface
*iface
;
293 struct usb_interface_descriptor
*altsd
;
294 struct usb_endpoint_descriptor
*epd
;
296 iface
= usb_ifnum_to_if(dev
, ifnum
);
297 if (!iface
|| iface
->num_altsetting
< altsetting
+ 1)
299 *alts
= &iface
->altsetting
[altsetting
];
300 altsd
= get_iface_desc(*alts
);
301 if (altsd
->bAlternateSetting
!= altsetting
||
302 altsd
->bInterfaceClass
!= USB_CLASS_VENDOR_SPEC
||
303 (altsd
->bInterfaceSubClass
!= 2 &&
304 altsd
->bInterfaceProtocol
!= 2 ) ||
305 altsd
->bNumEndpoints
< 1)
307 epd
= get_endpoint(*alts
, 0);
308 if (!usb_endpoint_is_isoc_in(epd
) ||
309 (epd
->bmAttributes
& USB_ENDPOINT_USAGE_MASK
) !=
310 USB_ENDPOINT_USAGE_IMPLICIT_FB
)
312 *ep
= epd
->bEndpointAddress
;
316 static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream
*subs
,
317 struct usb_device
*dev
,
318 struct usb_interface_descriptor
*altsd
,
321 struct usb_host_interface
*alts
;
322 struct usb_interface
*iface
;
325 /* Implicit feedback sync EPs consumers are always playback EPs */
326 if (subs
->direction
!= SNDRV_PCM_STREAM_PLAYBACK
)
329 switch (subs
->stream
->chip
->usb_id
) {
330 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
331 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
333 iface
= usb_ifnum_to_if(dev
, 3);
335 if (!iface
|| iface
->num_altsetting
== 0)
338 alts
= &iface
->altsetting
[1];
341 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
342 case USB_ID(0x0763, 0x2081):
344 iface
= usb_ifnum_to_if(dev
, 2);
346 if (!iface
|| iface
->num_altsetting
== 0)
349 alts
= &iface
->altsetting
[1];
351 case USB_ID(0x1397, 0x0002):
353 iface
= usb_ifnum_to_if(dev
, 1);
355 if (!iface
|| iface
->num_altsetting
== 0)
358 alts
= &iface
->altsetting
[1];
361 if (attr
== USB_ENDPOINT_SYNC_ASYNC
&&
362 altsd
->bInterfaceClass
== USB_CLASS_VENDOR_SPEC
&&
363 altsd
->bInterfaceProtocol
== 2 &&
364 altsd
->bNumEndpoints
== 1 &&
365 USB_ID_VENDOR(subs
->stream
->chip
->usb_id
) == 0x0582 /* Roland */ &&
366 search_roland_implicit_fb(dev
, altsd
->bInterfaceNumber
+ 1,
367 altsd
->bAlternateSetting
,
376 subs
->sync_endpoint
= snd_usb_add_endpoint(subs
->stream
->chip
,
377 alts
, ep
, !subs
->direction
,
378 SND_USB_ENDPOINT_TYPE_DATA
);
379 if (!subs
->sync_endpoint
)
382 subs
->data_endpoint
->sync_master
= subs
->sync_endpoint
;
387 static int set_sync_endpoint(struct snd_usb_substream
*subs
,
388 struct audioformat
*fmt
,
389 struct usb_device
*dev
,
390 struct usb_host_interface
*alts
,
391 struct usb_interface_descriptor
*altsd
)
393 int is_playback
= subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
;
394 unsigned int ep
, attr
;
398 /* we need a sync pipe in async OUT or adaptive IN mode */
399 /* check the number of EP, since some devices have broken
400 * descriptors which fool us. if it has only one EP,
401 * assume it as adaptive-out or sync-in.
403 attr
= fmt
->ep_attr
& USB_ENDPOINT_SYNCTYPE
;
405 if ((is_playback
&& (attr
!= USB_ENDPOINT_SYNC_ASYNC
)) ||
406 (!is_playback
&& (attr
!= USB_ENDPOINT_SYNC_ADAPTIVE
))) {
409 * In these modes the notion of sync_endpoint is irrelevant.
410 * Reset pointers to avoid using stale data from previously
411 * used settings, e.g. when configuration and endpoints were
415 subs
->sync_endpoint
= NULL
;
416 subs
->data_endpoint
->sync_master
= NULL
;
419 err
= set_sync_ep_implicit_fb_quirk(subs
, dev
, altsd
, attr
);
423 if (altsd
->bNumEndpoints
< 2)
426 if ((is_playback
&& (attr
== USB_ENDPOINT_SYNC_SYNC
||
427 attr
== USB_ENDPOINT_SYNC_ADAPTIVE
)) ||
428 (!is_playback
&& attr
!= USB_ENDPOINT_SYNC_ADAPTIVE
))
432 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
433 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
434 * error fall back to SYNC mode and don't create sync endpoint
437 /* check sync-pipe endpoint */
438 /* ... and check descriptor size before accessing bSynchAddress
439 because there is a version of the SB Audigy 2 NX firmware lacking
440 the audio fields in the endpoint descriptors */
441 if ((get_endpoint(alts
, 1)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_ISOC
||
442 (get_endpoint(alts
, 1)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
443 get_endpoint(alts
, 1)->bSynchAddress
!= 0)) {
445 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
446 fmt
->iface
, fmt
->altsetting
,
447 get_endpoint(alts
, 1)->bmAttributes
,
448 get_endpoint(alts
, 1)->bLength
,
449 get_endpoint(alts
, 1)->bSynchAddress
);
450 if (is_playback
&& attr
== USB_ENDPOINT_SYNC_NONE
)
454 ep
= get_endpoint(alts
, 1)->bEndpointAddress
;
455 if (get_endpoint(alts
, 0)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
456 ((is_playback
&& ep
!= (unsigned int)(get_endpoint(alts
, 0)->bSynchAddress
| USB_DIR_IN
)) ||
457 (!is_playback
&& ep
!= (unsigned int)(get_endpoint(alts
, 0)->bSynchAddress
& ~USB_DIR_IN
)))) {
459 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
460 fmt
->iface
, fmt
->altsetting
,
461 is_playback
, ep
, get_endpoint(alts
, 0)->bSynchAddress
);
462 if (is_playback
&& attr
== USB_ENDPOINT_SYNC_NONE
)
467 implicit_fb
= (get_endpoint(alts
, 1)->bmAttributes
& USB_ENDPOINT_USAGE_MASK
)
468 == USB_ENDPOINT_USAGE_IMPLICIT_FB
;
470 subs
->sync_endpoint
= snd_usb_add_endpoint(subs
->stream
->chip
,
471 alts
, ep
, !subs
->direction
,
473 SND_USB_ENDPOINT_TYPE_DATA
:
474 SND_USB_ENDPOINT_TYPE_SYNC
);
475 if (!subs
->sync_endpoint
) {
476 if (is_playback
&& attr
== USB_ENDPOINT_SYNC_NONE
)
481 subs
->data_endpoint
->sync_master
= subs
->sync_endpoint
;
487 * find a matching format and set up the interface
489 static int set_format(struct snd_usb_substream
*subs
, struct audioformat
*fmt
)
491 struct usb_device
*dev
= subs
->dev
;
492 struct usb_host_interface
*alts
;
493 struct usb_interface_descriptor
*altsd
;
494 struct usb_interface
*iface
;
497 iface
= usb_ifnum_to_if(dev
, fmt
->iface
);
500 alts
= &iface
->altsetting
[fmt
->altset_idx
];
501 altsd
= get_iface_desc(alts
);
502 if (WARN_ON(altsd
->bAlternateSetting
!= fmt
->altsetting
))
505 if (fmt
== subs
->cur_audiofmt
)
508 /* close the old interface */
509 if (subs
->interface
>= 0 && subs
->interface
!= fmt
->iface
) {
510 err
= usb_set_interface(subs
->dev
, subs
->interface
, 0);
513 "%d:%d: return to setting 0 failed (%d)\n",
514 fmt
->iface
, fmt
->altsetting
, err
);
517 subs
->interface
= -1;
518 subs
->altset_idx
= 0;
522 if (subs
->interface
!= fmt
->iface
||
523 subs
->altset_idx
!= fmt
->altset_idx
) {
525 err
= snd_usb_select_mode_quirk(subs
, fmt
);
529 err
= usb_set_interface(dev
, fmt
->iface
, fmt
->altsetting
);
532 "%d:%d: usb_set_interface failed (%d)\n",
533 fmt
->iface
, fmt
->altsetting
, err
);
536 dev_dbg(&dev
->dev
, "setting usb interface %d:%d\n",
537 fmt
->iface
, fmt
->altsetting
);
538 subs
->interface
= fmt
->iface
;
539 subs
->altset_idx
= fmt
->altset_idx
;
541 snd_usb_set_interface_quirk(dev
);
544 subs
->data_endpoint
= snd_usb_add_endpoint(subs
->stream
->chip
,
545 alts
, fmt
->endpoint
, subs
->direction
,
546 SND_USB_ENDPOINT_TYPE_DATA
);
548 if (!subs
->data_endpoint
)
551 err
= set_sync_endpoint(subs
, fmt
, dev
, alts
, altsd
);
555 err
= snd_usb_init_pitch(subs
->stream
->chip
, fmt
->iface
, alts
, fmt
);
559 subs
->cur_audiofmt
= fmt
;
561 snd_usb_set_format_quirk(subs
, fmt
);
567 * Return the score of matching two audioformats.
568 * Veto the audioformat if:
569 * - It has no channels for some reason.
570 * - Requested PCM format is not supported.
571 * - Requested sample rate is not supported.
573 static int match_endpoint_audioformats(struct snd_usb_substream
*subs
,
574 struct audioformat
*fp
,
575 struct audioformat
*match
, int rate
,
576 snd_pcm_format_t pcm_format
)
581 if (fp
->channels
< 1) {
582 dev_dbg(&subs
->dev
->dev
,
583 "%s: (fmt @%p) no channels\n", __func__
, fp
);
587 if (!(fp
->formats
& pcm_format_to_bits(pcm_format
))) {
588 dev_dbg(&subs
->dev
->dev
,
589 "%s: (fmt @%p) no match for format %d\n", __func__
,
594 for (i
= 0; i
< fp
->nr_rates
; i
++) {
595 if (fp
->rate_table
[i
] == rate
) {
601 dev_dbg(&subs
->dev
->dev
,
602 "%s: (fmt @%p) no match for rate %d\n", __func__
,
607 if (fp
->channels
== match
->channels
)
610 dev_dbg(&subs
->dev
->dev
,
611 "%s: (fmt @%p) score %d\n", __func__
, fp
, score
);
617 * Configure the sync ep using the rate and pcm format of the data ep.
619 static int configure_sync_endpoint(struct snd_usb_substream
*subs
)
622 struct audioformat
*fp
;
623 struct audioformat
*sync_fp
= NULL
;
625 int sync_period_bytes
= subs
->period_bytes
;
626 struct snd_usb_substream
*sync_subs
=
627 &subs
->stream
->substream
[subs
->direction
^ 1];
629 if (subs
->sync_endpoint
->type
!= SND_USB_ENDPOINT_TYPE_DATA
||
631 return snd_usb_endpoint_set_params(subs
->sync_endpoint
,
640 /* Try to find the best matching audioformat. */
641 list_for_each_entry(fp
, &sync_subs
->fmt_list
, list
) {
642 int score
= match_endpoint_audioformats(subs
,
643 fp
, subs
->cur_audiofmt
,
644 subs
->cur_rate
, subs
->pcm_format
);
646 if (score
> cur_score
) {
652 if (unlikely(sync_fp
== NULL
)) {
653 dev_err(&subs
->dev
->dev
,
654 "%s: no valid audioformat for sync ep %x found\n",
655 __func__
, sync_subs
->ep_num
);
660 * Recalculate the period bytes if channel number differ between
661 * data and sync ep audioformat.
663 if (sync_fp
->channels
!= subs
->channels
) {
664 sync_period_bytes
= (subs
->period_bytes
/ subs
->channels
) *
666 dev_dbg(&subs
->dev
->dev
,
667 "%s: adjusted sync ep period bytes (%d -> %d)\n",
668 __func__
, subs
->period_bytes
, sync_period_bytes
);
671 ret
= snd_usb_endpoint_set_params(subs
->sync_endpoint
,
684 * configure endpoint params
686 * called during initial setup and upon resume
688 static int configure_endpoint(struct snd_usb_substream
*subs
)
693 stop_endpoints(subs
, true);
694 ret
= snd_usb_endpoint_set_params(subs
->data_endpoint
,
699 subs
->buffer_periods
,
702 subs
->sync_endpoint
);
706 if (subs
->sync_endpoint
)
707 ret
= configure_sync_endpoint(subs
);
715 * allocate a buffer and set the given audio format.
717 * so far we use a physically linear buffer although packetize transfer
718 * doesn't need a continuous area.
719 * if sg buffer is supported on the later version of alsa, we'll follow
722 static int snd_usb_hw_params(struct snd_pcm_substream
*substream
,
723 struct snd_pcm_hw_params
*hw_params
)
725 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
726 struct audioformat
*fmt
;
729 ret
= snd_pcm_lib_alloc_vmalloc_buffer(substream
,
730 params_buffer_bytes(hw_params
));
734 subs
->pcm_format
= params_format(hw_params
);
735 subs
->period_bytes
= params_period_bytes(hw_params
);
736 subs
->period_frames
= params_period_size(hw_params
);
737 subs
->buffer_periods
= params_periods(hw_params
);
738 subs
->channels
= params_channels(hw_params
);
739 subs
->cur_rate
= params_rate(hw_params
);
741 fmt
= find_format(subs
);
743 dev_dbg(&subs
->dev
->dev
,
744 "cannot set format: format = %#x, rate = %d, channels = %d\n",
745 subs
->pcm_format
, subs
->cur_rate
, subs
->channels
);
749 ret
= snd_usb_lock_shutdown(subs
->stream
->chip
);
752 ret
= set_format(subs
, fmt
);
753 snd_usb_unlock_shutdown(subs
->stream
->chip
);
757 subs
->interface
= fmt
->iface
;
758 subs
->altset_idx
= fmt
->altset_idx
;
759 subs
->need_setup_ep
= true;
767 * reset the audio format and release the buffer
769 static int snd_usb_hw_free(struct snd_pcm_substream
*substream
)
771 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
773 subs
->cur_audiofmt
= NULL
;
775 subs
->period_bytes
= 0;
776 if (!snd_usb_lock_shutdown(subs
->stream
->chip
)) {
777 stop_endpoints(subs
, true);
778 snd_usb_endpoint_deactivate(subs
->sync_endpoint
);
779 snd_usb_endpoint_deactivate(subs
->data_endpoint
);
780 snd_usb_unlock_shutdown(subs
->stream
->chip
);
782 return snd_pcm_lib_free_vmalloc_buffer(substream
);
788 * only a few subtle things...
790 static int snd_usb_pcm_prepare(struct snd_pcm_substream
*substream
)
792 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
793 struct snd_usb_substream
*subs
= runtime
->private_data
;
794 struct usb_host_interface
*alts
;
795 struct usb_interface
*iface
;
798 if (! subs
->cur_audiofmt
) {
799 dev_err(&subs
->dev
->dev
, "no format is specified!\n");
803 ret
= snd_usb_lock_shutdown(subs
->stream
->chip
);
806 if (snd_BUG_ON(!subs
->data_endpoint
)) {
811 snd_usb_endpoint_sync_pending_stop(subs
->sync_endpoint
);
812 snd_usb_endpoint_sync_pending_stop(subs
->data_endpoint
);
814 ret
= set_format(subs
, subs
->cur_audiofmt
);
818 iface
= usb_ifnum_to_if(subs
->dev
, subs
->cur_audiofmt
->iface
);
819 alts
= &iface
->altsetting
[subs
->cur_audiofmt
->altset_idx
];
820 ret
= snd_usb_init_sample_rate(subs
->stream
->chip
,
821 subs
->cur_audiofmt
->iface
,
828 if (subs
->need_setup_ep
) {
829 ret
= configure_endpoint(subs
);
832 subs
->need_setup_ep
= false;
835 /* some unit conversions in runtime */
836 subs
->data_endpoint
->maxframesize
=
837 bytes_to_frames(runtime
, subs
->data_endpoint
->maxpacksize
);
838 subs
->data_endpoint
->curframesize
=
839 bytes_to_frames(runtime
, subs
->data_endpoint
->curpacksize
);
841 /* reset the pointer */
842 subs
->hwptr_done
= 0;
843 subs
->transfer_done
= 0;
844 subs
->last_delay
= 0;
845 subs
->last_frame_number
= 0;
848 /* for playback, submit the URBs now; otherwise, the first hwptr_done
849 * updates for all URBs would happen at the same time when starting */
850 if (subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
)
851 ret
= start_endpoints(subs
);
854 snd_usb_unlock_shutdown(subs
->stream
->chip
);
858 static struct snd_pcm_hardware snd_usb_hardware
=
860 .info
= SNDRV_PCM_INFO_MMAP
|
861 SNDRV_PCM_INFO_MMAP_VALID
|
862 SNDRV_PCM_INFO_BATCH
|
863 SNDRV_PCM_INFO_INTERLEAVED
|
864 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
865 SNDRV_PCM_INFO_PAUSE
,
866 .buffer_bytes_max
= 1024 * 1024,
867 .period_bytes_min
= 64,
868 .period_bytes_max
= 512 * 1024,
873 static int hw_check_valid_format(struct snd_usb_substream
*subs
,
874 struct snd_pcm_hw_params
*params
,
875 struct audioformat
*fp
)
877 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
878 struct snd_interval
*ct
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
879 struct snd_mask
*fmts
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
880 struct snd_interval
*pt
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
);
881 struct snd_mask check_fmts
;
884 /* check the format */
885 snd_mask_none(&check_fmts
);
886 check_fmts
.bits
[0] = (u32
)fp
->formats
;
887 check_fmts
.bits
[1] = (u32
)(fp
->formats
>> 32);
888 snd_mask_intersect(&check_fmts
, fmts
);
889 if (snd_mask_empty(&check_fmts
)) {
890 hwc_debug(" > check: no supported format %d\n", fp
->format
);
893 /* check the channels */
894 if (fp
->channels
< ct
->min
|| fp
->channels
> ct
->max
) {
895 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp
->channels
, ct
->min
, ct
->max
);
898 /* check the rate is within the range */
899 if (fp
->rate_min
> it
->max
|| (fp
->rate_min
== it
->max
&& it
->openmax
)) {
900 hwc_debug(" > check: rate_min %d > max %d\n", fp
->rate_min
, it
->max
);
903 if (fp
->rate_max
< it
->min
|| (fp
->rate_max
== it
->min
&& it
->openmin
)) {
904 hwc_debug(" > check: rate_max %d < min %d\n", fp
->rate_max
, it
->min
);
907 /* check whether the period time is >= the data packet interval */
908 if (subs
->speed
!= USB_SPEED_FULL
) {
909 ptime
= 125 * (1 << fp
->datainterval
);
910 if (ptime
> pt
->max
|| (ptime
== pt
->max
&& pt
->openmax
)) {
911 hwc_debug(" > check: ptime %u > max %u\n", ptime
, pt
->max
);
918 static int hw_rule_rate(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_RATE
);
924 unsigned int rmin
, rmax
;
927 hwc_debug("hw_rule_rate: (%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
->rate_min
)
936 if (rmax
< fp
->rate_max
)
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
);
970 static int hw_rule_channels(struct snd_pcm_hw_params
*params
,
971 struct snd_pcm_hw_rule
*rule
)
973 struct snd_usb_substream
*subs
= rule
->private;
974 struct audioformat
*fp
;
975 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
976 unsigned int rmin
, rmax
;
979 hwc_debug("hw_rule_channels: (%d,%d)\n", it
->min
, it
->max
);
982 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
983 if (!hw_check_valid_format(subs
, params
, fp
))
986 if (rmin
> fp
->channels
)
988 if (rmax
< fp
->channels
)
997 hwc_debug(" --> get empty\n");
1003 if (it
->min
< rmin
) {
1008 if (it
->max
> rmax
) {
1013 if (snd_interval_checkempty(it
)) {
1017 hwc_debug(" --> (%d, %d) (changed = %d)\n", it
->min
, it
->max
, changed
);
1021 static int hw_rule_format(struct snd_pcm_hw_params
*params
,
1022 struct snd_pcm_hw_rule
*rule
)
1024 struct snd_usb_substream
*subs
= rule
->private;
1025 struct audioformat
*fp
;
1026 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1031 hwc_debug("hw_rule_format: %x:%x\n", fmt
->bits
[0], fmt
->bits
[1]);
1033 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1034 if (!hw_check_valid_format(subs
, params
, fp
))
1036 fbits
|= fp
->formats
;
1039 oldbits
[0] = fmt
->bits
[0];
1040 oldbits
[1] = fmt
->bits
[1];
1041 fmt
->bits
[0] &= (u32
)fbits
;
1042 fmt
->bits
[1] &= (u32
)(fbits
>> 32);
1043 if (!fmt
->bits
[0] && !fmt
->bits
[1]) {
1044 hwc_debug(" --> get empty\n");
1047 changed
= (oldbits
[0] != fmt
->bits
[0] || oldbits
[1] != fmt
->bits
[1]);
1048 hwc_debug(" --> %x:%x (changed = %d)\n", fmt
->bits
[0], fmt
->bits
[1], changed
);
1052 static int hw_rule_period_time(struct snd_pcm_hw_params
*params
,
1053 struct snd_pcm_hw_rule
*rule
)
1055 struct snd_usb_substream
*subs
= rule
->private;
1056 struct audioformat
*fp
;
1057 struct snd_interval
*it
;
1058 unsigned char min_datainterval
;
1062 it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
);
1063 hwc_debug("hw_rule_period_time: (%u,%u)\n", it
->min
, it
->max
);
1064 min_datainterval
= 0xff;
1065 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1066 if (!hw_check_valid_format(subs
, params
, fp
))
1068 min_datainterval
= min(min_datainterval
, fp
->datainterval
);
1070 if (min_datainterval
== 0xff) {
1071 hwc_debug(" --> get empty\n");
1075 pmin
= 125 * (1 << min_datainterval
);
1077 if (it
->min
< pmin
) {
1082 if (snd_interval_checkempty(it
)) {
1086 hwc_debug(" --> (%u,%u) (changed = %d)\n", it
->min
, it
->max
, changed
);
1091 * If the device supports unusual bit rates, does the request meet these?
1093 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime
*runtime
,
1094 struct snd_usb_substream
*subs
)
1096 struct audioformat
*fp
;
1098 int count
= 0, needs_knot
= 0;
1101 kfree(subs
->rate_list
.list
);
1102 subs
->rate_list
.list
= NULL
;
1104 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1105 if (fp
->rates
& SNDRV_PCM_RATE_CONTINUOUS
)
1107 count
+= fp
->nr_rates
;
1108 if (fp
->rates
& SNDRV_PCM_RATE_KNOT
)
1114 subs
->rate_list
.list
= rate_list
=
1115 kmalloc(sizeof(int) * count
, GFP_KERNEL
);
1116 if (!subs
->rate_list
.list
)
1118 subs
->rate_list
.count
= count
;
1119 subs
->rate_list
.mask
= 0;
1121 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1123 for (i
= 0; i
< fp
->nr_rates
; i
++)
1124 rate_list
[count
++] = fp
->rate_table
[i
];
1126 err
= snd_pcm_hw_constraint_list(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1136 * set up the runtime hardware information.
1139 static int setup_hw_info(struct snd_pcm_runtime
*runtime
, struct snd_usb_substream
*subs
)
1141 struct audioformat
*fp
;
1142 unsigned int pt
, ptmin
;
1143 int param_period_time_if_needed
;
1146 runtime
->hw
.formats
= subs
->formats
;
1148 runtime
->hw
.rate_min
= 0x7fffffff;
1149 runtime
->hw
.rate_max
= 0;
1150 runtime
->hw
.channels_min
= 256;
1151 runtime
->hw
.channels_max
= 0;
1152 runtime
->hw
.rates
= 0;
1154 /* check min/max rates and channels */
1155 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
1156 runtime
->hw
.rates
|= fp
->rates
;
1157 if (runtime
->hw
.rate_min
> fp
->rate_min
)
1158 runtime
->hw
.rate_min
= fp
->rate_min
;
1159 if (runtime
->hw
.rate_max
< fp
->rate_max
)
1160 runtime
->hw
.rate_max
= fp
->rate_max
;
1161 if (runtime
->hw
.channels_min
> fp
->channels
)
1162 runtime
->hw
.channels_min
= fp
->channels
;
1163 if (runtime
->hw
.channels_max
< fp
->channels
)
1164 runtime
->hw
.channels_max
= fp
->channels
;
1165 if (fp
->fmt_type
== UAC_FORMAT_TYPE_II
&& fp
->frame_size
> 0) {
1166 /* FIXME: there might be more than one audio formats... */
1167 runtime
->hw
.period_bytes_min
= runtime
->hw
.period_bytes_max
=
1170 pt
= 125 * (1 << fp
->datainterval
);
1171 ptmin
= min(ptmin
, pt
);
1173 err
= snd_usb_autoresume(subs
->stream
->chip
);
1177 param_period_time_if_needed
= SNDRV_PCM_HW_PARAM_PERIOD_TIME
;
1178 if (subs
->speed
== USB_SPEED_FULL
)
1179 /* full speed devices have fixed data packet interval */
1182 /* if period time doesn't go below 1 ms, no rules needed */
1183 param_period_time_if_needed
= -1;
1184 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1187 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1189 SNDRV_PCM_HW_PARAM_FORMAT
,
1190 SNDRV_PCM_HW_PARAM_CHANNELS
,
1191 param_period_time_if_needed
,
1194 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1195 hw_rule_channels
, subs
,
1196 SNDRV_PCM_HW_PARAM_FORMAT
,
1197 SNDRV_PCM_HW_PARAM_RATE
,
1198 param_period_time_if_needed
,
1201 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1202 hw_rule_format
, subs
,
1203 SNDRV_PCM_HW_PARAM_RATE
,
1204 SNDRV_PCM_HW_PARAM_CHANNELS
,
1205 param_period_time_if_needed
,
1208 if (param_period_time_if_needed
>= 0) {
1209 err
= snd_pcm_hw_rule_add(runtime
, 0,
1210 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1211 hw_rule_period_time
, subs
,
1212 SNDRV_PCM_HW_PARAM_FORMAT
,
1213 SNDRV_PCM_HW_PARAM_CHANNELS
,
1214 SNDRV_PCM_HW_PARAM_RATE
,
1219 if ((err
= snd_usb_pcm_check_knot(runtime
, subs
)) < 0)
1224 snd_usb_autosuspend(subs
->stream
->chip
);
1228 static int snd_usb_pcm_open(struct snd_pcm_substream
*substream
, int direction
)
1230 struct snd_usb_stream
*as
= snd_pcm_substream_chip(substream
);
1231 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1232 struct snd_usb_substream
*subs
= &as
->substream
[direction
];
1234 subs
->interface
= -1;
1235 subs
->altset_idx
= 0;
1236 runtime
->hw
= snd_usb_hardware
;
1237 runtime
->private_data
= subs
;
1238 subs
->pcm_substream
= substream
;
1239 /* runtime PM is also done there */
1241 /* initialize DSD/DOP context */
1242 subs
->dsd_dop
.byte_idx
= 0;
1243 subs
->dsd_dop
.channel
= 0;
1244 subs
->dsd_dop
.marker
= 1;
1246 return setup_hw_info(runtime
, subs
);
1249 static int snd_usb_pcm_close(struct snd_pcm_substream
*substream
, int direction
)
1251 struct snd_usb_stream
*as
= snd_pcm_substream_chip(substream
);
1252 struct snd_usb_substream
*subs
= &as
->substream
[direction
];
1254 stop_endpoints(subs
, true);
1256 if (subs
->interface
>= 0 &&
1257 !snd_usb_lock_shutdown(subs
->stream
->chip
)) {
1258 usb_set_interface(subs
->dev
, subs
->interface
, 0);
1259 subs
->interface
= -1;
1260 snd_usb_unlock_shutdown(subs
->stream
->chip
);
1263 subs
->pcm_substream
= NULL
;
1264 snd_usb_autosuspend(subs
->stream
->chip
);
1269 /* Since a URB can handle only a single linear buffer, we must use double
1270 * buffering when the data to be transferred overflows the buffer boundary.
1271 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1274 static void retire_capture_urb(struct snd_usb_substream
*subs
,
1277 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1278 unsigned int stride
, frames
, bytes
, oldptr
;
1279 int i
, period_elapsed
= 0;
1280 unsigned long flags
;
1282 int current_frame_number
;
1284 /* read frame number here, update pointer in critical section */
1285 current_frame_number
= usb_get_current_frame_number(subs
->dev
);
1287 stride
= runtime
->frame_bits
>> 3;
1289 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1290 cp
= (unsigned char *)urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
+ subs
->pkt_offset_adj
;
1291 if (urb
->iso_frame_desc
[i
].status
&& printk_ratelimit()) {
1292 dev_dbg(&subs
->dev
->dev
, "frame %d active: %d\n",
1293 i
, urb
->iso_frame_desc
[i
].status
);
1296 bytes
= urb
->iso_frame_desc
[i
].actual_length
;
1297 frames
= bytes
/ stride
;
1298 if (!subs
->txfr_quirk
)
1299 bytes
= frames
* stride
;
1300 if (bytes
% (runtime
->sample_bits
>> 3) != 0) {
1301 int oldbytes
= bytes
;
1302 bytes
= frames
* stride
;
1303 dev_warn_ratelimited(&subs
->dev
->dev
,
1304 "Corrected urb data len. %d->%d\n",
1307 /* update the current pointer */
1308 spin_lock_irqsave(&subs
->lock
, flags
);
1309 oldptr
= subs
->hwptr_done
;
1310 subs
->hwptr_done
+= bytes
;
1311 if (subs
->hwptr_done
>= runtime
->buffer_size
* stride
)
1312 subs
->hwptr_done
-= runtime
->buffer_size
* stride
;
1313 frames
= (bytes
+ (oldptr
% stride
)) / stride
;
1314 subs
->transfer_done
+= frames
;
1315 if (subs
->transfer_done
>= runtime
->period_size
) {
1316 subs
->transfer_done
-= runtime
->period_size
;
1319 /* capture delay is by construction limited to one URB,
1322 runtime
->delay
= subs
->last_delay
= 0;
1324 /* realign last_frame_number */
1325 subs
->last_frame_number
= current_frame_number
;
1326 subs
->last_frame_number
&= 0xFF; /* keep 8 LSBs */
1328 spin_unlock_irqrestore(&subs
->lock
, flags
);
1329 /* copy a data chunk */
1330 if (oldptr
+ bytes
> runtime
->buffer_size
* stride
) {
1331 unsigned int bytes1
=
1332 runtime
->buffer_size
* stride
- oldptr
;
1333 memcpy(runtime
->dma_area
+ oldptr
, cp
, bytes1
);
1334 memcpy(runtime
->dma_area
, cp
+ bytes1
, bytes
- bytes1
);
1336 memcpy(runtime
->dma_area
+ oldptr
, cp
, bytes
);
1341 snd_pcm_period_elapsed(subs
->pcm_substream
);
1344 static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream
*subs
,
1345 struct urb
*urb
, unsigned int bytes
)
1347 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1348 unsigned int stride
= runtime
->frame_bits
>> 3;
1349 unsigned int dst_idx
= 0;
1350 unsigned int src_idx
= subs
->hwptr_done
;
1351 unsigned int wrap
= runtime
->buffer_size
* stride
;
1352 u8
*dst
= urb
->transfer_buffer
;
1353 u8
*src
= runtime
->dma_area
;
1354 u8 marker
[] = { 0x05, 0xfa };
1357 * The DSP DOP format defines a way to transport DSD samples over
1358 * normal PCM data endpoints. It requires stuffing of marker bytes
1359 * (0x05 and 0xfa, alternating per sample frame), and then expects
1360 * 2 additional bytes of actual payload. The whole frame is stored
1363 * Hence, for a stereo transport, the buffer layout looks like this,
1364 * where L refers to left channel samples and R to right.
1366 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1367 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1373 if (++subs
->dsd_dop
.byte_idx
== 3) {
1374 /* frame boundary? */
1375 dst
[dst_idx
++] = marker
[subs
->dsd_dop
.marker
];
1377 subs
->dsd_dop
.byte_idx
= 0;
1379 if (++subs
->dsd_dop
.channel
% runtime
->channels
== 0) {
1380 /* alternate the marker */
1381 subs
->dsd_dop
.marker
++;
1382 subs
->dsd_dop
.marker
%= ARRAY_SIZE(marker
);
1383 subs
->dsd_dop
.channel
= 0;
1386 /* stuff the DSD payload */
1387 int idx
= (src_idx
+ subs
->dsd_dop
.byte_idx
- 1) % wrap
;
1389 if (subs
->cur_audiofmt
->dsd_bitrev
)
1390 dst
[dst_idx
++] = bitrev8(src
[idx
]);
1392 dst
[dst_idx
++] = src
[idx
];
1397 if (subs
->hwptr_done
>= runtime
->buffer_size
* stride
)
1398 subs
->hwptr_done
-= runtime
->buffer_size
* stride
;
1401 static void copy_to_urb(struct snd_usb_substream
*subs
, struct urb
*urb
,
1402 int offset
, int stride
, unsigned int bytes
)
1404 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1406 if (subs
->hwptr_done
+ bytes
> runtime
->buffer_size
* stride
) {
1407 /* err, the transferred area goes over buffer boundary. */
1408 unsigned int bytes1
=
1409 runtime
->buffer_size
* stride
- subs
->hwptr_done
;
1410 memcpy(urb
->transfer_buffer
+ offset
,
1411 runtime
->dma_area
+ subs
->hwptr_done
, bytes1
);
1412 memcpy(urb
->transfer_buffer
+ offset
+ bytes1
,
1413 runtime
->dma_area
, bytes
- bytes1
);
1415 memcpy(urb
->transfer_buffer
+ offset
,
1416 runtime
->dma_area
+ subs
->hwptr_done
, bytes
);
1418 subs
->hwptr_done
+= bytes
;
1419 if (subs
->hwptr_done
>= runtime
->buffer_size
* stride
)
1420 subs
->hwptr_done
-= runtime
->buffer_size
* stride
;
1423 static unsigned int copy_to_urb_quirk(struct snd_usb_substream
*subs
,
1424 struct urb
*urb
, int stride
,
1427 __le32 packet_length
;
1430 /* Put __le32 length descriptor at start of each packet. */
1431 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1432 unsigned int length
= urb
->iso_frame_desc
[i
].length
;
1433 unsigned int offset
= urb
->iso_frame_desc
[i
].offset
;
1435 packet_length
= cpu_to_le32(length
);
1436 offset
+= i
* sizeof(packet_length
);
1437 urb
->iso_frame_desc
[i
].offset
= offset
;
1438 urb
->iso_frame_desc
[i
].length
+= sizeof(packet_length
);
1439 memcpy(urb
->transfer_buffer
+ offset
,
1440 &packet_length
, sizeof(packet_length
));
1441 copy_to_urb(subs
, urb
, offset
+ sizeof(packet_length
),
1444 /* Adjust transfer size accordingly. */
1445 bytes
+= urb
->number_of_packets
* sizeof(packet_length
);
1449 static void prepare_playback_urb(struct snd_usb_substream
*subs
,
1452 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1453 struct snd_usb_endpoint
*ep
= subs
->data_endpoint
;
1454 struct snd_urb_ctx
*ctx
= urb
->context
;
1455 unsigned int counts
, frames
, bytes
;
1456 int i
, stride
, period_elapsed
= 0;
1457 unsigned long flags
;
1459 stride
= runtime
->frame_bits
>> 3;
1462 urb
->number_of_packets
= 0;
1463 spin_lock_irqsave(&subs
->lock
, flags
);
1464 subs
->frame_limit
+= ep
->max_urb_frames
;
1465 for (i
= 0; i
< ctx
->packets
; i
++) {
1466 if (ctx
->packet_size
[i
])
1467 counts
= ctx
->packet_size
[i
];
1469 counts
= snd_usb_endpoint_next_packet_size(ep
);
1471 /* set up descriptor */
1472 urb
->iso_frame_desc
[i
].offset
= frames
* ep
->stride
;
1473 urb
->iso_frame_desc
[i
].length
= counts
* ep
->stride
;
1475 urb
->number_of_packets
++;
1476 subs
->transfer_done
+= counts
;
1477 if (subs
->transfer_done
>= runtime
->period_size
) {
1478 subs
->transfer_done
-= runtime
->period_size
;
1479 subs
->frame_limit
= 0;
1481 if (subs
->fmt_type
== UAC_FORMAT_TYPE_II
) {
1482 if (subs
->transfer_done
> 0) {
1483 /* FIXME: fill-max mode is not
1485 frames
-= subs
->transfer_done
;
1486 counts
-= subs
->transfer_done
;
1487 urb
->iso_frame_desc
[i
].length
=
1488 counts
* ep
->stride
;
1489 subs
->transfer_done
= 0;
1492 if (i
< ctx
->packets
) {
1493 /* add a transfer delimiter */
1494 urb
->iso_frame_desc
[i
].offset
=
1495 frames
* ep
->stride
;
1496 urb
->iso_frame_desc
[i
].length
= 0;
1497 urb
->number_of_packets
++;
1502 /* finish at the period boundary or after enough frames */
1503 if ((period_elapsed
||
1504 subs
->transfer_done
>= subs
->frame_limit
) &&
1505 !snd_usb_endpoint_implicit_feedback_sink(ep
))
1508 bytes
= frames
* ep
->stride
;
1510 if (unlikely(subs
->pcm_format
== SNDRV_PCM_FORMAT_DSD_U16_LE
&&
1511 subs
->cur_audiofmt
->dsd_dop
)) {
1512 fill_playback_urb_dsd_dop(subs
, urb
, bytes
);
1513 } else if (unlikely(subs
->pcm_format
== SNDRV_PCM_FORMAT_DSD_U8
&&
1514 subs
->cur_audiofmt
->dsd_bitrev
)) {
1515 /* bit-reverse the bytes */
1516 u8
*buf
= urb
->transfer_buffer
;
1517 for (i
= 0; i
< bytes
; i
++) {
1518 int idx
= (subs
->hwptr_done
+ i
)
1519 % (runtime
->buffer_size
* stride
);
1520 buf
[i
] = bitrev8(runtime
->dma_area
[idx
]);
1523 subs
->hwptr_done
+= bytes
;
1524 if (subs
->hwptr_done
>= runtime
->buffer_size
* stride
)
1525 subs
->hwptr_done
-= runtime
->buffer_size
* stride
;
1528 if (!subs
->tx_length_quirk
)
1529 copy_to_urb(subs
, urb
, 0, stride
, bytes
);
1531 bytes
= copy_to_urb_quirk(subs
, urb
, stride
, bytes
);
1532 /* bytes is now amount of outgoing data */
1535 /* update delay with exact number of samples queued */
1536 runtime
->delay
= subs
->last_delay
;
1537 runtime
->delay
+= frames
;
1538 subs
->last_delay
= runtime
->delay
;
1540 /* realign last_frame_number */
1541 subs
->last_frame_number
= usb_get_current_frame_number(subs
->dev
);
1542 subs
->last_frame_number
&= 0xFF; /* keep 8 LSBs */
1544 if (subs
->trigger_tstamp_pending_update
) {
1545 /* this is the first actual URB submitted,
1546 * update trigger timestamp to reflect actual start time
1548 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
1549 subs
->trigger_tstamp_pending_update
= false;
1552 spin_unlock_irqrestore(&subs
->lock
, flags
);
1553 urb
->transfer_buffer_length
= bytes
;
1555 snd_pcm_period_elapsed(subs
->pcm_substream
);
1559 * process after playback data complete
1560 * - decrease the delay count again
1562 static void retire_playback_urb(struct snd_usb_substream
*subs
,
1565 unsigned long flags
;
1566 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
1567 struct snd_usb_endpoint
*ep
= subs
->data_endpoint
;
1568 int processed
= urb
->transfer_buffer_length
/ ep
->stride
;
1571 /* ignore the delay accounting when procssed=0 is given, i.e.
1572 * silent payloads are procssed before handling the actual data
1577 spin_lock_irqsave(&subs
->lock
, flags
);
1578 if (!subs
->last_delay
)
1579 goto out
; /* short path */
1581 est_delay
= snd_usb_pcm_delay(subs
, runtime
->rate
);
1582 /* update delay with exact number of samples played */
1583 if (processed
> subs
->last_delay
)
1584 subs
->last_delay
= 0;
1586 subs
->last_delay
-= processed
;
1587 runtime
->delay
= subs
->last_delay
;
1590 * Report when delay estimate is off by more than 2ms.
1591 * The error should be lower than 2ms since the estimate relies
1592 * on two reads of a counter updated every ms.
1594 if (abs(est_delay
- subs
->last_delay
) * 1000 > runtime
->rate
* 2)
1595 dev_dbg_ratelimited(&subs
->dev
->dev
,
1596 "delay: estimated %d, actual %d\n",
1597 est_delay
, subs
->last_delay
);
1599 if (!subs
->running
) {
1600 /* update last_frame_number for delay counting here since
1601 * prepare_playback_urb won't be called during pause
1603 subs
->last_frame_number
=
1604 usb_get_current_frame_number(subs
->dev
) & 0xff;
1608 spin_unlock_irqrestore(&subs
->lock
, flags
);
1611 static int snd_usb_playback_open(struct snd_pcm_substream
*substream
)
1613 return snd_usb_pcm_open(substream
, SNDRV_PCM_STREAM_PLAYBACK
);
1616 static int snd_usb_playback_close(struct snd_pcm_substream
*substream
)
1618 return snd_usb_pcm_close(substream
, SNDRV_PCM_STREAM_PLAYBACK
);
1621 static int snd_usb_capture_open(struct snd_pcm_substream
*substream
)
1623 return snd_usb_pcm_open(substream
, SNDRV_PCM_STREAM_CAPTURE
);
1626 static int snd_usb_capture_close(struct snd_pcm_substream
*substream
)
1628 return snd_usb_pcm_close(substream
, SNDRV_PCM_STREAM_CAPTURE
);
1631 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream
*substream
,
1634 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
1637 case SNDRV_PCM_TRIGGER_START
:
1638 subs
->trigger_tstamp_pending_update
= true;
1639 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1640 subs
->data_endpoint
->prepare_data_urb
= prepare_playback_urb
;
1641 subs
->data_endpoint
->retire_data_urb
= retire_playback_urb
;
1644 case SNDRV_PCM_TRIGGER_STOP
:
1645 stop_endpoints(subs
, false);
1648 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1649 subs
->data_endpoint
->prepare_data_urb
= NULL
;
1650 /* keep retire_data_urb for delay calculation */
1651 subs
->data_endpoint
->retire_data_urb
= retire_playback_urb
;
1659 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream
*substream
,
1663 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
1666 case SNDRV_PCM_TRIGGER_START
:
1667 err
= start_endpoints(subs
);
1671 subs
->data_endpoint
->retire_data_urb
= retire_capture_urb
;
1674 case SNDRV_PCM_TRIGGER_STOP
:
1675 stop_endpoints(subs
, false);
1678 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1679 subs
->data_endpoint
->retire_data_urb
= NULL
;
1682 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1683 subs
->data_endpoint
->retire_data_urb
= retire_capture_urb
;
1691 static struct snd_pcm_ops snd_usb_playback_ops
= {
1692 .open
= snd_usb_playback_open
,
1693 .close
= snd_usb_playback_close
,
1694 .ioctl
= snd_pcm_lib_ioctl
,
1695 .hw_params
= snd_usb_hw_params
,
1696 .hw_free
= snd_usb_hw_free
,
1697 .prepare
= snd_usb_pcm_prepare
,
1698 .trigger
= snd_usb_substream_playback_trigger
,
1699 .pointer
= snd_usb_pcm_pointer
,
1700 .page
= snd_pcm_lib_get_vmalloc_page
,
1701 .mmap
= snd_pcm_lib_mmap_vmalloc
,
1704 static struct snd_pcm_ops snd_usb_capture_ops
= {
1705 .open
= snd_usb_capture_open
,
1706 .close
= snd_usb_capture_close
,
1707 .ioctl
= snd_pcm_lib_ioctl
,
1708 .hw_params
= snd_usb_hw_params
,
1709 .hw_free
= snd_usb_hw_free
,
1710 .prepare
= snd_usb_pcm_prepare
,
1711 .trigger
= snd_usb_substream_capture_trigger
,
1712 .pointer
= snd_usb_pcm_pointer
,
1713 .page
= snd_pcm_lib_get_vmalloc_page
,
1714 .mmap
= snd_pcm_lib_mmap_vmalloc
,
1717 void snd_usb_set_pcm_ops(struct snd_pcm
*pcm
, int stream
)
1719 snd_pcm_set_ops(pcm
, stream
,
1720 stream
== SNDRV_PCM_STREAM_PLAYBACK
?
1721 &snd_usb_playback_ops
: &snd_usb_capture_ops
);