1 /* $NetBSD: kern_drvctl.c,v 1.11 2007/04/03 23:02:39 rmind Exp $ */ 2 3 /* 4 * Copyright (c) 2004 5 * Matthias Drochner. 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.11 2007/04/03 23:02:39 rmind Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/conf.h> 36 #include <sys/device.h> 37 #include <sys/event.h> 38 #include <sys/malloc.h> 39 #include <sys/ioctl.h> 40 #include <sys/fcntl.h> 41 #include <sys/drvctlio.h> 42 43 dev_type_ioctl(drvctlioctl); 44 45 const struct cdevsw drvctl_cdevsw = { 46 nullopen, nullclose, nullread, nullwrite, drvctlioctl, 47 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER 48 }; 49 50 void drvctlattach(int); 51 52 #define MAXLOCATORS 100 53 54 static int drvctl_command(struct lwp *, struct plistref *, u_long, int flag); 55 56 static int 57 detachdevbyname(const char *devname) 58 { 59 struct device *d; 60 61 TAILQ_FOREACH(d, &alldevs, dv_list) { 62 if (!strcmp(devname, d->dv_xname)) { 63 #ifndef XXXFULLRISK 64 /* 65 * If the parent cannot be notified, it might keep 66 * pointers to the detached device. 67 * There might be a private notification mechanism, 68 * but better play save here. 69 */ 70 if (d->dv_parent && 71 !d->dv_parent->dv_cfattach->ca_childdetached) 72 return (ENOTSUP); 73 #endif 74 return (config_detach(d, 0)); 75 } 76 } 77 78 return (ENXIO); 79 } 80 81 static int 82 rescanbus(const char *busname, const char *ifattr, 83 int numlocators, const int *locators) 84 { 85 int i; 86 struct device *d; 87 const struct cfiattrdata * const *ap; 88 89 /* XXX there should be a way to get limits and defaults (per device) 90 from config generated data */ 91 int locs[MAXLOCATORS]; 92 for (i = 0; i < MAXLOCATORS; i++) 93 locs[i] = -1; 94 95 for (i = 0; i < numlocators;i++) 96 locs[i] = locators[i]; 97 98 TAILQ_FOREACH(d, &alldevs, dv_list) { 99 if (!strcmp(busname, d->dv_xname)) { 100 /* 101 * must support rescan, and must have something 102 * to attach to 103 */ 104 if (!d->dv_cfattach->ca_rescan || 105 !d->dv_cfdriver->cd_attrs) 106 return (ENODEV); 107 108 /* allow to omit attribute if there is exactly one */ 109 if (!ifattr) { 110 if (d->dv_cfdriver->cd_attrs[1]) 111 return (EINVAL); 112 ifattr = d->dv_cfdriver->cd_attrs[0]->ci_name; 113 } else { 114 /* check for valid attribute passed */ 115 for (ap = d->dv_cfdriver->cd_attrs; *ap; ap++) 116 if (!strcmp((*ap)->ci_name, ifattr)) 117 break; 118 if (!*ap) 119 return (EINVAL); 120 } 121 122 return (*d->dv_cfattach->ca_rescan)(d, ifattr, locs); 123 } 124 } 125 126 return (ENXIO); 127 } 128 129 int 130 drvctlioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *p) 131 { 132 int res; 133 char *ifattr; 134 int *locs; 135 136 switch (cmd) { 137 case DRVDETACHDEV: 138 #define d ((struct devdetachargs *)data) 139 res = detachdevbyname(d->devname); 140 #undef d 141 break; 142 case DRVRESCANBUS: 143 #define d ((struct devrescanargs *)data) 144 d->busname[sizeof(d->busname) - 1] = '\0'; 145 146 /* XXX better copyin? */ 147 if (d->ifattr[0]) { 148 d->ifattr[sizeof(d->ifattr) - 1] = '\0'; 149 ifattr = d->ifattr; 150 } else 151 ifattr = 0; 152 153 if (d->numlocators) { 154 if (d->numlocators > MAXLOCATORS) 155 return (EINVAL); 156 locs = malloc(d->numlocators * sizeof(int), M_DEVBUF, 157 M_WAITOK); 158 res = copyin(d->locators, locs, 159 d->numlocators * sizeof(int)); 160 if (res) { 161 free(locs, M_DEVBUF); 162 return (res); 163 } 164 } else 165 locs = 0; 166 res = rescanbus(d->busname, ifattr, d->numlocators, locs); 167 if (locs) 168 free(locs, M_DEVBUF); 169 #undef d 170 break; 171 case DRVCTLCOMMAND: 172 res = drvctl_command(p, (struct plistref *)data, cmd, flag); 173 break; 174 default: 175 return (EPASSTHROUGH); 176 } 177 return (res); 178 } 179 180 void 181 drvctlattach(int arg) 182 { 183 } 184 185 /***************************************************************************** 186 * Driver control command processing engine 187 *****************************************************************************/ 188 189 static int 190 drvctl_command_get_properties(struct lwp *l, 191 prop_dictionary_t command_dict, 192 prop_dictionary_t results_dict) 193 { 194 prop_dictionary_t args_dict; 195 prop_string_t devname_string; 196 device_t dev; 197 198 args_dict = prop_dictionary_get(command_dict, "drvctl-arguments"); 199 if (args_dict == NULL) 200 return (EINVAL); 201 202 devname_string = prop_dictionary_get(args_dict, "device-name"); 203 if (devname_string == NULL) 204 return (EINVAL); 205 206 TAILQ_FOREACH(dev, &alldevs, dv_list) { 207 if (prop_string_equals_cstring(devname_string, 208 device_xname(dev))) 209 break; 210 } 211 212 if (dev == NULL) 213 return (ESRCH); 214 215 prop_dictionary_set(results_dict, "drvctl-result-data", 216 device_properties(dev)); 217 218 return (0); 219 } 220 221 struct drvctl_command_desc { 222 const char *dcd_name; /* command name */ 223 int (*dcd_func)(struct lwp *, /* handler function */ 224 prop_dictionary_t, 225 prop_dictionary_t); 226 int dcd_rw; /* read or write required */ 227 }; 228 229 static const struct drvctl_command_desc drvctl_command_table[] = { 230 { .dcd_name = "get-properties", 231 .dcd_func = drvctl_command_get_properties, 232 .dcd_rw = FREAD, 233 }, 234 235 { .dcd_name = NULL } 236 }; 237 238 static int 239 drvctl_command(struct lwp *l, struct plistref *pref, u_long ioctl_cmd, 240 int fflag) 241 { 242 prop_dictionary_t command_dict, results_dict; 243 prop_string_t command_string; 244 const struct drvctl_command_desc *dcd; 245 int error; 246 247 error = prop_dictionary_copyin_ioctl(pref, ioctl_cmd, &command_dict); 248 if (error) 249 return (error); 250 251 results_dict = prop_dictionary_create(); 252 if (results_dict == NULL) { 253 prop_object_release(command_dict); 254 return (ENOMEM); 255 } 256 257 command_string = prop_dictionary_get(command_dict, "drvctl-command"); 258 if (command_string == NULL) { 259 error = EINVAL; 260 goto out; 261 } 262 263 for (dcd = drvctl_command_table; dcd->dcd_name != NULL; dcd++) { 264 if (prop_string_equals_cstring(command_string, 265 dcd->dcd_name)) 266 break; 267 } 268 269 if (dcd->dcd_name == NULL) { 270 error = EINVAL; 271 goto out; 272 } 273 274 if ((fflag & dcd->dcd_rw) == 0) { 275 error = EPERM; 276 goto out; 277 } 278 279 error = (*dcd->dcd_func)(l, command_dict, results_dict); 280 281 prop_dictionary_set_int32(results_dict, "drvctl-error", error); 282 283 error = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, results_dict); 284 out: 285 prop_object_release(command_dict); 286 prop_object_release(results_dict); 287 return (error); 288 } 289