1998-09-04 02:43:26 +00:00
|
|
|
/*
|
|
|
|
* mjs copyright
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* "Plug and Play" functionality.
|
|
|
|
*
|
|
|
|
* We use the PnP enumerators to obtain identifiers for installed hardware,
|
|
|
|
* and the contents of a database to determine modules to be loaded to support
|
|
|
|
* such hardware.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stand.h>
|
1998-09-14 18:27:06 +00:00
|
|
|
#include <string.h>
|
1998-09-04 02:43:26 +00:00
|
|
|
#include <bootstrap.h>
|
|
|
|
|
1998-10-21 20:07:05 +00:00
|
|
|
STAILQ_HEAD(,pnpinfo) pnp_devices;
|
|
|
|
static int pnp_devices_initted = 0;
|
1998-09-04 02:43:26 +00:00
|
|
|
|
1998-10-21 20:07:05 +00:00
|
|
|
static void pnp_discard(void);
|
1998-09-14 18:27:06 +00:00
|
|
|
static int pnp_readconf(char *path);
|
|
|
|
static int pnp_scankernel(void);
|
1998-09-04 02:43:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform complete enumeration sweep, and load required module(s) if possible.
|
|
|
|
*/
|
|
|
|
|
1998-09-14 18:27:06 +00:00
|
|
|
COMMAND_SET(pnpscan, "pnpscan", "scan for PnP devices", pnp_scan);
|
|
|
|
|
1998-09-04 02:43:26 +00:00
|
|
|
int
|
1998-09-14 18:27:06 +00:00
|
|
|
pnp_scan(int argc, char *argv[])
|
1998-09-04 02:43:26 +00:00
|
|
|
{
|
1998-10-21 20:07:05 +00:00
|
|
|
struct pnpinfo *pi;
|
1998-09-14 18:27:06 +00:00
|
|
|
int hdlr;
|
1998-10-21 20:07:05 +00:00
|
|
|
int verbose;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
if (pnp_devices_initted == 0) {
|
|
|
|
STAILQ_INIT(&pnp_devices);
|
|
|
|
pnp_devices_initted = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
verbose = 0;
|
|
|
|
optind = 1;
|
|
|
|
while ((ch = getopt(argc, argv, "v")) != -1) {
|
|
|
|
switch(ch) {
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
/* getopt has already reported an error */
|
|
|
|
return(CMD_OK);
|
|
|
|
}
|
|
|
|
}
|
1998-09-04 02:43:26 +00:00
|
|
|
|
|
|
|
/* forget anything we think we knew */
|
1998-10-21 20:07:05 +00:00
|
|
|
pnp_discard();
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
pager_open();
|
1998-09-04 02:43:26 +00:00
|
|
|
|
|
|
|
/* iterate over all of the handlers */
|
1998-09-14 18:27:06 +00:00
|
|
|
for (hdlr = 0; pnphandlers[hdlr] != NULL; hdlr++) {
|
1998-10-21 20:07:05 +00:00
|
|
|
if (verbose) {
|
|
|
|
pager_output("Probing ");
|
|
|
|
pager_output(pnphandlers[hdlr]->pp_name);
|
|
|
|
pager_output("...\n");
|
|
|
|
}
|
|
|
|
pnphandlers[hdlr]->pp_enumerate();
|
|
|
|
}
|
|
|
|
if (verbose) {
|
|
|
|
for (pi = pnp_devices.stqh_first; pi != NULL; pi = pi->pi_link.stqe_next) {
|
|
|
|
pager_output(pi->pi_ident.stqh_first->id_ident); /* first ident should be canonical */
|
|
|
|
if (pi->pi_desc != NULL) {
|
|
|
|
pager_output(" : ");
|
|
|
|
pager_output(pi->pi_desc);
|
|
|
|
pager_output("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pager_close();
|
1998-09-04 02:43:26 +00:00
|
|
|
}
|
1998-09-14 18:27:06 +00:00
|
|
|
return(CMD_OK);
|
1998-09-04 02:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to load outstanding modules (eg. after disk change)
|
|
|
|
*/
|
|
|
|
int
|
1998-09-14 18:27:06 +00:00
|
|
|
pnp_reload(char *fname)
|
1998-09-04 02:43:26 +00:00
|
|
|
{
|
|
|
|
struct pnpinfo *pi;
|
|
|
|
char *modfname;
|
|
|
|
|
1998-09-14 18:27:06 +00:00
|
|
|
/* find anything? */
|
1998-10-21 20:07:05 +00:00
|
|
|
if (pnp_devices.stqh_first != NULL) {
|
1998-09-14 18:27:06 +00:00
|
|
|
|
|
|
|
/* check for kernel, assign modules handled by static drivers there */
|
|
|
|
if (pnp_scankernel()) {
|
|
|
|
command_errmsg = "cannot load drivers until kernel loaded";
|
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
|
|
|
if (fname == NULL) {
|
|
|
|
/* default paths */
|
|
|
|
pnp_readconf("/boot/pnpdata.local");
|
|
|
|
pnp_readconf("/boot/pnpdata");
|
|
|
|
} else {
|
|
|
|
if (pnp_readconf("fname")) {
|
|
|
|
sprintf(command_errbuf, "can't read PnP information from '%s'", fname);
|
|
|
|
return(CMD_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try to load any modules that have been nominated */
|
1998-10-21 20:07:05 +00:00
|
|
|
for (pi = pnp_devices.stqh_first; pi != NULL; pi = pi->pi_link.stqe_next) {
|
1998-09-14 18:27:06 +00:00
|
|
|
/* Already loaded? */
|
|
|
|
if ((pi->pi_module != NULL) && (mod_findmodule(pi->pi_module, NULL) == NULL)) {
|
|
|
|
modfname = malloc(strlen(pi->pi_module + 3));
|
|
|
|
sprintf(modfname, "%s.ko", pi->pi_module); /* XXX implicit knowledge of KLD module filenames */
|
|
|
|
if (mod_load(pi->pi_module, pi->pi_argc, pi->pi_argv))
|
1998-10-21 20:07:05 +00:00
|
|
|
printf("Could not load module '%s' for device '%s'\n", modfname, pi->pi_ident.stqh_first->id_ident);
|
1998-09-14 18:27:06 +00:00
|
|
|
free(modfname);
|
|
|
|
}
|
1998-09-04 02:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
1998-09-14 18:27:06 +00:00
|
|
|
return(CMD_OK);
|
1998-09-04 02:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-09-14 18:27:06 +00:00
|
|
|
* Throw away anything we think we know about PnP devices on (list)
|
1998-09-04 02:43:26 +00:00
|
|
|
*/
|
|
|
|
static void
|
1998-10-21 20:07:05 +00:00
|
|
|
pnp_discard(void)
|
1998-09-04 02:43:26 +00:00
|
|
|
{
|
|
|
|
struct pnpinfo *pi;
|
1998-10-21 20:07:05 +00:00
|
|
|
|
|
|
|
while (pnp_devices.stqh_first != NULL) {
|
|
|
|
pi = pnp_devices.stqh_first;
|
|
|
|
STAILQ_REMOVE_HEAD(&pnp_devices, pi_link);
|
|
|
|
pnp_freeinfo(pi);
|
1998-09-04 02:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The PnP configuration database consists of a flat text file with
|
|
|
|
* entries one per line. Valid lines are:
|
|
|
|
*
|
|
|
|
* # <text>
|
|
|
|
*
|
|
|
|
* This line is a comment, and ignored.
|
|
|
|
*
|
|
|
|
* [<name>]
|
|
|
|
*
|
|
|
|
* Entries following this line are for devices connected to the
|
|
|
|
* bus <name>, At least one such entry must be encountered
|
|
|
|
* before identifiers are recognised.
|
|
|
|
*
|
|
|
|
* ident=<identifier> rev=<revision> module=<module> args=<arguments>
|
|
|
|
*
|
|
|
|
* This line describes an identifier:module mapping. The 'ident'
|
|
|
|
* and 'module' fields are required; the 'rev' field is currently
|
|
|
|
* ignored (but should be used), and the 'args' field must come
|
|
|
|
* last.
|
1998-09-26 01:29:13 +00:00
|
|
|
*
|
|
|
|
* Comments may be appended to lines; any character including or following
|
|
|
|
* '#' on a line is ignored.
|
1998-09-04 02:43:26 +00:00
|
|
|
*/
|
1998-09-14 18:27:06 +00:00
|
|
|
static int
|
1998-09-04 02:43:26 +00:00
|
|
|
pnp_readconf(char *path)
|
|
|
|
{
|
|
|
|
struct pnpinfo *pi;
|
1998-09-14 18:27:06 +00:00
|
|
|
struct pnpident *id;
|
1998-09-04 02:43:26 +00:00
|
|
|
int fd, line;
|
|
|
|
char lbuf[128], *currbus, *ident, *revision, *module, *args;
|
|
|
|
char *cp, *ep, *tp, c;
|
|
|
|
|
|
|
|
/* try to open the file */
|
|
|
|
if ((fd = open(path, O_RDONLY)) >= 0) {
|
|
|
|
line = 0;
|
|
|
|
currbus = NULL;
|
|
|
|
|
|
|
|
while (fgetstr(lbuf, sizeof(lbuf), fd) > 0) {
|
|
|
|
line++;
|
|
|
|
/* Find the first non-space character on the line */
|
|
|
|
for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* keep/discard? */
|
|
|
|
if ((*cp == 0) || (*cp == '#'))
|
|
|
|
continue;
|
1998-09-26 01:29:13 +00:00
|
|
|
|
|
|
|
/* cut trailing comment? */
|
|
|
|
if ((ep = strchr(cp, '#')) != NULL)
|
|
|
|
*ep = 0;
|
1998-09-04 02:43:26 +00:00
|
|
|
|
|
|
|
/* bus declaration? */
|
|
|
|
if (*cp == '[') {
|
|
|
|
if (((ep = strchr(cp, ']')) == NULL) || ((ep - cp) < 2)) {
|
|
|
|
printf("%s line %d: bad bus specification\n", path, line);
|
|
|
|
} else {
|
|
|
|
if (currbus != NULL)
|
|
|
|
free(currbus);
|
|
|
|
*ep = 0;
|
|
|
|
currbus = strdup(cp + 1);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX should we complain? */
|
|
|
|
if (currbus == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* mapping */
|
1998-09-14 18:27:06 +00:00
|
|
|
for (ident = module = args = revision = NULL; *cp != 0;) {
|
1998-09-04 02:43:26 +00:00
|
|
|
|
|
|
|
/* discard leading whitespace */
|
|
|
|
if (isspace(*cp)) {
|
|
|
|
cp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* scan for terminator, separator */
|
1998-09-14 18:27:06 +00:00
|
|
|
for (ep = cp; (*ep != 0) && (*ep != '=') && !isspace(*ep); ep++)
|
1998-09-04 02:43:26 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
if (*ep == '=') {
|
|
|
|
*ep = 0;
|
1998-09-14 18:27:06 +00:00
|
|
|
for (tp = ep + 1; (*tp != 0) && !isspace(*tp); tp++)
|
1998-09-04 02:43:26 +00:00
|
|
|
;
|
|
|
|
c = *tp;
|
|
|
|
*tp = 0;
|
|
|
|
if ((ident == NULL) && !strcmp(cp, "ident")) {
|
|
|
|
ident = ep + 1;
|
|
|
|
} else if ((revision == NULL) && !strcmp(cp, "revision")) {
|
|
|
|
revision = ep + 1;
|
|
|
|
} else if ((args == NULL) && !strcmp(cp, "args")) {
|
|
|
|
*tp = c;
|
|
|
|
while (*tp != 0) /* skip to end of string */
|
|
|
|
tp++;
|
|
|
|
args = ep + 1;
|
|
|
|
} else {
|
|
|
|
/* XXX complain? */
|
|
|
|
}
|
|
|
|
cp = tp;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it's garbage or a keyword - ignore it for now */
|
|
|
|
cp = ep;
|
|
|
|
}
|
|
|
|
|
1998-09-26 01:29:13 +00:00
|
|
|
/* we must have at least ident and module set to be interesting */
|
|
|
|
if ((ident == NULL) || (module == NULL))
|
1998-09-04 02:43:26 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
1998-09-14 18:27:06 +00:00
|
|
|
* Loop looking for module/bus that might match this, but aren't already
|
|
|
|
* assigned.
|
1998-09-04 02:43:26 +00:00
|
|
|
* XXX no revision parse/test here yet.
|
|
|
|
*/
|
1998-10-21 20:07:05 +00:00
|
|
|
for (pi = pnp_devices.stqh_first; pi != NULL; pi = pi->pi_link.stqe_next) {
|
1998-09-14 18:27:06 +00:00
|
|
|
|
|
|
|
/* no driver assigned, bus matches OK */
|
|
|
|
if ((pi->pi_module == NULL) &&
|
|
|
|
!strcmp(pi->pi_handler->pp_name, currbus)) {
|
|
|
|
|
|
|
|
/* scan idents, take first match */
|
1998-10-21 20:07:05 +00:00
|
|
|
for (id = pi->pi_ident.stqh_first; id != NULL; id = id->id_link.stqe_next)
|
1998-09-14 18:27:06 +00:00
|
|
|
if (!strcmp(id->id_ident, ident))
|
1998-09-04 02:43:26 +00:00
|
|
|
break;
|
1998-09-14 18:27:06 +00:00
|
|
|
|
|
|
|
/* find a match? */
|
|
|
|
if (id != NULL) {
|
|
|
|
if (args != NULL)
|
|
|
|
if (parse(&pi->pi_argc, &pi->pi_argv, args)) {
|
|
|
|
printf("%s line %d: bad arguments\n", path, line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pi->pi_module = strdup(module);
|
|
|
|
printf("use module '%s' for %s:%s\n", module, pi->pi_handler->pp_name, id->id_ident);
|
|
|
|
}
|
1998-09-04 02:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
1998-09-14 18:27:06 +00:00
|
|
|
return(CMD_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pnp_scankernel(void)
|
|
|
|
{
|
|
|
|
return(CMD_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a unique identifier to (pi)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pnp_addident(struct pnpinfo *pi, char *ident)
|
|
|
|
{
|
1998-10-21 20:07:05 +00:00
|
|
|
struct pnpident *id;
|
|
|
|
|
|
|
|
for (id = pi->pi_ident.stqh_first; id != NULL; id = id->id_link.stqe_next)
|
|
|
|
if (!strcmp(id->id_ident, ident))
|
|
|
|
return; /* already have this one */
|
|
|
|
|
|
|
|
id = malloc(sizeof(struct pnpident));
|
|
|
|
id->id_ident = strdup(ident);
|
|
|
|
STAILQ_INSERT_TAIL(&pi->pi_ident, id, id_link);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a new pnpinfo struct
|
|
|
|
*/
|
|
|
|
struct pnpinfo *
|
|
|
|
pnp_allocinfo(void)
|
|
|
|
{
|
|
|
|
struct pnpinfo *pi;
|
1998-09-14 18:27:06 +00:00
|
|
|
|
1998-10-21 20:07:05 +00:00
|
|
|
pi = malloc(sizeof(struct pnpinfo));
|
|
|
|
bzero(pi, sizeof(struct pnpinfo));
|
|
|
|
STAILQ_INIT(&pi->pi_ident);
|
|
|
|
return(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release storage held by a pnpinfo struct
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pnp_freeinfo(struct pnpinfo *pi)
|
|
|
|
{
|
|
|
|
struct pnpident *id;
|
|
|
|
|
|
|
|
while (pi->pi_ident.stqh_first != NULL) {
|
|
|
|
id = pi->pi_ident.stqh_first;
|
|
|
|
STAILQ_REMOVE_HEAD(&pi->pi_ident, id_link);
|
|
|
|
free(id->id_ident);
|
|
|
|
free(id);
|
1998-09-14 18:27:06 +00:00
|
|
|
}
|
1998-10-21 20:07:05 +00:00
|
|
|
if (pi->pi_desc)
|
|
|
|
free(pi->pi_desc);
|
|
|
|
if (pi->pi_module)
|
|
|
|
free(pi->pi_module);
|
|
|
|
if (pi->pi_argv)
|
|
|
|
free(pi->pi_argv);
|
|
|
|
free(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new pnpinfo struct to the list.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pnp_addinfo(struct pnpinfo *pi)
|
|
|
|
{
|
|
|
|
STAILQ_INSERT_TAIL(&pnp_devices, pi, pi_link);
|
1998-09-04 02:43:26 +00:00
|
|
|
}
|
|
|
|
|