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