-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxusb.c
320 lines (247 loc) · 8 KB
/
xusb.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "xusb.h"
#include <linux/workqueue.h>
#include <linux/spinlock.h>
#include <linux/spinlock_types.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/input.h>
/* XUSB_MAX_CONTROLLERS can be set to any arbitrary number.
We make it 4 to match XInput. */
/* TODO:
- Handle different controller types. Not sure how or why though...
- Possibly remove locking. Need to step through each line of code...
- Need to have hardware! I can't test enough. I don't even know
what parts are fragile and what parts aren't at this point.
TODO before requesting for patches:
- Clean up commit history.
- Fix data race between work queue and spinlocks. */
#define XINPUT_LIMIT 4
#define XINPUT_INVALID -1
/* Table and mapping of the buttons. */
static const u16 xinput_button_table[12] = {
XINPUT_GAMEPAD_START,
XINPUT_GAMEPAD_BACK,
XINPUT_GAMEPAD_LEFT_THUMB,
XINPUT_GAMEPAD_RIGHT_THUMB,
XINPUT_GAMEPAD_LEFT_SHOULDER,
XINPUT_GAMEPAD_RIGHT_SHOULDER,
XINPUT_GAMEPAD_GUIDE,
XINPUT_GAMEPAD_RESERVED,
XINPUT_GAMEPAD_A,
XINPUT_GAMEPAD_B,
XINPUT_GAMEPAD_X,
XINPUT_GAMEPAD_Y
};
static const size_t xinput_button_table_sz =
sizeof(xinput_button_table) / sizeof(xinput_button_table[0]);
static const int xinput_to_codes[12] = {
BTN_START, BTN_BACK,
BTN_THUMBL, BTN_THUMBR,
BTN_TL, BTN_TR,
BTN_MODE, 0,
BTN_A, BTN_B,
BTN_X, BTN_Y,
};
/* We don't hold a static number of input work.
Thus, it doesn't make sense to hold it within
the xusb_context structure. */
struct xusb_input_work {
XINPUT_GAMEPAD input;
struct input_dev *input_dev;
struct work_struct work;
};
struct xusb_context {
u8 index;
void *user_data;
struct xusb_driver *driver;
struct input_dev *input_dev;
struct work_struct register_work;
struct work_struct unregister_work;
struct xusb_device *device;
};
static struct workqueue_struct *xusb_wq;
static struct xusb_context *xusb_index[4] = { 0 };
static DEFINE_SPINLOCK(xusb_index_lock);
static void xusb_setup_analog(struct input_dev *input_dev, int code, s16 res)
{
if (res <= 0)
return;
input_set_capability(input_dev, EV_ABS, code);
input_set_abs_params(input_dev, code, -res, res, 0, 0);
}
static void xusb_setup_trigger(struct input_dev *input_dev, int code, u8 res)
{
input_set_capability(input_dev, EV_ABS, code);
input_set_abs_params(input_dev, code, 0, res, 0, 0);
}
static void xusb_setup_hatswitch(struct input_dev *input_dev, int code)
{
/* No point in checking the res... we must do this explicitly */
input_set_capability(input_dev, EV_ABS, code);
input_set_abs_params(input_dev, code, -1, 1, 0, 0);
}
static void xusb_handle_register(struct work_struct *pwork)
{
struct xusb_context *ctx =
container_of(pwork, struct xusb_context, register_work);
XINPUT_GAMEPAD *Gamepad = &ctx->device->caps->Gamepad;
struct input_dev* input_dev = input_allocate_device();
if (!input_dev) {
printk(KERN_ERR "Failed to allocate device!\n");
return;
}
for (int i = 0; i < xinput_button_table_sz; ++i) {
if (Gamepad->wButtons & xinput_button_table[i]) {
input_set_capability(
input_dev, EV_KEY, xinput_to_codes[i]);
}
}
/* Unfortunately, for the DPad to use a hatswitch,
we can't iterate over the bits that indicate
hat-switch capability. Perhaps a FIXME. */
if ((Gamepad->wButtons & XINPUT_GAMEPAD_DPAD_UP) &&
(Gamepad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN)) {
xusb_setup_hatswitch(input_dev, ABS_HAT0Y);
}
if ((Gamepad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT) &&
(Gamepad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT)) {
xusb_setup_hatswitch(input_dev, ABS_HAT0X);
}
xusb_setup_trigger(input_dev, ABS_Z, Gamepad->bLeftTrigger);
xusb_setup_trigger(input_dev, ABS_RZ, Gamepad->bRightTrigger);
xusb_setup_analog(input_dev, ABS_X, Gamepad->sThumbLX);
xusb_setup_analog(input_dev, ABS_Y, Gamepad->sThumbLY);
xusb_setup_analog(input_dev, ABS_RX, Gamepad->sThumbRX);
xusb_setup_analog(input_dev, ABS_RY, Gamepad->sThumbRY);
input_dev->name = ctx->device->name;
if (input_register_device(input_dev) != 0) {
printk(KERN_ERR "Failed to register input device!\n");
input_free_device(input_dev);
return;
}
ctx->input_dev = input_dev;
if (ctx->index != XINPUT_INVALID) {
ctx->driver->set_led(ctx->user_data,
XINPUT_LED_ON_1 + ctx->index);
}
}
static void xusb_handle_unregister(struct work_struct *pwork)
{
struct xusb_context *ctx =
container_of(pwork, struct xusb_context, unregister_work);
input_unregister_device(ctx->input_dev);
ctx->input_dev = 0;
ctx->user_data = 0;
ctx->driver = 0;
kfree(ctx);
}
static void xusb_handle_input(struct work_struct *pwork)
{
struct xusb_input_work *ctx =
container_of(pwork, struct xusb_input_work, work);
u16 buttons;
if (!ctx->input_dev) {
printk(KERN_ERR "Attempt to handle input for invalid input device!");
return;
}
buttons = ctx->input.wButtons;
/* The Input Subsystem checks for reported features each
time we submit an event. Inefficient but works for our case. */
for (int i = 0; i < xinput_button_table_sz; ++i) {
input_report_key(
ctx->input_dev,
xinput_to_codes[i],
buttons & xinput_button_table[i]);
}
input_report_abs(ctx->input_dev, ABS_HAT0X,
!!(buttons & XINPUT_GAMEPAD_DPAD_RIGHT) - !!(buttons & XINPUT_GAMEPAD_DPAD_LEFT));
input_report_abs(ctx->input_dev, ABS_HAT0Y,
!!(buttons & XINPUT_GAMEPAD_DPAD_DOWN) - !!(buttons & XINPUT_GAMEPAD_DPAD_UP));
input_report_abs(ctx->input_dev, ABS_Z, ctx->input.bLeftTrigger);
input_report_abs(ctx->input_dev, ABS_RZ, ctx->input.bRightTrigger);
input_report_abs(ctx->input_dev, ABS_X, ctx->input.sThumbLX);
input_report_abs(ctx->input_dev, ABS_Y, ctx->input.sThumbLY);
input_report_abs(ctx->input_dev, ABS_RX, ctx->input.sThumbRX);
input_report_abs(ctx->input_dev, ABS_RY, ctx->input.sThumbRY);
input_sync(ctx->input_dev);
kfree(ctx);
}
struct xusb_context *xusb_register_device(
struct xusb_driver *driver,
struct xusb_device *device,
void *user_data)
{
int index = XINPUT_INVALID;
unsigned long flags;
struct xusb_context *ctx;
/* FIXME: Should be allocated from a pre-allocated pool
specific to the xusb module. */
ctx = kmalloc(sizeof(struct xusb_context), GFP_ATOMIC);
spin_lock_irqsave(&xusb_index_lock, flags);
for (int i = 0; i < XINPUT_LIMIT; ++i) {
if (xusb_index[i] == 0) {
index = i;
break;
}
}
printk("Assigning controller index %d", index);
if (index == XINPUT_INVALID)
printk("More than 4 XInput controllers connected.");
else
xusb_index[index] = ctx;
spin_unlock_irqrestore(&xusb_index_lock, flags);
ctx->index = index;
ctx->driver = driver;
ctx->device = device;
ctx->user_data = user_data;
INIT_WORK(&ctx->register_work, xusb_handle_register);
INIT_WORK(&ctx->unregister_work, xusb_handle_unregister);
queue_work(xusb_wq, &ctx->register_work);
return ctx;
}
void xusb_unregister_device(struct xusb_context *ctx)
{
unsigned long flags;
if (ctx->index != XINPUT_INVALID) {
spin_lock_irqsave(&xusb_index_lock, flags);
xusb_index[ctx->index] = 0;
spin_unlock_irqrestore(&xusb_index_lock, flags);
}
queue_work(xusb_wq, &ctx->unregister_work);
}
void xusb_report_input(struct xusb_context *ctx, const XINPUT_GAMEPAD *input)
{
struct xusb_input_work *input_work =
kmalloc(sizeof(struct xusb_input_work), GFP_ATOMIC);
if (!input_work)
return;
input_work->input = *input;
input_work->input_dev = ctx->input_dev;
INIT_WORK(&input_work->work, xusb_handle_input);
queue_work(xusb_wq, &input_work->work);
}
void xusb_flush(void)
{
flush_workqueue(xusb_wq);
}
EXPORT_SYMBOL_GPL(xusb_report_input);
EXPORT_SYMBOL_GPL(xusb_unregister_device);
EXPORT_SYMBOL_GPL(xusb_register_device);
EXPORT_SYMBOL_GPL(xusb_flush);
static int __init xusb_init(void)
{
xusb_wq = alloc_ordered_workqueue("xusb", 0);
if (xusb_wq == NULL) {
return -ENOMEM;
}
return 0;
}
static void __exit xusb_exit(void)
{
destroy_workqueue(xusb_wq);
}
MODULE_AUTHOR("Zachary Lund <admin@computerquip`m>");
MODULE_DESCRIPTION("Common Xbox Controller Interface");
MODULE_LICENSE("GPL");
module_init(xusb_init);
module_exit(xusb_exit);