linux/sound/usb/pcm.c
<<
>>
Prefs
   1/*
   2 *   This program is free software; you can redistribute it and/or modify
   3 *   it under the terms of the GNU General Public License as published by
   4 *   the Free Software Foundation; either version 2 of the License, or
   5 *   (at your option) any later version.
   6 *
   7 *   This program is distributed in the hope that it will be useful,
   8 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   9 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10 *   GNU General Public License for more details.
  11 *
  12 *   You should have received a copy of the GNU General Public License
  13 *   along with this program; if not, write to the Free Software
  14 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  15 */
  16
  17#include <linux/init.h>
  18#include <linux/slab.h>
  19#include <linux/bitrev.h>
  20#include <linux/ratelimit.h>
  21#include <linux/usb.h>
  22#include <linux/usb/audio.h>
  23#include <linux/usb/audio-v2.h>
  24
  25#include <sound/core.h>
  26#include <sound/pcm.h>
  27#include <sound/pcm_params.h>
  28
  29#include "usbaudio.h"
  30#include "card.h"
  31#include "quirks.h"
  32#include "debug.h"
  33#include "endpoint.h"
  34#include "helper.h"
  35#include "pcm.h"
  36#include "clock.h"
  37#include "power.h"
  38
  39#define SUBSTREAM_FLAG_DATA_EP_STARTED  0
  40#define SUBSTREAM_FLAG_SYNC_EP_STARTED  1
  41
  42/* return the estimated delay based on USB frame counters */
  43snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
  44                                    unsigned int rate)
  45{
  46        int current_frame_number;
  47        int frame_diff;
  48        int est_delay;
  49
  50        if (!subs->last_delay)
  51                return 0; /* short path */
  52
  53        current_frame_number = usb_get_current_frame_number(subs->dev);
  54        /*
  55         * HCD implementations use different widths, use lower 8 bits.
  56         * The delay will be managed up to 256ms, which is more than
  57         * enough
  58         */
  59        frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
  60
  61        /* Approximation based on number of samples per USB frame (ms),
  62           some truncation for 44.1 but the estimate is good enough */
  63        est_delay =  frame_diff * rate / 1000;
  64        if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
  65                est_delay = subs->last_delay - est_delay;
  66        else
  67                est_delay = subs->last_delay + est_delay;
  68
  69        if (est_delay < 0)
  70                est_delay = 0;
  71        return est_delay;
  72}
  73
  74/*
  75 * return the current pcm pointer.  just based on the hwptr_done value.
  76 */
  77static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
  78{
  79        struct snd_usb_substream *subs;
  80        unsigned int hwptr_done;
  81
  82        subs = (struct snd_usb_substream *)substream->runtime->private_data;
  83        if (subs->stream->chip->shutdown)
  84                return SNDRV_PCM_POS_XRUN;
  85        spin_lock(&subs->lock);
  86        hwptr_done = subs->hwptr_done;
  87        substream->runtime->delay = snd_usb_pcm_delay(subs,
  88                                                substream->runtime->rate);
  89        spin_unlock(&subs->lock);
  90        return hwptr_done / (substream->runtime->frame_bits >> 3);
  91}
  92
  93/*
  94 * find a matching audio format
  95 */
  96static struct audioformat *find_format(struct snd_usb_substream *subs)
  97{
  98        struct audioformat *fp;
  99        struct audioformat *found = NULL;
 100        int cur_attr = 0, attr;
 101
 102        list_for_each_entry(fp, &subs->fmt_list, list) {
 103                if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
 104                        continue;
 105                if (fp->channels != subs->channels)
 106                        continue;
 107                if (subs->cur_rate < fp->rate_min ||
 108                    subs->cur_rate > fp->rate_max)
 109                        continue;
 110                if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
 111                        unsigned int i;
 112                        for (i = 0; i < fp->nr_rates; i++)
 113                                if (fp->rate_table[i] == subs->cur_rate)
 114                                        break;
 115                        if (i >= fp->nr_rates)
 116                                continue;
 117                }
 118                attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
 119                if (! found) {
 120                        found = fp;
 121                        cur_attr = attr;
 122                        continue;
 123                }
 124                /* avoid async out and adaptive in if the other method
 125                 * supports the same format.
 126                 * this is a workaround for the case like
 127                 * M-audio audiophile USB.
 128                 */
 129                if (attr != cur_attr) {
 130                        if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
 131                             subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
 132                            (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
 133                             subs->direction == SNDRV_PCM_STREAM_CAPTURE))
 134                                continue;
 135                        if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
 136                             subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
 137                            (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
 138                             subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
 139                                found = fp;
 140                                cur_attr = attr;
 141                                continue;
 142                        }
 143                }
 144                /* find the format with the largest max. packet size */
 145                if (fp->maxpacksize > found->maxpacksize) {
 146                        found = fp;
 147                        cur_attr = attr;
 148                }
 149        }
 150        return found;
 151}
 152
 153static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
 154                         struct usb_host_interface *alts,
 155                         struct audioformat *fmt)
 156{
 157        struct usb_device *dev = chip->dev;
 158        unsigned int ep;
 159        unsigned char data[1];
 160        int err;
 161
 162        ep = get_endpoint(alts, 0)->bEndpointAddress;
 163
 164        data[0] = 1;
 165        if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
 166                                   USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
 167                                   UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
 168                                   data, sizeof(data))) < 0) {
 169                snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
 170                           dev->devnum, iface, ep);
 171                return err;
 172        }
 173
 174        return 0;
 175}
 176
 177static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
 178                         struct usb_host_interface *alts,
 179                         struct audioformat *fmt)
 180{
 181        struct usb_device *dev = chip->dev;
 182        unsigned char data[1];
 183        int err;
 184
 185        data[0] = 1;
 186        if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
 187                                   USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
 188                                   UAC2_EP_CS_PITCH << 8, 0,
 189                                   data, sizeof(data))) < 0) {
 190                snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
 191                           dev->devnum, iface, fmt->altsetting);
 192                return err;
 193        }
 194
 195        return 0;
 196}
 197
 198/*
 199 * initialize the pitch control and sample rate
 200 */
 201int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
 202                       struct usb_host_interface *alts,
 203                       struct audioformat *fmt)
 204{
 205        /* if endpoint doesn't have pitch control, bail out */
 206        if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
 207                return 0;
 208
 209        switch (fmt->protocol) {
 210        case UAC_VERSION_1:
 211        default:
 212                return init_pitch_v1(chip, iface, alts, fmt);
 213
 214        case UAC_VERSION_2:
 215                return init_pitch_v2(chip, iface, alts, fmt);
 216        }
 217}
 218
 219static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
 220{
 221        int err;
 222
 223        if (!subs->data_endpoint)
 224                return -EINVAL;
 225
 226        if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
 227                struct snd_usb_endpoint *ep = subs->data_endpoint;
 228
 229                snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
 230
 231                ep->data_subs = subs;
 232                err = snd_usb_endpoint_start(ep, can_sleep);
 233                if (err < 0) {
 234                        clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
 235                        return err;
 236                }
 237        }
 238
 239        if (subs->sync_endpoint &&
 240            !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
 241                struct snd_usb_endpoint *ep = subs->sync_endpoint;
 242
 243                if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
 244                    subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
 245                        err = usb_set_interface(subs->dev,
 246                                                subs->sync_endpoint->iface,
 247                                                subs->sync_endpoint->alt_idx);
 248                        if (err < 0) {
 249                                snd_printk(KERN_ERR
 250                                           "%d:%d:%d: cannot set interface (%d)\n",
 251                                           subs->dev->devnum,
 252                                           subs->sync_endpoint->iface,
 253                                           subs->sync_endpoint->alt_idx, err);
 254                                return -EIO;
 255                        }
 256                }
 257
 258                snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
 259
 260                ep->sync_slave = subs->data_endpoint;
 261                err = snd_usb_endpoint_start(ep, can_sleep);
 262                if (err < 0) {
 263                        clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
 264                        return err;
 265                }
 266        }
 267
 268        return 0;
 269}
 270
 271static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
 272{
 273        if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
 274                snd_usb_endpoint_stop(subs->sync_endpoint);
 275
 276        if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
 277                snd_usb_endpoint_stop(subs->data_endpoint);
 278
 279        if (wait) {
 280                snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
 281                snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
 282        }
 283}
 284
 285static int deactivate_endpoints(struct snd_usb_substream *subs)
 286{
 287        int reta, retb;
 288
 289        reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
 290        retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
 291
 292        if (reta < 0)
 293                return reta;
 294
 295        if (retb < 0)
 296                return retb;
 297
 298        return 0;
 299}
 300
 301static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
 302                                     unsigned int altsetting,
 303                                     struct usb_host_interface **alts,
 304                                     unsigned int *ep)
 305{
 306        struct usb_interface *iface;
 307        struct usb_interface_descriptor *altsd;
 308        struct usb_endpoint_descriptor *epd;
 309
 310        iface = usb_ifnum_to_if(dev, ifnum);
 311        if (!iface || iface->num_altsetting < altsetting + 1)
 312                return -ENOENT;
 313        *alts = &iface->altsetting[altsetting];
 314        altsd = get_iface_desc(*alts);
 315        if (altsd->bAlternateSetting != altsetting ||
 316            altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
 317            (altsd->bInterfaceSubClass != 2 &&
 318             altsd->bInterfaceProtocol != 2   ) ||
 319            altsd->bNumEndpoints < 1)
 320                return -ENOENT;
 321        epd = get_endpoint(*alts, 0);
 322        if (!usb_endpoint_is_isoc_in(epd) ||
 323            (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
 324                                        USB_ENDPOINT_USAGE_IMPLICIT_FB)
 325                return -ENOENT;
 326        *ep = epd->bEndpointAddress;
 327        return 0;
 328}
 329
 330/*
 331 * find a matching format and set up the interface
 332 */
 333static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
 334{
 335        struct usb_device *dev = subs->dev;
 336        struct usb_host_interface *alts;
 337        struct usb_interface_descriptor *altsd;
 338        struct usb_interface *iface;
 339        unsigned int ep, attr;
 340        int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
 341        int err, implicit_fb = 0;
 342
 343        iface = usb_ifnum_to_if(dev, fmt->iface);
 344        if (WARN_ON(!iface))
 345                return -EINVAL;
 346        alts = &iface->altsetting[fmt->altset_idx];
 347        altsd = get_iface_desc(alts);
 348        if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
 349                return -EINVAL;
 350
 351        if (fmt == subs->cur_audiofmt)
 352                return 0;
 353
 354        /* close the old interface */
 355        if (subs->interface >= 0 && subs->interface != fmt->iface) {
 356                err = usb_set_interface(subs->dev, subs->interface, 0);
 357                if (err < 0) {
 358                        snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
 359                                dev->devnum, fmt->iface, fmt->altsetting, err);
 360                        return -EIO;
 361                }
 362                subs->interface = -1;
 363                subs->altset_idx = 0;
 364        }
 365
 366        /* set interface */
 367        if (subs->interface != fmt->iface ||
 368            subs->altset_idx != fmt->altset_idx) {
 369                err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
 370                if (err < 0) {
 371                        snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
 372                                   dev->devnum, fmt->iface, fmt->altsetting, err);
 373                        return -EIO;
 374                }
 375                snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
 376                                fmt->iface, fmt->altsetting);
 377                subs->interface = fmt->iface;
 378                subs->altset_idx = fmt->altset_idx;
 379
 380                snd_usb_set_interface_quirk(dev);
 381        }
 382
 383        subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
 384                                                   alts, fmt->endpoint, subs->direction,
 385                                                   SND_USB_ENDPOINT_TYPE_DATA);
 386        if (!subs->data_endpoint)
 387                return -EINVAL;
 388
 389        /* we need a sync pipe in async OUT or adaptive IN mode */
 390        /* check the number of EP, since some devices have broken
 391         * descriptors which fool us.  if it has only one EP,
 392         * assume it as adaptive-out or sync-in.
 393         */
 394        attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
 395
 396        switch (subs->stream->chip->usb_id) {
 397        case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
 398        case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
 399                if (is_playback) {
 400                        implicit_fb = 1;
 401                        ep = 0x81;
 402                        iface = usb_ifnum_to_if(dev, 3);
 403
 404                        if (!iface || iface->num_altsetting == 0)
 405                                return -EINVAL;
 406
 407                        alts = &iface->altsetting[1];
 408                        goto add_sync_ep;
 409                }
 410                break;
 411        case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
 412        case USB_ID(0x0763, 0x2081):
 413                if (is_playback) {
 414                        implicit_fb = 1;
 415                        ep = 0x81;
 416                        iface = usb_ifnum_to_if(dev, 2);
 417
 418                        if (!iface || iface->num_altsetting == 0)
 419                                return -EINVAL;
 420
 421                        alts = &iface->altsetting[1];
 422                        goto add_sync_ep;
 423                }
 424        }
 425        if (is_playback &&
 426            attr == USB_ENDPOINT_SYNC_ASYNC &&
 427            altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
 428            altsd->bInterfaceProtocol == 2 &&
 429            altsd->bNumEndpoints == 1 &&
 430            USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
 431            search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
 432                                      altsd->bAlternateSetting,
 433                                      &alts, &ep) >= 0) {
 434                implicit_fb = 1;
 435                goto add_sync_ep;
 436        }
 437
 438        if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
 439             (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
 440            altsd->bNumEndpoints >= 2) {
 441                /* check sync-pipe endpoint */
 442                /* ... and check descriptor size before accessing bSynchAddress
 443                   because there is a version of the SB Audigy 2 NX firmware lacking
 444                   the audio fields in the endpoint descriptors */
 445                if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
 446                    (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
 447                     get_endpoint(alts, 1)->bSynchAddress != 0 &&
 448                     !implicit_fb)) {
 449                        snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
 450                                   dev->devnum, fmt->iface, fmt->altsetting,
 451                                   get_endpoint(alts, 1)->bmAttributes,
 452                                   get_endpoint(alts, 1)->bLength,
 453                                   get_endpoint(alts, 1)->bSynchAddress);
 454                        return -EINVAL;
 455                }
 456                ep = get_endpoint(alts, 1)->bEndpointAddress;
 457                if (!implicit_fb &&
 458                    get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
 459                    (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
 460                     (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
 461                        snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
 462                                   dev->devnum, fmt->iface, fmt->altsetting,
 463                                   is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
 464                        return -EINVAL;
 465                }
 466
 467                implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
 468                                == USB_ENDPOINT_USAGE_IMPLICIT_FB;
 469
 470add_sync_ep:
 471                subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
 472                                                           alts, ep, !subs->direction,
 473                                                           implicit_fb ?
 474                                                                SND_USB_ENDPOINT_TYPE_DATA :
 475                                                                SND_USB_ENDPOINT_TYPE_SYNC);
 476                if (!subs->sync_endpoint)
 477                        return -EINVAL;
 478
 479                subs->data_endpoint->sync_master = subs->sync_endpoint;
 480        }
 481
 482        if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
 483                return err;
 484
 485        subs->cur_audiofmt = fmt;
 486
 487        snd_usb_set_format_quirk(subs, fmt);
 488
 489#if 0
 490        printk(KERN_DEBUG
 491               "setting done: format = %d, rate = %d..%d, channels = %d\n",
 492               fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
 493        printk(KERN_DEBUG
 494               "  datapipe = 0x%0x, syncpipe = 0x%0x\n",
 495               subs->datapipe, subs->syncpipe);
 496#endif
 497
 498        return 0;
 499}
 500
 501/*
 502 * Return the score of matching two audioformats.
 503 * Veto the audioformat if:
 504 * - It has no channels for some reason.
 505 * - Requested PCM format is not supported.
 506 * - Requested sample rate is not supported.
 507 */
 508static int match_endpoint_audioformats(struct audioformat *fp,
 509        struct audioformat *match, int rate,
 510        snd_pcm_format_t pcm_format)
 511{
 512        int i;
 513        int score = 0;
 514
 515        if (fp->channels < 1) {
 516                snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
 517                return 0;
 518        }
 519
 520        if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
 521                snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
 522                        fp, pcm_format);
 523                return 0;
 524        }
 525
 526        for (i = 0; i < fp->nr_rates; i++) {
 527                if (fp->rate_table[i] == rate) {
 528                        score++;
 529                        break;
 530                }
 531        }
 532        if (!score) {
 533                snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
 534                        fp, rate);
 535                return 0;
 536        }
 537
 538        if (fp->channels == match->channels)
 539                score++;
 540
 541        snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
 542
 543        return score;
 544}
 545
 546/*
 547 * Configure the sync ep using the rate and pcm format of the data ep.
 548 */
 549static int configure_sync_endpoint(struct snd_usb_substream *subs)
 550{
 551        int ret;
 552        struct audioformat *fp;
 553        struct audioformat *sync_fp = NULL;
 554        int cur_score = 0;
 555        int sync_period_bytes = subs->period_bytes;
 556        struct snd_usb_substream *sync_subs =
 557                &subs->stream->substream[subs->direction ^ 1];
 558
 559        if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
 560            !subs->stream)
 561                return snd_usb_endpoint_set_params(subs->sync_endpoint,
 562                                                   subs->pcm_format,
 563                                                   subs->channels,
 564                                                   subs->period_bytes,
 565                                                   subs->cur_rate,
 566                                                   subs->cur_audiofmt,
 567                                                   NULL);
 568
 569        /* Try to find the best matching audioformat. */
 570        list_for_each_entry(fp, &sync_subs->fmt_list, list) {
 571                int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
 572                        subs->cur_rate, subs->pcm_format);
 573
 574                if (score > cur_score) {
 575                        sync_fp = fp;
 576                        cur_score = score;
 577                }
 578        }
 579
 580        if (unlikely(sync_fp == NULL)) {
 581                snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
 582                        __func__, sync_subs->ep_num);
 583                return -EINVAL;
 584        }
 585
 586        /*
 587         * Recalculate the period bytes if channel number differ between
 588         * data and sync ep audioformat.
 589         */
 590        if (sync_fp->channels != subs->channels) {
 591                sync_period_bytes = (subs->period_bytes / subs->channels) *
 592                        sync_fp->channels;
 593                snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
 594                        __func__, subs->period_bytes, sync_period_bytes);
 595        }
 596
 597        ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
 598                                          subs->pcm_format,
 599                                          sync_fp->channels,
 600                                          sync_period_bytes,
 601                                          subs->cur_rate,
 602                                          sync_fp,
 603                                          NULL);
 604
 605        return ret;
 606}
 607
 608/*
 609 * configure endpoint params
 610 *
 611 * called  during initial setup and upon resume
 612 */
 613static int configure_endpoint(struct snd_usb_substream *subs)
 614{
 615        int ret;
 616
 617        /* format changed */
 618        stop_endpoints(subs, true);
 619        ret = snd_usb_endpoint_set_params(subs->data_endpoint,
 620                                          subs->pcm_format,
 621                                          subs->channels,
 622                                          subs->period_bytes,
 623                                          subs->cur_rate,
 624                                          subs->cur_audiofmt,
 625                                          subs->sync_endpoint);
 626        if (ret < 0)
 627                return ret;
 628
 629        if (subs->sync_endpoint)
 630                ret = configure_sync_endpoint(subs);
 631
 632        return ret;
 633}
 634
 635/*
 636 * hw_params callback
 637 *
 638 * allocate a buffer and set the given audio format.
 639 *
 640 * so far we use a physically linear buffer although packetize transfer
 641 * doesn't need a continuous area.
 642 * if sg buffer is supported on the later version of alsa, we'll follow
 643 * that.
 644 */
 645static int snd_usb_hw_params(struct snd_pcm_substream *substream,
 646                             struct snd_pcm_hw_params *hw_params)
 647{
 648        struct snd_usb_substream *subs = substream->runtime->private_data;
 649        struct audioformat *fmt;
 650        int ret;
 651
 652        ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
 653                                               params_buffer_bytes(hw_params));
 654        if (ret < 0)
 655                return ret;
 656
 657        subs->pcm_format = params_format(hw_params);
 658        subs->period_bytes = params_period_bytes(hw_params);
 659        subs->channels = params_channels(hw_params);
 660        subs->cur_rate = params_rate(hw_params);
 661
 662        fmt = find_format(subs);
 663        if (!fmt) {
 664                snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
 665                           subs->pcm_format, subs->cur_rate, subs->channels);
 666                return -EINVAL;
 667        }
 668
 669        down_read(&subs->stream->chip->shutdown_rwsem);
 670        if (subs->stream->chip->shutdown)
 671                ret = -ENODEV;
 672        else
 673                ret = set_format(subs, fmt);
 674        up_read(&subs->stream->chip->shutdown_rwsem);
 675        if (ret < 0)
 676                return ret;
 677
 678        subs->interface = fmt->iface;
 679        subs->altset_idx = fmt->altset_idx;
 680        subs->need_setup_ep = true;
 681
 682        return 0;
 683}
 684
 685/*
 686 * hw_free callback
 687 *
 688 * reset the audio format and release the buffer
 689 */
 690static int snd_usb_hw_free(struct snd_pcm_substream *substream)
 691{
 692        struct snd_usb_substream *subs = substream->runtime->private_data;
 693
 694        subs->cur_audiofmt = NULL;
 695        subs->cur_rate = 0;
 696        subs->period_bytes = 0;
 697        down_read(&subs->stream->chip->shutdown_rwsem);
 698        if (!subs->stream->chip->shutdown) {
 699                stop_endpoints(subs, true);
 700                deactivate_endpoints(subs);
 701        }
 702        up_read(&subs->stream->chip->shutdown_rwsem);
 703        return snd_pcm_lib_free_vmalloc_buffer(substream);
 704}
 705
 706/*
 707 * prepare callback
 708 *
 709 * only a few subtle things...
 710 */
 711static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
 712{
 713        struct snd_pcm_runtime *runtime = substream->runtime;
 714        struct snd_usb_substream *subs = runtime->private_data;
 715        struct usb_host_interface *alts;
 716        struct usb_interface *iface;
 717        int ret;
 718
 719        if (! subs->cur_audiofmt) {
 720                snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
 721                return -ENXIO;
 722        }
 723
 724        down_read(&subs->stream->chip->shutdown_rwsem);
 725        if (subs->stream->chip->shutdown) {
 726                ret = -ENODEV;
 727                goto unlock;
 728        }
 729        if (snd_BUG_ON(!subs->data_endpoint)) {
 730                ret = -EIO;
 731                goto unlock;
 732        }
 733
 734        snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
 735        snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
 736
 737        ret = set_format(subs, subs->cur_audiofmt);
 738        if (ret < 0)
 739                goto unlock;
 740
 741        iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
 742        alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
 743        ret = snd_usb_init_sample_rate(subs->stream->chip,
 744                                       subs->cur_audiofmt->iface,
 745                                       alts,
 746                                       subs->cur_audiofmt,
 747                                       subs->cur_rate);
 748        if (ret < 0)
 749                goto unlock;
 750
 751        if (subs->need_setup_ep) {
 752                ret = configure_endpoint(subs);
 753                if (ret < 0)
 754                        goto unlock;
 755                subs->need_setup_ep = false;
 756        }
 757
 758        /* some unit conversions in runtime */
 759        subs->data_endpoint->maxframesize =
 760                bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
 761        subs->data_endpoint->curframesize =
 762                bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
 763
 764        /* reset the pointer */
 765        subs->hwptr_done = 0;
 766        subs->transfer_done = 0;
 767        subs->last_delay = 0;
 768        subs->last_frame_number = 0;
 769        runtime->delay = 0;
 770
 771        /* for playback, submit the URBs now; otherwise, the first hwptr_done
 772         * updates for all URBs would happen at the same time when starting */
 773        if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
 774                ret = start_endpoints(subs, true);
 775
 776 unlock:
 777        up_read(&subs->stream->chip->shutdown_rwsem);
 778        return ret;
 779}
 780
 781static struct snd_pcm_hardware snd_usb_hardware =
 782{
 783        .info =                 SNDRV_PCM_INFO_MMAP |
 784                                SNDRV_PCM_INFO_MMAP_VALID |
 785                                SNDRV_PCM_INFO_BATCH |
 786                                SNDRV_PCM_INFO_INTERLEAVED |
 787                                SNDRV_PCM_INFO_BLOCK_TRANSFER |
 788                                SNDRV_PCM_INFO_PAUSE,
 789        .buffer_bytes_max =     1024 * 1024,
 790        .period_bytes_min =     64,
 791        .period_bytes_max =     512 * 1024,
 792        .periods_min =          2,
 793        .periods_max =          1024,
 794};
 795
 796static int hw_check_valid_format(struct snd_usb_substream *subs,
 797                                 struct snd_pcm_hw_params *params,
 798                                 struct audioformat *fp)
 799{
 800        struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
 801        struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
 802        struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
 803        struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
 804        struct snd_mask check_fmts;
 805        unsigned int ptime;
 806
 807        /* check the format */
 808        snd_mask_none(&check_fmts);
 809        check_fmts.bits[0] = (u32)fp->formats;
 810        check_fmts.bits[1] = (u32)(fp->formats >> 32);
 811        snd_mask_intersect(&check_fmts, fmts);
 812        if (snd_mask_empty(&check_fmts)) {
 813                hwc_debug("   > check: no supported format %d\n", fp->format);
 814                return 0;
 815        }
 816        /* check the channels */
 817        if (fp->channels < ct->min || fp->channels > ct->max) {
 818                hwc_debug("   > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
 819                return 0;
 820        }
 821        /* check the rate is within the range */
 822        if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
 823                hwc_debug("   > check: rate_min %d > max %d\n", fp->rate_min, it->max);
 824                return 0;
 825        }
 826        if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
 827                hwc_debug("   > check: rate_max %d < min %d\n", fp->rate_max, it->min);
 828                return 0;
 829        }
 830        /* check whether the period time is >= the data packet interval */
 831        if (subs->speed != USB_SPEED_FULL) {
 832                ptime = 125 * (1 << fp->datainterval);
 833                if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
 834                        hwc_debug("   > check: ptime %u > max %u\n", ptime, pt->max);
 835                        return 0;
 836                }
 837        }
 838        return 1;
 839}
 840
 841static int hw_rule_rate(struct snd_pcm_hw_params *params,
 842                        struct snd_pcm_hw_rule *rule)
 843{
 844        struct snd_usb_substream *subs = rule->private;
 845        struct audioformat *fp;
 846        struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
 847        unsigned int rmin, rmax;
 848        int changed;
 849
 850        hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
 851        changed = 0;
 852        rmin = rmax = 0;
 853        list_for_each_entry(fp, &subs->fmt_list, list) {
 854                if (!hw_check_valid_format(subs, params, fp))
 855                        continue;
 856                if (changed++) {
 857                        if (rmin > fp->rate_min)
 858                                rmin = fp->rate_min;
 859                        if (rmax < fp->rate_max)
 860                                rmax = fp->rate_max;
 861                } else {
 862                        rmin = fp->rate_min;
 863                        rmax = fp->rate_max;
 864                }
 865        }
 866
 867        if (!changed) {
 868                hwc_debug("  --> get empty\n");
 869                it->empty = 1;
 870                return -EINVAL;
 871        }
 872
 873        changed = 0;
 874        if (it->min < rmin) {
 875                it->min = rmin;
 876                it->openmin = 0;
 877                changed = 1;
 878        }
 879        if (it->max > rmax) {
 880                it->max = rmax;
 881                it->openmax = 0;
 882                changed = 1;
 883        }
 884        if (snd_interval_checkempty(it)) {
 885                it->empty = 1;
 886                return -EINVAL;
 887        }
 888        hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
 889        return changed;
 890}
 891
 892
 893static int hw_rule_channels(struct snd_pcm_hw_params *params,
 894                            struct snd_pcm_hw_rule *rule)
 895{
 896        struct snd_usb_substream *subs = rule->private;
 897        struct audioformat *fp;
 898        struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
 899        unsigned int rmin, rmax;
 900        int changed;
 901
 902        hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
 903        changed = 0;
 904        rmin = rmax = 0;
 905        list_for_each_entry(fp, &subs->fmt_list, list) {
 906                if (!hw_check_valid_format(subs, params, fp))
 907                        continue;
 908                if (changed++) {
 909                        if (rmin > fp->channels)
 910                                rmin = fp->channels;
 911                        if (rmax < fp->channels)
 912                                rmax = fp->channels;
 913                } else {
 914                        rmin = fp->channels;
 915                        rmax = fp->channels;
 916                }
 917        }
 918
 919        if (!changed) {
 920                hwc_debug("  --> get empty\n");
 921                it->empty = 1;
 922                return -EINVAL;
 923        }
 924
 925        changed = 0;
 926        if (it->min < rmin) {
 927                it->min = rmin;
 928                it->openmin = 0;
 929                changed = 1;
 930        }
 931        if (it->max > rmax) {
 932                it->max = rmax;
 933                it->openmax = 0;
 934                changed = 1;
 935        }
 936        if (snd_interval_checkempty(it)) {
 937                it->empty = 1;
 938                return -EINVAL;
 939        }
 940        hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
 941        return changed;
 942}
 943
 944static int hw_rule_format(struct snd_pcm_hw_params *params,
 945                          struct snd_pcm_hw_rule *rule)
 946{
 947        struct snd_usb_substream *subs = rule->private;
 948        struct audioformat *fp;
 949        struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
 950        u64 fbits;
 951        u32 oldbits[2];
 952        int changed;
 953
 954        hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
 955        fbits = 0;
 956        list_for_each_entry(fp, &subs->fmt_list, list) {
 957                if (!hw_check_valid_format(subs, params, fp))
 958                        continue;
 959                fbits |= fp->formats;
 960        }
 961
 962        oldbits[0] = fmt->bits[0];
 963        oldbits[1] = fmt->bits[1];
 964        fmt->bits[0] &= (u32)fbits;
 965        fmt->bits[1] &= (u32)(fbits >> 32);
 966        if (!fmt->bits[0] && !fmt->bits[1]) {
 967                hwc_debug("  --> get empty\n");
 968                return -EINVAL;
 969        }
 970        changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
 971        hwc_debug("  --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
 972        return changed;
 973}
 974
 975static int hw_rule_period_time(struct snd_pcm_hw_params *params,
 976                               struct snd_pcm_hw_rule *rule)
 977{
 978        struct snd_usb_substream *subs = rule->private;
 979        struct audioformat *fp;
 980        struct snd_interval *it;
 981        unsigned char min_datainterval;
 982        unsigned int pmin;
 983        int changed;
 984
 985        it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
 986        hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
 987        min_datainterval = 0xff;
 988        list_for_each_entry(fp, &subs->fmt_list, list) {
 989                if (!hw_check_valid_format(subs, params, fp))
 990                        continue;
 991                min_datainterval = min(min_datainterval, fp->datainterval);
 992        }
 993        if (min_datainterval == 0xff) {
 994                hwc_debug("  --> get empty\n");
 995                it->empty = 1;
 996                return -EINVAL;
 997        }
 998        pmin = 125 * (1 << min_datainterval);
 999        changed = 0;
1000        if (it->min < pmin) {
1001                it->min = pmin;
1002                it->openmin = 0;
1003                changed = 1;
1004        }
1005        if (snd_interval_checkempty(it)) {
1006                it->empty = 1;
1007                return -EINVAL;
1008        }
1009        hwc_debug("  --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1010        return changed;
1011}
1012
1013/*
1014 *  If the device supports unusual bit rates, does the request meet these?
1015 */
1016static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1017                                  struct snd_usb_substream *subs)
1018{
1019        struct audioformat *fp;
1020        int *rate_list;
1021        int count = 0, needs_knot = 0;
1022        int err;
1023
1024        kfree(subs->rate_list.list);
1025        subs->rate_list.list = NULL;
1026
1027        list_for_each_entry(fp, &subs->fmt_list, list) {
1028                if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1029                        return 0;
1030                count += fp->nr_rates;
1031                if (fp->rates & SNDRV_PCM_RATE_KNOT)
1032                        needs_knot = 1;
1033        }
1034        if (!needs_knot)
1035                return 0;
1036
1037        subs->rate_list.list = rate_list =
1038                kmalloc(sizeof(int) * count, GFP_KERNEL);
1039        if (!subs->rate_list.list)
1040                return -ENOMEM;
1041        subs->rate_list.count = count;
1042        subs->rate_list.mask = 0;
1043        count = 0;
1044        list_for_each_entry(fp, &subs->fmt_list, list) {
1045                int i;
1046                for (i = 0; i < fp->nr_rates; i++)
1047                        rate_list[count++] = fp->rate_table[i];
1048        }
1049        err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1050                                         &subs->rate_list);
1051        if (err < 0)
1052                return err;
1053
1054        return 0;
1055}
1056
1057
1058/*
1059 * set up the runtime hardware information.
1060 */
1061
1062static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1063{
1064        struct audioformat *fp;
1065        unsigned int pt, ptmin;
1066        int param_period_time_if_needed;
1067        int err;
1068
1069        runtime->hw.formats = subs->formats;
1070
1071        runtime->hw.rate_min = 0x7fffffff;
1072        runtime->hw.rate_max = 0;
1073        runtime->hw.channels_min = 256;
1074        runtime->hw.channels_max = 0;
1075        runtime->hw.rates = 0;
1076        ptmin = UINT_MAX;
1077        /* check min/max rates and channels */
1078        list_for_each_entry(fp, &subs->fmt_list, list) {
1079                runtime->hw.rates |= fp->rates;
1080                if (runtime->hw.rate_min > fp->rate_min)
1081                        runtime->hw.rate_min = fp->rate_min;
1082                if (runtime->hw.rate_max < fp->rate_max)
1083                        runtime->hw.rate_max = fp->rate_max;
1084                if (runtime->hw.channels_min > fp->channels)
1085                        runtime->hw.channels_min = fp->channels;
1086                if (runtime->hw.channels_max < fp->channels)
1087                        runtime->hw.channels_max = fp->channels;
1088                if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1089                        /* FIXME: there might be more than one audio formats... */
1090                        runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1091                                fp->frame_size;
1092                }
1093                pt = 125 * (1 << fp->datainterval);
1094                ptmin = min(ptmin, pt);
1095        }
1096        err = snd_usb_autoresume(subs->stream->chip);
1097        if (err < 0)
1098                return err;
1099
1100        param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1101        if (subs->speed == USB_SPEED_FULL)
1102                /* full speed devices have fixed data packet interval */
1103                ptmin = 1000;
1104        if (ptmin == 1000)
1105                /* if period time doesn't go below 1 ms, no rules needed */
1106                param_period_time_if_needed = -1;
1107        snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1108                                     ptmin, UINT_MAX);
1109
1110        if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1111                                       hw_rule_rate, subs,
1112                                       SNDRV_PCM_HW_PARAM_FORMAT,
1113                                       SNDRV_PCM_HW_PARAM_CHANNELS,
1114                                       param_period_time_if_needed,
1115                                       -1)) < 0)
1116                goto rep_err;
1117        if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1118                                       hw_rule_channels, subs,
1119                                       SNDRV_PCM_HW_PARAM_FORMAT,
1120                                       SNDRV_PCM_HW_PARAM_RATE,
1121                                       param_period_time_if_needed,
1122                                       -1)) < 0)
1123                goto rep_err;
1124        if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1125                                       hw_rule_format, subs,
1126                                       SNDRV_PCM_HW_PARAM_RATE,
1127                                       SNDRV_PCM_HW_PARAM_CHANNELS,
1128                                       param_period_time_if_needed,
1129                                       -1)) < 0)
1130                goto rep_err;
1131        if (param_period_time_if_needed >= 0) {
1132                err = snd_pcm_hw_rule_add(runtime, 0,
1133                                          SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1134                                          hw_rule_period_time, subs,
1135                                          SNDRV_PCM_HW_PARAM_FORMAT,
1136                                          SNDRV_PCM_HW_PARAM_CHANNELS,
1137                                          SNDRV_PCM_HW_PARAM_RATE,
1138                                          -1);
1139                if (err < 0)
1140                        goto rep_err;
1141        }
1142        if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
1143                goto rep_err;
1144        return 0;
1145
1146rep_err:
1147        snd_usb_autosuspend(subs->stream->chip);
1148        return err;
1149}
1150
1151static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1152{
1153        struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1154        struct snd_pcm_runtime *runtime = substream->runtime;
1155        struct snd_usb_substream *subs = &as->substream[direction];
1156
1157        subs->interface = -1;
1158        subs->altset_idx = 0;
1159        runtime->hw = snd_usb_hardware;
1160        runtime->private_data = subs;
1161        subs->pcm_substream = substream;
1162        /* runtime PM is also done there */
1163
1164        /* initialize DSD/DOP context */
1165        subs->dsd_dop.byte_idx = 0;
1166        subs->dsd_dop.channel = 0;
1167        subs->dsd_dop.marker = 1;
1168
1169        return setup_hw_info(runtime, subs);
1170}
1171
1172static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1173{
1174        struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1175        struct snd_usb_substream *subs = &as->substream[direction];
1176
1177        stop_endpoints(subs, true);
1178
1179        if (!as->chip->shutdown && subs->interface >= 0) {
1180                usb_set_interface(subs->dev, subs->interface, 0);
1181                subs->interface = -1;
1182        }
1183
1184        subs->pcm_substream = NULL;
1185        snd_usb_autosuspend(subs->stream->chip);
1186
1187        return 0;
1188}
1189
1190/* Since a URB can handle only a single linear buffer, we must use double
1191 * buffering when the data to be transferred overflows the buffer boundary.
1192 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1193 * for all URBs.
1194 */
1195static void retire_capture_urb(struct snd_usb_substream *subs,
1196                               struct urb *urb)
1197{
1198        struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1199        unsigned int stride, frames, bytes, oldptr;
1200        int i, period_elapsed = 0;
1201        unsigned long flags;
1202        unsigned char *cp;
1203        int current_frame_number;
1204
1205        /* read frame number here, update pointer in critical section */
1206        current_frame_number = usb_get_current_frame_number(subs->dev);
1207
1208        stride = runtime->frame_bits >> 3;
1209
1210        for (i = 0; i < urb->number_of_packets; i++) {
1211                cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
1212                if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1213                        snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1214                        // continue;
1215                }
1216                bytes = urb->iso_frame_desc[i].actual_length;
1217                frames = bytes / stride;
1218                if (!subs->txfr_quirk)
1219                        bytes = frames * stride;
1220                if (bytes % (runtime->sample_bits >> 3) != 0) {
1221                        int oldbytes = bytes;
1222                        bytes = frames * stride;
1223                        snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1224                                                        oldbytes, bytes);
1225                }
1226                /* update the current pointer */
1227                spin_lock_irqsave(&subs->lock, flags);
1228                oldptr = subs->hwptr_done;
1229                subs->hwptr_done += bytes;
1230                if (subs->hwptr_done >= runtime->buffer_size * stride)
1231                        subs->hwptr_done -= runtime->buffer_size * stride;
1232                frames = (bytes + (oldptr % stride)) / stride;
1233                subs->transfer_done += frames;
1234                if (subs->transfer_done >= runtime->period_size) {
1235                        subs->transfer_done -= runtime->period_size;
1236                        period_elapsed = 1;
1237                }
1238                /* capture delay is by construction limited to one URB,
1239                 * reset delays here
1240                 */
1241                runtime->delay = subs->last_delay = 0;
1242
1243                /* realign last_frame_number */
1244                subs->last_frame_number = current_frame_number;
1245                subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1246
1247                spin_unlock_irqrestore(&subs->lock, flags);
1248                /* copy a data chunk */
1249                if (oldptr + bytes > runtime->buffer_size * stride) {
1250                        unsigned int bytes1 =
1251                                        runtime->buffer_size * stride - oldptr;
1252                        memcpy(runtime->dma_area + oldptr, cp, bytes1);
1253                        memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1254                } else {
1255                        memcpy(runtime->dma_area + oldptr, cp, bytes);
1256                }
1257        }
1258
1259        if (period_elapsed)
1260                snd_pcm_period_elapsed(subs->pcm_substream);
1261}
1262
1263static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1264                                             struct urb *urb, unsigned int bytes)
1265{
1266        struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1267        unsigned int stride = runtime->frame_bits >> 3;
1268        unsigned int dst_idx = 0;
1269        unsigned int src_idx = subs->hwptr_done;
1270        unsigned int wrap = runtime->buffer_size * stride;
1271        u8 *dst = urb->transfer_buffer;
1272        u8 *src = runtime->dma_area;
1273        u8 marker[] = { 0x05, 0xfa };
1274
1275        /*
1276         * The DSP DOP format defines a way to transport DSD samples over
1277         * normal PCM data endpoints. It requires stuffing of marker bytes
1278         * (0x05 and 0xfa, alternating per sample frame), and then expects
1279         * 2 additional bytes of actual payload. The whole frame is stored
1280         * LSB.
1281         *
1282         * Hence, for a stereo transport, the buffer layout looks like this,
1283         * where L refers to left channel samples and R to right.
1284         *
1285         *   L1 L2 0x05   R1 R2 0x05   L3 L4 0xfa  R3 R4 0xfa
1286         *   L5 L6 0x05   R5 R6 0x05   L7 L8 0xfa  R7 R8 0xfa
1287         *   .....
1288         *
1289         */
1290
1291        while (bytes--) {
1292                if (++subs->dsd_dop.byte_idx == 3) {
1293                        /* frame boundary? */
1294                        dst[dst_idx++] = marker[subs->dsd_dop.marker];
1295                        src_idx += 2;
1296                        subs->dsd_dop.byte_idx = 0;
1297
1298                        if (++subs->dsd_dop.channel % runtime->channels == 0) {
1299                                /* alternate the marker */
1300                                subs->dsd_dop.marker++;
1301                                subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1302                                subs->dsd_dop.channel = 0;
1303                        }
1304                } else {
1305                        /* stuff the DSD payload */
1306                        int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
1307
1308                        if (subs->cur_audiofmt->dsd_bitrev)
1309                                dst[dst_idx++] = bitrev8(src[idx]);
1310                        else
1311                                dst[dst_idx++] = src[idx];
1312
1313                        subs->hwptr_done++;
1314                }
1315        }
1316}
1317
1318static void prepare_playback_urb(struct snd_usb_substream *subs,
1319                                 struct urb *urb)
1320{
1321        struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1322        struct snd_usb_endpoint *ep = subs->data_endpoint;
1323        struct snd_urb_ctx *ctx = urb->context;
1324        unsigned int counts, frames, bytes;
1325        int i, stride, period_elapsed = 0;
1326        unsigned long flags;
1327
1328        stride = runtime->frame_bits >> 3;
1329
1330        frames = 0;
1331        urb->number_of_packets = 0;
1332        spin_lock_irqsave(&subs->lock, flags);
1333        for (i = 0; i < ctx->packets; i++) {
1334                if (ctx->packet_size[i])
1335                        counts = ctx->packet_size[i];
1336                else
1337                        counts = snd_usb_endpoint_next_packet_size(ep);
1338
1339                /* set up descriptor */
1340                urb->iso_frame_desc[i].offset = frames * ep->stride;
1341                urb->iso_frame_desc[i].length = counts * ep->stride;
1342                frames += counts;
1343                urb->number_of_packets++;
1344                subs->transfer_done += counts;
1345                if (subs->transfer_done >= runtime->period_size) {
1346                        subs->transfer_done -= runtime->period_size;
1347                        period_elapsed = 1;
1348                        if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1349                                if (subs->transfer_done > 0) {
1350                                        /* FIXME: fill-max mode is not
1351                                         * supported yet */
1352                                        frames -= subs->transfer_done;
1353                                        counts -= subs->transfer_done;
1354                                        urb->iso_frame_desc[i].length =
1355                                                counts * ep->stride;
1356                                        subs->transfer_done = 0;
1357                                }
1358                                i++;
1359                                if (i < ctx->packets) {
1360                                        /* add a transfer delimiter */
1361                                        urb->iso_frame_desc[i].offset =
1362                                                frames * ep->stride;
1363                                        urb->iso_frame_desc[i].length = 0;
1364                                        urb->number_of_packets++;
1365                                }
1366                                break;
1367                        }
1368                }
1369                if (period_elapsed &&
1370                    !snd_usb_endpoint_implicit_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1371                        break;
1372        }
1373        bytes = frames * ep->stride;
1374
1375        if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1376                     subs->cur_audiofmt->dsd_dop)) {
1377                fill_playback_urb_dsd_dop(subs, urb, bytes);
1378        } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1379                           subs->cur_audiofmt->dsd_bitrev)) {
1380                /* bit-reverse the bytes */
1381                u8 *buf = urb->transfer_buffer;
1382                for (i = 0; i < bytes; i++) {
1383                        int idx = (subs->hwptr_done + i)
1384                                % (runtime->buffer_size * stride);
1385                        buf[i] = bitrev8(runtime->dma_area[idx]);
1386                }
1387
1388                subs->hwptr_done += bytes;
1389        } else {
1390                /* usual PCM */
1391                if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1392                        /* err, the transferred area goes over buffer boundary. */
1393                        unsigned int bytes1 =
1394                                runtime->buffer_size * stride - subs->hwptr_done;
1395                        memcpy(urb->transfer_buffer,
1396                               runtime->dma_area + subs->hwptr_done, bytes1);
1397                        memcpy(urb->transfer_buffer + bytes1,
1398                               runtime->dma_area, bytes - bytes1);
1399                } else {
1400                        memcpy(urb->transfer_buffer,
1401                               runtime->dma_area + subs->hwptr_done, bytes);
1402                }
1403
1404                subs->hwptr_done += bytes;
1405        }
1406
1407        if (subs->hwptr_done >= runtime->buffer_size * stride)
1408                subs->hwptr_done -= runtime->buffer_size * stride;
1409
1410        /* update delay with exact number of samples queued */
1411        runtime->delay = subs->last_delay;
1412        runtime->delay += frames;
1413        subs->last_delay = runtime->delay;
1414
1415        /* realign last_frame_number */
1416        subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1417        subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1418
1419        spin_unlock_irqrestore(&subs->lock, flags);
1420        urb->transfer_buffer_length = bytes;
1421        if (period_elapsed)
1422                snd_pcm_period_elapsed(subs->pcm_substream);
1423}
1424
1425/*
1426 * process after playback data complete
1427 * - decrease the delay count again
1428 */
1429static void retire_playback_urb(struct snd_usb_substream *subs,
1430                               struct urb *urb)
1431{
1432        unsigned long flags;
1433        struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1434        struct snd_usb_endpoint *ep = subs->data_endpoint;
1435        int processed = urb->transfer_buffer_length / ep->stride;
1436        int est_delay;
1437
1438        /* ignore the delay accounting when procssed=0 is given, i.e.
1439         * silent payloads are procssed before handling the actual data
1440         */
1441        if (!processed)
1442                return;
1443
1444        spin_lock_irqsave(&subs->lock, flags);
1445        if (!subs->last_delay)
1446                goto out; /* short path */
1447
1448        est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1449        /* update delay with exact number of samples played */
1450        if (processed > subs->last_delay)
1451                subs->last_delay = 0;
1452        else
1453                subs->last_delay -= processed;
1454        runtime->delay = subs->last_delay;
1455
1456        /*
1457         * Report when delay estimate is off by more than 2ms.
1458         * The error should be lower than 2ms since the estimate relies
1459         * on two reads of a counter updated every ms.
1460         */
1461        if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1462                snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1463                        est_delay, subs->last_delay);
1464
1465        if (!subs->running) {
1466                /* update last_frame_number for delay counting here since
1467                 * prepare_playback_urb won't be called during pause
1468                 */
1469                subs->last_frame_number =
1470                        usb_get_current_frame_number(subs->dev) & 0xff;
1471        }
1472
1473 out:
1474        spin_unlock_irqrestore(&subs->lock, flags);
1475}
1476
1477static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1478{
1479        return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1480}
1481
1482static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1483{
1484        return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1485}
1486
1487static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1488{
1489        return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1490}
1491
1492static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1493{
1494        return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1495}
1496
1497static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1498                                              int cmd)
1499{
1500        struct snd_usb_substream *subs = substream->runtime->private_data;
1501
1502        switch (cmd) {
1503        case SNDRV_PCM_TRIGGER_START:
1504        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1505                subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1506                subs->data_endpoint->retire_data_urb = retire_playback_urb;
1507                subs->running = 1;
1508                return 0;
1509        case SNDRV_PCM_TRIGGER_STOP:
1510                stop_endpoints(subs, false);
1511                subs->running = 0;
1512                return 0;
1513        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1514                subs->data_endpoint->prepare_data_urb = NULL;
1515                /* keep retire_data_urb for delay calculation */
1516                subs->data_endpoint->retire_data_urb = retire_playback_urb;
1517                subs->running = 0;
1518                return 0;
1519        }
1520
1521        return -EINVAL;
1522}
1523
1524static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1525                                             int cmd)
1526{
1527        int err;
1528        struct snd_usb_substream *subs = substream->runtime->private_data;
1529
1530        switch (cmd) {
1531        case SNDRV_PCM_TRIGGER_START:
1532                err = start_endpoints(subs, false);
1533                if (err < 0)
1534                        return err;
1535
1536                subs->data_endpoint->retire_data_urb = retire_capture_urb;
1537                subs->running = 1;
1538                return 0;
1539        case SNDRV_PCM_TRIGGER_STOP:
1540                stop_endpoints(subs, false);
1541                subs->running = 0;
1542                return 0;
1543        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1544                subs->data_endpoint->retire_data_urb = NULL;
1545                subs->running = 0;
1546                return 0;
1547        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1548                subs->data_endpoint->retire_data_urb = retire_capture_urb;
1549                subs->running = 1;
1550                return 0;
1551        }
1552
1553        return -EINVAL;
1554}
1555
1556static struct snd_pcm_ops snd_usb_playback_ops = {
1557        .open =         snd_usb_playback_open,
1558        .close =        snd_usb_playback_close,
1559        .ioctl =        snd_pcm_lib_ioctl,
1560        .hw_params =    snd_usb_hw_params,
1561        .hw_free =      snd_usb_hw_free,
1562        .prepare =      snd_usb_pcm_prepare,
1563        .trigger =      snd_usb_substream_playback_trigger,
1564        .pointer =      snd_usb_pcm_pointer,
1565        .page =         snd_pcm_lib_get_vmalloc_page,
1566        .mmap =         snd_pcm_lib_mmap_vmalloc,
1567};
1568
1569static struct snd_pcm_ops snd_usb_capture_ops = {
1570        .open =         snd_usb_capture_open,
1571        .close =        snd_usb_capture_close,
1572        .ioctl =        snd_pcm_lib_ioctl,
1573        .hw_params =    snd_usb_hw_params,
1574        .hw_free =      snd_usb_hw_free,
1575        .prepare =      snd_usb_pcm_prepare,
1576        .trigger =      snd_usb_substream_capture_trigger,
1577        .pointer =      snd_usb_pcm_pointer,
1578        .page =         snd_pcm_lib_get_vmalloc_page,
1579        .mmap =         snd_pcm_lib_mmap_vmalloc,
1580};
1581
1582void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1583{
1584        snd_pcm_set_ops(pcm, stream,
1585                        stream == SNDRV_PCM_STREAM_PLAYBACK ?
1586                        &snd_usb_playback_ops : &snd_usb_capture_ops);
1587}
1588