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