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