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/joy/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 /*-
    2  * Copyright (c) 1995 Jean-Marc Zucconi
    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  *    in this position and unchanged.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  *
   28  * $FreeBSD: releng/5.1/sys/dev/joy/joy.c 111815 2003-03-03 12:15:54Z phk $
   29  */
   30 
   31 #include <sys/param.h>
   32 #include <sys/systm.h>
   33 #include <sys/conf.h>
   34 #include <sys/uio.h>
   35 #include <sys/kernel.h>
   36 #include <sys/module.h>
   37 #include <sys/bus.h>
   38 #include <machine/bus.h>
   39 #include <machine/resource.h>
   40 #include <sys/rman.h>
   41 #include <sys/time.h>
   42 #include <sys/joystick.h>
   43 #include <dev/joy/joyvar.h>
   44 
   45 /* The game port can manage 4 buttons and 4 variable resistors (usually 2
   46  * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
   47  * Getting the state of the buttons is done by reading the game port:
   48  * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
   49  * to bits 0-3.
   50  * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
   51  * to get the value of a resistor, write the value 0xff at port and
   52  * wait until the corresponding bit returns to 0.
   53  */
   54 
   55 #define joypart(d) (minor(d)&1)
   56 #define UNIT(d) ((minor(d)>>1)&3)
   57 #ifndef JOY_TIMEOUT
   58 #define JOY_TIMEOUT   2000 /* 2 milliseconds */
   59 #endif
   60 
   61 #define JOY_SOFTC(unit) (struct joy_softc *) \
   62         devclass_get_softc(joy_devclass,(unit))
   63 
   64 #define CDEV_MAJOR 51
   65 static  d_open_t        joyopen;
   66 static  d_close_t       joyclose;
   67 static  d_read_t        joyread;
   68 static  d_ioctl_t       joyioctl;
   69 
   70 static struct cdevsw joy_cdevsw = {
   71         .d_open =       joyopen,
   72         .d_close =      joyclose,
   73         .d_read =       joyread,
   74         .d_ioctl =      joyioctl,
   75         .d_name =       "joy",
   76         .d_maj =        CDEV_MAJOR,
   77 };
   78 
   79 devclass_t joy_devclass;
   80 
   81 int
   82 joy_probe(device_t dev)
   83 {
   84 #ifdef WANT_JOYSTICK_CONNECTED
   85 #ifdef notyet
   86         outb(dev->id_iobase, 0xff);
   87         DELAY(10000); /*  10 ms delay */
   88         return (inb(dev->id_iobase) & 0x0f) != 0x0f;
   89 #else
   90         return (0);
   91 #endif
   92 #else
   93         return (0);
   94 #endif
   95 }
   96 
   97 int
   98 joy_attach(device_t dev)
   99 {
  100         int     unit = device_get_unit(dev);
  101         struct joy_softc *joy = device_get_softc(dev);
  102 
  103         joy->rid = 0;
  104         joy->res = bus_alloc_resource(dev, SYS_RES_IOPORT, &joy->rid, 0, ~0, 1,
  105             RF_ACTIVE);
  106         if (joy->res == NULL)
  107                 return ENXIO;
  108         joy->bt = rman_get_bustag(joy->res);
  109         joy->port = rman_get_bushandle(joy->res);
  110         joy->timeout[0] = joy->timeout[1] = 0;
  111         joy->d = make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit);
  112         return (0);
  113 }
  114 
  115 int
  116 joy_detach(device_t dev)
  117 {
  118         struct joy_softc *joy = device_get_softc(dev);
  119 
  120         if (joy->res != NULL)
  121                 bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
  122         if (joy->d)
  123                 destroy_dev(joy->d);
  124         return (0);
  125 }
  126 
  127 
  128 static int
  129 joyopen(dev_t dev, int flags, int fmt, struct thread *td)
  130 {
  131         int i = joypart (dev);
  132         struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
  133 
  134         if (joy->timeout[i])
  135                 return (EBUSY);
  136         joy->x_off[i] = joy->y_off[i] = 0;
  137         joy->timeout[i] = JOY_TIMEOUT;
  138         return (0);
  139 }
  140 
  141 static int
  142 joyclose(dev_t dev, int flags, int fmt, struct thread *td)
  143 {
  144         int i = joypart (dev);
  145         struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
  146 
  147         joy->timeout[i] = 0;
  148         return (0);
  149 }
  150 
  151 static int
  152 joyread(dev_t dev, struct uio *uio, int flag)
  153 {
  154         struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
  155         bus_space_handle_t port = joy->port;
  156         bus_space_tag_t bt = joy->bt;
  157         struct timespec t, start, end;
  158         int state = 0;
  159         struct timespec x, y;
  160         struct joystick c;
  161 #ifndef __i386__
  162         int s;
  163 
  164         s = splhigh();
  165 #else
  166         disable_intr ();
  167 #endif
  168         bus_space_write_1 (bt, port, 0, 0xff);
  169         nanotime(&start);
  170         end.tv_sec = 0;
  171         end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
  172         timespecadd(&end, &start);
  173         t = start;
  174         timespecclear(&x);
  175         timespecclear(&y);
  176         while (timespeccmp(&t, &end, <)) {
  177                 state = bus_space_read_1 (bt, port, 0);
  178                 if (joypart(dev) == 1)
  179                         state >>= 2;
  180                 nanotime(&t);
  181                 if (!timespecisset(&x) && !(state & 0x01))
  182                         x = t;
  183                 if (!timespecisset(&y) && !(state & 0x02))
  184                         y = t;
  185                 if (timespecisset(&x) && timespecisset(&y))
  186                         break;
  187         }
  188 #ifndef __i386__
  189         splx(s);
  190 #else
  191         enable_intr ();
  192 #endif
  193         if (timespecisset(&x)) {
  194                 timespecsub(&x, &start);
  195                 c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
  196         } else
  197                 c.x = 0x80000000;
  198         if (timespecisset(&y)) {
  199                 timespecsub(&y, &start);
  200                 c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
  201         } else
  202                 c.y = 0x80000000;
  203         state >>= 4;
  204         c.b1 = ~state & 1;
  205         c.b2 = ~(state >> 1) & 1;
  206         return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
  207 }
  208 
  209 static int
  210 joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
  211 {
  212         struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
  213         int i = joypart (dev);
  214         int x;
  215 
  216         switch (cmd) {
  217         case JOY_SETTIMEOUT:
  218                 x = *(int *) data;
  219                 if (x < 1 || x > 10000) /* 10ms maximum! */
  220                         return EINVAL;
  221                 joy->timeout[i] = x;
  222                 break;
  223         case JOY_GETTIMEOUT:
  224                 *(int *) data = joy->timeout[i];
  225                 break;
  226         case JOY_SET_X_OFFSET:
  227                 joy->x_off[i] = *(int *) data;
  228                 break;
  229         case JOY_SET_Y_OFFSET:
  230                 joy->y_off[i] = *(int *) data;
  231                 break;
  232         case JOY_GET_X_OFFSET:
  233                 *(int *) data = joy->x_off[i];
  234                 break;
  235         case JOY_GET_Y_OFFSET:
  236                 *(int *) data = joy->y_off[i];
  237                 break;
  238         default:
  239                 return (ENOTTY);
  240         }
  241         return (0);
  242 }

Cache object: 5c943b47dd05d6c50414b6aa7536b485


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