subr_devsw.c revision 1.29 1 1.29 mrg /* $NetBSD: subr_devsw.c,v 1.29 2011/12/12 19:03:12 mrg Exp $ */
2 1.11 ad
3 1.2 gehenna /*-
4 1.20 ad * Copyright (c) 2001, 2002, 2007, 2008 The NetBSD Foundation, Inc.
5 1.2 gehenna * All rights reserved.
6 1.2 gehenna *
7 1.2 gehenna * This code is derived from software contributed to The NetBSD Foundation
8 1.11 ad * by MAEKAWA Masahide <gehenna (at) NetBSD.org>, and by Andrew Doran.
9 1.2 gehenna *
10 1.2 gehenna * Redistribution and use in source and binary forms, with or without
11 1.2 gehenna * modification, are permitted provided that the following conditions
12 1.2 gehenna * are met:
13 1.2 gehenna * 1. Redistributions of source code must retain the above copyright
14 1.2 gehenna * notice, this list of conditions and the following disclaimer.
15 1.2 gehenna * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 gehenna * notice, this list of conditions and the following disclaimer in the
17 1.2 gehenna * documentation and/or other materials provided with the distribution.
18 1.2 gehenna *
19 1.2 gehenna * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 gehenna * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 gehenna * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 gehenna * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 gehenna * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 gehenna * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 gehenna * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 gehenna * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 gehenna * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 gehenna * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 gehenna * POSSIBILITY OF SUCH DAMAGE.
30 1.2 gehenna */
31 1.11 ad
32 1.11 ad /*
33 1.11 ad * Overview
34 1.11 ad *
35 1.11 ad * subr_devsw.c: registers device drivers by name and by major
36 1.11 ad * number, and provides wrapper methods for performing I/O and
37 1.11 ad * other tasks on device drivers, keying on the device number
38 1.11 ad * (dev_t).
39 1.11 ad *
40 1.11 ad * When the system is built, the config(8) command generates
41 1.11 ad * static tables of device drivers built into the kernel image
42 1.11 ad * along with their associated methods. These are recorded in
43 1.11 ad * the cdevsw0 and bdevsw0 tables. Drivers can also be added to
44 1.11 ad * and removed from the system dynamically.
45 1.11 ad *
46 1.11 ad * Allocation
47 1.11 ad *
48 1.11 ad * When the system initially boots only the statically allocated
49 1.11 ad * indexes (bdevsw0, cdevsw0) are used. If these overflow due to
50 1.11 ad * allocation, we allocate a fixed block of memory to hold the new,
51 1.11 ad * expanded index. This "fork" of the table is only ever performed
52 1.11 ad * once in order to guarantee that other threads may safely access
53 1.11 ad * the device tables:
54 1.11 ad *
55 1.11 ad * o Once a thread has a "reference" to the table via an earlier
56 1.11 ad * open() call, we know that the entry in the table must exist
57 1.11 ad * and so it is safe to access it.
58 1.11 ad *
59 1.11 ad * o Regardless of whether other threads see the old or new
60 1.11 ad * pointers, they will point to a correct device switch
61 1.11 ad * structure for the operation being performed.
62 1.11 ad *
63 1.11 ad * XXX Currently, the wrapper methods such as cdev_read() verify
64 1.11 ad * that a device driver does in fact exist before calling the
65 1.11 ad * associated driver method. This should be changed so that
66 1.11 ad * once the device is has been referenced by a vnode (opened),
67 1.11 ad * calling the other methods should be valid until that reference
68 1.11 ad * is dropped.
69 1.11 ad */
70 1.7 lukem
71 1.7 lukem #include <sys/cdefs.h>
72 1.29 mrg __KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.29 2011/12/12 19:03:12 mrg Exp $");
73 1.2 gehenna
74 1.2 gehenna #include <sys/param.h>
75 1.2 gehenna #include <sys/conf.h>
76 1.11 ad #include <sys/kmem.h>
77 1.2 gehenna #include <sys/systm.h>
78 1.11 ad #include <sys/poll.h>
79 1.11 ad #include <sys/tty.h>
80 1.15 matt #include <sys/cpu.h>
81 1.11 ad #include <sys/buf.h>
82 1.29 mrg #include <sys/reboot.h>
83 1.2 gehenna
84 1.2 gehenna #ifdef DEVSW_DEBUG
85 1.2 gehenna #define DPRINTF(x) printf x
86 1.2 gehenna #else /* DEVSW_DEBUG */
87 1.2 gehenna #define DPRINTF(x)
88 1.2 gehenna #endif /* DEVSW_DEBUG */
89 1.2 gehenna
90 1.11 ad #define MAXDEVSW 512 /* the maximum of major device number */
91 1.2 gehenna #define BDEVSW_SIZE (sizeof(struct bdevsw *))
92 1.2 gehenna #define CDEVSW_SIZE (sizeof(struct cdevsw *))
93 1.2 gehenna #define DEVSWCONV_SIZE (sizeof(struct devsw_conv))
94 1.2 gehenna
95 1.2 gehenna extern const struct bdevsw **bdevsw, *bdevsw0[];
96 1.2 gehenna extern const struct cdevsw **cdevsw, *cdevsw0[];
97 1.2 gehenna extern struct devsw_conv *devsw_conv, devsw_conv0[];
98 1.2 gehenna extern const int sys_bdevsws, sys_cdevsws;
99 1.2 gehenna extern int max_bdevsws, max_cdevsws, max_devsw_convs;
100 1.2 gehenna
101 1.24 drochner static int bdevsw_attach(const struct bdevsw *, devmajor_t *);
102 1.24 drochner static int cdevsw_attach(const struct cdevsw *, devmajor_t *);
103 1.11 ad static void devsw_detach_locked(const struct bdevsw *, const struct cdevsw *);
104 1.11 ad
105 1.23 pooka kmutex_t device_lock;
106 1.23 pooka
107 1.11 ad void
108 1.11 ad devsw_init(void)
109 1.11 ad {
110 1.11 ad
111 1.11 ad KASSERT(sys_bdevsws < MAXDEVSW - 1);
112 1.11 ad KASSERT(sys_cdevsws < MAXDEVSW - 1);
113 1.23 pooka mutex_init(&device_lock, MUTEX_DEFAULT, IPL_NONE);
114 1.11 ad }
115 1.2 gehenna
116 1.2 gehenna int
117 1.24 drochner devsw_attach(const char *devname,
118 1.24 drochner const struct bdevsw *bdev, devmajor_t *bmajor,
119 1.24 drochner const struct cdevsw *cdev, devmajor_t *cmajor)
120 1.2 gehenna {
121 1.2 gehenna struct devsw_conv *conv;
122 1.2 gehenna char *name;
123 1.2 gehenna int error, i;
124 1.25 enami size_t len;
125 1.2 gehenna
126 1.2 gehenna if (devname == NULL || cdev == NULL)
127 1.2 gehenna return (EINVAL);
128 1.2 gehenna
129 1.23 pooka mutex_enter(&device_lock);
130 1.11 ad
131 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
132 1.2 gehenna conv = &devsw_conv[i];
133 1.2 gehenna if (conv->d_name == NULL || strcmp(devname, conv->d_name) != 0)
134 1.2 gehenna continue;
135 1.2 gehenna
136 1.2 gehenna if (*bmajor < 0)
137 1.2 gehenna *bmajor = conv->d_bmajor;
138 1.2 gehenna if (*cmajor < 0)
139 1.2 gehenna *cmajor = conv->d_cmajor;
140 1.2 gehenna
141 1.11 ad if (*bmajor != conv->d_bmajor || *cmajor != conv->d_cmajor) {
142 1.11 ad error = EINVAL;
143 1.11 ad goto fail;
144 1.11 ad }
145 1.11 ad if ((*bmajor >= 0 && bdev == NULL) || *cmajor < 0) {
146 1.11 ad error = EINVAL;
147 1.11 ad goto fail;
148 1.11 ad }
149 1.2 gehenna
150 1.2 gehenna if ((*bmajor >= 0 && bdevsw[*bmajor] != NULL) ||
151 1.11 ad cdevsw[*cmajor] != NULL) {
152 1.11 ad error = EEXIST;
153 1.11 ad goto fail;
154 1.11 ad }
155 1.2 gehenna
156 1.2 gehenna if (bdev != NULL)
157 1.2 gehenna bdevsw[*bmajor] = bdev;
158 1.2 gehenna cdevsw[*cmajor] = cdev;
159 1.2 gehenna
160 1.23 pooka mutex_exit(&device_lock);
161 1.2 gehenna return (0);
162 1.2 gehenna }
163 1.2 gehenna
164 1.14 pooka error = bdevsw_attach(bdev, bmajor);
165 1.11 ad if (error != 0)
166 1.11 ad goto fail;
167 1.14 pooka error = cdevsw_attach(cdev, cmajor);
168 1.2 gehenna if (error != 0) {
169 1.11 ad devsw_detach_locked(bdev, NULL);
170 1.11 ad goto fail;
171 1.2 gehenna }
172 1.2 gehenna
173 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
174 1.2 gehenna if (devsw_conv[i].d_name == NULL)
175 1.2 gehenna break;
176 1.2 gehenna }
177 1.2 gehenna if (i == max_devsw_convs) {
178 1.2 gehenna struct devsw_conv *newptr;
179 1.2 gehenna int old, new;
180 1.2 gehenna
181 1.2 gehenna old = max_devsw_convs;
182 1.2 gehenna new = old + 1;
183 1.2 gehenna
184 1.11 ad newptr = kmem_zalloc(new * DEVSWCONV_SIZE, KM_NOSLEEP);
185 1.2 gehenna if (newptr == NULL) {
186 1.11 ad devsw_detach_locked(bdev, cdev);
187 1.11 ad error = ENOMEM;
188 1.11 ad goto fail;
189 1.2 gehenna }
190 1.2 gehenna newptr[old].d_name = NULL;
191 1.2 gehenna newptr[old].d_bmajor = -1;
192 1.2 gehenna newptr[old].d_cmajor = -1;
193 1.2 gehenna memcpy(newptr, devsw_conv, old * DEVSWCONV_SIZE);
194 1.2 gehenna if (devsw_conv != devsw_conv0)
195 1.11 ad kmem_free(devsw_conv, old * DEVSWCONV_SIZE);
196 1.2 gehenna devsw_conv = newptr;
197 1.2 gehenna max_devsw_convs = new;
198 1.2 gehenna }
199 1.2 gehenna
200 1.25 enami len = strlen(devname) + 1;
201 1.25 enami name = kmem_alloc(len, KM_NOSLEEP);
202 1.2 gehenna if (name == NULL) {
203 1.11 ad devsw_detach_locked(bdev, cdev);
204 1.25 enami error = ENOMEM;
205 1.11 ad goto fail;
206 1.2 gehenna }
207 1.25 enami strlcpy(name, devname, len);
208 1.2 gehenna
209 1.2 gehenna devsw_conv[i].d_name = name;
210 1.2 gehenna devsw_conv[i].d_bmajor = *bmajor;
211 1.2 gehenna devsw_conv[i].d_cmajor = *cmajor;
212 1.2 gehenna
213 1.23 pooka mutex_exit(&device_lock);
214 1.2 gehenna return (0);
215 1.11 ad fail:
216 1.23 pooka mutex_exit(&device_lock);
217 1.11 ad return (error);
218 1.2 gehenna }
219 1.2 gehenna
220 1.2 gehenna static int
221 1.24 drochner bdevsw_attach(const struct bdevsw *devsw, devmajor_t *devmajor)
222 1.2 gehenna {
223 1.11 ad const struct bdevsw **newptr;
224 1.24 drochner devmajor_t bmajor;
225 1.24 drochner int i;
226 1.2 gehenna
227 1.23 pooka KASSERT(mutex_owned(&device_lock));
228 1.11 ad
229 1.2 gehenna if (devsw == NULL)
230 1.2 gehenna return (0);
231 1.2 gehenna
232 1.2 gehenna if (*devmajor < 0) {
233 1.2 gehenna for (bmajor = sys_bdevsws ; bmajor < max_bdevsws ; bmajor++) {
234 1.2 gehenna if (bdevsw[bmajor] != NULL)
235 1.2 gehenna continue;
236 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
237 1.2 gehenna if (devsw_conv[i].d_bmajor == bmajor)
238 1.2 gehenna break;
239 1.2 gehenna }
240 1.2 gehenna if (i != max_devsw_convs)
241 1.2 gehenna continue;
242 1.2 gehenna break;
243 1.2 gehenna }
244 1.3 gehenna *devmajor = bmajor;
245 1.2 gehenna }
246 1.11 ad
247 1.2 gehenna if (*devmajor >= MAXDEVSW) {
248 1.11 ad printf("bdevsw_attach: block majors exhausted");
249 1.2 gehenna return (ENOMEM);
250 1.2 gehenna }
251 1.2 gehenna
252 1.2 gehenna if (*devmajor >= max_bdevsws) {
253 1.11 ad KASSERT(bdevsw == bdevsw0);
254 1.11 ad newptr = kmem_zalloc(MAXDEVSW * BDEVSW_SIZE, KM_NOSLEEP);
255 1.2 gehenna if (newptr == NULL)
256 1.2 gehenna return (ENOMEM);
257 1.11 ad memcpy(newptr, bdevsw, max_bdevsws * BDEVSW_SIZE);
258 1.2 gehenna bdevsw = newptr;
259 1.11 ad max_bdevsws = MAXDEVSW;
260 1.2 gehenna }
261 1.2 gehenna
262 1.2 gehenna if (bdevsw[*devmajor] != NULL)
263 1.2 gehenna return (EEXIST);
264 1.2 gehenna
265 1.2 gehenna bdevsw[*devmajor] = devsw;
266 1.2 gehenna
267 1.2 gehenna return (0);
268 1.2 gehenna }
269 1.2 gehenna
270 1.2 gehenna static int
271 1.24 drochner cdevsw_attach(const struct cdevsw *devsw, devmajor_t *devmajor)
272 1.2 gehenna {
273 1.11 ad const struct cdevsw **newptr;
274 1.24 drochner devmajor_t cmajor;
275 1.24 drochner int i;
276 1.2 gehenna
277 1.23 pooka KASSERT(mutex_owned(&device_lock));
278 1.11 ad
279 1.2 gehenna if (*devmajor < 0) {
280 1.2 gehenna for (cmajor = sys_cdevsws ; cmajor < max_cdevsws ; cmajor++) {
281 1.2 gehenna if (cdevsw[cmajor] != NULL)
282 1.2 gehenna continue;
283 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
284 1.2 gehenna if (devsw_conv[i].d_cmajor == cmajor)
285 1.2 gehenna break;
286 1.2 gehenna }
287 1.2 gehenna if (i != max_devsw_convs)
288 1.2 gehenna continue;
289 1.2 gehenna break;
290 1.2 gehenna }
291 1.3 gehenna *devmajor = cmajor;
292 1.2 gehenna }
293 1.11 ad
294 1.2 gehenna if (*devmajor >= MAXDEVSW) {
295 1.11 ad printf("cdevsw_attach: character majors exhausted");
296 1.2 gehenna return (ENOMEM);
297 1.2 gehenna }
298 1.2 gehenna
299 1.2 gehenna if (*devmajor >= max_cdevsws) {
300 1.11 ad KASSERT(cdevsw == cdevsw0);
301 1.11 ad newptr = kmem_zalloc(MAXDEVSW * CDEVSW_SIZE, KM_NOSLEEP);
302 1.2 gehenna if (newptr == NULL)
303 1.2 gehenna return (ENOMEM);
304 1.11 ad memcpy(newptr, cdevsw, max_cdevsws * CDEVSW_SIZE);
305 1.2 gehenna cdevsw = newptr;
306 1.11 ad max_cdevsws = MAXDEVSW;
307 1.2 gehenna }
308 1.2 gehenna
309 1.2 gehenna if (cdevsw[*devmajor] != NULL)
310 1.2 gehenna return (EEXIST);
311 1.2 gehenna
312 1.2 gehenna cdevsw[*devmajor] = devsw;
313 1.2 gehenna
314 1.2 gehenna return (0);
315 1.2 gehenna }
316 1.2 gehenna
317 1.11 ad static void
318 1.11 ad devsw_detach_locked(const struct bdevsw *bdev, const struct cdevsw *cdev)
319 1.2 gehenna {
320 1.2 gehenna int i;
321 1.2 gehenna
322 1.23 pooka KASSERT(mutex_owned(&device_lock));
323 1.11 ad
324 1.2 gehenna if (bdev != NULL) {
325 1.2 gehenna for (i = 0 ; i < max_bdevsws ; i++) {
326 1.2 gehenna if (bdevsw[i] != bdev)
327 1.2 gehenna continue;
328 1.2 gehenna bdevsw[i] = NULL;
329 1.2 gehenna break;
330 1.2 gehenna }
331 1.2 gehenna }
332 1.2 gehenna if (cdev != NULL) {
333 1.2 gehenna for (i = 0 ; i < max_cdevsws ; i++) {
334 1.2 gehenna if (cdevsw[i] != cdev)
335 1.2 gehenna continue;
336 1.2 gehenna cdevsw[i] = NULL;
337 1.2 gehenna break;
338 1.2 gehenna }
339 1.2 gehenna }
340 1.2 gehenna }
341 1.2 gehenna
342 1.19 ad int
343 1.11 ad devsw_detach(const struct bdevsw *bdev, const struct cdevsw *cdev)
344 1.11 ad {
345 1.11 ad
346 1.23 pooka mutex_enter(&device_lock);
347 1.11 ad devsw_detach_locked(bdev, cdev);
348 1.23 pooka mutex_exit(&device_lock);
349 1.19 ad return 0;
350 1.11 ad }
351 1.11 ad
352 1.11 ad /*
353 1.11 ad * Look up a block device by number.
354 1.11 ad *
355 1.11 ad * => Caller must ensure that the device is attached.
356 1.11 ad */
357 1.2 gehenna const struct bdevsw *
358 1.2 gehenna bdevsw_lookup(dev_t dev)
359 1.2 gehenna {
360 1.24 drochner devmajor_t bmajor;
361 1.2 gehenna
362 1.2 gehenna if (dev == NODEV)
363 1.2 gehenna return (NULL);
364 1.2 gehenna bmajor = major(dev);
365 1.2 gehenna if (bmajor < 0 || bmajor >= max_bdevsws)
366 1.2 gehenna return (NULL);
367 1.2 gehenna
368 1.2 gehenna return (bdevsw[bmajor]);
369 1.2 gehenna }
370 1.2 gehenna
371 1.11 ad /*
372 1.11 ad * Look up a character device by number.
373 1.11 ad *
374 1.11 ad * => Caller must ensure that the device is attached.
375 1.11 ad */
376 1.2 gehenna const struct cdevsw *
377 1.2 gehenna cdevsw_lookup(dev_t dev)
378 1.2 gehenna {
379 1.24 drochner devmajor_t cmajor;
380 1.2 gehenna
381 1.2 gehenna if (dev == NODEV)
382 1.2 gehenna return (NULL);
383 1.2 gehenna cmajor = major(dev);
384 1.2 gehenna if (cmajor < 0 || cmajor >= max_cdevsws)
385 1.2 gehenna return (NULL);
386 1.2 gehenna
387 1.2 gehenna return (cdevsw[cmajor]);
388 1.2 gehenna }
389 1.2 gehenna
390 1.11 ad /*
391 1.11 ad * Look up a block device by reference to its operations set.
392 1.11 ad *
393 1.11 ad * => Caller must ensure that the device is not detached, and therefore
394 1.11 ad * that the returned major is still valid when dereferenced.
395 1.11 ad */
396 1.24 drochner devmajor_t
397 1.2 gehenna bdevsw_lookup_major(const struct bdevsw *bdev)
398 1.2 gehenna {
399 1.24 drochner devmajor_t bmajor;
400 1.2 gehenna
401 1.2 gehenna for (bmajor = 0 ; bmajor < max_bdevsws ; bmajor++) {
402 1.2 gehenna if (bdevsw[bmajor] == bdev)
403 1.2 gehenna return (bmajor);
404 1.2 gehenna }
405 1.2 gehenna
406 1.24 drochner return (NODEVMAJOR);
407 1.2 gehenna }
408 1.2 gehenna
409 1.11 ad /*
410 1.11 ad * Look up a character device by reference to its operations set.
411 1.11 ad *
412 1.11 ad * => Caller must ensure that the device is not detached, and therefore
413 1.11 ad * that the returned major is still valid when dereferenced.
414 1.11 ad */
415 1.24 drochner devmajor_t
416 1.2 gehenna cdevsw_lookup_major(const struct cdevsw *cdev)
417 1.2 gehenna {
418 1.24 drochner devmajor_t cmajor;
419 1.2 gehenna
420 1.2 gehenna for (cmajor = 0 ; cmajor < max_cdevsws ; cmajor++) {
421 1.2 gehenna if (cdevsw[cmajor] == cdev)
422 1.2 gehenna return (cmajor);
423 1.2 gehenna }
424 1.2 gehenna
425 1.24 drochner return (NODEVMAJOR);
426 1.2 gehenna }
427 1.2 gehenna
428 1.2 gehenna /*
429 1.2 gehenna * Convert from block major number to name.
430 1.11 ad *
431 1.11 ad * => Caller must ensure that the device is not detached, and therefore
432 1.11 ad * that the name pointer is still valid when dereferenced.
433 1.2 gehenna */
434 1.2 gehenna const char *
435 1.24 drochner devsw_blk2name(devmajor_t bmajor)
436 1.2 gehenna {
437 1.11 ad const char *name;
438 1.24 drochner devmajor_t cmajor;
439 1.24 drochner int i;
440 1.2 gehenna
441 1.11 ad name = NULL;
442 1.11 ad cmajor = -1;
443 1.11 ad
444 1.23 pooka mutex_enter(&device_lock);
445 1.11 ad if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
446 1.23 pooka mutex_exit(&device_lock);
447 1.2 gehenna return (NULL);
448 1.2 gehenna }
449 1.11 ad for (i = 0 ; i < max_devsw_convs; i++) {
450 1.11 ad if (devsw_conv[i].d_bmajor == bmajor) {
451 1.11 ad cmajor = devsw_conv[i].d_cmajor;
452 1.11 ad break;
453 1.11 ad }
454 1.11 ad }
455 1.11 ad if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
456 1.11 ad name = devsw_conv[i].d_name;
457 1.23 pooka mutex_exit(&device_lock);
458 1.2 gehenna
459 1.11 ad return (name);
460 1.2 gehenna }
461 1.2 gehenna
462 1.2 gehenna /*
463 1.26 haad * Convert char major number to device driver name.
464 1.26 haad */
465 1.27 yamt const char *
466 1.26 haad cdevsw_getname(devmajor_t major)
467 1.26 haad {
468 1.26 haad const char *name;
469 1.26 haad int i;
470 1.26 haad
471 1.26 haad name = NULL;
472 1.26 haad
473 1.26 haad if (major < 0)
474 1.26 haad return (NULL);
475 1.26 haad
476 1.26 haad mutex_enter(&device_lock);
477 1.26 haad for (i = 0 ; i < max_devsw_convs; i++) {
478 1.26 haad if (devsw_conv[i].d_cmajor == major) {
479 1.26 haad name = devsw_conv[i].d_name;
480 1.26 haad break;
481 1.26 haad }
482 1.26 haad }
483 1.26 haad mutex_exit(&device_lock);
484 1.26 haad return (name);
485 1.26 haad }
486 1.26 haad
487 1.26 haad /*
488 1.26 haad * Convert block major number to device driver name.
489 1.26 haad */
490 1.27 yamt const char *
491 1.26 haad bdevsw_getname(devmajor_t major)
492 1.26 haad {
493 1.26 haad const char *name;
494 1.26 haad int i;
495 1.26 haad
496 1.26 haad name = NULL;
497 1.26 haad
498 1.26 haad if (major < 0)
499 1.26 haad return (NULL);
500 1.26 haad
501 1.26 haad mutex_enter(&device_lock);
502 1.26 haad for (i = 0 ; i < max_devsw_convs; i++) {
503 1.26 haad if (devsw_conv[i].d_bmajor == major) {
504 1.26 haad name = devsw_conv[i].d_name;
505 1.26 haad break;
506 1.26 haad }
507 1.26 haad }
508 1.26 haad mutex_exit(&device_lock);
509 1.26 haad return (name);
510 1.26 haad }
511 1.26 haad
512 1.26 haad /*
513 1.2 gehenna * Convert from device name to block major number.
514 1.11 ad *
515 1.11 ad * => Caller must ensure that the device is not detached, and therefore
516 1.11 ad * that the major number is still valid when dereferenced.
517 1.2 gehenna */
518 1.24 drochner devmajor_t
519 1.2 gehenna devsw_name2blk(const char *name, char *devname, size_t devnamelen)
520 1.2 gehenna {
521 1.2 gehenna struct devsw_conv *conv;
522 1.24 drochner devmajor_t bmajor;
523 1.24 drochner int i;
524 1.2 gehenna
525 1.2 gehenna if (name == NULL)
526 1.24 drochner return (NODEVMAJOR);
527 1.2 gehenna
528 1.23 pooka mutex_enter(&device_lock);
529 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
530 1.5 mrg size_t len;
531 1.5 mrg
532 1.2 gehenna conv = &devsw_conv[i];
533 1.2 gehenna if (conv->d_name == NULL)
534 1.2 gehenna continue;
535 1.5 mrg len = strlen(conv->d_name);
536 1.5 mrg if (strncmp(conv->d_name, name, len) != 0)
537 1.5 mrg continue;
538 1.5 mrg if (*(name +len) && !isdigit(*(name + len)))
539 1.2 gehenna continue;
540 1.2 gehenna bmajor = conv->d_bmajor;
541 1.2 gehenna if (bmajor < 0 || bmajor >= max_bdevsws ||
542 1.2 gehenna bdevsw[bmajor] == NULL)
543 1.5 mrg break;
544 1.2 gehenna if (devname != NULL) {
545 1.2 gehenna #ifdef DEVSW_DEBUG
546 1.2 gehenna if (strlen(conv->d_name) >= devnamelen)
547 1.2 gehenna printf("devsw_name2blk: too short buffer");
548 1.2 gehenna #endif /* DEVSW_DEBUG */
549 1.4 tsutsui strncpy(devname, conv->d_name, devnamelen);
550 1.2 gehenna devname[devnamelen - 1] = '\0';
551 1.2 gehenna }
552 1.23 pooka mutex_exit(&device_lock);
553 1.2 gehenna return (bmajor);
554 1.2 gehenna }
555 1.2 gehenna
556 1.23 pooka mutex_exit(&device_lock);
557 1.24 drochner return (NODEVMAJOR);
558 1.2 gehenna }
559 1.2 gehenna
560 1.2 gehenna /*
561 1.16 plunky * Convert from device name to char major number.
562 1.16 plunky *
563 1.16 plunky * => Caller must ensure that the device is not detached, and therefore
564 1.16 plunky * that the major number is still valid when dereferenced.
565 1.16 plunky */
566 1.24 drochner devmajor_t
567 1.16 plunky devsw_name2chr(const char *name, char *devname, size_t devnamelen)
568 1.16 plunky {
569 1.16 plunky struct devsw_conv *conv;
570 1.24 drochner devmajor_t cmajor;
571 1.24 drochner int i;
572 1.16 plunky
573 1.16 plunky if (name == NULL)
574 1.24 drochner return (NODEVMAJOR);
575 1.16 plunky
576 1.23 pooka mutex_enter(&device_lock);
577 1.16 plunky for (i = 0 ; i < max_devsw_convs ; i++) {
578 1.16 plunky size_t len;
579 1.16 plunky
580 1.16 plunky conv = &devsw_conv[i];
581 1.16 plunky if (conv->d_name == NULL)
582 1.16 plunky continue;
583 1.16 plunky len = strlen(conv->d_name);
584 1.16 plunky if (strncmp(conv->d_name, name, len) != 0)
585 1.16 plunky continue;
586 1.16 plunky if (*(name +len) && !isdigit(*(name + len)))
587 1.16 plunky continue;
588 1.16 plunky cmajor = conv->d_cmajor;
589 1.16 plunky if (cmajor < 0 || cmajor >= max_cdevsws ||
590 1.16 plunky cdevsw[cmajor] == NULL)
591 1.16 plunky break;
592 1.16 plunky if (devname != NULL) {
593 1.16 plunky #ifdef DEVSW_DEBUG
594 1.16 plunky if (strlen(conv->d_name) >= devnamelen)
595 1.16 plunky printf("devsw_name2chr: too short buffer");
596 1.16 plunky #endif /* DEVSW_DEBUG */
597 1.16 plunky strncpy(devname, conv->d_name, devnamelen);
598 1.16 plunky devname[devnamelen - 1] = '\0';
599 1.16 plunky }
600 1.23 pooka mutex_exit(&device_lock);
601 1.16 plunky return (cmajor);
602 1.16 plunky }
603 1.16 plunky
604 1.23 pooka mutex_exit(&device_lock);
605 1.24 drochner return (NODEVMAJOR);
606 1.16 plunky }
607 1.16 plunky
608 1.16 plunky /*
609 1.2 gehenna * Convert from character dev_t to block dev_t.
610 1.11 ad *
611 1.11 ad * => Caller must ensure that the device is not detached, and therefore
612 1.11 ad * that the major number is still valid when dereferenced.
613 1.2 gehenna */
614 1.2 gehenna dev_t
615 1.2 gehenna devsw_chr2blk(dev_t cdev)
616 1.2 gehenna {
617 1.24 drochner devmajor_t bmajor, cmajor;
618 1.24 drochner int i;
619 1.11 ad dev_t rv;
620 1.2 gehenna
621 1.2 gehenna cmajor = major(cdev);
622 1.24 drochner bmajor = NODEVMAJOR;
623 1.11 ad rv = NODEV;
624 1.2 gehenna
625 1.23 pooka mutex_enter(&device_lock);
626 1.11 ad if (cmajor < 0 || cmajor >= max_cdevsws || cdevsw[cmajor] == NULL) {
627 1.23 pooka mutex_exit(&device_lock);
628 1.11 ad return (NODEV);
629 1.11 ad }
630 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
631 1.11 ad if (devsw_conv[i].d_cmajor == cmajor) {
632 1.11 ad bmajor = devsw_conv[i].d_bmajor;
633 1.11 ad break;
634 1.11 ad }
635 1.2 gehenna }
636 1.11 ad if (bmajor >= 0 && bmajor < max_bdevsws && bdevsw[bmajor] != NULL)
637 1.11 ad rv = makedev(bmajor, minor(cdev));
638 1.23 pooka mutex_exit(&device_lock);
639 1.2 gehenna
640 1.11 ad return (rv);
641 1.2 gehenna }
642 1.2 gehenna
643 1.2 gehenna /*
644 1.2 gehenna * Convert from block dev_t to character dev_t.
645 1.11 ad *
646 1.11 ad * => Caller must ensure that the device is not detached, and therefore
647 1.11 ad * that the major number is still valid when dereferenced.
648 1.2 gehenna */
649 1.2 gehenna dev_t
650 1.2 gehenna devsw_blk2chr(dev_t bdev)
651 1.2 gehenna {
652 1.24 drochner devmajor_t bmajor, cmajor;
653 1.24 drochner int i;
654 1.11 ad dev_t rv;
655 1.2 gehenna
656 1.11 ad bmajor = major(bdev);
657 1.24 drochner cmajor = NODEVMAJOR;
658 1.11 ad rv = NODEV;
659 1.11 ad
660 1.23 pooka mutex_enter(&device_lock);
661 1.11 ad if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
662 1.23 pooka mutex_exit(&device_lock);
663 1.2 gehenna return (NODEV);
664 1.11 ad }
665 1.11 ad for (i = 0 ; i < max_devsw_convs ; i++) {
666 1.11 ad if (devsw_conv[i].d_bmajor == bmajor) {
667 1.11 ad cmajor = devsw_conv[i].d_cmajor;
668 1.11 ad break;
669 1.11 ad }
670 1.11 ad }
671 1.11 ad if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
672 1.11 ad rv = makedev(cmajor, minor(bdev));
673 1.23 pooka mutex_exit(&device_lock);
674 1.2 gehenna
675 1.11 ad return (rv);
676 1.11 ad }
677 1.11 ad
678 1.11 ad /*
679 1.11 ad * Device access methods.
680 1.11 ad */
681 1.11 ad
682 1.11 ad #define DEV_LOCK(d) \
683 1.17 ad if ((mpflag = (d->d_flag & D_MPSAFE)) == 0) { \
684 1.17 ad KERNEL_LOCK(1, NULL); \
685 1.11 ad }
686 1.2 gehenna
687 1.11 ad #define DEV_UNLOCK(d) \
688 1.17 ad if (mpflag == 0) { \
689 1.17 ad KERNEL_UNLOCK_ONE(NULL); \
690 1.2 gehenna }
691 1.2 gehenna
692 1.11 ad int
693 1.11 ad bdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
694 1.11 ad {
695 1.11 ad const struct bdevsw *d;
696 1.17 ad int rv, mpflag;
697 1.11 ad
698 1.11 ad /*
699 1.11 ad * For open we need to lock, in order to synchronize
700 1.11 ad * with attach/detach.
701 1.11 ad */
702 1.23 pooka mutex_enter(&device_lock);
703 1.11 ad d = bdevsw_lookup(dev);
704 1.23 pooka mutex_exit(&device_lock);
705 1.11 ad if (d == NULL)
706 1.11 ad return ENXIO;
707 1.11 ad
708 1.11 ad DEV_LOCK(d);
709 1.11 ad rv = (*d->d_open)(dev, flag, devtype, l);
710 1.11 ad DEV_UNLOCK(d);
711 1.11 ad
712 1.11 ad return rv;
713 1.11 ad }
714 1.11 ad
715 1.11 ad int
716 1.11 ad bdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
717 1.11 ad {
718 1.11 ad const struct bdevsw *d;
719 1.17 ad int rv, mpflag;
720 1.11 ad
721 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
722 1.11 ad return ENXIO;
723 1.11 ad
724 1.11 ad DEV_LOCK(d);
725 1.11 ad rv = (*d->d_close)(dev, flag, devtype, l);
726 1.11 ad DEV_UNLOCK(d);
727 1.11 ad
728 1.11 ad return rv;
729 1.11 ad }
730 1.11 ad
731 1.11 ad void
732 1.11 ad bdev_strategy(struct buf *bp)
733 1.11 ad {
734 1.11 ad const struct bdevsw *d;
735 1.17 ad int mpflag;
736 1.11 ad
737 1.28 jmcneill if ((d = bdevsw_lookup(bp->b_dev)) == NULL) {
738 1.28 jmcneill bp->b_error = ENXIO;
739 1.28 jmcneill bp->b_resid = bp->b_bcount;
740 1.28 jmcneill biodone(bp);
741 1.28 jmcneill return;
742 1.28 jmcneill }
743 1.11 ad
744 1.11 ad DEV_LOCK(d);
745 1.11 ad (*d->d_strategy)(bp);
746 1.11 ad DEV_UNLOCK(d);
747 1.11 ad }
748 1.11 ad
749 1.11 ad int
750 1.11 ad bdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
751 1.11 ad {
752 1.11 ad const struct bdevsw *d;
753 1.17 ad int rv, mpflag;
754 1.11 ad
755 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
756 1.11 ad return ENXIO;
757 1.11 ad
758 1.11 ad DEV_LOCK(d);
759 1.11 ad rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
760 1.11 ad DEV_UNLOCK(d);
761 1.11 ad
762 1.11 ad return rv;
763 1.11 ad }
764 1.11 ad
765 1.11 ad int
766 1.11 ad bdev_dump(dev_t dev, daddr_t addr, void *data, size_t sz)
767 1.11 ad {
768 1.11 ad const struct bdevsw *d;
769 1.11 ad int rv;
770 1.11 ad
771 1.11 ad /*
772 1.11 ad * Dump can be called without the device open. Since it can
773 1.11 ad * currently only be called with the system paused (and in a
774 1.11 ad * potentially unstable state), we don't perform any locking.
775 1.11 ad */
776 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
777 1.11 ad return ENXIO;
778 1.11 ad
779 1.11 ad /* DEV_LOCK(d); */
780 1.11 ad rv = (*d->d_dump)(dev, addr, data, sz);
781 1.11 ad /* DEV_UNLOCK(d); */
782 1.11 ad
783 1.11 ad return rv;
784 1.11 ad }
785 1.11 ad
786 1.11 ad int
787 1.11 ad bdev_type(dev_t dev)
788 1.11 ad {
789 1.11 ad const struct bdevsw *d;
790 1.11 ad
791 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
792 1.11 ad return D_OTHER;
793 1.11 ad return d->d_flag & D_TYPEMASK;
794 1.11 ad }
795 1.11 ad
796 1.11 ad int
797 1.29 mrg bdev_size(dev_t dev)
798 1.29 mrg {
799 1.29 mrg const struct bdevsw *d;
800 1.29 mrg int rv, mpflag = 0;
801 1.29 mrg
802 1.29 mrg if ((d = bdevsw_lookup(dev)) == NULL ||
803 1.29 mrg d->d_psize == NULL)
804 1.29 mrg return -1;
805 1.29 mrg
806 1.29 mrg /*
807 1.29 mrg * Don't to try lock the device if we're dumping.
808 1.29 mrg */
809 1.29 mrg if ((boothowto & RB_DUMP) == 0)
810 1.29 mrg DEV_LOCK(d);
811 1.29 mrg rv = (*d->d_psize)(dev);
812 1.29 mrg if ((boothowto & RB_DUMP) == 0)
813 1.29 mrg DEV_UNLOCK(d);
814 1.29 mrg
815 1.29 mrg return rv;
816 1.29 mrg }
817 1.29 mrg
818 1.29 mrg int
819 1.11 ad cdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
820 1.11 ad {
821 1.11 ad const struct cdevsw *d;
822 1.17 ad int rv, mpflag;
823 1.11 ad
824 1.11 ad /*
825 1.11 ad * For open we need to lock, in order to synchronize
826 1.11 ad * with attach/detach.
827 1.11 ad */
828 1.23 pooka mutex_enter(&device_lock);
829 1.11 ad d = cdevsw_lookup(dev);
830 1.23 pooka mutex_exit(&device_lock);
831 1.11 ad if (d == NULL)
832 1.11 ad return ENXIO;
833 1.11 ad
834 1.11 ad DEV_LOCK(d);
835 1.11 ad rv = (*d->d_open)(dev, flag, devtype, l);
836 1.11 ad DEV_UNLOCK(d);
837 1.11 ad
838 1.11 ad return rv;
839 1.11 ad }
840 1.11 ad
841 1.11 ad int
842 1.11 ad cdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
843 1.11 ad {
844 1.11 ad const struct cdevsw *d;
845 1.17 ad int rv, mpflag;
846 1.11 ad
847 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
848 1.11 ad return ENXIO;
849 1.11 ad
850 1.11 ad DEV_LOCK(d);
851 1.11 ad rv = (*d->d_close)(dev, flag, devtype, l);
852 1.11 ad DEV_UNLOCK(d);
853 1.11 ad
854 1.11 ad return rv;
855 1.11 ad }
856 1.11 ad
857 1.11 ad int
858 1.11 ad cdev_read(dev_t dev, struct uio *uio, int flag)
859 1.11 ad {
860 1.11 ad const struct cdevsw *d;
861 1.17 ad int rv, mpflag;
862 1.11 ad
863 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
864 1.11 ad return ENXIO;
865 1.11 ad
866 1.11 ad DEV_LOCK(d);
867 1.11 ad rv = (*d->d_read)(dev, uio, flag);
868 1.11 ad DEV_UNLOCK(d);
869 1.11 ad
870 1.11 ad return rv;
871 1.11 ad }
872 1.11 ad
873 1.11 ad int
874 1.11 ad cdev_write(dev_t dev, struct uio *uio, int flag)
875 1.11 ad {
876 1.11 ad const struct cdevsw *d;
877 1.17 ad int rv, mpflag;
878 1.11 ad
879 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
880 1.11 ad return ENXIO;
881 1.11 ad
882 1.11 ad DEV_LOCK(d);
883 1.11 ad rv = (*d->d_write)(dev, uio, flag);
884 1.11 ad DEV_UNLOCK(d);
885 1.11 ad
886 1.11 ad return rv;
887 1.11 ad }
888 1.11 ad
889 1.11 ad int
890 1.11 ad cdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
891 1.11 ad {
892 1.11 ad const struct cdevsw *d;
893 1.17 ad int rv, mpflag;
894 1.11 ad
895 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
896 1.11 ad return ENXIO;
897 1.11 ad
898 1.11 ad DEV_LOCK(d);
899 1.11 ad rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
900 1.11 ad DEV_UNLOCK(d);
901 1.11 ad
902 1.11 ad return rv;
903 1.11 ad }
904 1.11 ad
905 1.11 ad void
906 1.11 ad cdev_stop(struct tty *tp, int flag)
907 1.11 ad {
908 1.11 ad const struct cdevsw *d;
909 1.17 ad int mpflag;
910 1.11 ad
911 1.11 ad if ((d = cdevsw_lookup(tp->t_dev)) == NULL)
912 1.11 ad return;
913 1.11 ad
914 1.11 ad DEV_LOCK(d);
915 1.11 ad (*d->d_stop)(tp, flag);
916 1.11 ad DEV_UNLOCK(d);
917 1.11 ad }
918 1.11 ad
919 1.11 ad struct tty *
920 1.11 ad cdev_tty(dev_t dev)
921 1.11 ad {
922 1.11 ad const struct cdevsw *d;
923 1.11 ad
924 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
925 1.11 ad return NULL;
926 1.11 ad
927 1.12 ad /* XXX Check if necessary. */
928 1.12 ad if (d->d_tty == NULL)
929 1.12 ad return NULL;
930 1.12 ad
931 1.21 ad return (*d->d_tty)(dev);
932 1.11 ad }
933 1.11 ad
934 1.11 ad int
935 1.11 ad cdev_poll(dev_t dev, int flag, lwp_t *l)
936 1.11 ad {
937 1.11 ad const struct cdevsw *d;
938 1.17 ad int rv, mpflag;
939 1.11 ad
940 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
941 1.11 ad return POLLERR;
942 1.11 ad
943 1.11 ad DEV_LOCK(d);
944 1.11 ad rv = (*d->d_poll)(dev, flag, l);
945 1.11 ad DEV_UNLOCK(d);
946 1.11 ad
947 1.11 ad return rv;
948 1.11 ad }
949 1.11 ad
950 1.11 ad paddr_t
951 1.11 ad cdev_mmap(dev_t dev, off_t off, int flag)
952 1.11 ad {
953 1.11 ad const struct cdevsw *d;
954 1.11 ad paddr_t rv;
955 1.17 ad int mpflag;
956 1.11 ad
957 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
958 1.11 ad return (paddr_t)-1LL;
959 1.11 ad
960 1.11 ad DEV_LOCK(d);
961 1.11 ad rv = (*d->d_mmap)(dev, off, flag);
962 1.11 ad DEV_UNLOCK(d);
963 1.11 ad
964 1.11 ad return rv;
965 1.11 ad }
966 1.11 ad
967 1.11 ad int
968 1.11 ad cdev_kqfilter(dev_t dev, struct knote *kn)
969 1.11 ad {
970 1.11 ad const struct cdevsw *d;
971 1.17 ad int rv, mpflag;
972 1.11 ad
973 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
974 1.11 ad return ENXIO;
975 1.11 ad
976 1.11 ad DEV_LOCK(d);
977 1.11 ad rv = (*d->d_kqfilter)(dev, kn);
978 1.11 ad DEV_UNLOCK(d);
979 1.11 ad
980 1.11 ad return rv;
981 1.11 ad }
982 1.11 ad
983 1.11 ad int
984 1.11 ad cdev_type(dev_t dev)
985 1.11 ad {
986 1.11 ad const struct cdevsw *d;
987 1.11 ad
988 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
989 1.11 ad return D_OTHER;
990 1.11 ad return d->d_flag & D_TYPEMASK;
991 1.2 gehenna }
992