ppbus_conf.c revision 1.5 1 1.5 drochner /* $NetBSD: ppbus_conf.c,v 1.5 2004/09/08 20:12:20 drochner Exp $ */
2 1.2 bjh21
3 1.1 jdolecek /*-
4 1.1 jdolecek * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
5 1.1 jdolecek * All rights reserved.
6 1.1 jdolecek *
7 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.1 jdolecek * modification, are permitted provided that the following conditions
9 1.1 jdolecek * are met:
10 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.1 jdolecek * documentation and/or other materials provided with the distribution.
15 1.1 jdolecek *
16 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jdolecek * SUCH DAMAGE.
27 1.1 jdolecek *
28 1.3 bjh21 * FreeBSD: src/sys/dev/ppbus/ppbconf.c,v 1.17.2.1 2000/05/24 00:20:57 n_hibma Exp
29 1.1 jdolecek *
30 1.1 jdolecek */
31 1.3 bjh21
32 1.3 bjh21 #include <sys/cdefs.h>
33 1.5 drochner __KERNEL_RCSID(0, "$NetBSD: ppbus_conf.c,v 1.5 2004/09/08 20:12:20 drochner Exp $");
34 1.3 bjh21
35 1.1 jdolecek #include "opt_ppbus.h"
36 1.4 jdolecek #include "opt_ppbus_1284.h"
37 1.1 jdolecek
38 1.1 jdolecek #include <sys/param.h>
39 1.1 jdolecek #include <sys/systm.h>
40 1.1 jdolecek #include <sys/kernel.h>
41 1.1 jdolecek #include <sys/device.h>
42 1.1 jdolecek #include <sys/proc.h>
43 1.1 jdolecek
44 1.1 jdolecek #include <dev/ppbus/ppbus_1284.h>
45 1.1 jdolecek #include <dev/ppbus/ppbus_base.h>
46 1.1 jdolecek #include <dev/ppbus/ppbus_conf.h>
47 1.1 jdolecek #include <dev/ppbus/ppbus_device.h>
48 1.1 jdolecek #include <dev/ppbus/ppbus_var.h>
49 1.1 jdolecek
50 1.1 jdolecek /* Probe, attach, and detach functions for ppbus. */
51 1.5 drochner static int ppbus_probe(struct device *, struct cfdata *, void *);
52 1.5 drochner static void ppbus_attach(struct device *, struct device *, void *);
53 1.5 drochner static int ppbus_detach(struct device *, int);
54 1.1 jdolecek
55 1.1 jdolecek /* Utility function prototypes */
56 1.1 jdolecek static int ppbus_print_child(void *, const char *);
57 1.5 drochner static int ppbus_search_children(struct device *, struct cfdata *,
58 1.5 drochner const locdesc_t *, void *);
59 1.1 jdolecek
60 1.1 jdolecek
61 1.1 jdolecek CFATTACH_DECL(ppbus, sizeof(struct ppbus_softc), ppbus_probe, ppbus_attach,
62 1.1 jdolecek ppbus_detach, NULL);
63 1.1 jdolecek
64 1.1 jdolecek /* Probe function for ppbus. */
65 1.1 jdolecek static int
66 1.5 drochner ppbus_probe(struct device *parent, struct cfdata *cf, void *aux)
67 1.1 jdolecek {
68 1.5 drochner struct parport_adapter *sc_link = aux;
69 1.1 jdolecek
70 1.1 jdolecek /* Check adapter for consistency */
71 1.5 drochner if (
72 1.1 jdolecek /* Required methods for all parports */
73 1.1 jdolecek sc_link->parport_io == NULL ||
74 1.1 jdolecek sc_link->parport_exec_microseq == NULL ||
75 1.1 jdolecek sc_link->parport_setmode == NULL ||
76 1.1 jdolecek sc_link->parport_getmode == NULL ||
77 1.1 jdolecek sc_link->parport_read == NULL ||
78 1.1 jdolecek sc_link->parport_write == NULL ||
79 1.1 jdolecek sc_link->parport_read_ivar == NULL ||
80 1.1 jdolecek sc_link->parport_write_ivar == NULL ||
81 1.1 jdolecek /* Methods which conditional exist based on capabilities */
82 1.1 jdolecek ((sc_link->capabilities & PPBUS_HAS_EPP) &&
83 1.1 jdolecek (sc_link->parport_reset_epp_timeout == NULL)) ||
84 1.1 jdolecek ((sc_link->capabilities & PPBUS_HAS_ECP) &&
85 1.1 jdolecek (sc_link->parport_ecp_sync == NULL)) ||
86 1.1 jdolecek ((sc_link->capabilities & PPBUS_HAS_DMA) &&
87 1.1 jdolecek (sc_link->parport_dma_malloc == NULL ||
88 1.1 jdolecek sc_link->parport_dma_free == NULL)) ||
89 1.1 jdolecek ((sc_link->capabilities & PPBUS_HAS_INTR) &&
90 1.1 jdolecek (sc_link->parport_add_handler == NULL ||
91 1.1 jdolecek sc_link->parport_remove_handler == NULL))
92 1.1 jdolecek ) {
93 1.1 jdolecek
94 1.1 jdolecek #ifdef PPBUS_DEBUG
95 1.1 jdolecek printf("%s(%s): parport_adaptor is incomplete. Child device "
96 1.1 jdolecek "probe failed.\n", __func__, parent->dv_xname);
97 1.1 jdolecek #endif
98 1.1 jdolecek return 0;
99 1.5 drochner } else {
100 1.1 jdolecek return 1;
101 1.1 jdolecek }
102 1.1 jdolecek }
103 1.1 jdolecek
104 1.1 jdolecek /* Attach function for ppbus. */
105 1.1 jdolecek static void
106 1.5 drochner ppbus_attach(struct device *parent, struct device *self, void *aux)
107 1.1 jdolecek {
108 1.5 drochner struct ppbus_softc *ppbus = (struct ppbus_softc *)self;
109 1.5 drochner struct parport_adapter *sc_link = aux;
110 1.1 jdolecek struct ppbus_attach_args args;
111 1.1 jdolecek
112 1.1 jdolecek printf("\n");
113 1.1 jdolecek
114 1.1 jdolecek /* Initialize config data from adapter (bus + device methods) */
115 1.1 jdolecek args.capabilities = ppbus->sc_capabilities = sc_link->capabilities;
116 1.1 jdolecek ppbus->ppbus_io = sc_link->parport_io;
117 1.1 jdolecek ppbus->ppbus_exec_microseq = sc_link->parport_exec_microseq;
118 1.1 jdolecek ppbus->ppbus_reset_epp_timeout = sc_link->
119 1.1 jdolecek parport_reset_epp_timeout;
120 1.1 jdolecek ppbus->ppbus_setmode = sc_link->parport_setmode;
121 1.1 jdolecek ppbus->ppbus_getmode = sc_link->parport_getmode;
122 1.1 jdolecek ppbus->ppbus_ecp_sync = sc_link->parport_ecp_sync;
123 1.1 jdolecek ppbus->ppbus_read = sc_link->parport_read;
124 1.1 jdolecek ppbus->ppbus_write = sc_link->parport_write;
125 1.1 jdolecek ppbus->ppbus_read_ivar = sc_link->parport_read_ivar;
126 1.1 jdolecek ppbus->ppbus_write_ivar = sc_link->parport_write_ivar;
127 1.1 jdolecek ppbus->ppbus_dma_malloc = sc_link->parport_dma_malloc;
128 1.1 jdolecek ppbus->ppbus_dma_free = sc_link->parport_dma_free;
129 1.1 jdolecek ppbus->ppbus_add_handler = sc_link->parport_add_handler;
130 1.1 jdolecek ppbus->ppbus_remove_handler = sc_link->parport_remove_handler;
131 1.1 jdolecek
132 1.1 jdolecek /* Initially there is no device owning the bus */
133 1.1 jdolecek ppbus->ppbus_owner = NULL;
134 1.1 jdolecek
135 1.1 jdolecek /* Initialize locking structures */
136 1.1 jdolecek lockinit(&(ppbus->sc_lock), PPBUSPRI | PCATCH, "ppbuslock", 0,
137 1.1 jdolecek LK_NOWAIT);
138 1.1 jdolecek
139 1.1 jdolecek /* Set up bus mode and ieee state */
140 1.1 jdolecek ppbus->sc_mode = ppbus->ppbus_getmode(self->dv_parent);
141 1.1 jdolecek ppbus->sc_use_ieee = 1;
142 1.1 jdolecek ppbus->sc_1284_state = PPBUS_FORWARD_IDLE;
143 1.1 jdolecek ppbus->sc_1284_error = PPBUS_NO_ERROR;
144 1.1 jdolecek
145 1.1 jdolecek /* Record device's sucessful attachment */
146 1.1 jdolecek ppbus->sc_dev_ok = PPBUS_OK;
147 1.1 jdolecek
148 1.1 jdolecek #ifndef DONTPROBE_1284
149 1.1 jdolecek /* detect IEEE1284 compliant devices */
150 1.5 drochner if (ppbus_scan_bus(self)) {
151 1.1 jdolecek printf("%s: No IEEE1284 device found.\n", self->dv_xname);
152 1.5 drochner } else {
153 1.1 jdolecek printf("%s: IEEE1284 device found.\n", self->dv_xname);
154 1.1 jdolecek /*
155 1.1 jdolecek * Detect device ID (interrupts must be disabled because we
156 1.1 jdolecek * cannot do a ltsleep() to wait for it - no context)
157 1.1 jdolecek */
158 1.5 drochner if (args.capabilities & PPBUS_HAS_INTR) {
159 1.1 jdolecek int val = 0;
160 1.1 jdolecek if(ppbus_write_ivar(self, PPBUS_IVAR_INTR, &val) != 0) {
161 1.1 jdolecek printf(" <problem initializing interrupt "
162 1.1 jdolecek "usage>");
163 1.1 jdolecek }
164 1.1 jdolecek }
165 1.1 jdolecek ppbus_pnp_detect(self);
166 1.1 jdolecek }
167 1.1 jdolecek #endif /* !DONTPROBE_1284 */
168 1.1 jdolecek
169 1.1 jdolecek /* Configure child devices */
170 1.1 jdolecek SLIST_INIT(&(ppbus->sc_childlist_head));
171 1.5 drochner config_search_ia(ppbus_search_children, self, "ppbus", &args);
172 1.1 jdolecek
173 1.1 jdolecek return;
174 1.1 jdolecek }
175 1.1 jdolecek
176 1.1 jdolecek /* Detach function for ppbus. */
177 1.1 jdolecek static int
178 1.5 drochner ppbus_detach(struct device *self, int flag)
179 1.1 jdolecek {
180 1.1 jdolecek struct ppbus_softc * ppbus = (struct ppbus_softc *) self;
181 1.1 jdolecek struct ppbus_device_softc * child;
182 1.1 jdolecek
183 1.5 drochner if (ppbus->sc_dev_ok != PPBUS_OK) {
184 1.5 drochner if (!(flag & DETACH_QUIET))
185 1.1 jdolecek printf("%s: detach called on unattached device.\n",
186 1.1 jdolecek ppbus->sc_dev.dv_xname);
187 1.5 drochner if (!(flag & DETACH_FORCE))
188 1.1 jdolecek return 0;
189 1.5 drochner if (!(flag & DETACH_QUIET))
190 1.1 jdolecek printf("%s: continuing detach (DETACH_FORCE).\n",
191 1.1 jdolecek ppbus->sc_dev.dv_xname);
192 1.1 jdolecek }
193 1.1 jdolecek
194 1.5 drochner if (lockmgr(&(ppbus->sc_lock), LK_DRAIN, NULL)) {
195 1.5 drochner if (!(flag & DETACH_QUIET))
196 1.1 jdolecek printf("%s: error while waiting for lock activity to "
197 1.1 jdolecek "end.\n", ppbus->sc_dev.dv_xname);
198 1.5 drochner if (!(flag & DETACH_FORCE))
199 1.1 jdolecek return 0;
200 1.5 drochner if (!(flag & DETACH_QUIET))
201 1.1 jdolecek printf("%s: continuing detach (DETACH_FORCE).\n",
202 1.1 jdolecek ppbus->sc_dev.dv_xname);
203 1.1 jdolecek }
204 1.1 jdolecek
205 1.1 jdolecek /* Detach children devices */
206 1.5 drochner while (!SLIST_EMPTY(&(ppbus->sc_childlist_head))) {
207 1.1 jdolecek child = SLIST_FIRST(&(ppbus->sc_childlist_head));
208 1.1 jdolecek config_deactivate((struct device *)child);
209 1.5 drochner if (config_detach((struct device *)child, flag)) {
210 1.1 jdolecek if(!(flag & DETACH_QUIET))
211 1.1 jdolecek printf("%s: error detaching %s.",
212 1.1 jdolecek ppbus->sc_dev.dv_xname,
213 1.1 jdolecek child->sc_dev.dv_xname);
214 1.1 jdolecek if(!(flag & DETACH_FORCE))
215 1.1 jdolecek return 0;
216 1.1 jdolecek if(!(flag & DETACH_QUIET))
217 1.1 jdolecek printf("%s: continuing (DETACH_FORCE).\n",
218 1.1 jdolecek ppbus->sc_dev.dv_xname);
219 1.1 jdolecek }
220 1.1 jdolecek SLIST_REMOVE_HEAD(&(ppbus->sc_childlist_head), entries);
221 1.1 jdolecek }
222 1.1 jdolecek
223 1.5 drochner if (!(flag & DETACH_QUIET))
224 1.1 jdolecek printf("%s: detached.\n", ppbus->sc_dev.dv_xname);
225 1.1 jdolecek
226 1.1 jdolecek return 1;
227 1.1 jdolecek }
228 1.1 jdolecek
229 1.1 jdolecek
230 1.1 jdolecek /* Utility functions */
231 1.1 jdolecek
232 1.1 jdolecek /* Used by config_found_sm() to print out result of child probe */
233 1.1 jdolecek static int
234 1.5 drochner ppbus_print_child(void *aux, const char *name)
235 1.1 jdolecek {
236 1.5 drochner
237 1.5 drochner if (name != NULL) {
238 1.1 jdolecek printf("%s: child devices", name);
239 1.1 jdolecek return UNCONF;
240 1.5 drochner } else {
241 1.1 jdolecek return QUIET;
242 1.1 jdolecek }
243 1.1 jdolecek }
244 1.1 jdolecek
245 1.1 jdolecek /* Search for children device and add to list */
246 1.1 jdolecek static int
247 1.5 drochner ppbus_search_children(struct device *parent, struct cfdata *cf,
248 1.5 drochner const locdesc_t *ldesc, void *aux)
249 1.1 jdolecek {
250 1.5 drochner struct ppbus_softc *ppbus = (struct ppbus_softc *)parent;
251 1.5 drochner struct ppbus_device_softc *child;
252 1.1 jdolecek int rval = 0;
253 1.1 jdolecek
254 1.5 drochner if (config_match(parent, cf, aux) > 0) {
255 1.1 jdolecek child = (struct ppbus_device_softc *) config_attach(parent,
256 1.1 jdolecek cf, aux, ppbus_print_child);
257 1.5 drochner if (child) {
258 1.1 jdolecek SLIST_INSERT_HEAD(&(ppbus->sc_childlist_head), child,
259 1.1 jdolecek entries);
260 1.1 jdolecek rval = 1;
261 1.1 jdolecek }
262 1.1 jdolecek }
263 1.1 jdolecek
264 1.1 jdolecek return rval;
265 1.1 jdolecek }
266 1.1 jdolecek
267