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/joy.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: joy.c,v 1.8 2004/08/03 18:35:21 drochner Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 1995 Jean-Marc Zucconi
    5  * All rights reserved.
    6  *
    7  * Ported to NetBSD by Matthieu Herrb <matthieu@laas.fr>
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer
   14  *    in this position and unchanged.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. The name of the author may not be used to endorse or promote products
   19  *    derived from this software without specific prior written permission
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   31  *
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __KERNEL_RCSID(0, "$NetBSD: joy.c,v 1.8 2004/08/03 18:35:21 drochner Exp $");
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/kernel.h>
   40 #include <sys/device.h>
   41 #include <sys/errno.h>
   42 #include <sys/conf.h>
   43 #include <sys/event.h>
   44 #include <sys/vnode.h>
   45 #include <machine/bus.h>
   46 
   47 #include <sys/joystick.h>
   48 #include <dev/ic/joyvar.h>
   49 
   50 /*
   51  * The game port can manage 4 buttons and 4 variable resistors (usually 2
   52  * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
   53  * Getting the state of the buttons is done by reading the game port;
   54  * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
   55  * to bits 0-3.  If button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5,
   56  * 6, 7) is set to 0 to get the value of a resistor, write the value 0xff
   57  * at port and wait until the corresponding bit returns to 0.
   58  */
   59 
   60 
   61 #define JOYPART(d) (minor(d) & 1)
   62 #define JOYUNIT(d) (minor(d) >> 1)
   63 
   64 #ifndef JOY_TIMEOUT
   65 #define JOY_TIMEOUT   2000      /* 2 milliseconds */
   66 #endif
   67 
   68 extern struct cfdriver joy_cd;
   69 
   70 dev_type_open(joyopen);
   71 dev_type_close(joyclose);
   72 dev_type_read(joyread);
   73 dev_type_ioctl(joyioctl);
   74 
   75 const struct cdevsw joy_cdevsw = {
   76         joyopen, joyclose, joyread, nowrite, joyioctl,
   77         nostop, notty, nopoll, nommap, nokqfilter,
   78 };
   79 
   80 void
   81 joyattach(sc)
   82         struct joy_softc *sc;
   83 {
   84 
   85         sc->timeout[0] = sc->timeout[1] = 0;
   86         bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, 0xff);
   87         DELAY(10000);           /* 10 ms delay */
   88         printf("%s: joystick %sconnected\n", sc->sc_dev.dv_xname,
   89             (bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0) & 0x0f) == 0x0f ?
   90             "not " : "");
   91 }
   92 
   93 int
   94 joydetach(sc, flags)
   95         struct joy_softc *sc;
   96         int flags;
   97 {
   98         int maj, mn;
   99 
  100         maj = cdevsw_lookup_major(&joy_cdevsw);
  101         mn = sc->sc_dev.dv_unit << 1;
  102         vdevgone(maj, mn, mn, VCHR);
  103         vdevgone(maj, mn + 1, mn + 1, VCHR);
  104 
  105         return (0);
  106 }
  107 
  108 int
  109 joyopen(dev, flag, mode, p)
  110         dev_t dev;
  111         int flag, mode;
  112         struct proc *p;
  113 {
  114         int unit = JOYUNIT(dev);
  115         int i = JOYPART(dev);
  116         struct joy_softc *sc;
  117 
  118         if (unit >= joy_cd.cd_ndevs)
  119                 return (ENXIO);
  120         sc = joy_cd.cd_devs[unit];
  121         if (sc == 0)
  122                 return (ENXIO);
  123 
  124         if (sc->timeout[i])
  125                 return (EBUSY);
  126 
  127         sc->x_off[i] = sc->y_off[i] = 0;
  128         sc->timeout[i] = JOY_TIMEOUT;
  129         return (0);
  130 }
  131 
  132 int
  133 joyclose(dev, flag, mode, p)
  134         dev_t dev;
  135         int flag, mode;
  136         struct proc *p;
  137 {
  138         int unit = JOYUNIT(dev);
  139         int i = JOYPART(dev);
  140         struct joy_softc *sc = joy_cd.cd_devs[unit];
  141 
  142         sc->timeout[i] = 0;
  143         return (0);
  144 }
  145 
  146 int
  147 joyread(dev, uio, flag)
  148         dev_t dev;
  149         struct uio *uio;
  150         int flag;
  151 {
  152         int unit = JOYUNIT(dev);
  153         struct joy_softc *sc = joy_cd.cd_devs[unit];
  154         bus_space_tag_t iot = sc->sc_iot;
  155         bus_space_handle_t ioh = sc->sc_ioh;
  156         struct joystick c;
  157         int i, s;
  158         struct timeval start, now, diff;
  159         int state = 0, x = 0, y = 0;
  160 
  161         s = splhigh();  /* XXX */
  162         bus_space_write_1(iot, ioh, 0, 0xff);
  163         microtime(&start);
  164         now = start; /* structure assignment */
  165         i = sc->timeout[JOYPART(dev)];
  166         for (;;) {
  167                 timersub(&now, &start, &diff);
  168                 if (diff.tv_sec > 0 || diff.tv_usec > i)
  169                         break;
  170                 state = bus_space_read_1(iot, ioh, 0);
  171                 if (JOYPART(dev) == 1)
  172                         state >>= 2;
  173                 if (!x && !(state & 0x01))
  174                         x = diff.tv_usec;
  175                 if (!y && !(state & 0x02))
  176                         y = diff.tv_usec;
  177                 if (x && y)
  178                         break;
  179                 microtime(&now);
  180         }
  181         splx(s);        /* XXX */
  182 
  183         c.x = x ? sc->x_off[JOYPART(dev)] + x : 0x80000000;
  184         c.y = y ? sc->y_off[JOYPART(dev)] + y : 0x80000000;
  185         state >>= 4;
  186         c.b1 = ~state & 1;
  187         c.b2 = ~(state >> 1) & 1;
  188         return (uiomove(&c, sizeof(struct joystick), uio));
  189 }
  190 
  191 int
  192 joyioctl(dev, cmd, data, flag, p)
  193         dev_t dev;
  194         u_long cmd;
  195         caddr_t data;
  196         int flag;
  197         struct proc *p;
  198 {
  199         int unit = JOYUNIT(dev);
  200         struct joy_softc *sc = joy_cd.cd_devs[unit];
  201         int i = JOYPART(dev);
  202         int x;
  203 
  204         switch (cmd) {
  205         case JOY_SETTIMEOUT:
  206                 x = *(int *) data;
  207                 if (x < 1 || x > 10000) /* 10ms maximum! */
  208                         return (EINVAL);
  209                 sc->timeout[i] = x;
  210                 break;
  211         case JOY_GETTIMEOUT:
  212                 *(int *) data = sc->timeout[i];
  213                 break;
  214         case JOY_SET_X_OFFSET:
  215                 sc->x_off[i] = *(int *) data;
  216                 break;
  217         case JOY_SET_Y_OFFSET:
  218                 sc->y_off[i] = *(int *) data;
  219                 break;
  220         case JOY_GET_X_OFFSET:
  221                 *(int *) data = sc->x_off[i];
  222                 break;
  223         case JOY_GET_Y_OFFSET:
  224                 *(int *) data = sc->y_off[i];
  225                 break;
  226         default:
  227                 return (ENXIO);
  228         }
  229         return 0;
  230 }

Cache object: e93102c11c98564d1b5f461d34fd8d68


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