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/kernel.h> 16 #include <linux/module.h> 17 #include <sound/core.h> 18 #include <sound/pcm.h> 19 #include <sound/pcm_params.h> 20 #include <sound/control.h> 21 #include <sound/tlv.h> 22 #include <linux/usb/audio.h> 23 24 #include "u_audio.h" 25 26 #define BUFF_SIZE_MAX (PAGE_SIZE * 16) 27 #define PRD_SIZE_MAX PAGE_SIZE 28 #define MIN_PERIODS 4 29 30 enum { 31 UAC_FBACK_CTRL, 32 UAC_P_PITCH_CTRL, 33 UAC_MUTE_CTRL, 34 UAC_VOLUME_CTRL, 35 UAC_RATE_CTRL, 36 }; 37 38 /* Runtime data params for one stream */ 39 struct uac_rtd_params { 40 struct snd_uac_chip *uac; /* parent chip */ 41 bool ep_enabled; /* if the ep is enabled */ 42 43 struct snd_pcm_substream *ss; 44 45 /* Ring buffer */ 46 ssize_t hw_ptr; 47 48 void *rbuf; 49 50 unsigned int pitch; /* Stream pitch ratio to 1000000 */ 51 unsigned int max_psize; /* MaxPacketSize of endpoint */ 52 53 struct usb_request **reqs; 54 55 struct usb_request *req_fback; /* Feedback endpoint request */ 56 bool fb_ep_enabled; /* if the ep is enabled */ 57 58 /* Volume/Mute controls and their state */ 59 int fu_id; /* Feature Unit ID */ 60 struct snd_ctl_elem_id snd_kctl_volume_id; 61 struct snd_ctl_elem_id snd_kctl_mute_id; 62 s16 volume_min, volume_max, volume_res; 63 s16 volume; 64 int mute; 65 66 struct snd_ctl_elem_id snd_kctl_rate_id; /* read-only current rate */ 67 int srate; /* selected samplerate */ 68 int active; /* playback/capture running */ 69 70 spinlock_t lock; /* lock for control transfers */ 71 72 }; 73 74 struct snd_uac_chip { 75 struct g_audio *audio_dev; 76 77 struct uac_rtd_params p_prm; 78 struct uac_rtd_params c_prm; 79 80 struct snd_card *card; 81 struct snd_pcm *pcm; 82 83 /* pre-calculated values for playback iso completion */ 84 unsigned long long p_residue_mil; 85 unsigned int p_interval; 86 unsigned int p_framesize; 87 }; 88 89 static const struct snd_pcm_hardware uac_pcm_hardware = { 90 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER 91 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID 92 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME, 93 .rates = SNDRV_PCM_RATE_CONTINUOUS, 94 .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX, 95 .buffer_bytes_max = BUFF_SIZE_MAX, 96 .period_bytes_max = PRD_SIZE_MAX, 97 .periods_min = MIN_PERIODS, 98 }; 99 100 static void u_audio_set_fback_frequency(enum usb_device_speed speed, 101 struct usb_ep *out_ep, 102 unsigned long long freq, 103 unsigned int pitch, 104 void *buf) 105 { 106 u32 ff = 0; 107 const struct usb_endpoint_descriptor *ep_desc; 108 109 /* 110 * Because the pitch base is 1000000, the final divider here 111 * will be 1000 * 1000000 = 1953125 << 9 112 * 113 * Instead of dealing with big numbers lets fold this 9 left shift 114 */ 115 116 if (speed == USB_SPEED_FULL) { 117 /* 118 * Full-speed feedback endpoints report frequency 119 * in samples/frame 120 * Format is encoded in Q10.10 left-justified in the 24 bits, 121 * so that it has a Q10.14 format. 122 * 123 * ff = (freq << 14) / 1000 124 */ 125 freq <<= 5; 126 } else { 127 /* 128 * High-speed feedback endpoints report frequency 129 * in samples/microframe. 130 * Format is encoded in Q12.13 fitted into four bytes so that 131 * the binary point is located between the second and the third 132 * byte fromat (that is Q16.16) 133 * 134 * ff = (freq << 16) / 8000 135 * 136 * Win10 and OSX UAC2 drivers require number of samples per packet 137 * in order to honor the feedback value. 138 * Linux snd-usb-audio detects the applied bit-shift automatically. 139 */ 140 ep_desc = out_ep->desc; 141 freq <<= 4 + (ep_desc->bInterval - 1); 142 } 143 144 ff = DIV_ROUND_CLOSEST_ULL((freq * pitch), 1953125); 145 146 *(__le32 *)buf = cpu_to_le32(ff); 147 } 148 149 static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req) 150 { 151 unsigned int pending; 152 unsigned int hw_ptr; 153 int status = req->status; 154 struct snd_pcm_substream *substream; 155 struct snd_pcm_runtime *runtime; 156 struct uac_rtd_params *prm = req->context; 157 struct snd_uac_chip *uac = prm->uac; 158 unsigned int frames, p_pktsize; 159 unsigned long long pitched_rate_mil, p_pktsize_residue_mil, 160 residue_frames_mil, div_result; 161 162 /* i/f shutting down */ 163 if (!prm->ep_enabled) { 164 usb_ep_free_request(ep, req); 165 return; 166 } 167 168 if (req->status == -ESHUTDOWN) 169 return; 170 171 /* 172 * We can't really do much about bad xfers. 173 * Afterall, the ISOCH xfers could fail legitimately. 174 */ 175 if (status) 176 pr_debug("%s: iso_complete status(%d) %d/%d\n", 177 __func__, status, req->actual, req->length); 178 179 substream = prm->ss; 180 181 /* Do nothing if ALSA isn't active */ 182 if (!substream) 183 goto exit; 184 185 snd_pcm_stream_lock(substream); 186 187 runtime = substream->runtime; 188 if (!runtime || !snd_pcm_running(substream)) { 189 snd_pcm_stream_unlock(substream); 190 goto exit; 191 } 192 193 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 194 /* 195 * For each IN packet, take the quotient of the current data 196 * rate and the endpoint's interval as the base packet size. 197 * If there is a residue from this division, add it to the 198 * residue accumulator. 199 */ 200 unsigned long long p_interval_mil = uac->p_interval * 1000000ULL; 201 202 pitched_rate_mil = (unsigned long long) prm->srate * prm->pitch; 203 div_result = pitched_rate_mil; 204 do_div(div_result, uac->p_interval); 205 do_div(div_result, 1000000); 206 frames = (unsigned int) div_result; 207 208 pr_debug("p_srate %d, pitch %d, interval_mil %llu, frames %d\n", 209 prm->srate, prm->pitch, p_interval_mil, frames); 210 211 p_pktsize = min_t(unsigned int, 212 uac->p_framesize * frames, 213 ep->maxpacket); 214 215 if (p_pktsize < ep->maxpacket) { 216 residue_frames_mil = pitched_rate_mil - frames * p_interval_mil; 217 p_pktsize_residue_mil = uac->p_framesize * residue_frames_mil; 218 } else 219 p_pktsize_residue_mil = 0; 220 221 req->length = p_pktsize; 222 uac->p_residue_mil += p_pktsize_residue_mil; 223 224 /* 225 * Whenever there are more bytes in the accumulator p_residue_mil than we 226 * need to add one more sample frame, increase this packet's 227 * size and decrease the accumulator. 228 */ 229 div_result = uac->p_residue_mil; 230 do_div(div_result, uac->p_interval); 231 do_div(div_result, 1000000); 232 if ((unsigned int) div_result >= uac->p_framesize) { 233 req->length += uac->p_framesize; 234 uac->p_residue_mil -= uac->p_framesize * p_interval_mil; 235 pr_debug("increased req length to %d\n", req->length); 236 } 237 pr_debug("remains uac->p_residue_mil %llu\n", uac->p_residue_mil); 238 239 req->actual = req->length; 240 } 241 242 hw_ptr = prm->hw_ptr; 243 244 /* Pack USB load in ALSA ring buffer */ 245 pending = runtime->dma_bytes - hw_ptr; 246 247 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 248 if (unlikely(pending < req->actual)) { 249 memcpy(req->buf, runtime->dma_area + hw_ptr, pending); 250 memcpy(req->buf + pending, runtime->dma_area, 251 req->actual - pending); 252 } else { 253 memcpy(req->buf, runtime->dma_area + hw_ptr, 254 req->actual); 255 } 256 } else { 257 if (unlikely(pending < req->actual)) { 258 memcpy(runtime->dma_area + hw_ptr, req->buf, pending); 259 memcpy(runtime->dma_area, req->buf + pending, 260 req->actual - pending); 261 } else { 262 memcpy(runtime->dma_area + hw_ptr, req->buf, 263 req->actual); 264 } 265 } 266 267 /* update hw_ptr after data is copied to memory */ 268 prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes; 269 hw_ptr = prm->hw_ptr; 270 snd_pcm_stream_unlock(substream); 271 272 if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual) 273 snd_pcm_period_elapsed(substream); 274 275 exit: 276 if (usb_ep_queue(ep, req, GFP_ATOMIC)) 277 dev_err(uac->card->dev, "%d Error!\n", __LINE__); 278 } 279 280 static void u_audio_iso_fback_complete(struct usb_ep *ep, 281 struct usb_request *req) 282 { 283 struct uac_rtd_params *prm = req->context; 284 struct snd_uac_chip *uac = prm->uac; 285 struct g_audio *audio_dev = uac->audio_dev; 286 int status = req->status; 287 288 /* i/f shutting down */ 289 if (!prm->fb_ep_enabled) { 290 kfree(req->buf); 291 usb_ep_free_request(ep, req); 292 return; 293 } 294 295 if (req->status == -ESHUTDOWN) 296 return; 297 298 /* 299 * We can't really do much about bad xfers. 300 * Afterall, the ISOCH xfers could fail legitimately. 301 */ 302 if (status) 303 pr_debug("%s: iso_complete status(%d) %d/%d\n", 304 __func__, status, req->actual, req->length); 305 306 u_audio_set_fback_frequency(audio_dev->gadget->speed, audio_dev->out_ep, 307 prm->srate, prm->pitch, 308 req->buf); 309 310 if (usb_ep_queue(ep, req, GFP_ATOMIC)) 311 dev_err(uac->card->dev, "%d Error!\n", __LINE__); 312 } 313 314 static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 315 { 316 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 317 struct uac_rtd_params *prm; 318 struct g_audio *audio_dev; 319 struct uac_params *params; 320 int err = 0; 321 322 audio_dev = uac->audio_dev; 323 params = &audio_dev->params; 324 325 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 326 prm = &uac->p_prm; 327 else 328 prm = &uac->c_prm; 329 330 /* Reset */ 331 prm->hw_ptr = 0; 332 333 switch (cmd) { 334 case SNDRV_PCM_TRIGGER_START: 335 case SNDRV_PCM_TRIGGER_RESUME: 336 prm->ss = substream; 337 break; 338 case SNDRV_PCM_TRIGGER_STOP: 339 case SNDRV_PCM_TRIGGER_SUSPEND: 340 prm->ss = NULL; 341 break; 342 default: 343 err = -EINVAL; 344 } 345 346 /* Clear buffer after Play stops */ 347 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss) 348 memset(prm->rbuf, 0, prm->max_psize * params->req_number); 349 350 return err; 351 } 352 353 static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream) 354 { 355 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 356 struct uac_rtd_params *prm; 357 358 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 359 prm = &uac->p_prm; 360 else 361 prm = &uac->c_prm; 362 363 return bytes_to_frames(substream->runtime, prm->hw_ptr); 364 } 365 366 static u64 uac_ssize_to_fmt(int ssize) 367 { 368 u64 ret; 369 370 switch (ssize) { 371 case 3: 372 ret = SNDRV_PCM_FMTBIT_S24_3LE; 373 break; 374 case 4: 375 ret = SNDRV_PCM_FMTBIT_S32_LE; 376 break; 377 default: 378 ret = SNDRV_PCM_FMTBIT_S16_LE; 379 break; 380 } 381 382 return ret; 383 } 384 385 static int uac_pcm_open(struct snd_pcm_substream *substream) 386 { 387 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 388 struct snd_pcm_runtime *runtime = substream->runtime; 389 struct g_audio *audio_dev; 390 struct uac_params *params; 391 struct uac_rtd_params *prm; 392 int p_ssize, c_ssize; 393 int p_chmask, c_chmask; 394 395 audio_dev = uac->audio_dev; 396 params = &audio_dev->params; 397 p_ssize = params->p_ssize; 398 c_ssize = params->c_ssize; 399 p_chmask = params->p_chmask; 400 c_chmask = params->c_chmask; 401 uac->p_residue_mil = 0; 402 403 runtime->hw = uac_pcm_hardware; 404 405 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 406 runtime->hw.formats = uac_ssize_to_fmt(p_ssize); 407 runtime->hw.channels_min = num_channels(p_chmask); 408 prm = &uac->p_prm; 409 } else { 410 runtime->hw.formats = uac_ssize_to_fmt(c_ssize); 411 runtime->hw.channels_min = num_channels(c_chmask); 412 prm = &uac->c_prm; 413 } 414 415 runtime->hw.period_bytes_min = 2 * prm->max_psize 416 / runtime->hw.periods_min; 417 runtime->hw.rate_min = prm->srate; 418 runtime->hw.rate_max = runtime->hw.rate_min; 419 runtime->hw.channels_max = runtime->hw.channels_min; 420 421 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 422 423 return 0; 424 } 425 426 /* ALSA cries without these function pointers */ 427 static int uac_pcm_null(struct snd_pcm_substream *substream) 428 { 429 return 0; 430 } 431 432 static const struct snd_pcm_ops uac_pcm_ops = { 433 .open = uac_pcm_open, 434 .close = uac_pcm_null, 435 .trigger = uac_pcm_trigger, 436 .pointer = uac_pcm_pointer, 437 .prepare = uac_pcm_null, 438 }; 439 440 static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep) 441 { 442 struct snd_uac_chip *uac = prm->uac; 443 struct g_audio *audio_dev; 444 struct uac_params *params; 445 int i; 446 447 if (!prm->ep_enabled) 448 return; 449 450 audio_dev = uac->audio_dev; 451 params = &audio_dev->params; 452 453 for (i = 0; i < params->req_number; i++) { 454 if (prm->reqs[i]) { 455 if (usb_ep_dequeue(ep, prm->reqs[i])) 456 usb_ep_free_request(ep, prm->reqs[i]); 457 /* 458 * If usb_ep_dequeue() cannot successfully dequeue the 459 * request, the request will be freed by the completion 460 * callback. 461 */ 462 463 prm->reqs[i] = NULL; 464 } 465 } 466 467 prm->ep_enabled = false; 468 469 if (usb_ep_disable(ep)) 470 dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__); 471 } 472 473 static inline void free_ep_fback(struct uac_rtd_params *prm, struct usb_ep *ep) 474 { 475 struct snd_uac_chip *uac = prm->uac; 476 477 if (!prm->fb_ep_enabled) 478 return; 479 480 if (prm->req_fback) { 481 if (usb_ep_dequeue(ep, prm->req_fback)) { 482 kfree(prm->req_fback->buf); 483 usb_ep_free_request(ep, prm->req_fback); 484 } 485 prm->req_fback = NULL; 486 } 487 488 prm->fb_ep_enabled = false; 489 490 if (usb_ep_disable(ep)) 491 dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__); 492 } 493 494 static void set_active(struct uac_rtd_params *prm, bool active) 495 { 496 // notifying through the Rate ctrl 497 unsigned long flags; 498 499 spin_lock_irqsave(&prm->lock, flags); 500 if (prm->active != active) { 501 prm->active = active; 502 snd_ctl_notify(prm->uac->card, SNDRV_CTL_EVENT_MASK_VALUE, 503 &prm->snd_kctl_rate_id); 504 } 505 spin_unlock_irqrestore(&prm->lock, flags); 506 } 507 508 int u_audio_set_capture_srate(struct g_audio *audio_dev, int srate) 509 { 510 struct uac_params *params = &audio_dev->params; 511 struct snd_uac_chip *uac = audio_dev->uac; 512 struct uac_rtd_params *prm; 513 int i; 514 unsigned long flags; 515 516 dev_dbg(&audio_dev->gadget->dev, "%s: srate %d\n", __func__, srate); 517 prm = &uac->c_prm; 518 for (i = 0; i < UAC_MAX_RATES; i++) { 519 if (params->c_srates[i] == srate) { 520 spin_lock_irqsave(&prm->lock, flags); 521 prm->srate = srate; 522 spin_unlock_irqrestore(&prm->lock, flags); 523 return 0; 524 } 525 if (params->c_srates[i] == 0) 526 break; 527 } 528 529 return -EINVAL; 530 } 531 EXPORT_SYMBOL_GPL(u_audio_set_capture_srate); 532 533 int u_audio_get_capture_srate(struct g_audio *audio_dev, u32 *val) 534 { 535 struct snd_uac_chip *uac = audio_dev->uac; 536 struct uac_rtd_params *prm; 537 unsigned long flags; 538 539 prm = &uac->c_prm; 540 spin_lock_irqsave(&prm->lock, flags); 541 *val = prm->srate; 542 spin_unlock_irqrestore(&prm->lock, flags); 543 return 0; 544 } 545 EXPORT_SYMBOL_GPL(u_audio_get_capture_srate); 546 547 int u_audio_set_playback_srate(struct g_audio *audio_dev, int srate) 548 { 549 struct uac_params *params = &audio_dev->params; 550 struct snd_uac_chip *uac = audio_dev->uac; 551 struct uac_rtd_params *prm; 552 int i; 553 unsigned long flags; 554 555 dev_dbg(&audio_dev->gadget->dev, "%s: srate %d\n", __func__, srate); 556 prm = &uac->p_prm; 557 for (i = 0; i < UAC_MAX_RATES; i++) { 558 if (params->p_srates[i] == srate) { 559 spin_lock_irqsave(&prm->lock, flags); 560 prm->srate = srate; 561 spin_unlock_irqrestore(&prm->lock, flags); 562 return 0; 563 } 564 if (params->p_srates[i] == 0) 565 break; 566 } 567 568 return -EINVAL; 569 } 570 EXPORT_SYMBOL_GPL(u_audio_set_playback_srate); 571 572 int u_audio_get_playback_srate(struct g_audio *audio_dev, u32 *val) 573 { 574 struct snd_uac_chip *uac = audio_dev->uac; 575 struct uac_rtd_params *prm; 576 unsigned long flags; 577 578 prm = &uac->p_prm; 579 spin_lock_irqsave(&prm->lock, flags); 580 *val = prm->srate; 581 spin_unlock_irqrestore(&prm->lock, flags); 582 return 0; 583 } 584 EXPORT_SYMBOL_GPL(u_audio_get_playback_srate); 585 586 int u_audio_start_capture(struct g_audio *audio_dev) 587 { 588 struct snd_uac_chip *uac = audio_dev->uac; 589 struct usb_gadget *gadget = audio_dev->gadget; 590 struct device *dev = &gadget->dev; 591 struct usb_request *req, *req_fback; 592 struct usb_ep *ep, *ep_fback; 593 struct uac_rtd_params *prm; 594 struct uac_params *params = &audio_dev->params; 595 int req_len, i; 596 597 prm = &uac->c_prm; 598 dev_dbg(dev, "start capture with rate %d\n", prm->srate); 599 ep = audio_dev->out_ep; 600 config_ep_by_speed(gadget, &audio_dev->func, ep); 601 req_len = ep->maxpacket; 602 603 prm->ep_enabled = true; 604 usb_ep_enable(ep); 605 606 for (i = 0; i < params->req_number; i++) { 607 if (!prm->reqs[i]) { 608 req = usb_ep_alloc_request(ep, GFP_ATOMIC); 609 if (req == NULL) 610 return -ENOMEM; 611 612 prm->reqs[i] = req; 613 614 req->zero = 0; 615 req->context = prm; 616 req->length = req_len; 617 req->complete = u_audio_iso_complete; 618 req->buf = prm->rbuf + i * ep->maxpacket; 619 } 620 621 if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC)) 622 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 623 } 624 625 set_active(&uac->c_prm, true); 626 627 ep_fback = audio_dev->in_ep_fback; 628 if (!ep_fback) 629 return 0; 630 631 /* Setup feedback endpoint */ 632 config_ep_by_speed(gadget, &audio_dev->func, ep_fback); 633 prm->fb_ep_enabled = true; 634 usb_ep_enable(ep_fback); 635 req_len = ep_fback->maxpacket; 636 637 req_fback = usb_ep_alloc_request(ep_fback, GFP_ATOMIC); 638 if (req_fback == NULL) 639 return -ENOMEM; 640 641 prm->req_fback = req_fback; 642 req_fback->zero = 0; 643 req_fback->context = prm; 644 req_fback->length = req_len; 645 req_fback->complete = u_audio_iso_fback_complete; 646 647 req_fback->buf = kzalloc(req_len, GFP_ATOMIC); 648 if (!req_fback->buf) 649 return -ENOMEM; 650 651 /* 652 * Configure the feedback endpoint's reported frequency. 653 * Always start with original frequency since its deviation can't 654 * be meauserd at start of playback 655 */ 656 prm->pitch = 1000000; 657 u_audio_set_fback_frequency(audio_dev->gadget->speed, ep, 658 prm->srate, prm->pitch, 659 req_fback->buf); 660 661 if (usb_ep_queue(ep_fback, req_fback, GFP_ATOMIC)) 662 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 663 664 return 0; 665 } 666 EXPORT_SYMBOL_GPL(u_audio_start_capture); 667 668 void u_audio_stop_capture(struct g_audio *audio_dev) 669 { 670 struct snd_uac_chip *uac = audio_dev->uac; 671 672 set_active(&uac->c_prm, false); 673 if (audio_dev->in_ep_fback) 674 free_ep_fback(&uac->c_prm, audio_dev->in_ep_fback); 675 free_ep(&uac->c_prm, audio_dev->out_ep); 676 } 677 EXPORT_SYMBOL_GPL(u_audio_stop_capture); 678 679 int u_audio_start_playback(struct g_audio *audio_dev) 680 { 681 struct snd_uac_chip *uac = audio_dev->uac; 682 struct usb_gadget *gadget = audio_dev->gadget; 683 struct device *dev = &gadget->dev; 684 struct usb_request *req; 685 struct usb_ep *ep; 686 struct uac_rtd_params *prm; 687 struct uac_params *params = &audio_dev->params; 688 unsigned int factor; 689 const struct usb_endpoint_descriptor *ep_desc; 690 int req_len, i; 691 unsigned int p_pktsize; 692 693 prm = &uac->p_prm; 694 dev_dbg(dev, "start playback with rate %d\n", prm->srate); 695 ep = audio_dev->in_ep; 696 config_ep_by_speed(gadget, &audio_dev->func, ep); 697 698 ep_desc = ep->desc; 699 /* 700 * Always start with original frequency 701 */ 702 prm->pitch = 1000000; 703 704 /* pre-calculate the playback endpoint's interval */ 705 if (gadget->speed == USB_SPEED_FULL) 706 factor = 1000; 707 else 708 factor = 8000; 709 710 /* pre-compute some values for iso_complete() */ 711 uac->p_framesize = params->p_ssize * 712 num_channels(params->p_chmask); 713 uac->p_interval = factor / (1 << (ep_desc->bInterval - 1)); 714 p_pktsize = min_t(unsigned int, 715 uac->p_framesize * 716 (prm->srate / uac->p_interval), 717 ep->maxpacket); 718 719 req_len = p_pktsize; 720 uac->p_residue_mil = 0; 721 722 prm->ep_enabled = true; 723 usb_ep_enable(ep); 724 725 for (i = 0; i < params->req_number; i++) { 726 if (!prm->reqs[i]) { 727 req = usb_ep_alloc_request(ep, GFP_ATOMIC); 728 if (req == NULL) 729 return -ENOMEM; 730 731 prm->reqs[i] = req; 732 733 req->zero = 0; 734 req->context = prm; 735 req->length = req_len; 736 req->complete = u_audio_iso_complete; 737 req->buf = prm->rbuf + i * ep->maxpacket; 738 } 739 740 if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC)) 741 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 742 } 743 744 set_active(&uac->p_prm, true); 745 746 return 0; 747 } 748 EXPORT_SYMBOL_GPL(u_audio_start_playback); 749 750 void u_audio_stop_playback(struct g_audio *audio_dev) 751 { 752 struct snd_uac_chip *uac = audio_dev->uac; 753 754 set_active(&uac->p_prm, false); 755 free_ep(&uac->p_prm, audio_dev->in_ep); 756 } 757 EXPORT_SYMBOL_GPL(u_audio_stop_playback); 758 759 void u_audio_suspend(struct g_audio *audio_dev) 760 { 761 struct snd_uac_chip *uac = audio_dev->uac; 762 763 set_active(&uac->p_prm, false); 764 set_active(&uac->c_prm, false); 765 } 766 EXPORT_SYMBOL_GPL(u_audio_suspend); 767 768 int u_audio_get_volume(struct g_audio *audio_dev, int playback, s16 *val) 769 { 770 struct snd_uac_chip *uac = audio_dev->uac; 771 struct uac_rtd_params *prm; 772 unsigned long flags; 773 774 if (playback) 775 prm = &uac->p_prm; 776 else 777 prm = &uac->c_prm; 778 779 spin_lock_irqsave(&prm->lock, flags); 780 *val = prm->volume; 781 spin_unlock_irqrestore(&prm->lock, flags); 782 783 return 0; 784 } 785 EXPORT_SYMBOL_GPL(u_audio_get_volume); 786 787 int u_audio_set_volume(struct g_audio *audio_dev, int playback, s16 val) 788 { 789 struct snd_uac_chip *uac = audio_dev->uac; 790 struct uac_rtd_params *prm; 791 unsigned long flags; 792 int change = 0; 793 794 if (playback) 795 prm = &uac->p_prm; 796 else 797 prm = &uac->c_prm; 798 799 spin_lock_irqsave(&prm->lock, flags); 800 val = clamp(val, prm->volume_min, prm->volume_max); 801 if (prm->volume != val) { 802 prm->volume = val; 803 change = 1; 804 } 805 spin_unlock_irqrestore(&prm->lock, flags); 806 807 if (change) 808 snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE, 809 &prm->snd_kctl_volume_id); 810 811 return 0; 812 } 813 EXPORT_SYMBOL_GPL(u_audio_set_volume); 814 815 int u_audio_get_mute(struct g_audio *audio_dev, int playback, int *val) 816 { 817 struct snd_uac_chip *uac = audio_dev->uac; 818 struct uac_rtd_params *prm; 819 unsigned long flags; 820 821 if (playback) 822 prm = &uac->p_prm; 823 else 824 prm = &uac->c_prm; 825 826 spin_lock_irqsave(&prm->lock, flags); 827 *val = prm->mute; 828 spin_unlock_irqrestore(&prm->lock, flags); 829 830 return 0; 831 } 832 EXPORT_SYMBOL_GPL(u_audio_get_mute); 833 834 int u_audio_set_mute(struct g_audio *audio_dev, int playback, int val) 835 { 836 struct snd_uac_chip *uac = audio_dev->uac; 837 struct uac_rtd_params *prm; 838 unsigned long flags; 839 int change = 0; 840 int mute; 841 842 if (playback) 843 prm = &uac->p_prm; 844 else 845 prm = &uac->c_prm; 846 847 mute = val ? 1 : 0; 848 849 spin_lock_irqsave(&prm->lock, flags); 850 if (prm->mute != mute) { 851 prm->mute = mute; 852 change = 1; 853 } 854 spin_unlock_irqrestore(&prm->lock, flags); 855 856 if (change) 857 snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE, 858 &prm->snd_kctl_mute_id); 859 860 return 0; 861 } 862 EXPORT_SYMBOL_GPL(u_audio_set_mute); 863 864 865 static int u_audio_pitch_info(struct snd_kcontrol *kcontrol, 866 struct snd_ctl_elem_info *uinfo) 867 { 868 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 869 struct snd_uac_chip *uac = prm->uac; 870 struct g_audio *audio_dev = uac->audio_dev; 871 struct uac_params *params = &audio_dev->params; 872 unsigned int pitch_min, pitch_max; 873 874 pitch_min = (1000 - FBACK_SLOW_MAX) * 1000; 875 pitch_max = (1000 + params->fb_max) * 1000; 876 877 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 878 uinfo->count = 1; 879 uinfo->value.integer.min = pitch_min; 880 uinfo->value.integer.max = pitch_max; 881 uinfo->value.integer.step = 1; 882 return 0; 883 } 884 885 static int u_audio_pitch_get(struct snd_kcontrol *kcontrol, 886 struct snd_ctl_elem_value *ucontrol) 887 { 888 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 889 890 ucontrol->value.integer.value[0] = prm->pitch; 891 892 return 0; 893 } 894 895 static int u_audio_pitch_put(struct snd_kcontrol *kcontrol, 896 struct snd_ctl_elem_value *ucontrol) 897 { 898 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 899 struct snd_uac_chip *uac = prm->uac; 900 struct g_audio *audio_dev = uac->audio_dev; 901 struct uac_params *params = &audio_dev->params; 902 unsigned int val; 903 unsigned int pitch_min, pitch_max; 904 int change = 0; 905 906 pitch_min = (1000 - FBACK_SLOW_MAX) * 1000; 907 pitch_max = (1000 + params->fb_max) * 1000; 908 909 val = ucontrol->value.integer.value[0]; 910 911 if (val < pitch_min) 912 val = pitch_min; 913 if (val > pitch_max) 914 val = pitch_max; 915 916 if (prm->pitch != val) { 917 prm->pitch = val; 918 change = 1; 919 } 920 921 return change; 922 } 923 924 static int u_audio_mute_info(struct snd_kcontrol *kcontrol, 925 struct snd_ctl_elem_info *uinfo) 926 { 927 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 928 uinfo->count = 1; 929 uinfo->value.integer.min = 0; 930 uinfo->value.integer.max = 1; 931 uinfo->value.integer.step = 1; 932 933 return 0; 934 } 935 936 static int u_audio_mute_get(struct snd_kcontrol *kcontrol, 937 struct snd_ctl_elem_value *ucontrol) 938 { 939 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 940 unsigned long flags; 941 942 spin_lock_irqsave(&prm->lock, flags); 943 ucontrol->value.integer.value[0] = !prm->mute; 944 spin_unlock_irqrestore(&prm->lock, flags); 945 946 return 0; 947 } 948 949 static int u_audio_mute_put(struct snd_kcontrol *kcontrol, 950 struct snd_ctl_elem_value *ucontrol) 951 { 952 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 953 struct snd_uac_chip *uac = prm->uac; 954 struct g_audio *audio_dev = uac->audio_dev; 955 unsigned int val; 956 unsigned long flags; 957 int change = 0; 958 959 val = !ucontrol->value.integer.value[0]; 960 961 spin_lock_irqsave(&prm->lock, flags); 962 if (val != prm->mute) { 963 prm->mute = val; 964 change = 1; 965 } 966 spin_unlock_irqrestore(&prm->lock, flags); 967 968 if (change && audio_dev->notify) 969 audio_dev->notify(audio_dev, prm->fu_id, UAC_FU_MUTE); 970 971 return change; 972 } 973 974 /* 975 * TLV callback for mixer volume controls 976 */ 977 static int u_audio_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag, 978 unsigned int size, unsigned int __user *_tlv) 979 { 980 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 981 DECLARE_TLV_DB_MINMAX(scale, 0, 0); 982 983 if (size < sizeof(scale)) 984 return -ENOMEM; 985 986 /* UAC volume resolution is 1/256 dB, TLV is 1/100 dB */ 987 scale[2] = (prm->volume_min * 100) / 256; 988 scale[3] = (prm->volume_max * 100) / 256; 989 if (copy_to_user(_tlv, scale, sizeof(scale))) 990 return -EFAULT; 991 992 return 0; 993 } 994 995 static int u_audio_volume_info(struct snd_kcontrol *kcontrol, 996 struct snd_ctl_elem_info *uinfo) 997 { 998 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 999 1000 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1001 uinfo->count = 1; 1002 uinfo->value.integer.min = 0; 1003 uinfo->value.integer.max = 1004 (prm->volume_max - prm->volume_min + prm->volume_res - 1) 1005 / prm->volume_res; 1006 uinfo->value.integer.step = 1; 1007 1008 return 0; 1009 } 1010 1011 static int u_audio_volume_get(struct snd_kcontrol *kcontrol, 1012 struct snd_ctl_elem_value *ucontrol) 1013 { 1014 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 1015 unsigned long flags; 1016 1017 spin_lock_irqsave(&prm->lock, flags); 1018 ucontrol->value.integer.value[0] = 1019 (prm->volume - prm->volume_min) / prm->volume_res; 1020 spin_unlock_irqrestore(&prm->lock, flags); 1021 1022 return 0; 1023 } 1024 1025 static int u_audio_volume_put(struct snd_kcontrol *kcontrol, 1026 struct snd_ctl_elem_value *ucontrol) 1027 { 1028 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 1029 struct snd_uac_chip *uac = prm->uac; 1030 struct g_audio *audio_dev = uac->audio_dev; 1031 unsigned int val; 1032 s16 volume; 1033 unsigned long flags; 1034 int change = 0; 1035 1036 val = ucontrol->value.integer.value[0]; 1037 1038 spin_lock_irqsave(&prm->lock, flags); 1039 volume = (val * prm->volume_res) + prm->volume_min; 1040 volume = clamp(volume, prm->volume_min, prm->volume_max); 1041 if (volume != prm->volume) { 1042 prm->volume = volume; 1043 change = 1; 1044 } 1045 spin_unlock_irqrestore(&prm->lock, flags); 1046 1047 if (change && audio_dev->notify) 1048 audio_dev->notify(audio_dev, prm->fu_id, UAC_FU_VOLUME); 1049 1050 return change; 1051 } 1052 1053 static int get_max_srate(const int *srates) 1054 { 1055 int i, max_srate = 0; 1056 1057 for (i = 0; i < UAC_MAX_RATES; i++) { 1058 if (srates[i] == 0) 1059 break; 1060 if (srates[i] > max_srate) 1061 max_srate = srates[i]; 1062 } 1063 return max_srate; 1064 } 1065 1066 static int get_min_srate(const int *srates) 1067 { 1068 int i, min_srate = INT_MAX; 1069 1070 for (i = 0; i < UAC_MAX_RATES; i++) { 1071 if (srates[i] == 0) 1072 break; 1073 if (srates[i] < min_srate) 1074 min_srate = srates[i]; 1075 } 1076 return min_srate; 1077 } 1078 1079 static int u_audio_rate_info(struct snd_kcontrol *kcontrol, 1080 struct snd_ctl_elem_info *uinfo) 1081 { 1082 const int *srates; 1083 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 1084 struct snd_uac_chip *uac = prm->uac; 1085 struct g_audio *audio_dev = uac->audio_dev; 1086 struct uac_params *params = &audio_dev->params; 1087 1088 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1089 uinfo->count = 1; 1090 1091 if (prm == &uac->c_prm) 1092 srates = params->c_srates; 1093 else 1094 srates = params->p_srates; 1095 uinfo->value.integer.min = get_min_srate(srates); 1096 uinfo->value.integer.max = get_max_srate(srates); 1097 return 0; 1098 } 1099 1100 static int u_audio_rate_get(struct snd_kcontrol *kcontrol, 1101 struct snd_ctl_elem_value *ucontrol) 1102 { 1103 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 1104 unsigned long flags; 1105 1106 spin_lock_irqsave(&prm->lock, flags); 1107 if (prm->active) 1108 ucontrol->value.integer.value[0] = prm->srate; 1109 else 1110 /* not active: reporting zero rate */ 1111 ucontrol->value.integer.value[0] = 0; 1112 spin_unlock_irqrestore(&prm->lock, flags); 1113 return 0; 1114 } 1115 1116 static struct snd_kcontrol_new u_audio_controls[] = { 1117 [UAC_FBACK_CTRL] { 1118 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1119 .name = "Capture Pitch 1000000", 1120 .info = u_audio_pitch_info, 1121 .get = u_audio_pitch_get, 1122 .put = u_audio_pitch_put, 1123 }, 1124 [UAC_P_PITCH_CTRL] { 1125 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1126 .name = "Playback Pitch 1000000", 1127 .info = u_audio_pitch_info, 1128 .get = u_audio_pitch_get, 1129 .put = u_audio_pitch_put, 1130 }, 1131 [UAC_MUTE_CTRL] { 1132 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1133 .name = "", /* will be filled later */ 1134 .info = u_audio_mute_info, 1135 .get = u_audio_mute_get, 1136 .put = u_audio_mute_put, 1137 }, 1138 [UAC_VOLUME_CTRL] { 1139 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1140 .name = "", /* will be filled later */ 1141 .info = u_audio_volume_info, 1142 .get = u_audio_volume_get, 1143 .put = u_audio_volume_put, 1144 }, 1145 [UAC_RATE_CTRL] { 1146 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1147 .name = "", /* will be filled later */ 1148 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, 1149 .info = u_audio_rate_info, 1150 .get = u_audio_rate_get, 1151 }, 1152 }; 1153 1154 int g_audio_setup(struct g_audio *g_audio, const char *pcm_name, 1155 const char *card_name) 1156 { 1157 struct snd_uac_chip *uac; 1158 struct snd_card *card; 1159 struct snd_pcm *pcm; 1160 struct snd_kcontrol *kctl; 1161 struct uac_params *params; 1162 int p_chmask, c_chmask; 1163 int i, err; 1164 1165 if (!g_audio) 1166 return -EINVAL; 1167 1168 uac = kzalloc(sizeof(*uac), GFP_KERNEL); 1169 if (!uac) 1170 return -ENOMEM; 1171 g_audio->uac = uac; 1172 uac->audio_dev = g_audio; 1173 1174 params = &g_audio->params; 1175 p_chmask = params->p_chmask; 1176 c_chmask = params->c_chmask; 1177 1178 if (c_chmask) { 1179 struct uac_rtd_params *prm = &uac->c_prm; 1180 1181 spin_lock_init(&prm->lock); 1182 uac->c_prm.uac = uac; 1183 prm->max_psize = g_audio->out_ep_maxpsize; 1184 prm->srate = params->c_srates[0]; 1185 1186 prm->reqs = kcalloc(params->req_number, 1187 sizeof(struct usb_request *), 1188 GFP_KERNEL); 1189 if (!prm->reqs) { 1190 err = -ENOMEM; 1191 goto fail; 1192 } 1193 1194 prm->rbuf = kcalloc(params->req_number, prm->max_psize, 1195 GFP_KERNEL); 1196 if (!prm->rbuf) { 1197 prm->max_psize = 0; 1198 err = -ENOMEM; 1199 goto fail; 1200 } 1201 } 1202 1203 if (p_chmask) { 1204 struct uac_rtd_params *prm = &uac->p_prm; 1205 1206 spin_lock_init(&prm->lock); 1207 uac->p_prm.uac = uac; 1208 prm->max_psize = g_audio->in_ep_maxpsize; 1209 prm->srate = params->p_srates[0]; 1210 1211 prm->reqs = kcalloc(params->req_number, 1212 sizeof(struct usb_request *), 1213 GFP_KERNEL); 1214 if (!prm->reqs) { 1215 err = -ENOMEM; 1216 goto fail; 1217 } 1218 1219 prm->rbuf = kcalloc(params->req_number, prm->max_psize, 1220 GFP_KERNEL); 1221 if (!prm->rbuf) { 1222 prm->max_psize = 0; 1223 err = -ENOMEM; 1224 goto fail; 1225 } 1226 } 1227 1228 /* Choose any slot, with no id */ 1229 err = snd_card_new(&g_audio->gadget->dev, 1230 -1, NULL, THIS_MODULE, 0, &card); 1231 if (err < 0) 1232 goto fail; 1233 1234 uac->card = card; 1235 1236 /* 1237 * Create first PCM device 1238 * Create a substream only for non-zero channel streams 1239 */ 1240 err = snd_pcm_new(uac->card, pcm_name, 0, 1241 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm); 1242 if (err < 0) 1243 goto snd_fail; 1244 1245 strscpy(pcm->name, pcm_name); 1246 pcm->private_data = uac; 1247 uac->pcm = pcm; 1248 1249 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops); 1250 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops); 1251 1252 /* 1253 * Create mixer and controls 1254 * Create only if it's required on USB side 1255 */ 1256 if ((c_chmask && g_audio->in_ep_fback) 1257 || (p_chmask && params->p_fu.id) 1258 || (c_chmask && params->c_fu.id)) 1259 strscpy(card->mixername, card_name); 1260 1261 if (c_chmask && g_audio->in_ep_fback) { 1262 kctl = snd_ctl_new1(&u_audio_controls[UAC_FBACK_CTRL], 1263 &uac->c_prm); 1264 if (!kctl) { 1265 err = -ENOMEM; 1266 goto snd_fail; 1267 } 1268 1269 kctl->id.device = pcm->device; 1270 kctl->id.subdevice = 0; 1271 1272 err = snd_ctl_add(card, kctl); 1273 if (err < 0) 1274 goto snd_fail; 1275 } 1276 1277 if (p_chmask) { 1278 kctl = snd_ctl_new1(&u_audio_controls[UAC_P_PITCH_CTRL], 1279 &uac->p_prm); 1280 if (!kctl) { 1281 err = -ENOMEM; 1282 goto snd_fail; 1283 } 1284 1285 kctl->id.device = pcm->device; 1286 kctl->id.subdevice = 0; 1287 1288 err = snd_ctl_add(card, kctl); 1289 if (err < 0) 1290 goto snd_fail; 1291 } 1292 1293 for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) { 1294 struct uac_rtd_params *prm; 1295 struct uac_fu_params *fu; 1296 char ctrl_name[24]; 1297 char *direction; 1298 1299 if (!pcm->streams[i].substream_count) 1300 continue; 1301 1302 if (i == SNDRV_PCM_STREAM_PLAYBACK) { 1303 prm = &uac->p_prm; 1304 fu = ¶ms->p_fu; 1305 direction = "Playback"; 1306 } else { 1307 prm = &uac->c_prm; 1308 fu = ¶ms->c_fu; 1309 direction = "Capture"; 1310 } 1311 1312 prm->fu_id = fu->id; 1313 1314 if (fu->mute_present) { 1315 snprintf(ctrl_name, sizeof(ctrl_name), 1316 "PCM %s Switch", direction); 1317 1318 u_audio_controls[UAC_MUTE_CTRL].name = ctrl_name; 1319 1320 kctl = snd_ctl_new1(&u_audio_controls[UAC_MUTE_CTRL], 1321 prm); 1322 if (!kctl) { 1323 err = -ENOMEM; 1324 goto snd_fail; 1325 } 1326 1327 kctl->id.device = pcm->device; 1328 kctl->id.subdevice = 0; 1329 1330 err = snd_ctl_add(card, kctl); 1331 if (err < 0) 1332 goto snd_fail; 1333 prm->snd_kctl_mute_id = kctl->id; 1334 prm->mute = 0; 1335 } 1336 1337 if (fu->volume_present) { 1338 snprintf(ctrl_name, sizeof(ctrl_name), 1339 "PCM %s Volume", direction); 1340 1341 u_audio_controls[UAC_VOLUME_CTRL].name = ctrl_name; 1342 1343 kctl = snd_ctl_new1(&u_audio_controls[UAC_VOLUME_CTRL], 1344 prm); 1345 if (!kctl) { 1346 err = -ENOMEM; 1347 goto snd_fail; 1348 } 1349 1350 kctl->id.device = pcm->device; 1351 kctl->id.subdevice = 0; 1352 1353 1354 kctl->tlv.c = u_audio_volume_tlv; 1355 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ | 1356 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; 1357 1358 err = snd_ctl_add(card, kctl); 1359 if (err < 0) 1360 goto snd_fail; 1361 prm->snd_kctl_volume_id = kctl->id; 1362 prm->volume = fu->volume_max; 1363 prm->volume_max = fu->volume_max; 1364 prm->volume_min = fu->volume_min; 1365 prm->volume_res = fu->volume_res; 1366 } 1367 1368 /* Add rate control */ 1369 snprintf(ctrl_name, sizeof(ctrl_name), 1370 "%s Rate", direction); 1371 u_audio_controls[UAC_RATE_CTRL].name = ctrl_name; 1372 1373 kctl = snd_ctl_new1(&u_audio_controls[UAC_RATE_CTRL], prm); 1374 if (!kctl) { 1375 err = -ENOMEM; 1376 goto snd_fail; 1377 } 1378 1379 kctl->id.device = pcm->device; 1380 kctl->id.subdevice = 0; 1381 1382 err = snd_ctl_add(card, kctl); 1383 if (err < 0) 1384 goto snd_fail; 1385 prm->snd_kctl_rate_id = kctl->id; 1386 } 1387 1388 strscpy(card->driver, card_name); 1389 strscpy(card->shortname, card_name); 1390 snprintf(card->longname, sizeof(card->longname), "%s %i", 1391 card_name, card->dev->id); 1392 1393 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, 1394 NULL, 0, BUFF_SIZE_MAX); 1395 1396 err = snd_card_register(card); 1397 1398 if (!err) 1399 return 0; 1400 1401 snd_fail: 1402 snd_card_free(card); 1403 fail: 1404 kfree(uac->p_prm.reqs); 1405 kfree(uac->c_prm.reqs); 1406 kfree(uac->p_prm.rbuf); 1407 kfree(uac->c_prm.rbuf); 1408 kfree(uac); 1409 1410 return err; 1411 } 1412 EXPORT_SYMBOL_GPL(g_audio_setup); 1413 1414 void g_audio_cleanup(struct g_audio *g_audio) 1415 { 1416 struct snd_uac_chip *uac; 1417 struct snd_card *card; 1418 1419 if (!g_audio || !g_audio->uac) 1420 return; 1421 1422 uac = g_audio->uac; 1423 g_audio->uac = NULL; 1424 1425 card = uac->card; 1426 if (card) 1427 snd_card_free_when_closed(card); 1428 1429 kfree(uac->p_prm.reqs); 1430 kfree(uac->c_prm.reqs); 1431 kfree(uac->p_prm.rbuf); 1432 kfree(uac->c_prm.rbuf); 1433 kfree(uac); 1434 } 1435 EXPORT_SYMBOL_GPL(g_audio_cleanup); 1436 1437 MODULE_LICENSE("GPL"); 1438 MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities"); 1439 MODULE_AUTHOR("Ruslan Bilovol"); 1440