MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / input / joystick / warrior.c
blob5f19b2a51f27dacf1d067110c4d196149f295892
1 /*
2 * $Id: warrior.c,v 1.14 2002/01/22 20:32:10 vojtech Exp $
4 * Copyright (c) 1999-2001 Vojtech Pavlik
5 */
7 /*
8 * Logitech WingMan Warrior joystick driver for Linux
9 */
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.
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.
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
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
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>
38 #define DRIVER_DESC "Logitech WingMan Warrior joystick driver"
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION(DRIVER_DESC);
42 MODULE_LICENSE("GPL");
45 * Constants.
48 #define WARRIOR_MAX_LENGTH 16
49 static char warrior_lengths[] = { 0, 4, 12, 3, 4, 4, 0, 0 };
50 static char *warrior_name = "Logitech WingMan Warrior";
53 * Per-Warrior data.
56 struct warrior {
57 struct input_dev dev;
58 int idx, len;
59 unsigned char data[WARRIOR_MAX_LENGTH];
60 char phys[32];
64 * warrior_process_packet() decodes packets the driver receives from the
65 * Warrior. It updates the data accordingly.
68 static void warrior_process_packet(struct warrior *warrior, struct pt_regs *regs)
70 struct input_dev *dev = &warrior->dev;
71 unsigned char *data = warrior->data;
73 if (!warrior->idx) return;
75 input_regs(dev, regs);
77 switch ((data[0] >> 4) & 7) {
78 case 1: /* Button data */
79 input_report_key(dev, BTN_TRIGGER, data[3] & 1);
80 input_report_key(dev, BTN_THUMB, (data[3] >> 1) & 1);
81 input_report_key(dev, BTN_TOP, (data[3] >> 2) & 1);
82 input_report_key(dev, BTN_TOP2, (data[3] >> 3) & 1);
83 break;
84 case 3: /* XY-axis info->data */
85 input_report_abs(dev, ABS_X, ((data[0] & 8) << 5) - (data[2] | ((data[0] & 4) << 5)));
86 input_report_abs(dev, ABS_Y, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
87 break;
88 case 5: /* Throttle, spinner, hat info->data */
89 input_report_abs(dev, ABS_THROTTLE, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
90 input_report_abs(dev, ABS_HAT0X, (data[3] & 2 ? 1 : 0) - (data[3] & 1 ? 1 : 0));
91 input_report_abs(dev, ABS_HAT0Y, (data[3] & 8 ? 1 : 0) - (data[3] & 4 ? 1 : 0));
92 input_report_rel(dev, REL_DIAL, (data[2] | ((data[0] & 4) << 5)) - ((data[0] & 8) << 5));
93 break;
95 input_sync(dev);
99 * warrior_interrupt() is called by the low level driver when characters
100 * are ready for us. We then buffer them for further processing, or call the
101 * packet processing routine.
104 static irqreturn_t warrior_interrupt(struct serio *serio,
105 unsigned char data, unsigned int flags, struct pt_regs *regs)
107 struct warrior* warrior = serio->private;
109 if (data & 0x80) {
110 if (warrior->idx) warrior_process_packet(warrior, regs);
111 warrior->idx = 0;
112 warrior->len = warrior_lengths[(data >> 4) & 7];
115 if (warrior->idx < warrior->len)
116 warrior->data[warrior->idx++] = data;
118 if (warrior->idx == warrior->len) {
119 if (warrior->idx) warrior_process_packet(warrior, regs);
120 warrior->idx = 0;
121 warrior->len = 0;
123 return IRQ_HANDLED;
127 * warrior_disconnect() is the opposite of warrior_connect()
130 static void warrior_disconnect(struct serio *serio)
132 struct warrior* warrior = serio->private;
133 input_unregister_device(&warrior->dev);
134 serio_close(serio);
135 kfree(warrior);
139 * warrior_connect() is the routine that is called when someone adds a
140 * new serio device. It looks for the Warrior, and if found, registers
141 * it as an input device.
144 static void warrior_connect(struct serio *serio, struct serio_driver *drv)
146 struct warrior *warrior;
147 int i;
149 if (serio->type != (SERIO_RS232 | SERIO_WARRIOR))
150 return;
152 if (!(warrior = kmalloc(sizeof(struct warrior), GFP_KERNEL)))
153 return;
155 memset(warrior, 0, sizeof(struct warrior));
157 warrior->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL) | BIT(EV_ABS);
158 warrior->dev.keybit[LONG(BTN_TRIGGER)] = BIT(BTN_TRIGGER) | BIT(BTN_THUMB) | BIT(BTN_TOP) | BIT(BTN_TOP2);
159 warrior->dev.relbit[0] = BIT(REL_DIAL);
160 warrior->dev.absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_THROTTLE) | BIT(ABS_HAT0X) | BIT(ABS_HAT0Y);
162 sprintf(warrior->phys, "%s/input0", serio->phys);
164 init_input_dev(&warrior->dev);
165 warrior->dev.name = warrior_name;
166 warrior->dev.phys = warrior->phys;
167 warrior->dev.id.bustype = BUS_RS232;
168 warrior->dev.id.vendor = SERIO_WARRIOR;
169 warrior->dev.id.product = 0x0001;
170 warrior->dev.id.version = 0x0100;
172 for (i = 0; i < 2; i++) {
173 warrior->dev.absmax[ABS_X+i] = -64;
174 warrior->dev.absmin[ABS_X+i] = 64;
175 warrior->dev.absflat[ABS_X+i] = 8;
178 warrior->dev.absmax[ABS_THROTTLE] = -112;
179 warrior->dev.absmin[ABS_THROTTLE] = 112;
181 for (i = 0; i < 2; i++) {
182 warrior->dev.absmax[ABS_HAT0X+i] = -1;
183 warrior->dev.absmin[ABS_HAT0X+i] = 1;
186 warrior->dev.private = warrior;
188 serio->private = warrior;
190 if (serio_open(serio, drv)) {
191 kfree(warrior);
192 return;
195 input_register_device(&warrior->dev);
197 printk(KERN_INFO "input: Logitech WingMan Warrior on %s\n", serio->phys);
201 * The serio device structure.
204 static struct serio_driver warrior_drv = {
205 .driver = {
206 .name = "warrior",
208 .description = DRIVER_DESC,
209 .interrupt = warrior_interrupt,
210 .connect = warrior_connect,
211 .disconnect = warrior_disconnect,
215 * The functions for inserting/removing us as a module.
218 int __init warrior_init(void)
220 serio_register_driver(&warrior_drv);
221 return 0;
224 void __exit warrior_exit(void)
226 serio_unregister_driver(&warrior_drv);
229 module_init(warrior_init);
230 module_exit(warrior_exit);