Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / pccard / pccardd / util.c
1 /*
2  * Copyright (c) 1995 Andrew McRae.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
14  *
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.
25  *
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 $
28  */
29
30 /*
31  * Code cleanup, bug-fix and extension
32  * by:
33  *     Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>
34  *     Nate Williams <nate@FreeBSD.org>
35  */
36
37 #include <err.h>
38 #include <fcntl.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <sys/types.h>
45 #include <sys/ioctl.h>
46 #include <syslog.h>
47 #ifdef  SYSINSTALL
48 #include <dialog.h>
49 #endif
50 #include "cardd.h"
51
52 static int do_log = 0;
53
54 void
55 log_setup(void)
56 {
57 #ifndef SYSINSTALL
58         do_log = 1;
59         openlog("pccardd", LOG_PID, LOG_DAEMON);
60 #endif
61 }
62
63 void
64 logmsg(const char *fmt, ...)
65 {
66         va_list ap;
67         char s[256];
68
69         va_start(ap, fmt);
70         vsnprintf(s, 256, fmt, ap);
71
72         if (do_log)
73                 syslog(LOG_ERR, "%s", s);
74         else {
75 #ifdef SYSINSTALL
76                 dialog_clear();
77                 msgConfirm(s);
78 #else
79                 warnx("%s", s);
80 #endif
81         }
82 }
83
84 void
85 logerr(char *msg)
86 {
87         if (do_log)
88                 syslog(LOG_ERR, "%s: %m", msg);
89         else {
90 #ifdef  SYSINSTALL
91                 dialog_clear();
92                 msgConfirm(msg);
93 #else
94                 warn("%s", msg);
95 #endif
96         }
97 }
98
99 /*
100  *      Deliver last will and testament, and die.
101  */
102 void
103 die(char *msg)
104 {
105         if (do_log)
106                 syslog(LOG_CRIT, "fatal error: %s", msg);
107         else {
108 #ifdef SYSINSTALL               
109                 char s[256];
110
111                 sprintf(s, "cardd fatal error: %s\n", msg);
112                 dialog_clear();
113                 msgConfirm(s);
114 #else
115                 warnx("fatal error: %s", msg);
116 #endif
117         }
118         closelog();
119         exit(1);
120 }
121
122 void   *
123 xmalloc(int sz)
124 {
125         void   *p;
126
127         p = malloc(sz);
128         if (p)
129                 bzero(p, sz);
130         else
131                 die("malloc failed");
132         return (p);
133 }
134
135 char   *
136 newstr(char *p)
137 {
138         char   *s;
139
140         s = strdup(p);
141         if (s == 0)
142                 die("strdup failed");
143         return (s);
144 }
145
146 /*
147  *      Find contiguous bit string (all set) of at
148  *      least count number.
149  */
150 int
151 bit_fns(bitstr_t *nm, int nbits, int min, int count, int step)
152 {
153         int     i, j;
154         int     found = 0;
155
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)
160                                         return i;
161                         } else
162                                 break;
163         return (-1);
164 }
165
166 /*
167  *      Allocate a block of memory and return the address.
168  */
169 unsigned long
170 alloc_memory(int size)
171 {
172         int     i;
173
174         i = bit_fns(mem_avail, MEMBLKS, 0, size / MEMUNIT + (size % MEMUNIT != 0), 1);
175         if (i < 0)
176                 return (0);
177         bit_nclear(mem_avail, i, i + size / MEMUNIT + (size % MEMUNIT != 0) - 1);
178         return (BIT2MEM(i));
179 }
180
181 /*
182  *      reset_slot - Power has been applied to the card.
183  *      Now reset the card.
184  */
185 void
186 reset_slot(struct slot *sp)
187 {
188         char    c;
189         off_t   offs;
190         struct mem_desc mem;
191         struct io_desc io;
192         int     rw_flags;
193
194         rw_flags = MDF_ATTR;
195         ioctl(sp->fd, PIOCRWFLAG, &rw_flags);
196 #ifdef  DEBUG
197         printf("Resetting card, writing 0x80 to offs 0x%x\n",
198             sp->cis->reg_addr);
199 #endif
200         offs = sp->cis->reg_addr;
201         lseek(sp->fd, offs, SEEK_SET);
202         c = 0x80;
203         write(sp->fd, &c, sizeof(c));
204         usleep(10 * 1000);
205         c = 0;
206         lseek(sp->fd, offs, SEEK_SET);
207         write(sp->fd, &c, sizeof(c));
208
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);
216 }
217
218 /*
219  *      execute - Execute the command strings.
220  *      For the current slot (if any) perform macro
221  *      substitutions.
222  */
223 void
224 execute(struct cmd *cmdp, struct slot *sp)
225 {
226         char    cmd[1024];
227         char   *p, *cp, *lp;
228
229         for (; cmdp; cmdp = cmdp->next) {
230                 cp = cmd;
231                 lp = cmdp->line;
232                 if (*lp == 0)
233                         continue;
234                 while ((p = strchr(lp, '$')) != 0) {
235                         /* copy over preceding string. */
236                         while (lp != p)
237                                 *cp++ = *lp++;
238                         /* stringify ethernet address and place here. */
239                         if (strncmp(p, "$ether", 6) == 0) {
240                                 sprintf(cp, "%x:%x:%x:%x:%x:%x",
241                                     sp->eaddr[0],
242                                     sp->eaddr[1],
243                                     sp->eaddr[2],
244                                     sp->eaddr[3],
245                                     sp->eaddr[4],
246                                     sp->eaddr[5]);
247                                 while (*++cp)
248                                         continue;
249                                 lp += 6;
250                         } else
251                                 /* replace device name */
252                                 if (strncmp(p, "$device", 7) == 0) {
253                                         sprintf(cp, "%s%d",
254                                             sp->config->driver->kernel,
255                                             sp->config->driver->unit);
256                                         while (*cp)
257                                                 cp++;
258                                         lp += 7;
259                                 } else
260                                         /* Copy the `$' and rescan. */
261                                         *cp++ = *lp++;
262                 }
263                 /* No more replacements. Copy rest of string. */
264                 while ((*cp++ = *lp++) != 0)
265                         continue;
266 #ifdef  DEBUG
267                 fprintf(stderr, "Executing [%s]\n", cmd);
268 #endif
269                 system(cmd);
270         }
271 }