#include <sys/cdefs.h>
#include "opt_lpt.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/uio.h>
#include <sys/syslog.h>
#include <sys/malloc.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>
#include <dev/ppbus/lptio.h>
#include <dev/ppbus/ppbconf.h>
#include <dev/ppbus/ppb_1284.h>
#include <dev/ppbus/lpt.h>
#include "ppbus_if.h"
#include <dev/ppbus/ppbio.h>
#ifndef LPT_DEBUG
#define lprintf(args)
#else
#define lprintf(args) \
do { \
if (lptflag) \
printf args; \
} while (0)
static int volatile lptflag = 1;
#endif
#define LPINITRDY 4
#define LPTOUTINITIAL 10
#define LPTOUTMAX 1
#define LPPRI (PWAIT)
#define BUFSIZE 1024
#define BUFSTATSIZE 32
struct lpt_data {
device_t sc_dev;
struct cdev *sc_cdev;
struct cdev *sc_cdev_bypass;
short sc_state;
u_char sc_control;
char sc_flags;
#define LP_POS_INIT 0x04
#define LP_POS_ACK 0x08
#define LP_NO_PRIME 0x10
#define LP_PRIMEOPEN 0x20
#define LP_AUTOLF 0x40
#define LP_BYPASS 0x80
void *sc_inbuf;
void *sc_statbuf;
short sc_xfercnt ;
char sc_primed;
char *sc_cp ;
u_short sc_irq ;
#define LP_HAS_IRQ 0x01
#define LP_USE_IRQ 0x02
#define LP_ENABLE_IRQ 0x04
#define LP_ENABLE_EXT 0x10
u_char sc_backoff ;
struct callout sc_timer;
struct resource *sc_intr_resource;
void *sc_intr_cookie;
};
#define LPT_NAME "lpt"
static callout_func_t lptout;
static int lpt_port_test(device_t dev, u_char data, u_char mask);
static int lpt_detect(device_t dev);
#define DEVTOSOFTC(dev) \
((struct lpt_data *)device_get_softc(dev))
static void lptintr(void *arg);
#define OPEN (1<<0)
#define ASLP (1<<1)
#define EERROR (1<<2)
#define OBUSY (1<<3)
#define LPTOUT (1<<4)
#define TOUT (1<<5)
#define LPTINIT (1<<6)
#define INTERRUPTED (1<<7)
#define HAVEBUS (1<<8)
#define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)
#define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR)
#define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR)
#define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
#define NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK)
#define MAX_SLEEP (hz*5)
#define MAX_SPIN 20
static d_open_t lptopen;
static d_close_t lptclose;
static d_write_t lptwrite;
static d_read_t lptread;
static d_ioctl_t lptioctl;
static struct cdevsw lpt_cdevsw = {
.d_version = D_VERSION,
.d_open = lptopen,
.d_close = lptclose,
.d_read = lptread,
.d_write = lptwrite,
.d_ioctl = lptioctl,
.d_name = LPT_NAME,
};
static int
lpt_request_ppbus(device_t dev, int how)
{
device_t ppbus = device_get_parent(dev);
struct lpt_data *sc = DEVTOSOFTC(dev);
int error;
ppb_assert_locked(ppbus);
if (sc->sc_state & HAVEBUS)
return (0);
error = ppb_request_bus(ppbus, dev, how);
if (error == 0)
sc->sc_state |= HAVEBUS;
return (error);
}
static int
lpt_release_ppbus(device_t dev)
{
device_t ppbus = device_get_parent(dev);
struct lpt_data *sc = DEVTOSOFTC(dev);
int error = 0;
ppb_assert_locked(ppbus);
if (sc->sc_state & HAVEBUS) {
error = ppb_release_bus(ppbus, dev);
if (error == 0)
sc->sc_state &= ~HAVEBUS;
}
return (error);
}
static int
lpt_port_test(device_t ppbus, u_char data, u_char mask)
{
int temp, timeout;
data = data & mask;
ppb_wdtr(ppbus, data);
timeout = 10000;
do {
DELAY(10);
temp = ppb_rdtr(ppbus) & mask;
} while (temp != data && --timeout);
lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
return (temp == data);
}
static int
lpt_detect(device_t dev)
{
device_t ppbus = device_get_parent(dev);
static u_char testbyte[18] = {
0x55,
0xaa,
0xfe, 0xfd, 0xfb, 0xf7,
0xef, 0xdf, 0xbf, 0x7f,
0x01, 0x02, 0x04, 0x08,
0x10, 0x20, 0x40, 0x80
};
int i, error, status;
status = 1;
ppb_lock(ppbus);
if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
ppb_unlock(ppbus);
device_printf(dev, "cannot alloc ppbus (%d)!\n", error);
return (0);
}
for (i = 0; i < 18 && status; i++)
if (!lpt_port_test(ppbus, testbyte[i], 0xff)) {
status = 0;
break;
}
ppb_wdtr(ppbus, 0);
ppb_wctr(ppbus, 0);
lpt_release_ppbus(dev);
ppb_unlock(ppbus);
return (status);
}
static void
lpt_identify(driver_t *driver, device_t parent)
{
device_t dev;
dev = device_find_child(parent, LPT_NAME, DEVICE_UNIT_ANY);
if (!dev)
BUS_ADD_CHILD(parent, 0, LPT_NAME, DEVICE_UNIT_ANY);
}
static int
lpt_probe(device_t dev)
{
if (!lpt_detect(dev))
return (ENXIO);
device_set_desc(dev, "Printer");
return (0);
}
static int
lpt_attach(device_t dev)
{
device_t ppbus = device_get_parent(dev);
struct lpt_data *sc = DEVTOSOFTC(dev);
int rid = 0, unit = device_get_unit(dev);
int error;
sc->sc_primed = 0;
ppb_init_callout(ppbus, &sc->sc_timer, 0);
ppb_lock(ppbus);
if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
ppb_unlock(ppbus);
device_printf(dev, "cannot alloc ppbus (%d)!\n", error);
return (0);
}
ppb_wctr(ppbus, LPC_NINIT);
lpt_release_ppbus(dev);
ppb_unlock(ppbus);
sc->sc_intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
RF_SHAREABLE);
if (sc->sc_intr_resource) {
error = bus_setup_intr(dev, sc->sc_intr_resource,
INTR_TYPE_TTY | INTR_MPSAFE, NULL, lptintr, sc,
&sc->sc_intr_cookie);
if (error) {
bus_release_resource(dev, SYS_RES_IRQ, rid,
sc->sc_intr_resource);
device_printf(dev,
"Unable to register interrupt handler\n");
return (error);
}
sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
device_printf(dev, "Interrupt-driven port\n");
} else {
sc->sc_irq = 0;
device_printf(dev, "Polled port\n");
}
lprintf(("irq %x\n", sc->sc_irq));
sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
sc->sc_statbuf = malloc(BUFSTATSIZE, M_DEVBUF, M_WAITOK);
sc->sc_dev = dev;
sc->sc_cdev = make_dev(&lpt_cdevsw, unit,
UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit);
sc->sc_cdev->si_drv1 = sc;
sc->sc_cdev->si_drv2 = 0;
sc->sc_cdev_bypass = make_dev(&lpt_cdevsw, unit,
UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit);
sc->sc_cdev_bypass->si_drv1 = sc;
sc->sc_cdev_bypass->si_drv2 = (void *)LP_BYPASS;
return (0);
}
static int
lpt_detach(device_t dev)
{
struct lpt_data *sc = DEVTOSOFTC(dev);
device_t ppbus = device_get_parent(dev);
destroy_dev(sc->sc_cdev);
destroy_dev(sc->sc_cdev_bypass);
ppb_lock(ppbus);
lpt_release_ppbus(dev);
ppb_unlock(ppbus);
callout_drain(&sc->sc_timer);
if (sc->sc_intr_resource != NULL) {
bus_teardown_intr(dev, sc->sc_intr_resource,
sc->sc_intr_cookie);
bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_intr_resource);
}
free(sc->sc_inbuf, M_DEVBUF);
free(sc->sc_statbuf, M_DEVBUF);
return (0);
}
static void
lptout(void *arg)
{
struct lpt_data *sc = arg;
device_t dev = sc->sc_dev;
device_t ppbus __unused;
ppbus = device_get_parent(dev);
ppb_assert_locked(ppbus);
lprintf(("T %x ", ppb_rstr(ppbus)));
if (sc->sc_state & OPEN) {
sc->sc_backoff++;
if (sc->sc_backoff > hz/LPTOUTMAX)
sc->sc_backoff = hz/LPTOUTMAX;
callout_reset(&sc->sc_timer, sc->sc_backoff, lptout, sc);
} else
sc->sc_state &= ~TOUT;
if (sc->sc_state & EERROR)
sc->sc_state &= ~EERROR;
if (sc->sc_xfercnt) {
lptintr(sc);
} else {
sc->sc_state &= ~OBUSY;
wakeup(dev);
}
}
static int
lptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
{
int trys, err;
struct lpt_data *sc = dev->si_drv1;
device_t lptdev;
device_t ppbus;
if (!sc)
return (ENXIO);
lptdev = sc->sc_dev;
ppbus = device_get_parent(lptdev);
ppb_lock(ppbus);
if (sc->sc_state) {
lprintf(("%s: still open %x\n", device_get_nameunit(lptdev),
sc->sc_state));
ppb_unlock(ppbus);
return(EBUSY);
} else
sc->sc_state |= LPTINIT;
sc->sc_flags = (uintptr_t)dev->si_drv2;
if (sc->sc_flags & LP_BYPASS) {
sc->sc_state = OPEN;
ppb_unlock(ppbus);
return(0);
}
if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
sc->sc_state = 0;
ppb_unlock(ppbus);
return (err);
}
lprintf(("%s flags 0x%x\n", device_get_nameunit(lptdev),
sc->sc_flags));
if (sc->sc_irq & LP_ENABLE_IRQ)
sc->sc_irq |= LP_USE_IRQ;
else
sc->sc_irq &= ~LP_USE_IRQ;
if ((sc->sc_flags & LP_NO_PRIME) == 0) {
if ((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
ppb_wctr(ppbus, 0);
sc->sc_primed++;
DELAY(500);
}
}
ppb_wctr(ppbus, LPC_SEL|LPC_NINIT);
trys = 0;
do {
if (trys++ >= LPINITRDY*4) {
lprintf(("status %x\n", ppb_rstr(ppbus)));
lpt_release_ppbus(lptdev);
sc->sc_state = 0;
ppb_unlock(ppbus);
return (EBUSY);
}
if (ppb_sleep(ppbus, lptdev, LPPRI | PCATCH, "lptinit",
hz / 4) != EWOULDBLOCK) {
lpt_release_ppbus(lptdev);
sc->sc_state = 0;
ppb_unlock(ppbus);
return (EBUSY);
}
} while ((ppb_rstr(ppbus) & RDY_MASK) != LP_READY);
sc->sc_control = LPC_SEL|LPC_NINIT;
if (sc->sc_flags & LP_AUTOLF)
sc->sc_control |= LPC_AUTOL;
if (sc->sc_irq & LP_USE_IRQ)
sc->sc_control |= LPC_ENA;
ppb_wctr(ppbus, sc->sc_control);
sc->sc_state &= ~LPTINIT;
sc->sc_state |= OPEN;
sc->sc_xfercnt = 0;
lprintf(("irq %x\n", sc->sc_irq));
if (sc->sc_irq & LP_USE_IRQ) {
sc->sc_state |= TOUT;
sc->sc_backoff = hz / LPTOUTINITIAL;
callout_reset(&sc->sc_timer, sc->sc_backoff, lptout, sc);
}
lpt_release_ppbus(lptdev);
ppb_unlock(ppbus);
lprintf(("opened.\n"));
return(0);
}
static int
lptclose(struct cdev *dev, int flags, int fmt, struct thread *td)
{
struct lpt_data *sc = dev->si_drv1;
device_t lptdev = sc->sc_dev;
device_t ppbus = device_get_parent(lptdev);
int err;
ppb_lock(ppbus);
if (sc->sc_flags & LP_BYPASS)
goto end_close;
if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
ppb_unlock(ppbus);
return (err);
}
if ((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
while ((ppb_rstr(ppbus) & RDY_MASK) != LP_READY || sc->sc_xfercnt)
if (ppb_sleep(ppbus, lptdev, LPPRI | PCATCH, "lpclose",
hz) != EWOULDBLOCK)
break;
sc->sc_state &= ~OPEN;
callout_stop(&sc->sc_timer);
ppb_wctr(ppbus, LPC_NINIT);
lpt_release_ppbus(lptdev);
end_close:
sc->sc_state = 0;
sc->sc_xfercnt = 0;
ppb_unlock(ppbus);
lprintf(("closed.\n"));
return(0);
}
static int
lpt_pushbytes(struct lpt_data *sc)
{
device_t dev = sc->sc_dev;
device_t ppbus = device_get_parent(dev);
int spin, err, tic;
char ch;
ppb_assert_locked(ppbus);
lprintf(("p"));
while (sc->sc_xfercnt > 0) {
ch = *(sc->sc_cp);
sc->sc_cp++;
sc->sc_xfercnt--;
for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin)
DELAY(1);
if (spin >= MAX_SPIN) {
tic = 0;
while (NOT_READY(ppbus)) {
tic = tic + tic + 1;
if (tic > MAX_SLEEP)
tic = MAX_SLEEP;
err = ppb_sleep(ppbus, dev, LPPRI,
LPT_NAME "poll", tic);
if (err != EWOULDBLOCK) {
return (err);
}
}
}
ppb_wdtr(ppbus, ch);
ppb_wctr(ppbus, sc->sc_control|LPC_STB);
ppb_wctr(ppbus, sc->sc_control);
}
return(0);
}
static int
lptread(struct cdev *dev, struct uio *uio, int ioflag)
{
struct lpt_data *sc = dev->si_drv1;
device_t lptdev = sc->sc_dev;
device_t ppbus = device_get_parent(lptdev);
int error = 0, len;
if (sc->sc_flags & LP_BYPASS) {
return (EPERM);
}
ppb_lock(ppbus);
if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0))) {
ppb_unlock(ppbus);
return (error);
}
len = 0;
while (uio->uio_resid) {
if ((error = ppb_1284_read(ppbus, PPB_NIBBLE,
sc->sc_statbuf, min(BUFSTATSIZE,
uio->uio_resid), &len))) {
goto error;
}
if (!len)
goto error;
ppb_unlock(ppbus);
error = uiomove(sc->sc_statbuf, len, uio);
ppb_lock(ppbus);
if (error)
goto error;
}
error:
ppb_1284_terminate(ppbus);
ppb_unlock(ppbus);
return (error);
}
static int
lptwrite(struct cdev *dev, struct uio *uio, int ioflag)
{
register unsigned n;
int err;
struct lpt_data *sc = dev->si_drv1;
device_t lptdev = sc->sc_dev;
device_t ppbus = device_get_parent(lptdev);
if (sc->sc_flags & LP_BYPASS) {
return (EPERM);
}
ppb_lock(ppbus);
if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
ppb_unlock(ppbus);
return (err);
}
sc->sc_state &= ~INTERRUPTED;
while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
sc->sc_cp = sc->sc_inbuf;
ppb_unlock(ppbus);
err = uiomove(sc->sc_cp, n, uio);
ppb_lock(ppbus);
if (err)
break;
sc->sc_xfercnt = n;
if (sc->sc_irq & LP_ENABLE_EXT) {
err = ppb_write(ppbus, sc->sc_cp,
sc->sc_xfercnt, 0);
switch (err) {
case 0:
sc->sc_xfercnt = 0;
break;
case EINTR:
sc->sc_state |= INTERRUPTED;
ppb_unlock(ppbus);
return (err);
case EINVAL:
log(LOG_NOTICE,
"%s: advanced mode not avail, polling\n",
device_get_nameunit(sc->sc_dev));
break;
default:
ppb_unlock(ppbus);
return (err);
}
} else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
lprintf(("i"));
if ((sc->sc_state & OBUSY) == 0){
lprintf(("\nC %d. ", sc->sc_xfercnt));
lptintr(sc);
}
lprintf(("W "));
if (sc->sc_state & OBUSY)
if ((err = ppb_sleep(ppbus, lptdev,
LPPRI|PCATCH, LPT_NAME "write", 0))) {
sc->sc_state |= INTERRUPTED;
ppb_unlock(ppbus);
return(err);
}
}
if (!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
lprintf(("p"));
err = lpt_pushbytes(sc);
if (err) {
ppb_unlock(ppbus);
return (err);
}
}
}
lpt_release_ppbus(lptdev);
ppb_unlock(ppbus);
return (err);
}
static void
lptintr(void *arg)
{
struct lpt_data *sc = arg;
device_t lptdev = sc->sc_dev;
device_t ppbus = device_get_parent(lptdev);
int sts = 0;
int i;
for (i = 0; i < 100 &&
((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ;
if ((sts & RDY_MASK) == LP_READY) {
sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
sc->sc_backoff = hz / LPTOUTINITIAL;
if (sc->sc_xfercnt) {
ppb_wdtr(ppbus, *sc->sc_cp++) ;
ppb_wctr(ppbus, sc->sc_control|LPC_STB);
ppb_wctr(ppbus, sc->sc_control);
if (--(sc->sc_xfercnt) > 0)
return;
}
sc->sc_state &= ~OBUSY;
if (!(sc->sc_state & INTERRUPTED))
wakeup(lptdev);
lprintf(("w "));
return;
} else {
if (((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
(sc->sc_state & OPEN))
sc->sc_state |= EERROR;
}
lprintf(("sts %x ", sts));
}
static int
lptioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
{
int error = 0;
struct lpt_data *sc = dev->si_drv1;
device_t ppbus;
u_char old_sc_irq;
switch (cmd) {
case LPT_IRQ :
ppbus = device_get_parent(sc->sc_dev);
ppb_lock(ppbus);
if (sc->sc_irq & LP_HAS_IRQ) {
old_sc_irq = sc->sc_irq;
switch (*(int*)data) {
case 0:
sc->sc_irq &= (~LP_ENABLE_IRQ);
break;
case 1:
sc->sc_irq &= (~LP_ENABLE_EXT);
sc->sc_irq |= LP_ENABLE_IRQ;
break;
case 2:
sc->sc_irq &= (~LP_ENABLE_IRQ);
sc->sc_irq |= LP_ENABLE_EXT;
break;
case 3:
sc->sc_irq &= (~LP_ENABLE_EXT);
break;
default:
break;
}
if (old_sc_irq != sc->sc_irq )
log(LOG_NOTICE, "%s: switched to %s %s mode\n",
device_get_nameunit(sc->sc_dev),
(sc->sc_irq & LP_ENABLE_IRQ)?
"interrupt-driven":"polled",
(sc->sc_irq & LP_ENABLE_EXT)?
"extended":"standard");
} else
error = EOPNOTSUPP;
ppb_unlock(ppbus);
break;
default:
error = ENODEV;
}
return(error);
}
static device_method_t lpt_methods[] = {
DEVMETHOD(device_identify, lpt_identify),
DEVMETHOD(device_probe, lpt_probe),
DEVMETHOD(device_attach, lpt_attach),
DEVMETHOD(device_detach, lpt_detach),
DEVMETHOD_END
};
static driver_t lpt_driver = {
LPT_NAME,
lpt_methods,
sizeof(struct lpt_data),
};
DRIVER_MODULE(lpt, ppbus, lpt_driver, 0, 0);
MODULE_DEPEND(lpt, ppbus, 1, 1, 1);