2 * Copyright (c) 1999 Seigo Tanimura
3 * Copyright (c) 1999 Ville-Pertti Keinonen
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <machine/resource.h>
35 #include <machine/bus.h>
37 #include <sys/soundcard.h>
38 #include <dev/sound/pcm/sound.h>
39 #include <dev/sound/chip.h>
42 #include <isa/isavar.h>
43 #include <isa/isa_common.h>
45 SND_DECLARE_FILE("$FreeBSD$");
47 #define LOGICALID_NOPNP 0
48 #define LOGICALID_PCM 0x0000561e
49 #define LOGICALID_OPL 0x0300561e
50 #define LOGICALID_MIDI 0x0400561e
53 static struct isa_pnp_id gusc_ids[] = {
54 {LOGICALID_PCM, "GRV0000 Gravis UltraSound PnP PCM"}, /* GRV0000 */
55 {LOGICALID_OPL, "GRV0003 Gravis UltraSound PnP OPL"}, /* GRV0003 */
56 {LOGICALID_MIDI, "GRV0004 Gravis UltraSound PnP MIDI"}, /* GRV0004 */
59 /* Interrupt handler. */
60 struct gusc_ihandler {
65 /* Here is the parameter structure per a device. */
67 device_t dev; /* device */
68 int io_rid[3]; /* io port rids */
69 struct resource *io[3]; /* io port resources */
70 int io_alloced[3]; /* io port alloc flag */
71 int irq_rid; /* irq rids */
72 struct resource *irq; /* irq resources */
73 int irq_alloced; /* irq alloc flag */
74 int drq_rid[2]; /* drq rids */
75 struct resource *drq[2]; /* drq resources */
76 int drq_alloced[2]; /* drq alloc flag */
78 /* Interrupts are shared (XXX non-PnP only?) */
79 struct gusc_ihandler midi_intr;
80 struct gusc_ihandler pcm_intr;
83 typedef struct gusc_softc *sc_p;
85 static int gusc_probe(device_t dev);
86 static int gusc_attach(device_t dev);
87 static int gusisa_probe(device_t dev);
88 static void gusc_intr(void *);
89 static struct resource *gusc_alloc_resource(device_t bus, device_t child, int type, int *rid,
90 u_long start, u_long end, u_long count, u_int flags);
91 static int gusc_release_resource(device_t bus, device_t child, int type, int rid,
94 static device_t find_masterdev(sc_p scp);
95 static int alloc_resource(sc_p scp);
96 static int release_resource(sc_p scp);
98 static devclass_t gusc_devclass;
101 gusc_probe(device_t dev)
104 u_int32_t logical_id;
106 struct sndcard_func *func;
109 logical_id = isa_get_logicalid(dev);
112 /* Check isapnp ids */
113 if (logical_id != 0 && (ret = ISA_PNP_PROBE(device_get_parent(dev), dev, gusc_ids)) != 0)
117 return gusisa_probe(dev);
120 switch (logical_id) {
122 s = "Gravis UltraSound Plug & Play PCM";
123 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
126 func->func = SCF_PCM;
127 child = device_add_child(dev, "pcm", -1);
128 device_set_ivars(child, func);
131 s = "Gravis UltraSound Plug & Play OPL";
132 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
135 func->func = SCF_SYNTH;
136 child = device_add_child(dev, "midi", -1);
137 device_set_ivars(child, func);
140 s = "Gravis UltraSound Plug & Play MIDI";
141 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
144 func->func = SCF_MIDI;
145 child = device_add_child(dev, "midi", -1);
146 device_set_ivars(child, func);
151 device_set_desc(dev, s);
159 port_wr(struct resource *r, int i, unsigned char v)
161 bus_space_write_1(rman_get_bustag(r), rman_get_bushandle(r), i, v);
165 port_rd(struct resource *r, int i)
167 return bus_space_read_1(rman_get_bustag(r), rman_get_bushandle(r), i);
171 * Probe for an old (non-PnP) GUS card on the ISA bus.
175 gusisa_probe(device_t dev)
178 struct resource *res, *res2;
179 int base, rid, rid2, s, flags;
182 base = isa_get_port(dev);
183 flags = device_get_flags(dev);
185 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, base + 0x100,
186 base + 0x107, 8, RF_ACTIVE);
194 * Check for the presence of some GUS card. Reset the card,
195 * then see if we can access the memory on it.
198 port_wr(res, 3, 0x4c);
202 port_wr(res, 3, 0x4c);
210 port_wr(res, 3, 0x43); /* Register select */
211 port_wr(res, 4, 0); /* Low addr */
212 port_wr(res, 5, 0); /* Med addr */
214 port_wr(res, 3, 0x44); /* Register select */
215 port_wr(res, 4, 0); /* High addr */
216 port_wr(res, 7, 0x55); /* DRAM */
218 /* Read from DRAM. */
220 port_wr(res, 3, 0x43); /* Register select */
221 port_wr(res, 4, 0); /* Low addr */
222 port_wr(res, 5, 0); /* Med addr */
224 port_wr(res, 3, 0x44); /* Register select */
225 port_wr(res, 4, 0); /* High addr */
226 val = port_rd(res, 7); /* DRAM */
234 res2 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid2, base, base, 1,
241 port_wr(res2, 0x0f, 0x20);
242 val = port_rd(res2, 0x0f);
245 if (val == 0xff || (val & 0x06) == 0)
248 val = port_rd(res2, 0x506); /* XXX Out of range. */
253 bus_release_resource(dev, SYS_RES_IOPORT, rid2, res2);
254 bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
257 struct sndcard_func *func;
259 /* Looks like a GUS MAX. Set the rest of the resources. */
261 bus_set_resource(dev, SYS_RES_IOPORT, 2, base + 0x10c, 8);
263 if (flags & DV_F_DUAL_DMA)
264 bus_set_resource(dev, SYS_RES_DRQ, 1,
265 flags & DV_F_DRQ_MASK, 1);
267 /* We can support the CS4231 and MIDI devices. */
269 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
272 func->func = SCF_MIDI;
273 child = device_add_child(dev, "midi", -1);
274 device_set_ivars(child, func);
276 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
278 printf("xxx: gus pcm not attached, out of memory\n");
280 func->func = SCF_PCM;
281 child = device_add_child(dev, "pcm", -1);
282 device_set_ivars(child, func);
284 device_set_desc(dev, "Gravis UltraSound MAX");
289 * TODO: Support even older GUS cards. MIDI should work on
296 bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
301 gusc_attach(device_t dev)
307 scp = device_get_softc(dev);
308 unit = device_get_unit(dev);
310 bzero(scp, sizeof(*scp));
313 if (alloc_resource(scp)) {
314 release_resource(scp);
318 if (scp->irq != NULL)
319 snd_setup_intr(dev, scp->irq, 0, gusc_intr, scp, &ih);
320 bus_generic_attach(dev);
326 * Handle interrupts on GUS devices until there aren't any left.
331 sc_p scp = (sc_p)arg;
336 if (scp->pcm_intr.intr != NULL &&
337 (port_rd(scp->io[2], 2) & 1)) {
338 (*scp->pcm_intr.intr)(scp->pcm_intr.arg);
341 if (scp->midi_intr.intr != NULL &&
342 (port_rd(scp->io[1], 0) & 0x80)) {
343 (*scp->midi_intr.intr)(scp->midi_intr.arg);
346 } while (did_something != 0);
349 static struct resource *
350 gusc_alloc_resource(device_t bus, device_t child, int type, int *rid,
351 u_long start, u_long end, u_long count, u_int flags)
354 int *alloced, rid_max, alloced_max;
355 struct resource **res;
357 scp = device_get_softc(bus);
360 alloced = scp->io_alloced;
363 alloced_max = 2; /* pcm + midi (more to include synth) */
366 alloced = &scp->irq_alloced;
369 alloced_max = 2; /* pcm and midi share the single irq. */
372 alloced = scp->drq_alloced;
381 if (*rid > rid_max || alloced[*rid] == alloced_max)
389 gusc_release_resource(device_t bus, device_t child, int type, int rid,
393 int *alloced, rid_max;
395 scp = device_get_softc(bus);
398 alloced = scp->io_alloced;
402 alloced = &scp->irq_alloced;
406 alloced = scp->drq_alloced;
413 if (rid > rid_max || alloced[rid] == 0)
421 gusc_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
422 #if __FreeBSD_version >= 700031
423 driver_filter_t *filter,
425 driver_intr_t *intr, void *arg, void **cookiep)
427 sc_p scp = (sc_p)device_get_softc(dev);
430 #if __FreeBSD_version >= 700031
431 if (filter != NULL) {
432 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,
447 #if __FreeBSD_version >= 700031
454 find_masterdev(sc_p scp)
460 devclass = device_get_devclass(scp->dev);
461 units = devclass_get_maxunit(devclass);
463 for (i = 0 ; i < units ; i++) {
464 dev = devclass_get_device(devclass, i);
465 if (isa_get_vendorid(dev) == isa_get_vendorid(scp->dev)
466 && isa_get_logicalid(dev) == LOGICALID_PCM
467 && isa_get_serial(dev) == isa_get_serial(scp->dev))
476 static int io_range[3] = {0x10, 0x8 , 0x4 };
477 static int io_offset[3] = {0x0 , 0x100, 0x10c};
479 alloc_resource(sc_p scp)
481 int i, base, lid, flags;
485 if (isa_get_vendorid(scp->dev))
486 lid = isa_get_logicalid(scp->dev);
488 lid = LOGICALID_NOPNP;
489 flags = device_get_flags(scp->dev);
493 case LOGICALID_NOPNP: /* XXX Non-PnP */
494 if (lid == LOGICALID_NOPNP)
495 base = isa_get_port(scp->dev);
498 for (i = 0 ; i < sizeof(scp->io) / sizeof(*scp->io) ; i++) {
499 if (scp->io[i] == NULL) {
502 scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i],
503 0, ~0, io_range[i], RF_ACTIVE);
505 scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i],
507 base + io_offset[i] + io_range[i] - 1
508 , io_range[i], RF_ACTIVE);
509 if (scp->io[i] == NULL)
511 scp->io_alloced[i] = 0;
514 if (scp->irq == NULL) {
517 bus_alloc_resource_any(scp->dev, SYS_RES_IRQ,
519 RF_ACTIVE|RF_SHAREABLE);
520 if (scp->irq == NULL)
522 scp->irq_alloced = 0;
524 for (i = 0 ; i < sizeof(scp->drq) / sizeof(*scp->drq) ; i++) {
525 if (scp->drq[i] == NULL) {
527 if (base == 0 || i == 0)
529 bus_alloc_resource_any(
530 scp->dev, SYS_RES_DRQ,
533 else if ((flags & DV_F_DUAL_DMA) != 0)
534 /* XXX The secondary drq is specified in the flag. */
535 scp->drq[i] = bus_alloc_resource(scp->dev, SYS_RES_DRQ, &scp->drq_rid[i],
536 flags & DV_F_DRQ_MASK,
537 flags & DV_F_DRQ_MASK, 1, RF_ACTIVE);
538 if (scp->drq[i] == NULL)
540 scp->drq_alloced[i] = 0;
545 if (scp->io[0] == NULL) {
547 scp->io[0] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[0],
548 0, ~0, io_range[0], RF_ACTIVE);
549 if (scp->io[0] == NULL)
551 scp->io_alloced[0] = 0;
555 if (scp->io[0] == NULL) {
557 scp->io[0] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[0],
558 0, ~0, io_range[0], RF_ACTIVE);
559 if (scp->io[0] == NULL)
561 scp->io_alloced[0] = 0;
563 if (scp->irq == NULL) {
564 /* The irq is shared with pcm audio. */
565 dev = find_masterdev(scp);
569 scp->irq = BUS_ALLOC_RESOURCE(dev, NULL, SYS_RES_IRQ, &scp->irq_rid,
570 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
571 if (scp->irq == NULL)
573 scp->irq_alloced = 0;
581 release_resource(sc_p scp)
587 if (isa_get_vendorid(scp->dev))
588 lid = isa_get_logicalid(scp->dev);
590 lid = LOGICALID_NOPNP;
591 flags = device_get_flags(scp->dev);
595 case LOGICALID_NOPNP: /* XXX Non-PnP */
596 for (i = 0 ; i < sizeof(scp->io) / sizeof(*scp->io) ; i++) {
597 if (scp->io[i] != NULL) {
598 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[i], scp->io[i]);
602 if (scp->irq != NULL) {
603 bus_release_resource(scp->dev, SYS_RES_IRQ, scp->irq_rid, scp->irq);
606 for (i = 0 ; i < sizeof(scp->drq) / sizeof(*scp->drq) ; i++) {
607 if (scp->drq[i] != NULL) {
608 bus_release_resource(scp->dev, SYS_RES_DRQ, scp->drq_rid[i], scp->drq[i]);
614 if (scp->io[0] != NULL) {
615 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[0], scp->io[0]);
620 if (scp->io[0] != NULL) {
621 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[0], scp->io[0]);
624 if (scp->irq != NULL) {
625 /* The irq is shared with pcm audio. */
626 dev = find_masterdev(scp);
629 BUS_RELEASE_RESOURCE(dev, NULL, SYS_RES_IOPORT, scp->irq_rid, scp->irq);
637 static device_method_t gusc_methods[] = {
638 /* Device interface */
639 DEVMETHOD(device_probe, gusc_probe),
640 DEVMETHOD(device_attach, gusc_attach),
641 DEVMETHOD(device_detach, bus_generic_detach),
642 DEVMETHOD(device_shutdown, bus_generic_shutdown),
643 DEVMETHOD(device_suspend, bus_generic_suspend),
644 DEVMETHOD(device_resume, bus_generic_resume),
647 DEVMETHOD(bus_print_child, bus_generic_print_child),
648 DEVMETHOD(bus_alloc_resource, gusc_alloc_resource),
649 DEVMETHOD(bus_release_resource, gusc_release_resource),
650 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
651 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
652 DEVMETHOD(bus_setup_intr, gusc_setup_intr),
653 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
658 static driver_t gusc_driver = {
661 sizeof(struct gusc_softc),
665 * gusc can be attached to an isa bus.
667 DRIVER_MODULE(snd_gusc, isa, gusc_driver, gusc_devclass, 0, 0);
668 DRIVER_MODULE(snd_gusc, acpi, gusc_driver, gusc_devclass, 0, 0);
669 MODULE_DEPEND(snd_gusc, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
670 MODULE_VERSION(snd_gusc, 1);