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