Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 1008 lines 27 kB view raw
1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * US-X2Y AUDIO 4 * Copyright (c) 2002-2004 by Karsten Wiese 5 * 6 * based on 7 * 8 * (Tentative) USB Audio Driver for ALSA 9 * 10 * Main and PCM part 11 * 12 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de> 13 * 14 * Many codes borrowed from audio.c by 15 * Alan Cox (alan@lxorguk.ukuu.org.uk) 16 * Thomas Sailer (sailer@ife.ee.ethz.ch) 17 */ 18 19 20#include <linux/interrupt.h> 21#include <linux/slab.h> 22#include <linux/usb.h> 23#include <linux/moduleparam.h> 24#include <sound/core.h> 25#include <sound/info.h> 26#include <sound/pcm.h> 27#include <sound/pcm_params.h> 28#include "usx2y.h" 29#include "usbusx2y.h" 30 31static int usx2y_urb_capt_retire(struct snd_usx2y_substream *subs) 32{ 33 struct urb *urb = subs->completed_urb; 34 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 35 unsigned char *cp; 36 int i, len, lens = 0, hwptr_done = subs->hwptr_done; 37 int cnt, blen; 38 struct usx2ydev *usx2y = subs->usx2y; 39 40 for (i = 0; i < nr_of_packs(); i++) { 41 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset; 42 if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */ 43 dev_err(&usx2y->dev->dev, 44 "%s: active frame status %i. Most probably some hardware problem.\n", 45 __func__, 46 urb->iso_frame_desc[i].status); 47 return urb->iso_frame_desc[i].status; 48 } 49 len = urb->iso_frame_desc[i].actual_length / usx2y->stride; 50 if (!len) { 51 dev_dbg(&usx2y->dev->dev, "%s: 0 == len ERROR!\n", __func__); 52 continue; 53 } 54 55 /* copy a data chunk */ 56 if ((hwptr_done + len) > runtime->buffer_size) { 57 cnt = runtime->buffer_size - hwptr_done; 58 blen = cnt * usx2y->stride; 59 memcpy(runtime->dma_area + hwptr_done * usx2y->stride, cp, blen); 60 memcpy(runtime->dma_area, cp + blen, len * usx2y->stride - blen); 61 } else { 62 memcpy(runtime->dma_area + hwptr_done * usx2y->stride, cp, 63 len * usx2y->stride); 64 } 65 lens += len; 66 hwptr_done += len; 67 if (hwptr_done >= runtime->buffer_size) 68 hwptr_done -= runtime->buffer_size; 69 } 70 71 subs->hwptr_done = hwptr_done; 72 subs->transfer_done += lens; 73 /* update the pointer, call callback if necessary */ 74 if (subs->transfer_done >= runtime->period_size) { 75 subs->transfer_done -= runtime->period_size; 76 snd_pcm_period_elapsed(subs->pcm_substream); 77 } 78 return 0; 79} 80 81/* 82 * prepare urb for playback data pipe 83 * 84 * we copy the data directly from the pcm buffer. 85 * the current position to be copied is held in hwptr field. 86 * since a urb can handle only a single linear buffer, if the total 87 * transferred area overflows the buffer boundary, we cannot send 88 * it directly from the buffer. thus the data is once copied to 89 * a temporary buffer and urb points to that. 90 */ 91static int usx2y_urb_play_prepare(struct snd_usx2y_substream *subs, 92 struct urb *cap_urb, 93 struct urb *urb) 94{ 95 struct usx2ydev *usx2y = subs->usx2y; 96 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 97 int count, counts, pack, len; 98 99 count = 0; 100 for (pack = 0; pack < nr_of_packs(); pack++) { 101 /* calculate the size of a packet */ 102 counts = cap_urb->iso_frame_desc[pack].actual_length / usx2y->stride; 103 count += counts; 104 if (counts < 43 || counts > 50) { 105 dev_err(&usx2y->dev->dev, "%s: should not be here with counts=%i\n", 106 __func__, counts); 107 return -EPIPE; 108 } 109 /* set up descriptor */ 110 urb->iso_frame_desc[pack].offset = pack ? 111 urb->iso_frame_desc[pack - 1].offset + 112 urb->iso_frame_desc[pack - 1].length : 113 0; 114 urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length; 115 } 116 if (atomic_read(&subs->state) >= STATE_PRERUNNING) { 117 if (subs->hwptr + count > runtime->buffer_size) { 118 /* err, the transferred area goes over buffer boundary. 119 * copy the data to the temp buffer. 120 */ 121 len = runtime->buffer_size - subs->hwptr; 122 urb->transfer_buffer = subs->tmpbuf; 123 memcpy(subs->tmpbuf, runtime->dma_area + 124 subs->hwptr * usx2y->stride, len * usx2y->stride); 125 memcpy(subs->tmpbuf + len * usx2y->stride, 126 runtime->dma_area, (count - len) * usx2y->stride); 127 subs->hwptr += count; 128 subs->hwptr -= runtime->buffer_size; 129 } else { 130 /* set the buffer pointer */ 131 urb->transfer_buffer = runtime->dma_area + subs->hwptr * usx2y->stride; 132 subs->hwptr += count; 133 if (subs->hwptr >= runtime->buffer_size) 134 subs->hwptr -= runtime->buffer_size; 135 } 136 } else { 137 urb->transfer_buffer = subs->tmpbuf; 138 } 139 urb->transfer_buffer_length = count * usx2y->stride; 140 return 0; 141} 142 143/* 144 * process after playback data complete 145 * 146 * update the current position and call callback if a period is processed. 147 */ 148static void usx2y_urb_play_retire(struct snd_usx2y_substream *subs, struct urb *urb) 149{ 150 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 151 int len = urb->actual_length / subs->usx2y->stride; 152 153 subs->transfer_done += len; 154 subs->hwptr_done += len; 155 if (subs->hwptr_done >= runtime->buffer_size) 156 subs->hwptr_done -= runtime->buffer_size; 157 if (subs->transfer_done >= runtime->period_size) { 158 subs->transfer_done -= runtime->period_size; 159 snd_pcm_period_elapsed(subs->pcm_substream); 160 } 161} 162 163static int usx2y_urb_submit(struct snd_usx2y_substream *subs, struct urb *urb, int frame) 164{ 165 int err; 166 167 if (!urb) 168 return -ENODEV; 169 urb->start_frame = frame + NRURBS * nr_of_packs(); // let hcd do rollover sanity checks 170 urb->hcpriv = NULL; 171 urb->dev = subs->usx2y->dev; /* we need to set this at each time */ 172 err = usb_submit_urb(urb, GFP_ATOMIC); 173 if (err < 0) { 174 dev_err(&urb->dev->dev, "%s: usb_submit_urb() returned %i\n", 175 __func__, err); 176 return err; 177 } 178 return 0; 179} 180 181static int usx2y_usbframe_complete(struct snd_usx2y_substream *capsubs, 182 struct snd_usx2y_substream *playbacksubs, 183 int frame) 184{ 185 int err, state; 186 struct urb *urb = playbacksubs->completed_urb; 187 188 state = atomic_read(&playbacksubs->state); 189 if (urb) { 190 if (state == STATE_RUNNING) 191 usx2y_urb_play_retire(playbacksubs, urb); 192 else if (state >= STATE_PRERUNNING) 193 atomic_inc(&playbacksubs->state); 194 } else { 195 switch (state) { 196 case STATE_STARTING1: 197 urb = playbacksubs->urb[0]; 198 atomic_inc(&playbacksubs->state); 199 break; 200 case STATE_STARTING2: 201 urb = playbacksubs->urb[1]; 202 atomic_inc(&playbacksubs->state); 203 break; 204 } 205 } 206 if (urb) { 207 err = usx2y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb); 208 if (err) 209 return err; 210 err = usx2y_urb_submit(playbacksubs, urb, frame); 211 if (err) 212 return err; 213 } 214 215 playbacksubs->completed_urb = NULL; 216 217 state = atomic_read(&capsubs->state); 218 if (state >= STATE_PREPARED) { 219 if (state == STATE_RUNNING) { 220 err = usx2y_urb_capt_retire(capsubs); 221 if (err) 222 return err; 223 } else if (state >= STATE_PRERUNNING) { 224 atomic_inc(&capsubs->state); 225 } 226 err = usx2y_urb_submit(capsubs, capsubs->completed_urb, frame); 227 if (err) 228 return err; 229 } 230 capsubs->completed_urb = NULL; 231 return 0; 232} 233 234static void usx2y_clients_stop(struct usx2ydev *usx2y) 235{ 236 struct snd_usx2y_substream *subs; 237 struct urb *urb; 238 int s, u; 239 240 for (s = 0; s < 4; s++) { 241 subs = usx2y->subs[s]; 242 if (subs) { 243 dev_dbg(&usx2y->dev->dev, "%s: %i %p state=%i\n", 244 __func__, s, subs, atomic_read(&subs->state)); 245 atomic_set(&subs->state, STATE_STOPPED); 246 } 247 } 248 for (s = 0; s < 4; s++) { 249 subs = usx2y->subs[s]; 250 if (subs) { 251 if (atomic_read(&subs->state) >= STATE_PRERUNNING) 252 snd_pcm_stop_xrun(subs->pcm_substream); 253 for (u = 0; u < NRURBS; u++) { 254 urb = subs->urb[u]; 255 if (urb) 256 dev_dbg(&usx2y->dev->dev, 257 "%s: %i status=%i start_frame=%i\n", 258 __func__, u, urb->status, urb->start_frame); 259 } 260 } 261 } 262 usx2y->prepare_subs = NULL; 263 wake_up(&usx2y->prepare_wait_queue); 264} 265 266static void usx2y_error_urb_status(struct usx2ydev *usx2y, 267 struct snd_usx2y_substream *subs, struct urb *urb) 268{ 269 dev_err(&usx2y->dev->dev, "%s: ep=%i stalled with status=%i\n", 270 __func__, subs->endpoint, urb->status); 271 urb->status = 0; 272 usx2y_clients_stop(usx2y); 273} 274 275static void i_usx2y_urb_complete(struct urb *urb) 276{ 277 struct snd_usx2y_substream *subs = urb->context; 278 struct usx2ydev *usx2y = subs->usx2y; 279 struct snd_usx2y_substream *capsubs, *playbacksubs; 280 281 if (unlikely(atomic_read(&subs->state) < STATE_PREPARED)) { 282 dev_dbg(&usx2y->dev->dev, 283 "%s: hcd_frame=%i ep=%i%s status=%i start_frame=%i\n", 284 __func__, 285 usb_get_current_frame_number(usx2y->dev), 286 subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", 287 urb->status, urb->start_frame); 288 return; 289 } 290 if (unlikely(urb->status)) { 291 usx2y_error_urb_status(usx2y, subs, urb); 292 return; 293 } 294 295 subs->completed_urb = urb; 296 297 capsubs = usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]; 298 playbacksubs = usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]; 299 300 if (capsubs->completed_urb && 301 atomic_read(&capsubs->state) >= STATE_PREPARED && 302 (playbacksubs->completed_urb || 303 atomic_read(&playbacksubs->state) < STATE_PREPARED)) { 304 if (!usx2y_usbframe_complete(capsubs, playbacksubs, urb->start_frame)) { 305 usx2y->wait_iso_frame += nr_of_packs(); 306 } else { 307 usx2y_clients_stop(usx2y); 308 } 309 } 310} 311 312static void usx2y_urbs_set_complete(struct usx2ydev *usx2y, 313 void (*complete)(struct urb *)) 314{ 315 struct snd_usx2y_substream *subs; 316 struct urb *urb; 317 int s, u; 318 319 for (s = 0; s < 4; s++) { 320 subs = usx2y->subs[s]; 321 if (subs) { 322 for (u = 0; u < NRURBS; u++) { 323 urb = subs->urb[u]; 324 if (urb) 325 urb->complete = complete; 326 } 327 } 328 } 329} 330 331static void usx2y_subs_startup_finish(struct usx2ydev *usx2y) 332{ 333 usx2y_urbs_set_complete(usx2y, i_usx2y_urb_complete); 334 usx2y->prepare_subs = NULL; 335} 336 337static void i_usx2y_subs_startup(struct urb *urb) 338{ 339 struct snd_usx2y_substream *subs = urb->context; 340 struct usx2ydev *usx2y = subs->usx2y; 341 struct snd_usx2y_substream *prepare_subs = usx2y->prepare_subs; 342 343 if (prepare_subs) { 344 if (urb->start_frame == prepare_subs->urb[0]->start_frame) { 345 usx2y_subs_startup_finish(usx2y); 346 atomic_inc(&prepare_subs->state); 347 wake_up(&usx2y->prepare_wait_queue); 348 } 349 } 350 351 i_usx2y_urb_complete(urb); 352} 353 354static void usx2y_subs_prepare(struct snd_usx2y_substream *subs) 355{ 356 dev_dbg(&subs->usx2y->dev->dev, 357 "%s(%p) ep=%i urb0=%p urb1=%p\n", 358 __func__, subs, subs->endpoint, subs->urb[0], subs->urb[1]); 359 /* reset the pointer */ 360 subs->hwptr = 0; 361 subs->hwptr_done = 0; 362 subs->transfer_done = 0; 363} 364 365static void usx2y_urb_release(struct urb **urb, int free_tb) 366{ 367 if (*urb) { 368 usb_kill_urb(*urb); 369 if (free_tb) 370 kfree((*urb)->transfer_buffer); 371 usb_free_urb(*urb); 372 *urb = NULL; 373 } 374} 375 376/* 377 * release a substreams urbs 378 */ 379static void usx2y_urbs_release(struct snd_usx2y_substream *subs) 380{ 381 int i; 382 383 dev_dbg(&subs->usx2y->dev->dev, "%s %i\n", __func__, subs->endpoint); 384 for (i = 0; i < NRURBS; i++) 385 usx2y_urb_release(subs->urb + i, 386 subs != subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]); 387 388 kfree(subs->tmpbuf); 389 subs->tmpbuf = NULL; 390} 391 392/* 393 * initialize a substream's urbs 394 */ 395static int usx2y_urbs_allocate(struct snd_usx2y_substream *subs) 396{ 397 int i; 398 unsigned int pipe; 399 int is_playback = subs == subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]; 400 struct usb_device *dev = subs->usx2y->dev; 401 struct urb **purb; 402 403 pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) : 404 usb_rcvisocpipe(dev, subs->endpoint); 405 subs->maxpacksize = usb_maxpacket(dev, pipe); 406 if (!subs->maxpacksize) 407 return -EINVAL; 408 409 if (is_playback && !subs->tmpbuf) { /* allocate a temporary buffer for playback */ 410 subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL); 411 if (!subs->tmpbuf) 412 return -ENOMEM; 413 } 414 /* allocate and initialize data urbs */ 415 for (i = 0; i < NRURBS; i++) { 416 purb = subs->urb + i; 417 if (*purb) { 418 usb_kill_urb(*purb); 419 continue; 420 } 421 *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL); 422 if (!*purb) { 423 usx2y_urbs_release(subs); 424 return -ENOMEM; 425 } 426 if (!is_playback && !(*purb)->transfer_buffer) { 427 /* allocate a capture buffer per urb */ 428 (*purb)->transfer_buffer = 429 kmalloc_array(subs->maxpacksize, 430 nr_of_packs(), GFP_KERNEL); 431 if (!(*purb)->transfer_buffer) { 432 usx2y_urbs_release(subs); 433 return -ENOMEM; 434 } 435 } 436 (*purb)->dev = dev; 437 (*purb)->pipe = pipe; 438 (*purb)->number_of_packets = nr_of_packs(); 439 (*purb)->context = subs; 440 (*purb)->interval = 1; 441 (*purb)->complete = i_usx2y_subs_startup; 442 } 443 return 0; 444} 445 446static void usx2y_subs_startup(struct snd_usx2y_substream *subs) 447{ 448 struct usx2ydev *usx2y = subs->usx2y; 449 450 usx2y->prepare_subs = subs; 451 subs->urb[0]->start_frame = -1; 452 wmb(); 453 usx2y_urbs_set_complete(usx2y, i_usx2y_subs_startup); 454} 455 456static int usx2y_urbs_start(struct snd_usx2y_substream *subs) 457{ 458 int i, err; 459 struct usx2ydev *usx2y = subs->usx2y; 460 struct urb *urb; 461 unsigned long pack; 462 463 err = usx2y_urbs_allocate(subs); 464 if (err < 0) 465 return err; 466 subs->completed_urb = NULL; 467 for (i = 0; i < 4; i++) { 468 struct snd_usx2y_substream *subs = usx2y->subs[i]; 469 470 if (subs && atomic_read(&subs->state) >= STATE_PREPARED) 471 goto start; 472 } 473 474 start: 475 usx2y_subs_startup(subs); 476 for (i = 0; i < NRURBS; i++) { 477 urb = subs->urb[i]; 478 if (usb_pipein(urb->pipe)) { 479 if (!i) 480 atomic_set(&subs->state, STATE_STARTING3); 481 urb->dev = usx2y->dev; 482 for (pack = 0; pack < nr_of_packs(); pack++) { 483 urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack; 484 urb->iso_frame_desc[pack].length = subs->maxpacksize; 485 } 486 urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs(); 487 err = usb_submit_urb(urb, GFP_ATOMIC); 488 if (err < 0) { 489 dev_err(&urb->dev->dev, "%s: cannot submit datapipe for urb %d, err = %d\n", 490 __func__, i, err); 491 err = -EPIPE; 492 goto cleanup; 493 } else { 494 if (!i) 495 usx2y->wait_iso_frame = urb->start_frame; 496 } 497 urb->transfer_flags = 0; 498 } else { 499 atomic_set(&subs->state, STATE_STARTING1); 500 break; 501 } 502 } 503 err = 0; 504 wait_event(usx2y->prepare_wait_queue, !usx2y->prepare_subs); 505 if (atomic_read(&subs->state) != STATE_PREPARED) 506 err = -EPIPE; 507 508 cleanup: 509 if (err) { 510 usx2y_subs_startup_finish(usx2y); 511 usx2y_clients_stop(usx2y); // something is completely wrong > stop everything 512 } 513 return err; 514} 515 516/* 517 * return the current pcm pointer. just return the hwptr_done value. 518 */ 519static snd_pcm_uframes_t snd_usx2y_pcm_pointer(struct snd_pcm_substream *substream) 520{ 521 struct snd_usx2y_substream *subs = substream->runtime->private_data; 522 523 return subs->hwptr_done; 524} 525 526/* 527 * start/stop substream 528 */ 529static int snd_usx2y_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 530{ 531 struct snd_usx2y_substream *subs = substream->runtime->private_data; 532 533 switch (cmd) { 534 case SNDRV_PCM_TRIGGER_START: 535 dev_dbg(&subs->usx2y->dev->dev, "%s(START)\n", __func__); 536 if (atomic_read(&subs->state) == STATE_PREPARED && 537 atomic_read(&subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= STATE_PREPARED) { 538 atomic_set(&subs->state, STATE_PRERUNNING); 539 } else { 540 return -EPIPE; 541 } 542 break; 543 case SNDRV_PCM_TRIGGER_STOP: 544 dev_dbg(&subs->usx2y->dev->dev, "%s(STOP)\n", __func__); 545 if (atomic_read(&subs->state) >= STATE_PRERUNNING) 546 atomic_set(&subs->state, STATE_PREPARED); 547 break; 548 default: 549 return -EINVAL; 550 } 551 return 0; 552} 553 554/* 555 * allocate a buffer, setup samplerate 556 * 557 * so far we use a physically linear buffer although packetize transfer 558 * doesn't need a continuous area. 559 * if sg buffer is supported on the later version of alsa, we'll follow 560 * that. 561 */ 562struct s_c2 { 563 char c1, c2; 564}; 565 566static const struct s_c2 setrate_44100[] = { 567 { 0x14, 0x08}, // this line sets 44100, well actually a little less 568 { 0x18, 0x40}, // only tascam / frontier design knows the further lines ....... 569 { 0x18, 0x42}, 570 { 0x18, 0x45}, 571 { 0x18, 0x46}, 572 { 0x18, 0x48}, 573 { 0x18, 0x4A}, 574 { 0x18, 0x4C}, 575 { 0x18, 0x4E}, 576 { 0x18, 0x50}, 577 { 0x18, 0x52}, 578 { 0x18, 0x54}, 579 { 0x18, 0x56}, 580 { 0x18, 0x58}, 581 { 0x18, 0x5A}, 582 { 0x18, 0x5C}, 583 { 0x18, 0x5E}, 584 { 0x18, 0x60}, 585 { 0x18, 0x62}, 586 { 0x18, 0x64}, 587 { 0x18, 0x66}, 588 { 0x18, 0x68}, 589 { 0x18, 0x6A}, 590 { 0x18, 0x6C}, 591 { 0x18, 0x6E}, 592 { 0x18, 0x70}, 593 { 0x18, 0x72}, 594 { 0x18, 0x74}, 595 { 0x18, 0x76}, 596 { 0x18, 0x78}, 597 { 0x18, 0x7A}, 598 { 0x18, 0x7C}, 599 { 0x18, 0x7E} 600}; 601 602static const struct s_c2 setrate_48000[] = { 603 { 0x14, 0x09}, // this line sets 48000, well actually a little less 604 { 0x18, 0x40}, // only tascam / frontier design knows the further lines ....... 605 { 0x18, 0x42}, 606 { 0x18, 0x45}, 607 { 0x18, 0x46}, 608 { 0x18, 0x48}, 609 { 0x18, 0x4A}, 610 { 0x18, 0x4C}, 611 { 0x18, 0x4E}, 612 { 0x18, 0x50}, 613 { 0x18, 0x52}, 614 { 0x18, 0x54}, 615 { 0x18, 0x56}, 616 { 0x18, 0x58}, 617 { 0x18, 0x5A}, 618 { 0x18, 0x5C}, 619 { 0x18, 0x5E}, 620 { 0x18, 0x60}, 621 { 0x18, 0x62}, 622 { 0x18, 0x64}, 623 { 0x18, 0x66}, 624 { 0x18, 0x68}, 625 { 0x18, 0x6A}, 626 { 0x18, 0x6C}, 627 { 0x18, 0x6E}, 628 { 0x18, 0x70}, 629 { 0x18, 0x73}, 630 { 0x18, 0x74}, 631 { 0x18, 0x76}, 632 { 0x18, 0x78}, 633 { 0x18, 0x7A}, 634 { 0x18, 0x7C}, 635 { 0x18, 0x7E} 636}; 637 638#define NOOF_SETRATE_URBS ARRAY_SIZE(setrate_48000) 639 640static void i_usx2y_04int(struct urb *urb) 641{ 642 struct usx2ydev *usx2y = urb->context; 643 644 if (urb->status) 645 dev_err(&urb->dev->dev, "%s() urb->status=%i\n", 646 __func__, urb->status); 647 if (!--usx2y->us04->len) 648 wake_up(&usx2y->in04_wait_queue); 649} 650 651static int usx2y_rate_set(struct usx2ydev *usx2y, int rate) 652{ 653 int err = 0, i; 654 struct snd_usx2y_urb_seq *us = NULL; 655 int *usbdata = NULL; 656 const struct s_c2 *ra = rate == 48000 ? setrate_48000 : setrate_44100; 657 struct urb *urb; 658 659 if (usx2y->rate != rate) { 660 us = kzalloc_flex(*us, urb, NOOF_SETRATE_URBS); 661 if (!us) { 662 err = -ENOMEM; 663 goto cleanup; 664 } 665 us->len = NOOF_SETRATE_URBS; 666 usbdata = kmalloc_objs(int, NOOF_SETRATE_URBS); 667 if (!usbdata) { 668 err = -ENOMEM; 669 goto cleanup; 670 } 671 for (i = 0; i < NOOF_SETRATE_URBS; ++i) { 672 us->urb[i] = usb_alloc_urb(0, GFP_KERNEL); 673 if (!us->urb[i]) { 674 err = -ENOMEM; 675 goto cleanup; 676 } 677 ((char *)(usbdata + i))[0] = ra[i].c1; 678 ((char *)(usbdata + i))[1] = ra[i].c2; 679 usb_fill_bulk_urb(us->urb[i], usx2y->dev, usb_sndbulkpipe(usx2y->dev, 4), 680 usbdata + i, 2, i_usx2y_04int, usx2y); 681 } 682 err = usb_urb_ep_type_check(us->urb[0]); 683 if (err < 0) 684 goto cleanup; 685 us->submitted = 0; 686 usx2y->us04 = us; 687 wait_event_timeout(usx2y->in04_wait_queue, !us->len, HZ); 688 usx2y->us04 = NULL; 689 if (us->len) 690 err = -ENODEV; 691 cleanup: 692 if (us) { 693 us->submitted = 2*NOOF_SETRATE_URBS; 694 for (i = 0; i < NOOF_SETRATE_URBS; ++i) { 695 urb = us->urb[i]; 696 if (!urb) 697 continue; 698 if (urb->status) { 699 if (!err) 700 err = -ENODEV; 701 usb_kill_urb(urb); 702 } 703 usb_free_urb(urb); 704 } 705 usx2y->us04 = NULL; 706 kfree(usbdata); 707 kfree(us); 708 if (!err) 709 usx2y->rate = rate; 710 } 711 } 712 713 return err; 714} 715 716static int usx2y_format_set(struct usx2ydev *usx2y, snd_pcm_format_t format) 717{ 718 int alternate, err; 719 struct list_head *p; 720 721 if (format == SNDRV_PCM_FORMAT_S24_3LE) { 722 alternate = 2; 723 usx2y->stride = 6; 724 } else { 725 alternate = 1; 726 usx2y->stride = 4; 727 } 728 list_for_each(p, &usx2y->midi_list) { 729 snd_usbmidi_input_stop(p); 730 } 731 usb_kill_urb(usx2y->in04_urb); 732 err = usb_set_interface(usx2y->dev, 0, alternate); 733 if (err) { 734 dev_err(&usx2y->dev->dev, "%s: usb_set_interface error\n", 735 __func__); 736 return err; 737 } 738 usx2y->in04_urb->dev = usx2y->dev; 739 err = usb_submit_urb(usx2y->in04_urb, GFP_KERNEL); 740 list_for_each(p, &usx2y->midi_list) { 741 snd_usbmidi_input_start(p); 742 } 743 usx2y->format = format; 744 usx2y->rate = 0; 745 return err; 746} 747 748 749static int snd_usx2y_pcm_hw_params(struct snd_pcm_substream *substream, 750 struct snd_pcm_hw_params *hw_params) 751{ 752 unsigned int rate = params_rate(hw_params); 753 snd_pcm_format_t format = params_format(hw_params); 754 struct snd_card *card = substream->pstr->pcm->card; 755 struct usx2ydev *dev = usx2y(card); 756 struct snd_usx2y_substream *subs; 757 struct snd_pcm_substream *test_substream; 758 int i; 759 760 guard(mutex)(&usx2y(card)->pcm_mutex); 761 dev_dbg(&dev->dev->dev, "%s(%p, %p)\n", __func__, substream, hw_params); 762 /* all pcm substreams off one usx2y have to operate at the same 763 * rate & format 764 */ 765 for (i = 0; i < dev->pcm_devs * 2; i++) { 766 subs = dev->subs[i]; 767 if (!subs) 768 continue; 769 test_substream = subs->pcm_substream; 770 if (!test_substream || test_substream == substream || 771 !test_substream->runtime) 772 continue; 773 if ((test_substream->runtime->format && 774 test_substream->runtime->format != format) || 775 (test_substream->runtime->rate && 776 test_substream->runtime->rate != rate)) { 777 return -EINVAL; 778 } 779 } 780 781 return 0; 782} 783 784/* 785 * free the buffer 786 */ 787static int snd_usx2y_pcm_hw_free(struct snd_pcm_substream *substream) 788{ 789 struct snd_pcm_runtime *runtime = substream->runtime; 790 struct snd_usx2y_substream *subs = runtime->private_data; 791 struct snd_usx2y_substream *cap_subs, *playback_subs; 792 793 guard(mutex)(&subs->usx2y->pcm_mutex); 794 dev_dbg(&subs->usx2y->dev->dev, "%s(%p)\n", __func__, substream); 795 796 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 797 cap_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]; 798 atomic_set(&subs->state, STATE_STOPPED); 799 usx2y_urbs_release(subs); 800 if (!cap_subs->pcm_substream || 801 !cap_subs->pcm_substream->runtime || 802 cap_subs->pcm_substream->runtime->state < SNDRV_PCM_STATE_PREPARED) { 803 atomic_set(&cap_subs->state, STATE_STOPPED); 804 usx2y_urbs_release(cap_subs); 805 } 806 } else { 807 playback_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]; 808 if (atomic_read(&playback_subs->state) < STATE_PREPARED) { 809 atomic_set(&subs->state, STATE_STOPPED); 810 usx2y_urbs_release(subs); 811 } 812 } 813 return 0; 814} 815 816/* 817 * prepare callback 818 * 819 * set format and initialize urbs 820 */ 821static int snd_usx2y_pcm_prepare(struct snd_pcm_substream *substream) 822{ 823 struct snd_pcm_runtime *runtime = substream->runtime; 824 struct snd_usx2y_substream *subs = runtime->private_data; 825 struct usx2ydev *usx2y = subs->usx2y; 826 struct snd_usx2y_substream *capsubs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]; 827 int err = 0; 828 829 dev_dbg(&usx2y->dev->dev, "%s(%p)\n", __func__, substream); 830 831 guard(mutex)(&usx2y->pcm_mutex); 832 usx2y_subs_prepare(subs); 833 // Start hardware streams 834 // SyncStream first.... 835 if (atomic_read(&capsubs->state) < STATE_PREPARED) { 836 if (usx2y->format != runtime->format) { 837 err = usx2y_format_set(usx2y, runtime->format); 838 if (err < 0) 839 return err; 840 } 841 if (usx2y->rate != runtime->rate) { 842 err = usx2y_rate_set(usx2y, runtime->rate); 843 if (err < 0) 844 return err; 845 } 846 dev_dbg(&usx2y->dev->dev, "%s: starting capture pipe for %s\n", 847 __func__, subs == capsubs ? "self" : "playpipe"); 848 err = usx2y_urbs_start(capsubs); 849 if (err < 0) 850 return err; 851 } 852 853 if (subs != capsubs && atomic_read(&subs->state) < STATE_PREPARED) 854 err = usx2y_urbs_start(subs); 855 856 return err; 857} 858 859static const struct snd_pcm_hardware snd_usx2y_2c = { 860 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 861 SNDRV_PCM_INFO_BLOCK_TRANSFER | 862 SNDRV_PCM_INFO_MMAP_VALID | 863 SNDRV_PCM_INFO_BATCH), 864 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE, 865 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, 866 .rate_min = 44100, 867 .rate_max = 48000, 868 .channels_min = 2, 869 .channels_max = 2, 870 .buffer_bytes_max = (2*128*1024), 871 .period_bytes_min = 64, 872 .period_bytes_max = (128*1024), 873 .periods_min = 2, 874 .periods_max = 1024, 875 .fifo_size = 0 876}; 877 878static int snd_usx2y_pcm_open(struct snd_pcm_substream *substream) 879{ 880 struct snd_usx2y_substream *subs = 881 ((struct snd_usx2y_substream **) 882 snd_pcm_substream_chip(substream))[substream->stream]; 883 struct snd_pcm_runtime *runtime = substream->runtime; 884 885 if (subs->usx2y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS) 886 return -EBUSY; 887 888 runtime->hw = snd_usx2y_2c; 889 runtime->private_data = subs; 890 subs->pcm_substream = substream; 891 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000); 892 return 0; 893} 894 895static int snd_usx2y_pcm_close(struct snd_pcm_substream *substream) 896{ 897 struct snd_pcm_runtime *runtime = substream->runtime; 898 struct snd_usx2y_substream *subs = runtime->private_data; 899 900 subs->pcm_substream = NULL; 901 902 return 0; 903} 904 905static const struct snd_pcm_ops snd_usx2y_pcm_ops = { 906 .open = snd_usx2y_pcm_open, 907 .close = snd_usx2y_pcm_close, 908 .hw_params = snd_usx2y_pcm_hw_params, 909 .hw_free = snd_usx2y_pcm_hw_free, 910 .prepare = snd_usx2y_pcm_prepare, 911 .trigger = snd_usx2y_pcm_trigger, 912 .pointer = snd_usx2y_pcm_pointer, 913}; 914 915/* 916 * free a usb stream instance 917 */ 918static void usx2y_audio_stream_free(struct snd_usx2y_substream **usx2y_substream) 919{ 920 int stream; 921 922 for_each_pcm_streams(stream) { 923 kfree(usx2y_substream[stream]); 924 usx2y_substream[stream] = NULL; 925 } 926} 927 928static void snd_usx2y_pcm_private_free(struct snd_pcm *pcm) 929{ 930 struct snd_usx2y_substream **usx2y_stream = pcm->private_data; 931 932 if (usx2y_stream) 933 usx2y_audio_stream_free(usx2y_stream); 934} 935 936static int usx2y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint) 937{ 938 struct snd_pcm *pcm; 939 int err, i; 940 struct snd_usx2y_substream **usx2y_substream = 941 usx2y(card)->subs + 2 * usx2y(card)->pcm_devs; 942 943 for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; 944 i <= SNDRV_PCM_STREAM_CAPTURE; ++i) { 945 usx2y_substream[i] = kzalloc_obj(struct snd_usx2y_substream); 946 if (!usx2y_substream[i]) 947 return -ENOMEM; 948 949 usx2y_substream[i]->usx2y = usx2y(card); 950 } 951 952 if (playback_endpoint) 953 usx2y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint; 954 usx2y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint; 955 956 err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usx2y(card)->pcm_devs, 957 playback_endpoint ? 1 : 0, 1, 958 &pcm); 959 if (err < 0) { 960 usx2y_audio_stream_free(usx2y_substream); 961 return err; 962 } 963 964 if (playback_endpoint) 965 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usx2y_pcm_ops); 966 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usx2y_pcm_ops); 967 968 pcm->private_data = usx2y_substream; 969 pcm->private_free = snd_usx2y_pcm_private_free; 970 pcm->info_flags = 0; 971 972 sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usx2y(card)->pcm_devs); 973 974 if (playback_endpoint) { 975 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream, 976 SNDRV_DMA_TYPE_CONTINUOUS, 977 NULL, 978 64*1024, 128*1024); 979 } 980 981 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream, 982 SNDRV_DMA_TYPE_CONTINUOUS, 983 NULL, 984 64*1024, 128*1024); 985 usx2y(card)->pcm_devs++; 986 987 return 0; 988} 989 990/* 991 * create a chip instance and set its names. 992 */ 993int usx2y_audio_create(struct snd_card *card) 994{ 995 int err; 996 997 err = usx2y_audio_stream_new(card, 0xA, 0x8); 998 if (err < 0) 999 return err; 1000 if (le16_to_cpu(usx2y(card)->dev->descriptor.idProduct) == USB_ID_US428) { 1001 err = usx2y_audio_stream_new(card, 0, 0xA); 1002 if (err < 0) 1003 return err; 1004 } 1005 if (le16_to_cpu(usx2y(card)->dev->descriptor.idProduct) != USB_ID_US122) 1006 err = usx2y_rate_set(usx2y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122. 1007 return err; 1008}