1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2000-2001 Vojtech Pavlik
4 * Copyright (c) 2000 Mark Fletcher
8 * Gravis Stinger gamepad driver for Linux
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/input.h>
15 #include <linux/serio.h>
17 #define DRIVER_DESC "Gravis Stinger gamepad driver"
19 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
20 MODULE_DESCRIPTION(DRIVER_DESC
);
21 MODULE_LICENSE("GPL");
27 #define STINGER_MAX_LENGTH 8
34 struct input_dev
*dev
;
36 unsigned char data
[STINGER_MAX_LENGTH
];
41 * stinger_process_packet() decodes packets the driver receives from the
42 * Stinger. It updates the data accordingly.
45 static void stinger_process_packet(struct stinger
*stinger
)
47 struct input_dev
*dev
= stinger
->dev
;
48 unsigned char *data
= stinger
->data
;
50 if (!stinger
->idx
) return;
52 input_report_key(dev
, BTN_A
, ((data
[0] & 0x20) >> 5));
53 input_report_key(dev
, BTN_B
, ((data
[0] & 0x10) >> 4));
54 input_report_key(dev
, BTN_C
, ((data
[0] & 0x08) >> 3));
55 input_report_key(dev
, BTN_X
, ((data
[0] & 0x04) >> 2));
56 input_report_key(dev
, BTN_Y
, ((data
[3] & 0x20) >> 5));
57 input_report_key(dev
, BTN_Z
, ((data
[3] & 0x10) >> 4));
58 input_report_key(dev
, BTN_TL
, ((data
[3] & 0x08) >> 3));
59 input_report_key(dev
, BTN_TR
, ((data
[3] & 0x04) >> 2));
60 input_report_key(dev
, BTN_SELECT
, ((data
[3] & 0x02) >> 1));
61 input_report_key(dev
, BTN_START
, (data
[3] & 0x01));
63 input_report_abs(dev
, ABS_X
, (data
[1] & 0x3F) - ((data
[0] & 0x01) << 6));
64 input_report_abs(dev
, ABS_Y
, ((data
[0] & 0x02) << 5) - (data
[2] & 0x3F));
72 * stinger_interrupt() is called by the low level driver when characters
73 * are ready for us. We then buffer them for further processing, or call the
74 * packet processing routine.
77 static irqreturn_t
stinger_interrupt(struct serio
*serio
,
78 unsigned char data
, unsigned int flags
)
80 struct stinger
*stinger
= serio_get_drvdata(serio
);
82 /* All Stinger packets are 4 bytes */
84 if (stinger
->idx
< STINGER_MAX_LENGTH
)
85 stinger
->data
[stinger
->idx
++] = data
;
87 if (stinger
->idx
== 4) {
88 stinger_process_packet(stinger
);
96 * stinger_disconnect() is the opposite of stinger_connect()
99 static void stinger_disconnect(struct serio
*serio
)
101 struct stinger
*stinger
= serio_get_drvdata(serio
);
104 serio_set_drvdata(serio
, NULL
);
105 input_unregister_device(stinger
->dev
);
110 * stinger_connect() is the routine that is called when someone adds a
111 * new serio device that supports Stinger protocol and registers it as
115 static int stinger_connect(struct serio
*serio
, struct serio_driver
*drv
)
117 struct stinger
*stinger
;
118 struct input_dev
*input_dev
;
121 stinger
= kmalloc(sizeof(*stinger
), GFP_KERNEL
);
122 input_dev
= input_allocate_device();
123 if (!stinger
|| !input_dev
)
126 stinger
->dev
= input_dev
;
127 snprintf(stinger
->phys
, sizeof(stinger
->phys
), "%s/serio0", serio
->phys
);
129 input_dev
->name
= "Gravis Stinger";
130 input_dev
->phys
= stinger
->phys
;
131 input_dev
->id
.bustype
= BUS_RS232
;
132 input_dev
->id
.vendor
= SERIO_STINGER
;
133 input_dev
->id
.product
= 0x0001;
134 input_dev
->id
.version
= 0x0100;
135 input_dev
->dev
.parent
= &serio
->dev
;
137 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
138 input_dev
->keybit
[BIT_WORD(BTN_A
)] = BIT_MASK(BTN_A
) | BIT_MASK(BTN_B
) |
139 BIT_MASK(BTN_C
) | BIT_MASK(BTN_X
) | BIT_MASK(BTN_Y
) |
140 BIT_MASK(BTN_Z
) | BIT_MASK(BTN_TL
) | BIT_MASK(BTN_TR
) |
141 BIT_MASK(BTN_START
) | BIT_MASK(BTN_SELECT
);
142 input_set_abs_params(input_dev
, ABS_X
, -64, 64, 0, 4);
143 input_set_abs_params(input_dev
, ABS_Y
, -64, 64, 0, 4);
145 serio_set_drvdata(serio
, stinger
);
147 err
= serio_open(serio
, drv
);
151 err
= input_register_device(stinger
->dev
);
157 fail3
: serio_close(serio
);
158 fail2
: serio_set_drvdata(serio
, NULL
);
159 fail1
: input_free_device(input_dev
);
165 * The serio driver structure.
168 static const struct serio_device_id stinger_serio_ids
[] = {
171 .proto
= SERIO_STINGER
,
178 MODULE_DEVICE_TABLE(serio
, stinger_serio_ids
);
180 static struct serio_driver stinger_drv
= {
184 .description
= DRIVER_DESC
,
185 .id_table
= stinger_serio_ids
,
186 .interrupt
= stinger_interrupt
,
187 .connect
= stinger_connect
,
188 .disconnect
= stinger_disconnect
,
191 module_serio_driver(stinger_drv
);