Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / pccard / pccardc / enabler.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 #ifndef lint
28 static const char rcsid[] =
29   "$FreeBSD: src/usr.sbin/pccard/pccardc/enabler.c,v 1.13 1999/08/28 01:17:32 peter Exp $";
30 #endif /* not lint */
31
32 #include <err.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/ioctl.h>
39
40 #include <pccard/cardinfo.h>
41 #include <pccard/cis.h>
42
43 static void    usage __P((char *));
44
45 int
46 enabler_main(argc, argv)
47         int     argc;
48         char   *argv[];
49 {
50         struct dev_desc drv;
51         struct mem_desc mem;
52         struct io_desc io;
53         int     fd, slot, i, card_addr;
54         char    name[32];
55         char   *p;
56
57         bzero(&drv, sizeof(drv));
58         if (argc < 3)
59                 usage("arg count");
60         slot = atoi(argv[1]);
61         if (slot < 0 || slot >= MAXSLOT)
62                 usage("illegal slot number");
63         p = argv[2];
64         while (*p && (*p < '0' || *p > '9'))
65                 p++;
66         if (*p == 0)
67                 usage("no unit on device name");
68         drv.unit = atoi(p);
69         *p = 0;
70         strcpy(drv.name, argv[2]);
71         argv += 3;
72         argc -= 3;
73         while (argc > 1) {
74                 if (strcmp(argv[0], "-m") == 0) {
75                         if (argc < 4)
76                                 usage("memory argument error");
77                         if (sscanf(argv[1], "%x", &card_addr) != 1)
78                                 usage("bad card address");
79                         if (sscanf(argv[2], "%lx", &drv.mem) != 1)
80                                 usage("bad memory address");
81                         if (sscanf(argv[3], "%d", &i) != 1)
82                                 usage("bad memory size");
83                         drv.memsize = i * 1024;
84                         argc -= 2;
85                         argv += 2;
86                 } else if (strcmp(argv[0], "-f") == 0) {
87                         if (sscanf(argv[1], "%x", &drv.flags) != 1)
88                                 usage("bad driver flags");
89                 } else if (strcmp(argv[0], "-a") == 0) {
90                         if (sscanf(argv[1], "%x", &drv.iobase) != 1)
91                                 usage("bad I/O address");
92                 } else if (strcmp(argv[0], "-i") == 0) {
93                         if (sscanf(argv[1], "%d", &i) != 1 || i < 1 || i > 15)
94                                 usage("illegal IRQ");
95                         drv.irqmask = 1 << i;
96                 }
97                 argc -= 2;
98                 argv += 2;
99         }
100         if (argc)
101                 usage("no parameter for argument");
102         printf("drv %s%d, mem 0x%lx, size %d, io %d, irq 0x%x, flags 0x%x\n",
103                 drv.name, drv.unit, drv.mem, drv.memsize, drv.iobase,
104                 drv.irqmask, drv.flags);
105         sprintf(name, CARD_DEVICE, slot);
106         fd = open(name, O_RDWR);
107         if (fd < 0)
108                 err(1, "%s", name);
109
110         /* Map the memory and I/O contexts. */
111         if (drv.mem) {
112                 mem.window = 0;
113                 mem.flags = MDF_ACTIVE | MDF_16BITS;
114                 mem.start = (caddr_t)drv.mem;
115                 mem.size = drv.memsize;
116                 mem.card = card_addr;
117                 if (ioctl(fd, PIOCSMEM, &mem))
118                         err(1, "set memory context");
119         }
120         if (drv.iobase) {
121                 io.window = 0;
122                 io.flags = IODF_ACTIVE | IODF_CS16;
123                 io.start = drv.iobase;
124                 io.size = 32;   /* Blah... */
125                 if (ioctl(fd, PIOCSIO, &io))
126                         err(1, "set I/O context");
127         }
128         if (ioctl(fd, PIOCSDRV, &drv))
129                 warn("set driver");
130         close(fd);
131         return 0;
132 }
133
134 /*
135  *      usage - print usage and exit
136  */
137 void
138 usage(msg)
139         char   *msg;
140 {
141         fprintf(stderr, "enabler: %s\n", msg);
142         fprintf(stderr,
143 "usage: pccardc enabler slot driver [-m addr size] [-a iobase] [-i irq]\n");
144         fprintf(stderr,
145 "    -m card addr size : card address (hex), host address (hex) & size (Kb)\n");
146         fprintf(stderr,
147 "    -a iobase         : I/O port address (hex)\n");
148         fprintf(stderr,
149 "    -i irq            : interrupt request number (1-15)\n");
150         fprintf(stderr,
151 "   Example:  enabler 0 ed0 -m 2000 d4000 16 -a 300 -i 3\n");
152         exit(1);
153 }