ALSA: usb-audio: simplify endpoint deactivation code
[deliverable/linux.git] / sound / usb / pcm.c
CommitLineData
e5779998
DM
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
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.
11 *
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
15 */
16
17#include <linux/init.h>
9966ddaf 18#include <linux/slab.h>
edcd3633 19#include <linux/ratelimit.h>
e5779998
DM
20#include <linux/usb.h>
21#include <linux/usb/audio.h>
7e847894 22#include <linux/usb/audio-v2.h>
e5779998
DM
23
24#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27
28#include "usbaudio.h"
29#include "card.h"
30#include "quirks.h"
31#include "debug.h"
c731bc96 32#include "endpoint.h"
e5779998
DM
33#include "helper.h"
34#include "pcm.h"
79f920fb 35#include "clock.h"
88a8516a 36#include "power.h"
e5779998 37
edcd3633
DM
38#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
39#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
40
294c4fb8
PLB
41/* return the estimated delay based on USB frame counters */
42snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
43 unsigned int rate)
44{
45 int current_frame_number;
46 int frame_diff;
47 int est_delay;
48
49 current_frame_number = usb_get_current_frame_number(subs->dev);
50 /*
51 * HCD implementations use different widths, use lower 8 bits.
52 * The delay will be managed up to 256ms, which is more than
53 * enough
54 */
55 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
56
57 /* Approximation based on number of samples per USB frame (ms),
58 some truncation for 44.1 but the estimate is good enough */
59 est_delay = subs->last_delay - (frame_diff * rate / 1000);
60 if (est_delay < 0)
61 est_delay = 0;
62 return est_delay;
63}
64
e5779998
DM
65/*
66 * return the current pcm pointer. just based on the hwptr_done value.
67 */
68static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
69{
70 struct snd_usb_substream *subs;
71 unsigned int hwptr_done;
72
73 subs = (struct snd_usb_substream *)substream->runtime->private_data;
978520b7
TI
74 if (subs->stream->chip->shutdown)
75 return SNDRV_PCM_POS_XRUN;
e5779998
DM
76 spin_lock(&subs->lock);
77 hwptr_done = subs->hwptr_done;
294c4fb8
PLB
78 substream->runtime->delay = snd_usb_pcm_delay(subs,
79 substream->runtime->rate);
e5779998
DM
80 spin_unlock(&subs->lock);
81 return hwptr_done / (substream->runtime->frame_bits >> 3);
82}
83
84/*
85 * find a matching audio format
86 */
61a70950 87static struct audioformat *find_format(struct snd_usb_substream *subs)
e5779998
DM
88{
89 struct list_head *p;
90 struct audioformat *found = NULL;
91 int cur_attr = 0, attr;
92
93 list_for_each(p, &subs->fmt_list) {
94 struct audioformat *fp;
95 fp = list_entry(p, struct audioformat, list);
61a70950 96 if (!(fp->formats & (1uLL << subs->pcm_format)))
015eb0b0 97 continue;
61a70950 98 if (fp->channels != subs->channels)
e5779998 99 continue;
61a70950
DR
100 if (subs->cur_rate < fp->rate_min ||
101 subs->cur_rate > fp->rate_max)
e5779998
DM
102 continue;
103 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
104 unsigned int i;
105 for (i = 0; i < fp->nr_rates; i++)
61a70950 106 if (fp->rate_table[i] == subs->cur_rate)
e5779998
DM
107 break;
108 if (i >= fp->nr_rates)
109 continue;
110 }
111 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
112 if (! found) {
113 found = fp;
114 cur_attr = attr;
115 continue;
116 }
117 /* avoid async out and adaptive in if the other method
118 * supports the same format.
119 * this is a workaround for the case like
120 * M-audio audiophile USB.
121 */
122 if (attr != cur_attr) {
123 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
124 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
125 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
126 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
127 continue;
128 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
129 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
130 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
131 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
132 found = fp;
133 cur_attr = attr;
134 continue;
135 }
136 }
137 /* find the format with the largest max. packet size */
138 if (fp->maxpacksize > found->maxpacksize) {
139 found = fp;
140 cur_attr = attr;
141 }
142 }
143 return found;
144}
145
767d75ad
DM
146static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
147 struct usb_host_interface *alts,
148 struct audioformat *fmt)
149{
150 struct usb_device *dev = chip->dev;
151 unsigned int ep;
152 unsigned char data[1];
153 int err;
154
155 ep = get_endpoint(alts, 0)->bEndpointAddress;
156
767d75ad
DM
157 data[0] = 1;
158 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
159 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
160 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
17d900c4 161 data, sizeof(data))) < 0) {
767d75ad
DM
162 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
163 dev->devnum, iface, ep);
164 return err;
165 }
166
167 return 0;
168}
e5779998 169
92c25611
DM
170static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
171 struct usb_host_interface *alts,
172 struct audioformat *fmt)
173{
174 struct usb_device *dev = chip->dev;
175 unsigned char data[1];
92c25611
DM
176 int err;
177
92c25611
DM
178 data[0] = 1;
179 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
180 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
181 UAC2_EP_CS_PITCH << 8, 0,
17d900c4 182 data, sizeof(data))) < 0) {
92c25611
DM
183 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
184 dev->devnum, iface, fmt->altsetting);
185 return err;
186 }
187
188 return 0;
189}
190
e5779998 191/*
92c25611 192 * initialize the pitch control and sample rate
e5779998 193 */
767d75ad 194int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
e5779998
DM
195 struct usb_host_interface *alts,
196 struct audioformat *fmt)
197{
767d75ad
DM
198 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
199
92c25611
DM
200 /* if endpoint doesn't have pitch control, bail out */
201 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
202 return 0;
203
767d75ad
DM
204 switch (altsd->bInterfaceProtocol) {
205 case UAC_VERSION_1:
a2acad82 206 default:
767d75ad
DM
207 return init_pitch_v1(chip, iface, alts, fmt);
208
209 case UAC_VERSION_2:
92c25611 210 return init_pitch_v2(chip, iface, alts, fmt);
767d75ad 211 }
767d75ad
DM
212}
213
a9bb3626 214static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
edcd3633
DM
215{
216 int err;
217
218 if (!subs->data_endpoint)
219 return -EINVAL;
220
221 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
222 struct snd_usb_endpoint *ep = subs->data_endpoint;
223
224 snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
225
226 ep->data_subs = subs;
015618b9 227 err = snd_usb_endpoint_start(ep, can_sleep);
edcd3633
DM
228 if (err < 0) {
229 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
230 return err;
231 }
232 }
233
234 if (subs->sync_endpoint &&
235 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
236 struct snd_usb_endpoint *ep = subs->sync_endpoint;
237
2e4a263c
DM
238 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
239 subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
240 err = usb_set_interface(subs->dev,
241 subs->sync_endpoint->iface,
242 subs->sync_endpoint->alt_idx);
243 if (err < 0) {
244 snd_printk(KERN_ERR
245 "%d:%d:%d: cannot set interface (%d)\n",
246 subs->dev->devnum,
247 subs->sync_endpoint->iface,
248 subs->sync_endpoint->alt_idx, err);
249 return -EIO;
250 }
251 }
252
edcd3633
DM
253 snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
254
255 ep->sync_slave = subs->data_endpoint;
015618b9 256 err = snd_usb_endpoint_start(ep, can_sleep);
edcd3633
DM
257 if (err < 0) {
258 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
259 return err;
260 }
261 }
262
263 return 0;
264}
265
a9bb3626 266static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
edcd3633
DM
267{
268 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
a9bb3626 269 snd_usb_endpoint_stop(subs->sync_endpoint, wait);
edcd3633
DM
270
271 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
a9bb3626 272 snd_usb_endpoint_stop(subs->data_endpoint, wait);
edcd3633
DM
273}
274
edcd3633
DM
275static int deactivate_endpoints(struct snd_usb_substream *subs)
276{
277 int reta, retb;
278
279 reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
280 retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
281
282 if (reta < 0)
283 return reta;
284
285 if (retb < 0)
286 return retb;
287
288 return 0;
289}
290
e5779998
DM
291/*
292 * find a matching format and set up the interface
293 */
294static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
295{
296 struct usb_device *dev = subs->dev;
297 struct usb_host_interface *alts;
298 struct usb_interface_descriptor *altsd;
299 struct usb_interface *iface;
300 unsigned int ep, attr;
301 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
c75a8a7a 302 int err, implicit_fb = 0;
e5779998
DM
303
304 iface = usb_ifnum_to_if(dev, fmt->iface);
305 if (WARN_ON(!iface))
306 return -EINVAL;
307 alts = &iface->altsetting[fmt->altset_idx];
308 altsd = get_iface_desc(alts);
309 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
310 return -EINVAL;
311
312 if (fmt == subs->cur_audiofmt)
313 return 0;
314
68e67f40
DM
315 /* close the old interface */
316 if (subs->interface >= 0 && subs->interface != fmt->iface) {
317 err = usb_set_interface(subs->dev, subs->interface, 0);
318 if (err < 0) {
319 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
320 dev->devnum, fmt->iface, fmt->altsetting, err);
321 return -EIO;
322 }
323 subs->interface = -1;
324 subs->altset_idx = 0;
325 }
326
327 /* set interface */
328 if (subs->interface != fmt->iface ||
329 subs->altset_idx != fmt->altset_idx) {
330 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
331 if (err < 0) {
332 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
333 dev->devnum, fmt->iface, fmt->altsetting, err);
334 return -EIO;
335 }
336 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
337 fmt->iface, fmt->altsetting);
338 subs->interface = fmt->iface;
339 subs->altset_idx = fmt->altset_idx;
340 }
341
edcd3633
DM
342 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
343 alts, fmt->endpoint, subs->direction,
344 SND_USB_ENDPOINT_TYPE_DATA);
345 if (!subs->data_endpoint)
346 return -EINVAL;
e5779998
DM
347
348 /* we need a sync pipe in async OUT or adaptive IN mode */
349 /* check the number of EP, since some devices have broken
350 * descriptors which fool us. if it has only one EP,
351 * assume it as adaptive-out or sync-in.
352 */
353 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
c75a8a7a
DM
354
355 switch (subs->stream->chip->usb_id) {
356 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
357 case USB_ID(0x0763, 0x2081):
358 if (is_playback) {
359 implicit_fb = 1;
edcd3633
DM
360 ep = 0x81;
361 iface = usb_ifnum_to_if(dev, 2);
c75a8a7a
DM
362
363 if (!iface || iface->num_altsetting == 0)
364 return -EINVAL;
365
edcd3633
DM
366 alts = &iface->altsetting[1];
367 goto add_sync_ep;
368 }
c75a8a7a 369 }
edcd3633 370
c75a8a7a
DM
371 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
372 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
373 altsd->bNumEndpoints >= 2) {
e5779998
DM
374 /* check sync-pipe endpoint */
375 /* ... and check descriptor size before accessing bSynchAddress
376 because there is a version of the SB Audigy 2 NX firmware lacking
377 the audio fields in the endpoint descriptors */
378 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
379 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
c75a8a7a
DM
380 get_endpoint(alts, 1)->bSynchAddress != 0 &&
381 !implicit_fb)) {
7fb75db1
DM
382 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
383 dev->devnum, fmt->iface, fmt->altsetting,
384 get_endpoint(alts, 1)->bmAttributes,
385 get_endpoint(alts, 1)->bLength,
386 get_endpoint(alts, 1)->bSynchAddress);
e5779998
DM
387 return -EINVAL;
388 }
389 ep = get_endpoint(alts, 1)->bEndpointAddress;
7fb75db1
DM
390 if (!implicit_fb &&
391 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
e5779998 392 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
7fb75db1
DM
393 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
394 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
395 dev->devnum, fmt->iface, fmt->altsetting,
396 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
e5779998
DM
397 return -EINVAL;
398 }
c75a8a7a
DM
399
400 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
401 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
402
edcd3633
DM
403add_sync_ep:
404 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
405 alts, ep, !subs->direction,
c75a8a7a
DM
406 implicit_fb ?
407 SND_USB_ENDPOINT_TYPE_DATA :
408 SND_USB_ENDPOINT_TYPE_SYNC);
edcd3633
DM
409 if (!subs->sync_endpoint)
410 return -EINVAL;
411
412 subs->data_endpoint->sync_master = subs->sync_endpoint;
413 }
e5779998 414
9e9b5946 415 if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
e5779998
DM
416 return err;
417
418 subs->cur_audiofmt = fmt;
419
420 snd_usb_set_format_quirk(subs, fmt);
421
422#if 0
423 printk(KERN_DEBUG
424 "setting done: format = %d, rate = %d..%d, channels = %d\n",
425 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
426 printk(KERN_DEBUG
427 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
428 subs->datapipe, subs->syncpipe);
429#endif
430
431 return 0;
432}
433
61a70950
DR
434/*
435 * configure endpoint params
436 *
437 * called during initial setup and upon resume
438 */
439static int configure_endpoint(struct snd_usb_substream *subs)
440{
441 int ret;
442
61a70950 443 /* format changed */
a9bb3626 444 stop_endpoints(subs, false);
61a70950
DR
445 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
446 subs->pcm_format,
447 subs->channels,
448 subs->period_bytes,
449 subs->cur_rate,
450 subs->cur_audiofmt,
451 subs->sync_endpoint);
452 if (ret < 0)
978520b7 453 return ret;
61a70950
DR
454
455 if (subs->sync_endpoint)
456 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
457 subs->pcm_format,
458 subs->channels,
459 subs->period_bytes,
460 subs->cur_rate,
461 subs->cur_audiofmt,
462 NULL);
61a70950
DR
463 return ret;
464}
465
e5779998
DM
466/*
467 * hw_params callback
468 *
469 * allocate a buffer and set the given audio format.
470 *
471 * so far we use a physically linear buffer although packetize transfer
472 * doesn't need a continuous area.
473 * if sg buffer is supported on the later version of alsa, we'll follow
474 * that.
475 */
476static int snd_usb_hw_params(struct snd_pcm_substream *substream,
477 struct snd_pcm_hw_params *hw_params)
478{
479 struct snd_usb_substream *subs = substream->runtime->private_data;
480 struct audioformat *fmt;
61a70950 481 int ret;
e5779998
DM
482
483 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
484 params_buffer_bytes(hw_params));
485 if (ret < 0)
486 return ret;
487
61a70950
DR
488 subs->pcm_format = params_format(hw_params);
489 subs->period_bytes = params_period_bytes(hw_params);
490 subs->channels = params_channels(hw_params);
491 subs->cur_rate = params_rate(hw_params);
492
493 fmt = find_format(subs);
e5779998
DM
494 if (!fmt) {
495 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
61a70950 496 subs->pcm_format, subs->cur_rate, subs->channels);
e5779998
DM
497 return -EINVAL;
498 }
499
34f3c89f 500 down_read(&subs->stream->chip->shutdown_rwsem);
978520b7
TI
501 if (subs->stream->chip->shutdown)
502 ret = -ENODEV;
503 else
504 ret = set_format(subs, fmt);
34f3c89f 505 up_read(&subs->stream->chip->shutdown_rwsem);
978520b7 506 if (ret < 0)
e5779998
DM
507 return ret;
508
61a70950
DR
509 subs->interface = fmt->iface;
510 subs->altset_idx = fmt->altset_idx;
384dc085 511 subs->need_setup_ep = true;
715a1705 512
61a70950 513 return 0;
e5779998
DM
514}
515
516/*
517 * hw_free callback
518 *
519 * reset the audio format and release the buffer
520 */
521static int snd_usb_hw_free(struct snd_pcm_substream *substream)
522{
523 struct snd_usb_substream *subs = substream->runtime->private_data;
524
525 subs->cur_audiofmt = NULL;
526 subs->cur_rate = 0;
527 subs->period_bytes = 0;
34f3c89f 528 down_read(&subs->stream->chip->shutdown_rwsem);
978520b7 529 if (!subs->stream->chip->shutdown) {
a9bb3626 530 stop_endpoints(subs, true);
978520b7
TI
531 deactivate_endpoints(subs);
532 }
34f3c89f 533 up_read(&subs->stream->chip->shutdown_rwsem);
e5779998
DM
534 return snd_pcm_lib_free_vmalloc_buffer(substream);
535}
536
537/*
538 * prepare callback
539 *
540 * only a few subtle things...
541 */
542static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
543{
544 struct snd_pcm_runtime *runtime = substream->runtime;
545 struct snd_usb_substream *subs = runtime->private_data;
61a70950
DR
546 struct usb_host_interface *alts;
547 struct usb_interface *iface;
548 int ret;
e5779998
DM
549
550 if (! subs->cur_audiofmt) {
551 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
552 return -ENXIO;
553 }
554
34f3c89f 555 down_read(&subs->stream->chip->shutdown_rwsem);
978520b7
TI
556 if (subs->stream->chip->shutdown) {
557 ret = -ENODEV;
558 goto unlock;
559 }
560 if (snd_BUG_ON(!subs->data_endpoint)) {
561 ret = -EIO;
562 goto unlock;
563 }
edcd3633 564
f58161ba
TI
565 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
566 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
567
61a70950
DR
568 ret = set_format(subs, subs->cur_audiofmt);
569 if (ret < 0)
978520b7 570 goto unlock;
61a70950
DR
571
572 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
573 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
574 ret = snd_usb_init_sample_rate(subs->stream->chip,
575 subs->cur_audiofmt->iface,
576 alts,
577 subs->cur_audiofmt,
578 subs->cur_rate);
579 if (ret < 0)
978520b7 580 goto unlock;
61a70950 581
384dc085
TI
582 if (subs->need_setup_ep) {
583 ret = configure_endpoint(subs);
584 if (ret < 0)
978520b7 585 goto unlock;
384dc085
TI
586 subs->need_setup_ep = false;
587 }
61a70950 588
e5779998 589 /* some unit conversions in runtime */
edcd3633
DM
590 subs->data_endpoint->maxframesize =
591 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
592 subs->data_endpoint->curframesize =
593 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
e5779998
DM
594
595 /* reset the pointer */
596 subs->hwptr_done = 0;
597 subs->transfer_done = 0;
294c4fb8
PLB
598 subs->last_delay = 0;
599 subs->last_frame_number = 0;
e5779998
DM
600 runtime->delay = 0;
601
edcd3633
DM
602 /* for playback, submit the URBs now; otherwise, the first hwptr_done
603 * updates for all URBs would happen at the same time when starting */
604 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
a9bb3626 605 ret = start_endpoints(subs, true);
edcd3633 606
978520b7 607 unlock:
34f3c89f 608 up_read(&subs->stream->chip->shutdown_rwsem);
978520b7 609 return ret;
e5779998
DM
610}
611
612static struct snd_pcm_hardware snd_usb_hardware =
613{
614 .info = SNDRV_PCM_INFO_MMAP |
615 SNDRV_PCM_INFO_MMAP_VALID |
616 SNDRV_PCM_INFO_BATCH |
617 SNDRV_PCM_INFO_INTERLEAVED |
618 SNDRV_PCM_INFO_BLOCK_TRANSFER |
619 SNDRV_PCM_INFO_PAUSE,
620 .buffer_bytes_max = 1024 * 1024,
621 .period_bytes_min = 64,
622 .period_bytes_max = 512 * 1024,
623 .periods_min = 2,
624 .periods_max = 1024,
625};
626
627static int hw_check_valid_format(struct snd_usb_substream *subs,
628 struct snd_pcm_hw_params *params,
629 struct audioformat *fp)
630{
631 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
632 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
633 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
634 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
015eb0b0 635 struct snd_mask check_fmts;
e5779998
DM
636 unsigned int ptime;
637
638 /* check the format */
015eb0b0
CL
639 snd_mask_none(&check_fmts);
640 check_fmts.bits[0] = (u32)fp->formats;
641 check_fmts.bits[1] = (u32)(fp->formats >> 32);
642 snd_mask_intersect(&check_fmts, fmts);
643 if (snd_mask_empty(&check_fmts)) {
e5779998
DM
644 hwc_debug(" > check: no supported format %d\n", fp->format);
645 return 0;
646 }
647 /* check the channels */
648 if (fp->channels < ct->min || fp->channels > ct->max) {
649 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
650 return 0;
651 }
652 /* check the rate is within the range */
653 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
654 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
655 return 0;
656 }
657 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
658 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
659 return 0;
660 }
661 /* check whether the period time is >= the data packet interval */
978520b7 662 if (subs->speed != USB_SPEED_FULL) {
e5779998
DM
663 ptime = 125 * (1 << fp->datainterval);
664 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
665 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
666 return 0;
667 }
668 }
669 return 1;
670}
671
672static int hw_rule_rate(struct snd_pcm_hw_params *params,
673 struct snd_pcm_hw_rule *rule)
674{
675 struct snd_usb_substream *subs = rule->private;
676 struct list_head *p;
677 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
678 unsigned int rmin, rmax;
679 int changed;
680
681 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
682 changed = 0;
683 rmin = rmax = 0;
684 list_for_each(p, &subs->fmt_list) {
685 struct audioformat *fp;
686 fp = list_entry(p, struct audioformat, list);
687 if (!hw_check_valid_format(subs, params, fp))
688 continue;
689 if (changed++) {
690 if (rmin > fp->rate_min)
691 rmin = fp->rate_min;
692 if (rmax < fp->rate_max)
693 rmax = fp->rate_max;
694 } else {
695 rmin = fp->rate_min;
696 rmax = fp->rate_max;
697 }
698 }
699
700 if (!changed) {
701 hwc_debug(" --> get empty\n");
702 it->empty = 1;
703 return -EINVAL;
704 }
705
706 changed = 0;
707 if (it->min < rmin) {
708 it->min = rmin;
709 it->openmin = 0;
710 changed = 1;
711 }
712 if (it->max > rmax) {
713 it->max = rmax;
714 it->openmax = 0;
715 changed = 1;
716 }
717 if (snd_interval_checkempty(it)) {
718 it->empty = 1;
719 return -EINVAL;
720 }
721 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
722 return changed;
723}
724
725
726static int hw_rule_channels(struct snd_pcm_hw_params *params,
727 struct snd_pcm_hw_rule *rule)
728{
729 struct snd_usb_substream *subs = rule->private;
730 struct list_head *p;
731 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
732 unsigned int rmin, rmax;
733 int changed;
734
735 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
736 changed = 0;
737 rmin = rmax = 0;
738 list_for_each(p, &subs->fmt_list) {
739 struct audioformat *fp;
740 fp = list_entry(p, struct audioformat, list);
741 if (!hw_check_valid_format(subs, params, fp))
742 continue;
743 if (changed++) {
744 if (rmin > fp->channels)
745 rmin = fp->channels;
746 if (rmax < fp->channels)
747 rmax = fp->channels;
748 } else {
749 rmin = fp->channels;
750 rmax = fp->channels;
751 }
752 }
753
754 if (!changed) {
755 hwc_debug(" --> get empty\n");
756 it->empty = 1;
757 return -EINVAL;
758 }
759
760 changed = 0;
761 if (it->min < rmin) {
762 it->min = rmin;
763 it->openmin = 0;
764 changed = 1;
765 }
766 if (it->max > rmax) {
767 it->max = rmax;
768 it->openmax = 0;
769 changed = 1;
770 }
771 if (snd_interval_checkempty(it)) {
772 it->empty = 1;
773 return -EINVAL;
774 }
775 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
776 return changed;
777}
778
779static int hw_rule_format(struct snd_pcm_hw_params *params,
780 struct snd_pcm_hw_rule *rule)
781{
782 struct snd_usb_substream *subs = rule->private;
783 struct list_head *p;
784 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
785 u64 fbits;
786 u32 oldbits[2];
787 int changed;
788
789 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
790 fbits = 0;
791 list_for_each(p, &subs->fmt_list) {
792 struct audioformat *fp;
793 fp = list_entry(p, struct audioformat, list);
794 if (!hw_check_valid_format(subs, params, fp))
795 continue;
015eb0b0 796 fbits |= fp->formats;
e5779998
DM
797 }
798
799 oldbits[0] = fmt->bits[0];
800 oldbits[1] = fmt->bits[1];
801 fmt->bits[0] &= (u32)fbits;
802 fmt->bits[1] &= (u32)(fbits >> 32);
803 if (!fmt->bits[0] && !fmt->bits[1]) {
804 hwc_debug(" --> get empty\n");
805 return -EINVAL;
806 }
807 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
808 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
809 return changed;
810}
811
812static int hw_rule_period_time(struct snd_pcm_hw_params *params,
813 struct snd_pcm_hw_rule *rule)
814{
815 struct snd_usb_substream *subs = rule->private;
816 struct audioformat *fp;
817 struct snd_interval *it;
818 unsigned char min_datainterval;
819 unsigned int pmin;
820 int changed;
821
822 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
823 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
824 min_datainterval = 0xff;
825 list_for_each_entry(fp, &subs->fmt_list, list) {
826 if (!hw_check_valid_format(subs, params, fp))
827 continue;
828 min_datainterval = min(min_datainterval, fp->datainterval);
829 }
830 if (min_datainterval == 0xff) {
a7ce2e0d 831 hwc_debug(" --> get empty\n");
e5779998
DM
832 it->empty = 1;
833 return -EINVAL;
834 }
835 pmin = 125 * (1 << min_datainterval);
836 changed = 0;
837 if (it->min < pmin) {
838 it->min = pmin;
839 it->openmin = 0;
840 changed = 1;
841 }
842 if (snd_interval_checkempty(it)) {
843 it->empty = 1;
844 return -EINVAL;
845 }
846 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
847 return changed;
848}
849
850/*
851 * If the device supports unusual bit rates, does the request meet these?
852 */
853static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
854 struct snd_usb_substream *subs)
855{
856 struct audioformat *fp;
0717d0f5 857 int *rate_list;
e5779998
DM
858 int count = 0, needs_knot = 0;
859 int err;
860
5cd5d7c4
CL
861 kfree(subs->rate_list.list);
862 subs->rate_list.list = NULL;
863
e5779998
DM
864 list_for_each_entry(fp, &subs->fmt_list, list) {
865 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
866 return 0;
867 count += fp->nr_rates;
868 if (fp->rates & SNDRV_PCM_RATE_KNOT)
869 needs_knot = 1;
870 }
871 if (!needs_knot)
872 return 0;
873
0717d0f5
TI
874 subs->rate_list.list = rate_list =
875 kmalloc(sizeof(int) * count, GFP_KERNEL);
8a8d56b2
JJ
876 if (!subs->rate_list.list)
877 return -ENOMEM;
878 subs->rate_list.count = count;
e5779998
DM
879 subs->rate_list.mask = 0;
880 count = 0;
881 list_for_each_entry(fp, &subs->fmt_list, list) {
882 int i;
883 for (i = 0; i < fp->nr_rates; i++)
0717d0f5 884 rate_list[count++] = fp->rate_table[i];
e5779998
DM
885 }
886 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
887 &subs->rate_list);
888 if (err < 0)
889 return err;
890
891 return 0;
892}
893
894
895/*
896 * set up the runtime hardware information.
897 */
898
899static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
900{
901 struct list_head *p;
902 unsigned int pt, ptmin;
903 int param_period_time_if_needed;
904 int err;
905
906 runtime->hw.formats = subs->formats;
907
908 runtime->hw.rate_min = 0x7fffffff;
909 runtime->hw.rate_max = 0;
910 runtime->hw.channels_min = 256;
911 runtime->hw.channels_max = 0;
912 runtime->hw.rates = 0;
913 ptmin = UINT_MAX;
914 /* check min/max rates and channels */
915 list_for_each(p, &subs->fmt_list) {
916 struct audioformat *fp;
917 fp = list_entry(p, struct audioformat, list);
918 runtime->hw.rates |= fp->rates;
919 if (runtime->hw.rate_min > fp->rate_min)
920 runtime->hw.rate_min = fp->rate_min;
921 if (runtime->hw.rate_max < fp->rate_max)
922 runtime->hw.rate_max = fp->rate_max;
923 if (runtime->hw.channels_min > fp->channels)
924 runtime->hw.channels_min = fp->channels;
925 if (runtime->hw.channels_max < fp->channels)
926 runtime->hw.channels_max = fp->channels;
927 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
928 /* FIXME: there might be more than one audio formats... */
929 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
930 fp->frame_size;
931 }
932 pt = 125 * (1 << fp->datainterval);
933 ptmin = min(ptmin, pt);
934 }
88a8516a
ON
935 err = snd_usb_autoresume(subs->stream->chip);
936 if (err < 0)
937 return err;
e5779998
DM
938
939 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
978520b7 940 if (subs->speed == USB_SPEED_FULL)
e5779998
DM
941 /* full speed devices have fixed data packet interval */
942 ptmin = 1000;
943 if (ptmin == 1000)
944 /* if period time doesn't go below 1 ms, no rules needed */
945 param_period_time_if_needed = -1;
946 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
947 ptmin, UINT_MAX);
948
949 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
950 hw_rule_rate, subs,
951 SNDRV_PCM_HW_PARAM_FORMAT,
952 SNDRV_PCM_HW_PARAM_CHANNELS,
953 param_period_time_if_needed,
954 -1)) < 0)
88a8516a 955 goto rep_err;
e5779998
DM
956 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
957 hw_rule_channels, subs,
958 SNDRV_PCM_HW_PARAM_FORMAT,
959 SNDRV_PCM_HW_PARAM_RATE,
960 param_period_time_if_needed,
961 -1)) < 0)
88a8516a 962 goto rep_err;
e5779998
DM
963 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
964 hw_rule_format, subs,
965 SNDRV_PCM_HW_PARAM_RATE,
966 SNDRV_PCM_HW_PARAM_CHANNELS,
967 param_period_time_if_needed,
968 -1)) < 0)
88a8516a 969 goto rep_err;
e5779998
DM
970 if (param_period_time_if_needed >= 0) {
971 err = snd_pcm_hw_rule_add(runtime, 0,
972 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
973 hw_rule_period_time, subs,
974 SNDRV_PCM_HW_PARAM_FORMAT,
975 SNDRV_PCM_HW_PARAM_CHANNELS,
976 SNDRV_PCM_HW_PARAM_RATE,
977 -1);
978 if (err < 0)
88a8516a 979 goto rep_err;
e5779998
DM
980 }
981 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
88a8516a 982 goto rep_err;
e5779998 983 return 0;
88a8516a
ON
984
985rep_err:
986 snd_usb_autosuspend(subs->stream->chip);
987 return err;
e5779998
DM
988}
989
990static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
991{
992 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
993 struct snd_pcm_runtime *runtime = substream->runtime;
994 struct snd_usb_substream *subs = &as->substream[direction];
995
996 subs->interface = -1;
e11b4e0e 997 subs->altset_idx = 0;
e5779998
DM
998 runtime->hw = snd_usb_hardware;
999 runtime->private_data = subs;
1000 subs->pcm_substream = substream;
88a8516a 1001 /* runtime PM is also done there */
e5779998
DM
1002 return setup_hw_info(runtime, subs);
1003}
1004
1005static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1006{
1007 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1008 struct snd_usb_substream *subs = &as->substream[direction];
1009
a9bb3626 1010 stop_endpoints(subs, false);
68e67f40
DM
1011
1012 if (!as->chip->shutdown && subs->interface >= 0) {
1013 usb_set_interface(subs->dev, subs->interface, 0);
1014 subs->interface = -1;
1015 }
1016
e5779998 1017 subs->pcm_substream = NULL;
88a8516a 1018 snd_usb_autosuspend(subs->stream->chip);
edcd3633 1019
68e67f40 1020 return 0;
edcd3633
DM
1021}
1022
1023/* Since a URB can handle only a single linear buffer, we must use double
1024 * buffering when the data to be transferred overflows the buffer boundary.
1025 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1026 * for all URBs.
1027 */
1028static void retire_capture_urb(struct snd_usb_substream *subs,
1029 struct urb *urb)
1030{
1031 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1032 unsigned int stride, frames, bytes, oldptr;
1033 int i, period_elapsed = 0;
1034 unsigned long flags;
1035 unsigned char *cp;
1036
1037 stride = runtime->frame_bits >> 3;
1038
1039 for (i = 0; i < urb->number_of_packets; i++) {
1040 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1041 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1042 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1043 // continue;
1044 }
1045 bytes = urb->iso_frame_desc[i].actual_length;
1046 frames = bytes / stride;
1047 if (!subs->txfr_quirk)
1048 bytes = frames * stride;
1049 if (bytes % (runtime->sample_bits >> 3) != 0) {
1050#ifdef CONFIG_SND_DEBUG_VERBOSE
1051 int oldbytes = bytes;
1052#endif
1053 bytes = frames * stride;
1054 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1055 oldbytes, bytes);
1056 }
1057 /* update the current pointer */
1058 spin_lock_irqsave(&subs->lock, flags);
1059 oldptr = subs->hwptr_done;
1060 subs->hwptr_done += bytes;
1061 if (subs->hwptr_done >= runtime->buffer_size * stride)
1062 subs->hwptr_done -= runtime->buffer_size * stride;
1063 frames = (bytes + (oldptr % stride)) / stride;
1064 subs->transfer_done += frames;
1065 if (subs->transfer_done >= runtime->period_size) {
1066 subs->transfer_done -= runtime->period_size;
1067 period_elapsed = 1;
1068 }
1069 spin_unlock_irqrestore(&subs->lock, flags);
1070 /* copy a data chunk */
1071 if (oldptr + bytes > runtime->buffer_size * stride) {
1072 unsigned int bytes1 =
1073 runtime->buffer_size * stride - oldptr;
1074 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1075 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1076 } else {
1077 memcpy(runtime->dma_area + oldptr, cp, bytes);
1078 }
1079 }
1080
1081 if (period_elapsed)
1082 snd_pcm_period_elapsed(subs->pcm_substream);
1083}
1084
1085static void prepare_playback_urb(struct snd_usb_substream *subs,
1086 struct urb *urb)
1087{
1088 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
245baf98 1089 struct snd_usb_endpoint *ep = subs->data_endpoint;
edcd3633
DM
1090 struct snd_urb_ctx *ctx = urb->context;
1091 unsigned int counts, frames, bytes;
1092 int i, stride, period_elapsed = 0;
1093 unsigned long flags;
1094
1095 stride = runtime->frame_bits >> 3;
1096
1097 frames = 0;
1098 urb->number_of_packets = 0;
1099 spin_lock_irqsave(&subs->lock, flags);
1100 for (i = 0; i < ctx->packets; i++) {
245baf98
DM
1101 if (ctx->packet_size[i])
1102 counts = ctx->packet_size[i];
1103 else
1104 counts = snd_usb_endpoint_next_packet_size(ep);
1105
edcd3633
DM
1106 /* set up descriptor */
1107 urb->iso_frame_desc[i].offset = frames * stride;
1108 urb->iso_frame_desc[i].length = counts * stride;
1109 frames += counts;
1110 urb->number_of_packets++;
1111 subs->transfer_done += counts;
1112 if (subs->transfer_done >= runtime->period_size) {
1113 subs->transfer_done -= runtime->period_size;
1114 period_elapsed = 1;
1115 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1116 if (subs->transfer_done > 0) {
1117 /* FIXME: fill-max mode is not
1118 * supported yet */
1119 frames -= subs->transfer_done;
1120 counts -= subs->transfer_done;
1121 urb->iso_frame_desc[i].length =
1122 counts * stride;
1123 subs->transfer_done = 0;
1124 }
1125 i++;
1126 if (i < ctx->packets) {
1127 /* add a transfer delimiter */
1128 urb->iso_frame_desc[i].offset =
1129 frames * stride;
1130 urb->iso_frame_desc[i].length = 0;
1131 urb->number_of_packets++;
1132 }
1133 break;
1134 }
1135 }
1136 if (period_elapsed &&
1137 !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1138 break;
1139 }
1140 bytes = frames * stride;
1141 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1142 /* err, the transferred area goes over buffer boundary. */
1143 unsigned int bytes1 =
1144 runtime->buffer_size * stride - subs->hwptr_done;
1145 memcpy(urb->transfer_buffer,
1146 runtime->dma_area + subs->hwptr_done, bytes1);
1147 memcpy(urb->transfer_buffer + bytes1,
1148 runtime->dma_area, bytes - bytes1);
1149 } else {
1150 memcpy(urb->transfer_buffer,
1151 runtime->dma_area + subs->hwptr_done, bytes);
1152 }
1153 subs->hwptr_done += bytes;
1154 if (subs->hwptr_done >= runtime->buffer_size * stride)
1155 subs->hwptr_done -= runtime->buffer_size * stride;
fbcfbf5f
DM
1156
1157 /* update delay with exact number of samples queued */
1158 runtime->delay = subs->last_delay;
edcd3633 1159 runtime->delay += frames;
fbcfbf5f
DM
1160 subs->last_delay = runtime->delay;
1161
1162 /* realign last_frame_number */
1163 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1164 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1165
edcd3633
DM
1166 spin_unlock_irqrestore(&subs->lock, flags);
1167 urb->transfer_buffer_length = bytes;
1168 if (period_elapsed)
1169 snd_pcm_period_elapsed(subs->pcm_substream);
1170}
1171
1172/*
1173 * process after playback data complete
1174 * - decrease the delay count again
1175 */
1176static void retire_playback_urb(struct snd_usb_substream *subs,
1177 struct urb *urb)
1178{
1179 unsigned long flags;
1180 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1181 int stride = runtime->frame_bits >> 3;
1182 int processed = urb->transfer_buffer_length / stride;
fbcfbf5f 1183 int est_delay;
edcd3633 1184
1213a205
TI
1185 /* ignore the delay accounting when procssed=0 is given, i.e.
1186 * silent payloads are procssed before handling the actual data
1187 */
1188 if (!processed)
1189 return;
1190
edcd3633 1191 spin_lock_irqsave(&subs->lock, flags);
fbcfbf5f
DM
1192 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1193 /* update delay with exact number of samples played */
1194 if (processed > subs->last_delay)
1195 subs->last_delay = 0;
edcd3633 1196 else
fbcfbf5f
DM
1197 subs->last_delay -= processed;
1198 runtime->delay = subs->last_delay;
1199
1200 /*
1201 * Report when delay estimate is off by more than 2ms.
1202 * The error should be lower than 2ms since the estimate relies
1203 * on two reads of a counter updated every ms.
1204 */
1205 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1206 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1207 est_delay, subs->last_delay);
1208
edcd3633 1209 spin_unlock_irqrestore(&subs->lock, flags);
e5779998
DM
1210}
1211
1212static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1213{
1214 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1215}
1216
1217static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1218{
1219 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1220}
1221
1222static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1223{
1224 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1225}
1226
1227static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1228{
1229 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1230}
1231
edcd3633
DM
1232static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1233 int cmd)
1234{
1235 struct snd_usb_substream *subs = substream->runtime->private_data;
1236
1237 switch (cmd) {
1238 case SNDRV_PCM_TRIGGER_START:
1239 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1240 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1241 subs->data_endpoint->retire_data_urb = retire_playback_urb;
97f8d3b6 1242 subs->running = 1;
edcd3633
DM
1243 return 0;
1244 case SNDRV_PCM_TRIGGER_STOP:
a9bb3626 1245 stop_endpoints(subs, false);
97f8d3b6 1246 subs->running = 0;
edcd3633
DM
1247 return 0;
1248 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1249 subs->data_endpoint->prepare_data_urb = NULL;
1250 subs->data_endpoint->retire_data_urb = NULL;
97f8d3b6 1251 subs->running = 0;
edcd3633
DM
1252 return 0;
1253 }
1254
1255 return -EINVAL;
1256}
1257
afe25967
DM
1258static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1259 int cmd)
edcd3633
DM
1260{
1261 int err;
1262 struct snd_usb_substream *subs = substream->runtime->private_data;
1263
1264 switch (cmd) {
1265 case SNDRV_PCM_TRIGGER_START:
a9bb3626 1266 err = start_endpoints(subs, false);
edcd3633
DM
1267 if (err < 0)
1268 return err;
1269
1270 subs->data_endpoint->retire_data_urb = retire_capture_urb;
97f8d3b6 1271 subs->running = 1;
edcd3633
DM
1272 return 0;
1273 case SNDRV_PCM_TRIGGER_STOP:
a9bb3626 1274 stop_endpoints(subs, false);
97f8d3b6 1275 subs->running = 0;
edcd3633
DM
1276 return 0;
1277 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1278 subs->data_endpoint->retire_data_urb = NULL;
97f8d3b6 1279 subs->running = 0;
edcd3633
DM
1280 return 0;
1281 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1282 subs->data_endpoint->retire_data_urb = retire_capture_urb;
97f8d3b6 1283 subs->running = 1;
edcd3633
DM
1284 return 0;
1285 }
1286
1287 return -EINVAL;
1288}
1289
e5779998
DM
1290static struct snd_pcm_ops snd_usb_playback_ops = {
1291 .open = snd_usb_playback_open,
1292 .close = snd_usb_playback_close,
1293 .ioctl = snd_pcm_lib_ioctl,
1294 .hw_params = snd_usb_hw_params,
1295 .hw_free = snd_usb_hw_free,
1296 .prepare = snd_usb_pcm_prepare,
1297 .trigger = snd_usb_substream_playback_trigger,
1298 .pointer = snd_usb_pcm_pointer,
1299 .page = snd_pcm_lib_get_vmalloc_page,
1300 .mmap = snd_pcm_lib_mmap_vmalloc,
1301};
1302
1303static struct snd_pcm_ops snd_usb_capture_ops = {
1304 .open = snd_usb_capture_open,
1305 .close = snd_usb_capture_close,
1306 .ioctl = snd_pcm_lib_ioctl,
1307 .hw_params = snd_usb_hw_params,
1308 .hw_free = snd_usb_hw_free,
1309 .prepare = snd_usb_pcm_prepare,
1310 .trigger = snd_usb_substream_capture_trigger,
1311 .pointer = snd_usb_pcm_pointer,
1312 .page = snd_pcm_lib_get_vmalloc_page,
1313 .mmap = snd_pcm_lib_mmap_vmalloc,
1314};
1315
1316void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1317{
1318 snd_pcm_set_ops(pcm, stream,
1319 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1320 &snd_usb_playback_ops : &snd_usb_capture_ops);
1321}
This page took 0.193818 seconds and 5 git commands to generate.