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