1 /* $NetBSD: dev.c,v 1.4 1994/10/30 21:48:23 cgd Exp $ */ 2 3 /*- 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #include <sys/param.h> 34 #include <sys/reboot.h> 35 36 #include "stand.h" 37 38 int 39 nodev(void) 40 { 41 return (ENXIO); 42 } 43 44 void 45 nullsys(void) 46 { 47 } 48 49 /* ARGSUSED */ 50 int 51 noioctl(struct open_file *f __unused, u_long cmd __unused, void *data __unused) 52 { 53 return (EINVAL); 54 } 55 56 char * 57 devformat(struct devdesc *d) 58 { 59 static char name[DEV_DEVLEN]; 60 61 if (d->d_dev->dv_fmtdev != NULL) 62 return (d->d_dev->dv_fmtdev(d)); 63 snprintf(name, sizeof(name), "%s%d:", d->d_dev->dv_name, d->d_unit); 64 return (name); 65 } 66 67 /* NB: devspec points to the remainder of the device name after dv_name */ 68 static int 69 default_parsedev(struct devdesc **dev, const char *devspec, 70 const char **path) 71 { 72 struct devdesc *idev; 73 int unit, err; 74 char *cp; 75 76 idev = malloc(sizeof(struct devdesc)); 77 if (idev == NULL) 78 return (ENOMEM); 79 80 unit = 0; 81 cp = (char *)devspec; /* strtol interface, alas */ 82 83 if (*devspec != '\0' && *devspec != ':') { 84 errno = 0; 85 unit = strtol(devspec, &cp, 0); 86 if (errno != 0 || cp == devspec) { 87 err = EUNIT; 88 goto fail; 89 } 90 } 91 if (*cp != '\0' && *cp != ':') { 92 err = EINVAL; 93 goto fail; 94 } 95 96 idev->d_unit = unit; 97 if (path != NULL) 98 *path = (*cp == 0) ? cp : cp + 1; 99 *dev = idev; 100 return (0); 101 fail: 102 free(idev); 103 return (err); 104 } 105 106 /* NB: devspec points to the whole device spec, and possible trailing path */ 107 int 108 devparse(struct devdesc **dev, const char *devspec, const char **path) 109 { 110 struct devdesc *idev; 111 struct devsw *dv; 112 int i, err; 113 const char *np; 114 115 /* minimum length check */ 116 if (strlen(devspec) < 2) 117 return (EINVAL); 118 119 /* look for a device that matches */ 120 for (i = 0; devsw[i] != NULL; i++) { 121 dv = devsw[i]; 122 if (dv->dv_match != NULL) { 123 if (dv->dv_match(dv, devspec) != 0) 124 break; 125 } else { 126 if (!strncmp(devspec, dv->dv_name, strlen(dv->dv_name))) 127 break; 128 } 129 } 130 if (devsw[i] == NULL) 131 return (ENOENT); 132 idev = NULL; 133 err = 0; 134 if (dv->dv_parsedev) { 135 err = dv->dv_parsedev(&idev, devspec, path); 136 } else { 137 np = devspec + strlen(dv->dv_name); 138 err = default_parsedev(&idev, np, path); 139 } 140 if (err != 0) 141 return (err); 142 143 idev->d_dev = dv; 144 if (dev != NULL) 145 *dev = idev; 146 else 147 free(idev); 148 return (0); 149 } 150 151 int 152 devinit(void) 153 { 154 int err = 0; 155 156 /* 157 * March through the device switch probing for things. 158 */ 159 for (int i = 0; devsw[i] != NULL; i++) { 160 if (devsw[i]->dv_init != NULL) { 161 if ((devsw[i]->dv_init)() != 0) { 162 err++; 163 } 164 } 165 } 166 return (err); 167 } 168 169 void 170 dev_cleanup(void) 171 { 172 int i; 173 174 /* Call cleanup routines */ 175 for (i = 0; devsw[i] != NULL; ++i) 176 if (devsw[i]->dv_cleanup != NULL) 177 (devsw[i]->dv_cleanup)(); 178 } 179