/* Copyright 2018 Ian Cowburn 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* The softc holds our per-instance data. */ struct hardsid_softc { device_t dev; struct cdev *cdev; int rid; struct mtx lock; struct resource *res; int opened; int reg; }; /* Function prototypes */ static d_open_t hardsid_open; static d_close_t hardsid_close; static d_read_t hardsid_read; static d_write_t hardsid_write; /* Character device entry points */ static struct cdevsw hardsid_cdevsw = { .d_version = D_VERSION, .d_open = hardsid_open, .d_close = hardsid_close, .d_read = hardsid_read, .d_write = hardsid_write, .d_name = "hardsid", }; int hardsid_open(struct cdev *dev, int oflags, int devtype, struct thread *td) { struct hardsid_softc *sc; int ret; sc = dev->si_drv1; mtx_lock(&sc->lock); if (sc->opened) { ret = EACCES; } else { ret = 0; sc->opened = 1; sc->reg = -1; } mtx_unlock(&sc->lock); return ret; } int hardsid_close(struct cdev *dev, int fflag, int devtype, struct thread *td) { struct hardsid_softc *sc; sc = dev->si_drv1; mtx_lock(&sc->lock); sc->opened = 0; mtx_unlock(&sc->lock); return 0; } int hardsid_read(struct cdev *dev, struct uio *uio, int ioflag) { struct hardsid_softc *sc; sc = dev->si_drv1; return 0; } int hardsid_write(struct cdev *dev, struct uio *uio, int ioflag) { struct hardsid_softc *sc; sc = dev->si_drv1; return 0; } /* PCI Support Functions */ static int hardsid_probe(device_t dev) { if ((pci_get_vendor(dev) == 0x6581) && (pci_get_device(dev) == 0x8580)) { device_set_desc(dev, "hardsid"); return BUS_PROBE_DEFAULT; } return ENXIO; } /* Attach function is only called if the probe is successful. */ static int hardsid_attach(device_t dev) { struct hardsid_softc *sc; /* Look up our softc and initialize its fields. */ sc = device_get_softc(dev); sc->dev = dev; sc->opened = 0; sc->rid = PCIR_BAR(0); sc->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &sc->rid, RF_ACTIVE); mtx_init(&sc->lock, "hardsid", NULL, MTX_DEF); if (sc->res == NULL) { device_printf(dev, "couldn't configure PCI IO resources\n"); return ENXIO; } /*Create a /dev entry for this device. */ sc->cdev = make_dev(&hardsid_cdevsw, device_get_unit(dev), UID_ROOT, GID_WHEEL, 0666, "hardsid%u", device_get_unit(dev)); sc->cdev->si_drv1 = sc; return 0; } /* Detach device. */ static int hardsid_detach(device_t dev) { struct hardsid_softc *sc; /* Teardown the state in our softc created in our attach routine. */ sc = device_get_softc(dev); bus_release_resource(dev, SYS_RES_IOPORT, sc->rid, sc->res); destroy_dev(sc->cdev); return 0; } /* Called during system shutdown after sync. */ static int hardsid_shutdown(device_t dev) { return 0; } /* Device suspend routine. */ static int hardsid_suspend(device_t dev) { return 0; } /* Device resume routine. */ static int hardsid_resume(device_t dev) { return 0; } static device_method_t hardsid_methods[] = { /* Device interface */ DEVMETHOD(device_probe, hardsid_probe), DEVMETHOD(device_attach, hardsid_attach), DEVMETHOD(device_detach, hardsid_detach), DEVMETHOD(device_shutdown, hardsid_shutdown), DEVMETHOD(device_suspend, hardsid_suspend), DEVMETHOD(device_resume, hardsid_resume), DEVMETHOD_END }; static devclass_t hardsid_devclass; DEFINE_CLASS_0(hardsid, hardsid_driver, hardsid_methods, sizeof(struct hardsid_softc)); DRIVER_MODULE(hardsid, pci, hardsid_driver, hardsid_devclass, 0, 0);