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