]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/i386/conf.c
xnu-6153.61.1.tar.gz
[apple/xnu.git] / bsd / dev / i386 / conf.c
CommitLineData
1c79356b 1/*
5ba3f43e 2 * Copyright (c) 1997-2017 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
1c79356b
A
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)
0a7de745 36 * enabled file descriptor pseudo-device.
1c79356b
A
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>
1c79356b
A
43#include <sys/ioctl.h>
44#include <sys/tty.h>
45#include <sys/conf.h>
46
91447636 47/* Prototypes that should be elsewhere: */
0a7de745
A
48extern dev_t chrtoblk(dev_t dev);
49extern int chrtoblk_set(int cdev, int bdev);
1c79356b 50
0a7de745 51struct bdevsw bdevsw[] =
1c79356b
A
52{
53 /*
0a7de745 54 * For block devices, every other block of 8 slots is
2d21ac55 55 * reserved for Apple. The other slots are available for
1c79356b 56 * the user. This way we can both add new entries without
2d21ac55 57 * running into each other. Be sure to fill in Apple's
1c79356b
A
58 * 8 reserved slots when you jump over us -- we'll do the
59 * same for you.
60 */
61
2d21ac55 62 /* 0 - 7 are reserved for Apple */
1c79356b 63
0a7de745
A
64 NO_BDEVICE, /* 0*/
65 NO_BDEVICE, /* 1*/
66 NO_BDEVICE, /* 2*/
67 NO_BDEVICE, /* 3*/
68 NO_BDEVICE, /* 4*/
69 NO_BDEVICE, /* 5*/
70 NO_BDEVICE, /* 6*/
71 NO_BDEVICE, /* 7*/
1c79356b
A
72
73 /* 8 - 15 are reserved to the user */
0a7de745
A
74 NO_BDEVICE, /* 8*/
75 NO_BDEVICE, /* 9*/
76 NO_BDEVICE, /*10*/
77 NO_BDEVICE, /*11*/
78 NO_BDEVICE, /*12*/
79 NO_BDEVICE, /*13*/
80 NO_BDEVICE, /*14*/
81 NO_BDEVICE, /*15*/
1c79356b 82
2d21ac55 83 /* 16 - 23 are reserved for Apple */
0a7de745
A
84 NO_BDEVICE, /*16*/
85 NO_BDEVICE, /*17*/
86 NO_BDEVICE, /*18*/
87 NO_BDEVICE, /*18*/
88 NO_BDEVICE, /*20*/
89 NO_BDEVICE, /*21*/
90 NO_BDEVICE, /*22*/
91 NO_BDEVICE, /*23*/
1c79356b
A
92};
93
39037602 94const int nblkdev = sizeof(bdevsw) / sizeof(bdevsw[0]);
1c79356b
A
95
96extern struct tty *km_tty[];
0a7de745
A
97extern d_open_t cnopen;
98extern d_close_t cnclose;
99extern d_read_t cnread;
100extern d_write_t cnwrite;
101extern d_ioctl_t cnioctl;
102extern d_select_t cnselect;
103extern d_open_t kmopen;
104extern d_close_t kmclose;
105extern d_read_t kmread;
106extern d_write_t kmwrite;
107extern d_ioctl_t kmioctl;
108extern d_open_t sgopen;
109extern d_close_t sgclose;
110extern d_ioctl_t sgioctl;
1c79356b
A
111
112#if NVOL > 0
0a7de745
A
113extern d_open_t volopen;
114extern d_close_t volclose;
115extern d_ioctl_t volioctl;
1c79356b 116#else
0a7de745
A
117#define volopen eno_opcl
118#define volclose eno_opcl
119#define volioctl eno_ioctl
1c79356b
A
120#endif
121
0a7de745
A
122extern d_open_t cttyopen;
123extern d_read_t cttyread;
124extern d_write_t cttywrite;
125extern d_ioctl_t cttyioctl;
126extern d_select_t cttyselect;
1c79356b 127
0a7de745
A
128extern d_read_t mmread;
129extern d_write_t mmwrite;
130extern d_ioctl_t mmioctl;
131#define mmselect (select_fcn_t *)seltrue
132#define mmmmap eno_mmap
1c79356b
A
133
134#include <pty.h>
135#if NPTY > 0
0a7de745
A
136extern d_open_t ptsopen;
137extern d_close_t ptsclose;
138extern d_read_t ptsread;
139extern d_write_t ptswrite;
140extern d_stop_t ptsstop;
141extern d_select_t ptsselect;
142extern d_open_t ptcopen;
143extern d_close_t ptcclose;
144extern d_read_t ptcread;
145extern d_write_t ptcwrite;
146extern d_select_t ptcselect;
147extern d_ioctl_t ptyioctl;
1c79356b 148#else
0a7de745
A
149#define ptsopen eno_opcl
150#define ptsclose eno_opcl
151#define ptsread eno_rdwrt
152#define ptswrite eno_rdwrt
153#define ptsstop nulldev
1c79356b 154
0a7de745
A
155#define ptcopen eno_opcl
156#define ptcclose eno_opcl
157#define ptcread eno_rdwrt
158#define ptcwrite eno_rdwrt
159#define ptcselect eno_select
160#define ptyioctl eno_ioctl
1c79356b
A
161#endif
162
0a7de745
A
163extern d_open_t logopen;
164extern d_close_t logclose;
165extern d_read_t logread;
166extern d_ioctl_t logioctl;
167extern d_select_t logselect;
91447636 168
0a7de745
A
169extern d_open_t oslog_streamopen;
170extern d_close_t oslog_streamclose;
171extern d_read_t oslog_streamread;
172extern d_ioctl_t oslog_streamioctl;
173extern d_select_t oslog_streamselect;
39037602 174
0a7de745
A
175extern d_open_t oslogopen;
176extern d_close_t oslogclose;
177extern d_select_t oslogselect;
178extern d_ioctl_t oslogioctl;
39037602 179
0a7de745
A
180#define nullopen (d_open_t *)&nulldev
181#define nullclose (d_close_t *)&nulldev
182#define nullread (d_read_t *)&nulldev
183#define nullwrite (d_write_t *)&nulldev
184#define nullioctl (d_ioctl_t *)&nulldev
185#define nullselect (d_select_t *)&nulldev
186#define nullstop (d_stop_t *)&nulldev
187#define nullreset (d_reset_t *)&nulldev
1c79356b 188
5ba3f43e 189struct cdevsw cdevsw[] = {
1c79356b 190 /*
5ba3f43e 191 * To add character devices to this table dynamically, use cdevsw_add.
1c79356b
A
192 */
193
5ba3f43e
A
194 [0] = {
195 cnopen, cnclose, cnread, cnwrite,
196 cnioctl, nullstop, nullreset, 0, cnselect,
197 eno_mmap, eno_strat, eno_getc, eno_putc, D_TTY
198 },
199 [1] = NO_CDEVICE,
200 [2] = {
201 cttyopen, nullclose, cttyread, cttywrite,
202 cttyioctl, nullstop, nullreset, 0, cttyselect,
203 eno_mmap, eno_strat, eno_getc, eno_putc, D_TTY
204 },
205 [3] = {
206 nullopen, nullclose, mmread, mmwrite,
207 mmioctl, nullstop, nullreset, 0, mmselect,
208 mmmmap, eno_strat, eno_getc, eno_putc, D_DISK
209 },
210 [PTC_MAJOR] = {
211 ptsopen, ptsclose, ptsread, ptswrite,
212 ptyioctl, ptsstop, nullreset, 0, ptsselect,
213 eno_mmap, eno_strat, eno_getc, eno_putc, D_TTY
214 },
215 [PTS_MAJOR] = {
216 ptcopen, ptcclose, ptcread, ptcwrite,
217 ptyioctl, nullstop, nullreset, 0, ptcselect,
218 eno_mmap, eno_strat, eno_getc, eno_putc, D_TTY
219 },
220 [6] = {
221 logopen, logclose, logread, eno_rdwrt,
222 logioctl, eno_stop, nullreset, 0, logselect,
223 eno_mmap, eno_strat, eno_getc, eno_putc, 0
224 },
225 [7] = {
226 oslogopen, oslogclose, eno_rdwrt, eno_rdwrt,
227 oslogioctl, eno_stop, nullreset, 0, oslogselect,
228 eno_mmap, eno_strat, eno_getc, eno_putc, 0
229 },
230 [8] = {
231 oslog_streamopen, oslog_streamclose, oslog_streamread, eno_rdwrt,
232 oslog_streamioctl, eno_stop, nullreset, 0, oslog_streamselect,
233 eno_mmap, eno_strat, eno_getc, eno_putc, 0
234 },
235 [9 ... 11] = NO_CDEVICE,
236 [12] = {
237 kmopen, kmclose, kmread, kmwrite,
238 kmioctl, nullstop, nullreset, km_tty, ttselect,
239 eno_mmap, eno_strat, eno_getc, eno_putc, 0
240 },
cb323159 241 [13 ... 63] = NO_CDEVICE,
1c79356b 242};
39037602 243const int nchrdev = sizeof(cdevsw) / sizeof(cdevsw[0]);
1c79356b 244
39037602 245uint64_t cdevsw_flags[sizeof(cdevsw) / sizeof(cdevsw[0])];
1c79356b 246
0a7de745 247#include <sys/vnode.h> /* for VCHR and VBLK */
1c79356b
A
248/*
249 * return true if a disk
250 */
251int
91447636 252isdisk(dev_t dev, int type)
1c79356b 253{
0a7de745 254 dev_t maj = major(dev);
9bccf70c
A
255
256 switch (type) {
257 case VCHR:
cb323159 258 maj = chrtoblk(dev);
9bccf70c
A
259 if (maj == NODEV) {
260 break;
261 }
0a7de745 262 /* FALL THROUGH */
9bccf70c 263 case VBLK:
39236c6e 264 if (bdevsw[maj].d_type == D_DISK) {
0a7de745 265 return 1;
9bccf70c 266 }
1c79356b
A
267 break;
268 }
0a7de745 269 return 0;
1c79356b
A
270}
271
cb323159 272static int chrtoblktab[] = {[0 ... nchrdev] = NODEV };
1c79356b
A
273
274/*
275 * convert chr dev to blk dev
276 */
277dev_t
91447636 278chrtoblk(dev_t dev)
1c79356b
A
279{
280 int blkmaj;
281
0a7de745
A
282 if (major(dev) >= nchrdev) {
283 return NODEV;
284 }
1c79356b 285 blkmaj = chrtoblktab[major(dev)];
0a7de745
A
286 if (blkmaj == NODEV) {
287 return NODEV;
288 }
289 return makedev(blkmaj, minor(dev));
1c79356b
A
290}
291
9bccf70c
A
292int
293chrtoblk_set(int cdev, int bdev)
294{
0a7de745
A
295 if (cdev >= nchrdev) {
296 return -1;
297 }
298 if (bdev != NODEV && bdev >= nblkdev) {
299 return -1;
300 }
9bccf70c
A
301 chrtoblktab[cdev] = bdev;
302 return 0;
303}