ALSA: snd-usb: remove old streaming logic
[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;
304 int err;
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;
329 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
330 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
331 altsd->bNumEndpoints >= 2) {
edcd3633
DM
332 switch (subs->stream->chip->usb_id) {
333 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
334 case USB_ID(0x0763, 0x2081):
335 ep = 0x81;
336 iface = usb_ifnum_to_if(dev, 2);
337 alts = &iface->altsetting[1];
338 goto add_sync_ep;
339 }
340
e5779998
DM
341 /* check sync-pipe endpoint */
342 /* ... and check descriptor size before accessing bSynchAddress
343 because there is a version of the SB Audigy 2 NX firmware lacking
344 the audio fields in the endpoint descriptors */
345 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
346 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
347 get_endpoint(alts, 1)->bSynchAddress != 0)) {
348 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
349 dev->devnum, fmt->iface, fmt->altsetting);
350 return -EINVAL;
351 }
352 ep = get_endpoint(alts, 1)->bEndpointAddress;
353 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
354 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
355 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
356 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
357 dev->devnum, fmt->iface, fmt->altsetting);
358 return -EINVAL;
359 }
edcd3633
DM
360add_sync_ep:
361 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
362 alts, ep, !subs->direction,
363 SND_USB_ENDPOINT_TYPE_SYNC);
364
365 if (!subs->sync_endpoint)
366 return -EINVAL;
367
368 subs->data_endpoint->sync_master = subs->sync_endpoint;
369 }
e5779998 370
767d75ad 371 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
e5779998
DM
372 return err;
373
374 subs->cur_audiofmt = fmt;
375
376 snd_usb_set_format_quirk(subs, fmt);
377
378#if 0
379 printk(KERN_DEBUG
380 "setting done: format = %d, rate = %d..%d, channels = %d\n",
381 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
382 printk(KERN_DEBUG
383 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
384 subs->datapipe, subs->syncpipe);
385#endif
386
387 return 0;
388}
389
390/*
391 * hw_params callback
392 *
393 * allocate a buffer and set the given audio format.
394 *
395 * so far we use a physically linear buffer although packetize transfer
396 * doesn't need a continuous area.
397 * if sg buffer is supported on the later version of alsa, we'll follow
398 * that.
399 */
400static int snd_usb_hw_params(struct snd_pcm_substream *substream,
401 struct snd_pcm_hw_params *hw_params)
402{
403 struct snd_usb_substream *subs = substream->runtime->private_data;
404 struct audioformat *fmt;
405 unsigned int channels, rate, format;
406 int ret, changed;
407
408 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
409 params_buffer_bytes(hw_params));
410 if (ret < 0)
411 return ret;
412
413 format = params_format(hw_params);
414 rate = params_rate(hw_params);
415 channels = params_channels(hw_params);
416 fmt = find_format(subs, format, rate, channels);
417 if (!fmt) {
418 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
419 format, rate, channels);
420 return -EINVAL;
421 }
422
423 changed = subs->cur_audiofmt != fmt ||
424 subs->period_bytes != params_period_bytes(hw_params) ||
425 subs->cur_rate != rate;
426 if ((ret = set_format(subs, fmt)) < 0)
427 return ret;
428
429 if (subs->cur_rate != rate) {
430 struct usb_host_interface *alts;
431 struct usb_interface *iface;
432 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
433 alts = &iface->altsetting[fmt->altset_idx];
767d75ad 434 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
e5779998
DM
435 if (ret < 0)
436 return ret;
437 subs->cur_rate = rate;
438 }
439
440 if (changed) {
382225e6 441 mutex_lock(&subs->stream->chip->shutdown_mutex);
e5779998 442 /* format changed */
edcd3633
DM
443 stop_endpoints(subs, 0, 0, 0);
444 deactivate_endpoints(subs);
445
446 ret = activate_endpoints(subs);
447 if (ret < 0)
448 goto unlock;
449
450 ret = snd_usb_endpoint_set_params(subs->data_endpoint, hw_params, fmt,
451 subs->sync_endpoint);
452 if (ret < 0)
453 goto unlock;
454
455 if (subs->sync_endpoint)
456 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
457 hw_params, fmt, NULL);
458unlock:
382225e6 459 mutex_unlock(&subs->stream->chip->shutdown_mutex);
e5779998
DM
460 }
461
462 return ret;
463}
464
465/*
466 * hw_free callback
467 *
468 * reset the audio format and release the buffer
469 */
470static int snd_usb_hw_free(struct snd_pcm_substream *substream)
471{
472 struct snd_usb_substream *subs = substream->runtime->private_data;
473
474 subs->cur_audiofmt = NULL;
475 subs->cur_rate = 0;
476 subs->period_bytes = 0;
382225e6 477 mutex_lock(&subs->stream->chip->shutdown_mutex);
edcd3633 478 stop_endpoints(subs, 0, 1, 1);
382225e6 479 mutex_unlock(&subs->stream->chip->shutdown_mutex);
e5779998
DM
480 return snd_pcm_lib_free_vmalloc_buffer(substream);
481}
482
483/*
484 * prepare callback
485 *
486 * only a few subtle things...
487 */
488static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
489{
490 struct snd_pcm_runtime *runtime = substream->runtime;
491 struct snd_usb_substream *subs = runtime->private_data;
492
493 if (! subs->cur_audiofmt) {
494 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
495 return -ENXIO;
496 }
497
edcd3633
DM
498 if (snd_BUG_ON(!subs->data_endpoint))
499 return -EIO;
500
e5779998 501 /* some unit conversions in runtime */
edcd3633
DM
502 subs->data_endpoint->maxframesize =
503 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
504 subs->data_endpoint->curframesize =
505 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
e5779998
DM
506
507 /* reset the pointer */
508 subs->hwptr_done = 0;
509 subs->transfer_done = 0;
294c4fb8
PLB
510 subs->last_delay = 0;
511 subs->last_frame_number = 0;
e5779998
DM
512 runtime->delay = 0;
513
edcd3633
DM
514 /* for playback, submit the URBs now; otherwise, the first hwptr_done
515 * updates for all URBs would happen at the same time when starting */
516 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
517 return start_endpoints(subs);
518
519 return 0;
e5779998
DM
520}
521
522static struct snd_pcm_hardware snd_usb_hardware =
523{
524 .info = SNDRV_PCM_INFO_MMAP |
525 SNDRV_PCM_INFO_MMAP_VALID |
526 SNDRV_PCM_INFO_BATCH |
527 SNDRV_PCM_INFO_INTERLEAVED |
528 SNDRV_PCM_INFO_BLOCK_TRANSFER |
529 SNDRV_PCM_INFO_PAUSE,
530 .buffer_bytes_max = 1024 * 1024,
531 .period_bytes_min = 64,
532 .period_bytes_max = 512 * 1024,
533 .periods_min = 2,
534 .periods_max = 1024,
535};
536
537static int hw_check_valid_format(struct snd_usb_substream *subs,
538 struct snd_pcm_hw_params *params,
539 struct audioformat *fp)
540{
541 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
542 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
543 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
544 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
015eb0b0 545 struct snd_mask check_fmts;
e5779998
DM
546 unsigned int ptime;
547
548 /* check the format */
015eb0b0
CL
549 snd_mask_none(&check_fmts);
550 check_fmts.bits[0] = (u32)fp->formats;
551 check_fmts.bits[1] = (u32)(fp->formats >> 32);
552 snd_mask_intersect(&check_fmts, fmts);
553 if (snd_mask_empty(&check_fmts)) {
e5779998
DM
554 hwc_debug(" > check: no supported format %d\n", fp->format);
555 return 0;
556 }
557 /* check the channels */
558 if (fp->channels < ct->min || fp->channels > ct->max) {
559 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
560 return 0;
561 }
562 /* check the rate is within the range */
563 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
564 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
565 return 0;
566 }
567 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
568 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
569 return 0;
570 }
571 /* check whether the period time is >= the data packet interval */
4f4e8f69 572 if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) {
e5779998
DM
573 ptime = 125 * (1 << fp->datainterval);
574 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
575 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
576 return 0;
577 }
578 }
579 return 1;
580}
581
582static int hw_rule_rate(struct snd_pcm_hw_params *params,
583 struct snd_pcm_hw_rule *rule)
584{
585 struct snd_usb_substream *subs = rule->private;
586 struct list_head *p;
587 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
588 unsigned int rmin, rmax;
589 int changed;
590
591 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
592 changed = 0;
593 rmin = rmax = 0;
594 list_for_each(p, &subs->fmt_list) {
595 struct audioformat *fp;
596 fp = list_entry(p, struct audioformat, list);
597 if (!hw_check_valid_format(subs, params, fp))
598 continue;
599 if (changed++) {
600 if (rmin > fp->rate_min)
601 rmin = fp->rate_min;
602 if (rmax < fp->rate_max)
603 rmax = fp->rate_max;
604 } else {
605 rmin = fp->rate_min;
606 rmax = fp->rate_max;
607 }
608 }
609
610 if (!changed) {
611 hwc_debug(" --> get empty\n");
612 it->empty = 1;
613 return -EINVAL;
614 }
615
616 changed = 0;
617 if (it->min < rmin) {
618 it->min = rmin;
619 it->openmin = 0;
620 changed = 1;
621 }
622 if (it->max > rmax) {
623 it->max = rmax;
624 it->openmax = 0;
625 changed = 1;
626 }
627 if (snd_interval_checkempty(it)) {
628 it->empty = 1;
629 return -EINVAL;
630 }
631 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
632 return changed;
633}
634
635
636static int hw_rule_channels(struct snd_pcm_hw_params *params,
637 struct snd_pcm_hw_rule *rule)
638{
639 struct snd_usb_substream *subs = rule->private;
640 struct list_head *p;
641 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
642 unsigned int rmin, rmax;
643 int changed;
644
645 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
646 changed = 0;
647 rmin = rmax = 0;
648 list_for_each(p, &subs->fmt_list) {
649 struct audioformat *fp;
650 fp = list_entry(p, struct audioformat, list);
651 if (!hw_check_valid_format(subs, params, fp))
652 continue;
653 if (changed++) {
654 if (rmin > fp->channels)
655 rmin = fp->channels;
656 if (rmax < fp->channels)
657 rmax = fp->channels;
658 } else {
659 rmin = fp->channels;
660 rmax = fp->channels;
661 }
662 }
663
664 if (!changed) {
665 hwc_debug(" --> get empty\n");
666 it->empty = 1;
667 return -EINVAL;
668 }
669
670 changed = 0;
671 if (it->min < rmin) {
672 it->min = rmin;
673 it->openmin = 0;
674 changed = 1;
675 }
676 if (it->max > rmax) {
677 it->max = rmax;
678 it->openmax = 0;
679 changed = 1;
680 }
681 if (snd_interval_checkempty(it)) {
682 it->empty = 1;
683 return -EINVAL;
684 }
685 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
686 return changed;
687}
688
689static int hw_rule_format(struct snd_pcm_hw_params *params,
690 struct snd_pcm_hw_rule *rule)
691{
692 struct snd_usb_substream *subs = rule->private;
693 struct list_head *p;
694 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
695 u64 fbits;
696 u32 oldbits[2];
697 int changed;
698
699 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
700 fbits = 0;
701 list_for_each(p, &subs->fmt_list) {
702 struct audioformat *fp;
703 fp = list_entry(p, struct audioformat, list);
704 if (!hw_check_valid_format(subs, params, fp))
705 continue;
015eb0b0 706 fbits |= fp->formats;
e5779998
DM
707 }
708
709 oldbits[0] = fmt->bits[0];
710 oldbits[1] = fmt->bits[1];
711 fmt->bits[0] &= (u32)fbits;
712 fmt->bits[1] &= (u32)(fbits >> 32);
713 if (!fmt->bits[0] && !fmt->bits[1]) {
714 hwc_debug(" --> get empty\n");
715 return -EINVAL;
716 }
717 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
718 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
719 return changed;
720}
721
722static int hw_rule_period_time(struct snd_pcm_hw_params *params,
723 struct snd_pcm_hw_rule *rule)
724{
725 struct snd_usb_substream *subs = rule->private;
726 struct audioformat *fp;
727 struct snd_interval *it;
728 unsigned char min_datainterval;
729 unsigned int pmin;
730 int changed;
731
732 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
733 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
734 min_datainterval = 0xff;
735 list_for_each_entry(fp, &subs->fmt_list, list) {
736 if (!hw_check_valid_format(subs, params, fp))
737 continue;
738 min_datainterval = min(min_datainterval, fp->datainterval);
739 }
740 if (min_datainterval == 0xff) {
a7ce2e0d 741 hwc_debug(" --> get empty\n");
e5779998
DM
742 it->empty = 1;
743 return -EINVAL;
744 }
745 pmin = 125 * (1 << min_datainterval);
746 changed = 0;
747 if (it->min < pmin) {
748 it->min = pmin;
749 it->openmin = 0;
750 changed = 1;
751 }
752 if (snd_interval_checkempty(it)) {
753 it->empty = 1;
754 return -EINVAL;
755 }
756 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
757 return changed;
758}
759
760/*
761 * If the device supports unusual bit rates, does the request meet these?
762 */
763static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
764 struct snd_usb_substream *subs)
765{
766 struct audioformat *fp;
0717d0f5 767 int *rate_list;
e5779998
DM
768 int count = 0, needs_knot = 0;
769 int err;
770
771 list_for_each_entry(fp, &subs->fmt_list, list) {
772 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
773 return 0;
774 count += fp->nr_rates;
775 if (fp->rates & SNDRV_PCM_RATE_KNOT)
776 needs_knot = 1;
777 }
778 if (!needs_knot)
779 return 0;
780
0717d0f5
TI
781 subs->rate_list.list = rate_list =
782 kmalloc(sizeof(int) * count, GFP_KERNEL);
8a8d56b2
JJ
783 if (!subs->rate_list.list)
784 return -ENOMEM;
785 subs->rate_list.count = count;
e5779998
DM
786 subs->rate_list.mask = 0;
787 count = 0;
788 list_for_each_entry(fp, &subs->fmt_list, list) {
789 int i;
790 for (i = 0; i < fp->nr_rates; i++)
0717d0f5 791 rate_list[count++] = fp->rate_table[i];
e5779998
DM
792 }
793 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
794 &subs->rate_list);
795 if (err < 0)
796 return err;
797
798 return 0;
799}
800
801
802/*
803 * set up the runtime hardware information.
804 */
805
806static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
807{
808 struct list_head *p;
809 unsigned int pt, ptmin;
810 int param_period_time_if_needed;
811 int err;
812
813 runtime->hw.formats = subs->formats;
814
815 runtime->hw.rate_min = 0x7fffffff;
816 runtime->hw.rate_max = 0;
817 runtime->hw.channels_min = 256;
818 runtime->hw.channels_max = 0;
819 runtime->hw.rates = 0;
820 ptmin = UINT_MAX;
821 /* check min/max rates and channels */
822 list_for_each(p, &subs->fmt_list) {
823 struct audioformat *fp;
824 fp = list_entry(p, struct audioformat, list);
825 runtime->hw.rates |= fp->rates;
826 if (runtime->hw.rate_min > fp->rate_min)
827 runtime->hw.rate_min = fp->rate_min;
828 if (runtime->hw.rate_max < fp->rate_max)
829 runtime->hw.rate_max = fp->rate_max;
830 if (runtime->hw.channels_min > fp->channels)
831 runtime->hw.channels_min = fp->channels;
832 if (runtime->hw.channels_max < fp->channels)
833 runtime->hw.channels_max = fp->channels;
834 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
835 /* FIXME: there might be more than one audio formats... */
836 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
837 fp->frame_size;
838 }
839 pt = 125 * (1 << fp->datainterval);
840 ptmin = min(ptmin, pt);
841 }
88a8516a
ON
842 err = snd_usb_autoresume(subs->stream->chip);
843 if (err < 0)
844 return err;
e5779998
DM
845
846 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
4f4e8f69 847 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
e5779998
DM
848 /* full speed devices have fixed data packet interval */
849 ptmin = 1000;
850 if (ptmin == 1000)
851 /* if period time doesn't go below 1 ms, no rules needed */
852 param_period_time_if_needed = -1;
853 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
854 ptmin, UINT_MAX);
855
856 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
857 hw_rule_rate, subs,
858 SNDRV_PCM_HW_PARAM_FORMAT,
859 SNDRV_PCM_HW_PARAM_CHANNELS,
860 param_period_time_if_needed,
861 -1)) < 0)
88a8516a 862 goto rep_err;
e5779998
DM
863 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
864 hw_rule_channels, subs,
865 SNDRV_PCM_HW_PARAM_FORMAT,
866 SNDRV_PCM_HW_PARAM_RATE,
867 param_period_time_if_needed,
868 -1)) < 0)
88a8516a 869 goto rep_err;
e5779998
DM
870 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
871 hw_rule_format, subs,
872 SNDRV_PCM_HW_PARAM_RATE,
873 SNDRV_PCM_HW_PARAM_CHANNELS,
874 param_period_time_if_needed,
875 -1)) < 0)
88a8516a 876 goto rep_err;
e5779998
DM
877 if (param_period_time_if_needed >= 0) {
878 err = snd_pcm_hw_rule_add(runtime, 0,
879 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
880 hw_rule_period_time, subs,
881 SNDRV_PCM_HW_PARAM_FORMAT,
882 SNDRV_PCM_HW_PARAM_CHANNELS,
883 SNDRV_PCM_HW_PARAM_RATE,
884 -1);
885 if (err < 0)
88a8516a 886 goto rep_err;
e5779998
DM
887 }
888 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
88a8516a 889 goto rep_err;
e5779998 890 return 0;
88a8516a
ON
891
892rep_err:
893 snd_usb_autosuspend(subs->stream->chip);
894 return err;
e5779998
DM
895}
896
897static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
898{
899 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
900 struct snd_pcm_runtime *runtime = substream->runtime;
901 struct snd_usb_substream *subs = &as->substream[direction];
902
903 subs->interface = -1;
e11b4e0e 904 subs->altset_idx = 0;
e5779998
DM
905 runtime->hw = snd_usb_hardware;
906 runtime->private_data = subs;
907 subs->pcm_substream = substream;
88a8516a 908 /* runtime PM is also done there */
e5779998
DM
909 return setup_hw_info(runtime, subs);
910}
911
912static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
913{
edcd3633 914 int ret;
e5779998
DM
915 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
916 struct snd_usb_substream *subs = &as->substream[direction];
917
edcd3633
DM
918 stop_endpoints(subs, 0, 0, 0);
919 ret = deactivate_endpoints(subs);
e5779998 920 subs->pcm_substream = NULL;
88a8516a 921 snd_usb_autosuspend(subs->stream->chip);
edcd3633
DM
922
923 return ret;
924}
925
926/* Since a URB can handle only a single linear buffer, we must use double
927 * buffering when the data to be transferred overflows the buffer boundary.
928 * To avoid inconsistencies when updating hwptr_done, we use double buffering
929 * for all URBs.
930 */
931static void retire_capture_urb(struct snd_usb_substream *subs,
932 struct urb *urb)
933{
934 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
935 unsigned int stride, frames, bytes, oldptr;
936 int i, period_elapsed = 0;
937 unsigned long flags;
938 unsigned char *cp;
939
940 stride = runtime->frame_bits >> 3;
941
942 for (i = 0; i < urb->number_of_packets; i++) {
943 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
944 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
945 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
946 // continue;
947 }
948 bytes = urb->iso_frame_desc[i].actual_length;
949 frames = bytes / stride;
950 if (!subs->txfr_quirk)
951 bytes = frames * stride;
952 if (bytes % (runtime->sample_bits >> 3) != 0) {
953#ifdef CONFIG_SND_DEBUG_VERBOSE
954 int oldbytes = bytes;
955#endif
956 bytes = frames * stride;
957 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
958 oldbytes, bytes);
959 }
960 /* update the current pointer */
961 spin_lock_irqsave(&subs->lock, flags);
962 oldptr = subs->hwptr_done;
963 subs->hwptr_done += bytes;
964 if (subs->hwptr_done >= runtime->buffer_size * stride)
965 subs->hwptr_done -= runtime->buffer_size * stride;
966 frames = (bytes + (oldptr % stride)) / stride;
967 subs->transfer_done += frames;
968 if (subs->transfer_done >= runtime->period_size) {
969 subs->transfer_done -= runtime->period_size;
970 period_elapsed = 1;
971 }
972 spin_unlock_irqrestore(&subs->lock, flags);
973 /* copy a data chunk */
974 if (oldptr + bytes > runtime->buffer_size * stride) {
975 unsigned int bytes1 =
976 runtime->buffer_size * stride - oldptr;
977 memcpy(runtime->dma_area + oldptr, cp, bytes1);
978 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
979 } else {
980 memcpy(runtime->dma_area + oldptr, cp, bytes);
981 }
982 }
983
984 if (period_elapsed)
985 snd_pcm_period_elapsed(subs->pcm_substream);
986}
987
988static void prepare_playback_urb(struct snd_usb_substream *subs,
989 struct urb *urb)
990{
991 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
992 struct snd_urb_ctx *ctx = urb->context;
993 unsigned int counts, frames, bytes;
994 int i, stride, period_elapsed = 0;
995 unsigned long flags;
996
997 stride = runtime->frame_bits >> 3;
998
999 frames = 0;
1000 urb->number_of_packets = 0;
1001 spin_lock_irqsave(&subs->lock, flags);
1002 for (i = 0; i < ctx->packets; i++) {
1003 counts = ctx->packet_size[i];
1004 /* set up descriptor */
1005 urb->iso_frame_desc[i].offset = frames * stride;
1006 urb->iso_frame_desc[i].length = counts * stride;
1007 frames += counts;
1008 urb->number_of_packets++;
1009 subs->transfer_done += counts;
1010 if (subs->transfer_done >= runtime->period_size) {
1011 subs->transfer_done -= runtime->period_size;
1012 period_elapsed = 1;
1013 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1014 if (subs->transfer_done > 0) {
1015 /* FIXME: fill-max mode is not
1016 * supported yet */
1017 frames -= subs->transfer_done;
1018 counts -= subs->transfer_done;
1019 urb->iso_frame_desc[i].length =
1020 counts * stride;
1021 subs->transfer_done = 0;
1022 }
1023 i++;
1024 if (i < ctx->packets) {
1025 /* add a transfer delimiter */
1026 urb->iso_frame_desc[i].offset =
1027 frames * stride;
1028 urb->iso_frame_desc[i].length = 0;
1029 urb->number_of_packets++;
1030 }
1031 break;
1032 }
1033 }
1034 if (period_elapsed &&
1035 !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1036 break;
1037 }
1038 bytes = frames * stride;
1039 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1040 /* err, the transferred area goes over buffer boundary. */
1041 unsigned int bytes1 =
1042 runtime->buffer_size * stride - subs->hwptr_done;
1043 memcpy(urb->transfer_buffer,
1044 runtime->dma_area + subs->hwptr_done, bytes1);
1045 memcpy(urb->transfer_buffer + bytes1,
1046 runtime->dma_area, bytes - bytes1);
1047 } else {
1048 memcpy(urb->transfer_buffer,
1049 runtime->dma_area + subs->hwptr_done, bytes);
1050 }
1051 subs->hwptr_done += bytes;
1052 if (subs->hwptr_done >= runtime->buffer_size * stride)
1053 subs->hwptr_done -= runtime->buffer_size * stride;
1054 runtime->delay += frames;
1055 spin_unlock_irqrestore(&subs->lock, flags);
1056 urb->transfer_buffer_length = bytes;
1057 if (period_elapsed)
1058 snd_pcm_period_elapsed(subs->pcm_substream);
1059}
1060
1061/*
1062 * process after playback data complete
1063 * - decrease the delay count again
1064 */
1065static void retire_playback_urb(struct snd_usb_substream *subs,
1066 struct urb *urb)
1067{
1068 unsigned long flags;
1069 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1070 int stride = runtime->frame_bits >> 3;
1071 int processed = urb->transfer_buffer_length / stride;
1072
1073 spin_lock_irqsave(&subs->lock, flags);
1074 if (processed > runtime->delay)
1075 runtime->delay = 0;
1076 else
1077 runtime->delay -= processed;
1078 spin_unlock_irqrestore(&subs->lock, flags);
e5779998
DM
1079}
1080
1081static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1082{
1083 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1084}
1085
1086static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1087{
1088 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1089}
1090
1091static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1092{
1093 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1094}
1095
1096static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1097{
1098 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1099}
1100
edcd3633
DM
1101static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1102 int cmd)
1103{
1104 struct snd_usb_substream *subs = substream->runtime->private_data;
1105
1106 switch (cmd) {
1107 case SNDRV_PCM_TRIGGER_START:
1108 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1109 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1110 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1111 return 0;
1112 case SNDRV_PCM_TRIGGER_STOP:
1113 stop_endpoints(subs, 0, 0, 0);
1114 return 0;
1115 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1116 subs->data_endpoint->prepare_data_urb = NULL;
1117 subs->data_endpoint->retire_data_urb = NULL;
1118 return 0;
1119 }
1120
1121 return -EINVAL;
1122}
1123
1124int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)
1125{
1126 int err;
1127 struct snd_usb_substream *subs = substream->runtime->private_data;
1128
1129 switch (cmd) {
1130 case SNDRV_PCM_TRIGGER_START:
1131 err = start_endpoints(subs);
1132 if (err < 0)
1133 return err;
1134
1135 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1136 return 0;
1137 case SNDRV_PCM_TRIGGER_STOP:
1138 stop_endpoints(subs, 0, 0, 0);
1139 return 0;
1140 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1141 subs->data_endpoint->retire_data_urb = NULL;
1142 return 0;
1143 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1144 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1145 return 0;
1146 }
1147
1148 return -EINVAL;
1149}
1150
e5779998
DM
1151static struct snd_pcm_ops snd_usb_playback_ops = {
1152 .open = snd_usb_playback_open,
1153 .close = snd_usb_playback_close,
1154 .ioctl = snd_pcm_lib_ioctl,
1155 .hw_params = snd_usb_hw_params,
1156 .hw_free = snd_usb_hw_free,
1157 .prepare = snd_usb_pcm_prepare,
1158 .trigger = snd_usb_substream_playback_trigger,
1159 .pointer = snd_usb_pcm_pointer,
1160 .page = snd_pcm_lib_get_vmalloc_page,
1161 .mmap = snd_pcm_lib_mmap_vmalloc,
1162};
1163
1164static struct snd_pcm_ops snd_usb_capture_ops = {
1165 .open = snd_usb_capture_open,
1166 .close = snd_usb_capture_close,
1167 .ioctl = snd_pcm_lib_ioctl,
1168 .hw_params = snd_usb_hw_params,
1169 .hw_free = snd_usb_hw_free,
1170 .prepare = snd_usb_pcm_prepare,
1171 .trigger = snd_usb_substream_capture_trigger,
1172 .pointer = snd_usb_pcm_pointer,
1173 .page = snd_pcm_lib_get_vmalloc_page,
1174 .mmap = snd_pcm_lib_mmap_vmalloc,
1175};
1176
1177void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1178{
1179 snd_pcm_set_ops(pcm, stream,
1180 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1181 &snd_usb_playback_ops : &snd_usb_capture_ops);
1182}
This page took 0.132642 seconds and 5 git commands to generate.