1 /* $NetBSD: kern_drvctl.c,v 1.7 2006/10/12 04:29:37 thorpej 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.7 2006/10/12 04:29:37 thorpej 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 __unused, u_long cmd, caddr_t data, int flag, 131 struct lwp *p) 132 { 133 int res; 134 char *ifattr; 135 int *locs; 136 137 switch (cmd) { 138 case DRVDETACHDEV: 139 #define d ((struct devdetachargs *)data) 140 res = detachdevbyname(d->devname); 141 #undef d 142 break; 143 case DRVRESCANBUS: 144 #define d ((struct devrescanargs *)data) 145 d->busname[sizeof(d->busname) - 1] = '\0'; 146 147 /* XXX better copyin? */ 148 if (d->ifattr[0]) { 149 d->ifattr[sizeof(d->ifattr) - 1] = '\0'; 150 ifattr = d->ifattr; 151 } else 152 ifattr = 0; 153 154 if (d->numlocators) { 155 if (d->numlocators > MAXLOCATORS) 156 return (EINVAL); 157 locs = malloc(d->numlocators * sizeof(int), M_DEVBUF, 158 M_WAITOK); 159 res = copyin(d->locators, locs, 160 d->numlocators * sizeof(int)); 161 if (res) 162 return (res); 163 } else 164 locs = 0; 165 res = rescanbus(d->busname, ifattr, d->numlocators, locs); 166 if (locs) 167 free(locs, M_DEVBUF); 168 #undef d 169 break; 170 case DRVCTLCOMMAND: 171 res = drvctl_command(p, (struct plistref *)data, cmd, flag); 172 break; 173 default: 174 return (EPASSTHROUGH); 175 } 176 return (res); 177 } 178 179 void 180 drvctlattach(int arg __unused) 181 { 182 } 183 184 /***************************************************************************** 185 * Driver control command processing engine 186 *****************************************************************************/ 187 188 static int 189 drvctl_command_get_properties(struct lwp *l __unused, 190 prop_dictionary_t command_dict, 191 prop_dictionary_t results_dict) 192 { 193 prop_dictionary_t args_dict; 194 prop_string_t devname_string; 195 device_t dev; 196 197 args_dict = prop_dictionary_get(command_dict, "drvctl-arguments"); 198 if (args_dict == NULL) 199 return (EINVAL); 200 201 devname_string = prop_dictionary_get(args_dict, "device-name"); 202 if (devname_string == NULL) 203 return (EINVAL); 204 205 TAILQ_FOREACH(dev, &alldevs, dv_list) { 206 if (prop_string_equals_cstring(devname_string, 207 device_xname(dev))) 208 break; 209 } 210 211 if (dev == NULL) 212 return (ESRCH); 213 214 prop_dictionary_set(results_dict, "drvctl-result-data", 215 device_properties(dev)); 216 217 return (0); 218 } 219 220 struct drvctl_command_desc { 221 const char *dcd_name; /* command name */ 222 int (*dcd_func)(struct lwp *, /* handler function */ 223 prop_dictionary_t, 224 prop_dictionary_t); 225 int dcd_rw; /* read or write required */ 226 }; 227 228 static const struct drvctl_command_desc drvctl_command_table[] = { 229 { .dcd_name = "get-properties", 230 .dcd_func = drvctl_command_get_properties, 231 .dcd_rw = FREAD, 232 }, 233 234 { .dcd_name = NULL } 235 }; 236 237 static int 238 drvctl_command(struct lwp *l, struct plistref *pref, u_long ioctl_cmd, 239 int fflag) 240 { 241 prop_dictionary_t command_dict, results_dict; 242 prop_string_t command_string; 243 prop_number_t error_number; 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 error_number = prop_number_create_integer(error); 282 prop_dictionary_set(results_dict, "drvctl-error", error_number); 283 prop_object_release(error_number); 284 285 error = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, results_dict); 286 out: 287 prop_object_release(command_dict); 288 prop_object_release(results_dict); 289 return (error); 290 } 291