2 * Atari Joystick Driver for Linux
3 * by Robert de Vries (robert@and.nl) 19Jul93
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
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>
18 #include <asm/atarikb.h>
19 #include <asm/atari_joystick.h>
20 #include <asm/uaccess.h>
22 #define MAJOR_NR JOYSTICK_MAJOR
24 #define ANALOG_JOY(n) (!(n & 0x80))
25 #define DIGITAL_JOY(n) (n & 0x80)
26 #define DEVICE_NR(n) (MINOR(n) & 0x7f)
29 static struct joystick_status joystick[2];
30 int atari_mouse_buttons; /* for three-button mouse */
32 void atari_joystick_interrupt(char *buf)
35 /* ikbd_joystick_disable(); */
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);
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))
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);
54 atari_mouse_interrupt_hook (faked_packet);
57 /* ikbd_joystick_event_on(); */
60 static int release_joystick(struct inode *inode, struct file *file)
62 int minor = DEVICE_NR(inode->i_rdev);
65 joystick[minor].active = 0;
66 joystick[minor].ready = 0;
68 if ((joystick[0].active == 0) && (joystick[1].active == 0))
69 ikbd_joystick_disable();
74 static int open_joystick(struct inode *inode, struct file *file)
76 int minor = DEVICE_NR(inode->i_rdev);
78 if (!DIGITAL_JOY(inode->i_rdev) || minor > 1)
80 if (joystick[minor].active)
82 joystick[minor].active = 1;
83 joystick[minor].ready = 0;
84 ikbd_joystick_event_on();
88 static ssize_t write_joystick(struct file *file, const char *buffer,
89 size_t count, loff_t *ppos)
94 static ssize_t read_joystick(struct file *file, char *buffer, size_t count,
97 struct inode *inode = file->f_dentry->d_inode;
98 int minor = DEVICE_NR(inode->i_rdev);
102 if (!joystick[minor].ready)
104 joystick[minor].ready = 0;
105 if (put_user(joystick[minor].fire, buffer++) ||
106 put_user(joystick[minor].dir, buffer++))
109 if (clear_user(buffer, count - 2))
114 static unsigned int joystick_poll(struct file *file, poll_table *wait)
116 int minor = DEVICE_NR(file->f_dentry->d_inode->i_rdev);
118 poll_wait(file, &joystick[minor].wait, wait);
119 if (joystick[minor].ready)
120 return POLLIN | POLLRDNORM;
124 struct file_operations atari_joystick_fops = {
126 write: write_joystick,
129 release: release_joystick,
132 int __init atari_joystick_init(void)
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);
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);