]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - sound/usb/pcm.c
ALSA: usb-mixer: factor out quirks
[mirror_ubuntu-bionic-kernel.git] / sound / usb / pcm.c
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/usb.h>
19 #include <linux/usb/audio.h>
20 #include <linux/usb/audio-v2.h>
21
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25
26 #include "usbaudio.h"
27 #include "card.h"
28 #include "quirks.h"
29 #include "debug.h"
30 #include "urb.h"
31 #include "helper.h"
32 #include "pcm.h"
33
34 /*
35 * return the current pcm pointer. just based on the hwptr_done value.
36 */
37 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
38 {
39 struct snd_usb_substream *subs;
40 unsigned int hwptr_done;
41
42 subs = (struct snd_usb_substream *)substream->runtime->private_data;
43 spin_lock(&subs->lock);
44 hwptr_done = subs->hwptr_done;
45 spin_unlock(&subs->lock);
46 return hwptr_done / (substream->runtime->frame_bits >> 3);
47 }
48
49 /*
50 * find a matching audio format
51 */
52 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
53 unsigned int rate, unsigned int channels)
54 {
55 struct list_head *p;
56 struct audioformat *found = NULL;
57 int cur_attr = 0, attr;
58
59 list_for_each(p, &subs->fmt_list) {
60 struct audioformat *fp;
61 fp = list_entry(p, struct audioformat, list);
62 if (!(fp->formats & (1uLL << format)))
63 continue;
64 if (fp->channels != channels)
65 continue;
66 if (rate < fp->rate_min || rate > fp->rate_max)
67 continue;
68 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
69 unsigned int i;
70 for (i = 0; i < fp->nr_rates; i++)
71 if (fp->rate_table[i] == rate)
72 break;
73 if (i >= fp->nr_rates)
74 continue;
75 }
76 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
77 if (! found) {
78 found = fp;
79 cur_attr = attr;
80 continue;
81 }
82 /* avoid async out and adaptive in if the other method
83 * supports the same format.
84 * this is a workaround for the case like
85 * M-audio audiophile USB.
86 */
87 if (attr != cur_attr) {
88 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
89 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
90 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
91 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
92 continue;
93 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
94 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
95 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
96 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
97 found = fp;
98 cur_attr = attr;
99 continue;
100 }
101 }
102 /* find the format with the largest max. packet size */
103 if (fp->maxpacksize > found->maxpacksize) {
104 found = fp;
105 cur_attr = attr;
106 }
107 }
108 return found;
109 }
110
111 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
112 struct usb_host_interface *alts,
113 struct audioformat *fmt)
114 {
115 struct usb_device *dev = chip->dev;
116 unsigned int ep;
117 unsigned char data[1];
118 int err;
119
120 ep = get_endpoint(alts, 0)->bEndpointAddress;
121
122 /* if endpoint doesn't have pitch control, bail out */
123 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
124 return 0;
125
126 data[0] = 1;
127 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
128 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
129 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
130 data, sizeof(data), 1000)) < 0) {
131 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
132 dev->devnum, iface, ep);
133 return err;
134 }
135
136 return 0;
137 }
138
139 /*
140 * initialize the picth control and sample rate
141 */
142 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
143 struct usb_host_interface *alts,
144 struct audioformat *fmt)
145 {
146 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
147
148 switch (altsd->bInterfaceProtocol) {
149 case UAC_VERSION_1:
150 return init_pitch_v1(chip, iface, alts, fmt);
151
152 case UAC_VERSION_2:
153 /* not implemented yet */
154 return 0;
155 }
156
157 return -EINVAL;
158 }
159
160 static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
161 struct usb_host_interface *alts,
162 struct audioformat *fmt, int rate)
163 {
164 struct usb_device *dev = chip->dev;
165 unsigned int ep;
166 unsigned char data[3];
167 int err, crate;
168
169 ep = get_endpoint(alts, 0)->bEndpointAddress;
170 /* if endpoint doesn't have sampling rate control, bail out */
171 if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE)) {
172 snd_printk(KERN_WARNING "%d:%d:%d: endpoint lacks sample rate attribute bit, cannot set.\n",
173 dev->devnum, iface, fmt->altsetting);
174 return 0;
175 }
176
177 data[0] = rate;
178 data[1] = rate >> 8;
179 data[2] = rate >> 16;
180 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
181 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
182 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
183 data, sizeof(data), 1000)) < 0) {
184 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
185 dev->devnum, iface, fmt->altsetting, rate, ep);
186 return err;
187 }
188 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
189 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
190 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
191 data, sizeof(data), 1000)) < 0) {
192 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
193 dev->devnum, iface, fmt->altsetting, ep);
194 return 0; /* some devices don't support reading */
195 }
196 crate = data[0] | (data[1] << 8) | (data[2] << 16);
197 if (crate != rate) {
198 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
199 // runtime->rate = crate;
200 }
201
202 return 0;
203 }
204
205 static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
206 struct usb_host_interface *alts,
207 struct audioformat *fmt, int rate)
208 {
209 struct usb_device *dev = chip->dev;
210 unsigned char data[4];
211 int err, crate;
212
213 data[0] = rate;
214 data[1] = rate >> 8;
215 data[2] = rate >> 16;
216 data[3] = rate >> 24;
217 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
218 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
219 UAC2_CS_CONTROL_SAM_FREQ << 8, chip->clock_id << 8,
220 data, sizeof(data), 1000)) < 0) {
221 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2)\n",
222 dev->devnum, iface, fmt->altsetting, rate);
223 return err;
224 }
225 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
226 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
227 UAC2_CS_CONTROL_SAM_FREQ << 8, chip->clock_id << 8,
228 data, sizeof(data), 1000)) < 0) {
229 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2)\n",
230 dev->devnum, iface, fmt->altsetting);
231 return err;
232 }
233 crate = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
234 if (crate != rate)
235 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
236
237 return 0;
238 }
239
240 int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
241 struct usb_host_interface *alts,
242 struct audioformat *fmt, int rate)
243 {
244 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
245
246 switch (altsd->bInterfaceProtocol) {
247 case UAC_VERSION_1:
248 return set_sample_rate_v1(chip, iface, alts, fmt, rate);
249
250 case UAC_VERSION_2:
251 return set_sample_rate_v2(chip, iface, alts, fmt, rate);
252 }
253
254 return -EINVAL;
255 }
256
257 /*
258 * find a matching format and set up the interface
259 */
260 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
261 {
262 struct usb_device *dev = subs->dev;
263 struct usb_host_interface *alts;
264 struct usb_interface_descriptor *altsd;
265 struct usb_interface *iface;
266 unsigned int ep, attr;
267 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
268 int err;
269
270 iface = usb_ifnum_to_if(dev, fmt->iface);
271 if (WARN_ON(!iface))
272 return -EINVAL;
273 alts = &iface->altsetting[fmt->altset_idx];
274 altsd = get_iface_desc(alts);
275 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
276 return -EINVAL;
277
278 if (fmt == subs->cur_audiofmt)
279 return 0;
280
281 /* close the old interface */
282 if (subs->interface >= 0 && subs->interface != fmt->iface) {
283 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
284 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
285 dev->devnum, fmt->iface, fmt->altsetting);
286 return -EIO;
287 }
288 subs->interface = -1;
289 subs->altset_idx = 0;
290 }
291
292 /* set interface */
293 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
294 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
295 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
296 dev->devnum, fmt->iface, fmt->altsetting);
297 return -EIO;
298 }
299 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
300 subs->interface = fmt->iface;
301 subs->altset_idx = fmt->altset_idx;
302 }
303
304 /* create a data pipe */
305 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
306 if (is_playback)
307 subs->datapipe = usb_sndisocpipe(dev, ep);
308 else
309 subs->datapipe = usb_rcvisocpipe(dev, ep);
310 subs->datainterval = fmt->datainterval;
311 subs->syncpipe = subs->syncinterval = 0;
312 subs->maxpacksize = fmt->maxpacksize;
313 subs->fill_max = 0;
314
315 /* we need a sync pipe in async OUT or adaptive IN mode */
316 /* check the number of EP, since some devices have broken
317 * descriptors which fool us. if it has only one EP,
318 * assume it as adaptive-out or sync-in.
319 */
320 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
321 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
322 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
323 altsd->bNumEndpoints >= 2) {
324 /* check sync-pipe endpoint */
325 /* ... and check descriptor size before accessing bSynchAddress
326 because there is a version of the SB Audigy 2 NX firmware lacking
327 the audio fields in the endpoint descriptors */
328 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
329 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
330 get_endpoint(alts, 1)->bSynchAddress != 0)) {
331 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
332 dev->devnum, fmt->iface, fmt->altsetting);
333 return -EINVAL;
334 }
335 ep = get_endpoint(alts, 1)->bEndpointAddress;
336 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
337 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
338 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
339 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
340 dev->devnum, fmt->iface, fmt->altsetting);
341 return -EINVAL;
342 }
343 ep &= USB_ENDPOINT_NUMBER_MASK;
344 if (is_playback)
345 subs->syncpipe = usb_rcvisocpipe(dev, ep);
346 else
347 subs->syncpipe = usb_sndisocpipe(dev, ep);
348 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
349 get_endpoint(alts, 1)->bRefresh >= 1 &&
350 get_endpoint(alts, 1)->bRefresh <= 9)
351 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
352 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
353 subs->syncinterval = 1;
354 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
355 get_endpoint(alts, 1)->bInterval <= 16)
356 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
357 else
358 subs->syncinterval = 3;
359 }
360
361 /* always fill max packet size */
362 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
363 subs->fill_max = 1;
364
365 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
366 return err;
367
368 subs->cur_audiofmt = fmt;
369
370 snd_usb_set_format_quirk(subs, fmt);
371
372 #if 0
373 printk(KERN_DEBUG
374 "setting done: format = %d, rate = %d..%d, channels = %d\n",
375 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
376 printk(KERN_DEBUG
377 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
378 subs->datapipe, subs->syncpipe);
379 #endif
380
381 return 0;
382 }
383
384 /*
385 * hw_params callback
386 *
387 * allocate a buffer and set the given audio format.
388 *
389 * so far we use a physically linear buffer although packetize transfer
390 * doesn't need a continuous area.
391 * if sg buffer is supported on the later version of alsa, we'll follow
392 * that.
393 */
394 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
395 struct snd_pcm_hw_params *hw_params)
396 {
397 struct snd_usb_substream *subs = substream->runtime->private_data;
398 struct audioformat *fmt;
399 unsigned int channels, rate, format;
400 int ret, changed;
401
402 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
403 params_buffer_bytes(hw_params));
404 if (ret < 0)
405 return ret;
406
407 format = params_format(hw_params);
408 rate = params_rate(hw_params);
409 channels = params_channels(hw_params);
410 fmt = find_format(subs, format, rate, channels);
411 if (!fmt) {
412 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
413 format, rate, channels);
414 return -EINVAL;
415 }
416
417 changed = subs->cur_audiofmt != fmt ||
418 subs->period_bytes != params_period_bytes(hw_params) ||
419 subs->cur_rate != rate;
420 if ((ret = set_format(subs, fmt)) < 0)
421 return ret;
422
423 if (subs->cur_rate != rate) {
424 struct usb_host_interface *alts;
425 struct usb_interface *iface;
426 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
427 alts = &iface->altsetting[fmt->altset_idx];
428 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
429 if (ret < 0)
430 return ret;
431 subs->cur_rate = rate;
432 }
433
434 if (changed) {
435 /* format changed */
436 snd_usb_release_substream_urbs(subs, 0);
437 /* influenced: period_bytes, channels, rate, format, */
438 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
439 params_rate(hw_params),
440 snd_pcm_format_physical_width(params_format(hw_params)) *
441 params_channels(hw_params));
442 }
443
444 return ret;
445 }
446
447 /*
448 * hw_free callback
449 *
450 * reset the audio format and release the buffer
451 */
452 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
453 {
454 struct snd_usb_substream *subs = substream->runtime->private_data;
455
456 subs->cur_audiofmt = NULL;
457 subs->cur_rate = 0;
458 subs->period_bytes = 0;
459 if (!subs->stream->chip->shutdown)
460 snd_usb_release_substream_urbs(subs, 0);
461 return snd_pcm_lib_free_vmalloc_buffer(substream);
462 }
463
464 /*
465 * prepare callback
466 *
467 * only a few subtle things...
468 */
469 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
470 {
471 struct snd_pcm_runtime *runtime = substream->runtime;
472 struct snd_usb_substream *subs = runtime->private_data;
473
474 if (! subs->cur_audiofmt) {
475 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
476 return -ENXIO;
477 }
478
479 /* some unit conversions in runtime */
480 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
481 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
482
483 /* reset the pointer */
484 subs->hwptr_done = 0;
485 subs->transfer_done = 0;
486 subs->phase = 0;
487 runtime->delay = 0;
488
489 return snd_usb_substream_prepare(subs, runtime);
490 }
491
492 static struct snd_pcm_hardware snd_usb_hardware =
493 {
494 .info = SNDRV_PCM_INFO_MMAP |
495 SNDRV_PCM_INFO_MMAP_VALID |
496 SNDRV_PCM_INFO_BATCH |
497 SNDRV_PCM_INFO_INTERLEAVED |
498 SNDRV_PCM_INFO_BLOCK_TRANSFER |
499 SNDRV_PCM_INFO_PAUSE,
500 .buffer_bytes_max = 1024 * 1024,
501 .period_bytes_min = 64,
502 .period_bytes_max = 512 * 1024,
503 .periods_min = 2,
504 .periods_max = 1024,
505 };
506
507 static int hw_check_valid_format(struct snd_usb_substream *subs,
508 struct snd_pcm_hw_params *params,
509 struct audioformat *fp)
510 {
511 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
512 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
513 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
514 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
515 struct snd_mask check_fmts;
516 unsigned int ptime;
517
518 /* check the format */
519 snd_mask_none(&check_fmts);
520 check_fmts.bits[0] = (u32)fp->formats;
521 check_fmts.bits[1] = (u32)(fp->formats >> 32);
522 snd_mask_intersect(&check_fmts, fmts);
523 if (snd_mask_empty(&check_fmts)) {
524 hwc_debug(" > check: no supported format %d\n", fp->format);
525 return 0;
526 }
527 /* check the channels */
528 if (fp->channels < ct->min || fp->channels > ct->max) {
529 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
530 return 0;
531 }
532 /* check the rate is within the range */
533 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
534 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
535 return 0;
536 }
537 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
538 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
539 return 0;
540 }
541 /* check whether the period time is >= the data packet interval */
542 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
543 ptime = 125 * (1 << fp->datainterval);
544 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
545 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
546 return 0;
547 }
548 }
549 return 1;
550 }
551
552 static int hw_rule_rate(struct snd_pcm_hw_params *params,
553 struct snd_pcm_hw_rule *rule)
554 {
555 struct snd_usb_substream *subs = rule->private;
556 struct list_head *p;
557 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
558 unsigned int rmin, rmax;
559 int changed;
560
561 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
562 changed = 0;
563 rmin = rmax = 0;
564 list_for_each(p, &subs->fmt_list) {
565 struct audioformat *fp;
566 fp = list_entry(p, struct audioformat, list);
567 if (!hw_check_valid_format(subs, params, fp))
568 continue;
569 if (changed++) {
570 if (rmin > fp->rate_min)
571 rmin = fp->rate_min;
572 if (rmax < fp->rate_max)
573 rmax = fp->rate_max;
574 } else {
575 rmin = fp->rate_min;
576 rmax = fp->rate_max;
577 }
578 }
579
580 if (!changed) {
581 hwc_debug(" --> get empty\n");
582 it->empty = 1;
583 return -EINVAL;
584 }
585
586 changed = 0;
587 if (it->min < rmin) {
588 it->min = rmin;
589 it->openmin = 0;
590 changed = 1;
591 }
592 if (it->max > rmax) {
593 it->max = rmax;
594 it->openmax = 0;
595 changed = 1;
596 }
597 if (snd_interval_checkempty(it)) {
598 it->empty = 1;
599 return -EINVAL;
600 }
601 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
602 return changed;
603 }
604
605
606 static int hw_rule_channels(struct snd_pcm_hw_params *params,
607 struct snd_pcm_hw_rule *rule)
608 {
609 struct snd_usb_substream *subs = rule->private;
610 struct list_head *p;
611 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
612 unsigned int rmin, rmax;
613 int changed;
614
615 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
616 changed = 0;
617 rmin = rmax = 0;
618 list_for_each(p, &subs->fmt_list) {
619 struct audioformat *fp;
620 fp = list_entry(p, struct audioformat, list);
621 if (!hw_check_valid_format(subs, params, fp))
622 continue;
623 if (changed++) {
624 if (rmin > fp->channels)
625 rmin = fp->channels;
626 if (rmax < fp->channels)
627 rmax = fp->channels;
628 } else {
629 rmin = fp->channels;
630 rmax = fp->channels;
631 }
632 }
633
634 if (!changed) {
635 hwc_debug(" --> get empty\n");
636 it->empty = 1;
637 return -EINVAL;
638 }
639
640 changed = 0;
641 if (it->min < rmin) {
642 it->min = rmin;
643 it->openmin = 0;
644 changed = 1;
645 }
646 if (it->max > rmax) {
647 it->max = rmax;
648 it->openmax = 0;
649 changed = 1;
650 }
651 if (snd_interval_checkempty(it)) {
652 it->empty = 1;
653 return -EINVAL;
654 }
655 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
656 return changed;
657 }
658
659 static int hw_rule_format(struct snd_pcm_hw_params *params,
660 struct snd_pcm_hw_rule *rule)
661 {
662 struct snd_usb_substream *subs = rule->private;
663 struct list_head *p;
664 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
665 u64 fbits;
666 u32 oldbits[2];
667 int changed;
668
669 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
670 fbits = 0;
671 list_for_each(p, &subs->fmt_list) {
672 struct audioformat *fp;
673 fp = list_entry(p, struct audioformat, list);
674 if (!hw_check_valid_format(subs, params, fp))
675 continue;
676 fbits |= fp->formats;
677 }
678
679 oldbits[0] = fmt->bits[0];
680 oldbits[1] = fmt->bits[1];
681 fmt->bits[0] &= (u32)fbits;
682 fmt->bits[1] &= (u32)(fbits >> 32);
683 if (!fmt->bits[0] && !fmt->bits[1]) {
684 hwc_debug(" --> get empty\n");
685 return -EINVAL;
686 }
687 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
688 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
689 return changed;
690 }
691
692 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
693 struct snd_pcm_hw_rule *rule)
694 {
695 struct snd_usb_substream *subs = rule->private;
696 struct audioformat *fp;
697 struct snd_interval *it;
698 unsigned char min_datainterval;
699 unsigned int pmin;
700 int changed;
701
702 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
703 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
704 min_datainterval = 0xff;
705 list_for_each_entry(fp, &subs->fmt_list, list) {
706 if (!hw_check_valid_format(subs, params, fp))
707 continue;
708 min_datainterval = min(min_datainterval, fp->datainterval);
709 }
710 if (min_datainterval == 0xff) {
711 hwc_debug(" --> get emtpy\n");
712 it->empty = 1;
713 return -EINVAL;
714 }
715 pmin = 125 * (1 << min_datainterval);
716 changed = 0;
717 if (it->min < pmin) {
718 it->min = pmin;
719 it->openmin = 0;
720 changed = 1;
721 }
722 if (snd_interval_checkempty(it)) {
723 it->empty = 1;
724 return -EINVAL;
725 }
726 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
727 return changed;
728 }
729
730 /*
731 * If the device supports unusual bit rates, does the request meet these?
732 */
733 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
734 struct snd_usb_substream *subs)
735 {
736 struct audioformat *fp;
737 int count = 0, needs_knot = 0;
738 int err;
739
740 list_for_each_entry(fp, &subs->fmt_list, list) {
741 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
742 return 0;
743 count += fp->nr_rates;
744 if (fp->rates & SNDRV_PCM_RATE_KNOT)
745 needs_knot = 1;
746 }
747 if (!needs_knot)
748 return 0;
749
750 subs->rate_list.count = count;
751 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
752 subs->rate_list.mask = 0;
753 count = 0;
754 list_for_each_entry(fp, &subs->fmt_list, list) {
755 int i;
756 for (i = 0; i < fp->nr_rates; i++)
757 subs->rate_list.list[count++] = fp->rate_table[i];
758 }
759 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
760 &subs->rate_list);
761 if (err < 0)
762 return err;
763
764 return 0;
765 }
766
767
768 /*
769 * set up the runtime hardware information.
770 */
771
772 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
773 {
774 struct list_head *p;
775 unsigned int pt, ptmin;
776 int param_period_time_if_needed;
777 int err;
778
779 runtime->hw.formats = subs->formats;
780
781 runtime->hw.rate_min = 0x7fffffff;
782 runtime->hw.rate_max = 0;
783 runtime->hw.channels_min = 256;
784 runtime->hw.channels_max = 0;
785 runtime->hw.rates = 0;
786 ptmin = UINT_MAX;
787 /* check min/max rates and channels */
788 list_for_each(p, &subs->fmt_list) {
789 struct audioformat *fp;
790 fp = list_entry(p, struct audioformat, list);
791 runtime->hw.rates |= fp->rates;
792 if (runtime->hw.rate_min > fp->rate_min)
793 runtime->hw.rate_min = fp->rate_min;
794 if (runtime->hw.rate_max < fp->rate_max)
795 runtime->hw.rate_max = fp->rate_max;
796 if (runtime->hw.channels_min > fp->channels)
797 runtime->hw.channels_min = fp->channels;
798 if (runtime->hw.channels_max < fp->channels)
799 runtime->hw.channels_max = fp->channels;
800 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
801 /* FIXME: there might be more than one audio formats... */
802 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
803 fp->frame_size;
804 }
805 pt = 125 * (1 << fp->datainterval);
806 ptmin = min(ptmin, pt);
807 }
808
809 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
810 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
811 /* full speed devices have fixed data packet interval */
812 ptmin = 1000;
813 if (ptmin == 1000)
814 /* if period time doesn't go below 1 ms, no rules needed */
815 param_period_time_if_needed = -1;
816 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
817 ptmin, UINT_MAX);
818
819 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
820 hw_rule_rate, subs,
821 SNDRV_PCM_HW_PARAM_FORMAT,
822 SNDRV_PCM_HW_PARAM_CHANNELS,
823 param_period_time_if_needed,
824 -1)) < 0)
825 return err;
826 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
827 hw_rule_channels, subs,
828 SNDRV_PCM_HW_PARAM_FORMAT,
829 SNDRV_PCM_HW_PARAM_RATE,
830 param_period_time_if_needed,
831 -1)) < 0)
832 return err;
833 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
834 hw_rule_format, subs,
835 SNDRV_PCM_HW_PARAM_RATE,
836 SNDRV_PCM_HW_PARAM_CHANNELS,
837 param_period_time_if_needed,
838 -1)) < 0)
839 return err;
840 if (param_period_time_if_needed >= 0) {
841 err = snd_pcm_hw_rule_add(runtime, 0,
842 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
843 hw_rule_period_time, subs,
844 SNDRV_PCM_HW_PARAM_FORMAT,
845 SNDRV_PCM_HW_PARAM_CHANNELS,
846 SNDRV_PCM_HW_PARAM_RATE,
847 -1);
848 if (err < 0)
849 return err;
850 }
851 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
852 return err;
853 return 0;
854 }
855
856 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
857 {
858 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
859 struct snd_pcm_runtime *runtime = substream->runtime;
860 struct snd_usb_substream *subs = &as->substream[direction];
861
862 subs->interface = -1;
863 subs->altset_idx = 0;
864 runtime->hw = snd_usb_hardware;
865 runtime->private_data = subs;
866 subs->pcm_substream = substream;
867 return setup_hw_info(runtime, subs);
868 }
869
870 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
871 {
872 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
873 struct snd_usb_substream *subs = &as->substream[direction];
874
875 if (!as->chip->shutdown && subs->interface >= 0) {
876 usb_set_interface(subs->dev, subs->interface, 0);
877 subs->interface = -1;
878 }
879 subs->pcm_substream = NULL;
880 return 0;
881 }
882
883 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
884 {
885 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
886 }
887
888 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
889 {
890 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
891 }
892
893 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
894 {
895 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
896 }
897
898 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
899 {
900 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
901 }
902
903 static struct snd_pcm_ops snd_usb_playback_ops = {
904 .open = snd_usb_playback_open,
905 .close = snd_usb_playback_close,
906 .ioctl = snd_pcm_lib_ioctl,
907 .hw_params = snd_usb_hw_params,
908 .hw_free = snd_usb_hw_free,
909 .prepare = snd_usb_pcm_prepare,
910 .trigger = snd_usb_substream_playback_trigger,
911 .pointer = snd_usb_pcm_pointer,
912 .page = snd_pcm_lib_get_vmalloc_page,
913 .mmap = snd_pcm_lib_mmap_vmalloc,
914 };
915
916 static struct snd_pcm_ops snd_usb_capture_ops = {
917 .open = snd_usb_capture_open,
918 .close = snd_usb_capture_close,
919 .ioctl = snd_pcm_lib_ioctl,
920 .hw_params = snd_usb_hw_params,
921 .hw_free = snd_usb_hw_free,
922 .prepare = snd_usb_pcm_prepare,
923 .trigger = snd_usb_substream_capture_trigger,
924 .pointer = snd_usb_pcm_pointer,
925 .page = snd_pcm_lib_get_vmalloc_page,
926 .mmap = snd_pcm_lib_mmap_vmalloc,
927 };
928
929 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
930 {
931 snd_pcm_set_ops(pcm, stream,
932 stream == SNDRV_PCM_STREAM_PLAYBACK ?
933 &snd_usb_playback_ops : &snd_usb_capture_ops);
934 }