USB Mass storage driver fixed

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Post Reply
Mega Man
Posts: 260
Joined: Sat Jun 18, 2005 3:14 am
Contact:

USB Mass storage driver fixed

Post by Mega Man »

Hello,

I had some trouble with the usb mass storage driver. I added patches to solve it. Can someone check it into the svn repository. Some files in the svn are marked as binary, so CRLF line terminators are not converted to LF on linux. Can someone fix this also.

I can test it only with one USB stick.

The first patch fixes the mode of ohci. The ohci was not set to operational mode. I don't why this was working with ps2. With normal x86 hardware this is not working.

Code: Select all

diff -ur ps2sdk/iop/usb/usbd/src/hcd.c ps2sdk2/iop/usb/usbd/src/hcd.c
--- ps2sdk/iop/usb/usbd/src/hcd.c	2006-07-21 17:00:55.000000000 +0200
+++ ps2sdk2/iop/usb/usbd/src/hcd.c	2006-07-21 17:34:01.000000000 +0200
@@ -462,7 +462,7 @@
 	memPool.ohciRegs->HcFmInterval = 0x27782EDF;
 	memPool.ohciRegs->HcPeriodicStart = 0x2A2F;
 	memPool.ohciRegs->HcInterruptEnable = OHCI_INT_MIE | OHCI_INT_RHSC | OHCI_INT_UE | OHCI_INT_WDH | OHCI_INT_SO;
-	memPool.ohciRegs->HcControl |= OHCI_CTR_USB_OPERATIONAL | OHCI_CTR_PLE | OHCI_CTR_IE | OHCI_CTR_CLE | OHCI_CTR_BLE | 3;
+	memPool.ohciRegs->HcControl = OHCI_CTR_USB_OPERATIONAL | OHCI_CTR_PLE | OHCI_CTR_IE | OHCI_CTR_CLE | OHCI_CTR_BLE | 3;
 	return 0;
 }
- It was not possible to mount a partition which was configured to FAT16 LBA.
- There were some compiling problems.
- Get emulation working with linux (Makefile, missing return values and variables, segmentation fault).
- My USB stick was not initialized correctly and was only working when reseting iop without reseting my USB stick. This has been fixed.

Code: Select all

diff -ur usb_mass/iop/fat_driver.c usb_mass2/iop/fat_driver.c
--- usb_mass/iop/fat_driver.c	2006-07-21 17:00:28.000000000 +0200
+++ usb_mass2/iop/fat_driver.c	2006-07-21 17:07:26.000000000 +0200
@@ -354,7 +354,8 @@
        part -> record[ i ].sid == 4    ||
        part -> record[ i ].sid == 1    ||  // fat 16, fat 12
        part -> record[ i ].sid == 0x0B ||
-       part -> record[ i ].sid == 0x0C     // fat 32 
+       part -> record[ i ].sid == 0x0C ||  // fat 32 
+       part -> record[ i ].sid == 0x0E     // fat 16 LBA
   ) workPartition = i;
 
  }  /* end for */
@@ -1017,8 +1018,9 @@
 	if &#40;ret < 0&#41; &#123;
 		return ret;
 	&#125;
-	if &#40; &#40;&#40;dirName&#91;0&#93; == '/' || dirName&#91;0&#93;=='\\'&#41; && dirName&#91;1&#93; == 0&#41; || // the root directory
-		dirName&#91;0&#93; == 0 || dirName == NULL&#41; &#123;
+	if &#40; dirName == NULL ||
+		&#40;&#40;dirName&#91;0&#93; == '/' || dirName&#91;0&#93;=='\\'&#41; && dirName&#91;1&#93; == 0&#41; || // the root directory
+		dirName&#91;0&#93; == 0 &#41; &#123;
 			direntryCluster = 0;
 	&#125; else &#123;
 		ret = fat_getFileStartCluster&#40;&partBpb, dirName, &startCluster, fatDir&#41;;
@@ -1037,7 +1039,7 @@
 
 
 int fat_initDriver&#40;&#41; &#123;
-	int ret;
+	int ret = 0;
 
 	lastChainCluster = 0xFFFFFFFF;
 	lastChainResult = -1;
@@ -1058,7 +1060,7 @@
 #endif
 	fs_init&#40;NULL&#41;;
 	mounted = 1;
-
+	return ret;
 &#125;
 
 
diff -ur usb_mass/iop/imports.lst usb_mass2/iop/imports.lst
--- usb_mass/iop/imports.lst	2006-07-21 17&#58;00&#58;28.000000000 +0200
+++ usb_mass2/iop/imports.lst	2006-07-21 17&#58;25&#58;21.000000000 +0200
@@ -70,7 +70,6 @@
 usbd_IMPORTS_start
 I_UsbGetDeviceStaticDescriptor
 I_UsbOpenEndpoint
-I_UsbOpenBulkEndpoint
 I_UsbCloseEndpoint
 I_UsbSetDevicePrivateData
 I_UsbTransfer
diff -ur usb_mass/iop/mass_stor.c usb_mass2/iop/mass_stor.c
--- usb_mass/iop/mass_stor.c	2006-07-21 17&#58;00&#58;28.000000000 +0200
+++ usb_mass2/iop/mass_stor.c	2006-07-21 17&#58;43&#58;54.000000000 +0200
@@ -454,7 +454,7 @@
 
 	XPRINTF&#40;"...waiting for status 1 ...\n"&#41;;
 	ret = usb_bulk_status&#40;dev, &csw, tag&#41;; /* Attempt to read CSW from bulk in endpoint */
-	if &#40;ret == 4 || ret < 0&#41; &#123; /* STALL bulk in  -OR- Bulk error */
+	if &#40;ret == USB_RC_STALL || ret < 0&#41; &#123; /* STALL bulk in  -OR- Bulk error */
 		usb_bulk_clear_halt&#40;dev, 0&#41;; /* clear the stall condition for bulk in */
 		
 		XPRINTF&#40;"...waiting for status 2 ...\n"&#41;;
@@ -463,12 +463,17 @@
 	&#125;
 
 	/* CSW not valid  or stalled or phase error */
-	if &#40;csw.signature !=  0x53425355 || csw.tag != tag || ret != 0&#41; &#123;
+	if &#40;csw.signature !=  0x53425355 || csw.tag != tag&#41; &#123;
 		XPRINTF&#40;"...call reset recovery ...\n"&#41;;
 		usb_bulk_reset&#40;dev, 3&#41;;	/* Perform reset recovery */
 	&#125;
 
-	return 0; //device is ready to process next CBW
+	if &#40;ret != 0&#41; &#123;
+		XPRINTF&#40;"Mass storage device not ready!\n"&#41;;
+		return 1; //device is not ready, need to retry!
+	&#125; else &#123;
+		return 0; //device is ready to process next CBW
+	&#125;
 &#125;
 
 
@@ -549,7 +554,7 @@
 
 
 /* helper functions */
-#ifdef COMPILE_DUMPS
+#ifdef DEBUG
 void dumpDevice&#40;UsbDeviceDescriptor* data&#41; &#123;
 	printf&#40;"\n"&#41;;
 	printf&#40;"DEVICE info\n"&#41;;
@@ -572,7 +577,6 @@
 	printf&#40;"config value=0x%02X\n", data->bConfigurationValue&#41;;
 	printf&#40;"index configuration=0x%02X\n", data->iConfiguration&#41;;
 	printf&#40;"attributes=0x%02X\n", data->bmAttributes&#41;;
-	printf&#40;"max power=0x%02X\n", data->MaxPower&#41;;
 
 &#125;
 
@@ -731,14 +735,14 @@
 		/* out transfer */
 		if &#40;&#40;endpoint->bEndpointAddress & 0x80&#41; == 0 && dev->bulkEpO < 0&#41; &#123;
 			dev->bulkEpOAddr = endpoint->bEndpointAddress;
-			dev->bulkEpO = UsbOpenBulkEndpoint&#40;dev->devId, endpoint&#41;;
+			dev->bulkEpO = UsbOpenEndpoint&#40;dev->devId, endpoint&#41;;
 			dev->packetSzO = endpoint->wMaxPacketSizeHB * 256 + endpoint->wMaxPacketSizeLB;
 			XPRINTF&#40;"register Output endpoint id =%i addr=%02X packetSize=%i\n", dev->bulkEpO,dev->bulkEpOAddr, dev->packetSzO&#41;;
 		&#125;else
 		/* in transfer */
 		if &#40;&#40;endpoint->bEndpointAddress & 0x80&#41; != 0 && dev->bulkEpI < 0&#41; &#123;
 			dev->bulkEpIAddr = endpoint->bEndpointAddress;
-			dev->bulkEpI = UsbOpenBulkEndpoint&#40;dev->devId, endpoint&#41;;
+			dev->bulkEpI = UsbOpenEndpoint&#40;dev->devId, endpoint&#41;;
 			dev->packetSzI = endpoint->wMaxPacketSizeHB * 256 + endpoint->wMaxPacketSizeLB;
 			XPRINTF&#40;"register Intput endpoint id =%i addr=%02X packetSize=%i\n", dev->bulkEpI, dev->bulkEpIAddr, dev->packetSzI&#41;;
 		&#125;
@@ -960,7 +964,6 @@
 		printf&#40;"!Error&#58; device not ready!\n"&#41;;
 		return -1;
 	&#125; 
-/*
 	//send start stop command
 	cbw_scsi_start_stop_unit&#40;&cbw&#41;;
 	XPRINTF&#40;"-START COMMAND\n"&#41;;
@@ -970,14 +973,14 @@
 	usb_bulk_manage_status&#40;dev, -TAG_START_STOP_UNIT&#41;;
 
 
-	//send test unit ready command
-	cbw_scsi_test_unit_ready&#40;&cbw&#41;;
-	XPRINTF&#40;"-TUR COMMAND\n"&#41;;
-	usb_bulk_command&#40;dev, &cbw&#41;;
+	do &#123;
+		//send test unit ready command
+		cbw_scsi_test_unit_ready&#40;&cbw&#41;;
+		XPRINTF&#40;"-TUR COMMAND\n"&#41;;
+		usb_bulk_command&#40;dev, &cbw&#41;;
 
-	XPRINTF&#40;"-TUR STATUS\n"&#41;;
-	usb_bulk_manage_status&#40;dev, -TAG_TEST_UNIT_READY&#41;;
-*/
+		XPRINTF&#40;"-TUR STATUS\n"&#41;;
+	&#125; while&#40;usb_bulk_manage_status&#40;dev, -TAG_TEST_UNIT_READY&#41; != 0&#41;;
 
 	//send "request sense" command
 	//required for correct operation of some devices
@@ -1010,7 +1013,8 @@
 		//according to usb doc we should allways 
 		//attempt to read the CSW packet. But in some cases
 		//reading of CSW packet just freeze ps2....&#58;-&#40;
-		if &#40;returnCode == 4&#41; &#123;
+		if &#40;returnCode == USB_RC_STALL&#41; &#123;
+			XPRINTF&#40;"call reset recovery ...\n"&#41;;
 			usb_bulk_reset&#40;dev, 1&#41;;
 		&#125; else &#123;
 
diff -ur usb_mass/iop/usb_mass.c usb_mass2/iop/usb_mass.c
--- usb_mass/iop/usb_mass.c	2006-07-21 17&#58;00&#58;28.000000000 +0200
+++ usb_mass2/iop/usb_mass.c	2006-07-21 17&#58;16&#58;29.000000000 +0200
@@ -88,7 +88,7 @@
 	param.attr         = TH_C;
 	param.thread     = rpcMainThread;
 	param.priority = 40;
-	param.stacksize    = 0x800;
+	param.stacksize    = 0x8000;
 	param.option      = 0;
 
 
@@ -257,4 +257,4 @@
 	&#125;
 	buf&#91;0&#93; = ret; //store return code
 	return buffer;
-&#125;
\ Kein Zeilenumbruch am Dateiende.
+&#125;
diff -ur usb_mass/pc/fat_test.c usb_mass2/pc/fat_test.c
--- usb_mass/pc/fat_test.c	2006-07-21 17&#58;00&#58;27.000000000 +0200
+++ usb_mass2/pc/fat_test.c	2006-07-21 17&#58;23&#58;03.000000000 +0200
@@ -105,7 +105,7 @@
 	bufSize = 4096;
 
 	fd = fs_open&#40;&file, fname, O_RDONLY&#41;;
-	fo = open&#40;oname, O_RDWR | O_TRUNC | O_CREAT | O_BINARY, S_IWUSR | S_IRUSR&#41;; 
+	fo = open&#40;oname, O_RDWR | O_TRUNC | O_CREAT, S_IWUSR | S_IRUSR&#41;; 
 	if &#40;fd >=0 && fo >=0&#41; &#123;
 		size = fs_lseek&#40;&file, 0, SEEK_END&#41;;
 		printf&#40;"file&#58; %s size&#58; %i \n", fname, size&#41;;
@@ -300,7 +300,7 @@
 	bufSize = 4096;
 
 	fd = fs_open&#40;&file, fname, O_RDWR | O_CREAT | O_TRUNC &#41;;
-	fi = open&#40;iname, O_RDONLY | O_BINARY , S_IWUSR | S_IRUSR&#41;; 
+	fi = open&#40;iname, O_RDONLY, S_IWUSR | S_IRUSR&#41;; 
 	if &#40;fd >=0 && fi >=0&#41; &#123;
 		size = lseek&#40;fi, 0, SEEK_END&#41;;
 		printf&#40;"file&#58; %s size&#58; %i \n", iname, size&#41;;
diff -ur usb_mass/pc/makefile usb_mass2/pc/makefile
--- usb_mass/pc/makefile	2006-07-21 17&#58;00&#58;27.000000000 +0200
+++ usb_mass2/pc/makefile	2006-07-21 17&#58;22&#58;13.000000000 +0200
@@ -1,29 +1,35 @@
-CC=d&#58;/gcc/bin/g++
-CFLAGS= -s
+CC=gcc
+CFLAGS= -g3 -I../iop
 
 #uncomment to enable write support
 WRITE_SUPPORT=1
 
 
-OBJECTS1=fat_test.o fat_driver.o scache.o vdisk.o
-OBJECTS2=obj/fat_test.o obj/fat_driver.o obj/scache.o obj/vdisk.o 
+OBJECTS=fat_test.o fat_driver.o scache.o vdisk.o
 
 ifdef WRITE_SUPPORT
 CFLAGS += -DWRITE_SUPPORT
-OBJECTS1 += fat_write.o
-OBJECTS2 += obj/fat_write.o 
+OBJECTS += fat_write.o
 endif
 
-
-ALL&#58; fat_test.exe 
+all&#58; fat_test
 
 clean&#58;
-	rm obj/*.o
+	rm -f *.o
+
+fat_test&#58; $&#40;OBJECTS&#41;
+	$&#40;CC&#41; $&#40;CFLAGS&#41; -o $@ $^
+
+
+%.o&#58; %.c
+	$&#40;CC&#41; $&#40;CFLAGS&#41; -c $^ -o $@
 
-fat_test.exe&#58; $&#40;OBJECTS1&#41;
-	$&#40;CC&#41; $&#40;CFLAGS&#41; -ofat_test.exe $&#40;OBJECTS2&#41;
+fat_driver.c&#58; ../iop/fat_driver.c
+	ln -s $^ $@
 
+scache.c&#58; ../iop/scache.c
+	ln -s $^ $@
 
-.c.o&#58;
-	$&#40;CC&#41; $&#40;CFLAGS&#41; -c $&#40;<&#41; -o obj/$*.o
+fat_write.c&#58; ../iop/fat_write.c
+	ln -s $^ $@
 
diff -ur usb_mass/pc/vdisk.c usb_mass2/pc/vdisk.c
--- usb_mass/pc/vdisk.c	2006-07-21 17&#58;00&#58;27.000000000 +0200
+++ usb_mass2/pc/vdisk.c	2006-07-21 17&#58;22&#58;42.000000000 +0200
@@ -20,6 +20,7 @@
 int fileSize;	//virtual disk size &#40;given in bytes&#41;
 
 unsigned Size_Sector=512; // 
+unsigned g_MaxLBA; /* Size of disk */
 
 
 /*
@@ -29,7 +30,7 @@
 int vdisk_init&#40;char * param, int sectorSize&#41; &#123;
 	Size_Sector = sectorSize;
 	printf &#40;"Accessing file &#58; %s \n", param&#41;;
-	fi = open&#40;param, O_RDWR | O_BINARY , S_IWUSR&#41;;
+	fi = open&#40;param, O_RDWR, S_IWUSR&#41;;
 
 	if &#40;fi >=0&#41; &#123;
 		fileSize = lseek&#40;fi, 0, SEEK_END&#41;;
@@ -37,6 +38,7 @@
 	&#125; else &#123;
 		fileSize = 0;
 	&#125;
+	g_MaxLBA = fileSize / Size_Sector;
 	return fi;
 &#125;
[/code]
User avatar
Drakonite
Site Admin
Posts: 990
Joined: Sat Jan 17, 2004 1:30 am
Contact:

Post by Drakonite »

I'll try to check it and get it into svn soon for you... if it doesn't show up in svn in the next couple days it means I may have spaced it off, so remind me ;)
Shoot Pixels Not People!
Makeshift Development
Mega Man
Posts: 260
Joined: Sat Jun 18, 2005 3:14 am
Contact:

Post by Mega Man »

I forgot one patch. Please also add it to svn. The interface number will not be always 0. This can cause problems on some devices.

Code: Select all

--- mass_stor.c.orig	2006-07-22 01&#58;47&#58;11.000000000 +0200
+++ mass_stor.c	2006-07-22 01&#58;47&#58;30.000000000 +0200
@@ -385,7 +385,7 @@ void usb_bulk_reset&#40;mass_dev* dev, int m
 		0x21,			//bulk reset
 		0xFF,
 		0,
-		0,			//interface number  FIXME - correct number
+		dev->interfaceNumber,	//interface number
 		0,			//length
 		NULL,			//data
 		usb_callback, 
Post Reply