1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 1999-2001 Vojtech Pavlik
7 * Logitech WingMan Warrior joystick driver for Linux
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/input.h>
14 #include <linux/serio.h>
16 #define DRIVER_DESC "Logitech WingMan Warrior joystick driver"
19 MODULE_DESCRIPTION(DRIVER_DESC);
20 MODULE_LICENSE("GPL");
26 #define WARRIOR_MAX_LENGTH 16
27 static char warrior_lengths[] = { 0, 4, 12, 3, 4, 4, 0, 0 };
34 struct input_dev *dev;
36 unsigned char data[WARRIOR_MAX_LENGTH];
41 * warrior_process_packet() decodes packets the driver receives from the
42 * Warrior. It updates the data accordingly.
45 static void warrior_process_packet(struct warrior *warrior)
47 struct input_dev *dev = warrior->dev;
48 unsigned char *data = warrior->data;
50 if (!warrior->idx) return;
52 switch ((data[0] >> 4) & 7) {
53 case 1: /* Button data */
54 input_report_key(dev, BTN_TRIGGER, data[3] & 1);
55 input_report_key(dev, BTN_THUMB, (data[3] >> 1) & 1);
56 input_report_key(dev, BTN_TOP, (data[3] >> 2) & 1);
57 input_report_key(dev, BTN_TOP2, (data[3] >> 3) & 1);
59 case 3: /* XY-axis info->data */
60 input_report_abs(dev, ABS_X, ((data[0] & 8) << 5) - (data[2] | ((data[0] & 4) << 5)));
61 input_report_abs(dev, ABS_Y, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
63 case 5: /* Throttle, spinner, hat info->data */
64 input_report_abs(dev, ABS_THROTTLE, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
65 input_report_abs(dev, ABS_HAT0X, (data[3] & 2 ? 1 : 0) - (data[3] & 1 ? 1 : 0));
66 input_report_abs(dev, ABS_HAT0Y, (data[3] & 8 ? 1 : 0) - (data[3] & 4 ? 1 : 0));
67 input_report_rel(dev, REL_DIAL, (data[2] | ((data[0] & 4) << 5)) - ((data[0] & 8) << 5));
74 * warrior_interrupt() is called by the low level driver when characters
75 * are ready for us. We then buffer them for further processing, or call the
76 * packet processing routine.
79 static irqreturn_t warrior_interrupt(struct serio *serio,
80 unsigned char data, unsigned int flags)
82 struct warrior *warrior = serio_get_drvdata(serio);
85 if (warrior->idx) warrior_process_packet(warrior);
87 warrior->len = warrior_lengths[(data >> 4) & 7];
90 if (warrior->idx < warrior->len)
91 warrior->data[warrior->idx++] = data;
93 if (warrior->idx == warrior->len) {
94 if (warrior->idx) warrior_process_packet(warrior);
102 * warrior_disconnect() is the opposite of warrior_connect()
105 static void warrior_disconnect(struct serio *serio)
107 struct warrior *warrior = serio_get_drvdata(serio);
110 serio_set_drvdata(serio, NULL);
111 input_unregister_device(warrior->dev);
116 * warrior_connect() is the routine that is called when someone adds a
117 * new serio device. It looks for the Warrior, and if found, registers
118 * it as an input device.
121 static int warrior_connect(struct serio *serio, struct serio_driver *drv)
123 struct warrior *warrior;
124 struct input_dev *input_dev;
127 warrior = kzalloc(sizeof(struct warrior), GFP_KERNEL);
128 input_dev = input_allocate_device();
129 if (!warrior || !input_dev)
132 warrior->dev = input_dev;
133 snprintf(warrior->phys, sizeof(warrior->phys), "%s/input0", serio->phys);
135 input_dev->name = "Logitech WingMan Warrior";
136 input_dev->phys = warrior->phys;
137 input_dev->id.bustype = BUS_RS232;
138 input_dev->id.vendor = SERIO_WARRIOR;
139 input_dev->id.product = 0x0001;
140 input_dev->id.version = 0x0100;
141 input_dev->dev.parent = &serio->dev;
143 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) |
145 input_dev->keybit[BIT_WORD(BTN_TRIGGER)] = BIT_MASK(BTN_TRIGGER) |
146 BIT_MASK(BTN_THUMB) | BIT_MASK(BTN_TOP) | BIT_MASK(BTN_TOP2);
147 input_dev->relbit[0] = BIT_MASK(REL_DIAL);
148 input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 8);
149 input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 8);
150 input_set_abs_params(input_dev, ABS_THROTTLE, -112, 112, 0, 0);
151 input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
152 input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
154 serio_set_drvdata(serio, warrior);
156 err = serio_open(serio, drv);
160 err = input_register_device(warrior->dev);
166 fail3: serio_close(serio);
167 fail2: serio_set_drvdata(serio, NULL);
168 fail1: input_free_device(input_dev);
174 * The serio driver structure.
177 static const struct serio_device_id warrior_serio_ids[] = {
180 .proto = SERIO_WARRIOR,
187 MODULE_DEVICE_TABLE(serio, warrior_serio_ids);
189 static struct serio_driver warrior_drv = {
193 .description = DRIVER_DESC,
194 .id_table = warrior_serio_ids,
195 .interrupt = warrior_interrupt,
196 .connect = warrior_connect,
197 .disconnect = warrior_disconnect,
200 module_serio_driver(warrior_drv);