home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Drivers / joystick-0.0-I / linux / joystick.c next >
Encoding:
C/C++ Source or Header  |  1997-05-04  |  11.1 KB  |  376 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 <crux@Pool.Informatik.RWTH-Aachen.DE>
  40.                 to compile correctly under 1.3 in kernel or as module
  41. */
  42.  
  43. #include <linux/module.h>
  44. #include <linux/joystick.h>
  45. #include <linux/mm.h>
  46. #include <linux/ioport.h>
  47. #include <asm/io.h>
  48.  
  49. static struct JS_DATA_SAVE_TYPE JS_DATA[JS_MAX];    /* misc data */
  50. static int JS_EXIST;            /* which joysticks' axis exist? */
  51. static int JS_READ_SEMAPHORE;    /* to prevent two processes from trying
  52.                                    to read different joysticks at the
  53.                                    same time */
  54.  
  55. /* get_timer0():
  56.    returns the current value of timer 0. This is a 16 bit counter that starts
  57.    at LATCH and counts down to 0 */
  58. inline int get_timer0 (void)
  59. {
  60.     int t0, t1;
  61.     outb (0, PIT_MODE);
  62.     t0 = (int) inb (PIT_COUNTER_0);
  63.     t1 = ((int) inb (PIT_COUNTER_0) << 8) + t0;
  64.     return (t1);
  65. }
  66.  
  67. /* find_axes():
  68.  
  69.    returns which axes are hooked up, in a bitfield. 2^n is set if
  70.    axis n is hooked up, for 0 <= n < 4.
  71.  
  72.    REVIEW: should update this to handle eight-axis (four-stick) game port
  73.    cards. anyone have one of these to test on? mattrh 3/23/96 */
  74. inline int find_axes(void)
  75. {
  76.     int j;
  77.  
  78.     outb (0xff, JS_PORT);        /* trigger oneshots */
  79.                                 /* and see what happens */
  80.  
  81.     for (j = JS_DEF_TIMEOUT; (0x0f & inb (JS_PORT)) && j; j--)
  82.         ;                        /* do nothing; wait for the timeout */
  83.  
  84.     JS_EXIST = inb (JS_PORT) & 0x0f; /* get joystick status byte */
  85.  
  86.     JS_EXIST = (~JS_EXIST) & 0x0f;
  87.  
  88.     printk("find_axes: JS_EXIST is %d (0x%04X)\n", JS_EXIST, JS_EXIST);
  89.  
  90.     return JS_EXIST;
  91. }
  92.  
  93. static int js_ioctl (struct inode *inode,
  94.              struct file *file,
  95.              unsigned int cmd,
  96.              unsigned long arg)
  97. {
  98.     unsigned int minor, i, save_busy;
  99.     char *c;
  100.     minor = MINOR (inode->i_rdev);
  101.     if (MAJOR (inode->i_rdev) != JOYSTICK_MAJOR)
  102.         return -EINVAL;
  103.     if (minor >= JS_MAX)
  104.         return -ENODEV;
  105.     if ((((inb (JS_PORT) & 0x0f) >> (minor << 1)) & 0x03) == 0x03)    /*js minor exists?*/
  106.         return -ENODEV;
  107.     switch (cmd) {
  108.     case JS_SET_CAL:    /*from struct *arg to JS_DATA[minor]*/
  109.         verify_area (VERIFY_READ, (void *) arg,
  110.                  sizeof (struct JS_DATA_TYPE));
  111.         c = (char *) &JS_DATA[minor].JS_CORR;
  112.         for (i = 0; i < sizeof (struct JS_DATA_TYPE); i++)
  113.             *c++ = get_fs_byte ((char *) arg++);
  114.         break;
  115.     case JS_GET_CAL:    /*to struct *arg from JS_DATA[minor]*/
  116.         verify_area (VERIFY_WRITE, (void *) arg,
  117.                  sizeof (struct JS_DATA_TYPE));
  118.         c = (char *) &JS_DATA[minor].JS_CORR;
  119.         for (i = 0; i < sizeof (struct JS_DATA_TYPE); i++)
  120.             put_fs_byte (*c++, (char *) arg++);
  121.         break;
  122.     case JS_SET_TIMEOUT:
  123.         verify_area (VERIFY_READ, (void *) arg,
  124.                  sizeof (JS_DATA[0].JS_TIMEOUT));
  125.         c = (char *) &JS_DATA[minor].JS_TIMEOUT;
  126.         for (i = 0; i < sizeof (JS_DATA[0].JS_TIMEOUT); i++)
  127.             *c++ = get_fs_byte ((char *) arg++);
  128.         break;
  129.     case JS_GET_TIMEOUT:
  130.         verify_area (VERIFY_WRITE, (void *) arg,
  131.                  sizeof (JS_DATA[0].JS_TIMEOUT));
  132.         c = (char *) &JS_DATA[minor].JS_TIMEOUT;
  133.         for (i = 0; i < sizeof (JS_DATA[0].JS_TIMEOUT); i++)
  134.             put_fs_byte (*c++, (char *) arg++);
  135.         break;
  136.     case JS_SET_TIMELIMIT:
  137.         verify_area (VERIFY_READ, (void *) arg,
  138.                  sizeof (JS_DATA[0].JS_TIMELIMIT));
  139.         c = (char *) &JS_DATA[minor].JS_TIMELIMIT;
  140.         for (i = 0; i < sizeof (JS_DATA[0].JS_TIMELIMIT); i++)
  141.             *c++ = get_fs_byte ((char *) arg++);
  142.         break;
  143.     case JS_GET_TIMELIMIT:
  144.         verify_area (VERIFY_WRITE, (void *) arg,
  145.                  sizeof (JS_DATA[0].JS_TIMELIMIT));
  146.         c = (char *) &JS_DATA[minor].JS_TIMELIMIT;
  147.         for (i = 0; i < sizeof (JS_DATA[0].JS_TIMELIMIT); i++)
  148.             put_fs_byte (*c++, (char *) arg++);
  149.         break;
  150.     case JS_GET_ALL:
  151.         verify_area (VERIFY_WRITE, (void *) arg,
  152.                  sizeof (struct JS_DATA_SAVE_TYPE));
  153.         c = (char *) &JS_DATA[minor];
  154.         for (i = 0; i < sizeof (struct JS_DATA_SAVE_TYPE); i++)
  155.             put_fs_byte (*c++, (char *) arg++);
  156.         break;
  157.     case JS_SET_ALL:
  158.         verify_area (VERIFY_READ, (void *) arg,
  159.                  sizeof (struct JS_DATA_SAVE_TYPE));
  160.         save_busy = JS_DATA[minor].BUSY;
  161.         c = (char *) &JS_DATA[minor];
  162.         for (i = 0; i < sizeof (struct JS_DATA_SAVE_TYPE); i++)
  163.             *c++ = get_fs_byte ((char *) arg++);
  164.         JS_DATA[minor].BUSY = save_busy;
  165.         break;
  166.     default:
  167.         return -EINVAL;
  168.     }
  169.     return 0;
  170. }
  171.  
  172. /* js_open():
  173.    device open routine. increments module usage count, initializes
  174.    data for that joystick.
  175.  
  176.    returns: 0 or
  177.    -ENODEV: asked for joystick other than #0 or #1
  178.    -ENODEV: asked for joystick on axis where there is none
  179.    -EBUSY: attempt to open joystick already open
  180. */
  181. static int js_open (struct inode *inode, struct file *file)
  182. {
  183.     unsigned int minor = MINOR (inode->i_rdev);
  184.     int j;
  185.  
  186.     if (minor >= JS_MAX)
  187.         return -ENODEV;    /*check for joysticks*/
  188.  
  189.     for (j = JS_DEF_TIMEOUT; (JS_EXIST & inb (JS_PORT)) && j; j--);
  190.     cli ();            /*block js_read while JS_EXIST is being modified*/
  191.     /*js minor exists?*/
  192.     if ((((JS_EXIST = inb (JS_PORT)) >> (minor << 1)) & 0x03) == 0x03) {
  193.         JS_EXIST = (~JS_EXIST) & 0x0f;
  194.         sti ();
  195.         return -ENODEV;
  196.     }
  197.     JS_EXIST = (~JS_EXIST) & 0x0f;
  198.     sti ();
  199.  
  200.     if (JS_DATA[minor].BUSY)
  201.         return -EBUSY;
  202.     JS_DATA[minor].BUSY = JS_TRUE;
  203.     JS_DATA[minor].JS_CORR.x = JS_DEF_CORR;    /*default scale*/
  204.     JS_DATA[minor].JS_CORR.y = JS_DEF_CORR;
  205.     JS_DATA[minor].JS_TIMEOUT = JS_DEF_TIMEOUT;
  206.     JS_DATA[minor].JS_TIMELIMIT = JS_DEF_TIMELIMIT;
  207.     JS_DATA[minor].JS_EXPIRETIME = CURRENT_JIFFIES;
  208.  
  209.     MOD_INC_USE_COUNT;
  210.     return 0;
  211. }
  212.  
  213. static void js_release (struct inode *inode, struct file *file)
  214. {
  215.     unsigned int minor = MINOR (inode->i_rdev);
  216.     inode->i_atime = CURRENT_TIME;
  217.     JS_DATA[minor].BUSY = JS_FALSE;
  218.     MOD_DEC_USE_COUNT;
  219. }
  220.  
  221. /* js_read() reads the buttons x, and y axis from both joysticks if a
  222.  * given interval has expired since the last read or is equal to
  223.  * -1l. The buttons are in port 0x201 in the high nibble. The axis are
  224.  * read by writing to 0x201 and then measuring the time it takes the
  225.  * one shots to clear.
  226.  */
  227.  
  228. static int js_read (struct inode *inode, struct file *file, char *buf, int count)
  229. {
  230.     int j, chk, jsmask;
  231.     int t0, t_x0, t_y0, t_x1, t_y1;
  232.     char *c;
  233.     unsigned int minor, minor2;
  234.     int buttons;
  235.  
  236.     if (count != JS_RETURN)
  237.         return -EOVERFLOW;
  238.     verify_area (VERIFY_WRITE, (void *) buf, sizeof (struct JS_DATA_TYPE));
  239.     minor = MINOR (inode->i_rdev);
  240.     inode->i_atime = CURRENT_TIME;
  241.     if (CURRENT_JIFFIES >= JS_DATA[minor].JS_EXPIRETIME) {
  242.         minor2 = minor << 1;
  243.         j = JS_DATA[minor].JS_TIMEOUT;
  244.         for (; (JS_EXIST & inb (JS_PORT)) && j; j--);
  245.         if (j == 0)
  246.             return -ENODEV;    /*no joystick here*/
  247.         while (1) {    /*Make sure no other proc is using port*/
  248.             cli ();
  249.             if (!JS_READ_SEMAPHORE) {
  250.                 JS_READ_SEMAPHORE++;
  251.                 sti ();
  252.                 break;
  253.             }
  254.             sti ();
  255.         }
  256.         buttons = ~(inb (JS_PORT) >> 4);
  257.         JS_DATA[0].JS_SAVE.buttons = buttons & 0x03;
  258.         JS_DATA[1].JS_SAVE.buttons = (buttons >> 2) & 0x03;
  259.         j = JS_DATA[minor].JS_TIMEOUT;
  260.         jsmask = 0;
  261.  
  262.         cli ();        /*no interrupts!*/
  263.         outb (0xff, JS_PORT);    /*trigger one-shots*/
  264.         /*get init timestamp*/
  265.         t_x0 = t_y0 = t_x1 = t_y1 = t0 = get_timer0 ();
  266.         /*wait for an axis' bit to clear or timeout*/
  267.         while (j-- && (chk = (inb (JS_PORT) & JS_EXIST ) | jsmask)) {
  268.             if (!(chk & JS_X_0)) {
  269.                 t_x0 = get_timer0();
  270.                 jsmask |= JS_X_0;
  271.             }
  272.             if (!(chk & JS_Y_0)) {
  273.                 t_y0 = get_timer0();
  274.                 jsmask |= JS_Y_0;
  275.             }
  276.             if (!(chk & JS_X_1)) {
  277.                 t_x1 = get_timer0();
  278.                 jsmask |= JS_X_1;
  279.             }
  280.             if (!(chk & JS_Y_1)) {
  281.                 t_y1 = get_timer0();
  282.                 jsmask |= JS_Y_1;
  283.             }
  284.         }
  285.         sti ();                    /* allow interrupts */
  286.  
  287.         JS_READ_SEMAPHORE = 0;    /* allow other reads to progress */
  288.         if (j == 0)
  289.             return -ENODEV;    /*read timed out*/
  290.         JS_DATA[0].JS_EXPIRETIME = CURRENT_JIFFIES +
  291.             JS_DATA[0].JS_TIMELIMIT;    /*update data*/
  292.         JS_DATA[1].JS_EXPIRETIME = CURRENT_JIFFIES +
  293.             JS_DATA[1].JS_TIMELIMIT;
  294.         JS_DATA[0].JS_SAVE.x = DELTA_TIME (t0, t_x0) >>
  295.             JS_DATA[0].JS_CORR.x;
  296.         JS_DATA[0].JS_SAVE.y = DELTA_TIME (t0, t_y0) >>
  297.             JS_DATA[0].JS_CORR.y;
  298.         JS_DATA[1].JS_SAVE.x = DELTA_TIME (t0, t_x1) >>
  299.             JS_DATA[1].JS_CORR.x;
  300.         JS_DATA[1].JS_SAVE.y = DELTA_TIME (t0, t_y1) >>
  301.             JS_DATA[1].JS_CORR.y;
  302.     }
  303.  
  304.     for (c = (char *) &JS_DATA[minor].JS_SAVE, j = 0; j < JS_RETURN; j++)
  305.         put_fs_byte (*c++, buf++);    /*copy to user space*/
  306.     return JS_RETURN;
  307. }
  308.  
  309.  
  310. static struct file_operations js_fops =
  311. {
  312.     NULL,            /* js_lseek*/
  313.     js_read,        /* js_read */
  314.     NULL,            /* js_write*/
  315.     NULL,            /* js_readaddr*/
  316.     NULL,            /* js_select */
  317.     js_ioctl,        /* js_ioctl*/
  318.     NULL,            /* js_mmap */
  319.     js_open,        /* js_open*/
  320.     js_release,        /* js_release*/
  321.     NULL            /* js_sync */
  322. };
  323.  
  324. #ifdef MODULE
  325.  
  326. #define joystick_init init_module
  327.  
  328. void cleanup_module (void)
  329. {
  330.     if (unregister_chrdev (JOYSTICK_MAJOR, "joystick"))
  331.         printk ("joystick: cleanup_module failed\n");
  332.     release_region(JS_PORT, 1);
  333. }
  334.  
  335. #endif /* MODULE */
  336.  
  337. int joystick_init(void)
  338. {
  339.     int js_num;
  340.     int js_count;
  341.  
  342.     if (check_region(JS_PORT, 1)) {
  343.         printk("js_init: port already in use\n");
  344.         return -EBUSY;
  345.     }
  346.  
  347.     js_num = find_axes();
  348.     js_count = !!(js_num & 0x3) + !!(js_num & 0xC),
  349.  
  350.     printk ("js_init: found %d joystick%c.\n",
  351.             js_count,
  352.             (js_num == 1) ? ' ' : 's');
  353.  
  354.     if (js_count == 0) {
  355.         printk("No joysticks found.\n");
  356.         return -ENODEV;
  357.         /* if the user boots the machine, which runs insmod, and THEN
  358.            decides to hook up the joystick, well, then we do the wrong
  359.            thing. But it's a good idea to avoid giving out a false sense
  360.            of security by letting the module load otherwise. */
  361.     }
  362.  
  363.     if (register_chrdev (JOYSTICK_MAJOR, "joystick", &js_fops)) {
  364.         printk ("Unable to get major=%d for joystick\n",
  365.                     JOYSTICK_MAJOR);
  366.         return -EBUSY;
  367.     }
  368.     request_region(JS_PORT, 1, "joystick");
  369.         
  370.     for (js_num = 0; js_num < JS_MAX; js_num++)
  371.           JS_DATA[js_num].BUSY = JS_FALSE;
  372.     JS_READ_SEMAPHORE = 0;
  373.     return 0;
  374. }
  375.  
  376.