1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2012-2021 Ruslan Bukin <br@bsdpad.com> 5 * Copyright (c) 2023-2024 Florian Walpen <dev@submerge.ch> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * RME HDSPe driver for FreeBSD (pcm-part). 32 * Supported cards: AIO, RayDAT. 33 */ 34 35 #include <dev/sound/pcm/sound.h> 36 #include <dev/sound/pci/hdspe.h> 37 #include <dev/sound/chip.h> 38 39 #include <dev/pci/pcireg.h> 40 #include <dev/pci/pcivar.h> 41 42 #include <mixer_if.h> 43 44 #define HDSPE_MATRIX_MAX 8 45 46 struct hdspe_latency { 47 uint32_t n; 48 uint32_t period; 49 float ms; 50 }; 51 52 static struct hdspe_latency latency_map[] = { 53 { 7, 32, 0.7 }, 54 { 0, 64, 1.5 }, 55 { 1, 128, 3 }, 56 { 2, 256, 6 }, 57 { 3, 512, 12 }, 58 { 4, 1024, 23 }, 59 { 5, 2048, 46 }, 60 { 6, 4096, 93 }, 61 62 { 0, 0, 0 }, 63 }; 64 65 struct hdspe_rate { 66 uint32_t speed; 67 uint32_t reg; 68 }; 69 70 static struct hdspe_rate rate_map[] = { 71 { 32000, (HDSPE_FREQ_32000) }, 72 { 44100, (HDSPE_FREQ_44100) }, 73 { 48000, (HDSPE_FREQ_48000) }, 74 { 64000, (HDSPE_FREQ_32000 | HDSPE_FREQ_DOUBLE) }, 75 { 88200, (HDSPE_FREQ_44100 | HDSPE_FREQ_DOUBLE) }, 76 { 96000, (HDSPE_FREQ_48000 | HDSPE_FREQ_DOUBLE) }, 77 { 128000, (HDSPE_FREQ_32000 | HDSPE_FREQ_QUAD) }, 78 { 176400, (HDSPE_FREQ_44100 | HDSPE_FREQ_QUAD) }, 79 { 192000, (HDSPE_FREQ_48000 | HDSPE_FREQ_QUAD) }, 80 81 { 0, 0 }, 82 }; 83 84 static uint32_t 85 hdspe_channel_play_ports(struct hdspe_channel *hc) 86 { 87 return (hc->ports & (HDSPE_CHAN_AIO_ALL | HDSPE_CHAN_RAY_ALL)); 88 } 89 90 static uint32_t 91 hdspe_channel_rec_ports(struct hdspe_channel *hc) 92 { 93 return (hc->ports & (HDSPE_CHAN_AIO_ALL_REC | HDSPE_CHAN_RAY_ALL)); 94 } 95 96 static unsigned int 97 hdspe_adat_width(uint32_t speed) 98 { 99 if (speed > 96000) 100 return (2); 101 if (speed > 48000) 102 return (4); 103 return (8); 104 } 105 106 static uint32_t 107 hdspe_port_first(uint32_t ports) 108 { 109 return (ports & (~(ports - 1))); /* Extract first bit set. */ 110 } 111 112 static uint32_t 113 hdspe_port_first_row(uint32_t ports) 114 { 115 uint32_t ends; 116 117 /* Restrict ports to one set with contiguous slots. */ 118 if (ports & HDSPE_CHAN_AIO_LINE) 119 ports = HDSPE_CHAN_AIO_LINE; /* Gap in the AIO slots here. */ 120 else if (ports & HDSPE_CHAN_AIO_ALL) 121 ports &= HDSPE_CHAN_AIO_ALL; /* Rest of the AIO slots. */ 122 else if (ports & HDSPE_CHAN_RAY_ALL) 123 ports &= HDSPE_CHAN_RAY_ALL; /* All RayDAT slots. */ 124 125 /* Ends of port rows are followed by a port which is not in the set. */ 126 ends = ports & (~(ports >> 1)); 127 /* First row of contiguous ports ends in the first row end. */ 128 return (ports & (ends ^ (ends - 1))); 129 } 130 131 static unsigned int 132 hdspe_channel_count(uint32_t ports, uint32_t adat_width) 133 { 134 unsigned int count = 0; 135 136 if (ports & HDSPE_CHAN_AIO_ALL) { 137 /* AIO ports. */ 138 if (ports & HDSPE_CHAN_AIO_LINE) 139 count += 2; 140 if (ports & HDSPE_CHAN_AIO_PHONE) 141 count += 2; 142 if (ports & HDSPE_CHAN_AIO_AES) 143 count += 2; 144 if (ports & HDSPE_CHAN_AIO_SPDIF) 145 count += 2; 146 if (ports & HDSPE_CHAN_AIO_ADAT) 147 count += adat_width; 148 } else if (ports & HDSPE_CHAN_RAY_ALL) { 149 /* RayDAT ports. */ 150 if (ports & HDSPE_CHAN_RAY_AES) 151 count += 2; 152 if (ports & HDSPE_CHAN_RAY_SPDIF) 153 count += 2; 154 if (ports & HDSPE_CHAN_RAY_ADAT1) 155 count += adat_width; 156 if (ports & HDSPE_CHAN_RAY_ADAT2) 157 count += adat_width; 158 if (ports & HDSPE_CHAN_RAY_ADAT3) 159 count += adat_width; 160 if (ports & HDSPE_CHAN_RAY_ADAT4) 161 count += adat_width; 162 } 163 164 return (count); 165 } 166 167 static unsigned int 168 hdspe_channel_offset(uint32_t subset, uint32_t ports, unsigned int adat_width) 169 { 170 uint32_t preceding; 171 172 /* Make sure we have a subset of ports. */ 173 subset &= ports; 174 /* Include all ports preceding the first one of the subset. */ 175 preceding = ports & (~subset & (subset - 1)); 176 177 if (preceding & HDSPE_CHAN_AIO_ALL) 178 preceding &= HDSPE_CHAN_AIO_ALL; /* Contiguous AIO slots. */ 179 else if (preceding & HDSPE_CHAN_RAY_ALL) 180 preceding &= HDSPE_CHAN_RAY_ALL; /* Contiguous RayDAT slots. */ 181 182 return (hdspe_channel_count(preceding, adat_width)); 183 } 184 185 static unsigned int 186 hdspe_port_slot_offset(uint32_t port, unsigned int adat_width) 187 { 188 /* Exctract the first port (lowest bit) if set of ports. */ 189 switch (hdspe_port_first(port)) { 190 /* AIO ports */ 191 case HDSPE_CHAN_AIO_LINE: 192 return (0); 193 case HDSPE_CHAN_AIO_PHONE: 194 return (6); 195 case HDSPE_CHAN_AIO_AES: 196 return (8); 197 case HDSPE_CHAN_AIO_SPDIF: 198 return (10); 199 case HDSPE_CHAN_AIO_ADAT: 200 return (12); 201 202 /* RayDAT ports */ 203 case HDSPE_CHAN_RAY_AES: 204 return (0); 205 case HDSPE_CHAN_RAY_SPDIF: 206 return (2); 207 case HDSPE_CHAN_RAY_ADAT1: 208 return (4); 209 case HDSPE_CHAN_RAY_ADAT2: 210 return (4 + adat_width); 211 case HDSPE_CHAN_RAY_ADAT3: 212 return (4 + 2 * adat_width); 213 case HDSPE_CHAN_RAY_ADAT4: 214 return (4 + 3 * adat_width); 215 default: 216 return (0); 217 } 218 } 219 220 static unsigned int 221 hdspe_port_slot_width(uint32_t ports, unsigned int adat_width) 222 { 223 uint32_t row; 224 225 /* Count number of contiguous slots from the first physical port. */ 226 row = hdspe_port_first_row(ports); 227 return (hdspe_channel_count(row, adat_width)); 228 } 229 230 static int 231 hdspe_hw_mixer(struct sc_chinfo *ch, unsigned int dst, 232 unsigned int src, unsigned short data) 233 { 234 struct sc_pcminfo *scp; 235 struct sc_info *sc; 236 int offs; 237 238 scp = ch->parent; 239 sc = scp->sc; 240 241 offs = 0; 242 if (ch->dir == PCMDIR_PLAY) 243 offs = 64; 244 245 hdspe_write_4(sc, HDSPE_MIXER_BASE + 246 ((offs + src + 128 * dst) * sizeof(uint32_t)), 247 data & 0xFFFF); 248 249 return (0); 250 }; 251 252 static int 253 hdspechan_setgain(struct sc_chinfo *ch) 254 { 255 struct sc_info *sc; 256 uint32_t port, ports; 257 unsigned int slot, end_slot; 258 unsigned short volume; 259 260 sc = ch->parent->sc; 261 262 /* Iterate through all physical ports of the channel. */ 263 ports = ch->ports; 264 port = hdspe_port_first(ports); 265 while (port != 0) { 266 /* Get slot range of the physical port. */ 267 slot = 268 hdspe_port_slot_offset(port, hdspe_adat_width(sc->speed)); 269 end_slot = slot + 270 hdspe_port_slot_width(port, hdspe_adat_width(sc->speed)); 271 272 /* Treat first slot as left channel. */ 273 volume = ch->lvol * HDSPE_MAX_GAIN / 100; 274 for (; slot < end_slot; slot++) { 275 hdspe_hw_mixer(ch, slot, slot, volume); 276 /* Subsequent slots all get the right channel volume. */ 277 volume = ch->rvol * HDSPE_MAX_GAIN / 100; 278 } 279 280 ports &= ~port; 281 port = hdspe_port_first(ports); 282 } 283 284 return (0); 285 } 286 287 static int 288 hdspemixer_init(struct snd_mixer *m) 289 { 290 struct sc_pcminfo *scp; 291 struct sc_info *sc; 292 int mask; 293 294 scp = mix_getdevinfo(m); 295 sc = scp->sc; 296 if (sc == NULL) 297 return (-1); 298 299 mask = SOUND_MASK_PCM; 300 301 if (hdspe_channel_play_ports(scp->hc)) 302 mask |= SOUND_MASK_VOLUME; 303 304 if (hdspe_channel_rec_ports(scp->hc)) 305 mask |= SOUND_MASK_RECLEV; 306 307 snd_mtxlock(sc->lock); 308 pcm_setflags(scp->dev, pcm_getflags(scp->dev) | SD_F_SOFTPCMVOL); 309 mix_setdevs(m, mask); 310 snd_mtxunlock(sc->lock); 311 312 return (0); 313 } 314 315 static int 316 hdspemixer_set(struct snd_mixer *m, unsigned dev, 317 unsigned left, unsigned right) 318 { 319 struct sc_pcminfo *scp; 320 struct sc_chinfo *ch; 321 int i; 322 323 scp = mix_getdevinfo(m); 324 325 #if 0 326 device_printf(scp->dev, "hdspemixer_set() %d %d\n", 327 left, right); 328 #endif 329 330 for (i = 0; i < scp->chnum; i++) { 331 ch = &scp->chan[i]; 332 if ((dev == SOUND_MIXER_VOLUME && ch->dir == PCMDIR_PLAY) || 333 (dev == SOUND_MIXER_RECLEV && ch->dir == PCMDIR_REC)) { 334 ch->lvol = left; 335 ch->rvol = right; 336 if (ch->run) 337 hdspechan_setgain(ch); 338 } 339 } 340 341 return (0); 342 } 343 344 static kobj_method_t hdspemixer_methods[] = { 345 KOBJMETHOD(mixer_init, hdspemixer_init), 346 KOBJMETHOD(mixer_set, hdspemixer_set), 347 KOBJMETHOD_END 348 }; 349 MIXER_DECLARE(hdspemixer); 350 351 static void 352 hdspechan_enable(struct sc_chinfo *ch, int value) 353 { 354 struct sc_pcminfo *scp; 355 struct sc_info *sc; 356 uint32_t row, ports; 357 int reg; 358 unsigned int slot, end_slot; 359 360 scp = ch->parent; 361 sc = scp->sc; 362 363 if (ch->dir == PCMDIR_PLAY) 364 reg = HDSPE_OUT_ENABLE_BASE; 365 else 366 reg = HDSPE_IN_ENABLE_BASE; 367 368 ch->run = value; 369 370 /* Iterate through rows of ports with contiguous slots. */ 371 ports = ch->ports; 372 row = hdspe_port_first_row(ports); 373 while (row != 0) { 374 slot = 375 hdspe_port_slot_offset(row, hdspe_adat_width(sc->speed)); 376 end_slot = slot + 377 hdspe_port_slot_width(row, hdspe_adat_width(sc->speed)); 378 379 for (; slot < end_slot; slot++) { 380 hdspe_write_1(sc, reg + (4 * slot), value); 381 } 382 383 ports &= ~row; 384 row = hdspe_port_first_row(ports); 385 } 386 } 387 388 static int 389 hdspe_running(struct sc_info *sc) 390 { 391 struct sc_pcminfo *scp; 392 struct sc_chinfo *ch; 393 device_t *devlist; 394 int devcount; 395 int i, j; 396 int err; 397 398 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 399 goto bad; 400 401 for (i = 0; i < devcount; i++) { 402 scp = device_get_ivars(devlist[i]); 403 for (j = 0; j < scp->chnum; j++) { 404 ch = &scp->chan[j]; 405 if (ch->run) 406 goto bad; 407 } 408 } 409 410 free(devlist, M_TEMP); 411 412 return (0); 413 bad: 414 415 #if 0 416 device_printf(sc->dev, "hdspe is running\n"); 417 #endif 418 419 free(devlist, M_TEMP); 420 421 return (1); 422 } 423 424 static void 425 hdspe_start_audio(struct sc_info *sc) 426 { 427 428 sc->ctrl_register |= (HDSPE_AUDIO_INT_ENABLE | HDSPE_ENABLE); 429 hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register); 430 } 431 432 static void 433 hdspe_stop_audio(struct sc_info *sc) 434 { 435 436 if (hdspe_running(sc) == 1) 437 return; 438 439 sc->ctrl_register &= ~(HDSPE_AUDIO_INT_ENABLE | HDSPE_ENABLE); 440 hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register); 441 } 442 443 static void 444 buffer_mux_write(uint32_t *dma, uint32_t *pcm, unsigned int pos, 445 unsigned int samples, unsigned int slots, unsigned int channels) 446 { 447 int slot; 448 449 for (; samples > 0; samples--) { 450 for (slot = 0; slot < slots; slot++) { 451 dma[slot * HDSPE_CHANBUF_SAMPLES + pos] = 452 pcm[pos * channels + slot]; 453 } 454 pos = (pos + 1) % HDSPE_CHANBUF_SAMPLES; 455 } 456 } 457 458 static void 459 buffer_mux_port(uint32_t *dma, uint32_t *pcm, uint32_t subset, uint32_t ports, 460 unsigned int pos, unsigned int samples, unsigned int adat_width, 461 unsigned int pcm_width) 462 { 463 unsigned int slot_offset, slots; 464 unsigned int channels, chan_pos; 465 466 /* Translate DMA slot offset to DMA buffer offset. */ 467 slot_offset = hdspe_port_slot_offset(subset, adat_width); 468 dma += slot_offset * HDSPE_CHANBUF_SAMPLES; 469 470 /* Channel position of the port subset and total number of channels. */ 471 chan_pos = hdspe_channel_offset(subset, ports, pcm_width); 472 pcm += chan_pos; 473 channels = hdspe_channel_count(ports, pcm_width); 474 475 /* Only copy as much as supported by both hardware and pcm channel. */ 476 slots = hdspe_port_slot_width(subset, MIN(adat_width, pcm_width)); 477 478 /* Let the compiler inline and loop unroll common cases. */ 479 if (slots == 2) 480 buffer_mux_write(dma, pcm, pos, samples, 2, channels); 481 else if (slots == 4) 482 buffer_mux_write(dma, pcm, pos, samples, 4, channels); 483 else if (slots == 8) 484 buffer_mux_write(dma, pcm, pos, samples, 8, channels); 485 else 486 buffer_mux_write(dma, pcm, pos, samples, slots, channels); 487 } 488 489 static void 490 buffer_demux_read(uint32_t *dma, uint32_t *pcm, unsigned int pos, 491 unsigned int samples, unsigned int slots, unsigned int channels) 492 { 493 int slot; 494 495 for (; samples > 0; samples--) { 496 for (slot = 0; slot < slots; slot++) { 497 pcm[pos * channels + slot] = 498 dma[slot * HDSPE_CHANBUF_SAMPLES + pos]; 499 } 500 pos = (pos + 1) % HDSPE_CHANBUF_SAMPLES; 501 } 502 } 503 504 static void 505 buffer_demux_port(uint32_t *dma, uint32_t *pcm, uint32_t subset, uint32_t ports, 506 unsigned int pos, unsigned int samples, unsigned int adat_width, 507 unsigned int pcm_width) 508 { 509 unsigned int slot_offset, slots; 510 unsigned int channels, chan_pos; 511 512 /* Translate port slot offset to DMA buffer offset. */ 513 slot_offset = hdspe_port_slot_offset(subset, adat_width); 514 dma += slot_offset * HDSPE_CHANBUF_SAMPLES; 515 516 /* Channel position of the port subset and total number of channels. */ 517 chan_pos = hdspe_channel_offset(subset, ports, pcm_width); 518 pcm += chan_pos; 519 channels = hdspe_channel_count(ports, pcm_width); 520 521 /* Only copy as much as supported by both hardware and pcm channel. */ 522 slots = hdspe_port_slot_width(subset, MIN(adat_width, pcm_width)); 523 524 /* Let the compiler inline and loop unroll common cases. */ 525 if (slots == 2) 526 buffer_demux_read(dma, pcm, pos, samples, 2, channels); 527 else if (slots == 4) 528 buffer_demux_read(dma, pcm, pos, samples, 4, channels); 529 else if (slots == 8) 530 buffer_demux_read(dma, pcm, pos, samples, 8, channels); 531 else 532 buffer_demux_read(dma, pcm, pos, samples, slots, channels); 533 } 534 535 536 /* Copy data between DMA and PCM buffers. */ 537 static void 538 buffer_copy(struct sc_chinfo *ch) 539 { 540 struct sc_pcminfo *scp; 541 struct sc_info *sc; 542 uint32_t row, ports; 543 uint32_t dma_pos; 544 unsigned int pos, length, offset; 545 unsigned int n; 546 unsigned int adat_width, pcm_width; 547 548 scp = ch->parent; 549 sc = scp->sc; 550 551 n = AFMT_CHANNEL(ch->format); /* n channels */ 552 553 /* Let pcm formats differ from current hardware ADAT width. */ 554 adat_width = hdspe_adat_width(sc->speed); 555 if (n == hdspe_channel_count(ch->ports, 2)) 556 pcm_width = 2; 557 else if (n == hdspe_channel_count(ch->ports, 4)) 558 pcm_width = 4; 559 else 560 pcm_width = 8; 561 562 /* Derive buffer position and length to be copied. */ 563 if (ch->dir == PCMDIR_PLAY) { 564 /* Position per channel is n times smaller than PCM. */ 565 pos = sndbuf_getreadyptr(ch->buffer) / n; 566 length = sndbuf_getready(ch->buffer) / n; 567 /* Copy no more than 2 periods in advance. */ 568 if (length > (sc->period * 4 * 2)) 569 length = (sc->period * 4 * 2); 570 /* Skip what was already copied last time. */ 571 offset = (ch->position + HDSPE_CHANBUF_SIZE) - pos; 572 offset %= HDSPE_CHANBUF_SIZE; 573 if (offset <= length) { 574 pos = (pos + offset) % HDSPE_CHANBUF_SIZE; 575 length -= offset; 576 } 577 } else { 578 /* Position per channel is n times smaller than PCM. */ 579 pos = sndbuf_getfreeptr(ch->buffer) / n; 580 /* Get DMA buffer write position. */ 581 dma_pos = hdspe_read_2(sc, HDSPE_STATUS_REG); 582 dma_pos &= HDSPE_BUF_POSITION_MASK; 583 /* Copy what is newly available. */ 584 length = (dma_pos + HDSPE_CHANBUF_SIZE) - pos; 585 length %= HDSPE_CHANBUF_SIZE; 586 } 587 588 /* Position and length in samples (4 bytes). */ 589 pos /= 4; 590 length /= 4; 591 592 /* Iterate through rows of ports with contiguous slots. */ 593 ports = ch->ports; 594 if (pcm_width == adat_width) 595 row = hdspe_port_first_row(ports); 596 else 597 row = hdspe_port_first(ports); 598 599 while (row != 0) { 600 if (ch->dir == PCMDIR_PLAY) 601 buffer_mux_port(sc->pbuf, ch->data, row, ch->ports, pos, 602 length, adat_width, pcm_width); 603 else 604 buffer_demux_port(sc->rbuf, ch->data, row, ch->ports, 605 pos, length, adat_width, pcm_width); 606 607 ports &= ~row; 608 if (pcm_width == adat_width) 609 row = hdspe_port_first_row(ports); 610 else 611 row = hdspe_port_first(ports); 612 } 613 614 ch->position = ((pos + length) * 4) % HDSPE_CHANBUF_SIZE; 615 } 616 617 static int 618 clean(struct sc_chinfo *ch) 619 { 620 struct sc_pcminfo *scp; 621 struct sc_info *sc; 622 uint32_t *buf; 623 uint32_t row, ports; 624 unsigned int offset, slots; 625 626 scp = ch->parent; 627 sc = scp->sc; 628 buf = sc->rbuf; 629 630 if (ch->dir == PCMDIR_PLAY) 631 buf = sc->pbuf; 632 633 /* Iterate through rows of ports with contiguous slots. */ 634 ports = ch->ports; 635 row = hdspe_port_first_row(ports); 636 while (row != 0) { 637 offset = hdspe_port_slot_offset(row, 638 hdspe_adat_width(sc->speed)); 639 slots = hdspe_port_slot_width(row, hdspe_adat_width(sc->speed)); 640 641 bzero(buf + offset * HDSPE_CHANBUF_SAMPLES, 642 slots * HDSPE_CHANBUF_SIZE); 643 644 ports &= ~row; 645 row = hdspe_port_first_row(ports); 646 } 647 648 ch->position = 0; 649 650 return (0); 651 } 652 653 /* Channel interface. */ 654 static void * 655 hdspechan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, 656 struct pcm_channel *c, int dir) 657 { 658 struct sc_pcminfo *scp; 659 struct sc_chinfo *ch; 660 struct sc_info *sc; 661 int num; 662 663 scp = devinfo; 664 sc = scp->sc; 665 666 snd_mtxlock(sc->lock); 667 num = scp->chnum; 668 669 ch = &scp->chan[num]; 670 671 if (dir == PCMDIR_PLAY) 672 ch->ports = hdspe_channel_play_ports(scp->hc); 673 else 674 ch->ports = hdspe_channel_rec_ports(scp->hc); 675 676 ch->run = 0; 677 ch->lvol = 0; 678 ch->rvol = 0; 679 680 /* Support all possible ADAT widths as channel formats. */ 681 ch->cap_fmts[0] = 682 SND_FORMAT(AFMT_S32_LE, hdspe_channel_count(ch->ports, 2), 0); 683 ch->cap_fmts[1] = 684 SND_FORMAT(AFMT_S32_LE, hdspe_channel_count(ch->ports, 4), 0); 685 ch->cap_fmts[2] = 686 SND_FORMAT(AFMT_S32_LE, hdspe_channel_count(ch->ports, 8), 0); 687 ch->cap_fmts[3] = 0; 688 ch->caps = malloc(sizeof(struct pcmchan_caps), M_HDSPE, M_NOWAIT); 689 *(ch->caps) = (struct pcmchan_caps) {32000, 192000, ch->cap_fmts, 0}; 690 691 /* Allocate maximum buffer size. */ 692 ch->size = HDSPE_CHANBUF_SIZE * hdspe_channel_count(ch->ports, 8); 693 ch->data = malloc(ch->size, M_HDSPE, M_NOWAIT); 694 ch->position = 0; 695 696 ch->buffer = b; 697 ch->channel = c; 698 ch->parent = scp; 699 700 ch->dir = dir; 701 702 snd_mtxunlock(sc->lock); 703 704 if (sndbuf_setup(ch->buffer, ch->data, ch->size) != 0) { 705 device_printf(scp->dev, "Can't setup sndbuf.\n"); 706 return (NULL); 707 } 708 709 return (ch); 710 } 711 712 static int 713 hdspechan_trigger(kobj_t obj, void *data, int go) 714 { 715 struct sc_pcminfo *scp; 716 struct sc_chinfo *ch; 717 struct sc_info *sc; 718 719 ch = data; 720 scp = ch->parent; 721 sc = scp->sc; 722 723 snd_mtxlock(sc->lock); 724 switch (go) { 725 case PCMTRIG_START: 726 #if 0 727 device_printf(scp->dev, "hdspechan_trigger(): start\n"); 728 #endif 729 hdspechan_enable(ch, 1); 730 hdspechan_setgain(ch); 731 hdspe_start_audio(sc); 732 break; 733 734 case PCMTRIG_STOP: 735 case PCMTRIG_ABORT: 736 #if 0 737 device_printf(scp->dev, "hdspechan_trigger(): stop or abort\n"); 738 #endif 739 clean(ch); 740 hdspechan_enable(ch, 0); 741 hdspe_stop_audio(sc); 742 break; 743 744 case PCMTRIG_EMLDMAWR: 745 case PCMTRIG_EMLDMARD: 746 if(ch->run) 747 buffer_copy(ch); 748 break; 749 } 750 751 snd_mtxunlock(sc->lock); 752 753 return (0); 754 } 755 756 static uint32_t 757 hdspechan_getptr(kobj_t obj, void *data) 758 { 759 struct sc_pcminfo *scp; 760 struct sc_chinfo *ch; 761 struct sc_info *sc; 762 uint32_t ret, pos; 763 764 ch = data; 765 scp = ch->parent; 766 sc = scp->sc; 767 768 snd_mtxlock(sc->lock); 769 ret = hdspe_read_2(sc, HDSPE_STATUS_REG); 770 snd_mtxunlock(sc->lock); 771 772 pos = ret & HDSPE_BUF_POSITION_MASK; 773 pos *= AFMT_CHANNEL(ch->format); /* Hardbuf with multiple channels. */ 774 775 return (pos); 776 } 777 778 static int 779 hdspechan_free(kobj_t obj, void *data) 780 { 781 struct sc_pcminfo *scp; 782 struct sc_chinfo *ch; 783 struct sc_info *sc; 784 785 ch = data; 786 scp = ch->parent; 787 sc = scp->sc; 788 789 #if 0 790 device_printf(scp->dev, "hdspechan_free()\n"); 791 #endif 792 793 snd_mtxlock(sc->lock); 794 if (ch->data != NULL) { 795 free(ch->data, M_HDSPE); 796 ch->data = NULL; 797 } 798 if (ch->caps != NULL) { 799 free(ch->caps, M_HDSPE); 800 ch->caps = NULL; 801 } 802 snd_mtxunlock(sc->lock); 803 804 return (0); 805 } 806 807 static int 808 hdspechan_setformat(kobj_t obj, void *data, uint32_t format) 809 { 810 struct sc_chinfo *ch; 811 812 ch = data; 813 814 #if 0 815 struct sc_pcminfo *scp = ch->parent; 816 device_printf(scp->dev, "hdspechan_setformat(%d)\n", format); 817 #endif 818 819 ch->format = format; 820 821 return (0); 822 } 823 824 static uint32_t 825 hdspechan_setspeed(kobj_t obj, void *data, uint32_t speed) 826 { 827 struct sc_pcminfo *scp; 828 struct hdspe_rate *hr; 829 struct sc_chinfo *ch; 830 struct sc_info *sc; 831 long long period; 832 int threshold; 833 int i; 834 835 ch = data; 836 scp = ch->parent; 837 sc = scp->sc; 838 hr = NULL; 839 840 #if 0 841 device_printf(scp->dev, "hdspechan_setspeed(%d)\n", speed); 842 #endif 843 844 if (hdspe_running(sc) == 1) 845 goto end; 846 847 if (sc->force_speed > 0) 848 speed = sc->force_speed; 849 850 /* First look for equal frequency. */ 851 for (i = 0; rate_map[i].speed != 0; i++) { 852 if (rate_map[i].speed == speed) 853 hr = &rate_map[i]; 854 } 855 856 /* If no match, just find nearest. */ 857 if (hr == NULL) { 858 for (i = 0; rate_map[i].speed != 0; i++) { 859 hr = &rate_map[i]; 860 threshold = hr->speed + ((rate_map[i + 1].speed != 0) ? 861 ((rate_map[i + 1].speed - hr->speed) >> 1) : 0); 862 if (speed < threshold) 863 break; 864 } 865 } 866 867 switch (sc->type) { 868 case HDSPE_RAYDAT: 869 case HDSPE_AIO: 870 period = HDSPE_FREQ_AIO; 871 break; 872 default: 873 /* Unsupported card. */ 874 goto end; 875 } 876 877 /* Write frequency on the device. */ 878 sc->ctrl_register &= ~HDSPE_FREQ_MASK; 879 sc->ctrl_register |= hr->reg; 880 hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register); 881 882 speed = hr->speed; 883 if (speed > 96000) 884 speed /= 4; 885 else if (speed > 48000) 886 speed /= 2; 887 888 /* Set DDS value. */ 889 period /= speed; 890 hdspe_write_4(sc, HDSPE_FREQ_REG, period); 891 892 sc->speed = hr->speed; 893 end: 894 895 return (sc->speed); 896 } 897 898 static uint32_t 899 hdspechan_setblocksize(kobj_t obj, void *data, uint32_t blocksize) 900 { 901 struct hdspe_latency *hl; 902 struct sc_pcminfo *scp; 903 struct sc_chinfo *ch; 904 struct sc_info *sc; 905 int threshold; 906 int i; 907 908 ch = data; 909 scp = ch->parent; 910 sc = scp->sc; 911 hl = NULL; 912 913 #if 0 914 device_printf(scp->dev, "hdspechan_setblocksize(%d)\n", blocksize); 915 #endif 916 917 if (hdspe_running(sc) == 1) 918 goto end; 919 920 if (blocksize > HDSPE_LAT_BYTES_MAX) 921 blocksize = HDSPE_LAT_BYTES_MAX; 922 else if (blocksize < HDSPE_LAT_BYTES_MIN) 923 blocksize = HDSPE_LAT_BYTES_MIN; 924 925 blocksize /= 4 /* samples */; 926 927 if (sc->force_period > 0) 928 blocksize = sc->force_period; 929 930 /* First look for equal latency. */ 931 for (i = 0; latency_map[i].period != 0; i++) { 932 if (latency_map[i].period == blocksize) 933 hl = &latency_map[i]; 934 } 935 936 /* If no match, just find nearest. */ 937 if (hl == NULL) { 938 for (i = 0; latency_map[i].period != 0; i++) { 939 hl = &latency_map[i]; 940 threshold = hl->period + ((latency_map[i + 1].period != 0) ? 941 ((latency_map[i + 1].period - hl->period) >> 1) : 0); 942 if (blocksize < threshold) 943 break; 944 } 945 } 946 947 snd_mtxlock(sc->lock); 948 sc->ctrl_register &= ~HDSPE_LAT_MASK; 949 sc->ctrl_register |= hdspe_encode_latency(hl->n); 950 hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register); 951 sc->period = hl->period; 952 snd_mtxunlock(sc->lock); 953 954 #if 0 955 device_printf(scp->dev, "New period=%d\n", sc->period); 956 #endif 957 958 sndbuf_resize(ch->buffer, 959 (HDSPE_CHANBUF_SIZE * AFMT_CHANNEL(ch->format)) / (sc->period * 4), 960 (sc->period * 4)); 961 end: 962 963 return (sndbuf_getblksz(ch->buffer)); 964 } 965 966 static uint32_t hdspe_bkp_fmt[] = { 967 SND_FORMAT(AFMT_S32_LE, 2, 0), 968 0 969 }; 970 971 static struct pcmchan_caps hdspe_bkp_caps = {32000, 192000, hdspe_bkp_fmt, 0}; 972 973 static struct pcmchan_caps * 974 hdspechan_getcaps(kobj_t obj, void *data) 975 { 976 struct sc_chinfo *ch; 977 978 ch = data; 979 980 #if 0 981 struct sc_pcminfo *scl = ch->parent; 982 device_printf(scp->dev, "hdspechan_getcaps()\n"); 983 #endif 984 985 if (ch->caps != NULL) 986 return (ch->caps); 987 988 return (&hdspe_bkp_caps); 989 } 990 991 static kobj_method_t hdspechan_methods[] = { 992 KOBJMETHOD(channel_init, hdspechan_init), 993 KOBJMETHOD(channel_free, hdspechan_free), 994 KOBJMETHOD(channel_setformat, hdspechan_setformat), 995 KOBJMETHOD(channel_setspeed, hdspechan_setspeed), 996 KOBJMETHOD(channel_setblocksize, hdspechan_setblocksize), 997 KOBJMETHOD(channel_trigger, hdspechan_trigger), 998 KOBJMETHOD(channel_getptr, hdspechan_getptr), 999 KOBJMETHOD(channel_getcaps, hdspechan_getcaps), 1000 KOBJMETHOD_END 1001 }; 1002 CHANNEL_DECLARE(hdspechan); 1003 1004 static int 1005 hdspe_pcm_probe(device_t dev) 1006 { 1007 1008 #if 0 1009 device_printf(dev,"hdspe_pcm_probe()\n"); 1010 #endif 1011 1012 return (0); 1013 } 1014 1015 static uint32_t 1016 hdspe_pcm_intr(struct sc_pcminfo *scp) 1017 { 1018 struct sc_chinfo *ch; 1019 struct sc_info *sc; 1020 int i; 1021 1022 sc = scp->sc; 1023 1024 for (i = 0; i < scp->chnum; i++) { 1025 ch = &scp->chan[i]; 1026 snd_mtxunlock(sc->lock); 1027 chn_intr(ch->channel); 1028 snd_mtxlock(sc->lock); 1029 } 1030 1031 return (0); 1032 } 1033 1034 static int 1035 hdspe_pcm_attach(device_t dev) 1036 { 1037 char status[SND_STATUSLEN]; 1038 struct sc_pcminfo *scp; 1039 const char *buf; 1040 uint32_t pcm_flags; 1041 int err; 1042 int play, rec; 1043 1044 scp = device_get_ivars(dev); 1045 scp->ih = &hdspe_pcm_intr; 1046 1047 if (scp->hc->ports & HDSPE_CHAN_AIO_ALL) 1048 buf = "AIO"; 1049 else if (scp->hc->ports & HDSPE_CHAN_RAY_ALL) 1050 buf = "RayDAT"; 1051 else 1052 buf = "?"; 1053 device_set_descf(dev, "HDSPe %s [%s]", buf, scp->hc->descr); 1054 1055 /* 1056 * We don't register interrupt handler with snd_setup_intr 1057 * in pcm device. Mark pcm device as MPSAFE manually. 1058 */ 1059 pcm_flags = pcm_getflags(dev) | SD_F_MPSAFE; 1060 if (hdspe_channel_count(scp->hc->ports, 8) > HDSPE_MATRIX_MAX) 1061 /* Disable vchan conversion, too many channels. */ 1062 pcm_flags |= SD_F_BITPERFECT; 1063 pcm_setflags(dev, pcm_flags); 1064 1065 play = (hdspe_channel_play_ports(scp->hc)) ? 1 : 0; 1066 rec = (hdspe_channel_rec_ports(scp->hc)) ? 1 : 0; 1067 err = pcm_register(dev, scp, play, rec); 1068 if (err) { 1069 device_printf(dev, "Can't register pcm.\n"); 1070 return (ENXIO); 1071 } 1072 1073 scp->chnum = 0; 1074 if (play) { 1075 pcm_addchan(dev, PCMDIR_PLAY, &hdspechan_class, scp); 1076 scp->chnum++; 1077 } 1078 1079 if (rec) { 1080 pcm_addchan(dev, PCMDIR_REC, &hdspechan_class, scp); 1081 scp->chnum++; 1082 } 1083 1084 snprintf(status, SND_STATUSLEN, "port 0x%jx irq %jd on %s", 1085 rman_get_start(scp->sc->cs), 1086 rman_get_start(scp->sc->irq), 1087 device_get_nameunit(device_get_parent(dev))); 1088 pcm_setstatus(dev, status); 1089 1090 mixer_init(dev, &hdspemixer_class, scp); 1091 1092 return (0); 1093 } 1094 1095 static int 1096 hdspe_pcm_detach(device_t dev) 1097 { 1098 int err; 1099 1100 err = pcm_unregister(dev); 1101 if (err) { 1102 device_printf(dev, "Can't unregister device.\n"); 1103 return (err); 1104 } 1105 1106 return (0); 1107 } 1108 1109 static device_method_t hdspe_pcm_methods[] = { 1110 DEVMETHOD(device_probe, hdspe_pcm_probe), 1111 DEVMETHOD(device_attach, hdspe_pcm_attach), 1112 DEVMETHOD(device_detach, hdspe_pcm_detach), 1113 { 0, 0 } 1114 }; 1115 1116 static driver_t hdspe_pcm_driver = { 1117 "pcm", 1118 hdspe_pcm_methods, 1119 PCM_SOFTC_SIZE, 1120 }; 1121 1122 DRIVER_MODULE(snd_hdspe_pcm, hdspe, hdspe_pcm_driver, 0, 0); 1123 MODULE_DEPEND(snd_hdspe, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 1124 MODULE_VERSION(snd_hdspe, 1); 1125