]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pccard/pccardd/util.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.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$";
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         vsprintf(s, fmt, ap);
73
74         if (do_log)
75                 syslog(LOG_ERR, 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 count)
154 {
155         int     i;
156         int     found = 0;
157
158         for (i = 0; i < nbits; i++)
159                 if (bit_test(nm, i)) {
160                         if (++found == count)
161                                 return (i - count + 1);
162                 } else
163                         found = 0;
164         return (-1);
165 }
166
167 /*
168  *      Allocate a block of memory and return the address.
169  */
170 unsigned long
171 alloc_memory(int size)
172 {
173         int     i;
174
175         i = bit_fns(mem_avail, MEMBLKS, size / MEMUNIT);
176         if (i < 0)
177                 return (0);
178         bit_nclear(mem_avail, i, size / MEMUNIT);
179         return (BIT2MEM(i));
180 }
181
182 /*
183  *      reset_slot - Power has been applied to the card.
184  *      Now reset the card.
185  */
186 void
187 reset_slot(struct slot *sp)
188 {
189         char    c;
190         off_t   offs;
191         struct mem_desc mem;
192         struct io_desc io;
193         int     rw_flags;
194
195         rw_flags = MDF_ATTR;
196         ioctl(sp->fd, PIOCRWFLAG, &rw_flags);
197 #ifdef  DEBUG
198         printf("Resetting card, writing 0x80 to offs 0x%x\n",
199             sp->cis->reg_addr);
200 #endif
201         offs = sp->cis->reg_addr;
202         lseek(sp->fd, offs, SEEK_SET);
203         c = 0x80;
204         write(sp->fd, &c, sizeof(c));
205         usleep(10 * 1000);
206         c = 0;
207         lseek(sp->fd, offs, SEEK_SET);
208         write(sp->fd, &c, sizeof(c));
209
210         /* Reset all the memory and I/O windows. */
211         bzero((caddr_t) & mem, sizeof(mem));
212         bzero((caddr_t) & io, sizeof(io));
213         for (mem.window = 0; mem.window < NUM_MEM_WINDOWS; mem.window++)
214                 ioctl(sp->fd, PIOCSMEM, &mem);
215         for (io.window = 0; io.window < NUM_IO_WINDOWS; io.window++)
216                 ioctl(sp->fd, PIOCSIO, &io);
217 }
218
219 /*
220  *      execute - Execute the command strings.
221  *      For the current slot (if any) perform macro
222  *      substitutions.
223  */
224 void
225 execute(struct cmd *cmdp, struct slot *sp)
226 {
227         char    cmd[1024];
228         char   *p, *cp, *lp;
229
230         for (; cmdp; cmdp = cmdp->next) {
231                 cp = cmd;
232                 lp = cmdp->line;
233                 if (*lp == 0)
234                         continue;
235                 while ((p = strchr(lp, '$')) != 0) {
236                         /* copy over preceding string. */
237                         while (lp != p)
238                                 *cp++ = *lp++;
239                         /* stringify ethernet address and place here. */
240                         if (strncmp(p, "$ether", 6) == 0) {
241                                 sprintf(cp, "%x:%x:%x:%x:%x:%x",
242                                     sp->eaddr[0],
243                                     sp->eaddr[1],
244                                     sp->eaddr[2],
245                                     sp->eaddr[3],
246                                     sp->eaddr[4],
247                                     sp->eaddr[5]);
248                                 while (*++cp)
249                                         continue;
250                                 lp += 6;
251                         } else
252                                 /* replace device name */
253                                 if (strncmp(p, "$device", 7) == 0) {
254                                         sprintf(cp, "%s%d",
255                                             sp->config->driver->kernel,
256                                             sp->config->driver->unit);
257                                         while (*cp)
258                                                 cp++;
259                                         lp += 7;
260                                 } else
261                                         /* Copy the `$' and rescan. */
262                                         *cp++ = *lp++;
263                 }
264                 /* No more replacements. Copy rest of string. */
265                 while ((*cp++ = *lp++) != 0)
266                         continue;
267 #ifdef  DEBUG
268                 fprintf(stderr, "Executing [%s]\n", cmd);
269 #endif
270                 system(cmd);
271         }
272 }