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