1 /* 2 * dice_stream.c - a part of driver for DICE based devices 3 * 4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 5 * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp> 6 * 7 * Licensed under the terms of the GNU General Public License, version 2. 8 */ 9 10 #include "dice.h" 11 12 #define CALLBACK_TIMEOUT 200 13 #define NOTIFICATION_TIMEOUT_MS (2 * MSEC_PER_SEC) 14 15 struct reg_params { 16 unsigned int count; 17 unsigned int size; 18 }; 19 20 const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT] = { 21 /* mode 0 */ 22 [0] = 32000, 23 [1] = 44100, 24 [2] = 48000, 25 /* mode 1 */ 26 [3] = 88200, 27 [4] = 96000, 28 /* mode 2 */ 29 [5] = 176400, 30 [6] = 192000, 31 }; 32 33 int snd_dice_stream_get_rate_mode(struct snd_dice *dice, unsigned int rate, 34 enum snd_dice_rate_mode *mode) 35 { 36 /* Corresponding to each entry in snd_dice_rates. */ 37 static const enum snd_dice_rate_mode modes[] = { 38 [0] = SND_DICE_RATE_MODE_LOW, 39 [1] = SND_DICE_RATE_MODE_LOW, 40 [2] = SND_DICE_RATE_MODE_LOW, 41 [3] = SND_DICE_RATE_MODE_MIDDLE, 42 [4] = SND_DICE_RATE_MODE_MIDDLE, 43 [5] = SND_DICE_RATE_MODE_HIGH, 44 [6] = SND_DICE_RATE_MODE_HIGH, 45 }; 46 int i; 47 48 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); i++) { 49 if (!(dice->clock_caps & BIT(i))) 50 continue; 51 if (snd_dice_rates[i] != rate) 52 continue; 53 54 *mode = modes[i]; 55 return 0; 56 } 57 58 return -EINVAL; 59 } 60 61 /* 62 * This operation has an effect to synchronize GLOBAL_STATUS/GLOBAL_SAMPLE_RATE 63 * to GLOBAL_STATUS. Especially, just after powering on, these are different. 64 */ 65 static int ensure_phase_lock(struct snd_dice *dice, unsigned int rate) 66 { 67 __be32 reg, nominal; 68 u32 data; 69 int i; 70 int err; 71 72 err = snd_dice_transaction_read_global(dice, GLOBAL_CLOCK_SELECT, 73 ®, sizeof(reg)); 74 if (err < 0) 75 return err; 76 77 data = be32_to_cpu(reg); 78 79 data &= ~CLOCK_RATE_MASK; 80 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) { 81 if (snd_dice_rates[i] == rate) 82 break; 83 } 84 if (i == ARRAY_SIZE(snd_dice_rates)) 85 return -EINVAL; 86 data |= i << CLOCK_RATE_SHIFT; 87 88 if (completion_done(&dice->clock_accepted)) 89 reinit_completion(&dice->clock_accepted); 90 91 reg = cpu_to_be32(data); 92 err = snd_dice_transaction_write_global(dice, GLOBAL_CLOCK_SELECT, 93 ®, sizeof(reg)); 94 if (err < 0) 95 return err; 96 97 if (wait_for_completion_timeout(&dice->clock_accepted, 98 msecs_to_jiffies(NOTIFICATION_TIMEOUT_MS)) == 0) { 99 /* 100 * Old versions of Dice firmware transfer no notification when 101 * the same clock status as current one is set. In this case, 102 * just check current clock status. 103 */ 104 err = snd_dice_transaction_read_global(dice, GLOBAL_STATUS, 105 &nominal, sizeof(nominal)); 106 if (err < 0) 107 return err; 108 if (!(be32_to_cpu(nominal) & STATUS_SOURCE_LOCKED)) 109 return -ETIMEDOUT; 110 } 111 112 return 0; 113 } 114 115 static int get_register_params(struct snd_dice *dice, 116 struct reg_params *tx_params, 117 struct reg_params *rx_params) 118 { 119 __be32 reg[2]; 120 int err; 121 122 err = snd_dice_transaction_read_tx(dice, TX_NUMBER, reg, sizeof(reg)); 123 if (err < 0) 124 return err; 125 tx_params->count = 126 min_t(unsigned int, be32_to_cpu(reg[0]), MAX_STREAMS); 127 tx_params->size = be32_to_cpu(reg[1]) * 4; 128 129 err = snd_dice_transaction_read_rx(dice, RX_NUMBER, reg, sizeof(reg)); 130 if (err < 0) 131 return err; 132 rx_params->count = 133 min_t(unsigned int, be32_to_cpu(reg[0]), MAX_STREAMS); 134 rx_params->size = be32_to_cpu(reg[1]) * 4; 135 136 return 0; 137 } 138 139 static void release_resources(struct snd_dice *dice) 140 { 141 int i; 142 143 for (i = 0; i < MAX_STREAMS; ++i) { 144 fw_iso_resources_free(&dice->tx_resources[i]); 145 fw_iso_resources_free(&dice->rx_resources[i]); 146 } 147 } 148 149 static void stop_streams(struct snd_dice *dice, enum amdtp_stream_direction dir, 150 struct reg_params *params) 151 { 152 __be32 reg; 153 unsigned int i; 154 155 for (i = 0; i < params->count; i++) { 156 reg = cpu_to_be32((u32)-1); 157 if (dir == AMDTP_IN_STREAM) { 158 amdtp_stream_stop(&dice->tx_stream[i]); 159 160 snd_dice_transaction_write_tx(dice, 161 params->size * i + TX_ISOCHRONOUS, 162 ®, sizeof(reg)); 163 } else { 164 amdtp_stream_stop(&dice->rx_stream[i]); 165 166 snd_dice_transaction_write_rx(dice, 167 params->size * i + RX_ISOCHRONOUS, 168 ®, sizeof(reg)); 169 } 170 } 171 } 172 173 static int keep_resources(struct snd_dice *dice, struct amdtp_stream *stream, 174 struct fw_iso_resources *resources, unsigned int rate, 175 unsigned int pcm_chs, unsigned int midi_ports) 176 { 177 bool double_pcm_frames; 178 unsigned int i; 179 int err; 180 181 // At 176.4/192.0 kHz, Dice has a quirk to transfer two PCM frames in 182 // one data block of AMDTP packet. Thus sampling transfer frequency is 183 // a half of PCM sampling frequency, i.e. PCM frames at 192.0 kHz are 184 // transferred on AMDTP packets at 96 kHz. Two successive samples of a 185 // channel are stored consecutively in the packet. This quirk is called 186 // as 'Dual Wire'. 187 // For this quirk, blocking mode is required and PCM buffer size should 188 // be aligned to SYT_INTERVAL. 189 double_pcm_frames = rate > 96000; 190 if (double_pcm_frames) { 191 rate /= 2; 192 pcm_chs *= 2; 193 } 194 195 err = amdtp_am824_set_parameters(stream, rate, pcm_chs, midi_ports, 196 double_pcm_frames); 197 if (err < 0) 198 return err; 199 200 if (double_pcm_frames) { 201 pcm_chs /= 2; 202 203 for (i = 0; i < pcm_chs; i++) { 204 amdtp_am824_set_pcm_position(stream, i, i * 2); 205 amdtp_am824_set_pcm_position(stream, i + pcm_chs, 206 i * 2 + 1); 207 } 208 } 209 210 return fw_iso_resources_allocate(resources, 211 amdtp_stream_get_max_payload(stream), 212 fw_parent_device(dice->unit)->max_speed); 213 } 214 215 static int keep_dual_resources(struct snd_dice *dice, unsigned int rate, 216 enum amdtp_stream_direction dir, 217 struct reg_params *params) 218 { 219 enum snd_dice_rate_mode mode; 220 int i; 221 int err; 222 223 err = snd_dice_stream_get_rate_mode(dice, rate, &mode); 224 if (err < 0) 225 return err; 226 227 for (i = 0; i < params->count; ++i) { 228 __be32 reg[2]; 229 struct amdtp_stream *stream; 230 struct fw_iso_resources *resources; 231 unsigned int pcm_cache; 232 unsigned int midi_cache; 233 unsigned int pcm_chs; 234 unsigned int midi_ports; 235 236 if (dir == AMDTP_IN_STREAM) { 237 stream = &dice->tx_stream[i]; 238 resources = &dice->tx_resources[i]; 239 240 pcm_cache = dice->tx_pcm_chs[i][mode]; 241 midi_cache = dice->tx_midi_ports[i]; 242 err = snd_dice_transaction_read_tx(dice, 243 params->size * i + TX_NUMBER_AUDIO, 244 reg, sizeof(reg)); 245 } else { 246 stream = &dice->rx_stream[i]; 247 resources = &dice->rx_resources[i]; 248 249 pcm_cache = dice->rx_pcm_chs[i][mode]; 250 midi_cache = dice->rx_midi_ports[i]; 251 err = snd_dice_transaction_read_rx(dice, 252 params->size * i + RX_NUMBER_AUDIO, 253 reg, sizeof(reg)); 254 } 255 if (err < 0) 256 return err; 257 pcm_chs = be32_to_cpu(reg[0]); 258 midi_ports = be32_to_cpu(reg[1]); 259 260 // These are important for developer of this driver. 261 if (pcm_chs != pcm_cache || midi_ports != midi_cache) { 262 dev_info(&dice->unit->device, 263 "cache mismatch: pcm: %u:%u, midi: %u:%u\n", 264 pcm_chs, pcm_cache, midi_ports, midi_cache); 265 return -EPROTO; 266 } 267 268 err = keep_resources(dice, stream, resources, rate, pcm_chs, 269 midi_ports); 270 if (err < 0) 271 return err; 272 } 273 274 return 0; 275 } 276 277 static void finish_session(struct snd_dice *dice, struct reg_params *tx_params, 278 struct reg_params *rx_params) 279 { 280 stop_streams(dice, AMDTP_IN_STREAM, tx_params); 281 stop_streams(dice, AMDTP_OUT_STREAM, rx_params); 282 283 snd_dice_transaction_clear_enable(dice); 284 } 285 286 int snd_dice_stream_reserve_duplex(struct snd_dice *dice, unsigned int rate) 287 { 288 unsigned int curr_rate; 289 int err; 290 291 // Check sampling transmission frequency. 292 err = snd_dice_transaction_get_rate(dice, &curr_rate); 293 if (err < 0) 294 return err; 295 if (rate == 0) 296 rate = curr_rate; 297 298 if (dice->substreams_counter == 0 || curr_rate != rate) { 299 struct reg_params tx_params, rx_params; 300 301 err = get_register_params(dice, &tx_params, &rx_params); 302 if (err < 0) 303 return err; 304 305 finish_session(dice, &tx_params, &rx_params); 306 307 release_resources(dice); 308 309 // Just after owning the unit (GLOBAL_OWNER), the unit can 310 // return invalid stream formats. Selecting clock parameters 311 // have an effect for the unit to refine it. 312 err = ensure_phase_lock(dice, rate); 313 if (err < 0) 314 return err; 315 316 // After changing sampling transfer frequency, the value of 317 // register can be changed. 318 err = get_register_params(dice, &tx_params, &rx_params); 319 if (err < 0) 320 return err; 321 322 err = keep_dual_resources(dice, rate, AMDTP_IN_STREAM, 323 &tx_params); 324 if (err < 0) 325 goto error; 326 327 err = keep_dual_resources(dice, rate, AMDTP_OUT_STREAM, 328 &rx_params); 329 if (err < 0) 330 goto error; 331 } 332 333 return 0; 334 error: 335 release_resources(dice); 336 return err; 337 } 338 339 void snd_dice_stream_release_duplex(struct snd_dice *dice) 340 { 341 if (dice->substreams_counter == 0) 342 release_resources(dice); 343 } 344 345 static int start_streams(struct snd_dice *dice, enum amdtp_stream_direction dir, 346 unsigned int rate, struct reg_params *params) 347 { 348 unsigned int max_speed = fw_parent_device(dice->unit)->max_speed; 349 int i; 350 int err; 351 352 for (i = 0; i < params->count; i++) { 353 struct amdtp_stream *stream; 354 struct fw_iso_resources *resources; 355 __be32 reg; 356 357 if (dir == AMDTP_IN_STREAM) { 358 stream = dice->tx_stream + i; 359 resources = dice->tx_resources + i; 360 } else { 361 stream = dice->rx_stream + i; 362 resources = dice->rx_resources + i; 363 } 364 365 reg = cpu_to_be32(resources->channel); 366 if (dir == AMDTP_IN_STREAM) { 367 err = snd_dice_transaction_write_tx(dice, 368 params->size * i + TX_ISOCHRONOUS, 369 ®, sizeof(reg)); 370 } else { 371 err = snd_dice_transaction_write_rx(dice, 372 params->size * i + RX_ISOCHRONOUS, 373 ®, sizeof(reg)); 374 } 375 if (err < 0) 376 return err; 377 378 if (dir == AMDTP_IN_STREAM) { 379 reg = cpu_to_be32(max_speed); 380 err = snd_dice_transaction_write_tx(dice, 381 params->size * i + TX_SPEED, 382 ®, sizeof(reg)); 383 if (err < 0) 384 return err; 385 } 386 387 err = amdtp_stream_start(stream, resources->channel, max_speed); 388 if (err < 0) 389 return err; 390 } 391 392 return err; 393 } 394 395 /* 396 * MEMO: After this function, there're two states of streams: 397 * - None streams are running. 398 * - All streams are running. 399 */ 400 int snd_dice_stream_start_duplex(struct snd_dice *dice) 401 { 402 unsigned int generation = dice->rx_resources[0].generation; 403 struct reg_params tx_params, rx_params; 404 unsigned int i; 405 unsigned int rate; 406 enum snd_dice_rate_mode mode; 407 int err; 408 409 if (dice->substreams_counter == 0) 410 return -EIO; 411 412 err = get_register_params(dice, &tx_params, &rx_params); 413 if (err < 0) 414 return err; 415 416 // Check error of packet streaming. 417 for (i = 0; i < MAX_STREAMS; ++i) { 418 if (amdtp_streaming_error(&dice->tx_stream[i]) || 419 amdtp_streaming_error(&dice->rx_stream[i])) { 420 finish_session(dice, &tx_params, &rx_params); 421 break; 422 } 423 } 424 425 if (generation != fw_parent_device(dice->unit)->card->generation) { 426 for (i = 0; i < MAX_STREAMS; ++i) { 427 if (i < tx_params.count) 428 fw_iso_resources_update(dice->tx_resources + i); 429 if (i < rx_params.count) 430 fw_iso_resources_update(dice->rx_resources + i); 431 } 432 } 433 434 // Check required streams are running or not. 435 err = snd_dice_transaction_get_rate(dice, &rate); 436 if (err < 0) 437 return err; 438 err = snd_dice_stream_get_rate_mode(dice, rate, &mode); 439 if (err < 0) 440 return err; 441 for (i = 0; i < MAX_STREAMS; ++i) { 442 if (dice->tx_pcm_chs[i][mode] > 0 && 443 !amdtp_stream_running(&dice->tx_stream[i])) 444 break; 445 if (dice->rx_pcm_chs[i][mode] > 0 && 446 !amdtp_stream_running(&dice->rx_stream[i])) 447 break; 448 } 449 if (i < MAX_STREAMS) { 450 // Start both streams. 451 err = start_streams(dice, AMDTP_IN_STREAM, rate, &tx_params); 452 if (err < 0) 453 goto error; 454 455 err = start_streams(dice, AMDTP_OUT_STREAM, rate, &rx_params); 456 if (err < 0) 457 goto error; 458 459 err = snd_dice_transaction_set_enable(dice); 460 if (err < 0) { 461 dev_err(&dice->unit->device, 462 "fail to enable interface\n"); 463 goto error; 464 } 465 466 for (i = 0; i < MAX_STREAMS; i++) { 467 if ((i < tx_params.count && 468 !amdtp_stream_wait_callback(&dice->tx_stream[i], 469 CALLBACK_TIMEOUT)) || 470 (i < rx_params.count && 471 !amdtp_stream_wait_callback(&dice->rx_stream[i], 472 CALLBACK_TIMEOUT))) { 473 err = -ETIMEDOUT; 474 goto error; 475 } 476 } 477 } 478 479 return 0; 480 error: 481 finish_session(dice, &tx_params, &rx_params); 482 return err; 483 } 484 485 /* 486 * MEMO: After this function, there're two states of streams: 487 * - None streams are running. 488 * - All streams are running. 489 */ 490 void snd_dice_stream_stop_duplex(struct snd_dice *dice) 491 { 492 struct reg_params tx_params, rx_params; 493 494 if (dice->substreams_counter == 0) { 495 if (get_register_params(dice, &tx_params, &rx_params) >= 0) 496 finish_session(dice, &tx_params, &rx_params); 497 } 498 } 499 500 static int init_stream(struct snd_dice *dice, enum amdtp_stream_direction dir, 501 unsigned int index) 502 { 503 struct amdtp_stream *stream; 504 struct fw_iso_resources *resources; 505 int err; 506 507 if (dir == AMDTP_IN_STREAM) { 508 stream = &dice->tx_stream[index]; 509 resources = &dice->tx_resources[index]; 510 } else { 511 stream = &dice->rx_stream[index]; 512 resources = &dice->rx_resources[index]; 513 } 514 515 err = fw_iso_resources_init(resources, dice->unit); 516 if (err < 0) 517 goto end; 518 resources->channels_mask = 0x00000000ffffffffuLL; 519 520 err = amdtp_am824_init(stream, dice->unit, dir, CIP_BLOCKING); 521 if (err < 0) { 522 amdtp_stream_destroy(stream); 523 fw_iso_resources_destroy(resources); 524 } 525 end: 526 return err; 527 } 528 529 /* 530 * This function should be called before starting streams or after stopping 531 * streams. 532 */ 533 static void destroy_stream(struct snd_dice *dice, 534 enum amdtp_stream_direction dir, 535 unsigned int index) 536 { 537 struct amdtp_stream *stream; 538 struct fw_iso_resources *resources; 539 540 if (dir == AMDTP_IN_STREAM) { 541 stream = &dice->tx_stream[index]; 542 resources = &dice->tx_resources[index]; 543 } else { 544 stream = &dice->rx_stream[index]; 545 resources = &dice->rx_resources[index]; 546 } 547 548 amdtp_stream_destroy(stream); 549 fw_iso_resources_destroy(resources); 550 } 551 552 int snd_dice_stream_init_duplex(struct snd_dice *dice) 553 { 554 int i, err; 555 556 for (i = 0; i < MAX_STREAMS; i++) { 557 err = init_stream(dice, AMDTP_IN_STREAM, i); 558 if (err < 0) { 559 for (; i >= 0; i--) 560 destroy_stream(dice, AMDTP_IN_STREAM, i); 561 goto end; 562 } 563 } 564 565 for (i = 0; i < MAX_STREAMS; i++) { 566 err = init_stream(dice, AMDTP_OUT_STREAM, i); 567 if (err < 0) { 568 for (; i >= 0; i--) 569 destroy_stream(dice, AMDTP_OUT_STREAM, i); 570 for (i = 0; i < MAX_STREAMS; i++) 571 destroy_stream(dice, AMDTP_IN_STREAM, i); 572 break; 573 } 574 } 575 end: 576 return err; 577 } 578 579 void snd_dice_stream_destroy_duplex(struct snd_dice *dice) 580 { 581 unsigned int i; 582 583 for (i = 0; i < MAX_STREAMS; i++) { 584 destroy_stream(dice, AMDTP_IN_STREAM, i); 585 destroy_stream(dice, AMDTP_OUT_STREAM, i); 586 } 587 } 588 589 void snd_dice_stream_update_duplex(struct snd_dice *dice) 590 { 591 struct reg_params tx_params, rx_params; 592 593 /* 594 * On a bus reset, the DICE firmware disables streaming and then goes 595 * off contemplating its own navel for hundreds of milliseconds before 596 * it can react to any of our attempts to reenable streaming. This 597 * means that we lose synchronization anyway, so we force our streams 598 * to stop so that the application can restart them in an orderly 599 * manner. 600 */ 601 dice->global_enabled = false; 602 603 if (get_register_params(dice, &tx_params, &rx_params) == 0) { 604 stop_streams(dice, AMDTP_IN_STREAM, &tx_params); 605 stop_streams(dice, AMDTP_OUT_STREAM, &rx_params); 606 } 607 } 608 609 int snd_dice_stream_detect_current_formats(struct snd_dice *dice) 610 { 611 unsigned int rate; 612 enum snd_dice_rate_mode mode; 613 __be32 reg[2]; 614 struct reg_params tx_params, rx_params; 615 int i; 616 int err; 617 618 /* If extended protocol is available, detect detail spec. */ 619 err = snd_dice_detect_extension_formats(dice); 620 if (err >= 0) 621 return err; 622 623 /* 624 * Available stream format is restricted at current mode of sampling 625 * clock. 626 */ 627 err = snd_dice_transaction_get_rate(dice, &rate); 628 if (err < 0) 629 return err; 630 631 err = snd_dice_stream_get_rate_mode(dice, rate, &mode); 632 if (err < 0) 633 return err; 634 635 /* 636 * Just after owning the unit (GLOBAL_OWNER), the unit can return 637 * invalid stream formats. Selecting clock parameters have an effect 638 * for the unit to refine it. 639 */ 640 err = ensure_phase_lock(dice, rate); 641 if (err < 0) 642 return err; 643 644 err = get_register_params(dice, &tx_params, &rx_params); 645 if (err < 0) 646 return err; 647 648 for (i = 0; i < tx_params.count; ++i) { 649 err = snd_dice_transaction_read_tx(dice, 650 tx_params.size * i + TX_NUMBER_AUDIO, 651 reg, sizeof(reg)); 652 if (err < 0) 653 return err; 654 dice->tx_pcm_chs[i][mode] = be32_to_cpu(reg[0]); 655 dice->tx_midi_ports[i] = max_t(unsigned int, 656 be32_to_cpu(reg[1]), dice->tx_midi_ports[i]); 657 } 658 for (i = 0; i < rx_params.count; ++i) { 659 err = snd_dice_transaction_read_rx(dice, 660 rx_params.size * i + RX_NUMBER_AUDIO, 661 reg, sizeof(reg)); 662 if (err < 0) 663 return err; 664 dice->rx_pcm_chs[i][mode] = be32_to_cpu(reg[0]); 665 dice->rx_midi_ports[i] = max_t(unsigned int, 666 be32_to_cpu(reg[1]), dice->rx_midi_ports[i]); 667 } 668 669 return 0; 670 } 671 672 static void dice_lock_changed(struct snd_dice *dice) 673 { 674 dice->dev_lock_changed = true; 675 wake_up(&dice->hwdep_wait); 676 } 677 678 int snd_dice_stream_lock_try(struct snd_dice *dice) 679 { 680 int err; 681 682 spin_lock_irq(&dice->lock); 683 684 if (dice->dev_lock_count < 0) { 685 err = -EBUSY; 686 goto out; 687 } 688 689 if (dice->dev_lock_count++ == 0) 690 dice_lock_changed(dice); 691 err = 0; 692 out: 693 spin_unlock_irq(&dice->lock); 694 return err; 695 } 696 697 void snd_dice_stream_lock_release(struct snd_dice *dice) 698 { 699 spin_lock_irq(&dice->lock); 700 701 if (WARN_ON(dice->dev_lock_count <= 0)) 702 goto out; 703 704 if (--dice->dev_lock_count == 0) 705 dice_lock_changed(dice); 706 out: 707 spin_unlock_irq(&dice->lock); 708 } 709