1 | /* |
---|
2 | * Copyright (c) 1992-1998 Michael A. Cooper. |
---|
3 | * This software may be freely used and distributed provided it is not |
---|
4 | * sold for profit or used in part or in whole for commercial gain |
---|
5 | * without prior written agreement, and the author is credited |
---|
6 | * appropriately. |
---|
7 | */ |
---|
8 | |
---|
9 | #ifndef lint |
---|
10 | static char *RCSid = "$Revision: 1.1.1.1 $"; |
---|
11 | #endif |
---|
12 | |
---|
13 | /* |
---|
14 | * SunOS/Solaris DeviceDB routines. |
---|
15 | * |
---|
16 | * These routines will parse the "devicedb" file that comes with |
---|
17 | * i86pc Solaris 2.x. A copy of devicedb is distributed with |
---|
18 | * this program for use on SPARC Solaris 2.x which sometimes contains |
---|
19 | * some of these devices. |
---|
20 | */ |
---|
21 | |
---|
22 | #include "defs.h" |
---|
23 | |
---|
24 | #if defined(HAVE_DEVICEDB) |
---|
25 | /* |
---|
26 | * List of devicedb files to search for |
---|
27 | */ |
---|
28 | static char *DeviceDBList[] = { |
---|
29 | /* So far (Solaris 2.6) only i86pc has /platform/.../master */ |
---|
30 | "/platform/${KArch}/boot/solaris/devicedb/master", |
---|
31 | "${ConfDir}/${OSname}_${OSver}.ddb", |
---|
32 | "${ConfDir}/${OSname}_${OSmajver}.ddb", |
---|
33 | "${ConfDir}/${OSname}.ddb", |
---|
34 | "${ConfDir}/local.ddb", |
---|
35 | NULL |
---|
36 | }; |
---|
37 | |
---|
38 | /* |
---|
39 | * Parse a line from a DeviceDB file. |
---|
40 | */ |
---|
41 | static DevInfo_t *DDBparse(String, FileName, LineNo) |
---|
42 | char *String; |
---|
43 | char *FileName; |
---|
44 | int LineNo; |
---|
45 | { |
---|
46 | char **Argv; |
---|
47 | int Argc; |
---|
48 | char **NameArgv; |
---|
49 | int NameArgc; |
---|
50 | char *Model = NULL; |
---|
51 | DevInfo_t *DevInfo = NULL; |
---|
52 | DevType_t *DevType; |
---|
53 | ClassType_t *Class; |
---|
54 | |
---|
55 | Argc = StrToArgv(String, " \t", &Argv, "\"\"", 0); |
---|
56 | if (Argc < 1) { |
---|
57 | SImsg(SIM_GERR, "%s: Line %d: No fields found.", FileName, LineNo); |
---|
58 | return((DevInfo_t *) NULL); |
---|
59 | } |
---|
60 | if (Argc < 6) { |
---|
61 | SImsg(SIM_GERR, "%s: Line %d: Wrong number of fields (%d) found.", |
---|
62 | FileName, LineNo, Argc); |
---|
63 | return((DevInfo_t *) NULL); |
---|
64 | } |
---|
65 | |
---|
66 | DevInfo = NewDevInfo(NULL); |
---|
67 | |
---|
68 | if (Argv[0]) { /* Device IDs */ |
---|
69 | NameArgc = StrToArgv(Argv[0], "|", &NameArgv, NULL, 0); |
---|
70 | if (NameArgc < 1) { |
---|
71 | SImsg(SIM_GERR, "%s: Line %d: No names found.", FileName, LineNo); |
---|
72 | return((DevInfo_t *) NULL); |
---|
73 | } |
---|
74 | DevInfo->Aliases = NameArgv; |
---|
75 | } |
---|
76 | |
---|
77 | /* |
---|
78 | * If the driver name is not 'none' use it as the device name. |
---|
79 | * This enables us to later find the real device names in the |
---|
80 | * device trees. |
---|
81 | */ |
---|
82 | if (Argv[4]) { /* Name of driver */ |
---|
83 | if (!EQ(Argv[4], "none")) { |
---|
84 | char *cp; |
---|
85 | |
---|
86 | DevInfo->Name = strdup(Argv[4]); |
---|
87 | /* Zap the extension part of driver name, if any */ |
---|
88 | if (cp = strchr(DevInfo->Name, '.')) |
---|
89 | *cp = CNULL; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | if (Argv[1] && !DevInfo->Name) { /* Device Name */ |
---|
94 | if (!EQ(Argv[1], "none")) |
---|
95 | DevInfo->Name = strdup(Argv[1]); |
---|
96 | else |
---|
97 | DevInfo->Name = strdup(NameArgv[0]); |
---|
98 | } |
---|
99 | |
---|
100 | if (Argv[2]) { /* Device Type */ |
---|
101 | if (DevType = TypeGetByName(Argv[2])) |
---|
102 | DevInfo->Type = DevType->Type; |
---|
103 | else if (Debug && !EQ(Argv[2], "oth")) |
---|
104 | SImsg(SIM_DBG, "\tDeviceDB: Type <%s> is not defined.", Argv[2]); |
---|
105 | } |
---|
106 | |
---|
107 | if (Argv[3] && !EQ(Argv[3], "all")) /* Bus Type */ |
---|
108 | if (Class = ClassTypeGetByName(DT_BUS, Argv[3])) |
---|
109 | DevInfo->ClassType = Class->Type; |
---|
110 | |
---|
111 | if (Argv[5]) /* Description/Model */ |
---|
112 | DevInfo->Model = strdup(Argv[5]); |
---|
113 | |
---|
114 | return(DevInfo); |
---|
115 | } |
---|
116 | |
---|
117 | /* |
---|
118 | * Read a DeviceDB file and parse each line. |
---|
119 | */ |
---|
120 | static DevInfo_t *DDBreadFile(FileName) |
---|
121 | char *FileName; |
---|
122 | { |
---|
123 | DevInfo_t *DevInfoTree = NULL; |
---|
124 | DevInfo_t *LastDevInfo; |
---|
125 | DevInfo_t *DevInfo; |
---|
126 | static char Buff[BUFSIZ]; |
---|
127 | register char *cp; |
---|
128 | FILE *FilePtr; |
---|
129 | register int LineNo = 0; |
---|
130 | char **Argv; |
---|
131 | int Argc; |
---|
132 | |
---|
133 | FilePtr = fopen(FileName, "r"); |
---|
134 | if (!FilePtr) { |
---|
135 | SImsg(SIM_GERR, "Cannot open DeviceDB file %s: %s.", FileName, SYSERR); |
---|
136 | return((DevInfo_t *) NULL); |
---|
137 | } |
---|
138 | |
---|
139 | SImsg(SIM_DBG, "Reading DeviceDB file `%s' . . .", FileName); |
---|
140 | |
---|
141 | while (fgets(Buff, sizeof(Buff), FilePtr)) { |
---|
142 | ++LineNo; |
---|
143 | if (Buff[0] == '#' || Buff[0] == '\n') |
---|
144 | continue; |
---|
145 | if (cp = strchr(Buff, '\n')) *cp = CNULL; |
---|
146 | if (cp = strchr(Buff, '#')) *cp = CNULL; |
---|
147 | if (!Buff[0]) |
---|
148 | continue; |
---|
149 | |
---|
150 | if (LineNo == 1) { |
---|
151 | if (EQN("version", Buff, 7)) |
---|
152 | SImsg(SIM_DBG, "\tDeviceDB %s", Buff); |
---|
153 | } else { |
---|
154 | if (DevInfo = DDBparse(Buff, FileName, LineNo)) { |
---|
155 | if (!DevInfoTree) |
---|
156 | DevInfoTree = LastDevInfo = DevInfo; |
---|
157 | else { |
---|
158 | LastDevInfo->Next = DevInfo; |
---|
159 | LastDevInfo = DevInfo; |
---|
160 | } |
---|
161 | } |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | (void) fclose(FilePtr); |
---|
166 | |
---|
167 | return(DevInfoTree); |
---|
168 | } |
---|
169 | |
---|
170 | /* |
---|
171 | * Find and parse each devicedb (ddb) file we find. |
---|
172 | */ |
---|
173 | extern DevInfo_t *DDBreadFiles() |
---|
174 | { |
---|
175 | static char ErrBuff[BUFSIZ]; |
---|
176 | register char **cpp; |
---|
177 | char *File; |
---|
178 | DevInfo_t *DevInfo; |
---|
179 | DevInfo_t *DevInfoTree = NULL; |
---|
180 | DevInfo_t *LastDevInfo = NULL; |
---|
181 | |
---|
182 | for (cpp = DeviceDBList; cpp && *cpp; ++cpp) { |
---|
183 | File = VarSub(*cpp, ErrBuff, sizeof(ErrBuff), |
---|
184 | VarGetVal, (Opaque_t)NULL); |
---|
185 | if (!File) { |
---|
186 | SImsg(SIM_GERR, "Internal error: %s", ErrBuff); |
---|
187 | continue; |
---|
188 | } |
---|
189 | if (DevInfo = DDBreadFile(File)) { |
---|
190 | if (!DevInfoTree) |
---|
191 | DevInfoTree = LastDevInfo = DevInfo; |
---|
192 | else { |
---|
193 | LastDevInfo->Next = DevInfo; |
---|
194 | LastDevInfo = DevInfo; |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | return(DevInfoTree); |
---|
200 | } |
---|
201 | |
---|
202 | /* |
---|
203 | * Get the Device Info tree from the Solaris DeviceDB file(s). |
---|
204 | */ |
---|
205 | extern DevInfo_t *DDBgetDevInfo(DevData) |
---|
206 | DevData_t *DevData; |
---|
207 | { |
---|
208 | static DevInfo_t *DevInfoDeviceDB; |
---|
209 | register DevInfo_t *Ptr; |
---|
210 | register char **cpp; |
---|
211 | |
---|
212 | if (!DevData->DevName) |
---|
213 | return((DevInfo_t *) NULL); |
---|
214 | |
---|
215 | if (!DevInfoDeviceDB) |
---|
216 | DevInfoDeviceDB = DDBreadFiles(); |
---|
217 | |
---|
218 | for (Ptr = DevInfoDeviceDB; Ptr; Ptr = Ptr->Next) { |
---|
219 | if (Ptr->Name && EQ(DevData->DevName, Ptr->Name)) |
---|
220 | return(Ptr); |
---|
221 | for (cpp = Ptr->Aliases; cpp && *cpp; ++cpp) |
---|
222 | if (EQ(DevData->DevName, *cpp)) |
---|
223 | return(Ptr); |
---|
224 | } |
---|
225 | |
---|
226 | return((DevInfo_t *) NULL); |
---|
227 | } |
---|
228 | #endif /* HAVE_DEVICEDB */ |
---|