1 /*
2  * Amiga Mouse Driver for Linux 68k by Michael Rausch
3  * based upon:
4  *
5  * Logitech Bus Mouse Driver for Linux
6  * by James Banks
7  *
8  * Mods by Matthew Dillon
9  *   calls verify_area()
10  *   tracks better when X is busy or paging
11  *
12  * Heavily modified by David Giller
13  *   changed from queue- to counter- driven
14  *   hacked out a (probably incorrect) mouse_poll
15  *
16  * Modified again by Nathan Laredo to interface with
17  *   0.96c-pl1 IRQ handling changes (13JUL92)
18  *   didn't bother touching poll code.
19  *
20  * Modified the poll() code blindly to conform to the VFS
21  *   requirements. 92.07.14 - Linus. Somebody should test it out.
22  *
23  * Modified by Johan Myreen to make room for other mice (9AUG92)
24  *   removed assignment chr_fops[10] = &mouse_fops; see mouse.c
25  *   renamed mouse_fops => bus_mouse_fops, made bus_mouse_fops public.
26  *   renamed this file mouse.c => busmouse.c
27  *
28  * Modified for use in the 1.3 kernels by Jes Sorensen.
29  *
30  * Moved the isr-allocation to the mouse_{open,close} calls, as there
31  *   is no reason to service the mouse in the vertical blank isr if
32  *   the mouse is not in use.             Jes Sorensen
33  *
34  * Converted to use new generic busmouse code.  5 Apr 1998
35  *   Russell King <rmk@arm.uk.linux.org>
36  */
37 
38 #include <linux/module.h>
39 
40 #include <linux/types.h>
41 #include <linux/kernel.h>
42 #include <linux/sched.h>
43 #include <linux/mm.h>
44 #include <linux/signal.h>
45 #include <linux/errno.h>
46 #include <linux/miscdevice.h>
47 #include <linux/random.h>
48 #include <linux/poll.h>
49 #include <linux/init.h>
50 #include <linux/ioport.h>
51 #include <linux/logibusmouse.h>
52 
53 #include <asm/setup.h>
54 #include <asm/system.h>
55 #include <asm/uaccess.h>
56 #include <asm/irq.h>
57 #include <asm/amigahw.h>
58 #include <asm/amigaints.h>
59 
60 #include "busmouse.h"
61 
62 #if AMIGA_OLD_INT
63 #define AMI_MSE_INT_ON()	mouseint_allowed = 1
64 #define AMI_MSE_INT_OFF()	mouseint_allowed = 0
65 static int mouseint_allowed;
66 #endif
67 
68 static int msedev;
69 
mouse_interrupt(int irq,void * dummy,struct pt_regs * fp)70 static void mouse_interrupt(int irq, void *dummy, struct pt_regs *fp)
71 {
72 	static int lastx=0, lasty=0;
73 	int dx, dy;
74 	int nx, ny;
75 	unsigned char buttons;
76 
77 	unsigned short joy0dat, potgor;
78 
79 #if AMIGA_OLD_INT
80 	if(!mouseint_allowed)
81 		return;
82 	AMI_MSE_INT_OFF();
83 #endif
84 
85 	/*
86 	 *  This routine assumes, just like Kickstart, that the mouse
87 	 *  has not moved more than 127 ticks since last VBL.
88 	 */
89 
90 	joy0dat = custom.joy0dat;
91 
92 	nx = joy0dat & 0xff;
93 	ny = joy0dat >> 8;
94 
95 	dx = nx - lastx;
96 	if (dx < - 127)
97 		dx = (256 + nx) - lastx;
98 
99 	if (dx > 127)
100 		dx = (nx - 256) - lastx;
101 
102 	dy = ny - lasty;
103 	if (dy < - 127)
104 		dy = (256 + ny) - lasty;
105 
106 	if (dy > 127)
107 		dy = (ny - 256) - lasty;
108 
109 	lastx = nx;
110 	lasty = ny;
111 
112 #if 0
113 	dx = -lastdx;
114 	dx += (lastdx = joy0dat & 0xff);
115 	if (dx < -127)
116 	    dx = -255-dx;		/* underrun */
117 	else
118 	if (dx > 127)
119 	    dx = 255-dx;		/* overflow */
120 
121 	dy = -lastdy;
122 	dy += (lastdy = joy0dat >> 8);
123 	if (dy < -127)
124 	    dy = -255-dy;
125 	else
126 	if (dy > 127)
127 	    dy = 255-dy;
128 #endif
129 
130 
131 	potgor = custom.potgor;
132 	buttons = (ciaa.pra & 0x40 ? 4 : 0) |	/* left button; note that the bits are low-active, as are the expected results -> double negation */
133 #if 1
134 		  (potgor & 0x0100 ? 2 : 0) |	/* middle button; emulation goes here */
135 #endif
136 		  (potgor & 0x0400 ? 1 : 0);	/* right button */
137 
138 
139 	busmouse_add_movementbuttons(msedev, dx, -dy, buttons);
140 #if AMIGA_OLD_INT
141 	AMI_MSE_INT_ON();
142 #endif
143 }
144 
145 /*
146  * close access to the mouse
147  */
148 
release_mouse(struct inode * inode,struct file * file)149 static int release_mouse(struct inode * inode, struct file * file)
150 {
151 	free_irq(IRQ_AMIGA_VERTB, mouse_interrupt);
152 #if AMIGA_OLD_INT
153 	AMI_MSE_INT_OFF();
154 #endif
155 	return 0;
156 }
157 
158 /*
159  * open access to the mouse, currently only one open is
160  * allowed.
161  */
162 
open_mouse(struct inode * inode,struct file * file)163 static int open_mouse(struct inode * inode, struct file * file)
164 {
165 	/*
166 	 *  use VBL to poll mouse deltas
167 	 */
168 
169 	if(request_irq(IRQ_AMIGA_VERTB, mouse_interrupt, 0,
170 	               "Amiga mouse", mouse_interrupt)) {
171 		printk(KERN_INFO "Installing Amiga mouse failed.\n");
172 		return -EIO;
173 	}
174 
175 #if AMIGA_OLD_INT
176 	AMI_MSE_INT_ON();
177 #endif
178 	return 0;
179 }
180 
181 static struct busmouse amigamouse = {
182 	AMIGAMOUSE_MINOR, "amigamouse", THIS_MODULE, open_mouse, release_mouse, 7
183 };
184 
amiga_mouse_init(void)185 static int __init amiga_mouse_init(void)
186 {
187 	if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_MOUSE))
188 		return -ENODEV;
189 	if (!request_mem_region(CUSTOM_PHYSADDR+10, 2, "amigamouse [Denise]"))
190 		return -EBUSY;
191 
192 	custom.joytest = 0;	/* reset counters */
193 #if AMIGA_OLD_INT
194 	AMI_MSE_INT_OFF();
195 #endif
196 	msedev = register_busmouse(&amigamouse);
197 	if (msedev < 0)
198 		printk(KERN_WARNING "Unable to install Amiga mouse driver.\n");
199 	else
200 		printk(KERN_INFO "Amiga mouse installed.\n");
201 	return msedev < 0 ? msedev : 0;
202 }
203 
amiga_mouse_exit(void)204 static void __exit amiga_mouse_exit(void)
205 {
206 	unregister_busmouse(msedev);
207 	release_mem_region(CUSTOM_PHYSADDR+10, 2);
208 }
209 
210 module_init(amiga_mouse_init);
211 module_exit(amiga_mouse_exit);
212 
213 MODULE_LICENSE("GPL");
214