1 /*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * All advertising materials mentioning features or use of this software
10 * must display the following acknowledgement:
11 * This product includes software developed by the University of
12 * California, Lawrence Berkeley Laboratory.
13 *
14 * %sccs.include.redist.c%
15 *
16 * @(#)conf.c 8.4 (Berkeley) 11/21/94
17 *
18 * from: $Header: conf.c,v 1.17 93/10/31 05:37:39 torek Exp $ (LBL)
19 */
20
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/buf.h>
24 #include <sys/ioctl.h>
25 #include <sys/vnode.h>
26 #include <sys/tty.h>
27 #include <sys/conf.h>
28
29 int rawread __P((dev_t, struct uio *, int));
30 int rawwrite __P((dev_t, struct uio *, int));
31 int swstrategy __P((struct buf *));
32 int ttselect __P((dev_t, int, struct proc *));
33
34 #define dev_type_open(n) int n __P((dev_t, int, int, struct proc *))
35 #define dev_type_close(n) int n __P((dev_t, int, int, struct proc *))
36 #define dev_type_strategy(n) int n __P((struct buf *))
37 #define dev_type_ioctl(n) \
38 int n __P((dev_t, int, caddr_t, int, struct proc *))
39
40 /* bdevsw-specific types */
41 /* dev_type_dump(n) int n __P((dev_t, daddr_t, caddr_t, int))*/
42 #define dev_type_dump(n) int n ()
43 #define dev_type_size(n) int n __P((dev_t))
44
45 /* error/nullop functions */
46 #define error_open ((dev_type_open((*))) enodev)
47 #define error_close ((dev_type_close((*))) enodev)
48 #define error_ioctl ((dev_type_ioctl((*))) enodev)
49 #define error_dump ((dev_type_dump((*))) enodev)
50
51 #define null_open ((dev_type_open((*))) nullop)
52 #define null_close ((dev_type_close((*))) nullop)
53
54 #define dev_decl(n,t) __CONCAT(dev_type_,t)(__CONCAT(n,t))
55 #define dev_init(c,n,t) \
56 (c > 0 ? __CONCAT(n,t) : (__CONCAT(dev_type_,t)((*))) enxio)
57
58 /* bdevsw-specific initializations */
59 #define dev_size_init(c,n) (c > 0 ? __CONCAT(n,size) : 0)
60
61 #define bdev_decl(n) \
62 dev_decl(n,open); dev_decl(n,close); dev_decl(n,strategy); \
63 dev_decl(n,ioctl); dev_decl(n,dump); dev_decl(n,size)
64
65 #define bdev_disk_init(c,n) { \
66 dev_init(c,n,open), dev_init(c,n,close), \
67 dev_init(c,n,strategy), dev_init(c,n,ioctl), \
68 dev_init(c,n,dump), dev_size_init(c,n), 0 }
69
70 #define bdev_tape_init(c,n) { \
71 dev_init(c,n,open), dev_init(c,n,close), \
72 dev_init(c,n,strategy), dev_init(c,n,ioctl), \
73 dev_init(c,n,dump), 0, B_TAPE }
74
75 #define bdev_swap_init() { \
76 error_open, error_close, swstrategy, error_ioctl, error_dump, 0, 0 }
77
78 #define bdev_notdef() bdev_tape_init(0,no)
79 bdev_decl(no); /* dummy declarations */
80
81 #include "sd.h"
82
83 bdev_decl(sd);
84
85 struct bdevsw bdevsw[] =
86 {
87 bdev_notdef(), /* 0 */
88 bdev_notdef(), /* 1 */
89 bdev_notdef(), /* 2 */
90 bdev_swap_init(), /* 3 */
91 bdev_notdef(), /* 4 */
92 bdev_notdef(), /* 5 */
93 bdev_notdef(), /* 6 */
94 bdev_disk_init(NSD,sd), /* 7: scsi disk */
95 };
96
97 int nblkdev = sizeof (bdevsw) / sizeof (bdevsw[0]);
98
99 /* cdevsw-specific types */
100 #define dev_type_read(n) int n __P((dev_t, struct uio *, int))
101 #define dev_type_write(n) int n __P((dev_t, struct uio *, int))
102 #define dev_type_select(n) int n __P((dev_t, int, struct proc *))
103 #define dev_type_map(n) int n __P(())
104
105 #define error_read ((dev_type_read((*))) enodev)
106 #define error_write ((dev_type_write((*))) enodev)
107 #define error_select ((dev_type_select((*))) enodev)
108
109 #define cdev_decl(n) \
110 dev_decl(n,open); dev_decl(n,close); dev_decl(n,read); \
111 dev_decl(n,write); dev_decl(n,ioctl); dev_decl(n,select); \
112 dev_decl(n,map); dev_decl(n,strategy); \
113 extern struct tty __CONCAT(n,_tty)[]
114
115 #define dev_tty_init(c,n) (c > 0 ? __CONCAT(n,_tty) : 0)
116
117 /* open, close, read, write, ioctl, strategy */
118 #define cdev_disk_init(c,n) { \
119 dev_init(c,n,open), dev_init(c,n,close), rawread, rawwrite, \
120 dev_init(c,n,ioctl), 0, 0, 0, seltrue, 0, dev_init(c,n,strategy) }
121
122 /* open, close, read, write, ioctl, strategy */
123 #define cdev_tape_init(c,n) { \
124 dev_init(c,n,open), dev_init(c,n,close), dev_init(c,n,read), \
125 dev_init(c,n,write), dev_init(c,n,ioctl), 0, 0, 0, seltrue, 0, \
126 dev_init(c,n,strategy) }
127
128 /* open, close, read, write, ioctl, tty */
129 #define cdev_tty_init(c,n) { \
130 dev_init(c,n,open), dev_init(c,n,close), dev_init(c,n,read), \
131 dev_init(c,n,write), dev_init(c,n,ioctl), 0, 0, dev_tty_init(c,n), \
132 ttselect, 0, 0 }
133
134 /* open, close, read, write, ioctl, select */
135 #define cdev_gen_init(c,n) { \
136 dev_init(c,n,open), dev_init(c,n,close), dev_init(c,n,read), \
137 dev_init(c,n,write), dev_init(c,n,ioctl), 0, 0, 0, \
138 dev_init(c,n,select), 0, 0 }
139
140 /* open, close, ioctl, mmap */
141 #define cdev_fb_init(c,n) { \
142 dev_init(c,n,open), dev_init(c,n,close), error_read, error_write, \
143 dev_init(c,n,ioctl), 0, 0, 0, seltrue, dev_init(c,n,map), 0 }
144
145 #define cdev_notdef() { \
146 error_open, error_close, error_read, error_write, \
147 error_ioctl, 0, 0, 0, seltrue, 0, 0 }
148
149 cdev_decl(no); /* dummy declarations */
150
151 cdev_decl(cn);
152 /* open, close, read, write, ioctl, select -- XXX should be a tty */
153 extern struct tty cons;
154 #define cdev_cn_init(c,n) { \
155 dev_init(c,n,open), dev_init(c,n,close), dev_init(c,n,read), \
156 dev_init(c,n,write), dev_init(c,n,ioctl), 0, 0, &cons, \
157 dev_init(c,n,select), 0, 0 }
158
159 cdev_decl(ctty);
160 /* open, read, write, ioctl, select -- XXX should be a tty */
161 #define cdev_ctty_init(c,n) { \
162 dev_init(c,n,open), null_close, dev_init(c,n,read), \
163 dev_init(c,n,write), dev_init(c,n,ioctl), 0, 0, 0, \
164 dev_init(c,n,select), 0, 0 }
165
166 dev_type_read(mmrw);
167 /* read/write */
168 #define cdev_mm_init(c,n) { \
169 null_open, null_close, mmrw, mmrw, error_ioctl, 0, 0, 0, \
170 seltrue, 0, 0 }
171
172 /* read, write, strategy */
173 #define cdev_swap_init(c,n) { \
174 null_open, null_close, rawread, rawwrite, error_ioctl, 0, 0, 0, \
175 seltrue, 0, dev_init(c,n,strategy) }
176
177 #include "pty.h"
178 #define pts_tty pt_tty
179 #define ptsioctl ptyioctl
180 cdev_decl(pts);
181 #define ptc_tty pt_tty
182 #define ptcioctl ptyioctl
183 cdev_decl(ptc);
184
185 /* open, close, read, write, ioctl, tty, select */
186 #define cdev_ptc_init(c,n) { \
187 dev_init(c,n,open), dev_init(c,n,close), dev_init(c,n,read), \
188 dev_init(c,n,write), dev_init(c,n,ioctl), 0, 0, dev_tty_init(c,n), \
189 dev_init(c,n,select), 0, 0 }
190
191 cdev_decl(log);
192 /* open, close, read, ioctl, select -- XXX should be a generic device */
193 #define cdev_log_init(c,n) { \
194 dev_init(c,n,open), dev_init(c,n,close), dev_init(c,n,read), \
195 error_write, dev_init(c,n,ioctl), 0, 0, 0, dev_init(c,n,select), 0, 0 }
196
197 dev_type_open(fdopen);
198 /* open */
199 #define cdev_fd_init(c,n) { \
200 dev_init(c,n,open), error_close, error_read, error_write, \
201 error_ioctl, 0, 0, 0, error_select, 0, 0 }
202
203 #include "zs.h"
204 cdev_decl(zs);
205
206 cdev_decl(kbd);
207 cdev_decl(ms);
208 cdev_decl(fb);
209
210 #include "bwtwo.h"
211 cdev_decl(bwtwo);
212
213 #include "cgthree.h"
214 cdev_decl(cgthree);
215
216 #include "cgsix.h"
217 cdev_decl(cgsix);
218
219 #include "bsdaudio.h"
220 cdev_decl(audio);
221
222 cdev_decl(openprom);
223 /* open, close, ioctl */
224 #define cdev_openprom_init(c,n) { \
225 dev_init(c,n,open), dev_init(c,n,close), error_read, error_write, \
226 dev_init(c,n,ioctl), 0, 0, 0, error_select, 0, 0 }
227
228 #include "bpfilter.h"
229 cdev_decl(bpf);
230 /* open, close, read, write, ioctl, select -- XXX should be generic device */
231 #define cdev_bpf_init(c,n) { \
232 dev_init(c,n,open), dev_init(c,n,close), dev_init(c,n,read), \
233 dev_init(c,n,write), dev_init(c,n,ioctl), 0, 0, 0, \
234 dev_init(c,n,select), 0, 0 }
235
236 /* prototype sun-equivalent cdevsw[] */
237 struct cdevsw cdevsw[] =
238 {
239 cdev_cn_init(1,cn), /* 0: virtual console */
240 cdev_notdef(), /* 1 */
241 cdev_ctty_init(1,ctty), /* 2: controlling terminal */
242 cdev_mm_init(1,mm), /* 3: /dev/{null,mem,kmem,...} */
243 cdev_notdef(), /* 4 */
244 cdev_notdef(), /* 5 */
245 cdev_notdef(), /* 6 */
246 cdev_swap_init(1,sw), /* 7: /dev/drum (swap pseudo-device) */
247 cdev_notdef(), /* 8 */
248 cdev_notdef(), /* 9 */
249 cdev_notdef(), /* 10 */
250 cdev_notdef(), /* 11 */
251 cdev_tty_init(NZS,zs), /* 12: zs serial */
252 cdev_gen_init(1,ms), /* 13: /dev/mouse */
253 cdev_notdef(), /* 14 */
254 cdev_notdef(), /* 15: sun /dev/winNNN */
255 cdev_log_init(1,log), /* 16: /dev/klog */
256 cdev_disk_init(NSD,sd), /* 17: scsi disk */
257 cdev_notdef(), /* 18: should be scsi tape */
258 cdev_notdef(), /* 19 */
259 cdev_ptc_init(NPTY,ptc), /* 20: pseudo-tty master */
260 cdev_tty_init(NPTY,pts), /* 21: pseudo-tty slave */
261 cdev_fb_init(1,fb), /* 22: /dev/fb indirect driver */
262 cdev_notdef(), /* 23 */
263 cdev_fd_init(1,fd), /* 24: /dev/std{in,out,err} */
264 cdev_notdef(), /* 25 */
265 cdev_notdef(), /* 26 */
266 cdev_fb_init(NBWTWO,bwtwo), /* 27: /dev/bwtwo */
267 cdev_notdef(), /* 28 */
268 cdev_gen_init(1,kbd), /* 29: /dev/kbd */
269 cdev_notdef(), /* 30 */
270 cdev_notdef(), /* 31: should be /dev/cgtwo */
271 cdev_notdef(), /* 32: should be /dev/gpone */
272 cdev_notdef(), /* 33 */
273 cdev_notdef(), /* 34 */
274 cdev_notdef(), /* 35 */
275 cdev_notdef(), /* 36 */
276 cdev_notdef(), /* 37 */
277 cdev_notdef(), /* 38 */
278 cdev_notdef(), /* 39 */
279 cdev_notdef(), /* 40 */
280 cdev_notdef(), /* 41 */
281 cdev_notdef(), /* 42 */
282 cdev_notdef(), /* 43 */
283 cdev_notdef(), /* 44 */
284 cdev_notdef(), /* 45 */
285 cdev_notdef(), /* 46 */
286 cdev_notdef(), /* 47 */
287 cdev_notdef(), /* 48 */
288 cdev_notdef(), /* 49 */
289 cdev_notdef(), /* 50 */
290 cdev_notdef(), /* 51 */
291 cdev_notdef(), /* 52 */
292 cdev_notdef(), /* 53 */
293 cdev_notdef(), /* 54 */
294 cdev_fb_init(NCGTHREE,cgthree), /* 55: /dev/cgthree */
295 cdev_notdef(), /* 56 */
296 cdev_notdef(), /* 57 */
297 cdev_notdef(), /* 58 */
298 cdev_notdef(), /* 59 */
299 cdev_notdef(), /* 60 */
300 cdev_notdef(), /* 61 */
301 cdev_notdef(), /* 62 */
302 cdev_notdef(), /* 63 */
303 cdev_notdef(), /* 64 */
304 cdev_notdef(), /* 65 */
305 cdev_notdef(), /* 66 */
306 cdev_fb_init(NCGSIX,cgsix), /* 67: /dev/cgsix */
307 cdev_notdef(), /* 68 */
308 cdev_gen_init(NBSDAUDIO,audio), /* 69: /dev/audio */
309 cdev_openprom_init(1,openprom), /* 70: /dev/openprom */
310 cdev_notdef(), /* 71 */
311 cdev_notdef(), /* 72 */
312 cdev_notdef(), /* 73 */
313 cdev_notdef(), /* 74 */
314 cdev_notdef(), /* 75 */
315 cdev_notdef(), /* 76 */
316 cdev_notdef(), /* 77 */
317 cdev_notdef(), /* 78 */
318 cdev_notdef(), /* 79 */
319 cdev_notdef(), /* 80 */
320 cdev_notdef(), /* 81 */
321 cdev_notdef(), /* 82 */
322 cdev_notdef(), /* 83 */
323 cdev_notdef(), /* 84 */
324 cdev_notdef(), /* 85 */
325 cdev_notdef(), /* 86 */
326 cdev_notdef(), /* 87 */
327 cdev_notdef(), /* 88 */
328 cdev_notdef(), /* 89 */
329 cdev_notdef(), /* 90 */
330 cdev_notdef(), /* 91 */
331 cdev_notdef(), /* 92 */
332 cdev_notdef(), /* 93 */
333 cdev_notdef(), /* 94 */
334 cdev_notdef(), /* 95 */
335 cdev_notdef(), /* 96 */
336 cdev_notdef(), /* 97 */
337 cdev_notdef(), /* 98 */
338 cdev_notdef(), /* 99 */
339 cdev_notdef(), /* 100 */
340 cdev_notdef(), /* 101 */
341 cdev_notdef(), /* 102 */
342 cdev_notdef(), /* 103 */
343 cdev_notdef(), /* 104 */
344 cdev_bpf_init(NBPFILTER,bpf), /* 105: packet filter */
345 cdev_notdef(), /* 106 */
346 cdev_notdef(), /* 107 */
347 cdev_notdef(), /* 108 */
348 cdev_notdef(), /* 109 */
349 };
350
351 int nchrdev = sizeof (cdevsw) / sizeof (cdevsw[0]);
352
353 /*
354 * Swapdev is a fake device implemented
355 * in sw.c used only internally to get to swstrategy.
356 * It cannot be provided to the users, because the
357 * swstrategy routine munches the b_dev and b_blkno entries
358 * before calling the appropriate driver. This would horribly
359 * confuse, e.g. the hashing routines. Instead, /dev/drum is
360 * provided as a character (raw) device.
361 */
362 dev_t swapdev = makedev(3, 0);
363
364 /*
365 * Routine that identifies /dev/mem and /dev/kmem.
366 *
367 * A minimal stub routine can always return 0.
368 */
iskmemdev(dev)369 iskmemdev(dev)
370 dev_t dev;
371 {
372
373 return (major(dev) == 3 && minor(dev) < 2);
374 }
375
iszerodev(dev)376 iszerodev(dev)
377 dev_t dev;
378 {
379 return (major(dev) == 3 && minor(dev) == 12);
380 }
381
382 /*
383 * Routine to determine if a device is a tty.
384 *
385 * A minimal stub routine can always return 0.
386 */
istty(dev)387 istty(dev)
388 dev_t dev;
389 {
390
391 switch (major(dev)) {
392 case 0:
393 case 2:
394 case 12:
395 case 20:
396 case 21:
397 case 29:
398 return (1);
399 default:
400 return (0);
401 }
402 }
403
404 /*
405 * Routine to determine if a device is a disk.
406 *
407 * A minimal stub routine can always return 0.
408 */
isdisk(dev,type)409 isdisk(dev, type)
410 dev_t dev;
411 int type;
412 {
413
414 #ifdef notyet
415 /* someday, something like this, perhaps */
416 dev = devtab[major(dev)];
417 return (dev != NULL && dev->dv_class == DV_DISK);
418 #else
419 switch (major(dev)) {
420 case 7:
421 if (type == VBLK)
422 return (1);
423 return (0);
424 case 17:
425 if (type == VCHR)
426 return (1);
427 /* fall through */
428
429 default:
430 return (0);
431 }
432 #endif
433 }
434
435 /*
436 * Routine to convert from character to block device number.
437 *
438 * A minimal stub routine can always return NODEV.
439 */
chrtoblk(dev)440 chrtoblk(dev)
441 dev_t dev;
442 {
443
444 if (major(dev) != 17)
445 return (NODEV);
446 return (makedev(7, minor(dev)));
447 }
448