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, ret; 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 ret = config_ep_by_speed(gadget, &audio_dev->func, ep); 601 if (ret < 0) { 602 dev_err(dev, "config_ep_by_speed for out_ep failed (%d)\n", ret); 603 return ret; 604 } 605 606 req_len = ep->maxpacket; 607 608 prm->ep_enabled = true; 609 ret = usb_ep_enable(ep); 610 if (ret < 0) { 611 dev_err(dev, "usb_ep_enable failed for out_ep (%d)\n", ret); 612 return ret; 613 } 614 615 for (i = 0; i < params->req_number; i++) { 616 if (!prm->reqs[i]) { 617 req = usb_ep_alloc_request(ep, GFP_ATOMIC); 618 if (req == NULL) 619 return -ENOMEM; 620 621 prm->reqs[i] = req; 622 623 req->zero = 0; 624 req->context = prm; 625 req->length = req_len; 626 req->complete = u_audio_iso_complete; 627 req->buf = prm->rbuf + i * ep->maxpacket; 628 } 629 630 if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC)) 631 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 632 } 633 634 set_active(&uac->c_prm, true); 635 636 ep_fback = audio_dev->in_ep_fback; 637 if (!ep_fback) 638 return 0; 639 640 /* Setup feedback endpoint */ 641 ret = config_ep_by_speed(gadget, &audio_dev->func, ep_fback); 642 if (ret < 0) { 643 dev_err(dev, "config_ep_by_speed in_ep_fback failed (%d)\n", ret); 644 goto err_out_ep; 645 } 646 647 ret = usb_ep_enable(ep_fback); 648 if (ret < 0) { 649 dev_err(dev, "usb_ep_enable failed for in_ep_fback (%d)\n", ret); 650 goto err_out_ep; 651 } 652 prm->fb_ep_enabled = true; 653 req_len = ep_fback->maxpacket; 654 655 req_fback = usb_ep_alloc_request(ep_fback, GFP_ATOMIC); 656 if (req_fback == NULL) 657 return -ENOMEM; 658 659 prm->req_fback = req_fback; 660 req_fback->zero = 0; 661 req_fback->context = prm; 662 req_fback->length = req_len; 663 req_fback->complete = u_audio_iso_fback_complete; 664 665 req_fback->buf = kzalloc(req_len, GFP_ATOMIC); 666 if (!req_fback->buf) 667 return -ENOMEM; 668 669 /* 670 * Configure the feedback endpoint's reported frequency. 671 * Always start with original frequency since its deviation can't 672 * be meauserd at start of playback 673 */ 674 prm->pitch = 1000000; 675 u_audio_set_fback_frequency(audio_dev->gadget->speed, ep, 676 prm->srate, prm->pitch, 677 req_fback->buf); 678 679 if (usb_ep_queue(ep_fback, req_fback, GFP_ATOMIC)) 680 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 681 682 return 0; 683 684 err_out_ep: 685 set_active(prm, false); 686 free_ep(prm, ep); 687 688 return ret; 689 } 690 EXPORT_SYMBOL_GPL(u_audio_start_capture); 691 692 void u_audio_stop_capture(struct g_audio *audio_dev) 693 { 694 struct snd_uac_chip *uac = audio_dev->uac; 695 696 set_active(&uac->c_prm, false); 697 if (audio_dev->in_ep_fback) 698 free_ep_fback(&uac->c_prm, audio_dev->in_ep_fback); 699 free_ep(&uac->c_prm, audio_dev->out_ep); 700 } 701 EXPORT_SYMBOL_GPL(u_audio_stop_capture); 702 703 int u_audio_start_playback(struct g_audio *audio_dev) 704 { 705 struct snd_uac_chip *uac = audio_dev->uac; 706 struct usb_gadget *gadget = audio_dev->gadget; 707 struct device *dev = &gadget->dev; 708 struct usb_request *req; 709 struct usb_ep *ep; 710 struct uac_rtd_params *prm; 711 struct uac_params *params = &audio_dev->params; 712 unsigned int factor; 713 const struct usb_endpoint_descriptor *ep_desc; 714 int req_len, i, ret; 715 unsigned int p_pktsize; 716 717 prm = &uac->p_prm; 718 dev_dbg(dev, "start playback with rate %d\n", prm->srate); 719 ep = audio_dev->in_ep; 720 ret = config_ep_by_speed(gadget, &audio_dev->func, ep); 721 if (ret < 0) { 722 dev_err(dev, "config_ep_by_speed for in_ep failed (%d)\n", ret); 723 return ret; 724 } 725 726 ep_desc = ep->desc; 727 /* 728 * Always start with original frequency 729 */ 730 prm->pitch = 1000000; 731 732 /* pre-calculate the playback endpoint's interval */ 733 if (gadget->speed == USB_SPEED_FULL) 734 factor = 1000; 735 else 736 factor = 8000; 737 738 /* pre-compute some values for iso_complete() */ 739 uac->p_framesize = params->p_ssize * 740 num_channels(params->p_chmask); 741 uac->p_interval = factor / (1 << (ep_desc->bInterval - 1)); 742 p_pktsize = min_t(unsigned int, 743 uac->p_framesize * 744 (prm->srate / uac->p_interval), 745 ep->maxpacket); 746 747 req_len = p_pktsize; 748 uac->p_residue_mil = 0; 749 750 prm->ep_enabled = true; 751 ret = usb_ep_enable(ep); 752 if (ret < 0) { 753 dev_err(dev, "usb_ep_enable failed for in_ep (%d)\n", ret); 754 return ret; 755 } 756 757 for (i = 0; i < params->req_number; i++) { 758 if (!prm->reqs[i]) { 759 req = usb_ep_alloc_request(ep, GFP_ATOMIC); 760 if (req == NULL) 761 return -ENOMEM; 762 763 prm->reqs[i] = req; 764 765 req->zero = 0; 766 req->context = prm; 767 req->length = req_len; 768 req->complete = u_audio_iso_complete; 769 req->buf = prm->rbuf + i * ep->maxpacket; 770 } 771 772 if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC)) 773 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 774 } 775 776 set_active(&uac->p_prm, true); 777 778 return 0; 779 } 780 EXPORT_SYMBOL_GPL(u_audio_start_playback); 781 782 void u_audio_stop_playback(struct g_audio *audio_dev) 783 { 784 struct snd_uac_chip *uac = audio_dev->uac; 785 786 set_active(&uac->p_prm, false); 787 free_ep(&uac->p_prm, audio_dev->in_ep); 788 } 789 EXPORT_SYMBOL_GPL(u_audio_stop_playback); 790 791 void u_audio_suspend(struct g_audio *audio_dev) 792 { 793 struct snd_uac_chip *uac = audio_dev->uac; 794 795 set_active(&uac->p_prm, false); 796 set_active(&uac->c_prm, false); 797 } 798 EXPORT_SYMBOL_GPL(u_audio_suspend); 799 800 int u_audio_get_volume(struct g_audio *audio_dev, int playback, s16 *val) 801 { 802 struct snd_uac_chip *uac = audio_dev->uac; 803 struct uac_rtd_params *prm; 804 unsigned long flags; 805 806 if (playback) 807 prm = &uac->p_prm; 808 else 809 prm = &uac->c_prm; 810 811 spin_lock_irqsave(&prm->lock, flags); 812 *val = prm->volume; 813 spin_unlock_irqrestore(&prm->lock, flags); 814 815 return 0; 816 } 817 EXPORT_SYMBOL_GPL(u_audio_get_volume); 818 819 int u_audio_set_volume(struct g_audio *audio_dev, int playback, s16 val) 820 { 821 struct snd_uac_chip *uac = audio_dev->uac; 822 struct uac_rtd_params *prm; 823 unsigned long flags; 824 int change = 0; 825 826 if (playback) 827 prm = &uac->p_prm; 828 else 829 prm = &uac->c_prm; 830 831 spin_lock_irqsave(&prm->lock, flags); 832 val = clamp(val, prm->volume_min, prm->volume_max); 833 if (prm->volume != val) { 834 prm->volume = val; 835 change = 1; 836 } 837 spin_unlock_irqrestore(&prm->lock, flags); 838 839 if (change) 840 snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE, 841 &prm->snd_kctl_volume_id); 842 843 return 0; 844 } 845 EXPORT_SYMBOL_GPL(u_audio_set_volume); 846 847 int u_audio_get_mute(struct g_audio *audio_dev, int playback, int *val) 848 { 849 struct snd_uac_chip *uac = audio_dev->uac; 850 struct uac_rtd_params *prm; 851 unsigned long flags; 852 853 if (playback) 854 prm = &uac->p_prm; 855 else 856 prm = &uac->c_prm; 857 858 spin_lock_irqsave(&prm->lock, flags); 859 *val = prm->mute; 860 spin_unlock_irqrestore(&prm->lock, flags); 861 862 return 0; 863 } 864 EXPORT_SYMBOL_GPL(u_audio_get_mute); 865 866 int u_audio_set_mute(struct g_audio *audio_dev, int playback, int val) 867 { 868 struct snd_uac_chip *uac = audio_dev->uac; 869 struct uac_rtd_params *prm; 870 unsigned long flags; 871 int change = 0; 872 int mute; 873 874 if (playback) 875 prm = &uac->p_prm; 876 else 877 prm = &uac->c_prm; 878 879 mute = val ? 1 : 0; 880 881 spin_lock_irqsave(&prm->lock, flags); 882 if (prm->mute != mute) { 883 prm->mute = mute; 884 change = 1; 885 } 886 spin_unlock_irqrestore(&prm->lock, flags); 887 888 if (change) 889 snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE, 890 &prm->snd_kctl_mute_id); 891 892 return 0; 893 } 894 EXPORT_SYMBOL_GPL(u_audio_set_mute); 895 896 897 static int u_audio_pitch_info(struct snd_kcontrol *kcontrol, 898 struct snd_ctl_elem_info *uinfo) 899 { 900 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 901 struct snd_uac_chip *uac = prm->uac; 902 struct g_audio *audio_dev = uac->audio_dev; 903 struct uac_params *params = &audio_dev->params; 904 unsigned int pitch_min, pitch_max; 905 906 pitch_min = (1000 - FBACK_SLOW_MAX) * 1000; 907 pitch_max = (1000 + params->fb_max) * 1000; 908 909 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 910 uinfo->count = 1; 911 uinfo->value.integer.min = pitch_min; 912 uinfo->value.integer.max = pitch_max; 913 uinfo->value.integer.step = 1; 914 return 0; 915 } 916 917 static int u_audio_pitch_get(struct snd_kcontrol *kcontrol, 918 struct snd_ctl_elem_value *ucontrol) 919 { 920 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 921 922 ucontrol->value.integer.value[0] = prm->pitch; 923 924 return 0; 925 } 926 927 static int u_audio_pitch_put(struct snd_kcontrol *kcontrol, 928 struct snd_ctl_elem_value *ucontrol) 929 { 930 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 931 struct snd_uac_chip *uac = prm->uac; 932 struct g_audio *audio_dev = uac->audio_dev; 933 struct uac_params *params = &audio_dev->params; 934 unsigned int val; 935 unsigned int pitch_min, pitch_max; 936 int change = 0; 937 938 pitch_min = (1000 - FBACK_SLOW_MAX) * 1000; 939 pitch_max = (1000 + params->fb_max) * 1000; 940 941 val = ucontrol->value.integer.value[0]; 942 943 if (val < pitch_min) 944 val = pitch_min; 945 if (val > pitch_max) 946 val = pitch_max; 947 948 if (prm->pitch != val) { 949 prm->pitch = val; 950 change = 1; 951 } 952 953 return change; 954 } 955 956 static int u_audio_mute_info(struct snd_kcontrol *kcontrol, 957 struct snd_ctl_elem_info *uinfo) 958 { 959 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 960 uinfo->count = 1; 961 uinfo->value.integer.min = 0; 962 uinfo->value.integer.max = 1; 963 uinfo->value.integer.step = 1; 964 965 return 0; 966 } 967 968 static int u_audio_mute_get(struct snd_kcontrol *kcontrol, 969 struct snd_ctl_elem_value *ucontrol) 970 { 971 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 972 unsigned long flags; 973 974 spin_lock_irqsave(&prm->lock, flags); 975 ucontrol->value.integer.value[0] = !prm->mute; 976 spin_unlock_irqrestore(&prm->lock, flags); 977 978 return 0; 979 } 980 981 static int u_audio_mute_put(struct snd_kcontrol *kcontrol, 982 struct snd_ctl_elem_value *ucontrol) 983 { 984 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 985 struct snd_uac_chip *uac = prm->uac; 986 struct g_audio *audio_dev = uac->audio_dev; 987 unsigned int val; 988 unsigned long flags; 989 int change = 0; 990 991 val = !ucontrol->value.integer.value[0]; 992 993 spin_lock_irqsave(&prm->lock, flags); 994 if (val != prm->mute) { 995 prm->mute = val; 996 change = 1; 997 } 998 spin_unlock_irqrestore(&prm->lock, flags); 999 1000 if (change && audio_dev->notify) 1001 audio_dev->notify(audio_dev, prm->fu_id, UAC_FU_MUTE); 1002 1003 return change; 1004 } 1005 1006 /* 1007 * TLV callback for mixer volume controls 1008 */ 1009 static int u_audio_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag, 1010 unsigned int size, unsigned int __user *_tlv) 1011 { 1012 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 1013 DECLARE_TLV_DB_MINMAX(scale, 0, 0); 1014 1015 if (size < sizeof(scale)) 1016 return -ENOMEM; 1017 1018 /* UAC volume resolution is 1/256 dB, TLV is 1/100 dB */ 1019 scale[2] = (prm->volume_min * 100) / 256; 1020 scale[3] = (prm->volume_max * 100) / 256; 1021 if (copy_to_user(_tlv, scale, sizeof(scale))) 1022 return -EFAULT; 1023 1024 return 0; 1025 } 1026 1027 static int u_audio_volume_info(struct snd_kcontrol *kcontrol, 1028 struct snd_ctl_elem_info *uinfo) 1029 { 1030 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 1031 1032 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1033 uinfo->count = 1; 1034 uinfo->value.integer.min = 0; 1035 uinfo->value.integer.max = 1036 (prm->volume_max - prm->volume_min + prm->volume_res - 1) 1037 / prm->volume_res; 1038 uinfo->value.integer.step = 1; 1039 1040 return 0; 1041 } 1042 1043 static int u_audio_volume_get(struct snd_kcontrol *kcontrol, 1044 struct snd_ctl_elem_value *ucontrol) 1045 { 1046 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 1047 unsigned long flags; 1048 1049 spin_lock_irqsave(&prm->lock, flags); 1050 ucontrol->value.integer.value[0] = 1051 (prm->volume - prm->volume_min) / prm->volume_res; 1052 spin_unlock_irqrestore(&prm->lock, flags); 1053 1054 return 0; 1055 } 1056 1057 static int u_audio_volume_put(struct snd_kcontrol *kcontrol, 1058 struct snd_ctl_elem_value *ucontrol) 1059 { 1060 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 1061 struct snd_uac_chip *uac = prm->uac; 1062 struct g_audio *audio_dev = uac->audio_dev; 1063 unsigned int val; 1064 s16 volume; 1065 unsigned long flags; 1066 int change = 0; 1067 1068 val = ucontrol->value.integer.value[0]; 1069 1070 spin_lock_irqsave(&prm->lock, flags); 1071 volume = (val * prm->volume_res) + prm->volume_min; 1072 volume = clamp(volume, prm->volume_min, prm->volume_max); 1073 if (volume != prm->volume) { 1074 prm->volume = volume; 1075 change = 1; 1076 } 1077 spin_unlock_irqrestore(&prm->lock, flags); 1078 1079 if (change && audio_dev->notify) 1080 audio_dev->notify(audio_dev, prm->fu_id, UAC_FU_VOLUME); 1081 1082 return change; 1083 } 1084 1085 static int get_max_srate(const int *srates) 1086 { 1087 int i, max_srate = 0; 1088 1089 for (i = 0; i < UAC_MAX_RATES; i++) { 1090 if (srates[i] == 0) 1091 break; 1092 if (srates[i] > max_srate) 1093 max_srate = srates[i]; 1094 } 1095 return max_srate; 1096 } 1097 1098 static int get_min_srate(const int *srates) 1099 { 1100 int i, min_srate = INT_MAX; 1101 1102 for (i = 0; i < UAC_MAX_RATES; i++) { 1103 if (srates[i] == 0) 1104 break; 1105 if (srates[i] < min_srate) 1106 min_srate = srates[i]; 1107 } 1108 return min_srate; 1109 } 1110 1111 static int u_audio_rate_info(struct snd_kcontrol *kcontrol, 1112 struct snd_ctl_elem_info *uinfo) 1113 { 1114 const int *srates; 1115 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 1116 struct snd_uac_chip *uac = prm->uac; 1117 struct g_audio *audio_dev = uac->audio_dev; 1118 struct uac_params *params = &audio_dev->params; 1119 1120 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1121 uinfo->count = 1; 1122 1123 if (prm == &uac->c_prm) 1124 srates = params->c_srates; 1125 else 1126 srates = params->p_srates; 1127 uinfo->value.integer.min = get_min_srate(srates); 1128 uinfo->value.integer.max = get_max_srate(srates); 1129 return 0; 1130 } 1131 1132 static int u_audio_rate_get(struct snd_kcontrol *kcontrol, 1133 struct snd_ctl_elem_value *ucontrol) 1134 { 1135 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); 1136 unsigned long flags; 1137 1138 spin_lock_irqsave(&prm->lock, flags); 1139 if (prm->active) 1140 ucontrol->value.integer.value[0] = prm->srate; 1141 else 1142 /* not active: reporting zero rate */ 1143 ucontrol->value.integer.value[0] = 0; 1144 spin_unlock_irqrestore(&prm->lock, flags); 1145 return 0; 1146 } 1147 1148 static struct snd_kcontrol_new u_audio_controls[] = { 1149 [UAC_FBACK_CTRL] = { 1150 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1151 .name = "Capture Pitch 1000000", 1152 .info = u_audio_pitch_info, 1153 .get = u_audio_pitch_get, 1154 .put = u_audio_pitch_put, 1155 }, 1156 [UAC_P_PITCH_CTRL] = { 1157 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1158 .name = "Playback Pitch 1000000", 1159 .info = u_audio_pitch_info, 1160 .get = u_audio_pitch_get, 1161 .put = u_audio_pitch_put, 1162 }, 1163 [UAC_MUTE_CTRL] = { 1164 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1165 .name = "", /* will be filled later */ 1166 .info = u_audio_mute_info, 1167 .get = u_audio_mute_get, 1168 .put = u_audio_mute_put, 1169 }, 1170 [UAC_VOLUME_CTRL] = { 1171 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1172 .name = "", /* will be filled later */ 1173 .info = u_audio_volume_info, 1174 .get = u_audio_volume_get, 1175 .put = u_audio_volume_put, 1176 }, 1177 [UAC_RATE_CTRL] = { 1178 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1179 .name = "", /* will be filled later */ 1180 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, 1181 .info = u_audio_rate_info, 1182 .get = u_audio_rate_get, 1183 }, 1184 }; 1185 1186 static void u_audio_card_free(struct snd_card *card) 1187 { 1188 struct snd_uac_chip *uac = card->private_data; 1189 1190 if (!uac) 1191 return; 1192 1193 kfree(uac->p_prm.reqs); 1194 kfree(uac->c_prm.reqs); 1195 kfree(uac->p_prm.rbuf); 1196 kfree(uac->c_prm.rbuf); 1197 kfree(uac); 1198 } 1199 1200 int g_audio_setup(struct g_audio *g_audio, const char *pcm_name, 1201 const char *card_name) 1202 { 1203 struct snd_uac_chip *uac; 1204 struct snd_card *card; 1205 struct snd_pcm *pcm; 1206 struct snd_kcontrol *kctl; 1207 struct uac_params *params; 1208 int p_chmask, c_chmask; 1209 int i, err; 1210 1211 if (!g_audio) 1212 return -EINVAL; 1213 1214 uac = kzalloc_obj(*uac); 1215 if (!uac) 1216 return -ENOMEM; 1217 g_audio->uac = uac; 1218 uac->audio_dev = g_audio; 1219 1220 params = &g_audio->params; 1221 p_chmask = params->p_chmask; 1222 c_chmask = params->c_chmask; 1223 1224 if (c_chmask) { 1225 struct uac_rtd_params *prm = &uac->c_prm; 1226 1227 spin_lock_init(&prm->lock); 1228 uac->c_prm.uac = uac; 1229 prm->max_psize = g_audio->out_ep_maxpsize; 1230 prm->srate = params->c_srates[0]; 1231 1232 prm->reqs = kzalloc_objs(struct usb_request *, 1233 params->req_number); 1234 if (!prm->reqs) { 1235 err = -ENOMEM; 1236 goto fail; 1237 } 1238 1239 prm->rbuf = kcalloc(params->req_number, prm->max_psize, 1240 GFP_KERNEL); 1241 if (!prm->rbuf) { 1242 prm->max_psize = 0; 1243 err = -ENOMEM; 1244 goto fail; 1245 } 1246 } 1247 1248 if (p_chmask) { 1249 struct uac_rtd_params *prm = &uac->p_prm; 1250 1251 spin_lock_init(&prm->lock); 1252 uac->p_prm.uac = uac; 1253 prm->max_psize = g_audio->in_ep_maxpsize; 1254 prm->srate = params->p_srates[0]; 1255 1256 prm->reqs = kzalloc_objs(struct usb_request *, 1257 params->req_number); 1258 if (!prm->reqs) { 1259 err = -ENOMEM; 1260 goto fail; 1261 } 1262 1263 prm->rbuf = kcalloc(params->req_number, prm->max_psize, 1264 GFP_KERNEL); 1265 if (!prm->rbuf) { 1266 prm->max_psize = 0; 1267 err = -ENOMEM; 1268 goto fail; 1269 } 1270 } 1271 1272 /* Choose any slot, with no id */ 1273 err = snd_card_new(&g_audio->gadget->dev, 1274 -1, NULL, THIS_MODULE, 0, &card); 1275 if (err < 0) 1276 goto fail; 1277 1278 uac->card = card; 1279 card->private_data = uac; 1280 card->private_free = u_audio_card_free; 1281 1282 /* 1283 * Create first PCM device 1284 * Create a substream only for non-zero channel streams 1285 */ 1286 err = snd_pcm_new(uac->card, pcm_name, 0, 1287 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm); 1288 if (err < 0) 1289 goto snd_fail; 1290 1291 strscpy(pcm->name, pcm_name); 1292 pcm->private_data = uac; 1293 uac->pcm = pcm; 1294 1295 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops); 1296 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops); 1297 1298 /* 1299 * Create mixer and controls 1300 * Create only if it's required on USB side 1301 */ 1302 if ((c_chmask && g_audio->in_ep_fback) 1303 || (p_chmask && params->p_fu.id) 1304 || (c_chmask && params->c_fu.id)) 1305 strscpy(card->mixername, card_name); 1306 1307 if (c_chmask && g_audio->in_ep_fback) { 1308 kctl = snd_ctl_new1(&u_audio_controls[UAC_FBACK_CTRL], 1309 &uac->c_prm); 1310 if (!kctl) { 1311 err = -ENOMEM; 1312 goto snd_fail; 1313 } 1314 1315 kctl->id.device = pcm->device; 1316 kctl->id.subdevice = 0; 1317 1318 err = snd_ctl_add(card, kctl); 1319 if (err < 0) 1320 goto snd_fail; 1321 } 1322 1323 if (p_chmask) { 1324 kctl = snd_ctl_new1(&u_audio_controls[UAC_P_PITCH_CTRL], 1325 &uac->p_prm); 1326 if (!kctl) { 1327 err = -ENOMEM; 1328 goto snd_fail; 1329 } 1330 1331 kctl->id.device = pcm->device; 1332 kctl->id.subdevice = 0; 1333 1334 err = snd_ctl_add(card, kctl); 1335 if (err < 0) 1336 goto snd_fail; 1337 } 1338 1339 for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) { 1340 struct uac_rtd_params *prm; 1341 struct uac_fu_params *fu; 1342 char ctrl_name[24]; 1343 char *direction; 1344 1345 if (!pcm->streams[i].substream_count) 1346 continue; 1347 1348 if (i == SNDRV_PCM_STREAM_PLAYBACK) { 1349 prm = &uac->p_prm; 1350 fu = ¶ms->p_fu; 1351 direction = "Playback"; 1352 } else { 1353 prm = &uac->c_prm; 1354 fu = ¶ms->c_fu; 1355 direction = "Capture"; 1356 } 1357 1358 prm->fu_id = fu->id; 1359 1360 if (fu->mute_present) { 1361 snprintf(ctrl_name, sizeof(ctrl_name), 1362 "PCM %s Switch", direction); 1363 1364 u_audio_controls[UAC_MUTE_CTRL].name = ctrl_name; 1365 1366 kctl = snd_ctl_new1(&u_audio_controls[UAC_MUTE_CTRL], 1367 prm); 1368 if (!kctl) { 1369 err = -ENOMEM; 1370 goto snd_fail; 1371 } 1372 1373 kctl->id.device = pcm->device; 1374 kctl->id.subdevice = 0; 1375 1376 err = snd_ctl_add(card, kctl); 1377 if (err < 0) 1378 goto snd_fail; 1379 prm->snd_kctl_mute_id = kctl->id; 1380 prm->mute = 0; 1381 } 1382 1383 if (fu->volume_present) { 1384 snprintf(ctrl_name, sizeof(ctrl_name), 1385 "PCM %s Volume", direction); 1386 1387 u_audio_controls[UAC_VOLUME_CTRL].name = ctrl_name; 1388 1389 kctl = snd_ctl_new1(&u_audio_controls[UAC_VOLUME_CTRL], 1390 prm); 1391 if (!kctl) { 1392 err = -ENOMEM; 1393 goto snd_fail; 1394 } 1395 1396 kctl->id.device = pcm->device; 1397 kctl->id.subdevice = 0; 1398 1399 1400 kctl->tlv.c = u_audio_volume_tlv; 1401 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ | 1402 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; 1403 1404 err = snd_ctl_add(card, kctl); 1405 if (err < 0) 1406 goto snd_fail; 1407 prm->snd_kctl_volume_id = kctl->id; 1408 prm->volume = fu->volume_max; 1409 prm->volume_max = fu->volume_max; 1410 prm->volume_min = fu->volume_min; 1411 prm->volume_res = fu->volume_res; 1412 } 1413 1414 /* Add rate control */ 1415 snprintf(ctrl_name, sizeof(ctrl_name), 1416 "%s Rate", direction); 1417 u_audio_controls[UAC_RATE_CTRL].name = ctrl_name; 1418 1419 kctl = snd_ctl_new1(&u_audio_controls[UAC_RATE_CTRL], prm); 1420 if (!kctl) { 1421 err = -ENOMEM; 1422 goto snd_fail; 1423 } 1424 1425 kctl->id.device = pcm->device; 1426 kctl->id.subdevice = 0; 1427 1428 err = snd_ctl_add(card, kctl); 1429 if (err < 0) 1430 goto snd_fail; 1431 prm->snd_kctl_rate_id = kctl->id; 1432 } 1433 1434 strscpy(card->driver, card_name); 1435 strscpy(card->shortname, card_name); 1436 snprintf(card->longname, sizeof(card->longname), "%s %i", 1437 card_name, card->dev->id); 1438 1439 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, 1440 NULL, 0, BUFF_SIZE_MAX); 1441 1442 err = snd_card_register(card); 1443 1444 if (!err) 1445 return 0; 1446 1447 snd_fail: 1448 snd_card_free(card); 1449 return err; 1450 1451 fail: 1452 kfree(uac->p_prm.reqs); 1453 kfree(uac->c_prm.reqs); 1454 kfree(uac->p_prm.rbuf); 1455 kfree(uac->c_prm.rbuf); 1456 kfree(uac); 1457 1458 return err; 1459 } 1460 EXPORT_SYMBOL_GPL(g_audio_setup); 1461 1462 void g_audio_cleanup(struct g_audio *g_audio) 1463 { 1464 struct snd_uac_chip *uac; 1465 struct snd_card *card; 1466 1467 if (!g_audio || !g_audio->uac) 1468 return; 1469 1470 uac = g_audio->uac; 1471 g_audio->uac = NULL; 1472 1473 card = uac->card; 1474 if (card) 1475 snd_card_free_when_closed(card); 1476 } 1477 EXPORT_SYMBOL_GPL(g_audio_cleanup); 1478 1479 MODULE_LICENSE("GPL"); 1480 MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities"); 1481 MODULE_AUTHOR("Ruslan Bilovol"); 1482