xref: /netbsd-src/sys/dev/isapnp/gus_isapnp.c (revision c509fb12bb33ebfbee663f4835430f41519b3222)
1 /*	$NetBSD: gus_isapnp.c,v 1.41 2020/02/29 05:51:11 isaki Exp $	*/
2 
3 /*
4  * Copyright (c) 1997, 1999, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Author: Kari Mettinen
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: gus_isapnp.c,v 1.41 2020/02/29 05:51:11 isaki Exp $");
33 
34 #include "guspnp.h"
35 #if NGUSPNP > 0
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/errno.h>
40 #include <sys/ioctl.h>
41 #include <sys/syslog.h>
42 #include <sys/device.h>
43 #include <sys/proc.h>
44 #include <sys/bus.h>
45 #include <sys/audioio.h>
46 
47 #include <dev/audio/audio_if.h>
48 
49 #include <dev/isa/isavar.h>
50 #include <dev/isa/isadmavar.h>
51 
52 #include <dev/isapnp/isapnpreg.h>
53 #include <dev/isapnp/isapnpvar.h>
54 #include <dev/isapnp/isapnpdevs.h>
55 
56 #include <dev/ic/interwavevar.h>
57 #include <dev/ic/interwavereg.h>
58 
59 
60 int	gus_isapnp_match(device_t, cfdata_t, void *);
61 void	gus_isapnp_attach(device_t, device_t, void *);
62 static int     gus_isapnp_open(void *, int);
63 
64 static const struct audio_hw_if guspnp_hw_if = {
65 	.open			= gus_isapnp_open,
66 	.close			= iwclose,
67 	.query_format		= iw_query_format,
68 	.set_format		= iw_audio_set_format,
69 	.commit_settings	= iw_commit_settings,
70 	.init_output		= iw_init_output,
71 	.init_input		= iw_init_input,
72 	.start_output		= iw_start_output,
73 	.start_input		= iw_start_input,
74 	.halt_output		= iw_halt_output,
75 	.halt_input		= iw_halt_input,
76 	.speaker_ctl		= iw_speaker_ctl,
77 	.getdev			= iw_getdev,
78 	.set_port		= iw_set_port,
79 	.get_port		= iw_get_port,
80 	.query_devinfo		= iw_query_devinfo,
81 	.allocm			= iw_malloc,
82 	.freem			= iw_free,
83 	.round_buffersize	= iw_round_buffersize,
84 	.get_props		= iw_get_props,
85 	.get_locks		= iw_get_locks,
86 };
87 
88 CFATTACH_DECL_NEW(guspnp, sizeof(struct iw_softc),
89     gus_isapnp_match, gus_isapnp_attach, NULL, NULL);
90 
91 extern struct cfdriver guspnp_cd;
92 
93 /*
94  * Probe / attach routines.
95  */
96 
97 /*
98  * Probe for the guspnp hardware.
99  *
100  * The thing has 5 separate devices on the card
101  */
102 
103 static int gus_0 = 1;		/* XXX what's this */
104 
105 int
gus_isapnp_match(device_t parent,cfdata_t match,void * aux)106 gus_isapnp_match(device_t parent, cfdata_t match, void *aux)
107 {
108 	int pri, variant;
109 
110 	pri = isapnp_devmatch(aux, &isapnp_gus_devinfo, &variant);
111 	if (pri && variant > 0)
112 		pri = 0;
113 	return pri;
114 }
115 
116 /*
117  * Attach hardware to driver, attach hardware driver to audio
118  * pseudo-device driver.
119  */
120 void
gus_isapnp_attach(device_t parent,device_t self,void * aux)121 gus_isapnp_attach(device_t parent, device_t self, void *aux)
122 {
123 	struct iw_softc *sc;
124 	struct isapnp_attach_args *ipa;
125 
126 	sc = device_private(self);
127 	ipa = aux;
128 	aprint_naive("\n");
129 	aprint_normal("\n");
130 
131 	if (!gus_0)
132 		return;
133 	gus_0 = 0;
134 
135 	if (isapnp_config(ipa->ipa_iot, ipa->ipa_memt, ipa)) {
136 		aprint_error_dev(self, "error in region allocation\n");
137 		return;
138 	}
139 
140 	sc->sc_dev = self;
141 	sc->sc_iot = ipa->ipa_iot;
142 
143 	/* handle is the region base */
144 
145 	sc->dir_h = 0; /* XXXXX */
146 	sc->p2xr = 0;
147 	sc->p2xr_h = ipa->ipa_io[0].h;
148 	sc->sc_p2xr_ic = ipa->ipa_ic;
149 	sc->p3xr = 0;
150 	sc->p3xr_h = ipa->ipa_io[1].h;
151 	sc->sc_p3xr_ic = ipa->ipa_ic;
152 	sc->codec_index = 0;
153 	sc->codec_index_h = ipa->ipa_io[2].h;
154 	sc->sc_irq = ipa->ipa_irq[0].num;
155 	sc->sc_recdrq = ipa->ipa_drq[0].num;
156 	sc->sc_playdrq = ipa->ipa_drq[1].num;
157 
158 	sc->sc_ic = ipa->ipa_ic;
159 
160 	/*
161          * Create our DMA maps.
162          */
163 	if (sc->sc_playdrq != -1) {
164 		sc->sc_play_maxsize = isa_dmamaxsize(sc->sc_ic,
165 		    sc->sc_playdrq);
166 		if (isa_drq_alloc(sc->sc_ic, sc->sc_playdrq) != 0) {
167 			aprint_error_dev(self, "can't reserve drq %d\n",
168 			    sc->sc_playdrq);
169 			return;
170 		}
171 		if (isa_dmamap_create(sc->sc_ic, sc->sc_playdrq,
172 		    sc->sc_play_maxsize, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
173 			aprint_error_dev(self, "can't create map for drq %d\n",
174 			    sc->sc_playdrq);
175 			return;
176 		}
177 	}
178 	if (sc->sc_recdrq != -1) {
179 		sc->sc_rec_maxsize = isa_dmamaxsize(sc->sc_ic,
180 		    sc->sc_recdrq);
181 		if (isa_drq_alloc(sc->sc_ic, sc->sc_recdrq) != 0) {
182 			aprint_error_dev(self, "can't reserve drq %d\n",
183 			    sc->sc_recdrq);
184 			return;
185 		}
186 		if (isa_dmamap_create(sc->sc_ic, sc->sc_recdrq,
187 		    sc->sc_rec_maxsize, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
188 			aprint_error_dev(self, "can't create map for drq %d\n",
189 			    sc->sc_recdrq);
190 			return;
191 		}
192 	}
193 
194 	/*
195          * isapnp is a child if isa, and we need isa for the DMA
196          * routines.
197          */
198 	sc->iw_cd = &guspnp_cd;
199 	sc->iw_hw_if = &guspnp_hw_if;
200 
201 	aprint_normal_dev(self, "%s %s", ipa->ipa_devident, ipa->ipa_devclass);
202 
203 	iwattach(sc);
204 }
205 
206 static int
gus_isapnp_open(void * addr,int flags)207 gus_isapnp_open(void *addr, int flags)
208 {
209 	/* open hardware */
210 	struct iw_softc *sc;
211 
212 	sc = (struct iw_softc *)addr;
213 	if (!sc)
214 		return ENXIO;
215 
216 	return iwopen(sc,flags);
217 }
218 
219 #endif /* NGUSPNP */
220