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