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