xref: /freebsd/sys/dev/sound/pci/hdsp-pcm.c (revision d37eb51047221dc3322b34db1038ff3aa533883f)
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 HDSP driver for FreeBSD (pcm-part).
32  * Supported cards: HDSP 9632, HDSP 9652.
33  */
34 
35 #include <sys/libkern.h>
36 
37 #include <dev/sound/pcm/sound.h>
38 #include <dev/sound/pci/hdsp.h>
39 
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
42 
43 #include <mixer_if.h>
44 
45 #define HDSP_MATRIX_MAX	8
46 
47 struct hdsp_latency {
48 	uint32_t n;
49 	uint32_t period;
50 	float ms;
51 };
52 
53 static struct hdsp_latency latency_map[] = {
54 	{ 7,   32, 0.7 },
55 	{ 0,   64, 1.5 },
56 	{ 1,  128,   3 },
57 	{ 2,  256,   6 },
58 	{ 3,  512,  12 },
59 	{ 4, 1024,  23 },
60 	{ 5, 2048,  46 },
61 	{ 6, 4096,  93 },
62 
63 	{ 0,    0,   0 },
64 };
65 
66 struct hdsp_rate {
67 	uint32_t speed;
68 	uint32_t reg;
69 };
70 
71 static struct hdsp_rate rate_map[] = {
72 	{  32000, (HDSP_FREQ_32000) },
73 	{  44100, (HDSP_FREQ_44100) },
74 	{  48000, (HDSP_FREQ_48000) },
75 	{  64000, (HDSP_FREQ_32000 | HDSP_FREQ_DOUBLE) },
76 	{  88200, (HDSP_FREQ_44100 | HDSP_FREQ_DOUBLE) },
77 	{  96000, (HDSP_FREQ_48000 | HDSP_FREQ_DOUBLE) },
78 	{ 128000, (HDSP_FREQ_32000 | HDSP_FREQ_QUAD)   },
79 	{ 176400, (HDSP_FREQ_44100 | HDSP_FREQ_QUAD)   },
80 	{ 192000, (HDSP_FREQ_48000 | HDSP_FREQ_QUAD)   },
81 
82 	{ 0, 0 },
83 };
84 
85 static uint32_t
86 hdsp_adat_slot_map(uint32_t speed)
87 {
88 	/* ADAT slot bitmap depends on sample rate. */
89 	if (speed <= 48000)
90 		return (0x000000ff); /* 8 channels single speed. */
91 	else if (speed <= 96000)
92 		return (0x000000aa); /* 4 channels (1,3,5,7) double speed. */
93 	else
94 		return (0x00000000); /* ADAT disabled at quad speed. */
95 }
96 
97 static uint32_t
98 hdsp_port_slot_map(uint32_t ports, uint32_t speed)
99 {
100 	uint32_t slot_map = 0;
101 
102 	if (ports & HDSP_CHAN_9632_ALL) {
103 		/* Map HDSP 9632 ports to slot bitmap. */
104 		if (ports & HDSP_CHAN_9632_ADAT)
105 			slot_map |= (hdsp_adat_slot_map(speed) << 0);
106 		if (ports & HDSP_CHAN_9632_SPDIF)
107 			slot_map |= (0x03 << 8);  /* 2 channels SPDIF. */
108 		if (ports & HDSP_CHAN_9632_LINE)
109 			slot_map |= (0x03 << 10); /* 2 channels line. */
110 		if (ports & HDSP_CHAN_9632_EXT_BOARD)
111 			slot_map |= (0x0f << 12); /* 4 channels extension. */
112 	} else if ((ports & HDSP_CHAN_9652_ALL) && (speed <= 96000)) {
113 		/* Map HDSP 9652 ports to slot bitmap, no quad speed. */
114 		if (ports & HDSP_CHAN_9652_ADAT1)
115 			slot_map |= (hdsp_adat_slot_map(speed) << 0);
116 		if (ports & HDSP_CHAN_9652_ADAT2)
117 			slot_map |= (hdsp_adat_slot_map(speed) << 8);
118 		if (ports & HDSP_CHAN_9652_ADAT3)
119 			slot_map |= (hdsp_adat_slot_map(speed) << 16);
120 		if (ports & HDSP_CHAN_9652_SPDIF)
121 			slot_map |= (0x03 << 24); /* 2 channels SPDIF. */
122 	}
123 
124 	return (slot_map);
125 }
126 
127 static uint32_t
128 hdsp_slot_first(uint32_t slots)
129 {
130 	return (slots & (~(slots - 1)));	/* Extract first bit set. */
131 }
132 
133 static uint32_t
134 hdsp_slot_first_row(uint32_t slots)
135 {
136 	uint32_t ends;
137 
138 	/* Ends of slot rows are followed by a slot which is not in the set. */
139 	ends = slots & (~(slots >> 1));
140 	/* First row of contiguous slots ends in the first row end. */
141 	return (slots & (ends ^ (ends - 1)));
142 }
143 
144 static uint32_t
145 hdsp_slot_first_n(uint32_t slots, unsigned int n)
146 {
147 	/* Clear all but the first n slots. */
148 	for (uint32_t slot = 1; slot != 0; slot <<= 1) {
149 		if ((slots & slot) && n > 0)
150 			--n;
151 		else
152 			slots &= ~slot;
153 	}
154 	return (slots);
155 }
156 
157 static unsigned int
158 hdsp_slot_count(uint32_t slots)
159 {
160 	return (bitcount32(slots));
161 }
162 
163 static unsigned int
164 hdsp_slot_offset(uint32_t slots)
165 {
166 	return (hdsp_slot_count(hdsp_slot_first(slots) - 1));
167 }
168 
169 static unsigned int
170 hdsp_slot_channel_offset(uint32_t subset, uint32_t slots)
171 {
172 	uint32_t preceding;
173 
174 	/* Make sure we have a subset of slots. */
175 	subset &= slots;
176 	/* Include all slots preceding the first one of the subset. */
177 	preceding = slots & (hdsp_slot_first(subset) - 1);
178 
179 	return (hdsp_slot_count(preceding));
180 }
181 
182 static uint32_t
183 hdsp_port_first(uint32_t ports)
184 {
185 	return (ports & (~(ports - 1)));	/* Extract first bit set. */
186 }
187 
188 static unsigned int
189 hdsp_port_slot_count(uint32_t ports, uint32_t speed)
190 {
191 	return (hdsp_slot_count(hdsp_port_slot_map(ports, speed)));
192 }
193 
194 static unsigned int
195 hdsp_port_slot_count_max(uint32_t ports)
196 {
197 	return (hdsp_slot_count(hdsp_port_slot_map(ports, 48000)));
198 }
199 
200 static uint32_t
201 hdsp_channel_play_ports(struct hdsp_channel *hc)
202 {
203 	return (hc->ports & (HDSP_CHAN_9632_ALL | HDSP_CHAN_9652_ALL));
204 }
205 
206 static uint32_t
207 hdsp_channel_rec_ports(struct hdsp_channel *hc)
208 {
209 	return (hc->ports & (HDSP_CHAN_9632_ALL | HDSP_CHAN_9652_ALL));
210 }
211 
212 static int
213 hdsp_hw_mixer(struct sc_chinfo *ch, unsigned int dst,
214     unsigned int src, unsigned short data)
215 {
216 	struct sc_pcminfo *scp;
217 	struct sc_info *sc;
218 	uint32_t value;
219 	int offset;
220 
221 	scp = ch->parent;
222 	sc = scp->sc;
223 
224 	offset = 0;
225 	value = (HDSP_MIN_GAIN << 16) | (uint16_t) data;
226 
227 	if (ch->dir != PCMDIR_PLAY)
228 		return (0);
229 
230 	switch (sc->type) {
231 	case HDSP_9632:
232 		/* Mixer is 2 rows of sources (inputs, playback) per output. */
233 		offset = dst * (2 * HDSP_MIX_SLOTS_9632);
234 		/* Source index in the second row (playback). */
235 		offset += HDSP_MIX_SLOTS_9632 + src;
236 		break;
237 	case HDSP_9652:
238 		/* Mixer is 2 rows of sources (inputs, playback) per output. */
239 		offset = dst * (2 * HDSP_MIX_SLOTS_9652);
240 		/* Source index in the second row (playback). */
241 		offset += HDSP_MIX_SLOTS_9652 + src;
242 		break;
243 	default:
244 		return (0);
245 	}
246 
247 	/*
248 	 * We have to write mixer matrix values in pairs, with the second
249 	 * (odd) value in the upper 16 bits of the 32 bit value.
250 	 * Make value offset even and shift value accordingly.
251 	 * Assume the paired value to be silenced, since we only set gain
252 	 * on the diagonal where src and dst are the same.
253 	 */
254 	if (offset % 2) {
255 		offset -= 1;
256 		value = (value << 16) | HDSP_MIN_GAIN;
257 	}
258 
259 	hdsp_write_4(sc, HDSP_MIXER_BASE + offset * sizeof(uint16_t), value);
260 
261 	return (0);
262 };
263 
264 static int
265 hdspchan_setgain(struct sc_chinfo *ch)
266 {
267 	uint32_t port, ports;
268 	uint32_t slot, slots;
269 	unsigned int offset;
270 	unsigned short volume;
271 
272 	/* Iterate through all physical ports of the channel. */
273 	ports = ch->ports;
274 	port = hdsp_port_first(ports);
275 	while (port != 0) {
276 		/*
277 		 * Get slot map from physical port.
278 		 * Unlike DMA buffers, the hardware mixer's channel mapping
279 		 * does not change with double or quad speed sample rates.
280 		 */
281 		slots = hdsp_port_slot_map(port, 48000);
282 		slot = hdsp_slot_first(slots);
283 
284 		/* Treat first slot as left channel. */
285 		volume = ch->lvol * HDSP_MAX_GAIN / 100;
286 		while (slot != 0) {
287 			offset = hdsp_slot_offset(slot);
288 			hdsp_hw_mixer(ch, offset, offset, volume);
289 
290 			slots &= ~slot;
291 			slot = hdsp_slot_first(slots);
292 
293 			/* Subsequent slots all get the right channel volume. */
294 			volume = ch->rvol * HDSP_MAX_GAIN / 100;
295 		}
296 
297 		ports &= ~port;
298 		port = hdsp_port_first(ports);
299 	}
300 
301 	return (0);
302 }
303 
304 static int
305 hdspmixer_init(struct snd_mixer *m)
306 {
307 	struct sc_pcminfo *scp;
308 	struct sc_info *sc;
309 	int mask;
310 
311 	scp = mix_getdevinfo(m);
312 	sc = scp->sc;
313 	if (sc == NULL)
314 		return (-1);
315 
316 	mask = SOUND_MASK_PCM;
317 
318 	if (hdsp_channel_play_ports(scp->hc))
319 		mask |= SOUND_MASK_VOLUME;
320 
321 	if (hdsp_channel_rec_ports(scp->hc))
322 		mask |= SOUND_MASK_RECLEV;
323 
324 	snd_mtxlock(sc->lock);
325 	pcm_setflags(scp->dev, pcm_getflags(scp->dev) | SD_F_SOFTPCMVOL);
326 	mix_setdevs(m, mask);
327 	snd_mtxunlock(sc->lock);
328 
329 	return (0);
330 }
331 
332 static int
333 hdspmixer_set(struct snd_mixer *m, unsigned dev,
334     unsigned left, unsigned right)
335 {
336 	struct sc_pcminfo *scp;
337 	struct sc_chinfo *ch;
338 	int i;
339 
340 	scp = mix_getdevinfo(m);
341 
342 #if 0
343 	device_printf(scp->dev, "hdspmixer_set() %d %d\n",
344 	    left, right);
345 #endif
346 
347 	for (i = 0; i < scp->chnum; i++) {
348 		ch = &scp->chan[i];
349 		if ((dev == SOUND_MIXER_VOLUME && ch->dir == PCMDIR_PLAY) ||
350 		    (dev == SOUND_MIXER_RECLEV && ch->dir == PCMDIR_REC)) {
351 			ch->lvol = left;
352 			ch->rvol = right;
353 			if (ch->run)
354 				hdspchan_setgain(ch);
355 		}
356 	}
357 
358 	return (0);
359 }
360 
361 static kobj_method_t hdspmixer_methods[] = {
362 	KOBJMETHOD(mixer_init,      hdspmixer_init),
363 	KOBJMETHOD(mixer_set,       hdspmixer_set),
364 	KOBJMETHOD_END
365 };
366 MIXER_DECLARE(hdspmixer);
367 
368 static void
369 hdspchan_enable(struct sc_chinfo *ch, int value)
370 {
371 	struct sc_pcminfo *scp;
372 	struct sc_info *sc;
373 	uint32_t slot, slots;
374 	unsigned int offset;
375 	int reg;
376 
377 	scp = ch->parent;
378 	sc = scp->sc;
379 
380 	if (ch->dir == PCMDIR_PLAY)
381 		reg = HDSP_OUT_ENABLE_BASE;
382 	else
383 		reg = HDSP_IN_ENABLE_BASE;
384 
385 	ch->run = value;
386 
387 	/* Iterate through all slots of the channel's physical ports. */
388 	slots = hdsp_port_slot_map(ch->ports, sc->speed);
389 	slot = hdsp_slot_first(slots);
390 	while (slot != 0) {
391 		/* Set register to enable or disable slot. */
392 		offset = hdsp_slot_offset(slot);
393 		hdsp_write_1(sc, reg + (4 * offset), value);
394 
395 		slots &= ~slot;
396 		slot = hdsp_slot_first(slots);
397 	}
398 }
399 
400 static int
401 hdsp_running(struct sc_info *sc)
402 {
403 	struct sc_pcminfo *scp;
404 	struct sc_chinfo *ch;
405 	device_t *devlist;
406 	int devcount;
407 	int i, j;
408 	int running;
409 
410 	running = 0;
411 
412 	devlist = NULL;
413 	devcount = 0;
414 
415 	if (device_get_children(sc->dev, &devlist, &devcount) != 0)
416 		running = 1;	/* On error, avoid channel config changes. */
417 
418 	for (i = 0; running == 0 && i < devcount; i++) {
419 		scp = device_get_ivars(devlist[i]);
420 		for (j = 0; j < scp->chnum; j++) {
421 			ch = &scp->chan[j];
422 			if (ch->run) {
423 				running = 1;
424 				break;
425 			}
426 		}
427 	}
428 
429 #if 0
430 	if (running == 1)
431 		device_printf(sc->dev, "hdsp is running\n");
432 #endif
433 
434 	free(devlist, M_TEMP);
435 
436 	return (running);
437 }
438 
439 static void
440 hdsp_start_audio(struct sc_info *sc)
441 {
442 
443 	sc->ctrl_register |= (HDSP_AUDIO_INT_ENABLE | HDSP_ENABLE);
444 	hdsp_write_4(sc, HDSP_CONTROL_REG, sc->ctrl_register);
445 }
446 
447 static void
448 hdsp_stop_audio(struct sc_info *sc)
449 {
450 
451 	if (hdsp_running(sc) == 1)
452 		return;
453 
454 	sc->ctrl_register &= ~(HDSP_AUDIO_INT_ENABLE | HDSP_ENABLE);
455 	hdsp_write_4(sc, HDSP_CONTROL_REG, sc->ctrl_register);
456 }
457 
458 static void
459 buffer_mux_write(uint32_t *dma, uint32_t *pcm, unsigned int pos,
460     unsigned int pos_end, unsigned int width, unsigned int channels)
461 {
462 	unsigned int slot;
463 
464 	for (; pos < pos_end; ++pos) {
465 		for (slot = 0; slot < width; slot++) {
466 			dma[slot * HDSP_CHANBUF_SAMPLES + pos] =
467 			    pcm[pos * channels + slot];
468 		}
469 	}
470 }
471 
472 static void
473 buffer_mux_port(uint32_t *dma, uint32_t *pcm, uint32_t subset, uint32_t slots,
474     unsigned int pos, unsigned int samples, unsigned int channels)
475 {
476 	unsigned int slot_offset, width;
477 	unsigned int chan_pos;
478 
479 	/* Translate DMA slot offset to DMA buffer offset. */
480 	slot_offset = hdsp_slot_offset(subset);
481 	dma += slot_offset * HDSP_CHANBUF_SAMPLES;
482 
483 	/* Channel position of the slot subset. */
484 	chan_pos = hdsp_slot_channel_offset(subset, slots);
485 	pcm += chan_pos;
486 
487 	/* Only copy channels supported by both hardware and pcm format. */
488 	width = hdsp_slot_count(subset);
489 
490 	/* Let the compiler inline and loop unroll common cases. */
491 	if (width == 1)
492 		buffer_mux_write(dma, pcm, pos, pos + samples, 1, channels);
493 	else if (width == 2)
494 		buffer_mux_write(dma, pcm, pos, pos + samples, 2, channels);
495 	else if (width == 4)
496 		buffer_mux_write(dma, pcm, pos, pos + samples, 4, channels);
497 	else if (width == 8)
498 		buffer_mux_write(dma, pcm, pos, pos + samples, 8, channels);
499 	else
500 		buffer_mux_write(dma, pcm, pos, pos + samples, width, channels);
501 }
502 
503 static void
504 buffer_demux_read(uint32_t *dma, uint32_t *pcm, unsigned int pos,
505     unsigned int pos_end, unsigned int width, unsigned int channels)
506 {
507 	unsigned int slot;
508 
509 	for (; pos < pos_end; ++pos) {
510 		for (slot = 0; slot < width; slot++) {
511 			pcm[pos * channels + slot] =
512 			    dma[slot * HDSP_CHANBUF_SAMPLES + pos];
513 		}
514 	}
515 }
516 
517 static void
518 buffer_demux_port(uint32_t *dma, uint32_t *pcm, uint32_t subset, uint32_t slots,
519     unsigned int pos, unsigned int samples, unsigned int channels)
520 {
521 	unsigned int slot_offset, width;
522 	unsigned int chan_pos;
523 
524 	/* Translate DMA slot offset to DMA buffer offset. */
525 	slot_offset = hdsp_slot_offset(subset);
526 	dma += slot_offset * HDSP_CHANBUF_SAMPLES;
527 
528 	/* Channel position of the slot subset. */
529 	chan_pos = hdsp_slot_channel_offset(subset, slots);
530 	pcm += chan_pos;
531 
532 	/* Only copy channels supported by both hardware and pcm format. */
533 	width = hdsp_slot_count(subset);
534 
535 	/* Let the compiler inline and loop unroll common cases. */
536 	if (width == 1)
537 		buffer_demux_read(dma, pcm, pos, pos + samples, 1, channels);
538 	else if (width == 2)
539 		buffer_demux_read(dma, pcm, pos, pos + samples, 2, channels);
540 	else if (width == 4)
541 		buffer_demux_read(dma, pcm, pos, pos + samples, 4, channels);
542 	else if (width == 8)
543 		buffer_demux_read(dma, pcm, pos, pos + samples, 8, channels);
544 	else
545 		buffer_demux_read(dma, pcm, pos, pos + samples, width, channels);
546 }
547 
548 
549 /* Copy data between DMA and PCM buffers. */
550 static void
551 buffer_copy(struct sc_chinfo *ch)
552 {
553 	struct sc_pcminfo *scp;
554 	struct sc_info *sc;
555 	uint32_t row, slots;
556 	uint32_t dma_pos;
557 	unsigned int pos, length, remainder, offset, buffer_size;
558 	unsigned int channels;
559 
560 	scp = ch->parent;
561 	sc = scp->sc;
562 
563 	channels = AFMT_CHANNEL(ch->format); /* Number of PCM channels. */
564 
565 	/* HDSP cards read / write a double buffer, twice the latency period. */
566 	buffer_size = 2 * sc->period * sizeof(uint32_t);
567 
568 	/* Derive buffer position and length to be copied. */
569 	if (ch->dir == PCMDIR_PLAY) {
570 		/* Buffer position scaled down to a single channel. */
571 		pos = sndbuf_getreadyptr(ch->buffer) / channels;
572 		length = sndbuf_getready(ch->buffer) / channels;
573 		/* Copy no more than 2 periods in advance. */
574 		if (length > buffer_size)
575 			length = buffer_size;
576 		/* Skip what was already copied last time. */
577 		offset = (ch->position + buffer_size) - pos;
578 		offset %= buffer_size;
579 		if (offset <= length) {
580 			pos = (pos + offset) % buffer_size;
581 			length -= offset;
582 		}
583 	} else {
584 		/* Buffer position scaled down to a single channel. */
585 		pos = sndbuf_getfreeptr(ch->buffer) / channels;
586 		/* Get DMA buffer write position. */
587 		dma_pos = hdsp_read_2(sc, HDSP_STATUS_REG);
588 		dma_pos &= HDSP_BUF_POSITION_MASK;
589 		dma_pos %= buffer_size;
590 		/* Copy what is newly available. */
591 		length = (dma_pos + buffer_size) - pos;
592 		length %= buffer_size;
593 	}
594 
595 	/* Position and length in samples (4 bytes). */
596 	pos /= 4;
597 	length /= 4;
598 	buffer_size /= sizeof(uint32_t);
599 
600 	/* Split copy length to wrap around at buffer end. */
601 	remainder = 0;
602 	if (pos + length > buffer_size)
603 		remainder = (pos + length) - buffer_size;
604 
605 	/* Iterate through rows of contiguous slots. */
606 	slots = hdsp_port_slot_map(ch->ports, sc->speed);
607 	slots = hdsp_slot_first_n(slots, channels);
608 	row = hdsp_slot_first_row(slots);
609 
610 	while (row != 0) {
611 		if (ch->dir == PCMDIR_PLAY) {
612 			buffer_mux_port(sc->pbuf, ch->data, row, slots, pos,
613 			    length - remainder, channels);
614 			buffer_mux_port(sc->pbuf, ch->data, row, slots, 0,
615 			    remainder, channels);
616 		} else {
617 			buffer_demux_port(sc->rbuf, ch->data, row, slots, pos,
618 			    length - remainder, channels);
619 			buffer_demux_port(sc->rbuf, ch->data, row, slots, 0,
620 			    remainder, channels);
621 		}
622 
623 		slots &= ~row;
624 		row = hdsp_slot_first_row(slots);
625 	}
626 
627 	ch->position = ((pos + length) * 4) % buffer_size;
628 }
629 
630 static int
631 clean(struct sc_chinfo *ch)
632 {
633 	struct sc_pcminfo *scp;
634 	struct sc_info *sc;
635 	uint32_t *buf;
636 	uint32_t slot, slots;
637 	unsigned int offset;
638 
639 	scp = ch->parent;
640 	sc = scp->sc;
641 	buf = sc->rbuf;
642 
643 	if (ch->dir == PCMDIR_PLAY)
644 		buf = sc->pbuf;
645 
646 	/* Iterate through all of the channel's slots. */
647 	slots = hdsp_port_slot_map(ch->ports, sc->speed);
648 	slot = hdsp_slot_first(slots);
649 	while (slot != 0) {
650 		/* Clear the slot's buffer. */
651 		offset = hdsp_slot_offset(slot);
652 		bzero(buf + offset * HDSP_CHANBUF_SAMPLES, HDSP_CHANBUF_SIZE);
653 
654 		slots &= ~slot;
655 		slot = hdsp_slot_first(slots);
656 	}
657 
658 	ch->position = 0;
659 
660 	return (0);
661 }
662 
663 /* Channel interface. */
664 static void *
665 hdspchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
666     struct pcm_channel *c, int dir)
667 {
668 	struct sc_pcminfo *scp;
669 	struct sc_chinfo *ch;
670 	struct sc_info *sc;
671 	int num;
672 
673 	scp = devinfo;
674 	sc = scp->sc;
675 
676 	snd_mtxlock(sc->lock);
677 	num = scp->chnum;
678 
679 	ch = &scp->chan[num];
680 
681 	if (dir == PCMDIR_PLAY)
682 		ch->ports = hdsp_channel_play_ports(scp->hc);
683 	else
684 		ch->ports = hdsp_channel_rec_ports(scp->hc);
685 
686 	ch->run = 0;
687 	ch->lvol = 0;
688 	ch->rvol = 0;
689 
690 	/* Support all possible ADAT widths as channel formats. */
691 	ch->cap_fmts[0] =
692 	    SND_FORMAT(AFMT_S32_LE, hdsp_port_slot_count(ch->ports, 48000), 0);
693 	ch->cap_fmts[1] =
694 	    SND_FORMAT(AFMT_S32_LE, hdsp_port_slot_count(ch->ports, 96000), 0);
695 	ch->cap_fmts[2] =
696 	    SND_FORMAT(AFMT_S32_LE, hdsp_port_slot_count(ch->ports, 192000), 0);
697 	ch->cap_fmts[3] = 0;
698 
699 	ch->caps = malloc(sizeof(struct pcmchan_caps), M_HDSP, M_NOWAIT);
700 	*(ch->caps) = (struct pcmchan_caps) {32000, 192000, ch->cap_fmts, 0};
701 
702 	/* HDSP 9652 does not support quad speed sample rates. */
703 	if (sc->type == HDSP_9652) {
704 		ch->cap_fmts[2] = SND_FORMAT(AFMT_S32_LE, 2, 0);
705 		ch->caps->maxspeed = 96000;
706 	}
707 
708 	/* Allocate maximum buffer size. */
709 	ch->size = HDSP_CHANBUF_SIZE * hdsp_port_slot_count_max(ch->ports);
710 	ch->data = malloc(ch->size, M_HDSP, M_NOWAIT);
711 	ch->position = 0;
712 
713 	ch->buffer = b;
714 	ch->channel = c;
715 	ch->parent = scp;
716 
717 	ch->dir = dir;
718 
719 	snd_mtxunlock(sc->lock);
720 
721 	if (sndbuf_setup(ch->buffer, ch->data, ch->size) != 0) {
722 		device_printf(scp->dev, "Can't setup sndbuf.\n");
723 		return (NULL);
724 	}
725 
726 	return (ch);
727 }
728 
729 static int
730 hdspchan_trigger(kobj_t obj, void *data, int go)
731 {
732 	struct sc_pcminfo *scp;
733 	struct sc_chinfo *ch;
734 	struct sc_info *sc;
735 
736 	ch = data;
737 	scp = ch->parent;
738 	sc = scp->sc;
739 
740 	snd_mtxlock(sc->lock);
741 	switch (go) {
742 	case PCMTRIG_START:
743 #if 0
744 		device_printf(scp->dev, "hdspchan_trigger(): start\n");
745 #endif
746 		hdspchan_enable(ch, 1);
747 		hdspchan_setgain(ch);
748 		hdsp_start_audio(sc);
749 		break;
750 
751 	case PCMTRIG_STOP:
752 	case PCMTRIG_ABORT:
753 #if 0
754 		device_printf(scp->dev, "hdspchan_trigger(): stop or abort\n");
755 #endif
756 		clean(ch);
757 		hdspchan_enable(ch, 0);
758 		hdsp_stop_audio(sc);
759 		break;
760 
761 	case PCMTRIG_EMLDMAWR:
762 	case PCMTRIG_EMLDMARD:
763 		if(ch->run)
764 			buffer_copy(ch);
765 		break;
766 	}
767 
768 	snd_mtxunlock(sc->lock);
769 
770 	return (0);
771 }
772 
773 static uint32_t
774 hdspchan_getptr(kobj_t obj, void *data)
775 {
776 	struct sc_pcminfo *scp;
777 	struct sc_chinfo *ch;
778 	struct sc_info *sc;
779 	uint32_t ret, pos;
780 
781 	ch = data;
782 	scp = ch->parent;
783 	sc = scp->sc;
784 
785 	snd_mtxlock(sc->lock);
786 	ret = hdsp_read_2(sc, HDSP_STATUS_REG);
787 	snd_mtxunlock(sc->lock);
788 
789 	pos = ret & HDSP_BUF_POSITION_MASK;
790 	pos %= (2 * sc->period * sizeof(uint32_t)); /* Double buffer. */
791 	pos *= AFMT_CHANNEL(ch->format); /* Hardbuf with multiple channels. */
792 
793 	return (pos);
794 }
795 
796 static int
797 hdspchan_free(kobj_t obj, void *data)
798 {
799 	struct sc_pcminfo *scp;
800 	struct sc_chinfo *ch;
801 	struct sc_info *sc;
802 
803 	ch = data;
804 	scp = ch->parent;
805 	sc = scp->sc;
806 
807 #if 0
808 	device_printf(scp->dev, "hdspchan_free()\n");
809 #endif
810 
811 	snd_mtxlock(sc->lock);
812 	if (ch->data != NULL) {
813 		free(ch->data, M_HDSP);
814 		ch->data = NULL;
815 	}
816 	if (ch->caps != NULL) {
817 		free(ch->caps, M_HDSP);
818 		ch->caps = NULL;
819 	}
820 	snd_mtxunlock(sc->lock);
821 
822 	return (0);
823 }
824 
825 static int
826 hdspchan_setformat(kobj_t obj, void *data, uint32_t format)
827 {
828 	struct sc_chinfo *ch;
829 
830 	ch = data;
831 
832 #if 0
833 	struct sc_pcminfo *scp = ch->parent;
834 	device_printf(scp->dev, "hdspchan_setformat(%d)\n", format);
835 #endif
836 
837 	ch->format = format;
838 
839 	return (0);
840 }
841 
842 static uint32_t
843 hdspchan_setspeed(kobj_t obj, void *data, uint32_t speed)
844 {
845 	struct sc_pcminfo *scp;
846 	struct hdsp_rate *hr;
847 	struct sc_chinfo *ch;
848 	struct sc_info *sc;
849 	int threshold;
850 	int i;
851 
852 	ch = data;
853 	scp = ch->parent;
854 	sc = scp->sc;
855 	hr = NULL;
856 
857 #if 0
858 	device_printf(scp->dev, "hdspchan_setspeed(%d)\n", speed);
859 #endif
860 
861 	if (hdsp_running(sc) == 1)
862 		goto end;
863 
864 	/* HDSP 9652 only supports sample rates up to 96kHz. */
865 	if (sc->type == HDSP_9652 && speed > 96000)
866 		speed = 96000;
867 
868 	if (sc->force_speed > 0)
869 		speed = sc->force_speed;
870 
871 	/* First look for equal frequency. */
872 	for (i = 0; rate_map[i].speed != 0; i++) {
873 		if (rate_map[i].speed == speed)
874 			hr = &rate_map[i];
875 	}
876 
877 	/* If no match, just find nearest. */
878 	if (hr == NULL) {
879 		for (i = 0; rate_map[i].speed != 0; i++) {
880 			hr = &rate_map[i];
881 			threshold = hr->speed + ((rate_map[i + 1].speed != 0) ?
882 			    ((rate_map[i + 1].speed - hr->speed) >> 1) : 0);
883 			if (speed < threshold)
884 				break;
885 		}
886 	}
887 
888 	/* Write frequency on the device. */
889 	sc->ctrl_register &= ~HDSP_FREQ_MASK;
890 	sc->ctrl_register |= hr->reg;
891 	hdsp_write_4(sc, HDSP_CONTROL_REG, sc->ctrl_register);
892 
893 	if (sc->type == HDSP_9632) {
894 		/* Set DDS value. */
895 		hdsp_write_4(sc, HDSP_FREQ_REG, hdsp_freq_reg_value(hr->speed));
896 	}
897 
898 	sc->speed = hr->speed;
899 end:
900 
901 	return (sc->speed);
902 }
903 
904 static uint32_t
905 hdspchan_setblocksize(kobj_t obj, void *data, uint32_t blocksize)
906 {
907 	struct hdsp_latency *hl;
908 	struct sc_pcminfo *scp;
909 	struct sc_chinfo *ch;
910 	struct sc_info *sc;
911 	int threshold;
912 	int i;
913 
914 	ch = data;
915 	scp = ch->parent;
916 	sc = scp->sc;
917 	hl = NULL;
918 
919 #if 0
920 	device_printf(scp->dev, "hdspchan_setblocksize(%d)\n", blocksize);
921 #endif
922 
923 	if (hdsp_running(sc) == 1)
924 		goto end;
925 
926 	if (blocksize > HDSP_LAT_BYTES_MAX)
927 		blocksize = HDSP_LAT_BYTES_MAX;
928 	else if (blocksize < HDSP_LAT_BYTES_MIN)
929 		blocksize = HDSP_LAT_BYTES_MIN;
930 
931 	blocksize /= 4 /* samples */;
932 
933 	if (sc->force_period > 0)
934 		blocksize = sc->force_period;
935 
936 	/* First look for equal latency. */
937 	for (i = 0; latency_map[i].period != 0; i++) {
938 		if (latency_map[i].period == blocksize)
939 			hl = &latency_map[i];
940 	}
941 
942 	/* If no match, just find nearest. */
943 	if (hl == NULL) {
944 		for (i = 0; latency_map[i].period != 0; i++) {
945 			hl = &latency_map[i];
946 			threshold = hl->period + ((latency_map[i + 1].period != 0) ?
947 			    ((latency_map[i + 1].period - hl->period) >> 1) : 0);
948 			if (blocksize < threshold)
949 				break;
950 		}
951 	}
952 
953 	snd_mtxlock(sc->lock);
954 	sc->ctrl_register &= ~HDSP_LAT_MASK;
955 	sc->ctrl_register |= hdsp_encode_latency(hl->n);
956 	hdsp_write_4(sc, HDSP_CONTROL_REG, sc->ctrl_register);
957 	sc->period = hl->period;
958 	snd_mtxunlock(sc->lock);
959 
960 #if 0
961 	device_printf(scp->dev, "New period=%d\n", sc->period);
962 #endif
963 
964 	sndbuf_resize(ch->buffer, 2,
965 	    (sc->period * AFMT_CHANNEL(ch->format) * sizeof(uint32_t)));
966 
967 	/* Reset pointer, rewrite frequency (same register) for 9632. */
968 	hdsp_write_4(sc, HDSP_RESET_POINTER, 0);
969 	if (sc->type == HDSP_9632)
970 		hdsp_write_4(sc, HDSP_FREQ_REG, hdsp_freq_reg_value(sc->speed));
971 end:
972 
973 	return (sndbuf_getblksz(ch->buffer));
974 }
975 
976 static uint32_t hdsp_bkp_fmt[] = {
977 	SND_FORMAT(AFMT_S32_LE, 2, 0),
978 	0
979 };
980 
981 /* Capabilities fallback, no quad speed for HDSP 9652 compatibility. */
982 static struct pcmchan_caps hdsp_bkp_caps = {32000, 96000, hdsp_bkp_fmt, 0};
983 
984 static struct pcmchan_caps *
985 hdspchan_getcaps(kobj_t obj, void *data)
986 {
987 	struct sc_chinfo *ch;
988 
989 	ch = data;
990 
991 #if 0
992 	device_printf(ch->parent->dev, "hdspchan_getcaps()\n");
993 #endif
994 
995 	if (ch->caps != NULL)
996 		return (ch->caps);
997 
998 	return (&hdsp_bkp_caps);
999 }
1000 
1001 static kobj_method_t hdspchan_methods[] = {
1002 	KOBJMETHOD(channel_init,         hdspchan_init),
1003 	KOBJMETHOD(channel_free,         hdspchan_free),
1004 	KOBJMETHOD(channel_setformat,    hdspchan_setformat),
1005 	KOBJMETHOD(channel_setspeed,     hdspchan_setspeed),
1006 	KOBJMETHOD(channel_setblocksize, hdspchan_setblocksize),
1007 	KOBJMETHOD(channel_trigger,      hdspchan_trigger),
1008 	KOBJMETHOD(channel_getptr,       hdspchan_getptr),
1009 	KOBJMETHOD(channel_getcaps,      hdspchan_getcaps),
1010 	KOBJMETHOD_END
1011 };
1012 CHANNEL_DECLARE(hdspchan);
1013 
1014 static int
1015 hdsp_pcm_probe(device_t dev)
1016 {
1017 
1018 #if 0
1019 	device_printf(dev,"hdsp_pcm_probe()\n");
1020 #endif
1021 
1022 	return (0);
1023 }
1024 
1025 static uint32_t
1026 hdsp_pcm_intr(struct sc_pcminfo *scp)
1027 {
1028 	struct sc_chinfo *ch;
1029 	struct sc_info *sc;
1030 	int i;
1031 
1032 	sc = scp->sc;
1033 
1034 	for (i = 0; i < scp->chnum; i++) {
1035 		ch = &scp->chan[i];
1036 		snd_mtxunlock(sc->lock);
1037 		chn_intr(ch->channel);
1038 		snd_mtxlock(sc->lock);
1039 	}
1040 
1041 	return (0);
1042 }
1043 
1044 static int
1045 hdsp_pcm_attach(device_t dev)
1046 {
1047 	char status[SND_STATUSLEN];
1048 	struct sc_pcminfo *scp;
1049 	const char *buf;
1050 	uint32_t pcm_flags;
1051 	int err;
1052 	int play, rec;
1053 
1054 	scp = device_get_ivars(dev);
1055 	scp->ih = &hdsp_pcm_intr;
1056 
1057 	if (scp->hc->ports & HDSP_CHAN_9632_ALL)
1058 		buf = "9632";
1059 	else if (scp->hc->ports & HDSP_CHAN_9652_ALL)
1060 		buf = "9652";
1061 	else
1062 		buf = "?";
1063 	device_set_descf(dev, "HDSP %s [%s]", buf, scp->hc->descr);
1064 
1065 	/*
1066 	 * We don't register interrupt handler with snd_setup_intr
1067 	 * in pcm device. Mark pcm device as MPSAFE manually.
1068 	 */
1069 	pcm_flags = pcm_getflags(dev) | SD_F_MPSAFE;
1070 	if (hdsp_port_slot_count_max(scp->hc->ports) > HDSP_MATRIX_MAX)
1071 		/* Disable vchan conversion, too many channels. */
1072 		pcm_flags |= SD_F_BITPERFECT;
1073 	pcm_setflags(dev, pcm_flags);
1074 
1075 	play = (hdsp_channel_play_ports(scp->hc)) ? 1 : 0;
1076 	rec = (hdsp_channel_rec_ports(scp->hc)) ? 1 : 0;
1077 	err = pcm_register(dev, scp, play, rec);
1078 	if (err) {
1079 		device_printf(dev, "Can't register pcm.\n");
1080 		return (ENXIO);
1081 	}
1082 
1083 	scp->chnum = 0;
1084 	if (play) {
1085 		pcm_addchan(dev, PCMDIR_PLAY, &hdspchan_class, scp);
1086 		scp->chnum++;
1087 	}
1088 
1089 	if (rec) {
1090 		pcm_addchan(dev, PCMDIR_REC, &hdspchan_class, scp);
1091 		scp->chnum++;
1092 	}
1093 
1094 	snprintf(status, SND_STATUSLEN, "port 0x%jx irq %jd on %s",
1095 	    rman_get_start(scp->sc->cs),
1096 	    rman_get_start(scp->sc->irq),
1097 	    device_get_nameunit(device_get_parent(dev)));
1098 	pcm_setstatus(dev, status);
1099 
1100 	mixer_init(dev, &hdspmixer_class, scp);
1101 
1102 	return (0);
1103 }
1104 
1105 static int
1106 hdsp_pcm_detach(device_t dev)
1107 {
1108 	int err;
1109 
1110 	err = pcm_unregister(dev);
1111 	if (err) {
1112 		device_printf(dev, "Can't unregister device.\n");
1113 		return (err);
1114 	}
1115 
1116 	return (0);
1117 }
1118 
1119 static device_method_t hdsp_pcm_methods[] = {
1120 	DEVMETHOD(device_probe,     hdsp_pcm_probe),
1121 	DEVMETHOD(device_attach,    hdsp_pcm_attach),
1122 	DEVMETHOD(device_detach,    hdsp_pcm_detach),
1123 	{ 0, 0 }
1124 };
1125 
1126 static driver_t hdsp_pcm_driver = {
1127 	"pcm",
1128 	hdsp_pcm_methods,
1129 	PCM_SOFTC_SIZE,
1130 };
1131 
1132 DRIVER_MODULE(snd_hdsp_pcm, hdsp, hdsp_pcm_driver, 0, 0);
1133 MODULE_DEPEND(snd_hdsp, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1134 MODULE_VERSION(snd_hdsp, 1);
1135