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.6 2003/06/29 22:30:12 fvdl 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.6 2003/06/29 22:30:12 fvdl 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 
   45 #include <machine/bus.h>
   46 
   47 #include <machine/cpu.h>
   48 #include <machine/pio.h>
   49 #include <machine/joystick.h>
   50 
   51 #include <dev/isa/isavar.h>
   52 #include <dev/isa/isareg.h>
   53 
   54 #include <dev/ic/joyvar.h>
   55 
   56 /*
   57  * The game port can manage 4 buttons and 4 variable resistors (usually 2
   58  * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
   59  * Getting the state of the buttons is done by reading the game port;
   60  * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
   61  * to bits 0-3.  If button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5,
   62  * 6, 7) is set to 0 to get the value of a resistor, write the value 0xff
   63  * at port and wait until the corresponding bit returns to 0.
   64  */
   65 
   66 /*
   67  * The formulae below only work if u is ``not too large''.  See also
   68  * the discussion in microtime.s
   69  */
   70 #define USEC2TICKS(u)   (((u) * 19549) >> 14)
   71 #define TICKS2USEC(u)   (((u) * 3433) >> 12)
   72 
   73 
   74 #define JOYPART(d) (minor(d) & 1)
   75 #define JOYUNIT(d) minor(d) >> 1 & 3
   76 
   77 #ifndef JOY_TIMEOUT
   78 #define JOY_TIMEOUT   2000      /* 2 milliseconds */
   79 #endif
   80 
   81 extern struct cfdriver joy_cd;
   82 
   83 dev_type_open(joyopen);
   84 dev_type_close(joyclose);
   85 dev_type_read(joyread);
   86 dev_type_ioctl(joyioctl);
   87 
   88 const struct cdevsw joy_cdevsw = {
   89         joyopen, joyclose, joyread, nowrite, joyioctl,
   90         nostop, notty, nopoll, nommap, nokqfilter,
   91 };
   92 
   93 void
   94 joyattach(sc)
   95         struct joy_softc *sc;
   96 {
   97 
   98         sc->timeout[0] = sc->timeout[1] = 0;
   99         bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, 0xff);
  100         DELAY(10000);           /* 10 ms delay */
  101         printf("%s: joystick %sconnected\n", sc->sc_dev.dv_xname,
  102             (bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0) & 0x0f) == 0x0f ?
  103             "not " : "");
  104 
  105         sc->sc_timer_freq = joy_timer_freq();
  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, t0, t1, s;
  158         int state = 0, x = 0, y = 0;
  159 
  160         s = splhigh();  /* XXX */
  161         bus_space_write_1(iot, ioh, 0, 0xff);
  162         t0 = joy_get_tick();
  163         t1 = t0;
  164         i = USEC2TICKS(sc->timeout[JOYPART(dev)]);
  165         while (t0 - t1 < i) {
  166                 state = bus_space_read_1(iot, ioh, 0);
  167                 if (JOYPART(dev) == 1)
  168                         state >>= 2;
  169                 t1 = joy_get_tick();
  170                 if (t1 > t0)
  171                         t1 -= sc->sc_timer_freq / hz;
  172                 if (!x && !(state & 0x01))
  173                         x = t1;
  174                 if (!y && !(state & 0x02))
  175                         y = t1;
  176                 if (x && y)
  177                         break;
  178         }
  179         splx(s);        /* XXX */
  180 
  181         c.x = x ? sc->x_off[JOYPART(dev)] + TICKS2USEC(t0 - x) : 0x80000000;
  182         c.y = y ? sc->y_off[JOYPART(dev)] + TICKS2USEC(t0 - y) : 0x80000000;
  183         state >>= 4;
  184         c.b1 = ~state & 1;
  185         c.b2 = ~(state >> 1) & 1;
  186         return (uiomove(&c, sizeof(struct joystick), uio));
  187 }
  188 
  189 int
  190 joyioctl(dev, cmd, data, flag, p)
  191         dev_t dev;
  192         u_long cmd;
  193         caddr_t data;
  194         int flag;
  195         struct proc *p;
  196 {
  197         int unit = JOYUNIT(dev);
  198         struct joy_softc *sc = joy_cd.cd_devs[unit];
  199         int i = JOYPART(dev);
  200         int x;
  201 
  202         switch (cmd) {
  203         case JOY_SETTIMEOUT:
  204                 x = *(int *) data;
  205                 if (x < 1 || x > 10000) /* 10ms maximum! */
  206                         return (EINVAL);
  207                 sc->timeout[i] = x;
  208                 break;
  209         case JOY_GETTIMEOUT:
  210                 *(int *) data = sc->timeout[i];
  211                 break;
  212         case JOY_SET_X_OFFSET:
  213                 sc->x_off[i] = *(int *) data;
  214                 break;
  215         case JOY_SET_Y_OFFSET:
  216                 sc->y_off[i] = *(int *) data;
  217                 break;
  218         case JOY_GET_X_OFFSET:
  219                 *(int *) data = sc->x_off[i];
  220                 break;
  221         case JOY_GET_Y_OFFSET:
  222                 *(int *) data = sc->y_off[i];
  223                 break;
  224         default:
  225                 return (ENXIO);
  226         }
  227         return 0;
  228 }

Cache object: b86e9a7ea89f905913fc6805bedabcae


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