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