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