patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / input / joystick / warrior.c
1 /*
2  * $Id: warrior.c,v 1.14 2002/01/22 20:32:10 vojtech Exp $
3  *
4  *  Copyright (c) 1999-2001 Vojtech Pavlik
5  */
6
7 /*
8  * Logitech WingMan Warrior joystick driver for Linux
9  */
10
11 /*
12  * This program is free warftware; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  *  Should you need to contact me, the author, you can do so either by
27  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
28  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29  */
30
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/input.h>
35 #include <linux/serio.h>
36 #include <linux/init.h>
37
38 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
39 MODULE_DESCRIPTION("Logitech WingMan Warrior joystick driver");
40 MODULE_LICENSE("GPL");
41
42 /*
43  * Constants.
44  */
45
46 #define WARRIOR_MAX_LENGTH      16
47 static char warrior_lengths[] = { 0, 4, 12, 3, 4, 4, 0, 0 };
48 static char *warrior_name = "Logitech WingMan Warrior";
49
50 /*
51  * Per-Warrior data.
52  */
53
54 struct warrior {
55         struct input_dev dev;
56         int idx, len;
57         unsigned char data[WARRIOR_MAX_LENGTH];
58         char phys[32];
59 };
60
61 /*
62  * warrior_process_packet() decodes packets the driver receives from the
63  * Warrior. It updates the data accordingly.
64  */
65
66 static void warrior_process_packet(struct warrior *warrior, struct pt_regs *regs)
67 {
68         struct input_dev *dev = &warrior->dev;
69         unsigned char *data = warrior->data;
70
71         if (!warrior->idx) return;
72
73         input_regs(dev, regs);
74
75         switch ((data[0] >> 4) & 7) {
76                 case 1:                                 /* Button data */
77                         input_report_key(dev, BTN_TRIGGER,  data[3]       & 1);
78                         input_report_key(dev, BTN_THUMB,   (data[3] >> 1) & 1);
79                         input_report_key(dev, BTN_TOP,     (data[3] >> 2) & 1);
80                         input_report_key(dev, BTN_TOP2,    (data[3] >> 3) & 1);
81                         break;
82                 case 3:                                 /* XY-axis info->data */
83                         input_report_abs(dev, ABS_X, ((data[0] & 8) << 5) - (data[2] | ((data[0] & 4) << 5)));
84                         input_report_abs(dev, ABS_Y, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
85                         break;
86                 case 5:                                 /* Throttle, spinner, hat info->data */
87                         input_report_abs(dev, ABS_THROTTLE, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
88                         input_report_abs(dev, ABS_HAT0X, (data[3] & 2 ? 1 : 0) - (data[3] & 1 ? 1 : 0));
89                         input_report_abs(dev, ABS_HAT0Y, (data[3] & 8 ? 1 : 0) - (data[3] & 4 ? 1 : 0));
90                         input_report_rel(dev, REL_DIAL,  (data[2] | ((data[0] & 4) << 5)) - ((data[0] & 8) << 5));
91                         break;
92         }
93         input_sync(dev);
94 }
95
96 /*
97  * warrior_interrupt() is called by the low level driver when characters
98  * are ready for us. We then buffer them for further processing, or call the
99  * packet processing routine.
100  */
101
102 static irqreturn_t warrior_interrupt(struct serio *serio,
103                 unsigned char data, unsigned int flags, struct pt_regs *regs)
104 {
105         struct warrior* warrior = serio->private;
106
107         if (data & 0x80) {
108                 if (warrior->idx) warrior_process_packet(warrior, regs);
109                 warrior->idx = 0;
110                 warrior->len = warrior_lengths[(data >> 4) & 7];
111         }
112
113         if (warrior->idx < warrior->len)
114                 warrior->data[warrior->idx++] = data;
115
116         if (warrior->idx == warrior->len) {
117                 if (warrior->idx) warrior_process_packet(warrior, regs);
118                 warrior->idx = 0;
119                 warrior->len = 0;
120         }
121         return IRQ_HANDLED;
122 }
123
124 /*
125  * warrior_disconnect() is the opposite of warrior_connect()
126  */
127
128 static void warrior_disconnect(struct serio *serio)
129 {
130         struct warrior* warrior = serio->private;
131         input_unregister_device(&warrior->dev);
132         serio_close(serio);
133         kfree(warrior);
134 }
135
136 /*
137  * warrior_connect() is the routine that is called when someone adds a
138  * new serio device. It looks for the Warrior, and if found, registers
139  * it as an input device.
140  */
141
142 static void warrior_connect(struct serio *serio, struct serio_dev *dev)
143 {
144         struct warrior *warrior;
145         int i;
146
147         if (serio->type != (SERIO_RS232 | SERIO_WARRIOR))
148                 return;
149
150         if (!(warrior = kmalloc(sizeof(struct warrior), GFP_KERNEL)))
151                 return;
152
153         memset(warrior, 0, sizeof(struct warrior));
154
155         warrior->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL) | BIT(EV_ABS);
156         warrior->dev.keybit[LONG(BTN_TRIGGER)] = BIT(BTN_TRIGGER) | BIT(BTN_THUMB) | BIT(BTN_TOP) | BIT(BTN_TOP2);
157         warrior->dev.relbit[0] = BIT(REL_DIAL);
158         warrior->dev.absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_THROTTLE) | BIT(ABS_HAT0X) | BIT(ABS_HAT0Y);
159
160         sprintf(warrior->phys, "%s/input0", serio->phys);
161
162         init_input_dev(&warrior->dev);
163         warrior->dev.name = warrior_name;
164         warrior->dev.phys = warrior->phys;
165         warrior->dev.id.bustype = BUS_RS232;
166         warrior->dev.id.vendor = SERIO_WARRIOR;
167         warrior->dev.id.product = 0x0001;
168         warrior->dev.id.version = 0x0100;
169
170         for (i = 0; i < 2; i++) {
171                 warrior->dev.absmax[ABS_X+i] = -64;
172                 warrior->dev.absmin[ABS_X+i] =  64;
173                 warrior->dev.absflat[ABS_X+i] = 8;
174         }
175
176         warrior->dev.absmax[ABS_THROTTLE] = -112;
177         warrior->dev.absmin[ABS_THROTTLE] =  112;
178
179         for (i = 0; i < 2; i++) {
180                 warrior->dev.absmax[ABS_HAT0X+i] = -1;
181                 warrior->dev.absmin[ABS_HAT0X+i] =  1;
182         }
183
184         warrior->dev.private = warrior;
185
186         serio->private = warrior;
187
188         if (serio_open(serio, dev)) {
189                 kfree(warrior);
190                 return;
191         }
192
193         input_register_device(&warrior->dev);
194
195         printk(KERN_INFO "input: Logitech WingMan Warrior on %s\n", serio->phys);
196 }
197
198 /*
199  * The serio device structure.
200  */
201
202 static struct serio_dev warrior_dev = {
203         .interrupt =    warrior_interrupt,
204         .connect =      warrior_connect,
205         .disconnect =   warrior_disconnect,
206 };
207
208 /*
209  * The functions for inserting/removing us as a module.
210  */
211
212 int __init warrior_init(void)
213 {
214         serio_register_device(&warrior_dev);
215         return 0;
216 }
217
218 void __exit warrior_exit(void)
219 {
220         serio_unregister_device(&warrior_dev);
221 }
222
223 module_init(warrior_init);
224 module_exit(warrior_exit);