- A pass at staticizing things.

- Try to have all output go through the routines in util.c [logerr(),
  log_1s(), die()]
- Add *some* code in util.c to allow pccardd to run out of sysinstall.

Submitted by: Mostly me, but some by Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
This commit is contained in:
Nate Williams 1997-01-06 08:35:06 +00:00
parent dc9d5eda2a
commit 16a960917f
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=21371
5 changed files with 134 additions and 110 deletions

View File

@ -23,7 +23,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: cardd.c,v 1.12 1996/06/19 17:27:55 nate Exp $ * $Id: cardd.c,v 1.13 1996/06/20 21:06:51 nate Exp $
*/ */
#include <stdio.h> #include <stdio.h>
@ -34,21 +34,21 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/time.h> #include <sys/time.h>
#include <syslog.h> #define EXTERN
#include "cardd.h" #include "cardd.h"
char *config_file = "/etc/pccard.conf"; char *config_file = "/etc/pccard.conf";
struct card_config *assign_driver(struct card *); static struct card_config *assign_driver(struct card *);
int setup_slot(struct slot *); static int assign_io(struct slot *);
void read_ether(struct slot *); static int setup_slot(struct slot *);
void dump_config_file(void); static void card_inserted(struct slot *);
void pr_cmd(struct cmd *); static void card_removed(struct slot *);
void readslots(void); static void dump_config_file(void);
void slot_change(struct slot *); static void pr_cmd(struct cmd *);
void card_removed(struct slot *); static void read_ether(struct slot *);
void card_inserted(struct slot *); static void readslots(void);
int assign_io(struct slot *); static void slot_change(struct slot *);
/* /*
* mainline code for cardd * mainline code for cardd
@ -93,35 +93,22 @@ main(int argc, char *argv[])
readfile(config_file); readfile(config_file);
if (verbose) if (verbose)
dump_config_file(); dump_config_file();
if (!debug) { log_setup();
if (!debug)
if (daemon(0, 0)) if (daemon(0, 0))
die("fork failed"); die("fork failed");
openlog("cardd", LOG_PID, LOG_DAEMON);
do_log = 1;
}
#ifdef DEBUG
printf("Before readslots\n");
#endif
readslots(); readslots();
#ifdef DEBUG
printf("After readslots\n");
#endif
if (slots == 0) if (slots == 0)
die("No PC-CARD slots"); die("No PC-CARD slots");
log_1s("pccardd started", NULL);
for (;;) { for (;;) {
fd_set mask; fd_set mask;
FD_ZERO(&mask); FD_ZERO(&mask);
for (sp = slots; sp; sp = sp->next) for (sp = slots; sp; sp = sp->next)
FD_SET(sp->fd, &mask); FD_SET(sp->fd, &mask);
#ifdef DEBUG
printf("Doing select\n");
#endif
count = select(32, 0, 0, &mask, 0); count = select(32, 0, 0, &mask, 0);
#ifdef DEBUG
printf("select=%d\n", count);
#endif
if (count == -1) { if (count == -1) {
perror("Select"); logerr("Select");
continue; continue;
} }
if (count) if (count)
@ -157,8 +144,8 @@ dump_config_file(void)
} }
} }
void static void
pr_cmd(struct cmd * cp) pr_cmd(struct cmd *cp)
{ {
while (cp) { while (cp) {
printf("\t%s\n", cp->line); printf("\t%s\n", cp->line);
@ -182,9 +169,6 @@ readslots(void)
fd = open(name, 2); fd = open(name, 2);
if (fd < 0) if (fd < 0)
continue; continue;
#ifdef DEBUG
printf("opened %s\n", name);
#endif
sp = xmalloc(sizeof(*sp)); sp = xmalloc(sizeof(*sp));
sp->fd = fd; sp->fd = fd;
sp->name = newstr(name); sp->name = newstr(name);
@ -196,16 +180,16 @@ readslots(void)
unsigned long mem = 0; unsigned long mem = 0;
if (ioctl(fd, PIOCRWMEM, &mem)) if (ioctl(fd, PIOCRWMEM, &mem))
perror("ioctl (PIOCRWMEM)"); logerr("ioctl (PIOCRWMEM)");
#ifdef DEBUG #ifdef DEBUG
printf("mem=%x\n", mem); log_1s("mem=0x%x\n", mem);
#endif #endif
if (mem == 0) { if (mem == 0) {
mem = alloc_memory(4 * 1024); mem = alloc_memory(4 * 1024);
if (mem == 0) if (mem == 0)
die("Can't allocate memory for controller access"); die("Can't allocate memory for controller access");
if (ioctl(fd, PIOCRWMEM, &mem)) if (ioctl(fd, PIOCRWMEM, &mem))
perror("ioctl (PIOCRWMEM)"); logerr("ioctl (PIOCRWMEM)");
} }
} }
#ifdef DEBUG #ifdef DEBUG
@ -228,12 +212,9 @@ slot_change(struct slot *sp)
current_slot = sp; current_slot = sp;
if (ioctl(sp->fd, PIOCGSTATE, &state)) { if (ioctl(sp->fd, PIOCGSTATE, &state)) {
perror("ioctl (PIOCGSTATE)"); logerr("ioctl (PIOCGSTATE)");
return; return;
} }
#ifdef DEBUG
printf("%p %p %d %d\n", sp, &sp->state, state.state, sp->state);
#endif
if (state.state == sp->state) if (state.state == sp->state)
return; return;
sp->state = state.state; sp->state = state.state;
@ -309,8 +290,8 @@ card_inserted(struct slot *sp)
reset_slot(sp); reset_slot(sp);
#endif #endif
if (cp == 0) { if (cp == 0) {
log_1s("No card in database for \"%s\"", sp->cis->manuf); log_1s("No card in database for \"%s\"(\"%s\")",
log_1s("vers: \"%s\"", sp->cis->vers); sp->cis->manuf, sp->cis->vers);
return; return;
} }
if (cp->ether) if (cp->ether)
@ -326,8 +307,9 @@ card_inserted(struct slot *sp)
} }
/* /*
* Once assigned, then set up the I/O & mem contexts, and *
* set up the windows, and then attach the driver. * Once assigned, set up the I/O & mem contexts, set up the
* windows, and then attach the driver.
*/ */
if (setup_slot(sp)) if (setup_slot(sp))
execute(cp->insert); execute(cp->insert);
@ -341,7 +323,7 @@ card_inserted(struct slot *sp)
* read_ether - read ethernet address from card. Offset is * read_ether - read ethernet address from card. Offset is
* the offset into the attribute memory of the card. * the offset into the attribute memory of the card.
*/ */
void static void
read_ether(struct slot *sp) read_ether(struct slot *sp)
{ {
unsigned char net_addr[12]; unsigned char net_addr[12];
@ -357,7 +339,7 @@ read_ether(struct slot *sp)
sp->eaddr[3] = net_addr[6]; sp->eaddr[3] = net_addr[6];
sp->eaddr[4] = net_addr[8]; sp->eaddr[4] = net_addr[8];
sp->eaddr[5] = net_addr[10]; sp->eaddr[5] = net_addr[10];
printf("Ether=%02x:%02x:%02x:%02x:%02x:%02x\n", log_1s("Ether=%02x:%02x:%02x:%02x:%02x:%02x\n",
sp->eaddr[0], sp->eaddr[1], sp->eaddr[2], sp->eaddr[0], sp->eaddr[1], sp->eaddr[2],
sp->eaddr[3], sp->eaddr[4], sp->eaddr[5]); sp->eaddr[3], sp->eaddr[4], sp->eaddr[5]);
} }
@ -366,7 +348,7 @@ read_ether(struct slot *sp)
* assign_driver - Assign driver to card. * assign_driver - Assign driver to card.
* First, see if an existing driver is already setup. * First, see if an existing driver is already setup.
*/ */
struct card_config * static struct card_config *
assign_driver(struct card *cp) assign_driver(struct card *cp)
{ {
struct driver *drvp; struct driver *drvp;
@ -376,7 +358,7 @@ assign_driver(struct card *cp)
if (conf->inuse == 0 && conf->driver->card == cp && if (conf->inuse == 0 && conf->driver->card == cp &&
conf->driver->config == conf) { conf->driver->config == conf) {
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "Found existing driver (%s) for %s\n", log_1s(stderr, "Found existing driver (%s) for %s\n",
conf->driver->name, cp->manuf); conf->driver->name, cp->manuf);
#endif #endif
return (conf); return (conf);
@ -455,7 +437,7 @@ assign_driver(struct card *cp)
* assign_io - Allocate resources to slot matching the * assign_io - Allocate resources to slot matching the
* configuration index selected. * configuration index selected.
*/ */
int static int
assign_io(struct slot *sp) assign_io(struct slot *sp)
{ {
struct cis *cis; struct cis *cis;
@ -492,14 +474,13 @@ assign_io(struct slot *sp)
if (sp->mem.size && sp->mem.addr == 0) { if (sp->mem.size && sp->mem.addr == 0) {
sp->mem.addr = alloc_memory(mp->length); sp->mem.addr = alloc_memory(mp->length);
if (sp->mem.addr == 0) if (sp->mem.addr == 0)
return (-1); return (-2);
sp->config->driver->mem = sp->mem.addr; sp->config->driver->mem = sp->mem.addr;
} }
sp->mem.cardaddr = 0x4000; sp->mem.cardaddr = 0x4000;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, log_1s("Using mem addr 0x%x, size %d, card addr 0x%x\n",
"Using mem addr 0x%x, size %d, card addr 0x%x\n", sp->mem.addr, sp->mem.size, sp->mem.cardaddr);
sp->mem.addr, sp->mem.cardaddr, sp->mem.size);
#endif #endif
} }
@ -554,18 +535,19 @@ assign_io(struct slot *sp)
break; break;
} }
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "Using I/O addr 0x%x, size %d\n", log_1s("Using I/O addr 0x%x, size %d\n",
sp->io.addr, sp->io.size); sp->io.addr, sp->io.size);
#endif #endif
} }
sp->irq = sp->config->irq; sp->irq = sp->config->irq;
return (0); return (0);
} }
/* /*
* setup_slot - Allocate the I/O and memory contexts * setup_slot - Allocate the I/O and memory contexts
* return true if completed OK. * return true if completed OK.
*/ */
int static int
setup_slot(struct slot *sp) setup_slot(struct slot *sp)
{ {
struct mem_desc mem; struct mem_desc mem;
@ -594,9 +576,8 @@ setup_slot(struct slot *sp)
c |= 0x40; c |= 0x40;
write(sp->fd, &c, sizeof(c)); write(sp->fd, &c, sizeof(c));
#ifdef DEBUG #ifdef DEBUG
printf("Setting config reg at offs 0x%x", offs); log_1s("Setting config reg at offs 0x%lx to 0x%x, Reset time = %d ms\n",
printf(" to 0x%x\n", c); (unsigned long)offs, c, sp->card->reset_time);
printf("Reset time = %d ms\n", sp->card->reset_time);
#endif #endif
sleep(5); sleep(5);
usleep(sp->card->reset_time * 1000); usleep(sp->card->reset_time * 1000);
@ -612,14 +593,8 @@ setup_slot(struct slot *sp)
c |= 0x20; c |= 0x20;
lseek(sp->fd, offs + 2, SEEK_SET); lseek(sp->fd, offs + 2, SEEK_SET);
write(sp->fd, &c, sizeof(c)); write(sp->fd, &c, sizeof(c));
#ifdef DEBUG
printf("Setting CCSR reg to 0x%x\n", c);
#endif
} }
mem.window = 0; mem.window = 0;
#ifdef DEBUG
printf("Mem@ %x %d %x\n", sp->mem.addr, sp->mem.size, sp->mem.cardaddr);
#endif
if (sp->mem.addr) { if (sp->mem.addr) {
mem.window = 0; mem.window = 0;
mem.flags = sp->mem.flags | MDF_ACTIVE | MDF_16BITS; mem.flags = sp->mem.flags | MDF_ACTIVE | MDF_16BITS;
@ -645,8 +620,8 @@ setup_slot(struct slot *sp)
} }
#endif #endif
#ifdef DEBUG #ifdef DEBUG
printf("Assigning I/O window 0, start 0x%x, size 0x%x flags 0x%x\n", log_1s("Assigning I/O window %d, start 0x%x, size 0x%x flags 0x%x\n",
io.start, io.size, io.flags); io.window, io.start, io.size, io.flags);
#endif #endif
io.flags |= IODF_ACTIVE; io.flags |= IODF_ACTIVE;
if (ioctl(sp->fd, PIOCSIO, &io)) { if (ioctl(sp->fd, PIOCSIO, &io)) {
@ -670,9 +645,9 @@ setup_slot(struct slot *sp)
else else
drv.iobase = 0; drv.iobase = 0;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "Assign %s%d, io 0x%x, mem 0x%x, %d bytes, irq %x, flags %x\n", log_1s("Assign %s%d, io 0x%x, mem 0x%lx, %d bytes, irq %d, flags %x\n",
drv.name, drv.unit, drv.iobase, drv.mem, drv.memsize, sp->irq, drv.flags); drv.name, drv.unit, drv.iobase, drv.mem, drv.memsize, sp->irq, drv.flags);
#endif /* DEBUG */ #endif
/* /*
* If the driver fails to be connected to the device, * If the driver fails to be connected to the device,
@ -680,9 +655,6 @@ setup_slot(struct slot *sp)
*/ */
memcpy(drv.misc, sp->eaddr, 6); memcpy(drv.misc, sp->eaddr, 6);
if (ioctl(sp->fd, PIOCSDRV, &drv)) { if (ioctl(sp->fd, PIOCSDRV, &drv)) {
#ifdef DEBUG
perror(sp->card->manuf);
#endif
log_1s("driver allocation failed for %s", sp->card->manuf); log_1s("driver allocation failed for %s", sp->card->manuf);
return (0); return (0);
} }

View File

@ -23,7 +23,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: cardd.h,v 1.4 1996/04/18 04:25:12 nate Exp $ * $Id: cardd.h,v 1.5 1996/06/18 19:52:29 nate Exp $
* *
* Common include file for PCMCIA daemon * Common include file for PCMCIA daemon
*/ */
@ -34,6 +34,10 @@
#include "readcis.h" #include "readcis.h"
#ifndef EXTERN
#define EXTERN extern
#endif
struct cmd { struct cmd {
struct cmd *next; struct cmd *next;
char *line; /* Command line */ char *line; /* Command line */
@ -107,28 +111,32 @@ struct slot {
int irq; /* Irq value */ int irq; /* Irq value */
}; };
struct slot *slots, *current_slot; EXTERN struct slot *slots, *current_slot;
struct allocblk *pool_ioblks; /* I/O blocks in the pool */ EXTERN struct allocblk *pool_ioblks; /* I/O blocks in the pool */
struct allocblk *pool_mem; /* Memory in the pool */ EXTERN struct allocblk *pool_mem; /* Memory in the pool */
int pool_irq[16]; /* IRQ allocations */ EXTERN int pool_irq[16]; /* IRQ allocations */
struct driver *drivers; /* List of drivers */ EXTERN struct driver *drivers; /* List of drivers */
struct card *cards; EXTERN struct card *cards;
bitstr_t *mem_avail; EXTERN bitstr_t *mem_avail;
bitstr_t *io_avail; EXTERN bitstr_t *io_avail;
int verbose, do_log; EXTERN int verbose;
char *newstr(); /* util.c functions */
void die(char *);
void *xmalloc(int);
void log_1s(char *, char *);
void logerr(char *);
void reset_slot(struct slot *);
void execute(struct cmd *);
void readfile(char *);
int bit_fns(bitstr_t *, int, int);
unsigned long alloc_memory(int); unsigned long alloc_memory(int);
int bit_fns(bitstr_t *, int, int);
void die(char *);
void execute(struct cmd *);
void log_1s(const char *, ...);
void log_setup(void);
void logerr(char *);
char *newstr();
void reset_slot(struct slot *);
void *xmalloc(int);
/* file.c */
void readfile(char *);
#define IOPORTS 0x400 #define IOPORTS 0x400
#define MEMUNIT 0x1000 #define MEMUNIT 0x1000

View File

@ -23,7 +23,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: file.c,v 1.7 1996/06/19 01:08:58 nate Exp $ * $Id: file.c,v 1.8 1996/07/11 15:04:43 nate Exp $
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -69,7 +69,7 @@ static struct allocblk *ioblk_tok(int);
static struct allocblk *memblk_tok(int); static struct allocblk *memblk_tok(int);
static struct driver *new_driver(char *); static struct driver *new_driver(char *);
static void addcmd(struct cmd **cp); static void addcmd(struct cmd **);
static void parse_card(void); static void parse_card(void);
/* /*
@ -83,14 +83,13 @@ readfile(char *name)
in = fopen(name, "r"); in = fopen(name, "r");
if (in == 0) { if (in == 0) {
perror(name); logerr(name);
exit(1); die("readfile");
} }
parsefile(); parsefile();
for (cp = cards; cp; cp = cp->next) { for (cp = cards; cp; cp = cp->next) {
if (cp->config == 0) if (cp->config == 0)
fprintf(stderr, log_1s("warning: card %s(%s) has no valid configuration\n",
"warning: card %s(%s) has no valid configuration\n",
cp->manuf, cp->version); cp->manuf, cp->version);
} }
} }
@ -398,7 +397,7 @@ static void
error(char *msg) error(char *msg)
{ {
pusht = 1; pusht = 1;
fprintf(stderr, "%s: %s at line %d, near %s\n", log_1s("%s: %s at line %d, near %s\n",
filename, msg, lineno, next_tok()); filename, msg, lineno, next_tok());
pusht = 1; pusht = 1;
} }

View File

@ -23,7 +23,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: readcis.c,v 1.5 1996/06/17 22:30:29 nate Exp $ * $Id: readcis.c,v 1.6 1996/06/18 21:58:51 nate Exp $
*/ */
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -493,8 +493,7 @@ read_one_tuplelist(int fd, int flags, off_t offs)
total++; total++;
tp->length = length; tp->length = length;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "Tuple code = 0x%x, len = %d\n", printf("Tuple code = 0x%x, len = %d\n", code, length);
code, length);
#endif #endif
if (length == 0xFF) { if (length == 0xFF) {
length = tp->length = 0; length = tp->length = 0;

View File

@ -23,28 +23,59 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: util.c,v 1.5 1996/04/18 04:25:17 nate Exp $ * $Id: util.c,v 1.6 1996/06/20 21:06:53 nate Exp $
*/ */
/*
* Code cleanup, bug-fix and extension
* by:
* Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
* Nate Williams <nate@FreeBSD.org>
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdarg.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <syslog.h> #include <syslog.h>
#ifdef SYSINSTALL
#include <dialog.h>
#endif
#include "cardd.h" #include "cardd.h"
static int do_log = 0;
void void
log_1s(char *msg, char *arg) log_setup(void)
{ {
#ifndef SYSINSTALL
do_log = 1;
openlog("pccardd", LOG_PID, LOG_DAEMON);
#endif
}
void
log_1s(const char *fmt, ...)
{
va_list ap;
char s[256];
va_start(ap, fmt);
vsprintf(s, fmt, ap);
if (do_log) if (do_log)
syslog(LOG_ERR, msg, arg); syslog(LOG_ERR, s);
else { else {
fprintf(stderr, "cardd: "); #ifdef SYSINSTALL
fprintf(stderr, msg, arg); dialog_clear();
fprintf(stderr, "\n"); msgConfirm(s);
#else
fprintf(stderr, "cardd: %s\n", s);
#endif
} }
} }
@ -53,8 +84,14 @@ logerr(char *msg)
{ {
if (do_log) if (do_log)
syslog(LOG_ERR, "%s: %m", msg); syslog(LOG_ERR, "%s: %m", msg);
else else {
#ifdef SYSINSTALL
dialog_clear();
msgConfirm(msg);
#else
perror(msg); perror(msg);
#endif
}
} }
/* /*
@ -65,8 +102,17 @@ die(char *msg)
{ {
if (do_log) if (do_log)
syslog(LOG_CRIT, "fatal error: %s", msg); syslog(LOG_CRIT, "fatal error: %s", msg);
else else {
#ifdef SYSINSTALL
char s[256];
sprintf(s, "cardd fatal error: %s\n", msg);
dialog_clear();
msgConfirm(s);
#else
fprintf(stderr, "cardd fatal error: %s\n", msg); fprintf(stderr, "cardd fatal error: %s\n", msg);
#endif
}
closelog(); closelog();
exit(1); exit(1);
} }
@ -216,7 +262,7 @@ execute(struct cmd *cmdp)
continue; continue;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "Executing [%s]\n", cmd); fprintf(stderr, "Executing [%s]\n", cmd);
#endif /* DEBUG */ #endif
system(cmd); system(cmd);
} }
} }