1995-08-24 09:03:04 +00:00
|
|
|
/*
|
1996-04-18 04:25:17 +00:00
|
|
|
* Copyright (c) 1995 Andrew McRae. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1995-08-24 09:03:04 +00:00
|
|
|
*/
|
1997-01-06 08:35:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Code cleanup, bug-fix and extension
|
|
|
|
* by:
|
|
|
|
* Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
|
|
|
|
* Nate Williams <nate@FreeBSD.org>
|
|
|
|
*/
|
|
|
|
|
1997-10-06 11:36:08 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
1995-08-24 09:03:04 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1995-08-25 09:46:01 +00:00
|
|
|
#include <unistd.h>
|
1995-08-24 09:03:04 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <syslog.h>
|
1997-01-06 08:35:06 +00:00
|
|
|
#ifdef SYSINSTALL
|
|
|
|
#include <dialog.h>
|
|
|
|
#endif
|
1995-08-24 09:03:04 +00:00
|
|
|
#include "cardd.h"
|
|
|
|
|
1997-01-06 08:35:06 +00:00
|
|
|
static int do_log = 0;
|
|
|
|
|
|
|
|
void
|
|
|
|
log_setup(void)
|
|
|
|
{
|
|
|
|
#ifndef SYSINSTALL
|
|
|
|
do_log = 1;
|
|
|
|
openlog("pccardd", LOG_PID, LOG_DAEMON);
|
|
|
|
#endif
|
|
|
|
}
|
1995-08-24 09:03:04 +00:00
|
|
|
|
|
|
|
void
|
1997-01-06 08:35:06 +00:00
|
|
|
log_1s(const char *fmt, ...)
|
1995-08-24 09:03:04 +00:00
|
|
|
{
|
1997-01-06 08:35:06 +00:00
|
|
|
va_list ap;
|
|
|
|
char s[256];
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsprintf(s, fmt, ap);
|
|
|
|
|
1995-08-24 09:03:04 +00:00
|
|
|
if (do_log)
|
1997-01-06 08:35:06 +00:00
|
|
|
syslog(LOG_ERR, s);
|
1996-04-10 06:49:32 +00:00
|
|
|
else {
|
1997-01-06 08:35:06 +00:00
|
|
|
#ifdef SYSINSTALL
|
|
|
|
dialog_clear();
|
|
|
|
msgConfirm(s);
|
|
|
|
#else
|
1997-10-06 11:36:08 +00:00
|
|
|
warnx("%s", s);
|
1997-01-06 08:35:06 +00:00
|
|
|
#endif
|
1996-04-10 06:49:32 +00:00
|
|
|
}
|
1995-08-24 09:03:04 +00:00
|
|
|
}
|
1996-04-10 06:49:32 +00:00
|
|
|
|
1995-08-24 09:03:04 +00:00
|
|
|
void
|
|
|
|
logerr(char *msg)
|
|
|
|
{
|
|
|
|
if (do_log)
|
|
|
|
syslog(LOG_ERR, "%s: %m", msg);
|
1997-01-06 08:35:06 +00:00
|
|
|
else {
|
|
|
|
#ifdef SYSINSTALL
|
|
|
|
dialog_clear();
|
|
|
|
msgConfirm(msg);
|
|
|
|
#else
|
1997-10-06 11:36:08 +00:00
|
|
|
warn("%s", msg);
|
1997-01-06 08:35:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
1995-08-24 09:03:04 +00:00
|
|
|
}
|
1996-04-10 06:49:32 +00:00
|
|
|
|
1995-08-24 09:03:04 +00:00
|
|
|
/*
|
|
|
|
* Deliver last will and testament, and die.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
die(char *msg)
|
|
|
|
{
|
|
|
|
if (do_log)
|
|
|
|
syslog(LOG_CRIT, "fatal error: %s", msg);
|
1997-01-06 08:35:06 +00:00
|
|
|
else {
|
|
|
|
#ifdef SYSINSTALL
|
|
|
|
char s[256];
|
|
|
|
|
|
|
|
sprintf(s, "cardd fatal error: %s\n", msg);
|
|
|
|
dialog_clear();
|
|
|
|
msgConfirm(s);
|
|
|
|
#else
|
1997-10-06 11:36:08 +00:00
|
|
|
warnx("fatal error: %s", msg);
|
1997-01-06 08:35:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
1995-08-24 09:03:04 +00:00
|
|
|
closelog();
|
|
|
|
exit(1);
|
|
|
|
}
|
1995-08-28 10:30:47 +00:00
|
|
|
|
1996-04-10 06:49:32 +00:00
|
|
|
void *
|
1995-08-24 09:03:04 +00:00
|
|
|
xmalloc(int sz)
|
|
|
|
{
|
1996-04-10 06:49:32 +00:00
|
|
|
void *p;
|
1995-08-24 09:03:04 +00:00
|
|
|
|
1995-08-28 10:30:47 +00:00
|
|
|
p = malloc(sz);
|
1995-08-24 09:03:04 +00:00
|
|
|
if (p)
|
|
|
|
bzero(p, sz);
|
|
|
|
else
|
|
|
|
die("malloc failed");
|
1996-04-10 06:49:32 +00:00
|
|
|
return (p);
|
1995-08-24 09:03:04 +00:00
|
|
|
}
|
1995-08-28 10:30:47 +00:00
|
|
|
|
1996-04-10 06:49:32 +00:00
|
|
|
char *
|
1995-08-24 09:03:04 +00:00
|
|
|
newstr(char *p)
|
|
|
|
{
|
1996-04-10 06:49:32 +00:00
|
|
|
char *s;
|
1995-08-24 09:03:04 +00:00
|
|
|
|
|
|
|
s = strdup(p);
|
|
|
|
if (s == 0)
|
|
|
|
die("strdup failed");
|
1996-04-10 06:49:32 +00:00
|
|
|
return (s);
|
1995-08-24 09:03:04 +00:00
|
|
|
}
|
1996-04-10 06:49:32 +00:00
|
|
|
|
1995-08-24 09:03:04 +00:00
|
|
|
/*
|
|
|
|
* Find contiguous bit string (all set) of at
|
|
|
|
* least count number.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
bit_fns(bitstr_t *nm, int nbits, int count)
|
|
|
|
{
|
1996-04-10 06:49:32 +00:00
|
|
|
int i;
|
|
|
|
int found = 0;
|
1995-08-24 09:03:04 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nbits; i++)
|
1996-04-10 06:49:32 +00:00
|
|
|
if (bit_test(nm, i)) {
|
1995-08-24 09:03:04 +00:00
|
|
|
if (++found == count)
|
1996-04-10 06:49:32 +00:00
|
|
|
return (i - count + 1);
|
|
|
|
} else
|
1995-08-24 09:03:04 +00:00
|
|
|
found = 0;
|
1996-04-10 06:49:32 +00:00
|
|
|
return (-1);
|
1995-08-24 09:03:04 +00:00
|
|
|
}
|
1996-04-10 06:49:32 +00:00
|
|
|
|
1995-08-24 09:03:04 +00:00
|
|
|
/*
|
|
|
|
* Allocate a block of memory and return the address.
|
|
|
|
*/
|
|
|
|
unsigned long
|
|
|
|
alloc_memory(int size)
|
|
|
|
{
|
1996-04-10 06:49:32 +00:00
|
|
|
int i;
|
1995-08-24 09:03:04 +00:00
|
|
|
|
1996-04-10 06:49:32 +00:00
|
|
|
i = bit_fns(mem_avail, MEMBLKS, size / MEMUNIT);
|
1995-08-24 09:03:04 +00:00
|
|
|
if (i < 0)
|
1996-04-10 06:49:32 +00:00
|
|
|
return (0);
|
|
|
|
bit_nclear(mem_avail, i, size / MEMUNIT);
|
|
|
|
return (BIT2MEM(i));
|
1995-08-24 09:03:04 +00:00
|
|
|
}
|
1996-04-10 06:49:32 +00:00
|
|
|
|
1995-08-24 09:03:04 +00:00
|
|
|
/*
|
|
|
|
* reset_slot - Power has been applied to the card.
|
|
|
|
* Now reset the card.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
reset_slot(struct slot *sp)
|
|
|
|
{
|
1996-04-10 06:49:32 +00:00
|
|
|
char c;
|
|
|
|
off_t offs;
|
|
|
|
struct mem_desc mem;
|
|
|
|
struct io_desc io;
|
|
|
|
int rw_flags;
|
1995-08-24 09:03:04 +00:00
|
|
|
|
|
|
|
rw_flags = MDF_ATTR;
|
|
|
|
ioctl(sp->fd, PIOCRWFLAG, &rw_flags);
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Resetting card, writing 0x80 to offs 0x%x\n",
|
1996-04-10 06:49:32 +00:00
|
|
|
sp->cis->reg_addr);
|
1995-08-24 09:03:04 +00:00
|
|
|
#endif
|
|
|
|
offs = sp->cis->reg_addr;
|
|
|
|
lseek(sp->fd, offs, SEEK_SET);
|
|
|
|
c = 0x80;
|
|
|
|
write(sp->fd, &c, sizeof(c));
|
1996-04-10 06:49:32 +00:00
|
|
|
usleep(10 * 1000);
|
1995-08-24 09:03:04 +00:00
|
|
|
c = 0;
|
|
|
|
lseek(sp->fd, offs, SEEK_SET);
|
|
|
|
write(sp->fd, &c, sizeof(c));
|
1996-04-10 06:49:32 +00:00
|
|
|
|
|
|
|
/* Reset all the memory and I/O windows. */
|
|
|
|
bzero((caddr_t) & mem, sizeof(mem));
|
|
|
|
bzero((caddr_t) & io, sizeof(io));
|
1995-08-24 09:03:04 +00:00
|
|
|
for (mem.window = 0; mem.window < NUM_MEM_WINDOWS; mem.window++)
|
|
|
|
ioctl(sp->fd, PIOCSMEM, &mem);
|
|
|
|
for (io.window = 0; io.window < NUM_IO_WINDOWS; io.window++)
|
|
|
|
ioctl(sp->fd, PIOCSIO, &io);
|
|
|
|
}
|
1996-04-10 06:49:32 +00:00
|
|
|
|
1995-08-24 09:03:04 +00:00
|
|
|
/*
|
|
|
|
* execute - Execute the command strings.
|
|
|
|
* For the current slot (if any) perform macro
|
|
|
|
* substitutions.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
execute(struct cmd *cmdp)
|
|
|
|
{
|
1996-04-10 06:49:32 +00:00
|
|
|
char cmd[1024];
|
|
|
|
char *p, *cp, *lp;
|
1995-08-24 09:03:04 +00:00
|
|
|
|
1996-04-10 06:49:32 +00:00
|
|
|
for (; cmdp; cmdp = cmdp->next) {
|
1995-08-24 09:03:04 +00:00
|
|
|
cp = cmd;
|
|
|
|
lp = cmdp->line;
|
|
|
|
if (*lp == 0)
|
|
|
|
continue;
|
1996-04-10 06:49:32 +00:00
|
|
|
while ((p = strchr(lp, '$')) != 0) {
|
|
|
|
/* copy over preceding string. */
|
1995-08-24 09:03:04 +00:00
|
|
|
while (lp != p)
|
|
|
|
*cp++ = *lp++;
|
1996-04-10 06:49:32 +00:00
|
|
|
/* stringify ethernet address and place here. */
|
|
|
|
if (strncmp(p, "$ether", 6) == 0) {
|
1995-08-24 09:03:04 +00:00
|
|
|
sprintf(cp, "%x:%x:%x:%x:%x:%x",
|
1996-04-10 06:49:32 +00:00
|
|
|
current_slot->eaddr[0],
|
|
|
|
current_slot->eaddr[1],
|
|
|
|
current_slot->eaddr[2],
|
|
|
|
current_slot->eaddr[3],
|
|
|
|
current_slot->eaddr[4],
|
|
|
|
current_slot->eaddr[5]);
|
1995-08-24 09:03:04 +00:00
|
|
|
while (*++cp)
|
1996-04-10 06:49:32 +00:00
|
|
|
continue;
|
1995-08-24 09:03:04 +00:00
|
|
|
lp += 6;
|
1996-04-10 06:49:32 +00:00
|
|
|
} else
|
|
|
|
/* replace device name */
|
|
|
|
if (strncmp(p, "$device", 7) == 0) {
|
|
|
|
sprintf(cp, "%s%d",
|
|
|
|
current_slot->config->driver->kernel,
|
|
|
|
current_slot->config->driver->unit);
|
|
|
|
while (*cp)
|
|
|
|
cp++;
|
|
|
|
lp += 7;
|
|
|
|
} else
|
|
|
|
/* Copy the `$' and rescan. */
|
|
|
|
*cp++ = *lp++;
|
|
|
|
}
|
|
|
|
/* No more replacements. Copy rest of string. */
|
1995-08-25 09:46:01 +00:00
|
|
|
while ((*cp++ = *lp++) != 0)
|
1996-04-10 06:49:32 +00:00
|
|
|
continue;
|
1995-08-24 09:03:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Executing [%s]\n", cmd);
|
1997-01-06 08:35:06 +00:00
|
|
|
#endif
|
1995-08-24 09:03:04 +00:00
|
|
|
system(cmd);
|
1996-04-10 06:49:32 +00:00
|
|
|
}
|
1995-08-24 09:03:04 +00:00
|
|
|
}
|