home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Drivers / joystick-0.0-I / joystick.next / joystick.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-09  |  14.2 KB  |  537 lines

  1. /*
  2. linux/drivers/char/joystick.c
  3.    Copyright (C) 1992, 1993 Arthur C. Smith
  4.    Joystick driver for Linux running on an IBM compatible computer.
  5.  
  6. VERSION INFO:
  7. 01/08/93    ACS    0.1: Works but needs multi-joystick support
  8. 01/13/93    ACS    0.2: Added multi-joystick support (minor 0 and 1)
  9.                    Added delay between measuring joystick axis
  10.                     Added scaling ioctl
  11. 02/16/93    ACS    0.3: Modified scaling to use ints to prevent kernel
  12.                  panics 8-)
  13. 02/28/93    ACS    0.4: Linux99.6 and fixed race condition in js_read.
  14.                  After looking at a schematic of a joystick card
  15.                              it became apparent that any write to the joystick
  16.                  port started ALL the joystick one shots. If the
  17.                  one that we are reading is short enough and the
  18.                  first one to be read, the second one will return
  19.                  bad data if it's one shot has not expired when
  20.                  the joystick port is written for the second time.
  21.                  Thus solves the mystery delay problem in 0.2!
  22. 05/05/93    ACS/Eyal 0.5:Upgraded the driver to the 99.9 kernel, added
  23.                  joystick support to the make config options,
  24.                  updated the driver to return the buttons as
  25.                  positive logic, and read both axis at once
  26.                  (thanks Eyal!), and added some new ioctls.
  27. 02/12/94    Jeff Tranter 0.6:Made necessary changes to work with 0.99pl15
  28.                              kernel (and hopefully 1.0). Also did some
  29.                  cleanup: indented code, fixed some typos, wrote
  30.                  man page, etc...
  31. 05/17/95    Dan Fandrich 0.7.3:Added I/O port registration, cleaned up code
  32. 04/03/96    Matt Rhoten 0.8: many minor changes:
  33.         new read loop from Hal Maney <maney@norden.com>
  34.         cleaned up #includes to allow #include of joystick.h with
  35.             gcc -Wall and from g++
  36.         made js_init fail if it finds zero joysticks
  37.         general source/comment cleanup
  38.         use of MOD_(INC|DEC)_USE_COUNT
  39.         changes from Bernd Schmidt
  40.             <crux@Pool.Informatik.RWTH-Aachen.DE>
  41.         to compile correctly under 1.3 in kernel or as module
  42.  
  43. 05/09/97    Mike Kienenberger (mkienenb@alaska.net) ported driver to
  44.         be a mach Loadable Kernel Server unix device driver under
  45.         Nextstep 3.3.
  46.         
  47.         Known problems:  occasional sporatic behavior of PS/2 Mouse
  48.         when accessing the joystick device.
  49.         
  50.         Unhandled linux features:
  51.             - inode access time not updated
  52.         - cli/sti interrupts currently not called
  53. */
  54.  
  55.  
  56. #include <sys/param.h>
  57. #include <sys/proc.h>
  58. #include <sys/systm.h>
  59. #include <sys/mbuf.h>
  60. #include <sys/buf.h>
  61. #include <sys/protosw.h>
  62. #include <sys/socket.h>
  63. #include <sys/ioctl.h>
  64. #include <sys/errno.h>
  65. #include <sys/syslog.h>
  66. #include <sys/file.h>
  67. #include <sys/uio.h>
  68. #include <sys/conf.h>
  69.  
  70. #include <kernserv/prototypes.h>
  71. #include <kernserv/machine/spl.h>
  72. #include <kernserv/kern_server_types.h>
  73. #include <kernserv/kalloc.h>
  74.  
  75. #import <driverkit/i386/ioPorts.h>
  76.  
  77. #include "joystick_kernel.h"
  78.  
  79. /*
  80.  * Older BSDs don't have kernel malloc.
  81.  */
  82. #if BSD < 199103
  83. #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
  84. #else
  85. #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
  86. #endif
  87.  
  88.  
  89. extern int nulldev();
  90. extern int nodev();
  91. extern int seltrue();
  92. #define nullstr 0
  93.  
  94. int js_open();
  95. int js_release();
  96. int js_read();
  97. int js_ioctl();
  98.  
  99. struct cdevsw my_cdevsw =  { 
  100.                  js_open,
  101.                  js_release,
  102.                  js_read,
  103.                  nodev,
  104.                  js_ioctl,
  105.                  nodev,
  106.                  nulldev,
  107.                  seltrue,
  108.                  nodev,
  109.                  nodev,
  110.                  nodev };
  111.  
  112. struct cdevsw my_saved_cdevsw;
  113.  
  114. static struct JS_DATA_SAVE_TYPE JS_DATA[JS_MAX];    /* misc data */
  115. static int JS_EXIST;        /* which joysticks' axis exist? */
  116. static int JS_READ_SEMAPHORE;    /* to prevent two processes from trying to */
  117.                 /* read different joysticks at the same time */
  118.  
  119. #if    0
  120. int    printf(char *fmt,...);
  121. int    suser(void);
  122. #endif
  123.  
  124. kern_server_t instance;
  125. #define JOYSTICKDEBUG    if (joysticdebug) printf
  126. int    joysticdebug = 0;
  127. ns_time_t       now;
  128.  
  129. /* 
  130.  * Example of a driver inserting itself into the block and character
  131.  * device switch tables.
  132.  */
  133.  
  134. #import <sys/conf.h> 
  135.  
  136. /* find_axes():
  137.  
  138.    returns which axes are hooked up, in a bitfield. 2^n is set if
  139.    axis n is hooked up, for 0 <= n < 4.
  140.  
  141.    REVIEW: should update this to handle eight-axis (four-stick) game port
  142.    cards. anyone have one of these to test on? mattrh 3/23/96 */
  143.  
  144. inline int find_axes(void)
  145. {
  146.     int j;
  147.  
  148.     linux_outb (0xff, JS_PORT);        /* trigger oneshots */
  149.                     /* and see what happens */
  150.  
  151.     for (j = JS_DEF_TIMEOUT; (0x0f & inb (JS_PORT)) && j; j--)
  152.         ;        /* do nothing; wait for the timeout */
  153.  
  154.     JS_EXIST = inb (JS_PORT) & 0x0f; /* get joystick status byte */
  155.  
  156.     JS_EXIST = (~JS_EXIST) & 0x0f;
  157.  
  158.     printf("find_axes: JS_EXIST is %d (0x%04X)\n", JS_EXIST, JS_EXIST);
  159.  
  160.     return JS_EXIST;
  161. }
  162.  
  163. /* get_timer0():
  164.    returns the current value of timer 0. This is a 16 bit counter that starts
  165.    at LATCH and counts down to 0 */
  166. inline int get_timer0 (void)
  167. {
  168.     int t0, t1;
  169.     linux_outb (0, PIT_MODE);
  170.     t0 = (int) inb (PIT_COUNTER_0);
  171.     t1 = ((int) inb (PIT_COUNTER_0) << 8) + t0;
  172.     return (t1);
  173. }
  174.  
  175. int js_ioctl(dev, cmd, addr, flag)
  176.     dev_t dev;
  177.     int cmd;
  178.     caddr_t addr;
  179.     int flag;
  180. {
  181.     unsigned int minor;
  182. //    unsigned int i;
  183.     unsigned int save_busy;
  184. //    char *c;
  185.  
  186.     minor = minor(dev);
  187.     if (major(dev) != JOYSTICK_MAJOR)
  188.         return EINVAL;
  189.     if (minor >= JS_MAX)
  190.         return ENODEV;
  191.     if ((((inb (JS_PORT) & 0x0f) >> (minor << 1)) & 0x03) == 0x03)    /*js minor exists?*/
  192.         return ENODEV;
  193.     switch (cmd) {
  194.     case JS_SET_CAL:    /*from struct *arg to JS_DATA[minor]*/
  195. #ifdef VERIFY_AREA
  196.         verify_area (VERIFY_READ, (void *) arg,
  197.                  sizeof (struct JS_DATA_TYPE));
  198. #endif VERIFY_AREA
  199.         bcopy((struct JS_DATA_TYPE *)addr,
  200.             &JS_DATA[minor].JS_CORR,
  201.             sizeof (struct JS_DATA_TYPE));
  202. //        c = (char *) &JS_DATA[minor].JS_CORR;
  203. //        for (i = 0; i < sizeof (struct JS_DATA_TYPE); i++)
  204. //            *c++ = get_fs_byte ((char *) arg++);
  205.         break;
  206.     case JS_GET_CAL:    /*to struct *arg from JS_DATA[minor]*/
  207. #ifdef VERIFY_AREA
  208.         verify_area (VERIFY_WRITE, (void *) arg,
  209.                  sizeof (struct JS_DATA_TYPE));
  210. #endif VERIFY_AREA
  211.         bcopy(&JS_DATA[minor].JS_CORR,
  212.             (struct JS_DATA_TYPE *)addr,
  213.             sizeof (struct JS_DATA_TYPE));
  214. //        c = (char *) &JS_DATA[minor].JS_CORR;
  215. //        for (i = 0; i < sizeof (struct JS_DATA_TYPE); i++)
  216. //            put_fs_byte (*c++, (char *) arg++);
  217.         break;
  218.     case JS_SET_TIMEOUT:
  219. #ifdef VERIFY_AREA
  220.         verify_area (VERIFY_READ, (void *) arg,
  221.                  sizeof (JS_DATA[0].JS_TIMEOUT));
  222. #endif VERIFY_AREA
  223.         bcopy(addr,
  224.             &JS_DATA[minor].JS_TIMEOUT,
  225.             sizeof (JS_DATA[0].JS_TIMEOUT));
  226. //        c = (char *) &JS_DATA[minor].JS_TIMEOUT;
  227. //        for (i = 0; i < sizeof (JS_DATA[0].JS_TIMEOUT); i++)
  228. //            *c++ = get_fs_byte ((char *) arg++);
  229.         break;
  230.     case JS_GET_TIMEOUT:
  231. #ifdef VERIFY_AREA
  232.         verify_area (VERIFY_WRITE, (void *) arg,
  233.                  sizeof (JS_DATA[0].JS_TIMEOUT));
  234. #endif VERIFY_AREA
  235.         bcopy(&JS_DATA[minor].JS_TIMEOUT,
  236.             addr,
  237.             sizeof (JS_DATA[0].JS_TIMEOUT));
  238. //        c = (char *) &JS_DATA[minor].JS_TIMEOUT;
  239. //        for (i = 0; i < sizeof (JS_DATA[0].JS_TIMEOUT); i++)
  240. //            put_fs_byte (*c++, (char *) arg++);
  241.         break;
  242.     case JS_SET_TIMELIMIT:
  243. #ifdef VERIFY_AREA
  244.         verify_area (VERIFY_READ, (void *) arg,
  245.                  sizeof (JS_DATA[0].JS_TIMELIMIT));
  246. #endif VERIFY_AREA
  247.         bcopy(addr,
  248.             &JS_DATA[minor].JS_TIMELIMIT,
  249.             sizeof (JS_DATA[0].JS_TIMELIMIT));
  250. //        c = (char *) &JS_DATA[minor].JS_TIMELIMIT;
  251. //        for (i = 0; i < sizeof (JS_DATA[0].JS_TIMELIMIT); i++)
  252. //            *c++ = get_fs_byte ((char *) arg++);
  253.         break;
  254.     case JS_GET_TIMELIMIT:
  255. #ifdef VERIFY_AREA
  256.         verify_area (VERIFY_WRITE, (void *) arg,
  257.                  sizeof (JS_DATA[0].JS_TIMELIMIT));
  258. #endif VERIFY_AREA
  259.         bcopy(&JS_DATA[minor].JS_TIMELIMIT,
  260.             addr,
  261.             sizeof (JS_DATA[0].JS_TIMELIMIT));
  262. //        c = (char *) &JS_DATA[minor].JS_TIMELIMIT;
  263. //        for (i = 0; i < sizeof (JS_DATA[0].JS_TIMELIMIT); i++)
  264. //            put_fs_byte (*c++, (char *) arg++);
  265.         break;
  266.     case JS_GET_ALL:
  267. #ifdef VERIFY_AREA
  268.         verify_area (VERIFY_WRITE, (void *) arg,
  269.                  sizeof (struct JS_DATA_SAVE_TYPE));
  270. #endif VERIFY_AREA
  271.         bcopy(&JS_DATA[minor],
  272.             addr,
  273.             sizeof (struct JS_DATA_SAVE_TYPE));
  274. //        c = (char *) &JS_DATA[minor];
  275. //        for (i = 0; i < sizeof (struct JS_DATA_SAVE_TYPE); i++)
  276. //            put_fs_byte (*c++, (char *) arg++);
  277.         break;
  278.     case JS_SET_ALL:
  279. #ifdef VERIFY_AREA
  280.         verify_area (VERIFY_READ, (void *) arg,
  281.                  sizeof (struct JS_DATA_SAVE_TYPE));
  282. #endif VERIFY_AREA
  283.         save_busy = JS_DATA[minor].BUSY;
  284.         bcopy((struct JS_DATA_SAVE_TYPE *)addr,
  285.             &JS_DATA[minor],
  286.             sizeof (struct JS_DATA_SAVE_TYPE));
  287. //        c = (char *) &JS_DATA[minor];
  288. //        for (i = 0; i < sizeof (struct JS_DATA_SAVE_TYPE); i++)
  289. //            *c++ = get_fs_byte ((char *) arg++);
  290.         JS_DATA[minor].BUSY = save_busy;
  291.         break;
  292.     default:
  293.         return EINVAL;
  294.     }
  295.     return 0;
  296. }
  297.  
  298. /* js_open():
  299.    device open routine. increments module usage count, initializes
  300.    data for that joystick.
  301.  
  302.    returns: 0 or
  303.    ENODEV: asked for joystick other than #0 or #1
  304.    ENODEV: asked for joystick on axis where there is none
  305.    EBUSY: attempt to open joystick already open
  306. */
  307. int js_open(dev, flag)
  308. dev_t dev;
  309. int flag;
  310. {
  311.     unsigned int minor = minor(dev);
  312.     int j;
  313.  
  314.     if (minor >= JS_MAX)
  315.         return ENODEV;    /*check for joysticks*/
  316.  
  317.     for (j = JS_DEF_TIMEOUT; (JS_EXIST & inb (JS_PORT)) && j; j--);
  318. #ifdef    CLI_STI
  319.     cli ();        /*block js_read while JS_EXIST is being modified*/
  320. #endif    CLI_STI
  321.     /*js minor exists?*/
  322.     if ((((JS_EXIST = inb (JS_PORT)) >> (minor << 1)) & 0x03) == 0x03) {
  323.         JS_EXIST = (~JS_EXIST) & 0x0f;
  324. #ifdef    CLI_STI
  325.         sti ();
  326. #endif    CLI_STI
  327.         return ENODEV;
  328.     }
  329.     JS_EXIST = (~JS_EXIST) & 0x0f;
  330. #ifdef    CLI_STI
  331.     sti ();
  332. #endif    CLI_STI
  333.  
  334.     if (JS_DATA[minor].BUSY)
  335.         return EBUSY;
  336.     JS_DATA[minor].BUSY = JS_TRUE;
  337.     JS_DATA[minor].JS_CORR.x = JS_DEF_CORR;    /*default scale*/
  338.     JS_DATA[minor].JS_CORR.y = JS_DEF_CORR;
  339.     JS_DATA[minor].JS_TIMEOUT = JS_DEF_TIMEOUT;
  340.     JS_DATA[minor].JS_TIMELIMIT = JS_DEF_TIMELIMIT;
  341. #ifdef    TIMELIMIT
  342.     JS_DATA[minor].JS_EXPIRETIME = CURRENT_JIFFIES;
  343. #else    TIMELIMIT
  344.     JS_DATA[minor].JS_EXPIRETIME = -1;
  345. #endif    TIMELIMIT
  346.  
  347. #ifdef    TODO
  348.     MOD_INC_USE_COUNT;
  349. #endif    TODO
  350.  
  351.     return 0;
  352. }
  353.  
  354. int js_release (dev, flag)
  355.     dev_t dev;
  356.     int flag;
  357. {
  358.     unsigned int minor = minor(dev);
  359. #ifdef    TODO
  360.     inode->i_atime = CURRENT_TIME;
  361. #endif    TODO
  362.     JS_DATA[minor].BUSY = JS_FALSE;
  363. #ifdef    TODO
  364.     MOD_DEC_USE_COUNT;
  365. #endif    TODO
  366.  
  367.     return 0;
  368. }
  369.  
  370. /* js_read() reads the buttons x, and y axis from both joysticks if a
  371.  * given interval has expired since the last read or is equal to
  372.  * -1l. The buttons are in port 0x201 in the high nibble. The axis are
  373.  * read by writing to 0x201 and then measuring the time it takes the
  374.  * one shots to clear.
  375.  */
  376.  
  377. int js_read(dev, uio)
  378.     dev_t dev;
  379.     register struct uio *uio;
  380. // (struct inode *inode, struct file *file, char *buf, int count)
  381. {
  382.     int j, chk, jsmask;
  383.     int t0, t_x0, t_y0, t_x1, t_y1;
  384. //    char *c;
  385.     unsigned int minor, minor2;
  386.     int buttons;
  387.     int error;
  388.  
  389.     if (uio->uio_resid != JS_RETURN)
  390.         return EINVAL;
  391.  
  392. #ifdef VERIFY_AREA
  393.     verify_area (VERIFY_WRITE, (void *) buf, sizeof (struct JS_DATA_TYPE));
  394. #endif VERIFY_AREA
  395.     minor = minor(dev);
  396. #ifdef    TODO
  397.     inode->i_atime = CURRENT_TIME;
  398. #endif    TODO
  399. #ifdef    TIMELIMIT
  400.     if (CURRENT_JIFFIES >= JS_DATA[minor].JS_EXPIRETIME) {
  401. #else    TIMELIMIT
  402.     if (1) {
  403. #endif    TIMELIMIT
  404.         minor2 = minor << 1;
  405.         j = JS_DATA[minor].JS_TIMEOUT;
  406.         for (; (JS_EXIST & inb (JS_PORT)) && j; j--);
  407.         if (j == 0)
  408.             return ENODEV;    /*no joystick here*/
  409.         while (1) {    /*Make sure no other proc is using port*/
  410. #ifdef    CLI_STI
  411.             cli ();
  412. #endif    CLI_STI
  413.             if (!JS_READ_SEMAPHORE) {
  414.                 JS_READ_SEMAPHORE++;
  415. #ifdef    CLI_STI
  416.                 sti ();
  417. #endif    CLI_STI
  418.                 break;
  419.             }
  420. #ifdef    CLI_STI
  421.             sti ();
  422. #endif    CLI_STI
  423.         }
  424.         buttons = ~(inb (JS_PORT) >> 4);
  425.         JS_DATA[0].JS_SAVE.buttons = buttons & 0x03;
  426.         JS_DATA[1].JS_SAVE.buttons = (buttons >> 2) & 0x03;
  427.         j = JS_DATA[minor].JS_TIMEOUT;
  428.         jsmask = 0;
  429.  
  430. #ifdef    CLI_STI
  431.         cli ();        /*no interrupts!*/
  432. #endif    CLI_STI
  433.         linux_outb (0xff, JS_PORT);    /*trigger one-shots*/
  434.         /*get init timestamp*/
  435.         t_x0 = t_y0 = t_x1 = t_y1 = t0 = get_timer0 ();
  436.         /*wait for an axis' bit to clear or timeout*/
  437.         while (j-- && (chk = (inb (JS_PORT) & JS_EXIST ) | jsmask)) {
  438.             if (!(chk & JS_X_0)) {
  439.                 t_x0 = get_timer0();
  440.                 jsmask |= JS_X_0;
  441.             }
  442.             if (!(chk & JS_Y_0)) {
  443.                 t_y0 = get_timer0();
  444.                 jsmask |= JS_Y_0;
  445.             }
  446.             if (!(chk & JS_X_1)) {
  447.                 t_x1 = get_timer0();
  448.                 jsmask |= JS_X_1;
  449.             }
  450.             if (!(chk & JS_Y_1)) {
  451.                 t_y1 = get_timer0();
  452.                 jsmask |= JS_Y_1;
  453.             }
  454.         }
  455. #ifdef    CLI_STI
  456.         sti ();                    /* allow interrupts */
  457. #endif    CLI_STI
  458.  
  459.         JS_READ_SEMAPHORE = 0;    /* allow other reads to progress */
  460.         if (j == 0)
  461.             return ENODEV;    /*read timed out*/
  462. #ifdef    TIMELIMIT
  463.         JS_DATA[0].JS_EXPIRETIME = CURRENT_JIFFIES +
  464.             JS_DATA[0].JS_TIMELIMIT;    /*update data*/
  465.         JS_DATA[1].JS_EXPIRETIME = CURRENT_JIFFIES +
  466.             JS_DATA[1].JS_TIMELIMIT;
  467. #else    TIMELIMIT
  468.         JS_DATA[0].JS_EXPIRETIME = -1;
  469.         JS_DATA[1].JS_EXPIRETIME = -1;
  470. #endif    TIMELIMIT
  471.         JS_DATA[0].JS_SAVE.x = DELTA_TIME (t0, t_x0) >>
  472.             JS_DATA[0].JS_CORR.x;
  473.         JS_DATA[0].JS_SAVE.y = DELTA_TIME (t0, t_y0) >>
  474.             JS_DATA[0].JS_CORR.y;
  475.         JS_DATA[1].JS_SAVE.x = DELTA_TIME (t0, t_x1) >>
  476.             JS_DATA[1].JS_CORR.x;
  477.         JS_DATA[1].JS_SAVE.y = DELTA_TIME (t0, t_y1) >>
  478.             JS_DATA[1].JS_CORR.y;
  479.     }
  480.  
  481. //    for (c = (char *) &JS_DATA[minor].JS_SAVE, j = 0; j < JS_RETURN; j++)
  482. //        put_fs_byte (*c++, buf++);    /*copy to user space*/
  483.  
  484. //    return JS_RETURN;
  485.  
  486.     error = UIOMOVE(&JS_DATA[minor].JS_SAVE, JS_RETURN, UIO_READ, uio); 
  487.  
  488.     return error;
  489. }
  490.  
  491.  
  492. int joystickattach(int unused)
  493. {
  494.     int js_num;
  495.     int js_count;
  496.     
  497.     printf( "Joystick device driver by Mike Kienenberger loaded.\n");
  498.  
  499.     js_num = find_axes();
  500.     js_count = !!(js_num & 0x3) + !!(js_num & 0xC),
  501.  
  502.     printf ("js_init: found %d joystick%c.\n",
  503.         js_count, (js_num == 1) ? ' ' : 's');
  504.  
  505.     if (js_count == 0)
  506.     {
  507.     printf("No joysticks found.\n");
  508. //    return ENODEV;
  509.     return 0;
  510.     /* if the user boots the machine, which runs insmod, and THEN
  511.         decides to hook up the joystick, well, then we do the wrong
  512.         thing. But it's a good idea to avoid giving out a false sense
  513.         of security by letting the module load otherwise. */
  514.     }
  515.  
  516.     /* Save whatever entries were in the tables for our major numbers. */
  517.     my_saved_cdevsw = cdevsw[JOYSTICK_MAJOR];
  518.     
  519.     /* Put my entries in the switch tables. */
  520.     cdevsw[JOYSTICK_MAJOR]  = my_cdevsw;
  521.  
  522.     for (js_num = 0; js_num < JS_MAX; js_num++)
  523.         JS_DATA[js_num].BUSY = JS_FALSE;
  524.     JS_READ_SEMAPHORE = 0;
  525.     
  526.     return 1;
  527. }
  528.  
  529. /* Detach the joystick device */
  530. int joystickdetach( void)
  531. {
  532.     printf( "Joystick device driver unloaded.\n");
  533.     
  534.     return 0;
  535. }
  536.  
  537.