1/*
2 * Copyright (c) 2000-2001 Vojtech Pavlik
3 * Copyright (c) 2000 Mark Fletcher
4 */
5
6/*
7 * Gravis Stinger gamepad driver for Linux
8 */
9
10/*
11 * This program is free warftware; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/slab.h>
29#include <linux/input.h>
30#include <linux/serio.h>
31
32#define DRIVER_DESC "Gravis Stinger gamepad driver"
33
34MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
35MODULE_DESCRIPTION(DRIVER_DESC);
36MODULE_LICENSE("GPL");
37
38/*
39 * Constants.
40 */
41
42#define STINGER_MAX_LENGTH 8
43
44/*
45 * Per-Stinger data.
46 */
47
48struct stinger {
49 struct input_dev *dev;
50 int idx;
51 unsigned char data[STINGER_MAX_LENGTH];
52 char phys[32];
53};
54
55/*
56 * stinger_process_packet() decodes packets the driver receives from the
57 * Stinger. It updates the data accordingly.
58 */
59
60static void stinger_process_packet(struct stinger *stinger)
61{
62 struct input_dev *dev = stinger->dev;
63 unsigned char *data = stinger->data;
64
65 if (!stinger->idx) return;
66
67 input_report_key(dev, BTN_A, ((data[0] & 0x20) >> 5));
68 input_report_key(dev, BTN_B, ((data[0] & 0x10) >> 4));
69 input_report_key(dev, BTN_C, ((data[0] & 0x08) >> 3));
70 input_report_key(dev, BTN_X, ((data[0] & 0x04) >> 2));
71 input_report_key(dev, BTN_Y, ((data[3] & 0x20) >> 5));
72 input_report_key(dev, BTN_Z, ((data[3] & 0x10) >> 4));
73 input_report_key(dev, BTN_TL, ((data[3] & 0x08) >> 3));
74 input_report_key(dev, BTN_TR, ((data[3] & 0x04) >> 2));
75 input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1));
76 input_report_key(dev, BTN_START, (data[3] & 0x01));
77
78 input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6));
79 input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F));
80
81 input_sync(dev);
82
83 return;
84}
85
86/*
87 * stinger_interrupt() is called by the low level driver when characters
88 * are ready for us. We then buffer them for further processing, or call the
89 * packet processing routine.
90 */
91
92static irqreturn_t stinger_interrupt(struct serio *serio,
93 unsigned char data, unsigned int flags)
94{
95 struct stinger *stinger = serio_get_drvdata(serio);
96
97 /* All Stinger packets are 4 bytes */
98
99 if (stinger->idx < STINGER_MAX_LENGTH)
100 stinger->data[stinger->idx++] = data;
101
102 if (stinger->idx == 4) {
103 stinger_process_packet(stinger);
104 stinger->idx = 0;
105 }
106
107 return IRQ_HANDLED;
108}
109
110/*
111 * stinger_disconnect() is the opposite of stinger_connect()
112 */
113
114static void stinger_disconnect(struct serio *serio)
115{
116 struct stinger *stinger = serio_get_drvdata(serio);
117
118 serio_close(serio);
119 serio_set_drvdata(serio, NULL);
120 input_unregister_device(stinger->dev);
121 kfree(stinger);
122}
123
124/*
125 * stinger_connect() is the routine that is called when someone adds a
126 * new serio device that supports Stinger protocol and registers it as
127 * an input device.
128 */
129
130static int stinger_connect(struct serio *serio, struct serio_driver *drv)
131{
132 struct stinger *stinger;
133 struct input_dev *input_dev;
134 int err = -ENOMEM;
135
136 stinger = kmalloc(sizeof(struct stinger), GFP_KERNEL);
137 input_dev = input_allocate_device();
138 if (!stinger || !input_dev)
139 goto fail1;
140
141 stinger->dev = input_dev;
142 snprintf(stinger->phys, sizeof(stinger->phys), "%s/serio0", serio->phys);
143
144 input_dev->name = "Gravis Stinger";
145 input_dev->phys = stinger->phys;
146 input_dev->id.bustype = BUS_RS232;
147 input_dev->id.vendor = SERIO_STINGER;
148 input_dev->id.product = 0x0001;
149 input_dev->id.version = 0x0100;
150 input_dev->dev.parent = &serio->dev;
151
152 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
153 input_dev->keybit[BIT_WORD(BTN_A)] = BIT_MASK(BTN_A) | BIT_MASK(BTN_B) |
154 BIT_MASK(BTN_C) | BIT_MASK(BTN_X) | BIT_MASK(BTN_Y) |
155 BIT_MASK(BTN_Z) | BIT_MASK(BTN_TL) | BIT_MASK(BTN_TR) |
156 BIT_MASK(BTN_START) | BIT_MASK(BTN_SELECT);
157 input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4);
158 input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4);
159
160 serio_set_drvdata(serio, stinger);
161
162 err = serio_open(serio, drv);
163 if (err)
164 goto fail2;
165
166 err = input_register_device(stinger->dev);
167 if (err)
168 goto fail3;
169
170 return 0;
171
172 fail3: serio_close(serio);
173 fail2: serio_set_drvdata(serio, NULL);
174 fail1: input_free_device(input_dev);
175 kfree(stinger);
176 return err;
177}
178
179/*
180 * The serio driver structure.
181 */
182
183static const struct serio_device_id stinger_serio_ids[] = {
184 {
185 .type = SERIO_RS232,
186 .proto = SERIO_STINGER,
187 .id = SERIO_ANY,
188 .extra = SERIO_ANY,
189 },
190 { 0 }
191};
192
193MODULE_DEVICE_TABLE(serio, stinger_serio_ids);
194
195static struct serio_driver stinger_drv = {
196 .driver = {
197 .name = "stinger",
198 },
199 .description = DRIVER_DESC,
200 .id_table = stinger_serio_ids,
201 .interrupt = stinger_interrupt,
202 .connect = stinger_connect,
203 .disconnect = stinger_disconnect,
204};
205
206module_serio_driver(stinger_drv);
207