xref: /freebsd/sys/dev/sound/pci/hdsp-pcm.c (revision 0fc6c3f731a2cca3120798806c330a3081c9424b)
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
hdsp_adat_slot_map(uint32_t speed)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
hdsp_port_slot_map(uint32_t ports,uint32_t speed)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)
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
hdsp_slot_first(uint32_t slots)128 hdsp_slot_first(uint32_t slots)
129 {
130 	return (slots & (~(slots - 1)));	/* Extract first bit set. */
131 }
132 
133 static uint32_t
hdsp_slot_first_row(uint32_t slots)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
hdsp_slot_first_n(uint32_t slots,unsigned int n)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
hdsp_slot_count(uint32_t slots)158 hdsp_slot_count(uint32_t slots)
159 {
160 	return (bitcount32(slots));
161 }
162 
163 static unsigned int
hdsp_slot_offset(uint32_t slots)164 hdsp_slot_offset(uint32_t slots)
165 {
166 	return (hdsp_slot_count(hdsp_slot_first(slots) - 1));
167 }
168 
169 static unsigned int
hdsp_slot_channel_offset(uint32_t subset,uint32_t slots)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
hdsp_port_first(uint32_t ports)183 hdsp_port_first(uint32_t ports)
184 {
185 	return (ports & (~(ports - 1)));	/* Extract first bit set. */
186 }
187 
188 static unsigned int
hdsp_port_slot_count(uint32_t ports,uint32_t speed)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
hdsp_port_slot_count_max(uint32_t ports)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
hdsp_channel_play_ports(struct hdsp_channel * hc)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
hdsp_channel_rec_ports(struct hdsp_channel * hc)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
hdsp_hw_mixer(struct sc_chinfo * ch,unsigned int dst,unsigned int src,unsigned short data)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
hdspchan_setgain(struct sc_chinfo * ch)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
hdspmixer_init(struct snd_mixer * m)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 	mtx_lock(&sc->lock);
325 	pcm_setflags(scp->dev, pcm_getflags(scp->dev) | SD_F_SOFTPCMVOL);
326 	mix_setdevs(m, mask);
327 	mtx_unlock(&sc->lock);
328 
329 	return (0);
330 }
331 
332 static int
hdspmixer_set(struct snd_mixer * m,unsigned dev,unsigned left,unsigned right)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
hdspchan_enable(struct sc_chinfo * ch,int value)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
hdsp_running(struct sc_info * sc)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
hdsp_start_audio(struct sc_info * sc)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
hdsp_stop_audio(struct sc_info * sc)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
buffer_mux_write(uint32_t * dma,uint32_t * pcm,unsigned int pos,unsigned int pos_end,unsigned int width,unsigned int channels)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
buffer_mux_port(uint32_t * dma,uint32_t * pcm,uint32_t subset,uint32_t slots,unsigned int pos,unsigned int samples,unsigned int channels)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
buffer_demux_read(uint32_t * dma,uint32_t * pcm,unsigned int pos,unsigned int pos_end,unsigned int width,unsigned int channels)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
buffer_demux_port(uint32_t * dma,uint32_t * pcm,uint32_t subset,uint32_t slots,unsigned int pos,unsigned int samples,unsigned int channels)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
buffer_copy(struct sc_chinfo * ch)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
clean(struct sc_chinfo * ch)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 int
hdspchan_free(kobj_t obj,void * data)665 hdspchan_free(kobj_t obj, void *data)
666 {
667 	struct sc_pcminfo *scp;
668 	struct sc_chinfo *ch;
669 	struct sc_info *sc;
670 
671 	ch = data;
672 	scp = ch->parent;
673 	sc = scp->sc;
674 
675 #if 0
676 	device_printf(scp->dev, "hdspchan_free()\n");
677 #endif
678 
679 	mtx_lock(&sc->lock);
680 	free(ch->data, M_HDSP);
681 	ch->data = NULL;
682 	free(ch->caps, M_HDSP);
683 	ch->caps = NULL;
684 	mtx_unlock(&sc->lock);
685 
686 	return (0);
687 }
688 
689 static void *
hdspchan_init(kobj_t obj,void * devinfo,struct snd_dbuf * b,struct pcm_channel * c,int dir)690 hdspchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
691     struct pcm_channel *c, int dir)
692 {
693 	struct sc_pcminfo *scp;
694 	struct sc_chinfo *ch;
695 	struct sc_info *sc;
696 	int num;
697 
698 	scp = devinfo;
699 	sc = scp->sc;
700 
701 	mtx_lock(&sc->lock);
702 	num = scp->chnum;
703 
704 	ch = &scp->chan[num];
705 
706 	if (dir == PCMDIR_PLAY)
707 		ch->ports = hdsp_channel_play_ports(scp->hc);
708 	else
709 		ch->ports = hdsp_channel_rec_ports(scp->hc);
710 
711 	ch->run = 0;
712 	ch->lvol = 0;
713 	ch->rvol = 0;
714 
715 	/* Support all possible ADAT widths as channel formats. */
716 	ch->cap_fmts[0] =
717 	    SND_FORMAT(AFMT_S32_LE, hdsp_port_slot_count(ch->ports, 48000), 0);
718 	ch->cap_fmts[1] =
719 	    SND_FORMAT(AFMT_S32_LE, hdsp_port_slot_count(ch->ports, 96000), 0);
720 	ch->cap_fmts[2] =
721 	    SND_FORMAT(AFMT_S32_LE, hdsp_port_slot_count(ch->ports, 192000), 0);
722 	ch->cap_fmts[3] = 0;
723 
724 	ch->caps = malloc(sizeof(struct pcmchan_caps), M_HDSP, M_NOWAIT);
725 	*(ch->caps) = (struct pcmchan_caps) {32000, 192000, ch->cap_fmts, 0};
726 
727 	/* HDSP 9652 does not support quad speed sample rates. */
728 	if (sc->type == HDSP_9652) {
729 		ch->cap_fmts[2] = SND_FORMAT(AFMT_S32_LE, 2, 0);
730 		ch->caps->maxspeed = 96000;
731 	}
732 
733 	/* Allocate maximum buffer size. */
734 	ch->size = HDSP_CHANBUF_SIZE * hdsp_port_slot_count_max(ch->ports);
735 	ch->data = malloc(ch->size, M_HDSP, M_NOWAIT);
736 	ch->position = 0;
737 
738 	ch->buffer = b;
739 	ch->channel = c;
740 	ch->parent = scp;
741 
742 	ch->dir = dir;
743 
744 	mtx_unlock(&sc->lock);
745 
746 	if (sndbuf_setup(ch->buffer, ch->data, ch->size) != 0) {
747 		device_printf(scp->dev, "Can't setup sndbuf.\n");
748 		hdspchan_free(obj, ch);
749 		return (NULL);
750 	}
751 
752 	return (ch);
753 }
754 
755 static int
hdspchan_trigger(kobj_t obj,void * data,int go)756 hdspchan_trigger(kobj_t obj, void *data, int go)
757 {
758 	struct sc_pcminfo *scp;
759 	struct sc_chinfo *ch;
760 	struct sc_info *sc;
761 
762 	ch = data;
763 	scp = ch->parent;
764 	sc = scp->sc;
765 
766 	mtx_lock(&sc->lock);
767 	switch (go) {
768 	case PCMTRIG_START:
769 #if 0
770 		device_printf(scp->dev, "hdspchan_trigger(): start\n");
771 #endif
772 		hdspchan_enable(ch, 1);
773 		hdspchan_setgain(ch);
774 		hdsp_start_audio(sc);
775 		break;
776 
777 	case PCMTRIG_STOP:
778 	case PCMTRIG_ABORT:
779 #if 0
780 		device_printf(scp->dev, "hdspchan_trigger(): stop or abort\n");
781 #endif
782 		clean(ch);
783 		hdspchan_enable(ch, 0);
784 		hdsp_stop_audio(sc);
785 		break;
786 
787 	case PCMTRIG_EMLDMAWR:
788 	case PCMTRIG_EMLDMARD:
789 		if(ch->run)
790 			buffer_copy(ch);
791 		break;
792 	}
793 
794 	mtx_unlock(&sc->lock);
795 
796 	return (0);
797 }
798 
799 static uint32_t
hdspchan_getptr(kobj_t obj,void * data)800 hdspchan_getptr(kobj_t obj, void *data)
801 {
802 	struct sc_pcminfo *scp;
803 	struct sc_chinfo *ch;
804 	struct sc_info *sc;
805 	uint32_t ret, pos;
806 
807 	ch = data;
808 	scp = ch->parent;
809 	sc = scp->sc;
810 
811 	mtx_lock(&sc->lock);
812 	ret = hdsp_read_2(sc, HDSP_STATUS_REG);
813 	mtx_unlock(&sc->lock);
814 
815 	pos = ret & HDSP_BUF_POSITION_MASK;
816 	pos %= (2 * sc->period * sizeof(uint32_t)); /* Double buffer. */
817 	pos *= AFMT_CHANNEL(ch->format); /* Hardbuf with multiple channels. */
818 
819 	return (pos);
820 }
821 
822 static int
hdspchan_setformat(kobj_t obj,void * data,uint32_t format)823 hdspchan_setformat(kobj_t obj, void *data, uint32_t format)
824 {
825 	struct sc_chinfo *ch;
826 
827 	ch = data;
828 
829 #if 0
830 	struct sc_pcminfo *scp = ch->parent;
831 	device_printf(scp->dev, "hdspchan_setformat(%d)\n", format);
832 #endif
833 
834 	ch->format = format;
835 
836 	return (0);
837 }
838 
839 static uint32_t
hdspchan_setspeed(kobj_t obj,void * data,uint32_t speed)840 hdspchan_setspeed(kobj_t obj, void *data, uint32_t speed)
841 {
842 	struct sc_pcminfo *scp;
843 	struct hdsp_rate *hr;
844 	struct sc_chinfo *ch;
845 	struct sc_info *sc;
846 	int threshold;
847 	int i;
848 
849 	ch = data;
850 	scp = ch->parent;
851 	sc = scp->sc;
852 	hr = NULL;
853 
854 #if 0
855 	device_printf(scp->dev, "hdspchan_setspeed(%d)\n", speed);
856 #endif
857 
858 	if (hdsp_running(sc) == 1)
859 		goto end;
860 
861 	/* HDSP 9652 only supports sample rates up to 96kHz. */
862 	if (sc->type == HDSP_9652 && speed > 96000)
863 		speed = 96000;
864 
865 	if (sc->force_speed > 0)
866 		speed = sc->force_speed;
867 
868 	/* First look for equal frequency. */
869 	for (i = 0; rate_map[i].speed != 0; i++) {
870 		if (rate_map[i].speed == speed)
871 			hr = &rate_map[i];
872 	}
873 
874 	/* If no match, just find nearest. */
875 	if (hr == NULL) {
876 		for (i = 0; rate_map[i].speed != 0; i++) {
877 			hr = &rate_map[i];
878 			threshold = hr->speed + ((rate_map[i + 1].speed != 0) ?
879 			    ((rate_map[i + 1].speed - hr->speed) >> 1) : 0);
880 			if (speed < threshold)
881 				break;
882 		}
883 	}
884 
885 	/* Write frequency on the device. */
886 	sc->ctrl_register &= ~HDSP_FREQ_MASK;
887 	sc->ctrl_register |= hr->reg;
888 	hdsp_write_4(sc, HDSP_CONTROL_REG, sc->ctrl_register);
889 
890 	if (sc->type == HDSP_9632) {
891 		/* Set DDS value. */
892 		hdsp_write_4(sc, HDSP_FREQ_REG, hdsp_freq_reg_value(hr->speed));
893 	}
894 
895 	sc->speed = hr->speed;
896 end:
897 
898 	return (sc->speed);
899 }
900 
901 static uint32_t
hdspchan_setblocksize(kobj_t obj,void * data,uint32_t blocksize)902 hdspchan_setblocksize(kobj_t obj, void *data, uint32_t blocksize)
903 {
904 	struct hdsp_latency *hl;
905 	struct sc_pcminfo *scp;
906 	struct sc_chinfo *ch;
907 	struct sc_info *sc;
908 	int threshold;
909 	int i;
910 
911 	ch = data;
912 	scp = ch->parent;
913 	sc = scp->sc;
914 	hl = NULL;
915 
916 #if 0
917 	device_printf(scp->dev, "hdspchan_setblocksize(%d)\n", blocksize);
918 #endif
919 
920 	if (hdsp_running(sc) == 1)
921 		goto end;
922 
923 	if (blocksize > HDSP_LAT_BYTES_MAX)
924 		blocksize = HDSP_LAT_BYTES_MAX;
925 	else if (blocksize < HDSP_LAT_BYTES_MIN)
926 		blocksize = HDSP_LAT_BYTES_MIN;
927 
928 	blocksize /= 4 /* samples */;
929 
930 	if (sc->force_period > 0)
931 		blocksize = sc->force_period;
932 
933 	/* First look for equal latency. */
934 	for (i = 0; latency_map[i].period != 0; i++) {
935 		if (latency_map[i].period == blocksize)
936 			hl = &latency_map[i];
937 	}
938 
939 	/* If no match, just find nearest. */
940 	if (hl == NULL) {
941 		for (i = 0; latency_map[i].period != 0; i++) {
942 			hl = &latency_map[i];
943 			threshold = hl->period + ((latency_map[i + 1].period != 0) ?
944 			    ((latency_map[i + 1].period - hl->period) >> 1) : 0);
945 			if (blocksize < threshold)
946 				break;
947 		}
948 	}
949 
950 	mtx_lock(&sc->lock);
951 	sc->ctrl_register &= ~HDSP_LAT_MASK;
952 	sc->ctrl_register |= hdsp_encode_latency(hl->n);
953 	hdsp_write_4(sc, HDSP_CONTROL_REG, sc->ctrl_register);
954 	sc->period = hl->period;
955 	mtx_unlock(&sc->lock);
956 
957 #if 0
958 	device_printf(scp->dev, "New period=%d\n", sc->period);
959 #endif
960 
961 	sndbuf_resize(ch->buffer, 2,
962 	    (sc->period * AFMT_CHANNEL(ch->format) * sizeof(uint32_t)));
963 
964 	/* Reset pointer, rewrite frequency (same register) for 9632. */
965 	hdsp_write_4(sc, HDSP_RESET_POINTER, 0);
966 	if (sc->type == HDSP_9632)
967 		hdsp_write_4(sc, HDSP_FREQ_REG, hdsp_freq_reg_value(sc->speed));
968 end:
969 
970 	return (ch->buffer->blksz);
971 }
972 
973 static uint32_t hdsp_bkp_fmt[] = {
974 	SND_FORMAT(AFMT_S32_LE, 2, 0),
975 	0
976 };
977 
978 /* Capabilities fallback, no quad speed for HDSP 9652 compatibility. */
979 static struct pcmchan_caps hdsp_bkp_caps = {32000, 96000, hdsp_bkp_fmt, 0};
980 
981 static struct pcmchan_caps *
hdspchan_getcaps(kobj_t obj,void * data)982 hdspchan_getcaps(kobj_t obj, void *data)
983 {
984 	struct sc_chinfo *ch;
985 
986 	ch = data;
987 
988 #if 0
989 	device_printf(ch->parent->dev, "hdspchan_getcaps()\n");
990 #endif
991 
992 	if (ch->caps != NULL)
993 		return (ch->caps);
994 
995 	return (&hdsp_bkp_caps);
996 }
997 
998 static kobj_method_t hdspchan_methods[] = {
999 	KOBJMETHOD(channel_init,         hdspchan_init),
1000 	KOBJMETHOD(channel_free,         hdspchan_free),
1001 	KOBJMETHOD(channel_setformat,    hdspchan_setformat),
1002 	KOBJMETHOD(channel_setspeed,     hdspchan_setspeed),
1003 	KOBJMETHOD(channel_setblocksize, hdspchan_setblocksize),
1004 	KOBJMETHOD(channel_trigger,      hdspchan_trigger),
1005 	KOBJMETHOD(channel_getptr,       hdspchan_getptr),
1006 	KOBJMETHOD(channel_getcaps,      hdspchan_getcaps),
1007 	KOBJMETHOD_END
1008 };
1009 CHANNEL_DECLARE(hdspchan);
1010 
1011 static int
hdsp_pcm_probe(device_t dev)1012 hdsp_pcm_probe(device_t dev)
1013 {
1014 
1015 #if 0
1016 	device_printf(dev,"hdsp_pcm_probe()\n");
1017 #endif
1018 
1019 	return (0);
1020 }
1021 
1022 static uint32_t
hdsp_pcm_intr(struct sc_pcminfo * scp)1023 hdsp_pcm_intr(struct sc_pcminfo *scp)
1024 {
1025 	struct sc_chinfo *ch;
1026 	struct sc_info *sc;
1027 	int i;
1028 
1029 	sc = scp->sc;
1030 
1031 	for (i = 0; i < scp->chnum; i++) {
1032 		ch = &scp->chan[i];
1033 		mtx_unlock(&sc->lock);
1034 		chn_intr(ch->channel);
1035 		mtx_lock(&sc->lock);
1036 	}
1037 
1038 	return (0);
1039 }
1040 
1041 static int
hdsp_pcm_attach(device_t dev)1042 hdsp_pcm_attach(device_t dev)
1043 {
1044 	char status[SND_STATUSLEN];
1045 	struct sc_pcminfo *scp;
1046 	const char *buf;
1047 	uint32_t pcm_flags;
1048 	int err;
1049 	int play, rec;
1050 
1051 	scp = device_get_ivars(dev);
1052 	scp->ih = &hdsp_pcm_intr;
1053 
1054 	if (scp->hc->ports & HDSP_CHAN_9632_ALL)
1055 		buf = "9632";
1056 	else if (scp->hc->ports & HDSP_CHAN_9652_ALL)
1057 		buf = "9652";
1058 	else
1059 		buf = "?";
1060 	device_set_descf(dev, "HDSP %s [%s]", buf, scp->hc->descr);
1061 
1062 	/*
1063 	 * We don't register interrupt handler with snd_setup_intr
1064 	 * in pcm device. Mark pcm device as MPSAFE manually.
1065 	 */
1066 	pcm_flags = pcm_getflags(dev) | SD_F_MPSAFE;
1067 	if (hdsp_port_slot_count_max(scp->hc->ports) > HDSP_MATRIX_MAX)
1068 		/* Disable vchan conversion, too many channels. */
1069 		pcm_flags |= SD_F_BITPERFECT;
1070 	pcm_setflags(dev, pcm_flags);
1071 
1072 	pcm_init(dev, scp);
1073 
1074 	play = (hdsp_channel_play_ports(scp->hc)) ? 1 : 0;
1075 	rec = (hdsp_channel_rec_ports(scp->hc)) ? 1 : 0;
1076 
1077 	scp->chnum = 0;
1078 	if (play) {
1079 		pcm_addchan(dev, PCMDIR_PLAY, &hdspchan_class, scp);
1080 		scp->chnum++;
1081 	}
1082 
1083 	if (rec) {
1084 		pcm_addchan(dev, PCMDIR_REC, &hdspchan_class, scp);
1085 		scp->chnum++;
1086 	}
1087 
1088 	snprintf(status, SND_STATUSLEN, "port 0x%jx irq %jd on %s",
1089 	    rman_get_start(scp->sc->cs),
1090 	    rman_get_start(scp->sc->irq),
1091 	    device_get_nameunit(device_get_parent(dev)));
1092 	err = pcm_register(dev, status);
1093 	if (err) {
1094 		device_printf(dev, "Can't register pcm.\n");
1095 		return (ENXIO);
1096 	}
1097 
1098 	mixer_init(dev, &hdspmixer_class, scp);
1099 
1100 	return (0);
1101 }
1102 
1103 static int
hdsp_pcm_detach(device_t dev)1104 hdsp_pcm_detach(device_t dev)
1105 {
1106 	int err;
1107 
1108 	err = pcm_unregister(dev);
1109 	if (err) {
1110 		device_printf(dev, "Can't unregister device.\n");
1111 		return (err);
1112 	}
1113 
1114 	return (0);
1115 }
1116 
1117 static device_method_t hdsp_pcm_methods[] = {
1118 	DEVMETHOD(device_probe,     hdsp_pcm_probe),
1119 	DEVMETHOD(device_attach,    hdsp_pcm_attach),
1120 	DEVMETHOD(device_detach,    hdsp_pcm_detach),
1121 	DEVMETHOD_END
1122 };
1123 
1124 static driver_t hdsp_pcm_driver = {
1125 	"pcm",
1126 	hdsp_pcm_methods,
1127 	PCM_SOFTC_SIZE,
1128 };
1129 
1130 DRIVER_MODULE(snd_hdsp_pcm, hdsp, hdsp_pcm_driver, 0, 0);
1131 MODULE_DEPEND(snd_hdsp, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1132 MODULE_VERSION(snd_hdsp, 1);
1133