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/ic/lpt.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 /*      $NetBSD: lpt.c,v 1.75 2008/06/10 22:53:08 cegger Exp $  */
    2 
    3 /*
    4  * Copyright (c) 1993, 1994 Charles M. Hannum.
    5  * Copyright (c) 1990 William F. Jolitz, TeleMuse
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. All advertising materials mentioning features or use of this software
   17  *    must display the following acknowledgement:
   18  *      This software is a component of "386BSD" developed by
   19  *      William F. Jolitz, TeleMuse.
   20  * 4. Neither the name of the developer nor the name "386BSD"
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
   25  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
   26  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
   27  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
   28  * NOT MAKE USE OF THIS WORK.
   29  *
   30  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
   31  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
   32  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
   33  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
   34  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
   35  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
   36  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
   37  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
   38  *
   39  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
   40  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   42  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
   43  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   44  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   45  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   48  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   49  * SUCH DAMAGE.
   50  */
   51 
   52 /*
   53  * Device Driver for AT style parallel printer port
   54  */
   55 
   56 #include <sys/cdefs.h>
   57 __KERNEL_RCSID(0, "$NetBSD: lpt.c,v 1.75 2008/06/10 22:53:08 cegger Exp $");
   58 
   59 #include <sys/param.h>
   60 #include <sys/systm.h>
   61 #include <sys/proc.h>
   62 #include <sys/user.h>
   63 #include <sys/malloc.h>
   64 #include <sys/kernel.h>
   65 #include <sys/ioctl.h>
   66 #include <sys/uio.h>
   67 #include <sys/device.h>
   68 #include <sys/conf.h>
   69 #include <sys/syslog.h>
   70 #include <sys/intr.h>
   71 
   72 #include <sys/bus.h>
   73 
   74 #include <dev/ic/lptreg.h>
   75 #include <dev/ic/lptvar.h>
   76 
   77 #define TIMEOUT         hz*16   /* wait up to 16 seconds for a ready */
   78 #define STEP            hz/4
   79 
   80 #define LPTPRI          (PZERO+8)
   81 #define LPT_BSIZE       1024
   82 
   83 #define LPTDEBUG
   84 
   85 #ifndef LPTDEBUG
   86 #define LPRINTF(a)
   87 #else
   88 #define LPRINTF(a)      if (lptdebug) printf a
   89 int lptdebug = 0;
   90 #endif
   91 
   92 extern struct cfdriver lpt_cd;
   93 
   94 dev_type_open(lptopen);
   95 dev_type_close(lptclose);
   96 dev_type_write(lptwrite);
   97 dev_type_ioctl(lptioctl);
   98 
   99 const struct cdevsw lpt_cdevsw = {
  100         lptopen, lptclose, noread, lptwrite, lptioctl,
  101         nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
  102 };
  103 
  104 #define LPTUNIT(s)      (minor(s) & 0x1f)
  105 #define LPTFLAGS(s)     (minor(s) & 0xe0)
  106 
  107 static void     lptsoftintr(void *);
  108 
  109 void
  110 lpt_attach_subr(sc)
  111         struct lpt_softc *sc;
  112 {
  113         bus_space_tag_t iot;
  114         bus_space_handle_t ioh;
  115 
  116         sc->sc_state = 0;
  117 
  118         iot = sc->sc_iot;
  119         ioh = sc->sc_ioh;
  120 
  121         bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
  122 
  123         callout_init(&sc->sc_wakeup_ch, 0);
  124         sc->sc_sih = softint_establish(SOFTINT_SERIAL, lptsoftintr, sc);
  125 
  126         sc->sc_dev_ok = 1;
  127 }
  128 
  129 int
  130 lpt_detach_subr(device_t self, int flags)
  131 {
  132         struct lpt_softc *sc = device_private(self);
  133 
  134         sc->sc_dev_ok = 0;
  135         softint_disestablish(sc->sc_sih);
  136         callout_destroy(&sc->sc_wakeup_ch);
  137         return 0;
  138 }
  139 
  140 /*
  141  * Reset the printer, then wait until it's selected and not busy.
  142  */
  143 int
  144 lptopen(dev_t dev, int flag, int mode, struct lwp *l)
  145 {
  146         u_char flags = LPTFLAGS(dev);
  147         struct lpt_softc *sc;
  148         bus_space_tag_t iot;
  149         bus_space_handle_t ioh;
  150         u_char control;
  151         int error;
  152         int spin;
  153 
  154         sc = device_lookup_private(&lpt_cd, LPTUNIT(dev));
  155         if (!sc || !sc->sc_dev_ok)
  156                 return ENXIO;
  157 
  158 #if 0   /* XXX what to do? */
  159         if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
  160                 return ENXIO;
  161 #endif
  162 
  163 #ifdef DIAGNOSTIC
  164         if (sc->sc_state)
  165                 aprint_verbose_dev(sc->sc_dev, "stat=0x%x not zero\n",
  166                     sc->sc_state);
  167 #endif
  168 
  169         if (sc->sc_state)
  170                 return EBUSY;
  171 
  172         sc->sc_state = LPT_INIT;
  173         sc->sc_flags = flags;
  174         LPRINTF(("%s: open: flags=0x%x\n", device_xname(sc->sc_dev),
  175             (unsigned)flags));
  176         iot = sc->sc_iot;
  177         ioh = sc->sc_ioh;
  178 
  179         if ((flags & LPT_NOPRIME) == 0) {
  180                 /* assert INIT for 100 usec to start up printer */
  181                 bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
  182                 delay(100);
  183         }
  184 
  185         control = LPC_SELECT | LPC_NINIT;
  186         bus_space_write_1(iot, ioh, lpt_control, control);
  187 
  188         /* wait till ready (printer running diagnostics) */
  189         for (spin = 0; NOT_READY_ERR(); spin += STEP) {
  190                 if (spin >= TIMEOUT) {
  191                         sc->sc_state = 0;
  192                         return EBUSY;
  193                 }
  194 
  195                 /* wait 1/4 second, give up if we get a signal */
  196                 error = tsleep((void *)sc, LPTPRI | PCATCH, "lptopen", STEP);
  197                 if (error != EWOULDBLOCK) {
  198                         sc->sc_state = 0;
  199                         return error;
  200                 }
  201         }
  202 
  203         if ((flags & LPT_NOINTR) == 0)
  204                 control |= LPC_IENABLE;
  205         if (flags & LPT_AUTOLF)
  206                 control |= LPC_AUTOLF;
  207         sc->sc_control = control;
  208         bus_space_write_1(iot, ioh, lpt_control, control);
  209 
  210         sc->sc_inbuf = malloc(LPT_BSIZE, M_DEVBUF, M_WAITOK);
  211         sc->sc_count = 0;
  212         sc->sc_state = LPT_OPEN;
  213 
  214         if ((sc->sc_flags & LPT_NOINTR) == 0)
  215                 lptwakeup(sc);
  216 
  217         LPRINTF(("%s: opened\n", device_xname(sc->sc_dev)));
  218         return 0;
  219 }
  220 
  221 int
  222 lptnotready(status, sc)
  223         u_char status;
  224         struct lpt_softc *sc;
  225 {
  226         u_char new;
  227 
  228         status = (status ^ LPS_INVERT) & LPS_MASK;
  229         new = status & ~sc->sc_laststatus;
  230         sc->sc_laststatus = status;
  231 
  232         if (sc->sc_state & LPT_OPEN) {
  233                 if (new & LPS_SELECT)
  234                         log(LOG_NOTICE,
  235                             "%s: offline\n", device_xname(sc->sc_dev));
  236                 else if (new & LPS_NOPAPER)
  237                         log(LOG_NOTICE,
  238                             "%s: out of paper\n", device_xname(sc->sc_dev));
  239                 else if (new & LPS_NERR)
  240                         log(LOG_NOTICE,
  241                             "%s: output error\n", device_xname(sc->sc_dev));
  242         }
  243 
  244         return status;
  245 }
  246 
  247 void
  248 lptwakeup(arg)
  249         void *arg;
  250 {
  251         struct lpt_softc *sc = arg;
  252         int s;
  253 
  254         s = spllpt();
  255         lptintr(sc);
  256         splx(s);
  257 
  258         callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc);
  259 }
  260 
  261 /*
  262  * Close the device, and free the local line buffer.
  263  */
  264 int
  265 lptclose(dev_t dev, int flag, int mode,
  266     struct lwp *l)
  267 {
  268         struct lpt_softc *sc =
  269             device_lookup_private(&lpt_cd, LPTUNIT(dev));
  270         bus_space_tag_t iot = sc->sc_iot;
  271         bus_space_handle_t ioh = sc->sc_ioh;
  272 
  273         if (sc->sc_count)
  274                 (void) lptpushbytes(sc);
  275 
  276         if ((sc->sc_flags & LPT_NOINTR) == 0)
  277                 callout_stop(&sc->sc_wakeup_ch);
  278 
  279         bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
  280         sc->sc_state = 0;
  281         bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
  282         free(sc->sc_inbuf, M_DEVBUF);
  283 
  284         LPRINTF(("%s: closed\n", device_xname(sc->sc_dev)));
  285         return 0;
  286 }
  287 
  288 int
  289 lptpushbytes(sc)
  290         struct lpt_softc *sc;
  291 {
  292         bus_space_tag_t iot = sc->sc_iot;
  293         bus_space_handle_t ioh = sc->sc_ioh;
  294         int error;
  295 
  296         if (sc->sc_flags & LPT_NOINTR) {
  297                 int spin, tic;
  298                 u_char control = sc->sc_control;
  299 
  300                 while (sc->sc_count > 0) {
  301                         spin = 0;
  302                         while (NOT_READY()) {
  303                                 if (++spin < sc->sc_spinmax)
  304                                         continue;
  305                                 tic = 0;
  306                                 /* adapt busy-wait algorithm */
  307                                 sc->sc_spinmax++;
  308                                 while (NOT_READY_ERR()) {
  309                                         /* exponential backoff */
  310                                         tic = tic + tic + 1;
  311                                         if (tic > TIMEOUT)
  312                                                 tic = TIMEOUT;
  313                                         error = tsleep((void *)sc,
  314                                             LPTPRI | PCATCH, "lptpsh", tic);
  315                                         if (error != EWOULDBLOCK)
  316                                                 return error;
  317                                 }
  318                                 break;
  319                         }
  320 
  321                         bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
  322                         DELAY(1);
  323                         bus_space_write_1(iot, ioh, lpt_control,
  324                             control | LPC_STROBE);
  325                         DELAY(1);
  326                         sc->sc_count--;
  327                         bus_space_write_1(iot, ioh, lpt_control, control);
  328                         DELAY(1);
  329 
  330                         /* adapt busy-wait algorithm */
  331                         if (spin*2 + 16 < sc->sc_spinmax)
  332                                 sc->sc_spinmax--;
  333                 }
  334         } else {
  335                 int s;
  336 
  337                 while (sc->sc_count > 0) {
  338                         /* if the printer is ready for a char, give it one */
  339                         if ((sc->sc_state & LPT_OBUSY) == 0) {
  340                                 LPRINTF(("%s: write %lu\n",
  341                                     device_xname(sc->sc_dev),
  342                                     (u_long)sc->sc_count));
  343                                 s = spllpt();
  344                                 (void) lptintr(sc);
  345                                 splx(s);
  346                         }
  347                         error = tsleep((void *)sc, LPTPRI | PCATCH,
  348                             "lptwrite2", 0);
  349                         if (error)
  350                                 return error;
  351                 }
  352         }
  353         return 0;
  354 }
  355 
  356 /*
  357  * Copy a line from user space to a local buffer, then call putc to get the
  358  * chars moved to the output queue.
  359  */
  360 int
  361 lptwrite(dev_t dev, struct uio *uio, int flags)
  362 {
  363         struct lpt_softc *sc =
  364             device_lookup_private(&lpt_cd, LPTUNIT(dev));
  365         size_t n;
  366         int error = 0;
  367 
  368         while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
  369                 uiomove(sc->sc_cp = sc->sc_inbuf, n, uio);
  370                 sc->sc_count = n;
  371                 error = lptpushbytes(sc);
  372                 if (error) {
  373                         /*
  374                          * Return accurate residual if interrupted or timed
  375                          * out.
  376                          */
  377                         uio->uio_resid += sc->sc_count;
  378                         sc->sc_count = 0;
  379                         return error;
  380                 }
  381         }
  382         return 0;
  383 }
  384 
  385 /*
  386  * Handle printer interrupts which occur when the printer is ready to accept
  387  * another char.
  388  */
  389 int
  390 lptintr(arg)
  391         void *arg;
  392 {
  393         struct lpt_softc *sc = arg;
  394         bus_space_tag_t iot = sc->sc_iot;
  395         bus_space_handle_t ioh = sc->sc_ioh;
  396 
  397 #if 0
  398         if ((sc->sc_state & LPT_OPEN) == 0)
  399                 return 0;
  400 #endif
  401 
  402         /* is printer online and ready for output */
  403         if (NOT_READY() && NOT_READY_ERR())
  404                 return 0;
  405 
  406         if (sc->sc_count) {
  407                 u_char control = sc->sc_control;
  408                 /* send char */
  409                 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
  410                 DELAY(1);
  411                 bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
  412                 DELAY(1);
  413                 sc->sc_count--;
  414                 bus_space_write_1(iot, ioh, lpt_control, control);
  415                 DELAY(1);
  416                 sc->sc_state |= LPT_OBUSY;
  417         } else
  418                 sc->sc_state &= ~LPT_OBUSY;
  419 
  420         if (sc->sc_count == 0) {
  421                 /* none, wake up the top half to get more */
  422                 softint_schedule(sc->sc_sih);
  423         }
  424 
  425         return 1;
  426 }
  427 
  428 static void
  429 lptsoftintr(void *cookie)
  430 {
  431 
  432         wakeup(cookie);
  433 }
  434 
  435 int
  436 lptioctl(dev_t dev, u_long cmd, void *data,
  437     int flag, struct lwp *l)
  438 {
  439         return ENODEV;
  440 }

Cache object: 04640daf3f4250667b26bf5a1a9c4725


[ 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.