blob: 099c6d7b5e08732a2e0a1d26f0f4c25d1f3921cd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 * Should you need to contact me, the author, you can do so either by
26 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
27 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
28 */
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/input.h>
34#include <linux/serio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#define DRIVER_DESC "Gravis Stinger gamepad driver"
37
38MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
39MODULE_DESCRIPTION(DRIVER_DESC);
40MODULE_LICENSE("GPL");
41
42/*
43 * Constants.
44 */
45
46#define STINGER_MAX_LENGTH 8
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * Per-Stinger data.
50 */
51
52struct stinger {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050053 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int idx;
55 unsigned char data[STINGER_MAX_LENGTH];
56 char phys[32];
57};
58
59/*
60 * stinger_process_packet() decodes packets the driver receives from the
61 * Stinger. It updates the data accordingly.
62 */
63
David Howells7d12e782006-10-05 14:55:46 +010064static void stinger_process_packet(struct stinger *stinger)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050066 struct input_dev *dev = stinger->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 unsigned char *data = stinger->data;
68
69 if (!stinger->idx) return;
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 input_report_key(dev, BTN_A, ((data[0] & 0x20) >> 5));
72 input_report_key(dev, BTN_B, ((data[0] & 0x10) >> 4));
73 input_report_key(dev, BTN_C, ((data[0] & 0x08) >> 3));
74 input_report_key(dev, BTN_X, ((data[0] & 0x04) >> 2));
75 input_report_key(dev, BTN_Y, ((data[3] & 0x20) >> 5));
76 input_report_key(dev, BTN_Z, ((data[3] & 0x10) >> 4));
77 input_report_key(dev, BTN_TL, ((data[3] & 0x08) >> 3));
78 input_report_key(dev, BTN_TR, ((data[3] & 0x04) >> 2));
79 input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1));
80 input_report_key(dev, BTN_START, (data[3] & 0x01));
81
82 input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6));
83 input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F));
84
85 input_sync(dev);
86
87 return;
88}
89
90/*
91 * stinger_interrupt() is called by the low level driver when characters
92 * are ready for us. We then buffer them for further processing, or call the
93 * packet processing routine.
94 */
95
96static irqreturn_t stinger_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +010097 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct stinger *stinger = serio_get_drvdata(serio);
100
101 /* All Stinger packets are 4 bytes */
102
103 if (stinger->idx < STINGER_MAX_LENGTH)
104 stinger->data[stinger->idx++] = data;
105
106 if (stinger->idx == 4) {
David Howells7d12e782006-10-05 14:55:46 +0100107 stinger_process_packet(stinger);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 stinger->idx = 0;
109 }
110
111 return IRQ_HANDLED;
112}
113
114/*
115 * stinger_disconnect() is the opposite of stinger_connect()
116 */
117
118static void stinger_disconnect(struct serio *serio)
119{
120 struct stinger *stinger = serio_get_drvdata(serio);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 serio_close(serio);
123 serio_set_drvdata(serio, NULL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500124 input_unregister_device(stinger->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 kfree(stinger);
126}
127
128/*
129 * stinger_connect() is the routine that is called when someone adds a
130 * new serio device that supports Stinger protocol and registers it as
131 * an input device.
132 */
133
134static int stinger_connect(struct serio *serio, struct serio_driver *drv)
135{
136 struct stinger *stinger;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500137 struct input_dev *input_dev;
138 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500140 stinger = kmalloc(sizeof(struct stinger), GFP_KERNEL);
141 input_dev = input_allocate_device();
142 if (!stinger || !input_dev)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500143 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500145 stinger->dev = input_dev;
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400146 snprintf(stinger->phys, sizeof(stinger->phys), "%s/serio0", serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500148 input_dev->name = "Gravis Stinger";
149 input_dev->phys = stinger->phys;
150 input_dev->id.bustype = BUS_RS232;
151 input_dev->id.vendor = SERIO_STINGER;
152 input_dev->id.product = 0x0001;
153 input_dev->id.version = 0x0100;
Dmitry Torokhov935e6582007-04-12 01:35:26 -0400154 input_dev->dev.parent = &serio->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700156 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
157 input_dev->keybit[BIT_WORD(BTN_A)] = BIT_MASK(BTN_A) | BIT_MASK(BTN_B) |
158 BIT_MASK(BTN_C) | BIT_MASK(BTN_X) | BIT_MASK(BTN_Y) |
159 BIT_MASK(BTN_Z) | BIT_MASK(BTN_TL) | BIT_MASK(BTN_TR) |
160 BIT_MASK(BTN_START) | BIT_MASK(BTN_SELECT);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500161 input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4);
162 input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 serio_set_drvdata(serio, stinger);
165
166 err = serio_open(serio, drv);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500167 if (err)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500168 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500170 err = input_register_device(stinger->dev);
171 if (err)
172 goto fail3;
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500175
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500176 fail3: serio_close(serio);
177 fail2: serio_set_drvdata(serio, NULL);
178 fail1: input_free_device(input_dev);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500179 kfree(stinger);
180 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/*
184 * The serio driver structure.
185 */
186
187static struct serio_device_id stinger_serio_ids[] = {
188 {
189 .type = SERIO_RS232,
190 .proto = SERIO_STINGER,
191 .id = SERIO_ANY,
192 .extra = SERIO_ANY,
193 },
194 { 0 }
195};
196
197MODULE_DEVICE_TABLE(serio, stinger_serio_ids);
198
199static struct serio_driver stinger_drv = {
200 .driver = {
201 .name = "stinger",
202 },
203 .description = DRIVER_DESC,
204 .id_table = stinger_serio_ids,
205 .interrupt = stinger_interrupt,
206 .connect = stinger_connect,
207 .disconnect = stinger_disconnect,
208};
209
Axel Lin65ac9f72012-04-03 23:50:17 -0700210module_serio_driver(stinger_drv);