1223486Shselasky/* $FreeBSD: releng/11.0/tools/tools/bus_autoconf/bus_autoconf.c 228975 2011-12-30 00:04:11Z uqs $ */
2223486Shselasky
3223486Shselasky/*-
4223486Shselasky * Copyright (c) 2011 Hans Petter Selasky. All rights reserved.
5223486Shselasky *
6223486Shselasky * Redistribution and use in source and binary forms, with or without
7223486Shselasky * modification, are permitted provided that the following conditions
8223486Shselasky * are met:
9223486Shselasky * 1. Redistributions of source code must retain the above copyright
10223486Shselasky *    notice, this list of conditions and the following disclaimer.
11223486Shselasky * 2. Redistributions in binary form must reproduce the above copyright
12223486Shselasky *    notice, this list of conditions and the following disclaimer in the
13223486Shselasky *    documentation and/or other materials provided with the distribution.
14223486Shselasky *
15223486Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16223486Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17223486Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18223486Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19223486Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20223486Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21223486Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22223486Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23223486Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24223486Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25223486Shselasky * SUCH DAMAGE.
26223486Shselasky */
27223486Shselasky
28223486Shselasky/*
29223486Shselasky * Disclaimer: This utility and format is subject to change and not a
30228975Suqs * committed interface.
31223486Shselasky */
32223486Shselasky
33223486Shselasky#include <stdio.h>
34223486Shselasky#include <stdint.h>
35223486Shselasky#include <stdlib.h>
36223534Shselasky#include <string.h>
37223534Shselasky#include <err.h>
38223534Shselasky#include <sysexits.h>
39223486Shselasky#include <unistd.h>
40223486Shselasky
41223486Shselasky#include "bus_autoconf.h"
42223534Shselasky#include "bus_sections.h"
43223534Shselasky#include "bus_load_file.h"
44223534Shselasky#include "bus_usb.h"
45223486Shselasky
46223486Shselaskystatic void
47223486Shselaskyusage(void)
48223486Shselasky{
49223486Shselasky	fprintf(stderr,
50223486Shselasky	    "bus_autoconf - devd config file generator\n"
51223534Shselasky	    "	-i <structure_type,module.ko>\n"
52223534Shselasky	    "	-F <format_file>\n"
53223486Shselasky	    "	-h show usage\n"
54223486Shselasky	);
55223486Shselasky	exit(EX_USAGE);
56223486Shselasky}
57223486Shselasky
58223486Shselaskyint
59223486Shselaskymain(int argc, char **argv)
60223486Shselasky{
61223534Shselasky	const char *params = "i:F:h";
62223534Shselasky	char *fname;
63223534Shselasky	char *section;
64223534Shselasky	char *module;
65223534Shselasky	char *postfix;
66223534Shselasky	uint8_t *ptr;
67223534Shselasky	uint32_t len;
68223486Shselasky	int c;
69223534Shselasky	int any_opt = 0;
70223486Shselasky
71223486Shselasky	while ((c = getopt(argc, argv, params)) != -1) {
72223486Shselasky		switch (c) {
73223486Shselasky		case 'i':
74223534Shselasky			fname = optarg;
75223534Shselasky			load_file(fname, &ptr, &len);
76223534Shselasky
77223534Shselasky			module = strchr(fname, ',');
78223534Shselasky			if (module == NULL) {
79223534Shselasky				errx(EX_USAGE, "Invalid input "
80223534Shselasky				    "file name '%s'", fname);
81223534Shselasky			}
82223534Shselasky			/* split module and section */
83223534Shselasky			*module++ = 0;
84223534Shselasky
85223534Shselasky			/* remove postfix */
86223534Shselasky			postfix = strchr(module, '.');
87223534Shselasky			if (postfix)
88223534Shselasky				*postfix = 0;
89223534Shselasky
90223534Shselasky			/* get section name */
91223534Shselasky			section = fname;
92223534Shselasky
93223534Shselasky			/* check section type */
94223534Shselasky			if (strncmp(section, "usb_", 4) == 0)
95223534Shselasky				usb_import_entries(section, module, ptr, len);
96223534Shselasky			else
97223534Shselasky				errx(EX_USAGE, "Invalid section '%s'", section);
98223534Shselasky
99223534Shselasky			free(ptr);
100223534Shselasky
101223534Shselasky			any_opt = 1;
102223486Shselasky			break;
103223534Shselasky
104223534Shselasky		case 'F':
105223534Shselasky			fname = optarg;
106223534Shselasky			load_file(fname, &ptr, &len);
107223534Shselasky			format_parse_entries(ptr, len);
108223534Shselasky			free(ptr);
109223534Shselasky
110223534Shselasky			any_opt = 1;
111223486Shselasky			break;
112223534Shselasky
113223486Shselasky		default:
114223486Shselasky			usage();
115223486Shselasky			break;
116223486Shselasky		}
117223486Shselasky	}
118223486Shselasky
119223534Shselasky	if (any_opt == 0)
120223486Shselasky		usage();
121223486Shselasky
122223534Shselasky	usb_dump_entries();
123223486Shselasky
124223486Shselasky	return (0);
125223486Shselasky}
126