Home | History | Annotate | Line # | Download | only in ofw
ofbus.c revision 1.4
      1 /*	$NetBSD: ofbus.c,v 1.4 1997/04/16 23:32:04 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
      5  * Copyright (C) 1995, 1996 TooLs GmbH.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by TooLs GmbH.
     19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/param.h>
     35 #include <sys/device.h>
     36 
     37 #include <dev/ofw/openfirm.h>
     38 
     39 int ofbprobe __P((struct device *, struct cfdata *, void *));
     40 void ofbattach __P((struct device *, struct device *, void *));
     41 static int ofbprint __P((void *, const char *));
     42 
     43 struct cfattach ofbus_ca = {
     44 	sizeof(struct device), ofbprobe, ofbattach
     45 };
     46 
     47 struct cfdriver ofbus_cd = {
     48 	NULL, "ofbus", DV_DULL
     49 };
     50 
     51 struct cfattach ofroot_ca = {
     52 	sizeof(struct device), ofbprobe, ofbattach
     53 };
     54 
     55 struct cfdriver ofroot_cd = {
     56 	NULL, "ofroot", DV_DULL
     57 };
     58 
     59 static int
     60 ofbprint(aux, name)
     61 	void *aux;
     62 	const char *name;
     63 {
     64 	struct ofprobe *ofp = aux;
     65 	char child[64];
     66 	int l;
     67 
     68 	if ((l = OF_getprop(ofp->phandle, "name", child, sizeof child - 1)) < 0)
     69 		panic("device without name?");
     70 	if (l >= sizeof child)
     71 		l = sizeof child - 1;
     72 	child[l] = 0;
     73 
     74 	if (name)
     75 		printf("%s at %s", child, name);
     76 	else
     77 		printf(" (%s)", child);
     78 	return UNCONF;
     79 }
     80 
     81 int
     82 ofbprobe(parent, cf, aux)
     83 	struct device *parent;
     84 	struct cfdata *cf;
     85 	void *aux;
     86 {
     87 	struct ofprobe *ofp = aux;
     88 
     89 	if (!OF_child(ofp->phandle))
     90 		return 0;
     91 	return 1;
     92 }
     93 
     94 void
     95 ofbattach(parent, dev, aux)
     96 	struct device *parent, *dev;
     97 	void *aux;
     98 {
     99 	int child;
    100 	char name[5];
    101 	struct ofprobe *ofp = aux;
    102 	struct ofprobe probe;
    103 	int units;
    104 
    105 	if (!parent)
    106 		ofbprint(aux, 0);
    107 
    108 	printf("\n");
    109 
    110 	/*
    111 	 * This is a hack to make the probe work on the scsi (and ide) bus.
    112 	 * YES, I THINK IT IS A BUG IN THE OPENFIRMWARE TO NOT PROBE ALL
    113 	 * DEVICES ON THESE BUSSES.
    114 	 */
    115 	units = 1;
    116 	if (OF_getprop(ofp->phandle, "name", name, sizeof name) > 0) {
    117 		if (!strcmp(name, "scsi"))
    118 			units = 7; /* What about wide or hostid != 7?	XXX */
    119 		else if (!strcmp(name, "ide"))
    120 			units = 2;
    121 	}
    122 	for (child = OF_child(ofp->phandle); child; child = OF_peer(child)) {
    123 		/*
    124 		 * This is a hack to skip all the entries in the tree
    125 		 * that aren't devices (packages, openfirmware etc.).
    126 		 */
    127 		if (OF_getprop(child, "device_type", name, sizeof name) < 0)
    128 			continue;
    129 		probe.phandle = child;
    130 		for (probe.unit = 0; probe.unit < units; probe.unit++)
    131 			config_found(dev, &probe, ofbprint);
    132 	}
    133 }
    134 
    135 /*
    136  * Name matching routine for OpenFirmware
    137  */
    138 int
    139 ofnmmatch(cp1, cp2)
    140 	char *cp1, *cp2;
    141 {
    142 	int i;
    143 
    144 	for (i = 0; *cp2; i++)
    145 		if (*cp1++ != *cp2++)
    146 			return 0;
    147 	return i;
    148 }
    149