devopen.c revision 1.4 1 1.4 tsutsui /* $NetBSD: devopen.c,v 1.4 2003/09/28 08:21:08 tsutsui Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 1992, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to Berkeley by
8 1.1 thorpej * Ralph Campbell.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.3 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 thorpej * may be used to endorse or promote products derived from this software
20 1.1 thorpej * without specific prior written permission.
21 1.1 thorpej *
22 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 thorpej * SUCH DAMAGE.
33 1.1 thorpej *
34 1.1 thorpej * @(#)devopen.c 8.1 (Berkeley) 6/10/93
35 1.1 thorpej */
36 1.1 thorpej
37 1.1 thorpej #include <lib/libsa/stand.h>
38 1.1 thorpej #include <lib/libkern/libkern.h>
39 1.1 thorpej
40 1.1 thorpej /*
41 1.1 thorpej * Decode the string 'fname', open the device and return the remaining
42 1.1 thorpej * file name if any.
43 1.1 thorpej */
44 1.1 thorpej int
45 1.1 thorpej devopen(f, fname, file)
46 1.1 thorpej struct open_file *f;
47 1.1 thorpej const char *fname;
48 1.1 thorpej char **file; /* out */
49 1.1 thorpej {
50 1.1 thorpej /* int ctlr = 0, unit = 0, part = 0; */
51 1.1 thorpej int rc;
52 1.1 thorpej char namebuf[128];
53 1.1 thorpej char devtype[16];
54 1.1 thorpej const char *cp;
55 1.1 thorpej char *ncp;
56 1.1 thorpej #if !defined(LIBSA_SINGLE_DEVICE)
57 1.1 thorpej int i;
58 1.1 thorpej struct devsw *dp;
59 1.1 thorpej #endif
60 1.1 thorpej
61 1.1 thorpej cp = fname;
62 1.1 thorpej ncp = (char *)fname;
63 1.1 thorpej
64 1.2 rafal /*
65 1.2 rafal * If device starts with a PCI bus specifier, skip past it so the
66 1.2 rafal * device-matching code below gets the actual device type. Leave
67 1.2 rafal * fname as is, since it'll be passed back to ARCS to open the
68 1.2 rafal * device. This is necessary for the IP32.
69 1.2 rafal */
70 1.2 rafal if (strncmp(cp, "pci", 3) == 0) {
71 1.2 rafal while (*ncp && *ncp++ != ')')
72 1.2 rafal ;
73 1.2 rafal if (*ncp)
74 1.2 rafal cp = ncp;
75 1.2 rafal }
76 1.2 rafal
77 1.1 thorpej /*
78 1.2 rafal * Look for a string like 'scsi(0)disk(0)rdisk(0)partition(0)netbsd'
79 1.2 rafal * or 'dksc(0,0,0)/netbsd' (the file can either be a relative path
80 1.2 rafal * or an abosolute path).
81 1.1 thorpej */
82 1.2 rafal if (strncmp(cp, "scsi", 4) == 0) {
83 1.1 thorpej strcpy(devtype, "scsi");
84 1.2 rafal } else if (strncmp(cp, "dksc", 4) == 0) {
85 1.4 tsutsui strcpy(devtype, "dksc");
86 1.2 rafal } else {
87 1.2 rafal return ENXIO;
88 1.2 rafal }
89 1.2 rafal
90 1.1 thorpej while (ncp != NULL && *ncp != 0) {
91 1.1 thorpej while (*ncp && *ncp++ != ')')
92 1.1 thorpej ;
93 1.1 thorpej if (*ncp)
94 1.1 thorpej cp = ncp;
95 1.1 thorpej }
96 1.2 rafal
97 1.1 thorpej strncpy(namebuf, fname, sizeof(namebuf));
98 1.1 thorpej namebuf[cp - fname] = 0;
99 1.1 thorpej
100 1.1 thorpej printf("devopen: %s type %s file %s\n", namebuf, devtype, cp);
101 1.1 thorpej #ifdef LIBSA_SINGLE_DEVICE
102 1.1 thorpej rc = DEV_OPEN(dp)(f, fname);
103 1.1 thorpej #else /* !LIBSA_SINGLE_DEVICE */
104 1.1 thorpej for (dp = devsw, i = 0; i < ndevs; dp++, i++)
105 1.1 thorpej if (dp->dv_name && strcmp(devtype, dp->dv_name) == 0)
106 1.1 thorpej goto fnd;
107 1.1 thorpej printf("Unknown device '%s'\nKnown devices are:", devtype);
108 1.1 thorpej for (dp = devsw, i = 0; i < ndevs; dp++, i++)
109 1.1 thorpej if (dp->dv_name)
110 1.1 thorpej printf(" %s", dp->dv_name);
111 1.1 thorpej printf("\n");
112 1.2 rafal return ENXIO;
113 1.1 thorpej
114 1.1 thorpej fnd:
115 1.1 thorpej rc = (dp->dv_open)(f, namebuf);
116 1.1 thorpej #endif /* !LIBSA_SINGLE_DEVICE */
117 1.1 thorpej if (rc)
118 1.2 rafal return rc;
119 1.1 thorpej
120 1.1 thorpej #ifndef LIBSA_SINGLE_DEVICE
121 1.1 thorpej f->f_dev = dp;
122 1.1 thorpej #endif
123 1.1 thorpej if (file && *cp != '\0')
124 1.1 thorpej *file = (char *)cp; /* XXX */
125 1.2 rafal return 0;
126 1.1 thorpej }
127