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
27 * $FreeBSD: src/sys/dev/sound/isa/gusc.c,v 1.5.2.6 2002/04/22 15:49:30 cg Exp $
28 * $DragonFly: src/sys/dev/sound/isa/gusc.c,v 1.8 2006/09/05 00:55:43 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <machine/resource.h>
38 #include <machine/bus.h>
40 #include <sys/soundcard.h>
41 #include <dev/sound/pcm/sound.h>
42 #include <dev/sound/chip.h>
45 #include <bus/isa/isavar.h>
46 #include <bus/isa/isa_common.h>
48 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/isa/gusc.c,v 1.8 2006/09/05 00:55:43 dillon Exp $");
50 #define LOGICALID_NOPNP 0
51 #define LOGICALID_PCM 0x0000561e
52 #define LOGICALID_OPL 0x0300561e
53 #define LOGICALID_MIDI 0x0400561e
56 static struct isa_pnp_id gusc_ids[] = {
57 {LOGICALID_PCM, "GRV0000 Gravis UltraSound PnP PCM"}, /* GRV0000 */
58 {LOGICALID_OPL, "GRV0003 Gravis UltraSound PnP OPL"}, /* GRV0003 */
59 {LOGICALID_MIDI, "GRV0004 Gravis UltraSound PnP MIDI"}, /* GRV0004 */
62 /* Interrupt handler. */
63 struct gusc_ihandler {
68 /* Here is the parameter structure per a device. */
70 device_t dev; /* device */
71 int io_rid[3]; /* io port rids */
72 struct resource *io[3]; /* io port resources */
73 int io_alloced[3]; /* io port alloc flag */
74 int irq_rid; /* irq rids */
75 struct resource *irq; /* irq resources */
76 int irq_alloced; /* irq alloc flag */
77 int drq_rid[2]; /* drq rids */
78 struct resource *drq[2]; /* drq resources */
79 int drq_alloced[2]; /* drq alloc flag */
81 /* Interrupts are shared (XXX non-PnP only?) */
82 struct gusc_ihandler midi_intr;
83 struct gusc_ihandler pcm_intr;
86 typedef struct gusc_softc *sc_p;
88 static int gusc_probe(device_t dev);
89 static int gusc_attach(device_t dev);
90 static int gusisa_probe(device_t dev);
91 static void gusc_intr(void *);
92 static struct resource *gusc_alloc_resource(device_t bus, device_t child, int type, int *rid,
93 u_long start, u_long end, u_long count, u_int flags);
94 static int gusc_release_resource(device_t bus, device_t child, int type, int rid,
97 static device_t find_masterdev(sc_p scp);
98 static int alloc_resource(sc_p scp);
99 static int release_resource(sc_p scp);
101 static devclass_t gusc_devclass;
104 gusc_probe(device_t dev)
107 u_int32_t logical_id;
109 struct sndcard_func *func;
112 logical_id = isa_get_logicalid(dev);
115 /* Check isapnp ids */
116 if (logical_id != 0 && (ret = ISA_PNP_PROBE(device_get_parent(dev), dev, gusc_ids)) != 0)
120 return gusisa_probe(dev);
123 switch (logical_id) {
125 s = "Gravis UltraSound Plug & Play PCM";
126 func = kmalloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
129 func->func = SCF_PCM;
130 child = device_add_child(dev, "pcm", -1);
131 device_set_ivars(child, func);
134 s = "Gravis UltraSound Plug & Play OPL";
135 func = kmalloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
138 func->func = SCF_SYNTH;
139 child = device_add_child(dev, "midi", -1);
140 device_set_ivars(child, func);
143 s = "Gravis UltraSound Plug & Play MIDI";
144 func = kmalloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
147 func->func = SCF_MIDI;
148 child = device_add_child(dev, "midi", -1);
149 device_set_ivars(child, func);
154 device_set_desc(dev, s);
162 port_wr(struct resource *r, int i, unsigned char v)
164 bus_space_write_1(rman_get_bustag(r), rman_get_bushandle(r), i, v);
168 port_rd(struct resource *r, int i)
170 return bus_space_read_1(rman_get_bustag(r), rman_get_bushandle(r), i);
174 * Probe for an old (non-PnP) GUS card on the ISA bus.
178 gusisa_probe(device_t dev)
181 struct resource *res, *res2;
182 int base, rid, rid2, flags;
185 base = isa_get_port(dev);
186 flags = device_get_flags(dev);
188 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, base + 0x100,
189 base + 0x107, 8, RF_ACTIVE);
197 * Check for the presence of some GUS card. Reset the card,
198 * then see if we can access the memory on it.
201 port_wr(res, 3, 0x4c);
205 port_wr(res, 3, 0x4c);
213 port_wr(res, 3, 0x43); /* Register select */
214 port_wr(res, 4, 0); /* Low addr */
215 port_wr(res, 5, 0); /* Med addr */
217 port_wr(res, 3, 0x44); /* Register select */
218 port_wr(res, 4, 0); /* High addr */
219 port_wr(res, 7, 0x55); /* DRAM */
221 /* Read from DRAM. */
223 port_wr(res, 3, 0x43); /* Register select */
224 port_wr(res, 4, 0); /* Low addr */
225 port_wr(res, 5, 0); /* Med addr */
227 port_wr(res, 3, 0x44); /* Register select */
228 port_wr(res, 4, 0); /* High addr */
229 val = port_rd(res, 7); /* DRAM */
237 res2 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid2, base, base, 1,
244 port_wr(res2, 0x0f, 0x20);
245 val = port_rd(res2, 0x0f);
248 if (val == 0xff || (val & 0x06) == 0)
251 val = port_rd(res2, 0x506); /* XXX Out of range. */
256 bus_release_resource(dev, SYS_RES_IOPORT, rid2, res2);
257 bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
260 struct sndcard_func *func;
262 /* Looks like a GUS MAX. Set the rest of the resources. */
264 bus_set_resource(dev, SYS_RES_IOPORT, 2, base + 0x10c, 8);
266 if (flags & DV_F_DUAL_DMA)
267 bus_set_resource(dev, SYS_RES_DRQ, 1,
268 flags & DV_F_DRQ_MASK, 1);
270 /* We can support the CS4231 and MIDI devices. */
272 func = kmalloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
275 func->func = SCF_MIDI;
276 child = device_add_child(dev, "midi", -1);
277 device_set_ivars(child, func);
279 func = kmalloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
281 printf("xxx: gus pcm not attached, out of memory\n");
283 func->func = SCF_PCM;
284 child = device_add_child(dev, "pcm", -1);
285 device_set_ivars(child, func);
287 device_set_desc(dev, "Gravis UltraSound MAX");
292 * TODO: Support even older GUS cards. MIDI should work on
299 bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
304 gusc_attach(device_t dev)
310 scp = device_get_softc(dev);
311 unit = device_get_unit(dev);
313 bzero(scp, sizeof(*scp));
316 if (alloc_resource(scp)) {
317 release_resource(scp);
321 if (scp->irq != NULL) {
322 bus_setup_intr(dev, scp->irq, 0, gusc_intr, scp, &ih, NULL);
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 u_long start, u_long end, u_long 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,
426 int flags, driver_intr_t *intr, void *arg,
427 void **cookiep, lwkt_serialize_t serializer)
429 sc_p scp = (sc_p)device_get_softc(dev);
432 devclass = device_get_devclass(child);
433 if (strcmp(devclass_get_name(devclass), "midi") == 0) {
434 scp->midi_intr.intr = intr;
435 scp->midi_intr.arg = arg;
437 } else if (strcmp(devclass_get_name(devclass), "pcm") == 0) {
438 scp->pcm_intr.intr = intr;
439 scp->pcm_intr.arg = arg;
442 return bus_generic_setup_intr(dev, child, irq, flags, intr,
443 arg, cookiep, serializer);
447 find_masterdev(sc_p scp)
453 devclass = device_get_devclass(scp->dev);
454 units = devclass_get_maxunit(devclass);
456 for (i = 0 ; i < units ; i++) {
457 dev = devclass_get_device(devclass, i);
458 if (isa_get_vendorid(dev) == isa_get_vendorid(scp->dev)
459 && isa_get_logicalid(dev) == LOGICALID_PCM
460 && isa_get_serial(dev) == isa_get_serial(scp->dev))
469 static int io_range[3] = {0x10, 0x8 , 0x4 };
470 static int io_offset[3] = {0x0 , 0x100, 0x10c};
472 alloc_resource(sc_p scp)
474 int i, base, lid, flags;
478 if (isa_get_vendorid(scp->dev))
479 lid = isa_get_logicalid(scp->dev);
481 lid = LOGICALID_NOPNP;
482 flags = device_get_flags(scp->dev);
486 case LOGICALID_NOPNP: /* XXX Non-PnP */
487 if (lid == LOGICALID_NOPNP)
488 base = isa_get_port(scp->dev);
491 for (i = 0 ; i < sizeof(scp->io) / sizeof(*scp->io) ; i++) {
492 if (scp->io[i] == NULL) {
495 scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i],
496 0, ~0, io_range[i], RF_ACTIVE);
498 scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i],
500 base + io_offset[i] + io_range[i] - 1
501 , io_range[i], RF_ACTIVE);
502 if (scp->io[i] == NULL)
504 scp->io_alloced[i] = 0;
507 if (scp->irq == NULL) {
509 scp->irq = bus_alloc_resource(scp->dev, SYS_RES_IRQ, &scp->irq_rid,
510 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
511 if (scp->irq == NULL)
513 scp->irq_alloced = 0;
515 for (i = 0 ; i < sizeof(scp->drq) / sizeof(*scp->drq) ; i++) {
516 if (scp->drq[i] == NULL) {
518 if (base == 0 || i == 0)
519 scp->drq[i] = bus_alloc_resource(scp->dev, SYS_RES_DRQ, &scp->drq_rid[i],
520 0, ~0, 1, RF_ACTIVE);
521 else if ((flags & DV_F_DUAL_DMA) != 0)
522 /* XXX The secondary drq is specified in the flag. */
523 scp->drq[i] = bus_alloc_resource(scp->dev, SYS_RES_DRQ, &scp->drq_rid[i],
524 flags & DV_F_DRQ_MASK,
525 flags & DV_F_DRQ_MASK, 1, RF_ACTIVE);
526 if (scp->drq[i] == NULL)
528 scp->drq_alloced[i] = 0;
533 if (scp->io[0] == NULL) {
535 scp->io[0] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[0],
536 0, ~0, io_range[0], RF_ACTIVE);
537 if (scp->io[0] == NULL)
539 scp->io_alloced[0] = 0;
543 if (scp->io[0] == NULL) {
545 scp->io[0] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[0],
546 0, ~0, io_range[0], RF_ACTIVE);
547 if (scp->io[0] == NULL)
549 scp->io_alloced[0] = 0;
551 if (scp->irq == NULL) {
552 /* The irq is shared with pcm audio. */
553 dev = find_masterdev(scp);
557 scp->irq = BUS_ALLOC_RESOURCE(dev, NULL, SYS_RES_IRQ, &scp->irq_rid,
558 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
559 if (scp->irq == NULL)
561 scp->irq_alloced = 0;
569 release_resource(sc_p scp)
575 if (isa_get_vendorid(scp->dev))
576 lid = isa_get_logicalid(scp->dev);
578 lid = LOGICALID_NOPNP;
579 flags = device_get_flags(scp->dev);
583 case LOGICALID_NOPNP: /* XXX Non-PnP */
584 for (i = 0 ; i < sizeof(scp->io) / sizeof(*scp->io) ; i++) {
585 if (scp->io[i] != NULL) {
586 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[i], scp->io[i]);
590 if (scp->irq != NULL) {
591 bus_release_resource(scp->dev, SYS_RES_IRQ, scp->irq_rid, scp->irq);
594 for (i = 0 ; i < sizeof(scp->drq) / sizeof(*scp->drq) ; i++) {
595 if (scp->drq[i] != NULL) {
596 bus_release_resource(scp->dev, SYS_RES_DRQ, scp->drq_rid[i], scp->drq[i]);
602 if (scp->io[0] != NULL) {
603 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[0], scp->io[0]);
608 if (scp->io[0] != NULL) {
609 bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[0], scp->io[0]);
612 if (scp->irq != NULL) {
613 /* The irq is shared with pcm audio. */
614 dev = find_masterdev(scp);
617 BUS_RELEASE_RESOURCE(dev, NULL, SYS_RES_IOPORT, scp->irq_rid, scp->irq);
625 static device_method_t gusc_methods[] = {
626 /* Device interface */
627 DEVMETHOD(device_probe, gusc_probe),
628 DEVMETHOD(device_attach, gusc_attach),
629 DEVMETHOD(device_detach, bus_generic_detach),
630 DEVMETHOD(device_shutdown, bus_generic_shutdown),
631 DEVMETHOD(device_suspend, bus_generic_suspend),
632 DEVMETHOD(device_resume, bus_generic_resume),
635 DEVMETHOD(bus_print_child, bus_generic_print_child),
636 DEVMETHOD(bus_alloc_resource, gusc_alloc_resource),
637 DEVMETHOD(bus_release_resource, gusc_release_resource),
638 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
639 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
640 DEVMETHOD(bus_setup_intr, gusc_setup_intr),
641 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
646 static driver_t gusc_driver = {
649 sizeof(struct gusc_softc),
653 * gusc can be attached to an isa bus.
655 DRIVER_MODULE(snd_gusc, isa, gusc_driver, gusc_devclass, 0, 0);
656 MODULE_DEPEND(snd_gusc, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
657 MODULE_VERSION(snd_gusc, 1);