linux/drivers/usb/gadget/function/u_audio.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * u_audio.c -- interface to USB gadget "ALSA sound card" utilities
   4 *
   5 * Copyright (C) 2016
   6 * Author: Ruslan Bilovol <ruslan.bilovol@gmail.com>
   7 *
   8 * Sound card implementation was cut-and-pasted with changes
   9 * from f_uac2.c and has:
  10 *    Copyright (C) 2011
  11 *    Yadwinder Singh (yadi.brar01@gmail.com)
  12 *    Jaswinder Singh (jaswinder.singh@linaro.org)
  13 */
  14
  15#include <linux/module.h>
  16#include <sound/core.h>
  17#include <sound/pcm.h>
  18#include <sound/pcm_params.h>
  19#include <sound/control.h>
  20
  21#include "u_audio.h"
  22
  23#define BUFF_SIZE_MAX   (PAGE_SIZE * 16)
  24#define PRD_SIZE_MAX    PAGE_SIZE
  25#define MIN_PERIODS     4
  26
  27/* Runtime data params for one stream */
  28struct uac_rtd_params {
  29        struct snd_uac_chip *uac; /* parent chip */
  30        bool ep_enabled; /* if the ep is enabled */
  31
  32        struct snd_pcm_substream *ss;
  33
  34        /* Ring buffer */
  35        ssize_t hw_ptr;
  36
  37        void *rbuf;
  38
  39        unsigned int pitch;     /* Stream pitch ratio to 1000000 */
  40        unsigned int max_psize; /* MaxPacketSize of endpoint */
  41
  42        struct usb_request **reqs;
  43
  44        struct usb_request *req_fback; /* Feedback endpoint request */
  45        bool fb_ep_enabled; /* if the ep is enabled */
  46};
  47
  48struct snd_uac_chip {
  49        struct g_audio *audio_dev;
  50
  51        struct uac_rtd_params p_prm;
  52        struct uac_rtd_params c_prm;
  53
  54        struct snd_card *card;
  55        struct snd_pcm *pcm;
  56
  57        /* timekeeping for the playback endpoint */
  58        unsigned int p_interval;
  59        unsigned int p_residue;
  60
  61        /* pre-calculated values for playback iso completion */
  62        unsigned int p_pktsize;
  63        unsigned int p_pktsize_residue;
  64        unsigned int p_framesize;
  65};
  66
  67static const struct snd_pcm_hardware uac_pcm_hardware = {
  68        .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
  69                 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
  70                 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
  71        .rates = SNDRV_PCM_RATE_CONTINUOUS,
  72        .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
  73        .buffer_bytes_max = BUFF_SIZE_MAX,
  74        .period_bytes_max = PRD_SIZE_MAX,
  75        .periods_min = MIN_PERIODS,
  76};
  77
  78static void u_audio_set_fback_frequency(enum usb_device_speed speed,
  79                                        unsigned long long freq,
  80                                        unsigned int pitch,
  81                                        void *buf)
  82{
  83        u32 ff = 0;
  84
  85        /*
  86         * Because the pitch base is 1000000, the final divider here
  87         * will be 1000 * 1000000 = 1953125 << 9
  88         *
  89         * Instead of dealing with big numbers lets fold this 9 left shift
  90         */
  91
  92        if (speed == USB_SPEED_FULL) {
  93                /*
  94                 * Full-speed feedback endpoints report frequency
  95                 * in samples/frame
  96                 * Format is encoded in Q10.10 left-justified in the 24 bits,
  97                 * so that it has a Q10.14 format.
  98                 *
  99                 * ff = (freq << 14) / 1000
 100                 */
 101                freq <<= 5;
 102        } else {
 103                /*
 104                 * High-speed feedback endpoints report frequency
 105                 * in samples/microframe.
 106                 * Format is encoded in Q12.13 fitted into four bytes so that
 107                 * the binary point is located between the second and the third
 108                 * byte fromat (that is Q16.16)
 109                 *
 110                 * ff = (freq << 16) / 8000
 111                 */
 112                freq <<= 4;
 113        }
 114
 115        ff = DIV_ROUND_CLOSEST_ULL((freq * pitch), 1953125);
 116
 117        *(__le32 *)buf = cpu_to_le32(ff);
 118}
 119
 120static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req)
 121{
 122        unsigned int pending;
 123        unsigned int hw_ptr;
 124        int status = req->status;
 125        struct snd_pcm_substream *substream;
 126        struct snd_pcm_runtime *runtime;
 127        struct uac_rtd_params *prm = req->context;
 128        struct snd_uac_chip *uac = prm->uac;
 129
 130        /* i/f shutting down */
 131        if (!prm->ep_enabled) {
 132                usb_ep_free_request(ep, req);
 133                return;
 134        }
 135
 136        if (req->status == -ESHUTDOWN)
 137                return;
 138
 139        /*
 140         * We can't really do much about bad xfers.
 141         * Afterall, the ISOCH xfers could fail legitimately.
 142         */
 143        if (status)
 144                pr_debug("%s: iso_complete status(%d) %d/%d\n",
 145                        __func__, status, req->actual, req->length);
 146
 147        substream = prm->ss;
 148
 149        /* Do nothing if ALSA isn't active */
 150        if (!substream)
 151                goto exit;
 152
 153        snd_pcm_stream_lock(substream);
 154
 155        runtime = substream->runtime;
 156        if (!runtime || !snd_pcm_running(substream)) {
 157                snd_pcm_stream_unlock(substream);
 158                goto exit;
 159        }
 160
 161        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 162                /*
 163                 * For each IN packet, take the quotient of the current data
 164                 * rate and the endpoint's interval as the base packet size.
 165                 * If there is a residue from this division, add it to the
 166                 * residue accumulator.
 167                 */
 168                req->length = uac->p_pktsize;
 169                uac->p_residue += uac->p_pktsize_residue;
 170
 171                /*
 172                 * Whenever there are more bytes in the accumulator than we
 173                 * need to add one more sample frame, increase this packet's
 174                 * size and decrease the accumulator.
 175                 */
 176                if (uac->p_residue / uac->p_interval >= uac->p_framesize) {
 177                        req->length += uac->p_framesize;
 178                        uac->p_residue -= uac->p_framesize *
 179                                           uac->p_interval;
 180                }
 181
 182                req->actual = req->length;
 183        }
 184
 185        hw_ptr = prm->hw_ptr;
 186
 187        /* Pack USB load in ALSA ring buffer */
 188        pending = runtime->dma_bytes - hw_ptr;
 189
 190        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 191                if (unlikely(pending < req->actual)) {
 192                        memcpy(req->buf, runtime->dma_area + hw_ptr, pending);
 193                        memcpy(req->buf + pending, runtime->dma_area,
 194                               req->actual - pending);
 195                } else {
 196                        memcpy(req->buf, runtime->dma_area + hw_ptr,
 197                               req->actual);
 198                }
 199        } else {
 200                if (unlikely(pending < req->actual)) {
 201                        memcpy(runtime->dma_area + hw_ptr, req->buf, pending);
 202                        memcpy(runtime->dma_area, req->buf + pending,
 203                               req->actual - pending);
 204                } else {
 205                        memcpy(runtime->dma_area + hw_ptr, req->buf,
 206                               req->actual);
 207                }
 208        }
 209
 210        /* update hw_ptr after data is copied to memory */
 211        prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes;
 212        hw_ptr = prm->hw_ptr;
 213        snd_pcm_stream_unlock(substream);
 214
 215        if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual)
 216                snd_pcm_period_elapsed(substream);
 217
 218exit:
 219        if (usb_ep_queue(ep, req, GFP_ATOMIC))
 220                dev_err(uac->card->dev, "%d Error!\n", __LINE__);
 221}
 222
 223static void u_audio_iso_fback_complete(struct usb_ep *ep,
 224                                       struct usb_request *req)
 225{
 226        struct uac_rtd_params *prm = req->context;
 227        struct snd_uac_chip *uac = prm->uac;
 228        struct g_audio *audio_dev = uac->audio_dev;
 229        struct uac_params *params = &audio_dev->params;
 230        int status = req->status;
 231
 232        /* i/f shutting down */
 233        if (!prm->fb_ep_enabled) {
 234                kfree(req->buf);
 235                usb_ep_free_request(ep, req);
 236                return;
 237        }
 238
 239        if (req->status == -ESHUTDOWN)
 240                return;
 241
 242        /*
 243         * We can't really do much about bad xfers.
 244         * Afterall, the ISOCH xfers could fail legitimately.
 245         */
 246        if (status)
 247                pr_debug("%s: iso_complete status(%d) %d/%d\n",
 248                        __func__, status, req->actual, req->length);
 249
 250        u_audio_set_fback_frequency(audio_dev->gadget->speed,
 251                                    params->c_srate, prm->pitch,
 252                                    req->buf);
 253
 254        if (usb_ep_queue(ep, req, GFP_ATOMIC))
 255                dev_err(uac->card->dev, "%d Error!\n", __LINE__);
 256}
 257
 258static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 259{
 260        struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
 261        struct uac_rtd_params *prm;
 262        struct g_audio *audio_dev;
 263        struct uac_params *params;
 264        int err = 0;
 265
 266        audio_dev = uac->audio_dev;
 267        params = &audio_dev->params;
 268
 269        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 270                prm = &uac->p_prm;
 271        else
 272                prm = &uac->c_prm;
 273
 274        /* Reset */
 275        prm->hw_ptr = 0;
 276
 277        switch (cmd) {
 278        case SNDRV_PCM_TRIGGER_START:
 279        case SNDRV_PCM_TRIGGER_RESUME:
 280                prm->ss = substream;
 281                break;
 282        case SNDRV_PCM_TRIGGER_STOP:
 283        case SNDRV_PCM_TRIGGER_SUSPEND:
 284                prm->ss = NULL;
 285                break;
 286        default:
 287                err = -EINVAL;
 288        }
 289
 290        /* Clear buffer after Play stops */
 291        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
 292                memset(prm->rbuf, 0, prm->max_psize * params->req_number);
 293
 294        return err;
 295}
 296
 297static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream)
 298{
 299        struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
 300        struct uac_rtd_params *prm;
 301
 302        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 303                prm = &uac->p_prm;
 304        else
 305                prm = &uac->c_prm;
 306
 307        return bytes_to_frames(substream->runtime, prm->hw_ptr);
 308}
 309
 310static u64 uac_ssize_to_fmt(int ssize)
 311{
 312        u64 ret;
 313
 314        switch (ssize) {
 315        case 3:
 316                ret = SNDRV_PCM_FMTBIT_S24_3LE;
 317                break;
 318        case 4:
 319                ret = SNDRV_PCM_FMTBIT_S32_LE;
 320                break;
 321        default:
 322                ret = SNDRV_PCM_FMTBIT_S16_LE;
 323                break;
 324        }
 325
 326        return ret;
 327}
 328
 329static int uac_pcm_open(struct snd_pcm_substream *substream)
 330{
 331        struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
 332        struct snd_pcm_runtime *runtime = substream->runtime;
 333        struct g_audio *audio_dev;
 334        struct uac_params *params;
 335        int p_ssize, c_ssize;
 336        int p_srate, c_srate;
 337        int p_chmask, c_chmask;
 338
 339        audio_dev = uac->audio_dev;
 340        params = &audio_dev->params;
 341        p_ssize = params->p_ssize;
 342        c_ssize = params->c_ssize;
 343        p_srate = params->p_srate;
 344        c_srate = params->c_srate;
 345        p_chmask = params->p_chmask;
 346        c_chmask = params->c_chmask;
 347        uac->p_residue = 0;
 348
 349        runtime->hw = uac_pcm_hardware;
 350
 351        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 352                runtime->hw.rate_min = p_srate;
 353                runtime->hw.formats = uac_ssize_to_fmt(p_ssize);
 354                runtime->hw.channels_min = num_channels(p_chmask);
 355                runtime->hw.period_bytes_min = 2 * uac->p_prm.max_psize
 356                                                / runtime->hw.periods_min;
 357        } else {
 358                runtime->hw.rate_min = c_srate;
 359                runtime->hw.formats = uac_ssize_to_fmt(c_ssize);
 360                runtime->hw.channels_min = num_channels(c_chmask);
 361                runtime->hw.period_bytes_min = 2 * uac->c_prm.max_psize
 362                                                / runtime->hw.periods_min;
 363        }
 364
 365        runtime->hw.rate_max = runtime->hw.rate_min;
 366        runtime->hw.channels_max = runtime->hw.channels_min;
 367
 368        snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
 369
 370        return 0;
 371}
 372
 373/* ALSA cries without these function pointers */
 374static int uac_pcm_null(struct snd_pcm_substream *substream)
 375{
 376        return 0;
 377}
 378
 379static const struct snd_pcm_ops uac_pcm_ops = {
 380        .open = uac_pcm_open,
 381        .close = uac_pcm_null,
 382        .trigger = uac_pcm_trigger,
 383        .pointer = uac_pcm_pointer,
 384        .prepare = uac_pcm_null,
 385};
 386
 387static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep)
 388{
 389        struct snd_uac_chip *uac = prm->uac;
 390        struct g_audio *audio_dev;
 391        struct uac_params *params;
 392        int i;
 393
 394        if (!prm->ep_enabled)
 395                return;
 396
 397        audio_dev = uac->audio_dev;
 398        params = &audio_dev->params;
 399
 400        for (i = 0; i < params->req_number; i++) {
 401                if (prm->reqs[i]) {
 402                        if (usb_ep_dequeue(ep, prm->reqs[i]))
 403                                usb_ep_free_request(ep, prm->reqs[i]);
 404                        /*
 405                         * If usb_ep_dequeue() cannot successfully dequeue the
 406                         * request, the request will be freed by the completion
 407                         * callback.
 408                         */
 409
 410                        prm->reqs[i] = NULL;
 411                }
 412        }
 413
 414        prm->ep_enabled = false;
 415
 416        if (usb_ep_disable(ep))
 417                dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
 418}
 419
 420static inline void free_ep_fback(struct uac_rtd_params *prm, struct usb_ep *ep)
 421{
 422        struct snd_uac_chip *uac = prm->uac;
 423
 424        if (!prm->fb_ep_enabled)
 425                return;
 426
 427        if (prm->req_fback) {
 428                if (usb_ep_dequeue(ep, prm->req_fback)) {
 429                        kfree(prm->req_fback->buf);
 430                        usb_ep_free_request(ep, prm->req_fback);
 431                }
 432                prm->req_fback = NULL;
 433        }
 434
 435        prm->fb_ep_enabled = false;
 436
 437        if (usb_ep_disable(ep))
 438                dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
 439}
 440
 441int u_audio_start_capture(struct g_audio *audio_dev)
 442{
 443        struct snd_uac_chip *uac = audio_dev->uac;
 444        struct usb_gadget *gadget = audio_dev->gadget;
 445        struct device *dev = &gadget->dev;
 446        struct usb_request *req, *req_fback;
 447        struct usb_ep *ep, *ep_fback;
 448        struct uac_rtd_params *prm;
 449        struct uac_params *params = &audio_dev->params;
 450        int req_len, i;
 451
 452        ep = audio_dev->out_ep;
 453        prm = &uac->c_prm;
 454        config_ep_by_speed(gadget, &audio_dev->func, ep);
 455        req_len = ep->maxpacket;
 456
 457        prm->ep_enabled = true;
 458        usb_ep_enable(ep);
 459
 460        for (i = 0; i < params->req_number; i++) {
 461                if (!prm->reqs[i]) {
 462                        req = usb_ep_alloc_request(ep, GFP_ATOMIC);
 463                        if (req == NULL)
 464                                return -ENOMEM;
 465
 466                        prm->reqs[i] = req;
 467
 468                        req->zero = 0;
 469                        req->context = prm;
 470                        req->length = req_len;
 471                        req->complete = u_audio_iso_complete;
 472                        req->buf = prm->rbuf + i * ep->maxpacket;
 473                }
 474
 475                if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC))
 476                        dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
 477        }
 478
 479        ep_fback = audio_dev->in_ep_fback;
 480        if (!ep_fback)
 481                return 0;
 482
 483        /* Setup feedback endpoint */
 484        config_ep_by_speed(gadget, &audio_dev->func, ep_fback);
 485        prm->fb_ep_enabled = true;
 486        usb_ep_enable(ep_fback);
 487        req_len = ep_fback->maxpacket;
 488
 489        req_fback = usb_ep_alloc_request(ep_fback, GFP_ATOMIC);
 490        if (req_fback == NULL)
 491                return -ENOMEM;
 492
 493        prm->req_fback = req_fback;
 494        req_fback->zero = 0;
 495        req_fback->context = prm;
 496        req_fback->length = req_len;
 497        req_fback->complete = u_audio_iso_fback_complete;
 498
 499        req_fback->buf = kzalloc(req_len, GFP_ATOMIC);
 500        if (!req_fback->buf)
 501                return -ENOMEM;
 502
 503        /*
 504         * Configure the feedback endpoint's reported frequency.
 505         * Always start with original frequency since its deviation can't
 506         * be meauserd at start of playback
 507         */
 508        prm->pitch = 1000000;
 509        u_audio_set_fback_frequency(audio_dev->gadget->speed,
 510                                    params->c_srate, prm->pitch,
 511                                    req_fback->buf);
 512
 513        if (usb_ep_queue(ep_fback, req_fback, GFP_ATOMIC))
 514                dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
 515
 516        return 0;
 517}
 518EXPORT_SYMBOL_GPL(u_audio_start_capture);
 519
 520void u_audio_stop_capture(struct g_audio *audio_dev)
 521{
 522        struct snd_uac_chip *uac = audio_dev->uac;
 523
 524        if (audio_dev->in_ep_fback)
 525                free_ep_fback(&uac->c_prm, audio_dev->in_ep_fback);
 526        free_ep(&uac->c_prm, audio_dev->out_ep);
 527}
 528EXPORT_SYMBOL_GPL(u_audio_stop_capture);
 529
 530int u_audio_start_playback(struct g_audio *audio_dev)
 531{
 532        struct snd_uac_chip *uac = audio_dev->uac;
 533        struct usb_gadget *gadget = audio_dev->gadget;
 534        struct device *dev = &gadget->dev;
 535        struct usb_request *req;
 536        struct usb_ep *ep;
 537        struct uac_rtd_params *prm;
 538        struct uac_params *params = &audio_dev->params;
 539        unsigned int factor;
 540        const struct usb_endpoint_descriptor *ep_desc;
 541        int req_len, i;
 542
 543        ep = audio_dev->in_ep;
 544        prm = &uac->p_prm;
 545        config_ep_by_speed(gadget, &audio_dev->func, ep);
 546
 547        ep_desc = ep->desc;
 548
 549        /* pre-calculate the playback endpoint's interval */
 550        if (gadget->speed == USB_SPEED_FULL)
 551                factor = 1000;
 552        else
 553                factor = 8000;
 554
 555        /* pre-compute some values for iso_complete() */
 556        uac->p_framesize = params->p_ssize *
 557                            num_channels(params->p_chmask);
 558        uac->p_interval = factor / (1 << (ep_desc->bInterval - 1));
 559        uac->p_pktsize = min_t(unsigned int,
 560                                uac->p_framesize *
 561                                        (params->p_srate / uac->p_interval),
 562                                ep->maxpacket);
 563
 564        if (uac->p_pktsize < ep->maxpacket)
 565                uac->p_pktsize_residue = uac->p_framesize *
 566                        (params->p_srate % uac->p_interval);
 567        else
 568                uac->p_pktsize_residue = 0;
 569
 570        req_len = uac->p_pktsize;
 571        uac->p_residue = 0;
 572
 573        prm->ep_enabled = true;
 574        usb_ep_enable(ep);
 575
 576        for (i = 0; i < params->req_number; i++) {
 577                if (!prm->reqs[i]) {
 578                        req = usb_ep_alloc_request(ep, GFP_ATOMIC);
 579                        if (req == NULL)
 580                                return -ENOMEM;
 581
 582                        prm->reqs[i] = req;
 583
 584                        req->zero = 0;
 585                        req->context = prm;
 586                        req->length = req_len;
 587                        req->complete = u_audio_iso_complete;
 588                        req->buf = prm->rbuf + i * ep->maxpacket;
 589                }
 590
 591                if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC))
 592                        dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
 593        }
 594
 595        return 0;
 596}
 597EXPORT_SYMBOL_GPL(u_audio_start_playback);
 598
 599void u_audio_stop_playback(struct g_audio *audio_dev)
 600{
 601        struct snd_uac_chip *uac = audio_dev->uac;
 602
 603        free_ep(&uac->p_prm, audio_dev->in_ep);
 604}
 605EXPORT_SYMBOL_GPL(u_audio_stop_playback);
 606
 607static int u_audio_pitch_info(struct snd_kcontrol *kcontrol,
 608                                   struct snd_ctl_elem_info *uinfo)
 609{
 610        struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
 611        struct snd_uac_chip *uac = prm->uac;
 612        struct g_audio *audio_dev = uac->audio_dev;
 613        struct uac_params *params = &audio_dev->params;
 614        unsigned int pitch_min, pitch_max;
 615
 616        pitch_min = (1000 - FBACK_SLOW_MAX) * 1000;
 617        pitch_max = (1000 + params->fb_max) * 1000;
 618
 619        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 620        uinfo->count = 1;
 621        uinfo->value.integer.min = pitch_min;
 622        uinfo->value.integer.max = pitch_max;
 623        uinfo->value.integer.step = 1;
 624        return 0;
 625}
 626
 627static int u_audio_pitch_get(struct snd_kcontrol *kcontrol,
 628                                   struct snd_ctl_elem_value *ucontrol)
 629{
 630        struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
 631
 632        ucontrol->value.integer.value[0] = prm->pitch;
 633
 634        return 0;
 635}
 636
 637static int u_audio_pitch_put(struct snd_kcontrol *kcontrol,
 638                                  struct snd_ctl_elem_value *ucontrol)
 639{
 640        struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
 641        struct snd_uac_chip *uac = prm->uac;
 642        struct g_audio *audio_dev = uac->audio_dev;
 643        struct uac_params *params = &audio_dev->params;
 644        unsigned int val;
 645        unsigned int pitch_min, pitch_max;
 646        int change = 0;
 647
 648        pitch_min = (1000 - FBACK_SLOW_MAX) * 1000;
 649        pitch_max = (1000 + params->fb_max) * 1000;
 650
 651        val = ucontrol->value.integer.value[0];
 652
 653        if (val < pitch_min)
 654                val = pitch_min;
 655        if (val > pitch_max)
 656                val = pitch_max;
 657
 658        if (prm->pitch != val) {
 659                prm->pitch = val;
 660                change = 1;
 661        }
 662
 663        return change;
 664}
 665
 666static const struct snd_kcontrol_new u_audio_controls[]  = {
 667{
 668        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
 669        .name =         "Capture Pitch 1000000",
 670        .info =         u_audio_pitch_info,
 671        .get =          u_audio_pitch_get,
 672        .put =          u_audio_pitch_put,
 673},
 674};
 675
 676int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
 677                                        const char *card_name)
 678{
 679        struct snd_uac_chip *uac;
 680        struct snd_card *card;
 681        struct snd_pcm *pcm;
 682        struct snd_kcontrol *kctl;
 683        struct uac_params *params;
 684        int p_chmask, c_chmask;
 685        int err;
 686
 687        if (!g_audio)
 688                return -EINVAL;
 689
 690        uac = kzalloc(sizeof(*uac), GFP_KERNEL);
 691        if (!uac)
 692                return -ENOMEM;
 693        g_audio->uac = uac;
 694        uac->audio_dev = g_audio;
 695
 696        params = &g_audio->params;
 697        p_chmask = params->p_chmask;
 698        c_chmask = params->c_chmask;
 699
 700        if (c_chmask) {
 701                struct uac_rtd_params *prm = &uac->c_prm;
 702
 703                uac->c_prm.uac = uac;
 704                prm->max_psize = g_audio->out_ep_maxpsize;
 705
 706                prm->reqs = kcalloc(params->req_number,
 707                                    sizeof(struct usb_request *),
 708                                    GFP_KERNEL);
 709                if (!prm->reqs) {
 710                        err = -ENOMEM;
 711                        goto fail;
 712                }
 713
 714                prm->rbuf = kcalloc(params->req_number, prm->max_psize,
 715                                GFP_KERNEL);
 716                if (!prm->rbuf) {
 717                        prm->max_psize = 0;
 718                        err = -ENOMEM;
 719                        goto fail;
 720                }
 721        }
 722
 723        if (p_chmask) {
 724                struct uac_rtd_params *prm = &uac->p_prm;
 725
 726                uac->p_prm.uac = uac;
 727                prm->max_psize = g_audio->in_ep_maxpsize;
 728
 729                prm->reqs = kcalloc(params->req_number,
 730                                    sizeof(struct usb_request *),
 731                                    GFP_KERNEL);
 732                if (!prm->reqs) {
 733                        err = -ENOMEM;
 734                        goto fail;
 735                }
 736
 737                prm->rbuf = kcalloc(params->req_number, prm->max_psize,
 738                                GFP_KERNEL);
 739                if (!prm->rbuf) {
 740                        prm->max_psize = 0;
 741                        err = -ENOMEM;
 742                        goto fail;
 743                }
 744        }
 745
 746        /* Choose any slot, with no id */
 747        err = snd_card_new(&g_audio->gadget->dev,
 748                        -1, NULL, THIS_MODULE, 0, &card);
 749        if (err < 0)
 750                goto fail;
 751
 752        uac->card = card;
 753
 754        /*
 755         * Create first PCM device
 756         * Create a substream only for non-zero channel streams
 757         */
 758        err = snd_pcm_new(uac->card, pcm_name, 0,
 759                               p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
 760        if (err < 0)
 761                goto snd_fail;
 762
 763        strscpy(pcm->name, pcm_name, sizeof(pcm->name));
 764        pcm->private_data = uac;
 765        uac->pcm = pcm;
 766
 767        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops);
 768        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops);
 769
 770        if (c_chmask && g_audio->in_ep_fback) {
 771                strscpy(card->mixername, card_name, sizeof(card->driver));
 772
 773                kctl = snd_ctl_new1(&u_audio_controls[0], &uac->c_prm);
 774                if (!kctl) {
 775                        err = -ENOMEM;
 776                        goto snd_fail;
 777                }
 778
 779                kctl->id.device = pcm->device;
 780                kctl->id.subdevice = 0;
 781
 782                err = snd_ctl_add(card, kctl);
 783                if (err < 0)
 784                        goto snd_fail;
 785        }
 786
 787        strscpy(card->driver, card_name, sizeof(card->driver));
 788        strscpy(card->shortname, card_name, sizeof(card->shortname));
 789        sprintf(card->longname, "%s %i", card_name, card->dev->id);
 790
 791        snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
 792                                       NULL, 0, BUFF_SIZE_MAX);
 793
 794        err = snd_card_register(card);
 795
 796        if (!err)
 797                return 0;
 798
 799snd_fail:
 800        snd_card_free(card);
 801fail:
 802        kfree(uac->p_prm.reqs);
 803        kfree(uac->c_prm.reqs);
 804        kfree(uac->p_prm.rbuf);
 805        kfree(uac->c_prm.rbuf);
 806        kfree(uac);
 807
 808        return err;
 809}
 810EXPORT_SYMBOL_GPL(g_audio_setup);
 811
 812void g_audio_cleanup(struct g_audio *g_audio)
 813{
 814        struct snd_uac_chip *uac;
 815        struct snd_card *card;
 816
 817        if (!g_audio || !g_audio->uac)
 818                return;
 819
 820        uac = g_audio->uac;
 821        card = uac->card;
 822        if (card)
 823                snd_card_free(card);
 824
 825        kfree(uac->p_prm.reqs);
 826        kfree(uac->c_prm.reqs);
 827        kfree(uac->p_prm.rbuf);
 828        kfree(uac->c_prm.rbuf);
 829        kfree(uac);
 830}
 831EXPORT_SYMBOL_GPL(g_audio_cleanup);
 832
 833MODULE_LICENSE("GPL");
 834MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities");
 835MODULE_AUTHOR("Ruslan Bilovol");
 836