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