#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/device.h>
#include <sys/kernel.h>
#include <sys/uio.h>
#include <sys/syslog.h>
#include <sys/thread2.h>
#include <sys/malloc.h>
#include <sys/rman.h>
#include <machine/clock.h>
#include <machine/inttypes.h>
#include "lptio.h"
#include <bus/ppbus/ppbconf.h>
#include <bus/ppbus/ppb_1284.h>
#include "lpt.h"
#include "ppbus_if.h"
#include <bus/ppbus/ppbio.h>
MALLOC_DEFINE(M_LPT, "lpt", "LPT buffers");
#ifndef LPT_DEBUG
#define lprintf(args)
#else
#define lprintf(args) \
do { \
if (lptflag) \
kprintf args; \
} while (0)
static int volatile lptflag = 1;
#endif
#define LPINITRDY 4
#define LPTOUTINITIAL 10
#define LPTOUTMAX 1
#define BUFSIZE 1024
#define BUFSTATSIZE 32
#define LPTUNIT(s) ((s)&0x03)
#define LPTFLAGS(s) ((s)&0xfc)
struct lpt_data {
short sc_state;
u_char sc_control;
char sc_flags;
#define LP_UNITMASK 0x03
#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 resource *intr_resource;
void *intr_cookie;
struct callout sc_callout;
};
#define LPT_NAME "lpt"
static timeout_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))
#define UNITOSOFTC(unit) \
((struct lpt_data *)devclass_get_softc(lpt_devclass, (unit)))
#define UNITODEVICE(unit) \
(devclass_get_device(lpt_devclass, (unit)))
static void lptintr(device_t dev);
static void lpt_intr(void *arg);
static devclass_t lpt_devclass;
#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 dev_ops lpt_ops = {
{ LPT_NAME, 0, 0 },
.d_open = lptopen,
.d_close = lptclose,
.d_read = lptread,
.d_write = lptwrite,
.d_ioctl = lptioctl,
};
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;
if (sc->sc_state & HAVEBUS)
return (0);
if ((error = ppb_request_bus(ppbus, dev, how)) == 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;
if ((error = ppb_release_bus(ppbus, dev)) == 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;
if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
kprintf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
status = 0;
goto end_probe;
}
for (i = 0; i < 18 && status; i++)
if (!lpt_port_test(ppbus, testbyte[i], 0xff)) {
status = 0;
goto end_probe;
}
end_probe:
ppb_wdtr(ppbus, 0);
ppb_wctr(ppbus, 0);
lpt_release_ppbus(dev);
return (status);
}
static int
lpt_probe(device_t dev)
{
struct lpt_data *sc;
sc = DEVTOSOFTC(dev);
bzero(sc, sizeof(struct lpt_data));
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 zero = 0, unit = device_get_unit(dev);
int error;
uintptr_t irq;
sc->sc_primed = 0;
callout_init(&sc->sc_callout);
if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
kprintf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
return (0);
}
ppb_wctr(ppbus, LPC_NINIT);
lprintf(("oldirq %x\n", sc->sc_irq));
BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
if (irq > 0) {
sc->intr_resource = bus_alloc_legacy_irq_resource(dev, &zero,
irq, RF_SHAREABLE);
}
if (sc->intr_resource) {
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 %"PRIxPTR" %x\n", irq, sc->sc_irq));
lpt_release_ppbus(dev);
make_dev(&lpt_ops, unit, UID_ROOT, GID_WHEEL,
0600, LPT_NAME "%d", unit);
make_dev(&lpt_ops, unit | LP_BYPASS, UID_ROOT, GID_WHEEL,
0600, LPT_NAME "%d.ctl", unit);
return (0);
}
static void
lptout(void *arg)
{
device_t dev = (device_t)arg;
struct lpt_data *sc = DEVTOSOFTC(dev);
#ifdef LPT_DEBUG
device_t ppbus = device_get_parent(dev);
#endif
lprintf(("T %x ", ppb_rstr(ppbus)));
if (sc->sc_state & OPEN) {
sc->sc_backoff++;
if (sc->sc_backoff > hz/LPTOUTMAX)
sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
callout_reset(&sc->sc_callout, sc->sc_backoff, lptout, dev);
} else {
sc->sc_state &= ~TOUT;
}
if (sc->sc_state & EERROR)
sc->sc_state &= ~EERROR;
if (sc->sc_xfercnt) {
lptintr(dev);
} else {
sc->sc_state &= ~OBUSY;
wakeup((caddr_t)dev);
}
}
static int
lptopen(struct dev_open_args *ap)
{
cdev_t dev = ap->a_head.a_dev;
int trys, err;
u_int unit = LPTUNIT(minor(dev));
struct lpt_data *sc = UNITOSOFTC(unit);
device_t lptdev = UNITODEVICE(unit);
device_t ppbus = device_get_parent(lptdev);
if (!sc)
return (ENXIO);
if (sc->sc_state) {
lprintf((LPT_NAME ": still open %x\n", sc->sc_state));
return(EBUSY);
} else
sc->sc_state |= LPTINIT;
sc->sc_flags = LPTFLAGS(minor(dev));
if (sc->sc_flags & LP_BYPASS) {
sc->sc_state = OPEN;
return(0);
}
if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
sc->sc_state = 0;
return (err);
}
crit_enter();
lprintf((LPT_NAME " flags 0x%x\n", 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) {
crit_exit();
sc->sc_state = 0;
lprintf(("status %x\n", ppb_rstr(ppbus)));
lpt_release_ppbus(lptdev);
return (EBUSY);
}
if (tsleep((caddr_t)lptdev, PCATCH, "lptinit", hz/4) !=
EWOULDBLOCK) {
sc->sc_state = 0;
crit_exit();
lpt_release_ppbus(lptdev);
return (EBUSY);
}
} while ((ppb_rstr(ppbus) &
(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
(LPS_SEL|LPS_NBSY|LPS_NERR));
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 = OPEN;
sc->sc_inbuf = kmalloc(BUFSIZE, M_LPT, M_WAITOK);
sc->sc_statbuf = kmalloc(BUFSTATSIZE, M_LPT, M_WAITOK);
sc->sc_xfercnt = 0;
crit_exit();
lpt_release_ppbus(lptdev);
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_callout, sc->sc_backoff, lptout, lptdev);
}
lprintf(("opened.\n"));
return(0);
}
static int
lptclose(struct dev_close_args *ap)
{
cdev_t dev = ap->a_head.a_dev;
u_int unit = LPTUNIT(minor(dev));
struct lpt_data *sc = UNITOSOFTC(unit);
device_t lptdev = UNITODEVICE(unit);
device_t ppbus = device_get_parent(lptdev);
int err;
if(sc->sc_flags & LP_BYPASS)
goto end_close;
if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
return (err);
sc->sc_state &= ~OPEN;
if((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ)) {
while ((ppb_rstr(ppbus) &
(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
(LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt) {
if (tsleep((caddr_t)lptdev, PCATCH,
"lpclose", hz) != EWOULDBLOCK) {
break;
}
}
}
callout_stop(&sc->sc_callout);
ppb_wctr(ppbus, LPC_NINIT);
kfree(sc->sc_inbuf, M_LPT);
kfree(sc->sc_statbuf, M_LPT);
end_close:
lpt_release_ppbus(lptdev);
sc->sc_state = 0;
sc->sc_xfercnt = 0;
lprintf(("closed.\n"));
return(0);
}
static int
lpt_pushbytes(device_t dev)
{
struct lpt_data *sc = DEVTOSOFTC(dev);
device_t ppbus = device_get_parent(dev);
int spin, err, tic;
char ch;
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 = tsleep((caddr_t)dev, 0,
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 dev_read_args *ap)
{
cdev_t dev = ap->a_head.a_dev;
struct uio *uio = ap->a_uio;
u_int unit = LPTUNIT(minor(dev));
struct lpt_data *sc = UNITOSOFTC(unit);
device_t lptdev = UNITODEVICE(unit);
device_t ppbus = device_get_parent(lptdev);
int error = 0, len;
if (sc->sc_flags & LP_BYPASS) {
return (EPERM);
}
if ((error = ppb_1284_negotiate(ppbus, PPB_NIBBLE, 0)))
return (error);
len = 0;
while (uio->uio_resid) {
error = ppb_1284_read(ppbus, PPB_NIBBLE, sc->sc_statbuf,
(int)szmin(BUFSTATSIZE, uio->uio_resid),
&len);
if (error)
goto error;
if (!len)
goto error;
if ((error = uiomove(sc->sc_statbuf, (size_t)len, uio)))
goto error;
}
error:
ppb_1284_terminate(ppbus);
return (error);
}
static int
lptwrite(struct dev_write_args *ap)
{
cdev_t dev = ap->a_head.a_dev;
struct uio *uio = ap->a_uio;
unsigned n;
int err;
u_int unit = LPTUNIT(minor(dev));
struct lpt_data *sc = UNITOSOFTC(unit);
device_t lptdev = UNITODEVICE(unit);
device_t ppbus = device_get_parent(lptdev);
if (sc->sc_flags & LP_BYPASS) {
return (EPERM);
}
if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
return (err);
if (sc->sc_irq & LP_USE_IRQ) {
err = BUS_SETUP_INTR(ppbus, lptdev, sc->intr_resource,
0, lpt_intr, lptdev,
&sc->intr_cookie, NULL, NULL);
if (err) {
device_printf(lptdev, "handler registration failed, polled mode.\n");
sc->sc_irq &= ~LP_USE_IRQ;
}
}
sc->sc_state &= ~INTERRUPTED;
while ((n = (unsigned)szmin(BUFSIZE, uio->uio_resid)) != 0) {
sc->sc_cp = sc->sc_inbuf;
uiomove(sc->sc_cp, (size_t)n, uio);
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;
return (err);
case EINVAL:
log(LOG_NOTICE, LPT_NAME "%d: advanced mode not avail, polling\n", unit);
break;
default:
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(lptdev);
}
lprintf(("W "));
if (sc->sc_state & OBUSY)
if ((err = tsleep((caddr_t)lptdev,
PCATCH, LPT_NAME "write", 0))) {
sc->sc_state |= INTERRUPTED;
return(err);
}
}
if (!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
lprintf(("p"));
err = lpt_pushbytes(lptdev);
if (err)
return (err);
}
}
lpt_release_ppbus(lptdev);
return (0);
}
static void
lpt_intr(void *arg)
{
device_t lptdev = (device_t)arg;
device_t ppbus = device_get_parent(lptdev);
struct lpt_data *sc = DEVTOSOFTC(lptdev);
int sts = 0;
int i;
if ((sc->sc_state & HAVEBUS) == 0)
return;
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((caddr_t)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 void
lptintr(device_t dev)
{
crit_enter();
lpt_intr(dev);
crit_exit();
}
static int
lptioctl(struct dev_ioctl_args *ap)
{
cdev_t dev = ap->a_head.a_dev;
int error = 0;
u_int unit = LPTUNIT(minor(dev));
struct lpt_data *sc = UNITOSOFTC(unit);
u_char old_sc_irq;
switch (ap->a_cmd) {
case LPT_IRQ :
if (sc->sc_irq & LP_HAS_IRQ) {
old_sc_irq = sc->sc_irq;
switch (*(int*)ap->a_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, LPT_NAME "%d: switched to %s %s mode\n",
unit,
(sc->sc_irq & LP_ENABLE_IRQ)?
"interrupt-driven":"polled",
(sc->sc_irq & LP_ENABLE_EXT)?
"extended":"standard");
} else
error = EOPNOTSUPP;
break;
default:
error = ENODEV;
}
return(error);
}
static device_method_t lpt_methods[] = {
DEVMETHOD(device_identify, bus_generic_identify),
DEVMETHOD(device_probe, lpt_probe),
DEVMETHOD(device_attach, lpt_attach),
DEVMETHOD_END
};
static driver_t lpt_driver = {
LPT_NAME,
lpt_methods,
sizeof(struct lpt_data),
};
DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, NULL, NULL);