1/*	$OpenBSD: conf.c,v 1.18 2024/06/11 09:21:32 jsg Exp $	*/
2
3/*
4 * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by Charles Hannum.
17 * 4. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/device.h>
35#include <sys/disklabel.h>
36#include <sys/ioctl.h>
37#include <sys/tty.h>
38
39#include <machine/conf.h>
40
41#include "wd.h"
42bdev_decl(wd);
43#include "sd.h"
44#include "cd.h"
45#include "uk.h"
46#include "vnd.h"
47#include "rd.h"
48
49struct bdevsw	bdevsw[] =
50{
51	bdev_disk_init(NWD,wd),         /* 0: ST506/ESDI/IDE disk */
52	bdev_swap_init(1,sw),		/* 1: swap pseudo-device */
53	bdev_notdef(),			/* 2: was floppy diskette */
54	bdev_notdef(),			/* 3 */
55	bdev_disk_init(NSD,sd),		/* 4: SCSI disk */
56	bdev_notdef(),			/* 5: was: SCSI tape */
57	bdev_disk_init(NCD,cd),		/* 6: SCSI CD-ROM */
58	bdev_notdef(),			/* 7 */
59	bdev_disk_init(NRD,rd),		/* 8: ram disk driver */
60	bdev_notdef(),			/* 9 */
61	bdev_notdef(),			/* 10 */
62	bdev_notdef(),			/* 11 */
63	bdev_notdef(),			/* 12 */
64	bdev_notdef(),			/* 13 */
65	bdev_disk_init(NVND,vnd),	/* 14: vnode disk driver */
66	bdev_notdef(),			/* 15: was: Sony CD-ROM */
67	bdev_notdef(),			/* 16: was: concatenated disk driver */
68	bdev_notdef(),			/* 17 */
69	bdev_notdef(),			/* 18 */
70};
71int	nblkdev = nitems(bdevsw);
72
73#define	mmread	mmrw
74#define	mmwrite	mmrw
75cdev_decl(mm);
76cdev_decl(wd);
77#include "bio.h"
78#include "pty.h"
79#include "com.h"
80cdev_decl(com);
81#include "lpt.h"
82cdev_decl(lpt);
83#include "ch.h"
84#include "bpfilter.h"
85#include "tun.h"
86#include "audio.h"
87#include "video.h"
88#include "midi.h"
89#include "ksyms.h"
90#include "kstat.h"
91#include "usb.h"
92#include "uhid.h"
93#include "fido.h"
94#include "ujoy.h"
95#include "ugen.h"
96#include "ulpt.h"
97#include "ucom.h"
98#include "radio.h"
99#include "drm.h"
100
101#include "wsdisplay.h"
102#include "wskbd.h"
103#include "wsmouse.h"
104#include "wsmux.h"
105
106#ifdef USER_PCICONF
107#include "pci.h"
108cdev_decl(pci);
109#endif
110
111#include "dt.h"
112#include "pf.h"
113#include "hotplug.h"
114#include "vscsi.h"
115#include "pppx.h"
116#include "fuse.h"
117#include "openprom.h"
118#include "gpio.h"
119#include "ipmi.h"
120
121struct cdevsw	cdevsw[] =
122{
123	cdev_cn_init(1,cn),		/* 0: virtual console */
124	cdev_ctty_init(1,ctty),		/* 1: controlling terminal */
125	cdev_mm_init(1,mm),		/* 2: /dev/{null,mem,kmem,...} */
126	cdev_disk_init(NWD,wd),		/* 3: ST506/ESDI/IDE disk */
127	cdev_notdef(),			/* 4 was /dev/drum */
128	cdev_tty_init(NPTY,pts),	/* 5: pseudo-tty slave */
129	cdev_ptc_init(NPTY,ptc),	/* 6: pseudo-tty master */
130	cdev_log_init(1,log),		/* 7: /dev/klog */
131	cdev_tty_init(NCOM,com),	/* 8: serial port */
132	cdev_notdef(),			/* 9: was floppy disk */
133	cdev_notdef(),			/* 10 */
134	cdev_notdef(),			/* 11: Sony CD-ROM */
135	cdev_wsdisplay_init(NWSDISPLAY,	/* 12: frame buffers, etc. */
136	    wsdisplay),
137	cdev_disk_init(NSD,sd),		/* 13: SCSI disk */
138	cdev_notdef(),			/* 14: was: SCSI tape */
139	cdev_disk_init(NCD,cd),		/* 15: SCSI CD-ROM */
140	cdev_lpt_init(NLPT,lpt),	/* 16: parallel printer */
141	cdev_ch_init(NCH,ch),		/* 17: SCSI autochanger */
142	cdev_notdef(),			/* 18: was: concatenated disk driver */
143	cdev_notdef(),			/* 19 */
144	cdev_uk_init(NUK,uk),		/* 20: unknown SCSI */
145	cdev_notdef(),			/* 21 */
146	cdev_fd_init(1,filedesc),	/* 22: file descriptor pseudo-device */
147	cdev_bpf_init(NBPFILTER,bpf),	/* 23: Berkeley packet filter */
148	cdev_notdef(),			/* 24 */
149	cdev_notdef(),			/* 25 */
150	cdev_notdef(),			/* 26 */
151	cdev_notdef(),			/* 27 */
152	cdev_notdef(),			/* 28 was LKM */
153	cdev_notdef(),			/* 29 */
154	cdev_dt_init(NDT,dt),		/* 30: dynamic tracer */
155	cdev_notdef(),			/* 31 */
156	cdev_notdef(),			/* 32 */
157	cdev_notdef(),			/* 33 */
158	cdev_notdef(),			/* 34 */
159	cdev_notdef(),			/* 35: Microsoft mouse */
160	cdev_notdef(),			/* 36: Logitech mouse */
161	cdev_notdef(),			/* 37: Extended PS/2 mouse */
162	cdev_notdef(),			/* 38: was: Cyclom serial port */
163	cdev_notdef(),			/* 39: Mitsumi CD-ROM */
164	cdev_tun_init(NTUN,tun),	/* 40: network tunnel */
165	cdev_disk_init(NVND,vnd),	/* 41: vnode disk driver */
166	cdev_audio_init(NAUDIO,audio),	/* 42: generic audio I/O */
167	cdev_notdef(),			/* 43 */
168	cdev_video_init(NVIDEO,video),	/* 44: generic video I/O */
169	cdev_random_init(1,random),	/* 45: random data source */
170	cdev_notdef(),			/* 46 */
171	cdev_disk_init(NRD,rd),		/* 47: ram disk driver */
172	cdev_notdef(),			/* 48 */
173	cdev_notdef(),			/* 49: Bt848 video capture device */
174	cdev_ksyms_init(NKSYMS,ksyms),	/* 50: Kernel symbols device */
175	cdev_kstat_init(NKSTAT,kstat),	/* 51: kernel statistics */
176	cdev_midi_init(NMIDI,midi),	/* 52: MIDI I/O */
177	cdev_notdef(),			/* 53 was: sequencer I/O */
178	cdev_notdef(),			/* 54 was: RAIDframe disk driver */
179	cdev_notdef(),			/* 55: */
180	/* The following slots are reserved for isdn4bsd. */
181	cdev_notdef(),			/* 56: i4b main device */
182	cdev_notdef(),			/* 57: i4b control device */
183	cdev_notdef(),			/* 58: i4b raw b-channel access */
184	cdev_notdef(),			/* 59: i4b trace device */
185	cdev_notdef(),			/* 60: i4b phone device */
186	/* End of reserved slots for isdn4bsd. */
187	cdev_usb_init(NUSB,usb),	/* 61: USB controller */
188	cdev_usbdev_init(NUHID,uhid),	/* 62: USB generic HID */
189	cdev_usbdev_init(NUGEN,ugen),	/* 63: USB generic driver */
190	cdev_ulpt_init(NULPT,ulpt),	/* 64: USB printers */
191	cdev_notdef(),			/* 65: urio */
192	cdev_tty_init(NUCOM,ucom),	/* 66: USB tty */
193	cdev_mouse_init(NWSKBD, wskbd),	/* 67: keyboards */
194	cdev_mouse_init(NWSMOUSE,	/* 68: mice */
195	    wsmouse),
196	cdev_mouse_init(NWSMUX, wsmux),	/* 69: ws multiplexor */
197	cdev_openprom_init(NOPENPROM,openprom),	/* 70: /dev/openprom */
198	cdev_notdef(),			/* 71: was: Cyclades-Z serial port */
199#ifdef USER_PCICONF
200	cdev_pci_init(NPCI,pci),	/* 72: PCI user */
201#else
202	cdev_notdef(),
203#endif
204	cdev_pf_init(NPF,pf),		/* 73: packet filter */
205	cdev_notdef(),			/* 74: ALTQ (deprecated) */
206	cdev_notdef(),
207	cdev_radio_init(NRADIO, radio), /* 76: generic radio I/O */
208	cdev_notdef(),			/* 77: was USB scanners */
209	cdev_notdef(),			/* 78: was: system call tracing */
210	cdev_bio_init(NBIO,bio),	/* 79: ioctl tunnel */
211	cdev_notdef(),			/* 80 */
212	cdev_ptm_init(NPTY,ptm),	/* 81: pseudo-tty ptm device */
213	cdev_hotplug_init(NHOTPLUG,hotplug), /* 82: devices hot plugging */
214	cdev_notdef(),			/* 83: apm */
215	cdev_notdef(),			/* 84 */
216	cdev_notdef(),			/* 85 */
217	cdev_notdef(),			/* 86 */
218	cdev_drm_init(NDRM,drm),	/* 87: drm */
219	cdev_gpio_init(NGPIO,gpio),	/* 88: GPIO interface */
220	cdev_vscsi_init(NVSCSI,vscsi),	/* 89: vscsi */
221	cdev_disk_init(1,diskmap),	/* 90: disk mapper */
222	cdev_pppx_init(NPPPX,pppx),	/* 91: pppx */
223	cdev_fuse_init(NFUSE,fuse),	/* 92: fuse */
224	cdev_tun_init(NTUN,tap),	/* 93: Ethernet network tunnel */
225	cdev_notdef(),			/* 94 */
226	cdev_notdef(),			/* 95 */
227	cdev_ipmi_init(NIPMI,ipmi),	/* 96: ipmi */
228	cdev_notdef(),			/* 97: was switch(4) */
229	cdev_fido_init(NFIDO,fido),	/* 98: FIDO/U2F security key */
230	cdev_pppx_init(NPPPX,pppac),	/* 99: PPP Access Concentrator */
231	cdev_ujoy_init(NUJOY,ujoy),	/* 100: USB joystick/gamecontroller */
232};
233int	nchrdev = nitems(cdevsw);
234
235int	mem_no = 2;	/* major device number of memory special file */
236
237/*
238 * Swapdev is a fake device implemented
239 * in sw.c used only internally to get to swstrategy.
240 * It cannot be provided to the users, because the
241 * swstrategy routine munches the b_dev and b_blkno entries
242 * before calling the appropriate driver.  This would horribly
243 * confuse, e.g. the hashing routines. Instead, /dev/drum is
244 * provided as a character (raw) device.
245 */
246dev_t	swapdev = makedev(1, 0);
247
248/*
249 * Returns true if dev is /dev/mem or /dev/kmem.
250 */
251int
252iskmemdev(dev_t dev)
253{
254
255	return (major(dev) == mem_no && minor(dev) < 2);
256}
257
258/*
259 * Returns true if dev is /dev/zero.
260 */
261int
262iszerodev(dev_t dev)
263{
264
265	return (major(dev) == mem_no && minor(dev) == 12);
266}
267
268dev_t
269getnulldev(void)
270{
271	return makedev(mem_no, 2);
272}
273
274const int chrtoblktbl[] = {
275	/*VCHR*/	/*VBLK*/
276	/*  0 */	NODEV,
277	/*  1 */	NODEV,
278	/*  2 */	NODEV,
279	/*  3 */	0,		/* wd */
280	/*  4 */	NODEV,
281	/*  5 */	NODEV,
282	/*  6 */	NODEV,
283	/*  7 */	NODEV,
284	/*  8 */	NODEV,
285	/*  9 */	NODEV,		/* was fd */
286	/* 10 */	NODEV,
287	/* 11 */	NODEV,
288	/* 12 */	NODEV,
289	/* 13 */	4,		/* sd */
290	/* 14 */	NODEV,
291	/* 15 */	6,		/* cd */
292	/* 16 */	NODEV,
293	/* 17 */	NODEV,
294	/* 18 */	NODEV,
295	/* 19 */	NODEV,
296	/* 20 */	NODEV,
297	/* 21 */	NODEV,
298	/* 22 */	NODEV,
299	/* 23 */	NODEV,
300	/* 24 */	NODEV,
301	/* 25 */	NODEV,
302	/* 26 */	NODEV,
303	/* 27 */	NODEV,
304	/* 28 */	NODEV,
305	/* 29 */	NODEV,
306	/* 30 */	NODEV,
307	/* 31 */	NODEV,
308	/* 32 */	NODEV,
309	/* 33 */	NODEV,
310	/* 34 */	NODEV,
311	/* 35 */	NODEV,
312	/* 36 */	NODEV,
313	/* 37 */	NODEV,
314	/* 38 */	NODEV,
315	/* 39 */	NODEV,
316	/* 40 */	NODEV,
317	/* 41 */	14,		/* vnd */
318	/* 42 */	NODEV,
319	/* 43 */	NODEV,
320	/* 44 */	NODEV,
321	/* 45 */	NODEV,
322	/* 46 */	NODEV,
323	/* 47 */	8,		/* rd */
324};
325
326const int nchrtoblktbl = nitems(chrtoblktbl);
327
328/*
329 * In order to map BSD bdev numbers of disks to their BIOS equivalents
330 * we use several heuristics, one being using checksums of the first
331 * few blocks of a disk to get a signature we can match with /boot's
332 * computed signatures.  To know where from to read, we must provide a
333 * disk driver name -> bdev major number table, which follows.
334 * Note: floppies are not included as those are differentiated by the BIOS.
335 */
336int findblkmajor(struct device *dv);
337dev_t dev_rawpart(struct device *);	/* XXX */
338
339dev_t
340dev_rawpart(struct device *dv)
341{
342	int majdev;
343
344	majdev = findblkmajor(dv);
345
346	switch (majdev) {
347	/* add here any device you want to be checksummed on boot */
348	case 0:
349	case 4:
350		return (MAKEDISKDEV(majdev, dv->dv_unit, RAW_PART));
351		break;
352	default:
353		;
354	}
355
356	return (NODEV);
357}
358