LibXenon
Bare-metal Xbox 360 homebrew library
Loading...
Searching...
No Matches
usbstorage.c
Go to the documentation of this file.
1/*-------------------------------------------------------------
2
3usbstorage.c -- Bulk-only USB mass storage support
4
5Copyright (C) 2008
6Sven Peter (svpe) <svpe@gmx.net>
7
8quick port to ehci/ios: Kwiirk
9
10This software is provided 'as-is', without any express or implied
11warranty. In no event will the authors be held liable for any
12damages arising from the use of this software.
13
14Permission is granted to anyone to use this software for any
15purpose, including commercial applications, and to alter it and
16redistribute it freely, subject to the following restrictions:
17
181. The origin of this software must not be misrepresented; you
19must not claim that you wrote the original software. If you use
20this software in a product, an acknowledgment in the product
21documentation would be appreciated but is not required.
22
232. Altered source versions must be plainly marked as such, and
24must not be misrepresented as being the original software.
25
263. This notice may not be removed or altered from any source
27distribution.
28
29-------------------------------------------------------------*/
30
31#include "xetypes.h"
32#include "diskio/disc_io.h"
33#include "usb.h"
34
35void (*mount_usb_device)(int device) = 0; // Mount callback for new devices
36
37#define s_printf printf
38
39#define ROUNDDOWN32(v) (((u32)(v)-0x1f)&~0x1f)
40
41//#define HEAP_SIZE (32*1024)
42//#define TAG_START 0x1//BADC0DE
43#define TAG_START 0x22112211
44
45#define CBW_SIZE 31
46#define CBW_SIGNATURE 0x43425355
47#define CBW_IN (1 << 7)
48#define CBW_OUT 0
49
50
51#define CSW_SIZE 13
52#define CSW_SIGNATURE 0x53425355
53
54#define SCSI_TEST_UNIT_READY 0x00
55#define SCSI_INQUIRY 0x12
56#define SCSI_REQUEST_SENSE 0x03
57#define SCSI_START_STOP 0x1b
58#define SCSI_READ_CAPACITY 0x25
59#define SCSI_READ_10 0x28
60#define SCSI_WRITE_10 0x2A
61
62#define SCSI_SENSE_REPLY_SIZE 18
63#define SCSI_SENSE_NOT_READY 0x02
64#define SCSI_SENSE_MEDIUM_ERROR 0x03
65#define SCSI_SENSE_HARDWARE_ERROR 0x04
66
67#define USB_CLASS_MASS_STORAGE 0x08
68#define USB_CLASS_HUB 0x09
69
70#define MASS_STORAGE_RBC_COMMANDS 0x01
71#define MASS_STORAGE_ATA_COMMANDS 0x02
72#define MASS_STORAGE_QIC_COMMANDS 0x03
73#define MASS_STORAGE_UFI_COMMANDS 0x04
74#define MASS_STORAGE_SFF8070_COMMANDS 0x05
75#define MASS_STORAGE_SCSI_COMMANDS 0x06
76
77#define MASS_STORAGE_BULK_ONLY 0x50
78
79#define USBSTORAGE_GET_MAX_LUN 0xFE
80#define USBSTORAGE_RESET 0xFF
81
82#define USB_ENDPOINT_BULK 0x02
83
84#ifdef HOMEBREW
85#define USBSTORAGE_CYCLE_RETRIES 3
86#else
87#define USBSTORAGE_CYCLE_RETRIES 10
88#endif
89
90#define MAX_TRANSFER_SIZE 4096
91
92#define DEVLIST_MAXSIZE 8
93
94static int ums_init_done = 0;
95
96extern int handshake_mode; // 0->timeout force -ENODEV 1->timeout return -ETIMEDOUT
97
98static BOOL first_access = TRUE;
99
100//static struct bdev * __bdev = NULL;
101
102typedef struct ehci_device_data{
103 struct ehci_hcd * __ehci;
106
109
112
115
116static unsigned int _ehci_device_count = 0;
117static ehci_device_data _ehci_data[DEVLIST_MAXSIZE];
118
119static void init_ehci_device_struct(){
120 int i= 0;
121 for(i=0;i<DEVLIST_MAXSIZE;i++)
122 {
123 _ehci_data[i].__lun = 16;
124 _ehci_data[i].__mounted = 0;
125 _ehci_data[i].__vid = 0;
126 _ehci_data[i].__pid = 0;
127 _ehci_data[i].__ready = 0;
128
129 _ehci_data[i].__ehci = &ehci_hcds[i];
130 }
131}
132
133// find the device with the same ehci ptr
135 ehci_device_data * _device_data = NULL;
136 int j = 0;
137 for(j=0;j<DEVLIST_MAXSIZE;j++){
138 if(_ehci_data[j].__ehci == ehci){
139 _device_data = &_ehci_data[j];
140 break;
141 }
142 }
143 return _device_data;
144}
145
146//0x1377E000
147
148//#define MEM_PRINT 1
149
150#ifdef MEM_PRINT
151
152// this dump from 0x13750000 to 0x13770000 log messages
153
154char mem_cad[32];
155
156char *mem_log = (char *) 0x13750000;
157
158#include <stdarg.h> // for the s_printf function
159
160void int_char(int num) {
161 int sign = num < 0;
162 int n, m;
163
164 if (num == 0) {
165 mem_cad[0] = '0';
166 mem_cad[1] = 0;
167 return;
168 }
169
170 for (n = 0; n < 10; n++) {
171 m = num % 10;
172 num /= 10;
173 if (m < 0) m = -m;
174 mem_cad[25 - n] = 48 + m;
175 }
176
177 mem_cad[26] = 0;
178
179 n = 0;
180 m = 16;
181 if (sign) {
182 mem_cad[n] = '-';
183 n++;
184 }
185
186 while (mem_cad[m] == '0') m++;
187
188 if (mem_cad[m] == 0) m--;
189
190 while (mem_cad[m]) {
191 mem_cad[n] = mem_cad[m];
192 n++;
193 m++;
194 }
195 mem_cad[n] = 0;
196
197}
198
199void hex_char(u32 num) {
200 int n, m;
201
202 if (num == 0) {
203 mem_cad[0] = '0';
204 mem_cad[1] = 0;
205 return;
206 }
207
208 for (n = 0; n < 8; n++) {
209 m = num & 15;
210 num >>= 4;
211 if (m >= 10) m += 7;
212 mem_cad[23 - n] = 48 + m;
213 }
214
215 mem_cad[24] = 0;
216
217 n = 0;
218 m = 16;
219
220 mem_cad[n] = '0';
221 n++;
222 mem_cad[n] = 'x';
223 n++;
224
225 while (mem_cad[m] == '0') m++;
226
227 if (mem_cad[m] == 0) m--;
228
229 while (mem_cad[m]) {
230 mem_cad[n] = mem_cad[m];
231 n++;
232 m++;
233 }
234 mem_cad[n] = 0;
235
236}
237
238void log_status(struct ehci_hcd * ehci, char *s) {
240 u32 statusp = ehci_readl(&ehci->regs->port_status[0]);
241
242 s_printf(" log_status (%s)\n", s);
243 s_printf(" status: %x %s%s%s%s%s%s%s%s%s%s\n",
244 status,
245 (status & STS_ASS) ? " Async" : "",
246 (status & STS_PSS) ? " Periodic" : "",
247 (status & STS_RECL) ? " Recl" : "",
248 (status & STS_HALT) ? " Halt" : "",
249 (status & STS_IAA) ? " IAA" : "",
250 (status & STS_FATAL) ? " FATAL" : "",
251 (status & STS_FLR) ? " FLR" : "",
252 (status & STS_PCD) ? " PCD" : "",
253 (status & STS_ERR) ? " ERR" : "",
254 (status & STS_INT) ? " INT" : ""
255 );
256
257 s_printf(" status port: %x\n", statusp);
258}
259#endif
260
261
262static s32 __usbstorage_reset(struct ehci_hcd * ehci, usbstorage_handle *dev, int hard_reset);
263static s32 __usbstorage_clearerrors(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 lun);
264static s32 __usbstorage_start_stop(usbstorage_handle *dev, u8 lun, u8 start_stop);
265
266// ehci driver has its own timeout.
267
268static s32 __USB_BlkMsgTimeout(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 bEndpoint, u16 wLength, void *rpData) {
269 return USB_WriteBlkMsg(ehci, dev->usb_fd, bEndpoint, wLength, rpData);
270}
271
272static s32 __USB_CtrlMsgTimeout(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 bmRequestType, u8 bmRequest, u16 wValue, u16 wIndex, u16 wLength, void *rpData) {
273 return USB_WriteCtrlMsg(ehci, dev->usb_fd, bmRequestType, bmRequest, wValue, wIndex, wLength, rpData);
274}
275
276static s32 __send_cbw(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 lun, u32 len, u8 flags, const u8 *cb, u8 cbLen) {
277 s32 retval = USBSTORAGE_OK;
278
279 if (cbLen == 0 || cbLen > 16 || !dev->buffer)
280 return -EINVAL;
281 memset(dev->buffer, 0, CBW_SIZE);
282
283 ((u32*) dev->buffer)[0] = cpu_to_le32(CBW_SIGNATURE);
284 ((u32*) dev->buffer)[1] = cpu_to_le32(dev->tag);
285 ((u32*) dev->buffer)[2] = cpu_to_le32(len);
286 dev->buffer[12] = flags;
287 dev->buffer[13] = lun;
288 // linux usb/storage/protocol.c seems to say only difference is padding
289 // and fixed cw size
290 if (dev->ata_protocol)
291 dev->buffer[14] = 12;
292 else
293 dev->buffer[14] = (cbLen > 6 ? 0x10 : 6);
294 //debug_printf("send cb of size %d\n",dev->buffer[14]);
295 memcpy(dev->buffer + 15, cb, cbLen);
296 //hexdump(dev->buffer,CBW_SIZE);
297 retval = __USB_BlkMsgTimeout(ehci, dev, dev->ep_out, CBW_SIZE, (void *) dev->buffer);
298
299 if (retval == CBW_SIZE) return USBSTORAGE_OK;
300 else if (retval >= 0) return USBSTORAGE_ESHORTWRITE;
301
302 return retval;
303}
304
305static s32 __read_csw(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 *status, u32 *dataResidue) {
306 s32 retval = USBSTORAGE_OK;
307 u32 signature, tag, _dataResidue, _status;
308
309 memset(dev->buffer, 0xff, CSW_SIZE);
310
311 retval = __USB_BlkMsgTimeout(ehci, dev, dev->ep_in, CSW_SIZE, dev->buffer);
312 //print_hex_dump_bytes("csv resp:",DUMP_PREFIX_OFFSET,dev->buffer,CSW_SIZE);
313
314 if (retval >= 0 && retval != CSW_SIZE) return USBSTORAGE_ESHORTREAD;
315 else if (retval < 0) return retval;
316
317 signature = le32_to_cpu(((u32*) dev->buffer)[0]);
318 tag = le32_to_cpu(((u32*) dev->buffer)[1]);
319 _dataResidue = le32_to_cpu(((u32*) dev->buffer)[2]);
320 _status = dev->buffer[12];
321 //debug_printf("csv status: %d\n",_status);
322 if (signature != CSW_SIGNATURE) {
323 // BUG();
325 }
326
327 if (dataResidue != NULL)
328 *dataResidue = _dataResidue;
329 if (status != NULL)
330 *status = _status;
331
332 if (tag != dev->tag) return USBSTORAGE_ETAG;
333 dev->tag++;
334
335 return USBSTORAGE_OK;
336}
337
338extern u32 usb_timeout;
339
340static s32 __cycle(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 lun, u8 *buffer, u32 len, u8 *cb, u8 cbLen, u8 write, u8 *_status, u32 *_dataResidue) {
341 s32 retval = USBSTORAGE_OK;
342
343 u8 status = 0;
344 u32 dataResidue = 0;
345 u32 thisLen;
346
347 u8 *buffer2 = buffer;
348 u32 len2 = len;
349
350 s8 retries = USBSTORAGE_CYCLE_RETRIES + 1;
351
352 do {
353
354 if (retval == -ENODEV) {
355 unplug_device = 1;
356 return -ENODEV;
357 }
358
359
360
361 if (retval < 0)
362 retval = __usbstorage_reset(ehci, dev, retries < USBSTORAGE_CYCLE_RETRIES);
363
364 retries--;
365 if (retval < 0) continue; // nuevo
366
367 buffer = buffer2;
368 len = len2;
369
370 if (write) {
371
372 retval = __send_cbw(ehci, dev, lun, len, CBW_OUT, cb, cbLen);
373 if (retval < 0)
374 continue; //reset+retry
375 while (len > 0) {
376 thisLen = len;
377 retval = __USB_BlkMsgTimeout(ehci, dev, dev->ep_out, thisLen, buffer);
378
379 if (retval == -ENODEV || retval == -ETIMEDOUT) break;
380
381 if (retval < 0)
382 continue; //reset+retry
383
384 if (retval != thisLen && len > 0) {
386 continue; //reset+retry
387 }
388 len -= retval;
389 buffer += retval;
390 }
391
392 if (retval < 0)
393 continue;
394 } else {
395 retval = __send_cbw(ehci, dev, lun, len, CBW_IN, cb, cbLen);
396
397 if (retval < 0)
398 continue; //reset+retry
399 while (len > 0) {
400 thisLen = len;
401
402 retval = __USB_BlkMsgTimeout(ehci, dev, dev->ep_in, thisLen, buffer);
403
404 if (retval == -ENODEV || retval == -ETIMEDOUT) break;
405
406 if (retval < 0)
407 continue; //reset+retry
408 //hexdump(buffer,retval);
409
410 len -= retval;
411 buffer += retval;
412
413 if (retval != thisLen) {
414 retval = -1;
415 continue; //reset+retry
416 }
417 }
418
419 if (retval < 0)
420 continue;
421 }
422
423 retval = __read_csw(ehci, dev, &status, &dataResidue);
424
425 if (retval < 0)
426 continue;
427
428 retval = USBSTORAGE_OK;
429 } while (retval < 0 && retries > 0);
430
431 // force unplug
432 if (retval < 0 && retries <= 0 && handshake_mode == 0) {
433 unplug_device = 1;
434 return -ENODEV;
435 }
436
437 if (retval < 0 && retval != USBSTORAGE_ETIMEDOUT) {
438 if (__usbstorage_reset(ehci, dev, 0) == USBSTORAGE_ETIMEDOUT)
439 retval = USBSTORAGE_ETIMEDOUT;
440 }
441
442
443 if (_status != NULL)
444 *_status = status;
445 if (_dataResidue != NULL)
446 *_dataResidue = dataResidue;
447
448 return retval;
449}
450
451static s32 __usbstorage_start_stop(usbstorage_handle *dev, u8 lun, u8 start_stop) {
452#if 0
453 s32 retval;
454 u8 cmd[16];
455
456 u8 status = 0;
457
458 memset(cmd, 0, sizeof (cmd));
459 cmd[0] = SCSI_START_STOP;
460 cmd[1] = (lun << 5) | 1;
461 cmd[4] = start_stop & 3;
462 cmd[5] = 0;
463 //memset(sense, 0, SCSI_SENSE_REPLY_SIZE);
464 retval = __cycle(dev, lun, NULL, 0, cmd, 6, 0, &status, NULL);
465 // if(retval < 0) goto error;
466
467 /*
468 if(status == SCSI_SENSE_NOT_READY || status == SCSI_SENSE_MEDIUM_ERROR || status == SCSI_SENSE_HARDWARE_ERROR)
469 retval = USBSTORAGE_ESENSE;*/
470 //error:
471 return retval;
472#else
473 return 0;
474#endif
475}
476
477static s32 __usbstorage_clearerrors(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 lun) {
478 s32 retval;
479 u8 cmd[16];
481 u8 status = 0;
482 memset(cmd, 0, sizeof (cmd));
483 cmd[0] = SCSI_TEST_UNIT_READY;
484 int n;
485
486 if (!sense) return -ENOMEM;
487
488
489
490 for (n = 0; n < 5; n++) {
491
492 retval = __cycle(ehci, dev, lun, NULL, 0, cmd, 6, 1, &status, NULL);
493
494#ifdef MEM_PRINT
495 s_printf(" SCSI_TEST_UNIT_READY %i# ret %i\n", n, retval);
496#endif
497
498 if (retval == -ENODEV) goto error;
499
500
501 if (retval == 0) break;
502 }
503 if (retval < 0) goto error;
504
505
506 if (status != 0) {
507 cmd[0] = SCSI_REQUEST_SENSE;
508 cmd[1] = lun << 5;
509 cmd[4] = SCSI_SENSE_REPLY_SIZE;
510 cmd[5] = 0;
511 memset(sense, 0, SCSI_SENSE_REPLY_SIZE);
512 retval = __cycle(ehci, dev, lun, sense, SCSI_SENSE_REPLY_SIZE, cmd, 6, 0, NULL, NULL);
513
514#ifdef MEM_PRINT
515 s_printf(" SCSI_REQUEST_SENSE ret %i\n", retval);
516#endif
517
518 if (retval < 0) goto error;
519
520 status = sense[2] & 0x0F;
521
522#ifdef MEM_PRINT
523 s_printf(" SCSI_REQUEST_SENSE status %x\n", status);
524#endif
525
526
528 retval = USBSTORAGE_ESENSE;
529 }
530error:
531 USB_Free(sense);
532 return retval;
533}
534
535static s32 __usbstorage_reset(struct ehci_hcd * ehci, usbstorage_handle *dev, int hard_reset) {
536 s32 retval;
537 u32 old_usb_timeout = usb_timeout;
538 usb_timeout = 1000 * 1000;
539 //int retry = hard_reset;
540 int retry = 0; //first try soft reset
541retry:
542 if (retry >= 1) {
543 u8 conf;
544 debug_printf("reset device..\n");
545 retval = ehci_reset_device(ehci, dev->usb_fd);
546 ehci_msleep(10);
547 if (retval == -ENODEV) return retval;
548
549 if (retval < 0 && retval != -7004)
550 goto end;
551 // reset configuration
552 if (USB_GetConfiguration(ehci, dev->usb_fd, &conf) < 0)
553 goto end;
554 if (/*conf != dev->configuration &&*/ USB_SetConfiguration(ehci, dev->usb_fd, dev->configuration) < 0)
555 goto end;
556 if (dev->altInterface != 0 && USB_SetAlternativeInterface(ehci, dev->usb_fd, dev->interface, dev->altInterface) < 0)
557 goto end;
558
559
560
561 // find the device with the same ehci ptr
562 ehci_device_data * _device_data = find_ehci_data(ehci);
563
564 if (_device_data->__lun != 16) {
565 if (USBStorage_MountLUN(ehci, &_device_data->__usbfd, _device_data->__lun) < 0)
566 goto end;
567 }
568 }
569 debug_printf("usbstorage reset..\n");
571
572#ifdef MEM_PRINT
573 s_printf("usbstorage reset: Reset ret %i\n", retval);
574
575#endif
576
577 /* FIXME?: some devices return -7004 here which definitely violates the usb ms protocol but they still seem to be working... */
578 if (retval < 0 && retval != -7004)
579 goto end;
580
581
582 /* gives device enough time to process the reset */
583 ehci_msleep(10);
584
585
586 debug_printf("clear halt on bulk ep..\n");
587 retval = USB_ClearHalt(ehci, dev->usb_fd, dev->ep_in);
588
589#ifdef MEM_PRINT
590 s_printf("usbstorage reset: clearhalt in ret %i\n", retval);
591
592#endif
593 if (retval < 0)
594 goto end;
595 ehci_msleep(10);
596 retval = USB_ClearHalt(ehci, dev->usb_fd, dev->ep_out);
597
598#ifdef MEM_PRINT
599 s_printf("usbstorage reset: clearhalt in ret %i\n", retval);
600
601#endif
602 if (retval < 0)
603 goto end;
604 ehci_msleep(50);
605 usb_timeout = old_usb_timeout;
606 return retval;
607
608end:
609 if (retval == -ENODEV) return retval;
610#ifdef HOMEBREW
611 if (disable_hardreset) return retval;
612 if (retry < 1) { //only 1 hard reset
613 ehci_msleep(100);
614 debug_printf("retry with hard reset..\n");
615 retry++;
616 goto retry;
617 }
618#else
619 if (retry < 5) {
620 ehci_msleep(100);
621 debug_printf("retry with hard reset..\n");
622
623 retry++;
624 goto retry;
625 }
626#endif
627 usb_timeout = old_usb_timeout;
628 return retval;
629}
630
631int my_memcmp(char *a, char *b, int size_b) {
632 int n;
633
634 for (n = 0; n < size_b; n++) {
635 if (*a != *b) return 1;
636 a++;
637 b++;
638 }
639 return 0;
640}
641
642static usb_devdesc _old_udd; // used for device change protection
643
645
647 s32 retval = -1;
648 u8 conf, *max_lun = NULL;
649 u32 iConf, iInterface, iEp;
650 static usb_devdesc udd;
653 usb_endpointdesc *ued;
654
655 struct ehci_hcd * ehci = device_data->__ehci;
656 usbstorage_handle *dev = &device_data->__usbfd;
657 struct ehci_device *fd = device_data->__dev;
658
659 device_data->__lun = 16; // select bad LUN
660
661 max_lun = USB_Alloc(1);
662 if (max_lun == NULL) return -ENOMEM;
663
664 memset(dev, 0, sizeof (*dev));
665
666 dev->tag = TAG_START;
667 dev->usb_fd = fd;
668
669
670 retval = USB_GetDescriptors(ehci, dev->usb_fd, &udd);
671
672#ifdef MEM_PRINT
673 s_printf("USBStorage_Open(): USB_GetDescriptors %i\n", retval);
674#ifdef MEM_PRINT
675 log_status(ehci, "after USB_GetDescriptors");
676#endif
677
678#endif
679
680 if (retval < 0)
681 goto free_and_return;
682
683 /*
684 // test device changed without unmount (prevent device write corruption)
685 if (ums_init_done) {
686 if (my_memcmp((void *) &_old_udd, (void *) &udd, sizeof (usb_devdesc) - 4)) {
687 USB_Free(max_lun);
688 USB_FreeDescriptors(&udd);
689#ifdef MEM_PRINT
690 s_printf("USBStorage_Open(): device changed!!!\n");
691
692#endif
693 return -ENODEV;
694 }
695 }
696 */
697 _old_udd = udd;
698 try_status = -128;
699 for (iConf = 0; iConf < udd.bNumConfigurations; iConf++) {
700 ucd = &udd.configurations[iConf];
701 for (iInterface = 0; iInterface < ucd->bNumInterfaces; iInterface++) {
702 uid = &ucd->interfaces[iInterface];
703 // debug_printf("interface %d, class:%x subclass %x protocol %x\n",iInterface,uid->bInterfaceClass,uid->bInterfaceSubClass, uid->bInterfaceProtocol);
712
713 dev->ata_protocol = 0;
715 dev->ata_protocol = 1;
716
717#ifdef MEM_PRINT
718 s_printf("USBStorage_Open(): interface subclass %i ata_prot %i \n", uid->bInterfaceSubClass, dev->ata_protocol);
719
720#endif
721 dev->ep_in = dev->ep_out = 0;
722 for (iEp = 0; iEp < uid->bNumEndpoints; iEp++) {
723 ued = &uid->endpoints[iEp];
725 continue;
726
728 dev->ep_in = ued->bEndpointAddress;
729 else
730 dev->ep_out = ued->bEndpointAddress;
731 }
732 if (dev->ep_in != 0 && dev->ep_out != 0) {
734 dev->interface = uid->bInterfaceNumber;
736
737 goto found;
738 }
739 } else {
740
741
742 if (uid->endpoints != NULL)
743 USB_Free(uid->endpoints);
744 uid->endpoints = NULL;
745 if (uid->extra != NULL)
746 USB_Free(uid->extra);
747 uid->extra = NULL;
748
749 if (uid->bInterfaceClass == USB_CLASS_HUB) {
751 try_status = -20000;
752
754
755 goto free_and_return;
756 }
757
760 try_status = -(10000 + uid->bInterfaceSubClass);
761 }
762 }
763 }
764 }
765
766#ifdef MEM_PRINT
767 s_printf("USBStorage_Open(): cannot find any interface!!!\n");
768
769#endif
772 debug_printf("cannot find any interface\n");
773 goto free_and_return;
774
775found:
777
778 retval = USBSTORAGE_EINIT;
779 try_status = -1201;
780
781#ifdef MEM_PRINT
782 s_printf("USBStorage_Open(): conf: %x altInterface: %x\n", dev->configuration, dev->altInterface);
783
784#endif
785
786 if (USB_GetConfiguration(ehci, dev->usb_fd, &conf) < 0)
787 goto free_and_return;
788 try_status = -1202;
789
790#ifdef MEM_PRINT
791 log_status(ehci, "after USB_GetConfiguration");
792#endif
793
794#ifdef MEM_PRINT
795 if (conf != dev->configuration)
796 s_printf("USBStorage_Open(): changing conf from %x\n", conf);
797
798#endif
799 if (/*conf != dev->configuration &&*/ USB_SetConfiguration(ehci, dev->usb_fd, dev->configuration) < 0)
800 goto free_and_return;
801
802 try_status = -1203;
803 if (dev->altInterface != 0 && USB_SetAlternativeInterface(ehci, dev->usb_fd, dev->interface, dev->altInterface) < 0)
804 goto free_and_return;
805
806 try_status = -1204;
807
808#ifdef MEM_PRINT
809 log_status(ehci, "Before USBStorage_Reset");
810#endif
811 retval = USBStorage_Reset(ehci, dev);
812#ifdef MEM_PRINT
813 log_status(ehci, "After USBStorage_Reset");
814#endif
815 if (retval < 0)
816 goto free_and_return;
817
818
819
820 /* retval = __USB_CtrlMsgTimeout(dev, (USB_CTRLTYPE_DIR_DEVICE2HOST | USB_CTRLTYPE_TYPE_CLASS | USB_CTRLTYPE_REC_INTERFACE), USBSTORAGE_GET_MAX_LUN, 0, dev->interface, 1, max_lun);
821 if(retval < 0 )
822 dev->max_lun = 1;
823 else
824 dev->max_lun = (*max_lun+1);
825
826
827 if(retval == USBSTORAGE_ETIMEDOUT)*/
828
829 /* NOTE: from usbmassbulk_10.pdf "Devices that do not support multiple LUNs may STALL this command." */
830 dev->max_lun = 8; // max_lun can be from 1 to 16, but some devices do not support lun
831
832 retval = USBSTORAGE_OK;
833
834 /*if(dev->max_lun == 0)
835 dev->max_lun++;*/
836
837 /* taken from linux usbstorage module (drivers/usb/storage/transport.c) */
838 /*
839 * Some devices (i.e. Iomega Zip100) need this -- apparently
840 * the bulk pipes get STALLed when the GetMaxLUN request is
841 * processed. This is, in theory, harmless to all other devices
842 * (regardless of if they stall or not).
843 */
844 //USB_ClearHalt(dev->usb_fd, dev->ep_in);
845 //USB_ClearHalt(dev->usb_fd, dev->ep_out);
846
848
849 if (dev->buffer == NULL) {
850 retval = -ENOMEM;
851 try_status = -1205;
852 } else retval = USBSTORAGE_OK;
853
854free_and_return:
855
856 if (max_lun != NULL) USB_Free(max_lun);
857
858 if (retval < 0) {
859 if (dev->buffer != NULL)
860 USB_Free(dev->buffer);
861 memset(dev, 0, sizeof (*dev));
862
863#ifdef MEM_PRINT
864 s_printf("USBStorage_Open(): try_status %i\n", try_status);
865
866#endif
867 return retval;
868 }
869
870#ifdef MEM_PRINT
871 s_printf("USBStorage_Open(): return 0\n");
872
873#endif
874
875 return 0;
876}
877
879 if (dev->buffer != NULL)
880 USB_Free(dev->buffer);
881 memset(dev, 0, sizeof (*dev));
882
883 return 0;
884}
885
887 s32 retval;
888
889 retval = __usbstorage_reset(ehci, dev, 0);
890
891 return retval;
892}
893
895
896 return dev->max_lun;
897}
898
900 s32 retval;
901 int f = handshake_mode;
902
903 if (lun >= dev->max_lun)
904 return -EINVAL;
905 usb_timeout = 1000 * 1000;
906 handshake_mode = 1;
907
908 retval = __usbstorage_start_stop(dev, lun, 1);
909
910#ifdef MEM_PRINT
911 s_printf(" start_stop cmd ret %i\n", retval);
912#endif
913 if (retval < 0)
914 goto ret;
915
916 retval = __usbstorage_clearerrors(ehci, dev, lun);
917 if (retval < 0)
918 goto ret;
919 usb_timeout = 1000 * 1000;
920 retval = USBStorage_Inquiry(ehci, dev, lun);
921#ifdef MEM_PRINT
922 s_printf(" Inquiry ret %i\n", retval);
923#endif
924 if (retval < 0)
925 goto ret;
926 retval = USBStorage_ReadCapacity(ehci, dev, lun, &dev->sector_size[lun], &dev->n_sector[lun]);
927#ifdef MEM_PRINT
928 s_printf(" ReadCapacity ret %i\n", retval);
929#endif
930ret:
931 handshake_mode = f;
932 return retval;
933}
934
936 s32 retval;
937 u8 cmd[] = {SCSI_INQUIRY, lun << 5, 0, 0, 36, 0};
938 u8 *response = USB_Alloc(36);
939
940 if (!response) return -ENOMEM;
941
942 retval = __cycle(ehci, dev, lun, response, 36, cmd, 6, 0, NULL, NULL);
943 //print_hex_dump_bytes("inquiry result:",DUMP_PREFIX_OFFSET,response,36);
944 USB_Free(response);
945 return retval;
946}
947
948s32 USBStorage_ReadCapacity(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 lun, u32 *sector_size, u32 *n_sectors) {
949 s32 retval;
950 u8 cmd[] = {SCSI_READ_CAPACITY, lun << 5};
951 u8 *response = USB_Alloc(8);
952 u32 val;
953 if (!response) return -ENOMEM;
954
955 retval = __cycle(ehci, dev, lun, response, 8, cmd, 2, 0, NULL, NULL);
956 if (retval >= 0) {
957
958 memcpy(&val, response, 4);
959 if (n_sectors != NULL)
960 *n_sectors = be32_to_cpu(val);
961 memcpy(&val, response + 4, 4);
962 if (sector_size != NULL)
963 *sector_size = be32_to_cpu(val);
964 retval = USBSTORAGE_OK;
965 }
966 USB_Free(response);
967 return retval;
968}
969
970static s32 __USBStorage_Read(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 lun, u32 sector, u16 n_sectors, u8 *buffer) {
971 u8 status = 0;
972 s32 retval;
973 u8 cmd[] = {
975 lun << 5,
976 sector >> 24,
977 sector >> 16,
978 sector >> 8,
979 sector,
980 0,
981 n_sectors >> 8,
982 n_sectors,
983 0
984 };
985 if (lun >= dev->max_lun || dev->sector_size[lun] == 0 || !dev)
986 return -EINVAL;
987
988 retval = __cycle(ehci, dev, lun, buffer, n_sectors * dev->sector_size[lun], cmd, sizeof (cmd), 0, &status, NULL);
989 if (retval > 0 && status != 0)
990 retval = USBSTORAGE_ESTATUS;
991 return retval;
992}
993
994static s32 __USBStorage_Write(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 lun, u32 sector, u16 n_sectors, const u8 *buffer) {
995 u8 status = 0;
996 s32 retval;
997 u8 cmd[] = {
999 lun << 5,
1000 sector >> 24,
1001 sector >> 16,
1002 sector >> 8,
1003 sector,
1004 0,
1005 n_sectors >> 8,
1006 n_sectors,
1007 0
1008 };
1009 if (lun >= dev->max_lun || dev->sector_size[lun] == 0)
1010 return -EINVAL;
1011 retval = __cycle(ehci, dev, lun, (u8 *) buffer, n_sectors * dev->sector_size[lun], cmd, sizeof (cmd), 1, &status, NULL);
1012 if (retval > 0 && status != 0)
1013 retval = USBSTORAGE_ESTATUS;
1014 return retval;
1015}
1016
1017s32 USBStorage_Read(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 lun, u32 sector, u16 n_sectors, u8 *buffer) {
1018 u32 max_sectors = n_sectors;
1019 u32 sectors;
1020 s32 ret = -1;
1021
1022 if (n_sectors * dev->sector_size[lun] > 32768) max_sectors = 32768 / dev->sector_size[lun];
1023
1024 while (n_sectors > 0) {
1025 sectors = n_sectors > max_sectors ? max_sectors : n_sectors;
1026 ret = __USBStorage_Read(ehci, dev, lun, sector, sectors, buffer);
1027 if (ret < 0) return ret;
1028
1029 n_sectors -= sectors;
1030 sector += sectors;
1031 buffer += sectors * dev->sector_size[lun];
1032 }
1033
1034 return ret;
1035}
1036
1037s32 USBStorage_Write(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 lun, u32 sector, u16 n_sectors, const u8 *buffer) {
1038 u32 max_sectors = n_sectors;
1039 u32 sectors;
1040 s32 ret = -1;
1041
1042 if ((n_sectors * dev->sector_size[lun]) > 32768) max_sectors = 32768 / dev->sector_size[lun];
1043
1044 while (n_sectors > 0) {
1045 sectors = n_sectors > max_sectors ? max_sectors : n_sectors;
1046 ret = __USBStorage_Write(ehci, dev, lun, sector, sectors, buffer);
1047 if (ret < 0) return ret;
1048
1049 n_sectors -= sectors;
1050 sector += sectors;
1051 buffer += sectors * dev->sector_size[lun];
1052 }
1053
1054 return ret;
1055}
1056/*
1057The following is for implementing the ioctl interface inpired by the disc_io.h
1058as used by libfat
1059
1060This opens the first lun of the first usbstorage device found.
1061 */
1062#if 0
1063/* perform 512 time the same read */
1064s32 USBStorage_Read_Stress(u32 sector, u32 numSectors, void *buffer) {
1065 s32 retval;
1066 int i;
1067
1068 ehci_device_data * device_data = find_ehci_data(ehci);
1069
1070 if (__mounted != 1)
1071 return FALSE;
1072
1073 for (i = 0; i < 512; i++) {
1074 retval = USBStorage_Read(__ehci, &__usbfd, __lun, sector, numSectors, buffer);
1075 sector += numSectors;
1076 if (retval == USBSTORAGE_ETIMEDOUT) {
1077 __mounted = 0;
1078 USBStorage_Close(&__usbfd);
1079 }
1080 if (retval < 0)
1081 return FALSE;
1082 }
1083 return TRUE;
1084
1085}
1086#endif
1087
1088
1089
1090// temp function before libfat is available */
1091
1093 struct ehci_hcd * ehci; struct ehci_device *fd;
1094 int maxLun, j, retval;
1095 int test_max_lun = 1;
1096
1097 ehci = device_data->__ehci;
1098 fd = device_data->__dev;
1099
1100 try_status = -120;
1101 if (USBStorage_Open(device_data) < 0)
1102 return -EINVAL;
1103
1104 /*
1105 maxLun = USBStorage_GetMaxLUN(&__usbfd);
1106 if(maxLun == USBSTORAGE_ETIMEDOUT)
1107 {
1108 __mounted = 0;
1109 USBStorage_Close(&__usbfd);
1110 return -EINVAL;
1111 }
1112 */
1113
1114 maxLun = 1;
1115 device_data->__usbfd.max_lun = 1;
1116
1117 j = 0;
1118 //for(j = 0; j < maxLun; j++)
1119 while (1) {
1120
1121#ifdef MEM_PRINT
1122 s_printf("USBStorage_MountLUN %i#\n", j);
1123#endif
1124 retval = USBStorage_MountLUN(ehci, &device_data->__usbfd, j);
1125#ifdef MEM_PRINT
1126 s_printf("USBStorage_MountLUN: ret %i\n", retval);
1127#endif
1128
1129
1130 if (retval == USBSTORAGE_ETIMEDOUT /*&& test_max_lun==0*/) {
1131 //USBStorage_Reset(&__usbfd);
1132 try_status = -121;
1133 device_data->__mounted = 0;
1134 USBStorage_Close(&device_data->__usbfd);
1135 return -EINVAL;
1136 // break;
1137 }
1138
1139 if (retval < 0) {
1140 if (test_max_lun) {
1141 device_data->__usbfd.max_lun = 0;
1142 retval = __USB_CtrlMsgTimeout(ehci, &device_data->__usbfd,
1144 USBSTORAGE_GET_MAX_LUN, 0, device_data->__usbfd.interface, 1, &device_data->__usbfd.max_lun);
1145 if (retval < 0)
1146 device_data->__usbfd.max_lun = 1;
1147 else device_data->__usbfd.max_lun++;
1148 maxLun =device_data-> __usbfd.max_lun;
1149
1150#ifdef MEM_PRINT
1151 s_printf("USBSTORAGE_GET_MAX_LUN ret %i maxlun %i\n", retval, maxLun);
1152#endif
1153 test_max_lun = 0;
1154 } else j++;
1155
1156 if (j >= maxLun) break;
1157 continue;
1158 }
1159
1160 device_data->__vid = fd->desc.idVendor;
1161 device_data->__pid = fd->desc.idProduct;
1162 device_data->__mounted = 1;
1163 device_data->__lun = j;
1164 usb_timeout = 1000 * 1000;
1165 try_status = 0;
1166 return 0;
1167 }
1168 try_status = -122;
1169 device_data->__mounted = 0;
1170 USBStorage_Close(&device_data->__usbfd);
1171
1172#ifdef MEM_PRINT
1173 s_printf("USBStorage_MountLUN fail!!!\n");
1174#endif
1175
1176 return -EINVAL;
1177}
1178
1179static int USBStorage_True(void) {
1180 return true;
1181}
1182
1183static int USBStorage_Inserted(void) {
1184 return 1;
1185}
1186
1187#if 0
1188void USBStorage_Umount(void) {
1189 if (!ums_init_done) return;
1190
1191 ehci_device_data * device_data = find_ehci_data(ehci);
1192
1193 if (__mounted && !unplug_device) {
1194 if (__usbstorage_start_stop(&__usbfd, __lun, 0x0) == 0) // stop
1195 ehci_msleep(1000);
1196 }
1197
1198 USBStorage_Close(&__usbfd);
1199 __lun = 16;
1200 __mounted = 0;
1201 ums_init_done = 0;
1202 unplug_device = 0;
1203 //unregister_bdev(__bdev);
1204}
1205#endif
1206
1207/*
1208struct bdev_ops usb2mass_ops =
1209{
1210 .read = usb2mass_read,
1211 .write = usb2mass_write
1212};
1213 */
1214
1216 int i, j, retries = 1;
1217 // debug_printf("usbstorage init %d\n", ums_init_done);
1218 if (!ums_init_done)
1219 {
1220 _ehci_device_count = 0;
1221 init_ehci_device_struct();
1222 }
1223 struct ehci_hcd *ehci = &ehci_hcds[0];
1224
1225 try_status = -1;
1226
1227 for (j = 0; j < EHCI_HCD_COUNT; j++) {
1228
1229 for (i = 0; i < ehci->num_port; i++) {
1230 struct ehci_device *dev = &ehci->devices[i];
1231
1232retry:
1233 dev->port = i;
1234 if (dev->id != 0 && dev->busy == 0) {
1235 handshake_mode = 1;
1236 if (ehci_reset_port(ehci, i) >= 0) {
1237
1238 //ehci_device_data * device_data = find_ehci_data(ehci);
1239
1240 ehci_device_data * device_data = &_ehci_data[_ehci_device_count];
1241 dev->busy = 1;
1242 device_data->__ehci = ehci;
1243 device_data->__dev = dev;
1244
1245 if (USBStorage_Try_Device(device_data) == 0) {
1246 printf("EHCI bus %d device %d: vendor %04X product %04X : Mass-Storage Device\n", j, dev->id, device_data->__vid, device_data->__pid);
1247
1248 first_access = TRUE;
1249 handshake_mode = 0;
1250 ums_init_done = 1;
1251 unplug_device = 0;
1252 //__bdev=register_bdev(NULL, &usb2mass_ops, "uda");
1253 //register_disc_interface(&usb2mass_ops);
1254 device_data->__ready = 1;
1255 if (mount_usb_device)
1256 mount_usb_device(_ehci_device_count);
1257 _ehci_device_count++;
1258#ifdef MEM_PRINT
1259 s_printf("USBStorage_Init() Ok\n");
1260
1261#endif
1262
1263 //return 0;
1264 }
1265 }
1266 } else if (dev->busy == 0) {
1267 u32 status;
1268 handshake_mode = 1;
1270
1271#ifdef MEM_PRINT
1272 s_printf("USBStorage_Init() status %x\n", status);
1273
1274#endif
1275
1276 if (status & 1) {
1277 if (ehci_reset_port2(ehci, i) < 0) {
1278 ehci_msleep(100);
1280 }
1281 if (retries) {
1282 retries--;
1283 goto retry;
1284 }
1285 }
1286 }
1287 }
1288
1289 ehci++;
1290 }
1291
1292 return try_status;
1293}
1294//#if 0
1295//s32 USBStorage_Get_Capacity(u32*sector_size) {
1296// if (__mounted == 1) {
1297// if (sector_size) {
1298// *sector_size = __usbfd.sector_size[__lun];
1299// }
1300// return __usbfd.n_sector[__lun];
1301// }
1302// return 0;
1303//}
1304//#endif
1305
1306s32 USBStorage_Get_Capacity(int device, u32* sector_size) {
1307 if (_ehci_data[device].__mounted == 1) {
1308 if (sector_size) {
1309 *sector_size = _ehci_data[device].__usbfd.sector_size[_ehci_data[device].__lun];
1310 }
1311 return _ehci_data[device].__usbfd.n_sector[_ehci_data[device].__lun];
1312 }
1313 return 0;
1314}
1315
1316int unplug_procedure(int device) {
1317 int retval = 1;
1318 if (unplug_device != 0) {
1319
1320 if (_ehci_data[device].__usbfd.usb_fd)
1321 if (ehci_reset_port2(_ehci_data[device].__ehci, /*__usbfd.usb_fd->port*/0) >= 0) {
1322 if (_ehci_data[device].__usbfd.buffer != NULL)
1323 USB_Free(_ehci_data[device].__usbfd.buffer);
1324 _ehci_data[device].__usbfd.buffer = NULL;
1325
1326 if (ehci_reset_port(_ehci_data[device].__ehci, 0) >= 0) {
1327 handshake_mode = 1;
1328 if (USBStorage_Try_Device(&_ehci_data[device]) == 0) {
1329 retval = 0;
1330 unplug_device = 0;
1331 }
1332 handshake_mode = 0;
1333 }
1334 }
1335 ehci_msleep(100);
1336 }
1337
1338 return retval;
1339}
1340
1341
1342s32 USBStorage_Read_Sectors(int device, u32 sector, u32 numSectors, void *buffer) {
1343 s32 retval = 0;
1344 int retry;
1345
1346 if (_ehci_data[device].__mounted != 1)
1347 return FALSE;
1348
1349 for (retry = 0; retry < 16; retry++) {
1350 if (retry > 12) retry = 12; // infinite loop
1351 //ehci_usleep(100);
1352 if (!unplug_procedure(device)) {
1353 retval = 0;
1354 }
1355
1356 if (retval == USBSTORAGE_ETIMEDOUT && retry > 0) {
1357 unplug_device = 1;
1358 /*retval=__usbstorage_reset(&__usbfd,1);
1359 if(retval>=0) retval=-666;
1360 ehci_msleep(10);*/
1361 }
1362 if (unplug_device != 0) continue;
1363 //if(retval==-ENODEV) return 0;
1364 usb_timeout = 1000 * 1000; // 4 seconds to wait
1365 if (retval >= 0)
1366 retval = USBStorage_Read(_ehci_data[device].__ehci, &_ehci_data[device].__usbfd, _ehci_data[device].__lun, sector, numSectors, buffer);
1367 usb_timeout = 1000 * 1000;
1368 if (unplug_device != 0) continue;
1369 //if(retval==-ENODEV) return 0;
1370 if (retval >= 0) break;
1371 }
1372
1373 if (retval < 0)
1374 return FALSE;
1375 return TRUE;
1376}
1377
1378s32 USBStorage_Write_Sectors(int device, u32 sector, u32 numSectors, const void *buffer) {
1379 s32 retval = 0;
1380 int retry;
1381
1382 if (_ehci_data[device].__mounted != 1)
1383 return FALSE;
1384
1385 for (retry = 0; retry < 16; retry++) {
1386 if (retry > 12) retry = 12; // infinite loop
1387
1388 //ehci_usleep(100);
1389
1390 if (!unplug_procedure(device)) {
1391 retval = 0;
1392 }
1393
1394 if (retval == USBSTORAGE_ETIMEDOUT && retry > 0) {
1395 unplug_device = 1;
1396 //retval=__usbstorage_reset(&__usbfd,1);
1397 //if(retval>=0) retval=-666;
1398 }
1399 if (unplug_device != 0) continue;
1400 usb_timeout = 1000 * 1000; // 4 seconds to wait
1401 if (retval >= 0)
1402 retval = USBStorage_Write(_ehci_data[device].__ehci, &_ehci_data[device].__usbfd, _ehci_data[device].__lun, sector, numSectors, buffer);
1403 usb_timeout = 1000 * 1000;
1404 if (unplug_device != 0) continue;
1405 if (retval >= 0) break;
1406 }
1407
1408 if (retval < 0)
1409 return FALSE;
1410 return TRUE;
1411}
1412
1413s32 USBStorage_Read_Sectors_0(u32 sector, u32 numSectors, void *buffer){
1414 return USBStorage_Read_Sectors(0, sector, numSectors, buffer);
1415}
1416s32 USBStorage_Write_Sectors_0(u32 sector, u32 numSectors, const void *buffer){
1417 return USBStorage_Write_Sectors(0, sector, numSectors, buffer);
1418}
1419s32 USBStorage_Read_Sectors_1(u32 sector, u32 numSectors, void *buffer){
1420 return USBStorage_Read_Sectors(1, sector, numSectors, buffer);
1421}
1422s32 USBStorage_Write_Sectors_1(u32 sector, u32 numSectors, const void *buffer){
1423 return USBStorage_Write_Sectors(1, sector, numSectors, buffer);
1424}
1425s32 USBStorage_Read_Sectors_2(u32 sector, u32 numSectors, void *buffer){
1426 return USBStorage_Read_Sectors(2, sector, numSectors, buffer);
1427}
1428s32 USBStorage_Write_Sectors_2(u32 sector, u32 numSectors, const void *buffer){
1429 return USBStorage_Write_Sectors(2, sector, numSectors, buffer);
1430}
1432 return _ehci_data[0].__ready;
1433}
1435 return _ehci_data[1].__ready;
1436}
1438 return _ehci_data[2].__ready;
1439}
1440
1442 u32 tmp;
1443 return USBStorage_Get_Capacity(0, &tmp);
1444}
1445
1447 u32 tmp;
1448 return USBStorage_Get_Capacity(1, &tmp);
1449}
1450
1452 u32 tmp;
1453 return USBStorage_Get_Capacity(2, &tmp);
1454}
1455
1460 .clearStatus = (FN_MEDIUM_CLEARSTATUS) & USBStorage_True,
1461 .shutdown = (FN_MEDIUM_SHUTDOWN) & USBStorage_True,
1463 .startup = (FN_MEDIUM_STARTUP) & USBStorage_True,
1464 .ioType = FEATURE_XENON_USB,
1466};
1467
1472 .clearStatus = (FN_MEDIUM_CLEARSTATUS) & USBStorage_True,
1473 .shutdown = (FN_MEDIUM_SHUTDOWN) & USBStorage_True,
1475 .startup = (FN_MEDIUM_STARTUP) & USBStorage_True,
1476 .ioType = FEATURE_XENON_USB,
1478};
1479
1484 .clearStatus = (FN_MEDIUM_CLEARSTATUS) & USBStorage_True,
1485 .shutdown = (FN_MEDIUM_SHUTDOWN) & USBStorage_True,
1487 .startup = (FN_MEDIUM_STARTUP) & USBStorage_True,
1488 .ioType = FEATURE_XENON_USB,
1490};
1491
1496 .clearStatus = (FN_MEDIUM_CLEARSTATUS) & USBStorage_True,
1497 .shutdown = (FN_MEDIUM_SHUTDOWN) & USBStorage_True,
1499 .startup = (FN_MEDIUM_STARTUP) & USBStorage_True,
1500 .ioType = FEATURE_XENON_USB,
1502};
1503
1504
#define NULL
Definition: def.h:47
bool(* FN_MEDIUM_STARTUP)(void)
Definition: disc_io.h:47
bool(* FN_MEDIUM_CLEARSTATUS)(void)
Definition: disc_io.h:51
#define FEATURE_MEDIUM_CANREAD
Definition: disc_io.h:39
s32(* FN_MEDIUM_DEVSECTORS)(void)
Definition: disc_io.h:53
bool(* FN_MEDIUM_SHUTDOWN)(void)
Definition: disc_io.h:52
#define FEATURE_XENON_USB
Definition: disc_io.h:43
bool(* FN_MEDIUM_READSECTORS)(sec_t sector, sec_t numSectors, void *buffer)
Definition: disc_io.h:49
bool(* FN_MEDIUM_WRITESECTORS)(sec_t sector, sec_t numSectors, const void *buffer)
Definition: disc_io.h:50
#define FEATURE_MEDIUM_CANWRITE
Definition: disc_io.h:40
bool(* FN_MEDIUM_ISINSERTED)(void)
Definition: disc_io.h:48
int ehci_reset_device(struct ehci_hcd *ehci, struct ehci_device *dev)
Definition: ehci.c:979
int ehci_reset_port(struct ehci_hcd *ehci, int port)
Definition: ehci.c:959
int unplug_device
Definition: ehci.c:160
int ehci_reset_port2(struct ehci_hcd *ehci, int port)
Definition: ehci.c:966
s32 USBStorage_Read_Stress(u32 sector, u32 numSectors, void *buffer)
void ehci_msleep(int time)
Definition: usb_os.c:110
struct ehci_hcd * ehci
Definition: ehci.h:23
u32 status
Definition: ehci_defs.h:15
#define STS_ERR
Definition: ehci_defs.h:26
#define STS_FLR
Definition: ehci_defs.h:24
#define STS_PSS
Definition: ehci_defs.h:17
#define STS_FATAL
Definition: ehci_defs.h:23
#define STS_ASS
Definition: ehci_defs.h:16
#define STS_HALT
Definition: ehci_defs.h:19
#define STS_PCD
Definition: ehci_defs.h:25
#define STS_INT
Definition: ehci_defs.h:27
#define STS_RECL
Definition: ehci_defs.h:18
#define STS_IAA
Definition: ehci_defs.h:22
static uint32_t val
Definition: io.h:17
int write(int fileDesc, const void *ptr, size_t len)
Definition: newlib.c:584
FN_MEDIUM_DEVSECTORS sectors
Definition: disc_io.h:64
struct _usbinterfacedesc * interfaces
Definition: usb.h:104
u8 bConfigurationValue
Definition: usb.h:100
u8 bNumInterfaces
Definition: usb.h:99
u8 bNumConfigurations
Definition: usb.h:122
struct _usbconfdesc * configurations
Definition: usb.h:123
u8 bmAttributes
Definition: usb.h:73
u8 bEndpointAddress
Definition: usb.h:72
u8 bInterfaceNumber
Definition: usb.h:82
u8 bNumEndpoints
Definition: usb.h:84
u8 bInterfaceSubClass
Definition: usb.h:86
struct _usbendpointdesc * endpoints
Definition: usb.h:91
u8 * extra
Definition: usb.h:89
u8 bAlternateSetting
Definition: usb.h:83
u8 bInterfaceClass
Definition: usb.h:85
u8 bInterfaceProtocol
Definition: usb.h:87
usbstorage_handle __usbfd
Definition: usbstorage.c:105
struct ehci_device * __dev
Definition: usbstorage.c:104
struct ehci_hcd * __ehci
Definition: usbstorage.c:103
int busy
Definition: ehci.h:55
int fd
Definition: ehci.h:53
int port
Definition: ehci.h:52
int id
Definition: ehci.h:51
Definition: ehci.h:75
struct ehci_device devices[EHCI_MAX_ROOT_PORTS]
Definition: ehci.h:99
struct ehci_regs __iomem * regs
Definition: ehci.h:79
u8 num_port
Definition: ehci.h:98
u32 status
Definition: ehci_defs.h:69
u32 port_status[0]
Definition: ehci_defs.h:102
u32 sector_size[16]
Definition: usbstorage.h:34
u32 n_sector[16]
Definition: usbstorage.h:35
struct ehci_device * usb_fd
Definition: usbstorage.h:37
struct ehci_hcd ehci_hcds[EHCI_HCD_COUNT]
Definition: tinyehci.c:66
#define EHCI_HCD_COUNT
Definition: tinyehci.c:63
#define debug_printf(a...)
Definition: tinyehci.c:44
#define ehci_readl(a)
Definition: tinyehci.c:59
#define be32_to_cpu(a)
Definition: tinyehci.c:58
#define le32_to_cpu(a)
Definition: tinyehci.c:54
#define cpu_to_le32(a)
Definition: tinyehci.c:53
void USB_FreeDescriptors(usb_devdesc *udd)
Definition: usb2.c:147
#define USB_CTRLTYPE_DIR_HOST2DEVICE
Definition: usb.h:38
#define USB_CTRLTYPE_DIR_DEVICE2HOST
Definition: usb.h:39
s32 USB_GetDescriptors(struct ehci_hcd *ehci, struct ehci_device *fd, usb_devdesc *udd)
Definition: usb2.c:29
void USB_Free(void *ptr)
Definition: usb_os.c:121
#define USB_ENDPOINT_IN
Definition: usb.h:51
void * USB_Alloc(int size)
Definition: usb_os.c:116
#define USB_CTRLTYPE_REC_INTERFACE
Definition: usb.h:45
#define USB_CTRLTYPE_TYPE_CLASS
Definition: usb.h:41
s32 USB_SetAlternativeInterface(struct ehci_hcd *ehci, struct ehci_device *fd, u8 interface, u8 alternateSetting)
Definition: usb2.c:205
s32 USB_ClearHalt(struct ehci_hcd *ehci, struct ehci_device *fd, u8 endpointAddress)
Definition: usb2.c:213
s32 USB_SetConfiguration(struct ehci_hcd *ehci, struct ehci_device *fd, u8 configuration)
Definition: usb2.c:201
s32 USB_WriteBlkMsg(struct ehci_hcd *ehci, struct ehci_device *fd, u8 bEndpoint, u16 wLength, void *rpData)
Definition: usb2.c:175
s32 USB_GetConfiguration(struct ehci_hcd *ehci, struct ehci_device *fd, u8 *configuration)
Definition: usb2.c:185
s32 USB_WriteCtrlMsg(struct ehci_hcd *ehci, struct ehci_device *fd, u8 bmRequestType, u8 bmRequest, u16 wValue, u16 wIndex, u16 wLength, void *rpData)
Definition: usb2.c:180
#define FALSE
Definition: usbhack.h:58
int unplug_procedure(int device)
Definition: usbstorage.c:1316
s32 USBStorage_Write_Sectors_1(u32 sector, u32 numSectors, const void *buffer)
Definition: usbstorage.c:1422
s32 USBStorage_Read_Sectors_0(u32 sector, u32 numSectors, void *buffer)
Definition: usbstorage.c:1413
DISC_INTERFACE usb2mass_ops_2
Definition: usbstorage.c:1492
#define MASS_STORAGE_QIC_COMMANDS
Definition: usbstorage.c:72
s32 USBStorage_GetMaxLUN(usbstorage_handle *dev)
Definition: usbstorage.c:894
#define TAG_START
Definition: usbstorage.c:43
#define SCSI_SENSE_REPLY_SIZE
Definition: usbstorage.c:62
ehci_device_data * find_ehci_data(struct ehci_hcd *ehci)
Definition: usbstorage.c:134
s32 USBStorage_Read(struct ehci_hcd *ehci, usbstorage_handle *dev, u8 lun, u32 sector, u16 n_sectors, u8 *buffer)
Definition: usbstorage.c:1017
int my_memcmp(char *a, char *b, int size_b)
Definition: usbstorage.c:631
s32 USBStorage_Reset(struct ehci_hcd *ehci, usbstorage_handle *dev)
Definition: usbstorage.c:886
s32 USBStorage_Open(ehci_device_data *device_data)
Definition: usbstorage.c:646
void(* mount_usb_device)(int device)=0
Definition: usbstorage.c:35
#define USBSTORAGE_CYCLE_RETRIES
Definition: usbstorage.c:87
#define SCSI_READ_CAPACITY
Definition: usbstorage.c:58
s32 USBStorage_devsectors_2(void)
Definition: usbstorage.c:1451
DISC_INTERFACE usb2mass_ops_1
Definition: usbstorage.c:1480
#define MASS_STORAGE_RBC_COMMANDS
Definition: usbstorage.c:70
#define MASS_STORAGE_SFF8070_COMMANDS
Definition: usbstorage.c:74
#define SCSI_SENSE_MEDIUM_ERROR
Definition: usbstorage.c:64
s32 USBStorage_Inserted_2()
Definition: usbstorage.c:1437
s32 USBStorage_Inserted_0()
Definition: usbstorage.c:1431
DISC_INTERFACE usb2mass_ops
Definition: usbstorage.c:1456
#define CBW_OUT
Definition: usbstorage.c:48
#define DEVLIST_MAXSIZE
Definition: usbstorage.c:92
#define SCSI_START_STOP
Definition: usbstorage.c:57
s32 USBStorage_MountLUN(struct ehci_hcd *ehci, usbstorage_handle *dev, u8 lun)
Definition: usbstorage.c:899
s32 USBStorage_Write(struct ehci_hcd *ehci, usbstorage_handle *dev, u8 lun, u32 sector, u16 n_sectors, const u8 *buffer)
Definition: usbstorage.c:1037
s32 USBStorage_Inquiry(struct ehci_hcd *ehci, usbstorage_handle *dev, u8 lun)
Definition: usbstorage.c:935
#define SCSI_READ_10
Definition: usbstorage.c:59
s32 USBStorage_Close(usbstorage_handle *dev)
Definition: usbstorage.c:878
s32 USBStorage_Read_Sectors_2(u32 sector, u32 numSectors, void *buffer)
Definition: usbstorage.c:1425
s32 USBStorage_Init(void)
Definition: usbstorage.c:1215
s32 USBStorage_Read_Sectors_1(u32 sector, u32 numSectors, void *buffer)
Definition: usbstorage.c:1419
s32 try_status
Definition: usbstorage.c:644
s32 USBStorage_Inserted_1()
Definition: usbstorage.c:1434
#define USB_ENDPOINT_BULK
Definition: usbstorage.c:82
#define MASS_STORAGE_BULK_ONLY
Definition: usbstorage.c:77
DISC_INTERFACE usb2mass_ops_0
Definition: usbstorage.c:1468
s32 USBStorage_Write_Sectors_0(u32 sector, u32 numSectors, const void *buffer)
Definition: usbstorage.c:1416
#define MASS_STORAGE_UFI_COMMANDS
Definition: usbstorage.c:73
#define CSW_SIGNATURE
Definition: usbstorage.c:52
s32 USBStorage_Write_Sectors(int device, u32 sector, u32 numSectors, const void *buffer)
Definition: usbstorage.c:1378
#define USB_CLASS_MASS_STORAGE
Definition: usbstorage.c:67
#define s_printf
Definition: usbstorage.c:37
#define USB_CLASS_HUB
Definition: usbstorage.c:68
#define SCSI_INQUIRY
Definition: usbstorage.c:55
#define CBW_IN
Definition: usbstorage.c:47
#define CSW_SIZE
Definition: usbstorage.c:51
#define SCSI_REQUEST_SENSE
Definition: usbstorage.c:56
#define CBW_SIZE
Definition: usbstorage.c:45
#define CBW_SIGNATURE
Definition: usbstorage.c:46
u32 usb_timeout
Definition: ehci.c:502
#define SCSI_WRITE_10
Definition: usbstorage.c:60
s32 USBStorage_devsectors_0(void)
Definition: usbstorage.c:1441
#define USBSTORAGE_GET_MAX_LUN
Definition: usbstorage.c:79
s32 USBStorage_Write_Sectors_2(u32 sector, u32 numSectors, const void *buffer)
Definition: usbstorage.c:1428
#define USBSTORAGE_RESET
Definition: usbstorage.c:80
s32 USBStorage_Read_Sectors(int device, u32 sector, u32 numSectors, void *buffer)
Definition: usbstorage.c:1342
#define SCSI_SENSE_HARDWARE_ERROR
Definition: usbstorage.c:65
s32 USBStorage_Try_Device(ehci_device_data *device_data)
Definition: usbstorage.c:1092
int handshake_mode
Definition: ehci.c:166
#define SCSI_TEST_UNIT_READY
Definition: usbstorage.c:54
s32 USBStorage_ReadCapacity(struct ehci_hcd *ehci, usbstorage_handle *dev, u8 lun, u32 *sector_size, u32 *n_sectors)
Definition: usbstorage.c:948
s32 USBStorage_devsectors_1(void)
Definition: usbstorage.c:1446
#define SCSI_SENSE_NOT_READY
Definition: usbstorage.c:63
s32 USBStorage_Get_Capacity(int device, u32 *sector_size)
Definition: usbstorage.c:1306
#define MAX_TRANSFER_SIZE
Definition: usbstorage.c:90
#define MASS_STORAGE_SCSI_COMMANDS
Definition: usbstorage.c:75
#define MASS_STORAGE_ATA_COMMANDS
Definition: usbstorage.c:71
#define USBSTORAGE_ETAG
Definition: usbstorage.h:18
#define USBSTORAGE_ETIMEDOUT
Definition: usbstorage.h:21
#define USBSTORAGE_EINIT
Definition: usbstorage.h:22
#define USBSTORAGE_ENOINTERFACE
Definition: usbstorage.h:13
#define USBSTORAGE_ESIGNATURE
Definition: usbstorage.h:17
#define USBSTORAGE_ESHORTREAD
Definition: usbstorage.h:16
#define USBSTORAGE_ESENSE
Definition: usbstorage.h:14
#define USBSTORAGE_ESTATUS
Definition: usbstorage.h:19
#define USBSTORAGE_OK
Definition: usbstorage.h:12
#define USBSTORAGE_EDATARESIDUE
Definition: usbstorage.h:20
void USBStorage_Umount(void)
#define USBSTORAGE_ESHORTWRITE
Definition: usbstorage.h:15
u8 features
Definition: xenos_edid.h:15
u8 j
Definition: xenos_edid.h:10
__le16 m
Definition: xenos_edid.h:8
uint8_t u8
8bit unsigned integer
Definition: xetypes.h:12
int8_t s8
8bit signed integer
Definition: xetypes.h:17
#define TRUE
True.
Definition: xetypes.h:52
unsigned int BOOL
Definition: xetypes.h:46
uint16_t u16
16bit unsigned integer
Definition: xetypes.h:13
int32_t s32
32bit signed integer
Definition: xetypes.h:19
uint32_t u32
32bit unsigned integer
Definition: xetypes.h:14