kern_drvctl.c revision 1.13 1 /* $NetBSD: kern_drvctl.c,v 1.13 2008/02/06 20:24:17 drochner 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.13 2008/02/06 20:24:17 drochner 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 static struct device *devbyname(const char *);
52
53 #define MAXLOCATORS 100
54
55 static int drvctl_command(struct lwp *, struct plistref *, u_long, int flag);
56
57 static struct device *
58 devbyname(const char *devname)
59 {
60 struct device *d;
61
62 TAILQ_FOREACH(d, &alldevs, dv_list) {
63 if (strcmp(devname, device_xname(d)) == 0)
64 break;
65 }
66 return d;
67 }
68
69 static int
70 pmdevbyname(int cmd, struct devpmargs *a)
71 {
72 struct device *d;
73
74 if ((d = devbyname(a->devname)) == NULL)
75 return ENXIO;
76
77 switch (cmd) {
78 case DRVSUSPENDDEV:
79 return pmf_device_recursive_suspend(d) ? 0 : EBUSY;
80 case DRVRESUMEDEV:
81 if (a->flags & DEVPM_F_SUBTREE)
82 return pmf_device_resume_subtree(d) ? 0 : EBUSY;
83 else
84 return pmf_device_recursive_resume(d) ? 0 : EBUSY;
85 default:
86 return EPASSTHROUGH;
87 }
88 }
89
90 static int
91 listdevbyname(struct devlistargs *l)
92 {
93 device_t d, child;
94 int cnt = 0, idx, error;
95
96 if ((d = devbyname(l->l_devname)) == NULL)
97 return ENXIO;
98
99 TAILQ_FOREACH(child, &alldevs, dv_list) {
100 if (device_parent(child) != d)
101 continue;
102 idx = cnt++;
103 if (l->l_childname == NULL || idx >= l->l_children)
104 continue;
105 error = copyoutstr(device_xname(child), l->l_childname[idx],
106 sizeof(l->l_childname[idx]), NULL);
107 if (error)
108 return error;
109 }
110
111 l->l_children = cnt;
112 return 0;
113 }
114
115 static int
116 detachdevbyname(const char *devname)
117 {
118 struct device *d;
119
120 if ((d = devbyname(devname)) == NULL)
121 return ENXIO;
122
123 #ifndef XXXFULLRISK
124 /*
125 * If the parent cannot be notified, it might keep
126 * pointers to the detached device.
127 * There might be a private notification mechanism,
128 * but better play save here.
129 */
130 if (d->dv_parent && !d->dv_parent->dv_cfattach->ca_childdetached)
131 return (ENOTSUP);
132 #endif
133 return (config_detach(d, 0));
134 }
135
136 static int
137 rescanbus(const char *busname, const char *ifattr,
138 int numlocators, const int *locators)
139 {
140 int i, rc;
141 struct device *d;
142 const struct cfiattrdata * const *ap;
143
144 /* XXX there should be a way to get limits and defaults (per device)
145 from config generated data */
146 int locs[MAXLOCATORS];
147 for (i = 0; i < MAXLOCATORS; i++)
148 locs[i] = -1;
149
150 for (i = 0; i < numlocators;i++)
151 locs[i] = locators[i];
152
153 if ((d = devbyname(busname)) == NULL)
154 return ENXIO;
155
156 /*
157 * must support rescan, and must have something
158 * to attach to
159 */
160 if (!d->dv_cfattach->ca_rescan ||
161 !d->dv_cfdriver->cd_attrs)
162 return (ENODEV);
163
164 /* allow to omit attribute if there is exactly one */
165 if (!ifattr) {
166 if (d->dv_cfdriver->cd_attrs[1])
167 return (EINVAL);
168 ifattr = d->dv_cfdriver->cd_attrs[0]->ci_name;
169 } else {
170 /* check for valid attribute passed */
171 for (ap = d->dv_cfdriver->cd_attrs; *ap; ap++)
172 if (!strcmp((*ap)->ci_name, ifattr))
173 break;
174 if (!*ap)
175 return (EINVAL);
176 }
177
178 rc = (*d->dv_cfattach->ca_rescan)(d, ifattr, locs);
179 config_deferred(NULL);
180 return rc;
181 }
182
183 int
184 drvctlioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *p)
185 {
186 int res;
187 char *ifattr;
188 int *locs;
189
190 switch (cmd) {
191 case DRVSUSPENDDEV:
192 case DRVRESUMEDEV:
193 #define d ((struct devpmargs *)data)
194 res = pmdevbyname(cmd, d);
195 #undef d
196 break;
197 case DRVLISTDEV:
198 res = listdevbyname((struct devlistargs *)data);
199 break;
200 case DRVDETACHDEV:
201 #define d ((struct devdetachargs *)data)
202 res = detachdevbyname(d->devname);
203 #undef d
204 break;
205 case DRVRESCANBUS:
206 #define d ((struct devrescanargs *)data)
207 d->busname[sizeof(d->busname) - 1] = '\0';
208
209 /* XXX better copyin? */
210 if (d->ifattr[0]) {
211 d->ifattr[sizeof(d->ifattr) - 1] = '\0';
212 ifattr = d->ifattr;
213 } else
214 ifattr = 0;
215
216 if (d->numlocators) {
217 if (d->numlocators > MAXLOCATORS)
218 return (EINVAL);
219 locs = malloc(d->numlocators * sizeof(int), M_DEVBUF,
220 M_WAITOK);
221 res = copyin(d->locators, locs,
222 d->numlocators * sizeof(int));
223 if (res) {
224 free(locs, M_DEVBUF);
225 return (res);
226 }
227 } else
228 locs = 0;
229 res = rescanbus(d->busname, ifattr, d->numlocators, locs);
230 if (locs)
231 free(locs, M_DEVBUF);
232 #undef d
233 break;
234 case DRVCTLCOMMAND:
235 res = drvctl_command(p, (struct plistref *)data, cmd, flag);
236 break;
237 default:
238 return (EPASSTHROUGH);
239 }
240 return (res);
241 }
242
243 void
244 drvctlattach(int arg)
245 {
246 }
247
248 /*****************************************************************************
249 * Driver control command processing engine
250 *****************************************************************************/
251
252 static int
253 drvctl_command_get_properties(struct lwp *l,
254 prop_dictionary_t command_dict,
255 prop_dictionary_t results_dict)
256 {
257 prop_dictionary_t args_dict;
258 prop_string_t devname_string;
259 device_t dev;
260
261 args_dict = prop_dictionary_get(command_dict, "drvctl-arguments");
262 if (args_dict == NULL)
263 return (EINVAL);
264
265 devname_string = prop_dictionary_get(args_dict, "device-name");
266 if (devname_string == NULL)
267 return (EINVAL);
268
269 TAILQ_FOREACH(dev, &alldevs, dv_list) {
270 if (prop_string_equals_cstring(devname_string,
271 device_xname(dev)))
272 break;
273 }
274
275 if (dev == NULL)
276 return (ESRCH);
277
278 prop_dictionary_set(results_dict, "drvctl-result-data",
279 device_properties(dev));
280
281 return (0);
282 }
283
284 struct drvctl_command_desc {
285 const char *dcd_name; /* command name */
286 int (*dcd_func)(struct lwp *, /* handler function */
287 prop_dictionary_t,
288 prop_dictionary_t);
289 int dcd_rw; /* read or write required */
290 };
291
292 static const struct drvctl_command_desc drvctl_command_table[] = {
293 { .dcd_name = "get-properties",
294 .dcd_func = drvctl_command_get_properties,
295 .dcd_rw = FREAD,
296 },
297
298 { .dcd_name = NULL }
299 };
300
301 static int
302 drvctl_command(struct lwp *l, struct plistref *pref, u_long ioctl_cmd,
303 int fflag)
304 {
305 prop_dictionary_t command_dict, results_dict;
306 prop_string_t command_string;
307 const struct drvctl_command_desc *dcd;
308 int error;
309
310 error = prop_dictionary_copyin_ioctl(pref, ioctl_cmd, &command_dict);
311 if (error)
312 return (error);
313
314 results_dict = prop_dictionary_create();
315 if (results_dict == NULL) {
316 prop_object_release(command_dict);
317 return (ENOMEM);
318 }
319
320 command_string = prop_dictionary_get(command_dict, "drvctl-command");
321 if (command_string == NULL) {
322 error = EINVAL;
323 goto out;
324 }
325
326 for (dcd = drvctl_command_table; dcd->dcd_name != NULL; dcd++) {
327 if (prop_string_equals_cstring(command_string,
328 dcd->dcd_name))
329 break;
330 }
331
332 if (dcd->dcd_name == NULL) {
333 error = EINVAL;
334 goto out;
335 }
336
337 if ((fflag & dcd->dcd_rw) == 0) {
338 error = EPERM;
339 goto out;
340 }
341
342 error = (*dcd->dcd_func)(l, command_dict, results_dict);
343
344 prop_dictionary_set_int32(results_dict, "drvctl-error", error);
345
346 error = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, results_dict);
347 out:
348 prop_object_release(command_dict);
349 prop_object_release(results_dict);
350 return (error);
351 }
352