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