diff --git a/sys/dev/superio/superio.c b/sys/dev/superio/superio.c index da4dd112533..24dc67b3d4f 100644 --- a/sys/dev/superio/superio.c +++ b/sys/dev/superio/superio.c @@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include "isa_if.h" @@ -84,6 +85,7 @@ struct siosc { struct mtx conf_lock; STAILQ_HEAD(, superio_devinfo) devlist; struct resource* io_res; + struct cdev *chardev; int io_rid; uint16_t io_port; const struct sio_conf_methods *methods; @@ -96,6 +98,14 @@ struct siosc { uint8_t enable_reg; }; +static d_ioctl_t superio_ioctl; + +static struct cdevsw superio_cdevsw = { + .d_version = D_VERSION, + .d_ioctl = superio_ioctl, + .d_name = "superio", +}; + #define NUMPORTS 2 static uint8_t @@ -621,6 +631,12 @@ superio_attach(device_t dev) bus_generic_probe(dev); bus_generic_attach(dev); + + sc->chardev = make_dev(&superio_cdevsw, device_get_unit(dev), + UID_ROOT, GID_WHEEL, 0600, "superio%d", device_get_unit(dev)); + if (sc->chardev == NULL) + device_printf(dev, "failed to create character device\n"); + sc->chardev->si_drv1 = sc; return (0); } @@ -633,6 +649,8 @@ superio_detach(device_t dev) error = bus_generic_detach(dev); if (error != 0) return (error); + if (sc->chardev != NULL) + destroy_dev(sc->chardev); device_delete_children(dev); bus_release_resource(dev, SYS_RES_IOPORT, sc->io_rid, sc->io_res); mtx_destroy(&sc->conf_lock); @@ -915,6 +933,31 @@ superio_find_dev(device_t superio, superio_dev_type_t type, int ldn) return (NULL); } +static int +superio_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, + struct thread *td) +{ + struct siosc *sc; + struct superiocmd *s; + + sc = dev->si_drv1; + s = (struct superiocmd *)data; + switch (cmd) { + case SUPERIO_CR_READ: + sio_conf_enter(sc); + s->val = sio_ldn_read(sc, s->ldn, s->cr); + sio_conf_exit(sc); + return (0); + case SUPERIO_CR_WRITE: + sio_conf_enter(sc); + sio_ldn_write(sc, s->ldn, s->cr, s->val); + sio_conf_exit(sc); + return (0); + default: + return (ENOTTY); + } +} + static devclass_t superio_devclass; static device_method_t superio_methods[] = { diff --git a/sys/dev/superio/superio_io.h b/sys/dev/superio/superio_io.h new file mode 100644 index 00000000000..e1e4026fd9c --- /dev/null +++ b/sys/dev/superio/superio_io.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 Andriy Gapon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef SUPERIO_IO_H +#define SUPERIO_IO_H + +#include + +struct superiocmd { + uint8_t ldn; + uint8_t cr; + uint8_t val; +}; + +#define SUPERIO_CR_READ _IOWR('s', 0, struct superiocmd) +#define SUPERIO_CR_WRITE _IOW('s', 1, struct superiocmd) + +#endif /*SUPERIO_IO_H*/