/*-
* Copyright (c) 2016 Izumi Tsutsui. All rights reserved.
*
* 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 ``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 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.
*/
static int
xp_open(dev_t dev, int flags, int devtype, struct lwp *l)
{
struct xp_softc *sc;
int unit;
u_int a;
DPRINTF(XP_DEBUG_ALL, ("%s\n", __func__));
unit = minor(dev);
sc = device_lookup_private(&xp_cd, unit);
if (sc == NULL)
return ENXIO;
if (sc->sc_isopen)
return EBUSY;
if ((flags & FWRITE) != 0) {
/* exclusive if write */
a = xp_acquire(DEVID_XPBUS, XP_ACQ_EXCL);
if (a == 0)
return EBUSY;
if (a != (1 << DEVID_XPBUS)) {
xp_release(DEVID_XPBUS);
return EBUSY;
}
} else {
a = xp_acquire(DEVID_XPBUS, 0);
if (a == 0)
return EBUSY;
}
sc->sc_isopen = true;
sc->sc_flags = flags;
return 0;
}
static int
xp_close(dev_t dev, int flags, int mode, struct lwp *l)
{
struct xp_softc *sc;
int unit;
DPRINTF(XP_DEBUG_ALL, ("%s\n", __func__));
unit = minor(dev);
sc = device_lookup_private(&xp_cd, unit);
xp_release(DEVID_XPBUS);
sc->sc_isopen = false;
return 0;
}
static int
xp_ioctl(dev_t dev, u_long cmd, void *addr, int flags, struct lwp *l)
{
struct xp_softc *sc;
int unit, error;
struct xp_download *downld;
uint8_t *loadbuf;
size_t loadsize;
DPRINTF(XP_DEBUG_ALL, ("%s\n", __func__));
unit = minor(dev);
sc = device_lookup_private(&xp_cd, unit);