blob: f66bddf145c22af99f8481577b28cefaa4ff9841 [file] [log] [blame]
Thomas Gleixnere8fc98282019-05-20 19:07:56 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (c) 1999-2001 Vojtech Pavlik
4 */
5
6/*
7 * Logitech WingMan Warrior joystick driver for Linux
8 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/input.h>
14#include <linux/serio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#define DRIVER_DESC "Logitech WingMan Warrior joystick driver"
17
18MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
19MODULE_DESCRIPTION(DRIVER_DESC);
20MODULE_LICENSE("GPL");
21
22/*
23 * Constants.
24 */
25
26#define WARRIOR_MAX_LENGTH 16
27static char warrior_lengths[] = { 0, 4, 12, 3, 4, 4, 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * Per-Warrior data.
31 */
32
33struct warrior {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050034 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 int idx, len;
36 unsigned char data[WARRIOR_MAX_LENGTH];
37 char phys[32];
38};
39
40/*
41 * warrior_process_packet() decodes packets the driver receives from the
42 * Warrior. It updates the data accordingly.
43 */
44
David Howells7d12e782006-10-05 14:55:46 +010045static void warrior_process_packet(struct warrior *warrior)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050047 struct input_dev *dev = warrior->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 unsigned char *data = warrior->data;
49
50 if (!warrior->idx) return;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 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);
58 break;
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));
62 break;
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));
68 break;
69 }
70 input_sync(dev);
71}
72
73/*
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.
77 */
78
79static irqreturn_t warrior_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +010080 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct warrior *warrior = serio_get_drvdata(serio);
83
84 if (data & 0x80) {
David Howells7d12e782006-10-05 14:55:46 +010085 if (warrior->idx) warrior_process_packet(warrior);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 warrior->idx = 0;
87 warrior->len = warrior_lengths[(data >> 4) & 7];
88 }
89
90 if (warrior->idx < warrior->len)
91 warrior->data[warrior->idx++] = data;
92
93 if (warrior->idx == warrior->len) {
David Howells7d12e782006-10-05 14:55:46 +010094 if (warrior->idx) warrior_process_packet(warrior);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 warrior->idx = 0;
96 warrior->len = 0;
97 }
98 return IRQ_HANDLED;
99}
100
101/*
102 * warrior_disconnect() is the opposite of warrior_connect()
103 */
104
105static void warrior_disconnect(struct serio *serio)
106{
107 struct warrior *warrior = serio_get_drvdata(serio);
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 serio_close(serio);
110 serio_set_drvdata(serio, NULL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500111 input_unregister_device(warrior->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 kfree(warrior);
113}
114
115/*
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.
119 */
120
121static int warrior_connect(struct serio *serio, struct serio_driver *drv)
122{
123 struct warrior *warrior;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500124 struct input_dev *input_dev;
125 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500127 warrior = kzalloc(sizeof(struct warrior), GFP_KERNEL);
128 input_dev = input_allocate_device();
129 if (!warrior || !input_dev)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500130 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500132 warrior->dev = input_dev;
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400133 snprintf(warrior->phys, sizeof(warrior->phys), "%s/input0", serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500135 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;
Dmitry Torokhov935e6582007-04-12 01:35:26 -0400141 input_dev->dev.parent = &serio->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700143 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) |
144 BIT_MASK(EV_ABS);
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);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500148 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);
Dmitry Torokhovae5536d2005-12-29 22:19:08 -0500152 input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 serio_set_drvdata(serio, warrior);
155
156 err = serio_open(serio, drv);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500157 if (err)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500158 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500160 err = input_register_device(warrior->dev);
161 if (err)
162 goto fail3;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500165
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500166 fail3: serio_close(serio);
167 fail2: serio_set_drvdata(serio, NULL);
168 fail1: input_free_device(input_dev);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500169 kfree(warrior);
170 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/*
174 * The serio driver structure.
175 */
176
Arvind Yadavbee186d2017-08-18 17:08:52 -0700177static const struct serio_device_id warrior_serio_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 {
179 .type = SERIO_RS232,
180 .proto = SERIO_WARRIOR,
181 .id = SERIO_ANY,
182 .extra = SERIO_ANY,
183 },
184 { 0 }
185};
186
187MODULE_DEVICE_TABLE(serio, warrior_serio_ids);
188
189static struct serio_driver warrior_drv = {
190 .driver = {
191 .name = "warrior",
192 },
193 .description = DRIVER_DESC,
194 .id_table = warrior_serio_ids,
195 .interrupt = warrior_interrupt,
196 .connect = warrior_connect,
197 .disconnect = warrior_disconnect,
198};
199
Axel Lin65ac9f7a2012-04-03 23:50:17 -0700200module_serio_driver(warrior_drv);