forked from kakaroto/PSFreedom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
psfreedom_devices.c
511 lines (454 loc) · 15.5 KB
/
psfreedom_devices.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* psfreedom_devices.c -- PS3 Jailbreak exploit Gadget Driver
*
* Copyright (C) Youness Alaoui (KaKaRoTo)
*
* This software is distributed under the terms of the GNU General Public
* License ("GPL") version 3, as published by the Free Software Foundation.
*
* This code is based in part on:
*
* PSGroove
* USB MIDI Gadget Driver, Copyright (C) 2006 Thumtronics Pty Ltd.
* Gadget Zero driver, Copyright (C) 2003-2004 David Brownell.
* USB Audio driver, Copyright (C) 2002 by Takashi Iwai.
* USB MIDI driver, Copyright (C) 2002-2005 Clemens Ladisch.
*
*/
#include "psfreedom_devices.h"
static void jig_response_send (struct psfreedom_device *dev,
struct usb_request *req);
static void jig_response_complete(struct usb_ep *ep, struct usb_request *req)
{
struct psfreedom_device *dev = ep->driver_data;
int status = req->status;
unsigned long flags;
spin_lock_irqsave (&dev->lock, flags);
DBG (dev, "Jig response sent (status %d). Sent data so far : %d + %d\n",
status, dev->response_len, req->length);
switch (status) {
case 0: /* normal completion */
if (ep == dev->in_ep) {
/* our transmit completed.
see if there's more to go.
hub_transmit eats req, don't queue it again. */
dev->response_len += req->length;
if (dev->response_len < 64) {
jig_response_send (dev, req);
} else {
dev->status = DEVICE5_READY;
SET_TIMER (150);
}
spin_unlock_irqrestore (&dev->lock, flags);
return;
}
break;
/* this endpoint is normally active while we're configured */
case -ECONNABORTED: /* hardware forced ep reset */
case -ESHUTDOWN: /* disconnect from host */
VDBG(dev, "%s gone (%d), %d/%d\n", ep->name, status,
req->actual, req->length);
case -ECONNRESET: /* request dequeued */
hub_interrupt_queued = 0;
spin_unlock_irqrestore (&dev->lock, flags);
return;
case -EOVERFLOW: /* buffer overrun on read means that
* we didn't provide a big enough
* buffer.
*/
default:
DBG(dev, "%s complete --> %d, %d/%d\n", ep->name,
status, req->actual, req->length);
break;
case -EREMOTEIO: /* short read */
break;
}
status = usb_ep_queue(ep, req, GFP_ATOMIC);
if (status) {
ERROR(dev, "kill %s: resubmit %d bytes --> %d\n",
ep->name, req->length, status);
usb_ep_set_halt(ep);
/* FIXME recover later ... somehow */
}
spin_unlock_irqrestore (&dev->lock, flags);
}
/* Send the challenge response */
static void jig_response_send (struct psfreedom_device *dev,
struct usb_request *req)
{
struct usb_ep *ep = dev->in_ep;
if (!ep)
return;
if (!req)
req = alloc_ep_req(ep, 8);
if (!req) {
ERROR(dev, "hub_interrupt_transmit: alloc_ep_request failed\n");
return;
}
req->complete = jig_response_complete;
memcpy (req->buf, jig_response + dev->response_len, 8);
req->length = 8;
DBG (dev, "transmitting response. Sent so far %d\n", dev->response_len);
DBG (dev, "Sending %X %X %X %X %X %X %X %X\n",
((char *)req->buf)[0], ((char *)req->buf)[1],
((char *)req->buf)[2], ((char *)req->buf)[3],
((char *)req->buf)[4], ((char *)req->buf)[5],
((char *)req->buf)[6], ((char *)req->buf)[7]);
usb_ep_queue(ep, req, GFP_ATOMIC);
}
/* Received challenge data */
static void jig_interrupt_complete(struct usb_ep *ep, struct usb_request *req)
{
struct psfreedom_device *dev = ep->driver_data;
int status = req->status;
unsigned long flags;
spin_lock_irqsave (&dev->lock, flags);
DBG (dev, "******Out interrupt complete (status %d) : length %d, actual %d\n",
status, req->length, req->actual);
switch (status) {
case 0: /* normal completion */
if (ep == dev->out_ep) {
/* our transmit completed */
/* TODO handle data */
dev->challenge_len += req->actual;
DBG (dev, "******Challenge length : %d\n", dev->challenge_len);
if (dev->challenge_len >= 64) {
dev->status = DEVICE5_CHALLENGED;
SET_TIMER (450);
}
}
break;
/* this endpoint is normally active while we're configured */
case -ECONNABORTED: /* hardware forced ep reset */
case -ECONNRESET: /* request dequeued */
case -ESHUTDOWN: /* disconnect from host */
VDBG(dev, "%s gone (%d), %d/%d\n", ep->name, status,
req->actual, req->length);
spin_unlock_irqrestore (&dev->lock, flags);
return;
case -EOVERFLOW: /* buffer overrun on read means that
* we didn't provide a big enough
* buffer.
*/
default:
DBG(dev, "%s complete --> %d, %d/%d\n", ep->name,
status, req->actual, req->length);
break;
case -EREMOTEIO: /* short read */
break;
}
status = usb_ep_queue(ep, req, GFP_ATOMIC);
if (status) {
ERROR(dev, "kill %s: resubmit %d bytes --> %d\n",
ep->name, req->length, status);
usb_ep_set_halt(ep);
/* FIXME recover later ... somehow */
}
spin_unlock_irqrestore (&dev->lock, flags);
}
/* queue a request for receiving the challenge on endpoint 2 */
static void jig_interrupt_start (struct psfreedom_device *dev)
{
struct usb_ep *ep = dev->out_ep;
struct usb_request *req = NULL;
if (!ep)
return;
req = alloc_ep_req(ep, USB_BUFSIZ);
if (!req) {
ERROR(dev, "out_interrupt_transmit: alloc_ep_request failed\n");
return;
}
req->complete = jig_interrupt_complete;
req->length = 8;
usb_ep_queue(ep, req, GFP_ATOMIC);
}
static int set_jig_config(struct psfreedom_device *dev)
{
int err = 0;
err = usb_ep_enable(dev->out_ep, &jig_out_endpoint_desc);
if (err) {
ERROR(dev, "can't start %s: %d\n", dev->out_ep->name, err);
goto fail;
}
dev->out_ep->driver_data = dev;
DBG (dev, "Enabled BULK OUT endpoint\n");
err = usb_ep_enable(dev->in_ep, &jig_in_endpoint_desc);
if (err) {
ERROR(dev, "can't start %s: %d\n", dev->in_ep->name, err);
goto fail;
}
dev->in_ep->driver_data = dev;
DBG (dev, "Enabled BULK IN endpoint\n");
jig_interrupt_start (dev);
fail:
/* caller is responsible for cleanup on error */
return err;
}
static void
jig_reset_config(struct psfreedom_device *dev)
{
DBG(dev, "JIG reset config\n");
usb_ep_disable(dev->out_ep);
usb_ep_disable(dev->in_ep);
}
/* change our operational config. this code must agree with the code
* that returns config descriptors, and altsetting code.
*
* it's also responsible for power management interactions. some
* configurations might not work with our current power sources.
*
* note that some device controller hardware will constrain what this
* code can do, perhaps by disallowing more than one configuration or
* by limiting configuration choices (like the pxa2xx).
*/
static int
jig_set_config(struct psfreedom_device *dev, unsigned number)
{
int result = 0;
struct usb_gadget *gadget = dev->gadget;
jig_reset_config(dev);
result = set_jig_config(dev);
if (!result && !dev->in_ep) {
result = -ENODEV;
}
if (result) {
jig_reset_config(dev);
} else {
char *speed;
switch (gadget->speed) {
case USB_SPEED_LOW: speed = "low"; break;
case USB_SPEED_FULL: speed = "full"; break;
case USB_SPEED_HIGH: speed = "high"; break;
default: speed = "?"; break;
}
INFO(dev, "%s speed\n", speed);
}
return result;
}
/*
* The setup() callback implements all the ep0 functionality that's
* not handled lower down, in hardware or the hardware driver (like
* device and endpoint feature flags, and their status). It's all
* housekeeping for the gadget function we're implementing. Most of
* the work is in config-specific setup.
*/
static int devices_setup(struct usb_gadget *gadget,
const struct usb_ctrlrequest *ctrl, u16 request,
u16 w_index, u16 w_value, u16 w_length)
{
struct psfreedom_device *dev = get_gadget_data(gadget);
struct usb_request *req = dev->req;
int value = -EOPNOTSUPP;
/* usually this stores reply data in the pre-allocated ep0 buffer,
* but config change events will reconfigure hardware.
*/
switch (ctrl->bRequest) {
case USB_REQ_GET_DESCRIPTOR:
if ((ctrl->bRequestType & USB_DIR_IN) == 0) {
goto unknown;
}
switch (w_value >> 8) {
case USB_DT_DEVICE:
switch (dev->current_port) {
case 1:
value = min(w_length, (u16) sizeof(port1_device_desc));
memcpy(req->buf, port1_device_desc, value);
break;
case 2:
value = min(w_length, (u16) sizeof(port2_device_desc));
memcpy(req->buf, port2_device_desc, value);
break;
case 3:
value = min(w_length, (u16) sizeof(port3_device_desc));
memcpy(req->buf, port3_device_desc, value);
break;
case 4:
value = min(w_length, (u16) sizeof(port4_device_desc));
memcpy(req->buf, port4_device_desc, value);
break;
case 5:
value = min(w_length, (u16) sizeof(port5_device_desc));
memcpy(req->buf, port5_device_desc, value);
break;
case 6:
value = min(w_length, (u16) sizeof(port6_device_desc));
memcpy(req->buf, port6_device_desc, value);
break;
default:
value = -EINVAL;
break;
}
break;
case USB_DT_CONFIG:
value = 0;
switch (dev->current_port) {
case 1:
if ((w_value & 0xff) < 4) {
if (w_length == 8) {
value = sizeof(port1_short_config_desc);
memcpy(req->buf, port1_short_config_desc, value);
} else {
value = dev->port1_config_desc_size;
memcpy(req->buf, dev->port1_config_desc, value);
}
if ((w_value & 0xff) == 3 && w_length > 8) {
dev->status = DEVICE1_READY;
SET_TIMER (100);
}
}
break;
case 2:
value = sizeof(port2_config_desc);
memcpy(req->buf, port2_config_desc, value);
if (w_length > 8) {
dev->status = DEVICE2_READY;
SET_TIMER (150);
}
break;
case 3:
value = sizeof(port3_config_desc);
memcpy(req->buf, port3_config_desc, value);
if ((w_value & 0xff) == 1 && w_length > 8) {
dev->status = DEVICE3_READY;
SET_TIMER (80);
}
break;
case 4:
if ((w_value & 0xff) == 0) {
value = sizeof(port4_config_desc_1);
memcpy(req->buf, port4_config_desc_1, value);
} else if ((w_value & 0xff) == 1) {
if (w_length == 8) {
value = sizeof(port4_short_config_desc_2);
memcpy(req->buf, port4_short_config_desc_2, value);
} else {
value = sizeof(port4_config_desc_2);
memcpy(req->buf, port4_config_desc_2, value);
}
} else if ((w_value & 0xff) == 2) {
value = sizeof(port4_config_desc_3);
memcpy(req->buf, port4_config_desc_3, value);
if (w_length > 8) {
dev->status = DEVICE4_READY;
SET_TIMER (180);
}
}
break;
case 5:
value = sizeof(port5_config_desc);
memcpy(req->buf, port5_config_desc, value);
break;
case 6:
value = sizeof(port6_config_desc);
memcpy(req->buf, port6_config_desc, value);
break;
default:
value = -EINVAL;
break;
}
if (value >= 0)
value = min(w_length, (u16)value);
break;
case USB_DT_STRING:
value = 0;
break;
}
break;
case USB_REQ_SET_CONFIGURATION:
if (dev->current_port == 5) {
DBG (dev, "SET CONFIGURATION ON JIG\n");
jig_set_config(dev, 0);
}
value = 0;
break;
case USB_REQ_GET_CONFIGURATION:
case USB_REQ_GET_STATUS:
case USB_REQ_SET_INTERFACE:
if (dev->current_port == 5)
DBG (dev, "SET INTERFACE ON JIG\n");
value = 0;
break;
case USB_REQ_GET_INTERFACE:
if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) {
goto unknown;
}
*(u8 *)req->buf = 0;
value = min(w_length, (u16)1);
break;
case 0xAA:
INFO (dev, "JAILBROKEN!!! DONE!!!!!!!!!\n");
dev->status = DEVICE6_READY;
SET_TIMER (0);
value = 0;
break;
default:
unknown:
DBG(dev, "unknown control req%02x.%02x v%04x i%04x l%d\n",
ctrl->bRequestType, ctrl->bRequest,
w_value, w_index, w_length);
}
/* device either stalls (value < 0) or reports success */
return value;
}
static void devices_disconnect (struct usb_gadget *gadget)
{
struct psfreedom_device *dev = get_gadget_data (gadget);
jig_reset_config (dev);
}
static int __init devices_bind(struct usb_gadget *gadget,
struct psfreedom_device *dev)
{
struct usb_ep *out_ep;
struct usb_ep *in_ep;
gadget_for_each_ep (out_ep, gadget) {
if (0 == strcmp (out_ep->name,
psfreedom_get_endpoint_name (&jig_out_endpoint_desc)))
break;
}
if (!out_ep) {
ERROR (dev, "%s: can't find %s on %s\n",
psfreedom_get_endpoint_name (&jig_out_endpoint_desc),
shortname, gadget->name);
return -ENODEV;
}
out_ep->driver_data = out_ep; /* claim */
gadget_for_each_ep (in_ep, gadget) {
if (0 == strcmp (in_ep->name,
psfreedom_get_endpoint_name (&jig_in_endpoint_desc)))
break;
}
if (!in_ep) {
ERROR (dev, "%s: can't find %s on %s\n",
psfreedom_get_endpoint_name (&jig_in_endpoint_desc),
shortname, gadget->name);
return -ENODEV;
}
in_ep->driver_data = in_ep; /* claim */
/* ok, we made sense of the hardware ... */
dev->in_ep = in_ep;
dev->out_ep = out_ep;
/* If the machine specific code changed our descriptors,
change the ones we'll send too */
memcpy (port5_config_desc + USB_DT_CONFIG_SIZE + USB_DT_INTERFACE_SIZE,
&jig_out_endpoint_desc,
USB_DT_ENDPOINT_SIZE);
memcpy (port5_config_desc + USB_DT_CONFIG_SIZE + \
USB_DT_INTERFACE_SIZE + USB_DT_ENDPOINT_SIZE,
&jig_in_endpoint_desc,
USB_DT_ENDPOINT_SIZE);
INFO(dev, "using %s, EP IN %s (0x%X)\n", gadget->name, in_ep->name,
jig_in_endpoint_desc.bEndpointAddress);
INFO(dev, "using %s, EP OUT %s (0x%X)\n", gadget->name, out_ep->name,
jig_out_endpoint_desc.bEndpointAddress);
/* the max packet size of all the devices must be the same as the ep0 max
packet size, otherwise it won't work */
((struct usb_device_descriptor *)port1_device_desc)->bMaxPacketSize0 = \
((struct usb_device_descriptor *)port2_device_desc)->bMaxPacketSize0 = \
((struct usb_device_descriptor *)port3_device_desc)->bMaxPacketSize0 = \
((struct usb_device_descriptor *)port4_device_desc)->bMaxPacketSize0 = \
((struct usb_device_descriptor *)port5_device_desc)->bMaxPacketSize0 = \
((struct usb_device_descriptor *)port6_device_desc)->bMaxPacketSize0 = \
gadget->ep0->maxpacket;
VDBG(dev, "devices_bind finished ok\n");
return 0;
}