The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/ppbus/nlpt.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 1990 William F. Jolitz, TeleMuse
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This software is a component of "386BSD" developed by
   16  *      William F. Jolitz, TeleMuse.
   17  * 4. Neither the name of the developer nor the name "386BSD"
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
   22  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
   23  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
   24  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
   25  * NOT MAKE USE OF THIS WORK.
   26  *
   27  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
   28  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
   29  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
   30  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
   31  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
   32  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
   33  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
   34  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
   35  *
   36  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
   37  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   39  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
   40  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   41  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   42  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   44  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   46  * SUCH DAMAGE.
   47  *
   48  *      from: unknown origin, 386BSD 0.1
   49  *      From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
   50  * $FreeBSD$
   51  */
   52 
   53 /*
   54  * Device Driver for AT parallel printer port
   55  * Written by William Jolitz 12/18/90
   56  */
   57 
   58 /*
   59  * Updated for ppbus by Nicolas Souchu
   60  * [Mon Jul 28 1997]
   61  */
   62 
   63 
   64 #ifdef KERNEL
   65 #include "opt_devfs.h"
   66 
   67 #include <sys/param.h>
   68 #include <sys/systm.h>
   69 #include <sys/conf.h>
   70 #include <sys/buf.h>
   71 #include <sys/kernel.h>
   72 #include <sys/uio.h>
   73 #include <sys/syslog.h>
   74 #ifdef DEVFS
   75 #include <sys/devfsext.h>
   76 #endif /*DEVFS*/
   77 #include <sys/malloc.h>
   78 
   79 #include <machine/clock.h>
   80 #include <machine/lpt.h>
   81 #endif /*KERNEL*/
   82 
   83 #include <dev/ppbus/ppbconf.h>
   84 #include <dev/ppbus/ppb_1284.h>
   85 #include <dev/ppbus/nlpt.h>
   86 
   87 #include "opt_nlpt.h"
   88 
   89 #ifndef NLPT_DEBUG
   90 #define nlprintf(args)
   91 #else
   92 #define nlprintf(args)                                          \
   93                 do {                                            \
   94                         if (nlptflag)                           \
   95                                 printf args;                    \
   96                 } while (0)
   97 static int volatile nlptflag = 1;
   98 #endif
   99 
  100 #define LPINITRDY       4       /* wait up to 4 seconds for a ready */
  101 #define LPTOUTINITIAL   10      /* initial timeout to wait for ready 1/10 s */
  102 #define LPTOUTMAX       1       /* maximal timeout 1 s */
  103 #define LPPRI           (PZERO+8)
  104 #define BUFSIZE         1024
  105 #define BUFSTATSIZE     32
  106 
  107 #define LPTUNIT(s)      ((s)&0x03)
  108 #define LPTFLAGS(s)     ((s)&0xfc)
  109 
  110 static int      nlpt = 0;
  111 #define MAXLPT  8                       /* XXX not much better! */
  112 static struct lpt_data *lptdata[MAXLPT];
  113 
  114 #define LPT_NAME        "nlpt"          /* our official name */
  115 
  116 static timeout_t nlptout;
  117 static int      nlpt_port_test(struct lpt_data *sc, u_char data, u_char mask);
  118 static int      nlpt_detect(struct lpt_data *sc);
  119 
  120 /*
  121  * Make ourselves visible as a ppbus driver
  122  */
  123 
  124 static struct ppb_device        *nlptprobe(struct ppb_data *ppb);
  125 static int                      nlptattach(struct ppb_device *dev);
  126 static void                     nlptintr(int unit);
  127 static void                     nlpt_drvinit(void *unused);
  128 
  129 static void                     nlpt_intr(int unit);    /* without spls */
  130 
  131 #ifdef KERNEL
  132 
  133 static struct ppb_driver nlptdriver = {
  134     nlptprobe, nlptattach, LPT_NAME
  135 };
  136 DATA_SET(ppbdriver_set, nlptdriver);
  137 
  138 #endif /* KERNEL */
  139 
  140 /* bits for state */
  141 #define OPEN            (1<<0)  /* device is open */
  142 #define ASLP            (1<<1)  /* awaiting draining of printer */
  143 #define EERROR          (1<<2)  /* error was received from printer */
  144 #define OBUSY           (1<<3)  /* printer is busy doing output */
  145 #define LPTOUT          (1<<4)  /* timeout while not selected */
  146 #define TOUT            (1<<5)  /* timeout while not selected */
  147 #define LPTINIT         (1<<6)  /* waiting to initialize for open */
  148 #define INTERRUPTED     (1<<7)  /* write call was interrupted */
  149 
  150 #define HAVEBUS         (1<<8)  /* the driver owns the bus */
  151 
  152 
  153 /* status masks to interrogate printer status */
  154 #define RDY_MASK        (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)     /* ready ? */
  155 #define LP_READY        (LPS_SEL|LPS_NBSY|LPS_NERR)
  156 
  157 /* Printer Ready condition  - from lpa.c */
  158 /* Only used in polling code */
  159 #define LPS_INVERT      (LPS_NBSY | LPS_NACK |           LPS_SEL | LPS_NERR)
  160 #define LPS_MASK        (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
  161 #define NOT_READY(lpt)  ((ppb_rstr(&(lpt)->lpt_dev)^LPS_INVERT)&LPS_MASK)
  162 
  163 #define MAX_SLEEP       (hz*5)  /* Timeout while waiting for device ready */
  164 #define MAX_SPIN        20      /* Max delay for device ready in usecs */
  165 
  166 
  167 static  d_open_t        nlptopen;
  168 static  d_close_t       nlptclose;
  169 static  d_write_t       nlptwrite;
  170 static  d_read_t        nlptread;
  171 static  d_ioctl_t       nlptioctl;
  172 
  173 #define CDEV_MAJOR 16
  174 static struct cdevsw nlpt_cdevsw = 
  175         { nlptopen,     nlptclose,      nlptread,       nlptwrite,      /*16*/
  176           nlptioctl,    nullstop,       nullreset,      nodevtotty,     /* lpt */
  177           seltrue,      nommap,         nostrat,        LPT_NAME,       NULL,   -1 };
  178 
  179 static int
  180 lpt_request_ppbus(struct lpt_data *sc, int how)
  181 {
  182         int error;
  183 
  184         if (sc->sc_state & HAVEBUS)
  185                 return (0);
  186 
  187         /* we have the bus only if the request succeded */
  188         if ((error = ppb_request_bus(&sc->lpt_dev, how)) == 0)
  189                 sc->sc_state |= HAVEBUS;
  190 
  191         return (error);
  192 }
  193 
  194 static int
  195 lpt_release_ppbus(struct lpt_data *sc)
  196 {
  197         ppb_release_bus(&sc->lpt_dev);
  198         sc->sc_state &= ~HAVEBUS;
  199 
  200         return (0);
  201 }
  202 
  203 /*
  204  * Internal routine to nlptprobe to do port tests of one byte value
  205  */
  206 static int
  207 nlpt_port_test(struct lpt_data *sc, u_char data, u_char mask)
  208 {
  209         int     temp, timeout;
  210 
  211         data = data & mask;
  212         ppb_wdtr(&sc->lpt_dev, data);
  213         timeout = 10000;
  214         do {
  215                 DELAY(10);
  216                 temp = ppb_rdtr(&sc->lpt_dev) & mask;
  217         }
  218         while (temp != data && --timeout);
  219         nlprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
  220         return (temp == data);
  221 }
  222 
  223 /*
  224  * Probe simplified by replacing multiple loops with a hardcoded
  225  * test pattern - 1999/02/08 des@freebsd.org
  226  *
  227  * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
  228  * Based partially on Rod Grimes' printer probe
  229  *
  230  * Logic:
  231  *      1) If no port address was given, use the bios detected ports
  232  *         and autodetect what ports the printers are on.
  233  *      2) Otherwise, probe the data port at the address given,
  234  *         using the method in Rod Grimes' port probe.
  235  *         (Much code ripped off directly from Rod's probe.)
  236  *
  237  * Comments from Rod's probe:
  238  * Logic:
  239  *      1) You should be able to write to and read back the same value
  240  *         to the data port.  Do an alternating zeros, alternating ones,
  241  *         walking zero, and walking one test to check for stuck bits.
  242  *
  243  *      2) You should be able to write to and read back the same value
  244  *         to the control port lower 5 bits, the upper 3 bits are reserved
  245  *         per the IBM PC technical reference manauls and different boards
  246  *         do different things with them.  Do an alternating zeros, alternating
  247  *         ones, walking zero, and walking one test to check for stuck bits.
  248  *
  249  *         Some printers drag the strobe line down when the are powered off
  250  *         so this bit has been masked out of the control port test.
  251  *
  252  *         XXX Some printers may not like a fast pulse on init or strobe, I
  253  *         don't know at this point, if that becomes a problem these bits
  254  *         should be turned off in the mask byte for the control port test.
  255  *
  256  *         We are finally left with a mask of 0x14, due to some printers
  257  *         being adamant about holding other bits high ........
  258  *
  259  *         Before probing the control port, we write a 0 to the data port -
  260  *         If not, some printers chuck out garbage when the strobe line
  261  *         gets toggled.
  262  *
  263  *      3) Set the data and control ports to a value of 0
  264  *
  265  *      This probe routine has been tested on Epson Lx-800, HP LJ3P,
  266  *      Epson FX-1170 and C.Itoh 8510RM
  267  *      printers.
  268  *      Quick exit on fail added.
  269  */
  270 static int
  271 nlpt_detect(struct lpt_data *sc)
  272 {
  273         static u_char   testbyte[18] = {
  274                 0x55,                   /* alternating zeros */
  275                 0xaa,                   /* alternating ones */
  276                 0xfe, 0xfd, 0xfb, 0xf7,
  277                 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */
  278                 0x01, 0x02, 0x04, 0x08,
  279                 0x10, 0x20, 0x40, 0x80  /* walking one */
  280         };
  281         int             i, error, status;
  282 
  283         status = 1;                             /* assume success */
  284 
  285         if ((error = lpt_request_ppbus(sc, PPB_DONTWAIT))) {
  286                 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
  287                 status = 0;
  288                 goto end_probe;
  289         }
  290 
  291         for (i = 0; i < 18 && status; i++)
  292                 if (!nlpt_port_test(sc, testbyte[i], 0xff)) {
  293                         status = 0;
  294                         goto end_probe;
  295                 }
  296 
  297 end_probe:
  298         /* write 0's to control and data ports */
  299         ppb_wdtr(&sc->lpt_dev, 0);
  300         ppb_wctr(&sc->lpt_dev, 0);
  301 
  302         lpt_release_ppbus(sc);
  303 
  304         return (status);
  305 }
  306 
  307 /*
  308  * nlptprobe()
  309  */
  310 static struct ppb_device *
  311 nlptprobe(struct ppb_data *ppb)
  312 {
  313         struct lpt_data *sc;
  314 
  315         sc = (struct lpt_data *) malloc(sizeof(struct lpt_data),
  316                                                         M_TEMP, M_NOWAIT);
  317         if (!sc) {
  318                 printf(LPT_NAME ": cannot malloc!\n");
  319                 return (0);
  320         }
  321         bzero(sc, sizeof(struct lpt_data));
  322 
  323         lptdata[nlpt] = sc;
  324 
  325         /*
  326          * lpt dependent initialisation.
  327          */
  328         sc->lpt_unit = nlpt;
  329 
  330         /*
  331          * ppbus dependent initialisation.
  332          */
  333         sc->lpt_dev.id_unit = sc->lpt_unit;
  334         sc->lpt_dev.name = nlptdriver.name;
  335         sc->lpt_dev.ppb = ppb;
  336         sc->lpt_dev.intr = nlptintr;
  337 
  338         /*
  339          * Now, try to detect the printer.
  340          */
  341         if (!nlpt_detect(sc)) {
  342                 free(sc, M_TEMP);
  343                 return (0);
  344         }
  345 
  346         /* Ok, go to next device on next probe */
  347         nlpt ++;
  348 
  349         return (&sc->lpt_dev);
  350 }
  351 
  352 static int
  353 nlptattach(struct ppb_device *dev)
  354 {
  355         struct lpt_data *sc = lptdata[dev->id_unit];
  356         int error;
  357 
  358         /*
  359          * Report ourselves
  360          */
  361         printf(LPT_NAME "%d: <generic printer> on ppbus %d\n",
  362                dev->id_unit, dev->ppb->ppb_link->adapter_unit);
  363 
  364         sc->sc_primed = 0;      /* not primed yet */
  365 
  366         if ((error = lpt_request_ppbus(sc, PPB_DONTWAIT))) {
  367                 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
  368                 return (0);
  369         }
  370 
  371         ppb_wctr(&sc->lpt_dev, LPC_NINIT);
  372 
  373         /* check if we can use interrupt, should be done by ppc stuff */
  374         nlprintf(("oldirq %x\n", sc->sc_irq));
  375         if (ppb_get_irq(&sc->lpt_dev)) {
  376                 sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
  377                 printf(LPT_NAME "%d: Interrupt-driven port\n", dev->id_unit);
  378         } else {
  379                 sc->sc_irq = 0;
  380                 nlprintf((LPT_NAME "%d: Polled port\n", dev->id_unit));
  381         }
  382         nlprintf(("irq %x\n", sc->sc_irq));
  383 
  384         lpt_release_ppbus(sc);
  385 
  386 #ifdef DEVFS
  387         sc->devfs_token = devfs_add_devswf(&nlpt_cdevsw,
  388                 dev->id_unit, DV_CHR,
  389                 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", dev->id_unit);
  390         sc->devfs_token_ctl = devfs_add_devswf(&nlpt_cdevsw,
  391                 dev->id_unit | LP_BYPASS, DV_CHR,
  392                 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", dev->id_unit);
  393 #endif
  394 
  395         return (1);
  396 }
  397 
  398 static void
  399 nlptout(void *arg)
  400 {
  401         struct lpt_data *sc = arg;
  402         int pl;
  403 
  404         nlprintf(("T %x ", ppb_rstr(&sc->lpt_dev)));
  405         if (sc->sc_state & OPEN) {
  406                 sc->sc_backoff++;
  407                 if (sc->sc_backoff > hz/LPTOUTMAX)
  408                         sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
  409                 timeout(nlptout, (caddr_t)sc, sc->sc_backoff);
  410         } else
  411                 sc->sc_state &= ~TOUT;
  412 
  413         if (sc->sc_state & EERROR)
  414                 sc->sc_state &= ~EERROR;
  415 
  416         /*
  417          * Avoid possible hangs do to missed interrupts
  418          */
  419         if (sc->sc_xfercnt) {
  420                 pl = spltty();
  421                 nlpt_intr(sc->lpt_unit);
  422                 splx(pl);
  423         } else {
  424                 sc->sc_state &= ~OBUSY;
  425                 wakeup((caddr_t)sc);
  426         }
  427 }
  428 
  429 /*
  430  * nlptopen -- reset the printer, then wait until it's selected and not busy.
  431  *      If LP_BYPASS flag is selected, then we do not try to select the
  432  *      printer -- this is just used for passing ioctls.
  433  */
  434 
  435 static  int
  436 nlptopen(dev_t dev, int flags, int fmt, struct proc *p)
  437 {
  438         struct lpt_data *sc;
  439 
  440         int s;
  441         int trys, err;
  442         u_int unit = LPTUNIT(minor(dev));
  443 
  444         if ((unit >= nlpt))
  445                 return (ENXIO);
  446 
  447         sc = lptdata[unit];
  448 
  449         if (sc->sc_state) {
  450                 nlprintf((LPT_NAME ": still open %x\n", sc->sc_state));
  451                 return(EBUSY);
  452         } else
  453                 sc->sc_state |= LPTINIT;
  454 
  455         sc->sc_flags = LPTFLAGS(minor(dev));
  456 
  457         /* Check for open with BYPASS flag set. */
  458         if (sc->sc_flags & LP_BYPASS) {
  459                 sc->sc_state = OPEN;
  460                 return(0);
  461         }
  462 
  463         /* request the ppbus only if we don't have it already */
  464         if ((err = lpt_request_ppbus(sc, PPB_WAIT|PPB_INTR)) != 0)
  465                 return (err);
  466 
  467         s = spltty();
  468         nlprintf((LPT_NAME " flags 0x%x\n", sc->sc_flags));
  469 
  470         /* set IRQ status according to ENABLE_IRQ flag */
  471         if (sc->sc_irq & LP_ENABLE_IRQ)
  472                 sc->sc_irq |= LP_USE_IRQ;
  473         else
  474                 sc->sc_irq &= ~LP_USE_IRQ;
  475 
  476         /* init printer */
  477         if ((sc->sc_flags & LP_NO_PRIME) == 0) {
  478                 if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
  479                         ppb_wctr(&sc->lpt_dev, 0);
  480                         sc->sc_primed++;
  481                         DELAY(500);
  482                 }
  483         }
  484 
  485         ppb_wctr(&sc->lpt_dev, LPC_SEL|LPC_NINIT);
  486 
  487         /* wait till ready (printer running diagnostics) */
  488         trys = 0;
  489         do {
  490                 /* ran out of waiting for the printer */
  491                 if (trys++ >= LPINITRDY*4) {
  492                         splx(s);
  493                         sc->sc_state = 0;
  494                         nlprintf(("status %x\n", ppb_rstr(&sc->lpt_dev)));
  495 
  496                         lpt_release_ppbus(sc);
  497                         return (EBUSY);
  498                 }
  499 
  500                 /* wait 1/4 second, give up if we get a signal */
  501                 if (tsleep((caddr_t)sc, LPPRI|PCATCH, "lptinit", hz/4) !=
  502                     EWOULDBLOCK) {
  503                         sc->sc_state = 0;
  504                         splx(s);
  505 
  506                         lpt_release_ppbus(sc);
  507                         return (EBUSY);
  508                 }
  509 
  510                 /* is printer online and ready for output */
  511         } while ((ppb_rstr(&sc->lpt_dev) &
  512                         (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
  513                                         (LPS_SEL|LPS_NBSY|LPS_NERR));
  514 
  515         sc->sc_control = LPC_SEL|LPC_NINIT;
  516         if (sc->sc_flags & LP_AUTOLF)
  517                 sc->sc_control |= LPC_AUTOL;
  518 
  519         /* enable interrupt if interrupt-driven */
  520         if (sc->sc_irq & LP_USE_IRQ)
  521                 sc->sc_control |= LPC_ENA;
  522 
  523         ppb_wctr(&sc->lpt_dev, sc->sc_control);
  524 
  525         sc->sc_state = OPEN;
  526         sc->sc_inbuf = geteblk(BUFSIZE);
  527         sc->sc_statbuf = geteblk(BUFSTATSIZE);
  528         sc->sc_xfercnt = 0;
  529         splx(s);
  530 
  531         /* release the ppbus */
  532         lpt_release_ppbus(sc);
  533 
  534         /* only use timeout if using interrupt */
  535         nlprintf(("irq %x\n", sc->sc_irq));
  536         if (sc->sc_irq & LP_USE_IRQ) {
  537                 sc->sc_state |= TOUT;
  538                 timeout(nlptout, (caddr_t)sc,
  539                          (sc->sc_backoff = hz/LPTOUTINITIAL));
  540         }
  541 
  542         nlprintf(("opened.\n"));
  543         return(0);
  544 }
  545 
  546 /*
  547  * nlptclose -- close the device, free the local line buffer.
  548  *
  549  * Check for interrupted write call added.
  550  */
  551 
  552 static  int
  553 nlptclose(dev_t dev, int flags, int fmt, struct proc *p)
  554 {
  555         struct lpt_data *sc = lptdata[LPTUNIT(minor(dev))];
  556         int err;
  557 
  558         if(sc->sc_flags & LP_BYPASS)
  559                 goto end_close;
  560 
  561         if ((err = lpt_request_ppbus(sc, PPB_WAIT|PPB_INTR)) != 0)
  562                 return (err);
  563 
  564         sc->sc_state &= ~OPEN;
  565 
  566         /* if the last write was interrupted, don't complete it */
  567         if((!(sc->sc_state  & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
  568                 while ((ppb_rstr(&sc->lpt_dev) &
  569                         (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
  570                         (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
  571                         /* wait 1/4 second, give up if we get a signal */
  572                         if (tsleep((caddr_t)sc, LPPRI|PCATCH,
  573                                 "lpclose", hz) != EWOULDBLOCK)
  574                                 break;
  575 
  576         ppb_wctr(&sc->lpt_dev, LPC_NINIT);
  577         brelse(sc->sc_inbuf);
  578         brelse(sc->sc_statbuf);
  579 
  580 end_close:
  581         /* release the bus anyway */
  582         lpt_release_ppbus(sc);
  583 
  584         sc->sc_state = 0;
  585         sc->sc_xfercnt = 0;
  586         nlprintf(("closed.\n"));
  587         return(0);
  588 }
  589 
  590 /*
  591  * nlpt_pushbytes()
  592  *      Workhorse for actually spinning and writing bytes to printer
  593  *      Derived from lpa.c
  594  *      Originally by ?
  595  *
  596  *      This code is only used when we are polling the port
  597  */
  598 static int
  599 nlpt_pushbytes(struct lpt_data *sc)
  600 {
  601         int spin, err, tic;
  602         char ch;
  603 
  604         nlprintf(("p"));
  605         /* loop for every character .. */
  606         while (sc->sc_xfercnt > 0) {
  607                 /* printer data */
  608                 ch = *(sc->sc_cp);
  609                 sc->sc_cp++;
  610                 sc->sc_xfercnt--;
  611 
  612                 /*
  613                  * Wait for printer ready.
  614                  * Loop 20 usecs testing BUSY bit, then sleep
  615                  * for exponentially increasing timeout. (vak)
  616                  */
  617                 for (spin = 0; NOT_READY(sc) && spin < MAX_SPIN; ++spin)
  618                         DELAY(1);       /* XXX delay is NOT this accurate! */
  619                 if (spin >= MAX_SPIN) {
  620                         tic = 0;
  621                         while (NOT_READY(sc)) {
  622                                 /*
  623                                  * Now sleep, every cycle a
  624                                  * little longer ..
  625                                  */
  626                                 tic = tic + tic + 1;
  627                                 /*
  628                                  * But no more than 10 seconds. (vak)
  629                                  */
  630                                 if (tic > MAX_SLEEP)
  631                                         tic = MAX_SLEEP;
  632                                 err = tsleep((caddr_t)sc, LPPRI,
  633                                         LPT_NAME "poll", tic);
  634                                 if (err != EWOULDBLOCK) {
  635                                         return (err);
  636                                 }
  637                         }
  638                 }
  639 
  640                 /* output data */
  641                 ppb_wdtr(&sc->lpt_dev, ch);
  642                 /* strobe */
  643                 ppb_wctr(&sc->lpt_dev, sc->sc_control|LPC_STB);
  644                 ppb_wctr(&sc->lpt_dev, sc->sc_control);
  645 
  646         }
  647         return(0);
  648 }
  649 
  650 /*
  651  * nlptread --retrieve printer status in IEEE1284 NIBBLE mode
  652  */
  653 
  654 static int
  655 nlptread(dev_t dev, struct uio *uio, int ioflag)
  656 {
  657         struct lpt_data *sc = lptdata[LPTUNIT(minor(dev))];
  658         int error = 0, len;
  659 
  660         if ((error = ppb_1284_negociate(&sc->lpt_dev, PPB_NIBBLE, 0)))
  661                 return (error);
  662 
  663         /* read data in an other buffer, read/write may be simultaneous */
  664         len = 0;
  665         while (uio->uio_resid) {
  666                 if ((error = ppb_1284_read(&sc->lpt_dev, PPB_NIBBLE,
  667                                 sc->sc_statbuf->b_data, min(BUFSTATSIZE,
  668                                 uio->uio_resid), &len))) {
  669                         goto error;
  670                 }
  671 
  672                 if (!len)
  673                         goto error;             /* no more data */
  674 
  675                 if ((error = uiomove(sc->sc_statbuf->b_data, len, uio)))
  676                         goto error;
  677         }
  678 
  679 error:
  680         ppb_1284_terminate(&sc->lpt_dev);
  681         return (error);
  682 }
  683 
  684 /*
  685  * nlptwrite --copy a line from user space to a local buffer, then call
  686  * putc to get the chars moved to the output queue.
  687  *
  688  * Flagging of interrupted write added.
  689  */
  690 
  691 static  int
  692 nlptwrite(dev_t dev, struct uio *uio, int ioflag)
  693 {
  694         register unsigned n;
  695         int pl, err;
  696         u_int   unit = LPTUNIT(minor(dev));
  697         struct lpt_data *sc = lptdata[LPTUNIT(minor(dev))];
  698 
  699         if(sc->sc_flags & LP_BYPASS) {
  700                 /* we can't do writes in bypass mode */
  701                 return(EPERM);
  702         }
  703 
  704         /* request the ppbus only if we don't have it already */
  705         if ((err = lpt_request_ppbus(sc, PPB_WAIT|PPB_INTR)) != 0)
  706                 return (err);
  707 
  708         sc->sc_state &= ~INTERRUPTED;
  709         while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
  710                 sc->sc_cp = sc->sc_inbuf->b_data ;
  711                 uiomove(sc->sc_cp, n, uio);
  712                 sc->sc_xfercnt = n ;
  713 
  714                 if (sc->sc_irq & LP_ENABLE_EXT) {
  715                         /* try any extended mode */
  716                         err = ppb_write(&sc->lpt_dev, sc->sc_cp,
  717                                                         sc->sc_xfercnt, 0);
  718                         switch (err) {
  719                         case 0:
  720                                 /* if not all data was sent, we could rely
  721                                  * on polling for the last bytes */
  722                                 sc->sc_xfercnt = 0;
  723                                 break;
  724                         case EINTR:
  725                                 sc->sc_state |= INTERRUPTED;    
  726                                 return(err);
  727                         case EINVAL:
  728                                 /* advanced mode not avail */
  729                                 log(LOG_NOTICE, LPT_NAME "%d: advanced mode not avail, polling\n", unit);
  730                                 break;
  731                         default:
  732                                 return(err);
  733                         }
  734                 } else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
  735                         nlprintf(("i"));
  736                         /* if the printer is ready for a char, */
  737                         /* give it one */
  738                         if ((sc->sc_state & OBUSY) == 0){
  739                                 nlprintf(("\nC %d. ", sc->sc_xfercnt));
  740                                 pl = spltty();
  741                                 nlpt_intr(sc->lpt_unit);
  742                                 (void) splx(pl);
  743                         }
  744                         nlprintf(("W "));
  745                         if (sc->sc_state & OBUSY)
  746                                 if ((err = tsleep((caddr_t)sc,
  747                                          LPPRI|PCATCH, LPT_NAME "write", 0))) {
  748                                         sc->sc_state |= INTERRUPTED;
  749                                         return(err);
  750                                 }
  751                 }
  752 
  753                 /* check to see if we must do a polled write */
  754                 if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
  755                         nlprintf(("p"));
  756 
  757                         err = nlpt_pushbytes(sc);
  758 
  759                         if (err)
  760                                 return(err);
  761                 }
  762         }
  763 
  764         /* we have not been interrupted, release the ppbus */
  765         lpt_release_ppbus(sc);
  766 
  767         return(0);
  768 }
  769 
  770 /*
  771  * nlpt_intr -- handle printer interrupts which occur when the printer is
  772  * ready to accept another char.
  773  *
  774  * do checking for interrupted write call.
  775  */
  776 
  777 static void
  778 nlpt_intr(int unit)
  779 {
  780         struct lpt_data *sc = lptdata[unit];
  781         int sts;
  782         int i;
  783         
  784         /* we must own the bus to use it */
  785         if ((sc->sc_state & HAVEBUS) == 0)
  786                 return;
  787 
  788         /*
  789          * Is printer online and ready for output?
  790          *
  791          * Avoid falling back to nlptout() too quickly.  First spin-loop
  792          * to see if the printer will become ready ``really soon now''.
  793          */
  794         for (i = 0; i < 100 &&
  795              ((sts=ppb_rstr(&sc->lpt_dev)) & RDY_MASK) != LP_READY; i++) ;
  796 
  797         if ((sts & RDY_MASK) == LP_READY) {
  798                 sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
  799                 sc->sc_backoff = hz/LPTOUTINITIAL;
  800 
  801                 if (sc->sc_xfercnt) {
  802                         /* send char */
  803                         /*nlprintf(("%x ", *sc->sc_cp)); */
  804                         ppb_wdtr(&sc->lpt_dev, *sc->sc_cp++) ;
  805                         ppb_wctr(&sc->lpt_dev, sc->sc_control|LPC_STB);
  806                         /* DELAY(X) */
  807                         ppb_wctr(&sc->lpt_dev, sc->sc_control);
  808 
  809                         /* any more data for printer */
  810                         if(--(sc->sc_xfercnt) > 0) return;
  811                 }
  812 
  813                 /*
  814                  * No more data waiting for printer.
  815                  * Wakeup is not done if write call was interrupted.
  816                  */
  817                 sc->sc_state &= ~OBUSY;
  818 
  819                 if(!(sc->sc_state & INTERRUPTED))
  820                         wakeup((caddr_t)sc);
  821                 nlprintf(("w "));
  822                 return;
  823         } else  {       /* check for error */
  824                 if(((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
  825                                 (sc->sc_state & OPEN))
  826                         sc->sc_state |= EERROR;
  827                 /* nlptout() will jump in and try to restart. */
  828         }
  829         nlprintf(("sts %x ", sts));
  830 }
  831 
  832 static void
  833 nlptintr(int unit)
  834 {
  835         /* call the interrupt at required spl level */
  836         int s = spltty();
  837 
  838         nlpt_intr(unit);
  839 
  840         splx(s);
  841         return;
  842 }
  843 
  844 static  int
  845 nlptioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
  846 {
  847         int     error = 0;
  848         struct  lpt_data *sc;
  849         u_int   unit = LPTUNIT(minor(dev));
  850         u_char  old_sc_irq;     /* old printer IRQ status */
  851 
  852         sc = lptdata[unit];
  853 
  854         switch (cmd) {
  855         case LPT_IRQ :
  856                 if(sc->sc_irq & LP_HAS_IRQ) {
  857                         /*
  858                          * NOTE:
  859                          * If the IRQ status is changed,
  860                          * this will only be visible on the
  861                          * next open.
  862                          *
  863                          * If interrupt status changes,
  864                          * this gets syslog'd.
  865                          */
  866                         old_sc_irq = sc->sc_irq;
  867                         switch(*(int*)data) {
  868                         case 0:
  869                                 sc->sc_irq &= (~LP_ENABLE_IRQ);
  870                                 break;
  871                         case 1:
  872                                 sc->sc_irq &= (~LP_ENABLE_EXT);
  873                                 sc->sc_irq |= LP_ENABLE_IRQ;
  874                                 break;
  875                         case 2:
  876                                 /* classic irq based transfer and advanced
  877                                  * modes are in conflict
  878                                  */
  879                                 sc->sc_irq &= (~LP_ENABLE_IRQ);
  880                                 sc->sc_irq |= LP_ENABLE_EXT;
  881                                 break;
  882                         case 3:
  883                                 sc->sc_irq &= (~LP_ENABLE_EXT);
  884                                 break;
  885                         default:
  886                                 break;
  887                         }
  888                                 
  889                         if (old_sc_irq != sc->sc_irq )
  890                                 log(LOG_NOTICE, LPT_NAME "%d: switched to %s %s mode\n",
  891                                         unit,
  892                                         (sc->sc_irq & LP_ENABLE_IRQ)?
  893                                         "interrupt-driven":"polled",
  894                                         (sc->sc_irq & LP_ENABLE_EXT)?
  895                                         "extended":"standard");
  896                 } else /* polled port */
  897                         error = EOPNOTSUPP;
  898                 break;
  899         default:
  900                 error = ENODEV;
  901         }
  902 
  903         return(error);
  904 }
  905 
  906 static nlpt_devsw_installed = 0;
  907 
  908 static void
  909 nlpt_drvinit(void *unused)
  910 {
  911         dev_t dev;
  912 
  913         if( ! nlpt_devsw_installed ) {
  914                 dev = makedev(CDEV_MAJOR, 0);
  915                 cdevsw_add(&dev,&nlpt_cdevsw, NULL);
  916                 nlpt_devsw_installed = 1;
  917         }
  918 }
  919 
  920 SYSINIT(nlptdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nlpt_drvinit,NULL)

Cache object: 6bb3cc0b952a5a4acd2e5b66c57f8a73


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.