-
Notifications
You must be signed in to change notification settings - Fork 0
/
xbox360.c
262 lines (216 loc) · 5.65 KB
/
xbox360.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
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/usb.h>
#include "xusb.h"
MODULE_AUTHOR("Zachary Lund <[email protected]>");
MODULE_DESCRIPTION("Wired Xbox 360 Controller Driver");
MODULE_LICENSE("GPL");
#define XBOX360_PACKET_SIZE 32
struct xbox360_context {
struct xusb_context *xusb_ctx;
struct usb_interface *usb_intf;
struct urb *in;
int pipe_out;
};
static XINPUT_CAPABILITIES xbox360_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
}
};
static struct xusb_device xbox360_devices[] = {
{
"Microsoft X-Box 360 pad",
&xbox360_gamepad_caps
}
};
static struct usb_device_id xbox360_table[] = {
{ USB_DEVICE_INTERFACE_PROTOCOL(0x045E, 0x028e, 1) },
{}
};
static int xbox360_send(struct xbox360_context *ctx, void *data, int size)
{
struct usb_device *usb_dev = interface_to_usbdev(ctx->usb_intf);
int actual_length = 0;
int 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 xbox360_set_vibration(
void *data, XINPUT_VIBRATION ff)
{
printk(KERN_INFO "Setting vibration!\n");
}
static void xbox360_set_led(
void *data, enum XINPUT_LED_STATUS led_status)
{
struct xbox360_context *ctx = data;
u8 packet[] = { 0x01, 0x03, led_status };
xbox360_send(ctx, packet, sizeof(packet));
}
static void xpad360_parse_input(void *data, PXINPUT_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 xbox360_receive(struct urb* urb)
{
struct xbox360_context *context = urb->context;
u8 *data = urb->transfer_buffer;
switch (urb->status) {
case 0:
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
return;
default:
goto finish;
}
/* Packets arrive respective to how the switch is laid out. */
switch(le16_to_cpup((u16*)&data[0])) {
case 0x0301: /* LED status */ /* What can we do with this? */
break;
case 0x0302: /* Unknown! */
case 0x0303: /* Unknown! */
break;
case 0x0308: /* Attachment */
break;
case 0x1400: {
XINPUT_GAMEPAD input;
xpad360_parse_input(&data[2], &input);
xusb_report_input(context->xusb_ctx, &input);
break;
}
}
finish:
usb_submit_urb(urb, GFP_ATOMIC);
}
static struct xusb_driver xbox360_driver = {
.set_led = xbox360_set_led,
.set_vibration = xbox360_set_vibration
};
static int xbox360_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 xbox360_context *ctx;
int error = 0;
void *in_buffer;
dma_addr_t in_dma;
ctx = kmalloc(sizeof(struct xbox360_context), GFP_KERNEL);
if (!ctx) {
return -ENOMEM;
}
usb_set_intfdata(intf, ctx);
ctx->usb_intf = intf;
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, XBOX360_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, XBOX360_PACKET_SIZE,
xbox360_receive, ctx, ep->bInterval);
error = usb_submit_urb(ctx->in, GFP_KERNEL);
if (error) {
error = -ENOMEM;
goto fail_in_submit;
}
ctx->xusb_ctx =
xusb_register_device(
&xbox360_driver,
&xbox360_devices[id - xbox360_table], ctx);
if (ctx->xusb_ctx < 0) {
error = -ENODEV;
goto fail_xusb;
}
return 0;
fail_xusb:
usb_kill_urb(ctx->in);
fail_in_submit:
usb_free_coherent(usb_dev, XBOX360_PACKET_SIZE, in_buffer, in_dma);
fail_alloc_coherent:
usb_free_urb(ctx->in);
fail_urb_alloc:
kfree(ctx);
return error;
}
static void xbox360_disconnect(struct usb_interface *intf)
{
struct xbox360_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, XBOX360_PACKET_SIZE,
in_urb->transfer_buffer, in_urb->transfer_dma);
usb_free_urb(in_urb);
xusb_unregister_device(ctx->xusb_ctx);
xbox360_set_led(ctx, XINPUT_LED_ROTATING);
xusb_flush();
kfree(ctx);
}
static struct usb_driver xbox360_usb_driver = {
.name = "xbox360",
.id_table = xbox360_table,
.probe = xbox360_probe,
.disconnect = xbox360_disconnect,
.soft_unbind = 1
};
module_usb_driver(xbox360_usb_driver);