1997-08-14 13:57:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1990 William F. Jolitz, TeleMuse
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This software is a component of "386BSD" developed by
|
|
|
|
* William F. Jolitz, TeleMuse.
|
|
|
|
* 4. Neither the name of the developer nor the name "386BSD"
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
|
|
|
|
* AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
|
|
|
|
* SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
|
|
|
|
* THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
|
|
|
|
* NOT MAKE USE OF THIS WORK.
|
|
|
|
*
|
|
|
|
* FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
|
|
|
|
* BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
|
|
|
|
* REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
|
|
|
|
* (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
|
|
|
|
* JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
|
|
|
|
* LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
|
|
|
|
* ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
|
|
|
|
* OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``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 DEVELOPER 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.
|
|
|
|
*
|
|
|
|
* from: unknown origin, 386BSD 0.1
|
|
|
|
* From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
|
1999-02-14 12:00:00 +00:00
|
|
|
* From Id: nlpt.c,v 1.14 1999/02/08 13:55:43 des Exp
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1997-08-14 13:57:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Device Driver for AT parallel printer port
|
|
|
|
* Written by William Jolitz 12/18/90
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Updated for ppbus by Nicolas Souchu
|
|
|
|
* [Mon Jul 28 1997]
|
|
|
|
*/
|
1998-01-24 02:54:56 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
#include "opt_lpt.h"
|
|
|
|
|
1997-08-14 13:57:45 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2000-01-14 00:18:06 +00:00
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/bus.h>
|
1997-08-14 13:57:45 +00:00
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
|
|
|
|
#include <machine/clock.h>
|
2000-01-14 00:18:06 +00:00
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <machine/resource.h>
|
|
|
|
#include <sys/rman.h>
|
|
|
|
|
1997-08-14 13:57:45 +00:00
|
|
|
#include <machine/lpt.h>
|
|
|
|
|
|
|
|
#include <dev/ppbus/ppbconf.h>
|
1999-01-10 12:04:56 +00:00
|
|
|
#include <dev/ppbus/ppb_1284.h>
|
1999-02-14 12:00:00 +00:00
|
|
|
#include <dev/ppbus/lpt.h>
|
2000-01-14 00:18:06 +00:00
|
|
|
#include "ppbus_if.h"
|
|
|
|
#include <dev/ppbus/ppbio.h>
|
1999-01-10 12:04:56 +00:00
|
|
|
|
1999-02-14 12:00:00 +00:00
|
|
|
#ifndef LPT_DEBUG
|
|
|
|
#define lprintf(args)
|
1997-08-14 13:57:45 +00:00
|
|
|
#else
|
1999-02-14 12:00:00 +00:00
|
|
|
#define lprintf(args) \
|
1998-12-04 22:00:33 +00:00
|
|
|
do { \
|
1999-02-14 12:00:00 +00:00
|
|
|
if (lptflag) \
|
1998-12-04 22:00:33 +00:00
|
|
|
printf args; \
|
|
|
|
} while (0)
|
1999-02-14 12:00:00 +00:00
|
|
|
static int volatile lptflag = 1;
|
1997-08-14 13:57:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define LPINITRDY 4 /* wait up to 4 seconds for a ready */
|
|
|
|
#define LPTOUTINITIAL 10 /* initial timeout to wait for ready 1/10 s */
|
|
|
|
#define LPTOUTMAX 1 /* maximal timeout 1 s */
|
|
|
|
#define LPPRI (PZERO+8)
|
|
|
|
#define BUFSIZE 1024
|
1999-01-10 12:04:56 +00:00
|
|
|
#define BUFSTATSIZE 32
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
#define LPTUNIT(s) ((s)&0x03)
|
|
|
|
#define LPTFLAGS(s) ((s)&0xfc)
|
|
|
|
|
1999-02-14 16:19:16 +00:00
|
|
|
struct lpt_data {
|
|
|
|
|
|
|
|
short sc_state;
|
|
|
|
/* default case: negative prime, negative ack, handshake strobe,
|
|
|
|
prime once */
|
|
|
|
u_char sc_control;
|
|
|
|
char sc_flags;
|
|
|
|
#define LP_POS_INIT 0x04 /* if we are a postive init signal */
|
|
|
|
#define LP_POS_ACK 0x08 /* if we are a positive going ack */
|
|
|
|
#define LP_NO_PRIME 0x10 /* don't prime the printer at all */
|
|
|
|
#define LP_PRIMEOPEN 0x20 /* prime on every open */
|
|
|
|
#define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */
|
|
|
|
#define LP_BYPASS 0x80 /* bypass printer ready checks */
|
|
|
|
struct buf *sc_inbuf;
|
|
|
|
struct buf *sc_statbuf;
|
|
|
|
short sc_xfercnt ;
|
|
|
|
char sc_primed;
|
|
|
|
char *sc_cp ;
|
|
|
|
u_short sc_irq ; /* IRQ status of port */
|
|
|
|
#define LP_HAS_IRQ 0x01 /* we have an irq available */
|
|
|
|
#define LP_USE_IRQ 0x02 /* we are using our irq */
|
|
|
|
#define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */
|
|
|
|
#define LP_ENABLE_EXT 0x10 /* we shall use advanced mode when possible */
|
|
|
|
u_char sc_backoff ; /* time to call lptout() again */
|
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
struct resource *intr_resource; /* interrupt resource */
|
|
|
|
void *intr_cookie; /* interrupt registration cookie */
|
1999-02-14 16:19:16 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
};
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1999-02-14 12:00:00 +00:00
|
|
|
#define LPT_NAME "lpt" /* our official name */
|
1997-08-28 11:12:08 +00:00
|
|
|
|
1999-02-14 12:00:00 +00:00
|
|
|
static timeout_t lptout;
|
2000-01-14 00:18:06 +00:00
|
|
|
static int lpt_port_test(device_t dev, u_char data, u_char mask);
|
|
|
|
static int lpt_detect(device_t dev);
|
1997-08-28 11:12:08 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
#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)))
|
1997-08-14 13:57:45 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
static void lptintr(device_t dev);
|
|
|
|
static void lpt_intr(void *arg); /* without spls */
|
1998-09-20 14:41:54 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
static devclass_t lpt_devclass;
|
|
|
|
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/* bits for state */
|
|
|
|
#define OPEN (1<<0) /* device is open */
|
|
|
|
#define ASLP (1<<1) /* awaiting draining of printer */
|
|
|
|
#define EERROR (1<<2) /* error was received from printer */
|
|
|
|
#define OBUSY (1<<3) /* printer is busy doing output */
|
|
|
|
#define LPTOUT (1<<4) /* timeout while not selected */
|
|
|
|
#define TOUT (1<<5) /* timeout while not selected */
|
1998-08-03 19:14:33 +00:00
|
|
|
#define LPTINIT (1<<6) /* waiting to initialize for open */
|
1997-08-14 13:57:45 +00:00
|
|
|
#define INTERRUPTED (1<<7) /* write call was interrupted */
|
|
|
|
|
1997-08-16 14:05:38 +00:00
|
|
|
#define HAVEBUS (1<<8) /* the driver owns the bus */
|
|
|
|
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/* status masks to interrogate printer status */
|
|
|
|
#define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */
|
|
|
|
#define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR)
|
|
|
|
|
|
|
|
/* Printer Ready condition - from lpa.c */
|
|
|
|
/* Only used in polling code */
|
|
|
|
#define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR)
|
|
|
|
#define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
|
2000-01-14 00:18:06 +00:00
|
|
|
#define NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK)
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
#define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */
|
|
|
|
#define MAX_SPIN 20 /* Max delay for device ready in usecs */
|
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
|
1999-02-14 12:00:00 +00:00
|
|
|
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;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
#define CDEV_MAJOR 16
|
1999-05-30 16:53:49 +00:00
|
|
|
static struct cdevsw lpt_cdevsw = {
|
|
|
|
/* open */ lptopen,
|
|
|
|
/* close */ lptclose,
|
|
|
|
/* read */ lptread,
|
|
|
|
/* write */ lptwrite,
|
|
|
|
/* ioctl */ lptioctl,
|
|
|
|
/* poll */ nopoll,
|
|
|
|
/* mmap */ nommap,
|
|
|
|
/* strategy */ nostrategy,
|
|
|
|
/* name */ LPT_NAME,
|
|
|
|
/* maj */ CDEV_MAJOR,
|
|
|
|
/* dump */ nodump,
|
|
|
|
/* psize */ nopsize,
|
|
|
|
/* flags */ 0,
|
|
|
|
/* bmaj */ -1
|
|
|
|
};
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1997-08-16 14:05:38 +00:00
|
|
|
static int
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_request_ppbus(device_t dev, int how)
|
1997-08-16 14:05:38 +00:00
|
|
|
{
|
2000-01-14 00:18:06 +00:00
|
|
|
device_t ppbus = device_get_parent(dev);
|
|
|
|
struct lpt_data *sc = DEVTOSOFTC(dev);
|
1998-08-03 19:14:33 +00:00
|
|
|
int error;
|
|
|
|
|
1999-01-10 12:04:56 +00:00
|
|
|
if (sc->sc_state & HAVEBUS)
|
|
|
|
return (0);
|
|
|
|
|
1998-08-03 19:14:33 +00:00
|
|
|
/* we have the bus only if the request succeded */
|
2000-01-14 00:18:06 +00:00
|
|
|
if ((error = ppb_request_bus(ppbus, dev, how)) == 0)
|
1998-08-03 19:14:33 +00:00
|
|
|
sc->sc_state |= HAVEBUS;
|
|
|
|
|
|
|
|
return (error);
|
1997-08-16 14:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_release_ppbus(device_t dev)
|
1997-08-16 14:05:38 +00:00
|
|
|
{
|
2000-01-14 00:18:06 +00:00
|
|
|
device_t ppbus = device_get_parent(dev);
|
|
|
|
struct lpt_data *sc = DEVTOSOFTC(dev);
|
|
|
|
int error = 0;
|
1998-08-03 19:14:33 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
if ((error = ppb_release_bus(ppbus, dev)) == 0)
|
|
|
|
sc->sc_state &= ~HAVEBUS;
|
|
|
|
|
|
|
|
return (error);
|
1997-08-16 14:05:38 +00:00
|
|
|
}
|
|
|
|
|
1997-08-14 13:57:45 +00:00
|
|
|
/*
|
1999-02-14 12:00:00 +00:00
|
|
|
* Internal routine to lptprobe to do port tests of one byte value
|
1997-08-14 13:57:45 +00:00
|
|
|
*/
|
|
|
|
static int
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_port_test(device_t ppbus, u_char data, u_char mask)
|
1997-08-14 13:57:45 +00:00
|
|
|
{
|
|
|
|
int temp, timeout;
|
|
|
|
|
|
|
|
data = data & mask;
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wdtr(ppbus, data);
|
1997-08-14 13:57:45 +00:00
|
|
|
timeout = 10000;
|
|
|
|
do {
|
|
|
|
DELAY(10);
|
2000-01-14 00:18:06 +00:00
|
|
|
temp = ppb_rdtr(ppbus) & mask;
|
1997-08-14 13:57:45 +00:00
|
|
|
}
|
|
|
|
while (temp != data && --timeout);
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
|
1997-08-14 13:57:45 +00:00
|
|
|
return (temp == data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-02-08 13:55:43 +00:00
|
|
|
* Probe simplified by replacing multiple loops with a hardcoded
|
|
|
|
* test pattern - 1999/02/08 des@freebsd.org
|
|
|
|
*
|
1997-08-14 13:57:45 +00:00
|
|
|
* New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
|
|
|
|
* Based partially on Rod Grimes' printer probe
|
|
|
|
*
|
|
|
|
* Logic:
|
|
|
|
* 1) If no port address was given, use the bios detected ports
|
|
|
|
* and autodetect what ports the printers are on.
|
|
|
|
* 2) Otherwise, probe the data port at the address given,
|
|
|
|
* using the method in Rod Grimes' port probe.
|
|
|
|
* (Much code ripped off directly from Rod's probe.)
|
|
|
|
*
|
|
|
|
* Comments from Rod's probe:
|
|
|
|
* Logic:
|
|
|
|
* 1) You should be able to write to and read back the same value
|
|
|
|
* to the data port. Do an alternating zeros, alternating ones,
|
|
|
|
* walking zero, and walking one test to check for stuck bits.
|
|
|
|
*
|
|
|
|
* 2) You should be able to write to and read back the same value
|
|
|
|
* to the control port lower 5 bits, the upper 3 bits are reserved
|
|
|
|
* per the IBM PC technical reference manauls and different boards
|
|
|
|
* do different things with them. Do an alternating zeros, alternating
|
|
|
|
* ones, walking zero, and walking one test to check for stuck bits.
|
|
|
|
*
|
|
|
|
* Some printers drag the strobe line down when the are powered off
|
|
|
|
* so this bit has been masked out of the control port test.
|
|
|
|
*
|
|
|
|
* XXX Some printers may not like a fast pulse on init or strobe, I
|
|
|
|
* don't know at this point, if that becomes a problem these bits
|
|
|
|
* should be turned off in the mask byte for the control port test.
|
|
|
|
*
|
|
|
|
* We are finally left with a mask of 0x14, due to some printers
|
|
|
|
* being adamant about holding other bits high ........
|
|
|
|
*
|
|
|
|
* Before probing the control port, we write a 0 to the data port -
|
|
|
|
* If not, some printers chuck out garbage when the strobe line
|
|
|
|
* gets toggled.
|
|
|
|
*
|
|
|
|
* 3) Set the data and control ports to a value of 0
|
|
|
|
*
|
|
|
|
* This probe routine has been tested on Epson Lx-800, HP LJ3P,
|
|
|
|
* Epson FX-1170 and C.Itoh 8510RM
|
|
|
|
* printers.
|
|
|
|
* Quick exit on fail added.
|
|
|
|
*/
|
|
|
|
static int
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_detect(device_t dev)
|
1997-08-14 13:57:45 +00:00
|
|
|
{
|
2000-01-14 00:18:06 +00:00
|
|
|
device_t ppbus = device_get_parent(dev);
|
|
|
|
|
1999-02-08 13:55:43 +00:00
|
|
|
static u_char testbyte[18] = {
|
|
|
|
0x55, /* alternating zeros */
|
|
|
|
0xaa, /* alternating ones */
|
|
|
|
0xfe, 0xfd, 0xfb, 0xf7,
|
|
|
|
0xef, 0xdf, 0xbf, 0x7f, /* walking zero */
|
|
|
|
0x01, 0x02, 0x04, 0x08,
|
|
|
|
0x10, 0x20, 0x40, 0x80 /* walking one */
|
|
|
|
};
|
|
|
|
int i, error, status;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1997-08-28 10:15:20 +00:00
|
|
|
status = 1; /* assume success */
|
1997-08-14 13:57:45 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
|
1997-08-28 11:12:08 +00:00
|
|
|
printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
|
1999-02-08 13:55:43 +00:00
|
|
|
status = 0;
|
|
|
|
goto end_probe;
|
1997-08-14 13:57:45 +00:00
|
|
|
}
|
|
|
|
|
1999-02-08 13:55:43 +00:00
|
|
|
for (i = 0; i < 18 && status; i++)
|
2000-01-14 00:18:06 +00:00
|
|
|
if (!lpt_port_test(ppbus, testbyte[i], 0xff)) {
|
1999-02-08 13:55:43 +00:00
|
|
|
status = 0;
|
|
|
|
goto end_probe;
|
|
|
|
}
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
end_probe:
|
|
|
|
/* write 0's to control and data ports */
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wdtr(ppbus, 0);
|
|
|
|
ppb_wctr(ppbus, 0);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_release_ppbus(dev);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
return (status);
|
|
|
|
}
|
|
|
|
|
2000-01-23 14:41:04 +00:00
|
|
|
static void
|
|
|
|
lpt_identify(driver_t *driver, device_t parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
BUS_ADD_CHILD(parent, 0, LPT_NAME, 0);
|
|
|
|
}
|
|
|
|
|
1997-08-14 13:57:45 +00:00
|
|
|
/*
|
2000-01-14 00:18:06 +00:00
|
|
|
* lpt_probe()
|
1997-08-14 13:57:45 +00:00
|
|
|
*/
|
2000-01-14 00:18:06 +00:00
|
|
|
static int
|
|
|
|
lpt_probe(device_t dev)
|
1997-08-14 13:57:45 +00:00
|
|
|
{
|
1997-08-28 11:12:08 +00:00
|
|
|
struct lpt_data *sc;
|
1999-05-31 11:29:30 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
sc = DEVTOSOFTC(dev);
|
1997-08-28 11:12:08 +00:00
|
|
|
bzero(sc, sizeof(struct lpt_data));
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now, try to detect the printer.
|
|
|
|
*/
|
2000-01-14 00:18:06 +00:00
|
|
|
if (!lpt_detect(dev))
|
|
|
|
return (ENXIO);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
device_set_desc(dev, "Printer");
|
1997-08-14 13:57:45 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
return (0);
|
1997-08-14 13:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_attach(device_t dev)
|
1997-08-14 13:57:45 +00:00
|
|
|
{
|
2000-01-14 00:18:06 +00:00
|
|
|
device_t ppbus = device_get_parent(dev);
|
|
|
|
struct lpt_data *sc = DEVTOSOFTC(dev);
|
2000-01-25 22:23:48 +00:00
|
|
|
int zero = 0, unit = device_get_unit(dev);
|
1997-08-16 14:05:38 +00:00
|
|
|
int error;
|
2000-02-13 03:03:31 +00:00
|
|
|
uintptr_t irq;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_primed = 0; /* not primed yet */
|
1997-08-14 13:57:45 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
|
1997-08-28 11:12:08 +00:00
|
|
|
printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
|
1997-08-14 13:57:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wctr(ppbus, LPC_NINIT);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/* check if we can use interrupt, should be done by ppc stuff */
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf(("oldirq %x\n", sc->sc_irq));
|
2000-01-14 00:18:06 +00:00
|
|
|
|
|
|
|
/* retrieve the ppbus irq */
|
|
|
|
BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
|
|
|
|
|
|
|
|
if (irq > 0) {
|
|
|
|
/* declare our interrupt handler */
|
|
|
|
sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
|
|
|
|
&zero, irq, irq, 1, RF_SHAREABLE);
|
|
|
|
}
|
|
|
|
if (sc->intr_resource) {
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
|
2000-01-14 00:18:06 +00:00
|
|
|
device_printf(dev, "Interrupt-driven port\n");
|
1997-08-14 13:57:45 +00:00
|
|
|
} else {
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_irq = 0;
|
2000-01-14 00:18:06 +00:00
|
|
|
device_printf(dev, "Polled port\n");
|
1997-08-14 13:57:45 +00:00
|
|
|
}
|
2000-01-14 00:18:06 +00:00
|
|
|
lprintf(("irq %x %x\n", irq, sc->sc_irq));
|
1997-08-14 13:57:45 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_release_ppbus(dev);
|
1997-08-16 14:05:38 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
make_dev(&lpt_cdevsw, unit,
|
|
|
|
UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit);
|
|
|
|
make_dev(&lpt_cdevsw, unit | LP_BYPASS,
|
|
|
|
UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit);
|
|
|
|
return (0);
|
1997-08-14 13:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-02-14 12:00:00 +00:00
|
|
|
lptout(void *arg)
|
1997-08-28 11:12:08 +00:00
|
|
|
{
|
2000-01-14 00:18:06 +00:00
|
|
|
device_t dev = (device_t)arg;
|
|
|
|
struct lpt_data *sc = DEVTOSOFTC(dev);
|
|
|
|
#ifdef LPT_DEBUG
|
|
|
|
device_t ppbus = device_get_parent(dev);
|
|
|
|
#endif
|
1997-08-28 11:12:08 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
lprintf(("T %x ", ppb_rstr(ppbus)));
|
1997-08-28 11:12:08 +00:00
|
|
|
if (sc->sc_state & OPEN) {
|
|
|
|
sc->sc_backoff++;
|
|
|
|
if (sc->sc_backoff > hz/LPTOUTMAX)
|
|
|
|
sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
|
2000-01-14 00:18:06 +00:00
|
|
|
timeout(lptout, (caddr_t)dev, sc->sc_backoff);
|
1997-08-14 13:57:45 +00:00
|
|
|
} else
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_state &= ~TOUT;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
if (sc->sc_state & EERROR)
|
|
|
|
sc->sc_state &= ~EERROR;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/*
|
2000-01-14 00:18:06 +00:00
|
|
|
* Avoid possible hangs due to missed interrupts
|
1997-08-14 13:57:45 +00:00
|
|
|
*/
|
1997-08-28 11:12:08 +00:00
|
|
|
if (sc->sc_xfercnt) {
|
2000-01-14 00:18:06 +00:00
|
|
|
lptintr(dev);
|
1997-08-14 13:57:45 +00:00
|
|
|
} else {
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_state &= ~OBUSY;
|
2000-01-14 00:18:06 +00:00
|
|
|
wakeup((caddr_t)dev);
|
1997-08-14 13:57:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-02-14 12:00:00 +00:00
|
|
|
* lptopen -- reset the printer, then wait until it's selected and not busy.
|
1997-08-14 13:57:45 +00:00
|
|
|
* If LP_BYPASS flag is selected, then we do not try to select the
|
|
|
|
* printer -- this is just used for passing ioctls.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
1999-02-14 12:00:00 +00:00
|
|
|
lptopen(dev_t dev, int flags, int fmt, struct proc *p)
|
1997-08-14 13:57:45 +00:00
|
|
|
{
|
|
|
|
int s;
|
1999-01-10 12:04:56 +00:00
|
|
|
int trys, err;
|
1997-08-14 13:57:45 +00:00
|
|
|
u_int unit = LPTUNIT(minor(dev));
|
2000-01-14 00:18:06 +00:00
|
|
|
struct lpt_data *sc = UNITOSOFTC(unit);
|
|
|
|
device_t lptdev = UNITODEVICE(unit);
|
|
|
|
device_t ppbus = device_get_parent(lptdev);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
if (!sc)
|
1997-08-14 13:57:45 +00:00
|
|
|
return (ENXIO);
|
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
if (sc->sc_state) {
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf((LPT_NAME ": still open %x\n", sc->sc_state));
|
1997-08-14 13:57:45 +00:00
|
|
|
return(EBUSY);
|
|
|
|
} else
|
1998-08-03 19:14:33 +00:00
|
|
|
sc->sc_state |= LPTINIT;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_flags = LPTFLAGS(minor(dev));
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/* Check for open with BYPASS flag set. */
|
1997-08-28 11:12:08 +00:00
|
|
|
if (sc->sc_flags & LP_BYPASS) {
|
|
|
|
sc->sc_state = OPEN;
|
1997-08-14 13:57:45 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1998-08-03 19:14:33 +00:00
|
|
|
/* request the ppbus only if we don't have it already */
|
2000-01-14 00:18:06 +00:00
|
|
|
if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
|
|
|
|
/* give it a chance to try later */
|
|
|
|
sc->sc_state = 0;
|
1999-01-10 12:04:56 +00:00
|
|
|
return (err);
|
2000-01-14 00:18:06 +00:00
|
|
|
}
|
1997-08-16 14:05:38 +00:00
|
|
|
|
1997-08-14 13:57:45 +00:00
|
|
|
s = spltty();
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf((LPT_NAME " flags 0x%x\n", sc->sc_flags));
|
1997-08-14 13:57:45 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
/* set IRQ status according to ENABLE_IRQ flag
|
|
|
|
*/
|
1997-08-28 11:12:08 +00:00
|
|
|
if (sc->sc_irq & LP_ENABLE_IRQ)
|
|
|
|
sc->sc_irq |= LP_USE_IRQ;
|
1997-08-14 13:57:45 +00:00
|
|
|
else
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_irq &= ~LP_USE_IRQ;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/* init printer */
|
1997-08-28 11:12:08 +00:00
|
|
|
if ((sc->sc_flags & LP_NO_PRIME) == 0) {
|
|
|
|
if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wctr(ppbus, 0);
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_primed++;
|
1997-08-14 13:57:45 +00:00
|
|
|
DELAY(500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wctr(ppbus, LPC_SEL|LPC_NINIT);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/* wait till ready (printer running diagnostics) */
|
|
|
|
trys = 0;
|
|
|
|
do {
|
|
|
|
/* ran out of waiting for the printer */
|
|
|
|
if (trys++ >= LPINITRDY*4) {
|
|
|
|
splx(s);
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_state = 0;
|
2000-01-14 00:18:06 +00:00
|
|
|
lprintf(("status %x\n", ppb_rstr(ppbus)));
|
1997-08-14 13:57:45 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_release_ppbus(lptdev);
|
1997-08-14 13:57:45 +00:00
|
|
|
return (EBUSY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait 1/4 second, give up if we get a signal */
|
2000-01-14 00:18:06 +00:00
|
|
|
if (tsleep((caddr_t)lptdev, LPPRI|PCATCH, "lptinit", hz/4) !=
|
1997-08-14 13:57:45 +00:00
|
|
|
EWOULDBLOCK) {
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_state = 0;
|
1997-08-14 13:57:45 +00:00
|
|
|
splx(s);
|
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_release_ppbus(lptdev);
|
1997-08-14 13:57:45 +00:00
|
|
|
return (EBUSY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* is printer online and ready for output */
|
2000-01-14 00:18:06 +00:00
|
|
|
} while ((ppb_rstr(ppbus) &
|
1997-08-14 13:57:45 +00:00
|
|
|
(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
|
|
|
|
(LPS_SEL|LPS_NBSY|LPS_NERR));
|
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_control = LPC_SEL|LPC_NINIT;
|
|
|
|
if (sc->sc_flags & LP_AUTOLF)
|
|
|
|
sc->sc_control |= LPC_AUTOL;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/* enable interrupt if interrupt-driven */
|
1997-08-28 11:12:08 +00:00
|
|
|
if (sc->sc_irq & LP_USE_IRQ)
|
|
|
|
sc->sc_control |= LPC_ENA;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wctr(ppbus, sc->sc_control);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_state = OPEN;
|
|
|
|
sc->sc_inbuf = geteblk(BUFSIZE);
|
1999-01-10 12:04:56 +00:00
|
|
|
sc->sc_statbuf = geteblk(BUFSTATSIZE);
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_xfercnt = 0;
|
1997-08-14 13:57:45 +00:00
|
|
|
splx(s);
|
|
|
|
|
1998-08-03 19:14:33 +00:00
|
|
|
/* release the ppbus */
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_release_ppbus(lptdev);
|
1997-08-16 14:05:38 +00:00
|
|
|
|
1997-08-14 13:57:45 +00:00
|
|
|
/* only use timeout if using interrupt */
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf(("irq %x\n", sc->sc_irq));
|
1997-08-28 11:12:08 +00:00
|
|
|
if (sc->sc_irq & LP_USE_IRQ) {
|
|
|
|
sc->sc_state |= TOUT;
|
2000-01-14 00:18:06 +00:00
|
|
|
timeout(lptout, (caddr_t)lptdev,
|
1997-08-28 11:12:08 +00:00
|
|
|
(sc->sc_backoff = hz/LPTOUTINITIAL));
|
1997-08-16 14:05:38 +00:00
|
|
|
}
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf(("opened.\n"));
|
1997-08-14 13:57:45 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-02-14 12:00:00 +00:00
|
|
|
* lptclose -- close the device, free the local line buffer.
|
1997-08-14 13:57:45 +00:00
|
|
|
*
|
|
|
|
* Check for interrupted write call added.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
1999-02-14 12:00:00 +00:00
|
|
|
lptclose(dev_t dev, int flags, int fmt, struct proc *p)
|
1997-08-14 13:57:45 +00:00
|
|
|
{
|
2000-01-14 00:18:06 +00:00
|
|
|
u_int unit = LPTUNIT(minor(dev));
|
|
|
|
struct lpt_data *sc = UNITOSOFTC(unit);
|
|
|
|
device_t lptdev = UNITODEVICE(unit);
|
|
|
|
device_t ppbus = device_get_parent(lptdev);
|
1997-08-16 14:05:38 +00:00
|
|
|
int err;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
if(sc->sc_flags & LP_BYPASS)
|
1997-08-14 13:57:45 +00:00
|
|
|
goto end_close;
|
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
|
1997-08-16 14:05:38 +00:00
|
|
|
return (err);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_state &= ~OPEN;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/* if the last write was interrupted, don't complete it */
|
1997-08-28 11:12:08 +00:00
|
|
|
if((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
|
2000-01-14 00:18:06 +00:00
|
|
|
while ((ppb_rstr(ppbus) &
|
1997-08-14 13:57:45 +00:00
|
|
|
(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
|
1997-08-28 11:12:08 +00:00
|
|
|
(LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
|
1997-08-14 13:57:45 +00:00
|
|
|
/* wait 1/4 second, give up if we get a signal */
|
2000-01-14 00:18:06 +00:00
|
|
|
if (tsleep((caddr_t)lptdev, LPPRI|PCATCH,
|
1997-08-14 13:57:45 +00:00
|
|
|
"lpclose", hz) != EWOULDBLOCK)
|
|
|
|
break;
|
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wctr(ppbus, LPC_NINIT);
|
1997-08-28 11:12:08 +00:00
|
|
|
brelse(sc->sc_inbuf);
|
1999-01-10 12:04:56 +00:00
|
|
|
brelse(sc->sc_statbuf);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1998-08-03 19:14:33 +00:00
|
|
|
end_close:
|
2000-01-14 00:18:06 +00:00
|
|
|
/* release the bus anyway
|
|
|
|
* unregistration of interrupt forced by release
|
|
|
|
*/
|
|
|
|
lpt_release_ppbus(lptdev);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_state = 0;
|
|
|
|
sc->sc_xfercnt = 0;
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf(("closed.\n"));
|
1997-08-14 13:57:45 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-02-14 12:00:00 +00:00
|
|
|
* lpt_pushbytes()
|
1997-08-14 13:57:45 +00:00
|
|
|
* Workhorse for actually spinning and writing bytes to printer
|
|
|
|
* Derived from lpa.c
|
|
|
|
* Originally by ?
|
|
|
|
*
|
|
|
|
* This code is only used when we are polling the port
|
|
|
|
*/
|
|
|
|
static int
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_pushbytes(device_t dev)
|
1997-08-14 13:57:45 +00:00
|
|
|
{
|
2000-01-14 00:18:06 +00:00
|
|
|
struct lpt_data *sc = DEVTOSOFTC(dev);
|
|
|
|
device_t ppbus = device_get_parent(dev);
|
1997-08-14 13:57:45 +00:00
|
|
|
int spin, err, tic;
|
|
|
|
char ch;
|
|
|
|
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf(("p"));
|
1997-08-14 13:57:45 +00:00
|
|
|
/* loop for every character .. */
|
1997-08-28 11:12:08 +00:00
|
|
|
while (sc->sc_xfercnt > 0) {
|
1997-08-14 13:57:45 +00:00
|
|
|
/* printer data */
|
1997-08-28 11:12:08 +00:00
|
|
|
ch = *(sc->sc_cp);
|
|
|
|
sc->sc_cp++;
|
|
|
|
sc->sc_xfercnt--;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for printer ready.
|
|
|
|
* Loop 20 usecs testing BUSY bit, then sleep
|
|
|
|
* for exponentially increasing timeout. (vak)
|
|
|
|
*/
|
2000-01-14 00:18:06 +00:00
|
|
|
for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin)
|
1997-08-14 13:57:45 +00:00
|
|
|
DELAY(1); /* XXX delay is NOT this accurate! */
|
|
|
|
if (spin >= MAX_SPIN) {
|
|
|
|
tic = 0;
|
2000-01-14 00:18:06 +00:00
|
|
|
while (NOT_READY(ppbus)) {
|
1997-08-14 13:57:45 +00:00
|
|
|
/*
|
|
|
|
* Now sleep, every cycle a
|
|
|
|
* little longer ..
|
|
|
|
*/
|
|
|
|
tic = tic + tic + 1;
|
|
|
|
/*
|
|
|
|
* But no more than 10 seconds. (vak)
|
|
|
|
*/
|
|
|
|
if (tic > MAX_SLEEP)
|
|
|
|
tic = MAX_SLEEP;
|
2000-01-14 00:18:06 +00:00
|
|
|
err = tsleep((caddr_t)dev, LPPRI,
|
1997-08-28 11:12:08 +00:00
|
|
|
LPT_NAME "poll", tic);
|
1997-08-14 13:57:45 +00:00
|
|
|
if (err != EWOULDBLOCK) {
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output data */
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wdtr(ppbus, ch);
|
1997-08-14 13:57:45 +00:00
|
|
|
/* strobe */
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wctr(ppbus, sc->sc_control|LPC_STB);
|
|
|
|
ppb_wctr(ppbus, sc->sc_control);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1999-01-10 12:04:56 +00:00
|
|
|
/*
|
1999-02-14 12:00:00 +00:00
|
|
|
* lptread --retrieve printer status in IEEE1284 NIBBLE mode
|
1999-01-10 12:04:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
1999-02-14 12:00:00 +00:00
|
|
|
lptread(dev_t dev, struct uio *uio, int ioflag)
|
1999-01-10 12:04:56 +00:00
|
|
|
{
|
2000-01-14 00:18:06 +00:00
|
|
|
u_int unit = LPTUNIT(minor(dev));
|
|
|
|
struct lpt_data *sc = UNITOSOFTC(unit);
|
|
|
|
device_t lptdev = UNITODEVICE(unit);
|
|
|
|
device_t ppbus = device_get_parent(lptdev);
|
1999-01-10 12:04:56 +00:00
|
|
|
int error = 0, len;
|
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0)))
|
1999-01-10 12:04:56 +00:00
|
|
|
return (error);
|
|
|
|
|
|
|
|
/* read data in an other buffer, read/write may be simultaneous */
|
|
|
|
len = 0;
|
|
|
|
while (uio->uio_resid) {
|
2000-01-14 00:18:06 +00:00
|
|
|
if ((error = ppb_1284_read(ppbus, PPB_NIBBLE,
|
1999-01-10 12:04:56 +00:00
|
|
|
sc->sc_statbuf->b_data, min(BUFSTATSIZE,
|
|
|
|
uio->uio_resid), &len))) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
goto error; /* no more data */
|
|
|
|
|
|
|
|
if ((error = uiomove(sc->sc_statbuf->b_data, len, uio)))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_1284_terminate(ppbus);
|
1999-01-10 12:04:56 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1997-08-14 13:57:45 +00:00
|
|
|
/*
|
1999-02-14 12:00:00 +00:00
|
|
|
* lptwrite --copy a line from user space to a local buffer, then call
|
1997-08-14 13:57:45 +00:00
|
|
|
* putc to get the chars moved to the output queue.
|
|
|
|
*
|
|
|
|
* Flagging of interrupted write added.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
1999-02-14 12:00:00 +00:00
|
|
|
lptwrite(dev_t dev, struct uio *uio, int ioflag)
|
1997-08-14 13:57:45 +00:00
|
|
|
{
|
|
|
|
register unsigned n;
|
2000-01-14 00:18:06 +00:00
|
|
|
int err;
|
1999-01-10 12:04:56 +00:00
|
|
|
u_int unit = LPTUNIT(minor(dev));
|
2000-01-14 00:18:06 +00:00
|
|
|
struct lpt_data *sc = UNITOSOFTC(unit);
|
|
|
|
device_t lptdev = UNITODEVICE(unit);
|
|
|
|
device_t ppbus = device_get_parent(lptdev);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
if(sc->sc_flags & LP_BYPASS) {
|
1997-08-14 13:57:45 +00:00
|
|
|
/* we can't do writes in bypass mode */
|
|
|
|
return(EPERM);
|
|
|
|
}
|
|
|
|
|
1998-08-03 19:14:33 +00:00
|
|
|
/* request the ppbus only if we don't have it already */
|
2000-01-14 00:18:06 +00:00
|
|
|
/* XXX interrupt registration?! */
|
|
|
|
if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
|
1999-01-10 12:04:56 +00:00
|
|
|
return (err);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
/* if interrupts are working, register the handler */
|
|
|
|
if (sc->sc_irq & LP_USE_IRQ) {
|
|
|
|
/* register our interrupt handler */
|
|
|
|
err = BUS_SETUP_INTR(ppbus, lptdev, sc->intr_resource,
|
|
|
|
INTR_TYPE_TTY, lpt_intr, lptdev,
|
|
|
|
&sc->intr_cookie);
|
|
|
|
if (err) {
|
|
|
|
device_printf(lptdev, "handler registration failed, polled mode.\n");
|
|
|
|
sc->sc_irq &= ~LP_USE_IRQ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_state &= ~INTERRUPTED;
|
1997-08-14 13:57:45 +00:00
|
|
|
while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
|
1997-12-02 21:07:20 +00:00
|
|
|
sc->sc_cp = sc->sc_inbuf->b_data ;
|
1997-08-28 11:12:08 +00:00
|
|
|
uiomove(sc->sc_cp, n, uio);
|
|
|
|
sc->sc_xfercnt = n ;
|
1999-01-10 12:04:56 +00:00
|
|
|
|
|
|
|
if (sc->sc_irq & LP_ENABLE_EXT) {
|
|
|
|
/* try any extended mode */
|
2000-01-14 00:18:06 +00:00
|
|
|
err = ppb_write(ppbus, sc->sc_cp,
|
|
|
|
sc->sc_xfercnt, 0);
|
1999-01-10 12:04:56 +00:00
|
|
|
switch (err) {
|
|
|
|
case 0:
|
|
|
|
/* if not all data was sent, we could rely
|
|
|
|
* on polling for the last bytes */
|
|
|
|
sc->sc_xfercnt = 0;
|
|
|
|
break;
|
|
|
|
case EINTR:
|
|
|
|
sc->sc_state |= INTERRUPTED;
|
|
|
|
return(err);
|
|
|
|
case EINVAL:
|
|
|
|
/* advanced mode not avail */
|
|
|
|
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)) {
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf(("i"));
|
1997-08-14 13:57:45 +00:00
|
|
|
/* if the printer is ready for a char, */
|
|
|
|
/* give it one */
|
1997-08-28 11:12:08 +00:00
|
|
|
if ((sc->sc_state & OBUSY) == 0){
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf(("\nC %d. ", sc->sc_xfercnt));
|
2000-01-14 00:18:06 +00:00
|
|
|
lptintr(lptdev);
|
1997-08-14 13:57:45 +00:00
|
|
|
}
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf(("W "));
|
1997-08-28 11:12:08 +00:00
|
|
|
if (sc->sc_state & OBUSY)
|
2000-01-14 00:18:06 +00:00
|
|
|
if ((err = tsleep((caddr_t)lptdev,
|
1997-08-28 11:12:08 +00:00
|
|
|
LPPRI|PCATCH, LPT_NAME "write", 0))) {
|
|
|
|
sc->sc_state |= INTERRUPTED;
|
1997-08-14 13:57:45 +00:00
|
|
|
return(err);
|
|
|
|
}
|
|
|
|
}
|
1999-01-10 12:04:56 +00:00
|
|
|
|
1997-08-14 13:57:45 +00:00
|
|
|
/* check to see if we must do a polled write */
|
1997-08-28 11:12:08 +00:00
|
|
|
if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf(("p"));
|
1997-08-16 14:05:38 +00:00
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
err = lpt_pushbytes(lptdev);
|
1997-08-16 14:05:38 +00:00
|
|
|
|
|
|
|
if (err)
|
1997-08-14 13:57:45 +00:00
|
|
|
return(err);
|
|
|
|
}
|
|
|
|
}
|
1998-08-03 19:14:33 +00:00
|
|
|
|
|
|
|
/* we have not been interrupted, release the ppbus */
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_release_ppbus(lptdev);
|
1998-08-03 19:14:33 +00:00
|
|
|
|
1997-08-14 13:57:45 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-02-14 12:00:00 +00:00
|
|
|
* lpt_intr -- handle printer interrupts which occur when the printer is
|
1997-08-14 13:57:45 +00:00
|
|
|
* ready to accept another char.
|
|
|
|
*
|
|
|
|
* do checking for interrupted write call.
|
|
|
|
*/
|
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
static void
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_intr(void *arg)
|
1997-08-14 13:57:45 +00:00
|
|
|
{
|
2000-01-14 00:18:06 +00:00
|
|
|
device_t lptdev = (device_t)arg;
|
|
|
|
device_t ppbus = device_get_parent(lptdev);
|
|
|
|
struct lpt_data *sc = DEVTOSOFTC(lptdev);
|
2000-01-25 22:23:48 +00:00
|
|
|
int sts = 0;
|
1997-08-14 13:57:45 +00:00
|
|
|
int i;
|
1998-08-03 19:14:33 +00:00
|
|
|
|
|
|
|
/* we must own the bus to use it */
|
|
|
|
if ((sc->sc_state & HAVEBUS) == 0)
|
|
|
|
return;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Is printer online and ready for output?
|
|
|
|
*
|
1999-02-14 12:00:00 +00:00
|
|
|
* Avoid falling back to lptout() too quickly. First spin-loop
|
1997-08-14 13:57:45 +00:00
|
|
|
* to see if the printer will become ready ``really soon now''.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 100 &&
|
2000-01-14 00:18:06 +00:00
|
|
|
((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
if ((sts & RDY_MASK) == LP_READY) {
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
|
|
|
|
sc->sc_backoff = hz/LPTOUTINITIAL;
|
1997-08-14 13:57:45 +00:00
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
if (sc->sc_xfercnt) {
|
1997-08-14 13:57:45 +00:00
|
|
|
/* send char */
|
1999-02-14 12:00:00 +00:00
|
|
|
/*lprintf(("%x ", *sc->sc_cp)); */
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wdtr(ppbus, *sc->sc_cp++) ;
|
|
|
|
ppb_wctr(ppbus, sc->sc_control|LPC_STB);
|
1997-08-14 13:57:45 +00:00
|
|
|
/* DELAY(X) */
|
2000-01-14 00:18:06 +00:00
|
|
|
ppb_wctr(ppbus, sc->sc_control);
|
1997-08-14 13:57:45 +00:00
|
|
|
|
|
|
|
/* any more data for printer */
|
1997-08-28 11:12:08 +00:00
|
|
|
if(--(sc->sc_xfercnt) > 0) return;
|
1997-08-14 13:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No more data waiting for printer.
|
2000-02-08 21:46:27 +00:00
|
|
|
* Wakeup is not done if write call was not interrupted.
|
1997-08-14 13:57:45 +00:00
|
|
|
*/
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_state &= ~OBUSY;
|
1997-08-16 14:05:38 +00:00
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
if(!(sc->sc_state & INTERRUPTED))
|
2000-02-08 21:46:27 +00:00
|
|
|
wakeup((caddr_t)lptdev);
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf(("w "));
|
1997-08-14 13:57:45 +00:00
|
|
|
return;
|
|
|
|
} else { /* check for error */
|
|
|
|
if(((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
|
1997-08-28 11:12:08 +00:00
|
|
|
(sc->sc_state & OPEN))
|
|
|
|
sc->sc_state |= EERROR;
|
1999-02-14 12:00:00 +00:00
|
|
|
/* lptout() will jump in and try to restart. */
|
1997-08-14 13:57:45 +00:00
|
|
|
}
|
1999-02-14 12:00:00 +00:00
|
|
|
lprintf(("sts %x ", sts));
|
1997-08-14 13:57:45 +00:00
|
|
|
}
|
|
|
|
|
1998-09-20 14:41:54 +00:00
|
|
|
static void
|
2000-01-14 00:18:06 +00:00
|
|
|
lptintr(device_t dev)
|
1998-09-20 14:41:54 +00:00
|
|
|
{
|
|
|
|
/* call the interrupt at required spl level */
|
|
|
|
int s = spltty();
|
|
|
|
|
2000-01-14 00:18:06 +00:00
|
|
|
lpt_intr(dev);
|
1998-09-20 14:41:54 +00:00
|
|
|
|
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1997-08-14 13:57:45 +00:00
|
|
|
static int
|
1999-02-14 12:00:00 +00:00
|
|
|
lptioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
|
1997-08-14 13:57:45 +00:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
u_int unit = LPTUNIT(minor(dev));
|
2000-01-14 00:18:06 +00:00
|
|
|
struct lpt_data *sc = UNITOSOFTC(unit);
|
1997-08-14 13:57:45 +00:00
|
|
|
u_char old_sc_irq; /* old printer IRQ status */
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case LPT_IRQ :
|
1997-08-28 11:12:08 +00:00
|
|
|
if(sc->sc_irq & LP_HAS_IRQ) {
|
1997-08-14 13:57:45 +00:00
|
|
|
/*
|
|
|
|
* NOTE:
|
|
|
|
* If the IRQ status is changed,
|
|
|
|
* this will only be visible on the
|
|
|
|
* next open.
|
|
|
|
*
|
|
|
|
* If interrupt status changes,
|
|
|
|
* this gets syslog'd.
|
|
|
|
*/
|
1997-08-28 11:12:08 +00:00
|
|
|
old_sc_irq = sc->sc_irq;
|
1999-01-10 12:04:56 +00:00
|
|
|
switch(*(int*)data) {
|
|
|
|
case 0:
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_irq &= (~LP_ENABLE_IRQ);
|
1999-01-10 12:04:56 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
sc->sc_irq &= (~LP_ENABLE_EXT);
|
1997-08-28 11:12:08 +00:00
|
|
|
sc->sc_irq |= LP_ENABLE_IRQ;
|
1999-01-10 12:04:56 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
/* classic irq based transfer and advanced
|
|
|
|
* modes are in conflict
|
|
|
|
*/
|
|
|
|
sc->sc_irq &= (~LP_ENABLE_IRQ);
|
|
|
|
sc->sc_irq |= LP_ENABLE_EXT;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
sc->sc_irq &= (~LP_ENABLE_EXT);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1997-08-28 11:12:08 +00:00
|
|
|
if (old_sc_irq != sc->sc_irq )
|
1999-01-10 12:04:56 +00:00
|
|
|
log(LOG_NOTICE, LPT_NAME "%d: switched to %s %s mode\n",
|
1997-08-28 11:12:08 +00:00
|
|
|
unit,
|
|
|
|
(sc->sc_irq & LP_ENABLE_IRQ)?
|
1999-01-10 12:04:56 +00:00
|
|
|
"interrupt-driven":"polled",
|
|
|
|
(sc->sc_irq & LP_ENABLE_EXT)?
|
|
|
|
"extended":"standard");
|
1997-08-14 13:57:45 +00:00
|
|
|
} else /* polled port */
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(error);
|
|
|
|
}
|
2000-01-14 00:18:06 +00:00
|
|
|
|
2000-01-23 14:41:04 +00:00
|
|
|
static device_method_t lpt_methods[] = {
|
|
|
|
/* device interface */
|
|
|
|
DEVMETHOD(device_identify, lpt_identify),
|
|
|
|
DEVMETHOD(device_probe, lpt_probe),
|
|
|
|
DEVMETHOD(device_attach, lpt_attach),
|
2000-01-14 00:18:06 +00:00
|
|
|
|
2000-01-23 14:41:04 +00:00
|
|
|
{ 0, 0 }
|
|
|
|
};
|
2000-01-14 00:18:06 +00:00
|
|
|
|
2000-01-23 14:41:04 +00:00
|
|
|
static driver_t lpt_driver = {
|
|
|
|
LPT_NAME,
|
|
|
|
lpt_methods,
|
|
|
|
sizeof(struct lpt_data),
|
|
|
|
};
|
|
|
|
|
|
|
|
DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, 0, 0);
|