1 /* $NetBSD: ofbus.c,v 1.26 2017/03/10 00:26:43 macallan 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/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: ofbus.c,v 1.26 2017/03/10 00:26:43 macallan Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/device.h> 40 41 #include <dev/ofw/openfirm.h> 42 43 int ofbus_match(device_t, cfdata_t, void *); 44 void ofbus_attach(device_t, device_t, void *); 45 static int ofbus_print(void *, const char *); 46 47 CFATTACH_DECL_NEW(ofbus, 0, 48 ofbus_match, ofbus_attach, NULL, NULL); 49 50 static int 51 ofbus_print(void *aux, const char *pnp) 52 { 53 struct ofbus_attach_args *oba = aux; 54 55 if (pnp) 56 aprint_normal("%s at %s", oba->oba_ofname, pnp); 57 else 58 aprint_normal(" (%s)", oba->oba_ofname); 59 return UNCONF; 60 } 61 62 int 63 ofbus_match(device_t parent, cfdata_t cf, void *aux) 64 { 65 struct ofbus_attach_args *oba = aux; 66 67 if (strcmp(oba->oba_busname, "ofw")) 68 return (0); 69 if (!OF_child(oba->oba_phandle)) 70 return (0); 71 return (1); 72 } 73 74 void 75 ofbus_attach(device_t parent, device_t dev, void *aux) 76 { 77 struct ofbus_attach_args *oba = aux; 78 struct ofbus_attach_args oba2; 79 char name[64], type[64]; 80 int child, units; 81 82 printf("\n"); 83 84 /* 85 * This is a hack to make the probe work on the scsi (and ide) bus. 86 * YES, I THINK IT IS A BUG IN THE OPENFIRMWARE TO NOT PROBE ALL 87 * DEVICES ON THESE BUSSES. 88 */ 89 units = 1; 90 name[0] = 0; 91 if (OF_getprop(oba->oba_phandle, "name", name, sizeof name) > 0) { 92 if (!strcmp(name, "scsi")) 93 units = 7; /* What about wide or hostid != 7? XXX */ 94 else if (!strcmp(name, "ide")) 95 units = 2; 96 } 97 98 /* attach displays first */ 99 for (child = OF_child(oba->oba_phandle); child != 0; 100 child = OF_peer(child)) { 101 oba2.oba_busname = "ofw"; 102 type[0] = 0; 103 if (OF_getprop(child, "device_type", type, sizeof(type)) <= 0) 104 continue; 105 if (strncmp(type, "display", sizeof(type)) != 0) 106 continue; 107 of_packagename(child, name, sizeof name); 108 oba2.oba_phandle = child; 109 for (oba2.oba_unit = 0; oba2.oba_unit < units; 110 oba2.oba_unit++) { 111 if (units > 1) { 112 snprintf(oba2.oba_ofname, 113 sizeof(oba2.oba_ofname), "%s@%d", name, 114 oba2.oba_unit); 115 } else { 116 strlcpy(oba2.oba_ofname, name, 117 sizeof(oba2.oba_ofname)); 118 } 119 config_found(dev, &oba2, ofbus_print); 120 } 121 } 122 123 /* now the rest */ 124 for (child = OF_child(oba->oba_phandle); child != 0; 125 child = OF_peer(child)) { 126 oba2.oba_busname = "ofw"; 127 type[0] = 0; 128 if (OF_getprop(child, "device_type", type, sizeof(type)) > 0) { 129 if (strncmp(type, "display", sizeof(type)) == 0) 130 continue; 131 } 132 of_packagename(child, name, sizeof name); 133 oba2.oba_phandle = child; 134 for (oba2.oba_unit = 0; oba2.oba_unit < units; 135 oba2.oba_unit++) { 136 if (units > 1) { 137 snprintf(oba2.oba_ofname, 138 sizeof(oba2.oba_ofname), "%s@%d", name, 139 oba2.oba_unit); 140 } else { 141 strlcpy(oba2.oba_ofname, name, 142 sizeof(oba2.oba_ofname)); 143 } 144 config_found(dev, &oba2, ofbus_print); 145 } 146 } 147 148 } 149