FreeBSD kernel sound device code
hdspe.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012-2016 Ruslan Bukin <br@bsdpad.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
30 * RME HDSPe driver for FreeBSD.
31 * Supported cards: AIO, RayDAT.
32 */
33
34#include <dev/sound/pcm/sound.h>
35#include <dev/sound/pci/hdspe.h>
36#include <dev/sound/chip.h>
37
38#include <dev/pci/pcireg.h>
39#include <dev/pci/pcivar.h>
40
41#include <mixer_if.h>
42
43SND_DECLARE_FILE("$FreeBSD$");
44
45static struct hdspe_channel chan_map_aio[] = {
46 { 0, 1, "line", 1, 1 },
47 { 6, 7, "phone", 1, 0 },
48 { 8, 9, "aes", 1, 1 },
49 { 10, 11, "s/pdif", 1, 1 },
50 { 12, 16, "adat", 1, 1 },
51
52 /* Single or double speed. */
53 { 14, 18, "adat", 1, 1 },
54
55 /* Single speed only. */
56 { 13, 15, "adat", 1, 1 },
57 { 17, 19, "adat", 1, 1 },
58
59 { 0, 0, NULL, 0, 0 },
60};
61
62static struct hdspe_channel chan_map_rd[] = {
63 { 0, 1, "aes", 1, 1 },
64 { 2, 3, "s/pdif", 1, 1 },
65 { 4, 5, "adat", 1, 1 },
66 { 6, 7, "adat", 1, 1 },
67 { 8, 9, "adat", 1, 1 },
68 { 10, 11, "adat", 1, 1 },
69
70 /* Single or double speed. */
71 { 12, 13, "adat", 1, 1 },
72 { 14, 15, "adat", 1, 1 },
73 { 16, 17, "adat", 1, 1 },
74 { 18, 19, "adat", 1, 1 },
75
76 /* Single speed only. */
77 { 20, 21, "adat", 1, 1 },
78 { 22, 23, "adat", 1, 1 },
79 { 24, 25, "adat", 1, 1 },
80 { 26, 27, "adat", 1, 1 },
81 { 28, 29, "adat", 1, 1 },
82 { 30, 31, "adat", 1, 1 },
83 { 32, 33, "adat", 1, 1 },
84 { 34, 35, "adat", 1, 1 },
85
86 { 0, 0, NULL, 0, 0 },
87};
88
89static void
90hdspe_intr(void *p)
91{
92 struct sc_pcminfo *scp;
93 struct sc_info *sc;
94 device_t *devlist;
95 int devcount;
96 int status;
97 int err;
98 int i;
99
100 sc = (struct sc_info *)p;
101
102 snd_mtxlock(sc->lock);
103
106 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
107 return;
108
109 for (i = 0; i < devcount; i++) {
110 scp = device_get_ivars(devlist[i]);
111 if (scp->ih != NULL)
112 scp->ih(scp);
113 }
114
116 free(devlist, M_TEMP);
117 }
118
119 snd_mtxunlock(sc->lock);
120}
121
122static void
123hdspe_dmapsetmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
124{
125 struct sc_info *sc;
126
127 sc = (struct sc_info *)arg;
128
129#if 0
130 device_printf(sc->dev, "hdspe_dmapsetmap()\n");
131#endif
132}
133
134static int
136{
137
138 /* Allocate resource. */
139 sc->csid = PCIR_BAR(0);
140 sc->cs = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
141 &sc->csid, RF_ACTIVE);
142
143 if (!sc->cs) {
144 device_printf(sc->dev, "Unable to map SYS_RES_MEMORY.\n");
145 return (ENXIO);
146 }
147
148 sc->cst = rman_get_bustag(sc->cs);
149 sc->csh = rman_get_bushandle(sc->cs);
150
151 /* Allocate interrupt resource. */
152 sc->irqid = 0;
153 sc->irq = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &sc->irqid,
154 RF_ACTIVE | RF_SHAREABLE);
155
156 if (!sc->irq ||
157 bus_setup_intr(sc->dev, sc->irq, INTR_MPSAFE | INTR_TYPE_AV,
158 NULL, hdspe_intr, sc, &sc->ih)) {
159 device_printf(sc->dev, "Unable to alloc interrupt resource.\n");
160 return (ENXIO);
161 }
162
163 /* Allocate DMA resources. */
164 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(sc->dev),
165 /*alignment*/4,
166 /*boundary*/0,
167 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
168 /*highaddr*/BUS_SPACE_MAXADDR,
169 /*filter*/NULL,
170 /*filterarg*/NULL,
171 /*maxsize*/2 * HDSPE_DMASEGSIZE,
172 /*nsegments*/2,
173 /*maxsegsz*/HDSPE_DMASEGSIZE,
174 /*flags*/0,
175 /*lockfunc*/NULL,
176 /*lockarg*/NULL,
177 /*dmatag*/&sc->dmat) != 0) {
178 device_printf(sc->dev, "Unable to create dma tag.\n");
179 return (ENXIO);
180 }
181
183
184 /* pbuf (play buffer). */
185 if (bus_dmamem_alloc(sc->dmat, (void **)&sc->pbuf, BUS_DMA_WAITOK,
186 &sc->pmap)) {
187 device_printf(sc->dev, "Can't alloc pbuf.\n");
188 return (ENXIO);
189 }
190
191 if (bus_dmamap_load(sc->dmat, sc->pmap, sc->pbuf, sc->bufsize,
192 hdspe_dmapsetmap, sc, BUS_DMA_NOWAIT)) {
193 device_printf(sc->dev, "Can't load pbuf.\n");
194 return (ENXIO);
195 }
196
197 /* rbuf (rec buffer). */
198 if (bus_dmamem_alloc(sc->dmat, (void **)&sc->rbuf, BUS_DMA_WAITOK,
199 &sc->rmap)) {
200 device_printf(sc->dev, "Can't alloc rbuf.\n");
201 return (ENXIO);
202 }
203
204 if (bus_dmamap_load(sc->dmat, sc->rmap, sc->rbuf, sc->bufsize,
205 hdspe_dmapsetmap, sc, BUS_DMA_NOWAIT)) {
206 device_printf(sc->dev, "Can't load rbuf.\n");
207 return (ENXIO);
208 }
209
210 bzero(sc->pbuf, sc->bufsize);
211 bzero(sc->rbuf, sc->bufsize);
212
213 return (0);
214}
215
216static void
218{
219 uint32_t paddr, raddr;
220 int i;
221
222 paddr = vtophys(sc->pbuf);
223 raddr = vtophys(sc->rbuf);
224
225 for (i = 0; i < HDSPE_MAX_SLOTS * 16; i++) {
227 paddr + i * 4096);
229 raddr + i * 4096);
230 }
231}
232
233static int
235{
236 uint32_t rev;
237
238 if (pci_get_vendor(dev) == PCI_VENDOR_XILINX &&
239 pci_get_device(dev) == PCI_DEVICE_XILINX_HDSPE) {
240 rev = pci_get_revid(dev);
241 switch (rev) {
242 case PCI_REVISION_AIO:
243 device_set_desc(dev, "RME HDSPe AIO");
244 return (0);
246 device_set_desc(dev, "RME HDSPe RayDAT");
247 return (0);
248 }
249 }
250
251 return (ENXIO);
252}
253
254static int
256{
257 long long period;
258
259 /* Set defaults. */
261
262 /* Set latency. */
263 sc->period = 32;
265
266 /* Set rate. */
268 sc->ctrl_register &= ~HDSPE_FREQ_MASK;
271
272 switch (sc->type) {
273 case RAYDAT:
274 case AIO:
276 break;
277 default:
278 return (ENXIO);
279 }
280
281 /* Set DDS value. */
282 period /= sc->speed;
284
285 /* Other settings. */
286 sc->settings_register = 0;
288
289 return (0);
290}
291
292static int
294{
295 struct hdspe_channel *chan_map;
296 struct sc_pcminfo *scp;
297 struct sc_info *sc;
298 uint32_t rev;
299 int i, err;
300
301#if 0
302 device_printf(dev, "hdspe_attach()\n");
303#endif
304
305 sc = device_get_softc(dev);
306 sc->lock = snd_mtxcreate(device_get_nameunit(dev),
307 "snd_hdspe softc");
308 sc->dev = dev;
309
310 pci_enable_busmaster(dev);
311 rev = pci_get_revid(dev);
312 switch (rev) {
313 case PCI_REVISION_AIO:
314 sc->type = AIO;
315 chan_map = chan_map_aio;
316 break;
318 sc->type = RAYDAT;
319 chan_map = chan_map_rd;
320 break;
321 default:
322 return (ENXIO);
323 }
324
325 /* Allocate resources. */
326 err = hdspe_alloc_resources(sc);
327 if (err) {
328 device_printf(dev, "Unable to allocate system resources.\n");
329 return (ENXIO);
330 }
331
332 if (hdspe_init(sc) != 0)
333 return (ENXIO);
334
335 for (i = 0; i < HDSPE_MAX_CHANS && chan_map[i].descr != NULL; i++) {
336 scp = malloc(sizeof(struct sc_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
337 scp->hc = &chan_map[i];
338 scp->sc = sc;
339 scp->dev = device_add_child(dev, "pcm", -1);
340 device_set_ivars(scp->dev, scp);
341 }
342
344
345 return (bus_generic_attach(dev));
346}
347
348static void
350{
351
352 bus_dmamap_unload(sc->dmat, sc->rmap);
353 bus_dmamap_unload(sc->dmat, sc->pmap);
354 bus_dmamem_free(sc->dmat, sc->rbuf, sc->rmap);
355 bus_dmamem_free(sc->dmat, sc->pbuf, sc->pmap);
356 sc->rbuf = sc->pbuf = NULL;
357}
358
359static int
361{
362 struct sc_info *sc;
363 int err;
364
365 sc = device_get_softc(dev);
366 if (sc == NULL) {
367 device_printf(dev,"Can't detach: softc is null.\n");
368 return (0);
369 }
370
371 err = device_delete_children(dev);
372 if (err)
373 return (err);
374
375 hdspe_dmafree(sc);
376
377 if (sc->ih)
378 bus_teardown_intr(dev, sc->irq, sc->ih);
379 if (sc->dmat)
380 bus_dma_tag_destroy(sc->dmat);
381 if (sc->irq)
382 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
383 if (sc->cs)
384 bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(0), sc->cs);
385 if (sc->lock)
386 snd_mtxfree(sc->lock);
387
388 return (0);
389}
390
391static device_method_t hdspe_methods[] = {
392 DEVMETHOD(device_probe, hdspe_probe),
393 DEVMETHOD(device_attach, hdspe_attach),
394 DEVMETHOD(device_detach, hdspe_detach),
395 { 0, 0 }
396};
397
398static driver_t hdspe_driver = {
399 "hdspe",
402};
403
404static devclass_t hdspe_devclass;
405
METHOD int free
Definition: channel_if.m:110
static void hdspe_dmapsetmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
Definition: hdspe.c:123
static int hdspe_init(struct sc_info *sc)
Definition: hdspe.c:255
static device_method_t hdspe_methods[]
Definition: hdspe.c:391
static driver_t hdspe_driver
Definition: hdspe.c:398
static void hdspe_intr(void *p)
Definition: hdspe.c:90
static struct hdspe_channel chan_map_aio[]
Definition: hdspe.c:45
static struct hdspe_channel chan_map_rd[]
Definition: hdspe.c:62
static int hdspe_alloc_resources(struct sc_info *sc)
Definition: hdspe.c:135
SND_DECLARE_FILE("$FreeBSD$")
static void hdspe_dmafree(struct sc_info *sc)
Definition: hdspe.c:349
DRIVER_MODULE(snd_hdspe, pci, hdspe_driver, hdspe_devclass, 0, 0)
static int hdspe_detach(device_t dev)
Definition: hdspe.c:360
static devclass_t hdspe_devclass
Definition: hdspe.c:404
static int hdspe_probe(device_t dev)
Definition: hdspe.c:234
static void hdspe_map_dmabuf(struct sc_info *sc)
Definition: hdspe.c:217
static int hdspe_attach(device_t dev)
Definition: hdspe.c:293
#define HDSPE_SPEED_DEFAULT
Definition: hdspe.h:66
#define HDSPE_DMASEGSIZE
Definition: hdspe.h:121
#define PCI_REVISION_RAYDAT
Definition: hdspe.h:35
#define HDSPE_INTERRUPT_ACK
Definition: hdspe.h:113
#define HDSPE_CONTROL_REG
Definition: hdspe.h:105
#define hdspe_write_1(sc, regno, data)
Definition: hdspe.h:202
#define HDSPE_FREQ_MASK_DEFAULT
Definition: hdspe.h:62
#define HDSPE_PAGE_ADDR_BUF_OUT
Definition: hdspe.h:47
#define hdspe_write_4(sc, regno, data)
Definition: hdspe.h:206
#define PCI_REVISION_AIO
Definition: hdspe.h:34
#define HDSPE_FREQ_AIO
Definition: hdspe.h:64
#define PCI_DEVICE_XILINX_HDSPE
Definition: hdspe.h:32
#define HDSPM_CLOCK_MODE_MASTER
Definition: hdspe.h:108
#define HDSPE_AUDIO_IRQ_PENDING
Definition: hdspe.h:111
#define HDSPE_FREQ_REG
Definition: hdspe.h:63
#define PCI_VENDOR_XILINX
Definition: hdspe.h:31
#define HDSPE_MAX_CHANS
Definition: hdspe.h:117
#define AIO
Definition: hdspe.h:37
#define HDSPE_STATUS_REG
Definition: hdspe.h:106
#define HDSPE_SETTINGS_REG
Definition: hdspe.h:104
#define hdspe_read_1(sc, regno)
Definition: hdspe.h:195
#define RAYDAT
Definition: hdspe.h:38
#define HDSPE_PAGE_ADDR_BUF_IN
Definition: hdspe.h:48
#define HDSPE_MAX_SLOTS
Definition: hdspe.h:116
#define hdspe_encode_latency(x)
Definition: hdspe.h:75
unsigned dev
Definition: mixer_if.m:59
bool * status
#define PCIR_BAR(x)
void * snd_mtxcreate(const char *desc, const char *type)
Definition: sound.c:88
void snd_mtxfree(void *m)
Definition: sound.c:98
#define snd_mtxlock(m)
Definition: sound.h:347
#define snd_mtxunlock(m)
Definition: sound.h:348
#define PCM_SOFTC_SIZE
Definition: sound.h:96
char * descr
Definition: hdspe.h:126
u_int32_t type
Definition: cs4281.c:87
uint32_t ctrl_register
Definition: hdspe.h:170
struct mtx * lock
Definition: als4000.c:84
struct resource * cs
Definition: envy24.c:124
sample32_t * rbuf
Definition: envy24.c:164
bus_dmamap_t pmap
Definition: envy24.c:167
struct resource * irq
Definition: als4000.c:81
sample32_t * pbuf
Definition: envy24.c:163
device_t dev
Definition: als4000.c:77
int csid
Definition: envy24.c:125
bus_addr_t raddr
Definition: envy24.c:168
bus_space_handle_t csh
Definition: envy24.c:127
int irqid
Definition: als4000.c:82
uint32_t period
Definition: hdspe.h:191
bus_dma_tag_t dmat
Definition: envy24.c:144
bus_addr_t paddr
Definition: envy24.c:168
uint32_t bufsize
Definition: hdspe.h:188
void * ih
Definition: als4000.c:83
bus_space_tag_t cst
Definition: envy24.c:126
bus_dmamap_t rmap
Definition: envy24.c:167
u_int32_t rev
Definition: emu10k1.c:213
uint32_t settings_register
Definition: hdspe.h:171
u_int32_t speed
Definition: envy24.c:171
struct hdspe_channel * hc
Definition: hdspe.h:162
uint32_t(* ih)(struct sc_pcminfo *scp)
Definition: hdspe.h:158
struct sc_info * sc
Definition: hdspe.h:161
device_t dev
Definition: hdspe.h:157