1*62b0bd50Smsaitoh /* $NetBSD: devopen.c,v 1.5 2014/06/19 15:09:50 msaitoh Exp $ */
2c188bc0cScgd
3c188bc0cScgd /*-
4c188bc0cScgd * Copyright (c) 1992, 1993
5c188bc0cScgd * The Regents of the University of California. All rights reserved.
6c188bc0cScgd *
7c188bc0cScgd * This code is derived from software contributed to Berkeley by
8c188bc0cScgd * Ralph Campbell.
9c188bc0cScgd *
10c188bc0cScgd * Redistribution and use in source and binary forms, with or without
11c188bc0cScgd * modification, are permitted provided that the following conditions
12c188bc0cScgd * are met:
13c188bc0cScgd * 1. Redistributions of source code must retain the above copyright
14c188bc0cScgd * notice, this list of conditions and the following disclaimer.
15c188bc0cScgd * 2. Redistributions in binary form must reproduce the above copyright
16c188bc0cScgd * notice, this list of conditions and the following disclaimer in the
17c188bc0cScgd * documentation and/or other materials provided with the distribution.
18aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
19c188bc0cScgd * may be used to endorse or promote products derived from this software
20c188bc0cScgd * without specific prior written permission.
21c188bc0cScgd *
22c188bc0cScgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23c188bc0cScgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24c188bc0cScgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25c188bc0cScgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26c188bc0cScgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27c188bc0cScgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28c188bc0cScgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29c188bc0cScgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30c188bc0cScgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31c188bc0cScgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32c188bc0cScgd * SUCH DAMAGE.
33c188bc0cScgd *
34c188bc0cScgd * @(#)devopen.c 8.1 (Berkeley) 6/10/93
35c188bc0cScgd */
36c188bc0cScgd
37c188bc0cScgd #include <lib/libsa/stand.h>
38c188bc0cScgd
39c188bc0cScgd /*
40c188bc0cScgd * Decode the string 'fname', open the device and return the remaining
41c188bc0cScgd * file name if any.
42c188bc0cScgd */
43c188bc0cScgd int
devopen(struct open_file * f,const char * fname,char ** file)4482357f6dSdsl devopen(struct open_file *f, const char *fname, char **file)
4582357f6dSdsl /* file: out */
46c188bc0cScgd {
47c188bc0cScgd register char *cp;
48c188bc0cScgd register struct devsw *dp;
49c188bc0cScgd #if 0
50*62b0bd50Smsaitoh register char *ncp;
51c188bc0cScgd register int c, i;
52*62b0bd50Smsaitoh char namebuf[20];
53c188bc0cScgd #endif
54c188bc0cScgd int ctlr = 0, unit = 0, part = 0;
55c188bc0cScgd int rc;
56c188bc0cScgd
57c188bc0cScgd cp = (char *)fname;
58*62b0bd50Smsaitoh #if 0
59c188bc0cScgd ncp = namebuf;
60c188bc0cScgd
61c188bc0cScgd /* look for a string like '5/rz0/vmunix' or '5/rz3f/vmunix */
62c188bc0cScgd if ((c = *cp) >= '0' && c <= '9') {
63c188bc0cScgd ctlr = c - '0';
64c188bc0cScgd /* skip the '/' */
65c188bc0cScgd if (*++cp != '/')
66c188bc0cScgd return (ENXIO);
67c188bc0cScgd cp++;
68c188bc0cScgd while ((c = *cp) != '\0') {
69c188bc0cScgd if (c == '/')
70c188bc0cScgd break;
71c188bc0cScgd if (c >= '0' && c <= '9') {
72c188bc0cScgd /* read unit number */
73c188bc0cScgd unit = c - '0';
74c188bc0cScgd
75c188bc0cScgd /* look for a partition */
76c188bc0cScgd if ((c = *++cp) >= 'a' && c <= 'h') {
77c188bc0cScgd part = c - 'a';
78c188bc0cScgd c = *++cp;
79c188bc0cScgd }
80c188bc0cScgd if (c != '/')
81c188bc0cScgd return (ENXIO);
82c188bc0cScgd break;
83c188bc0cScgd }
84c188bc0cScgd if (ncp < namebuf + sizeof(namebuf) - 1)
85c188bc0cScgd *ncp++ = c;
86c188bc0cScgd cp++;
87c188bc0cScgd }
88c188bc0cScgd *ncp = '\0';
89c188bc0cScgd /*
90c188bc0cScgd * XXX
91c188bc0cScgd * pulling strchr from the C library, should pull from libkern.
92c188bc0cScgd */
93c188bc0cScgd } else if (strchr(cp, '(')) {
94c188bc0cScgd /* expect a string like 'rz(0,0,0)vmunix' */
95c188bc0cScgd while ((c = *cp) != '\0') {
96c188bc0cScgd if (c == '(') {
97c188bc0cScgd cp++;
98c188bc0cScgd break;
99c188bc0cScgd }
100c188bc0cScgd if (ncp < namebuf + sizeof(namebuf) - 1)
101c188bc0cScgd *ncp++ = c;
102c188bc0cScgd cp++;
103c188bc0cScgd }
104c188bc0cScgd
105c188bc0cScgd /* get controller number */
106c188bc0cScgd if ((c = *cp) >= '0' && c <= '9') {
107c188bc0cScgd ctlr = c - '0';
108c188bc0cScgd c = *++cp;
109c188bc0cScgd }
110c188bc0cScgd
111c188bc0cScgd if (c == ',') {
112c188bc0cScgd /* get SCSI device number */
113c188bc0cScgd if ((c = *++cp) >= '0' && c <= '9') {
114c188bc0cScgd unit = c - '0';
115c188bc0cScgd c = *++cp;
116c188bc0cScgd }
117c188bc0cScgd
118c188bc0cScgd if (c == ',') {
119c188bc0cScgd /* get partition number */
120c188bc0cScgd if ((c = *++cp) >= '0' && c <= '9') {
121c188bc0cScgd part = c - '0';
122c188bc0cScgd c = *++cp;
123c188bc0cScgd }
124c188bc0cScgd }
125c188bc0cScgd }
126c188bc0cScgd if (c != ')')
127c188bc0cScgd return (ENXIO);
128c188bc0cScgd cp++;
129c188bc0cScgd *ncp = '\0';
130c188bc0cScgd } else {
131c188bc0cScgd #endif
132c188bc0cScgd dp = devsw;
133c188bc0cScgd ctlr = unit = part = 0;
134c188bc0cScgd goto fnd;
135c188bc0cScgd #if 0
136c188bc0cScgd }
137c188bc0cScgd
138c188bc0cScgd for (dp = devsw, i = 0; i < ndevs; dp++, i++)
139c188bc0cScgd if (dp->dv_name && strcmp(namebuf, dp->dv_name) == 0)
140c188bc0cScgd goto fnd;
141c188bc0cScgd printf("Unknown device '%s'\nKnown devices are:", namebuf);
142c188bc0cScgd for (dp = devsw, i = 0; i < ndevs; dp++, i++)
143c188bc0cScgd if (dp->dv_name)
144c188bc0cScgd printf(" %s", dp->dv_name);
145c188bc0cScgd printf("\n");
146c188bc0cScgd return (ENXIO);
147c188bc0cScgd #endif
148c188bc0cScgd
149c188bc0cScgd fnd:
150c188bc0cScgd rc = (dp->dv_open)(f, ctlr, unit, part);
151c188bc0cScgd if (rc)
152c188bc0cScgd return (rc);
153c188bc0cScgd
154c188bc0cScgd f->f_dev = dp;
155c188bc0cScgd if (file && *cp != '\0')
156c188bc0cScgd *file = cp;
157c188bc0cScgd return (0);
158c188bc0cScgd }
159