1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/slab.h>
53#include <linux/input.h>
54#include <linux/serio.h>
55#include <linux/init.h>
56
57#define DRIVER_DESC "Handykey Twiddler keyboard as a joystick driver"
58
59MODULE_DESCRIPTION(DRIVER_DESC);
60MODULE_LICENSE("GPL");
61
62
63
64
65
66#define TWIDJOY_MAX_LENGTH 5
67
68static struct twidjoy_button_spec {
69 int bitshift;
70 int bitmask;
71 int buttons[3];
72}
73twidjoy_buttons[] = {
74 { 0, 3, { BTN_A, BTN_B, BTN_C } },
75 { 2, 3, { BTN_X, BTN_Y, BTN_Z } },
76 { 4, 3, { BTN_TL, BTN_TR, BTN_TR2 } },
77 { 6, 3, { BTN_SELECT, BTN_START, BTN_MODE } },
78 { 8, 1, { BTN_BASE5 } },
79 { 9, 1, { BTN_BASE } },
80 { 10, 1, { BTN_BASE3 } },
81 { 11, 1, { BTN_BASE4 } },
82 { 12, 1, { BTN_BASE2 } },
83 { 13, 1, { BTN_BASE6 } },
84 { 0, 0, { 0 } }
85};
86
87
88
89
90
91struct twidjoy {
92 struct input_dev *dev;
93 int idx;
94 unsigned char data[TWIDJOY_MAX_LENGTH];
95 char phys[32];
96};
97
98
99
100
101
102
103static void twidjoy_process_packet(struct twidjoy *twidjoy)
104{
105 struct input_dev *dev = twidjoy->dev;
106 unsigned char *data = twidjoy->data;
107 struct twidjoy_button_spec *bp;
108 int button_bits, abs_x, abs_y;
109
110 button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
111
112 for (bp = twidjoy_buttons; bp->bitmask; bp++) {
113 int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
114 int i;
115
116 for (i = 0; i < bp->bitmask; i++)
117 input_report_key(dev, bp->buttons[i], i+1 == value);
118 }
119
120 abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
121 if (data[4] & 0x08) abs_x -= 256;
122
123 abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
124 if (data[3] & 0x02) abs_y -= 256;
125
126 input_report_abs(dev, ABS_X, -abs_x);
127 input_report_abs(dev, ABS_Y, +abs_y);
128
129 input_sync(dev);
130}
131
132
133
134
135
136
137
138static irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
139{
140 struct twidjoy *twidjoy = serio_get_drvdata(serio);
141
142
143
144
145
146 if ((data & 0x80) == 0)
147 twidjoy->idx = 0;
148 else if (twidjoy->idx == 0)
149 return IRQ_HANDLED;
150
151 if (twidjoy->idx < TWIDJOY_MAX_LENGTH)
152 twidjoy->data[twidjoy->idx++] = data;
153
154 if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
155 twidjoy_process_packet(twidjoy);
156 twidjoy->idx = 0;
157 }
158
159 return IRQ_HANDLED;
160}
161
162
163
164
165
166static void twidjoy_disconnect(struct serio *serio)
167{
168 struct twidjoy *twidjoy = serio_get_drvdata(serio);
169
170 serio_close(serio);
171 serio_set_drvdata(serio, NULL);
172 input_unregister_device(twidjoy->dev);
173 kfree(twidjoy);
174}
175
176
177
178
179
180
181
182static int twidjoy_connect(struct serio *serio, struct serio_driver *drv)
183{
184 struct twidjoy_button_spec *bp;
185 struct twidjoy *twidjoy;
186 struct input_dev *input_dev;
187 int err = -ENOMEM;
188 int i;
189
190 twidjoy = kzalloc(sizeof(struct twidjoy), GFP_KERNEL);
191 input_dev = input_allocate_device();
192 if (!twidjoy || !input_dev)
193 goto fail1;
194
195 twidjoy->dev = input_dev;
196 snprintf(twidjoy->phys, sizeof(twidjoy->phys), "%s/input0", serio->phys);
197
198 input_dev->name = "Handykey Twiddler";
199 input_dev->phys = twidjoy->phys;
200 input_dev->id.bustype = BUS_RS232;
201 input_dev->id.vendor = SERIO_TWIDJOY;
202 input_dev->id.product = 0x0001;
203 input_dev->id.version = 0x0100;
204 input_dev->dev.parent = &serio->dev;
205
206 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
207 input_set_abs_params(input_dev, ABS_X, -50, 50, 4, 4);
208 input_set_abs_params(input_dev, ABS_Y, -50, 50, 4, 4);
209
210 for (bp = twidjoy_buttons; bp->bitmask; bp++)
211 for (i = 0; i < bp->bitmask; i++)
212 set_bit(bp->buttons[i], input_dev->keybit);
213
214 serio_set_drvdata(serio, twidjoy);
215
216 err = serio_open(serio, drv);
217 if (err)
218 goto fail2;
219
220 err = input_register_device(twidjoy->dev);
221 if (err)
222 goto fail3;
223
224 return 0;
225
226 fail3: serio_close(serio);
227 fail2: serio_set_drvdata(serio, NULL);
228 fail1: input_free_device(input_dev);
229 kfree(twidjoy);
230 return err;
231}
232
233
234
235
236
237static struct serio_device_id twidjoy_serio_ids[] = {
238 {
239 .type = SERIO_RS232,
240 .proto = SERIO_TWIDJOY,
241 .id = SERIO_ANY,
242 .extra = SERIO_ANY,
243 },
244 { 0 }
245};
246
247MODULE_DEVICE_TABLE(serio, twidjoy_serio_ids);
248
249static struct serio_driver twidjoy_drv = {
250 .driver = {
251 .name = "twidjoy",
252 },
253 .description = DRIVER_DESC,
254 .id_table = twidjoy_serio_ids,
255 .interrupt = twidjoy_interrupt,
256 .connect = twidjoy_connect,
257 .disconnect = twidjoy_disconnect,
258};
259
260
261
262
263
264static int __init twidjoy_init(void)
265{
266 return serio_register_driver(&twidjoy_drv);
267}
268
269static void __exit twidjoy_exit(void)
270{
271 serio_unregister_driver(&twidjoy_drv);
272}
273
274module_init(twidjoy_init);
275module_exit(twidjoy_exit);
276