File: /usr/src/linux/drivers/char/joystick/warrior.c

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