1232337Smav/*-
2232337Smav * Copyright (c) 2012 Ruslan Bukin <br@bsdpad.com>
3232337Smav * All rights reserved.
4232337Smav *
5232337Smav * Redistribution and use in source and binary forms, with or without
6232337Smav * modification, are permitted provided that the following conditions
7232337Smav * are met:
8232337Smav * 1. Redistributions of source code must retain the above copyright
9232337Smav *    notice, this list of conditions and the following disclaimer.
10232337Smav * 2. Redistributions in binary form must reproduce the above copyright
11232337Smav *    notice, this list of conditions and the following disclaimer in the
12232337Smav *    documentation and/or other materials provided with the distribution.
13232337Smav *
14232337Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15232337Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16232337Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17232337Smav * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18232337Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19232337Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20232337Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21232337Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22232337Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23232337Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24232337Smav * SUCH DAMAGE.
25232337Smav */
26232337Smav
27232337Smav/*
28232337Smav * RME HDSPe driver for FreeBSD (pcm-part).
29232337Smav * Supported cards: AIO, RayDAT.
30232337Smav */
31232337Smav
32232337Smav#include <dev/sound/pcm/sound.h>
33232337Smav#include <dev/sound/pci/hdspe.h>
34232337Smav#include <dev/sound/chip.h>
35232337Smav
36232337Smav#include <dev/pci/pcireg.h>
37232337Smav#include <dev/pci/pcivar.h>
38232337Smav
39232337Smav#include <mixer_if.h>
40232337Smav
41232337SmavSND_DECLARE_FILE("$FreeBSD$");
42232337Smav
43232337Smavstruct hdspe_latency {
44232337Smav	uint32_t n;
45232337Smav	uint32_t period;
46232337Smav	float ms;
47232337Smav};
48232337Smav
49232337Smavstatic struct hdspe_latency latency_map[] = {
50232337Smav	{ 7,   32, 0.7 },
51232337Smav	{ 0,   64, 1.5 },
52232337Smav	{ 1,  128,   3 },
53232337Smav	{ 2,  256,   6 },
54232337Smav	{ 3,  512,  12 },
55232337Smav	{ 4, 1024,  23 },
56232337Smav	{ 5, 2048,  46 },
57232337Smav	{ 6, 4096,  93 },
58232337Smav
59232337Smav	{ 0,    0,   0 },
60232337Smav};
61232337Smav
62232337Smavstruct hdspe_rate {
63232337Smav	uint32_t speed;
64232337Smav	uint32_t reg;
65232337Smav};
66232337Smav
67232337Smavstatic struct hdspe_rate rate_map[] = {
68232337Smav	{  32000, (HDSPE_FREQ_32000) },
69232337Smav	{  44100, (HDSPE_FREQ_44100) },
70232337Smav	{  48000, (HDSPE_FREQ_48000) },
71232337Smav	{  64000, (HDSPE_FREQ_32000 | HDSPE_FREQ_DOUBLE) },
72232337Smav	{  88200, (HDSPE_FREQ_44100 | HDSPE_FREQ_DOUBLE) },
73232337Smav	{  96000, (HDSPE_FREQ_48000 | HDSPE_FREQ_DOUBLE) },
74232337Smav	{ 128000, (HDSPE_FREQ_32000 | HDSPE_FREQ_QUAD)   },
75232337Smav	{ 176400, (HDSPE_FREQ_44100 | HDSPE_FREQ_QUAD)   },
76232337Smav	{ 192000, (HDSPE_FREQ_48000 | HDSPE_FREQ_QUAD)   },
77232337Smav
78232337Smav	{ 0, 0 },
79232337Smav};
80232337Smav
81232337Smav
82232337Smavstatic int
83232337Smavhdspe_hw_mixer(struct sc_chinfo *ch, unsigned int dst,
84232337Smav    unsigned int src, unsigned short data)
85232337Smav{
86232337Smav	struct sc_pcminfo *scp = ch->parent;
87232337Smav	struct sc_info *sc = scp->sc;
88232337Smav	int offs = 0;
89232337Smav
90232337Smav	if (ch->dir == PCMDIR_PLAY)
91232337Smav		offs = 64;
92232337Smav
93232337Smav	hdspe_write_4(sc, HDSPE_MIXER_BASE +
94232337Smav	    ((offs + src + 128 * dst) * sizeof(uint32_t)),
95232337Smav	    data & 0xFFFF);
96232337Smav
97232337Smav	return 0;
98232337Smav};
99232337Smav
100232337Smavstatic int
101232337Smavhdspechan_setgain(struct sc_chinfo *ch)
102232337Smav{
103232337Smav
104232337Smav	hdspe_hw_mixer(ch, ch->lslot, ch->lslot,
105232337Smav	    ch->lvol * HDSPE_MAX_GAIN / 100);
106232337Smav	hdspe_hw_mixer(ch, ch->rslot, ch->rslot,
107232337Smav	    ch->rvol * HDSPE_MAX_GAIN / 100);
108232337Smav
109232337Smav	return 0;
110232337Smav}
111232337Smav
112232337Smavstatic int
113232337Smavhdspemixer_init(struct snd_mixer *m)
114232337Smav{
115232337Smav	struct sc_pcminfo *scp = mix_getdevinfo(m);
116232337Smav	struct sc_info *sc = scp->sc;
117232337Smav	int mask;
118232337Smav
119232337Smav	if (sc == NULL)
120232337Smav		return -1;
121232337Smav
122232337Smav	mask = SOUND_MASK_PCM;
123232337Smav
124232337Smav	if (scp->hc->play)
125232337Smav		mask |= SOUND_MASK_VOLUME;
126232337Smav
127232337Smav	if (scp->hc->rec)
128232337Smav		mask |= SOUND_MASK_RECLEV;
129232337Smav
130232337Smav	snd_mtxlock(sc->lock);
131232337Smav	pcm_setflags(scp->dev, pcm_getflags(scp->dev) | SD_F_SOFTPCMVOL);
132232337Smav	mix_setdevs(m, mask);
133232337Smav	snd_mtxunlock(sc->lock);
134232337Smav
135232337Smav	return 0;
136232337Smav}
137232337Smav
138232337Smavstatic int
139232337Smavhdspemixer_set(struct snd_mixer *m, unsigned dev,
140232337Smav    unsigned left, unsigned right)
141232337Smav{
142232337Smav	struct sc_pcminfo *scp = mix_getdevinfo(m);
143232337Smav	struct sc_chinfo *ch;
144232337Smav	int i;
145232337Smav
146232337Smav#if 0
147232337Smav	device_printf(scp->dev, "hdspemixer_set() %d %d\n",
148232337Smav	    left,right);
149232337Smav#endif
150232337Smav
151232337Smav	for (i = 0; i < scp->chnum; i++) {
152232337Smav		ch = &scp->chan[i];
153232337Smav		if ((dev == SOUND_MIXER_VOLUME && ch->dir == PCMDIR_PLAY) ||
154232337Smav		    (dev == SOUND_MIXER_RECLEV && ch->dir == PCMDIR_REC)) {
155232337Smav			ch->lvol = left;
156232337Smav			ch->rvol = right;
157232337Smav			if (ch->run)
158232337Smav				hdspechan_setgain(ch);
159232337Smav		}
160232337Smav	}
161232337Smav
162232337Smav	return 0;
163232337Smav}
164232337Smav
165232337Smavstatic kobj_method_t hdspemixer_methods[] = {
166232337Smav	KOBJMETHOD(mixer_init,      hdspemixer_init),
167232337Smav	KOBJMETHOD(mixer_set,       hdspemixer_set),
168232337Smav	KOBJMETHOD_END
169232337Smav};
170232337SmavMIXER_DECLARE(hdspemixer);
171232337Smav
172232337Smavstatic void
173232337Smavhdspechan_enable(struct sc_chinfo *ch, int value)
174232337Smav{
175232337Smav	struct sc_pcminfo *scp = ch->parent;
176232337Smav	struct sc_info *sc = scp->sc;
177232337Smav	int reg;
178232337Smav
179232337Smav	if (ch->dir == PCMDIR_PLAY)
180232337Smav		reg = HDSPE_OUT_ENABLE_BASE;
181232337Smav	else
182232337Smav		reg = HDSPE_IN_ENABLE_BASE;
183232337Smav
184232337Smav	ch->run = value;
185232337Smav
186232337Smav	hdspe_write_1(sc, reg + (4 * ch->lslot), value);
187232337Smav	hdspe_write_1(sc, reg + (4 * ch->rslot), value);
188232337Smav}
189232337Smav
190232337Smavstatic int
191232337Smavhdspe_running(struct sc_info *sc)
192232337Smav{
193232337Smav	struct sc_pcminfo *scp;
194232337Smav	struct sc_chinfo *ch;
195232337Smav	int i, j, devcount, err;
196232337Smav	device_t *devlist;
197232337Smav
198232337Smav	if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
199232337Smav		goto bad;
200232337Smav
201232337Smav	for (i = 0; i < devcount; i++) {
202232337Smav		scp = device_get_ivars(devlist[i]);
203232337Smav		for (j = 0; j < scp->chnum; j++) {
204232337Smav			ch = &scp->chan[j];
205232337Smav			if (ch->run)
206232337Smav				goto bad;
207232337Smav		}
208232337Smav	}
209232337Smav
210241066Skevlo	free(devlist, M_TEMP);
211232337Smav	return 0;
212232337Smavbad:
213232337Smav
214232337Smav#if 0
215232337Smav	device_printf(sc->dev,"hdspe is running\n");
216232337Smav#endif
217232337Smav
218241066Skevlo	free(devlist, M_TEMP);
219232337Smav	return 1;
220232337Smav}
221232337Smav
222232337Smavstatic void
223232337Smavhdspe_start_audio(struct sc_info *sc)
224232337Smav{
225232337Smav
226232337Smav	sc->ctrl_register |= (HDSPE_AUDIO_INT_ENABLE | HDSPE_ENABLE);
227232337Smav	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
228232337Smav}
229232337Smav
230232337Smavstatic void
231232337Smavhdspe_stop_audio(struct sc_info *sc)
232232337Smav{
233232337Smav
234232337Smav	if (hdspe_running(sc) == 1)
235232337Smav		return;
236232337Smav
237232337Smav	sc->ctrl_register &= ~(HDSPE_AUDIO_INT_ENABLE | HDSPE_ENABLE);
238232337Smav	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
239232337Smav}
240232337Smav
241232337Smav/* Multiplex / demultiplex: 2.0 <-> 2 x 1.0. */
242232337Smavstatic void
243232337Smavbuffer_copy(struct sc_chinfo *ch)
244232337Smav{
245232337Smav	struct sc_pcminfo *scp = ch->parent;
246232337Smav	struct sc_info *sc = scp->sc;
247232337Smav	int length,src,dst;
248232337Smav	int ssize, dsize;
249232337Smav	int i;
250232337Smav
251232337Smav	length = sndbuf_getready(ch->buffer) /
252232337Smav	    (4 /* Bytes per sample. */ * 2 /* channels */);
253232337Smav
254232337Smav	if (ch->dir == PCMDIR_PLAY) {
255232337Smav		src = sndbuf_getreadyptr(ch->buffer);
256232337Smav	} else {
257232337Smav		src = sndbuf_getfreeptr(ch->buffer);
258232337Smav	}
259232337Smav
260232337Smav	src /= 4; /* Bytes per sample. */
261232337Smav	dst = src / 2; /* Destination buffer twice smaller. */
262232337Smav
263232337Smav	ssize = ch->size / 4;
264232337Smav	dsize = ch->size / 8;
265232337Smav
266232337Smav	/*
267232337Smav	 * Use two fragment buffer to avoid sound clipping.
268232337Smav	 */
269232337Smav
270232337Smav	for (i = 0; i < sc->period * 2 /* fragments */; i++) {
271232337Smav		if (ch->dir == PCMDIR_PLAY) {
272232337Smav			sc->pbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->lslot] =
273232337Smav			    ch->data[src];
274232337Smav			sc->pbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->rslot] =
275232337Smav			    ch->data[src + 1];
276232337Smav
277232337Smav		} else {
278232337Smav			ch->data[src] =
279232337Smav			    sc->rbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->lslot];
280232337Smav			ch->data[src+1] =
281232337Smav			    sc->rbuf[dst + HDSPE_CHANBUF_SAMPLES * ch->rslot];
282232337Smav		}
283232337Smav
284232337Smav		dst+=1;
285232337Smav		dst %= dsize;
286232337Smav		src+=2;
287232337Smav		src %= ssize;
288232337Smav	}
289232337Smav}
290232337Smav
291232337Smavstatic int
292232337Smavclean(struct sc_chinfo *ch){
293232337Smav	struct sc_pcminfo *scp = ch->parent;
294232337Smav	struct sc_info *sc = scp->sc;
295232337Smav	uint32_t *buf = sc->rbuf;
296232337Smav
297232337Smav	if (ch->dir == PCMDIR_PLAY) {
298232337Smav		buf = sc->pbuf;
299232337Smav	}
300232337Smav
301232337Smav	bzero(buf + HDSPE_CHANBUF_SAMPLES * ch->lslot, HDSPE_CHANBUF_SIZE);
302232337Smav	bzero(buf + HDSPE_CHANBUF_SAMPLES * ch->rslot, HDSPE_CHANBUF_SIZE);
303232337Smav
304232337Smav	return 0;
305232337Smav}
306232337Smav
307232337Smav
308232337Smav/* Channel interface. */
309232337Smavstatic void *
310232337Smavhdspechan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
311232337Smav               struct pcm_channel *c, int dir)
312232337Smav{
313232337Smav	struct sc_pcminfo *scp = devinfo;
314232337Smav	struct sc_info *sc = scp->sc;
315232337Smav	struct sc_chinfo *ch;
316232337Smav	int num;
317232337Smav
318232337Smav	snd_mtxlock(sc->lock);
319232337Smav	num = scp->chnum;
320232337Smav
321232337Smav	ch = &scp->chan[num];
322232337Smav	ch->lslot = scp->hc->left;
323232337Smav	ch->rslot = scp->hc->right;
324232337Smav	ch->run = 0;
325232337Smav	ch->lvol = 0;
326232337Smav	ch->rvol = 0;
327232337Smav
328232337Smav	ch->size = HDSPE_CHANBUF_SIZE * 2 /* slots */;
329232337Smav	ch->data = malloc(ch->size, M_HDSPE, M_NOWAIT);
330232337Smav
331232337Smav	ch->buffer = b;
332232337Smav	ch->channel = c;
333232337Smav	ch->parent = scp;
334232337Smav
335232337Smav	ch->dir = dir;
336232337Smav
337232337Smav	snd_mtxunlock(sc->lock);
338232337Smav
339232337Smav	if (sndbuf_setup(ch->buffer, ch->data, ch->size) != 0) {
340232337Smav		device_printf(scp->dev, "Can't setup sndbuf.\n");
341232337Smav		return NULL;
342232337Smav	}
343232337Smav
344232337Smav	return ch;
345232337Smav}
346232337Smav
347232337Smavstatic int
348232337Smavhdspechan_trigger(kobj_t obj, void *data, int go)
349232337Smav{
350232337Smav	struct sc_chinfo *ch = data;
351232337Smav	struct sc_pcminfo *scp = ch->parent;
352232337Smav	struct sc_info *sc = scp->sc;
353232337Smav
354232337Smav	snd_mtxlock(sc->lock);
355232337Smav	switch (go) {
356232337Smav	case PCMTRIG_START:
357232337Smav#if 0
358232337Smav		device_printf(scp->dev, "hdspechan_trigger(): start\n");
359232337Smav#endif
360232337Smav		hdspechan_enable(ch, 1);
361232337Smav		hdspechan_setgain(ch);
362232337Smav		hdspe_start_audio(sc);
363232337Smav		break;
364232337Smav
365232337Smav	case PCMTRIG_STOP:
366232337Smav	case PCMTRIG_ABORT:
367232337Smav#if 0
368232337Smav		device_printf(scp->dev, "hdspechan_trigger(): stop or abort\n");
369232337Smav#endif
370232337Smav		clean(ch);
371232337Smav		hdspechan_enable(ch, 0);
372232337Smav		hdspe_stop_audio(sc);
373232337Smav		break;
374232337Smav
375232337Smav	case PCMTRIG_EMLDMAWR:
376232337Smav	case PCMTRIG_EMLDMARD:
377232337Smav		if(ch->run)
378232337Smav			buffer_copy(ch);
379232337Smav		break;
380232337Smav	}
381232337Smav
382232337Smav	snd_mtxunlock(sc->lock);
383232337Smav
384232337Smav	return 0;
385232337Smav}
386232337Smav
387232337Smavstatic uint32_t
388232337Smavhdspechan_getptr(kobj_t obj, void *data)
389232337Smav{
390232337Smav	struct sc_chinfo *ch = data;
391232337Smav	struct sc_pcminfo *scp = ch->parent;
392232337Smav	struct sc_info *sc = scp->sc;
393232337Smav	uint32_t ret, pos;
394232337Smav
395232337Smav	snd_mtxlock(sc->lock);
396232337Smav	ret = hdspe_read_2(sc, HDSPE_STATUS_REG);
397232337Smav	snd_mtxunlock(sc->lock);
398232337Smav
399232337Smav	pos = ret & HDSPE_BUF_POSITION_MASK;
400232337Smav	pos *= 2; /* Hardbuf twice bigger. */
401232337Smav
402232337Smav	return pos;
403232337Smav}
404232337Smav
405232337Smavstatic int
406232337Smavhdspechan_free(kobj_t obj, void *data)
407232337Smav{
408232337Smav	struct sc_chinfo *ch = data;
409232337Smav	struct sc_pcminfo *scp = ch->parent;
410232337Smav	struct sc_info *sc = scp->sc;
411232337Smav
412232337Smav#if 0
413232337Smav	device_printf(scp->dev, "hdspechan_free()\n");
414232337Smav#endif
415232337Smav	snd_mtxlock(sc->lock);
416232337Smav	if (ch->data != NULL) {
417232337Smav		free(ch->data, M_HDSPE);
418232337Smav		ch->data = NULL;
419232337Smav	}
420232337Smav	snd_mtxunlock(sc->lock);
421232337Smav
422232337Smav	return 0;
423232337Smav}
424232337Smav
425232337Smavstatic int
426232337Smavhdspechan_setformat(kobj_t obj, void *data, uint32_t format)
427232337Smav{
428232337Smav	struct sc_chinfo *ch = data;
429232337Smav
430232337Smav#if 0
431232337Smav	struct sc_pcminfo *scp = ch->parent;
432232337Smav	device_printf(scp->dev, "hdspechan_setformat(%d)\n", format);
433232337Smav#endif
434232337Smav
435232337Smav	ch->format = format;
436232337Smav
437232337Smav	return 0;
438232337Smav}
439232337Smav
440232337Smavstatic uint32_t
441232337Smavhdspechan_setspeed(kobj_t obj, void *data, uint32_t speed)
442232337Smav{
443232337Smav	struct sc_chinfo *ch = data;
444232337Smav	struct sc_pcminfo *scp = ch->parent;
445232337Smav	struct sc_info *sc = scp->sc;
446232337Smav	struct hdspe_rate *hr = NULL;
447232337Smav	long long period;
448232337Smav	int threshold;
449232337Smav	int i;
450232337Smav
451232337Smav#if 0
452232337Smav	device_printf(scp->dev, "hdspechan_setspeed(%d)\n", speed);
453232337Smav#endif
454232337Smav
455232337Smav	if (hdspe_running(sc) == 1)
456232337Smav		goto end;
457232337Smav
458232337Smav	/* First look for equal frequency. */
459232337Smav	for (i = 0; rate_map[i].speed != 0; i++) {
460232337Smav		if (rate_map[i].speed == speed)
461232337Smav			hr = &rate_map[i];
462232337Smav	}
463232337Smav
464232337Smav	/* If no match, just find nearest. */
465232337Smav	if (hr == NULL) {
466232337Smav		for (i = 0; rate_map[i].speed != 0; i++) {
467232337Smav			hr = &rate_map[i];
468232337Smav			threshold = hr->speed + ((rate_map[i + 1].speed != 0) ?
469232337Smav			    ((rate_map[i + 1].speed - hr->speed) >> 1) : 0);
470232337Smav			if (speed < threshold)
471232337Smav				break;
472232337Smav		}
473232337Smav	}
474232337Smav
475232337Smav	switch (sc->type) {
476232337Smav	case RAYDAT:
477232337Smav	case AIO:
478232337Smav		period = HDSPE_FREQ_AIO;
479232337Smav		break;
480232337Smav	default:
481232337Smav		/* Unsupported card. */
482232337Smav		goto end;
483232337Smav	}
484232337Smav
485232337Smav	/* Write frequency on the device. */
486232337Smav	sc->ctrl_register &= ~HDSPE_FREQ_MASK;
487232337Smav	sc->ctrl_register |= hr->reg;
488232337Smav	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
489232337Smav
490232337Smav	speed = hr->speed;
491232337Smav	if (speed > 96000)
492232337Smav		speed /= 4;
493232337Smav	else if (speed > 48000)
494232337Smav		speed /= 2;
495232337Smav
496232337Smav	/* Set DDS value. */
497232337Smav	period /= speed;
498232337Smav	hdspe_write_4(sc, HDSPE_FREQ_REG, period);
499232337Smav
500232337Smav	sc->speed = hr->speed;
501232337Smavend:
502232337Smav	return sc->speed;
503232337Smav}
504232337Smav
505232337Smavstatic uint32_t
506232337Smavhdspechan_setblocksize(kobj_t obj, void *data, uint32_t blocksize)
507232337Smav{
508232337Smav	struct sc_chinfo *ch = data;
509232337Smav	struct sc_pcminfo *scp = ch->parent;
510232337Smav	struct sc_info *sc = scp->sc;
511232337Smav	struct hdspe_latency *hl = NULL;
512232337Smav	int threshold;
513232337Smav	int i;
514232337Smav
515232337Smav#if 0
516232337Smav	device_printf(scp->dev, "hdspechan_setblocksize(%d)\n", blocksize);
517232337Smav#endif
518232337Smav
519232337Smav	if (hdspe_running(sc) == 1)
520232337Smav		goto end;
521232337Smav
522232337Smav	if (blocksize > HDSPE_LAT_BYTES_MAX)
523232337Smav		blocksize = HDSPE_LAT_BYTES_MAX;
524232337Smav	else if (blocksize < HDSPE_LAT_BYTES_MIN)
525232337Smav		blocksize = HDSPE_LAT_BYTES_MIN;
526232337Smav
527232337Smav	blocksize /= 4 /* samples */;
528232337Smav
529232337Smav	/* First look for equal latency. */
530232337Smav	for (i = 0; latency_map[i].period != 0; i++) {
531232337Smav		if (latency_map[i].period == blocksize) {
532232337Smav			hl = &latency_map[i];
533232337Smav		}
534232337Smav	}
535232337Smav
536232337Smav	/* If no match, just find nearest. */
537232337Smav	if (hl == NULL) {
538232337Smav		for (i = 0; latency_map[i].period != 0; i++) {
539232337Smav			hl = &latency_map[i];
540232337Smav			threshold = hl->period + ((latency_map[i + 1].period != 0) ?
541232337Smav			    ((latency_map[i + 1].period - hl->period) >> 1) : 0);
542232337Smav			if (blocksize < threshold)
543232337Smav				break;
544232337Smav		}
545232337Smav	}
546232337Smav
547232337Smav	snd_mtxlock(sc->lock);
548232337Smav	sc->ctrl_register &= ~HDSPE_LAT_MASK;
549232337Smav	sc->ctrl_register |= hdspe_encode_latency(hl->n);
550232337Smav	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
551232337Smav	sc->period = hl->period;
552232337Smav	snd_mtxunlock(sc->lock);
553232337Smav
554232337Smav#if 0
555232337Smav	device_printf(scp->dev, "New period=%d\n", sc->period);
556232337Smav#endif
557232337Smav
558232337Smav	sndbuf_resize(ch->buffer, (HDSPE_CHANBUF_SIZE * 2) / (sc->period * 4),
559232337Smav	    (sc->period * 4));
560232337Smavend:
561232337Smav	return sndbuf_getblksz(ch->buffer);
562232337Smav}
563232337Smav
564232337Smavstatic uint32_t hdspe_rfmt[] = {
565232337Smav	SND_FORMAT(AFMT_S32_LE, 2, 0),
566232337Smav	0
567232337Smav};
568232337Smav
569232337Smavstatic struct pcmchan_caps hdspe_rcaps = {32000, 192000, hdspe_rfmt, 0};
570232337Smav
571232337Smavstatic uint32_t hdspe_pfmt[] = {
572232337Smav	SND_FORMAT(AFMT_S32_LE, 2, 0),
573232337Smav	0
574232337Smav};
575232337Smav
576232337Smavstatic struct pcmchan_caps hdspe_pcaps = {32000, 192000, hdspe_pfmt, 0};
577232337Smav
578232337Smavstatic struct pcmchan_caps *
579232337Smavhdspechan_getcaps(kobj_t obj, void *data)
580232337Smav{
581232337Smav	struct sc_chinfo *ch = data;
582232337Smav
583232337Smav#if 0
584232337Smav	struct sc_pcminfo *scl = ch->parent;
585232337Smav	device_printf(scp->dev, "hdspechan_getcaps()\n");
586232337Smav#endif
587232337Smav
588232337Smav	return (ch->dir == PCMDIR_PLAY) ?
589232337Smav	    &hdspe_pcaps : &hdspe_rcaps;
590232337Smav}
591232337Smav
592232337Smavstatic kobj_method_t hdspechan_methods[] = {
593232337Smav	KOBJMETHOD(channel_init,         hdspechan_init),
594232337Smav	KOBJMETHOD(channel_free,         hdspechan_free),
595232337Smav	KOBJMETHOD(channel_setformat,    hdspechan_setformat),
596232337Smav	KOBJMETHOD(channel_setspeed,     hdspechan_setspeed),
597232337Smav	KOBJMETHOD(channel_setblocksize, hdspechan_setblocksize),
598232337Smav	KOBJMETHOD(channel_trigger,      hdspechan_trigger),
599232337Smav	KOBJMETHOD(channel_getptr,       hdspechan_getptr),
600232337Smav	KOBJMETHOD(channel_getcaps,      hdspechan_getcaps),
601232337Smav	KOBJMETHOD_END
602232337Smav};
603232337SmavCHANNEL_DECLARE(hdspechan);
604232337Smav
605232337Smav
606232337Smavstatic int
607232337Smavhdspe_pcm_probe(device_t dev)
608232337Smav{
609232337Smav
610232337Smav#if 0
611232337Smav	device_printf(dev,"hdspe_pcm_probe()\n");
612232337Smav#endif
613232337Smav
614232337Smav	return 0;
615232337Smav}
616232337Smav
617232337Smavstatic uint32_t
618232337Smavhdspe_pcm_intr(struct sc_pcminfo *scp) {
619232337Smav	struct sc_chinfo *ch;
620232337Smav	struct sc_info *sc = scp->sc;
621232337Smav	int i;
622232337Smav
623232337Smav	for (i = 0; i < scp->chnum; i++) {
624232337Smav		ch = &scp->chan[i];
625232337Smav		snd_mtxunlock(sc->lock);
626232337Smav		chn_intr(ch->channel);
627232337Smav		snd_mtxlock(sc->lock);
628232337Smav	}
629232337Smav
630232337Smav	return 0;
631232337Smav}
632232337Smav
633232337Smavstatic int
634232337Smavhdspe_pcm_attach(device_t dev)
635232337Smav{
636232337Smav	struct sc_pcminfo *scp;
637232337Smav	char status[SND_STATUSLEN];
638232337Smav	char desc[64];
639232337Smav	int i, err;
640232337Smav
641232337Smav	scp = device_get_ivars(dev);
642232337Smav	scp->ih = &hdspe_pcm_intr;
643232337Smav
644232337Smav	bzero(desc, sizeof(desc));
645232337Smav	snprintf(desc, sizeof(desc), "HDSPe AIO [%s]", scp->hc->descr);
646232337Smav	device_set_desc_copy(dev, desc);
647232337Smav
648232337Smav	/*
649232337Smav	 * We don't register interrupt handler with snd_setup_intr
650232337Smav	 * in pcm device. Mark pcm device as MPSAFE manually.
651232337Smav	 */
652232337Smav	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
653232337Smav
654232337Smav	err = pcm_register(dev, scp, scp->hc->play, scp->hc->rec);
655232337Smav	if (err) {
656232337Smav		device_printf(dev, "Can't register pcm.\n");
657232337Smav		return ENXIO;
658232337Smav	}
659232337Smav
660232337Smav	scp->chnum = 0;
661232337Smav	for (i = 0; i < scp->hc->play; i++) {
662232337Smav		pcm_addchan(dev, PCMDIR_PLAY, &hdspechan_class, scp);
663232337Smav		scp->chnum++;
664232337Smav	}
665232337Smav
666232337Smav	for (i = 0; i < scp->hc->rec; i++) {
667232337Smav		pcm_addchan(dev, PCMDIR_REC, &hdspechan_class, scp);
668232337Smav		scp->chnum++;
669232337Smav	}
670232337Smav
671232337Smav	snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld %s",
672232337Smav	    rman_get_start(scp->sc->cs),
673232337Smav	    rman_get_start(scp->sc->irq),
674232337Smav	    PCM_KLDSTRING(snd_hdspe));
675232337Smav	pcm_setstatus(dev, status);
676232337Smav
677232337Smav	mixer_init(dev, &hdspemixer_class, scp);
678232337Smav
679232337Smav	return 0;
680232337Smav}
681232337Smav
682232337Smavstatic int
683232337Smavhdspe_pcm_detach(device_t dev)
684232337Smav{
685232337Smav	int err;
686232337Smav
687232337Smav	err = pcm_unregister(dev);
688232337Smav	if (err) {
689232337Smav		device_printf(dev, "Can't unregister device.\n");
690232337Smav		return err;
691232337Smav	}
692232337Smav
693232337Smav	return 0;
694232337Smav}
695232337Smav
696232337Smavstatic device_method_t hdspe_pcm_methods[] = {
697232337Smav	DEVMETHOD(device_probe,     hdspe_pcm_probe),
698232337Smav	DEVMETHOD(device_attach,    hdspe_pcm_attach),
699232337Smav	DEVMETHOD(device_detach,    hdspe_pcm_detach),
700232337Smav	{ 0, 0 }
701232337Smav};
702232337Smav
703232337Smavstatic driver_t hdspe_pcm_driver = {
704232337Smav	"pcm",
705232337Smav	hdspe_pcm_methods,
706232337Smav	PCM_SOFTC_SIZE,
707232337Smav};
708232337Smav
709232337SmavDRIVER_MODULE(snd_hdspe_pcm, hdspe, hdspe_pcm_driver, pcm_devclass, 0, 0);
710232337SmavMODULE_DEPEND(snd_hdspe, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
711232337SmavMODULE_VERSION(snd_hdspe, 1);
712