1 /* $NetBSD: mkdevsw.c,v 1.6 2008/04/28 20:24:12 martin Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by MAEKAWA Masahide (gehenna@NetBSD.org).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
43 static void emitconv(FILE *);
44 static void emitdev(FILE *);
45 static void emitdevm(FILE *);
46 static void emitheader(FILE *);
53 if ((fp
= fopen("devsw.c.tmp", "w")) == NULL
) {
54 warn("cannot create devsw.c");
65 warn("error writing devsw.c");
72 if (moveifchanged("devsw.c.tmp", "devsw.c") != 0) {
73 warn("error renaming devsw.c");
83 autogen_comment(fp
, "devsw.c");
85 fputs("#include <sys/param.h>\n"
86 "#include <sys/conf.h>\n"
87 "\n#define\tDEVSW_ARRAY_SIZE(x)\t"
88 "(sizeof((x))/sizeof((x)[0]))\n", fp
);
92 * Emit device switch table for character/block device.
101 fputs("\n/* device switch table for block device */\n", fp
);
103 for (i
= 0 ; i
<= maxbdevm
; i
++) {
104 (void)snprintf(mstr
, sizeof(mstr
), "%d", i
);
105 if ((dm
= ht_lookup(bdevmtab
, intern(mstr
))) == NULL
)
108 fprintf(fp
, "extern const struct bdevsw %s_bdevsw;\n",
112 fputs("\nconst struct bdevsw *bdevsw0[] = {\n", fp
);
114 for (i
= 0 ; i
<= maxbdevm
; i
++) {
115 (void)snprintf(mstr
, sizeof(mstr
), "%d", i
);
116 if ((dm
= ht_lookup(bdevmtab
, intern(mstr
))) == NULL
) {
117 fprintf(fp
, "\tNULL,\n");
119 fprintf(fp
, "\t&%s_bdevsw,\n", dm
->dm_name
);
123 fputs("};\n\nconst struct bdevsw **bdevsw = bdevsw0;\n", fp
);
125 fputs("const int sys_bdevsws = DEVSW_ARRAY_SIZE(bdevsw0);\n"
126 "int max_bdevsws = DEVSW_ARRAY_SIZE(bdevsw0);\n", fp
);
128 fputs("\n/* device switch table for character device */\n", fp
);
130 for (i
= 0 ; i
<= maxcdevm
; i
++) {
131 (void)snprintf(mstr
, sizeof(mstr
), "%d", i
);
132 if ((dm
= ht_lookup(cdevmtab
, intern(mstr
))) == NULL
)
135 fprintf(fp
, "extern const struct cdevsw %s_cdevsw;\n",
139 fputs("\nconst struct cdevsw *cdevsw0[] = {\n", fp
);
141 for (i
= 0 ; i
<= maxcdevm
; i
++) {
142 (void)snprintf(mstr
, sizeof(mstr
), "%d", i
);
143 if ((dm
= ht_lookup(cdevmtab
, intern(mstr
))) == NULL
) {
144 fprintf(fp
, "\tNULL,\n");
146 fprintf(fp
, "\t&%s_cdevsw,\n", dm
->dm_name
);
150 fputs("};\n\nconst struct cdevsw **cdevsw = cdevsw0;\n", fp
);
152 fputs("const int sys_cdevsws = DEVSW_ARRAY_SIZE(cdevsw0);\n"
153 "int max_cdevsws = DEVSW_ARRAY_SIZE(cdevsw0);\n", fp
);
157 * Emit device major conversion table.
164 fputs("\n/* device conversion table */\n"
165 "struct devsw_conv devsw_conv0[] = {\n", fp
);
166 TAILQ_FOREACH(dm
, &alldevms
, dm_next
) {
167 fprintf(fp
, "\t{ \"%s\", %d, %d },\n", dm
->dm_name
,
168 dm
->dm_bmajor
, dm
->dm_cmajor
);
171 "struct devsw_conv *devsw_conv = devsw_conv0;\n"
172 "int max_devsw_convs = DEVSW_ARRAY_SIZE(devsw_conv0);\n",
177 * Emit specific device major informations.
187 (void)strlcpy(mstr
, "swap", sizeof(mstr
));
188 if ((dm
= ht_lookup(bdevmtab
, intern(mstr
))) != NULL
) {
189 fprintf(fp
, "const dev_t swapdev = makedev(%d, 0);\n",
193 (void)strlcpy(mstr
, "mem", sizeof(mstr
));
194 if ((dm
= ht_lookup(cdevmtab
, intern(mstr
))) == NULL
)
195 panic("memory device is not configured");
196 fprintf(fp
, "const dev_t zerodev = makedev(%d, DEV_ZERO);\n",
199 fputs("\n/* mem_no is only used in iskmemdev() */\n", fp
);
200 fprintf(fp
, "const int mem_no = %d;\n", dm
->dm_cmajor
);