#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ppbus_1284.c,v 1.14 2021/11/14 20:51:57 andvar Exp $");
#include "opt_ppbus_1284.h"
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <dev/ppbus/ppbus_conf.h>
#include <dev/ppbus/ppbus_base.h>
#include <dev/ppbus/ppbus_1284.h>
#include <dev/ppbus/ppbus_io.h>
#include <dev/ppbus/ppbus_var.h>
static int
do_1284_wait(struct ppbus_softc * bus, char mask, char status)
{
return (ppbus_poll_bus(bus->sc_dev, 4, mask, status,
PPBUS_NOINTR | PPBUS_POLL));
}
static int
do_peripheral_wait(struct ppbus_softc * bus, char mask, char status)
{
return (ppbus_poll_bus(bus->sc_dev, 100, mask, status,
PPBUS_NOINTR | PPBUS_POLL));
}
static int
ppbus_1284_reset_error(struct ppbus_softc * bus, int state)
{
bus->sc_1284_error = PPBUS_NO_ERROR;
bus->sc_1284_state = state;
return 0;
}
int
ppbus_1284_get_state(device_t dev)
{
struct ppbus_softc *sc = device_private(dev);
return sc->sc_1284_state;
}
int
ppbus_1284_set_state(device_t dev, int state)
{
struct ppbus_softc * bus = device_private(dev);
if ((bus->sc_1284_state != PPBUS_ERROR) &&
(bus->sc_1284_error == PPBUS_NO_ERROR)) {
bus->sc_1284_state = state;
bus->sc_1284_error = PPBUS_NO_ERROR;
}
return 0;
}
static int
ppbus_1284_set_error(struct ppbus_softc * bus, int error, int event)
{
if ((bus->sc_1284_error == PPBUS_NO_ERROR) &&
(bus->sc_1284_state != PPBUS_ERROR)) {
bus->sc_1284_error = error;
bus->sc_1284_state = PPBUS_ERROR;
}
#ifdef DEBUG_1284
printf("%s<1284>: error=%d status=0x%x event=%d\n",
device_xname(bus->sc_dev), error, ppbus_rstr(bus->sc_dev),
event);
#endif
return 0;
}
static int
ppbus_request_mode(int mode, int options)
{
int request_mode = 0;
if (options & PPBUS_EXTENSIBILITY_LINK) {
request_mode = EXT_LINK_1284_NORMAL;
}
else {
switch (mode) {
case PPBUS_NIBBLE:
request_mode = (options & PPBUS_REQUEST_ID) ?
NIBBLE_1284_REQUEST_ID :
NIBBLE_1284_NORMAL;
break;
case PPBUS_PS2:
request_mode = (options & PPBUS_REQUEST_ID) ?
BYTE_1284_REQUEST_ID :
BYTE_1284_NORMAL;
break;
case PPBUS_ECP:
if (options & PPBUS_USE_RLE)
request_mode = (options & PPBUS_REQUEST_ID) ?
ECP_1284_RLE_REQUEST_ID :
ECP_1284_RLE;
else
request_mode = (options & PPBUS_REQUEST_ID) ?
ECP_1284_REQUEST_ID :
ECP_1284_NORMAL;
break;
case PPBUS_EPP:
request_mode = EPP_1284_NORMAL;
break;
default:
panic("%s: unsupported mode %d\n", __func__, mode);
}
}
return (request_mode);
}
int
ppbus_peripheral_negotiate(device_t dev, int mode, int options)
{
struct ppbus_softc * bus = device_private(dev);
int spin, request_mode, error = 0;
char r;
ppbus_1284_terminate(dev);
ppbus_1284_set_state(dev, PPBUS_PERIPHERAL_NEGOTIATION);
request_mode = ppbus_request_mode(mode, options);
spin = 10;
while (spin-- && (ppbus_rstr(dev) & nBUSY))
DELAY(1);
if (!(ppbus_rstr(dev) & SELECT) || !spin) {
error = ENODEV;
goto error;
}
r = ppbus_rdtr(dev);
if ((r == (char)request_mode) ||
(r == NIBBLE_1284_NORMAL)) {
ppbus_wctr(dev, (STROBE | nINIT) & ~(SELECTIN));
DELAY(1);
ppbus_wctr(dev, (nINIT) & ~(SELECTIN | STROBE));
if (r == NIBBLE_1284_NORMAL) {
#ifdef DEBUG_1284
printf("R");
#endif
ppbus_1284_set_error(bus, PPBUS_MODE_UNSUPPORTED, 4);
error = EINVAL;
goto error;
}
else {
ppbus_1284_set_state(dev, PPBUS_PERIPHERAL_IDLE);
#ifdef DEBUG_1284
printf("A");
#endif
}
}
else {
ppbus_wctr(dev, SELECTIN);
DELAY(1);
ppbus_wctr(dev, (SELECTIN) & ~(STROBE | nINIT));
ppbus_1284_set_error(bus, PPBUS_MODE_UNSUPPORTED, 4);
#ifdef DEBUG_1284
printf("r");
#endif
error = EINVAL;
goto error;
}
return (0);
error:
ppbus_peripheral_terminate(dev, PPBUS_WAIT);
return (error);
}
int
ppbus_peripheral_terminate(device_t dev, int how)
{
struct ppbus_softc * bus = device_private(dev);
int error = 0;
#ifdef DEBUG_1284
printf("t");
#endif
ppbus_1284_set_state(dev, PPBUS_PERIPHERAL_TERMINATION);
if ((error = do_peripheral_wait(bus, SELECT | nBUSY, 0))) {
ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 22);
goto error;
}
ppbus_wctr(dev, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
if ((error = do_peripheral_wait(bus, nBUSY, nBUSY))) {
ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 25);
goto error;
}
ppbus_wctr(dev, (SELECTIN | nINIT | STROBE) & ~(AUTOFEED));
DELAY(1);
ppbus_wctr(dev, (SELECTIN | nINIT) & ~(STROBE | AUTOFEED));
if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 28);
goto error;
}
error:
ppbus_1284_terminate(dev);
ppbus_1284_set_state(dev, PPBUS_FORWARD_IDLE);
return (0);
}
static int
byte_peripheral_outbyte(device_t dev, char *buffer, int last)
{
struct ppbus_softc * bus = device_private(dev);
int error = 0;
if ((error = do_1284_wait(bus, nBUSY, nBUSY))) {
ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 7);
goto error;
}
if (!(ppbus_rstr(dev) & SELECT)) {
ppbus_peripheral_terminate(dev, PPBUS_WAIT);
goto error;
}
#ifdef DEBUG_1284
printf("B");
#endif
ppbus_wdtr(dev, *buffer);
ppbus_wctr(dev, (AUTOFEED | STROBE) & ~(nINIT | SELECTIN));
if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 16);
goto error;
}
if (!last) {
ppbus_wctr(dev, (AUTOFEED) & ~(nINIT | STROBE | SELECTIN));
} else {
ppbus_wctr(dev, (nINIT) & ~(STROBE | SELECTIN | AUTOFEED));
}
#if 0
if ((error = do_peripheral_wait(bus, nACK | nBUSY, 0))) {
ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 16);
goto error;
}
#endif
if (!(ppbus_rstr(dev) & SELECT)) {
ppbus_peripheral_terminate(dev, PPBUS_WAIT);
goto error;
}
error:
return (error);
}
int
byte_peripheral_write(device_t dev, char *buffer, int len,
int *sent)
{
int error = 0, i;
char r;
ppbus_1284_set_state(dev, PPBUS_PERIPHERAL_TRANSFER);
for(i = 0; i < len; i++) {
r = ppbus_rctr(dev);
ppbus_wctr(dev, r & ~nINIT);
#ifdef DEBUG_1284
printf("y");
#endif
error = ppbus_poll_bus(dev, PPBUS_FOREVER, nBUSY, nBUSY,
PPBUS_INTR);
if (error && error != EWOULDBLOCK)
goto error;
#ifdef DEBUG_1284
printf("b");
#endif
if ((error = byte_peripheral_outbyte(dev, buffer+i, (i == len-1))))
goto error;
}
error:
if (!error)
ppbus_1284_set_state(dev, PPBUS_PERIPHERAL_IDLE);
*sent = i;
return (error);
}
int
ppbus_1284_read_id(device_t dev, int mode, char ** buffer,
size_t * size, size_t * read)
{
u_int16_t msg_sz;
u_int8_t length_field;
u_int8_t old_mode;
int error;
int old_ivar;
int new_ivar = 1;
error = ppbus_read_ivar(dev, PPBUS_IVAR_IEEE, &old_ivar);
if(error) {
printf("%s(%s): error reading PPBUS_IVAR_IEEE.\n", __func__,
device_xname(dev));
return error;
}
if(old_ivar == 0) {
error = ppbus_write_ivar(dev, PPBUS_IVAR_IEEE, &new_ivar);
if(error) {
printf("%s(%s): error enabling IEEE usage.\n", __func__,
device_xname(dev));
return error;
}
}
old_mode = ppbus_get_mode(dev);
switch (mode) {
case PPBUS_NIBBLE:
case PPBUS_ECP:
case PPBUS_BYTE:
error = ppbus_set_mode(dev, mode, PPBUS_REQUEST_ID);
if(error) {
printf("%s(%s): error setting bus mode.\n", __func__,
device_xname(dev));
goto end_read_id;
}
break;
default:
printf("%s(%s): mode does not support returning device ID.\n",
__func__, device_xname(dev));
error = ENODEV;
goto end_read_id;
}
error = ppbus_read(dev, &length_field, 1, 0, read);
if(error) {
printf("%s(%s): error reading first byte.\n", __func__,
device_xname(dev));
goto end_read_id;
}
msg_sz = length_field;
error = ppbus_read(dev, &length_field, 1, 0, read);
if(error) {
printf("%s(%s): error reading second byte.\n",
__func__, device_xname(dev));
goto end_read_id;
}
msg_sz <<= 8;
msg_sz |= length_field;
msg_sz -= 2;
if(msg_sz <= 0) {
printf("%s(%s): device ID length <= 0.\n", __func__,
device_xname(dev));
goto end_read_id;
}
*buffer = malloc(msg_sz, M_DEVBUF, M_WAITOK);
*size = msg_sz;
error = ppbus_read(dev, *buffer, msg_sz, 0, read);
end_read_id:
ppbus_set_mode(dev, old_mode, 0);
if(old_ivar == 0) {
if(ppbus_write_ivar(dev, PPBUS_IVAR_IEEE, &old_ivar)) {
printf("%s(%s): error restoring PPBUS_IVAR_IEEE.\n",
__func__, device_xname(dev));
}
}
return (error);
}
int
ppbus_1284_negotiate(device_t dev, int mode, int options)
{
struct ppbus_softc * bus = device_private(dev);
int error;
int request_mode;
#ifdef DEBUG_1284
printf("n");
#endif
if (ppbus_1284_get_state(dev) >= PPBUS_PERIPHERAL_NEGOTIATION)
ppbus_peripheral_terminate(dev, PPBUS_WAIT);
#ifdef DEBUG_1284
printf("%d", mode);
#endif
ppbus_1284_terminate(dev);
ppbus_1284_reset_error(bus, PPBUS_FORWARD_IDLE);
request_mode = ppbus_request_mode(mode, options);
ppbus_wctr(dev, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
DELAY(1);
ppbus_1284_set_state(dev, PPBUS_NEGOTIATION);
ppbus_wdtr(dev, request_mode);
#ifdef PERIPH_1284
ppbus_wctr(dev, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
DELAY(1);
ppbus_wctr(dev, (nINIT) & ~(STROBE | AUTOFEED | SELECTIN));
#else
DELAY(1);
#endif
ppbus_wctr(dev, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
#ifdef PERIPH_1284
if (ppbus_poll_bus(bus, 40, nACK | SELECT | nFAULT,
SELECT | nFAULT, PPBUS_NOINTR | PPBUS_POLL)) {
ppbus_1284_set_error(bus, PPBUS_NOT_IEEE1284, 2);
error = ENODEV;
goto error;
}
#else
if (do_1284_wait(bus, nACK | PERROR | SELECT | nFAULT,
PERROR | SELECT | nFAULT)) {
ppbus_1284_set_error(bus, PPBUS_NOT_IEEE1284, 2);
error = ENODEV;
goto error;
}
#endif
ppbus_wctr(dev, (nINIT | STROBE | AUTOFEED) & ~SELECTIN);
DELAY(1);
ppbus_wctr(dev, nINIT & ~(SELECTIN | AUTOFEED | STROBE));
if (do_1284_wait(bus, nACK, nACK)) {
ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 6);
error = EBUSY;
goto error;
}
if (options & PPBUS_EXTENSIBILITY_LINK) {
ppbus_1284_terminate(dev);
error = ENODEV;
goto error;
}
if (request_mode == NIBBLE_1284_NORMAL) {
if (do_1284_wait(bus, nACK | SELECT, nACK)) {
ppbus_1284_set_error(bus, PPBUS_MODE_UNSUPPORTED, 7);
error = ENODEV;
goto error;
}
} else {
if (do_1284_wait(bus, nACK | SELECT, SELECT | nACK)) {
ppbus_1284_set_error(bus, PPBUS_MODE_UNSUPPORTED, 7);
error = ENODEV;
goto error;
}
}
switch (mode) {
case PPBUS_NIBBLE:
case PPBUS_PS2:
ppbus_1284_set_state(dev, PPBUS_REVERSE_IDLE);
break;
case PPBUS_ECP:
ppbus_1284_set_state(dev, PPBUS_SETUP);
ppbus_wctr(dev, (nINIT | AUTOFEED) & ~(SELECTIN | STROBE));
#ifdef PERIPH_1284
if (do_1284_wait(bus, nACK | SELECT | nBUSY,
nACK | SELECT | nBUSY)) {
ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 30);
error = ENODEV;
goto error;
}
#else
if (do_1284_wait(bus, nACK | SELECT | PERROR | nBUSY,
nACK | SELECT | PERROR | nBUSY)) {
ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 30);
error = ENODEV;
goto error;
}
#endif
ppbus_1284_set_state(dev, PPBUS_ECP_FORWARD_IDLE);
break;
case PPBUS_EPP:
ppbus_1284_set_state(dev, PPBUS_EPP_IDLE);
break;
default:
panic("%s: unknown mode (%d)!", __func__, mode);
}
return 0;
error:
ppbus_1284_terminate(dev);
return error;
}
int
ppbus_1284_terminate(device_t dev)
{
struct ppbus_softc * bus = device_private(dev);
#ifdef DEBUG_1284
printf("T");
#endif
ppbus_1284_set_state(dev, PPBUS_TERMINATION);
#ifdef PERIPH_1284
ppbus_wctr(dev, (nINIT | STROBE | SELECTIN) & ~(AUTOFEED));
DELAY(1);
#endif
ppbus_wctr(dev, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
if (do_1284_wait(bus, nACK | nBUSY | nFAULT, nFAULT)) {
ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 24);
goto error;
}
ppbus_wctr(dev, (nINIT | SELECTIN | AUTOFEED) & ~STROBE);
if (do_1284_wait(bus, nACK, nACK)) {
ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 27);
}
ppbus_wctr(dev, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
error:
ppbus_1284_set_state(dev, PPBUS_FORWARD_IDLE);
return (0);
}