devopen.c revision 1.1.6.2 1 1.1.6.2 bouyer /* $NetBSD: devopen.c,v 1.1.6.2 2000/11/20 20:23:08 bouyer Exp $ */
2 1.1.6.2 bouyer
3 1.1.6.2 bouyer /*-
4 1.1.6.2 bouyer * Copyright (c) 1993 John Brezak
5 1.1.6.2 bouyer * All rights reserved.
6 1.1.6.2 bouyer *
7 1.1.6.2 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 bouyer * modification, are permitted provided that the following conditions
9 1.1.6.2 bouyer * are met:
10 1.1.6.2 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 bouyer * documentation and/or other materials provided with the distribution.
15 1.1.6.2 bouyer * 3. The name of the author may not be used to endorse or promote products
16 1.1.6.2 bouyer * derived from this software without specific prior written permission.
17 1.1.6.2 bouyer *
18 1.1.6.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19 1.1.6.2 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1.6.2 bouyer * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1.6.2 bouyer * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 1.1.6.2 bouyer * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 1.1.6.2 bouyer * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1.6.2 bouyer * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1.6.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 1.1.6.2 bouyer * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 1.1.6.2 bouyer * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1.6.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
29 1.1.6.2 bouyer */
30 1.1.6.2 bouyer
31 1.1.6.2 bouyer #include <lib/libsa/stand.h>
32 1.1.6.2 bouyer
33 1.1.6.2 bouyer #define ispart(c) ((c) >= 'a' && (c) <= 'h')
34 1.1.6.2 bouyer
35 1.1.6.2 bouyer int atoi __P((char *));
36 1.1.6.2 bouyer int devlookup __P((char *));
37 1.1.6.2 bouyer int devparse __P((const char *, int *, int *, int *, int *, int *, char **));
38 1.1.6.2 bouyer
39 1.1.6.2 bouyer int
40 1.1.6.2 bouyer atoi(cp)
41 1.1.6.2 bouyer char *cp;
42 1.1.6.2 bouyer {
43 1.1.6.2 bouyer int val = 0;
44 1.1.6.2 bouyer
45 1.1.6.2 bouyer while (isdigit(*cp))
46 1.1.6.2 bouyer val = val * 10 + (*cp++ - '0');
47 1.1.6.2 bouyer return (val);
48 1.1.6.2 bouyer }
49 1.1.6.2 bouyer
50 1.1.6.2 bouyer int
51 1.1.6.2 bouyer devlookup(d)
52 1.1.6.2 bouyer char *d;
53 1.1.6.2 bouyer {
54 1.1.6.2 bouyer struct devsw *dp = devsw;
55 1.1.6.2 bouyer int i;
56 1.1.6.2 bouyer
57 1.1.6.2 bouyer for (i = 0; i < ndevs; i++, dp++)
58 1.1.6.2 bouyer if (dp->dv_name && strcmp(dp->dv_name, d) == 0)
59 1.1.6.2 bouyer return (i);
60 1.1.6.2 bouyer
61 1.1.6.2 bouyer printf("No such device - Configured devices are:\n");
62 1.1.6.2 bouyer for (dp = devsw, i = 0; i < ndevs; i++, dp++)
63 1.1.6.2 bouyer if (dp->dv_name)
64 1.1.6.2 bouyer printf(" %s", dp->dv_name);
65 1.1.6.2 bouyer printf("\n");
66 1.1.6.2 bouyer return (-1);
67 1.1.6.2 bouyer }
68 1.1.6.2 bouyer
69 1.1.6.2 bouyer /*
70 1.1.6.2 bouyer * Parse a device spec in one of two forms.
71 1.1.6.2 bouyer * dev(ctlr, unit, part)file
72 1.1.6.2 bouyer */
73 1.1.6.2 bouyer int
74 1.1.6.2 bouyer devparse(fname, dev, adapt, ctlr, unit, part, file)
75 1.1.6.2 bouyer const char *fname;
76 1.1.6.2 bouyer int *dev;
77 1.1.6.2 bouyer int *adapt;
78 1.1.6.2 bouyer int *ctlr;
79 1.1.6.2 bouyer int *unit;
80 1.1.6.2 bouyer int *part;
81 1.1.6.2 bouyer char **file;
82 1.1.6.2 bouyer {
83 1.1.6.2 bouyer int argc, flag;
84 1.1.6.2 bouyer char *s, *args[3];
85 1.1.6.2 bouyer extern char nametmp[];
86 1.1.6.2 bouyer
87 1.1.6.2 bouyer /* get device name and make lower case */
88 1.1.6.2 bouyer strcpy(nametmp, (char *)fname);
89 1.1.6.2 bouyer for (s = nametmp; *s && *s != '('; s++)
90 1.1.6.2 bouyer if (isupper(*s)) *s = tolower(*s);
91 1.1.6.2 bouyer
92 1.1.6.2 bouyer if (*s == '(') {
93 1.1.6.2 bouyer /* lookup device and get index */
94 1.1.6.2 bouyer *s = NULL;
95 1.1.6.2 bouyer if ((*dev = devlookup(nametmp)) < 0)
96 1.1.6.2 bouyer goto baddev;
97 1.1.6.2 bouyer
98 1.1.6.2 bouyer /* tokenize device ident */
99 1.1.6.2 bouyer for (++s, flag = 0, argc = 0; *s && *s != ')'; s++) {
100 1.1.6.2 bouyer if (*s != ',') {
101 1.1.6.2 bouyer if (!flag) {
102 1.1.6.2 bouyer flag++;
103 1.1.6.2 bouyer args[argc++] = s;
104 1.1.6.2 bouyer }
105 1.1.6.2 bouyer } else {
106 1.1.6.2 bouyer if (flag) {
107 1.1.6.2 bouyer *s = NULL;
108 1.1.6.2 bouyer flag = 0;
109 1.1.6.2 bouyer }
110 1.1.6.2 bouyer }
111 1.1.6.2 bouyer }
112 1.1.6.2 bouyer if (*s == ')')
113 1.1.6.2 bouyer *s = NULL;
114 1.1.6.2 bouyer
115 1.1.6.2 bouyer switch (argc) {
116 1.1.6.2 bouyer case 3:
117 1.1.6.2 bouyer *part = atoi(args[2]);
118 1.1.6.2 bouyer /* FALL THROUGH */
119 1.1.6.2 bouyer case 2:
120 1.1.6.2 bouyer *unit = atoi(args[1]);
121 1.1.6.2 bouyer /* FALL THROUGH */
122 1.1.6.2 bouyer case 1:
123 1.1.6.2 bouyer *ctlr = atoi(args[0]);
124 1.1.6.2 bouyer break;
125 1.1.6.2 bouyer }
126 1.1.6.2 bouyer *file = ++s;
127 1.1.6.2 bouyer } else {
128 1.1.6.2 bouyer /* no device present */
129 1.1.6.2 bouyer *file = (char *)fname;
130 1.1.6.2 bouyer }
131 1.1.6.2 bouyer return (0);
132 1.1.6.2 bouyer
133 1.1.6.2 bouyer baddev:
134 1.1.6.2 bouyer return (EINVAL);
135 1.1.6.2 bouyer }
136 1.1.6.2 bouyer
137 1.1.6.2 bouyer int
138 1.1.6.2 bouyer devopen(f, fname, file)
139 1.1.6.2 bouyer struct open_file *f;
140 1.1.6.2 bouyer const char *fname;
141 1.1.6.2 bouyer char **file;
142 1.1.6.2 bouyer {
143 1.1.6.2 bouyer int error;
144 1.1.6.2 bouyer int dev = 0, ctlr = 0, unit = 0, part = 0;
145 1.1.6.2 bouyer int adapt = 0;
146 1.1.6.2 bouyer struct devsw *dp = &devsw[0];
147 1.1.6.2 bouyer
148 1.1.6.2 bouyer if ((error =
149 1.1.6.2 bouyer devparse(fname, &dev, &adapt, &ctlr, &unit, &part, file)) != 0)
150 1.1.6.2 bouyer return (error);
151 1.1.6.2 bouyer
152 1.1.6.2 bouyer dp = &devsw[dev];
153 1.1.6.2 bouyer if (!dp->dv_open)
154 1.1.6.2 bouyer return (ENODEV);
155 1.1.6.2 bouyer
156 1.1.6.2 bouyer f->f_dev = dp;
157 1.1.6.2 bouyer if ((error = (*dp->dv_open)(f, ctlr, unit, part)) == 0)
158 1.1.6.2 bouyer return (0);
159 1.1.6.2 bouyer
160 1.1.6.2 bouyer printf("%s(%d,%d,%d): %s\n", devsw[dev].dv_name,
161 1.1.6.2 bouyer ctlr, unit, part, strerror(error));
162 1.1.6.2 bouyer
163 1.1.6.2 bouyer return (error);
164 1.1.6.2 bouyer }
165