#include "opt_ppb_1284.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/fcntl.h>
#include <sys/rman.h>
#include <machine/clock.h>
#include <bus/ppbus/ppbconf.h>
#include <bus/ppbus/ppb_msq.h>
#ifdef PERIPH_1284
#include <bus/ppbus/ppb_1284.h>
#endif
#include "ppi.h"
#include "ppbus_if.h"
#include <bus/ppbus/ppbio.h>
#define BUFSIZE 512
struct ppi_data {
int ppi_unit;
int ppi_flags;
#define HAVE_PPBUS (1<<0)
#define HAD_PPBUS (1<<1)
int ppi_count;
int ppi_mode;
char ppi_buffer[BUFSIZE];
#ifdef PERIPH_1284
struct resource *intr_resource;
void *intr_cookie;
#endif
};
#define DEVTOSOFTC(dev) \
((struct ppi_data *)device_get_softc(dev))
#define UNITOSOFTC(unit) \
((struct ppi_data *)devclass_get_softc(ppi_devclass, (unit)))
#define UNITODEVICE(unit) \
(devclass_get_device(ppi_devclass, (unit)))
static devclass_t ppi_devclass;
static d_open_t ppiopen;
static d_close_t ppiclose;
static d_ioctl_t ppiioctl;
static d_write_t ppiwrite;
static d_read_t ppiread;
static struct dev_ops ppi_ops = {
{ "ppi", 0, 0 },
.d_open = ppiopen,
.d_close = ppiclose,
.d_read = ppiread,
.d_write = ppiwrite,
.d_ioctl = ppiioctl,
};
#ifdef PERIPH_1284
static void
ppi_enable_intr(device_t ppidev)
{
char r;
device_t ppbus = device_get_parent(ppidev);
r = ppb_rctr(ppbus);
ppb_wctr(ppbus, r | IRQENABLE);
return;
}
static void
ppi_disable_intr(device_t ppidev)
{
char r;
device_t ppbus = device_get_parent(ppidev);
r = ppb_rctr(ppbus);
ppb_wctr(ppbus, r & ~IRQENABLE);
return;
}
#endif
static int
ppi_probe(device_t dev)
{
struct ppi_data *ppi;
device_set_desc(dev, "Parallel I/O");
ppi = DEVTOSOFTC(dev);
bzero(ppi, sizeof(struct ppi_data));
return (0);
}
static int
ppi_attach(device_t dev)
{
#ifdef PERIPH_1284
uintptr_t irq;
int zero = 0;
struct ppi_data *ppi = DEVTOSOFTC(dev);
BUS_READ_IVAR(device_get_parent(dev), dev, PPBUS_IVAR_IRQ, &irq);
ppi->intr_resource = bus_alloc_legacy_irq_resource(dev, &zero, irq,
RF_ACTIVE);
#endif
make_dev(&ppi_ops, device_get_unit(dev),
UID_ROOT, GID_WHEEL,
0600, "ppi%d", device_get_unit(dev));
return (0);
}
#ifdef PERIPH_1284
static void
ppiintr(void *arg)
{
device_t ppidev = (device_t)arg;
device_t ppbus = device_get_parent(ppidev);
struct ppi_data *ppi = DEVTOSOFTC(ppidev);
ppi_disable_intr(ppidev);
switch (ppb_1284_get_state(ppbus)) {
case PPB_FORWARD_IDLE:
if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
(SELECT | nBUSY)) {
#ifdef DEBUG_1284
kprintf("N");
#endif
ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
ppb_1284_set_state(ppbus, PPB_NEGOTIATION);
} else {
#ifdef DEBUG_1284
kprintf("0x%x", ppb_rstr(ppbus));
#endif
ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
break;
}
wakeup(ppi);
break;
default:
#ifdef DEBUG_1284
kprintf("?%d", ppb_1284_get_state(ppbus));
#endif
ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
ppb_set_mode(ppbus, PPB_COMPATIBLE);
break;
}
ppi_enable_intr(ppidev);
return;
}
#endif
static int
ppiopen(struct dev_open_args *ap)
{
cdev_t dev = ap->a_head.a_dev;
u_int unit = minor(dev);
struct ppi_data *ppi = UNITOSOFTC(unit);
device_t ppidev = UNITODEVICE(unit);
device_t ppbus = device_get_parent(ppidev);
int res;
if (!ppi)
return (ENXIO);
if (!(ppi->ppi_flags & HAVE_PPBUS)) {
if ((res = ppb_request_bus(ppbus, ppidev,
(ap->a_oflags & O_NONBLOCK) ? PPB_DONTWAIT :
(PPB_WAIT | PPB_INTR))))
return (res);
ppi->ppi_flags |= HAVE_PPBUS;
#ifdef PERIPH_1284
if (ppi->intr_resource) {
BUS_SETUP_INTR(device_get_parent(ppidev), ppidev,
ppi->intr_resource, 0,
ppiintr, dev,
&ppi->intr_cookie, NULL, NULL);
}
#endif
}
ppi->ppi_count += 1;
return (0);
}
static int
ppiclose(struct dev_close_args *ap)
{
cdev_t dev = ap->a_head.a_dev;
u_int unit = minor(dev);
struct ppi_data *ppi = UNITOSOFTC(unit);
device_t ppidev = UNITODEVICE(unit);
device_t ppbus = device_get_parent(ppidev);
ppi->ppi_count --;
if (!ppi->ppi_count) {
#ifdef PERIPH_1284
switch (ppb_1284_get_state(ppbus)) {
case PPB_PERIPHERAL_IDLE:
ppb_peripheral_terminate(ppbus, 0);
break;
case PPB_REVERSE_IDLE:
case PPB_EPP_IDLE:
case PPB_ECP_FORWARD_IDLE:
default:
ppb_1284_terminate(ppbus);
break;
}
#endif
ppb_release_bus(ppbus, ppidev);
ppi->ppi_flags &= ~HAVE_PPBUS;
}
return (0);
}
static int
ppiread(struct dev_read_args *ap)
{
#ifdef PERIPH_1284
cdev_t dev = ap->a_head.a_dev;
struct uio *uio = ap->a_uio;
u_int unit = minor(dev);
struct ppi_data *ppi = UNITOSOFTC(unit);
device_t ppidev = UNITODEVICE(unit);
device_t ppbus = device_get_parent(ppidev);
int len, error = 0;
switch (ppb_1284_get_state(ppbus)) {
case PPB_PERIPHERAL_IDLE:
ppb_peripheral_terminate(ppbus, 0);
case PPB_FORWARD_IDLE:
if ((ppb_1284_negotiate(ppbus,
ppi->ppi_mode = PPB_NIBBLE, 0))) {
tsleep(ppi, 0, "ppiread", 2*hz);
if ((error = ppb_1284_negotiate(ppbus,
ppi->ppi_mode = PPB_BYTE, 0)))
return (error);
}
break;
case PPB_REVERSE_IDLE:
case PPB_EPP_IDLE:
case PPB_ECP_FORWARD_IDLE:
default:
break;
}
#ifdef DEBUG_1284
kprintf("N");
#endif
len = 0;
while (uio->uio_resid) {
error = ppb_1284_read(ppbus, ppi->ppi_mode, ppi->ppi_buffer,
(int)szmin(BUFSIZE, uio->uio_resid),
&len);
if (error)
goto error;
if (!len)
goto error;
#ifdef DEBUG_1284
kprintf("d");
#endif
if ((error = uiomove(ppi->ppi_buffer, (size_t)len, uio)))
goto error;
}
error:
#else
int error = ENODEV;
#endif
return (error);
}
static int
ppiwrite(struct dev_write_args *ap)
{
#ifdef PERIPH_1284
cdev_t dev = ap->a_head.a_dev;
struct uio *uio = ap->a_uio;
u_int unit = minor(dev);
struct ppi_data *ppi = UNITOSOFTC(unit);
device_t ppidev = UNITODEVICE(unit);
device_t ppbus = device_get_parent(ppidev);
int len, error = 0, sent;
#if 0
int ret;
#define ADDRESS MS_PARAM(0, 0, MS_TYP_PTR)
#define LENGTH MS_PARAM(0, 1, MS_TYP_INT)
struct ppb_microseq msq[] = {
{ MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
MS_RET(0)
};
if (ppb_1284_negotiate(ppbus, PPB_ECP, 0)) {
kprintf("ppiwrite: ECP negotiation failed\n");
}
while (!error && (len = (int)szmin(uio->uio_resid, BUFSIZE))) {
uiomove(ppi->ppi_buffer, (size_t)len, uio);
ppb_MS_init_msq(msq, 2, ADDRESS, ppi->ppi_buffer, LENGTH, len);
error = ppb_MS_microseq(ppbus, msq, &ret);
}
#endif
if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOTIATION)
ppb_1284_terminate(ppbus);
while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
#ifdef DEBUG_1284
kprintf("s");
#endif
ppi_enable_intr(ppidev);
error = tsleep(ppi, PCATCH, "ppiwrite", 0);
switch (error) {
case 0:
ppb_peripheral_negotiate(ppbus, PPB_BYTE, 0);
break;
case EWOULDBLOCK:
break;
default:
goto error;
}
}
#ifdef DEBUG_1284
kprintf("N");
#endif
while ((len = (int)szmin(uio->uio_resid, BUFSIZE)) != 0) {
uiomove(ppi->ppi_buffer, (size_t)len, uio);
if ((error = byte_peripheral_write(ppbus,
ppi->ppi_buffer, len, &sent)))
goto error;
#ifdef DEBUG_1284
kprintf("d");
#endif
}
error:
#else
int error = ENODEV;
#endif
return (error);
}
static int
ppiioctl(struct dev_ioctl_args *ap)
{
cdev_t dev = ap->a_head.a_dev;
u_int unit = minor(dev);
device_t ppidev = UNITODEVICE(unit);
device_t ppbus = device_get_parent(ppidev);
int error = 0;
u_int8_t *val = (u_int8_t *)ap->a_data;
switch (ap->a_cmd) {
case PPIGDATA:
*val = ppb_rdtr(ppbus);
break;
case PPIGSTATUS:
*val = ppb_rstr(ppbus);
break;
case PPIGCTRL:
*val = ppb_rctr(ppbus);
break;
case PPIGEPPD:
*val = ppb_repp_D(ppbus);
break;
case PPIGECR:
*val = ppb_recr(ppbus);
break;
case PPIGFIFO:
*val = ppb_rfifo(ppbus);
break;
case PPISDATA:
ppb_wdtr(ppbus, *val);
break;
case PPISSTATUS:
ppb_wstr(ppbus, *val);
break;
case PPISCTRL:
ppb_wctr(ppbus, *val);
break;
case PPISEPPD:
ppb_wepp_D(ppbus, *val);
break;
case PPISECR:
ppb_wecr(ppbus, *val);
break;
case PPISFIFO:
ppb_wfifo(ppbus, *val);
break;
case PPIGEPPA:
*val = ppb_repp_A(ppbus);
break;
case PPISEPPA:
ppb_wepp_A(ppbus, *val);
break;
default:
error = ENOTTY;
break;
}
return (error);
}
static device_method_t ppi_methods[] = {
DEVMETHOD(device_identify, bus_generic_identify),
DEVMETHOD(device_probe, ppi_probe),
DEVMETHOD(device_attach, ppi_attach),
DEVMETHOD_END
};
static driver_t ppi_driver = {
"ppi",
ppi_methods,
sizeof(struct ppi_data),
};
DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, NULL, NULL);