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