Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * u_audio.c -- interface to USB gadget "ALSA sound card" utilities
  4 *
  5 * Copyright (C) 2016
  6 * Author: Ruslan Bilovol <ruslan.bilovol@gmail.com>
  7 *
  8 * Sound card implementation was cut-and-pasted with changes
  9 * from f_uac2.c and has:
 10 *    Copyright (C) 2011
 11 *    Yadwinder Singh (yadi.brar01@gmail.com)
 12 *    Jaswinder Singh (jaswinder.singh@linaro.org)
 13 */
 14
 15#include <linux/module.h>
 16#include <sound/core.h>
 17#include <sound/pcm.h>
 18#include <sound/pcm_params.h>
 19#include <sound/control.h>
 20
 21#include "u_audio.h"
 22
 23#define BUFF_SIZE_MAX	(PAGE_SIZE * 16)
 24#define PRD_SIZE_MAX	PAGE_SIZE
 25#define MIN_PERIODS	4
 26
 27/* Runtime data params for one stream */
 28struct uac_rtd_params {
 29	struct snd_uac_chip *uac; /* parent chip */
 30	bool ep_enabled; /* if the ep is enabled */
 31
 32	struct snd_pcm_substream *ss;
 33
 34	/* Ring buffer */
 35	ssize_t hw_ptr;
 36
 37	void *rbuf;
 38
 39	unsigned int pitch;	/* Stream pitch ratio to 1000000 */
 40	unsigned int max_psize;	/* MaxPacketSize of endpoint */
 41
 42	struct usb_request **reqs;
 43
 44	struct usb_request *req_fback; /* Feedback endpoint request */
 45	bool fb_ep_enabled; /* if the ep is enabled */
 46};
 47
 48struct snd_uac_chip {
 49	struct g_audio *audio_dev;
 50
 51	struct uac_rtd_params p_prm;
 52	struct uac_rtd_params c_prm;
 53
 54	struct snd_card *card;
 55	struct snd_pcm *pcm;
 56
 57	/* timekeeping for the playback endpoint */
 58	unsigned int p_interval;
 59	unsigned int p_residue;
 60
 61	/* pre-calculated values for playback iso completion */
 62	unsigned int p_pktsize;
 63	unsigned int p_pktsize_residue;
 64	unsigned int p_framesize;
 65};
 66
 67static const struct snd_pcm_hardware uac_pcm_hardware = {
 68	.info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
 69		 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
 70		 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
 71	.rates = SNDRV_PCM_RATE_CONTINUOUS,
 72	.periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
 73	.buffer_bytes_max = BUFF_SIZE_MAX,
 74	.period_bytes_max = PRD_SIZE_MAX,
 75	.periods_min = MIN_PERIODS,
 76};
 77
 78static void u_audio_set_fback_frequency(enum usb_device_speed speed,
 79					struct usb_ep *out_ep,
 80					unsigned long long freq,
 81					unsigned int pitch,
 82					void *buf)
 83{
 84	u32 ff = 0;
 85	const struct usb_endpoint_descriptor *ep_desc;
 86
 87	/*
 88	 * Because the pitch base is 1000000, the final divider here
 89	 * will be 1000 * 1000000 = 1953125 << 9
 90	 *
 91	 * Instead of dealing with big numbers lets fold this 9 left shift
 92	 */
 93
 94	if (speed == USB_SPEED_FULL) {
 95		/*
 96		 * Full-speed feedback endpoints report frequency
 97		 * in samples/frame
 98		 * Format is encoded in Q10.10 left-justified in the 24 bits,
 99		 * so that it has a Q10.14 format.
100		 *
101		 * ff = (freq << 14) / 1000
102		 */
103		freq <<= 5;
104	} else {
105		/*
106		 * High-speed feedback endpoints report frequency
107		 * in samples/microframe.
108		 * Format is encoded in Q12.13 fitted into four bytes so that
109		 * the binary point is located between the second and the third
110		 * byte fromat (that is Q16.16)
111		 *
112		 * ff = (freq << 16) / 8000
113		 *
114		 * Win10 and OSX UAC2 drivers require number of samples per packet
115		 * in order to honor the feedback value.
116		 * Linux snd-usb-audio detects the applied bit-shift automatically.
117		 */
118		ep_desc = out_ep->desc;
119		freq <<= 4 + (ep_desc->bInterval - 1);
120	}
121
122	ff = DIV_ROUND_CLOSEST_ULL((freq * pitch), 1953125);
123
124	*(__le32 *)buf = cpu_to_le32(ff);
125}
126
127static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req)
128{
129	unsigned int pending;
130	unsigned int hw_ptr;
131	int status = req->status;
132	struct snd_pcm_substream *substream;
133	struct snd_pcm_runtime *runtime;
134	struct uac_rtd_params *prm = req->context;
135	struct snd_uac_chip *uac = prm->uac;
136
137	/* i/f shutting down */
138	if (!prm->ep_enabled) {
139		usb_ep_free_request(ep, req);
140		return;
141	}
142
143	if (req->status == -ESHUTDOWN)
144		return;
145
146	/*
147	 * We can't really do much about bad xfers.
148	 * Afterall, the ISOCH xfers could fail legitimately.
149	 */
150	if (status)
151		pr_debug("%s: iso_complete status(%d) %d/%d\n",
152			__func__, status, req->actual, req->length);
153
154	substream = prm->ss;
155
156	/* Do nothing if ALSA isn't active */
157	if (!substream)
158		goto exit;
159
160	snd_pcm_stream_lock(substream);
161
162	runtime = substream->runtime;
163	if (!runtime || !snd_pcm_running(substream)) {
164		snd_pcm_stream_unlock(substream);
165		goto exit;
166	}
167
168	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
169		/*
170		 * For each IN packet, take the quotient of the current data
171		 * rate and the endpoint's interval as the base packet size.
172		 * If there is a residue from this division, add it to the
173		 * residue accumulator.
174		 */
175		req->length = uac->p_pktsize;
176		uac->p_residue += uac->p_pktsize_residue;
177
178		/*
179		 * Whenever there are more bytes in the accumulator than we
180		 * need to add one more sample frame, increase this packet's
181		 * size and decrease the accumulator.
182		 */
183		if (uac->p_residue / uac->p_interval >= uac->p_framesize) {
184			req->length += uac->p_framesize;
185			uac->p_residue -= uac->p_framesize *
186					   uac->p_interval;
187		}
188
189		req->actual = req->length;
190	}
191
192	hw_ptr = prm->hw_ptr;
193
194	/* Pack USB load in ALSA ring buffer */
195	pending = runtime->dma_bytes - hw_ptr;
196
197	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
198		if (unlikely(pending < req->actual)) {
199			memcpy(req->buf, runtime->dma_area + hw_ptr, pending);
200			memcpy(req->buf + pending, runtime->dma_area,
201			       req->actual - pending);
202		} else {
203			memcpy(req->buf, runtime->dma_area + hw_ptr,
204			       req->actual);
205		}
206	} else {
207		if (unlikely(pending < req->actual)) {
208			memcpy(runtime->dma_area + hw_ptr, req->buf, pending);
209			memcpy(runtime->dma_area, req->buf + pending,
210			       req->actual - pending);
211		} else {
212			memcpy(runtime->dma_area + hw_ptr, req->buf,
213			       req->actual);
214		}
215	}
216
217	/* update hw_ptr after data is copied to memory */
218	prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes;
219	hw_ptr = prm->hw_ptr;
220	snd_pcm_stream_unlock(substream);
221
222	if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual)
223		snd_pcm_period_elapsed(substream);
224
225exit:
226	if (usb_ep_queue(ep, req, GFP_ATOMIC))
227		dev_err(uac->card->dev, "%d Error!\n", __LINE__);
228}
229
230static void u_audio_iso_fback_complete(struct usb_ep *ep,
231				       struct usb_request *req)
232{
233	struct uac_rtd_params *prm = req->context;
234	struct snd_uac_chip *uac = prm->uac;
235	struct g_audio *audio_dev = uac->audio_dev;
236	struct uac_params *params = &audio_dev->params;
237	int status = req->status;
238
239	/* i/f shutting down */
240	if (!prm->fb_ep_enabled) {
241		kfree(req->buf);
242		usb_ep_free_request(ep, req);
243		return;
244	}
245
246	if (req->status == -ESHUTDOWN)
247		return;
248
249	/*
250	 * We can't really do much about bad xfers.
251	 * Afterall, the ISOCH xfers could fail legitimately.
252	 */
253	if (status)
254		pr_debug("%s: iso_complete status(%d) %d/%d\n",
255			__func__, status, req->actual, req->length);
256
257	u_audio_set_fback_frequency(audio_dev->gadget->speed, audio_dev->out_ep,
258				    params->c_srate, prm->pitch,
259				    req->buf);
260
261	if (usb_ep_queue(ep, req, GFP_ATOMIC))
262		dev_err(uac->card->dev, "%d Error!\n", __LINE__);
263}
264
265static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
266{
267	struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
268	struct uac_rtd_params *prm;
269	struct g_audio *audio_dev;
270	struct uac_params *params;
271	int err = 0;
272
273	audio_dev = uac->audio_dev;
274	params = &audio_dev->params;
275
276	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
277		prm = &uac->p_prm;
278	else
279		prm = &uac->c_prm;
280
281	/* Reset */
282	prm->hw_ptr = 0;
283
284	switch (cmd) {
285	case SNDRV_PCM_TRIGGER_START:
286	case SNDRV_PCM_TRIGGER_RESUME:
287		prm->ss = substream;
288		break;
289	case SNDRV_PCM_TRIGGER_STOP:
290	case SNDRV_PCM_TRIGGER_SUSPEND:
291		prm->ss = NULL;
292		break;
293	default:
294		err = -EINVAL;
295	}
296
297	/* Clear buffer after Play stops */
298	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
299		memset(prm->rbuf, 0, prm->max_psize * params->req_number);
300
301	return err;
302}
303
304static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream)
305{
306	struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
307	struct uac_rtd_params *prm;
308
309	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
310		prm = &uac->p_prm;
311	else
312		prm = &uac->c_prm;
313
314	return bytes_to_frames(substream->runtime, prm->hw_ptr);
315}
316
317static u64 uac_ssize_to_fmt(int ssize)
318{
319	u64 ret;
320
321	switch (ssize) {
322	case 3:
323		ret = SNDRV_PCM_FMTBIT_S24_3LE;
324		break;
325	case 4:
326		ret = SNDRV_PCM_FMTBIT_S32_LE;
327		break;
328	default:
329		ret = SNDRV_PCM_FMTBIT_S16_LE;
330		break;
331	}
332
333	return ret;
334}
335
336static int uac_pcm_open(struct snd_pcm_substream *substream)
337{
338	struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
339	struct snd_pcm_runtime *runtime = substream->runtime;
340	struct g_audio *audio_dev;
341	struct uac_params *params;
342	int p_ssize, c_ssize;
343	int p_srate, c_srate;
344	int p_chmask, c_chmask;
345
346	audio_dev = uac->audio_dev;
347	params = &audio_dev->params;
348	p_ssize = params->p_ssize;
349	c_ssize = params->c_ssize;
350	p_srate = params->p_srate;
351	c_srate = params->c_srate;
352	p_chmask = params->p_chmask;
353	c_chmask = params->c_chmask;
354	uac->p_residue = 0;
355
356	runtime->hw = uac_pcm_hardware;
357
358	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
359		runtime->hw.rate_min = p_srate;
360		runtime->hw.formats = uac_ssize_to_fmt(p_ssize);
361		runtime->hw.channels_min = num_channels(p_chmask);
362		runtime->hw.period_bytes_min = 2 * uac->p_prm.max_psize
363						/ runtime->hw.periods_min;
364	} else {
365		runtime->hw.rate_min = c_srate;
366		runtime->hw.formats = uac_ssize_to_fmt(c_ssize);
367		runtime->hw.channels_min = num_channels(c_chmask);
368		runtime->hw.period_bytes_min = 2 * uac->c_prm.max_psize
369						/ runtime->hw.periods_min;
370	}
371
372	runtime->hw.rate_max = runtime->hw.rate_min;
373	runtime->hw.channels_max = runtime->hw.channels_min;
374
375	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
376
377	return 0;
378}
379
380/* ALSA cries without these function pointers */
381static int uac_pcm_null(struct snd_pcm_substream *substream)
382{
383	return 0;
384}
385
386static const struct snd_pcm_ops uac_pcm_ops = {
387	.open = uac_pcm_open,
388	.close = uac_pcm_null,
389	.trigger = uac_pcm_trigger,
390	.pointer = uac_pcm_pointer,
391	.prepare = uac_pcm_null,
392};
393
394static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep)
395{
396	struct snd_uac_chip *uac = prm->uac;
397	struct g_audio *audio_dev;
398	struct uac_params *params;
399	int i;
400
401	if (!prm->ep_enabled)
402		return;
403
404	audio_dev = uac->audio_dev;
405	params = &audio_dev->params;
406
407	for (i = 0; i < params->req_number; i++) {
408		if (prm->reqs[i]) {
409			if (usb_ep_dequeue(ep, prm->reqs[i]))
410				usb_ep_free_request(ep, prm->reqs[i]);
411			/*
412			 * If usb_ep_dequeue() cannot successfully dequeue the
413			 * request, the request will be freed by the completion
414			 * callback.
415			 */
416
417			prm->reqs[i] = NULL;
418		}
419	}
420
421	prm->ep_enabled = false;
422
423	if (usb_ep_disable(ep))
424		dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
425}
426
427static inline void free_ep_fback(struct uac_rtd_params *prm, struct usb_ep *ep)
428{
429	struct snd_uac_chip *uac = prm->uac;
430
431	if (!prm->fb_ep_enabled)
432		return;
433
434	if (prm->req_fback) {
435		if (usb_ep_dequeue(ep, prm->req_fback)) {
436			kfree(prm->req_fback->buf);
437			usb_ep_free_request(ep, prm->req_fback);
438		}
439		prm->req_fback = NULL;
440	}
441
442	prm->fb_ep_enabled = false;
443
444	if (usb_ep_disable(ep))
445		dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
446}
447
448int u_audio_start_capture(struct g_audio *audio_dev)
449{
450	struct snd_uac_chip *uac = audio_dev->uac;
451	struct usb_gadget *gadget = audio_dev->gadget;
452	struct device *dev = &gadget->dev;
453	struct usb_request *req, *req_fback;
454	struct usb_ep *ep, *ep_fback;
455	struct uac_rtd_params *prm;
456	struct uac_params *params = &audio_dev->params;
457	int req_len, i;
458
459	ep = audio_dev->out_ep;
460	prm = &uac->c_prm;
461	config_ep_by_speed(gadget, &audio_dev->func, ep);
462	req_len = ep->maxpacket;
463
464	prm->ep_enabled = true;
465	usb_ep_enable(ep);
466
467	for (i = 0; i < params->req_number; i++) {
468		if (!prm->reqs[i]) {
469			req = usb_ep_alloc_request(ep, GFP_ATOMIC);
470			if (req == NULL)
471				return -ENOMEM;
472
473			prm->reqs[i] = req;
474
475			req->zero = 0;
476			req->context = prm;
477			req->length = req_len;
478			req->complete = u_audio_iso_complete;
479			req->buf = prm->rbuf + i * ep->maxpacket;
480		}
481
482		if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC))
483			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
484	}
485
486	ep_fback = audio_dev->in_ep_fback;
487	if (!ep_fback)
488		return 0;
489
490	/* Setup feedback endpoint */
491	config_ep_by_speed(gadget, &audio_dev->func, ep_fback);
492	prm->fb_ep_enabled = true;
493	usb_ep_enable(ep_fback);
494	req_len = ep_fback->maxpacket;
495
496	req_fback = usb_ep_alloc_request(ep_fback, GFP_ATOMIC);
497	if (req_fback == NULL)
498		return -ENOMEM;
499
500	prm->req_fback = req_fback;
501	req_fback->zero = 0;
502	req_fback->context = prm;
503	req_fback->length = req_len;
504	req_fback->complete = u_audio_iso_fback_complete;
505
506	req_fback->buf = kzalloc(req_len, GFP_ATOMIC);
507	if (!req_fback->buf)
508		return -ENOMEM;
509
510	/*
511	 * Configure the feedback endpoint's reported frequency.
512	 * Always start with original frequency since its deviation can't
513	 * be meauserd at start of playback
514	 */
515	prm->pitch = 1000000;
516	u_audio_set_fback_frequency(audio_dev->gadget->speed, ep,
517				    params->c_srate, prm->pitch,
518				    req_fback->buf);
519
520	if (usb_ep_queue(ep_fback, req_fback, GFP_ATOMIC))
521		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
522
523	return 0;
524}
525EXPORT_SYMBOL_GPL(u_audio_start_capture);
526
527void u_audio_stop_capture(struct g_audio *audio_dev)
528{
529	struct snd_uac_chip *uac = audio_dev->uac;
530
531	if (audio_dev->in_ep_fback)
532		free_ep_fback(&uac->c_prm, audio_dev->in_ep_fback);
533	free_ep(&uac->c_prm, audio_dev->out_ep);
534}
535EXPORT_SYMBOL_GPL(u_audio_stop_capture);
536
537int u_audio_start_playback(struct g_audio *audio_dev)
538{
539	struct snd_uac_chip *uac = audio_dev->uac;
540	struct usb_gadget *gadget = audio_dev->gadget;
541	struct device *dev = &gadget->dev;
542	struct usb_request *req;
543	struct usb_ep *ep;
544	struct uac_rtd_params *prm;
545	struct uac_params *params = &audio_dev->params;
546	unsigned int factor;
547	const struct usb_endpoint_descriptor *ep_desc;
548	int req_len, i;
549
550	ep = audio_dev->in_ep;
551	prm = &uac->p_prm;
552	config_ep_by_speed(gadget, &audio_dev->func, ep);
553
554	ep_desc = ep->desc;
555
556	/* pre-calculate the playback endpoint's interval */
557	if (gadget->speed == USB_SPEED_FULL)
558		factor = 1000;
559	else
560		factor = 8000;
561
562	/* pre-compute some values for iso_complete() */
563	uac->p_framesize = params->p_ssize *
564			    num_channels(params->p_chmask);
565	uac->p_interval = factor / (1 << (ep_desc->bInterval - 1));
566	uac->p_pktsize = min_t(unsigned int,
567				uac->p_framesize *
568					(params->p_srate / uac->p_interval),
569				ep->maxpacket);
570
571	if (uac->p_pktsize < ep->maxpacket)
572		uac->p_pktsize_residue = uac->p_framesize *
573			(params->p_srate % uac->p_interval);
574	else
575		uac->p_pktsize_residue = 0;
576
577	req_len = uac->p_pktsize;
578	uac->p_residue = 0;
579
580	prm->ep_enabled = true;
581	usb_ep_enable(ep);
582
583	for (i = 0; i < params->req_number; i++) {
584		if (!prm->reqs[i]) {
585			req = usb_ep_alloc_request(ep, GFP_ATOMIC);
586			if (req == NULL)
587				return -ENOMEM;
588
589			prm->reqs[i] = req;
590
591			req->zero = 0;
592			req->context = prm;
593			req->length = req_len;
594			req->complete = u_audio_iso_complete;
595			req->buf = prm->rbuf + i * ep->maxpacket;
596		}
597
598		if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC))
599			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
600	}
601
602	return 0;
603}
604EXPORT_SYMBOL_GPL(u_audio_start_playback);
605
606void u_audio_stop_playback(struct g_audio *audio_dev)
607{
608	struct snd_uac_chip *uac = audio_dev->uac;
609
610	free_ep(&uac->p_prm, audio_dev->in_ep);
611}
612EXPORT_SYMBOL_GPL(u_audio_stop_playback);
613
614static int u_audio_pitch_info(struct snd_kcontrol *kcontrol,
615				   struct snd_ctl_elem_info *uinfo)
616{
617	struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
618	struct snd_uac_chip *uac = prm->uac;
619	struct g_audio *audio_dev = uac->audio_dev;
620	struct uac_params *params = &audio_dev->params;
621	unsigned int pitch_min, pitch_max;
622
623	pitch_min = (1000 - FBACK_SLOW_MAX) * 1000;
624	pitch_max = (1000 + params->fb_max) * 1000;
625
626	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
627	uinfo->count = 1;
628	uinfo->value.integer.min = pitch_min;
629	uinfo->value.integer.max = pitch_max;
630	uinfo->value.integer.step = 1;
631	return 0;
632}
633
634static int u_audio_pitch_get(struct snd_kcontrol *kcontrol,
635				   struct snd_ctl_elem_value *ucontrol)
636{
637	struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
638
639	ucontrol->value.integer.value[0] = prm->pitch;
640
641	return 0;
642}
643
644static int u_audio_pitch_put(struct snd_kcontrol *kcontrol,
645				  struct snd_ctl_elem_value *ucontrol)
646{
647	struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
648	struct snd_uac_chip *uac = prm->uac;
649	struct g_audio *audio_dev = uac->audio_dev;
650	struct uac_params *params = &audio_dev->params;
651	unsigned int val;
652	unsigned int pitch_min, pitch_max;
653	int change = 0;
654
655	pitch_min = (1000 - FBACK_SLOW_MAX) * 1000;
656	pitch_max = (1000 + params->fb_max) * 1000;
657
658	val = ucontrol->value.integer.value[0];
659
660	if (val < pitch_min)
661		val = pitch_min;
662	if (val > pitch_max)
663		val = pitch_max;
664
665	if (prm->pitch != val) {
666		prm->pitch = val;
667		change = 1;
668	}
669
670	return change;
671}
672
673static const struct snd_kcontrol_new u_audio_controls[]  = {
674{
675	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
676	.name =         "Capture Pitch 1000000",
677	.info =         u_audio_pitch_info,
678	.get =          u_audio_pitch_get,
679	.put =          u_audio_pitch_put,
680},
681};
682
683int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
684					const char *card_name)
685{
686	struct snd_uac_chip *uac;
687	struct snd_card *card;
688	struct snd_pcm *pcm;
689	struct snd_kcontrol *kctl;
690	struct uac_params *params;
691	int p_chmask, c_chmask;
692	int err;
693
694	if (!g_audio)
695		return -EINVAL;
696
697	uac = kzalloc(sizeof(*uac), GFP_KERNEL);
698	if (!uac)
699		return -ENOMEM;
700	g_audio->uac = uac;
701	uac->audio_dev = g_audio;
702
703	params = &g_audio->params;
704	p_chmask = params->p_chmask;
705	c_chmask = params->c_chmask;
706
707	if (c_chmask) {
708		struct uac_rtd_params *prm = &uac->c_prm;
709
710		uac->c_prm.uac = uac;
711		prm->max_psize = g_audio->out_ep_maxpsize;
712
713		prm->reqs = kcalloc(params->req_number,
714				    sizeof(struct usb_request *),
715				    GFP_KERNEL);
716		if (!prm->reqs) {
717			err = -ENOMEM;
718			goto fail;
719		}
720
721		prm->rbuf = kcalloc(params->req_number, prm->max_psize,
722				GFP_KERNEL);
723		if (!prm->rbuf) {
724			prm->max_psize = 0;
725			err = -ENOMEM;
726			goto fail;
727		}
728	}
729
730	if (p_chmask) {
731		struct uac_rtd_params *prm = &uac->p_prm;
732
733		uac->p_prm.uac = uac;
734		prm->max_psize = g_audio->in_ep_maxpsize;
735
736		prm->reqs = kcalloc(params->req_number,
737				    sizeof(struct usb_request *),
738				    GFP_KERNEL);
739		if (!prm->reqs) {
740			err = -ENOMEM;
741			goto fail;
742		}
743
744		prm->rbuf = kcalloc(params->req_number, prm->max_psize,
745				GFP_KERNEL);
746		if (!prm->rbuf) {
747			prm->max_psize = 0;
748			err = -ENOMEM;
749			goto fail;
750		}
751	}
752
753	/* Choose any slot, with no id */
754	err = snd_card_new(&g_audio->gadget->dev,
755			-1, NULL, THIS_MODULE, 0, &card);
756	if (err < 0)
757		goto fail;
758
759	uac->card = card;
760
761	/*
762	 * Create first PCM device
763	 * Create a substream only for non-zero channel streams
764	 */
765	err = snd_pcm_new(uac->card, pcm_name, 0,
766			       p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
767	if (err < 0)
768		goto snd_fail;
769
770	strscpy(pcm->name, pcm_name, sizeof(pcm->name));
771	pcm->private_data = uac;
772	uac->pcm = pcm;
773
774	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops);
775	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops);
776
777	if (c_chmask && g_audio->in_ep_fback) {
778		strscpy(card->mixername, card_name, sizeof(card->driver));
779
780		kctl = snd_ctl_new1(&u_audio_controls[0], &uac->c_prm);
781		if (!kctl) {
782			err = -ENOMEM;
783			goto snd_fail;
784		}
785
786		kctl->id.device = pcm->device;
787		kctl->id.subdevice = 0;
788
789		err = snd_ctl_add(card, kctl);
790		if (err < 0)
791			goto snd_fail;
792	}
793
794	strscpy(card->driver, card_name, sizeof(card->driver));
795	strscpy(card->shortname, card_name, sizeof(card->shortname));
796	sprintf(card->longname, "%s %i", card_name, card->dev->id);
797
798	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
799				       NULL, 0, BUFF_SIZE_MAX);
800
801	err = snd_card_register(card);
802
803	if (!err)
804		return 0;
805
806snd_fail:
807	snd_card_free(card);
808fail:
809	kfree(uac->p_prm.reqs);
810	kfree(uac->c_prm.reqs);
811	kfree(uac->p_prm.rbuf);
812	kfree(uac->c_prm.rbuf);
813	kfree(uac);
814
815	return err;
816}
817EXPORT_SYMBOL_GPL(g_audio_setup);
818
819void g_audio_cleanup(struct g_audio *g_audio)
820{
821	struct snd_uac_chip *uac;
822	struct snd_card *card;
823
824	if (!g_audio || !g_audio->uac)
825		return;
826
827	uac = g_audio->uac;
828	card = uac->card;
829	if (card)
830		snd_card_free(card);
831
832	kfree(uac->p_prm.reqs);
833	kfree(uac->c_prm.reqs);
834	kfree(uac->p_prm.rbuf);
835	kfree(uac->c_prm.rbuf);
836	kfree(uac);
837}
838EXPORT_SYMBOL_GPL(g_audio_cleanup);
839
840MODULE_LICENSE("GPL");
841MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities");
842MODULE_AUTHOR("Ruslan Bilovol");