• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/bitrev.h>
20 #include <linux/ratelimit.h>
21 #include <linux/usb.h>
22 #include <linux/usb/audio.h>
23 #include <linux/usb/audio-v2.h>
24 
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 
29 #include "usbaudio.h"
30 #include "card.h"
31 #include "quirks.h"
32 #include "debug.h"
33 #include "endpoint.h"
34 #include "helper.h"
35 #include "pcm.h"
36 #include "clock.h"
37 #include "power.h"
38 
39 #define SUBSTREAM_FLAG_DATA_EP_STARTED	0
40 #define SUBSTREAM_FLAG_SYNC_EP_STARTED	1
41 
42 /* return the estimated delay based on USB frame counters */
snd_usb_pcm_delay(struct snd_usb_substream * subs,unsigned int rate)43 snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
44 				    unsigned int rate)
45 {
46 	int current_frame_number;
47 	int frame_diff;
48 	int est_delay;
49 
50 	if (!subs->last_delay)
51 		return 0; /* short path */
52 
53 	current_frame_number = usb_get_current_frame_number(subs->dev);
54 	/*
55 	 * HCD implementations use different widths, use lower 8 bits.
56 	 * The delay will be managed up to 256ms, which is more than
57 	 * enough
58 	 */
59 	frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
60 
61 	/* Approximation based on number of samples per USB frame (ms),
62 	   some truncation for 44.1 but the estimate is good enough */
63 	est_delay =  frame_diff * rate / 1000;
64 	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
65 		est_delay = subs->last_delay - est_delay;
66 	else
67 		est_delay = subs->last_delay + est_delay;
68 
69 	if (est_delay < 0)
70 		est_delay = 0;
71 	return est_delay;
72 }
73 
74 /*
75  * return the current pcm pointer.  just based on the hwptr_done value.
76  */
snd_usb_pcm_pointer(struct snd_pcm_substream * substream)77 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
78 {
79 	struct snd_usb_substream *subs;
80 	unsigned int hwptr_done;
81 
82 	subs = (struct snd_usb_substream *)substream->runtime->private_data;
83 	if (atomic_read(&subs->stream->chip->shutdown))
84 		return SNDRV_PCM_POS_XRUN;
85 	spin_lock(&subs->lock);
86 	hwptr_done = subs->hwptr_done;
87 	substream->runtime->delay = snd_usb_pcm_delay(subs,
88 						substream->runtime->rate);
89 	spin_unlock(&subs->lock);
90 	return hwptr_done / (substream->runtime->frame_bits >> 3);
91 }
92 
93 /*
94  * find a matching audio format
95  */
find_format(struct snd_usb_substream * subs)96 static struct audioformat *find_format(struct snd_usb_substream *subs)
97 {
98 	struct audioformat *fp;
99 	struct audioformat *found = NULL;
100 	int cur_attr = 0, attr;
101 
102 	list_for_each_entry(fp, &subs->fmt_list, list) {
103 		if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
104 			continue;
105 		if (fp->channels != subs->channels)
106 			continue;
107 		if (subs->cur_rate < fp->rate_min ||
108 		    subs->cur_rate > fp->rate_max)
109 			continue;
110 		if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
111 			unsigned int i;
112 			for (i = 0; i < fp->nr_rates; i++)
113 				if (fp->rate_table[i] == subs->cur_rate)
114 					break;
115 			if (i >= fp->nr_rates)
116 				continue;
117 		}
118 		attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
119 		if (! found) {
120 			found = fp;
121 			cur_attr = attr;
122 			continue;
123 		}
124 		/* avoid async out and adaptive in if the other method
125 		 * supports the same format.
126 		 * this is a workaround for the case like
127 		 * M-audio audiophile USB.
128 		 */
129 		if (attr != cur_attr) {
130 			if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
131 			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
132 			    (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
133 			     subs->direction == SNDRV_PCM_STREAM_CAPTURE))
134 				continue;
135 			if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
136 			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
137 			    (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
138 			     subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
139 				found = fp;
140 				cur_attr = attr;
141 				continue;
142 			}
143 		}
144 		/* find the format with the largest max. packet size */
145 		if (fp->maxpacksize > found->maxpacksize) {
146 			found = fp;
147 			cur_attr = attr;
148 		}
149 	}
150 	return found;
151 }
152 
init_pitch_v1(struct snd_usb_audio * chip,int iface,struct usb_host_interface * alts,struct audioformat * fmt)153 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
154 			 struct usb_host_interface *alts,
155 			 struct audioformat *fmt)
156 {
157 	struct usb_device *dev = chip->dev;
158 	unsigned int ep;
159 	unsigned char data[1];
160 	int err;
161 
162 	if (get_iface_desc(alts)->bNumEndpoints < 1)
163 		return -EINVAL;
164 	ep = get_endpoint(alts, 0)->bEndpointAddress;
165 
166 	data[0] = 1;
167 	if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
168 				   USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
169 				   UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
170 				   data, sizeof(data))) < 0) {
171 		usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
172 			      iface, ep);
173 		return err;
174 	}
175 
176 	return 0;
177 }
178 
init_pitch_v2(struct snd_usb_audio * chip,int iface,struct usb_host_interface * alts,struct audioformat * fmt)179 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
180 			 struct usb_host_interface *alts,
181 			 struct audioformat *fmt)
182 {
183 	struct usb_device *dev = chip->dev;
184 	unsigned char data[1];
185 	int err;
186 
187 	data[0] = 1;
188 	if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
189 				   USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
190 				   UAC2_EP_CS_PITCH << 8, 0,
191 				   data, sizeof(data))) < 0) {
192 		usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
193 			      iface, fmt->altsetting);
194 		return err;
195 	}
196 
197 	return 0;
198 }
199 
200 /*
201  * initialize the pitch control and sample rate
202  */
snd_usb_init_pitch(struct snd_usb_audio * chip,int iface,struct usb_host_interface * alts,struct audioformat * fmt)203 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
204 		       struct usb_host_interface *alts,
205 		       struct audioformat *fmt)
206 {
207 	/* if endpoint doesn't have pitch control, bail out */
208 	if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
209 		return 0;
210 
211 	switch (fmt->protocol) {
212 	case UAC_VERSION_1:
213 	default:
214 		return init_pitch_v1(chip, iface, alts, fmt);
215 
216 	case UAC_VERSION_2:
217 		return init_pitch_v2(chip, iface, alts, fmt);
218 	}
219 }
220 
start_endpoints(struct snd_usb_substream * subs)221 static int start_endpoints(struct snd_usb_substream *subs)
222 {
223 	int err;
224 
225 	if (!subs->data_endpoint)
226 		return -EINVAL;
227 
228 	if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
229 		struct snd_usb_endpoint *ep = subs->data_endpoint;
230 
231 		dev_dbg(&subs->dev->dev, "Starting data EP @%p\n", ep);
232 
233 		ep->data_subs = subs;
234 		err = snd_usb_endpoint_start(ep);
235 		if (err < 0) {
236 			clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
237 			return err;
238 		}
239 	}
240 
241 	if (subs->sync_endpoint &&
242 	    !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
243 		struct snd_usb_endpoint *ep = subs->sync_endpoint;
244 
245 		if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
246 		    subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
247 			err = usb_set_interface(subs->dev,
248 						subs->sync_endpoint->iface,
249 						subs->sync_endpoint->altsetting);
250 			if (err < 0) {
251 				clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
252 				dev_err(&subs->dev->dev,
253 					   "%d:%d: cannot set interface (%d)\n",
254 					   subs->sync_endpoint->iface,
255 					   subs->sync_endpoint->altsetting, err);
256 				return -EIO;
257 			}
258 		}
259 
260 		dev_dbg(&subs->dev->dev, "Starting sync EP @%p\n", ep);
261 
262 		ep->sync_slave = subs->data_endpoint;
263 		err = snd_usb_endpoint_start(ep);
264 		if (err < 0) {
265 			clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
266 			return err;
267 		}
268 	}
269 
270 	return 0;
271 }
272 
stop_endpoints(struct snd_usb_substream * subs,bool wait)273 static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
274 {
275 	if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
276 		snd_usb_endpoint_stop(subs->sync_endpoint);
277 
278 	if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
279 		snd_usb_endpoint_stop(subs->data_endpoint);
280 
281 	if (wait) {
282 		snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
283 		snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
284 	}
285 }
286 
search_roland_implicit_fb(struct usb_device * dev,int ifnum,unsigned int altsetting,struct usb_host_interface ** alts,unsigned int * ep)287 static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
288 				     unsigned int altsetting,
289 				     struct usb_host_interface **alts,
290 				     unsigned int *ep)
291 {
292 	struct usb_interface *iface;
293 	struct usb_interface_descriptor *altsd;
294 	struct usb_endpoint_descriptor *epd;
295 
296 	iface = usb_ifnum_to_if(dev, ifnum);
297 	if (!iface || iface->num_altsetting < altsetting + 1)
298 		return -ENOENT;
299 	*alts = &iface->altsetting[altsetting];
300 	altsd = get_iface_desc(*alts);
301 	if (altsd->bAlternateSetting != altsetting ||
302 	    altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
303 	    (altsd->bInterfaceSubClass != 2 &&
304 	     altsd->bInterfaceProtocol != 2   ) ||
305 	    altsd->bNumEndpoints < 1)
306 		return -ENOENT;
307 	epd = get_endpoint(*alts, 0);
308 	if (!usb_endpoint_is_isoc_in(epd) ||
309 	    (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
310 					USB_ENDPOINT_USAGE_IMPLICIT_FB)
311 		return -ENOENT;
312 	*ep = epd->bEndpointAddress;
313 	return 0;
314 }
315 
316 /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
317  * applies. Returns 1 if a quirk was found.
318  */
set_sync_ep_implicit_fb_quirk(struct snd_usb_substream * subs,struct usb_device * dev,struct usb_interface_descriptor * altsd,unsigned int attr)319 static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
320 					 struct usb_device *dev,
321 					 struct usb_interface_descriptor *altsd,
322 					 unsigned int attr)
323 {
324 	struct usb_host_interface *alts;
325 	struct usb_interface *iface;
326 	unsigned int ep;
327 
328 	/* Implicit feedback sync EPs consumers are always playback EPs */
329 	if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
330 		return 0;
331 
332 	switch (subs->stream->chip->usb_id) {
333 	case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
334 	case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
335 		ep = 0x81;
336 		iface = usb_ifnum_to_if(dev, 3);
337 
338 		if (!iface || iface->num_altsetting == 0)
339 			return -EINVAL;
340 
341 		alts = &iface->altsetting[1];
342 		goto add_sync_ep;
343 		break;
344 	case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
345 	case USB_ID(0x0763, 0x2081):
346 		ep = 0x81;
347 		iface = usb_ifnum_to_if(dev, 2);
348 
349 		if (!iface || iface->num_altsetting == 0)
350 			return -EINVAL;
351 
352 		alts = &iface->altsetting[1];
353 		goto add_sync_ep;
354 	case USB_ID(0x2466, 0x8003):
355 		ep = 0x86;
356 		iface = usb_ifnum_to_if(dev, 2);
357 
358 		if (!iface || iface->num_altsetting == 0)
359 			return -EINVAL;
360 
361 		alts = &iface->altsetting[1];
362 		goto add_sync_ep;
363 	case USB_ID(0x1397, 0x0002):
364 		ep = 0x81;
365 		iface = usb_ifnum_to_if(dev, 1);
366 
367 		if (!iface || iface->num_altsetting == 0)
368 			return -EINVAL;
369 
370 		alts = &iface->altsetting[1];
371 		goto add_sync_ep;
372 
373 	}
374 	if (attr == USB_ENDPOINT_SYNC_ASYNC &&
375 	    altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
376 	    altsd->bInterfaceProtocol == 2 &&
377 	    altsd->bNumEndpoints == 1 &&
378 	    USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
379 	    search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
380 				      altsd->bAlternateSetting,
381 				      &alts, &ep) >= 0) {
382 		goto add_sync_ep;
383 	}
384 
385 	/* No quirk */
386 	return 0;
387 
388 add_sync_ep:
389 	subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
390 						   alts, ep, !subs->direction,
391 						   SND_USB_ENDPOINT_TYPE_DATA);
392 	if (!subs->sync_endpoint)
393 		return -EINVAL;
394 
395 	subs->data_endpoint->sync_master = subs->sync_endpoint;
396 
397 	return 1;
398 }
399 
set_sync_endpoint(struct snd_usb_substream * subs,struct audioformat * fmt,struct usb_device * dev,struct usb_host_interface * alts,struct usb_interface_descriptor * altsd)400 static int set_sync_endpoint(struct snd_usb_substream *subs,
401 			     struct audioformat *fmt,
402 			     struct usb_device *dev,
403 			     struct usb_host_interface *alts,
404 			     struct usb_interface_descriptor *altsd)
405 {
406 	int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
407 	unsigned int ep, attr;
408 	bool implicit_fb;
409 	int err;
410 
411 	/* we need a sync pipe in async OUT or adaptive IN mode */
412 	/* check the number of EP, since some devices have broken
413 	 * descriptors which fool us.  if it has only one EP,
414 	 * assume it as adaptive-out or sync-in.
415 	 */
416 	attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
417 
418 	if ((is_playback && (attr != USB_ENDPOINT_SYNC_ASYNC)) ||
419 		(!is_playback && (attr != USB_ENDPOINT_SYNC_ADAPTIVE))) {
420 
421 		/*
422 		 * In these modes the notion of sync_endpoint is irrelevant.
423 		 * Reset pointers to avoid using stale data from previously
424 		 * used settings, e.g. when configuration and endpoints were
425 		 * changed
426 		 */
427 
428 		subs->sync_endpoint = NULL;
429 		subs->data_endpoint->sync_master = NULL;
430 	}
431 
432 	err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
433 	if (err < 0)
434 		return err;
435 
436 	/* endpoint set by quirk */
437 	if (err > 0)
438 		return 0;
439 
440 	if (altsd->bNumEndpoints < 2)
441 		return 0;
442 
443 	if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
444 			     attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
445 	    (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
446 		return 0;
447 
448 	/*
449 	 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
450 	 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
451 	 * error fall back to SYNC mode and don't create sync endpoint
452 	 */
453 
454 	/* check sync-pipe endpoint */
455 	/* ... and check descriptor size before accessing bSynchAddress
456 	   because there is a version of the SB Audigy 2 NX firmware lacking
457 	   the audio fields in the endpoint descriptors */
458 	if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
459 	    (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
460 	     get_endpoint(alts, 1)->bSynchAddress != 0)) {
461 		dev_err(&dev->dev,
462 			"%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
463 			   fmt->iface, fmt->altsetting,
464 			   get_endpoint(alts, 1)->bmAttributes,
465 			   get_endpoint(alts, 1)->bLength,
466 			   get_endpoint(alts, 1)->bSynchAddress);
467 		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
468 			return 0;
469 		return -EINVAL;
470 	}
471 	ep = get_endpoint(alts, 1)->bEndpointAddress;
472 	if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
473 	    get_endpoint(alts, 0)->bSynchAddress != 0 &&
474 	    ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
475 	     (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
476 		dev_err(&dev->dev,
477 			"%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
478 			   fmt->iface, fmt->altsetting,
479 			   is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
480 		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
481 			return 0;
482 		return -EINVAL;
483 	}
484 
485 	implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
486 			== USB_ENDPOINT_USAGE_IMPLICIT_FB;
487 
488 	subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
489 						   alts, ep, !subs->direction,
490 						   implicit_fb ?
491 							SND_USB_ENDPOINT_TYPE_DATA :
492 							SND_USB_ENDPOINT_TYPE_SYNC);
493 	if (!subs->sync_endpoint) {
494 		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
495 			return 0;
496 		return -EINVAL;
497 	}
498 
499 	subs->data_endpoint->sync_master = subs->sync_endpoint;
500 
501 	return 0;
502 }
503 
504 /*
505  * find a matching format and set up the interface
506  */
set_format(struct snd_usb_substream * subs,struct audioformat * fmt)507 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
508 {
509 	struct usb_device *dev = subs->dev;
510 	struct usb_host_interface *alts;
511 	struct usb_interface_descriptor *altsd;
512 	struct usb_interface *iface;
513 	int err;
514 
515 	iface = usb_ifnum_to_if(dev, fmt->iface);
516 	if (WARN_ON(!iface))
517 		return -EINVAL;
518 	alts = &iface->altsetting[fmt->altset_idx];
519 	altsd = get_iface_desc(alts);
520 	if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
521 		return -EINVAL;
522 
523 	if (fmt == subs->cur_audiofmt)
524 		return 0;
525 
526 	/* close the old interface */
527 	if (subs->interface >= 0 && subs->interface != fmt->iface) {
528 		err = usb_set_interface(subs->dev, subs->interface, 0);
529 		if (err < 0) {
530 			dev_err(&dev->dev,
531 				"%d:%d: return to setting 0 failed (%d)\n",
532 				fmt->iface, fmt->altsetting, err);
533 			return -EIO;
534 		}
535 		subs->interface = -1;
536 		subs->altset_idx = 0;
537 	}
538 
539 	/* set interface */
540 	if (subs->interface != fmt->iface ||
541 	    subs->altset_idx != fmt->altset_idx) {
542 
543 		err = snd_usb_select_mode_quirk(subs, fmt);
544 		if (err < 0)
545 			return -EIO;
546 
547 		err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
548 		if (err < 0) {
549 			dev_err(&dev->dev,
550 				"%d:%d: usb_set_interface failed (%d)\n",
551 				fmt->iface, fmt->altsetting, err);
552 			return -EIO;
553 		}
554 		dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
555 			fmt->iface, fmt->altsetting);
556 		subs->interface = fmt->iface;
557 		subs->altset_idx = fmt->altset_idx;
558 
559 		snd_usb_set_interface_quirk(dev);
560 	}
561 
562 	subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
563 						   alts, fmt->endpoint, subs->direction,
564 						   SND_USB_ENDPOINT_TYPE_DATA);
565 
566 	if (!subs->data_endpoint)
567 		return -EINVAL;
568 
569 	err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
570 	if (err < 0)
571 		return err;
572 
573 	err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
574 	if (err < 0)
575 		return err;
576 
577 	subs->cur_audiofmt = fmt;
578 
579 	snd_usb_set_format_quirk(subs, fmt);
580 
581 	return 0;
582 }
583 
584 /*
585  * Return the score of matching two audioformats.
586  * Veto the audioformat if:
587  * - It has no channels for some reason.
588  * - Requested PCM format is not supported.
589  * - Requested sample rate is not supported.
590  */
match_endpoint_audioformats(struct snd_usb_substream * subs,struct audioformat * fp,struct audioformat * match,int rate,snd_pcm_format_t pcm_format)591 static int match_endpoint_audioformats(struct snd_usb_substream *subs,
592 				       struct audioformat *fp,
593 				       struct audioformat *match, int rate,
594 				       snd_pcm_format_t pcm_format)
595 {
596 	int i;
597 	int score = 0;
598 
599 	if (fp->channels < 1) {
600 		dev_dbg(&subs->dev->dev,
601 			"%s: (fmt @%p) no channels\n", __func__, fp);
602 		return 0;
603 	}
604 
605 	if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
606 		dev_dbg(&subs->dev->dev,
607 			"%s: (fmt @%p) no match for format %d\n", __func__,
608 			fp, pcm_format);
609 		return 0;
610 	}
611 
612 	for (i = 0; i < fp->nr_rates; i++) {
613 		if (fp->rate_table[i] == rate) {
614 			score++;
615 			break;
616 		}
617 	}
618 	if (!score) {
619 		dev_dbg(&subs->dev->dev,
620 			"%s: (fmt @%p) no match for rate %d\n", __func__,
621 			fp, rate);
622 		return 0;
623 	}
624 
625 	if (fp->channels == match->channels)
626 		score++;
627 
628 	dev_dbg(&subs->dev->dev,
629 		"%s: (fmt @%p) score %d\n", __func__, fp, score);
630 
631 	return score;
632 }
633 
634 /*
635  * Configure the sync ep using the rate and pcm format of the data ep.
636  */
configure_sync_endpoint(struct snd_usb_substream * subs)637 static int configure_sync_endpoint(struct snd_usb_substream *subs)
638 {
639 	int ret;
640 	struct audioformat *fp;
641 	struct audioformat *sync_fp = NULL;
642 	int cur_score = 0;
643 	int sync_period_bytes = subs->period_bytes;
644 	struct snd_usb_substream *sync_subs =
645 		&subs->stream->substream[subs->direction ^ 1];
646 
647 	if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
648 	    !subs->stream)
649 		return snd_usb_endpoint_set_params(subs->sync_endpoint,
650 						   subs->pcm_format,
651 						   subs->channels,
652 						   subs->period_bytes,
653 						   0, 0,
654 						   subs->cur_rate,
655 						   subs->cur_audiofmt,
656 						   NULL);
657 
658 	/* Try to find the best matching audioformat. */
659 	list_for_each_entry(fp, &sync_subs->fmt_list, list) {
660 		int score = match_endpoint_audioformats(subs,
661 							fp, subs->cur_audiofmt,
662 			subs->cur_rate, subs->pcm_format);
663 
664 		if (score > cur_score) {
665 			sync_fp = fp;
666 			cur_score = score;
667 		}
668 	}
669 
670 	if (unlikely(sync_fp == NULL)) {
671 		dev_err(&subs->dev->dev,
672 			"%s: no valid audioformat for sync ep %x found\n",
673 			__func__, sync_subs->ep_num);
674 		return -EINVAL;
675 	}
676 
677 	/*
678 	 * Recalculate the period bytes if channel number differ between
679 	 * data and sync ep audioformat.
680 	 */
681 	if (sync_fp->channels != subs->channels) {
682 		sync_period_bytes = (subs->period_bytes / subs->channels) *
683 			sync_fp->channels;
684 		dev_dbg(&subs->dev->dev,
685 			"%s: adjusted sync ep period bytes (%d -> %d)\n",
686 			__func__, subs->period_bytes, sync_period_bytes);
687 	}
688 
689 	ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
690 					  subs->pcm_format,
691 					  sync_fp->channels,
692 					  sync_period_bytes,
693 					  0, 0,
694 					  subs->cur_rate,
695 					  sync_fp,
696 					  NULL);
697 
698 	return ret;
699 }
700 
701 /*
702  * configure endpoint params
703  *
704  * called  during initial setup and upon resume
705  */
configure_endpoint(struct snd_usb_substream * subs)706 static int configure_endpoint(struct snd_usb_substream *subs)
707 {
708 	int ret;
709 
710 	/* format changed */
711 	stop_endpoints(subs, true);
712 	ret = snd_usb_endpoint_set_params(subs->data_endpoint,
713 					  subs->pcm_format,
714 					  subs->channels,
715 					  subs->period_bytes,
716 					  subs->period_frames,
717 					  subs->buffer_periods,
718 					  subs->cur_rate,
719 					  subs->cur_audiofmt,
720 					  subs->sync_endpoint);
721 	if (ret < 0)
722 		return ret;
723 
724 	if (subs->sync_endpoint)
725 		ret = configure_sync_endpoint(subs);
726 
727 	return ret;
728 }
729 
730 /*
731  * hw_params callback
732  *
733  * allocate a buffer and set the given audio format.
734  *
735  * so far we use a physically linear buffer although packetize transfer
736  * doesn't need a continuous area.
737  * if sg buffer is supported on the later version of alsa, we'll follow
738  * that.
739  */
snd_usb_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)740 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
741 			     struct snd_pcm_hw_params *hw_params)
742 {
743 	struct snd_usb_substream *subs = substream->runtime->private_data;
744 	struct audioformat *fmt;
745 	int ret;
746 
747 	ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
748 					       params_buffer_bytes(hw_params));
749 	if (ret < 0)
750 		return ret;
751 
752 	subs->pcm_format = params_format(hw_params);
753 	subs->period_bytes = params_period_bytes(hw_params);
754 	subs->period_frames = params_period_size(hw_params);
755 	subs->buffer_periods = params_periods(hw_params);
756 	subs->channels = params_channels(hw_params);
757 	subs->cur_rate = params_rate(hw_params);
758 
759 	fmt = find_format(subs);
760 	if (!fmt) {
761 		dev_dbg(&subs->dev->dev,
762 			"cannot set format: format = %#x, rate = %d, channels = %d\n",
763 			   subs->pcm_format, subs->cur_rate, subs->channels);
764 		return -EINVAL;
765 	}
766 
767 	ret = snd_usb_lock_shutdown(subs->stream->chip);
768 	if (ret < 0)
769 		return ret;
770 	ret = set_format(subs, fmt);
771 	snd_usb_unlock_shutdown(subs->stream->chip);
772 	if (ret < 0)
773 		return ret;
774 
775 	subs->interface = fmt->iface;
776 	subs->altset_idx = fmt->altset_idx;
777 	subs->need_setup_ep = true;
778 
779 	return 0;
780 }
781 
782 /*
783  * hw_free callback
784  *
785  * reset the audio format and release the buffer
786  */
snd_usb_hw_free(struct snd_pcm_substream * substream)787 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
788 {
789 	struct snd_usb_substream *subs = substream->runtime->private_data;
790 
791 	subs->cur_audiofmt = NULL;
792 	subs->cur_rate = 0;
793 	subs->period_bytes = 0;
794 	if (!snd_usb_lock_shutdown(subs->stream->chip)) {
795 		stop_endpoints(subs, true);
796 		snd_usb_endpoint_deactivate(subs->sync_endpoint);
797 		snd_usb_endpoint_deactivate(subs->data_endpoint);
798 		snd_usb_unlock_shutdown(subs->stream->chip);
799 	}
800 	return snd_pcm_lib_free_vmalloc_buffer(substream);
801 }
802 
803 /*
804  * prepare callback
805  *
806  * only a few subtle things...
807  */
snd_usb_pcm_prepare(struct snd_pcm_substream * substream)808 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
809 {
810 	struct snd_pcm_runtime *runtime = substream->runtime;
811 	struct snd_usb_substream *subs = runtime->private_data;
812 	struct usb_host_interface *alts;
813 	struct usb_interface *iface;
814 	int ret;
815 
816 	if (! subs->cur_audiofmt) {
817 		dev_err(&subs->dev->dev, "no format is specified!\n");
818 		return -ENXIO;
819 	}
820 
821 	ret = snd_usb_lock_shutdown(subs->stream->chip);
822 	if (ret < 0)
823 		return ret;
824 	if (snd_BUG_ON(!subs->data_endpoint)) {
825 		ret = -EIO;
826 		goto unlock;
827 	}
828 
829 	snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
830 	snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
831 
832 	ret = set_format(subs, subs->cur_audiofmt);
833 	if (ret < 0)
834 		goto unlock;
835 
836 	if (subs->need_setup_ep) {
837 
838 		iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
839 		alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
840 		ret = snd_usb_init_sample_rate(subs->stream->chip,
841 					       subs->cur_audiofmt->iface,
842 					       alts,
843 					       subs->cur_audiofmt,
844 					       subs->cur_rate);
845 		if (ret < 0)
846 			goto unlock;
847 
848 		ret = configure_endpoint(subs);
849 		if (ret < 0)
850 			goto unlock;
851 		subs->need_setup_ep = false;
852 	}
853 
854 	/* some unit conversions in runtime */
855 	subs->data_endpoint->maxframesize =
856 		bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
857 	subs->data_endpoint->curframesize =
858 		bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
859 
860 	/* reset the pointer */
861 	subs->hwptr_done = 0;
862 	subs->transfer_done = 0;
863 	subs->last_delay = 0;
864 	subs->last_frame_number = 0;
865 	runtime->delay = 0;
866 
867 	/* for playback, submit the URBs now; otherwise, the first hwptr_done
868 	 * updates for all URBs would happen at the same time when starting */
869 	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
870 		ret = start_endpoints(subs);
871 
872  unlock:
873 	snd_usb_unlock_shutdown(subs->stream->chip);
874 	return ret;
875 }
876 
877 static const struct snd_pcm_hardware snd_usb_hardware =
878 {
879 	.info =			SNDRV_PCM_INFO_MMAP |
880 				SNDRV_PCM_INFO_MMAP_VALID |
881 				SNDRV_PCM_INFO_BATCH |
882 				SNDRV_PCM_INFO_INTERLEAVED |
883 				SNDRV_PCM_INFO_BLOCK_TRANSFER |
884 				SNDRV_PCM_INFO_PAUSE,
885 	.buffer_bytes_max =	1024 * 1024,
886 	.period_bytes_min =	64,
887 	.period_bytes_max =	512 * 1024,
888 	.periods_min =		2,
889 	.periods_max =		1024,
890 };
891 
hw_check_valid_format(struct snd_usb_substream * subs,struct snd_pcm_hw_params * params,struct audioformat * fp)892 static int hw_check_valid_format(struct snd_usb_substream *subs,
893 				 struct snd_pcm_hw_params *params,
894 				 struct audioformat *fp)
895 {
896 	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
897 	struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
898 	struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
899 	struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
900 	struct snd_mask check_fmts;
901 	unsigned int ptime;
902 
903 	/* check the format */
904 	snd_mask_none(&check_fmts);
905 	check_fmts.bits[0] = (u32)fp->formats;
906 	check_fmts.bits[1] = (u32)(fp->formats >> 32);
907 	snd_mask_intersect(&check_fmts, fmts);
908 	if (snd_mask_empty(&check_fmts)) {
909 		hwc_debug("   > check: no supported format %d\n", fp->format);
910 		return 0;
911 	}
912 	/* check the channels */
913 	if (fp->channels < ct->min || fp->channels > ct->max) {
914 		hwc_debug("   > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
915 		return 0;
916 	}
917 	/* check the rate is within the range */
918 	if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
919 		hwc_debug("   > check: rate_min %d > max %d\n", fp->rate_min, it->max);
920 		return 0;
921 	}
922 	if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
923 		hwc_debug("   > check: rate_max %d < min %d\n", fp->rate_max, it->min);
924 		return 0;
925 	}
926 	/* check whether the period time is >= the data packet interval */
927 	if (subs->speed != USB_SPEED_FULL) {
928 		ptime = 125 * (1 << fp->datainterval);
929 		if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
930 			hwc_debug("   > check: ptime %u > max %u\n", ptime, pt->max);
931 			return 0;
932 		}
933 	}
934 	return 1;
935 }
936 
hw_rule_rate(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)937 static int hw_rule_rate(struct snd_pcm_hw_params *params,
938 			struct snd_pcm_hw_rule *rule)
939 {
940 	struct snd_usb_substream *subs = rule->private;
941 	struct audioformat *fp;
942 	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
943 	unsigned int rmin, rmax;
944 	int changed;
945 
946 	hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
947 	changed = 0;
948 	rmin = rmax = 0;
949 	list_for_each_entry(fp, &subs->fmt_list, list) {
950 		if (!hw_check_valid_format(subs, params, fp))
951 			continue;
952 		if (changed++) {
953 			if (rmin > fp->rate_min)
954 				rmin = fp->rate_min;
955 			if (rmax < fp->rate_max)
956 				rmax = fp->rate_max;
957 		} else {
958 			rmin = fp->rate_min;
959 			rmax = fp->rate_max;
960 		}
961 	}
962 
963 	if (!changed) {
964 		hwc_debug("  --> get empty\n");
965 		it->empty = 1;
966 		return -EINVAL;
967 	}
968 
969 	changed = 0;
970 	if (it->min < rmin) {
971 		it->min = rmin;
972 		it->openmin = 0;
973 		changed = 1;
974 	}
975 	if (it->max > rmax) {
976 		it->max = rmax;
977 		it->openmax = 0;
978 		changed = 1;
979 	}
980 	if (snd_interval_checkempty(it)) {
981 		it->empty = 1;
982 		return -EINVAL;
983 	}
984 	hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
985 	return changed;
986 }
987 
988 
hw_rule_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)989 static int hw_rule_channels(struct snd_pcm_hw_params *params,
990 			    struct snd_pcm_hw_rule *rule)
991 {
992 	struct snd_usb_substream *subs = rule->private;
993 	struct audioformat *fp;
994 	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
995 	unsigned int rmin, rmax;
996 	int changed;
997 
998 	hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
999 	changed = 0;
1000 	rmin = rmax = 0;
1001 	list_for_each_entry(fp, &subs->fmt_list, list) {
1002 		if (!hw_check_valid_format(subs, params, fp))
1003 			continue;
1004 		if (changed++) {
1005 			if (rmin > fp->channels)
1006 				rmin = fp->channels;
1007 			if (rmax < fp->channels)
1008 				rmax = fp->channels;
1009 		} else {
1010 			rmin = fp->channels;
1011 			rmax = fp->channels;
1012 		}
1013 	}
1014 
1015 	if (!changed) {
1016 		hwc_debug("  --> get empty\n");
1017 		it->empty = 1;
1018 		return -EINVAL;
1019 	}
1020 
1021 	changed = 0;
1022 	if (it->min < rmin) {
1023 		it->min = rmin;
1024 		it->openmin = 0;
1025 		changed = 1;
1026 	}
1027 	if (it->max > rmax) {
1028 		it->max = rmax;
1029 		it->openmax = 0;
1030 		changed = 1;
1031 	}
1032 	if (snd_interval_checkempty(it)) {
1033 		it->empty = 1;
1034 		return -EINVAL;
1035 	}
1036 	hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1037 	return changed;
1038 }
1039 
hw_rule_format(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1040 static int hw_rule_format(struct snd_pcm_hw_params *params,
1041 			  struct snd_pcm_hw_rule *rule)
1042 {
1043 	struct snd_usb_substream *subs = rule->private;
1044 	struct audioformat *fp;
1045 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1046 	u64 fbits;
1047 	u32 oldbits[2];
1048 	int changed;
1049 
1050 	hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1051 	fbits = 0;
1052 	list_for_each_entry(fp, &subs->fmt_list, list) {
1053 		if (!hw_check_valid_format(subs, params, fp))
1054 			continue;
1055 		fbits |= fp->formats;
1056 	}
1057 
1058 	oldbits[0] = fmt->bits[0];
1059 	oldbits[1] = fmt->bits[1];
1060 	fmt->bits[0] &= (u32)fbits;
1061 	fmt->bits[1] &= (u32)(fbits >> 32);
1062 	if (!fmt->bits[0] && !fmt->bits[1]) {
1063 		hwc_debug("  --> get empty\n");
1064 		return -EINVAL;
1065 	}
1066 	changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1067 	hwc_debug("  --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1068 	return changed;
1069 }
1070 
hw_rule_period_time(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1071 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1072 			       struct snd_pcm_hw_rule *rule)
1073 {
1074 	struct snd_usb_substream *subs = rule->private;
1075 	struct audioformat *fp;
1076 	struct snd_interval *it;
1077 	unsigned char min_datainterval;
1078 	unsigned int pmin;
1079 	int changed;
1080 
1081 	it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1082 	hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1083 	min_datainterval = 0xff;
1084 	list_for_each_entry(fp, &subs->fmt_list, list) {
1085 		if (!hw_check_valid_format(subs, params, fp))
1086 			continue;
1087 		min_datainterval = min(min_datainterval, fp->datainterval);
1088 	}
1089 	if (min_datainterval == 0xff) {
1090 		hwc_debug("  --> get empty\n");
1091 		it->empty = 1;
1092 		return -EINVAL;
1093 	}
1094 	pmin = 125 * (1 << min_datainterval);
1095 	changed = 0;
1096 	if (it->min < pmin) {
1097 		it->min = pmin;
1098 		it->openmin = 0;
1099 		changed = 1;
1100 	}
1101 	if (snd_interval_checkempty(it)) {
1102 		it->empty = 1;
1103 		return -EINVAL;
1104 	}
1105 	hwc_debug("  --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1106 	return changed;
1107 }
1108 
1109 /*
1110  *  If the device supports unusual bit rates, does the request meet these?
1111  */
snd_usb_pcm_check_knot(struct snd_pcm_runtime * runtime,struct snd_usb_substream * subs)1112 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1113 				  struct snd_usb_substream *subs)
1114 {
1115 	struct audioformat *fp;
1116 	int *rate_list;
1117 	int count = 0, needs_knot = 0;
1118 	int err;
1119 
1120 	kfree(subs->rate_list.list);
1121 	subs->rate_list.list = NULL;
1122 
1123 	list_for_each_entry(fp, &subs->fmt_list, list) {
1124 		if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1125 			return 0;
1126 		count += fp->nr_rates;
1127 		if (fp->rates & SNDRV_PCM_RATE_KNOT)
1128 			needs_knot = 1;
1129 	}
1130 	if (!needs_knot)
1131 		return 0;
1132 
1133 	subs->rate_list.list = rate_list =
1134 		kmalloc(sizeof(int) * count, GFP_KERNEL);
1135 	if (!subs->rate_list.list)
1136 		return -ENOMEM;
1137 	subs->rate_list.count = count;
1138 	subs->rate_list.mask = 0;
1139 	count = 0;
1140 	list_for_each_entry(fp, &subs->fmt_list, list) {
1141 		int i;
1142 		for (i = 0; i < fp->nr_rates; i++)
1143 			rate_list[count++] = fp->rate_table[i];
1144 	}
1145 	err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1146 					 &subs->rate_list);
1147 	if (err < 0)
1148 		return err;
1149 
1150 	return 0;
1151 }
1152 
1153 
1154 /*
1155  * set up the runtime hardware information.
1156  */
1157 
setup_hw_info(struct snd_pcm_runtime * runtime,struct snd_usb_substream * subs)1158 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1159 {
1160 	struct audioformat *fp;
1161 	unsigned int pt, ptmin;
1162 	int param_period_time_if_needed;
1163 	int err;
1164 
1165 	runtime->hw.formats = subs->formats;
1166 
1167 	runtime->hw.rate_min = 0x7fffffff;
1168 	runtime->hw.rate_max = 0;
1169 	runtime->hw.channels_min = 256;
1170 	runtime->hw.channels_max = 0;
1171 	runtime->hw.rates = 0;
1172 	ptmin = UINT_MAX;
1173 	/* check min/max rates and channels */
1174 	list_for_each_entry(fp, &subs->fmt_list, list) {
1175 		runtime->hw.rates |= fp->rates;
1176 		if (runtime->hw.rate_min > fp->rate_min)
1177 			runtime->hw.rate_min = fp->rate_min;
1178 		if (runtime->hw.rate_max < fp->rate_max)
1179 			runtime->hw.rate_max = fp->rate_max;
1180 		if (runtime->hw.channels_min > fp->channels)
1181 			runtime->hw.channels_min = fp->channels;
1182 		if (runtime->hw.channels_max < fp->channels)
1183 			runtime->hw.channels_max = fp->channels;
1184 		if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1185 			/* FIXME: there might be more than one audio formats... */
1186 			runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1187 				fp->frame_size;
1188 		}
1189 		pt = 125 * (1 << fp->datainterval);
1190 		ptmin = min(ptmin, pt);
1191 	}
1192 	err = snd_usb_autoresume(subs->stream->chip);
1193 	if (err < 0)
1194 		return err;
1195 
1196 	param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1197 	if (subs->speed == USB_SPEED_FULL)
1198 		/* full speed devices have fixed data packet interval */
1199 		ptmin = 1000;
1200 	if (ptmin == 1000)
1201 		/* if period time doesn't go below 1 ms, no rules needed */
1202 		param_period_time_if_needed = -1;
1203 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1204 				     ptmin, UINT_MAX);
1205 
1206 	if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1207 				       hw_rule_rate, subs,
1208 				       SNDRV_PCM_HW_PARAM_FORMAT,
1209 				       SNDRV_PCM_HW_PARAM_CHANNELS,
1210 				       param_period_time_if_needed,
1211 				       -1)) < 0)
1212 		goto rep_err;
1213 	if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1214 				       hw_rule_channels, subs,
1215 				       SNDRV_PCM_HW_PARAM_FORMAT,
1216 				       SNDRV_PCM_HW_PARAM_RATE,
1217 				       param_period_time_if_needed,
1218 				       -1)) < 0)
1219 		goto rep_err;
1220 	if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1221 				       hw_rule_format, subs,
1222 				       SNDRV_PCM_HW_PARAM_RATE,
1223 				       SNDRV_PCM_HW_PARAM_CHANNELS,
1224 				       param_period_time_if_needed,
1225 				       -1)) < 0)
1226 		goto rep_err;
1227 	if (param_period_time_if_needed >= 0) {
1228 		err = snd_pcm_hw_rule_add(runtime, 0,
1229 					  SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1230 					  hw_rule_period_time, subs,
1231 					  SNDRV_PCM_HW_PARAM_FORMAT,
1232 					  SNDRV_PCM_HW_PARAM_CHANNELS,
1233 					  SNDRV_PCM_HW_PARAM_RATE,
1234 					  -1);
1235 		if (err < 0)
1236 			goto rep_err;
1237 	}
1238 	if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
1239 		goto rep_err;
1240 	return 0;
1241 
1242 rep_err:
1243 	snd_usb_autosuspend(subs->stream->chip);
1244 	return err;
1245 }
1246 
snd_usb_pcm_open(struct snd_pcm_substream * substream,int direction)1247 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1248 {
1249 	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1250 	struct snd_pcm_runtime *runtime = substream->runtime;
1251 	struct snd_usb_substream *subs = &as->substream[direction];
1252 
1253 	subs->interface = -1;
1254 	subs->altset_idx = 0;
1255 	runtime->hw = snd_usb_hardware;
1256 	runtime->private_data = subs;
1257 	subs->pcm_substream = substream;
1258 	/* runtime PM is also done there */
1259 
1260 	/* initialize DSD/DOP context */
1261 	subs->dsd_dop.byte_idx = 0;
1262 	subs->dsd_dop.channel = 0;
1263 	subs->dsd_dop.marker = 1;
1264 
1265 	return setup_hw_info(runtime, subs);
1266 }
1267 
snd_usb_pcm_close(struct snd_pcm_substream * substream,int direction)1268 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1269 {
1270 	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1271 	struct snd_usb_substream *subs = &as->substream[direction];
1272 
1273 	stop_endpoints(subs, true);
1274 
1275 	if (subs->interface >= 0 &&
1276 	    !snd_usb_lock_shutdown(subs->stream->chip)) {
1277 		usb_set_interface(subs->dev, subs->interface, 0);
1278 		subs->interface = -1;
1279 		snd_usb_unlock_shutdown(subs->stream->chip);
1280 	}
1281 
1282 	subs->pcm_substream = NULL;
1283 	snd_usb_autosuspend(subs->stream->chip);
1284 
1285 	return 0;
1286 }
1287 
1288 /* Since a URB can handle only a single linear buffer, we must use double
1289  * buffering when the data to be transferred overflows the buffer boundary.
1290  * To avoid inconsistencies when updating hwptr_done, we use double buffering
1291  * for all URBs.
1292  */
retire_capture_urb(struct snd_usb_substream * subs,struct urb * urb)1293 static void retire_capture_urb(struct snd_usb_substream *subs,
1294 			       struct urb *urb)
1295 {
1296 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1297 	unsigned int stride, frames, bytes, oldptr;
1298 	int i, period_elapsed = 0;
1299 	unsigned long flags;
1300 	unsigned char *cp;
1301 	int current_frame_number;
1302 
1303 	/* read frame number here, update pointer in critical section */
1304 	current_frame_number = usb_get_current_frame_number(subs->dev);
1305 
1306 	stride = runtime->frame_bits >> 3;
1307 
1308 	for (i = 0; i < urb->number_of_packets; i++) {
1309 		cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
1310 		if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1311 			dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1312 				i, urb->iso_frame_desc[i].status);
1313 			// continue;
1314 		}
1315 		bytes = urb->iso_frame_desc[i].actual_length;
1316 		frames = bytes / stride;
1317 		if (!subs->txfr_quirk)
1318 			bytes = frames * stride;
1319 		if (bytes % (runtime->sample_bits >> 3) != 0) {
1320 			int oldbytes = bytes;
1321 			bytes = frames * stride;
1322 			dev_warn_ratelimited(&subs->dev->dev,
1323 				 "Corrected urb data len. %d->%d\n",
1324 							oldbytes, bytes);
1325 		}
1326 		/* update the current pointer */
1327 		spin_lock_irqsave(&subs->lock, flags);
1328 		oldptr = subs->hwptr_done;
1329 		subs->hwptr_done += bytes;
1330 		if (subs->hwptr_done >= runtime->buffer_size * stride)
1331 			subs->hwptr_done -= runtime->buffer_size * stride;
1332 		frames = (bytes + (oldptr % stride)) / stride;
1333 		subs->transfer_done += frames;
1334 		if (subs->transfer_done >= runtime->period_size) {
1335 			subs->transfer_done -= runtime->period_size;
1336 			period_elapsed = 1;
1337 		}
1338 		/* capture delay is by construction limited to one URB,
1339 		 * reset delays here
1340 		 */
1341 		runtime->delay = subs->last_delay = 0;
1342 
1343 		/* realign last_frame_number */
1344 		subs->last_frame_number = current_frame_number;
1345 		subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1346 
1347 		spin_unlock_irqrestore(&subs->lock, flags);
1348 		/* copy a data chunk */
1349 		if (oldptr + bytes > runtime->buffer_size * stride) {
1350 			unsigned int bytes1 =
1351 					runtime->buffer_size * stride - oldptr;
1352 			memcpy(runtime->dma_area + oldptr, cp, bytes1);
1353 			memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1354 		} else {
1355 			memcpy(runtime->dma_area + oldptr, cp, bytes);
1356 		}
1357 	}
1358 
1359 	if (period_elapsed)
1360 		snd_pcm_period_elapsed(subs->pcm_substream);
1361 }
1362 
fill_playback_urb_dsd_dop(struct snd_usb_substream * subs,struct urb * urb,unsigned int bytes)1363 static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1364 					     struct urb *urb, unsigned int bytes)
1365 {
1366 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1367 	unsigned int stride = runtime->frame_bits >> 3;
1368 	unsigned int dst_idx = 0;
1369 	unsigned int src_idx = subs->hwptr_done;
1370 	unsigned int wrap = runtime->buffer_size * stride;
1371 	u8 *dst = urb->transfer_buffer;
1372 	u8 *src = runtime->dma_area;
1373 	u8 marker[] = { 0x05, 0xfa };
1374 
1375 	/*
1376 	 * The DSP DOP format defines a way to transport DSD samples over
1377 	 * normal PCM data endpoints. It requires stuffing of marker bytes
1378 	 * (0x05 and 0xfa, alternating per sample frame), and then expects
1379 	 * 2 additional bytes of actual payload. The whole frame is stored
1380 	 * LSB.
1381 	 *
1382 	 * Hence, for a stereo transport, the buffer layout looks like this,
1383 	 * where L refers to left channel samples and R to right.
1384 	 *
1385 	 *   L1 L2 0x05   R1 R2 0x05   L3 L4 0xfa  R3 R4 0xfa
1386 	 *   L5 L6 0x05   R5 R6 0x05   L7 L8 0xfa  R7 R8 0xfa
1387 	 *   .....
1388 	 *
1389 	 */
1390 
1391 	while (bytes--) {
1392 		if (++subs->dsd_dop.byte_idx == 3) {
1393 			/* frame boundary? */
1394 			dst[dst_idx++] = marker[subs->dsd_dop.marker];
1395 			src_idx += 2;
1396 			subs->dsd_dop.byte_idx = 0;
1397 
1398 			if (++subs->dsd_dop.channel % runtime->channels == 0) {
1399 				/* alternate the marker */
1400 				subs->dsd_dop.marker++;
1401 				subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1402 				subs->dsd_dop.channel = 0;
1403 			}
1404 		} else {
1405 			/* stuff the DSD payload */
1406 			int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
1407 
1408 			if (subs->cur_audiofmt->dsd_bitrev)
1409 				dst[dst_idx++] = bitrev8(src[idx]);
1410 			else
1411 				dst[dst_idx++] = src[idx];
1412 
1413 			subs->hwptr_done++;
1414 		}
1415 	}
1416 	if (subs->hwptr_done >= runtime->buffer_size * stride)
1417 		subs->hwptr_done -= runtime->buffer_size * stride;
1418 }
1419 
copy_to_urb(struct snd_usb_substream * subs,struct urb * urb,int offset,int stride,unsigned int bytes)1420 static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1421 			int offset, int stride, unsigned int bytes)
1422 {
1423 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1424 
1425 	if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1426 		/* err, the transferred area goes over buffer boundary. */
1427 		unsigned int bytes1 =
1428 			runtime->buffer_size * stride - subs->hwptr_done;
1429 		memcpy(urb->transfer_buffer + offset,
1430 		       runtime->dma_area + subs->hwptr_done, bytes1);
1431 		memcpy(urb->transfer_buffer + offset + bytes1,
1432 		       runtime->dma_area, bytes - bytes1);
1433 	} else {
1434 		memcpy(urb->transfer_buffer + offset,
1435 		       runtime->dma_area + subs->hwptr_done, bytes);
1436 	}
1437 	subs->hwptr_done += bytes;
1438 	if (subs->hwptr_done >= runtime->buffer_size * stride)
1439 		subs->hwptr_done -= runtime->buffer_size * stride;
1440 }
1441 
copy_to_urb_quirk(struct snd_usb_substream * subs,struct urb * urb,int stride,unsigned int bytes)1442 static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1443 				      struct urb *urb, int stride,
1444 				      unsigned int bytes)
1445 {
1446 	__le32 packet_length;
1447 	int i;
1448 
1449 	/* Put __le32 length descriptor at start of each packet. */
1450 	for (i = 0; i < urb->number_of_packets; i++) {
1451 		unsigned int length = urb->iso_frame_desc[i].length;
1452 		unsigned int offset = urb->iso_frame_desc[i].offset;
1453 
1454 		packet_length = cpu_to_le32(length);
1455 		offset += i * sizeof(packet_length);
1456 		urb->iso_frame_desc[i].offset = offset;
1457 		urb->iso_frame_desc[i].length += sizeof(packet_length);
1458 		memcpy(urb->transfer_buffer + offset,
1459 		       &packet_length, sizeof(packet_length));
1460 		copy_to_urb(subs, urb, offset + sizeof(packet_length),
1461 			    stride, length);
1462 	}
1463 	/* Adjust transfer size accordingly. */
1464 	bytes += urb->number_of_packets * sizeof(packet_length);
1465 	return bytes;
1466 }
1467 
prepare_playback_urb(struct snd_usb_substream * subs,struct urb * urb)1468 static void prepare_playback_urb(struct snd_usb_substream *subs,
1469 				 struct urb *urb)
1470 {
1471 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1472 	struct snd_usb_endpoint *ep = subs->data_endpoint;
1473 	struct snd_urb_ctx *ctx = urb->context;
1474 	unsigned int counts, frames, bytes;
1475 	int i, stride, period_elapsed = 0;
1476 	unsigned long flags;
1477 
1478 	stride = runtime->frame_bits >> 3;
1479 
1480 	frames = 0;
1481 	urb->number_of_packets = 0;
1482 	spin_lock_irqsave(&subs->lock, flags);
1483 	subs->frame_limit += ep->max_urb_frames;
1484 	for (i = 0; i < ctx->packets; i++) {
1485 		if (ctx->packet_size[i])
1486 			counts = ctx->packet_size[i];
1487 		else
1488 			counts = snd_usb_endpoint_next_packet_size(ep);
1489 
1490 		/* set up descriptor */
1491 		urb->iso_frame_desc[i].offset = frames * ep->stride;
1492 		urb->iso_frame_desc[i].length = counts * ep->stride;
1493 		frames += counts;
1494 		urb->number_of_packets++;
1495 		subs->transfer_done += counts;
1496 		if (subs->transfer_done >= runtime->period_size) {
1497 			subs->transfer_done -= runtime->period_size;
1498 			subs->frame_limit = 0;
1499 			period_elapsed = 1;
1500 			if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1501 				if (subs->transfer_done > 0) {
1502 					/* FIXME: fill-max mode is not
1503 					 * supported yet */
1504 					frames -= subs->transfer_done;
1505 					counts -= subs->transfer_done;
1506 					urb->iso_frame_desc[i].length =
1507 						counts * ep->stride;
1508 					subs->transfer_done = 0;
1509 				}
1510 				i++;
1511 				if (i < ctx->packets) {
1512 					/* add a transfer delimiter */
1513 					urb->iso_frame_desc[i].offset =
1514 						frames * ep->stride;
1515 					urb->iso_frame_desc[i].length = 0;
1516 					urb->number_of_packets++;
1517 				}
1518 				break;
1519 			}
1520 		}
1521 		/* finish at the period boundary or after enough frames */
1522 		if ((period_elapsed ||
1523 				subs->transfer_done >= subs->frame_limit) &&
1524 		    !snd_usb_endpoint_implicit_feedback_sink(ep))
1525 			break;
1526 	}
1527 	bytes = frames * ep->stride;
1528 
1529 	if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1530 		     subs->cur_audiofmt->dsd_dop)) {
1531 		fill_playback_urb_dsd_dop(subs, urb, bytes);
1532 	} else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1533 			   subs->cur_audiofmt->dsd_bitrev)) {
1534 		/* bit-reverse the bytes */
1535 		u8 *buf = urb->transfer_buffer;
1536 		for (i = 0; i < bytes; i++) {
1537 			int idx = (subs->hwptr_done + i)
1538 				% (runtime->buffer_size * stride);
1539 			buf[i] = bitrev8(runtime->dma_area[idx]);
1540 		}
1541 
1542 		subs->hwptr_done += bytes;
1543 		if (subs->hwptr_done >= runtime->buffer_size * stride)
1544 			subs->hwptr_done -= runtime->buffer_size * stride;
1545 	} else {
1546 		/* usual PCM */
1547 		if (!subs->tx_length_quirk)
1548 			copy_to_urb(subs, urb, 0, stride, bytes);
1549 		else
1550 			bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1551 			/* bytes is now amount of outgoing data */
1552 	}
1553 
1554 	/* update delay with exact number of samples queued */
1555 	runtime->delay = subs->last_delay;
1556 	runtime->delay += frames;
1557 	subs->last_delay = runtime->delay;
1558 
1559 	/* realign last_frame_number */
1560 	subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1561 	subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1562 
1563 	if (subs->trigger_tstamp_pending_update) {
1564 		/* this is the first actual URB submitted,
1565 		 * update trigger timestamp to reflect actual start time
1566 		 */
1567 		snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1568 		subs->trigger_tstamp_pending_update = false;
1569 	}
1570 
1571 	spin_unlock_irqrestore(&subs->lock, flags);
1572 	urb->transfer_buffer_length = bytes;
1573 	if (period_elapsed)
1574 		snd_pcm_period_elapsed(subs->pcm_substream);
1575 }
1576 
1577 /*
1578  * process after playback data complete
1579  * - decrease the delay count again
1580  */
retire_playback_urb(struct snd_usb_substream * subs,struct urb * urb)1581 static void retire_playback_urb(struct snd_usb_substream *subs,
1582 			       struct urb *urb)
1583 {
1584 	unsigned long flags;
1585 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1586 	struct snd_usb_endpoint *ep = subs->data_endpoint;
1587 	int processed = urb->transfer_buffer_length / ep->stride;
1588 	int est_delay;
1589 
1590 	/* ignore the delay accounting when procssed=0 is given, i.e.
1591 	 * silent payloads are procssed before handling the actual data
1592 	 */
1593 	if (!processed)
1594 		return;
1595 
1596 	spin_lock_irqsave(&subs->lock, flags);
1597 	if (!subs->last_delay)
1598 		goto out; /* short path */
1599 
1600 	est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1601 	/* update delay with exact number of samples played */
1602 	if (processed > subs->last_delay)
1603 		subs->last_delay = 0;
1604 	else
1605 		subs->last_delay -= processed;
1606 	runtime->delay = subs->last_delay;
1607 
1608 	/*
1609 	 * Report when delay estimate is off by more than 2ms.
1610 	 * The error should be lower than 2ms since the estimate relies
1611 	 * on two reads of a counter updated every ms.
1612 	 */
1613 	if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1614 		dev_dbg_ratelimited(&subs->dev->dev,
1615 			"delay: estimated %d, actual %d\n",
1616 			est_delay, subs->last_delay);
1617 
1618 	if (!subs->running) {
1619 		/* update last_frame_number for delay counting here since
1620 		 * prepare_playback_urb won't be called during pause
1621 		 */
1622 		subs->last_frame_number =
1623 			usb_get_current_frame_number(subs->dev) & 0xff;
1624 	}
1625 
1626  out:
1627 	spin_unlock_irqrestore(&subs->lock, flags);
1628 }
1629 
snd_usb_playback_open(struct snd_pcm_substream * substream)1630 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1631 {
1632 	return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1633 }
1634 
snd_usb_playback_close(struct snd_pcm_substream * substream)1635 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1636 {
1637 	return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1638 }
1639 
snd_usb_capture_open(struct snd_pcm_substream * substream)1640 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1641 {
1642 	return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1643 }
1644 
snd_usb_capture_close(struct snd_pcm_substream * substream)1645 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1646 {
1647 	return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1648 }
1649 
snd_usb_substream_playback_trigger(struct snd_pcm_substream * substream,int cmd)1650 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1651 					      int cmd)
1652 {
1653 	struct snd_usb_substream *subs = substream->runtime->private_data;
1654 
1655 	switch (cmd) {
1656 	case SNDRV_PCM_TRIGGER_START:
1657 		subs->trigger_tstamp_pending_update = true;
1658 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1659 		subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1660 		subs->data_endpoint->retire_data_urb = retire_playback_urb;
1661 		subs->running = 1;
1662 		return 0;
1663 	case SNDRV_PCM_TRIGGER_STOP:
1664 		stop_endpoints(subs, false);
1665 		subs->running = 0;
1666 		return 0;
1667 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1668 		subs->data_endpoint->prepare_data_urb = NULL;
1669 		/* keep retire_data_urb for delay calculation */
1670 		subs->data_endpoint->retire_data_urb = retire_playback_urb;
1671 		subs->running = 0;
1672 		return 0;
1673 	}
1674 
1675 	return -EINVAL;
1676 }
1677 
snd_usb_substream_capture_trigger(struct snd_pcm_substream * substream,int cmd)1678 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1679 					     int cmd)
1680 {
1681 	int err;
1682 	struct snd_usb_substream *subs = substream->runtime->private_data;
1683 
1684 	switch (cmd) {
1685 	case SNDRV_PCM_TRIGGER_START:
1686 		err = start_endpoints(subs);
1687 		if (err < 0)
1688 			return err;
1689 
1690 		subs->data_endpoint->retire_data_urb = retire_capture_urb;
1691 		subs->running = 1;
1692 		return 0;
1693 	case SNDRV_PCM_TRIGGER_STOP:
1694 		stop_endpoints(subs, false);
1695 		subs->running = 0;
1696 		return 0;
1697 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1698 		subs->data_endpoint->retire_data_urb = NULL;
1699 		subs->running = 0;
1700 		return 0;
1701 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1702 		subs->data_endpoint->retire_data_urb = retire_capture_urb;
1703 		subs->running = 1;
1704 		return 0;
1705 	}
1706 
1707 	return -EINVAL;
1708 }
1709 
1710 static const struct snd_pcm_ops snd_usb_playback_ops = {
1711 	.open =		snd_usb_playback_open,
1712 	.close =	snd_usb_playback_close,
1713 	.ioctl =	snd_pcm_lib_ioctl,
1714 	.hw_params =	snd_usb_hw_params,
1715 	.hw_free =	snd_usb_hw_free,
1716 	.prepare =	snd_usb_pcm_prepare,
1717 	.trigger =	snd_usb_substream_playback_trigger,
1718 	.pointer =	snd_usb_pcm_pointer,
1719 	.page =		snd_pcm_lib_get_vmalloc_page,
1720 	.mmap =		snd_pcm_lib_mmap_vmalloc,
1721 };
1722 
1723 static const struct snd_pcm_ops snd_usb_capture_ops = {
1724 	.open =		snd_usb_capture_open,
1725 	.close =	snd_usb_capture_close,
1726 	.ioctl =	snd_pcm_lib_ioctl,
1727 	.hw_params =	snd_usb_hw_params,
1728 	.hw_free =	snd_usb_hw_free,
1729 	.prepare =	snd_usb_pcm_prepare,
1730 	.trigger =	snd_usb_substream_capture_trigger,
1731 	.pointer =	snd_usb_pcm_pointer,
1732 	.page =		snd_pcm_lib_get_vmalloc_page,
1733 	.mmap =		snd_pcm_lib_mmap_vmalloc,
1734 };
1735 
snd_usb_set_pcm_ops(struct snd_pcm * pcm,int stream)1736 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1737 {
1738 	snd_pcm_set_ops(pcm, stream,
1739 			stream == SNDRV_PCM_STREAM_PLAYBACK ?
1740 			&snd_usb_playback_ops : &snd_usb_capture_ops);
1741 }
1742