1 /*
2 * u_audio.c -- interface to USB gadget "ALSA sound card" utilities
3 *
4 * Copyright (C) 2016
5 * Author: Ruslan Bilovol <ruslan.bilovol@gmail.com>
6 *
7 * Sound card implementation was cut-and-pasted with changes
8 * from f_uac2.c and has:
9 * Copyright (C) 2011
10 * Yadwinder Singh (yadi.brar01@gmail.com)
11 * Jaswinder Singh (jaswinder.singh@linaro.org)
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24 #include <linux/module.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28
29 #include "u_audio.h"
30
31 #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
32 #define PRD_SIZE_MAX PAGE_SIZE
33 #define MIN_PERIODS 4
34
35 struct uac_req {
36 struct uac_rtd_params *pp; /* parent param */
37 struct usb_request *req;
38 };
39
40 /* Runtime data params for one stream */
41 struct uac_rtd_params {
42 struct snd_uac_chip *uac; /* parent chip */
43 bool ep_enabled; /* if the ep is enabled */
44
45 struct snd_pcm_substream *ss;
46
47 /* Ring buffer */
48 ssize_t hw_ptr;
49
50 void *rbuf;
51
52 unsigned max_psize; /* MaxPacketSize of endpoint */
53 struct uac_req *ureq;
54
55 spinlock_t lock;
56 };
57
58 struct snd_uac_chip {
59 struct g_audio *audio_dev;
60
61 struct uac_rtd_params p_prm;
62 struct uac_rtd_params c_prm;
63
64 struct snd_card *card;
65 struct snd_pcm *pcm;
66
67 /* timekeeping for the playback endpoint */
68 unsigned int p_interval;
69 unsigned int p_residue;
70
71 /* pre-calculated values for playback iso completion */
72 unsigned int p_pktsize;
73 unsigned int p_pktsize_residue;
74 unsigned int p_framesize;
75 };
76
77 static const struct snd_pcm_hardware uac_pcm_hardware = {
78 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
79 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
80 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
81 .rates = SNDRV_PCM_RATE_CONTINUOUS,
82 .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
83 .buffer_bytes_max = BUFF_SIZE_MAX,
84 .period_bytes_max = PRD_SIZE_MAX,
85 .periods_min = MIN_PERIODS,
86 };
87
u_audio_iso_complete(struct usb_ep * ep,struct usb_request * req)88 static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req)
89 {
90 unsigned pending;
91 unsigned long flags, flags2;
92 unsigned int hw_ptr;
93 int status = req->status;
94 struct uac_req *ur = req->context;
95 struct snd_pcm_substream *substream;
96 struct snd_pcm_runtime *runtime;
97 struct uac_rtd_params *prm = ur->pp;
98 struct snd_uac_chip *uac = prm->uac;
99
100 /* i/f shutting down */
101 if (!prm->ep_enabled || req->status == -ESHUTDOWN)
102 return;
103
104 /*
105 * We can't really do much about bad xfers.
106 * Afterall, the ISOCH xfers could fail legitimately.
107 */
108 if (status)
109 pr_debug("%s: iso_complete status(%d) %d/%d\n",
110 __func__, status, req->actual, req->length);
111
112 substream = prm->ss;
113
114 /* Do nothing if ALSA isn't active */
115 if (!substream)
116 goto exit;
117
118 snd_pcm_stream_lock_irqsave(substream, flags2);
119
120 runtime = substream->runtime;
121 if (!runtime || !snd_pcm_running(substream)) {
122 snd_pcm_stream_unlock_irqrestore(substream, flags2);
123 goto exit;
124 }
125
126 spin_lock_irqsave(&prm->lock, flags);
127
128 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
129 /*
130 * For each IN packet, take the quotient of the current data
131 * rate and the endpoint's interval as the base packet size.
132 * If there is a residue from this division, add it to the
133 * residue accumulator.
134 */
135 req->length = uac->p_pktsize;
136 uac->p_residue += uac->p_pktsize_residue;
137
138 /*
139 * Whenever there are more bytes in the accumulator than we
140 * need to add one more sample frame, increase this packet's
141 * size and decrease the accumulator.
142 */
143 if (uac->p_residue / uac->p_interval >= uac->p_framesize) {
144 req->length += uac->p_framesize;
145 uac->p_residue -= uac->p_framesize *
146 uac->p_interval;
147 }
148
149 req->actual = req->length;
150 }
151
152 hw_ptr = prm->hw_ptr;
153
154 spin_unlock_irqrestore(&prm->lock, flags);
155
156 /* Pack USB load in ALSA ring buffer */
157 pending = runtime->dma_bytes - hw_ptr;
158
159 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
160 if (unlikely(pending < req->actual)) {
161 memcpy(req->buf, runtime->dma_area + hw_ptr, pending);
162 memcpy(req->buf + pending, runtime->dma_area,
163 req->actual - pending);
164 } else {
165 memcpy(req->buf, runtime->dma_area + hw_ptr,
166 req->actual);
167 }
168 } else {
169 if (unlikely(pending < req->actual)) {
170 memcpy(runtime->dma_area + hw_ptr, req->buf, pending);
171 memcpy(runtime->dma_area, req->buf + pending,
172 req->actual - pending);
173 } else {
174 memcpy(runtime->dma_area + hw_ptr, req->buf,
175 req->actual);
176 }
177 }
178
179 spin_lock_irqsave(&prm->lock, flags);
180 /* update hw_ptr after data is copied to memory */
181 prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes;
182 hw_ptr = prm->hw_ptr;
183 spin_unlock_irqrestore(&prm->lock, flags);
184 snd_pcm_stream_unlock_irqrestore(substream, flags2);
185
186 if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual)
187 snd_pcm_period_elapsed(substream);
188
189 exit:
190 if (usb_ep_queue(ep, req, GFP_ATOMIC))
191 dev_err(uac->card->dev, "%d Error!\n", __LINE__);
192 }
193
uac_pcm_trigger(struct snd_pcm_substream * substream,int cmd)194 static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
195 {
196 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
197 struct uac_rtd_params *prm;
198 struct g_audio *audio_dev;
199 struct uac_params *params;
200 unsigned long flags;
201 int err = 0;
202
203 audio_dev = uac->audio_dev;
204 params = &audio_dev->params;
205
206 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
207 prm = &uac->p_prm;
208 else
209 prm = &uac->c_prm;
210
211 spin_lock_irqsave(&prm->lock, flags);
212
213 /* Reset */
214 prm->hw_ptr = 0;
215
216 switch (cmd) {
217 case SNDRV_PCM_TRIGGER_START:
218 case SNDRV_PCM_TRIGGER_RESUME:
219 prm->ss = substream;
220 break;
221 case SNDRV_PCM_TRIGGER_STOP:
222 case SNDRV_PCM_TRIGGER_SUSPEND:
223 prm->ss = NULL;
224 break;
225 default:
226 err = -EINVAL;
227 }
228
229 spin_unlock_irqrestore(&prm->lock, flags);
230
231 /* Clear buffer after Play stops */
232 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
233 memset(prm->rbuf, 0, prm->max_psize * params->req_number);
234
235 return err;
236 }
237
uac_pcm_pointer(struct snd_pcm_substream * substream)238 static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream)
239 {
240 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
241 struct uac_rtd_params *prm;
242
243 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
244 prm = &uac->p_prm;
245 else
246 prm = &uac->c_prm;
247
248 return bytes_to_frames(substream->runtime, prm->hw_ptr);
249 }
250
uac_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)251 static int uac_pcm_hw_params(struct snd_pcm_substream *substream,
252 struct snd_pcm_hw_params *hw_params)
253 {
254 return snd_pcm_lib_malloc_pages(substream,
255 params_buffer_bytes(hw_params));
256 }
257
uac_pcm_hw_free(struct snd_pcm_substream * substream)258 static int uac_pcm_hw_free(struct snd_pcm_substream *substream)
259 {
260 return snd_pcm_lib_free_pages(substream);
261 }
262
uac_pcm_open(struct snd_pcm_substream * substream)263 static int uac_pcm_open(struct snd_pcm_substream *substream)
264 {
265 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
266 struct snd_pcm_runtime *runtime = substream->runtime;
267 struct g_audio *audio_dev;
268 struct uac_params *params;
269 int p_ssize, c_ssize;
270 int p_srate, c_srate;
271 int p_chmask, c_chmask;
272
273 audio_dev = uac->audio_dev;
274 params = &audio_dev->params;
275 p_ssize = params->p_ssize;
276 c_ssize = params->c_ssize;
277 p_srate = params->p_srate;
278 c_srate = params->c_srate;
279 p_chmask = params->p_chmask;
280 c_chmask = params->c_chmask;
281 uac->p_residue = 0;
282
283 runtime->hw = uac_pcm_hardware;
284
285 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
286 spin_lock_init(&uac->p_prm.lock);
287 runtime->hw.rate_min = p_srate;
288 switch (p_ssize) {
289 case 3:
290 runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
291 break;
292 case 4:
293 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
294 break;
295 default:
296 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
297 break;
298 }
299 runtime->hw.channels_min = num_channels(p_chmask);
300 runtime->hw.period_bytes_min = 2 * uac->p_prm.max_psize
301 / runtime->hw.periods_min;
302 } else {
303 spin_lock_init(&uac->c_prm.lock);
304 runtime->hw.rate_min = c_srate;
305 switch (c_ssize) {
306 case 3:
307 runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
308 break;
309 case 4:
310 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
311 break;
312 default:
313 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
314 break;
315 }
316 runtime->hw.channels_min = num_channels(c_chmask);
317 runtime->hw.period_bytes_min = 2 * uac->c_prm.max_psize
318 / runtime->hw.periods_min;
319 }
320
321 runtime->hw.rate_max = runtime->hw.rate_min;
322 runtime->hw.channels_max = runtime->hw.channels_min;
323
324 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
325
326 return 0;
327 }
328
329 /* ALSA cries without these function pointers */
uac_pcm_null(struct snd_pcm_substream * substream)330 static int uac_pcm_null(struct snd_pcm_substream *substream)
331 {
332 return 0;
333 }
334
335 static const struct snd_pcm_ops uac_pcm_ops = {
336 .open = uac_pcm_open,
337 .close = uac_pcm_null,
338 .ioctl = snd_pcm_lib_ioctl,
339 .hw_params = uac_pcm_hw_params,
340 .hw_free = uac_pcm_hw_free,
341 .trigger = uac_pcm_trigger,
342 .pointer = uac_pcm_pointer,
343 .prepare = uac_pcm_null,
344 };
345
free_ep(struct uac_rtd_params * prm,struct usb_ep * ep)346 static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep)
347 {
348 struct snd_uac_chip *uac = prm->uac;
349 struct g_audio *audio_dev;
350 struct uac_params *params;
351 int i;
352
353 if (!prm->ep_enabled)
354 return;
355
356 prm->ep_enabled = false;
357
358 audio_dev = uac->audio_dev;
359 params = &audio_dev->params;
360
361 for (i = 0; i < params->req_number; i++) {
362 if (prm->ureq[i].req) {
363 usb_ep_dequeue(ep, prm->ureq[i].req);
364 usb_ep_free_request(ep, prm->ureq[i].req);
365 prm->ureq[i].req = NULL;
366 }
367 }
368
369 if (usb_ep_disable(ep))
370 dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
371 }
372
373
u_audio_start_capture(struct g_audio * audio_dev)374 int u_audio_start_capture(struct g_audio *audio_dev)
375 {
376 struct snd_uac_chip *uac = audio_dev->uac;
377 struct usb_gadget *gadget = audio_dev->gadget;
378 struct device *dev = &gadget->dev;
379 struct usb_request *req;
380 struct usb_ep *ep;
381 struct uac_rtd_params *prm;
382 struct uac_params *params = &audio_dev->params;
383 int req_len, i;
384
385 ep = audio_dev->out_ep;
386 prm = &uac->c_prm;
387 config_ep_by_speed(gadget, &audio_dev->func, ep);
388 req_len = prm->max_psize;
389
390 prm->ep_enabled = true;
391 usb_ep_enable(ep);
392
393 for (i = 0; i < params->req_number; i++) {
394 if (!prm->ureq[i].req) {
395 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
396 if (req == NULL)
397 return -ENOMEM;
398
399 prm->ureq[i].req = req;
400 prm->ureq[i].pp = prm;
401
402 req->zero = 0;
403 req->context = &prm->ureq[i];
404 req->length = req_len;
405 req->complete = u_audio_iso_complete;
406 req->buf = prm->rbuf + i * prm->max_psize;
407 }
408
409 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
410 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
411 }
412
413 return 0;
414 }
415 EXPORT_SYMBOL_GPL(u_audio_start_capture);
416
u_audio_stop_capture(struct g_audio * audio_dev)417 void u_audio_stop_capture(struct g_audio *audio_dev)
418 {
419 struct snd_uac_chip *uac = audio_dev->uac;
420
421 free_ep(&uac->c_prm, audio_dev->out_ep);
422 }
423 EXPORT_SYMBOL_GPL(u_audio_stop_capture);
424
u_audio_start_playback(struct g_audio * audio_dev)425 int u_audio_start_playback(struct g_audio *audio_dev)
426 {
427 struct snd_uac_chip *uac = audio_dev->uac;
428 struct usb_gadget *gadget = audio_dev->gadget;
429 struct device *dev = &gadget->dev;
430 struct usb_request *req;
431 struct usb_ep *ep;
432 struct uac_rtd_params *prm;
433 struct uac_params *params = &audio_dev->params;
434 unsigned int factor, rate;
435 const struct usb_endpoint_descriptor *ep_desc;
436 int req_len, i;
437
438 ep = audio_dev->in_ep;
439 prm = &uac->p_prm;
440 config_ep_by_speed(gadget, &audio_dev->func, ep);
441
442 ep_desc = ep->desc;
443
444 /* pre-calculate the playback endpoint's interval */
445 if (gadget->speed == USB_SPEED_FULL)
446 factor = 1000;
447 else
448 factor = 8000;
449
450 /* pre-compute some values for iso_complete() */
451 uac->p_framesize = params->p_ssize *
452 num_channels(params->p_chmask);
453 rate = params->p_srate * uac->p_framesize;
454 uac->p_interval = factor / (1 << (ep_desc->bInterval - 1));
455 uac->p_pktsize = min_t(unsigned int, rate / uac->p_interval,
456 prm->max_psize);
457
458 if (uac->p_pktsize < prm->max_psize)
459 uac->p_pktsize_residue = rate % uac->p_interval;
460 else
461 uac->p_pktsize_residue = 0;
462
463 req_len = uac->p_pktsize;
464 uac->p_residue = 0;
465
466 prm->ep_enabled = true;
467 usb_ep_enable(ep);
468
469 for (i = 0; i < params->req_number; i++) {
470 if (!prm->ureq[i].req) {
471 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
472 if (req == NULL)
473 return -ENOMEM;
474
475 prm->ureq[i].req = req;
476 prm->ureq[i].pp = prm;
477
478 req->zero = 0;
479 req->context = &prm->ureq[i];
480 req->length = req_len;
481 req->complete = u_audio_iso_complete;
482 req->buf = prm->rbuf + i * prm->max_psize;
483 }
484
485 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
486 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
487 }
488
489 return 0;
490 }
491 EXPORT_SYMBOL_GPL(u_audio_start_playback);
492
u_audio_stop_playback(struct g_audio * audio_dev)493 void u_audio_stop_playback(struct g_audio *audio_dev)
494 {
495 struct snd_uac_chip *uac = audio_dev->uac;
496
497 free_ep(&uac->p_prm, audio_dev->in_ep);
498 }
499 EXPORT_SYMBOL_GPL(u_audio_stop_playback);
500
g_audio_setup(struct g_audio * g_audio,const char * pcm_name,const char * card_name)501 int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
502 const char *card_name)
503 {
504 struct snd_uac_chip *uac;
505 struct snd_card *card;
506 struct snd_pcm *pcm;
507 struct uac_params *params;
508 int p_chmask, c_chmask;
509 int err;
510
511 if (!g_audio)
512 return -EINVAL;
513
514 uac = kzalloc(sizeof(*uac), GFP_KERNEL);
515 if (!uac)
516 return -ENOMEM;
517 g_audio->uac = uac;
518 uac->audio_dev = g_audio;
519
520 params = &g_audio->params;
521 p_chmask = params->p_chmask;
522 c_chmask = params->c_chmask;
523
524 if (c_chmask) {
525 struct uac_rtd_params *prm = &uac->c_prm;
526
527 uac->c_prm.uac = uac;
528 prm->max_psize = g_audio->out_ep_maxpsize;
529
530 prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req),
531 GFP_KERNEL);
532 if (!prm->ureq) {
533 err = -ENOMEM;
534 goto fail;
535 }
536
537 prm->rbuf = kcalloc(params->req_number, prm->max_psize,
538 GFP_KERNEL);
539 if (!prm->rbuf) {
540 prm->max_psize = 0;
541 err = -ENOMEM;
542 goto fail;
543 }
544 }
545
546 if (p_chmask) {
547 struct uac_rtd_params *prm = &uac->p_prm;
548
549 uac->p_prm.uac = uac;
550 prm->max_psize = g_audio->in_ep_maxpsize;
551
552 prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req),
553 GFP_KERNEL);
554 if (!prm->ureq) {
555 err = -ENOMEM;
556 goto fail;
557 }
558
559 prm->rbuf = kcalloc(params->req_number, prm->max_psize,
560 GFP_KERNEL);
561 if (!prm->rbuf) {
562 prm->max_psize = 0;
563 err = -ENOMEM;
564 goto fail;
565 }
566 }
567
568 /* Choose any slot, with no id */
569 err = snd_card_new(&g_audio->gadget->dev,
570 -1, NULL, THIS_MODULE, 0, &card);
571 if (err < 0)
572 goto fail;
573
574 uac->card = card;
575
576 /*
577 * Create first PCM device
578 * Create a substream only for non-zero channel streams
579 */
580 err = snd_pcm_new(uac->card, pcm_name, 0,
581 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
582 if (err < 0)
583 goto snd_fail;
584
585 strlcpy(pcm->name, pcm_name, sizeof(pcm->name));
586 pcm->private_data = uac;
587 uac->pcm = pcm;
588
589 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops);
590 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops);
591
592 strlcpy(card->driver, card_name, sizeof(card->driver));
593 strlcpy(card->shortname, card_name, sizeof(card->shortname));
594 sprintf(card->longname, "%s %i", card_name, card->dev->id);
595
596 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
597 snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX);
598
599 err = snd_card_register(card);
600
601 if (!err)
602 return 0;
603
604 snd_fail:
605 snd_card_free(card);
606 fail:
607 kfree(uac->p_prm.ureq);
608 kfree(uac->c_prm.ureq);
609 kfree(uac->p_prm.rbuf);
610 kfree(uac->c_prm.rbuf);
611 kfree(uac);
612
613 return err;
614 }
615 EXPORT_SYMBOL_GPL(g_audio_setup);
616
g_audio_cleanup(struct g_audio * g_audio)617 void g_audio_cleanup(struct g_audio *g_audio)
618 {
619 struct snd_uac_chip *uac;
620 struct snd_card *card;
621
622 if (!g_audio || !g_audio->uac)
623 return;
624
625 uac = g_audio->uac;
626 card = uac->card;
627 if (card)
628 snd_card_free(card);
629
630 kfree(uac->p_prm.ureq);
631 kfree(uac->c_prm.ureq);
632 kfree(uac->p_prm.rbuf);
633 kfree(uac->c_prm.rbuf);
634 kfree(uac);
635 }
636 EXPORT_SYMBOL_GPL(g_audio_cleanup);
637
638 MODULE_LICENSE("GPL");
639 MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities");
640 MODULE_AUTHOR("Ruslan Bilovol");
641