more changes on original files
[linux-2.4.git] / arch / m68k / atari / joystick.c
1 /*
2  * Atari Joystick Driver for Linux
3  * by Robert de Vries (robert@and.nl) 19Jul93
4  *
5  * 16 Nov 1994 Andreas Schwab
6  * Support for three button mouse (shamelessly stolen from MiNT)
7  * third button wired to one of the joystick directions on joystick 1
8  */
9
10 #include <linux/sched.h>
11 #include <linux/errno.h>
12 #include <linux/major.h>
13 #include <linux/poll.h>
14 #include <linux/init.h>
15 #include <linux/devfs_fs_kernel.h>
16 #include <linux/smp_lock.h>
17
18 #include <asm/atarikb.h>
19 #include <asm/atari_joystick.h>
20 #include <asm/uaccess.h>
21
22 #define MAJOR_NR    JOYSTICK_MAJOR
23
24 #define ANALOG_JOY(n)   (!(n & 0x80))
25 #define DIGITAL_JOY(n)  (n & 0x80)
26 #define DEVICE_NR(n)    (MINOR(n) & 0x7f)
27
28
29 static struct joystick_status joystick[2];
30 int atari_mouse_buttons; /* for three-button mouse */
31
32 void atari_joystick_interrupt(char *buf)
33 {
34     int j;
35 /*    ikbd_joystick_disable(); */
36
37     j = buf[0] & 0x1;
38     joystick[j].dir   = buf[1] & 0xF;
39     joystick[j].fire  = (buf[1] & 0x80) >> 7;
40     joystick[j].ready = 1;
41     wake_up_interruptible(&joystick[j].wait);
42
43     /* For three-button mouse emulation fake a mouse packet */
44     if (atari_mouse_interrupt_hook &&
45         j == 1 && (buf[1] & 1) != ((atari_mouse_buttons & 2) >> 1))
46       {
47         char faked_packet[3];
48
49         atari_mouse_buttons = (atari_mouse_buttons & 5) | ((buf[1] & 1) << 1);
50         faked_packet[0] = (atari_mouse_buttons & 1) | 
51                           (atari_mouse_buttons & 4 ? 2 : 0);
52         faked_packet[1] = 0;
53         faked_packet[2] = 0;
54         atari_mouse_interrupt_hook (faked_packet);
55       }
56
57 /*    ikbd_joystick_event_on(); */
58 }
59
60 static int release_joystick(struct inode *inode, struct file *file)
61 {
62     int minor = DEVICE_NR(inode->i_rdev);
63
64     lock_kernel();
65     joystick[minor].active = 0;
66     joystick[minor].ready = 0;
67
68     if ((joystick[0].active == 0) && (joystick[1].active == 0))
69         ikbd_joystick_disable();
70     unlock_kernel();
71     return 0;
72 }
73
74 static int open_joystick(struct inode *inode, struct file *file)
75 {
76     int minor = DEVICE_NR(inode->i_rdev);
77
78     if (!DIGITAL_JOY(inode->i_rdev) || minor > 1)
79         return -ENODEV;
80     if (joystick[minor].active)
81         return -EBUSY;
82     joystick[minor].active = 1;
83     joystick[minor].ready = 0;
84     ikbd_joystick_event_on();
85     return 0;
86 }
87
88 static ssize_t write_joystick(struct file *file, const char *buffer,
89                               size_t count, loff_t *ppos)
90 {
91     return -EINVAL;
92 }
93
94 static ssize_t read_joystick(struct file *file, char *buffer, size_t count,
95                              loff_t *ppos)
96 {
97     struct inode *inode = file->f_dentry->d_inode;
98     int minor = DEVICE_NR(inode->i_rdev);
99
100     if (count < 2)
101         return -EINVAL;
102     if (!joystick[minor].ready)
103         return -EAGAIN;
104     joystick[minor].ready = 0;
105     if (put_user(joystick[minor].fire, buffer++) ||
106         put_user(joystick[minor].dir, buffer++))
107         return -EFAULT;
108     if (count > 2)
109         if (clear_user(buffer, count - 2))
110             return -EFAULT;
111     return count;
112 }
113
114 static unsigned int joystick_poll(struct file *file, poll_table *wait)
115 {
116     int minor = DEVICE_NR(file->f_dentry->d_inode->i_rdev);
117
118     poll_wait(file, &joystick[minor].wait, wait);
119     if (joystick[minor].ready)
120         return POLLIN | POLLRDNORM;
121     return 0;
122 }
123
124 struct file_operations atari_joystick_fops = {
125         read:           read_joystick,
126         write:          write_joystick,
127         poll:           joystick_poll,
128         open:           open_joystick,
129         release:        release_joystick,
130 };
131
132 int __init atari_joystick_init(void)
133 {
134     joystick[0].active = joystick[1].active = 0;
135     joystick[0].ready = joystick[1].ready = 0;
136     init_waitqueue_head(&joystick[0].wait);
137     init_waitqueue_head(&joystick[1].wait);
138
139     if (devfs_register_chrdev(MAJOR_NR, "Joystick", &atari_joystick_fops))
140         printk("unable to get major %d for joystick devices\n", MAJOR_NR);
141     devfs_register_series (NULL, "joysticks/digital%u", 2, DEVFS_FL_DEFAULT,
142                            MAJOR_NR, 128, S_IFCHR | S_IRUSR | S_IWUSR,
143                            &atari_joystick_fops, NULL);
144
145     return 0;
146 }