subr_devsw.c revision 1.39 1 1.39 riastrad /* $NetBSD: subr_devsw.c,v 1.39 2022/03/28 12:33:22 riastradh 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.39 riastrad __KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.39 2022/03/28 12:33:22 riastradh Exp $");
73 1.34 riz
74 1.34 riz #ifdef _KERNEL_OPT
75 1.34 riz #include "opt_dtrace.h"
76 1.34 riz #endif
77 1.2 gehenna
78 1.2 gehenna #include <sys/param.h>
79 1.2 gehenna #include <sys/conf.h>
80 1.11 ad #include <sys/kmem.h>
81 1.2 gehenna #include <sys/systm.h>
82 1.11 ad #include <sys/poll.h>
83 1.11 ad #include <sys/tty.h>
84 1.15 matt #include <sys/cpu.h>
85 1.11 ad #include <sys/buf.h>
86 1.29 mrg #include <sys/reboot.h>
87 1.34 riz #include <sys/sdt.h>
88 1.2 gehenna
89 1.2 gehenna #ifdef DEVSW_DEBUG
90 1.2 gehenna #define DPRINTF(x) printf x
91 1.2 gehenna #else /* DEVSW_DEBUG */
92 1.2 gehenna #define DPRINTF(x)
93 1.2 gehenna #endif /* DEVSW_DEBUG */
94 1.2 gehenna
95 1.11 ad #define MAXDEVSW 512 /* the maximum of major device number */
96 1.2 gehenna #define BDEVSW_SIZE (sizeof(struct bdevsw *))
97 1.2 gehenna #define CDEVSW_SIZE (sizeof(struct cdevsw *))
98 1.2 gehenna #define DEVSWCONV_SIZE (sizeof(struct devsw_conv))
99 1.2 gehenna
100 1.2 gehenna extern const struct bdevsw **bdevsw, *bdevsw0[];
101 1.2 gehenna extern const struct cdevsw **cdevsw, *cdevsw0[];
102 1.2 gehenna extern struct devsw_conv *devsw_conv, devsw_conv0[];
103 1.2 gehenna extern const int sys_bdevsws, sys_cdevsws;
104 1.2 gehenna extern int max_bdevsws, max_cdevsws, max_devsw_convs;
105 1.2 gehenna
106 1.24 drochner static int bdevsw_attach(const struct bdevsw *, devmajor_t *);
107 1.24 drochner static int cdevsw_attach(const struct cdevsw *, devmajor_t *);
108 1.11 ad static void devsw_detach_locked(const struct bdevsw *, const struct cdevsw *);
109 1.11 ad
110 1.23 pooka kmutex_t device_lock;
111 1.23 pooka
112 1.31 pooka void (*biodone_vfs)(buf_t *) = (void *)nullop;
113 1.31 pooka
114 1.11 ad void
115 1.11 ad devsw_init(void)
116 1.11 ad {
117 1.11 ad
118 1.11 ad KASSERT(sys_bdevsws < MAXDEVSW - 1);
119 1.11 ad KASSERT(sys_cdevsws < MAXDEVSW - 1);
120 1.23 pooka mutex_init(&device_lock, MUTEX_DEFAULT, IPL_NONE);
121 1.11 ad }
122 1.2 gehenna
123 1.2 gehenna int
124 1.24 drochner devsw_attach(const char *devname,
125 1.24 drochner const struct bdevsw *bdev, devmajor_t *bmajor,
126 1.24 drochner const struct cdevsw *cdev, devmajor_t *cmajor)
127 1.2 gehenna {
128 1.2 gehenna struct devsw_conv *conv;
129 1.2 gehenna char *name;
130 1.2 gehenna int error, i;
131 1.2 gehenna
132 1.2 gehenna if (devname == NULL || cdev == NULL)
133 1.2 gehenna return (EINVAL);
134 1.2 gehenna
135 1.23 pooka mutex_enter(&device_lock);
136 1.11 ad
137 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
138 1.2 gehenna conv = &devsw_conv[i];
139 1.2 gehenna if (conv->d_name == NULL || strcmp(devname, conv->d_name) != 0)
140 1.2 gehenna continue;
141 1.2 gehenna
142 1.2 gehenna if (*bmajor < 0)
143 1.2 gehenna *bmajor = conv->d_bmajor;
144 1.2 gehenna if (*cmajor < 0)
145 1.2 gehenna *cmajor = conv->d_cmajor;
146 1.2 gehenna
147 1.11 ad if (*bmajor != conv->d_bmajor || *cmajor != conv->d_cmajor) {
148 1.11 ad error = EINVAL;
149 1.11 ad goto fail;
150 1.11 ad }
151 1.11 ad if ((*bmajor >= 0 && bdev == NULL) || *cmajor < 0) {
152 1.11 ad error = EINVAL;
153 1.11 ad goto fail;
154 1.11 ad }
155 1.2 gehenna
156 1.2 gehenna if ((*bmajor >= 0 && bdevsw[*bmajor] != NULL) ||
157 1.11 ad cdevsw[*cmajor] != NULL) {
158 1.11 ad error = EEXIST;
159 1.11 ad goto fail;
160 1.11 ad }
161 1.2 gehenna
162 1.2 gehenna if (bdev != NULL)
163 1.2 gehenna bdevsw[*bmajor] = bdev;
164 1.2 gehenna cdevsw[*cmajor] = cdev;
165 1.2 gehenna
166 1.23 pooka mutex_exit(&device_lock);
167 1.2 gehenna return (0);
168 1.2 gehenna }
169 1.2 gehenna
170 1.14 pooka error = bdevsw_attach(bdev, bmajor);
171 1.11 ad if (error != 0)
172 1.11 ad goto fail;
173 1.14 pooka error = cdevsw_attach(cdev, cmajor);
174 1.2 gehenna if (error != 0) {
175 1.11 ad devsw_detach_locked(bdev, NULL);
176 1.11 ad goto fail;
177 1.2 gehenna }
178 1.2 gehenna
179 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
180 1.2 gehenna if (devsw_conv[i].d_name == NULL)
181 1.2 gehenna break;
182 1.2 gehenna }
183 1.2 gehenna if (i == max_devsw_convs) {
184 1.2 gehenna struct devsw_conv *newptr;
185 1.33 matt int old_convs, new_convs;
186 1.2 gehenna
187 1.33 matt old_convs = max_devsw_convs;
188 1.33 matt new_convs = old_convs + 1;
189 1.2 gehenna
190 1.33 matt newptr = kmem_zalloc(new_convs * DEVSWCONV_SIZE, KM_NOSLEEP);
191 1.2 gehenna if (newptr == NULL) {
192 1.11 ad devsw_detach_locked(bdev, cdev);
193 1.11 ad error = ENOMEM;
194 1.11 ad goto fail;
195 1.2 gehenna }
196 1.33 matt newptr[old_convs].d_name = NULL;
197 1.33 matt newptr[old_convs].d_bmajor = -1;
198 1.33 matt newptr[old_convs].d_cmajor = -1;
199 1.33 matt memcpy(newptr, devsw_conv, old_convs * DEVSWCONV_SIZE);
200 1.2 gehenna if (devsw_conv != devsw_conv0)
201 1.33 matt kmem_free(devsw_conv, old_convs * DEVSWCONV_SIZE);
202 1.2 gehenna devsw_conv = newptr;
203 1.33 matt max_devsw_convs = new_convs;
204 1.2 gehenna }
205 1.2 gehenna
206 1.38 christos name = kmem_strdupsize(devname, NULL, KM_NOSLEEP);
207 1.2 gehenna if (name == NULL) {
208 1.11 ad devsw_detach_locked(bdev, cdev);
209 1.25 enami error = ENOMEM;
210 1.11 ad goto fail;
211 1.2 gehenna }
212 1.2 gehenna
213 1.2 gehenna devsw_conv[i].d_name = name;
214 1.2 gehenna devsw_conv[i].d_bmajor = *bmajor;
215 1.2 gehenna devsw_conv[i].d_cmajor = *cmajor;
216 1.2 gehenna
217 1.23 pooka mutex_exit(&device_lock);
218 1.2 gehenna return (0);
219 1.11 ad fail:
220 1.23 pooka mutex_exit(&device_lock);
221 1.11 ad return (error);
222 1.2 gehenna }
223 1.2 gehenna
224 1.2 gehenna static int
225 1.24 drochner bdevsw_attach(const struct bdevsw *devsw, devmajor_t *devmajor)
226 1.2 gehenna {
227 1.11 ad const struct bdevsw **newptr;
228 1.24 drochner devmajor_t bmajor;
229 1.24 drochner int i;
230 1.2 gehenna
231 1.23 pooka KASSERT(mutex_owned(&device_lock));
232 1.11 ad
233 1.2 gehenna if (devsw == NULL)
234 1.2 gehenna return (0);
235 1.2 gehenna
236 1.2 gehenna if (*devmajor < 0) {
237 1.2 gehenna for (bmajor = sys_bdevsws ; bmajor < max_bdevsws ; bmajor++) {
238 1.2 gehenna if (bdevsw[bmajor] != NULL)
239 1.2 gehenna continue;
240 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
241 1.2 gehenna if (devsw_conv[i].d_bmajor == bmajor)
242 1.2 gehenna break;
243 1.2 gehenna }
244 1.2 gehenna if (i != max_devsw_convs)
245 1.2 gehenna continue;
246 1.2 gehenna break;
247 1.2 gehenna }
248 1.3 gehenna *devmajor = bmajor;
249 1.2 gehenna }
250 1.11 ad
251 1.2 gehenna if (*devmajor >= MAXDEVSW) {
252 1.37 pgoyette printf("%s: block majors exhausted", __func__);
253 1.2 gehenna return (ENOMEM);
254 1.2 gehenna }
255 1.2 gehenna
256 1.2 gehenna if (*devmajor >= max_bdevsws) {
257 1.11 ad KASSERT(bdevsw == bdevsw0);
258 1.11 ad newptr = kmem_zalloc(MAXDEVSW * BDEVSW_SIZE, KM_NOSLEEP);
259 1.2 gehenna if (newptr == NULL)
260 1.2 gehenna return (ENOMEM);
261 1.11 ad memcpy(newptr, bdevsw, max_bdevsws * BDEVSW_SIZE);
262 1.2 gehenna bdevsw = newptr;
263 1.11 ad max_bdevsws = MAXDEVSW;
264 1.2 gehenna }
265 1.2 gehenna
266 1.2 gehenna if (bdevsw[*devmajor] != NULL)
267 1.2 gehenna return (EEXIST);
268 1.2 gehenna
269 1.2 gehenna bdevsw[*devmajor] = devsw;
270 1.2 gehenna
271 1.2 gehenna return (0);
272 1.2 gehenna }
273 1.2 gehenna
274 1.2 gehenna static int
275 1.24 drochner cdevsw_attach(const struct cdevsw *devsw, devmajor_t *devmajor)
276 1.2 gehenna {
277 1.11 ad const struct cdevsw **newptr;
278 1.24 drochner devmajor_t cmajor;
279 1.24 drochner int i;
280 1.2 gehenna
281 1.23 pooka KASSERT(mutex_owned(&device_lock));
282 1.11 ad
283 1.2 gehenna if (*devmajor < 0) {
284 1.2 gehenna for (cmajor = sys_cdevsws ; cmajor < max_cdevsws ; cmajor++) {
285 1.2 gehenna if (cdevsw[cmajor] != NULL)
286 1.2 gehenna continue;
287 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
288 1.2 gehenna if (devsw_conv[i].d_cmajor == cmajor)
289 1.2 gehenna break;
290 1.2 gehenna }
291 1.2 gehenna if (i != max_devsw_convs)
292 1.2 gehenna continue;
293 1.2 gehenna break;
294 1.2 gehenna }
295 1.3 gehenna *devmajor = cmajor;
296 1.2 gehenna }
297 1.11 ad
298 1.2 gehenna if (*devmajor >= MAXDEVSW) {
299 1.37 pgoyette printf("%s: character majors exhausted", __func__);
300 1.2 gehenna return (ENOMEM);
301 1.2 gehenna }
302 1.2 gehenna
303 1.2 gehenna if (*devmajor >= max_cdevsws) {
304 1.11 ad KASSERT(cdevsw == cdevsw0);
305 1.11 ad newptr = kmem_zalloc(MAXDEVSW * CDEVSW_SIZE, KM_NOSLEEP);
306 1.2 gehenna if (newptr == NULL)
307 1.2 gehenna return (ENOMEM);
308 1.11 ad memcpy(newptr, cdevsw, max_cdevsws * CDEVSW_SIZE);
309 1.2 gehenna cdevsw = newptr;
310 1.11 ad max_cdevsws = MAXDEVSW;
311 1.2 gehenna }
312 1.2 gehenna
313 1.2 gehenna if (cdevsw[*devmajor] != NULL)
314 1.2 gehenna return (EEXIST);
315 1.2 gehenna
316 1.2 gehenna cdevsw[*devmajor] = devsw;
317 1.2 gehenna
318 1.2 gehenna return (0);
319 1.2 gehenna }
320 1.2 gehenna
321 1.11 ad static void
322 1.11 ad devsw_detach_locked(const struct bdevsw *bdev, const struct cdevsw *cdev)
323 1.2 gehenna {
324 1.2 gehenna int i;
325 1.2 gehenna
326 1.23 pooka KASSERT(mutex_owned(&device_lock));
327 1.11 ad
328 1.2 gehenna if (bdev != NULL) {
329 1.2 gehenna for (i = 0 ; i < max_bdevsws ; i++) {
330 1.2 gehenna if (bdevsw[i] != bdev)
331 1.2 gehenna continue;
332 1.2 gehenna bdevsw[i] = NULL;
333 1.2 gehenna break;
334 1.2 gehenna }
335 1.2 gehenna }
336 1.2 gehenna if (cdev != NULL) {
337 1.2 gehenna for (i = 0 ; i < max_cdevsws ; i++) {
338 1.2 gehenna if (cdevsw[i] != cdev)
339 1.2 gehenna continue;
340 1.2 gehenna cdevsw[i] = NULL;
341 1.2 gehenna break;
342 1.2 gehenna }
343 1.2 gehenna }
344 1.2 gehenna }
345 1.2 gehenna
346 1.39 riastrad void
347 1.11 ad devsw_detach(const struct bdevsw *bdev, const struct cdevsw *cdev)
348 1.11 ad {
349 1.11 ad
350 1.23 pooka mutex_enter(&device_lock);
351 1.11 ad devsw_detach_locked(bdev, cdev);
352 1.23 pooka mutex_exit(&device_lock);
353 1.11 ad }
354 1.11 ad
355 1.11 ad /*
356 1.11 ad * Look up a block device by number.
357 1.11 ad *
358 1.11 ad * => Caller must ensure that the device is attached.
359 1.11 ad */
360 1.2 gehenna const struct bdevsw *
361 1.2 gehenna bdevsw_lookup(dev_t dev)
362 1.2 gehenna {
363 1.24 drochner devmajor_t bmajor;
364 1.2 gehenna
365 1.2 gehenna if (dev == NODEV)
366 1.2 gehenna return (NULL);
367 1.2 gehenna bmajor = major(dev);
368 1.2 gehenna if (bmajor < 0 || bmajor >= max_bdevsws)
369 1.2 gehenna return (NULL);
370 1.2 gehenna
371 1.2 gehenna return (bdevsw[bmajor]);
372 1.2 gehenna }
373 1.2 gehenna
374 1.11 ad /*
375 1.11 ad * Look up a character device by number.
376 1.11 ad *
377 1.11 ad * => Caller must ensure that the device is attached.
378 1.11 ad */
379 1.2 gehenna const struct cdevsw *
380 1.2 gehenna cdevsw_lookup(dev_t dev)
381 1.2 gehenna {
382 1.24 drochner devmajor_t cmajor;
383 1.2 gehenna
384 1.2 gehenna if (dev == NODEV)
385 1.2 gehenna return (NULL);
386 1.2 gehenna cmajor = major(dev);
387 1.2 gehenna if (cmajor < 0 || cmajor >= max_cdevsws)
388 1.2 gehenna return (NULL);
389 1.2 gehenna
390 1.2 gehenna return (cdevsw[cmajor]);
391 1.2 gehenna }
392 1.2 gehenna
393 1.11 ad /*
394 1.11 ad * Look up a block device by reference to its operations set.
395 1.11 ad *
396 1.11 ad * => Caller must ensure that the device is not detached, and therefore
397 1.11 ad * that the returned major is still valid when dereferenced.
398 1.11 ad */
399 1.24 drochner devmajor_t
400 1.2 gehenna bdevsw_lookup_major(const struct bdevsw *bdev)
401 1.2 gehenna {
402 1.24 drochner devmajor_t bmajor;
403 1.2 gehenna
404 1.2 gehenna for (bmajor = 0 ; bmajor < max_bdevsws ; bmajor++) {
405 1.2 gehenna if (bdevsw[bmajor] == bdev)
406 1.2 gehenna return (bmajor);
407 1.2 gehenna }
408 1.2 gehenna
409 1.24 drochner return (NODEVMAJOR);
410 1.2 gehenna }
411 1.2 gehenna
412 1.11 ad /*
413 1.11 ad * Look up a character device by reference to its operations set.
414 1.11 ad *
415 1.11 ad * => Caller must ensure that the device is not detached, and therefore
416 1.11 ad * that the returned major is still valid when dereferenced.
417 1.11 ad */
418 1.24 drochner devmajor_t
419 1.2 gehenna cdevsw_lookup_major(const struct cdevsw *cdev)
420 1.2 gehenna {
421 1.24 drochner devmajor_t cmajor;
422 1.2 gehenna
423 1.2 gehenna for (cmajor = 0 ; cmajor < max_cdevsws ; cmajor++) {
424 1.2 gehenna if (cdevsw[cmajor] == cdev)
425 1.2 gehenna return (cmajor);
426 1.2 gehenna }
427 1.2 gehenna
428 1.24 drochner return (NODEVMAJOR);
429 1.2 gehenna }
430 1.2 gehenna
431 1.2 gehenna /*
432 1.2 gehenna * Convert from block major number to name.
433 1.11 ad *
434 1.11 ad * => Caller must ensure that the device is not detached, and therefore
435 1.11 ad * that the name pointer is still valid when dereferenced.
436 1.2 gehenna */
437 1.2 gehenna const char *
438 1.24 drochner devsw_blk2name(devmajor_t bmajor)
439 1.2 gehenna {
440 1.11 ad const char *name;
441 1.24 drochner devmajor_t cmajor;
442 1.24 drochner int i;
443 1.2 gehenna
444 1.11 ad name = NULL;
445 1.11 ad cmajor = -1;
446 1.11 ad
447 1.23 pooka mutex_enter(&device_lock);
448 1.11 ad if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
449 1.23 pooka mutex_exit(&device_lock);
450 1.2 gehenna return (NULL);
451 1.2 gehenna }
452 1.11 ad for (i = 0 ; i < max_devsw_convs; i++) {
453 1.11 ad if (devsw_conv[i].d_bmajor == bmajor) {
454 1.11 ad cmajor = devsw_conv[i].d_cmajor;
455 1.11 ad break;
456 1.11 ad }
457 1.11 ad }
458 1.11 ad if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
459 1.11 ad name = devsw_conv[i].d_name;
460 1.23 pooka mutex_exit(&device_lock);
461 1.2 gehenna
462 1.11 ad return (name);
463 1.2 gehenna }
464 1.2 gehenna
465 1.2 gehenna /*
466 1.26 haad * Convert char major number to device driver name.
467 1.26 haad */
468 1.27 yamt const char *
469 1.26 haad cdevsw_getname(devmajor_t major)
470 1.26 haad {
471 1.26 haad const char *name;
472 1.26 haad int i;
473 1.26 haad
474 1.26 haad name = NULL;
475 1.26 haad
476 1.26 haad if (major < 0)
477 1.26 haad return (NULL);
478 1.26 haad
479 1.26 haad mutex_enter(&device_lock);
480 1.26 haad for (i = 0 ; i < max_devsw_convs; i++) {
481 1.26 haad if (devsw_conv[i].d_cmajor == major) {
482 1.26 haad name = devsw_conv[i].d_name;
483 1.26 haad break;
484 1.26 haad }
485 1.26 haad }
486 1.26 haad mutex_exit(&device_lock);
487 1.26 haad return (name);
488 1.26 haad }
489 1.26 haad
490 1.26 haad /*
491 1.26 haad * Convert block major number to device driver name.
492 1.26 haad */
493 1.27 yamt const char *
494 1.26 haad bdevsw_getname(devmajor_t major)
495 1.26 haad {
496 1.26 haad const char *name;
497 1.26 haad int i;
498 1.26 haad
499 1.26 haad name = NULL;
500 1.26 haad
501 1.26 haad if (major < 0)
502 1.26 haad return (NULL);
503 1.26 haad
504 1.26 haad mutex_enter(&device_lock);
505 1.26 haad for (i = 0 ; i < max_devsw_convs; i++) {
506 1.26 haad if (devsw_conv[i].d_bmajor == major) {
507 1.26 haad name = devsw_conv[i].d_name;
508 1.26 haad break;
509 1.26 haad }
510 1.26 haad }
511 1.26 haad mutex_exit(&device_lock);
512 1.26 haad return (name);
513 1.26 haad }
514 1.26 haad
515 1.26 haad /*
516 1.2 gehenna * Convert from device name to block major number.
517 1.11 ad *
518 1.11 ad * => Caller must ensure that the device is not detached, and therefore
519 1.11 ad * that the major number is still valid when dereferenced.
520 1.2 gehenna */
521 1.24 drochner devmajor_t
522 1.2 gehenna devsw_name2blk(const char *name, char *devname, size_t devnamelen)
523 1.2 gehenna {
524 1.2 gehenna struct devsw_conv *conv;
525 1.24 drochner devmajor_t bmajor;
526 1.24 drochner int i;
527 1.2 gehenna
528 1.2 gehenna if (name == NULL)
529 1.24 drochner return (NODEVMAJOR);
530 1.2 gehenna
531 1.23 pooka mutex_enter(&device_lock);
532 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
533 1.5 mrg size_t len;
534 1.5 mrg
535 1.2 gehenna conv = &devsw_conv[i];
536 1.2 gehenna if (conv->d_name == NULL)
537 1.2 gehenna continue;
538 1.5 mrg len = strlen(conv->d_name);
539 1.5 mrg if (strncmp(conv->d_name, name, len) != 0)
540 1.5 mrg continue;
541 1.5 mrg if (*(name +len) && !isdigit(*(name + len)))
542 1.2 gehenna continue;
543 1.2 gehenna bmajor = conv->d_bmajor;
544 1.2 gehenna if (bmajor < 0 || bmajor >= max_bdevsws ||
545 1.2 gehenna bdevsw[bmajor] == NULL)
546 1.5 mrg break;
547 1.2 gehenna if (devname != NULL) {
548 1.2 gehenna #ifdef DEVSW_DEBUG
549 1.2 gehenna if (strlen(conv->d_name) >= devnamelen)
550 1.37 pgoyette printf("%s: too short buffer", __func__);
551 1.2 gehenna #endif /* DEVSW_DEBUG */
552 1.4 tsutsui strncpy(devname, conv->d_name, devnamelen);
553 1.2 gehenna devname[devnamelen - 1] = '\0';
554 1.2 gehenna }
555 1.23 pooka mutex_exit(&device_lock);
556 1.2 gehenna return (bmajor);
557 1.2 gehenna }
558 1.2 gehenna
559 1.23 pooka mutex_exit(&device_lock);
560 1.24 drochner return (NODEVMAJOR);
561 1.2 gehenna }
562 1.2 gehenna
563 1.2 gehenna /*
564 1.16 plunky * Convert from device name to char major number.
565 1.16 plunky *
566 1.16 plunky * => Caller must ensure that the device is not detached, and therefore
567 1.16 plunky * that the major number is still valid when dereferenced.
568 1.16 plunky */
569 1.24 drochner devmajor_t
570 1.16 plunky devsw_name2chr(const char *name, char *devname, size_t devnamelen)
571 1.16 plunky {
572 1.16 plunky struct devsw_conv *conv;
573 1.24 drochner devmajor_t cmajor;
574 1.24 drochner int i;
575 1.16 plunky
576 1.16 plunky if (name == NULL)
577 1.24 drochner return (NODEVMAJOR);
578 1.16 plunky
579 1.23 pooka mutex_enter(&device_lock);
580 1.16 plunky for (i = 0 ; i < max_devsw_convs ; i++) {
581 1.16 plunky size_t len;
582 1.16 plunky
583 1.16 plunky conv = &devsw_conv[i];
584 1.16 plunky if (conv->d_name == NULL)
585 1.16 plunky continue;
586 1.16 plunky len = strlen(conv->d_name);
587 1.16 plunky if (strncmp(conv->d_name, name, len) != 0)
588 1.16 plunky continue;
589 1.16 plunky if (*(name +len) && !isdigit(*(name + len)))
590 1.16 plunky continue;
591 1.16 plunky cmajor = conv->d_cmajor;
592 1.16 plunky if (cmajor < 0 || cmajor >= max_cdevsws ||
593 1.16 plunky cdevsw[cmajor] == NULL)
594 1.16 plunky break;
595 1.16 plunky if (devname != NULL) {
596 1.16 plunky #ifdef DEVSW_DEBUG
597 1.16 plunky if (strlen(conv->d_name) >= devnamelen)
598 1.37 pgoyette printf("%s: too short buffer", __func__);
599 1.16 plunky #endif /* DEVSW_DEBUG */
600 1.16 plunky strncpy(devname, conv->d_name, devnamelen);
601 1.16 plunky devname[devnamelen - 1] = '\0';
602 1.16 plunky }
603 1.23 pooka mutex_exit(&device_lock);
604 1.16 plunky return (cmajor);
605 1.16 plunky }
606 1.16 plunky
607 1.23 pooka mutex_exit(&device_lock);
608 1.24 drochner return (NODEVMAJOR);
609 1.16 plunky }
610 1.16 plunky
611 1.16 plunky /*
612 1.2 gehenna * Convert from character dev_t to block dev_t.
613 1.11 ad *
614 1.11 ad * => Caller must ensure that the device is not detached, and therefore
615 1.11 ad * that the major number is still valid when dereferenced.
616 1.2 gehenna */
617 1.2 gehenna dev_t
618 1.2 gehenna devsw_chr2blk(dev_t cdev)
619 1.2 gehenna {
620 1.24 drochner devmajor_t bmajor, cmajor;
621 1.24 drochner int i;
622 1.11 ad dev_t rv;
623 1.2 gehenna
624 1.2 gehenna cmajor = major(cdev);
625 1.24 drochner bmajor = NODEVMAJOR;
626 1.11 ad rv = NODEV;
627 1.2 gehenna
628 1.23 pooka mutex_enter(&device_lock);
629 1.11 ad if (cmajor < 0 || cmajor >= max_cdevsws || cdevsw[cmajor] == NULL) {
630 1.23 pooka mutex_exit(&device_lock);
631 1.11 ad return (NODEV);
632 1.11 ad }
633 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
634 1.11 ad if (devsw_conv[i].d_cmajor == cmajor) {
635 1.11 ad bmajor = devsw_conv[i].d_bmajor;
636 1.11 ad break;
637 1.11 ad }
638 1.2 gehenna }
639 1.11 ad if (bmajor >= 0 && bmajor < max_bdevsws && bdevsw[bmajor] != NULL)
640 1.11 ad rv = makedev(bmajor, minor(cdev));
641 1.23 pooka mutex_exit(&device_lock);
642 1.2 gehenna
643 1.11 ad return (rv);
644 1.2 gehenna }
645 1.2 gehenna
646 1.2 gehenna /*
647 1.2 gehenna * Convert from block dev_t to character dev_t.
648 1.11 ad *
649 1.11 ad * => Caller must ensure that the device is not detached, and therefore
650 1.11 ad * that the major number is still valid when dereferenced.
651 1.2 gehenna */
652 1.2 gehenna dev_t
653 1.2 gehenna devsw_blk2chr(dev_t bdev)
654 1.2 gehenna {
655 1.24 drochner devmajor_t bmajor, cmajor;
656 1.24 drochner int i;
657 1.11 ad dev_t rv;
658 1.2 gehenna
659 1.11 ad bmajor = major(bdev);
660 1.24 drochner cmajor = NODEVMAJOR;
661 1.11 ad rv = NODEV;
662 1.11 ad
663 1.23 pooka mutex_enter(&device_lock);
664 1.11 ad if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
665 1.23 pooka mutex_exit(&device_lock);
666 1.2 gehenna return (NODEV);
667 1.11 ad }
668 1.11 ad for (i = 0 ; i < max_devsw_convs ; i++) {
669 1.11 ad if (devsw_conv[i].d_bmajor == bmajor) {
670 1.11 ad cmajor = devsw_conv[i].d_cmajor;
671 1.11 ad break;
672 1.11 ad }
673 1.11 ad }
674 1.11 ad if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
675 1.11 ad rv = makedev(cmajor, minor(bdev));
676 1.23 pooka mutex_exit(&device_lock);
677 1.2 gehenna
678 1.11 ad return (rv);
679 1.11 ad }
680 1.11 ad
681 1.11 ad /*
682 1.11 ad * Device access methods.
683 1.11 ad */
684 1.11 ad
685 1.11 ad #define DEV_LOCK(d) \
686 1.17 ad if ((mpflag = (d->d_flag & D_MPSAFE)) == 0) { \
687 1.17 ad KERNEL_LOCK(1, NULL); \
688 1.11 ad }
689 1.2 gehenna
690 1.11 ad #define DEV_UNLOCK(d) \
691 1.17 ad if (mpflag == 0) { \
692 1.17 ad KERNEL_UNLOCK_ONE(NULL); \
693 1.2 gehenna }
694 1.2 gehenna
695 1.11 ad int
696 1.11 ad bdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
697 1.11 ad {
698 1.11 ad const struct bdevsw *d;
699 1.17 ad int rv, mpflag;
700 1.11 ad
701 1.11 ad /*
702 1.11 ad * For open we need to lock, in order to synchronize
703 1.11 ad * with attach/detach.
704 1.11 ad */
705 1.23 pooka mutex_enter(&device_lock);
706 1.11 ad d = bdevsw_lookup(dev);
707 1.23 pooka mutex_exit(&device_lock);
708 1.11 ad if (d == NULL)
709 1.11 ad return ENXIO;
710 1.11 ad
711 1.11 ad DEV_LOCK(d);
712 1.11 ad rv = (*d->d_open)(dev, flag, devtype, l);
713 1.11 ad DEV_UNLOCK(d);
714 1.11 ad
715 1.11 ad return rv;
716 1.11 ad }
717 1.11 ad
718 1.11 ad int
719 1.11 ad bdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
720 1.11 ad {
721 1.11 ad const struct bdevsw *d;
722 1.17 ad int rv, mpflag;
723 1.11 ad
724 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
725 1.11 ad return ENXIO;
726 1.11 ad
727 1.11 ad DEV_LOCK(d);
728 1.11 ad rv = (*d->d_close)(dev, flag, devtype, l);
729 1.11 ad DEV_UNLOCK(d);
730 1.11 ad
731 1.11 ad return rv;
732 1.11 ad }
733 1.11 ad
734 1.34 riz SDT_PROVIDER_DECLARE(io);
735 1.34 riz SDT_PROBE_DEFINE1(io, kernel, , start, "struct buf *"/*bp*/);
736 1.34 riz
737 1.11 ad void
738 1.11 ad bdev_strategy(struct buf *bp)
739 1.11 ad {
740 1.11 ad const struct bdevsw *d;
741 1.17 ad int mpflag;
742 1.11 ad
743 1.34 riz SDT_PROBE1(io, kernel, , start, bp);
744 1.34 riz
745 1.28 jmcneill if ((d = bdevsw_lookup(bp->b_dev)) == NULL) {
746 1.28 jmcneill bp->b_error = ENXIO;
747 1.28 jmcneill bp->b_resid = bp->b_bcount;
748 1.31 pooka biodone_vfs(bp); /* biodone() iff vfs present */
749 1.28 jmcneill return;
750 1.28 jmcneill }
751 1.11 ad
752 1.11 ad DEV_LOCK(d);
753 1.11 ad (*d->d_strategy)(bp);
754 1.11 ad DEV_UNLOCK(d);
755 1.11 ad }
756 1.11 ad
757 1.11 ad int
758 1.11 ad bdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
759 1.11 ad {
760 1.11 ad const struct bdevsw *d;
761 1.17 ad int rv, mpflag;
762 1.11 ad
763 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
764 1.11 ad return ENXIO;
765 1.11 ad
766 1.11 ad DEV_LOCK(d);
767 1.11 ad rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
768 1.11 ad DEV_UNLOCK(d);
769 1.11 ad
770 1.11 ad return rv;
771 1.11 ad }
772 1.11 ad
773 1.11 ad int
774 1.11 ad bdev_dump(dev_t dev, daddr_t addr, void *data, size_t sz)
775 1.11 ad {
776 1.11 ad const struct bdevsw *d;
777 1.11 ad int rv;
778 1.11 ad
779 1.11 ad /*
780 1.11 ad * Dump can be called without the device open. Since it can
781 1.11 ad * currently only be called with the system paused (and in a
782 1.11 ad * potentially unstable state), we don't perform any locking.
783 1.11 ad */
784 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
785 1.11 ad return ENXIO;
786 1.11 ad
787 1.11 ad /* DEV_LOCK(d); */
788 1.11 ad rv = (*d->d_dump)(dev, addr, data, sz);
789 1.11 ad /* DEV_UNLOCK(d); */
790 1.11 ad
791 1.11 ad return rv;
792 1.11 ad }
793 1.11 ad
794 1.11 ad int
795 1.35 nat bdev_flags(dev_t dev)
796 1.35 nat {
797 1.35 nat const struct bdevsw *d;
798 1.35 nat
799 1.35 nat if ((d = bdevsw_lookup(dev)) == NULL)
800 1.35 nat return 0;
801 1.35 nat return d->d_flag & ~D_TYPEMASK;
802 1.35 nat }
803 1.35 nat
804 1.35 nat int
805 1.11 ad bdev_type(dev_t dev)
806 1.11 ad {
807 1.11 ad const struct bdevsw *d;
808 1.11 ad
809 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
810 1.11 ad return D_OTHER;
811 1.11 ad return d->d_flag & D_TYPEMASK;
812 1.11 ad }
813 1.11 ad
814 1.11 ad int
815 1.29 mrg bdev_size(dev_t dev)
816 1.29 mrg {
817 1.29 mrg const struct bdevsw *d;
818 1.29 mrg int rv, mpflag = 0;
819 1.29 mrg
820 1.29 mrg if ((d = bdevsw_lookup(dev)) == NULL ||
821 1.29 mrg d->d_psize == NULL)
822 1.29 mrg return -1;
823 1.29 mrg
824 1.29 mrg /*
825 1.29 mrg * Don't to try lock the device if we're dumping.
826 1.30 mrg * XXX: is there a better way to test this?
827 1.29 mrg */
828 1.29 mrg if ((boothowto & RB_DUMP) == 0)
829 1.29 mrg DEV_LOCK(d);
830 1.29 mrg rv = (*d->d_psize)(dev);
831 1.29 mrg if ((boothowto & RB_DUMP) == 0)
832 1.29 mrg DEV_UNLOCK(d);
833 1.29 mrg
834 1.29 mrg return rv;
835 1.29 mrg }
836 1.29 mrg
837 1.29 mrg int
838 1.32 dholland bdev_discard(dev_t dev, off_t pos, off_t len)
839 1.32 dholland {
840 1.32 dholland const struct bdevsw *d;
841 1.32 dholland int rv, mpflag;
842 1.32 dholland
843 1.32 dholland if ((d = bdevsw_lookup(dev)) == NULL)
844 1.32 dholland return ENXIO;
845 1.32 dholland
846 1.32 dholland DEV_LOCK(d);
847 1.32 dholland rv = (*d->d_discard)(dev, pos, len);
848 1.32 dholland DEV_UNLOCK(d);
849 1.32 dholland
850 1.32 dholland return rv;
851 1.32 dholland }
852 1.32 dholland
853 1.32 dholland int
854 1.11 ad cdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
855 1.11 ad {
856 1.11 ad const struct cdevsw *d;
857 1.17 ad int rv, mpflag;
858 1.11 ad
859 1.11 ad /*
860 1.11 ad * For open we need to lock, in order to synchronize
861 1.11 ad * with attach/detach.
862 1.11 ad */
863 1.23 pooka mutex_enter(&device_lock);
864 1.11 ad d = cdevsw_lookup(dev);
865 1.23 pooka mutex_exit(&device_lock);
866 1.11 ad if (d == NULL)
867 1.11 ad return ENXIO;
868 1.11 ad
869 1.11 ad DEV_LOCK(d);
870 1.11 ad rv = (*d->d_open)(dev, flag, devtype, l);
871 1.11 ad DEV_UNLOCK(d);
872 1.11 ad
873 1.11 ad return rv;
874 1.11 ad }
875 1.11 ad
876 1.11 ad int
877 1.11 ad cdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
878 1.11 ad {
879 1.11 ad const struct cdevsw *d;
880 1.17 ad int rv, mpflag;
881 1.11 ad
882 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
883 1.11 ad return ENXIO;
884 1.11 ad
885 1.11 ad DEV_LOCK(d);
886 1.11 ad rv = (*d->d_close)(dev, flag, devtype, l);
887 1.11 ad DEV_UNLOCK(d);
888 1.11 ad
889 1.11 ad return rv;
890 1.11 ad }
891 1.11 ad
892 1.11 ad int
893 1.11 ad cdev_read(dev_t dev, struct uio *uio, int flag)
894 1.11 ad {
895 1.11 ad const struct cdevsw *d;
896 1.17 ad int rv, mpflag;
897 1.11 ad
898 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
899 1.11 ad return ENXIO;
900 1.11 ad
901 1.11 ad DEV_LOCK(d);
902 1.11 ad rv = (*d->d_read)(dev, uio, flag);
903 1.11 ad DEV_UNLOCK(d);
904 1.11 ad
905 1.11 ad return rv;
906 1.11 ad }
907 1.11 ad
908 1.11 ad int
909 1.11 ad cdev_write(dev_t dev, struct uio *uio, int flag)
910 1.11 ad {
911 1.11 ad const struct cdevsw *d;
912 1.17 ad int rv, mpflag;
913 1.11 ad
914 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
915 1.11 ad return ENXIO;
916 1.11 ad
917 1.11 ad DEV_LOCK(d);
918 1.11 ad rv = (*d->d_write)(dev, uio, flag);
919 1.11 ad DEV_UNLOCK(d);
920 1.11 ad
921 1.11 ad return rv;
922 1.11 ad }
923 1.11 ad
924 1.11 ad int
925 1.11 ad cdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
926 1.11 ad {
927 1.11 ad const struct cdevsw *d;
928 1.17 ad int rv, mpflag;
929 1.11 ad
930 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
931 1.11 ad return ENXIO;
932 1.11 ad
933 1.11 ad DEV_LOCK(d);
934 1.11 ad rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
935 1.11 ad DEV_UNLOCK(d);
936 1.11 ad
937 1.11 ad return rv;
938 1.11 ad }
939 1.11 ad
940 1.11 ad void
941 1.11 ad cdev_stop(struct tty *tp, int flag)
942 1.11 ad {
943 1.11 ad const struct cdevsw *d;
944 1.17 ad int mpflag;
945 1.11 ad
946 1.11 ad if ((d = cdevsw_lookup(tp->t_dev)) == NULL)
947 1.11 ad return;
948 1.11 ad
949 1.11 ad DEV_LOCK(d);
950 1.11 ad (*d->d_stop)(tp, flag);
951 1.11 ad DEV_UNLOCK(d);
952 1.11 ad }
953 1.11 ad
954 1.11 ad struct tty *
955 1.11 ad cdev_tty(dev_t dev)
956 1.11 ad {
957 1.11 ad const struct cdevsw *d;
958 1.11 ad
959 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
960 1.11 ad return NULL;
961 1.11 ad
962 1.12 ad /* XXX Check if necessary. */
963 1.12 ad if (d->d_tty == NULL)
964 1.12 ad return NULL;
965 1.12 ad
966 1.21 ad return (*d->d_tty)(dev);
967 1.11 ad }
968 1.11 ad
969 1.11 ad int
970 1.11 ad cdev_poll(dev_t dev, int flag, lwp_t *l)
971 1.11 ad {
972 1.11 ad const struct cdevsw *d;
973 1.17 ad int rv, mpflag;
974 1.11 ad
975 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
976 1.11 ad return POLLERR;
977 1.11 ad
978 1.11 ad DEV_LOCK(d);
979 1.11 ad rv = (*d->d_poll)(dev, flag, l);
980 1.11 ad DEV_UNLOCK(d);
981 1.11 ad
982 1.11 ad return rv;
983 1.11 ad }
984 1.11 ad
985 1.11 ad paddr_t
986 1.11 ad cdev_mmap(dev_t dev, off_t off, int flag)
987 1.11 ad {
988 1.11 ad const struct cdevsw *d;
989 1.11 ad paddr_t rv;
990 1.17 ad int mpflag;
991 1.11 ad
992 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
993 1.11 ad return (paddr_t)-1LL;
994 1.11 ad
995 1.11 ad DEV_LOCK(d);
996 1.11 ad rv = (*d->d_mmap)(dev, off, flag);
997 1.11 ad DEV_UNLOCK(d);
998 1.11 ad
999 1.11 ad return rv;
1000 1.11 ad }
1001 1.11 ad
1002 1.11 ad int
1003 1.11 ad cdev_kqfilter(dev_t dev, struct knote *kn)
1004 1.11 ad {
1005 1.11 ad const struct cdevsw *d;
1006 1.17 ad int rv, mpflag;
1007 1.11 ad
1008 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
1009 1.11 ad return ENXIO;
1010 1.11 ad
1011 1.11 ad DEV_LOCK(d);
1012 1.11 ad rv = (*d->d_kqfilter)(dev, kn);
1013 1.11 ad DEV_UNLOCK(d);
1014 1.11 ad
1015 1.11 ad return rv;
1016 1.11 ad }
1017 1.11 ad
1018 1.11 ad int
1019 1.32 dholland cdev_discard(dev_t dev, off_t pos, off_t len)
1020 1.32 dholland {
1021 1.32 dholland const struct cdevsw *d;
1022 1.32 dholland int rv, mpflag;
1023 1.32 dholland
1024 1.32 dholland if ((d = cdevsw_lookup(dev)) == NULL)
1025 1.32 dholland return ENXIO;
1026 1.32 dholland
1027 1.32 dholland DEV_LOCK(d);
1028 1.32 dholland rv = (*d->d_discard)(dev, pos, len);
1029 1.32 dholland DEV_UNLOCK(d);
1030 1.32 dholland
1031 1.32 dholland return rv;
1032 1.32 dholland }
1033 1.32 dholland
1034 1.32 dholland int
1035 1.35 nat cdev_flags(dev_t dev)
1036 1.35 nat {
1037 1.35 nat const struct cdevsw *d;
1038 1.35 nat
1039 1.35 nat if ((d = cdevsw_lookup(dev)) == NULL)
1040 1.35 nat return 0;
1041 1.35 nat return d->d_flag & ~D_TYPEMASK;
1042 1.35 nat }
1043 1.35 nat
1044 1.35 nat int
1045 1.11 ad cdev_type(dev_t dev)
1046 1.11 ad {
1047 1.11 ad const struct cdevsw *d;
1048 1.11 ad
1049 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
1050 1.11 ad return D_OTHER;
1051 1.11 ad return d->d_flag & D_TYPEMASK;
1052 1.2 gehenna }
1053 1.36 riastrad
1054 1.36 riastrad /*
1055 1.36 riastrad * nommap(dev, off, prot)
1056 1.36 riastrad *
1057 1.36 riastrad * mmap routine that always fails, for non-mmappable devices.
1058 1.36 riastrad */
1059 1.36 riastrad paddr_t
1060 1.36 riastrad nommap(dev_t dev, off_t off, int prot)
1061 1.36 riastrad {
1062 1.36 riastrad
1063 1.36 riastrad return (paddr_t)-1;
1064 1.36 riastrad }
1065