2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Seigo Tanimura
5 * Copyright (c) 1999 Ville-Pertti Keinonen
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <machine/resource.h>
37 #include <machine/bus.h>
40 #ifdef HAVE_KERNEL_OPTION_HEADERS
44 #include <dev/sound/pcm/sound.h>
45 #include <dev/sound/chip.h>
48 #include <isa/isavar.h>
49 #include <isa/isa_common.h>
51 SND_DECLARE_FILE("$FreeBSD$");
53 #define LOGICALID_NOPNP 0
54 #define LOGICALID_PCM 0x0000561e
55 #define LOGICALID_OPL 0x0300561e
56 #define LOGICALID_MIDI 0x0400561e
59 static struct isa_pnp_id gusc_ids[] = {
60 {LOGICALID_PCM, "GRV0000 Gravis UltraSound PnP PCM"}, /* GRV0000 */
61 {LOGICALID_OPL, "GRV0003 Gravis UltraSound PnP OPL"}, /* GRV0003 */
62 {LOGICALID_MIDI, "GRV0004 Gravis UltraSound PnP MIDI"}, /* GRV0004 */
65 /* Interrupt handler. */
66 struct gusc_ihandler {
71 /* Here is the parameter structure per a device. */
73 device_t dev; /* device */
74 int io_rid[3]; /* io port rids */
75 struct resource *io[3]; /* io port resources */
76 int io_alloced[3]; /* io port alloc flag */
77 int irq_rid; /* irq rids */
78 struct resource *irq; /* irq resources */
79 int irq_alloced; /* irq alloc flag */
80 int drq_rid[2]; /* drq rids */
81 struct resource *drq[2]; /* drq resources */
82 int drq_alloced[2]; /* drq alloc flag */
84 /* Interrupts are shared (XXX non-PnP only?) */
85 struct gusc_ihandler midi_intr;
86 struct gusc_ihandler pcm_intr;
89 typedef struct gusc_softc *sc_p;
91 static int gusc_probe(device_t dev);
92 static int gusc_attach(device_t dev);
93 static int gusisa_probe(device_t dev);
94 static void gusc_intr(void *);
95 static struct resource *gusc_alloc_resource(device_t bus, device_t child, int type, int *rid,
96 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags);
97 static int gusc_release_resource(device_t bus, device_t child, int type, int rid,
100 static device_t find_masterdev(sc_p scp);
101 static int alloc_resource(sc_p scp);
102 static int release_resource(sc_p scp);
104 static devclass_t gusc_devclass;
107 gusc_probe(device_t dev)
110 u_int32_t logical_id;
112 struct sndcard_func *func;
115 logical_id = isa_get_logicalid(dev);
118 /* Check isapnp ids */
119 if (logical_id != 0 && (ret = ISA_PNP_PROBE(device_get_parent(dev), dev, gusc_ids)) != 0)
123 return gusisa_probe(dev);
126 switch (logical_id) {
128 s = "Gravis UltraSound Plug & Play PCM";
129 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
132 func->func = SCF_PCM;
133 child = device_add_child(dev, "pcm", -1);
134 device_set_ivars(child, func);
137 s = "Gravis UltraSound Plug & Play OPL";
138 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
141 func->func = SCF_SYNTH;
142 child = device_add_child(dev, "midi", -1);
143 device_set_ivars(child, func);
146 s = "Gravis UltraSound Plug & Play MIDI";
147 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
150 func->func = SCF_MIDI;
151 child = device_add_child(dev, "midi", -1);
152 device_set_ivars(child, func);
157 device_set_desc(dev, s);
165 port_wr(struct resource *r, int i, unsigned char v)
167 bus_space_write_1(rman_get_bustag(r), rman_get_bushandle(r), i, v);
171 port_rd(struct resource *r, int i)
173 return bus_space_read_1(rman_get_bustag(r), rman_get_bushandle(r), i);
177 * Probe for an old (non-PnP) GUS card on the ISA bus.
181 gusisa_probe(device_t dev)
184 struct resource *res, *res2;
185 int base, rid, rid2, s, flags;
188 base = isa_get_port(dev);
189 flags = device_get_flags(dev);
191 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, base + 0x100,
192 base + 0x107, 8, RF_ACTIVE);
200 * Check for the presence of some GUS card. Reset the card,
201 * then see if we can access the memory on it.
204 port_wr(res, 3, 0x4c);
208 port_wr(res, 3, 0x4c);
216 port_wr(res, 3, 0x43); /* Register select */
217 port_wr(res, 4, 0); /* Low addr */
218 port_wr(res, 5, 0); /* Med addr */
220 port_wr(res, 3, 0x44); /* Register select */
221 port_wr(res, 4, 0); /* High addr */
222 port_wr(res, 7, 0x55); /* DRAM */
224 /* Read from DRAM. */
226 port_wr(res, 3, 0x43); /* Register select */
227 port_wr(res, 4, 0); /* Low addr */
228 port_wr(res, 5, 0); /* Med addr */
230 port_wr(res, 3, 0x44); /* Register select */
231 port_wr(res, 4, 0); /* High addr */
232 val = port_rd(res, 7); /* DRAM */
240 res2 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid2, base, base, 1,
247 port_wr(res2, 0x0f, 0x20);
248 val = port_rd(res2, 0x0f);
251 if (val == 0xff || (val & 0x06) == 0)
254 val = port_rd(res2, 0x506); /* XXX Out of range. */
259 bus_release_resource(dev, SYS_RES_IOPORT, rid2, res2);
260 bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
263 struct sndcard_func *func;
265 /* Looks like a GUS MAX. Set the rest of the resources. */
267 bus_set_resource(dev, SYS_RES_IOPORT, 2, base + 0x10c, 8);
269 if (flags & DV_F_DUAL_DMA)
270 bus_set_resource(dev, SYS_RES_DRQ, 1,
271 flags & DV_F_DRQ_MASK, 1);
273 /* We can support the CS4231 and MIDI devices. */
275 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
278 func->func = SCF_MIDI;
279 child = device_add_child(dev, "midi", -1);
280 device_set_ivars(child, func);
282 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
284 printf("xxx: gus pcm not attached, out of memory\n");
286 func->func = SCF_PCM;
287 child = device_add_child(dev, "pcm", -1);
288 device_set_ivars(child, func);
290 device_set_desc(dev, "Gravis UltraSound MAX");
295 * TODO: Support even older GUS cards. MIDI should work on
302 bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
307 gusc_attach(device_t dev)
312 scp = device_get_softc(dev);
314 bzero(scp, sizeof(*scp));
317 if (alloc_resource(scp)) {
318 release_resource(scp);
322 if (scp->irq != NULL)
323 snd_setup_intr(dev, scp->irq, 0, gusc_intr, scp, &ih);
324 bus_generic_attach(dev);
330 * Handle interrupts on GUS devices until there aren't any left.
335 sc_p scp = (sc_p)arg;
340 if (scp->pcm_intr.intr != NULL &&
341 (port_rd(scp->io[2], 2) & 1)) {
342 (*scp->pcm_intr.intr)(scp->pcm_intr.arg);
345 if (scp->midi_intr.intr != NULL &&
346 (port_rd(scp->io[1], 0) & 0x80)) {
347 (*scp->midi_intr.intr)(scp->midi_intr.arg);
350 } while (did_something != 0);
353 static struct resource *
354 gusc_alloc_resource(device_t bus, device_t child, int type, int *rid,
355 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
358 int *alloced, rid_max, alloced_max;
359 struct resource **res;
361 scp = device_get_softc(bus);
364 alloced = scp->io_alloced;
367 alloced_max = 2; /* pcm + midi (more to include synth) */
370 alloced = &scp->irq_alloced;
373 alloced_max = 2; /* pcm and midi share the single irq. */
376 alloced = scp->drq_alloced;
385 if (*rid > rid_max || alloced[*rid] == alloced_max)
393 gusc_release_resource(device_t bus, device_t child, int type, int rid,
397 int *alloced, rid_max;
399 scp = device_get_softc(bus);
402 alloced = scp->io_alloced;
406 alloced = &scp->irq_alloced;
410 alloced = scp->drq_alloced;
417 if (rid > rid_max || alloced[rid] == 0)
425 gusc_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
426 driver_filter_t *filter,
427 driver_intr_t *intr, void *arg, void **cookiep)
429 sc_p scp = (sc_p)device_get_softc(dev);
432 if (filter != NULL) {
433 printf("gusc.c: we cannot use a filter here\n");
436 devclass = device_get_devclass(child);
437 if (strcmp(devclass_get_name(devclass), "midi") == 0) {
438 scp->midi_intr.intr = intr;
439 scp->midi_intr.arg = arg;
441 } else if (strcmp(devclass_get_name(devclass), "pcm") == 0) {
442 scp->pcm_intr.intr = intr;
443 scp->pcm_intr.arg = arg;
446 return bus_generic_setup_intr(dev, child, irq, flags,
452 find_masterdev(sc_p scp)
458 devclass = device_get_devclass(scp->dev);
459 units = devclass_get_maxunit(devclass);
461 for (i = 0 ; i < units ; i++) {
462 dev = devclass_get_device(devclass, i);
463 if (isa_get_vendorid(dev) == isa_get_vendorid(scp->dev)
464 && isa_get_logicalid(dev) == LOGICALID_PCM
465 && isa_get_serial(dev) == isa_get_serial(scp->dev))
474 static int io_range[3] = {0x10, 0x8 , 0x4 };
475 static int io_offset[3] = {0x0 , 0x100, 0x10c};
477 alloc_resource(sc_p scp)
479 int i, base, lid, flags;
483 if (isa_get_vendorid(scp->dev))
484 lid = isa_get_logicalid(scp->dev);
486 lid = LOGICALID_NOPNP;
487 flags = device_get_flags(scp->dev);
491 case LOGICALID_NOPNP: /* XXX Non-PnP */
492 if (lid == LOGICALID_NOPNP)
493 base = isa_get_port(scp->dev);
496 for (i = 0 ; i < nitems(scp->io); i++) {
497 if (scp->io[i] == NULL) {
501 bus_alloc_resource_anywhere(scp->dev,
507 scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i],
509 base + io_offset[i] + io_range[i] - 1
510 , io_range[i], RF_ACTIVE);
511 if (scp->io[i] == NULL)
513 scp->io_alloced[i] = 0;
516 if (scp->irq == NULL) {
519 bus_alloc_resource_any(scp->dev, SYS_RES_IRQ,
521 RF_ACTIVE|RF_SHAREABLE);
522 if (scp->irq == NULL)
524 scp->irq_alloced = 0;
526 for (i = 0 ; i < nitems(scp->drq); i++) {
527 if (scp->drq[i] == NULL) {
529 if (base == 0 || i == 0)
531 bus_alloc_resource_any(
532 scp->dev, SYS_RES_DRQ,
535 else if ((flags & DV_F_DUAL_DMA) != 0)
536 /* XXX The secondary drq is specified in the flag. */
537 scp->drq[i] = bus_alloc_resource(scp->dev, SYS_RES_DRQ, &scp->drq_rid[i],
538 flags & DV_F_DRQ_MASK,
539 flags & DV_F_DRQ_MASK, 1, RF_ACTIVE);
540 if (scp->drq[i] == NULL)
542 scp->drq_alloced[i] = 0;
547 if (scp->io[0] == NULL) {
549 scp->io[0] = bus_alloc_resource_anywhere(scp->dev,
554 if (scp->io[0] == NULL)
556 scp->io_alloced[0] = 0;
560 if (scp->io[0] == NULL) {
562 scp->io[0] = bus_alloc_resource_anywhere(scp->dev,
567 if (scp->io[0] == NULL)
569 scp->io_alloced[0] = 0;
571 if (scp->irq == NULL) {
572 /* The irq is shared with pcm audio. */
573 dev = find_masterdev(scp);
577 scp->irq = BUS_ALLOC_RESOURCE(dev, NULL, SYS_RES_IRQ, &scp->irq_rid,
578 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
579 if (scp->irq == NULL)
581 scp->irq_alloced = 0;
589 release_resource(sc_p scp)
594 if (isa_get_vendorid(scp->dev))
595 lid = isa_get_logicalid(scp->dev);
597 lid = LOGICALID_NOPNP;
601 case LOGICALID_NOPNP: /* XXX Non-PnP */
602 for (i = 0 ; i < nitems(scp->io); i++) {
603 if (scp->io[i] != NULL) {
604 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[i], scp->io[i]);
608 if (scp->irq != NULL) {
609 bus_release_resource(scp->dev, SYS_RES_IRQ, scp->irq_rid, scp->irq);
612 for (i = 0 ; i < nitems(scp->drq); i++) {
613 if (scp->drq[i] != NULL) {
614 bus_release_resource(scp->dev, SYS_RES_DRQ, scp->drq_rid[i], scp->drq[i]);
620 if (scp->io[0] != NULL) {
621 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[0], scp->io[0]);
626 if (scp->io[0] != NULL) {
627 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[0], scp->io[0]);
630 if (scp->irq != NULL) {
631 /* The irq is shared with pcm audio. */
632 dev = find_masterdev(scp);
635 BUS_RELEASE_RESOURCE(dev, NULL, SYS_RES_IOPORT, scp->irq_rid, scp->irq);
643 static device_method_t gusc_methods[] = {
644 /* Device interface */
645 DEVMETHOD(device_probe, gusc_probe),
646 DEVMETHOD(device_attach, gusc_attach),
647 DEVMETHOD(device_detach, bus_generic_detach),
648 DEVMETHOD(device_shutdown, bus_generic_shutdown),
649 DEVMETHOD(device_suspend, bus_generic_suspend),
650 DEVMETHOD(device_resume, bus_generic_resume),
653 DEVMETHOD(bus_alloc_resource, gusc_alloc_resource),
654 DEVMETHOD(bus_release_resource, gusc_release_resource),
655 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
656 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
657 DEVMETHOD(bus_setup_intr, gusc_setup_intr),
658 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
663 static driver_t gusc_driver = {
666 sizeof(struct gusc_softc),
670 * gusc can be attached to an isa bus.
672 DRIVER_MODULE(snd_gusc, isa, gusc_driver, gusc_devclass, 0, 0);
673 DRIVER_MODULE(snd_gusc, acpi, gusc_driver, gusc_devclass, 0, 0);
674 MODULE_DEPEND(snd_gusc, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
675 MODULE_VERSION(snd_gusc, 1);
676 ISA_PNP_INFO(gusc_ids);