root/sound/usb/pcm.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. snd_usb_pcm_delay
  2. snd_usb_pcm_pointer
  3. find_format
  4. init_pitch_v1
  5. init_pitch_v2
  6. snd_usb_init_pitch
  7. start_endpoints
  8. stop_endpoints
  9. search_roland_implicit_fb
  10. set_sync_ep_implicit_fb_quirk
  11. set_sync_endpoint
  12. set_format
  13. match_endpoint_audioformats
  14. configure_sync_endpoint
  15. configure_endpoint
  16. snd_usb_pcm_change_state
  17. snd_usb_pcm_suspend
  18. snd_usb_pcm_resume
  19. snd_usb_hw_params
  20. snd_usb_hw_free
  21. snd_usb_pcm_prepare
  22. hw_check_valid_format
  23. hw_rule_rate
  24. hw_rule_channels
  25. hw_rule_format
  26. hw_rule_period_time
  27. snd_usb_pcm_check_knot
  28. setup_hw_info
  29. snd_usb_pcm_open
  30. snd_usb_pcm_close
  31. retire_capture_urb
  32. fill_playback_urb_dsd_dop
  33. copy_to_urb
  34. copy_to_urb_quirk
  35. prepare_playback_urb
  36. retire_playback_urb
  37. snd_usb_substream_playback_trigger
  38. snd_usb_substream_capture_trigger
  39. snd_usb_set_pcm_ops
  40. snd_usb_preallocate_buffer

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

/* [<][>][^][v][top][bottom][index][help] */