treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / input / joystick / stinger.c
blobc20425f52bd8afd233098816fc85fc474f229a83
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2000-2001 Vojtech Pavlik
4 * Copyright (c) 2000 Mark Fletcher
5 */
7 /*
8 * Gravis Stinger gamepad driver for Linux
9 */
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/input.h>
18 #include <linux/serio.h>
20 #define DRIVER_DESC "Gravis Stinger gamepad driver"
22 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
23 MODULE_DESCRIPTION(DRIVER_DESC);
24 MODULE_LICENSE("GPL");
27 * Constants.
30 #define STINGER_MAX_LENGTH 8
33 * Per-Stinger data.
36 struct stinger {
37 struct input_dev *dev;
38 int idx;
39 unsigned char data[STINGER_MAX_LENGTH];
40 char phys[32];
44 * stinger_process_packet() decodes packets the driver receives from the
45 * Stinger. It updates the data accordingly.
48 static void stinger_process_packet(struct stinger *stinger)
50 struct input_dev *dev = stinger->dev;
51 unsigned char *data = stinger->data;
53 if (!stinger->idx) return;
55 input_report_key(dev, BTN_A, ((data[0] & 0x20) >> 5));
56 input_report_key(dev, BTN_B, ((data[0] & 0x10) >> 4));
57 input_report_key(dev, BTN_C, ((data[0] & 0x08) >> 3));
58 input_report_key(dev, BTN_X, ((data[0] & 0x04) >> 2));
59 input_report_key(dev, BTN_Y, ((data[3] & 0x20) >> 5));
60 input_report_key(dev, BTN_Z, ((data[3] & 0x10) >> 4));
61 input_report_key(dev, BTN_TL, ((data[3] & 0x08) >> 3));
62 input_report_key(dev, BTN_TR, ((data[3] & 0x04) >> 2));
63 input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1));
64 input_report_key(dev, BTN_START, (data[3] & 0x01));
66 input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6));
67 input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F));
69 input_sync(dev);
71 return;
75 * stinger_interrupt() is called by the low level driver when characters
76 * are ready for us. We then buffer them for further processing, or call the
77 * packet processing routine.
80 static irqreturn_t stinger_interrupt(struct serio *serio,
81 unsigned char data, unsigned int flags)
83 struct stinger *stinger = serio_get_drvdata(serio);
85 /* All Stinger packets are 4 bytes */
87 if (stinger->idx < STINGER_MAX_LENGTH)
88 stinger->data[stinger->idx++] = data;
90 if (stinger->idx == 4) {
91 stinger_process_packet(stinger);
92 stinger->idx = 0;
95 return IRQ_HANDLED;
99 * stinger_disconnect() is the opposite of stinger_connect()
102 static void stinger_disconnect(struct serio *serio)
104 struct stinger *stinger = serio_get_drvdata(serio);
106 serio_close(serio);
107 serio_set_drvdata(serio, NULL);
108 input_unregister_device(stinger->dev);
109 kfree(stinger);
113 * stinger_connect() is the routine that is called when someone adds a
114 * new serio device that supports Stinger protocol and registers it as
115 * an input device.
118 static int stinger_connect(struct serio *serio, struct serio_driver *drv)
120 struct stinger *stinger;
121 struct input_dev *input_dev;
122 int err = -ENOMEM;
124 stinger = kmalloc(sizeof(struct stinger), GFP_KERNEL);
125 input_dev = input_allocate_device();
126 if (!stinger || !input_dev)
127 goto fail1;
129 stinger->dev = input_dev;
130 snprintf(stinger->phys, sizeof(stinger->phys), "%s/serio0", serio->phys);
132 input_dev->name = "Gravis Stinger";
133 input_dev->phys = stinger->phys;
134 input_dev->id.bustype = BUS_RS232;
135 input_dev->id.vendor = SERIO_STINGER;
136 input_dev->id.product = 0x0001;
137 input_dev->id.version = 0x0100;
138 input_dev->dev.parent = &serio->dev;
140 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
141 input_dev->keybit[BIT_WORD(BTN_A)] = BIT_MASK(BTN_A) | BIT_MASK(BTN_B) |
142 BIT_MASK(BTN_C) | BIT_MASK(BTN_X) | BIT_MASK(BTN_Y) |
143 BIT_MASK(BTN_Z) | BIT_MASK(BTN_TL) | BIT_MASK(BTN_TR) |
144 BIT_MASK(BTN_START) | BIT_MASK(BTN_SELECT);
145 input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4);
146 input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4);
148 serio_set_drvdata(serio, stinger);
150 err = serio_open(serio, drv);
151 if (err)
152 goto fail2;
154 err = input_register_device(stinger->dev);
155 if (err)
156 goto fail3;
158 return 0;
160 fail3: serio_close(serio);
161 fail2: serio_set_drvdata(serio, NULL);
162 fail1: input_free_device(input_dev);
163 kfree(stinger);
164 return err;
168 * The serio driver structure.
171 static const struct serio_device_id stinger_serio_ids[] = {
173 .type = SERIO_RS232,
174 .proto = SERIO_STINGER,
175 .id = SERIO_ANY,
176 .extra = SERIO_ANY,
178 { 0 }
181 MODULE_DEVICE_TABLE(serio, stinger_serio_ids);
183 static struct serio_driver stinger_drv = {
184 .driver = {
185 .name = "stinger",
187 .description = DRIVER_DESC,
188 .id_table = stinger_serio_ids,
189 .interrupt = stinger_interrupt,
190 .connect = stinger_connect,
191 .disconnect = stinger_disconnect,
194 module_serio_driver(stinger_drv);