]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/i386/conf.c
xnu-1228.tar.gz
[apple/xnu.git] / bsd / dev / i386 / conf.c
1 /*
2 * Copyright (c) 1997-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1993 NeXT Computer, Inc.
30 *
31 * UNIX Device switch tables.
32 *
33 * HISTORY
34 *
35 * 30 July 1997 Umesh Vaishampayan (umeshv@apple.com)
36 * enabled file descriptor pseudo-device.
37 * 18 June 1993 ? at NeXT
38 * Cleaned up a lot of stuff in this file.
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/ioctl.h>
44 #include <sys/tty.h>
45 #include <sys/conf.h>
46
47 /* Prototypes that should be elsewhere: */
48 extern int isdisk(dev_t dev, int type);
49 extern dev_t chrtoblk(dev_t dev);
50 extern int chrtoblk_set(int cdev, int bdev);
51 extern int iskmemdev(dev_t dev);
52
53 struct bdevsw bdevsw[] =
54 {
55 /*
56 * For block devices, every other block of 8 slots is
57 * reserved for Apple. The other slots are available for
58 * the user. This way we can both add new entries without
59 * running into each other. Be sure to fill in Apple's
60 * 8 reserved slots when you jump over us -- we'll do the
61 * same for you.
62 */
63
64 /* 0 - 7 are reserved for Apple */
65
66 NO_BDEVICE, /* 0*/
67 NO_BDEVICE, /* 1*/
68 NO_BDEVICE, /* 2*/
69 NO_BDEVICE, /* 3*/
70 NO_BDEVICE, /* 4*/
71 NO_BDEVICE, /* 5*/
72 NO_BDEVICE, /* 6*/
73 NO_BDEVICE, /* 7*/
74
75 /* 8 - 15 are reserved to the user */
76 NO_BDEVICE, /* 8*/
77 NO_BDEVICE, /* 9*/
78 NO_BDEVICE, /*10*/
79 NO_BDEVICE, /*11*/
80 NO_BDEVICE, /*12*/
81 NO_BDEVICE, /*13*/
82 NO_BDEVICE, /*14*/
83 NO_BDEVICE, /*15*/
84
85 /* 16 - 23 are reserved for Apple */
86 NO_BDEVICE, /*16*/
87 NO_BDEVICE, /*17*/
88 NO_BDEVICE, /*18*/
89 NO_BDEVICE, /*18*/
90 NO_BDEVICE, /*20*/
91 NO_BDEVICE, /*21*/
92 NO_BDEVICE, /*22*/
93 NO_BDEVICE, /*23*/
94 };
95
96 int nblkdev = sizeof (bdevsw) / sizeof (bdevsw[0]);
97
98 extern struct tty *km_tty[];
99 extern d_open_t cnopen;
100 extern d_close_t cnclose;
101 extern d_read_t cnread;
102 extern d_write_t cnwrite;
103 extern d_ioctl_t cnioctl;
104 extern d_select_t cnselect;
105 extern d_getc_t cngetc;
106 extern d_putc_t cnputc;
107 extern d_open_t kmopen;
108 extern d_close_t kmclose;
109 extern d_read_t kmread;
110 extern d_write_t kmwrite;
111 extern d_ioctl_t kmioctl;
112 extern d_getc_t kmgetc;
113 extern d_putc_t kmputc;
114 extern d_open_t sgopen;
115 extern d_close_t sgclose;
116 extern d_ioctl_t sgioctl;
117
118 #if NVOL > 0
119 extern d_open_t volopen;
120 extern d_close_t volclose;
121 extern d_ioctl_t volioctl;
122 #else
123 #define volopen eno_opcl
124 #define volclose eno_opcl
125 #define volioctl eno_ioctl
126 #endif
127
128 extern d_open_t cttyopen;
129 extern d_read_t cttyread;
130 extern d_write_t cttywrite;
131 extern d_ioctl_t cttyioctl;
132 extern d_select_t cttyselect;
133
134 extern d_read_t mmread;
135 extern d_write_t mmwrite;
136 extern d_ioctl_t mmioctl;
137 #define mmselect (select_fcn_t *)seltrue
138 #define mmmmap eno_mmap
139
140 #include <pty.h>
141 #if NPTY > 0
142 extern struct tty *pt_tty[];
143 extern d_open_t ptsopen;
144 extern d_close_t ptsclose;
145 extern d_read_t ptsread;
146 extern d_write_t ptswrite;
147 extern d_stop_t ptsstop;
148 extern d_putc_t ptsputc;
149 extern d_open_t ptcopen;
150 extern d_close_t ptcclose;
151 extern d_read_t ptcread;
152 extern d_write_t ptcwrite;
153 extern d_select_t ptcselect;
154 extern d_ioctl_t ptyioctl;
155 #else
156 #define ptsopen eno_opcl
157 #define ptsclose eno_opcl
158 #define ptsread eno_rdwrt
159 #define ptswrite eno_rdwrt
160 #define ptsstop nulldev
161 #define ptsputc nulldev
162
163 #define ptcopen eno_opcl
164 #define ptcclose eno_opcl
165 #define ptcread eno_rdwrt
166 #define ptcwrite eno_rdwrt
167 #define ptcselect eno_select
168 #define ptyioctl eno_ioctl
169 #endif
170
171 extern d_open_t logopen;
172 extern d_close_t logclose;
173 extern d_read_t logread;
174 extern d_ioctl_t logioctl;
175 extern d_select_t logselect;
176 extern d_open_t fdesc_open;
177 extern d_read_t fdesc_read;
178 extern d_write_t fdesc_write;
179 extern d_ioctl_t fdesc_ioctl;
180 extern d_select_t fdesc_select;
181
182 #define nullopen (d_open_t *)&nulldev
183 #define nullclose (d_close_t *)&nulldev
184 #define nullread (d_read_t *)&nulldev
185 #define nullwrite (d_write_t *)&nulldev
186 #define nullioctl (d_ioctl_t *)&nulldev
187 #define nullselect (d_select_t *)&nulldev
188 #define nullstop (d_stop_t *)&nulldev
189 #define nullreset (d_reset_t *)&nulldev
190
191 struct cdevsw cdevsw[] =
192 {
193 /*
194 * For character devices, every other block of 16 slots is
195 * reserved for Apple. The other slots are available for
196 * the user. This way we can both add new entries without
197 * running into each other. Be sure to fill in Apple's
198 * 16 reserved slots when you jump over us -- we'll do the
199 * same for you.
200 */
201
202 /* 0 - 15 are reserved for Apple */
203
204 {
205 cnopen, cnclose, cnread, cnwrite, /* 0*/
206 cnioctl, nullstop, nullreset, 0, cnselect,
207 eno_mmap, eno_strat, cngetc, cnputc, D_TTY
208 },
209 NO_CDEVICE, /* 1*/
210 {
211 cttyopen, nullclose, cttyread, cttywrite, /* 2*/
212 cttyioctl, nullstop, nullreset, 0, cttyselect,
213 eno_mmap, eno_strat, eno_getc, eno_putc, D_TTY
214 },
215 {
216 nullopen, nullclose, mmread, mmwrite, /* 3*/
217 mmioctl, nullstop, nullreset, 0, mmselect,
218 mmmmap, eno_strat, eno_getc, eno_putc, D_DISK
219 },
220 {
221 ptsopen, ptsclose, ptsread, ptswrite, /* 4*/
222 ptyioctl, ptsstop, nullreset, pt_tty, ttselect,
223 eno_mmap, eno_strat, eno_getc, eno_putc, D_TTY
224 },
225 {
226 ptcopen, ptcclose, ptcread, ptcwrite, /* 5*/
227 ptyioctl, nullstop, nullreset, 0, ptcselect,
228 eno_mmap, eno_strat, eno_getc, eno_putc, D_TTY
229 },
230 {
231 logopen, logclose, logread, eno_rdwrt, /* 6*/
232 logioctl, eno_stop, nullreset, 0, logselect,
233 eno_mmap, eno_strat, eno_getc, eno_putc, 0
234 },
235 NO_CDEVICE, /* 7*/
236 NO_CDEVICE, /* 8*/
237 NO_CDEVICE, /* 9*/
238 NO_CDEVICE, /*10*/
239 NO_CDEVICE, /*11*/
240 {
241 kmopen, kmclose, kmread, kmwrite, /*12*/
242 kmioctl, nullstop, nullreset, km_tty, ttselect,
243 eno_mmap, eno_strat, kmgetc, kmputc, 0
244 },
245 NO_CDEVICE, /*13*/
246 NO_CDEVICE, /*14*/
247 NO_CDEVICE, /*15*/
248
249 /* 16 - 31 are reserved to the user */
250 NO_CDEVICE, /*16*/
251 NO_CDEVICE, /*17*/
252 NO_CDEVICE, /*18*/
253 NO_CDEVICE, /*19*/
254 NO_CDEVICE, /*20*/
255 NO_CDEVICE, /*21*/
256 NO_CDEVICE, /*22*/
257 NO_CDEVICE, /*23*/
258 NO_CDEVICE, /*24*/
259 NO_CDEVICE, /*25*/
260 NO_CDEVICE, /*26*/
261 NO_CDEVICE, /*27*/
262 NO_CDEVICE, /*28*/
263 NO_CDEVICE, /*29*/
264 NO_CDEVICE, /*30*/
265 NO_CDEVICE, /*31*/
266
267 /* 32 - 47 are reserved to NeXT */
268 {
269 fdesc_open, eno_opcl, fdesc_read, fdesc_write, /*32*/
270 fdesc_ioctl, eno_stop, eno_reset, 0, fdesc_select,
271 eno_mmap, eno_strat, eno_getc, eno_putc, 0
272 },
273 #if 1
274 NO_CDEVICE,
275 #else
276 {
277 sgopen, sgclose, eno_rdwrt, eno_rdwrt, /*33*/
278 sgioctl, eno_stop, eno_reset, 0, eno_select,
279 eno_mmap, eno_strat, eno_getc, eno_putc, D_TAPE
280 },
281 #endif
282 NO_CDEVICE, /*34*/
283 NO_CDEVICE, /*35*/
284 NO_CDEVICE, /*36*/
285 NO_CDEVICE, /*37*/
286 NO_CDEVICE, /*38*/
287 NO_CDEVICE, /*39*/
288 NO_CDEVICE, /*40*/
289 NO_CDEVICE, /*41*/
290 {
291 volopen, volclose, eno_rdwrt, eno_rdwrt, /*42*/
292 volioctl, eno_stop, eno_reset, 0, (select_fcn_t *)seltrue,
293 eno_mmap, eno_strat, eno_getc, eno_putc, 0
294 },
295 };
296 int nchrdev = sizeof (cdevsw) / sizeof (cdevsw[0]);
297
298
299 #include <sys/vnode.h> /* for VCHR and VBLK */
300 /*
301 * return true if a disk
302 */
303 int
304 isdisk(dev_t dev, int type)
305 {
306 dev_t maj = major(dev);
307
308 switch (type) {
309 case VCHR:
310 maj = chrtoblk(maj);
311 if (maj == NODEV) {
312 break;
313 }
314 /* FALL THROUGH */
315 case VBLK:
316 if (bdevsw[maj].d_type == D_DISK) {
317 return (1);
318 }
319 break;
320 }
321 return(0);
322 }
323
324 static int chrtoblktab[] = {
325 /* CHR*/ /* BLK*/ /* CHR*/ /* BLK*/
326 /* 0 */ NODEV, /* 1 */ NODEV,
327 /* 2 */ NODEV, /* 3 */ NODEV,
328 /* 4 */ NODEV, /* 5 */ NODEV,
329 /* 6 */ NODEV, /* 7 */ NODEV,
330 /* 8 */ NODEV, /* 9 */ NODEV,
331 /* 10 */ NODEV, /* 11 */ NODEV,
332 /* 12 */ NODEV, /* 13 */ NODEV,
333 /* 14 */ 6, /* 15 */ NODEV,
334 /* 16 */ NODEV, /* 17 */ NODEV,
335 /* 18 */ NODEV, /* 19 */ NODEV,
336 /* 20 */ NODEV, /* 21 */ NODEV,
337 /* 22 */ NODEV, /* 23 */ NODEV,
338 /* 24 */ NODEV, /* 25 */ NODEV,
339 /* 26 */ NODEV, /* 27 */ NODEV,
340 /* 28 */ NODEV, /* 29 */ NODEV,
341 /* 30 */ NODEV, /* 31 */ NODEV,
342 /* 32 */ NODEV, /* 33 */ NODEV,
343 /* 34 */ NODEV, /* 35 */ NODEV,
344 /* 36 */ NODEV, /* 37 */ NODEV,
345 /* 38 */ NODEV, /* 39 */ NODEV,
346 /* 40 */ NODEV, /* 41 */ 1,
347 /* 42 */ NODEV, /* 43 */ NODEV,
348 /* 44 */ NODEV,
349 };
350
351 /*
352 * convert chr dev to blk dev
353 */
354 dev_t
355 chrtoblk(dev_t dev)
356 {
357 int blkmaj;
358
359 if (major(dev) >= nchrdev)
360 return(NODEV);
361 blkmaj = chrtoblktab[major(dev)];
362 if (blkmaj == NODEV)
363 return(NODEV);
364 return(makedev(blkmaj, minor(dev)));
365 }
366
367 int
368 chrtoblk_set(int cdev, int bdev)
369 {
370 if (cdev >= nchrdev)
371 return (-1);
372 if (bdev != NODEV && bdev >= nblkdev)
373 return (-1);
374 chrtoblktab[cdev] = bdev;
375 return 0;
376 }
377
378 /*
379 * Returns true if dev is /dev/mem or /dev/kmem.
380 */
381 int iskmemdev(dev_t dev)
382 {
383 return (major(dev) == 3 && minor(dev) < 2);
384 }