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
38 #include <dev/pci/pcireg.h>
39 #include <dev/pci/pcivar.h>
40
41 #include <mixer_if.h>
42
43 #define HDSPE_MATRIX_MAX 8
44
45 struct hdspe_latency {
46 uint32_t n;
47 uint32_t period;
48 float ms;
49 };
50
51 static struct hdspe_latency latency_map[] = {
52 { 7, 32, 0.7 },
53 { 0, 64, 1.5 },
54 { 1, 128, 3 },
55 { 2, 256, 6 },
56 { 3, 512, 12 },
57 { 4, 1024, 23 },
58 { 5, 2048, 46 },
59 { 6, 4096, 93 },
60
61 { 0, 0, 0 },
62 };
63
64 struct hdspe_rate {
65 uint32_t speed;
66 uint32_t reg;
67 };
68
69 static struct hdspe_rate rate_map[] = {
70 { 32000, (HDSPE_FREQ_32000) },
71 { 44100, (HDSPE_FREQ_44100) },
72 { 48000, (HDSPE_FREQ_48000) },
73 { 64000, (HDSPE_FREQ_32000 | HDSPE_FREQ_DOUBLE) },
74 { 88200, (HDSPE_FREQ_44100 | HDSPE_FREQ_DOUBLE) },
75 { 96000, (HDSPE_FREQ_48000 | HDSPE_FREQ_DOUBLE) },
76 { 128000, (HDSPE_FREQ_32000 | HDSPE_FREQ_QUAD) },
77 { 176400, (HDSPE_FREQ_44100 | HDSPE_FREQ_QUAD) },
78 { 192000, (HDSPE_FREQ_48000 | HDSPE_FREQ_QUAD) },
79
80 { 0, 0 },
81 };
82
83 static uint32_t
hdspe_channel_play_ports(struct hdspe_channel * hc)84 hdspe_channel_play_ports(struct hdspe_channel *hc)
85 {
86 return (hc->ports & (HDSPE_CHAN_AIO_ALL | HDSPE_CHAN_RAY_ALL));
87 }
88
89 static uint32_t
hdspe_channel_rec_ports(struct hdspe_channel * hc)90 hdspe_channel_rec_ports(struct hdspe_channel *hc)
91 {
92 return (hc->ports & (HDSPE_CHAN_AIO_ALL_REC | HDSPE_CHAN_RAY_ALL));
93 }
94
95 static unsigned int
hdspe_adat_width(uint32_t speed)96 hdspe_adat_width(uint32_t speed)
97 {
98 if (speed > 96000)
99 return (2);
100 if (speed > 48000)
101 return (4);
102 return (8);
103 }
104
105 static uint32_t
hdspe_port_first(uint32_t ports)106 hdspe_port_first(uint32_t ports)
107 {
108 return (ports & (~(ports - 1))); /* Extract first bit set. */
109 }
110
111 static uint32_t
hdspe_port_first_row(uint32_t ports)112 hdspe_port_first_row(uint32_t ports)
113 {
114 uint32_t ends;
115
116 /* Restrict ports to one set with contiguous slots. */
117 if (ports & HDSPE_CHAN_AIO_LINE)
118 ports = HDSPE_CHAN_AIO_LINE; /* Gap in the AIO slots here. */
119 else if (ports & HDSPE_CHAN_AIO_ALL)
120 ports &= HDSPE_CHAN_AIO_ALL; /* Rest of the AIO slots. */
121 else if (ports & HDSPE_CHAN_RAY_ALL)
122 ports &= HDSPE_CHAN_RAY_ALL; /* All RayDAT slots. */
123
124 /* Ends of port rows are followed by a port which is not in the set. */
125 ends = ports & (~(ports >> 1));
126 /* First row of contiguous ports ends in the first row end. */
127 return (ports & (ends ^ (ends - 1)));
128 }
129
130 static unsigned int
hdspe_channel_count(uint32_t ports,uint32_t adat_width)131 hdspe_channel_count(uint32_t ports, uint32_t adat_width)
132 {
133 unsigned int count = 0;
134
135 if (ports & HDSPE_CHAN_AIO_ALL) {
136 /* AIO ports. */
137 if (ports & HDSPE_CHAN_AIO_LINE)
138 count += 2;
139 if (ports & HDSPE_CHAN_AIO_PHONE)
140 count += 2;
141 if (ports & HDSPE_CHAN_AIO_AES)
142 count += 2;
143 if (ports & HDSPE_CHAN_AIO_SPDIF)
144 count += 2;
145 if (ports & HDSPE_CHAN_AIO_ADAT)
146 count += adat_width;
147 } else if (ports & HDSPE_CHAN_RAY_ALL) {
148 /* RayDAT ports. */
149 if (ports & HDSPE_CHAN_RAY_AES)
150 count += 2;
151 if (ports & HDSPE_CHAN_RAY_SPDIF)
152 count += 2;
153 if (ports & HDSPE_CHAN_RAY_ADAT1)
154 count += adat_width;
155 if (ports & HDSPE_CHAN_RAY_ADAT2)
156 count += adat_width;
157 if (ports & HDSPE_CHAN_RAY_ADAT3)
158 count += adat_width;
159 if (ports & HDSPE_CHAN_RAY_ADAT4)
160 count += adat_width;
161 }
162
163 return (count);
164 }
165
166 static unsigned int
hdspe_channel_offset(uint32_t subset,uint32_t ports,unsigned int adat_width)167 hdspe_channel_offset(uint32_t subset, uint32_t ports, unsigned int adat_width)
168 {
169 uint32_t preceding;
170
171 /* Make sure we have a subset of ports. */
172 subset &= ports;
173 /* Include all ports preceding the first one of the subset. */
174 preceding = ports & (~subset & (subset - 1));
175
176 if (preceding & HDSPE_CHAN_AIO_ALL)
177 preceding &= HDSPE_CHAN_AIO_ALL; /* Contiguous AIO slots. */
178 else if (preceding & HDSPE_CHAN_RAY_ALL)
179 preceding &= HDSPE_CHAN_RAY_ALL; /* Contiguous RayDAT slots. */
180
181 return (hdspe_channel_count(preceding, adat_width));
182 }
183
184 static unsigned int
hdspe_port_slot_offset(uint32_t port,unsigned int adat_width)185 hdspe_port_slot_offset(uint32_t port, unsigned int adat_width)
186 {
187 /* Exctract the first port (lowest bit) if set of ports. */
188 switch (hdspe_port_first(port)) {
189 /* AIO ports */
190 case HDSPE_CHAN_AIO_LINE:
191 return (0);
192 case HDSPE_CHAN_AIO_PHONE:
193 return (6);
194 case HDSPE_CHAN_AIO_AES:
195 return (8);
196 case HDSPE_CHAN_AIO_SPDIF:
197 return (10);
198 case HDSPE_CHAN_AIO_ADAT:
199 return (12);
200
201 /* RayDAT ports */
202 case HDSPE_CHAN_RAY_AES:
203 return (0);
204 case HDSPE_CHAN_RAY_SPDIF:
205 return (2);
206 case HDSPE_CHAN_RAY_ADAT1:
207 return (4);
208 case HDSPE_CHAN_RAY_ADAT2:
209 return (4 + adat_width);
210 case HDSPE_CHAN_RAY_ADAT3:
211 return (4 + 2 * adat_width);
212 case HDSPE_CHAN_RAY_ADAT4:
213 return (4 + 3 * adat_width);
214 default:
215 return (0);
216 }
217 }
218
219 static unsigned int
hdspe_port_slot_width(uint32_t ports,unsigned int adat_width)220 hdspe_port_slot_width(uint32_t ports, unsigned int adat_width)
221 {
222 uint32_t row;
223
224 /* Count number of contiguous slots from the first physical port. */
225 row = hdspe_port_first_row(ports);
226 return (hdspe_channel_count(row, adat_width));
227 }
228
229 static int
hdspe_hw_mixer(struct sc_chinfo * ch,unsigned int dst,unsigned int src,unsigned short data)230 hdspe_hw_mixer(struct sc_chinfo *ch, unsigned int dst,
231 unsigned int src, unsigned short data)
232 {
233 struct sc_pcminfo *scp;
234 struct sc_info *sc;
235 int offs;
236
237 scp = ch->parent;
238 sc = scp->sc;
239
240 offs = 0;
241 if (ch->dir == PCMDIR_PLAY)
242 offs = 64;
243
244 hdspe_write_4(sc, HDSPE_MIXER_BASE +
245 ((offs + src + 128 * dst) * sizeof(uint32_t)),
246 data & 0xFFFF);
247
248 return (0);
249 };
250
251 static int
hdspechan_setgain(struct sc_chinfo * ch)252 hdspechan_setgain(struct sc_chinfo *ch)
253 {
254 struct sc_info *sc;
255 uint32_t port, ports;
256 unsigned int slot, end_slot;
257 unsigned short volume;
258
259 sc = ch->parent->sc;
260
261 /* Iterate through all physical ports of the channel. */
262 ports = ch->ports;
263 port = hdspe_port_first(ports);
264 while (port != 0) {
265 /* Get slot range of the physical port. */
266 slot =
267 hdspe_port_slot_offset(port, hdspe_adat_width(sc->speed));
268 end_slot = slot +
269 hdspe_port_slot_width(port, hdspe_adat_width(sc->speed));
270
271 /* Treat first slot as left channel. */
272 volume = ch->lvol * HDSPE_MAX_GAIN / 100;
273 for (; slot < end_slot; slot++) {
274 hdspe_hw_mixer(ch, slot, slot, volume);
275 /* Subsequent slots all get the right channel volume. */
276 volume = ch->rvol * HDSPE_MAX_GAIN / 100;
277 }
278
279 ports &= ~port;
280 port = hdspe_port_first(ports);
281 }
282
283 return (0);
284 }
285
286 static int
hdspemixer_init(struct snd_mixer * m)287 hdspemixer_init(struct snd_mixer *m)
288 {
289 struct sc_pcminfo *scp;
290 struct sc_info *sc;
291 int mask;
292
293 scp = mix_getdevinfo(m);
294 sc = scp->sc;
295 if (sc == NULL)
296 return (-1);
297
298 mask = SOUND_MASK_PCM;
299
300 if (hdspe_channel_play_ports(scp->hc))
301 mask |= SOUND_MASK_VOLUME;
302
303 if (hdspe_channel_rec_ports(scp->hc))
304 mask |= SOUND_MASK_RECLEV;
305
306 snd_mtxlock(sc->lock);
307 pcm_setflags(scp->dev, pcm_getflags(scp->dev) | SD_F_SOFTPCMVOL);
308 mix_setdevs(m, mask);
309 snd_mtxunlock(sc->lock);
310
311 return (0);
312 }
313
314 static int
hdspemixer_set(struct snd_mixer * m,unsigned dev,unsigned left,unsigned right)315 hdspemixer_set(struct snd_mixer *m, unsigned dev,
316 unsigned left, unsigned right)
317 {
318 struct sc_pcminfo *scp;
319 struct sc_chinfo *ch;
320 int i;
321
322 scp = mix_getdevinfo(m);
323
324 #if 0
325 device_printf(scp->dev, "hdspemixer_set() %d %d\n",
326 left, right);
327 #endif
328
329 for (i = 0; i < scp->chnum; i++) {
330 ch = &scp->chan[i];
331 if ((dev == SOUND_MIXER_VOLUME && ch->dir == PCMDIR_PLAY) ||
332 (dev == SOUND_MIXER_RECLEV && ch->dir == PCMDIR_REC)) {
333 ch->lvol = left;
334 ch->rvol = right;
335 if (ch->run)
336 hdspechan_setgain(ch);
337 }
338 }
339
340 return (0);
341 }
342
343 static kobj_method_t hdspemixer_methods[] = {
344 KOBJMETHOD(mixer_init, hdspemixer_init),
345 KOBJMETHOD(mixer_set, hdspemixer_set),
346 KOBJMETHOD_END
347 };
348 MIXER_DECLARE(hdspemixer);
349
350 static void
hdspechan_enable(struct sc_chinfo * ch,int value)351 hdspechan_enable(struct sc_chinfo *ch, int value)
352 {
353 struct sc_pcminfo *scp;
354 struct sc_info *sc;
355 uint32_t row, ports;
356 int reg;
357 unsigned int slot, end_slot;
358
359 scp = ch->parent;
360 sc = scp->sc;
361
362 if (ch->dir == PCMDIR_PLAY)
363 reg = HDSPE_OUT_ENABLE_BASE;
364 else
365 reg = HDSPE_IN_ENABLE_BASE;
366
367 ch->run = value;
368
369 /* Iterate through rows of ports with contiguous slots. */
370 ports = ch->ports;
371 row = hdspe_port_first_row(ports);
372 while (row != 0) {
373 slot =
374 hdspe_port_slot_offset(row, hdspe_adat_width(sc->speed));
375 end_slot = slot +
376 hdspe_port_slot_width(row, hdspe_adat_width(sc->speed));
377
378 for (; slot < end_slot; slot++) {
379 hdspe_write_1(sc, reg + (4 * slot), value);
380 }
381
382 ports &= ~row;
383 row = hdspe_port_first_row(ports);
384 }
385 }
386
387 static int
hdspe_running(struct sc_info * sc)388 hdspe_running(struct sc_info *sc)
389 {
390 struct sc_pcminfo *scp;
391 struct sc_chinfo *ch;
392 device_t *devlist;
393 int devcount;
394 int i, j;
395 int err;
396
397 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
398 goto bad;
399
400 for (i = 0; i < devcount; i++) {
401 scp = device_get_ivars(devlist[i]);
402 for (j = 0; j < scp->chnum; j++) {
403 ch = &scp->chan[j];
404 if (ch->run)
405 goto bad;
406 }
407 }
408
409 free(devlist, M_TEMP);
410
411 return (0);
412 bad:
413
414 #if 0
415 device_printf(sc->dev, "hdspe is running\n");
416 #endif
417
418 free(devlist, M_TEMP);
419
420 return (1);
421 }
422
423 static void
hdspe_start_audio(struct sc_info * sc)424 hdspe_start_audio(struct sc_info *sc)
425 {
426
427 sc->ctrl_register |= (HDSPE_AUDIO_INT_ENABLE | HDSPE_ENABLE);
428 hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
429 }
430
431 static void
hdspe_stop_audio(struct sc_info * sc)432 hdspe_stop_audio(struct sc_info *sc)
433 {
434
435 if (hdspe_running(sc) == 1)
436 return;
437
438 sc->ctrl_register &= ~(HDSPE_AUDIO_INT_ENABLE | HDSPE_ENABLE);
439 hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
440 }
441
442 static void
buffer_mux_write(uint32_t * dma,uint32_t * pcm,unsigned int pos,unsigned int samples,unsigned int slots,unsigned int channels)443 buffer_mux_write(uint32_t *dma, uint32_t *pcm, unsigned int pos,
444 unsigned int samples, unsigned int slots, unsigned int channels)
445 {
446 int slot;
447
448 for (; samples > 0; samples--) {
449 for (slot = 0; slot < slots; slot++) {
450 dma[slot * HDSPE_CHANBUF_SAMPLES + pos] =
451 pcm[pos * channels + slot];
452 }
453 pos = (pos + 1) % HDSPE_CHANBUF_SAMPLES;
454 }
455 }
456
457 static void
buffer_mux_port(uint32_t * dma,uint32_t * pcm,uint32_t subset,uint32_t ports,unsigned int pos,unsigned int samples,unsigned int adat_width,unsigned int pcm_width)458 buffer_mux_port(uint32_t *dma, uint32_t *pcm, uint32_t subset, uint32_t ports,
459 unsigned int pos, unsigned int samples, unsigned int adat_width,
460 unsigned int pcm_width)
461 {
462 unsigned int slot_offset, slots;
463 unsigned int channels, chan_pos;
464
465 /* Translate DMA slot offset to DMA buffer offset. */
466 slot_offset = hdspe_port_slot_offset(subset, adat_width);
467 dma += slot_offset * HDSPE_CHANBUF_SAMPLES;
468
469 /* Channel position of the port subset and total number of channels. */
470 chan_pos = hdspe_channel_offset(subset, ports, pcm_width);
471 pcm += chan_pos;
472 channels = hdspe_channel_count(ports, pcm_width);
473
474 /* Only copy as much as supported by both hardware and pcm channel. */
475 slots = hdspe_port_slot_width(subset, MIN(adat_width, pcm_width));
476
477 /* Let the compiler inline and loop unroll common cases. */
478 if (slots == 2)
479 buffer_mux_write(dma, pcm, pos, samples, 2, channels);
480 else if (slots == 4)
481 buffer_mux_write(dma, pcm, pos, samples, 4, channels);
482 else if (slots == 8)
483 buffer_mux_write(dma, pcm, pos, samples, 8, channels);
484 else
485 buffer_mux_write(dma, pcm, pos, samples, slots, channels);
486 }
487
488 static void
buffer_demux_read(uint32_t * dma,uint32_t * pcm,unsigned int pos,unsigned int samples,unsigned int slots,unsigned int channels)489 buffer_demux_read(uint32_t *dma, uint32_t *pcm, unsigned int pos,
490 unsigned int samples, unsigned int slots, unsigned int channels)
491 {
492 int slot;
493
494 for (; samples > 0; samples--) {
495 for (slot = 0; slot < slots; slot++) {
496 pcm[pos * channels + slot] =
497 dma[slot * HDSPE_CHANBUF_SAMPLES + pos];
498 }
499 pos = (pos + 1) % HDSPE_CHANBUF_SAMPLES;
500 }
501 }
502
503 static void
buffer_demux_port(uint32_t * dma,uint32_t * pcm,uint32_t subset,uint32_t ports,unsigned int pos,unsigned int samples,unsigned int adat_width,unsigned int pcm_width)504 buffer_demux_port(uint32_t *dma, uint32_t *pcm, uint32_t subset, uint32_t ports,
505 unsigned int pos, unsigned int samples, unsigned int adat_width,
506 unsigned int pcm_width)
507 {
508 unsigned int slot_offset, slots;
509 unsigned int channels, chan_pos;
510
511 /* Translate port slot offset to DMA buffer offset. */
512 slot_offset = hdspe_port_slot_offset(subset, adat_width);
513 dma += slot_offset * HDSPE_CHANBUF_SAMPLES;
514
515 /* Channel position of the port subset and total number of channels. */
516 chan_pos = hdspe_channel_offset(subset, ports, pcm_width);
517 pcm += chan_pos;
518 channels = hdspe_channel_count(ports, pcm_width);
519
520 /* Only copy as much as supported by both hardware and pcm channel. */
521 slots = hdspe_port_slot_width(subset, MIN(adat_width, pcm_width));
522
523 /* Let the compiler inline and loop unroll common cases. */
524 if (slots == 2)
525 buffer_demux_read(dma, pcm, pos, samples, 2, channels);
526 else if (slots == 4)
527 buffer_demux_read(dma, pcm, pos, samples, 4, channels);
528 else if (slots == 8)
529 buffer_demux_read(dma, pcm, pos, samples, 8, channels);
530 else
531 buffer_demux_read(dma, pcm, pos, samples, slots, channels);
532 }
533
534
535 /* Copy data between DMA and PCM buffers. */
536 static void
buffer_copy(struct sc_chinfo * ch)537 buffer_copy(struct sc_chinfo *ch)
538 {
539 struct sc_pcminfo *scp;
540 struct sc_info *sc;
541 uint32_t row, ports;
542 uint32_t dma_pos;
543 unsigned int pos, length, offset;
544 unsigned int n;
545 unsigned int adat_width, pcm_width;
546
547 scp = ch->parent;
548 sc = scp->sc;
549
550 n = AFMT_CHANNEL(ch->format); /* n channels */
551
552 /* Let pcm formats differ from current hardware ADAT width. */
553 adat_width = hdspe_adat_width(sc->speed);
554 if (n == hdspe_channel_count(ch->ports, 2))
555 pcm_width = 2;
556 else if (n == hdspe_channel_count(ch->ports, 4))
557 pcm_width = 4;
558 else
559 pcm_width = 8;
560
561 /* Derive buffer position and length to be copied. */
562 if (ch->dir == PCMDIR_PLAY) {
563 /* Position per channel is n times smaller than PCM. */
564 pos = sndbuf_getreadyptr(ch->buffer) / n;
565 length = sndbuf_getready(ch->buffer) / n;
566 /* Copy no more than 2 periods in advance. */
567 if (length > (sc->period * 4 * 2))
568 length = (sc->period * 4 * 2);
569 /* Skip what was already copied last time. */
570 offset = (ch->position + HDSPE_CHANBUF_SIZE) - pos;
571 offset %= HDSPE_CHANBUF_SIZE;
572 if (offset <= length) {
573 pos = (pos + offset) % HDSPE_CHANBUF_SIZE;
574 length -= offset;
575 }
576 } else {
577 /* Position per channel is n times smaller than PCM. */
578 pos = sndbuf_getfreeptr(ch->buffer) / n;
579 /* Get DMA buffer write position. */
580 dma_pos = hdspe_read_2(sc, HDSPE_STATUS_REG);
581 dma_pos &= HDSPE_BUF_POSITION_MASK;
582 /* Copy what is newly available. */
583 length = (dma_pos + HDSPE_CHANBUF_SIZE) - pos;
584 length %= HDSPE_CHANBUF_SIZE;
585 }
586
587 /* Position and length in samples (4 bytes). */
588 pos /= 4;
589 length /= 4;
590
591 /* Iterate through rows of ports with contiguous slots. */
592 ports = ch->ports;
593 if (pcm_width == adat_width)
594 row = hdspe_port_first_row(ports);
595 else
596 row = hdspe_port_first(ports);
597
598 while (row != 0) {
599 if (ch->dir == PCMDIR_PLAY)
600 buffer_mux_port(sc->pbuf, ch->data, row, ch->ports, pos,
601 length, adat_width, pcm_width);
602 else
603 buffer_demux_port(sc->rbuf, ch->data, row, ch->ports,
604 pos, length, adat_width, pcm_width);
605
606 ports &= ~row;
607 if (pcm_width == adat_width)
608 row = hdspe_port_first_row(ports);
609 else
610 row = hdspe_port_first(ports);
611 }
612
613 ch->position = ((pos + length) * 4) % HDSPE_CHANBUF_SIZE;
614 }
615
616 static int
clean(struct sc_chinfo * ch)617 clean(struct sc_chinfo *ch)
618 {
619 struct sc_pcminfo *scp;
620 struct sc_info *sc;
621 uint32_t *buf;
622 uint32_t row, ports;
623 unsigned int offset, slots;
624
625 scp = ch->parent;
626 sc = scp->sc;
627 buf = sc->rbuf;
628
629 if (ch->dir == PCMDIR_PLAY)
630 buf = sc->pbuf;
631
632 /* Iterate through rows of ports with contiguous slots. */
633 ports = ch->ports;
634 row = hdspe_port_first_row(ports);
635 while (row != 0) {
636 offset = hdspe_port_slot_offset(row,
637 hdspe_adat_width(sc->speed));
638 slots = hdspe_port_slot_width(row, hdspe_adat_width(sc->speed));
639
640 bzero(buf + offset * HDSPE_CHANBUF_SAMPLES,
641 slots * HDSPE_CHANBUF_SIZE);
642
643 ports &= ~row;
644 row = hdspe_port_first_row(ports);
645 }
646
647 ch->position = 0;
648
649 return (0);
650 }
651
652 /* Channel interface. */
653 static int
hdspechan_free(kobj_t obj,void * data)654 hdspechan_free(kobj_t obj, void *data)
655 {
656 struct sc_pcminfo *scp;
657 struct sc_chinfo *ch;
658 struct sc_info *sc;
659
660 ch = data;
661 scp = ch->parent;
662 sc = scp->sc;
663
664 #if 0
665 device_printf(scp->dev, "hdspechan_free()\n");
666 #endif
667
668 snd_mtxlock(sc->lock);
669 if (ch->data != NULL) {
670 free(ch->data, M_HDSPE);
671 ch->data = NULL;
672 }
673 if (ch->caps != NULL) {
674 free(ch->caps, M_HDSPE);
675 ch->caps = NULL;
676 }
677 snd_mtxunlock(sc->lock);
678
679 return (0);
680 }
681
682 static void *
hdspechan_init(kobj_t obj,void * devinfo,struct snd_dbuf * b,struct pcm_channel * c,int dir)683 hdspechan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
684 struct pcm_channel *c, int dir)
685 {
686 struct sc_pcminfo *scp;
687 struct sc_chinfo *ch;
688 struct sc_info *sc;
689 int num;
690
691 scp = devinfo;
692 sc = scp->sc;
693
694 snd_mtxlock(sc->lock);
695 num = scp->chnum;
696
697 ch = &scp->chan[num];
698
699 if (dir == PCMDIR_PLAY)
700 ch->ports = hdspe_channel_play_ports(scp->hc);
701 else
702 ch->ports = hdspe_channel_rec_ports(scp->hc);
703
704 ch->run = 0;
705 ch->lvol = 0;
706 ch->rvol = 0;
707
708 /* Support all possible ADAT widths as channel formats. */
709 ch->cap_fmts[0] =
710 SND_FORMAT(AFMT_S32_LE, hdspe_channel_count(ch->ports, 2), 0);
711 ch->cap_fmts[1] =
712 SND_FORMAT(AFMT_S32_LE, hdspe_channel_count(ch->ports, 4), 0);
713 ch->cap_fmts[2] =
714 SND_FORMAT(AFMT_S32_LE, hdspe_channel_count(ch->ports, 8), 0);
715 ch->cap_fmts[3] = 0;
716 ch->caps = malloc(sizeof(struct pcmchan_caps), M_HDSPE, M_NOWAIT);
717 *(ch->caps) = (struct pcmchan_caps) {32000, 192000, ch->cap_fmts, 0};
718
719 /* Allocate maximum buffer size. */
720 ch->size = HDSPE_CHANBUF_SIZE * hdspe_channel_count(ch->ports, 8);
721 ch->data = malloc(ch->size, M_HDSPE, M_NOWAIT);
722 ch->position = 0;
723
724 ch->buffer = b;
725 ch->channel = c;
726 ch->parent = scp;
727
728 ch->dir = dir;
729
730 snd_mtxunlock(sc->lock);
731
732 if (sndbuf_setup(ch->buffer, ch->data, ch->size) != 0) {
733 device_printf(scp->dev, "Can't setup sndbuf.\n");
734 hdspechan_free(obj, ch);
735 return (NULL);
736 }
737
738 return (ch);
739 }
740
741 static int
hdspechan_trigger(kobj_t obj,void * data,int go)742 hdspechan_trigger(kobj_t obj, void *data, int go)
743 {
744 struct sc_pcminfo *scp;
745 struct sc_chinfo *ch;
746 struct sc_info *sc;
747
748 ch = data;
749 scp = ch->parent;
750 sc = scp->sc;
751
752 snd_mtxlock(sc->lock);
753 switch (go) {
754 case PCMTRIG_START:
755 #if 0
756 device_printf(scp->dev, "hdspechan_trigger(): start\n");
757 #endif
758 hdspechan_enable(ch, 1);
759 hdspechan_setgain(ch);
760 hdspe_start_audio(sc);
761 break;
762
763 case PCMTRIG_STOP:
764 case PCMTRIG_ABORT:
765 #if 0
766 device_printf(scp->dev, "hdspechan_trigger(): stop or abort\n");
767 #endif
768 clean(ch);
769 hdspechan_enable(ch, 0);
770 hdspe_stop_audio(sc);
771 break;
772
773 case PCMTRIG_EMLDMAWR:
774 case PCMTRIG_EMLDMARD:
775 if(ch->run)
776 buffer_copy(ch);
777 break;
778 }
779
780 snd_mtxunlock(sc->lock);
781
782 return (0);
783 }
784
785 static uint32_t
hdspechan_getptr(kobj_t obj,void * data)786 hdspechan_getptr(kobj_t obj, void *data)
787 {
788 struct sc_pcminfo *scp;
789 struct sc_chinfo *ch;
790 struct sc_info *sc;
791 uint32_t ret, pos;
792
793 ch = data;
794 scp = ch->parent;
795 sc = scp->sc;
796
797 snd_mtxlock(sc->lock);
798 ret = hdspe_read_2(sc, HDSPE_STATUS_REG);
799 snd_mtxunlock(sc->lock);
800
801 pos = ret & HDSPE_BUF_POSITION_MASK;
802 pos *= AFMT_CHANNEL(ch->format); /* Hardbuf with multiple channels. */
803
804 return (pos);
805 }
806
807 static int
hdspechan_setformat(kobj_t obj,void * data,uint32_t format)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
hdspechan_setspeed(kobj_t obj,void * data,uint32_t speed)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
hdspechan_setblocksize(kobj_t obj,void * data,uint32_t blocksize)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 *
hdspechan_getcaps(kobj_t obj,void * data)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
hdspe_pcm_probe(device_t dev)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
hdspe_pcm_intr(struct sc_pcminfo * scp)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
hdspe_pcm_attach(device_t dev)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
hdspe_pcm_detach(device_t dev)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