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