kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / usr.sbin / config / mkioconf.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)mkioconf.c       8.2 (Berkeley) 1/21/94
34  * $FreeBSD: src/usr.sbin/config/mkioconf.c,v 1.62 2000/01/29 18:14:59 peter Exp $
35  * $DragonFly: src/usr.sbin/config/mkioconf.c,v 1.3 2003/08/07 21:19:25 dillon Exp $
36  */
37
38 #include <err.h>
39 #include <stdio.h>
40 #include "y.tab.h"
41 #include "config.h"
42
43 /*
44  * build the ioconf.c file
45  */
46
47 static char *
48 devstr(struct device *dp)
49 {
50     static char buf[100];
51
52     if (dp->d_unit >= 0) {
53         snprintf(buf, sizeof(buf), "%s%d", dp->d_name, dp->d_unit);
54         return buf;
55     } else
56         return dp->d_name;
57 }
58
59 static void
60 write_device_resources(FILE *fp, struct device *dp)
61 {
62     int count = 0;
63     char buf[80];
64
65     fprintf(fp, "struct config_resource %s_resources[] = {\n", devstr(dp));
66     if (dp->d_conn) {
67         if (dp->d_connunit >= 0)
68             snprintf(buf, sizeof(buf), "%s%d", dp->d_conn, dp->d_connunit);
69         else
70             snprintf(buf, sizeof(buf), "%s", dp->d_conn);
71         fprintf(fp, "\t{ \"at\",\tRES_STRING,\t{ (long)\"%s\" }},\n", buf);
72         count++;
73     }
74     if (dp->d_drive != -2) {
75         fprintf(fp, "\t{ \"drive\",\tRES_INT,\t{ %d }},\n", dp->d_drive);
76         count++;
77     }
78     if (dp->d_target != -2) {
79         fprintf(fp, "\t{ \"target\",\tRES_INT,\t{ %d }},\n", dp->d_target);
80         count++;
81     }
82     if (dp->d_lun != -2) {
83         fprintf(fp, "\t{ \"lun\",\tRES_INT,\t{ %d }},\n", dp->d_lun);
84         count++;
85     }
86     if (dp->d_bus != -2) {
87         fprintf(fp, "\t{ \"bus\",\tRES_INT,\t{ %d }},\n", dp->d_bus);
88         count++;
89     }
90     if (dp->d_flags) {
91         fprintf(fp, "\t{ \"flags\",\tRES_INT,\t{ 0x%x }},\n", dp->d_flags);
92         count++;
93     }
94     if (dp->d_disabled) {
95         fprintf(fp, "\t{ \"disabled\",\tRES_INT,\t{ %d }},\n", dp->d_disabled);
96         count++;
97     }
98     if (dp->d_port) {
99         fprintf(fp, "\t{ \"port\",\tRES_INT,\t { %s }},\n", dp->d_port);
100         count++;
101     }
102     if (dp->d_portn > 0) {
103         fprintf(fp, "\t{ \"port\",\tRES_INT,\t{ 0x%x }},\n", dp->d_portn);
104         count++;
105     }
106     if (dp->d_maddr > 0) {
107         fprintf(fp, "\t{ \"maddr\",\tRES_INT,\t{ 0x%x }},\n", dp->d_maddr);
108         count++;
109     }
110     if (dp->d_msize > 0) {
111         fprintf(fp, "\t{ \"msize\",\tRES_INT,\t{ 0x%x }},\n", dp->d_msize);
112         count++;
113     }
114     if (dp->d_drq >= 0) {
115         fprintf(fp, "\t{ \"drq\",\tRES_INT,\t{ %d }},\n", dp->d_drq);
116         count++;
117     }
118     if (dp->d_irq > 0) {
119         fprintf(fp, "\t{ \"irq\",\tRES_INT,\t{ %d }},\n", dp->d_irq);
120         count++;
121     }
122     fprintf(fp, "};\n");
123     fprintf(fp, "#define %s_count %d\n", devstr(dp), count);
124 }
125
126 static void
127 write_all_device_resources(FILE *fp)
128 {
129         struct device *dp;
130
131         for (dp = dtab; dp != 0; dp = dp->d_next) {
132                 if (dp->d_type != DEVICE)
133                         continue;
134                 if (dp->d_unit == UNKNOWN)
135                         continue;
136                 write_device_resources(fp, dp);
137         }
138 }
139
140 static void
141 write_devtab(FILE *fp)
142 {
143         struct device *dp;
144         int count;
145
146         write_all_device_resources(fp);
147
148         count = 0;
149         fprintf(fp, "struct config_device config_devtab[] = {\n");
150         for (dp = dtab; dp != 0; dp = dp->d_next) {
151                 char* n = devstr(dp);
152                 if (dp->d_type != DEVICE)
153                         continue;
154                 if (dp->d_unit == UNKNOWN)
155                         continue;
156                 fprintf(fp, "\t{ \"%s\",\t%d,\t%s_count,\t%s_resources },\n",
157                         dp->d_name, dp->d_unit, n, n);
158                 count++;
159         }
160         fprintf(fp, "};\n");
161         fprintf(fp, "int devtab_count = %d;\n", count);
162 }
163
164 void
165 newbus_ioconf()
166 {
167         FILE *fp;
168
169         fp = fopen(path("ioconf.c.new"), "w");
170         if (fp == 0)
171                 err(1, "%s", path("ioconf.c.new"));
172         fprintf(fp, "/*\n");
173         fprintf(fp, " * I/O configuration.\n");
174         fprintf(fp, " * DO NOT EDIT-- this file is automatically generated.\n");
175         fprintf(fp, " */\n");
176         fprintf(fp, "\n");
177         fprintf(fp, "#include <sys/param.h>\n");
178         fprintf(fp, "\n");
179         fprintf(fp, "/*\n");
180         fprintf(fp, " * New bus architecture devices.\n");
181         fprintf(fp, " */\n");
182         fprintf(fp, "\n");
183         fprintf(fp, "#include <sys/queue.h>\n");
184         fprintf(fp, "#include <sys/sysctl.h>\n");
185         if (machine == MACHINE_PC98)
186                 fprintf(fp, "#include <pc98/pc98/pc98.h>\n");
187         else
188                 fprintf(fp, "#include <bus/isa/isareg.h>\n");
189         fprintf(fp, "#include <sys/bus_private.h>\n");
190         fprintf(fp, "\n");
191
192         write_devtab(fp);
193
194         (void) fclose(fp);
195         moveifchanged(path("ioconf.c.new"), path("ioconf.c"));
196 }