blob: f72c83e15e6020ddfa288558011ef8918efaa3d3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 1999-2001 Vojtech Pavlik
3 */
4
5/*
6 * Logitech WingMan Warrior joystick driver for Linux
7 */
8
9/*
10 * This program is free warftware; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
27 */
28
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/slab.h>
32#include <linux/input.h>
33#include <linux/serio.h>
34#include <linux/init.h>
35
36#define DRIVER_DESC "Logitech WingMan Warrior joystick driver"
37
38MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
39MODULE_DESCRIPTION(DRIVER_DESC);
40MODULE_LICENSE("GPL");
41
42/*
43 * Constants.
44 */
45
46#define WARRIOR_MAX_LENGTH 16
47static char warrior_lengths[] = { 0, 4, 12, 3, 4, 4, 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * Per-Warrior data.
51 */
52
53struct warrior {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050054 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int idx, len;
56 unsigned char data[WARRIOR_MAX_LENGTH];
57 char phys[32];
58};
59
60/*
61 * warrior_process_packet() decodes packets the driver receives from the
62 * Warrior. It updates the data accordingly.
63 */
64
David Howells7d12e782006-10-05 14:55:46 +010065static void warrior_process_packet(struct warrior *warrior)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050067 struct input_dev *dev = warrior->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned char *data = warrior->data;
69
70 if (!warrior->idx) return;
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 switch ((data[0] >> 4) & 7) {
73 case 1: /* Button data */
74 input_report_key(dev, BTN_TRIGGER, data[3] & 1);
75 input_report_key(dev, BTN_THUMB, (data[3] >> 1) & 1);
76 input_report_key(dev, BTN_TOP, (data[3] >> 2) & 1);
77 input_report_key(dev, BTN_TOP2, (data[3] >> 3) & 1);
78 break;
79 case 3: /* XY-axis info->data */
80 input_report_abs(dev, ABS_X, ((data[0] & 8) << 5) - (data[2] | ((data[0] & 4) << 5)));
81 input_report_abs(dev, ABS_Y, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
82 break;
83 case 5: /* Throttle, spinner, hat info->data */
84 input_report_abs(dev, ABS_THROTTLE, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
85 input_report_abs(dev, ABS_HAT0X, (data[3] & 2 ? 1 : 0) - (data[3] & 1 ? 1 : 0));
86 input_report_abs(dev, ABS_HAT0Y, (data[3] & 8 ? 1 : 0) - (data[3] & 4 ? 1 : 0));
87 input_report_rel(dev, REL_DIAL, (data[2] | ((data[0] & 4) << 5)) - ((data[0] & 8) << 5));
88 break;
89 }
90 input_sync(dev);
91}
92
93/*
94 * warrior_interrupt() is called by the low level driver when characters
95 * are ready for us. We then buffer them for further processing, or call the
96 * packet processing routine.
97 */
98
99static irqreturn_t warrior_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100100 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct warrior *warrior = serio_get_drvdata(serio);
103
104 if (data & 0x80) {
David Howells7d12e782006-10-05 14:55:46 +0100105 if (warrior->idx) warrior_process_packet(warrior);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 warrior->idx = 0;
107 warrior->len = warrior_lengths[(data >> 4) & 7];
108 }
109
110 if (warrior->idx < warrior->len)
111 warrior->data[warrior->idx++] = data;
112
113 if (warrior->idx == warrior->len) {
David Howells7d12e782006-10-05 14:55:46 +0100114 if (warrior->idx) warrior_process_packet(warrior);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 warrior->idx = 0;
116 warrior->len = 0;
117 }
118 return IRQ_HANDLED;
119}
120
121/*
122 * warrior_disconnect() is the opposite of warrior_connect()
123 */
124
125static void warrior_disconnect(struct serio *serio)
126{
127 struct warrior *warrior = serio_get_drvdata(serio);
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 serio_close(serio);
130 serio_set_drvdata(serio, NULL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500131 input_unregister_device(warrior->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 kfree(warrior);
133}
134
135/*
136 * warrior_connect() is the routine that is called when someone adds a
137 * new serio device. It looks for the Warrior, and if found, registers
138 * it as an input device.
139 */
140
141static int warrior_connect(struct serio *serio, struct serio_driver *drv)
142{
143 struct warrior *warrior;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500144 struct input_dev *input_dev;
145 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500147 warrior = kzalloc(sizeof(struct warrior), GFP_KERNEL);
148 input_dev = input_allocate_device();
149 if (!warrior || !input_dev)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500150 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500152 warrior->dev = input_dev;
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400153 snprintf(warrior->phys, sizeof(warrior->phys), "%s/input0", serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500155 input_dev->name = "Logitech WingMan Warrior";
156 input_dev->phys = warrior->phys;
157 input_dev->id.bustype = BUS_RS232;
158 input_dev->id.vendor = SERIO_WARRIOR;
159 input_dev->id.product = 0x0001;
160 input_dev->id.version = 0x0100;
Dmitry Torokhov935e6582007-04-12 01:35:26 -0400161 input_dev->dev.parent = &serio->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700163 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) |
164 BIT_MASK(EV_ABS);
165 input_dev->keybit[BIT_WORD(BTN_TRIGGER)] = BIT_MASK(BTN_TRIGGER) |
166 BIT_MASK(BTN_THUMB) | BIT_MASK(BTN_TOP) | BIT_MASK(BTN_TOP2);
167 input_dev->relbit[0] = BIT_MASK(REL_DIAL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500168 input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 8);
169 input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 8);
170 input_set_abs_params(input_dev, ABS_THROTTLE, -112, 112, 0, 0);
171 input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
Dmitry Torokhovae5536d2005-12-29 22:19:08 -0500172 input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 serio_set_drvdata(serio, warrior);
175
176 err = serio_open(serio, drv);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500177 if (err)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500178 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500180 err = input_register_device(warrior->dev);
181 if (err)
182 goto fail3;
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500185
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500186 fail3: serio_close(serio);
187 fail2: serio_set_drvdata(serio, NULL);
188 fail1: input_free_device(input_dev);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500189 kfree(warrior);
190 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193/*
194 * The serio driver structure.
195 */
196
197static struct serio_device_id warrior_serio_ids[] = {
198 {
199 .type = SERIO_RS232,
200 .proto = SERIO_WARRIOR,
201 .id = SERIO_ANY,
202 .extra = SERIO_ANY,
203 },
204 { 0 }
205};
206
207MODULE_DEVICE_TABLE(serio, warrior_serio_ids);
208
209static struct serio_driver warrior_drv = {
210 .driver = {
211 .name = "warrior",
212 },
213 .description = DRIVER_DESC,
214 .id_table = warrior_serio_ids,
215 .interrupt = warrior_interrupt,
216 .connect = warrior_connect,
217 .disconnect = warrior_disconnect,
218};
219
220/*
221 * The functions for inserting/removing us as a module.
222 */
223
224static int __init warrior_init(void)
225{
Akinobu Mita153a9df02006-11-23 23:35:10 -0500226 return serio_register_driver(&warrior_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229static void __exit warrior_exit(void)
230{
231 serio_unregister_driver(&warrior_drv);
232}
233
234module_init(warrior_init);
235module_exit(warrior_exit);