| 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 <[email protected]> |
| 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 ([email protected]) |
| 12 |
* Jaswinder Singh ([email protected]) |
| 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 |
return ret; // TODO: Clean up out_ep |
| 645 |
} |
| 646 |
|
| 647 |
prm->fb_ep_enabled = true; |
| 648 |
ret = usb_ep_enable(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_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 |
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(&uac->c_prm, false); |
| 691 |
if (audio_dev->in_ep_fback) |
| 692 |
free_ep_fback(&uac->c_prm, audio_dev->in_ep_fback); |
| 693 |
free_ep(&uac->c_prm, 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(gadget, &audio_dev->func, 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(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, prm->reqs[i], GFP_ATOMIC)) |
| 767 |
dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
| 768 |
} |
| 769 |
|
| 770 |
set_active(&uac->p_prm, 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(&uac->p_prm, false); |
| 781 |
free_ep(&uac->p_prm, 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(&uac->p_prm, false); |
| 790 |
set_active(&uac->c_prm, 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(&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(&prm->lock, flags); |
| 832 |
|
| 833 |
if (change) |
| 834 |
snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 835 |
&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(&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(&prm->lock, flags); |
| 881 |
|
| 882 |
if (change) |
| 883 |
snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 884 |
&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(&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(&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(_tlv, scale, 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(&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(&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(&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_obj(*uac); |
| 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 = kzalloc_objs(struct usb_request *, |
| 1213 |
params->req_number); |
| 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 |
if (p_chmask) { |
| 1229 |
struct uac_rtd_params *prm = &uac->p_prm; |
| 1230 |
|
| 1231 |
spin_lock_init(&prm->lock); |
| 1232 |
uac->p_prm.uac = uac; |
| 1233 |
prm->max_psize = g_audio->in_ep_maxpsize; |
| 1234 |
prm->srate = params->p_srates[0]; |
| 1235 |
|
| 1236 |
prm->reqs = kzalloc_objs(struct usb_request *, |
| 1237 |
params->req_number); |
| 1238 |
if (!prm->reqs) { |
| 1239 |
err = -ENOMEM; |
| 1240 |
goto fail; |
| 1241 |
} |
| 1242 |
|
| 1243 |
prm->rbuf = kcalloc(params->req_number, prm->max_psize, |
| 1244 |
GFP_KERNEL); |
| 1245 |
if (!prm->rbuf) { |
| 1246 |
prm->max_psize = 0; |
| 1247 |
err = -ENOMEM; |
| 1248 |
goto fail; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
/* Choose any slot, with no id */ |
| 1253 |
err = snd_card_new(&g_audio->gadget->dev, |
| 1254 |
-1, NULL, THIS_MODULE, 0, &card); |
| 1255 |
if (err < 0) |
| 1256 |
goto fail; |
| 1257 |
|
| 1258 |
uac->card = card; |
| 1259 |
|
| 1260 |
/* |
| 1261 |
* Create first PCM device |
| 1262 |
* Create a substream only for non-zero channel streams |
| 1263 |
*/ |
| 1264 |
err = snd_pcm_new(uac->card, pcm_name, 0, |
| 1265 |
p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm); |
| 1266 |
if (err < 0) |
| 1267 |
goto snd_fail; |
| 1268 |
|
| 1269 |
strscpy(pcm->name, pcm_name); |
| 1270 |
pcm->private_data = uac; |
| 1271 |
uac->pcm = pcm; |
| 1272 |
|
| 1273 |
snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops); |
| 1274 |
snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops); |
| 1275 |
|
| 1276 |
/* |
| 1277 |
* Create mixer and controls |
| 1278 |
* Create only if it's required on USB side |
| 1279 |
*/ |
| 1280 |
if ((c_chmask && g_audio->in_ep_fback) |
| 1281 |
|| (p_chmask && params->p_fu.id) |
| 1282 |
|| (c_chmask && params->c_fu.id)) |
| 1283 |
strscpy(card->mixername, card_name); |
| 1284 |
|
| 1285 |
if (c_chmask && g_audio->in_ep_fback) { |
| 1286 |
kctl = snd_ctl_new1(&u_audio_controls[UAC_FBACK_CTRL], |
| 1287 |
&uac->c_prm); |
| 1288 |
if (!kctl) { |
| 1289 |
err = -ENOMEM; |
| 1290 |
goto snd_fail; |
| 1291 |
} |
| 1292 |
|
| 1293 |
kctl->id.device = pcm->device; |
| 1294 |
kctl->id.subdevice = 0; |
| 1295 |
|
| 1296 |
err = snd_ctl_add(card, kctl); |
| 1297 |
if (err < 0) |
| 1298 |
goto snd_fail; |
| 1299 |
} |
| 1300 |
|
| 1301 |
if (p_chmask) { |
| 1302 |
kctl = snd_ctl_new1(&u_audio_controls[UAC_P_PITCH_CTRL], |
| 1303 |
&uac->p_prm); |
| 1304 |
if (!kctl) { |
| 1305 |
err = -ENOMEM; |
| 1306 |
goto snd_fail; |
| 1307 |
} |
| 1308 |
|
| 1309 |
kctl->id.device = pcm->device; |
| 1310 |
kctl->id.subdevice = 0; |
| 1311 |
|
| 1312 |
err = snd_ctl_add(card, kctl); |
| 1313 |
if (err < 0) |
| 1314 |
goto snd_fail; |
| 1315 |
} |
| 1316 |
|
| 1317 |
for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) { |
| 1318 |
struct uac_rtd_params *prm; |
| 1319 |
struct uac_fu_params *fu; |
| 1320 |
char ctrl_name[24]; |
| 1321 |
char *direction; |
| 1322 |
|
| 1323 |
if (!pcm->streams[i].substream_count) |
| 1324 |
continue; |
| 1325 |
|
| 1326 |
if (i == SNDRV_PCM_STREAM_PLAYBACK) { |
| 1327 |
prm = &uac->p_prm; |
| 1328 |
fu = ¶ms->p_fu; |
| 1329 |
direction = "Playback"; |
| 1330 |
} else { |
| 1331 |
prm = &uac->c_prm; |
| 1332 |
fu = ¶ms->c_fu; |
| 1333 |
direction = "Capture"; |
| 1334 |
} |
| 1335 |
|
| 1336 |
prm->fu_id = fu->id; |
| 1337 |
|
| 1338 |
if (fu->mute_present) { |
| 1339 |
snprintf(ctrl_name, sizeof(ctrl_name), |
| 1340 |
"PCM %s Switch", direction); |
| 1341 |
|
| 1342 |
u_audio_controls[UAC_MUTE_CTRL].name = ctrl_name; |
| 1343 |
|
| 1344 |
kctl = snd_ctl_new1(&u_audio_controls[UAC_MUTE_CTRL], |
| 1345 |
prm); |
| 1346 |
if (!kctl) { |
| 1347 |
err = -ENOMEM; |
| 1348 |
goto snd_fail; |
| 1349 |
} |
| 1350 |
|
| 1351 |
kctl->id.device = pcm->device; |
| 1352 |
kctl->id.subdevice = 0; |
| 1353 |
|
| 1354 |
err = snd_ctl_add(card, kctl); |
| 1355 |
if (err < 0) |
| 1356 |
goto snd_fail; |
| 1357 |
prm->snd_kctl_mute_id = kctl->id; |
| 1358 |
prm->mute = 0; |
| 1359 |
} |
| 1360 |
|
| 1361 |
if (fu->volume_present) { |
| 1362 |
snprintf(ctrl_name, sizeof(ctrl_name), |
| 1363 |
"PCM %s Volume", direction); |
| 1364 |
|
| 1365 |
u_audio_controls[UAC_VOLUME_CTRL].name = ctrl_name; |
| 1366 |
|
| 1367 |
kctl = snd_ctl_new1(&u_audio_controls[UAC_VOLUME_CTRL], |
| 1368 |
prm); |
| 1369 |
if (!kctl) { |
| 1370 |
err = -ENOMEM; |
| 1371 |
goto snd_fail; |
| 1372 |
} |
| 1373 |
|
| 1374 |
kctl->id.device = pcm->device; |
| 1375 |
kctl->id.subdevice = 0; |
| 1376 |
|
| 1377 |
|
| 1378 |
kctl->tlv.c = u_audio_volume_tlv; |
| 1379 |
kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ | |
| 1380 |
SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; |
| 1381 |
|
| 1382 |
err = snd_ctl_add(card, kctl); |
| 1383 |
if (err < 0) |
| 1384 |
goto snd_fail; |
| 1385 |
prm->snd_kctl_volume_id = kctl->id; |
| 1386 |
prm->volume = fu->volume_max; |
| 1387 |
prm->volume_max = fu->volume_max; |
| 1388 |
prm->volume_min = fu->volume_min; |
| 1389 |
prm->volume_res = fu->volume_res; |
| 1390 |
} |
| 1391 |
|
| 1392 |
/* Add rate control */ |
| 1393 |
snprintf(ctrl_name, sizeof(ctrl_name), |
| 1394 |
"%s Rate", direction); |
| 1395 |
u_audio_controls[UAC_RATE_CTRL].name = ctrl_name; |
| 1396 |
|
| 1397 |
kctl = snd_ctl_new1(&u_audio_controls[UAC_RATE_CTRL], prm); |
| 1398 |
if (!kctl) { |
| 1399 |
err = -ENOMEM; |
| 1400 |
goto snd_fail; |
| 1401 |
} |
| 1402 |
|
| 1403 |
kctl->id.device = pcm->device; |
| 1404 |
kctl->id.subdevice = 0; |
| 1405 |
|
| 1406 |
err = snd_ctl_add(card, kctl); |
| 1407 |
if (err < 0) |
| 1408 |
goto snd_fail; |
| 1409 |
prm->snd_kctl_rate_id = kctl->id; |
| 1410 |
} |
| 1411 |
|
| 1412 |
strscpy(card->driver, card_name); |
| 1413 |
strscpy(card->shortname, card_name); |
| 1414 |
snprintf(card->longname, sizeof(card->longname), "%s %i", |
| 1415 |
card_name, card->dev->id); |
| 1416 |
|
| 1417 |
snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, |
| 1418 |
NULL, 0, BUFF_SIZE_MAX); |
| 1419 |
|
| 1420 |
err = snd_card_register(card); |
| 1421 |
|
| 1422 |
if (!err) |
| 1423 |
return 0; |
| 1424 |
|
| 1425 |
snd_fail: |
| 1426 |
snd_card_free(card); |
| 1427 |
fail: |
| 1428 |
kfree(uac->p_prm.reqs); |
| 1429 |
kfree(uac->c_prm.reqs); |
| 1430 |
kfree(uac->p_prm.rbuf); |
| 1431 |
kfree(uac->c_prm.rbuf); |
| 1432 |
kfree(uac); |
| 1433 |
|
| 1434 |
return err; |
| 1435 |
} |
| 1436 |
EXPORT_SYMBOL_GPL(g_audio_setup); |
| 1437 |
|
| 1438 |
void g_audio_cleanup(struct g_audio *g_audio) |
| 1439 |
{ |
| 1440 |
struct snd_uac_chip *uac; |
| 1441 |
struct snd_card *card; |
| 1442 |
|
| 1443 |
if (!g_audio || !g_audio->uac) |
| 1444 |
return; |
| 1445 |
|
| 1446 |
uac = g_audio->uac; |
| 1447 |
g_audio->uac = NULL; |
| 1448 |
|
| 1449 |
card = uac->card; |
| 1450 |
if (card) |
| 1451 |
snd_card_free_when_closed(card); |
| 1452 |
|
| 1453 |
kfree(uac->p_prm.reqs); |
| 1454 |
kfree(uac->c_prm.reqs); |
| 1455 |
kfree(uac->p_prm.rbuf); |
| 1456 |
kfree(uac->c_prm.rbuf); |
| 1457 |
kfree(uac); |
| 1458 |
} |
| 1459 |
EXPORT_SYMBOL_GPL(g_audio_cleanup); |
| 1460 |
|
| 1461 |
MODULE_LICENSE("GPL"); |
| 1462 |
MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities"); |
| 1463 |
MODULE_AUTHOR("Ruslan Bilovol"); |