Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: gadget: f_uac2: split out audio core

Abstract the peripheral side ALSA sound card code from
the f_uac2 function into a component that can be called
by various functions, so the various flavors can be split
apart and selectively reused.

Visible changes:
- add uac_params structure to pass audio paramteres for
g_audio_setup
- make ALSA sound card's name configurable
- add [in/out]_ep_maxpsize
- allocate snd_uac_chip structure during g_audio_setup
- add u_audio_[start/stop]_[capture/playback] functions

Signed-off-by: Ruslan Bilovol <ruslan.bilovol@gmail.com>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>

authored by

Ruslan Bilovol and committed by
Felipe Balbi
eb9fecb9 7158b57a

+844 -635
+4
drivers/usb/gadget/Kconfig
··· 158 158 config USB_U_ETHER 159 159 tristate 160 160 161 + config USB_U_AUDIO 162 + tristate 163 + 161 164 config USB_F_SERIAL 162 165 tristate 163 166 ··· 384 381 depends on SND 385 382 select USB_LIBCOMPOSITE 386 383 select SND_PCM 384 + select USB_U_AUDIO 387 385 select USB_F_UAC2 388 386 help 389 387 This Audio function is compatible with USB Audio Class
+1
drivers/usb/gadget/function/Makefile
··· 32 32 obj-$(CONFIG_USB_F_MASS_STORAGE)+= usb_f_mass_storage.o 33 33 usb_f_fs-y := f_fs.o 34 34 obj-$(CONFIG_USB_F_FS) += usb_f_fs.o 35 + obj-$(CONFIG_USB_U_AUDIO) += u_audio.o 35 36 usb_f_uac1-y := f_uac1.o u_uac1.o 36 37 obj-$(CONFIG_USB_F_UAC1) += usb_f_uac1.o 37 38 usb_f_uac2-y := f_uac2.o
+81 -635
drivers/usb/gadget/function/f_uac2.c
··· 15 15 #include <linux/usb/audio-v2.h> 16 16 #include <linux/module.h> 17 17 18 - #include <sound/core.h> 19 - #include <sound/pcm.h> 20 - #include <sound/pcm_params.h> 21 - 18 + #include "u_audio.h" 22 19 #include "u_uac2.h" 23 20 24 21 /* ··· 47 50 #define UNFLW_CTRL 8 48 51 #define OVFLW_CTRL 10 49 52 50 - struct uac2_req { 51 - struct uac2_rtd_params *pp; /* parent param */ 52 - struct usb_request *req; 53 + struct f_uac2 { 54 + struct g_audio g_audio; 55 + u8 ac_intf, as_in_intf, as_out_intf; 56 + u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */ 53 57 }; 54 58 55 - struct uac2_rtd_params { 56 - struct snd_uac2_chip *uac2; /* parent chip */ 57 - bool ep_enabled; /* if the ep is enabled */ 58 - /* Size of the ring buffer */ 59 - size_t dma_bytes; 60 - unsigned char *dma_area; 61 - 62 - struct snd_pcm_substream *ss; 63 - 64 - /* Ring buffer */ 65 - ssize_t hw_ptr; 66 - 67 - void *rbuf; 68 - 69 - size_t period_size; 70 - 71 - unsigned max_psize; 72 - struct uac2_req *ureq; 73 - 74 - spinlock_t lock; 75 - }; 76 - 77 - struct snd_uac2_chip { 78 - struct uac2_rtd_params p_prm; 79 - struct uac2_rtd_params c_prm; 80 - 81 - struct snd_card *card; 82 - struct snd_pcm *pcm; 83 - 84 - /* timekeeping for the playback endpoint */ 85 - unsigned int p_interval; 86 - unsigned int p_residue; 87 - 88 - /* pre-calculated values for playback iso completion */ 89 - unsigned int p_pktsize; 90 - unsigned int p_pktsize_residue; 91 - unsigned int p_framesize; 92 - }; 93 - 94 - #define BUFF_SIZE_MAX (PAGE_SIZE * 16) 95 - #define PRD_SIZE_MAX PAGE_SIZE 96 - #define MIN_PERIODS 4 97 - 98 - static struct snd_pcm_hardware uac2_pcm_hardware = { 99 - .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER 100 - | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID 101 - | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME, 102 - .rates = SNDRV_PCM_RATE_CONTINUOUS, 103 - .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX, 104 - .buffer_bytes_max = BUFF_SIZE_MAX, 105 - .period_bytes_max = PRD_SIZE_MAX, 106 - .periods_min = MIN_PERIODS, 107 - }; 108 - 109 - struct audio_dev { 110 - u8 ac_intf, ac_alt; 111 - u8 as_out_intf, as_out_alt; 112 - u8 as_in_intf, as_in_alt; 113 - 114 - struct usb_ep *in_ep, *out_ep; 115 - struct usb_function func; 116 - struct usb_gadget *gadget; 117 - 118 - /* The ALSA Sound Card it represents on the USB-Client side */ 119 - struct snd_uac2_chip uac2; 120 - }; 121 - 122 - static inline 123 - struct audio_dev *func_to_agdev(struct usb_function *f) 59 + static inline struct f_uac2 *func_to_uac2(struct usb_function *f) 124 60 { 125 - return container_of(f, struct audio_dev, func); 61 + return container_of(f, struct f_uac2, g_audio.func); 126 62 } 127 63 128 64 static inline 129 - struct audio_dev *uac2_to_agdev(struct snd_uac2_chip *u) 130 - { 131 - return container_of(u, struct audio_dev, uac2); 132 - } 133 - 134 - static inline 135 - struct f_uac2_opts *agdev_to_uac2_opts(struct audio_dev *agdev) 65 + struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev) 136 66 { 137 67 return container_of(agdev->func.fi, struct f_uac2_opts, func_inst); 138 68 } 139 - 140 - static inline 141 - uint num_channels(uint chanmask) 142 - { 143 - uint num = 0; 144 - 145 - while (chanmask) { 146 - num += (chanmask & 1); 147 - chanmask >>= 1; 148 - } 149 - 150 - return num; 151 - } 152 - 153 - static void 154 - agdev_iso_complete(struct usb_ep *ep, struct usb_request *req) 155 - { 156 - unsigned pending; 157 - unsigned long flags; 158 - unsigned int hw_ptr; 159 - bool update_alsa = false; 160 - int status = req->status; 161 - struct uac2_req *ur = req->context; 162 - struct snd_pcm_substream *substream; 163 - struct uac2_rtd_params *prm = ur->pp; 164 - struct snd_uac2_chip *uac2 = prm->uac2; 165 - 166 - /* i/f shutting down */ 167 - if (!prm->ep_enabled || req->status == -ESHUTDOWN) 168 - return; 169 - 170 - /* 171 - * We can't really do much about bad xfers. 172 - * Afterall, the ISOCH xfers could fail legitimately. 173 - */ 174 - if (status) 175 - pr_debug("%s: iso_complete status(%d) %d/%d\n", 176 - __func__, status, req->actual, req->length); 177 - 178 - substream = prm->ss; 179 - 180 - /* Do nothing if ALSA isn't active */ 181 - if (!substream) 182 - goto exit; 183 - 184 - spin_lock_irqsave(&prm->lock, flags); 185 - 186 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 187 - /* 188 - * For each IN packet, take the quotient of the current data 189 - * rate and the endpoint's interval as the base packet size. 190 - * If there is a residue from this division, add it to the 191 - * residue accumulator. 192 - */ 193 - req->length = uac2->p_pktsize; 194 - uac2->p_residue += uac2->p_pktsize_residue; 195 - 196 - /* 197 - * Whenever there are more bytes in the accumulator than we 198 - * need to add one more sample frame, increase this packet's 199 - * size and decrease the accumulator. 200 - */ 201 - if (uac2->p_residue / uac2->p_interval >= uac2->p_framesize) { 202 - req->length += uac2->p_framesize; 203 - uac2->p_residue -= uac2->p_framesize * 204 - uac2->p_interval; 205 - } 206 - 207 - req->actual = req->length; 208 - } 209 - 210 - pending = prm->hw_ptr % prm->period_size; 211 - pending += req->actual; 212 - if (pending >= prm->period_size) 213 - update_alsa = true; 214 - 215 - hw_ptr = prm->hw_ptr; 216 - prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes; 217 - 218 - spin_unlock_irqrestore(&prm->lock, flags); 219 - 220 - /* Pack USB load in ALSA ring buffer */ 221 - pending = prm->dma_bytes - hw_ptr; 222 - 223 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 224 - if (unlikely(pending < req->actual)) { 225 - memcpy(req->buf, prm->dma_area + hw_ptr, pending); 226 - memcpy(req->buf + pending, prm->dma_area, 227 - req->actual - pending); 228 - } else { 229 - memcpy(req->buf, prm->dma_area + hw_ptr, req->actual); 230 - } 231 - } else { 232 - if (unlikely(pending < req->actual)) { 233 - memcpy(prm->dma_area + hw_ptr, req->buf, pending); 234 - memcpy(prm->dma_area, req->buf + pending, 235 - req->actual - pending); 236 - } else { 237 - memcpy(prm->dma_area + hw_ptr, req->buf, req->actual); 238 - } 239 - } 240 - 241 - exit: 242 - if (usb_ep_queue(ep, req, GFP_ATOMIC)) 243 - dev_err(uac2->card->dev, "%d Error!\n", __LINE__); 244 - 245 - if (update_alsa) 246 - snd_pcm_period_elapsed(substream); 247 - 248 - return; 249 - } 250 - 251 - static int 252 - uac2_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 253 - { 254 - struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); 255 - struct audio_dev *agdev = uac2_to_agdev(uac2); 256 - struct f_uac2_opts *uac2_opts = agdev_to_uac2_opts(agdev); 257 - struct uac2_rtd_params *prm; 258 - unsigned long flags; 259 - int err = 0; 260 - 261 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 262 - prm = &uac2->p_prm; 263 - else 264 - prm = &uac2->c_prm; 265 - 266 - spin_lock_irqsave(&prm->lock, flags); 267 - 268 - /* Reset */ 269 - prm->hw_ptr = 0; 270 - 271 - switch (cmd) { 272 - case SNDRV_PCM_TRIGGER_START: 273 - case SNDRV_PCM_TRIGGER_RESUME: 274 - prm->ss = substream; 275 - break; 276 - case SNDRV_PCM_TRIGGER_STOP: 277 - case SNDRV_PCM_TRIGGER_SUSPEND: 278 - prm->ss = NULL; 279 - break; 280 - default: 281 - err = -EINVAL; 282 - } 283 - 284 - spin_unlock_irqrestore(&prm->lock, flags); 285 - 286 - /* Clear buffer after Play stops */ 287 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss) 288 - memset(prm->rbuf, 0, prm->max_psize * uac2_opts->req_number); 289 - 290 - return err; 291 - } 292 - 293 - static snd_pcm_uframes_t uac2_pcm_pointer(struct snd_pcm_substream *substream) 294 - { 295 - struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); 296 - struct uac2_rtd_params *prm; 297 - 298 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 299 - prm = &uac2->p_prm; 300 - else 301 - prm = &uac2->c_prm; 302 - 303 - return bytes_to_frames(substream->runtime, prm->hw_ptr); 304 - } 305 - 306 - static int uac2_pcm_hw_params(struct snd_pcm_substream *substream, 307 - struct snd_pcm_hw_params *hw_params) 308 - { 309 - struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); 310 - struct uac2_rtd_params *prm; 311 - int err; 312 - 313 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 314 - prm = &uac2->p_prm; 315 - else 316 - prm = &uac2->c_prm; 317 - 318 - err = snd_pcm_lib_malloc_pages(substream, 319 - params_buffer_bytes(hw_params)); 320 - if (err >= 0) { 321 - prm->dma_bytes = substream->runtime->dma_bytes; 322 - prm->dma_area = substream->runtime->dma_area; 323 - prm->period_size = params_period_bytes(hw_params); 324 - } 325 - 326 - return err; 327 - } 328 - 329 - static int uac2_pcm_hw_free(struct snd_pcm_substream *substream) 330 - { 331 - struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); 332 - struct uac2_rtd_params *prm; 333 - 334 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 335 - prm = &uac2->p_prm; 336 - else 337 - prm = &uac2->c_prm; 338 - 339 - prm->dma_area = NULL; 340 - prm->dma_bytes = 0; 341 - prm->period_size = 0; 342 - 343 - return snd_pcm_lib_free_pages(substream); 344 - } 345 - 346 - static int uac2_pcm_open(struct snd_pcm_substream *substream) 347 - { 348 - struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); 349 - struct snd_pcm_runtime *runtime = substream->runtime; 350 - struct audio_dev *audio_dev; 351 - struct f_uac2_opts *opts; 352 - int p_ssize, c_ssize; 353 - int p_srate, c_srate; 354 - int p_chmask, c_chmask; 355 - 356 - audio_dev = uac2_to_agdev(uac2); 357 - opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst); 358 - p_ssize = opts->p_ssize; 359 - c_ssize = opts->c_ssize; 360 - p_srate = opts->p_srate; 361 - c_srate = opts->c_srate; 362 - p_chmask = opts->p_chmask; 363 - c_chmask = opts->c_chmask; 364 - uac2->p_residue = 0; 365 - 366 - runtime->hw = uac2_pcm_hardware; 367 - 368 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 369 - spin_lock_init(&uac2->p_prm.lock); 370 - runtime->hw.rate_min = p_srate; 371 - switch (p_ssize) { 372 - case 3: 373 - runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE; 374 - break; 375 - case 4: 376 - runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; 377 - break; 378 - default: 379 - runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; 380 - break; 381 - } 382 - runtime->hw.channels_min = num_channels(p_chmask); 383 - runtime->hw.period_bytes_min = 2 * uac2->p_prm.max_psize 384 - / runtime->hw.periods_min; 385 - } else { 386 - spin_lock_init(&uac2->c_prm.lock); 387 - runtime->hw.rate_min = c_srate; 388 - switch (c_ssize) { 389 - case 3: 390 - runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE; 391 - break; 392 - case 4: 393 - runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; 394 - break; 395 - default: 396 - runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; 397 - break; 398 - } 399 - runtime->hw.channels_min = num_channels(c_chmask); 400 - runtime->hw.period_bytes_min = 2 * uac2->c_prm.max_psize 401 - / runtime->hw.periods_min; 402 - } 403 - 404 - runtime->hw.rate_max = runtime->hw.rate_min; 405 - runtime->hw.channels_max = runtime->hw.channels_min; 406 - 407 - snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 408 - 409 - return 0; 410 - } 411 - 412 - /* ALSA cries without these function pointers */ 413 - static int uac2_pcm_null(struct snd_pcm_substream *substream) 414 - { 415 - return 0; 416 - } 417 - 418 - static struct snd_pcm_ops uac2_pcm_ops = { 419 - .open = uac2_pcm_open, 420 - .close = uac2_pcm_null, 421 - .ioctl = snd_pcm_lib_ioctl, 422 - .hw_params = uac2_pcm_hw_params, 423 - .hw_free = uac2_pcm_hw_free, 424 - .trigger = uac2_pcm_trigger, 425 - .pointer = uac2_pcm_pointer, 426 - .prepare = uac2_pcm_null, 427 - }; 428 - 429 - static int snd_uac2_probe(struct audio_dev *audio_dev) 430 - { 431 - struct snd_uac2_chip *uac2 = &audio_dev->uac2; 432 - struct snd_card *card; 433 - struct snd_pcm *pcm; 434 - struct f_uac2_opts *opts; 435 - int err; 436 - int p_chmask, c_chmask; 437 - 438 - opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst); 439 - p_chmask = opts->p_chmask; 440 - c_chmask = opts->c_chmask; 441 - 442 - /* Choose any slot, with no id */ 443 - err = snd_card_new(&audio_dev->gadget->dev, 444 - -1, NULL, THIS_MODULE, 0, &card); 445 - if (err < 0) 446 - return err; 447 - 448 - uac2->card = card; 449 - 450 - /* 451 - * Create first PCM device 452 - * Create a substream only for non-zero channel streams 453 - */ 454 - err = snd_pcm_new(uac2->card, "UAC2 PCM", 0, 455 - p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm); 456 - if (err < 0) 457 - goto snd_fail; 458 - 459 - strcpy(pcm->name, "UAC2 PCM"); 460 - pcm->private_data = uac2; 461 - 462 - uac2->pcm = pcm; 463 - 464 - snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac2_pcm_ops); 465 - snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac2_pcm_ops); 466 - 467 - strcpy(card->driver, "UAC2_Gadget"); 468 - strcpy(card->shortname, "UAC2_Gadget"); 469 - sprintf(card->longname, "UAC2_Gadget %i", card->dev->id); 470 - 471 - snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, 472 - snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX); 473 - 474 - err = snd_card_register(card); 475 - 476 - if (!err) 477 - return 0; 478 - 479 - snd_fail: 480 - snd_card_free(card); 481 - 482 - uac2->pcm = NULL; 483 - uac2->card = NULL; 484 - 485 - return err; 486 - } 487 - 488 - static int snd_uac2_remove(struct audio_dev *audio_dev) 489 - { 490 - struct snd_card *card = audio_dev->uac2.card; 491 - 492 - if (card) 493 - return snd_card_free(card); 494 - 495 - return 0; 496 - } 497 - 498 69 499 70 /* --------- USB Function Interface ------------- */ 500 71 ··· 451 886 __u32 dRES; 452 887 } __packed; 453 888 454 - static inline void 455 - free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep) 456 - { 457 - struct snd_uac2_chip *uac2 = prm->uac2; 458 - struct audio_dev *agdev = uac2_to_agdev(uac2); 459 - struct f_uac2_opts *uac2_opts = agdev_to_uac2_opts(agdev); 460 - int i; 461 - 462 - if (!prm->ep_enabled) 463 - return; 464 - 465 - prm->ep_enabled = false; 466 - 467 - for (i = 0; i < uac2_opts->req_number; i++) { 468 - if (prm->ureq[i].req) { 469 - usb_ep_dequeue(ep, prm->ureq[i].req); 470 - usb_ep_free_request(ep, prm->ureq[i].req); 471 - prm->ureq[i].req = NULL; 472 - } 473 - } 474 - 475 - if (usb_ep_disable(ep)) 476 - dev_err(uac2->card->dev, 477 - "%s:%d Error!\n", __func__, __LINE__); 478 - } 479 - 480 889 static void set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts, 481 890 struct usb_endpoint_descriptor *ep_desc, 482 891 unsigned int factor, bool is_playback) ··· 477 938 static int 478 939 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) 479 940 { 480 - struct audio_dev *agdev = func_to_agdev(fn); 481 - struct snd_uac2_chip *uac2 = &agdev->uac2; 941 + struct f_uac2 *uac2 = func_to_uac2(fn); 942 + struct g_audio *agdev = func_to_g_audio(fn); 482 943 struct usb_composite_dev *cdev = cfg->cdev; 483 944 struct usb_gadget *gadget = cdev->gadget; 484 945 struct device *dev = &gadget->dev; 485 - struct uac2_rtd_params *prm; 486 946 struct f_uac2_opts *uac2_opts; 487 947 struct usb_string *us; 488 948 int ret; ··· 528 990 return ret; 529 991 } 530 992 std_ac_if_desc.bInterfaceNumber = ret; 531 - agdev->ac_intf = ret; 532 - agdev->ac_alt = 0; 993 + uac2->ac_intf = ret; 994 + uac2->ac_alt = 0; 533 995 534 996 ret = usb_interface_id(cfg, fn); 535 997 if (ret < 0) { ··· 538 1000 } 539 1001 std_as_out_if0_desc.bInterfaceNumber = ret; 540 1002 std_as_out_if1_desc.bInterfaceNumber = ret; 541 - agdev->as_out_intf = ret; 542 - agdev->as_out_alt = 0; 1003 + uac2->as_out_intf = ret; 1004 + uac2->as_out_alt = 0; 543 1005 544 1006 ret = usb_interface_id(cfg, fn); 545 1007 if (ret < 0) { ··· 548 1010 } 549 1011 std_as_in_if0_desc.bInterfaceNumber = ret; 550 1012 std_as_in_if1_desc.bInterfaceNumber = ret; 551 - agdev->as_in_intf = ret; 552 - agdev->as_in_alt = 0; 1013 + uac2->as_in_intf = ret; 1014 + uac2->as_in_alt = 0; 553 1015 554 1016 /* Calculate wMaxPacketSize according to audio bandwidth */ 555 1017 set_ep_max_packet_size(uac2_opts, &fs_epin_desc, 1000, true); ··· 569 1031 return ret; 570 1032 } 571 1033 572 - uac2->p_prm.uac2 = uac2; 573 - uac2->c_prm.uac2 = uac2; 1034 + agdev->in_ep_maxpsize = max(fs_epin_desc.wMaxPacketSize, 1035 + hs_epin_desc.wMaxPacketSize); 1036 + agdev->out_ep_maxpsize = max(fs_epout_desc.wMaxPacketSize, 1037 + hs_epout_desc.wMaxPacketSize); 574 1038 575 1039 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress; 576 1040 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress; ··· 584 1044 585 1045 agdev->gadget = gadget; 586 1046 587 - prm = &agdev->uac2.c_prm; 588 - prm->max_psize = hs_epout_desc.wMaxPacketSize; 589 - prm->ureq = kcalloc(uac2_opts->req_number, sizeof(struct uac2_req), 590 - GFP_KERNEL); 591 - if (!prm->ureq) { 592 - ret = -ENOMEM; 593 - goto err_free_descs; 594 - } 595 - prm->rbuf = kcalloc(uac2_opts->req_number, prm->max_psize, GFP_KERNEL); 596 - if (!prm->rbuf) { 597 - prm->max_psize = 0; 598 - ret = -ENOMEM; 599 - goto err_free_descs; 600 - } 601 - 602 - prm = &agdev->uac2.p_prm; 603 - prm->max_psize = hs_epin_desc.wMaxPacketSize; 604 - prm->ureq = kcalloc(uac2_opts->req_number, sizeof(struct uac2_req), 605 - GFP_KERNEL); 606 - if (!prm->ureq) { 607 - ret = -ENOMEM; 608 - goto err_free_descs; 609 - } 610 - prm->rbuf = kcalloc(uac2_opts->req_number, prm->max_psize, GFP_KERNEL); 611 - if (!prm->rbuf) { 612 - prm->max_psize = 0; 613 - ret = -ENOMEM; 614 - goto err_no_memory; 615 - } 616 - 617 - ret = snd_uac2_probe(agdev); 1047 + agdev->params.p_chmask = uac2_opts->p_chmask; 1048 + agdev->params.p_srate = uac2_opts->p_srate; 1049 + agdev->params.p_ssize = uac2_opts->p_ssize; 1050 + agdev->params.c_chmask = uac2_opts->c_chmask; 1051 + agdev->params.c_srate = uac2_opts->c_srate; 1052 + agdev->params.c_ssize = uac2_opts->c_ssize; 1053 + agdev->params.req_number = uac2_opts->req_number; 1054 + ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget"); 618 1055 if (ret) 619 - goto err_no_memory; 1056 + goto err_free_descs; 620 1057 return 0; 621 1058 622 - err_no_memory: 623 - kfree(agdev->uac2.p_prm.ureq); 624 - kfree(agdev->uac2.c_prm.ureq); 625 - kfree(agdev->uac2.p_prm.rbuf); 626 - kfree(agdev->uac2.c_prm.rbuf); 627 1059 err_free_descs: 628 1060 usb_free_all_descriptors(fn); 629 1061 agdev->gadget = NULL; ··· 606 1094 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt) 607 1095 { 608 1096 struct usb_composite_dev *cdev = fn->config->cdev; 609 - struct audio_dev *agdev = func_to_agdev(fn); 610 - struct f_uac2_opts *opts = agdev_to_uac2_opts(agdev); 611 - struct snd_uac2_chip *uac2 = &agdev->uac2; 1097 + struct f_uac2 *uac2 = func_to_uac2(fn); 612 1098 struct usb_gadget *gadget = cdev->gadget; 613 1099 struct device *dev = &gadget->dev; 614 - struct usb_request *req; 615 - struct usb_ep *ep; 616 - struct uac2_rtd_params *prm; 617 - int req_len, i; 1100 + int ret = 0; 618 1101 619 1102 /* No i/f has more than 2 alt settings */ 620 1103 if (alt > 1) { ··· 617 1110 return -EINVAL; 618 1111 } 619 1112 620 - if (intf == agdev->ac_intf) { 1113 + if (intf == uac2->ac_intf) { 621 1114 /* Control I/f has only 1 AltSetting - 0 */ 622 1115 if (alt) { 623 1116 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); ··· 626 1119 return 0; 627 1120 } 628 1121 629 - if (intf == agdev->as_out_intf) { 630 - ep = agdev->out_ep; 631 - prm = &uac2->c_prm; 632 - config_ep_by_speed(gadget, fn, ep); 633 - agdev->as_out_alt = alt; 634 - req_len = prm->max_psize; 635 - } else if (intf == agdev->as_in_intf) { 636 - unsigned int factor, rate; 637 - struct usb_endpoint_descriptor *ep_desc; 1122 + if (intf == uac2->as_out_intf) { 1123 + uac2->as_out_alt = alt; 638 1124 639 - ep = agdev->in_ep; 640 - prm = &uac2->p_prm; 641 - config_ep_by_speed(gadget, fn, ep); 642 - agdev->as_in_alt = alt; 643 - 644 - /* pre-calculate the playback endpoint's interval */ 645 - if (gadget->speed == USB_SPEED_FULL) { 646 - ep_desc = &fs_epin_desc; 647 - factor = 1000; 648 - } else { 649 - ep_desc = &hs_epin_desc; 650 - factor = 8000; 651 - } 652 - 653 - /* pre-compute some values for iso_complete() */ 654 - uac2->p_framesize = opts->p_ssize * 655 - num_channels(opts->p_chmask); 656 - rate = opts->p_srate * uac2->p_framesize; 657 - uac2->p_interval = factor / (1 << (ep_desc->bInterval - 1)); 658 - uac2->p_pktsize = min_t(unsigned int, rate / uac2->p_interval, 659 - prm->max_psize); 660 - 661 - if (uac2->p_pktsize < prm->max_psize) 662 - uac2->p_pktsize_residue = rate % uac2->p_interval; 1125 + if (alt) 1126 + ret = u_audio_start_capture(&uac2->g_audio); 663 1127 else 664 - uac2->p_pktsize_residue = 0; 1128 + u_audio_stop_capture(&uac2->g_audio); 1129 + } else if (intf == uac2->as_in_intf) { 1130 + uac2->as_in_alt = alt; 665 1131 666 - req_len = uac2->p_pktsize; 667 - uac2->p_residue = 0; 1132 + if (alt) 1133 + ret = u_audio_start_playback(&uac2->g_audio); 1134 + else 1135 + u_audio_stop_playback(&uac2->g_audio); 668 1136 } else { 669 1137 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 670 1138 return -EINVAL; 671 1139 } 672 1140 673 - if (alt == 0) { 674 - free_ep(prm, ep); 675 - return 0; 676 - } 677 - 678 - prm->ep_enabled = true; 679 - usb_ep_enable(ep); 680 - 681 - for (i = 0; i < opts->req_number; i++) { 682 - if (!prm->ureq[i].req) { 683 - req = usb_ep_alloc_request(ep, GFP_ATOMIC); 684 - if (req == NULL) 685 - return -ENOMEM; 686 - 687 - prm->ureq[i].req = req; 688 - prm->ureq[i].pp = prm; 689 - 690 - req->zero = 0; 691 - req->context = &prm->ureq[i]; 692 - req->length = req_len; 693 - req->complete = agdev_iso_complete; 694 - req->buf = prm->rbuf + i * prm->max_psize; 695 - } 696 - 697 - if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC)) 698 - dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 699 - } 700 - 701 - return 0; 1141 + return ret; 702 1142 } 703 1143 704 1144 static int 705 1145 afunc_get_alt(struct usb_function *fn, unsigned intf) 706 1146 { 707 - struct audio_dev *agdev = func_to_agdev(fn); 1147 + struct f_uac2 *uac2 = func_to_uac2(fn); 1148 + struct g_audio *agdev = func_to_g_audio(fn); 708 1149 709 - if (intf == agdev->ac_intf) 710 - return agdev->ac_alt; 711 - else if (intf == agdev->as_out_intf) 712 - return agdev->as_out_alt; 713 - else if (intf == agdev->as_in_intf) 714 - return agdev->as_in_alt; 1150 + if (intf == uac2->ac_intf) 1151 + return uac2->ac_alt; 1152 + else if (intf == uac2->as_out_intf) 1153 + return uac2->as_out_alt; 1154 + else if (intf == uac2->as_in_intf) 1155 + return uac2->as_in_alt; 715 1156 else 716 1157 dev_err(&agdev->gadget->dev, 717 1158 "%s:%d Invalid Interface %d!\n", ··· 671 1216 static void 672 1217 afunc_disable(struct usb_function *fn) 673 1218 { 674 - struct audio_dev *agdev = func_to_agdev(fn); 675 - struct snd_uac2_chip *uac2 = &agdev->uac2; 1219 + struct f_uac2 *uac2 = func_to_uac2(fn); 676 1220 677 - free_ep(&uac2->p_prm, agdev->in_ep); 678 - agdev->as_in_alt = 0; 679 - 680 - free_ep(&uac2->c_prm, agdev->out_ep); 681 - agdev->as_out_alt = 0; 1221 + uac2->as_in_alt = 0; 1222 + uac2->as_out_alt = 0; 1223 + u_audio_stop_capture(&uac2->g_audio); 1224 + u_audio_stop_playback(&uac2->g_audio); 682 1225 } 683 1226 684 1227 static int 685 1228 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) 686 1229 { 687 1230 struct usb_request *req = fn->config->cdev->req; 688 - struct audio_dev *agdev = func_to_agdev(fn); 1231 + struct g_audio *agdev = func_to_g_audio(fn); 689 1232 struct f_uac2_opts *opts; 690 1233 u16 w_length = le16_to_cpu(cr->wLength); 691 1234 u16 w_index = le16_to_cpu(cr->wIndex); ··· 693 1240 int value = -EOPNOTSUPP; 694 1241 int p_srate, c_srate; 695 1242 696 - opts = agdev_to_uac2_opts(agdev); 1243 + opts = g_audio_to_uac2_opts(agdev); 697 1244 p_srate = opts->p_srate; 698 1245 c_srate = opts->c_srate; 699 1246 ··· 724 1271 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr) 725 1272 { 726 1273 struct usb_request *req = fn->config->cdev->req; 727 - struct audio_dev *agdev = func_to_agdev(fn); 1274 + struct g_audio *agdev = func_to_g_audio(fn); 728 1275 struct f_uac2_opts *opts; 729 1276 u16 w_length = le16_to_cpu(cr->wLength); 730 1277 u16 w_index = le16_to_cpu(cr->wIndex); ··· 735 1282 int value = -EOPNOTSUPP; 736 1283 int p_srate, c_srate; 737 1284 738 - opts = agdev_to_uac2_opts(agdev); 1285 + opts = g_audio_to_uac2_opts(agdev); 739 1286 p_srate = opts->p_srate; 740 1287 c_srate = opts->c_srate; 741 1288 ··· 789 1336 static int 790 1337 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr) 791 1338 { 792 - struct audio_dev *agdev = func_to_agdev(fn); 1339 + struct f_uac2 *uac2 = func_to_uac2(fn); 1340 + struct g_audio *agdev = func_to_g_audio(fn); 793 1341 u16 w_index = le16_to_cpu(cr->wIndex); 794 1342 u8 intf = w_index & 0xff; 795 1343 796 - if (intf != agdev->ac_intf) { 1344 + if (intf != uac2->ac_intf) { 797 1345 dev_err(&agdev->gadget->dev, 798 1346 "%s:%d Error!\n", __func__, __LINE__); 799 1347 return -EOPNOTSUPP; ··· 812 1358 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr) 813 1359 { 814 1360 struct usb_composite_dev *cdev = fn->config->cdev; 815 - struct audio_dev *agdev = func_to_agdev(fn); 1361 + struct g_audio *agdev = func_to_g_audio(fn); 816 1362 struct usb_request *req = cdev->req; 817 1363 u16 w_length = le16_to_cpu(cr->wLength); 818 1364 int value = -EOPNOTSUPP; ··· 958 1504 959 1505 static void afunc_free(struct usb_function *f) 960 1506 { 961 - struct audio_dev *agdev; 1507 + struct g_audio *agdev; 962 1508 struct f_uac2_opts *opts; 963 1509 964 - agdev = func_to_agdev(f); 1510 + agdev = func_to_g_audio(f); 965 1511 opts = container_of(f->fi, struct f_uac2_opts, func_inst); 966 1512 kfree(agdev); 967 1513 mutex_lock(&opts->lock); ··· 971 1517 972 1518 static void afunc_unbind(struct usb_configuration *c, struct usb_function *f) 973 1519 { 974 - struct audio_dev *agdev = func_to_agdev(f); 975 - struct uac2_rtd_params *prm; 1520 + struct g_audio *agdev = func_to_g_audio(f); 976 1521 977 - snd_uac2_remove(agdev); 978 - 979 - prm = &agdev->uac2.p_prm; 980 - kfree(prm->rbuf); 981 - 982 - prm = &agdev->uac2.c_prm; 983 - kfree(prm->rbuf); 984 - kfree(prm->ureq); 1522 + g_audio_cleanup(agdev); 985 1523 usb_free_all_descriptors(f); 986 1524 987 1525 agdev->gadget = NULL; ··· 981 1535 982 1536 static struct usb_function *afunc_alloc(struct usb_function_instance *fi) 983 1537 { 984 - struct audio_dev *agdev; 1538 + struct f_uac2 *uac2; 985 1539 struct f_uac2_opts *opts; 986 1540 987 - agdev = kzalloc(sizeof(*agdev), GFP_KERNEL); 988 - if (agdev == NULL) 1541 + uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL); 1542 + if (uac2 == NULL) 989 1543 return ERR_PTR(-ENOMEM); 990 1544 991 1545 opts = container_of(fi, struct f_uac2_opts, func_inst); ··· 993 1547 ++opts->refcnt; 994 1548 mutex_unlock(&opts->lock); 995 1549 996 - agdev->func.name = "uac2_func"; 997 - agdev->func.bind = afunc_bind; 998 - agdev->func.unbind = afunc_unbind; 999 - agdev->func.set_alt = afunc_set_alt; 1000 - agdev->func.get_alt = afunc_get_alt; 1001 - agdev->func.disable = afunc_disable; 1002 - agdev->func.setup = afunc_setup; 1003 - agdev->func.free_func = afunc_free; 1550 + uac2->g_audio.func.name = "uac2_func"; 1551 + uac2->g_audio.func.bind = afunc_bind; 1552 + uac2->g_audio.func.unbind = afunc_unbind; 1553 + uac2->g_audio.func.set_alt = afunc_set_alt; 1554 + uac2->g_audio.func.get_alt = afunc_get_alt; 1555 + uac2->g_audio.func.disable = afunc_disable; 1556 + uac2->g_audio.func.setup = afunc_setup; 1557 + uac2->g_audio.func.free_func = afunc_free; 1004 1558 1005 - return &agdev->func; 1559 + return &uac2->g_audio.func; 1006 1560 } 1007 1561 1008 1562 DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
+662
drivers/usb/gadget/function/u_audio.c
··· 1 + /* 2 + * u_audio.c -- interface to USB gadget "ALSA sound card" utilities 3 + * 4 + * Copyright (C) 2016 5 + * Author: Ruslan Bilovol <ruslan.bilovol@gmail.com> 6 + * 7 + * Sound card implementation was cut-and-pasted with changes 8 + * from f_uac2.c and has: 9 + * Copyright (C) 2011 10 + * Yadwinder Singh (yadi.brar01@gmail.com) 11 + * Jaswinder Singh (jaswinder.singh@linaro.org) 12 + * 13 + * This program is free software; you can redistribute it and/or modify 14 + * it under the terms of the GNU General Public License as published by 15 + * the Free Software Foundation; either version 2 of the License, or 16 + * (at your option) any later version. 17 + * 18 + * This program is distributed in the hope that it will be useful, 19 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 + * GNU General Public License for more details. 22 + */ 23 + 24 + #include <linux/module.h> 25 + #include <sound/core.h> 26 + #include <sound/pcm.h> 27 + #include <sound/pcm_params.h> 28 + 29 + #include "u_audio.h" 30 + 31 + #define BUFF_SIZE_MAX (PAGE_SIZE * 16) 32 + #define PRD_SIZE_MAX PAGE_SIZE 33 + #define MIN_PERIODS 4 34 + 35 + struct uac_req { 36 + struct uac_rtd_params *pp; /* parent param */ 37 + struct usb_request *req; 38 + }; 39 + 40 + /* Runtime data params for one stream */ 41 + struct uac_rtd_params { 42 + struct snd_uac_chip *uac; /* parent chip */ 43 + bool ep_enabled; /* if the ep is enabled */ 44 + /* Size of the ring buffer */ 45 + size_t dma_bytes; 46 + unsigned char *dma_area; 47 + 48 + struct snd_pcm_substream *ss; 49 + 50 + /* Ring buffer */ 51 + ssize_t hw_ptr; 52 + 53 + void *rbuf; 54 + 55 + size_t period_size; 56 + 57 + unsigned max_psize; /* MaxPacketSize of endpoint */ 58 + struct uac_req *ureq; 59 + 60 + spinlock_t lock; 61 + }; 62 + 63 + struct snd_uac_chip { 64 + struct g_audio *audio_dev; 65 + 66 + struct uac_rtd_params p_prm; 67 + struct uac_rtd_params c_prm; 68 + 69 + struct snd_card *card; 70 + struct snd_pcm *pcm; 71 + 72 + /* timekeeping for the playback endpoint */ 73 + unsigned int p_interval; 74 + unsigned int p_residue; 75 + 76 + /* pre-calculated values for playback iso completion */ 77 + unsigned int p_pktsize; 78 + unsigned int p_pktsize_residue; 79 + unsigned int p_framesize; 80 + }; 81 + 82 + static struct snd_pcm_hardware uac_pcm_hardware = { 83 + .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER 84 + | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID 85 + | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME, 86 + .rates = SNDRV_PCM_RATE_CONTINUOUS, 87 + .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX, 88 + .buffer_bytes_max = BUFF_SIZE_MAX, 89 + .period_bytes_max = PRD_SIZE_MAX, 90 + .periods_min = MIN_PERIODS, 91 + }; 92 + 93 + static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req) 94 + { 95 + unsigned pending; 96 + unsigned long flags; 97 + unsigned int hw_ptr; 98 + bool update_alsa = false; 99 + int status = req->status; 100 + struct uac_req *ur = req->context; 101 + struct snd_pcm_substream *substream; 102 + struct uac_rtd_params *prm = ur->pp; 103 + struct snd_uac_chip *uac = prm->uac; 104 + 105 + /* i/f shutting down */ 106 + if (!prm->ep_enabled || req->status == -ESHUTDOWN) 107 + return; 108 + 109 + /* 110 + * We can't really do much about bad xfers. 111 + * Afterall, the ISOCH xfers could fail legitimately. 112 + */ 113 + if (status) 114 + pr_debug("%s: iso_complete status(%d) %d/%d\n", 115 + __func__, status, req->actual, req->length); 116 + 117 + substream = prm->ss; 118 + 119 + /* Do nothing if ALSA isn't active */ 120 + if (!substream) 121 + goto exit; 122 + 123 + spin_lock_irqsave(&prm->lock, flags); 124 + 125 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 126 + /* 127 + * For each IN packet, take the quotient of the current data 128 + * rate and the endpoint's interval as the base packet size. 129 + * If there is a residue from this division, add it to the 130 + * residue accumulator. 131 + */ 132 + req->length = uac->p_pktsize; 133 + uac->p_residue += uac->p_pktsize_residue; 134 + 135 + /* 136 + * Whenever there are more bytes in the accumulator than we 137 + * need to add one more sample frame, increase this packet's 138 + * size and decrease the accumulator. 139 + */ 140 + if (uac->p_residue / uac->p_interval >= uac->p_framesize) { 141 + req->length += uac->p_framesize; 142 + uac->p_residue -= uac->p_framesize * 143 + uac->p_interval; 144 + } 145 + 146 + req->actual = req->length; 147 + } 148 + 149 + pending = prm->hw_ptr % prm->period_size; 150 + pending += req->actual; 151 + if (pending >= prm->period_size) 152 + update_alsa = true; 153 + 154 + hw_ptr = prm->hw_ptr; 155 + prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes; 156 + 157 + spin_unlock_irqrestore(&prm->lock, flags); 158 + 159 + /* Pack USB load in ALSA ring buffer */ 160 + pending = prm->dma_bytes - hw_ptr; 161 + 162 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 163 + if (unlikely(pending < req->actual)) { 164 + memcpy(req->buf, prm->dma_area + hw_ptr, pending); 165 + memcpy(req->buf + pending, prm->dma_area, 166 + req->actual - pending); 167 + } else { 168 + memcpy(req->buf, prm->dma_area + hw_ptr, req->actual); 169 + } 170 + } else { 171 + if (unlikely(pending < req->actual)) { 172 + memcpy(prm->dma_area + hw_ptr, req->buf, pending); 173 + memcpy(prm->dma_area, req->buf + pending, 174 + req->actual - pending); 175 + } else { 176 + memcpy(prm->dma_area + hw_ptr, req->buf, req->actual); 177 + } 178 + } 179 + 180 + exit: 181 + if (usb_ep_queue(ep, req, GFP_ATOMIC)) 182 + dev_err(uac->card->dev, "%d Error!\n", __LINE__); 183 + 184 + if (update_alsa) 185 + snd_pcm_period_elapsed(substream); 186 + } 187 + 188 + static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 189 + { 190 + struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 191 + struct uac_rtd_params *prm; 192 + struct g_audio *audio_dev; 193 + struct uac_params *params; 194 + unsigned long flags; 195 + int err = 0; 196 + 197 + audio_dev = uac->audio_dev; 198 + params = &audio_dev->params; 199 + 200 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 201 + prm = &uac->p_prm; 202 + else 203 + prm = &uac->c_prm; 204 + 205 + spin_lock_irqsave(&prm->lock, flags); 206 + 207 + /* Reset */ 208 + prm->hw_ptr = 0; 209 + 210 + switch (cmd) { 211 + case SNDRV_PCM_TRIGGER_START: 212 + case SNDRV_PCM_TRIGGER_RESUME: 213 + prm->ss = substream; 214 + break; 215 + case SNDRV_PCM_TRIGGER_STOP: 216 + case SNDRV_PCM_TRIGGER_SUSPEND: 217 + prm->ss = NULL; 218 + break; 219 + default: 220 + err = -EINVAL; 221 + } 222 + 223 + spin_unlock_irqrestore(&prm->lock, flags); 224 + 225 + /* Clear buffer after Play stops */ 226 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss) 227 + memset(prm->rbuf, 0, prm->max_psize * params->req_number); 228 + 229 + return err; 230 + } 231 + 232 + static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream) 233 + { 234 + struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 235 + struct uac_rtd_params *prm; 236 + 237 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 238 + prm = &uac->p_prm; 239 + else 240 + prm = &uac->c_prm; 241 + 242 + return bytes_to_frames(substream->runtime, prm->hw_ptr); 243 + } 244 + 245 + static int uac_pcm_hw_params(struct snd_pcm_substream *substream, 246 + struct snd_pcm_hw_params *hw_params) 247 + { 248 + struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 249 + struct uac_rtd_params *prm; 250 + int err; 251 + 252 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 253 + prm = &uac->p_prm; 254 + else 255 + prm = &uac->c_prm; 256 + 257 + err = snd_pcm_lib_malloc_pages(substream, 258 + params_buffer_bytes(hw_params)); 259 + if (err >= 0) { 260 + prm->dma_bytes = substream->runtime->dma_bytes; 261 + prm->dma_area = substream->runtime->dma_area; 262 + prm->period_size = params_period_bytes(hw_params); 263 + } 264 + 265 + return err; 266 + } 267 + 268 + static int uac_pcm_hw_free(struct snd_pcm_substream *substream) 269 + { 270 + struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 271 + struct uac_rtd_params *prm; 272 + 273 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 274 + prm = &uac->p_prm; 275 + else 276 + prm = &uac->c_prm; 277 + 278 + prm->dma_area = NULL; 279 + prm->dma_bytes = 0; 280 + prm->period_size = 0; 281 + 282 + return snd_pcm_lib_free_pages(substream); 283 + } 284 + 285 + static int uac_pcm_open(struct snd_pcm_substream *substream) 286 + { 287 + struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 288 + struct snd_pcm_runtime *runtime = substream->runtime; 289 + struct g_audio *audio_dev; 290 + struct uac_params *params; 291 + int p_ssize, c_ssize; 292 + int p_srate, c_srate; 293 + int p_chmask, c_chmask; 294 + 295 + audio_dev = uac->audio_dev; 296 + params = &audio_dev->params; 297 + p_ssize = params->p_ssize; 298 + c_ssize = params->c_ssize; 299 + p_srate = params->p_srate; 300 + c_srate = params->c_srate; 301 + p_chmask = params->p_chmask; 302 + c_chmask = params->c_chmask; 303 + uac->p_residue = 0; 304 + 305 + runtime->hw = uac_pcm_hardware; 306 + 307 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 308 + spin_lock_init(&uac->p_prm.lock); 309 + runtime->hw.rate_min = p_srate; 310 + switch (p_ssize) { 311 + case 3: 312 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE; 313 + break; 314 + case 4: 315 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; 316 + break; 317 + default: 318 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; 319 + break; 320 + } 321 + runtime->hw.channels_min = num_channels(p_chmask); 322 + runtime->hw.period_bytes_min = 2 * uac->p_prm.max_psize 323 + / runtime->hw.periods_min; 324 + } else { 325 + spin_lock_init(&uac->c_prm.lock); 326 + runtime->hw.rate_min = c_srate; 327 + switch (c_ssize) { 328 + case 3: 329 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE; 330 + break; 331 + case 4: 332 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; 333 + break; 334 + default: 335 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; 336 + break; 337 + } 338 + runtime->hw.channels_min = num_channels(c_chmask); 339 + runtime->hw.period_bytes_min = 2 * uac->c_prm.max_psize 340 + / runtime->hw.periods_min; 341 + } 342 + 343 + runtime->hw.rate_max = runtime->hw.rate_min; 344 + runtime->hw.channels_max = runtime->hw.channels_min; 345 + 346 + snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 347 + 348 + return 0; 349 + } 350 + 351 + /* ALSA cries without these function pointers */ 352 + static int uac_pcm_null(struct snd_pcm_substream *substream) 353 + { 354 + return 0; 355 + } 356 + 357 + static struct snd_pcm_ops uac_pcm_ops = { 358 + .open = uac_pcm_open, 359 + .close = uac_pcm_null, 360 + .ioctl = snd_pcm_lib_ioctl, 361 + .hw_params = uac_pcm_hw_params, 362 + .hw_free = uac_pcm_hw_free, 363 + .trigger = uac_pcm_trigger, 364 + .pointer = uac_pcm_pointer, 365 + .prepare = uac_pcm_null, 366 + }; 367 + 368 + static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep) 369 + { 370 + struct snd_uac_chip *uac = prm->uac; 371 + struct g_audio *audio_dev; 372 + struct uac_params *params; 373 + int i; 374 + 375 + if (!prm->ep_enabled) 376 + return; 377 + 378 + prm->ep_enabled = false; 379 + 380 + audio_dev = uac->audio_dev; 381 + params = &audio_dev->params; 382 + 383 + for (i = 0; i < params->req_number; i++) { 384 + if (prm->ureq[i].req) { 385 + usb_ep_dequeue(ep, prm->ureq[i].req); 386 + usb_ep_free_request(ep, prm->ureq[i].req); 387 + prm->ureq[i].req = NULL; 388 + } 389 + } 390 + 391 + if (usb_ep_disable(ep)) 392 + dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__); 393 + } 394 + 395 + 396 + int u_audio_start_capture(struct g_audio *audio_dev) 397 + { 398 + struct snd_uac_chip *uac = audio_dev->uac; 399 + struct usb_gadget *gadget = audio_dev->gadget; 400 + struct device *dev = &gadget->dev; 401 + struct usb_request *req; 402 + struct usb_ep *ep; 403 + struct uac_rtd_params *prm; 404 + struct uac_params *params = &audio_dev->params; 405 + int req_len, i; 406 + 407 + ep = audio_dev->out_ep; 408 + prm = &uac->c_prm; 409 + config_ep_by_speed(gadget, &audio_dev->func, ep); 410 + req_len = prm->max_psize; 411 + 412 + prm->ep_enabled = true; 413 + usb_ep_enable(ep); 414 + 415 + for (i = 0; i < params->req_number; i++) { 416 + if (!prm->ureq[i].req) { 417 + req = usb_ep_alloc_request(ep, GFP_ATOMIC); 418 + if (req == NULL) 419 + return -ENOMEM; 420 + 421 + prm->ureq[i].req = req; 422 + prm->ureq[i].pp = prm; 423 + 424 + req->zero = 0; 425 + req->context = &prm->ureq[i]; 426 + req->length = req_len; 427 + req->complete = u_audio_iso_complete; 428 + req->buf = prm->rbuf + i * prm->max_psize; 429 + } 430 + 431 + if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC)) 432 + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 433 + } 434 + 435 + return 0; 436 + } 437 + EXPORT_SYMBOL_GPL(u_audio_start_capture); 438 + 439 + void u_audio_stop_capture(struct g_audio *audio_dev) 440 + { 441 + struct snd_uac_chip *uac = audio_dev->uac; 442 + 443 + free_ep(&uac->c_prm, audio_dev->out_ep); 444 + } 445 + EXPORT_SYMBOL_GPL(u_audio_stop_capture); 446 + 447 + int u_audio_start_playback(struct g_audio *audio_dev) 448 + { 449 + struct snd_uac_chip *uac = audio_dev->uac; 450 + struct usb_gadget *gadget = audio_dev->gadget; 451 + struct device *dev = &gadget->dev; 452 + struct usb_request *req; 453 + struct usb_ep *ep; 454 + struct uac_rtd_params *prm; 455 + struct uac_params *params = &audio_dev->params; 456 + unsigned int factor, rate; 457 + const struct usb_endpoint_descriptor *ep_desc; 458 + int req_len, i; 459 + 460 + ep = audio_dev->in_ep; 461 + prm = &uac->p_prm; 462 + config_ep_by_speed(gadget, &audio_dev->func, ep); 463 + 464 + ep_desc = ep->desc; 465 + 466 + /* pre-calculate the playback endpoint's interval */ 467 + if (gadget->speed == USB_SPEED_FULL) 468 + factor = 1000; 469 + else 470 + factor = 8000; 471 + 472 + /* pre-compute some values for iso_complete() */ 473 + uac->p_framesize = params->p_ssize * 474 + num_channels(params->p_chmask); 475 + rate = params->p_srate * uac->p_framesize; 476 + uac->p_interval = factor / (1 << (ep_desc->bInterval - 1)); 477 + uac->p_pktsize = min_t(unsigned int, rate / uac->p_interval, 478 + prm->max_psize); 479 + 480 + if (uac->p_pktsize < prm->max_psize) 481 + uac->p_pktsize_residue = rate % uac->p_interval; 482 + else 483 + uac->p_pktsize_residue = 0; 484 + 485 + req_len = uac->p_pktsize; 486 + uac->p_residue = 0; 487 + 488 + prm->ep_enabled = true; 489 + usb_ep_enable(ep); 490 + 491 + for (i = 0; i < params->req_number; i++) { 492 + if (!prm->ureq[i].req) { 493 + req = usb_ep_alloc_request(ep, GFP_ATOMIC); 494 + if (req == NULL) 495 + return -ENOMEM; 496 + 497 + prm->ureq[i].req = req; 498 + prm->ureq[i].pp = prm; 499 + 500 + req->zero = 0; 501 + req->context = &prm->ureq[i]; 502 + req->length = req_len; 503 + req->complete = u_audio_iso_complete; 504 + req->buf = prm->rbuf + i * prm->max_psize; 505 + } 506 + 507 + if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC)) 508 + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 509 + } 510 + 511 + return 0; 512 + } 513 + EXPORT_SYMBOL_GPL(u_audio_start_playback); 514 + 515 + void u_audio_stop_playback(struct g_audio *audio_dev) 516 + { 517 + struct snd_uac_chip *uac = audio_dev->uac; 518 + 519 + free_ep(&uac->p_prm, audio_dev->in_ep); 520 + } 521 + EXPORT_SYMBOL_GPL(u_audio_stop_playback); 522 + 523 + int g_audio_setup(struct g_audio *g_audio, const char *pcm_name, 524 + const char *card_name) 525 + { 526 + struct snd_uac_chip *uac; 527 + struct snd_card *card; 528 + struct snd_pcm *pcm; 529 + struct uac_params *params; 530 + int p_chmask, c_chmask; 531 + int err; 532 + 533 + if (!g_audio) 534 + return -EINVAL; 535 + 536 + uac = kzalloc(sizeof(*uac), GFP_KERNEL); 537 + if (!uac) 538 + return -ENOMEM; 539 + g_audio->uac = uac; 540 + uac->audio_dev = g_audio; 541 + 542 + params = &g_audio->params; 543 + p_chmask = params->p_chmask; 544 + c_chmask = params->c_chmask; 545 + 546 + if (c_chmask) { 547 + struct uac_rtd_params *prm = &uac->c_prm; 548 + 549 + uac->c_prm.uac = uac; 550 + prm->max_psize = g_audio->out_ep_maxpsize; 551 + 552 + prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req), 553 + GFP_KERNEL); 554 + if (!prm->ureq) { 555 + err = -ENOMEM; 556 + goto fail; 557 + } 558 + 559 + prm->rbuf = kcalloc(params->req_number, prm->max_psize, 560 + GFP_KERNEL); 561 + if (!prm->rbuf) { 562 + prm->max_psize = 0; 563 + err = -ENOMEM; 564 + goto fail; 565 + } 566 + } 567 + 568 + if (p_chmask) { 569 + struct uac_rtd_params *prm = &uac->p_prm; 570 + 571 + uac->p_prm.uac = uac; 572 + prm->max_psize = g_audio->in_ep_maxpsize; 573 + 574 + prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req), 575 + GFP_KERNEL); 576 + if (!prm->ureq) { 577 + err = -ENOMEM; 578 + goto fail; 579 + } 580 + 581 + prm->rbuf = kcalloc(params->req_number, prm->max_psize, 582 + GFP_KERNEL); 583 + if (!prm->rbuf) { 584 + prm->max_psize = 0; 585 + err = -ENOMEM; 586 + goto fail; 587 + } 588 + } 589 + 590 + /* Choose any slot, with no id */ 591 + err = snd_card_new(&g_audio->gadget->dev, 592 + -1, NULL, THIS_MODULE, 0, &card); 593 + if (err < 0) 594 + goto fail; 595 + 596 + uac->card = card; 597 + 598 + /* 599 + * Create first PCM device 600 + * Create a substream only for non-zero channel streams 601 + */ 602 + err = snd_pcm_new(uac->card, pcm_name, 0, 603 + p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm); 604 + if (err < 0) 605 + goto snd_fail; 606 + 607 + strcpy(pcm->name, pcm_name); 608 + pcm->private_data = uac; 609 + uac->pcm = pcm; 610 + 611 + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops); 612 + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops); 613 + 614 + strcpy(card->driver, card_name); 615 + strcpy(card->shortname, card_name); 616 + sprintf(card->longname, "%s %i", card_name, card->dev->id); 617 + 618 + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, 619 + snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX); 620 + 621 + err = snd_card_register(card); 622 + 623 + if (!err) 624 + return 0; 625 + 626 + snd_fail: 627 + snd_card_free(card); 628 + fail: 629 + kfree(uac->p_prm.ureq); 630 + kfree(uac->c_prm.ureq); 631 + kfree(uac->p_prm.rbuf); 632 + kfree(uac->c_prm.rbuf); 633 + kfree(uac); 634 + 635 + return err; 636 + } 637 + EXPORT_SYMBOL_GPL(g_audio_setup); 638 + 639 + void g_audio_cleanup(struct g_audio *g_audio) 640 + { 641 + struct snd_uac_chip *uac; 642 + struct snd_card *card; 643 + 644 + if (!g_audio || !g_audio->uac) 645 + return; 646 + 647 + uac = g_audio->uac; 648 + card = uac->card; 649 + if (card) 650 + snd_card_free(card); 651 + 652 + kfree(uac->p_prm.ureq); 653 + kfree(uac->c_prm.ureq); 654 + kfree(uac->p_prm.rbuf); 655 + kfree(uac->c_prm.rbuf); 656 + kfree(uac); 657 + } 658 + EXPORT_SYMBOL_GPL(g_audio_cleanup); 659 + 660 + MODULE_LICENSE("GPL"); 661 + MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities"); 662 + MODULE_AUTHOR("Ruslan Bilovol");
+95
drivers/usb/gadget/function/u_audio.h
··· 1 + /* 2 + * u_audio.h -- interface to USB gadget "ALSA sound card" utilities 3 + * 4 + * Copyright (C) 2016 5 + * Author: Ruslan Bilovol <ruslan.bilovol@gmail.com> 6 + * 7 + * This program is free software; you can redistribute it and/or modify 8 + * it under the terms of the GNU General Public License as published by 9 + * the Free Software Foundation; either version 2 of the License, or 10 + * (at your option) any later version. 11 + * 12 + * This program is distributed in the hope that it will be useful, 13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 + * GNU General Public License for more details. 16 + * 17 + */ 18 + 19 + #ifndef __U_AUDIO_H 20 + #define __U_AUDIO_H 21 + 22 + #include <linux/usb/composite.h> 23 + 24 + struct uac_params { 25 + /* playback */ 26 + int p_chmask; /* channel mask */ 27 + int p_srate; /* rate in Hz */ 28 + int p_ssize; /* sample size */ 29 + 30 + /* capture */ 31 + int c_chmask; /* channel mask */ 32 + int c_srate; /* rate in Hz */ 33 + int c_ssize; /* sample size */ 34 + 35 + int req_number; /* number of preallocated requests */ 36 + }; 37 + 38 + struct g_audio { 39 + struct usb_function func; 40 + struct usb_gadget *gadget; 41 + 42 + struct usb_ep *in_ep; 43 + struct usb_ep *out_ep; 44 + 45 + /* Max packet size for all in_ep possible speeds */ 46 + unsigned int in_ep_maxpsize; 47 + /* Max packet size for all out_ep possible speeds */ 48 + unsigned int out_ep_maxpsize; 49 + 50 + /* The ALSA Sound Card it represents on the USB-Client side */ 51 + struct snd_uac_chip *uac; 52 + 53 + struct uac_params params; 54 + }; 55 + 56 + static inline struct g_audio *func_to_g_audio(struct usb_function *f) 57 + { 58 + return container_of(f, struct g_audio, func); 59 + } 60 + 61 + static inline uint num_channels(uint chanmask) 62 + { 63 + uint num = 0; 64 + 65 + while (chanmask) { 66 + num += (chanmask & 1); 67 + chanmask >>= 1; 68 + } 69 + 70 + return num; 71 + } 72 + 73 + /* 74 + * g_audio_setup - initialize one virtual ALSA sound card 75 + * @g_audio: struct with filled params, in_ep_maxpsize, out_ep_maxpsize 76 + * @pcm_name: the id string for a PCM instance of this sound card 77 + * @card_name: name of this soundcard 78 + * 79 + * This sets up the single virtual ALSA sound card that may be exported by a 80 + * gadget driver using this framework. 81 + * 82 + * Context: may sleep 83 + * 84 + * Returns zero on success, or a negative error on failure. 85 + */ 86 + int g_audio_setup(struct g_audio *g_audio, const char *pcm_name, 87 + const char *card_name); 88 + void g_audio_cleanup(struct g_audio *g_audio); 89 + 90 + int u_audio_start_capture(struct g_audio *g_audio); 91 + void u_audio_stop_capture(struct g_audio *g_audio); 92 + int u_audio_start_playback(struct g_audio *g_audio); 93 + void u_audio_stop_playback(struct g_audio *g_audio); 94 + 95 + #endif /* __U_AUDIO_H */
+1
drivers/usb/gadget/legacy/Kconfig
··· 56 56 select SND_PCM 57 57 select USB_F_UAC1 if GADGET_UAC1 58 58 select USB_F_UAC2 if !GADGET_UAC1 59 + select USB_U_AUDIO if USB_F_UAC2 59 60 help 60 61 This Gadget Audio driver is compatible with USB Audio Class 61 62 specification 2.0. It implements 1 AudioControl interface,