olpt.c revision 146051
1139825Simp/*- 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 146051 2005-05-10 12:30:30Z nyan $ 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> 10761116Snyan#include <sys/bus.h> 10848006Skato#include <sys/kernel.h> 109130174Sphk#include <sys/module.h> 11048006Skato#include <sys/uio.h> 11148006Skato#include <sys/syslog.h> 112116431Sphk#include <sys/malloc.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 121146051Snyan#include <pc98/cbus/olptreg.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 DEBUG 13148006Skato#define lprintf(args) 13248006Skato#else 13348006Skato#define lprintf(args) do { \ 13448006Skato if (lptflag) \ 13548006Skato printf args; \ 13648006Skato } while (0) 13748006Skatostatic int volatile lptflag = 1; 13848006Skato#endif 13948006Skato 14048006Skato#define LPTUNIT(s) ((s)&0x03) 14148006Skato#define LPTFLAGS(s) ((s)&0xfc) 14248006Skato 14380537Snyanstruct lpt_softc { 14480537Snyan struct resource *res_port; 14580537Snyan struct resource *res_irq; 14680537Snyan void *sc_ih; 14780537Snyan 14848006Skato int sc_port; 14948006Skato short sc_state; 15048006Skato /* default case: negative prime, negative ack, handshake strobe, 15148006Skato prime once */ 15248006Skato u_char sc_control; 15348006Skato char sc_flags; 15448006Skato#define LP_POS_INIT 0x04 /* if we are a postive init signal */ 15548006Skato#define LP_POS_ACK 0x08 /* if we are a positive going ack */ 15648006Skato#define LP_NO_PRIME 0x10 /* don't prime the printer at all */ 15748006Skato#define LP_PRIMEOPEN 0x20 /* prime on every open */ 15848006Skato#define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */ 15948006Skato#define LP_BYPASS 0x80 /* bypass printer ready checks */ 160116431Sphk void *sc_inbuf; 16148006Skato short sc_xfercnt ; 16248006Skato char sc_primed; 16348006Skato char *sc_cp ; 16448006Skato u_char sc_irq ; /* IRQ status of port */ 16548006Skato#define LP_HAS_IRQ 0x01 /* we have an irq available */ 16648006Skato#define LP_USE_IRQ 0x02 /* we are using our irq */ 16748006Skato#define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */ 16848006Skato u_char sc_backoff ; /* time to call lptout() again */ 16980537Snyan}; 17048006Skato 17148006Skato/* bits for state */ 17248006Skato#define OPEN (1<<0) /* device is open */ 17348006Skato#define ASLP (1<<1) /* awaiting draining of printer */ 17448006Skato#define ERROR (1<<2) /* error was received from printer */ 17548006Skato#define OBUSY (1<<3) /* printer is busy doing output */ 17648006Skato#define LPTOUT (1<<4) /* timeout while not selected */ 17748006Skato#define TOUT (1<<5) /* timeout while not selected */ 17848006Skato#define INIT (1<<6) /* waiting to initialize for open */ 17948006Skato#define INTERRUPTED (1<<7) /* write call was interrupted */ 18048006Skato 18148006Skato 18248006Skato/* status masks to interrogate printer status */ 18348006Skato#define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */ 18448006Skato#define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR) 18548006Skato 18648006Skato/* Printer Ready condition - from lpa.c */ 18748006Skato/* Only used in polling code */ 18848006Skato#define NOT_READY(x) ((inb(x) & LPS_NBSY) != LPS_NBSY) 18948006Skato 19048006Skato#define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */ 19148006Skato#define MAX_SPIN 20 /* Max delay for device ready in usecs */ 19248006Skato 19348006Skatostatic timeout_t lptout; 19480537Snyanstatic int lpt_probe(device_t); 19580537Snyanstatic int lpt_attach(device_t); 19680537Snyanstatic void lpt_intr(void *); 19748006Skato 19880537Snyanstatic devclass_t olpt_devclass; 19980537Snyan 20080537Snyanstatic device_method_t olpt_methods[] = { 20180537Snyan DEVMETHOD(device_probe, lpt_probe), 20280537Snyan DEVMETHOD(device_attach, lpt_attach), 20380537Snyan { 0, 0 } 20448006Skato}; 20548006Skato 20680537Snyanstatic driver_t olpt_driver = { 20780537Snyan "olpt", 20880537Snyan olpt_methods, 20980537Snyan sizeof (struct lpt_softc), 21080537Snyan}; 21180537Snyan 21280537SnyanDRIVER_MODULE(olpt, isa, olpt_driver, olpt_devclass, 0, 0); 21380537Snyan 21448006Skatostatic d_open_t lptopen; 21548006Skatostatic d_close_t lptclose; 21648006Skatostatic d_write_t lptwrite; 21748006Skatostatic d_ioctl_t lptioctl; 21848006Skato 21948006Skatostatic struct cdevsw lpt_cdevsw = { 220126080Sphk .d_version = D_VERSION, 221126080Sphk .d_flags = D_NEEDGIANT, 222111815Sphk .d_open = lptopen, 223111815Sphk .d_close = lptclose, 224111815Sphk .d_write = lptwrite, 225111815Sphk .d_ioctl = lptioctl, 226111815Sphk .d_name = "lpt", 22748006Skato}; 22848006Skato 22980537Snyanstatic bus_addr_t lpt_iat[] = {0, 2, 4, 6}; 23080537Snyan 23148006Skato/* 23248006Skato * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94 23348006Skato * Based partially on Rod Grimes' printer probe 23448006Skato * 23548006Skato * Logic: 23648006Skato * 1) If no port address was given, use the bios detected ports 23748006Skato * and autodetect what ports the printers are on. 23848006Skato * 2) Otherwise, probe the data port at the address given, 23948006Skato * using the method in Rod Grimes' port probe. 24048006Skato * (Much code ripped off directly from Rod's probe.) 24148006Skato * 24248006Skato * Comments from Rod's probe: 24348006Skato * Logic: 24448006Skato * 1) You should be able to write to and read back the same value 24548006Skato * to the data port. Do an alternating zeros, alternating ones, 24648006Skato * walking zero, and walking one test to check for stuck bits. 24748006Skato * 24848006Skato * 2) You should be able to write to and read back the same value 24948006Skato * to the control port lower 5 bits, the upper 3 bits are reserved 25048006Skato * per the IBM PC technical reference manauls and different boards 25148006Skato * do different things with them. Do an alternating zeros, alternating 25248006Skato * ones, walking zero, and walking one test to check for stuck bits. 25348006Skato * 25448006Skato * Some printers drag the strobe line down when the are powered off 25548006Skato * so this bit has been masked out of the control port test. 25648006Skato * 25748006Skato * XXX Some printers may not like a fast pulse on init or strobe, I 25848006Skato * don't know at this point, if that becomes a problem these bits 25948006Skato * should be turned off in the mask byte for the control port test. 26048006Skato * 26148006Skato * We are finally left with a mask of 0x14, due to some printers 26248006Skato * being adamant about holding other bits high ........ 26348006Skato * 26448006Skato * Before probing the control port, we write a 0 to the data port - 26548006Skato * If not, some printers chuck out garbage when the strobe line 26648006Skato * gets toggled. 26748006Skato * 26848006Skato * 3) Set the data and control ports to a value of 0 26948006Skato * 27048006Skato * This probe routine has been tested on Epson Lx-800, HP LJ3P, 27148006Skato * Epson FX-1170 and C.Itoh 8510RM 27248006Skato * printers. 27348006Skato * Quick exit on fail added. 27448006Skato */ 27548006Skato 27648006Skatoint 27780537Snyanlpt_probe(device_t dev) 27848006Skato{ 27948006Skato#define PC98_OLD_LPT 0x40 28048006Skato#define PC98_IEEE_1284_FUNCTION 0x149 28180537Snyan int rid; 28280537Snyan struct resource *res; 28348006Skato 28480537Snyan /* Check isapnp ids */ 28580537Snyan if (isa_get_vendorid(dev)) 28680537Snyan return ENXIO; 28780537Snyan 28880537Snyan rid = 0; 28980537Snyan res = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, lpt_iat, 4, 29080537Snyan RF_ACTIVE); 29180537Snyan if (res == NULL) 29280537Snyan return ENXIO; 29380537Snyan isa_load_resourcev(res, lpt_iat, 4); 29480537Snyan 29580537Snyan if (isa_get_port(dev) == PC98_OLD_LPT) { 29680537Snyan unsigned int pc98_ieee_mode, tmp; 29780537Snyan 29848006Skato tmp = inb(PC98_IEEE_1284_FUNCTION); 29948006Skato pc98_ieee_mode = tmp; 30048006Skato if ((tmp & 0x10) == 0x10) { 30148006Skato outb(PC98_IEEE_1284_FUNCTION, tmp & ~0x10); 30248006Skato tmp = inb(PC98_IEEE_1284_FUNCTION); 30348006Skato if ((tmp & 0x10) != 0x10) { 30448006Skato outb(PC98_IEEE_1284_FUNCTION, pc98_ieee_mode); 30580537Snyan bus_release_resource(dev, SYS_RES_IOPORT, rid, 30680537Snyan res); 30780537Snyan return ENXIO; 30848006Skato } 30948006Skato } 31048006Skato } 31180537Snyan 31280537Snyan bus_release_resource(dev, SYS_RES_IOPORT, rid, res); 31380537Snyan return 0; 31448006Skato} 31548006Skato 31648006Skato/* XXX Todo - try and detect if interrupt is working */ 31748006Skatoint 31880537Snyanlpt_attach(device_t dev) 31948006Skato{ 32080537Snyan int rid, unit; 32148006Skato struct lpt_softc *sc; 32248006Skato 32380537Snyan unit = device_get_unit(dev); 32480537Snyan sc = device_get_softc(dev); 32580537Snyan 32680537Snyan rid = 0; 32780537Snyan sc->res_port = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, 32880537Snyan lpt_iat, 4, RF_ACTIVE); 32980537Snyan if (sc->res_port == NULL) 33080537Snyan return ENXIO; 33180537Snyan isa_load_resourcev(sc->res_port, lpt_iat, 4); 33280537Snyan 33380537Snyan sc->sc_port = rman_get_start(sc->res_port); 33448006Skato sc->sc_primed = 0; /* not primed yet */ 335145012Snyan 33648006Skato outb(sc->sc_port+lpt_pstb_ctrl, LPC_DIS_PSTB); /* PSTB disable */ 33748006Skato outb(sc->sc_port+lpt_control, LPC_MODE8255); /* 8255 mode set */ 33848006Skato outb(sc->sc_port+lpt_control, LPC_NIRQ8); /* IRQ8 inactive */ 33948006Skato outb(sc->sc_port+lpt_control, LPC_NPSTB); /* PSTB inactive */ 34048006Skato outb(sc->sc_port+lpt_pstb_ctrl, LPC_EN_PSTB); /* PSTB enable */ 34148006Skato 34280537Snyan sc->sc_irq = 0; 34380537Snyan if (isa_get_irq(dev) != -1) { 34480537Snyan rid = 0; 345127135Snjl sc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 346127135Snjl RF_ACTIVE); 34780537Snyan if (sc->res_irq == NULL) { 34880537Snyan bus_release_resource(dev, SYS_RES_IOPORT, 0, 34980537Snyan sc->res_port); 35080537Snyan return ENXIO; 35180537Snyan } 35280537Snyan if (bus_setup_intr(dev, sc->res_irq, INTR_TYPE_TTY, lpt_intr, 35380537Snyan sc, &sc->sc_ih)) { 35480537Snyan bus_release_resource(dev, SYS_RES_IOPORT, 0, 35580537Snyan sc->res_port); 35680537Snyan bus_release_resource(dev, SYS_RES_IRQ, 0, 35780537Snyan sc->res_irq); 35880537Snyan return ENXIO; 35980537Snyan } 36048006Skato sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ; 36180537Snyan device_printf(dev, "Interrupt-driven port"); 36248006Skato } 36348006Skato 36448006Skato /* XXX what to do about the flags in the minor number? */ 36550436Sjulian make_dev(&lpt_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "lpt%d", unit); 36650436Sjulian make_dev(&lpt_cdevsw, unit | LP_BYPASS, 36750436Sjulian UID_ROOT, GID_WHEEL, 0600, "lpctl%d", unit); 36858142Snyan 36980537Snyan return 0; 37048006Skato} 37148006Skato 37248006Skato/* 37348006Skato * lptopen -- reset the printer, then wait until it's selected and not busy. 37448006Skato * If LP_BYPASS flag is selected, then we do not try to select the 37548006Skato * printer -- this is just used for passing ioctls. 37648006Skato */ 37748006Skato 37848006Skatostatic int 379130585Sphklptopen (struct cdev *dev, int flags, int fmt, struct thread *td) 38048006Skato{ 38148006Skato struct lpt_softc *sc; 38248006Skato int s; 38348006Skato int port; 38448006Skato 38580537Snyan sc = devclass_get_softc(olpt_devclass, LPTUNIT(minor(dev))); 38680537Snyan if (sc->sc_port == 0) 38748006Skato return (ENXIO); 38848006Skato 38948006Skato if (sc->sc_state) { 39048006Skato lprintf(("lp: still open %x\n", sc->sc_state)); 39148006Skato return(EBUSY); 39248006Skato } else 39348006Skato sc->sc_state |= INIT; 39448006Skato 39548006Skato sc->sc_flags = LPTFLAGS(minor(dev)); 39648006Skato 39748006Skato /* Check for open with BYPASS flag set. */ 39848006Skato if (sc->sc_flags & LP_BYPASS) { 39948006Skato sc->sc_state = OPEN; 40048006Skato return(0); 40148006Skato } 40248006Skato 40348006Skato s = spltty(); 40448006Skato lprintf(("lp flags 0x%x\n", sc->sc_flags)); 40548006Skato port = sc->sc_port; 40648006Skato 40748006Skato /* set IRQ status according to ENABLE_IRQ flag */ 40848006Skato if (sc->sc_irq & LP_ENABLE_IRQ) 40948006Skato sc->sc_irq |= LP_USE_IRQ; 41048006Skato else 41148006Skato sc->sc_irq &= ~LP_USE_IRQ; 41248006Skato 41348006Skato /* init printer */ 41448006Skato sc->sc_state = OPEN; 415116431Sphk sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK); 41648006Skato sc->sc_xfercnt = 0; 41748006Skato splx(s); 41848006Skato 41948006Skato /* only use timeout if using interrupt */ 42048006Skato lprintf(("irq %x\n", sc->sc_irq)); 42148006Skato if (sc->sc_irq & LP_USE_IRQ) { 42248006Skato sc->sc_state |= TOUT; 42348006Skato timeout (lptout, (caddr_t)sc, 42448006Skato (sc->sc_backoff = hz/LPTOUTINITIAL)); 42548006Skato } 42648006Skato 42748006Skato lprintf(("opened.\n")); 42848006Skato return(0); 42948006Skato} 43048006Skato 43148006Skatostatic void 43248006Skatolptout (void *arg) 43348006Skato{ 43448006Skato struct lpt_softc *sc = arg; 43548006Skato int pl; 43648006Skato 43748006Skato lprintf(("T %x ", inb(sc->sc_port+lpt_status))); 43848006Skato if (sc->sc_state & OPEN) { 43948006Skato sc->sc_backoff++; 44048006Skato if (sc->sc_backoff > hz/LPTOUTMAX) 44148006Skato sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX; 44248006Skato timeout (lptout, (caddr_t)sc, sc->sc_backoff); 44348006Skato } else 44448006Skato sc->sc_state &= ~TOUT; 44548006Skato 44648006Skato if (sc->sc_state & ERROR) 44748006Skato sc->sc_state &= ~ERROR; 44848006Skato 44948006Skato /* 45048006Skato * Avoid possible hangs do to missed interrupts 45148006Skato */ 45248006Skato if (sc->sc_xfercnt) { 45348006Skato pl = spltty(); 45480537Snyan lpt_intr(sc); 45548006Skato splx(pl); 45648006Skato } else { 45748006Skato sc->sc_state &= ~OBUSY; 458111748Sdes wakeup(sc); 45948006Skato } 46048006Skato} 46148006Skato 46248006Skato/* 46348006Skato * lptclose -- close the device, free the local line buffer. 46448006Skato * 46548006Skato * Check for interrupted write call added. 46648006Skato */ 46748006Skato 46848006Skatostatic int 469130585Sphklptclose(struct cdev *dev, int flags, int fmt, struct thread *td) 47048006Skato{ 47180537Snyan struct lpt_softc *sc; 47248006Skato 47380537Snyan sc = devclass_get_softc(olpt_devclass, LPTUNIT(minor(dev))); 47448006Skato if(sc->sc_flags & LP_BYPASS) 47548006Skato goto end_close; 47648006Skato 47748006Skato sc->sc_state &= ~OPEN; 478116431Sphk free(sc->sc_inbuf, M_DEVBUF); 47948006Skato 48048006Skatoend_close: 48148006Skato sc->sc_state = 0; 48248006Skato sc->sc_xfercnt = 0; 48348006Skato lprintf(("closed.\n")); 48448006Skato return(0); 48548006Skato} 48648006Skato 48748006Skato/* 48848006Skato * pushbytes() 48948006Skato * Workhorse for actually spinning and writing bytes to printer 49048006Skato * Derived from lpa.c 49148006Skato * Originally by ? 49248006Skato * 49348006Skato * This code is only used when we are polling the port 49448006Skato */ 49548006Skatostatic int 49648006Skatopushbytes(struct lpt_softc * sc) 49748006Skato{ 49848006Skato int spin, err, tic; 49948006Skato char ch; 50048006Skato int port = sc->sc_port; 50148006Skato 50248006Skato lprintf(("p")); 50348006Skato /* loop for every character .. */ 50448006Skato while (sc->sc_xfercnt > 0) { 50548006Skato /* printer data */ 50648006Skato ch = *(sc->sc_cp); 50748006Skato sc->sc_cp++; 50848006Skato sc->sc_xfercnt--; 50948006Skato 51048006Skato /* 51148006Skato * Wait for printer ready. 51248006Skato * Loop 20 usecs testing BUSY bit, then sleep 51348006Skato * for exponentially increasing timeout. (vak) 51448006Skato */ 51548006Skato for (spin=0; NOT_READY(port+lpt_status) && spin<MAX_SPIN; ++spin) 51648006Skato DELAY(1); /* XXX delay is NOT this accurate! */ 51748006Skato if (spin >= MAX_SPIN) { 51848006Skato tic = 0; 51948006Skato while (NOT_READY(port+lpt_status)) { 52048006Skato /* 52148006Skato * Now sleep, every cycle a 52248006Skato * little longer .. 52348006Skato */ 52448006Skato tic = tic + tic + 1; 52548006Skato /* 52648006Skato * But no more than 10 seconds. (vak) 52748006Skato */ 52848006Skato if (tic > MAX_SLEEP) 52948006Skato tic = MAX_SLEEP; 530111748Sdes err = tsleep(sc, LPPRI, 53148006Skato "lptpoll", tic); 53248006Skato if (err != EWOULDBLOCK) { 53348006Skato return (err); 53448006Skato } 53548006Skato } 53648006Skato } 53748006Skato 53848006Skato /* output data */ 53948006Skato outb(port+lpt_data, ch); 54048006Skato DELAY(1); 54148006Skato outb(port+lpt_control, LPC_PSTB); 54248006Skato DELAY(1); 54348006Skato outb(port+lpt_control, LPC_NPSTB); 54448006Skato } 54548006Skato return(0); 54648006Skato} 54748006Skato 54848006Skato/* 54948006Skato * lptwrite --copy a line from user space to a local buffer, then call 55048006Skato * putc to get the chars moved to the output queue. 55148006Skato * 55248006Skato * Flagging of interrupted write added. 55348006Skato */ 55448006Skato 55548006Skatostatic int 556130585Sphklptwrite(struct cdev *dev, struct uio * uio, int ioflag) 55748006Skato{ 55848006Skato register unsigned n; 55948006Skato int pl, err; 56080537Snyan struct lpt_softc *sc; 56148006Skato 56280537Snyan sc = devclass_get_softc(olpt_devclass, LPTUNIT(minor(dev))); 56348006Skato if(sc->sc_flags & LP_BYPASS) { 56448006Skato /* we can't do writes in bypass mode */ 56548006Skato return(EPERM); 56648006Skato } 56748006Skato 56848006Skato sc->sc_state &= ~INTERRUPTED; 56948006Skato while ((n = min(BUFSIZE, uio->uio_resid)) != 0) { 570116431Sphk sc->sc_cp = sc->sc_inbuf; 57148006Skato uiomove(sc->sc_cp, n, uio); 57248006Skato sc->sc_xfercnt = n ; 57348006Skato while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) { 57448006Skato lprintf(("i")); 57548006Skato /* if the printer is ready for a char, */ 57648006Skato /* give it one */ 57748006Skato if ((sc->sc_state & OBUSY) == 0){ 57848006Skato lprintf(("\nC %d. ", sc->sc_xfercnt)); 57948006Skato pl = spltty(); 58080537Snyan lpt_intr(sc); 58148006Skato (void) splx(pl); 58248006Skato } 58348006Skato lprintf(("W ")); 58448006Skato if (sc->sc_state & OBUSY) 585111748Sdes if ((err = tsleep (sc, 58648006Skato LPPRI|PCATCH, "lpwrite", 0))) { 58748006Skato sc->sc_state |= INTERRUPTED; 58848006Skato return(err); 58948006Skato } 59048006Skato } 59148006Skato /* check to see if we must do a polled write */ 59248006Skato if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) { 59348006Skato lprintf(("p")); 59448006Skato if((err = pushbytes(sc))) 59548006Skato return(err); 59648006Skato } 59748006Skato } 59848006Skato return(0); 59948006Skato} 60048006Skato 60148006Skato/* 60248006Skato * lptintr -- handle printer interrupts which occur when the printer is 60348006Skato * ready to accept another char. 60448006Skato * 60548006Skato * do checking for interrupted write call. 60648006Skato */ 60748006Skato 60848006Skatostatic void 60980537Snyanlpt_intr(void *arg) 61048006Skato{ 61148006Skato} 61248006Skato 61348006Skatostatic int 614130585Sphklptioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td) 61548006Skato{ 61648006Skato int error = 0; 61748006Skato struct lpt_softc *sc; 61848006Skato u_int unit = LPTUNIT(minor(dev)); 61948006Skato u_char old_sc_irq; /* old printer IRQ status */ 62048006Skato 62180537Snyan sc = devclass_get_softc(olpt_devclass, unit); 62248006Skato 62348006Skato switch (cmd) { 62448006Skato case LPT_IRQ : 62548006Skato if(sc->sc_irq & LP_HAS_IRQ) { 62648006Skato /* 62748006Skato * NOTE: 62848006Skato * If the IRQ status is changed, 62948006Skato * this will only be visible on the 63048006Skato * next open. 63148006Skato * 63248006Skato * If interrupt status changes, 63348006Skato * this gets syslog'd. 63448006Skato */ 63548006Skato old_sc_irq = sc->sc_irq; 63648006Skato if(*(int*)data == 0) 63748006Skato sc->sc_irq &= (~LP_ENABLE_IRQ); 63848006Skato else 63948006Skato sc->sc_irq |= LP_ENABLE_IRQ; 64048006Skato if (old_sc_irq != sc->sc_irq ) 64148006Skato log(LOG_NOTICE, "lpt%c switched to %s mode\n", 64248006Skato (char)unit+'0', 64348006Skato (sc->sc_irq & LP_ENABLE_IRQ)? 64448006Skato "interrupt-driven":"polled"); 64548006Skato } else /* polled port */ 64648006Skato error = EOPNOTSUPP; 64748006Skato break; 64848006Skato default: 64948006Skato error = ENODEV; 65048006Skato } 65148006Skato 65248006Skato return(error); 65348006Skato} 654