#include <err.h>
#include <stdio.h>
#include "y.tab.h"
#include "config.h"
static const char *
devstr(struct device *dp)
{
static char buf[100];
if (dp->d_unit >= 0) {
snprintf(buf, sizeof(buf), "%s%d", dp->d_name, dp->d_unit);
return(buf);
} else
return(dp->d_name);
}
static void
write_device_resources(FILE *fp, struct device *dp)
{
int count = 0;
char buf[80];
fprintf(fp, "struct config_resource %s_resources[] = {\n", devstr(dp));
if (dp->d_conn) {
if (dp->d_connunit >= 0)
snprintf(buf, sizeof(buf), "%s%d", dp->d_conn, dp->d_connunit);
else
snprintf(buf, sizeof(buf), "%s", dp->d_conn);
fprintf(fp, "\t{ \"at\",\tRES_STRING,\t{ (long)\"%s\" }},\n", buf);
count++;
}
if (dp->d_drive != -2) {
fprintf(fp, "\t{ \"drive\",\tRES_INT,\t{ %d }},\n", dp->d_drive);
count++;
}
if (dp->d_target != -2) {
fprintf(fp, "\t{ \"target\",\tRES_INT,\t{ %d }},\n", dp->d_target);
count++;
}
if (dp->d_lun != -2) {
fprintf(fp, "\t{ \"lun\",\tRES_INT,\t{ %d }},\n", dp->d_lun);
count++;
}
if (dp->d_bus != -2) {
fprintf(fp, "\t{ \"bus\",\tRES_INT,\t{ %d }},\n", dp->d_bus);
count++;
}
if (dp->d_flags) {
fprintf(fp, "\t{ \"flags\",\tRES_INT,\t{ 0x%x }},\n", dp->d_flags);
count++;
}
if (dp->d_disabled) {
fprintf(fp, "\t{ \"disabled\",\tRES_INT,\t{ %d }},\n", dp->d_disabled);
count++;
}
if (dp->d_port) {
fprintf(fp, "\t{ \"port\",\tRES_INT,\t { %s }},\n", dp->d_port);
count++;
}
if (dp->d_portn > 0) {
fprintf(fp, "\t{ \"port\",\tRES_INT,\t{ 0x%x }},\n", dp->d_portn);
count++;
}
if (dp->d_maddr > 0) {
fprintf(fp, "\t{ \"maddr\",\tRES_INT,\t{ 0x%x }},\n", dp->d_maddr);
count++;
}
if (dp->d_msize > 0) {
fprintf(fp, "\t{ \"msize\",\tRES_INT,\t{ 0x%x }},\n", dp->d_msize);
count++;
}
if (dp->d_drq >= 0) {
fprintf(fp, "\t{ \"drq\",\tRES_INT,\t{ %d }},\n", dp->d_drq);
count++;
}
if (dp->d_irq > 0) {
fprintf(fp, "\t{ \"irq\",\tRES_INT,\t{ %d }},\n", dp->d_irq);
count++;
}
fprintf(fp, "};\n");
fprintf(fp, "#define %s_count %d\n", devstr(dp), count);
}
static void
write_all_device_resources(FILE *fp)
{
struct device *dp;
for (dp = dtab; dp != NULL; dp = dp->d_next) {
if (dp->d_type != DEVICE)
continue;
if (dp->d_unit == UNKNOWN)
continue;
write_device_resources(fp, dp);
}
}
static void
write_devtab(FILE *fp)
{
struct device *dp;
int count;
write_all_device_resources(fp);
count = 0;
fprintf(fp, "struct config_device config_devtab[] = {\n");
for (dp = dtab; dp != NULL; dp = dp->d_next) {
const char *n;
n = devstr(dp);
if (dp->d_type != DEVICE)
continue;
if (dp->d_unit == UNKNOWN)
continue;
fprintf(fp, "\t{ \"%s\",\t%d,\t%s_count,\t%s_resources },\n",
dp->d_name, dp->d_unit, n, n);
count++;
}
fprintf(fp, "};\n");
fprintf(fp, "int devtab_count = %d;\n", count);
}
void
newbus_ioconf(void)
{
FILE *fp;
fp = fopen(path("ioconf.c.new"), "w");
if (fp == NULL)
err(1, "%s", path("ioconf.c.new"));
fprintf(fp, "/*\n");
fprintf(fp, " * I/O configuration.\n");
fprintf(fp, " * DO NOT EDIT-- this file is automatically generated.\n");
fprintf(fp, " */\n");
fprintf(fp, "\n");
fprintf(fp, "#include <sys/param.h>\n");
fprintf(fp, "\n");
fprintf(fp, "/*\n");
fprintf(fp, " * New bus architecture devices.\n");
fprintf(fp, " */\n");
fprintf(fp, "\n");
fprintf(fp, "#include <sys/queue.h>\n");
fprintf(fp, "#include <sys/sysctl.h>\n");
fprintf(fp, "#include <bus/isa/isareg.h>\n");
fprintf(fp, "#include <sys/bus_private.h>\n");
fprintf(fp, "\n");
write_devtab(fp);
fclose(fp);
moveifchanged(path("ioconf.c.new"), path("ioconf.c"));
}