xref: /freebsd/sys/dev/sound/pci/hdspe.c (revision 9e7e15b539a6f3ef98854a47ee346c9100875c8a)
1e4afd792SAlexander Motin /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
420a9f771SRuslan Bukin  * Copyright (c) 2012-2016 Ruslan Bukin <br@bsdpad.com>
5d7fde2c9SFlorian Walpen  * Copyright (c) 2023-2024 Florian Walpen <dev@submerge.ch>
6e4afd792SAlexander Motin  * All rights reserved.
7e4afd792SAlexander Motin  *
8e4afd792SAlexander Motin  * Redistribution and use in source and binary forms, with or without
9e4afd792SAlexander Motin  * modification, are permitted provided that the following conditions
10e4afd792SAlexander Motin  * are met:
11e4afd792SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
12e4afd792SAlexander Motin  *    notice, this list of conditions and the following disclaimer.
13e4afd792SAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
14e4afd792SAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
15e4afd792SAlexander Motin  *    documentation and/or other materials provided with the distribution.
16e4afd792SAlexander Motin  *
17e4afd792SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18e4afd792SAlexander Motin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19e4afd792SAlexander Motin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20e4afd792SAlexander Motin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21e4afd792SAlexander Motin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22e4afd792SAlexander Motin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23e4afd792SAlexander Motin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24e4afd792SAlexander Motin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25e4afd792SAlexander Motin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26e4afd792SAlexander Motin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27e4afd792SAlexander Motin  * SUCH DAMAGE.
28e4afd792SAlexander Motin  */
29e4afd792SAlexander Motin 
30e4afd792SAlexander Motin /*
31e4afd792SAlexander Motin  * RME HDSPe driver for FreeBSD.
32e4afd792SAlexander Motin  * Supported cards: AIO, RayDAT.
33e4afd792SAlexander Motin  */
34e4afd792SAlexander Motin 
35b6052c10SRuslan Bukin #include <sys/types.h>
36b6052c10SRuslan Bukin #include <sys/sysctl.h>
37b6052c10SRuslan Bukin 
38e4afd792SAlexander Motin #include <dev/sound/pcm/sound.h>
39e4afd792SAlexander Motin #include <dev/sound/pci/hdspe.h>
40e4afd792SAlexander Motin 
41e4afd792SAlexander Motin #include <dev/pci/pcireg.h>
42e4afd792SAlexander Motin #include <dev/pci/pcivar.h>
43e4afd792SAlexander Motin 
44e4afd792SAlexander Motin #include <mixer_if.h>
45e4afd792SAlexander Motin 
46dc15f025SFlorian Walpen static bool hdspe_unified_pcm = false;
47dc15f025SFlorian Walpen 
48dc15f025SFlorian Walpen static SYSCTL_NODE(_hw, OID_AUTO, hdspe, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
49dc15f025SFlorian Walpen     "PCI HDSPe");
50dc15f025SFlorian Walpen 
51dc15f025SFlorian Walpen SYSCTL_BOOL(_hw_hdspe, OID_AUTO, unified_pcm, CTLFLAG_RWTUN,
52dc15f025SFlorian Walpen     &hdspe_unified_pcm, 0, "Combine physical ports in one unified pcm device");
53dc15f025SFlorian Walpen 
54b6052c10SRuslan Bukin static struct hdspe_clock_source hdspe_clock_source_table_rd[] = {
55b6052c10SRuslan Bukin 	{ "internal", 0 << 1 | 1, HDSPE_STATUS1_CLOCK(15),       0,       0 },
56b6052c10SRuslan Bukin 	{ "word",     0 << 1 | 0, HDSPE_STATUS1_CLOCK( 0), 1 << 24, 1 << 25 },
57b6052c10SRuslan Bukin 	{ "aes",      1 << 1 | 0, HDSPE_STATUS1_CLOCK( 1),  1 << 0,  1 << 8 },
58b6052c10SRuslan Bukin 	{ "spdif",    2 << 1 | 0, HDSPE_STATUS1_CLOCK( 2),  1 << 1,  1 << 9 },
59b6052c10SRuslan Bukin 	{ "adat1",    3 << 1 | 0, HDSPE_STATUS1_CLOCK( 3),  1 << 2, 1 << 10 },
60b6052c10SRuslan Bukin 	{ "adat2",    4 << 1 | 0, HDSPE_STATUS1_CLOCK( 4),  1 << 3, 1 << 11 },
61b6052c10SRuslan Bukin 	{ "adat3",    5 << 1 | 0, HDSPE_STATUS1_CLOCK( 5),  1 << 4, 1 << 12 },
62b6052c10SRuslan Bukin 	{ "adat4",    6 << 1 | 0, HDSPE_STATUS1_CLOCK( 6),  1 << 5, 1 << 13 },
63b6052c10SRuslan Bukin 	{ "tco",      9 << 1 | 0, HDSPE_STATUS1_CLOCK( 9), 1 << 26, 1 << 27 },
64b6052c10SRuslan Bukin 	{ "sync_in", 10 << 1 | 0, HDSPE_STATUS1_CLOCK(10),       0,       0 },
65b6052c10SRuslan Bukin 	{ NULL,       0 << 1 | 0, HDSPE_STATUS1_CLOCK( 0),       0,       0 },
66b6052c10SRuslan Bukin };
67b6052c10SRuslan Bukin 
68b6052c10SRuslan Bukin static struct hdspe_clock_source hdspe_clock_source_table_aio[] = {
69b6052c10SRuslan Bukin 	{ "internal", 0 << 1 | 1, HDSPE_STATUS1_CLOCK(15),       0,       0 },
70b6052c10SRuslan Bukin 	{ "word",     0 << 1 | 0, HDSPE_STATUS1_CLOCK( 0), 1 << 24, 1 << 25 },
71b6052c10SRuslan Bukin 	{ "aes",      1 << 1 | 0, HDSPE_STATUS1_CLOCK( 1),  1 << 0,  1 << 8 },
72b6052c10SRuslan Bukin 	{ "spdif",    2 << 1 | 0, HDSPE_STATUS1_CLOCK( 2),  1 << 1,  1 << 9 },
73b6052c10SRuslan Bukin 	{ "adat",     3 << 1 | 0, HDSPE_STATUS1_CLOCK( 3),  1 << 2, 1 << 10 },
74b6052c10SRuslan Bukin 	{ "tco",      9 << 1 | 0, HDSPE_STATUS1_CLOCK( 9), 1 << 26, 1 << 27 },
75b6052c10SRuslan Bukin 	{ "sync_in", 10 << 1 | 0, HDSPE_STATUS1_CLOCK(10),       0,       0 },
76b6052c10SRuslan Bukin 	{ NULL,       0 << 1 | 0, HDSPE_STATUS1_CLOCK( 0),       0,       0 },
77b6052c10SRuslan Bukin };
78b6052c10SRuslan Bukin 
79e4afd792SAlexander Motin static struct hdspe_channel chan_map_aio[] = {
80d7fde2c9SFlorian Walpen 	{ HDSPE_CHAN_AIO_LINE,    "line" },
81*9e7e15b5SRuslan Bukin 	{ HDSPE_CHAN_AIO_EXT,      "ext" },
82d7fde2c9SFlorian Walpen 	{ HDSPE_CHAN_AIO_PHONE,  "phone" },
83d7fde2c9SFlorian Walpen 	{ HDSPE_CHAN_AIO_AES,      "aes" },
84d7fde2c9SFlorian Walpen 	{ HDSPE_CHAN_AIO_SPDIF, "s/pdif" },
85d7fde2c9SFlorian Walpen 	{ HDSPE_CHAN_AIO_ADAT,    "adat" },
86d7fde2c9SFlorian Walpen 	{ 0,                        NULL },
87e4afd792SAlexander Motin };
88e4afd792SAlexander Motin 
89dc15f025SFlorian Walpen static struct hdspe_channel chan_map_aio_uni[] = {
90dc15f025SFlorian Walpen 	{ HDSPE_CHAN_AIO_ALL, "all" },
91dc15f025SFlorian Walpen 	{ 0,                   NULL },
92dc15f025SFlorian Walpen };
93dc15f025SFlorian Walpen 
94e4afd792SAlexander Motin static struct hdspe_channel chan_map_rd[] = {
95d7fde2c9SFlorian Walpen 	{ HDSPE_CHAN_RAY_AES,      "aes" },
96d7fde2c9SFlorian Walpen 	{ HDSPE_CHAN_RAY_SPDIF, "s/pdif" },
97d7fde2c9SFlorian Walpen 	{ HDSPE_CHAN_RAY_ADAT1,  "adat1" },
98d7fde2c9SFlorian Walpen 	{ HDSPE_CHAN_RAY_ADAT2,  "adat2" },
99d7fde2c9SFlorian Walpen 	{ HDSPE_CHAN_RAY_ADAT3,  "adat3" },
100d7fde2c9SFlorian Walpen 	{ HDSPE_CHAN_RAY_ADAT4,  "adat4" },
101d7fde2c9SFlorian Walpen 	{ 0,                        NULL },
102e4afd792SAlexander Motin };
103e4afd792SAlexander Motin 
104dc15f025SFlorian Walpen static struct hdspe_channel chan_map_rd_uni[] = {
105dc15f025SFlorian Walpen 	{ HDSPE_CHAN_RAY_ALL, "all" },
106dc15f025SFlorian Walpen 	{ 0,                   NULL },
107dc15f025SFlorian Walpen };
108dc15f025SFlorian Walpen 
109e4afd792SAlexander Motin static void
110e4afd792SAlexander Motin hdspe_intr(void *p)
111e4afd792SAlexander Motin {
112e4afd792SAlexander Motin 	struct sc_pcminfo *scp;
11320a9f771SRuslan Bukin 	struct sc_info *sc;
114e4afd792SAlexander Motin 	device_t *devlist;
11520a9f771SRuslan Bukin 	int devcount;
11620a9f771SRuslan Bukin 	int status;
11720a9f771SRuslan Bukin 	int err;
11820a9f771SRuslan Bukin 	int i;
11920a9f771SRuslan Bukin 
12020a9f771SRuslan Bukin 	sc = (struct sc_info *)p;
121e4afd792SAlexander Motin 
122e4afd792SAlexander Motin 	snd_mtxlock(sc->lock);
123e4afd792SAlexander Motin 
124e4afd792SAlexander Motin 	status = hdspe_read_1(sc, HDSPE_STATUS_REG);
125e4afd792SAlexander Motin 	if (status & HDSPE_AUDIO_IRQ_PENDING) {
126e4afd792SAlexander Motin 		if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
127e4afd792SAlexander Motin 			return;
128e4afd792SAlexander Motin 
129e4afd792SAlexander Motin 		for (i = 0; i < devcount; i++) {
130e4afd792SAlexander Motin 			scp = device_get_ivars(devlist[i]);
131e4afd792SAlexander Motin 			if (scp->ih != NULL)
132e4afd792SAlexander Motin 				scp->ih(scp);
133e4afd792SAlexander Motin 		}
134e4afd792SAlexander Motin 
135e4afd792SAlexander Motin 		hdspe_write_1(sc, HDSPE_INTERRUPT_ACK, 0);
136b5db12bfSKevin Lo 		free(devlist, M_TEMP);
137e4afd792SAlexander Motin 	}
138e4afd792SAlexander Motin 
139e4afd792SAlexander Motin 	snd_mtxunlock(sc->lock);
140e4afd792SAlexander Motin }
141e4afd792SAlexander Motin 
142e4afd792SAlexander Motin static void
143e4afd792SAlexander Motin hdspe_dmapsetmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
144e4afd792SAlexander Motin {
145e4afd792SAlexander Motin #if 0
146e4afd792SAlexander Motin 	device_printf(sc->dev, "hdspe_dmapsetmap()\n");
147e4afd792SAlexander Motin #endif
148e4afd792SAlexander Motin }
149e4afd792SAlexander Motin 
150e4afd792SAlexander Motin static int
151e4afd792SAlexander Motin hdspe_alloc_resources(struct sc_info *sc)
152e4afd792SAlexander Motin {
153e4afd792SAlexander Motin 
154e4afd792SAlexander Motin 	/* Allocate resource. */
155e4afd792SAlexander Motin 	sc->csid = PCIR_BAR(0);
15643cd6160SJustin Hibbits 	sc->cs = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
15743cd6160SJustin Hibbits 	    &sc->csid, RF_ACTIVE);
158e4afd792SAlexander Motin 
159e4afd792SAlexander Motin 	if (!sc->cs) {
160e4afd792SAlexander Motin 		device_printf(sc->dev, "Unable to map SYS_RES_MEMORY.\n");
161e4afd792SAlexander Motin 		return (ENXIO);
162e4afd792SAlexander Motin 	}
16320a9f771SRuslan Bukin 
164e4afd792SAlexander Motin 	sc->cst = rman_get_bustag(sc->cs);
165e4afd792SAlexander Motin 	sc->csh = rman_get_bushandle(sc->cs);
166e4afd792SAlexander Motin 
167e4afd792SAlexander Motin 	/* Allocate interrupt resource. */
168e4afd792SAlexander Motin 	sc->irqid = 0;
16943cd6160SJustin Hibbits 	sc->irq = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &sc->irqid,
17043cd6160SJustin Hibbits 	    RF_ACTIVE | RF_SHAREABLE);
171e4afd792SAlexander Motin 
172e4afd792SAlexander Motin 	if (!sc->irq ||
173e4afd792SAlexander Motin 	    bus_setup_intr(sc->dev, sc->irq, INTR_MPSAFE | INTR_TYPE_AV,
174e4afd792SAlexander Motin 		NULL, hdspe_intr, sc, &sc->ih)) {
175e4afd792SAlexander Motin 		device_printf(sc->dev, "Unable to alloc interrupt resource.\n");
176e4afd792SAlexander Motin 		return (ENXIO);
177e4afd792SAlexander Motin 	}
178e4afd792SAlexander Motin 
179e4afd792SAlexander Motin 	/* Allocate DMA resources. */
180e4afd792SAlexander Motin 	if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(sc->dev),
181e4afd792SAlexander Motin 		/*alignment*/4,
182e4afd792SAlexander Motin 		/*boundary*/0,
183e4afd792SAlexander Motin 		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
184e4afd792SAlexander Motin 		/*highaddr*/BUS_SPACE_MAXADDR,
185e4afd792SAlexander Motin 		/*filter*/NULL,
186e4afd792SAlexander Motin 		/*filterarg*/NULL,
187e4afd792SAlexander Motin 		/*maxsize*/2 * HDSPE_DMASEGSIZE,
188e4afd792SAlexander Motin 		/*nsegments*/2,
189e4afd792SAlexander Motin 		/*maxsegsz*/HDSPE_DMASEGSIZE,
190e4afd792SAlexander Motin 		/*flags*/0,
1911f7a6325SAlexander Motin 		/*lockfunc*/NULL,
1921f7a6325SAlexander Motin 		/*lockarg*/NULL,
193e4afd792SAlexander Motin 		/*dmatag*/&sc->dmat) != 0) {
194e4afd792SAlexander Motin 		device_printf(sc->dev, "Unable to create dma tag.\n");
195e4afd792SAlexander Motin 		return (ENXIO);
196e4afd792SAlexander Motin 	}
197e4afd792SAlexander Motin 
198e4afd792SAlexander Motin 	sc->bufsize = HDSPE_DMASEGSIZE;
199e4afd792SAlexander Motin 
200e4afd792SAlexander Motin 	/* pbuf (play buffer). */
2011f7a6325SAlexander Motin 	if (bus_dmamem_alloc(sc->dmat, (void **)&sc->pbuf, BUS_DMA_WAITOK,
2021f7a6325SAlexander Motin 	    &sc->pmap)) {
203e4afd792SAlexander Motin 		device_printf(sc->dev, "Can't alloc pbuf.\n");
204e4afd792SAlexander Motin 		return (ENXIO);
205e4afd792SAlexander Motin 	}
206e4afd792SAlexander Motin 
207e4afd792SAlexander Motin 	if (bus_dmamap_load(sc->dmat, sc->pmap, sc->pbuf, sc->bufsize,
2081f7a6325SAlexander Motin 	    hdspe_dmapsetmap, sc, BUS_DMA_NOWAIT)) {
209e4afd792SAlexander Motin 		device_printf(sc->dev, "Can't load pbuf.\n");
210e4afd792SAlexander Motin 		return (ENXIO);
211e4afd792SAlexander Motin 	}
212e4afd792SAlexander Motin 
213e4afd792SAlexander Motin 	/* rbuf (rec buffer). */
2141f7a6325SAlexander Motin 	if (bus_dmamem_alloc(sc->dmat, (void **)&sc->rbuf, BUS_DMA_WAITOK,
2151f7a6325SAlexander Motin 	    &sc->rmap)) {
216e4afd792SAlexander Motin 		device_printf(sc->dev, "Can't alloc rbuf.\n");
217e4afd792SAlexander Motin 		return (ENXIO);
218e4afd792SAlexander Motin 	}
219e4afd792SAlexander Motin 
220e4afd792SAlexander Motin 	if (bus_dmamap_load(sc->dmat, sc->rmap, sc->rbuf, sc->bufsize,
2211f7a6325SAlexander Motin 	    hdspe_dmapsetmap, sc, BUS_DMA_NOWAIT)) {
222e4afd792SAlexander Motin 		device_printf(sc->dev, "Can't load rbuf.\n");
223e4afd792SAlexander Motin 		return (ENXIO);
224e4afd792SAlexander Motin 	}
225e4afd792SAlexander Motin 
226e4afd792SAlexander Motin 	bzero(sc->pbuf, sc->bufsize);
227e4afd792SAlexander Motin 	bzero(sc->rbuf, sc->bufsize);
228e4afd792SAlexander Motin 
229e4afd792SAlexander Motin 	return (0);
230e4afd792SAlexander Motin }
231e4afd792SAlexander Motin 
232e4afd792SAlexander Motin static void
233e4afd792SAlexander Motin hdspe_map_dmabuf(struct sc_info *sc)
234e4afd792SAlexander Motin {
235e4afd792SAlexander Motin 	uint32_t paddr, raddr;
236e4afd792SAlexander Motin 	int i;
237e4afd792SAlexander Motin 
238e4afd792SAlexander Motin 	paddr = vtophys(sc->pbuf);
239e4afd792SAlexander Motin 	raddr = vtophys(sc->rbuf);
240e4afd792SAlexander Motin 
241e4afd792SAlexander Motin 	for (i = 0; i < HDSPE_MAX_SLOTS * 16; i++) {
242e4afd792SAlexander Motin 		hdspe_write_4(sc, HDSPE_PAGE_ADDR_BUF_OUT + 4 * i,
243e4afd792SAlexander Motin                     paddr + i * 4096);
244e4afd792SAlexander Motin 		hdspe_write_4(sc, HDSPE_PAGE_ADDR_BUF_IN + 4 * i,
245e4afd792SAlexander Motin                     raddr + i * 4096);
246e4afd792SAlexander Motin 	}
247e4afd792SAlexander Motin }
248e4afd792SAlexander Motin 
249e4afd792SAlexander Motin static int
2506c892b79SFlorian Walpen hdspe_sysctl_sample_rate(SYSCTL_HANDLER_ARGS)
2516c892b79SFlorian Walpen {
2526c892b79SFlorian Walpen 	struct sc_info *sc = oidp->oid_arg1;
2536c892b79SFlorian Walpen 	int error;
2546c892b79SFlorian Walpen 	unsigned int speed, multiplier;
2556c892b79SFlorian Walpen 
2566c892b79SFlorian Walpen 	speed = sc->force_speed;
2576c892b79SFlorian Walpen 
2586c892b79SFlorian Walpen 	/* Process sysctl (unsigned) integer request. */
2596c892b79SFlorian Walpen 	error = sysctl_handle_int(oidp, &speed, 0, req);
2606c892b79SFlorian Walpen 	if (error != 0 || req->newptr == NULL)
2616c892b79SFlorian Walpen 		return (error);
2626c892b79SFlorian Walpen 
2636c892b79SFlorian Walpen 	/* Speed from 32000 to 192000, 0 falls back to pcm speed setting. */
2646c892b79SFlorian Walpen 	sc->force_speed = 0;
2656c892b79SFlorian Walpen 	if (speed > 0) {
2666c892b79SFlorian Walpen 		multiplier = 1;
2676c892b79SFlorian Walpen 		if (speed > (96000 + 128000) / 2)
2686c892b79SFlorian Walpen 			multiplier = 4;
2696c892b79SFlorian Walpen 		else if (speed > (48000 + 64000) / 2)
2706c892b79SFlorian Walpen 			multiplier = 2;
2716c892b79SFlorian Walpen 
2726c892b79SFlorian Walpen 		if (speed < ((32000 + 44100) / 2) * multiplier)
2736c892b79SFlorian Walpen 			sc->force_speed = 32000 * multiplier;
2746c892b79SFlorian Walpen 		else if (speed < ((44100 + 48000) / 2) * multiplier)
2756c892b79SFlorian Walpen 			sc->force_speed = 44100 * multiplier;
2766c892b79SFlorian Walpen 		else
2776c892b79SFlorian Walpen 			sc->force_speed = 48000 * multiplier;
2786c892b79SFlorian Walpen 	}
2796c892b79SFlorian Walpen 
2806c892b79SFlorian Walpen 	return (0);
2816c892b79SFlorian Walpen }
2826c892b79SFlorian Walpen 
2836c892b79SFlorian Walpen 
2846c892b79SFlorian Walpen static int
285fb877263SFlorian Walpen hdspe_sysctl_period(SYSCTL_HANDLER_ARGS)
286fb877263SFlorian Walpen {
287fb877263SFlorian Walpen 	struct sc_info *sc = oidp->oid_arg1;
288fb877263SFlorian Walpen 	int error;
289fb877263SFlorian Walpen 	unsigned int period;
290fb877263SFlorian Walpen 
291fb877263SFlorian Walpen 	period = sc->force_period;
292fb877263SFlorian Walpen 
293fb877263SFlorian Walpen 	/* Process sysctl (unsigned) integer request. */
294fb877263SFlorian Walpen 	error = sysctl_handle_int(oidp, &period, 0, req);
295fb877263SFlorian Walpen 	if (error != 0 || req->newptr == NULL)
296fb877263SFlorian Walpen 		return (error);
297fb877263SFlorian Walpen 
298fb877263SFlorian Walpen 	/* Period is from 2^5 to 2^14, 0 falls back to pcm latency settings. */
299fb877263SFlorian Walpen 	sc->force_period = 0;
300fb877263SFlorian Walpen 	if (period > 0) {
301fb877263SFlorian Walpen 		sc->force_period = 32;
302fb877263SFlorian Walpen 		while (sc->force_period < period && sc->force_period < 4096)
303fb877263SFlorian Walpen 			sc->force_period <<= 1;
304fb877263SFlorian Walpen 	}
305fb877263SFlorian Walpen 
306fb877263SFlorian Walpen 	return (0);
307fb877263SFlorian Walpen }
308fb877263SFlorian Walpen 
309fb877263SFlorian Walpen static int
310b6052c10SRuslan Bukin hdspe_sysctl_clock_preference(SYSCTL_HANDLER_ARGS)
311b6052c10SRuslan Bukin {
312b6052c10SRuslan Bukin 	struct sc_info *sc;
313b6052c10SRuslan Bukin 	struct hdspe_clock_source *clock_table, *clock;
314b6052c10SRuslan Bukin 	char buf[16] = "invalid";
315b6052c10SRuslan Bukin 	int error;
316b6052c10SRuslan Bukin 	uint32_t setting;
317b6052c10SRuslan Bukin 
318b6052c10SRuslan Bukin 	sc = oidp->oid_arg1;
319b6052c10SRuslan Bukin 
320b6052c10SRuslan Bukin 	/* Select sync ports table for device type. */
321b6052c10SRuslan Bukin 	if (sc->type == HDSPE_AIO)
322b6052c10SRuslan Bukin 		clock_table = hdspe_clock_source_table_aio;
323b6052c10SRuslan Bukin 	else if (sc->type == HDSPE_RAYDAT)
324b6052c10SRuslan Bukin 		clock_table = hdspe_clock_source_table_rd;
325b6052c10SRuslan Bukin 	else
326b6052c10SRuslan Bukin 		return (ENXIO);
327b6052c10SRuslan Bukin 
328b6052c10SRuslan Bukin 	/* Extract preferred clock source from settings register. */
329b6052c10SRuslan Bukin 	setting = sc->settings_register & HDSPE_SETTING_CLOCK_MASK;
330b6052c10SRuslan Bukin 	for (clock = clock_table; clock->name != NULL; ++clock) {
331b6052c10SRuslan Bukin 		if (clock->setting == setting)
332b6052c10SRuslan Bukin 			break;
333b6052c10SRuslan Bukin 	}
334b6052c10SRuslan Bukin 	if (clock->name != NULL)
335b6052c10SRuslan Bukin 		strlcpy(buf, clock->name, sizeof(buf));
336b6052c10SRuslan Bukin 
337b6052c10SRuslan Bukin 	/* Process sysctl string request. */
338b6052c10SRuslan Bukin 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
339b6052c10SRuslan Bukin 	if (error != 0 || req->newptr == NULL)
340b6052c10SRuslan Bukin 		return (error);
341b6052c10SRuslan Bukin 
342b6052c10SRuslan Bukin 	/* Find clock source matching the sysctl string. */
343b6052c10SRuslan Bukin 	for (clock = clock_table; clock->name != NULL; ++clock) {
344b6052c10SRuslan Bukin 		if (strncasecmp(buf, clock->name, sizeof(buf)) == 0)
345b6052c10SRuslan Bukin 			break;
346b6052c10SRuslan Bukin 	}
347b6052c10SRuslan Bukin 
348b6052c10SRuslan Bukin 	/* Set preferred clock source in settings register. */
349b6052c10SRuslan Bukin 	if (clock->name != NULL) {
350b6052c10SRuslan Bukin 		setting = clock->setting & HDSPE_SETTING_CLOCK_MASK;
351b6052c10SRuslan Bukin 		snd_mtxlock(sc->lock);
352b6052c10SRuslan Bukin 		sc->settings_register &= ~HDSPE_SETTING_CLOCK_MASK;
353b6052c10SRuslan Bukin 		sc->settings_register |= setting;
354b6052c10SRuslan Bukin 		hdspe_write_4(sc, HDSPE_SETTINGS_REG, sc->settings_register);
355b6052c10SRuslan Bukin 		snd_mtxunlock(sc->lock);
356b6052c10SRuslan Bukin 	}
357b6052c10SRuslan Bukin 	return (0);
358b6052c10SRuslan Bukin }
359b6052c10SRuslan Bukin 
360b6052c10SRuslan Bukin static int
361b6052c10SRuslan Bukin hdspe_sysctl_clock_source(SYSCTL_HANDLER_ARGS)
362b6052c10SRuslan Bukin {
363b6052c10SRuslan Bukin 	struct sc_info *sc;
364b6052c10SRuslan Bukin 	struct hdspe_clock_source *clock_table, *clock;
365b6052c10SRuslan Bukin 	char buf[16] = "invalid";
366b6052c10SRuslan Bukin 	uint32_t status;
367b6052c10SRuslan Bukin 
368b6052c10SRuslan Bukin 	sc = oidp->oid_arg1;
369b6052c10SRuslan Bukin 
370b6052c10SRuslan Bukin 	/* Select sync ports table for device type. */
371b6052c10SRuslan Bukin 	if (sc->type == HDSPE_AIO)
372b6052c10SRuslan Bukin 		clock_table = hdspe_clock_source_table_aio;
373b6052c10SRuslan Bukin 	else if (sc->type == HDSPE_RAYDAT)
374b6052c10SRuslan Bukin 		clock_table = hdspe_clock_source_table_rd;
375b6052c10SRuslan Bukin 	else
376b6052c10SRuslan Bukin 		return (ENXIO);
377b6052c10SRuslan Bukin 
378b6052c10SRuslan Bukin 	/* Read current (autosync) clock source from status register. */
379b6052c10SRuslan Bukin 	snd_mtxlock(sc->lock);
380b6052c10SRuslan Bukin 	status = hdspe_read_4(sc, HDSPE_STATUS1_REG);
381b6052c10SRuslan Bukin 	status &= HDSPE_STATUS1_CLOCK_MASK;
382b6052c10SRuslan Bukin 	snd_mtxunlock(sc->lock);
383b6052c10SRuslan Bukin 
384b6052c10SRuslan Bukin 	/* Translate status register value to clock source. */
385b6052c10SRuslan Bukin 	for (clock = clock_table; clock->name != NULL; ++clock) {
386b6052c10SRuslan Bukin 		/* In clock master mode, override with internal clock source. */
387b6052c10SRuslan Bukin 		if (sc->settings_register & HDSPE_SETTING_MASTER) {
388b6052c10SRuslan Bukin 			if (clock->setting & HDSPE_SETTING_MASTER)
389b6052c10SRuslan Bukin 				break;
390b6052c10SRuslan Bukin 		} else if (clock->status == status)
391b6052c10SRuslan Bukin 			break;
392b6052c10SRuslan Bukin 	}
393b6052c10SRuslan Bukin 
394b6052c10SRuslan Bukin 	/* Process sysctl string request. */
395b6052c10SRuslan Bukin 	if (clock->name != NULL)
396b6052c10SRuslan Bukin 		strlcpy(buf, clock->name, sizeof(buf));
397b6052c10SRuslan Bukin 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
398b6052c10SRuslan Bukin }
399b6052c10SRuslan Bukin 
400b6052c10SRuslan Bukin static int
401b6052c10SRuslan Bukin hdspe_sysctl_clock_list(SYSCTL_HANDLER_ARGS)
402b6052c10SRuslan Bukin {
403b6052c10SRuslan Bukin 	struct sc_info *sc;
404b6052c10SRuslan Bukin 	struct hdspe_clock_source *clock_table, *clock;
405b6052c10SRuslan Bukin 	char buf[256];
406b6052c10SRuslan Bukin 	int n;
407b6052c10SRuslan Bukin 
408b6052c10SRuslan Bukin 	sc = oidp->oid_arg1;
409b6052c10SRuslan Bukin 	n = 0;
410b6052c10SRuslan Bukin 
411b6052c10SRuslan Bukin 	/* Select clock source table for device type. */
412b6052c10SRuslan Bukin 	if (sc->type == HDSPE_AIO)
413b6052c10SRuslan Bukin 		clock_table = hdspe_clock_source_table_aio;
414b6052c10SRuslan Bukin 	else if (sc->type == HDSPE_RAYDAT)
415b6052c10SRuslan Bukin 		clock_table = hdspe_clock_source_table_rd;
416b6052c10SRuslan Bukin 	else
417b6052c10SRuslan Bukin 		return (ENXIO);
418b6052c10SRuslan Bukin 
419b6052c10SRuslan Bukin 	/* List available clock sources. */
420b6052c10SRuslan Bukin 	buf[0] = 0;
421b6052c10SRuslan Bukin 	for (clock = clock_table; clock->name != NULL; ++clock) {
422b6052c10SRuslan Bukin 		if (n > 0)
423b6052c10SRuslan Bukin 			n += strlcpy(buf + n, ",", sizeof(buf) - n);
424b6052c10SRuslan Bukin 		n += strlcpy(buf + n, clock->name, sizeof(buf) - n);
425b6052c10SRuslan Bukin 	}
426b6052c10SRuslan Bukin 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
427b6052c10SRuslan Bukin }
428b6052c10SRuslan Bukin 
429b6052c10SRuslan Bukin static int
430b6052c10SRuslan Bukin hdspe_sysctl_sync_status(SYSCTL_HANDLER_ARGS)
431b6052c10SRuslan Bukin {
432b6052c10SRuslan Bukin 	struct sc_info *sc;
433b6052c10SRuslan Bukin 	struct hdspe_clock_source *clock_table, *clock;
434b6052c10SRuslan Bukin 	char buf[256];
435b6052c10SRuslan Bukin 	char *state;
436b6052c10SRuslan Bukin 	int n;
437b6052c10SRuslan Bukin 	uint32_t status;
438b6052c10SRuslan Bukin 
439b6052c10SRuslan Bukin 	sc = oidp->oid_arg1;
440b6052c10SRuslan Bukin 	n = 0;
441b6052c10SRuslan Bukin 
442b6052c10SRuslan Bukin 	/* Select sync ports table for device type. */
443b6052c10SRuslan Bukin 	if (sc->type == HDSPE_AIO)
444b6052c10SRuslan Bukin 		clock_table = hdspe_clock_source_table_aio;
445b6052c10SRuslan Bukin 	else if (sc->type == HDSPE_RAYDAT)
446b6052c10SRuslan Bukin 		clock_table = hdspe_clock_source_table_rd;
447b6052c10SRuslan Bukin 	else
448b6052c10SRuslan Bukin 		return (ENXIO);
449b6052c10SRuslan Bukin 
450b6052c10SRuslan Bukin 	/* Read current lock and sync bits from status register. */
451b6052c10SRuslan Bukin 	snd_mtxlock(sc->lock);
452b6052c10SRuslan Bukin 	status = hdspe_read_4(sc, HDSPE_STATUS1_REG);
453b6052c10SRuslan Bukin 	snd_mtxunlock(sc->lock);
454b6052c10SRuslan Bukin 
455b6052c10SRuslan Bukin 	/* List clock sources with lock and sync state. */
456b6052c10SRuslan Bukin 	for (clock = clock_table; clock->name != NULL; ++clock) {
457b6052c10SRuslan Bukin 		if (clock->sync_bit != 0) {
458b6052c10SRuslan Bukin 			if (n > 0)
459b6052c10SRuslan Bukin 				n += strlcpy(buf + n, ",", sizeof(buf) - n);
460b6052c10SRuslan Bukin 			state = "none";
461b6052c10SRuslan Bukin 			if ((clock->sync_bit & status) != 0)
462b6052c10SRuslan Bukin 				state = "sync";
463b6052c10SRuslan Bukin 			else if ((clock->lock_bit & status) != 0)
464b6052c10SRuslan Bukin 				state = "lock";
465b6052c10SRuslan Bukin 			n += snprintf(buf + n, sizeof(buf) - n, "%s(%s)",
466b6052c10SRuslan Bukin 			    clock->name, state);
467b6052c10SRuslan Bukin 		}
468b6052c10SRuslan Bukin 	}
469b6052c10SRuslan Bukin 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
470b6052c10SRuslan Bukin }
471b6052c10SRuslan Bukin 
472b6052c10SRuslan Bukin static int
473e4afd792SAlexander Motin hdspe_probe(device_t dev)
474e4afd792SAlexander Motin {
475e4afd792SAlexander Motin 	uint32_t rev;
476e4afd792SAlexander Motin 
4779718d4abSFlorian Walpen 	if ((pci_get_vendor(dev) == PCI_VENDOR_XILINX ||
4789718d4abSFlorian Walpen 	    pci_get_vendor(dev) == PCI_VENDOR_RME) &&
479e4afd792SAlexander Motin 	    pci_get_device(dev) == PCI_DEVICE_XILINX_HDSPE) {
480e4afd792SAlexander Motin 		rev = pci_get_revid(dev);
481e4afd792SAlexander Motin 		switch (rev) {
482e4afd792SAlexander Motin 		case PCI_REVISION_AIO:
483e4afd792SAlexander Motin 			device_set_desc(dev, "RME HDSPe AIO");
48420a9f771SRuslan Bukin 			return (0);
485e4afd792SAlexander Motin 		case PCI_REVISION_RAYDAT:
486e4afd792SAlexander Motin 			device_set_desc(dev, "RME HDSPe RayDAT");
48720a9f771SRuslan Bukin 			return (0);
488e4afd792SAlexander Motin 		}
489e4afd792SAlexander Motin 	}
490e4afd792SAlexander Motin 
491e4afd792SAlexander Motin 	return (ENXIO);
492e4afd792SAlexander Motin }
493e4afd792SAlexander Motin 
494e4afd792SAlexander Motin static int
495e4afd792SAlexander Motin hdspe_init(struct sc_info *sc)
496e4afd792SAlexander Motin {
497e4afd792SAlexander Motin 	long long period;
498e4afd792SAlexander Motin 
499e4afd792SAlexander Motin 	/* Set latency. */
500e4afd792SAlexander Motin 	sc->period = 32;
501fb877263SFlorian Walpen 	/*
502fb877263SFlorian Walpen 	 * The pcm channel latency settings propagate unreliable blocksizes,
503fb877263SFlorian Walpen 	 * different for recording and playback, and skewed due to rounding
504fb877263SFlorian Walpen 	 * and total buffer size limits.
505fb877263SFlorian Walpen 	 * Force period to a consistent default until these issues are fixed.
506fb877263SFlorian Walpen 	 */
507fb877263SFlorian Walpen 	sc->force_period = 256;
508e4afd792SAlexander Motin 	sc->ctrl_register = hdspe_encode_latency(7);
509e4afd792SAlexander Motin 
510e4afd792SAlexander Motin 	/* Set rate. */
511e4afd792SAlexander Motin 	sc->speed = HDSPE_SPEED_DEFAULT;
5126c892b79SFlorian Walpen 	sc->force_speed = 0;
513e4afd792SAlexander Motin 	sc->ctrl_register &= ~HDSPE_FREQ_MASK;
514e4afd792SAlexander Motin 	sc->ctrl_register |= HDSPE_FREQ_MASK_DEFAULT;
515e4afd792SAlexander Motin 	hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
516e4afd792SAlexander Motin 
517e4afd792SAlexander Motin 	switch (sc->type) {
518b6052c10SRuslan Bukin 	case HDSPE_RAYDAT:
519b6052c10SRuslan Bukin 	case HDSPE_AIO:
520e4afd792SAlexander Motin 		period = HDSPE_FREQ_AIO;
521e4afd792SAlexander Motin 		break;
522e4afd792SAlexander Motin 	default:
523e4afd792SAlexander Motin 		return (ENXIO);
524e4afd792SAlexander Motin 	}
525e4afd792SAlexander Motin 
526e4afd792SAlexander Motin 	/* Set DDS value. */
527e4afd792SAlexander Motin 	period /= sc->speed;
528e4afd792SAlexander Motin 	hdspe_write_4(sc, HDSPE_FREQ_REG, period);
529e4afd792SAlexander Motin 
530e4afd792SAlexander Motin 	/* Other settings. */
531e4afd792SAlexander Motin 	sc->settings_register = 0;
532e4afd792SAlexander Motin 	hdspe_write_4(sc, HDSPE_SETTINGS_REG, sc->settings_register);
533e4afd792SAlexander Motin 
53420a9f771SRuslan Bukin 	return (0);
535e4afd792SAlexander Motin }
536e4afd792SAlexander Motin 
537e4afd792SAlexander Motin static int
538e4afd792SAlexander Motin hdspe_attach(device_t dev)
539e4afd792SAlexander Motin {
540e4afd792SAlexander Motin 	struct hdspe_channel *chan_map;
54120a9f771SRuslan Bukin 	struct sc_pcminfo *scp;
54220a9f771SRuslan Bukin 	struct sc_info *sc;
543e4afd792SAlexander Motin 	uint32_t rev;
544e4afd792SAlexander Motin 	int i, err;
545e4afd792SAlexander Motin 
546e4afd792SAlexander Motin #if 0
547e4afd792SAlexander Motin 	device_printf(dev, "hdspe_attach()\n");
548e4afd792SAlexander Motin #endif
549e4afd792SAlexander Motin 
550e4afd792SAlexander Motin 	sc = device_get_softc(dev);
551e4afd792SAlexander Motin 	sc->lock = snd_mtxcreate(device_get_nameunit(dev),
552e4afd792SAlexander Motin 	    "snd_hdspe softc");
553e4afd792SAlexander Motin 	sc->dev = dev;
554e4afd792SAlexander Motin 
555c68534f1SScott Long 	pci_enable_busmaster(dev);
556e4afd792SAlexander Motin 	rev = pci_get_revid(dev);
557e4afd792SAlexander Motin 	switch (rev) {
558e4afd792SAlexander Motin 	case PCI_REVISION_AIO:
559b6052c10SRuslan Bukin 		sc->type = HDSPE_AIO;
560dc15f025SFlorian Walpen 		chan_map = hdspe_unified_pcm ? chan_map_aio_uni : chan_map_aio;
561e4afd792SAlexander Motin 		break;
562e4afd792SAlexander Motin 	case PCI_REVISION_RAYDAT:
563b6052c10SRuslan Bukin 		sc->type = HDSPE_RAYDAT;
564dc15f025SFlorian Walpen 		chan_map = hdspe_unified_pcm ? chan_map_rd_uni : chan_map_rd;
565e4afd792SAlexander Motin 		break;
566e4afd792SAlexander Motin 	default:
56720a9f771SRuslan Bukin 		return (ENXIO);
568e4afd792SAlexander Motin 	}
569e4afd792SAlexander Motin 
570e4afd792SAlexander Motin 	/* Allocate resources. */
571e4afd792SAlexander Motin 	err = hdspe_alloc_resources(sc);
572e4afd792SAlexander Motin 	if (err) {
573e4afd792SAlexander Motin 		device_printf(dev, "Unable to allocate system resources.\n");
57420a9f771SRuslan Bukin 		return (ENXIO);
575e4afd792SAlexander Motin 	}
576e4afd792SAlexander Motin 
577e4afd792SAlexander Motin 	if (hdspe_init(sc) != 0)
57820a9f771SRuslan Bukin 		return (ENXIO);
579e4afd792SAlexander Motin 
580e4afd792SAlexander Motin 	for (i = 0; i < HDSPE_MAX_CHANS && chan_map[i].descr != NULL; i++) {
581e4afd792SAlexander Motin 		scp = malloc(sizeof(struct sc_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
582e4afd792SAlexander Motin 		scp->hc = &chan_map[i];
583e4afd792SAlexander Motin 		scp->sc = sc;
5845b56413dSWarner Losh 		scp->dev = device_add_child(dev, "pcm", DEVICE_UNIT_ANY);
585e4afd792SAlexander Motin 		device_set_ivars(scp->dev, scp);
586e4afd792SAlexander Motin 	}
587e4afd792SAlexander Motin 
588e4afd792SAlexander Motin 	hdspe_map_dmabuf(sc);
589e4afd792SAlexander Motin 
590b6052c10SRuslan Bukin 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
591b6052c10SRuslan Bukin 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
592b6052c10SRuslan Bukin 	    "sync_status", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
593b6052c10SRuslan Bukin 	    sc, 0, hdspe_sysctl_sync_status, "A",
594b6052c10SRuslan Bukin 	    "List clock source signal lock and sync status");
595b6052c10SRuslan Bukin 
596b6052c10SRuslan Bukin 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
597b6052c10SRuslan Bukin 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
598b6052c10SRuslan Bukin 	    "clock_source", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
599b6052c10SRuslan Bukin 	    sc, 0, hdspe_sysctl_clock_source, "A",
600b6052c10SRuslan Bukin 	    "Currently effective clock source");
601b6052c10SRuslan Bukin 
602b6052c10SRuslan Bukin 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
603b6052c10SRuslan Bukin 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
604b6052c10SRuslan Bukin 	    "clock_preference", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
605b6052c10SRuslan Bukin 	    sc, 0, hdspe_sysctl_clock_preference, "A",
606b6052c10SRuslan Bukin 	    "Set 'internal' (master) or preferred autosync clock source");
607b6052c10SRuslan Bukin 
608b6052c10SRuslan Bukin 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
609b6052c10SRuslan Bukin 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
610b6052c10SRuslan Bukin 	    "clock_list", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
611b6052c10SRuslan Bukin 	    sc, 0, hdspe_sysctl_clock_list, "A",
612b6052c10SRuslan Bukin 	    "List of supported clock sources");
613b6052c10SRuslan Bukin 
614fb877263SFlorian Walpen 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
615fb877263SFlorian Walpen 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
616fb877263SFlorian Walpen 	    "period", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
617fb877263SFlorian Walpen 	    sc, 0, hdspe_sysctl_period, "A",
618fb877263SFlorian Walpen 	    "Force period of samples per interrupt (32, 64, ... 4096)");
619fb877263SFlorian Walpen 
6206c892b79SFlorian Walpen 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6216c892b79SFlorian Walpen 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6226c892b79SFlorian Walpen 	    "sample_rate", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
6236c892b79SFlorian Walpen 	    sc, 0, hdspe_sysctl_sample_rate, "A",
6246c892b79SFlorian Walpen 	    "Force sample rate (32000, 44100, 48000, ... 192000)");
6256c892b79SFlorian Walpen 
62635d393bfSGleb Smirnoff 	return (bus_generic_attach(dev));
627e4afd792SAlexander Motin }
628e4afd792SAlexander Motin 
629e4afd792SAlexander Motin static void
630e4afd792SAlexander Motin hdspe_dmafree(struct sc_info *sc)
631e4afd792SAlexander Motin {
632e4afd792SAlexander Motin 
633e4afd792SAlexander Motin 	bus_dmamap_unload(sc->dmat, sc->rmap);
634e4afd792SAlexander Motin 	bus_dmamap_unload(sc->dmat, sc->pmap);
635e4afd792SAlexander Motin 	bus_dmamem_free(sc->dmat, sc->rbuf, sc->rmap);
636e4afd792SAlexander Motin 	bus_dmamem_free(sc->dmat, sc->pbuf, sc->pmap);
637e4afd792SAlexander Motin 	sc->rbuf = sc->pbuf = NULL;
638e4afd792SAlexander Motin }
639e4afd792SAlexander Motin 
640e4afd792SAlexander Motin static int
641e4afd792SAlexander Motin hdspe_detach(device_t dev)
642e4afd792SAlexander Motin {
643e4afd792SAlexander Motin 	struct sc_info *sc;
644e4afd792SAlexander Motin 	int err;
645e4afd792SAlexander Motin 
646e4afd792SAlexander Motin 	sc = device_get_softc(dev);
647e4afd792SAlexander Motin 	if (sc == NULL) {
648e4afd792SAlexander Motin 		device_printf(dev,"Can't detach: softc is null.\n");
64920a9f771SRuslan Bukin 		return (0);
650e4afd792SAlexander Motin 	}
651e4afd792SAlexander Motin 
652e4afd792SAlexander Motin 	err = device_delete_children(dev);
653e4afd792SAlexander Motin 	if (err)
654e4afd792SAlexander Motin 		return (err);
655e4afd792SAlexander Motin 
656e4afd792SAlexander Motin 	hdspe_dmafree(sc);
657e4afd792SAlexander Motin 
658e4afd792SAlexander Motin 	if (sc->ih)
659e4afd792SAlexander Motin 		bus_teardown_intr(dev, sc->irq, sc->ih);
660e4afd792SAlexander Motin 	if (sc->dmat)
661e4afd792SAlexander Motin 		bus_dma_tag_destroy(sc->dmat);
662e4afd792SAlexander Motin 	if (sc->irq)
663e4afd792SAlexander Motin 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
664e4afd792SAlexander Motin 	if (sc->cs)
665e4afd792SAlexander Motin 		bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(0), sc->cs);
666e4afd792SAlexander Motin 	if (sc->lock)
667e4afd792SAlexander Motin 		snd_mtxfree(sc->lock);
668e4afd792SAlexander Motin 
66920a9f771SRuslan Bukin 	return (0);
670e4afd792SAlexander Motin }
671e4afd792SAlexander Motin 
672e4afd792SAlexander Motin static device_method_t hdspe_methods[] = {
673e4afd792SAlexander Motin 	DEVMETHOD(device_probe,     hdspe_probe),
674e4afd792SAlexander Motin 	DEVMETHOD(device_attach,    hdspe_attach),
675e4afd792SAlexander Motin 	DEVMETHOD(device_detach,    hdspe_detach),
676e4afd792SAlexander Motin 	{ 0, 0 }
677e4afd792SAlexander Motin };
678e4afd792SAlexander Motin 
679e4afd792SAlexander Motin static driver_t hdspe_driver = {
680e4afd792SAlexander Motin 	"hdspe",
681e4afd792SAlexander Motin 	hdspe_methods,
682e4afd792SAlexander Motin 	PCM_SOFTC_SIZE,
683e4afd792SAlexander Motin };
684e4afd792SAlexander Motin 
6853390adfeSJohn Baldwin DRIVER_MODULE(snd_hdspe, pci, hdspe_driver, 0, 0);
686