Merge rsync://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-drm-fsl-dcu.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 #define DRIVER_DESC     "Logitech WingMan Warrior joystick driver"
39
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION(DRIVER_DESC);
42 MODULE_LICENSE("GPL");
43
44 /*
45  * Constants.
46  */
47
48 #define WARRIOR_MAX_LENGTH      16
49 static char warrior_lengths[] = { 0, 4, 12, 3, 4, 4, 0, 0 };
50
51 /*
52  * Per-Warrior data.
53  */
54
55 struct warrior {
56         struct input_dev *dev;
57         int idx, len;
58         unsigned char data[WARRIOR_MAX_LENGTH];
59         char phys[32];
60 };
61
62 /*
63  * warrior_process_packet() decodes packets the driver receives from the
64  * Warrior. It updates the data accordingly.
65  */
66
67 static void warrior_process_packet(struct warrior *warrior, struct pt_regs *regs)
68 {
69         struct input_dev *dev = warrior->dev;
70         unsigned char *data = warrior->data;
71
72         if (!warrior->idx) return;
73
74         input_regs(dev, regs);
75
76         switch ((data[0] >> 4) & 7) {
77                 case 1:                                 /* Button data */
78                         input_report_key(dev, BTN_TRIGGER,  data[3]       & 1);
79                         input_report_key(dev, BTN_THUMB,   (data[3] >> 1) & 1);
80                         input_report_key(dev, BTN_TOP,     (data[3] >> 2) & 1);
81                         input_report_key(dev, BTN_TOP2,    (data[3] >> 3) & 1);
82                         break;
83                 case 3:                                 /* XY-axis info->data */
84                         input_report_abs(dev, ABS_X, ((data[0] & 8) << 5) - (data[2] | ((data[0] & 4) << 5)));
85                         input_report_abs(dev, ABS_Y, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
86                         break;
87                 case 5:                                 /* Throttle, spinner, hat info->data */
88                         input_report_abs(dev, ABS_THROTTLE, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
89                         input_report_abs(dev, ABS_HAT0X, (data[3] & 2 ? 1 : 0) - (data[3] & 1 ? 1 : 0));
90                         input_report_abs(dev, ABS_HAT0Y, (data[3] & 8 ? 1 : 0) - (data[3] & 4 ? 1 : 0));
91                         input_report_rel(dev, REL_DIAL,  (data[2] | ((data[0] & 4) << 5)) - ((data[0] & 8) << 5));
92                         break;
93         }
94         input_sync(dev);
95 }
96
97 /*
98  * warrior_interrupt() is called by the low level driver when characters
99  * are ready for us. We then buffer them for further processing, or call the
100  * packet processing routine.
101  */
102
103 static irqreturn_t warrior_interrupt(struct serio *serio,
104                 unsigned char data, unsigned int flags, struct pt_regs *regs)
105 {
106         struct warrior *warrior = serio_get_drvdata(serio);
107
108         if (data & 0x80) {
109                 if (warrior->idx) warrior_process_packet(warrior, regs);
110                 warrior->idx = 0;
111                 warrior->len = warrior_lengths[(data >> 4) & 7];
112         }
113
114         if (warrior->idx < warrior->len)
115                 warrior->data[warrior->idx++] = data;
116
117         if (warrior->idx == warrior->len) {
118                 if (warrior->idx) warrior_process_packet(warrior, regs);
119                 warrior->idx = 0;
120                 warrior->len = 0;
121         }
122         return IRQ_HANDLED;
123 }
124
125 /*
126  * warrior_disconnect() is the opposite of warrior_connect()
127  */
128
129 static void warrior_disconnect(struct serio *serio)
130 {
131         struct warrior *warrior = serio_get_drvdata(serio);
132
133         serio_close(serio);
134         serio_set_drvdata(serio, NULL);
135         input_unregister_device(warrior->dev);
136         kfree(warrior);
137 }
138
139 /*
140  * warrior_connect() is the routine that is called when someone adds a
141  * new serio device. It looks for the Warrior, and if found, registers
142  * it as an input device.
143  */
144
145 static int warrior_connect(struct serio *serio, struct serio_driver *drv)
146 {
147         struct warrior *warrior;
148         struct input_dev *input_dev;
149         int err = -ENOMEM;
150
151         warrior = kzalloc(sizeof(struct warrior), GFP_KERNEL);
152         input_dev = input_allocate_device();
153         if (!warrior || !input_dev)
154                 goto fail;
155
156         warrior->dev = input_dev;
157         snprintf(warrior->phys, sizeof(warrior->phys), "%s/input0", serio->phys);
158
159         input_dev->name = "Logitech WingMan Warrior";
160         input_dev->phys = warrior->phys;
161         input_dev->id.bustype = BUS_RS232;
162         input_dev->id.vendor = SERIO_WARRIOR;
163         input_dev->id.product = 0x0001;
164         input_dev->id.version = 0x0100;
165         input_dev->cdev.dev = &serio->dev;
166         input_dev->private = warrior;
167
168         input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL) | BIT(EV_ABS);
169         input_dev->keybit[LONG(BTN_TRIGGER)] = BIT(BTN_TRIGGER) | BIT(BTN_THUMB) | BIT(BTN_TOP) | BIT(BTN_TOP2);
170         input_dev->relbit[0] = BIT(REL_DIAL);
171         input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 8);
172         input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 8);
173         input_set_abs_params(input_dev, ABS_THROTTLE, -112, 112, 0, 0);
174         input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
175         input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
176
177         serio_set_drvdata(serio, warrior);
178
179         err = serio_open(serio, drv);
180         if (err)
181                 goto fail;
182
183         input_register_device(warrior->dev);
184         return 0;
185
186  fail:  serio_set_drvdata(serio, NULL);
187         input_free_device(input_dev);
188         kfree(warrior);
189         return err;
190 }
191
192 /*
193  * The serio driver structure.
194  */
195
196 static struct serio_device_id warrior_serio_ids[] = {
197         {
198                 .type   = SERIO_RS232,
199                 .proto  = SERIO_WARRIOR,
200                 .id     = SERIO_ANY,
201                 .extra  = SERIO_ANY,
202         },
203         { 0 }
204 };
205
206 MODULE_DEVICE_TABLE(serio, warrior_serio_ids);
207
208 static struct serio_driver warrior_drv = {
209         .driver         = {
210                 .name   = "warrior",
211         },
212         .description    = DRIVER_DESC,
213         .id_table       = warrior_serio_ids,
214         .interrupt      = warrior_interrupt,
215         .connect        = warrior_connect,
216         .disconnect     = warrior_disconnect,
217 };
218
219 /*
220  * The functions for inserting/removing us as a module.
221  */
222
223 static int __init warrior_init(void)
224 {
225         serio_register_driver(&warrior_drv);
226         return 0;
227 }
228
229 static void __exit warrior_exit(void)
230 {
231         serio_unregister_driver(&warrior_drv);
232 }
233
234 module_init(warrior_init);
235 module_exit(warrior_exit);