2 * Copyright (c) 1995 Andrew McRae. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Code cleanup, bug-fix and extension
30 * Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
31 * Nate Williams <nate@FreeBSD.org>
35 static const char rcsid[] =
36 "$FreeBSD: src/usr.sbin/pccard/pccardd/util.c,v 1.13.2.4 2001/02/25 20:10:12 gj Exp $";
46 #include <sys/types.h>
47 #include <sys/ioctl.h>
54 static int do_log = 0;
61 openlog("pccardd", LOG_PID, LOG_DAEMON);
66 logmsg(const char *fmt, ...)
72 vsnprintf(s, 256, fmt, ap);
75 syslog(LOG_ERR, "%s", s);
90 syslog(LOG_ERR, "%s: %m", msg);
102 * Deliver last will and testament, and die.
108 syslog(LOG_CRIT, "fatal error: %s", msg);
113 sprintf(s, "cardd fatal error: %s\n", msg);
117 warnx("fatal error: %s", msg);
133 die("malloc failed");
144 die("strdup failed");
149 * Find contiguous bit string (all set) of at
150 * least count number.
153 bit_fns(bitstr_t *nm, int nbits, int min, int count, int step)
158 for (i = min; i < nbits; i += step)
159 for (j = i, found = 0; j < nbits; j++)
160 if (bit_test(nm, j)) {
161 if (++found == count)
169 * Allocate a block of memory and return the address.
172 alloc_memory(int size)
176 i = bit_fns(mem_avail, MEMBLKS, 0, size / MEMUNIT + (size % MEMUNIT != 0), 1);
179 bit_nclear(mem_avail, i, i + size / MEMUNIT + (size % MEMUNIT != 0) - 1);
184 * reset_slot - Power has been applied to the card.
185 * Now reset the card.
188 reset_slot(struct slot *sp)
197 ioctl(sp->fd, PIOCRWFLAG, &rw_flags);
199 printf("Resetting card, writing 0x80 to offs 0x%x\n",
202 offs = sp->cis->reg_addr;
203 lseek(sp->fd, offs, SEEK_SET);
205 write(sp->fd, &c, sizeof(c));
208 lseek(sp->fd, offs, SEEK_SET);
209 write(sp->fd, &c, sizeof(c));
211 /* Reset all the memory and I/O windows. */
212 bzero((caddr_t) & mem, sizeof(mem));
213 bzero((caddr_t) & io, sizeof(io));
214 for (mem.window = 0; mem.window < NUM_MEM_WINDOWS; mem.window++)
215 ioctl(sp->fd, PIOCSMEM, &mem);
216 for (io.window = 0; io.window < NUM_IO_WINDOWS; io.window++)
217 ioctl(sp->fd, PIOCSIO, &io);
221 * execute - Execute the command strings.
222 * For the current slot (if any) perform macro
226 execute(struct cmd *cmdp, struct slot *sp)
231 for (; cmdp; cmdp = cmdp->next) {
236 while ((p = strchr(lp, '$')) != 0) {
237 /* copy over preceding string. */
240 /* stringify ethernet address and place here. */
241 if (strncmp(p, "$ether", 6) == 0) {
242 sprintf(cp, "%x:%x:%x:%x:%x:%x",
253 /* replace device name */
254 if (strncmp(p, "$device", 7) == 0) {
256 sp->config->driver->kernel,
257 sp->config->driver->unit);
262 /* Copy the `$' and rescan. */
265 /* No more replacements. Copy rest of string. */
266 while ((*cp++ = *lp++) != 0)
269 fprintf(stderr, "Executing [%s]\n", cmd);