root/drivers/char/busmouse.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. bmouse_setup
  2. mouse_interrupt
  3. fasync_mouse
  4. close_mouse
  5. open_mouse
  6. write_mouse
  7. read_mouse
  8. mouse_select
  9. bus_mouse_init
  10. init_module
  11. cleanup_module

   1 /*
   2  * Logitech Bus Mouse Driver for Linux
   3  * by James Banks
   4  *
   5  * Mods by Matthew Dillon
   6  *   calls verify_area()
   7  *   tracks better when X is busy or paging
   8  *
   9  * Heavily modified by David Giller
  10  *   changed from queue- to counter- driven
  11  *   hacked out a (probably incorrect) mouse_select
  12  *
  13  * Modified again by Nathan Laredo to interface with
  14  *   0.96c-pl1 IRQ handling changes (13JUL92)
  15  *   didn't bother touching select code.
  16  *
  17  * Modified the select() code blindly to conform to the VFS
  18  *   requirements. 92.07.14 - Linus. Somebody should test it out.
  19  *
  20  * Modified by Johan Myreen to make room for other mice (9AUG92)
  21  *   removed assignment chr_fops[10] = &mouse_fops; see mouse.c
  22  *   renamed mouse_fops => bus_mouse_fops, made bus_mouse_fops public.
  23  *   renamed this file mouse.c => busmouse.c
  24  *
  25  * Minor addition by Cliff Matthews
  26  *   added fasync support
  27  *
  28  * Modularised 6-Sep-95 Philip Blundell <pjb27@cam.ac.uk> 
  29  *
  30  * Replaced dumb busy loop with udelay()  16 Nov 95
  31  *   Nathan Laredo <laredo@gnu.ai.mit.edu>
  32  *
  33  */
  34 
  35 #include <linux/module.h>
  36 
  37 #include <linux/kernel.h>
  38 #include <linux/sched.h>
  39 #include <linux/busmouse.h>
  40 #include <linux/signal.h>
  41 #include <linux/errno.h>
  42 #include <linux/mm.h>
  43 #include <linux/mouse.h>
  44 #include <linux/random.h>
  45 #include <linux/delay.h>
  46 
  47 #include <asm/io.h>
  48 #include <asm/segment.h>
  49 #include <asm/system.h>
  50 #include <asm/irq.h>
  51 
  52 static struct mouse_status mouse;
  53 static int mouse_irq = MOUSE_IRQ;
  54 
  55 void bmouse_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
  56 {
  57         if (ints[0] > 0)
  58                 mouse_irq=ints[1];
  59 }
  60 
  61 static void mouse_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
  62 {
  63         char dx, dy;
  64         unsigned char buttons;
  65 
  66         MSE_INT_OFF();
  67         outb(MSE_READ_X_LOW, MSE_CONTROL_PORT);
  68         dx = (inb(MSE_DATA_PORT) & 0xf);
  69         outb(MSE_READ_X_HIGH, MSE_CONTROL_PORT);
  70         dx |= (inb(MSE_DATA_PORT) & 0xf) << 4;
  71         outb(MSE_READ_Y_LOW, MSE_CONTROL_PORT );
  72         dy = (inb(MSE_DATA_PORT) & 0xf);
  73         outb(MSE_READ_Y_HIGH, MSE_CONTROL_PORT);
  74         buttons = inb(MSE_DATA_PORT);
  75         dy |= (buttons & 0xf) << 4;
  76         buttons = ((buttons >> 5) & 0x07);
  77         if (dx != 0 || dy != 0 || buttons != mouse.buttons) {
  78           add_mouse_randomness((buttons << 16) + (dy << 8) + dx);
  79           mouse.buttons = buttons;
  80           mouse.dx += dx;
  81           mouse.dy -= dy;
  82           mouse.ready = 1;
  83           wake_up_interruptible(&mouse.wait);
  84 
  85           /*
  86            * keep dx/dy reasonable, but still able to track when X (or
  87            * whatever) must page or is busy (i.e. long waits between
  88            * reads)
  89            */
  90           if (mouse.dx < -2048)
  91               mouse.dx = -2048;
  92           if (mouse.dx >  2048)
  93               mouse.dx =  2048;
  94 
  95           if (mouse.dy < -2048)
  96               mouse.dy = -2048;
  97           if (mouse.dy >  2048)
  98               mouse.dy =  2048;
  99 
 100           if (mouse.fasyncptr)
 101               kill_fasync(mouse.fasyncptr, SIGIO);
 102         }
 103         MSE_INT_ON();
 104 }
 105 
 106 static int fasync_mouse(struct inode *inode, struct file *filp, int on)
     /* [previous][next][first][last][top][bottom][index][help] */
 107 {
 108         int retval;
 109 
 110         retval = fasync_helper(inode, filp, on, &mouse.fasyncptr);
 111         if (retval < 0)
 112                 return retval;
 113         return 0;
 114 }
 115 
 116 /*
 117  * close access to the mouse
 118  */
 119 
 120 static void close_mouse(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
 121 {
 122         fasync_mouse(inode, file, 0);
 123         if (--mouse.active)
 124                 return;
 125         MSE_INT_OFF();
 126         free_irq(mouse_irq);
 127         MOD_DEC_USE_COUNT;
 128 }
 129 
 130 /*
 131  * open access to the mouse
 132  */
 133 
 134 static int open_mouse(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
 135 {
 136         if (!mouse.present)
 137                 return -EINVAL;
 138         if (mouse.active++)
 139                 return 0;
 140         if (request_irq(mouse_irq, mouse_interrupt, 0, "Busmouse")) {
 141                 mouse.active--;
 142                 return -EBUSY;
 143         }
 144         mouse.ready = 0;
 145         mouse.dx = 0;
 146         mouse.dy = 0;
 147         mouse.buttons = 0x87;
 148         MOD_INC_USE_COUNT;
 149         MSE_INT_ON();
 150         return 0;
 151 }
 152 
 153 /*
 154  * writes are disallowed
 155  */
 156 
 157 static int write_mouse(struct inode * inode, struct file * file, const char * buffer, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 158 {
 159         return -EINVAL;
 160 }
 161 
 162 /*
 163  * read mouse data.  Currently never blocks.
 164  */
 165 
 166 static int read_mouse(struct inode * inode, struct file * file, char * buffer, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168         int r;
 169         int dx;
 170         int dy;
 171         unsigned char buttons; 
 172 
 173         if (count < 3)
 174                 return -EINVAL;
 175         if ((r = verify_area(VERIFY_WRITE, buffer, count)))
 176                 return r;
 177         if (!mouse.ready)
 178                 return -EAGAIN;
 179 
 180         /*
 181          * Obtain the current mouse parameters and limit as appropriate for
 182          * the return data format.  Interrupts are only disabled while 
 183          * obtaining the parameters, NOT during the puts_fs_byte() calls,
 184          * so paging in put_user() does not effect mouse tracking.
 185          */
 186 
 187         MSE_INT_OFF();
 188         dx = mouse.dx;
 189         dy = mouse.dy;
 190         if (dx < -127)
 191             dx = -127;
 192         if (dx > 127)
 193             dx = 127;
 194         if (dy < -127)
 195             dy = -127;
 196         if (dy > 127)
 197             dy = 127;
 198         buttons = mouse.buttons;
 199         mouse.dx -= dx;
 200         mouse.dy -= dy;
 201         mouse.ready = 0;
 202         MSE_INT_ON();
 203 
 204         put_user(buttons | 0x80, buffer);
 205         put_user((char)dx, buffer + 1);
 206         put_user((char)dy, buffer + 2);
 207         for (r = 3; r < count; r++)
 208             put_user(0x00, buffer + r);
 209         return r;
 210 }
 211 
 212 /*
 213  * select for mouse input
 214  */
 215 static int mouse_select(struct inode *inode, struct file *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 216 {
 217         if (sel_type == SEL_IN) {
 218                 if (mouse.ready)
 219                         return 1;
 220                 select_wait(&mouse.wait, wait);
 221         }
 222         return 0;
 223 }
 224 
 225 struct file_operations bus_mouse_fops = {
 226         NULL,           /* mouse_seek */
 227         read_mouse,
 228         write_mouse,
 229         NULL,           /* mouse_readdir */
 230         mouse_select,   /* mouse_select */
 231         NULL,           /* mouse_ioctl */
 232         NULL,           /* mouse_mmap */
 233         open_mouse,
 234         close_mouse,
 235         NULL,
 236         fasync_mouse,
 237 };
 238 
 239 static struct mouse bus_mouse = {
 240         LOGITECH_BUSMOUSE, "busmouse", &bus_mouse_fops
 241 };
 242 
 243 int bus_mouse_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 244 {
 245         int i;
 246 
 247         outb(MSE_CONFIG_BYTE, MSE_CONFIG_PORT);
 248         outb(MSE_SIGNATURE_BYTE, MSE_SIGNATURE_PORT);
 249         udelay(100L);   /* wait for reply from mouse */
 250         if (inb(MSE_SIGNATURE_PORT) != MSE_SIGNATURE_BYTE) {
 251                 mouse.present = 0;
 252                 return -EIO;
 253         }
 254         outb(MSE_DEFAULT_MODE, MSE_CONFIG_PORT);
 255         MSE_INT_OFF();
 256         mouse.present = 1;
 257         mouse.active = 0;
 258         mouse.ready = 0;
 259         mouse.buttons = 0x87;
 260         mouse.dx = 0;
 261         mouse.dy = 0;
 262         mouse.wait = NULL;
 263         printk("Logitech Bus mouse detected and installed with IRQ %d.\n",
 264                mouse_irq);
 265         mouse_register(&bus_mouse);
 266         return 0;
 267 }
 268 
 269 #ifdef MODULE
 270 
 271 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 272 {
 273         return bus_mouse_init();
 274 }
 275 
 276 void cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 277 {
 278         mouse_deregister(&bus_mouse);
 279 }
 280 #endif

/* [previous][next][first][last][top][bottom][index][help] */