2 * Copyright (c) 2004 Mark R V Murray
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/ioccom.h>
35 #include <sys/module.h>
38 #include <sys/systm.h>
40 #include <machine/iodev.h>
42 #include <dev/io/iodev.h>
44 static int ioopen(struct cdev *dev, int flags, int fmt,
46 static int ioclose(struct cdev *dev, int flags, int fmt,
48 static int ioioctl(struct cdev *dev, u_long cmd, caddr_t data,
49 int fflag, struct thread *td);
51 static int iopio_read(struct iodev_pio_req *req);
52 static int iopio_write(struct iodev_pio_req *req);
54 static struct cdev *iodev;
56 static struct cdevsw io_cdevsw = {
57 .d_version = D_VERSION,
66 ioopen(struct cdev *dev __unused, int flags __unused, int fmt __unused,
71 error = priv_check(td, PRIV_IO);
74 error = securelevel_gt(td->td_ucred, 0);
77 error = iodev_open(td);
84 ioclose(struct cdev *dev __unused, int flags __unused, int fmt __unused,
88 return (iodev_close(td));
93 ioioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
94 int fflag __unused, struct thread *td __unused)
96 struct iodev_pio_req *pio_req;
101 pio_req = (struct iodev_pio_req *)data;
102 switch (pio_req->access) {
104 error = iopio_read(pio_req);
106 case IODEV_PIO_WRITE:
107 error = iopio_write(pio_req);
115 error = iodev_ioctl(cmd, data);
122 iopio_read(struct iodev_pio_req *req)
125 switch (req->width) {
127 req->val = iodev_read_1(req->port);
131 req->val = iodev_read_1(req->port);
132 req->val |= iodev_read_1(req->port + 1) << 8;
134 req->val = iodev_read_2(req->port);
138 req->val = iodev_read_1(req->port);
139 req->val |= iodev_read_2(req->port + 1) << 8;
140 req->val |= iodev_read_1(req->port + 3) << 24;
141 } else if (req->port & 2) {
142 req->val = iodev_read_2(req->port);
143 req->val |= iodev_read_2(req->port + 2) << 16;
145 req->val = iodev_read_4(req->port);
155 iopio_write(struct iodev_pio_req *req)
158 switch (req->width) {
160 iodev_write_1(req->port, req->val);
164 iodev_write_1(req->port, req->val);
165 iodev_write_1(req->port + 1, req->val >> 8);
167 iodev_write_2(req->port, req->val);
171 iodev_write_1(req->port, req->val);
172 iodev_write_2(req->port + 1, req->val >> 8);
173 iodev_write_1(req->port + 3, req->val >> 24);
174 } else if (req->port & 2) {
175 iodev_write_2(req->port, req->val);
176 iodev_write_2(req->port + 2, req->val >> 16);
178 iodev_write_4(req->port, req->val);
189 io_modevent(module_t mod __unused, int type, void *data __unused)
194 printf("io: <I/O>\n");
195 iodev = make_dev(&io_cdevsw, 0,
196 UID_ROOT, GID_WHEEL, 0600, "io");
215 DEV_MODULE(io, io_modevent, NULL);
216 MODULE_VERSION(io, 1);