adbmouse.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Macintosh ADB Mouse driver for Linux
  3.  *
  4.  * 27 Oct 1997 Michael Schmitz
  5.  * logitech fixes by anthony tong
  6.  * further hacking by Paul Mackerras
  7.  *
  8.  * Apple mouse protocol according to:
  9.  *
  10.  * Device code shamelessly stolen from:
  11.  */
  12. /*
  13.  * Atari Mouse Driver for Linux
  14.  * by Robert de Vries (robert@and.nl) 19Jul93
  15.  *
  16.  * 16 Nov 1994 Andreas Schwab
  17.  * Compatibility with busmouse
  18.  * Support for three button mouse (shamelessly stolen from MiNT)
  19.  * third button wired to one of the joystick directions on joystick 1
  20.  *
  21.  * 1996/02/11 Andreas Schwab
  22.  * Module support
  23.  * Allow multiple open's
  24.  *
  25.  * Converted to use new generic busmouse code.  11 July 1998
  26.  *   Russell King <rmk@arm.uk.linux.org>
  27.  */
  28. #include <linux/module.h>
  29. #include <linux/sched.h>
  30. #include <linux/errno.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/mm.h>
  33. #include <linux/random.h>
  34. #include <linux/poll.h>
  35. #include <linux/init.h>
  36. #include <linux/adb_mouse.h>
  37. #ifdef __powerpc__
  38. #include <asm/processor.h>
  39. #endif
  40. #if defined(__mc68000__) || defined(MODULE)
  41. #include <asm/setup.h>
  42. #endif
  43. #include "busmouse.h"
  44. static int msedev;
  45. static unsigned char adb_mouse_buttons[16];
  46. extern void (*adb_mouse_interrupt_hook)(unsigned char *, int);
  47. extern int adb_emulate_buttons;
  48. extern int adb_button2_keycode;
  49. extern int adb_button3_keycode;
  50. /*
  51.  *    XXX: need to figure out what ADB mouse packets mean ... 
  52.  *      This is the stuff stolen from the Atari driver ...
  53.  */
  54. static void adb_mouse_interrupt(unsigned char *buf, int nb)
  55. {
  56. int buttons, id;
  57. char dx, dy;
  58. /*
  59.    Handler 1 -- 100cpi original Apple mouse protocol.
  60.    Handler 2 -- 200cpi original Apple mouse protocol.
  61.    For Apple's standard one-button mouse protocol the data array will
  62.    contain the following values:
  63.        BITS    COMMENTS
  64.    data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
  65.    data[1] = bxxx xxxx First button and x-axis motion.
  66.    data[2] = byyy yyyy Second button and y-axis motion.
  67.    Handler 4 -- Apple Extended mouse protocol.
  68.    For Apple's 3-button mouse protocol the data array will contain the
  69.    following values:
  70.        BITS    COMMENTS
  71.    data[0] = dddd 1100 ADB command: Talk, register 0, for device dddd.
  72.    data[1] = bxxx xxxx Left button and x-axis motion.
  73.    data[2] = byyy yyyy Second button and y-axis motion.
  74.    data[3] = byyy bxxx Third button and fourth button.  
  75.    Y is additiona. high bits of y-axis motion.  
  76.    X is additional high bits of x-axis motion.
  77.    'buttons' here means 'button down' states!
  78.    Button 1 (left)  : bit 2, busmouse button 3
  79.    Button 2 (right) : bit 0, busmouse button 1
  80.    Button 3 (middle): bit 1, busmouse button 2
  81.  */
  82. /* x/y and buttons swapped */
  83. id = (buf[0] >> 4) & 0xf;
  84. buttons = adb_mouse_buttons[id];
  85. /* button 1 (left, bit 2) */
  86. buttons = (buttons & 3) | (buf[1] & 0x80 ? 4 : 0); /* 1+2 unchanged */
  87. /* button 2 (middle) */
  88. buttons = (buttons & 5) | (buf[2] & 0x80 ? 2 : 0); /* 2+3 unchanged */
  89. /* button 3 (right) present?
  90.  *  on a logitech mouseman, the right and mid buttons sometimes behave
  91.  *  strangely until they both have been pressed after booting. */
  92. /* data valid only if extended mouse format ! */
  93. if (nb >= 4)
  94. buttons = (buttons & 6) | (buf[3] & 0x80 ? 1 : 0); /* 1+3 unchanged */
  95. adb_mouse_buttons[id] = buttons;
  96. /* a button is down if it is down on any mouse */
  97. for (id = 0; id < 16; ++id)
  98. buttons &= adb_mouse_buttons[id];
  99. dx = ((buf[2] & 0x7f) < 64 ? (buf[2] & 0x7f) : (buf[2] & 0x7f) - 128);
  100. dy = ((buf[1] & 0x7f) < 64 ? (buf[1] & 0x7f) : (buf[1] & 0x7f) - 128);
  101. busmouse_add_movementbuttons(msedev, dx, -dy, buttons);
  102. if (console_loglevel >= 8)
  103. printk(" %X %X %X dx %d dy %d n",
  104.        buf[1], buf[2], buf[3], dx, dy);
  105. }
  106. static int release_mouse(struct inode *inode, struct file *file)
  107. {
  108. adb_mouse_interrupt_hook = NULL;
  109. /*
  110.  * FIXME?: adb_mouse_interrupt_hook may still be executing
  111.  * on another CPU.
  112.  */
  113. return 0;
  114. }
  115. static int open_mouse(struct inode *inode, struct file *file)
  116. {
  117. adb_mouse_interrupt_hook = adb_mouse_interrupt;
  118. return 0;
  119. }
  120. static struct busmouse adb_mouse =
  121. {
  122. ADB_MOUSE_MINOR, "adbmouse", THIS_MODULE, open_mouse, release_mouse, 7
  123. };
  124. static int __init adb_mouse_init(void)
  125. {
  126. #ifdef __powerpc__
  127. if ((_machine != _MACH_chrp) && (_machine != _MACH_Pmac))
  128. return -ENODEV;
  129. #endif
  130. #ifdef __mc68000__
  131. if (!MACH_IS_MAC)
  132. return -ENODEV;
  133. #endif
  134. /* all buttons up */
  135. memset(adb_mouse_buttons, 7, sizeof(adb_mouse_buttons));
  136. msedev = register_busmouse(&adb_mouse);
  137. if (msedev < 0)
  138. printk(KERN_WARNING "Unable to register ADB mouse driver.n");
  139. else
  140. printk(KERN_INFO "Macintosh ADB mouse driver installed.n");
  141. return msedev < 0 ? msedev : 0;
  142. }
  143. #ifndef MODULE
  144. /*
  145.  * XXX this function is misnamed.
  146.  * It is called if the kernel is booted with the adb_buttons=xxx
  147.  * option, which is about using ADB keyboard buttons to emulate
  148.  * mouse buttons. -- paulus
  149.  */
  150. static int __init adb_mouse_setup(char *str)
  151. {
  152. int ints[4];
  153. str = get_options(str, ARRAY_SIZE(ints), ints);
  154. if (ints[0] >= 1) {
  155. adb_emulate_buttons = ints[1];
  156. if (ints[0] >= 2)
  157. adb_button2_keycode = ints[2];
  158. if (ints[0] >= 3)
  159. adb_button3_keycode = ints[3];
  160. }
  161. return 1;
  162. }
  163. __setup("adb_buttons=", adb_mouse_setup);
  164. #endif /* !MODULE */
  165. static void __exit adb_mouse_cleanup(void)
  166. {
  167. unregister_busmouse(msedev);
  168. }
  169. module_init(adb_mouse_init);
  170. module_exit(adb_mouse_cleanup);
  171. MODULE_LICENSE("GPL");