subr_devsw.c revision 1.24 1 1.24 drochner /* $NetBSD: subr_devsw.c,v 1.24 2009/01/20 18:20:48 drochner 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.24 drochner __KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.24 2009/01/20 18:20:48 drochner 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.2 gehenna
83 1.2 gehenna #ifdef DEVSW_DEBUG
84 1.2 gehenna #define DPRINTF(x) printf x
85 1.2 gehenna #else /* DEVSW_DEBUG */
86 1.2 gehenna #define DPRINTF(x)
87 1.2 gehenna #endif /* DEVSW_DEBUG */
88 1.2 gehenna
89 1.11 ad #define MAXDEVSW 512 /* the maximum of major device number */
90 1.2 gehenna #define BDEVSW_SIZE (sizeof(struct bdevsw *))
91 1.2 gehenna #define CDEVSW_SIZE (sizeof(struct cdevsw *))
92 1.2 gehenna #define DEVSWCONV_SIZE (sizeof(struct devsw_conv))
93 1.2 gehenna
94 1.2 gehenna extern const struct bdevsw **bdevsw, *bdevsw0[];
95 1.2 gehenna extern const struct cdevsw **cdevsw, *cdevsw0[];
96 1.2 gehenna extern struct devsw_conv *devsw_conv, devsw_conv0[];
97 1.2 gehenna extern const int sys_bdevsws, sys_cdevsws;
98 1.2 gehenna extern int max_bdevsws, max_cdevsws, max_devsw_convs;
99 1.2 gehenna
100 1.24 drochner static int bdevsw_attach(const struct bdevsw *, devmajor_t *);
101 1.24 drochner static int cdevsw_attach(const struct cdevsw *, devmajor_t *);
102 1.11 ad static void devsw_detach_locked(const struct bdevsw *, const struct cdevsw *);
103 1.11 ad
104 1.23 pooka kmutex_t device_lock;
105 1.23 pooka
106 1.11 ad void
107 1.11 ad devsw_init(void)
108 1.11 ad {
109 1.11 ad
110 1.11 ad KASSERT(sys_bdevsws < MAXDEVSW - 1);
111 1.11 ad KASSERT(sys_cdevsws < MAXDEVSW - 1);
112 1.23 pooka mutex_init(&device_lock, MUTEX_DEFAULT, IPL_NONE);
113 1.11 ad }
114 1.2 gehenna
115 1.2 gehenna int
116 1.24 drochner devsw_attach(const char *devname,
117 1.24 drochner const struct bdevsw *bdev, devmajor_t *bmajor,
118 1.24 drochner const struct cdevsw *cdev, devmajor_t *cmajor)
119 1.2 gehenna {
120 1.2 gehenna struct devsw_conv *conv;
121 1.2 gehenna char *name;
122 1.2 gehenna int error, i;
123 1.2 gehenna
124 1.2 gehenna if (devname == NULL || cdev == NULL)
125 1.2 gehenna return (EINVAL);
126 1.2 gehenna
127 1.23 pooka mutex_enter(&device_lock);
128 1.11 ad
129 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
130 1.2 gehenna conv = &devsw_conv[i];
131 1.2 gehenna if (conv->d_name == NULL || strcmp(devname, conv->d_name) != 0)
132 1.2 gehenna continue;
133 1.2 gehenna
134 1.2 gehenna if (*bmajor < 0)
135 1.2 gehenna *bmajor = conv->d_bmajor;
136 1.2 gehenna if (*cmajor < 0)
137 1.2 gehenna *cmajor = conv->d_cmajor;
138 1.2 gehenna
139 1.11 ad if (*bmajor != conv->d_bmajor || *cmajor != conv->d_cmajor) {
140 1.11 ad error = EINVAL;
141 1.11 ad goto fail;
142 1.11 ad }
143 1.11 ad if ((*bmajor >= 0 && bdev == NULL) || *cmajor < 0) {
144 1.11 ad error = EINVAL;
145 1.11 ad goto fail;
146 1.11 ad }
147 1.2 gehenna
148 1.2 gehenna if ((*bmajor >= 0 && bdevsw[*bmajor] != NULL) ||
149 1.11 ad cdevsw[*cmajor] != NULL) {
150 1.11 ad error = EEXIST;
151 1.11 ad goto fail;
152 1.11 ad }
153 1.2 gehenna
154 1.2 gehenna if (bdev != NULL)
155 1.2 gehenna bdevsw[*bmajor] = bdev;
156 1.2 gehenna cdevsw[*cmajor] = cdev;
157 1.2 gehenna
158 1.23 pooka mutex_exit(&device_lock);
159 1.2 gehenna return (0);
160 1.2 gehenna }
161 1.2 gehenna
162 1.14 pooka error = bdevsw_attach(bdev, bmajor);
163 1.11 ad if (error != 0)
164 1.11 ad goto fail;
165 1.14 pooka error = cdevsw_attach(cdev, cmajor);
166 1.2 gehenna if (error != 0) {
167 1.11 ad devsw_detach_locked(bdev, NULL);
168 1.11 ad goto fail;
169 1.2 gehenna }
170 1.2 gehenna
171 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
172 1.2 gehenna if (devsw_conv[i].d_name == NULL)
173 1.2 gehenna break;
174 1.2 gehenna }
175 1.2 gehenna if (i == max_devsw_convs) {
176 1.2 gehenna struct devsw_conv *newptr;
177 1.2 gehenna int old, new;
178 1.2 gehenna
179 1.2 gehenna old = max_devsw_convs;
180 1.2 gehenna new = old + 1;
181 1.2 gehenna
182 1.11 ad newptr = kmem_zalloc(new * DEVSWCONV_SIZE, KM_NOSLEEP);
183 1.2 gehenna if (newptr == NULL) {
184 1.11 ad devsw_detach_locked(bdev, cdev);
185 1.11 ad error = ENOMEM;
186 1.11 ad goto fail;
187 1.2 gehenna }
188 1.2 gehenna newptr[old].d_name = NULL;
189 1.2 gehenna newptr[old].d_bmajor = -1;
190 1.2 gehenna newptr[old].d_cmajor = -1;
191 1.2 gehenna memcpy(newptr, devsw_conv, old * DEVSWCONV_SIZE);
192 1.2 gehenna if (devsw_conv != devsw_conv0)
193 1.11 ad kmem_free(devsw_conv, old * DEVSWCONV_SIZE);
194 1.2 gehenna devsw_conv = newptr;
195 1.2 gehenna max_devsw_convs = new;
196 1.2 gehenna }
197 1.2 gehenna
198 1.6 itojun i = strlen(devname) + 1;
199 1.11 ad name = kmem_alloc(i, KM_NOSLEEP);
200 1.2 gehenna if (name == NULL) {
201 1.11 ad devsw_detach_locked(bdev, cdev);
202 1.11 ad goto fail;
203 1.2 gehenna }
204 1.6 itojun strlcpy(name, devname, i);
205 1.2 gehenna
206 1.2 gehenna devsw_conv[i].d_name = name;
207 1.2 gehenna devsw_conv[i].d_bmajor = *bmajor;
208 1.2 gehenna devsw_conv[i].d_cmajor = *cmajor;
209 1.2 gehenna
210 1.23 pooka mutex_exit(&device_lock);
211 1.2 gehenna return (0);
212 1.11 ad fail:
213 1.23 pooka mutex_exit(&device_lock);
214 1.11 ad return (error);
215 1.2 gehenna }
216 1.2 gehenna
217 1.2 gehenna static int
218 1.24 drochner bdevsw_attach(const struct bdevsw *devsw, devmajor_t *devmajor)
219 1.2 gehenna {
220 1.11 ad const struct bdevsw **newptr;
221 1.24 drochner devmajor_t bmajor;
222 1.24 drochner int i;
223 1.2 gehenna
224 1.23 pooka KASSERT(mutex_owned(&device_lock));
225 1.11 ad
226 1.2 gehenna if (devsw == NULL)
227 1.2 gehenna return (0);
228 1.2 gehenna
229 1.2 gehenna if (*devmajor < 0) {
230 1.2 gehenna for (bmajor = sys_bdevsws ; bmajor < max_bdevsws ; bmajor++) {
231 1.2 gehenna if (bdevsw[bmajor] != NULL)
232 1.2 gehenna continue;
233 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
234 1.2 gehenna if (devsw_conv[i].d_bmajor == bmajor)
235 1.2 gehenna break;
236 1.2 gehenna }
237 1.2 gehenna if (i != max_devsw_convs)
238 1.2 gehenna continue;
239 1.2 gehenna break;
240 1.2 gehenna }
241 1.3 gehenna *devmajor = bmajor;
242 1.2 gehenna }
243 1.11 ad
244 1.2 gehenna if (*devmajor >= MAXDEVSW) {
245 1.11 ad printf("bdevsw_attach: block majors exhausted");
246 1.2 gehenna return (ENOMEM);
247 1.2 gehenna }
248 1.2 gehenna
249 1.2 gehenna if (*devmajor >= max_bdevsws) {
250 1.11 ad KASSERT(bdevsw == bdevsw0);
251 1.11 ad newptr = kmem_zalloc(MAXDEVSW * BDEVSW_SIZE, KM_NOSLEEP);
252 1.2 gehenna if (newptr == NULL)
253 1.2 gehenna return (ENOMEM);
254 1.11 ad memcpy(newptr, bdevsw, max_bdevsws * BDEVSW_SIZE);
255 1.2 gehenna bdevsw = newptr;
256 1.11 ad max_bdevsws = MAXDEVSW;
257 1.2 gehenna }
258 1.2 gehenna
259 1.2 gehenna if (bdevsw[*devmajor] != NULL)
260 1.2 gehenna return (EEXIST);
261 1.2 gehenna
262 1.2 gehenna bdevsw[*devmajor] = devsw;
263 1.2 gehenna
264 1.2 gehenna return (0);
265 1.2 gehenna }
266 1.2 gehenna
267 1.2 gehenna static int
268 1.24 drochner cdevsw_attach(const struct cdevsw *devsw, devmajor_t *devmajor)
269 1.2 gehenna {
270 1.11 ad const struct cdevsw **newptr;
271 1.24 drochner devmajor_t cmajor;
272 1.24 drochner int i;
273 1.2 gehenna
274 1.23 pooka KASSERT(mutex_owned(&device_lock));
275 1.11 ad
276 1.2 gehenna if (*devmajor < 0) {
277 1.2 gehenna for (cmajor = sys_cdevsws ; cmajor < max_cdevsws ; cmajor++) {
278 1.2 gehenna if (cdevsw[cmajor] != NULL)
279 1.2 gehenna continue;
280 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
281 1.2 gehenna if (devsw_conv[i].d_cmajor == cmajor)
282 1.2 gehenna break;
283 1.2 gehenna }
284 1.2 gehenna if (i != max_devsw_convs)
285 1.2 gehenna continue;
286 1.2 gehenna break;
287 1.2 gehenna }
288 1.3 gehenna *devmajor = cmajor;
289 1.2 gehenna }
290 1.11 ad
291 1.2 gehenna if (*devmajor >= MAXDEVSW) {
292 1.11 ad printf("cdevsw_attach: character majors exhausted");
293 1.2 gehenna return (ENOMEM);
294 1.2 gehenna }
295 1.2 gehenna
296 1.2 gehenna if (*devmajor >= max_cdevsws) {
297 1.11 ad KASSERT(cdevsw == cdevsw0);
298 1.11 ad newptr = kmem_zalloc(MAXDEVSW * CDEVSW_SIZE, KM_NOSLEEP);
299 1.2 gehenna if (newptr == NULL)
300 1.2 gehenna return (ENOMEM);
301 1.11 ad memcpy(newptr, cdevsw, max_cdevsws * CDEVSW_SIZE);
302 1.2 gehenna cdevsw = newptr;
303 1.11 ad max_cdevsws = MAXDEVSW;
304 1.2 gehenna }
305 1.2 gehenna
306 1.2 gehenna if (cdevsw[*devmajor] != NULL)
307 1.2 gehenna return (EEXIST);
308 1.2 gehenna
309 1.2 gehenna cdevsw[*devmajor] = devsw;
310 1.2 gehenna
311 1.2 gehenna return (0);
312 1.2 gehenna }
313 1.2 gehenna
314 1.11 ad static void
315 1.11 ad devsw_detach_locked(const struct bdevsw *bdev, const struct cdevsw *cdev)
316 1.2 gehenna {
317 1.2 gehenna int i;
318 1.2 gehenna
319 1.23 pooka KASSERT(mutex_owned(&device_lock));
320 1.11 ad
321 1.2 gehenna if (bdev != NULL) {
322 1.2 gehenna for (i = 0 ; i < max_bdevsws ; i++) {
323 1.2 gehenna if (bdevsw[i] != bdev)
324 1.2 gehenna continue;
325 1.2 gehenna bdevsw[i] = NULL;
326 1.2 gehenna break;
327 1.2 gehenna }
328 1.2 gehenna }
329 1.2 gehenna if (cdev != NULL) {
330 1.2 gehenna for (i = 0 ; i < max_cdevsws ; i++) {
331 1.2 gehenna if (cdevsw[i] != cdev)
332 1.2 gehenna continue;
333 1.2 gehenna cdevsw[i] = NULL;
334 1.2 gehenna break;
335 1.2 gehenna }
336 1.2 gehenna }
337 1.2 gehenna }
338 1.2 gehenna
339 1.19 ad int
340 1.11 ad devsw_detach(const struct bdevsw *bdev, const struct cdevsw *cdev)
341 1.11 ad {
342 1.11 ad
343 1.23 pooka mutex_enter(&device_lock);
344 1.11 ad devsw_detach_locked(bdev, cdev);
345 1.23 pooka mutex_exit(&device_lock);
346 1.19 ad return 0;
347 1.11 ad }
348 1.11 ad
349 1.11 ad /*
350 1.11 ad * Look up a block device by number.
351 1.11 ad *
352 1.11 ad * => Caller must ensure that the device is attached.
353 1.11 ad */
354 1.2 gehenna const struct bdevsw *
355 1.2 gehenna bdevsw_lookup(dev_t dev)
356 1.2 gehenna {
357 1.24 drochner devmajor_t bmajor;
358 1.2 gehenna
359 1.2 gehenna if (dev == NODEV)
360 1.2 gehenna return (NULL);
361 1.2 gehenna bmajor = major(dev);
362 1.2 gehenna if (bmajor < 0 || bmajor >= max_bdevsws)
363 1.2 gehenna return (NULL);
364 1.2 gehenna
365 1.2 gehenna return (bdevsw[bmajor]);
366 1.2 gehenna }
367 1.2 gehenna
368 1.11 ad /*
369 1.11 ad * Look up a character device by number.
370 1.11 ad *
371 1.11 ad * => Caller must ensure that the device is attached.
372 1.11 ad */
373 1.2 gehenna const struct cdevsw *
374 1.2 gehenna cdevsw_lookup(dev_t dev)
375 1.2 gehenna {
376 1.24 drochner devmajor_t cmajor;
377 1.2 gehenna
378 1.2 gehenna if (dev == NODEV)
379 1.2 gehenna return (NULL);
380 1.2 gehenna cmajor = major(dev);
381 1.2 gehenna if (cmajor < 0 || cmajor >= max_cdevsws)
382 1.2 gehenna return (NULL);
383 1.2 gehenna
384 1.2 gehenna return (cdevsw[cmajor]);
385 1.2 gehenna }
386 1.2 gehenna
387 1.11 ad /*
388 1.11 ad * Look up a block device by reference to its operations set.
389 1.11 ad *
390 1.11 ad * => Caller must ensure that the device is not detached, and therefore
391 1.11 ad * that the returned major is still valid when dereferenced.
392 1.11 ad */
393 1.24 drochner devmajor_t
394 1.2 gehenna bdevsw_lookup_major(const struct bdevsw *bdev)
395 1.2 gehenna {
396 1.24 drochner devmajor_t bmajor;
397 1.2 gehenna
398 1.2 gehenna for (bmajor = 0 ; bmajor < max_bdevsws ; bmajor++) {
399 1.2 gehenna if (bdevsw[bmajor] == bdev)
400 1.2 gehenna return (bmajor);
401 1.2 gehenna }
402 1.2 gehenna
403 1.24 drochner return (NODEVMAJOR);
404 1.2 gehenna }
405 1.2 gehenna
406 1.11 ad /*
407 1.11 ad * Look up a character device by reference to its operations set.
408 1.11 ad *
409 1.11 ad * => Caller must ensure that the device is not detached, and therefore
410 1.11 ad * that the returned major is still valid when dereferenced.
411 1.11 ad */
412 1.24 drochner devmajor_t
413 1.2 gehenna cdevsw_lookup_major(const struct cdevsw *cdev)
414 1.2 gehenna {
415 1.24 drochner devmajor_t cmajor;
416 1.2 gehenna
417 1.2 gehenna for (cmajor = 0 ; cmajor < max_cdevsws ; cmajor++) {
418 1.2 gehenna if (cdevsw[cmajor] == cdev)
419 1.2 gehenna return (cmajor);
420 1.2 gehenna }
421 1.2 gehenna
422 1.24 drochner return (NODEVMAJOR);
423 1.2 gehenna }
424 1.2 gehenna
425 1.2 gehenna /*
426 1.2 gehenna * Convert from block major number to name.
427 1.11 ad *
428 1.11 ad * => Caller must ensure that the device is not detached, and therefore
429 1.11 ad * that the name pointer is still valid when dereferenced.
430 1.2 gehenna */
431 1.2 gehenna const char *
432 1.24 drochner devsw_blk2name(devmajor_t bmajor)
433 1.2 gehenna {
434 1.11 ad const char *name;
435 1.24 drochner devmajor_t cmajor;
436 1.24 drochner int i;
437 1.2 gehenna
438 1.11 ad name = NULL;
439 1.11 ad cmajor = -1;
440 1.11 ad
441 1.23 pooka mutex_enter(&device_lock);
442 1.11 ad if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
443 1.23 pooka mutex_exit(&device_lock);
444 1.2 gehenna return (NULL);
445 1.2 gehenna }
446 1.11 ad for (i = 0 ; i < max_devsw_convs; i++) {
447 1.11 ad if (devsw_conv[i].d_bmajor == bmajor) {
448 1.11 ad cmajor = devsw_conv[i].d_cmajor;
449 1.11 ad break;
450 1.11 ad }
451 1.11 ad }
452 1.11 ad if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
453 1.11 ad name = devsw_conv[i].d_name;
454 1.23 pooka mutex_exit(&device_lock);
455 1.2 gehenna
456 1.11 ad return (name);
457 1.2 gehenna }
458 1.2 gehenna
459 1.2 gehenna /*
460 1.2 gehenna * Convert from device name to block major number.
461 1.11 ad *
462 1.11 ad * => Caller must ensure that the device is not detached, and therefore
463 1.11 ad * that the major number is still valid when dereferenced.
464 1.2 gehenna */
465 1.24 drochner devmajor_t
466 1.2 gehenna devsw_name2blk(const char *name, char *devname, size_t devnamelen)
467 1.2 gehenna {
468 1.2 gehenna struct devsw_conv *conv;
469 1.24 drochner devmajor_t bmajor;
470 1.24 drochner int i;
471 1.2 gehenna
472 1.2 gehenna if (name == NULL)
473 1.24 drochner return (NODEVMAJOR);
474 1.2 gehenna
475 1.23 pooka mutex_enter(&device_lock);
476 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
477 1.5 mrg size_t len;
478 1.5 mrg
479 1.2 gehenna conv = &devsw_conv[i];
480 1.2 gehenna if (conv->d_name == NULL)
481 1.2 gehenna continue;
482 1.5 mrg len = strlen(conv->d_name);
483 1.5 mrg if (strncmp(conv->d_name, name, len) != 0)
484 1.5 mrg continue;
485 1.5 mrg if (*(name +len) && !isdigit(*(name + len)))
486 1.2 gehenna continue;
487 1.2 gehenna bmajor = conv->d_bmajor;
488 1.2 gehenna if (bmajor < 0 || bmajor >= max_bdevsws ||
489 1.2 gehenna bdevsw[bmajor] == NULL)
490 1.5 mrg break;
491 1.2 gehenna if (devname != NULL) {
492 1.2 gehenna #ifdef DEVSW_DEBUG
493 1.2 gehenna if (strlen(conv->d_name) >= devnamelen)
494 1.2 gehenna printf("devsw_name2blk: too short buffer");
495 1.2 gehenna #endif /* DEVSW_DEBUG */
496 1.4 tsutsui strncpy(devname, conv->d_name, devnamelen);
497 1.2 gehenna devname[devnamelen - 1] = '\0';
498 1.2 gehenna }
499 1.23 pooka mutex_exit(&device_lock);
500 1.2 gehenna return (bmajor);
501 1.2 gehenna }
502 1.2 gehenna
503 1.23 pooka mutex_exit(&device_lock);
504 1.24 drochner return (NODEVMAJOR);
505 1.2 gehenna }
506 1.2 gehenna
507 1.2 gehenna /*
508 1.16 plunky * Convert from device name to char major number.
509 1.16 plunky *
510 1.16 plunky * => Caller must ensure that the device is not detached, and therefore
511 1.16 plunky * that the major number is still valid when dereferenced.
512 1.16 plunky */
513 1.24 drochner devmajor_t
514 1.16 plunky devsw_name2chr(const char *name, char *devname, size_t devnamelen)
515 1.16 plunky {
516 1.16 plunky struct devsw_conv *conv;
517 1.24 drochner devmajor_t cmajor;
518 1.24 drochner int i;
519 1.16 plunky
520 1.16 plunky if (name == NULL)
521 1.24 drochner return (NODEVMAJOR);
522 1.16 plunky
523 1.23 pooka mutex_enter(&device_lock);
524 1.16 plunky for (i = 0 ; i < max_devsw_convs ; i++) {
525 1.16 plunky size_t len;
526 1.16 plunky
527 1.16 plunky conv = &devsw_conv[i];
528 1.16 plunky if (conv->d_name == NULL)
529 1.16 plunky continue;
530 1.16 plunky len = strlen(conv->d_name);
531 1.16 plunky if (strncmp(conv->d_name, name, len) != 0)
532 1.16 plunky continue;
533 1.16 plunky if (*(name +len) && !isdigit(*(name + len)))
534 1.16 plunky continue;
535 1.16 plunky cmajor = conv->d_cmajor;
536 1.16 plunky if (cmajor < 0 || cmajor >= max_cdevsws ||
537 1.16 plunky cdevsw[cmajor] == NULL)
538 1.16 plunky break;
539 1.16 plunky if (devname != NULL) {
540 1.16 plunky #ifdef DEVSW_DEBUG
541 1.16 plunky if (strlen(conv->d_name) >= devnamelen)
542 1.16 plunky printf("devsw_name2chr: too short buffer");
543 1.16 plunky #endif /* DEVSW_DEBUG */
544 1.16 plunky strncpy(devname, conv->d_name, devnamelen);
545 1.16 plunky devname[devnamelen - 1] = '\0';
546 1.16 plunky }
547 1.23 pooka mutex_exit(&device_lock);
548 1.16 plunky return (cmajor);
549 1.16 plunky }
550 1.16 plunky
551 1.23 pooka mutex_exit(&device_lock);
552 1.24 drochner return (NODEVMAJOR);
553 1.16 plunky }
554 1.16 plunky
555 1.16 plunky /*
556 1.2 gehenna * Convert from character dev_t to block dev_t.
557 1.11 ad *
558 1.11 ad * => Caller must ensure that the device is not detached, and therefore
559 1.11 ad * that the major number is still valid when dereferenced.
560 1.2 gehenna */
561 1.2 gehenna dev_t
562 1.2 gehenna devsw_chr2blk(dev_t cdev)
563 1.2 gehenna {
564 1.24 drochner devmajor_t bmajor, cmajor;
565 1.24 drochner int i;
566 1.11 ad dev_t rv;
567 1.2 gehenna
568 1.2 gehenna cmajor = major(cdev);
569 1.24 drochner bmajor = NODEVMAJOR;
570 1.11 ad rv = NODEV;
571 1.2 gehenna
572 1.23 pooka mutex_enter(&device_lock);
573 1.11 ad if (cmajor < 0 || cmajor >= max_cdevsws || cdevsw[cmajor] == NULL) {
574 1.23 pooka mutex_exit(&device_lock);
575 1.11 ad return (NODEV);
576 1.11 ad }
577 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
578 1.11 ad if (devsw_conv[i].d_cmajor == cmajor) {
579 1.11 ad bmajor = devsw_conv[i].d_bmajor;
580 1.11 ad break;
581 1.11 ad }
582 1.2 gehenna }
583 1.11 ad if (bmajor >= 0 && bmajor < max_bdevsws && bdevsw[bmajor] != NULL)
584 1.11 ad rv = makedev(bmajor, minor(cdev));
585 1.23 pooka mutex_exit(&device_lock);
586 1.2 gehenna
587 1.11 ad return (rv);
588 1.2 gehenna }
589 1.2 gehenna
590 1.2 gehenna /*
591 1.2 gehenna * Convert from block dev_t to character dev_t.
592 1.11 ad *
593 1.11 ad * => Caller must ensure that the device is not detached, and therefore
594 1.11 ad * that the major number is still valid when dereferenced.
595 1.2 gehenna */
596 1.2 gehenna dev_t
597 1.2 gehenna devsw_blk2chr(dev_t bdev)
598 1.2 gehenna {
599 1.24 drochner devmajor_t bmajor, cmajor;
600 1.24 drochner int i;
601 1.11 ad dev_t rv;
602 1.2 gehenna
603 1.11 ad bmajor = major(bdev);
604 1.24 drochner cmajor = NODEVMAJOR;
605 1.11 ad rv = NODEV;
606 1.11 ad
607 1.23 pooka mutex_enter(&device_lock);
608 1.11 ad if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
609 1.23 pooka mutex_exit(&device_lock);
610 1.2 gehenna return (NODEV);
611 1.11 ad }
612 1.11 ad for (i = 0 ; i < max_devsw_convs ; i++) {
613 1.11 ad if (devsw_conv[i].d_bmajor == bmajor) {
614 1.11 ad cmajor = devsw_conv[i].d_cmajor;
615 1.11 ad break;
616 1.11 ad }
617 1.11 ad }
618 1.11 ad if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
619 1.11 ad rv = makedev(cmajor, minor(bdev));
620 1.23 pooka mutex_exit(&device_lock);
621 1.2 gehenna
622 1.11 ad return (rv);
623 1.11 ad }
624 1.11 ad
625 1.11 ad /*
626 1.11 ad * Device access methods.
627 1.11 ad */
628 1.11 ad
629 1.11 ad #define DEV_LOCK(d) \
630 1.17 ad if ((mpflag = (d->d_flag & D_MPSAFE)) == 0) { \
631 1.17 ad KERNEL_LOCK(1, NULL); \
632 1.11 ad }
633 1.2 gehenna
634 1.11 ad #define DEV_UNLOCK(d) \
635 1.17 ad if (mpflag == 0) { \
636 1.17 ad KERNEL_UNLOCK_ONE(NULL); \
637 1.2 gehenna }
638 1.2 gehenna
639 1.11 ad int
640 1.11 ad bdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
641 1.11 ad {
642 1.11 ad const struct bdevsw *d;
643 1.17 ad int rv, mpflag;
644 1.11 ad
645 1.11 ad /*
646 1.11 ad * For open we need to lock, in order to synchronize
647 1.11 ad * with attach/detach.
648 1.11 ad */
649 1.23 pooka mutex_enter(&device_lock);
650 1.11 ad d = bdevsw_lookup(dev);
651 1.23 pooka mutex_exit(&device_lock);
652 1.11 ad if (d == NULL)
653 1.11 ad return ENXIO;
654 1.11 ad
655 1.11 ad DEV_LOCK(d);
656 1.11 ad rv = (*d->d_open)(dev, flag, devtype, l);
657 1.11 ad DEV_UNLOCK(d);
658 1.11 ad
659 1.11 ad return rv;
660 1.11 ad }
661 1.11 ad
662 1.11 ad int
663 1.11 ad bdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
664 1.11 ad {
665 1.11 ad const struct bdevsw *d;
666 1.17 ad int rv, mpflag;
667 1.11 ad
668 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
669 1.11 ad return ENXIO;
670 1.11 ad
671 1.11 ad DEV_LOCK(d);
672 1.11 ad rv = (*d->d_close)(dev, flag, devtype, l);
673 1.11 ad DEV_UNLOCK(d);
674 1.11 ad
675 1.11 ad return rv;
676 1.11 ad }
677 1.11 ad
678 1.11 ad void
679 1.11 ad bdev_strategy(struct buf *bp)
680 1.11 ad {
681 1.11 ad const struct bdevsw *d;
682 1.17 ad int mpflag;
683 1.11 ad
684 1.11 ad if ((d = bdevsw_lookup(bp->b_dev)) == NULL)
685 1.11 ad panic("bdev_strategy");
686 1.11 ad
687 1.11 ad DEV_LOCK(d);
688 1.11 ad (*d->d_strategy)(bp);
689 1.11 ad DEV_UNLOCK(d);
690 1.11 ad }
691 1.11 ad
692 1.11 ad int
693 1.11 ad bdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, 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 if ((d = bdevsw_lookup(dev)) == NULL)
699 1.11 ad return ENXIO;
700 1.11 ad
701 1.11 ad DEV_LOCK(d);
702 1.11 ad rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
703 1.11 ad DEV_UNLOCK(d);
704 1.11 ad
705 1.11 ad return rv;
706 1.11 ad }
707 1.11 ad
708 1.11 ad int
709 1.11 ad bdev_dump(dev_t dev, daddr_t addr, void *data, size_t sz)
710 1.11 ad {
711 1.11 ad const struct bdevsw *d;
712 1.11 ad int rv;
713 1.11 ad
714 1.11 ad /*
715 1.11 ad * Dump can be called without the device open. Since it can
716 1.11 ad * currently only be called with the system paused (and in a
717 1.11 ad * potentially unstable state), we don't perform any locking.
718 1.11 ad */
719 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
720 1.11 ad return ENXIO;
721 1.11 ad
722 1.11 ad /* DEV_LOCK(d); */
723 1.11 ad rv = (*d->d_dump)(dev, addr, data, sz);
724 1.11 ad /* DEV_UNLOCK(d); */
725 1.11 ad
726 1.11 ad return rv;
727 1.11 ad }
728 1.11 ad
729 1.11 ad int
730 1.11 ad bdev_type(dev_t dev)
731 1.11 ad {
732 1.11 ad const struct bdevsw *d;
733 1.11 ad
734 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
735 1.11 ad return D_OTHER;
736 1.11 ad return d->d_flag & D_TYPEMASK;
737 1.11 ad }
738 1.11 ad
739 1.11 ad int
740 1.11 ad cdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
741 1.11 ad {
742 1.11 ad const struct cdevsw *d;
743 1.17 ad int rv, mpflag;
744 1.11 ad
745 1.11 ad /*
746 1.11 ad * For open we need to lock, in order to synchronize
747 1.11 ad * with attach/detach.
748 1.11 ad */
749 1.23 pooka mutex_enter(&device_lock);
750 1.11 ad d = cdevsw_lookup(dev);
751 1.23 pooka mutex_exit(&device_lock);
752 1.11 ad if (d == NULL)
753 1.11 ad return ENXIO;
754 1.11 ad
755 1.11 ad DEV_LOCK(d);
756 1.11 ad rv = (*d->d_open)(dev, flag, devtype, l);
757 1.11 ad DEV_UNLOCK(d);
758 1.11 ad
759 1.11 ad return rv;
760 1.11 ad }
761 1.11 ad
762 1.11 ad int
763 1.11 ad cdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
764 1.11 ad {
765 1.11 ad const struct cdevsw *d;
766 1.17 ad int rv, mpflag;
767 1.11 ad
768 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
769 1.11 ad return ENXIO;
770 1.11 ad
771 1.11 ad DEV_LOCK(d);
772 1.11 ad rv = (*d->d_close)(dev, flag, devtype, l);
773 1.11 ad DEV_UNLOCK(d);
774 1.11 ad
775 1.11 ad return rv;
776 1.11 ad }
777 1.11 ad
778 1.11 ad int
779 1.11 ad cdev_read(dev_t dev, struct uio *uio, int flag)
780 1.11 ad {
781 1.11 ad const struct cdevsw *d;
782 1.17 ad int rv, mpflag;
783 1.11 ad
784 1.11 ad if ((d = cdevsw_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_read)(dev, uio, flag);
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.11 ad cdev_write(dev_t dev, struct uio *uio, int flag)
796 1.11 ad {
797 1.11 ad const struct cdevsw *d;
798 1.17 ad int rv, mpflag;
799 1.11 ad
800 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
801 1.11 ad return ENXIO;
802 1.11 ad
803 1.11 ad DEV_LOCK(d);
804 1.11 ad rv = (*d->d_write)(dev, uio, flag);
805 1.11 ad DEV_UNLOCK(d);
806 1.11 ad
807 1.11 ad return rv;
808 1.11 ad }
809 1.11 ad
810 1.11 ad int
811 1.11 ad cdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
812 1.11 ad {
813 1.11 ad const struct cdevsw *d;
814 1.17 ad int rv, mpflag;
815 1.11 ad
816 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
817 1.11 ad return ENXIO;
818 1.11 ad
819 1.11 ad DEV_LOCK(d);
820 1.11 ad rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
821 1.11 ad DEV_UNLOCK(d);
822 1.11 ad
823 1.11 ad return rv;
824 1.11 ad }
825 1.11 ad
826 1.11 ad void
827 1.11 ad cdev_stop(struct tty *tp, int flag)
828 1.11 ad {
829 1.11 ad const struct cdevsw *d;
830 1.17 ad int mpflag;
831 1.11 ad
832 1.11 ad if ((d = cdevsw_lookup(tp->t_dev)) == NULL)
833 1.11 ad return;
834 1.11 ad
835 1.11 ad DEV_LOCK(d);
836 1.11 ad (*d->d_stop)(tp, flag);
837 1.11 ad DEV_UNLOCK(d);
838 1.11 ad }
839 1.11 ad
840 1.11 ad struct tty *
841 1.11 ad cdev_tty(dev_t dev)
842 1.11 ad {
843 1.11 ad const struct cdevsw *d;
844 1.11 ad
845 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
846 1.11 ad return NULL;
847 1.11 ad
848 1.12 ad /* XXX Check if necessary. */
849 1.12 ad if (d->d_tty == NULL)
850 1.12 ad return NULL;
851 1.12 ad
852 1.21 ad return (*d->d_tty)(dev);
853 1.11 ad }
854 1.11 ad
855 1.11 ad int
856 1.11 ad cdev_poll(dev_t dev, int flag, lwp_t *l)
857 1.11 ad {
858 1.11 ad const struct cdevsw *d;
859 1.17 ad int rv, mpflag;
860 1.11 ad
861 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
862 1.11 ad return POLLERR;
863 1.11 ad
864 1.11 ad DEV_LOCK(d);
865 1.11 ad rv = (*d->d_poll)(dev, flag, l);
866 1.11 ad DEV_UNLOCK(d);
867 1.11 ad
868 1.11 ad return rv;
869 1.11 ad }
870 1.11 ad
871 1.11 ad paddr_t
872 1.11 ad cdev_mmap(dev_t dev, off_t off, int flag)
873 1.11 ad {
874 1.11 ad const struct cdevsw *d;
875 1.11 ad paddr_t rv;
876 1.17 ad int mpflag;
877 1.11 ad
878 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
879 1.11 ad return (paddr_t)-1LL;
880 1.11 ad
881 1.11 ad DEV_LOCK(d);
882 1.11 ad rv = (*d->d_mmap)(dev, off, flag);
883 1.11 ad DEV_UNLOCK(d);
884 1.11 ad
885 1.11 ad return rv;
886 1.11 ad }
887 1.11 ad
888 1.11 ad int
889 1.11 ad cdev_kqfilter(dev_t dev, struct knote *kn)
890 1.11 ad {
891 1.11 ad const struct cdevsw *d;
892 1.17 ad int rv, mpflag;
893 1.11 ad
894 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
895 1.11 ad return ENXIO;
896 1.11 ad
897 1.11 ad DEV_LOCK(d);
898 1.11 ad rv = (*d->d_kqfilter)(dev, kn);
899 1.11 ad DEV_UNLOCK(d);
900 1.11 ad
901 1.11 ad return rv;
902 1.11 ad }
903 1.11 ad
904 1.11 ad int
905 1.11 ad cdev_type(dev_t dev)
906 1.11 ad {
907 1.11 ad const struct cdevsw *d;
908 1.11 ad
909 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
910 1.11 ad return D_OTHER;
911 1.11 ad return d->d_flag & D_TYPEMASK;
912 1.2 gehenna }
913