-
Notifications
You must be signed in to change notification settings - Fork 0
/
xbox360wr.c
374 lines (312 loc) · 9.02 KB
/
xbox360wr.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "xusb.h"
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/usb.h>
MODULE_AUTHOR("Zachary Lund <[email protected]>");
MODULE_DESCRIPTION("Xbox 360 Wireless Adapter Driver");
MODULE_LICENSE("GPL");
#define XBOX360WR_PACKET_SIZE 32
static XINPUT_CAPABILITIES xbox360wr_gamepad_caps = {
.Type = XINPUT_DEVTYPE_GAMEPAD,
.SubType = XINPUT_DEVSUBTYPE_GAMEPAD,
.Flags = XINPUT_CAPS_FFB_SUPPORTED,
.Gamepad = {
.wButtons =
XINPUT_GAMEPAD_DPAD_UP |
XINPUT_GAMEPAD_DPAD_DOWN |
XINPUT_GAMEPAD_DPAD_LEFT |
XINPUT_GAMEPAD_DPAD_RIGHT |
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_A |
XINPUT_GAMEPAD_B |
XINPUT_GAMEPAD_X |
XINPUT_GAMEPAD_Y,
.bLeftTrigger = 255,
.bRightTrigger = 255,
.sThumbLX = 32767,
.sThumbLY = 32767,
.sThumbRX = 32767,
.sThumbRY = 32767
},
.Vibration = {
.wLeftMotorSpeed = 65535,
.wRightMotorSpeed = 65535
}
};
/* There's a finite amount of devices that we can
match up to a table instead of dynamically
generating the data on the fly. We're not
actually provided with capability information
anyways... the best we can get is an identifier
for the device connected and even then, we're
barely managing that with a serial we don't
even know is reliable.*/
static struct xusb_device xbox360wr_devices[] = {
{
"Xbox 360 Wireless Receiver",
&xbox360wr_gamepad_caps,
}
};
struct xbox360wr_context {
struct xusb_context *xusb_ctx;
struct usb_interface *usb_intf;
struct urb *in;
int pipe_out; /* I don't like the pipe... */
};
/* There's a lot of oddities with the outward packets.
We don't know why or how they are...
They're just from observing the packets from the
Microsoft driver */
static int xbox360wr_send(struct xbox360wr_context *ctx, void *data, int size)
{
struct usb_device *usb_dev = interface_to_usbdev(ctx->usb_intf);
int actual_length = 0, error;
if (usb_dev->state == USB_STATE_NOTATTACHED)
return 0;
error = usb_interrupt_msg(usb_dev, ctx->pipe_out,
data, size, &actual_length, 0);
if (error) {
printk(KERN_ERR "Error during submission."
"Error code: %d - Actual Length %d\n", error, actual_length);
}
return actual_length;
}
static void xbox360wr_set_vibration(
void *data, XINPUT_VIBRATION ff)
{
struct xbox360wr_context *ctx = data;
s8 left = (ff.wLeftMotorSpeed / 255) - 1;
s8 right = (ff.wRightMotorSpeed / 255) - 1;
u8 packet[] = {
0x00, 0x01, 0x0F, 0xC0,
0x00, left, right, 0x00,
0x00, 0x00, 0x00, 0x00
};
xbox360wr_send(ctx, packet, sizeof(packet));
}
/* While this does seem to effectively set the LED,
we're probably doing it incorrectly. In order
to understand what I'm talking about, you'll
need to observe packets sent from the xusb22
driver. */
static void xbox360wr_set_led(
void *data, enum XINPUT_LED_STATUS led_status)
{
struct xbox360wr_context *ctx = data;
u8 packet[] = {
0x00, 0x00, 0x08, led_status + 0x40,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
xbox360wr_send(ctx, packet, sizeof(packet));
}
static void xbox360wr_query_presence(struct xbox360wr_context *ctx)
{
#define PRESENCE_PACKET_SIZE 12
int actual_length, error;
struct usb_device *usb_dev = interface_to_usbdev(ctx->usb_intf);
u8 packet[PRESENCE_PACKET_SIZE] = {
0x08, 0x00, 0x0F, 0xC0,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
error =
usb_interrupt_msg(usb_dev, ctx->pipe_out,
packet, PRESENCE_PACKET_SIZE, &actual_length, 0);
if (error) {
printk(KERN_ERR "Error during submission."
"Error code: %d - Actual Length %d\n", error, actual_length);
}
/* Can't really do anything here... */
}
static struct xusb_driver xbox360wr_driver = {
.set_led = xbox360wr_set_led,
.set_vibration = xbox360wr_set_vibration
};
static void xpad360_parse_input(void *data, XINPUT_GAMEPAD *out)
{
u8 *buffer = data;
out->wButtons = le16_to_cpup((__le16*)&buffer[0]);
out->bLeftTrigger = buffer[2];
out->bRightTrigger = buffer[3];
out->sThumbLX = (__s16)le16_to_cpup((__le16*)&buffer[4]);
out->sThumbLY = (__s16)le16_to_cpup((__le16*)&buffer[6]);
out->sThumbRX = (__s16)le16_to_cpup((__le16*)&buffer[8]);
out->sThumbRY = (__s16)le16_to_cpup((__le16*)&buffer[10]);
}
/* Interrupt for incoming URB. */
static void xbox360wr_receive(struct urb* urb)
{
struct xbox360wr_context *ctx = urb->context;
u8 *data = urb->transfer_buffer;
switch (urb->status) {
case 0:
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
return;
default:
goto finish;
}
/* Event from Adapter */
if (data[0] == 0x08) {
switch (data[1]) {
case 0x00:
/* Disconnect */
/* This might happen if we request a
presence packet while we're disconnected */
if (!ctx->xusb_ctx)
break;
xusb_unregister_device(ctx->xusb_ctx);
ctx->xusb_ctx = 0;
break;
case 0xC0:
/* Connect w/ Headset (attachment?) */
/* We don't handle attachments. TODO */
case 0x80: {
/* Connect */
/* Might happen if a presence packet is sent
while we're already connected */
if (ctx->xusb_ctx != 0)
break;
ctx->xusb_ctx = xusb_register_device( /* HARDCODED FIXME */
&xbox360wr_driver, &xbox360wr_devices[0], ctx);
break;
}
case 0x40:
/* Headset Connected (attachment?) */
/* We don't handle attachments. TODO */
break;
}
}
/* Event from Controller */
else if (data[0] == 0x00) {
u16 header = le16_to_cpup((__le16*)&data[1]);
switch (header) {
case 0x0000: /* Unknown! */
break;
case 0x0001: { /* Input Event */
XINPUT_GAMEPAD input;
xpad360_parse_input(&data[6], &input);
xusb_report_input(ctx->xusb_ctx, &input);
break;
}
case 0x000A:
/* Occurs after Headset Connection packet (0x40)
An arbitrarily sized description string
delimited by a series of 0xFF bytes. */
case 0x0009:
/* Occurs right after 0x000A. First two bytes are unknown.
14 bytes past that is the serial of the attachment. */
break;
case 0x01F8:
/* Seems to be a PING or PONG type event. */
case 0x02F8:
/* Seems to complement 0x01F8 */
break;
case 0x000F:
/* Announcement Packet. Unknown layout!
Occurs right after Controller Connection Packet (0x80)*/
break;
default:
printk(KERN_ERR "Unknown packet receieved. Header was %#.8x\n", header);
}
}
finish:
usb_submit_urb(urb, GFP_ATOMIC);
}
/* The wireless adapter will throw four interfaces at us,
each one representing a controller. Initialization is
very similar to the wired version. So much so, I'd imagine
we can combine the code. However, I wont' do that for now
for the sake of debugging and usability at this moment. */
static int xbox360wr_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_device *usb_dev = interface_to_usbdev(intf);
struct usb_endpoint_descriptor *ep =
&intf->cur_altsetting->endpoint[0].desc;
const int pipe = usb_rcvintpipe(usb_dev, ep->bEndpointAddress);
struct xbox360wr_context *ctx;
int error = 0;
void *in_buffer;
dma_addr_t in_dma;
ctx = kmalloc(sizeof(struct xbox360wr_context), GFP_KERNEL);
if (!ctx) {
return -ENOMEM;
}
usb_set_intfdata(intf, ctx);
ctx->usb_intf = intf;
ctx->xusb_ctx = 0;
ctx->pipe_out =
usb_sndintpipe(usb_dev,
intf->cur_altsetting->endpoint[1].desc.bEndpointAddress);
ctx->in = usb_alloc_urb(0, GFP_KERNEL);
if (!ctx->in) {
error = -ENOMEM;
goto fail_urb_alloc;
}
in_buffer =
usb_alloc_coherent(
usb_dev, XBOX360WR_PACKET_SIZE,
GFP_KERNEL, &in_dma);
if (!in_buffer) {
error = -ENOMEM;
goto fail_alloc_coherent;
}
usb_fill_int_urb(
ctx->in, usb_dev,
pipe, in_buffer, XBOX360WR_PACKET_SIZE,
xbox360wr_receive, ctx, ep->bInterval);
error = usb_submit_urb(ctx->in, GFP_KERNEL);
if (error) {
error = -ENOMEM;
goto fail_in_submit;
}
/* This will force the controller to resend connection packets.
This is useful in the case we activate the module after the
adapter has been plugged in, as it won't automatically
send us info about the controllers. */
xbox360wr_query_presence(ctx);
return 0;
fail_in_submit:
usb_free_coherent(usb_dev, XBOX360WR_PACKET_SIZE, in_buffer, in_dma);
fail_alloc_coherent:
usb_free_urb(ctx->in);
fail_urb_alloc:
kfree(ctx);
return error;
}
static void xbox360wr_disconnect(struct usb_interface *intf)
{
struct xbox360wr_context *ctx = usb_get_intfdata(intf);
struct usb_device *usb_dev = interface_to_usbdev(intf);
struct urb *in_urb = ctx->in;
usb_kill_urb(in_urb);
usb_free_coherent(usb_dev, XBOX360WR_PACKET_SIZE,
in_urb->transfer_buffer, in_urb->transfer_dma);
usb_free_urb(in_urb);
if (ctx->xusb_ctx != 0) {
xusb_unregister_device(ctx->xusb_ctx);
xusb_flush();
}
kfree(ctx);
}
static const struct usb_device_id xbox360wr_table[] = {
{ USB_DEVICE_INTERFACE_PROTOCOL(0x045E, 0x0719, 129) },
{}
};
static struct usb_driver xbox360wr_usb_driver = {
.name = "xbox360wr",
.id_table = xbox360wr_table,
.probe = xbox360wr_probe,
.disconnect = xbox360wr_disconnect,
.soft_unbind = 1
};
module_usb_driver(xbox360wr_usb_driver);