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