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/ppi.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) 1997, 1998, 1999 Nicolas Souchu, Michael Smith
    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  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  *
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD: releng/6.4/sys/dev/ppbus/ppi.c 160503 2006-07-19 16:31:12Z kib $");
   31 #include "opt_ppb_1284.h"
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/module.h>
   36 #include <sys/bus.h>
   37 #include <sys/conf.h>
   38 #include <sys/kernel.h>
   39 #include <sys/uio.h>
   40 #include <sys/fcntl.h>
   41 
   42 #include <machine/bus.h>
   43 #include <machine/resource.h>
   44 #include <sys/rman.h>
   45 
   46 #include <dev/ppbus/ppbconf.h>
   47 #include <dev/ppbus/ppb_msq.h>
   48 
   49 #ifdef PERIPH_1284
   50 #include <dev/ppbus/ppb_1284.h>
   51 #endif
   52 
   53 #include <dev/ppbus/ppi.h>
   54 
   55 #include "ppbus_if.h"
   56 
   57 #include <dev/ppbus/ppbio.h>
   58 
   59 #define BUFSIZE         512
   60 
   61 struct ppi_data {
   62 
   63     int         ppi_unit;
   64     int         ppi_flags;
   65 #define HAVE_PPBUS      (1<<0)
   66 #define HAD_PPBUS       (1<<1)
   67 
   68     int         ppi_count;
   69     int         ppi_mode;                       /* IEEE1284 mode */
   70     char        ppi_buffer[BUFSIZE];
   71 
   72 #ifdef PERIPH_1284
   73     struct resource *intr_resource;     /* interrupt resource */
   74     void *intr_cookie;                  /* interrupt registration cookie */
   75 #endif /* PERIPH_1284 */
   76 };
   77 
   78 #define DEVTOSOFTC(dev) \
   79         ((struct ppi_data *)device_get_softc(dev))
   80 #define UNITOSOFTC(unit) \
   81         ((struct ppi_data *)devclass_get_softc(ppi_devclass, (unit)))
   82 #define UNITODEVICE(unit) \
   83         (devclass_get_device(ppi_devclass, (unit)))
   84 
   85 static devclass_t ppi_devclass;
   86 
   87 static  d_open_t        ppiopen;
   88 static  d_close_t       ppiclose;
   89 static  d_ioctl_t       ppiioctl;
   90 static  d_write_t       ppiwrite;
   91 static  d_read_t        ppiread;
   92 
   93 static struct cdevsw ppi_cdevsw = {
   94         .d_version =    D_VERSION,
   95         .d_flags =      D_NEEDGIANT,
   96         .d_open =       ppiopen,
   97         .d_close =      ppiclose,
   98         .d_read =       ppiread,
   99         .d_write =      ppiwrite,
  100         .d_ioctl =      ppiioctl,
  101         .d_name =       "ppi",
  102 };
  103 
  104 #ifdef PERIPH_1284
  105 
  106 static void
  107 ppi_enable_intr(device_t ppidev)
  108 {
  109         char r;
  110         device_t ppbus = device_get_parent(ppidev);
  111 
  112         r = ppb_rctr(ppbus);
  113         ppb_wctr(ppbus, r | IRQENABLE);
  114 
  115         return;
  116 }
  117 
  118 static void
  119 ppi_disable_intr(device_t ppidev)
  120 {
  121         char r;
  122         device_t ppbus = device_get_parent(ppidev);
  123 
  124         r = ppb_rctr(ppbus);
  125         ppb_wctr(ppbus, r & ~IRQENABLE);
  126 
  127         return;
  128 }
  129 
  130 #endif /* PERIPH_1284 */
  131 
  132 static void
  133 ppi_identify(driver_t *driver, device_t parent)
  134 {
  135 
  136         device_t dev;
  137 
  138         dev = device_find_child(parent, "ppi", 0);
  139         if (!dev)
  140                 BUS_ADD_CHILD(parent, 0, "ppi", -1);
  141 }
  142 
  143 /*
  144  * ppi_probe()
  145  */
  146 static int
  147 ppi_probe(device_t dev)
  148 {
  149         struct ppi_data *ppi;
  150 
  151         /* probe is always ok */
  152         device_set_desc(dev, "Parallel I/O");
  153 
  154         ppi = DEVTOSOFTC(dev);
  155 
  156         return (0);
  157 }
  158 
  159 /*
  160  * ppi_attach()
  161  */
  162 static int
  163 ppi_attach(device_t dev)
  164 {
  165 #ifdef PERIPH_1284
  166         uintptr_t irq;
  167         int zero = 0;
  168         struct ppi_data *ppi = DEVTOSOFTC(dev);
  169 
  170         /* retrive the irq */
  171         BUS_READ_IVAR(device_get_parent(dev), dev, PPBUS_IVAR_IRQ, &irq);
  172 
  173         /* declare our interrupt handler */
  174         ppi->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
  175                                                 &zero, irq, irq, 1, RF_ACTIVE);
  176 #endif /* PERIPH_1284 */
  177 
  178         make_dev(&ppi_cdevsw, device_get_unit(dev),     /* XXX cleanup */
  179                  UID_ROOT, GID_WHEEL,
  180                  0600, "ppi%d", device_get_unit(dev));
  181 
  182         return (0);
  183 }
  184 
  185 #ifdef PERIPH_1284
  186 /*
  187  * Cable
  188  * -----
  189  *
  190  * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
  191  *
  192  * nStrobe   <-> nAck           1  <-> 10
  193  * nAutofd   <-> Busy           11 <-> 14
  194  * nSelectin <-> Select         17 <-> 13
  195  * nInit     <-> nFault         15 <-> 16
  196  *
  197  */
  198 static void
  199 ppiintr(void *arg)
  200 {
  201         device_t ppidev = (device_t)arg;
  202         device_t ppbus = device_get_parent(ppidev);
  203         struct ppi_data *ppi = DEVTOSOFTC(ppidev);
  204 
  205         ppi_disable_intr(ppidev);
  206 
  207         switch (ppb_1284_get_state(ppbus)) {
  208 
  209         /* accept IEEE1284 negotiation then wakeup a waiting process to
  210          * continue negotiation at process level */
  211         case PPB_FORWARD_IDLE:
  212                 /* Event 1 */
  213                 if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
  214                                                         (SELECT | nBUSY)) {
  215                         /* IEEE1284 negotiation */
  216 #ifdef DEBUG_1284
  217                         printf("N");
  218 #endif
  219 
  220                         /* Event 2 - prepare for reading the ext. value */
  221                         ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
  222 
  223                         ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
  224 
  225                 } else {
  226 #ifdef DEBUG_1284
  227                         printf("0x%x", ppb_rstr(ppbus));
  228 #endif
  229                         ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
  230                         break;
  231                 }
  232 
  233                 /* wake up any process waiting for negotiation from
  234                  * remote master host */
  235 
  236                 /* XXX should set a variable to warn the process about
  237                  * the interrupt */
  238 
  239                 wakeup(ppi);
  240                 break;
  241         default:
  242 #ifdef DEBUG_1284
  243                 printf("?%d", ppb_1284_get_state(ppbus));
  244 #endif
  245                 ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
  246                 ppb_set_mode(ppbus, PPB_COMPATIBLE);
  247                 break;
  248         }
  249 
  250         ppi_enable_intr(ppidev);
  251 
  252         return;
  253 }
  254 #endif /* PERIPH_1284 */
  255 
  256 static int
  257 ppiopen(struct cdev *dev, int flags, int fmt, struct thread *td)
  258 {
  259         u_int unit = minor(dev);
  260         struct ppi_data *ppi = UNITOSOFTC(unit);
  261         device_t ppidev = UNITODEVICE(unit);
  262         device_t ppbus = device_get_parent(ppidev);
  263         int res;
  264 
  265         if (!ppi)
  266                 return (ENXIO);
  267 
  268         if (!(ppi->ppi_flags & HAVE_PPBUS)) {
  269                 if ((res = ppb_request_bus(ppbus, ppidev,
  270                         (flags & O_NONBLOCK) ? PPB_DONTWAIT :
  271                                                 (PPB_WAIT | PPB_INTR))))
  272                         return (res);
  273 
  274                 ppi->ppi_flags |= HAVE_PPBUS;
  275 
  276 #ifdef PERIPH_1284
  277                 if (ppi->intr_resource) {
  278                         /* register our interrupt handler */
  279                         BUS_SETUP_INTR(device_get_parent(ppidev), ppidev, ppi->intr_resource,
  280                                        INTR_TYPE_TTY, ppiintr, dev, &ppi->intr_cookie);
  281                 }
  282 #endif /* PERIPH_1284 */
  283         }
  284         ppi->ppi_count += 1;
  285 
  286         return (0);
  287 }
  288 
  289 static int
  290 ppiclose(struct cdev *dev, int flags, int fmt, struct thread *td)
  291 {
  292         u_int unit = minor(dev);
  293         struct ppi_data *ppi = UNITOSOFTC(unit);
  294         device_t ppidev = UNITODEVICE(unit);
  295         device_t ppbus = device_get_parent(ppidev);
  296 
  297         ppi->ppi_count --;
  298         if (!ppi->ppi_count) {
  299 
  300 #ifdef PERIPH_1284
  301                 switch (ppb_1284_get_state(ppbus)) {
  302                 case PPB_PERIPHERAL_IDLE:
  303                         ppb_peripheral_terminate(ppbus, 0);
  304                         break;
  305                 case PPB_REVERSE_IDLE:
  306                 case PPB_EPP_IDLE:
  307                 case PPB_ECP_FORWARD_IDLE:
  308                 default:
  309                         ppb_1284_terminate(ppbus);
  310                         break;
  311                 }
  312 #endif /* PERIPH_1284 */
  313 
  314                 /* unregistration of interrupt forced by release */
  315                 ppb_release_bus(ppbus, ppidev);
  316 
  317                 ppi->ppi_flags &= ~HAVE_PPBUS;
  318         }
  319 
  320         return (0);
  321 }
  322 
  323 /*
  324  * ppiread()
  325  *
  326  * IEEE1284 compliant read.
  327  *
  328  * First, try negotiation to BYTE then NIBBLE mode
  329  * If no data is available, wait for it otherwise transfer as much as possible
  330  */
  331 static int
  332 ppiread(struct cdev *dev, struct uio *uio, int ioflag)
  333 {
  334 #ifdef PERIPH_1284
  335         u_int unit = minor(dev);
  336         struct ppi_data *ppi = UNITOSOFTC(unit);
  337         device_t ppidev = UNITODEVICE(unit);
  338         device_t ppbus = device_get_parent(ppidev);
  339         int len, error = 0;
  340 
  341         switch (ppb_1284_get_state(ppbus)) {
  342         case PPB_PERIPHERAL_IDLE:
  343                 ppb_peripheral_terminate(ppbus, 0);
  344                 /* FALLTHROUGH */
  345 
  346         case PPB_FORWARD_IDLE:
  347                 /* if can't negotiate NIBBLE mode then try BYTE mode,
  348                  * the peripheral may be a computer
  349                  */
  350                 if ((ppb_1284_negociate(ppbus,
  351                         ppi->ppi_mode = PPB_NIBBLE, 0))) {
  352 
  353                         /* XXX Wait 2 seconds to let the remote host some
  354                          * time to terminate its interrupt
  355                          */
  356                         tsleep(ppi, PPBPRI, "ppiread", 2*hz);
  357                         
  358                         if ((error = ppb_1284_negociate(ppbus,
  359                                 ppi->ppi_mode = PPB_BYTE, 0)))
  360                                 return (error);
  361                 }
  362                 break;
  363 
  364         case PPB_REVERSE_IDLE:
  365         case PPB_EPP_IDLE:
  366         case PPB_ECP_FORWARD_IDLE:
  367         default:
  368                 break;
  369         }
  370 
  371 #ifdef DEBUG_1284
  372         printf("N");
  373 #endif
  374         /* read data */
  375         len = 0;
  376         while (uio->uio_resid) {
  377                 if ((error = ppb_1284_read(ppbus, ppi->ppi_mode,
  378                         ppi->ppi_buffer, min(BUFSIZE, uio->uio_resid),
  379                         &len))) {
  380                         goto error;
  381                 }
  382 
  383                 if (!len)
  384                         goto error;             /* no more data */
  385 
  386 #ifdef DEBUG_1284
  387                 printf("d");
  388 #endif
  389                 if ((error = uiomove(ppi->ppi_buffer, len, uio)))
  390                         goto error;
  391         }
  392 
  393 error:
  394 
  395 #else /* PERIPH_1284 */
  396         int error = ENODEV;
  397 #endif
  398 
  399         return (error);
  400 }
  401 
  402 /*
  403  * ppiwrite()
  404  *
  405  * IEEE1284 compliant write
  406  *
  407  * Actually, this is the peripheral side of a remote IEEE1284 read
  408  *
  409  * The first part of the negotiation (IEEE1284 device detection) is
  410  * done at interrupt level, then the remaining is done by the writing
  411  * process
  412  *
  413  * Once negotiation done, transfer data
  414  */
  415 static int
  416 ppiwrite(struct cdev *dev, struct uio *uio, int ioflag)
  417 {
  418 #ifdef PERIPH_1284
  419         u_int unit = minor(dev);
  420         struct ppi_data *ppi = UNITOSOFTC(unit);
  421         device_t ppidev = UNITODEVICE(unit);
  422         device_t ppbus = device_get_parent(ppidev);
  423         int len, error = 0, sent;
  424 
  425 #if 0
  426         int ret;
  427 
  428         #define ADDRESS         MS_PARAM(0, 0, MS_TYP_PTR)
  429         #define LENGTH          MS_PARAM(0, 1, MS_TYP_INT)
  430 
  431         struct ppb_microseq msq[] = {
  432                   { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
  433                   MS_RET(0)
  434         };
  435 
  436         /* negotiate ECP mode */
  437         if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
  438                 printf("ppiwrite: ECP negotiation failed\n");
  439         }
  440 
  441         while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
  442                 uiomove(ppi->ppi_buffer, len, uio);
  443 
  444                 ppb_MS_init_msq(msq, 2, ADDRESS, ppi->ppi_buffer, LENGTH, len);
  445 
  446                 error = ppb_MS_microseq(ppbus, msq, &ret);
  447         }
  448 #endif
  449 
  450         /* we have to be peripheral to be able to send data, so
  451          * wait for the appropriate state
  452          */
  453         if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
  454                 ppb_1284_terminate(ppbus);
  455 
  456         while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
  457                 /* XXX should check a variable before sleeping */
  458 #ifdef DEBUG_1284
  459                 printf("s");
  460 #endif
  461 
  462                 ppi_enable_intr(ppidev);
  463 
  464                 /* sleep until IEEE1284 negotiation starts */
  465                 error = tsleep(ppi, PCATCH | PPBPRI, "ppiwrite", 0);
  466 
  467                 switch (error) {
  468                 case 0:
  469                         /* negotiate peripheral side with BYTE mode */
  470                         ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
  471                         break;
  472                 case EWOULDBLOCK:
  473                         break;
  474                 default:
  475                         goto error;
  476                 }
  477         }
  478 #ifdef DEBUG_1284
  479         printf("N");
  480 #endif
  481 
  482         /* negotiation done, write bytes to master host */
  483         while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
  484                 uiomove(ppi->ppi_buffer, len, uio);
  485                 if ((error = byte_peripheral_write(ppbus,
  486                                                 ppi->ppi_buffer, len, &sent)))
  487                         goto error;
  488 #ifdef DEBUG_1284
  489                 printf("d");
  490 #endif
  491         }
  492 
  493 error:
  494 
  495 #else /* PERIPH_1284 */
  496         int error = ENODEV;
  497 #endif
  498 
  499         return (error);
  500 }
  501 
  502 static int
  503 ppiioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
  504 {
  505         u_int unit = minor(dev);
  506         device_t ppidev = UNITODEVICE(unit);
  507         device_t ppbus = device_get_parent(ppidev);
  508         int error = 0;
  509         u_int8_t *val = (u_int8_t *)data;
  510 
  511         switch (cmd) {
  512 
  513         case PPIGDATA:                  /* get data register */
  514                 *val = ppb_rdtr(ppbus);
  515                 break;
  516         case PPIGSTATUS:                /* get status bits */
  517                 *val = ppb_rstr(ppbus);
  518                 break;
  519         case PPIGCTRL:                  /* get control bits */
  520                 *val = ppb_rctr(ppbus);
  521                 break;
  522         case PPIGEPPD:                  /* get EPP data bits */
  523                 *val = ppb_repp_D(ppbus);
  524                 break;
  525         case PPIGECR:                   /* get ECP bits */
  526                 *val = ppb_recr(ppbus);
  527                 break;
  528         case PPIGFIFO:                  /* read FIFO */
  529                 *val = ppb_rfifo(ppbus);
  530                 break;
  531         case PPISDATA:                  /* set data register */
  532                 ppb_wdtr(ppbus, *val);
  533                 break;
  534         case PPISSTATUS:                /* set status bits */
  535                 ppb_wstr(ppbus, *val);
  536                 break;
  537         case PPISCTRL:                  /* set control bits */
  538                 ppb_wctr(ppbus, *val);
  539                 break;
  540         case PPISEPPD:                  /* set EPP data bits */
  541                 ppb_wepp_D(ppbus, *val);
  542                 break;
  543         case PPISECR:                   /* set ECP bits */
  544                 ppb_wecr(ppbus, *val);
  545                 break;
  546         case PPISFIFO:                  /* write FIFO */
  547                 ppb_wfifo(ppbus, *val);
  548                 break;
  549         case PPIGEPPA:                  /* get EPP address bits */
  550                 *val = ppb_repp_A(ppbus);
  551                 break;
  552         case PPISEPPA:                  /* set EPP address bits */
  553                 ppb_wepp_A(ppbus, *val);
  554                 break;
  555         default:
  556                 error = ENOTTY;
  557                 break;
  558         }
  559     
  560         return (error);
  561 }
  562 
  563 static device_method_t ppi_methods[] = {
  564         /* device interface */
  565         DEVMETHOD(device_identify,      ppi_identify),
  566         DEVMETHOD(device_probe,         ppi_probe),
  567         DEVMETHOD(device_attach,        ppi_attach),
  568 
  569         { 0, 0 }
  570 };
  571 
  572 static driver_t ppi_driver = {
  573         "ppi",
  574         ppi_methods,
  575         sizeof(struct ppi_data),
  576 };
  577 DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);
  578 MODULE_DEPEND(ppi, ppbus, 1, 1, 1);

Cache object: ce658e2cc017a9ddfe5ceef644e5a71d


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