2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Mark R V Murray
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/ioccom.h>
37 #include <sys/module.h>
40 #include <sys/systm.h>
42 #include <machine/iodev.h>
44 #include <dev/io/iodev.h>
46 static int ioopen(struct cdev *dev, int flags, int fmt,
48 static int ioclose(struct cdev *dev, int flags, int fmt,
50 static int ioioctl(struct cdev *dev, u_long cmd, caddr_t data,
51 int fflag, struct thread *td);
53 static int iopio_read(struct iodev_pio_req *req);
54 static int iopio_write(struct iodev_pio_req *req);
56 static struct cdev *iodev;
58 static struct cdevsw io_cdevsw = {
59 .d_version = D_VERSION,
68 ioopen(struct cdev *dev __unused, int flags __unused, int fmt __unused,
73 error = priv_check(td, PRIV_IO);
76 error = securelevel_gt(td->td_ucred, 0);
79 error = iodev_open(td);
86 ioclose(struct cdev *dev __unused, int flags __unused, int fmt __unused,
90 return (iodev_close(td));
95 ioioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
96 int fflag __unused, struct thread *td __unused)
98 struct iodev_pio_req *pio_req;
103 pio_req = (struct iodev_pio_req *)data;
104 switch (pio_req->access) {
106 error = iopio_read(pio_req);
108 case IODEV_PIO_WRITE:
109 error = iopio_write(pio_req);
117 error = iodev_ioctl(cmd, data);
124 iopio_read(struct iodev_pio_req *req)
127 switch (req->width) {
129 req->val = iodev_read_1(req->port);
133 req->val = iodev_read_1(req->port);
134 req->val |= iodev_read_1(req->port + 1) << 8;
136 req->val = iodev_read_2(req->port);
140 req->val = iodev_read_1(req->port);
141 req->val |= iodev_read_2(req->port + 1) << 8;
142 req->val |= iodev_read_1(req->port + 3) << 24;
143 } else if (req->port & 2) {
144 req->val = iodev_read_2(req->port);
145 req->val |= iodev_read_2(req->port + 2) << 16;
147 req->val = iodev_read_4(req->port);
157 iopio_write(struct iodev_pio_req *req)
160 switch (req->width) {
162 iodev_write_1(req->port, req->val);
166 iodev_write_1(req->port, req->val);
167 iodev_write_1(req->port + 1, req->val >> 8);
169 iodev_write_2(req->port, req->val);
173 iodev_write_1(req->port, req->val);
174 iodev_write_2(req->port + 1, req->val >> 8);
175 iodev_write_1(req->port + 3, req->val >> 24);
176 } else if (req->port & 2) {
177 iodev_write_2(req->port, req->val);
178 iodev_write_2(req->port + 2, req->val >> 16);
180 iodev_write_4(req->port, req->val);
191 io_modevent(module_t mod __unused, int type, void *data __unused)
196 printf("io: <I/O>\n");
197 iodev = make_dev(&io_cdevsw, 0,
198 UID_ROOT, GID_WHEEL, 0600, "io");
217 DEV_MODULE(io, io_modevent, NULL);
218 MODULE_VERSION(io, 1);