olpt.c revision 108470
148006Skato/* 248006Skato * Copyright (c) 1990 William F. Jolitz, TeleMuse 348006Skato * All rights reserved. 448006Skato * 548006Skato * Redistribution and use in source and binary forms, with or without 648006Skato * modification, are permitted provided that the following conditions 748006Skato * are met: 848006Skato * 1. Redistributions of source code must retain the above copyright 948006Skato * notice, this list of conditions and the following disclaimer. 1048006Skato * 2. Redistributions in binary form must reproduce the above copyright 1148006Skato * notice, this list of conditions and the following disclaimer in the 1248006Skato * documentation and/or other materials provided with the distribution. 1348006Skato * 3. All advertising materials mentioning features or use of this software 1448006Skato * must display the following acknowledgement: 1548006Skato * This software is a component of "386BSD" developed by 1648006Skato * William F. Jolitz, TeleMuse. 1748006Skato * 4. Neither the name of the developer nor the name "386BSD" 1848006Skato * may be used to endorse or promote products derived from this software 1948006Skato * without specific prior written permission. 2048006Skato * 2148006Skato * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ 2248006Skato * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS 2348006Skato * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT. 2448006Skato * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT 2548006Skato * NOT MAKE USE OF THIS WORK. 2648006Skato * 2748006Skato * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED 2848006Skato * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN 2948006Skato * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES 3048006Skato * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING 3148006Skato * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND 3248006Skato * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE 3348006Skato * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS 3448006Skato * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992. 3548006Skato * 3648006Skato * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND 3748006Skato * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3848006Skato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3948006Skato * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE 4048006Skato * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4148006Skato * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4248006Skato * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4348006Skato * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4448006Skato * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 4548006Skato * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 4648006Skato * SUCH DAMAGE. 4748006Skato * 4848006Skato * from: unknown origin, 386BSD 0.1 4950477Speter * $FreeBSD: head/sys/pc98/cbus/olpt.c 108470 2002-12-30 21:18:15Z schweikh $ 5048006Skato */ 5148006Skato 5248006Skato/* 5348006Skato * Device Driver for AT parallel printer port 5448006Skato * Written by William Jolitz 12/18/90 5548006Skato */ 5648006Skato 5748006Skato/* 5848006Skato * Parallel port TCP/IP interfaces added. I looked at the driver from 5948006Skato * MACH but this is a complete rewrite, and btw. incompatible, and it 6048006Skato * should perform better too. I have never run the MACH driver though. 6148006Skato * 6248006Skato * This driver sends two bytes (0x08, 0x00) in front of each packet, 6348006Skato * to allow us to distinguish another format later. 6448006Skato * 65108470Sschweikh * Now added a Linux/Crynwr compatibility mode which is enabled using 6648006Skato * IF_LINK0 - Tim Wilkinson. 6748006Skato * 6848006Skato * TODO: 6948006Skato * Make HDLC/PPP mode, use IF_LLC1 to enable. 7048006Skato * 7148006Skato * Connect the two computers using a Laplink parallel cable to use this 7248006Skato * feature: 7348006Skato * 7448006Skato * +----------------------------------------+ 7548006Skato * |A-name A-End B-End Descr. Port/Bit | 7648006Skato * +----------------------------------------+ 7748006Skato * |DATA0 2 15 Data 0/0x01 | 7848006Skato * |-ERROR 15 2 1/0x08 | 7948006Skato * +----------------------------------------+ 8048006Skato * |DATA1 3 13 Data 0/0x02 | 8148006Skato * |+SLCT 13 3 1/0x10 | 8248006Skato * +----------------------------------------+ 8348006Skato * |DATA2 4 12 Data 0/0x04 | 8448006Skato * |+PE 12 4 1/0x20 | 8548006Skato * +----------------------------------------+ 8648006Skato * |DATA3 5 10 Strobe 0/0x08 | 8748006Skato * |-ACK 10 5 1/0x40 | 8848006Skato * +----------------------------------------+ 8948006Skato * |DATA4 6 11 Data 0/0x10 | 9048006Skato * |BUSY 11 6 1/~0x80 | 9148006Skato * +----------------------------------------+ 9248006Skato * |GND 18-25 18-25 GND - | 9348006Skato * +----------------------------------------+ 9448006Skato * 9548006Skato * Expect transfer-rates up to 75 kbyte/sec. 9648006Skato * 9748006Skato * If GCC could correctly grok 9848006Skato * register int port asm("edx") 9948006Skato * the code would be cleaner 10048006Skato * 10148006Skato * Poul-Henning Kamp <phk@freebsd.org> 10248006Skato */ 10348006Skato 10448006Skato#include <sys/param.h> 10548006Skato#include <sys/systm.h> 10648006Skato#include <sys/conf.h> 10760041Sphk#include <sys/bio.h> 10848006Skato#include <sys/buf.h> 10961116Snyan#include <sys/bus.h> 11048006Skato#include <sys/kernel.h> 11148006Skato#include <sys/uio.h> 11248006Skato#include <sys/syslog.h> 11348006Skato 11480537Snyan#include <machine/clock.h> 11580537Snyan#include <machine/bus.h> 11680537Snyan#include <machine/resource.h> 11780537Snyan#include <sys/rman.h> 11880537Snyan 11980537Snyan#include <isa/isavar.h> 12080537Snyan 12148006Skato#include <i386/isa/lptreg.h> 12260950Snyan#include <dev/ppbus/lptio.h> 12348006Skato 12448006Skato#define LPINITRDY 4 /* wait up to 4 seconds for a ready */ 12548006Skato#define LPTOUTINITIAL 10 /* initial timeout to wait for ready 1/10 s */ 12648006Skato#define LPTOUTMAX 1 /* maximal timeout 1 s */ 12748006Skato#define LPPRI (PZERO+8) 12848006Skato#define BUFSIZE 1024 12948006Skato 13048006Skato#ifndef PC98 13148006Skato/* BIOS printer list - used by BIOS probe*/ 13248006Skato#define BIOS_LPT_PORTS 0x408 13348006Skato#define BIOS_PORTS (short *)(KERNBASE+BIOS_LPT_PORTS) 13448006Skato#define BIOS_MAX_LPT 4 13548006Skato#endif 13648006Skato 13748006Skato 13848006Skato#ifndef DEBUG 13948006Skato#define lprintf(args) 14048006Skato#else 14148006Skato#define lprintf(args) do { \ 14248006Skato if (lptflag) \ 14348006Skato printf args; \ 14448006Skato } while (0) 14548006Skatostatic int volatile lptflag = 1; 14648006Skato#endif 14748006Skato 14848006Skato#define LPTUNIT(s) ((s)&0x03) 14948006Skato#define LPTFLAGS(s) ((s)&0xfc) 15048006Skato 15180537Snyanstruct lpt_softc { 15280537Snyan struct resource *res_port; 15380537Snyan struct resource *res_irq; 15480537Snyan void *sc_ih; 15580537Snyan 15648006Skato int sc_port; 15748006Skato short sc_state; 15848006Skato /* default case: negative prime, negative ack, handshake strobe, 15948006Skato prime once */ 16048006Skato u_char sc_control; 16148006Skato char sc_flags; 16248006Skato#define LP_POS_INIT 0x04 /* if we are a postive init signal */ 16348006Skato#define LP_POS_ACK 0x08 /* if we are a positive going ack */ 16448006Skato#define LP_NO_PRIME 0x10 /* don't prime the printer at all */ 16548006Skato#define LP_PRIMEOPEN 0x20 /* prime on every open */ 16648006Skato#define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */ 16748006Skato#define LP_BYPASS 0x80 /* bypass printer ready checks */ 16848006Skato struct buf *sc_inbuf; 16948006Skato short sc_xfercnt ; 17048006Skato char sc_primed; 17148006Skato char *sc_cp ; 17248006Skato u_char sc_irq ; /* IRQ status of port */ 17348006Skato#define LP_HAS_IRQ 0x01 /* we have an irq available */ 17448006Skato#define LP_USE_IRQ 0x02 /* we are using our irq */ 17548006Skato#define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */ 17648006Skato u_char sc_backoff ; /* time to call lptout() again */ 17780537Snyan}; 17848006Skato 17948006Skato/* bits for state */ 18048006Skato#define OPEN (1<<0) /* device is open */ 18148006Skato#define ASLP (1<<1) /* awaiting draining of printer */ 18248006Skato#define ERROR (1<<2) /* error was received from printer */ 18348006Skato#define OBUSY (1<<3) /* printer is busy doing output */ 18448006Skato#define LPTOUT (1<<4) /* timeout while not selected */ 18548006Skato#define TOUT (1<<5) /* timeout while not selected */ 18648006Skato#define INIT (1<<6) /* waiting to initialize for open */ 18748006Skato#define INTERRUPTED (1<<7) /* write call was interrupted */ 18848006Skato 18948006Skato 19048006Skato/* status masks to interrogate printer status */ 19148006Skato#define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */ 19248006Skato#define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR) 19348006Skato 19448006Skato/* Printer Ready condition - from lpa.c */ 19548006Skato/* Only used in polling code */ 19648006Skato#ifdef PC98 19748006Skato#define NOT_READY(x) ((inb(x) & LPS_NBSY) != LPS_NBSY) 19848006Skato#else /* IBM-PC */ 19948006Skato#define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR) 20048006Skato#define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR) 20148006Skato#define NOT_READY(x) ((inb(x)^LPS_INVERT)&LPS_MASK) 20248006Skato#endif 20348006Skato 20448006Skato#define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */ 20548006Skato#define MAX_SPIN 20 /* Max delay for device ready in usecs */ 20648006Skato 20748006Skatostatic timeout_t lptout; 20880537Snyanstatic int lpt_probe(device_t); 20980537Snyanstatic int lpt_attach(device_t); 21080537Snyanstatic void lpt_intr(void *); 21148006Skato 21280537Snyanstatic devclass_t olpt_devclass; 21380537Snyan 21480537Snyanstatic device_method_t olpt_methods[] = { 21580537Snyan DEVMETHOD(device_probe, lpt_probe), 21680537Snyan DEVMETHOD(device_attach, lpt_attach), 21780537Snyan { 0, 0 } 21848006Skato}; 21948006Skato 22080537Snyanstatic driver_t olpt_driver = { 22180537Snyan "olpt", 22280537Snyan olpt_methods, 22380537Snyan sizeof (struct lpt_softc), 22480537Snyan}; 22580537Snyan 22680537SnyanDRIVER_MODULE(olpt, isa, olpt_driver, olpt_devclass, 0, 0); 22780537Snyan 22848006Skatostatic d_open_t lptopen; 22948006Skatostatic d_close_t lptclose; 23048006Skatostatic d_write_t lptwrite; 23148006Skatostatic d_ioctl_t lptioctl; 23248006Skato 23348006Skato#define CDEV_MAJOR 16 23448006Skatostatic struct cdevsw lpt_cdevsw = { 23548006Skato /* open */ lptopen, 23648006Skato /* close */ lptclose, 23748006Skato /* read */ noread, 23848006Skato /* write */ lptwrite, 23948006Skato /* ioctl */ lptioctl, 24048006Skato /* poll */ nopoll, 24148006Skato /* mmap */ nommap, 24248006Skato /* strategy */ nostrategy, 24348006Skato /* name */ "lpt", 24448006Skato /* maj */ CDEV_MAJOR, 24548006Skato /* dump */ nodump, 24648006Skato /* psize */ nopsize, 24748006Skato /* flags */ 0, 24848006Skato}; 24948006Skato 25080537Snyanstatic bus_addr_t lpt_iat[] = {0, 2, 4, 6}; 25180537Snyan 25248006Skato#ifndef PC98 25348006Skato/* 25448006Skato * Internal routine to lptprobe to do port tests of one byte value 25548006Skato */ 25648006Skatostatic int 25748006Skatolpt_port_test (int port, u_char data, u_char mask) 25848006Skato{ 25948006Skato int temp, timeout; 26048006Skato 26148006Skato data = data & mask; 26248006Skato outb(port, data); 26348006Skato timeout = 10000; 26448006Skato do { 26548006Skato DELAY(10); 26648006Skato temp = inb(port) & mask; 26748006Skato } 26848006Skato while (temp != data && --timeout); 26948006Skato lprintf(("Port 0x%x\tout=%x\tin=%x\ttout=%d\n", 27048006Skato port, data, temp, timeout)); 27148006Skato return (temp == data); 27248006Skato} 27348006Skato#endif /* PC98 */ 27448006Skato 27548006Skato/* 27648006Skato * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94 27748006Skato * Based partially on Rod Grimes' printer probe 27848006Skato * 27948006Skato * Logic: 28048006Skato * 1) If no port address was given, use the bios detected ports 28148006Skato * and autodetect what ports the printers are on. 28248006Skato * 2) Otherwise, probe the data port at the address given, 28348006Skato * using the method in Rod Grimes' port probe. 28448006Skato * (Much code ripped off directly from Rod's probe.) 28548006Skato * 28648006Skato * Comments from Rod's probe: 28748006Skato * Logic: 28848006Skato * 1) You should be able to write to and read back the same value 28948006Skato * to the data port. Do an alternating zeros, alternating ones, 29048006Skato * walking zero, and walking one test to check for stuck bits. 29148006Skato * 29248006Skato * 2) You should be able to write to and read back the same value 29348006Skato * to the control port lower 5 bits, the upper 3 bits are reserved 29448006Skato * per the IBM PC technical reference manauls and different boards 29548006Skato * do different things with them. Do an alternating zeros, alternating 29648006Skato * ones, walking zero, and walking one test to check for stuck bits. 29748006Skato * 29848006Skato * Some printers drag the strobe line down when the are powered off 29948006Skato * so this bit has been masked out of the control port test. 30048006Skato * 30148006Skato * XXX Some printers may not like a fast pulse on init or strobe, I 30248006Skato * don't know at this point, if that becomes a problem these bits 30348006Skato * should be turned off in the mask byte for the control port test. 30448006Skato * 30548006Skato * We are finally left with a mask of 0x14, due to some printers 30648006Skato * being adamant about holding other bits high ........ 30748006Skato * 30848006Skato * Before probing the control port, we write a 0 to the data port - 30948006Skato * If not, some printers chuck out garbage when the strobe line 31048006Skato * gets toggled. 31148006Skato * 31248006Skato * 3) Set the data and control ports to a value of 0 31348006Skato * 31448006Skato * This probe routine has been tested on Epson Lx-800, HP LJ3P, 31548006Skato * Epson FX-1170 and C.Itoh 8510RM 31648006Skato * printers. 31748006Skato * Quick exit on fail added. 31848006Skato */ 31948006Skato 32048006Skatoint 32180537Snyanlpt_probe(device_t dev) 32248006Skato{ 32348006Skato#ifdef PC98 32448006Skato#define PC98_OLD_LPT 0x40 32548006Skato#define PC98_IEEE_1284_FUNCTION 0x149 32680537Snyan int rid; 32780537Snyan struct resource *res; 32848006Skato 32980537Snyan /* Check isapnp ids */ 33080537Snyan if (isa_get_vendorid(dev)) 33180537Snyan return ENXIO; 33280537Snyan 33380537Snyan rid = 0; 33480537Snyan res = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, lpt_iat, 4, 33580537Snyan RF_ACTIVE); 33680537Snyan if (res == NULL) 33780537Snyan return ENXIO; 33880537Snyan isa_load_resourcev(res, lpt_iat, 4); 33980537Snyan 34080537Snyan if (isa_get_port(dev) == PC98_OLD_LPT) { 34180537Snyan unsigned int pc98_ieee_mode, tmp; 34280537Snyan 34348006Skato tmp = inb(PC98_IEEE_1284_FUNCTION); 34448006Skato pc98_ieee_mode = tmp; 34548006Skato if ((tmp & 0x10) == 0x10) { 34648006Skato outb(PC98_IEEE_1284_FUNCTION, tmp & ~0x10); 34748006Skato tmp = inb(PC98_IEEE_1284_FUNCTION); 34848006Skato if ((tmp & 0x10) != 0x10) { 34948006Skato outb(PC98_IEEE_1284_FUNCTION, pc98_ieee_mode); 35080537Snyan bus_release_resource(dev, SYS_RES_IOPORT, rid, 35180537Snyan res); 35280537Snyan return ENXIO; 35348006Skato } 35448006Skato } 35548006Skato } 35680537Snyan 35780537Snyan bus_release_resource(dev, SYS_RES_IOPORT, rid, res); 35880537Snyan return 0; 35948006Skato#else 36048006Skato int port; 36148006Skato static short next_bios_lpt = 0; 36248006Skato int status; 36348006Skato static u_char testbyte[18] = { 36448006Skato 0x55, /* alternating zeros */ 36548006Skato 0xaa, /* alternating ones */ 36648006Skato 0xfe, 0xfd, 0xfb, 0xf7, 36748006Skato 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */ 36848006Skato 0x01, 0x02, 0x04, 0x08, 36948006Skato 0x10, 0x20, 0x40, 0x80 /* walking one */ 37048006Skato }; 37148006Skato int i; 37248006Skato 37348006Skato /* 37448006Skato * Make sure there is some way for lptopen to see that 37548006Skato * the port is not configured 37648006Skato * This 0 will remain if the port isn't attached 37748006Skato */ 37848006Skato (lpt_sc + dvp->id_unit)->sc_port = 0; 37948006Skato 38048006Skato status = IO_LPTSIZE; 38148006Skato /* If port not specified, use bios list */ 38248006Skato if(dvp->id_iobase < 0) { /* port? */ 38348006Skato if((next_bios_lpt < BIOS_MAX_LPT) && 38448006Skato (*(BIOS_PORTS+next_bios_lpt) != 0) ) { 38548006Skato dvp->id_iobase = *(BIOS_PORTS+next_bios_lpt++); 38648006Skato goto end_probe; 38748006Skato } else 38848006Skato return (0); 38948006Skato } 39048006Skato 39148006Skato /* Port was explicitly specified */ 39248006Skato /* This allows probing of ports unknown to the BIOS */ 39348006Skato port = dvp->id_iobase + lpt_data; 39448006Skato for (i = 0; i < 18; i++) { 39548006Skato if (!lpt_port_test(port, testbyte[i], 0xff)) { 39648006Skato status = 0; 39748006Skato goto end_probe; 39848006Skato } 39948006Skato } 40048006Skato 40148006Skatoend_probe: 40248006Skato /* write 0's to control and data ports */ 40348006Skato outb(dvp->id_iobase+lpt_data, 0); 40448006Skato outb(dvp->id_iobase+lpt_control, 0); 40548006Skato 40648006Skato return (status); 40748006Skato#endif 40848006Skato} 40948006Skato 41048006Skato/* XXX Todo - try and detect if interrupt is working */ 41148006Skatoint 41280537Snyanlpt_attach(device_t dev) 41348006Skato{ 41480537Snyan int rid, unit; 41548006Skato struct lpt_softc *sc; 41648006Skato 41780537Snyan unit = device_get_unit(dev); 41880537Snyan sc = device_get_softc(dev); 41980537Snyan 42080537Snyan rid = 0; 42180537Snyan sc->res_port = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, 42280537Snyan lpt_iat, 4, RF_ACTIVE); 42380537Snyan if (sc->res_port == NULL) 42480537Snyan return ENXIO; 42580537Snyan isa_load_resourcev(sc->res_port, lpt_iat, 4); 42680537Snyan 42780537Snyan sc->sc_port = rman_get_start(sc->res_port); 42848006Skato sc->sc_primed = 0; /* not primed yet */ 42948006Skato#ifdef PC98 43048006Skato outb(sc->sc_port+lpt_pstb_ctrl, LPC_DIS_PSTB); /* PSTB disable */ 43148006Skato outb(sc->sc_port+lpt_control, LPC_MODE8255); /* 8255 mode set */ 43248006Skato outb(sc->sc_port+lpt_control, LPC_NIRQ8); /* IRQ8 inactive */ 43348006Skato outb(sc->sc_port+lpt_control, LPC_NPSTB); /* PSTB inactive */ 43448006Skato outb(sc->sc_port+lpt_pstb_ctrl, LPC_EN_PSTB); /* PSTB enable */ 43548006Skato#else 43648006Skato outb(sc->sc_port+lpt_control, LPC_NINIT); 43748006Skato#endif 43848006Skato 43980537Snyan sc->sc_irq = 0; 44080537Snyan if (isa_get_irq(dev) != -1) { 44180537Snyan rid = 0; 44280537Snyan sc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 44380537Snyan 0, ~0, 1, RF_ACTIVE); 44480537Snyan if (sc->res_irq == NULL) { 44580537Snyan bus_release_resource(dev, SYS_RES_IOPORT, 0, 44680537Snyan sc->res_port); 44780537Snyan return ENXIO; 44880537Snyan } 44980537Snyan if (bus_setup_intr(dev, sc->res_irq, INTR_TYPE_TTY, lpt_intr, 45080537Snyan sc, &sc->sc_ih)) { 45180537Snyan bus_release_resource(dev, SYS_RES_IOPORT, 0, 45280537Snyan sc->res_port); 45380537Snyan bus_release_resource(dev, SYS_RES_IRQ, 0, 45480537Snyan sc->res_irq); 45580537Snyan return ENXIO; 45680537Snyan } 45748006Skato sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ; 45880537Snyan device_printf(dev, "Interrupt-driven port"); 45948006Skato } 46048006Skato 46148006Skato /* XXX what to do about the flags in the minor number? */ 46250436Sjulian make_dev(&lpt_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "lpt%d", unit); 46350436Sjulian make_dev(&lpt_cdevsw, unit | LP_BYPASS, 46450436Sjulian UID_ROOT, GID_WHEEL, 0600, "lpctl%d", unit); 46558142Snyan 46680537Snyan return 0; 46748006Skato} 46848006Skato 46948006Skato/* 47048006Skato * lptopen -- reset the printer, then wait until it's selected and not busy. 47148006Skato * If LP_BYPASS flag is selected, then we do not try to select the 47248006Skato * printer -- this is just used for passing ioctls. 47348006Skato */ 47448006Skato 47548006Skatostatic int 47683430Simplptopen (dev_t dev, int flags, int fmt, struct thread *td) 47748006Skato{ 47848006Skato struct lpt_softc *sc; 47948006Skato int s; 48048006Skato#ifdef PC98 48148006Skato int port; 48248006Skato#else 48348006Skato int trys, port; 48448006Skato#endif 48548006Skato 48680537Snyan sc = devclass_get_softc(olpt_devclass, LPTUNIT(minor(dev))); 48780537Snyan if (sc->sc_port == 0) 48848006Skato return (ENXIO); 48948006Skato 49048006Skato if (sc->sc_state) { 49148006Skato lprintf(("lp: still open %x\n", sc->sc_state)); 49248006Skato return(EBUSY); 49348006Skato } else 49448006Skato sc->sc_state |= INIT; 49548006Skato 49648006Skato sc->sc_flags = LPTFLAGS(minor(dev)); 49748006Skato 49848006Skato /* Check for open with BYPASS flag set. */ 49948006Skato if (sc->sc_flags & LP_BYPASS) { 50048006Skato sc->sc_state = OPEN; 50148006Skato return(0); 50248006Skato } 50348006Skato 50448006Skato s = spltty(); 50548006Skato lprintf(("lp flags 0x%x\n", sc->sc_flags)); 50648006Skato port = sc->sc_port; 50748006Skato 50848006Skato /* set IRQ status according to ENABLE_IRQ flag */ 50948006Skato if (sc->sc_irq & LP_ENABLE_IRQ) 51048006Skato sc->sc_irq |= LP_USE_IRQ; 51148006Skato else 51248006Skato sc->sc_irq &= ~LP_USE_IRQ; 51348006Skato 51448006Skato /* init printer */ 51548006Skato#ifndef PC98 51648006Skato if ((sc->sc_flags & LP_NO_PRIME) == 0) { 51748006Skato if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) { 51848006Skato outb(port+lpt_control, 0); 51948006Skato sc->sc_primed++; 52048006Skato DELAY(500); 52148006Skato } 52248006Skato } 52348006Skato 52448006Skato outb (port+lpt_control, LPC_SEL|LPC_NINIT); 52548006Skato 52648006Skato /* wait till ready (printer running diagnostics) */ 52748006Skato trys = 0; 52848006Skato do { 52948006Skato /* ran out of waiting for the printer */ 53048006Skato if (trys++ >= LPINITRDY*4) { 53148006Skato splx(s); 53248006Skato sc->sc_state = 0; 53348006Skato lprintf(("status %x\n", inb(port+lpt_status))); 53448006Skato return (EBUSY); 53548006Skato } 53648006Skato 53748006Skato /* wait 1/4 second, give up if we get a signal */ 53848006Skato if (tsleep ((caddr_t)sc, LPPRI|PCATCH, "lptinit", hz/4) != 53948006Skato EWOULDBLOCK) { 54048006Skato sc->sc_state = 0; 54148006Skato splx(s); 54248006Skato return (EBUSY); 54348006Skato } 54448006Skato 54548006Skato /* is printer online and ready for output */ 54648006Skato } while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 54748006Skato (LPS_SEL|LPS_NBSY|LPS_NERR)); 54848006Skato 54948006Skato sc->sc_control = LPC_SEL|LPC_NINIT; 55048006Skato if (sc->sc_flags & LP_AUTOLF) 55148006Skato sc->sc_control |= LPC_AUTOL; 55248006Skato 55348006Skato /* enable interrupt if interrupt-driven */ 55448006Skato if (sc->sc_irq & LP_USE_IRQ) 55548006Skato sc->sc_control |= LPC_ENA; 55648006Skato 55748006Skato outb(port+lpt_control, sc->sc_control); 55848006Skato#endif 55948006Skato 56048006Skato sc->sc_state = OPEN; 56148006Skato sc->sc_inbuf = geteblk(BUFSIZE); 56248006Skato sc->sc_xfercnt = 0; 56348006Skato splx(s); 56448006Skato 56548006Skato /* only use timeout if using interrupt */ 56648006Skato lprintf(("irq %x\n", sc->sc_irq)); 56748006Skato if (sc->sc_irq & LP_USE_IRQ) { 56848006Skato sc->sc_state |= TOUT; 56948006Skato timeout (lptout, (caddr_t)sc, 57048006Skato (sc->sc_backoff = hz/LPTOUTINITIAL)); 57148006Skato } 57248006Skato 57348006Skato lprintf(("opened.\n")); 57448006Skato return(0); 57548006Skato} 57648006Skato 57748006Skatostatic void 57848006Skatolptout (void *arg) 57948006Skato{ 58048006Skato struct lpt_softc *sc = arg; 58148006Skato int pl; 58248006Skato 58348006Skato lprintf(("T %x ", inb(sc->sc_port+lpt_status))); 58448006Skato if (sc->sc_state & OPEN) { 58548006Skato sc->sc_backoff++; 58648006Skato if (sc->sc_backoff > hz/LPTOUTMAX) 58748006Skato sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX; 58848006Skato timeout (lptout, (caddr_t)sc, sc->sc_backoff); 58948006Skato } else 59048006Skato sc->sc_state &= ~TOUT; 59148006Skato 59248006Skato if (sc->sc_state & ERROR) 59348006Skato sc->sc_state &= ~ERROR; 59448006Skato 59548006Skato /* 59648006Skato * Avoid possible hangs do to missed interrupts 59748006Skato */ 59848006Skato if (sc->sc_xfercnt) { 59948006Skato pl = spltty(); 60080537Snyan lpt_intr(sc); 60148006Skato splx(pl); 60248006Skato } else { 60348006Skato sc->sc_state &= ~OBUSY; 60448006Skato wakeup((caddr_t)sc); 60548006Skato } 60648006Skato} 60748006Skato 60848006Skato/* 60948006Skato * lptclose -- close the device, free the local line buffer. 61048006Skato * 61148006Skato * Check for interrupted write call added. 61248006Skato */ 61348006Skato 61448006Skatostatic int 61583430Simplptclose(dev_t dev, int flags, int fmt, struct thread *td) 61648006Skato{ 61780537Snyan struct lpt_softc *sc; 61848006Skato#ifndef PC98 61980537Snyan int port; 62048006Skato#endif 62148006Skato 62280537Snyan sc = devclass_get_softc(olpt_devclass, LPTUNIT(minor(dev))); 62348006Skato if(sc->sc_flags & LP_BYPASS) 62448006Skato goto end_close; 62548006Skato 62680537Snyan#ifndef PC98 62780537Snyan port = sc->sc_port; 62880537Snyan#endif 62948006Skato sc->sc_state &= ~OPEN; 63048006Skato 63148006Skato#ifndef PC98 63248006Skato /* if the last write was interrupted, don't complete it */ 63348006Skato if((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ)) 63448006Skato while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 63548006Skato (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt) 63648006Skato /* wait 1/4 second, give up if we get a signal */ 63748006Skato if (tsleep ((caddr_t)sc, LPPRI|PCATCH, 63848006Skato "lpclose", hz) != EWOULDBLOCK) 63948006Skato break; 64048006Skato 64148006Skato outb(sc->sc_port+lpt_control, LPC_NINIT); 64248006Skato#endif 64348006Skato brelse(sc->sc_inbuf); 64448006Skato 64548006Skatoend_close: 64648006Skato sc->sc_state = 0; 64748006Skato sc->sc_xfercnt = 0; 64848006Skato lprintf(("closed.\n")); 64948006Skato return(0); 65048006Skato} 65148006Skato 65248006Skato/* 65348006Skato * pushbytes() 65448006Skato * Workhorse for actually spinning and writing bytes to printer 65548006Skato * Derived from lpa.c 65648006Skato * Originally by ? 65748006Skato * 65848006Skato * This code is only used when we are polling the port 65948006Skato */ 66048006Skatostatic int 66148006Skatopushbytes(struct lpt_softc * sc) 66248006Skato{ 66348006Skato int spin, err, tic; 66448006Skato char ch; 66548006Skato int port = sc->sc_port; 66648006Skato 66748006Skato lprintf(("p")); 66848006Skato /* loop for every character .. */ 66948006Skato while (sc->sc_xfercnt > 0) { 67048006Skato /* printer data */ 67148006Skato ch = *(sc->sc_cp); 67248006Skato sc->sc_cp++; 67348006Skato sc->sc_xfercnt--; 67448006Skato 67548006Skato /* 67648006Skato * Wait for printer ready. 67748006Skato * Loop 20 usecs testing BUSY bit, then sleep 67848006Skato * for exponentially increasing timeout. (vak) 67948006Skato */ 68048006Skato for (spin=0; NOT_READY(port+lpt_status) && spin<MAX_SPIN; ++spin) 68148006Skato DELAY(1); /* XXX delay is NOT this accurate! */ 68248006Skato if (spin >= MAX_SPIN) { 68348006Skato tic = 0; 68448006Skato while (NOT_READY(port+lpt_status)) { 68548006Skato /* 68648006Skato * Now sleep, every cycle a 68748006Skato * little longer .. 68848006Skato */ 68948006Skato tic = tic + tic + 1; 69048006Skato /* 69148006Skato * But no more than 10 seconds. (vak) 69248006Skato */ 69348006Skato if (tic > MAX_SLEEP) 69448006Skato tic = MAX_SLEEP; 69548006Skato err = tsleep((caddr_t)sc, LPPRI, 69648006Skato "lptpoll", tic); 69748006Skato if (err != EWOULDBLOCK) { 69848006Skato return (err); 69948006Skato } 70048006Skato } 70148006Skato } 70248006Skato 70348006Skato /* output data */ 70448006Skato outb(port+lpt_data, ch); 70548006Skato#ifdef PC98 70648006Skato DELAY(1); 70748006Skato outb(port+lpt_control, LPC_PSTB); 70848006Skato DELAY(1); 70948006Skato outb(port+lpt_control, LPC_NPSTB); 71048006Skato#else 71148006Skato /* strobe */ 71248006Skato outb(port+lpt_control, sc->sc_control|LPC_STB); 71348006Skato outb(port+lpt_control, sc->sc_control); 71448006Skato#endif 71548006Skato 71648006Skato } 71748006Skato return(0); 71848006Skato} 71948006Skato 72048006Skato/* 72148006Skato * lptwrite --copy a line from user space to a local buffer, then call 72248006Skato * putc to get the chars moved to the output queue. 72348006Skato * 72448006Skato * Flagging of interrupted write added. 72548006Skato */ 72648006Skato 72748006Skatostatic int 72848006Skatolptwrite(dev_t dev, struct uio * uio, int ioflag) 72948006Skato{ 73048006Skato register unsigned n; 73148006Skato int pl, err; 73280537Snyan struct lpt_softc *sc; 73348006Skato 73480537Snyan sc = devclass_get_softc(olpt_devclass, LPTUNIT(minor(dev))); 73548006Skato if(sc->sc_flags & LP_BYPASS) { 73648006Skato /* we can't do writes in bypass mode */ 73748006Skato return(EPERM); 73848006Skato } 73948006Skato 74048006Skato sc->sc_state &= ~INTERRUPTED; 74148006Skato while ((n = min(BUFSIZE, uio->uio_resid)) != 0) { 74248006Skato sc->sc_cp = sc->sc_inbuf->b_data ; 74348006Skato uiomove(sc->sc_cp, n, uio); 74448006Skato sc->sc_xfercnt = n ; 74548006Skato while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) { 74648006Skato lprintf(("i")); 74748006Skato /* if the printer is ready for a char, */ 74848006Skato /* give it one */ 74948006Skato if ((sc->sc_state & OBUSY) == 0){ 75048006Skato lprintf(("\nC %d. ", sc->sc_xfercnt)); 75148006Skato pl = spltty(); 75280537Snyan lpt_intr(sc); 75348006Skato (void) splx(pl); 75448006Skato } 75548006Skato lprintf(("W ")); 75648006Skato if (sc->sc_state & OBUSY) 75748006Skato if ((err = tsleep ((caddr_t)sc, 75848006Skato LPPRI|PCATCH, "lpwrite", 0))) { 75948006Skato sc->sc_state |= INTERRUPTED; 76048006Skato return(err); 76148006Skato } 76248006Skato } 76348006Skato /* check to see if we must do a polled write */ 76448006Skato if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) { 76548006Skato lprintf(("p")); 76648006Skato if((err = pushbytes(sc))) 76748006Skato return(err); 76848006Skato } 76948006Skato } 77048006Skato return(0); 77148006Skato} 77248006Skato 77348006Skato/* 77448006Skato * lptintr -- handle printer interrupts which occur when the printer is 77548006Skato * ready to accept another char. 77648006Skato * 77748006Skato * do checking for interrupted write call. 77848006Skato */ 77948006Skato 78048006Skatostatic void 78180537Snyanlpt_intr(void *arg) 78248006Skato{ 78348006Skato} 78448006Skato 78548006Skatostatic int 78683430Simplptioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) 78748006Skato{ 78848006Skato int error = 0; 78948006Skato struct lpt_softc *sc; 79048006Skato u_int unit = LPTUNIT(minor(dev)); 79148006Skato u_char old_sc_irq; /* old printer IRQ status */ 79248006Skato 79380537Snyan sc = devclass_get_softc(olpt_devclass, unit); 79448006Skato 79548006Skato switch (cmd) { 79648006Skato case LPT_IRQ : 79748006Skato if(sc->sc_irq & LP_HAS_IRQ) { 79848006Skato /* 79948006Skato * NOTE: 80048006Skato * If the IRQ status is changed, 80148006Skato * this will only be visible on the 80248006Skato * next open. 80348006Skato * 80448006Skato * If interrupt status changes, 80548006Skato * this gets syslog'd. 80648006Skato */ 80748006Skato old_sc_irq = sc->sc_irq; 80848006Skato if(*(int*)data == 0) 80948006Skato sc->sc_irq &= (~LP_ENABLE_IRQ); 81048006Skato else 81148006Skato sc->sc_irq |= LP_ENABLE_IRQ; 81248006Skato if (old_sc_irq != sc->sc_irq ) 81348006Skato log(LOG_NOTICE, "lpt%c switched to %s mode\n", 81448006Skato (char)unit+'0', 81548006Skato (sc->sc_irq & LP_ENABLE_IRQ)? 81648006Skato "interrupt-driven":"polled"); 81748006Skato } else /* polled port */ 81848006Skato error = EOPNOTSUPP; 81948006Skato break; 82048006Skato default: 82148006Skato error = ENODEV; 82248006Skato } 82348006Skato 82448006Skato return(error); 82548006Skato} 826