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