37#define s_printf printf
39#define ROUNDDOWN32(v) (((u32)(v)-0x1f)&~0x1f)
43#define TAG_START 0x22112211
46#define CBW_SIGNATURE 0x43425355
47#define CBW_IN (1 << 7)
52#define CSW_SIGNATURE 0x53425355
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
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
67#define USB_CLASS_MASS_STORAGE 0x08
68#define USB_CLASS_HUB 0x09
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
77#define MASS_STORAGE_BULK_ONLY 0x50
79#define USBSTORAGE_GET_MAX_LUN 0xFE
80#define USBSTORAGE_RESET 0xFF
82#define USB_ENDPOINT_BULK 0x02
85#define USBSTORAGE_CYCLE_RETRIES 3
87#define USBSTORAGE_CYCLE_RETRIES 10
90#define MAX_TRANSFER_SIZE 4096
92#define DEVLIST_MAXSIZE 8
94static int ums_init_done = 0;
116static unsigned int _ehci_device_count = 0;
119static void init_ehci_device_struct(){
123 _ehci_data[i].
__lun = 16;
125 _ehci_data[i].
__vid = 0;
126 _ehci_data[i].
__pid = 0;
138 if(_ehci_data[
j].__ehci ==
ehci){
139 _device_data = &_ehci_data[
j];
156char *mem_log = (
char *) 0x13750000;
160void int_char(
int num) {
170 for (n = 0; n < 10; n++) {
174 mem_cad[25 - n] = 48 +
m;
186 while (mem_cad[
m] ==
'0')
m++;
188 if (mem_cad[
m] == 0)
m--;
191 mem_cad[n] = mem_cad[
m];
199void hex_char(
u32 num) {
208 for (n = 0; n < 8; n++) {
212 mem_cad[23 - n] = 48 +
m;
225 while (mem_cad[
m] ==
'0')
m++;
227 if (mem_cad[
m] == 0)
m--;
230 mem_cad[n] = mem_cad[
m];
243 s_printf(
" status: %x %s%s%s%s%s%s%s%s%s%s\n",
257 s_printf(
" status port: %x\n", statusp);
279 if (cbLen == 0 || cbLen > 16 || !dev->
buffer)
293 dev->
buffer[14] = (cbLen > 6 ? 0x10 : 6);
295 memcpy(dev->
buffer + 15, cb, cbLen);
307 u32 signature, tag, _dataResidue, _status;
315 else if (retval < 0)
return retval;
320 _status = dev->
buffer[12];
327 if (dataResidue !=
NULL)
328 *dataResidue = _dataResidue;
347 u8 *buffer2 = buffer;
354 if (retval == -ENODEV) {
365 if (retval < 0)
continue;
372 retval = __send_cbw(
ehci, dev, lun, len,
CBW_OUT, cb, cbLen);
377 retval = __USB_BlkMsgTimeout(
ehci, dev, dev->
ep_out, thisLen, buffer);
379 if (retval == -ENODEV || retval == -ETIMEDOUT)
break;
384 if (retval != thisLen && len > 0) {
395 retval = __send_cbw(
ehci, dev, lun, len,
CBW_IN, cb, cbLen);
402 retval = __USB_BlkMsgTimeout(
ehci, dev, dev->
ep_in, thisLen, buffer);
404 if (retval == -ENODEV || retval == -ETIMEDOUT)
break;
413 if (retval != thisLen) {
423 retval = __read_csw(
ehci, dev, &
status, &dataResidue);
429 }
while (retval < 0 && retries > 0);
445 if (_dataResidue !=
NULL)
446 *_dataResidue = dataResidue;
458 memset(cmd, 0,
sizeof (cmd));
460 cmd[1] = (lun << 5) | 1;
461 cmd[4] = start_stop & 3;
482 memset(cmd, 0,
sizeof (cmd));
486 if (!sense)
return -ENOMEM;
490 for (n = 0; n < 5; n++) {
495 s_printf(
" SCSI_TEST_UNIT_READY %i# ret %i\n", n, retval);
498 if (retval == -ENODEV)
goto error;
501 if (retval == 0)
break;
503 if (retval < 0)
goto error;
515 s_printf(
" SCSI_REQUEST_SENSE ret %i\n", retval);
518 if (retval < 0)
goto error;
547 if (retval == -ENODEV)
return retval;
549 if (retval < 0 && retval != -7004)
564 if (_device_data->
__lun != 16) {
573 s_printf(
"usbstorage reset: Reset ret %i\n", retval);
578 if (retval < 0 && retval != -7004)
590 s_printf(
"usbstorage reset: clearhalt in ret %i\n", retval);
599 s_printf(
"usbstorage reset: clearhalt in ret %i\n", retval);
609 if (retval == -ENODEV)
return retval;
611 if (disable_hardreset)
return retval;
634 for (n = 0; n < size_b; n++) {
635 if (*a != *b)
return 1;
649 u32 iConf, iInterface, iEp;
659 device_data->
__lun = 16;
662 if (max_lun ==
NULL)
return -ENOMEM;
664 memset(dev, 0,
sizeof (*dev));
673 s_printf(
"USBStorage_Open(): USB_GetDescriptors %i\n", retval);
675 log_status(
ehci,
"after USB_GetDescriptors");
681 goto free_and_return;
701 for (iInterface = 0; iInterface < ucd->
bNumInterfaces; iInterface++) {
755 goto free_and_return;
767 s_printf(
"USBStorage_Open(): cannot find any interface!!!\n");
773 goto free_and_return;
787 goto free_and_return;
791 log_status(
ehci,
"after USB_GetConfiguration");
796 s_printf(
"USBStorage_Open(): changing conf from %x\n", conf);
800 goto free_and_return;
804 goto free_and_return;
809 log_status(
ehci,
"Before USBStorage_Reset");
813 log_status(
ehci,
"After USBStorage_Reset");
816 goto free_and_return;
861 memset(dev, 0,
sizeof (*dev));
871 s_printf(
"USBStorage_Open(): return 0\n");
881 memset(dev, 0,
sizeof (*dev));
889 retval = __usbstorage_reset(
ehci, dev, 0);
908 retval = __usbstorage_start_stop(dev, lun, 1);
911 s_printf(
" start_stop cmd ret %i\n", retval);
916 retval = __usbstorage_clearerrors(
ehci, dev, lun);
922 s_printf(
" Inquiry ret %i\n", retval);
928 s_printf(
" ReadCapacity ret %i\n", retval);
940 if (!response)
return -ENOMEM;
942 retval = __cycle(
ehci, dev, lun, response, 36, cmd, 6, 0,
NULL,
NULL);
953 if (!response)
return -ENOMEM;
955 retval = __cycle(
ehci, dev, lun, response, 8, cmd, 2, 0,
NULL,
NULL);
958 memcpy(&
val, response, 4);
959 if (n_sectors !=
NULL)
961 memcpy(&
val, response + 4, 4);
962 if (sector_size !=
NULL)
989 if (retval > 0 &&
status != 0)
1012 if (retval > 0 &&
status != 0)
1018 u32 max_sectors = n_sectors;
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;
1029 n_sectors -= sectors;
1038 u32 max_sectors = n_sectors;
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;
1049 n_sectors -= sectors;
1073 for (i = 0; i < 512; i++) {
1074 retval =
USBStorage_Read(__ehci, &__usbfd, __lun, sector, numSectors, buffer);
1075 sector += numSectors;
1094 int maxLun,
j, retval;
1095 int test_max_lun = 1;
1122 s_printf(
"USBStorage_MountLUN %i#\n",
j);
1126 s_printf(
"USBStorage_MountLUN: ret %i\n", retval);
1142 retval = __USB_CtrlMsgTimeout(
ehci, &device_data->
__usbfd,
1148 maxLun =device_data-> __usbfd.max_lun;
1151 s_printf(
"USBSTORAGE_GET_MAX_LUN ret %i maxlun %i\n", retval, maxLun);
1156 if (
j >= maxLun)
break;
1160 device_data->
__vid =
fd->desc.idVendor;
1161 device_data->
__pid =
fd->desc.idProduct;
1173 s_printf(
"USBStorage_MountLUN fail!!!\n");
1179static int USBStorage_True(
void) {
1183static int USBStorage_Inserted(
void) {
1189 if (!ums_init_done)
return;
1194 if (__usbstorage_start_stop(&__usbfd, __lun, 0x0) == 0)
1216 int i,
j, retries = 1;
1220 _ehci_device_count = 0;
1221 init_ehci_device_struct();
1234 if (dev->
id != 0 && dev->
busy == 0) {
1243 device_data->
__dev = dev;
1246 printf(
"EHCI bus %d device %d: vendor %04X product %04X : Mass-Storage Device\n",
j, dev->
id, device_data->
__vid, device_data->
__pid);
1248 first_access =
TRUE;
1257 _ehci_device_count++;
1259 s_printf(
"USBStorage_Init() Ok\n");
1266 }
else if (dev->
busy == 0) {
1307 if (_ehci_data[device].__mounted == 1) {
1320 if (_ehci_data[device].__usbfd.usb_fd)
1322 if (_ehci_data[device].__usbfd.buffer !=
NULL)
1323 USB_Free(_ehci_data[device].__usbfd.buffer);
1346 if (_ehci_data[device].__mounted != 1)
1349 for (retry = 0; retry < 16; retry++) {
1350 if (retry > 12) retry = 12;
1366 retval =
USBStorage_Read(_ehci_data[device].__ehci, &_ehci_data[device].__usbfd, _ehci_data[device].__lun, sector, numSectors, buffer);
1370 if (retval >= 0)
break;
1382 if (_ehci_data[device].__mounted != 1)
1385 for (retry = 0; retry < 16; retry++) {
1386 if (retry > 12) retry = 12;
1402 retval =
USBStorage_Write(_ehci_data[device].__ehci, &_ehci_data[device].__usbfd, _ehci_data[device].__lun, sector, numSectors, buffer);
1405 if (retval >= 0)
break;
bool(* FN_MEDIUM_STARTUP)(void)
bool(* FN_MEDIUM_CLEARSTATUS)(void)
#define FEATURE_MEDIUM_CANREAD
s32(* FN_MEDIUM_DEVSECTORS)(void)
bool(* FN_MEDIUM_SHUTDOWN)(void)
#define FEATURE_XENON_USB
bool(* FN_MEDIUM_READSECTORS)(sec_t sector, sec_t numSectors, void *buffer)
bool(* FN_MEDIUM_WRITESECTORS)(sec_t sector, sec_t numSectors, const void *buffer)
#define FEATURE_MEDIUM_CANWRITE
bool(* FN_MEDIUM_ISINSERTED)(void)
int ehci_reset_device(struct ehci_hcd *ehci, struct ehci_device *dev)
int ehci_reset_port(struct ehci_hcd *ehci, int port)
int ehci_reset_port2(struct ehci_hcd *ehci, int port)
s32 USBStorage_Read_Stress(u32 sector, u32 numSectors, void *buffer)
void ehci_msleep(int time)
int write(int fileDesc, const void *ptr, size_t len)
FN_MEDIUM_DEVSECTORS sectors
struct _usbinterfacedesc * interfaces
struct _usbconfdesc * configurations
struct _usbendpointdesc * endpoints
usbstorage_handle __usbfd
struct ehci_device * __dev
struct ehci_device devices[EHCI_MAX_ROOT_PORTS]
struct ehci_regs __iomem * regs
struct ehci_device * usb_fd
struct ehci_hcd ehci_hcds[EHCI_HCD_COUNT]
#define debug_printf(a...)
void USB_FreeDescriptors(usb_devdesc *udd)
#define USB_CTRLTYPE_DIR_HOST2DEVICE
#define USB_CTRLTYPE_DIR_DEVICE2HOST
s32 USB_GetDescriptors(struct ehci_hcd *ehci, struct ehci_device *fd, usb_devdesc *udd)
void * USB_Alloc(int size)
#define USB_CTRLTYPE_REC_INTERFACE
#define USB_CTRLTYPE_TYPE_CLASS
s32 USB_SetAlternativeInterface(struct ehci_hcd *ehci, struct ehci_device *fd, u8 interface, u8 alternateSetting)
s32 USB_ClearHalt(struct ehci_hcd *ehci, struct ehci_device *fd, u8 endpointAddress)
s32 USB_SetConfiguration(struct ehci_hcd *ehci, struct ehci_device *fd, u8 configuration)
s32 USB_WriteBlkMsg(struct ehci_hcd *ehci, struct ehci_device *fd, u8 bEndpoint, u16 wLength, void *rpData)
s32 USB_GetConfiguration(struct ehci_hcd *ehci, struct ehci_device *fd, u8 *configuration)
s32 USB_WriteCtrlMsg(struct ehci_hcd *ehci, struct ehci_device *fd, u8 bmRequestType, u8 bmRequest, u16 wValue, u16 wIndex, u16 wLength, void *rpData)
int unplug_procedure(int device)
s32 USBStorage_Write_Sectors_1(u32 sector, u32 numSectors, const void *buffer)
s32 USBStorage_Read_Sectors_0(u32 sector, u32 numSectors, void *buffer)
DISC_INTERFACE usb2mass_ops_2
#define MASS_STORAGE_QIC_COMMANDS
s32 USBStorage_GetMaxLUN(usbstorage_handle *dev)
#define SCSI_SENSE_REPLY_SIZE
ehci_device_data * find_ehci_data(struct ehci_hcd *ehci)
s32 USBStorage_Read(struct ehci_hcd *ehci, usbstorage_handle *dev, u8 lun, u32 sector, u16 n_sectors, u8 *buffer)
int my_memcmp(char *a, char *b, int size_b)
s32 USBStorage_Reset(struct ehci_hcd *ehci, usbstorage_handle *dev)
s32 USBStorage_Open(ehci_device_data *device_data)
void(* mount_usb_device)(int device)=0
#define USBSTORAGE_CYCLE_RETRIES
#define SCSI_READ_CAPACITY
s32 USBStorage_devsectors_2(void)
DISC_INTERFACE usb2mass_ops_1
#define MASS_STORAGE_RBC_COMMANDS
#define MASS_STORAGE_SFF8070_COMMANDS
#define SCSI_SENSE_MEDIUM_ERROR
s32 USBStorage_Inserted_2()
s32 USBStorage_Inserted_0()
DISC_INTERFACE usb2mass_ops
s32 USBStorage_MountLUN(struct ehci_hcd *ehci, usbstorage_handle *dev, u8 lun)
s32 USBStorage_Write(struct ehci_hcd *ehci, usbstorage_handle *dev, u8 lun, u32 sector, u16 n_sectors, const u8 *buffer)
s32 USBStorage_Inquiry(struct ehci_hcd *ehci, usbstorage_handle *dev, u8 lun)
s32 USBStorage_Close(usbstorage_handle *dev)
s32 USBStorage_Read_Sectors_2(u32 sector, u32 numSectors, void *buffer)
s32 USBStorage_Init(void)
s32 USBStorage_Read_Sectors_1(u32 sector, u32 numSectors, void *buffer)
s32 USBStorage_Inserted_1()
#define USB_ENDPOINT_BULK
#define MASS_STORAGE_BULK_ONLY
DISC_INTERFACE usb2mass_ops_0
s32 USBStorage_Write_Sectors_0(u32 sector, u32 numSectors, const void *buffer)
#define MASS_STORAGE_UFI_COMMANDS
s32 USBStorage_Write_Sectors(int device, u32 sector, u32 numSectors, const void *buffer)
#define USB_CLASS_MASS_STORAGE
#define SCSI_REQUEST_SENSE
s32 USBStorage_devsectors_0(void)
#define USBSTORAGE_GET_MAX_LUN
s32 USBStorage_Write_Sectors_2(u32 sector, u32 numSectors, const void *buffer)
s32 USBStorage_Read_Sectors(int device, u32 sector, u32 numSectors, void *buffer)
#define SCSI_SENSE_HARDWARE_ERROR
s32 USBStorage_Try_Device(ehci_device_data *device_data)
#define SCSI_TEST_UNIT_READY
s32 USBStorage_ReadCapacity(struct ehci_hcd *ehci, usbstorage_handle *dev, u8 lun, u32 *sector_size, u32 *n_sectors)
s32 USBStorage_devsectors_1(void)
#define SCSI_SENSE_NOT_READY
s32 USBStorage_Get_Capacity(int device, u32 *sector_size)
#define MAX_TRANSFER_SIZE
#define MASS_STORAGE_SCSI_COMMANDS
#define MASS_STORAGE_ATA_COMMANDS
#define USBSTORAGE_ETIMEDOUT
#define USBSTORAGE_ENOINTERFACE
#define USBSTORAGE_ESIGNATURE
#define USBSTORAGE_ESHORTREAD
#define USBSTORAGE_ESENSE
#define USBSTORAGE_ESTATUS
#define USBSTORAGE_EDATARESIDUE
void USBStorage_Umount(void)
#define USBSTORAGE_ESHORTWRITE
uint8_t u8
8bit unsigned integer
int8_t s8
8bit signed integer
uint16_t u16
16bit unsigned integer
int32_t s32
32bit signed integer
uint32_t u32
32bit unsigned integer