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