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.
26 * $FreeBSD: src/usr.sbin/pccard/pccardd/util.c,v 1.13.2.4 2001/02/25 20:10:12 gj Exp $
27 * $DragonFly: src/usr.sbin/pccard/pccardd/Attic/util.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
31 * Code cleanup, bug-fix and extension
33 * Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
34 * Nate Williams <nate@FreeBSD.org>
44 #include <sys/types.h>
45 #include <sys/ioctl.h>
52 static int do_log = 0;
59 openlog("pccardd", LOG_PID, LOG_DAEMON);
64 logmsg(const char *fmt, ...)
70 vsnprintf(s, 256, fmt, ap);
73 syslog(LOG_ERR, "%s", s);
88 syslog(LOG_ERR, "%s: %m", msg);
100 * Deliver last will and testament, and die.
106 syslog(LOG_CRIT, "fatal error: %s", msg);
111 sprintf(s, "cardd fatal error: %s\n", msg);
115 warnx("fatal error: %s", msg);
131 die("malloc failed");
142 die("strdup failed");
147 * Find contiguous bit string (all set) of at
148 * least count number.
151 bit_fns(bitstr_t *nm, int nbits, int min, int count, int step)
156 for (i = min; i < nbits; i += step)
157 for (j = i, found = 0; j < nbits; j++)
158 if (bit_test(nm, j)) {
159 if (++found == count)
167 * Allocate a block of memory and return the address.
170 alloc_memory(int size)
174 i = bit_fns(mem_avail, MEMBLKS, 0, size / MEMUNIT + (size % MEMUNIT != 0), 1);
177 bit_nclear(mem_avail, i, i + size / MEMUNIT + (size % MEMUNIT != 0) - 1);
182 * reset_slot - Power has been applied to the card.
183 * Now reset the card.
186 reset_slot(struct slot *sp)
195 ioctl(sp->fd, PIOCRWFLAG, &rw_flags);
197 printf("Resetting card, writing 0x80 to offs 0x%x\n",
200 offs = sp->cis->reg_addr;
201 lseek(sp->fd, offs, SEEK_SET);
203 write(sp->fd, &c, sizeof(c));
206 lseek(sp->fd, offs, SEEK_SET);
207 write(sp->fd, &c, sizeof(c));
209 /* Reset all the memory and I/O windows. */
210 bzero((caddr_t) & mem, sizeof(mem));
211 bzero((caddr_t) & io, sizeof(io));
212 for (mem.window = 0; mem.window < NUM_MEM_WINDOWS; mem.window++)
213 ioctl(sp->fd, PIOCSMEM, &mem);
214 for (io.window = 0; io.window < NUM_IO_WINDOWS; io.window++)
215 ioctl(sp->fd, PIOCSIO, &io);
219 * execute - Execute the command strings.
220 * For the current slot (if any) perform macro
224 execute(struct cmd *cmdp, struct slot *sp)
229 for (; cmdp; cmdp = cmdp->next) {
234 while ((p = strchr(lp, '$')) != 0) {
235 /* copy over preceding string. */
238 /* stringify ethernet address and place here. */
239 if (strncmp(p, "$ether", 6) == 0) {
240 sprintf(cp, "%x:%x:%x:%x:%x:%x",
251 /* replace device name */
252 if (strncmp(p, "$device", 7) == 0) {
254 sp->config->driver->kernel,
255 sp->config->driver->unit);
260 /* Copy the `$' and rescan. */
263 /* No more replacements. Copy rest of string. */
264 while ((*cp++ = *lp++) != 0)
267 fprintf(stderr, "Executing [%s]\n", cmd);