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