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. init_module
  10. 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 
  31 #ifdef MODULE
  32 #include <linux/module.h>
  33 #include <linux/version.h>
  34 #else
  35 #define MOD_INC_USE_COUNT
  36 #define MOD_DEC_USE_COUNT
  37 #endif
  38 
  39 #include <linux/kernel.h>
  40 #include <linux/sched.h>
  41 #include <linux/busmouse.h>
  42 #include <linux/signal.h>
  43 #include <linux/errno.h>
  44 #include <linux/mm.h>
  45 #include <linux/mouse.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           mouse.buttons = buttons;
  79           mouse.dx += dx;
  80           mouse.dy -= dy;
  81           mouse.ready = 1;
  82           wake_up_interruptible(&mouse.wait);
  83 
  84           /*
  85            * keep dx/dy reasonable, but still able to track when X (or
  86            * whatever) must page or is busy (i.e. long waits between
  87            * reads)
  88            */
  89           if (mouse.dx < -2048)
  90               mouse.dx = -2048;
  91           if (mouse.dx >  2048)
  92               mouse.dx =  2048;
  93 
  94           if (mouse.dy < -2048)
  95               mouse.dy = -2048;
  96           if (mouse.dy >  2048)
  97               mouse.dy =  2048;
  98 
  99           if (mouse.fasyncptr)
 100               kill_fasync(mouse.fasyncptr, SIGIO);
 101         }
 102         MSE_INT_ON();
 103 }
 104 
 105 static int fasync_mouse(struct inode *inode, struct file *filp, int on)
     /* [previous][next][first][last][top][bottom][index][help] */
 106 {
 107         int retval;
 108 
 109         retval = fasync_helper(inode, filp, on, &mouse.fasyncptr);
 110         if (retval < 0)
 111                 return retval;
 112         return 0;
 113 }
 114 
 115 /*
 116  * close access to the mouse (can deal with multiple
 117  * opens if allowed in the future)
 118  */
 119 
 120 static void close_mouse(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
 121 {
 122         if (--mouse.active == 0) {
 123             MSE_INT_OFF();
 124             free_irq(mouse_irq);
 125         }
 126         fasync_mouse(inode, file, 0);
 127 }
 128 
 129 /*
 130  * open access to the mouse, currently only one open is
 131  * allowed.
 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 -EBUSY;
 140         mouse.ready = 0;
 141         mouse.dx = 0;
 142         mouse.dy = 0;
 143         mouse.buttons = 0x87;
 144         if (request_irq(mouse_irq, mouse_interrupt, 0, "Busmouse"))
 145                 return -EBUSY;
 146         mouse.active = 1;
 147         MSE_INT_ON();
 148         return 0;
 149 }
 150 
 151 /*
 152  * writes are disallowed
 153  */
 154 
 155 static int write_mouse(struct inode * inode, struct file * file, const char * buffer, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 156 {
 157         return -EINVAL;
 158 }
 159 
 160 /*
 161  * read mouse data.  Currently never blocks.
 162  */
 163 
 164 static int read_mouse(struct inode * inode, struct file * file, char * buffer, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 165 {
 166         int r;
 167         int dx;
 168         int dy;
 169         unsigned char buttons; 
 170 
 171         if (count < 3)
 172                 return -EINVAL;
 173         if ((r = verify_area(VERIFY_WRITE, buffer, count)))
 174                 return r;
 175         if (!mouse.ready)
 176                 return -EAGAIN;
 177 
 178         /*
 179          * Obtain the current mouse parameters and limit as appropriate for
 180          * the return data format.  Interrupts are only disabled while 
 181          * obtaining the parameters, NOT during the puts_fs_byte() calls,
 182          * so paging in put_user() does not effect mouse tracking.
 183          */
 184 
 185         MSE_INT_OFF();
 186         dx = mouse.dx;
 187         dy = mouse.dy;
 188         if (dx < -127)
 189             dx = -127;
 190         if (dx > 127)
 191             dx = 127;
 192         if (dy < -127)
 193             dy = -127;
 194         if (dy > 127)
 195             dy = 127;
 196         buttons = mouse.buttons;
 197         mouse.dx -= dx;
 198         mouse.dy -= dy;
 199         mouse.ready = 0;
 200         MSE_INT_ON();
 201 
 202         put_user(buttons | 0x80, buffer);
 203         put_user((char)dx, buffer + 1);
 204         put_user((char)dy, buffer + 2);
 205         for (r = 3; r < count; r++)
 206             put_user(0x00, buffer + r);
 207         return r;
 208 }
 209 
 210 /*
 211  * select for mouse input, must disable the mouse interrupt while checking
 212  * mouse.ready/select_wait() to avoid race condition (though in reality
 213  * such a condition is not fatal to the proper operation of the mouse since
 214  * multiple interrupts generally occur).
 215  */
 216 
 217 static int mouse_select(struct inode *inode, struct file *file, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 218 {
 219     int r = 0;
 220 
 221     if (sel_type == SEL_IN) {
 222         MSE_INT_OFF();
 223         if (mouse.ready) {
 224             r = 1;
 225         } else {
 226             select_wait(&mouse.wait, wait);
 227         }
 228         MSE_INT_ON();
 229     }
 230     return(r);
 231 }
 232 
 233 struct file_operations bus_mouse_fops = {
 234         NULL,           /* mouse_seek */
 235         read_mouse,
 236         write_mouse,
 237         NULL,           /* mouse_readdir */
 238         mouse_select,   /* mouse_select */
 239         NULL,           /* mouse_ioctl */
 240         NULL,           /* mouse_mmap */
 241         open_mouse,
 242         close_mouse,
 243         NULL,
 244         fasync_mouse,
 245 };
 246 
 247 static struct mouse bus_mouse = {
 248         LOGITECH_BUSMOUSE, "busmouse", &bus_mouse_fops
 249 };
 250 
 251 #ifdef MODULE
 252 char kernel_version[] = UTS_RELEASE;
 253 
 254 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 255 #else
 256 unsigned long bus_mouse_init(unsigned long kmem_start)
 257 #endif
 258 {
 259         int i;
 260 
 261         outb(MSE_CONFIG_BYTE, MSE_CONFIG_PORT);
 262         outb(MSE_SIGNATURE_BYTE, MSE_SIGNATURE_PORT);
 263         for (i = 0; i < 100000; i++)
 264                 /* busy loop */;
 265         if (inb(MSE_SIGNATURE_PORT) != MSE_SIGNATURE_BYTE) {
 266                 mouse.present = 0;
 267 #ifdef MODULE
 268                 return -EIO;
 269 #else
 270                 return kmem_start;
 271 #endif
 272         }
 273         outb(MSE_DEFAULT_MODE, MSE_CONFIG_PORT);
 274         MSE_INT_OFF();
 275         mouse.present = 1;
 276         mouse.active = 0;
 277         mouse.ready = 0;
 278         mouse.buttons = 0x87;
 279         mouse.dx = 0;
 280         mouse.dy = 0;
 281         mouse.wait = NULL;
 282         printk("Logitech Bus mouse detected and installed with IRQ %d.\n",
 283                mouse_irq);
 284         mouse_register(&bus_mouse);
 285 #ifdef MODULE
 286         return 0;
 287 #else
 288         return kmem_start;
 289 #endif
 290 }
 291 
 292 #ifdef MODULE
 293 void cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 294 {
 295         if (MOD_IN_USE)
 296                 printk("busmouse: in use - remove delayed\n");
 297         mouse_deregister(&bus_mouse);
 298 }
 299 #endif

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