Index: trunk/VoodooSDHC.cpp
===================================================================
--- trunk/VoodooSDHC.cpp	(revision 2)
+++ trunk/VoodooSDHC.cpp	(working copy)
@@ -14,7 +14,7 @@
 /*
  * Builds the driver with High Speed Card support.
  */
-//#define HIGHSPEED_CARD_MODE	1
+#define HIGHSPEED_CARD_MODE	1
 
 /*
  * Builds the driver with 4-bit Bus support.
@@ -95,7 +95,7 @@
 	if ((workLoop = IOWorkLoop::workLoop()) == NULL)
 		return false;
 #endif
-	
+    
 	return true;
 }
 
@@ -345,7 +345,7 @@
 			SDCommand(slot, SD_APP_CMD, SDCR55, 0);
 			SDCommand(slot, SD_APP_OP_COND, SDACR41, 0x00FF8000);
 			IODelay(1000);
-		} while (!(this->PCIRegP[slot]->Response[0] & BIT31));
+		} while (!this->PCIRegP[slot]->Response[0] & BIT31);
 	} else {
 		// check and init SDHC (wait for 2 secs; spec requires 1 sec)
 		IOLog("VoodooSDHCI: initializing spec 2.0 SD card\n");
@@ -543,7 +543,7 @@
 bool VoodooSDHC::SDCommand(UInt8 slot, UInt8 command, UInt16 response,
 								UInt32 arg) {
 	if (command != 0) {
-		while(this->PCIRegP[slot]->PresentState & ComInhibitCMD);
+		while(this->PCIRegP[slot]->PresentState & ComInhibitCMD) ;
 	}
 
 	//if(command 1= COMMANDS?)
@@ -629,6 +629,7 @@
 	this->PCIRegP[slot]->ClockControl = 0;
 	this->PCIRegP[slot]->ClockControl = 0;
 	this->PCIRegP[slot]->ClockControl |= BIT0;
+
 	while(!this->PCIRegP[slot]->ClockControl & BIT1);
 	baseClock = ((this->PCIRegP[slot]->Capabilities[0] & 0x3F00) >> 8);
 	baseClock *= 1000000;
@@ -941,20 +942,17 @@
 
 bool VoodooSDHC::waitIntStatus(UInt32 maskBits)
 {
-	// roughly 5 seconds before timeout
-	for (int cnt = 0; cnt < 500000; cnt++) {
-		while (1) {
-			UInt32 nis = PCIRegP[0]->NormalIntStatus;
-			if (nis & ErrorInterrupt) {
-				return false;
-			}
-			if (nis & maskBits) {
-				PCIRegP[0]->NormalIntStatus = nis | maskBits;
-				return true;
-			}
-		}
-		::IODelay(10);
-	}
+	for (int cnt = 0; cnt < 10000; cnt++) {
+        UInt32 nis = PCIRegP[0]->NormalIntStatus;
+        if (nis & ErrorInterrupt) {
+            return false;
+        }
+        if (nis & maskBits) {
+            PCIRegP[0]->NormalIntStatus = nis | maskBits;
+            return true;
+        }
+    }
+    
 	return false;
 }
 
@@ -1052,13 +1050,12 @@
  *      bool   read: true if read, false if write
  */
 IOReturn VoodooSDHC::sdma_access(IOMemoryDescriptor *buffer,
-					UInt32 block, UInt32 nblks, bool read) {
+					UInt32 block, UInt32 nblks, UInt32 offset, bool read) {
 	IOReturn ret = kIOReturnError;
-	UInt32 nis, offset = 0;
 	AbsoluteTime deadline;
-
+    
 #ifdef __DEBUG__
-IOLog("VoodooSDHCI readBlockMulti_sdma:  block = %d, nblks = %d\n", block, nblks);
+    IOLog("VoodooSDHCI sdma_access:  block = %d, nblks = %d\n", block, nblks);
 #endif /* __DEBUG__ */
 #ifndef NO_RESET_WAR	
 	// Reset card before every operation.  The Linux driver
@@ -1093,14 +1090,26 @@
 	this->PCIRegP[0]->BlockSize = 512 | SDMA_BUFFER_SIZE_IN_REG;
 	this->PCIRegP[0]->BlockCount = nblks;
 	::OSSynchronizeIO();
-	
+    
 	// Issue read command to host controller
-	SDCommand(0,
+	if (!SDCommand(0,
 		read ? SD_READ_MULTIPLE_BLOCK : SD_WRITE_MULTIPLE_BLOCK,
 		SDCR18,
-		isHighCapacity ? block : block * 512);
+		isHighCapacity ? block : block * 512))
+    {
+        IOLog("VoodooSDHCI: I/O can't send command %d (SDMA): Status: 0x%x, Error: 0x%x\n",
+              read ? SD_READ_MULTIPLE_BLOCK : SD_WRITE_MULTIPLE_BLOCK, PCIRegP[0]->NormalIntStatus, PCIRegP[0]->ErrorIntStatus);
+        Reset(0, FULL_RESET);
+		if (! cardInit(0)) {
+			IOLog("VoodooSDHCI: reset failed, disabling access\n");
+			cardPresence = kCardRemount;
+		}
+		ret = kIOReturnTimeout;
+		goto out;
+    }
+    
 	::OSSynchronizeIO();
-	
+    
 	// wait for CmdComplete
 	if (! waitIntStatus(CmdComplete)) {
 		IOLog("VoodooSDHCI: I/O error after command %d (SDMA): Status: 0x%x, Error: 0x%x\n",
@@ -1113,6 +1122,7 @@
 		ret = kIOReturnTimeout;
 		goto out;
 	}
+    
 	// check response
 	if (PCIRegP[0]->Response[0] & (read ? 0xcff80000 : 0xeff80000)) {
 		IOLog("VoodooSDHCI: Unexpected response from command %d (SDMA): Response: 0x%x\n",
@@ -1121,9 +1131,11 @@
 	}
 	
 	clock_interval_to_deadline(5000, kMillisecondScale, (uint64_t*)&deadline);
-	IOLockLock(sdmaCond);
-	while ((PCIRegP[0]->NormalIntStatus & ErrorInterrupt) == 0) {
-		if (IOLockSleepDeadline(sdmaCond, sdmaCond, deadline, THREAD_UNINT) == THREAD_TIMED_OUT) {
+
+    IOLockLock(sdmaCond);
+	while ((PCIRegP[0]->NormalIntStatus & ErrorInterrupt) == 0)
+    {
+        if (IOLockSleepDeadline(sdmaCond, sdmaCond, deadline, THREAD_UNINT) == THREAD_TIMED_OUT) {
 			IOLockUnlock(sdmaCond);
 			// timeout
 			IOLog("VoodooSDHCI: I/O timeout during SDMA transfer: Status: 0x%x, Error: 0x%x, Block: %d, Offset: %d, Blocks: %d\n",
@@ -1131,7 +1143,7 @@
 			ret = kIOReturnTimeout;
 			goto out;
 		}
-		nis = PCIRegP[0]->NormalIntStatus;
+		UInt32 nis = PCIRegP[0]->NormalIntStatus;
 		if (nis & XferComplete) {
 			IOLockUnlock(sdmaCond);
 			if (read) {
@@ -1156,6 +1168,7 @@
 			PCIRegP[0]->SDMASysAddr = physSdmaBuff;
 		}
 	}
+    
 	IOLockUnlock(sdmaCond);
 	// error
 	IOLog("VoodooSDHCI: I/O error during SDMA transfer: Status: 0x%x, Error: 0x%x, Block: %d, Offset: %d, Blocks: %d\n",
@@ -1512,16 +1525,22 @@
 		IOLog("READING FROM CARD!\n");
 #endif
 		blk = block;
-		n = nblks;
+        n = nblks;
 		while (n) {
 			if (USE_SDMA) {
 				int i;
+                int b = MIN(8192 /* should fit in sdma buff */, n);
 				for (i = 0; i < SDMA_RETRY_COUNT; i++)
-					if ((ret = sdma_access(buffer, block, nblks, true)) != kIOReturnTimeout)
+                {
+					if ((ret = sdma_access(buffer, blk, b, blk - block, true)) != kIOReturnTimeout)
 						break;
-				if (i != 0)
-					IOLog("VoodooSDHCI: retry succeeded\n");
-				n = 0;
+                }
+				if (i != 0 && i < SDMA_RETRY_COUNT)
+					IOLog("VoodooSDHCI: read retry succeeded, tries = %d\n", i);
+                else if (i >= SDMA_RETRY_COUNT)
+                    IOLog("VoodooSDHCI: read retry failed\n");
+                n -= b;
+                blk += b;
 			} else if ((nblks > 1) && USE_MULTIBLOCK) {
 				int b = MIN(2048 /* should fit in sdma buff */, n);
 				
@@ -1560,15 +1579,21 @@
 		while (n) {
 			if (USE_SDMA) {
 				int i;
+                int b = MIN(8192 /* should fit in sdma buff */, n);
 				for (i = 0; i < SDMA_RETRY_COUNT; i++)
-					if ((ret = sdma_access(buffer, block, nblks, false)) != kIOReturnTimeout)
+                {
+					if ((ret = sdma_access(buffer, blk, b, blk - block, false)) != kIOReturnTimeout)
 						break;
-				if (i != 0)
-					IOLog("VoodooSDHCI: retry succeeded\n");
-				n = 0;
+                }
+				if (i != 0 && i < SDMA_RETRY_COUNT)
+					IOLog("VoodooSDHCI: write retry succeeded, tries = %d\n", i);
+                else if (i >= SDMA_RETRY_COUNT)
+                    IOLog("VoodooSDHCI: write retry failed\n");
+                n -= b;
+                blk += b;
 			} 
 			else if ((nblks > 1) && USE_MULTIBLOCK) {
-				int b = MIN(2048, n);
+				int b = MIN(2048 /* should fit in sdma buff */, n);
 				ret = writeBlockMulti_pio(buffer, blk, b,
 										  blk - block);
 				n -= b;
@@ -1640,11 +1665,14 @@
 		while (n) {
 			if (USE_SDMA) {
 				int i;
+                int b = MIN(8192 /* should fit in sdma buff */, n);
 				for (i = 0; i < SDMA_RETRY_COUNT; i++)
-					if ((ret = sdma_access(buffer, block, nblks, true)) != kIOReturnTimeout)
+					if ((ret = sdma_access(buffer, blk, b, blk - block, true)) != kIOReturnTimeout)
 						break;
-				if (i != 0)
-					IOLog("VoodooSDHCI: retry succeeded\n");
+				if (i != 0 && i < SDMA_RETRY_COUNT)
+					IOLog("VoodooSDHCI: read retry succeeded, tries = %d\n", i);
+                else if (i >= SDMA_RETRY_COUNT)
+                    IOLog("VoodooSDHCI: read retry failed\n");
 				n = 0;
 			} else if ((nblks > 1) && USE_MULTIBLOCK) {
 				int b = MIN(2048 /* should fit in sdma buff */, n);
@@ -1684,11 +1712,14 @@
 		while (n) {
 			if (USE_SDMA) {
 				int i;
+                int b = MIN(8192 /* should fit in sdma buff */, n);
 				for (i = 0; i < SDMA_RETRY_COUNT; i++)
-					if ((ret = sdma_access(buffer, block, nblks, false)) != kIOReturnTimeout)
+					if ((ret = sdma_access(buffer, blk, b, blk - block, false)) != kIOReturnTimeout)
 						break;
-				if (i != 0)
-					IOLog("VoodooSDHCI: retry succeeded\n");
+				if (i != 0 && i < SDMA_RETRY_COUNT)
+					IOLog("VoodooSDHCI: write retry succeeded, tries = %d\n", i);
+                else if (i >= SDMA_RETRY_COUNT)
+                    IOLog("VoodooSDHCI: write retry failed\n");
 				n = 0;
 			} 
 			else if ((nblks > 1) && USE_MULTIBLOCK) {
@@ -1741,7 +1772,7 @@
 void VoodooSDHC::handleInterrupt()
 {
 	IOLockLock(sdmaCond);
-	IOLockWakeup(sdmaCond, sdmaCond, true);
+	IOLockWakeup(sdmaCond, sdmaCond, false);
 	IOLockUnlock(sdmaCond);
 }
 
Index: trunk/VoodooSDHC.h
===================================================================
--- trunk/VoodooSDHC.h	(revision 2)
+++ trunk/VoodooSDHC.h	(working copy)
@@ -144,7 +144,7 @@
 	IOReturn		reportMaxWriteTransfer(UInt64 blockSize, UInt64 *max);
 	IOReturn		reportMaxReadTransfer (UInt64 blockSize, UInt64 *max);
 #endif
-	IOReturn		sdma_access(IOMemoryDescriptor *buffer, UInt32 block, UInt32 nblks, bool read);
+	IOReturn		sdma_access(IOMemoryDescriptor *buffer, UInt32 block, UInt32 nblks, UInt32 offset, bool read);
 	IOReturn		readBlockMulti_pio(IOMemoryDescriptor *buffer, UInt32 block, UInt32 nblks,
 							UInt32 offset);
 	IOReturn		readBlockSingle_pio(UInt8 *buff, UInt32 block);
Index: trunk/VoodooSDHC.xcodeproj/project.pbxproj
===================================================================
--- trunk/VoodooSDHC.xcodeproj/project.pbxproj	(revision 2)
+++ trunk/VoodooSDHC.xcodeproj/project.pbxproj	(working copy)
@@ -9,7 +9,6 @@
 /* Begin PBXBuildFile section */
 		052A08C50BF57D0A00D3692D /* SD_DataTypes.h in Headers */ = {isa = PBXBuildFile; fileRef = 052A08C40BF57D0A00D3692D /* SD_DataTypes.h */; };
 		05D69FA60BF0CE7D00AA4006 /* SDHCI_Register_Map.h in Headers */ = {isa = PBXBuildFile; fileRef = 05D69FA50BF0CE7D00AA4006 /* SDHCI_Register_Map.h */; };
-		05D69FBB0BF0D16100AA4006 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 32D94FCF0562CBF700B6AF17 /* Info.plist */; };
 		05D6A0630BF128B500AA4006 /* SD_Commands.h in Headers */ = {isa = PBXBuildFile; fileRef = 05D6A0620BF128B500AA4006 /* SD_Commands.h */; };
 		05D6A0670BF1293100AA4006 /* SD_Misc.h in Headers */ = {isa = PBXBuildFile; fileRef = 05D6A0660BF1293100AA4006 /* SD_Misc.h */; };
 		1A147D05107EB37E006FFB43 /* License.h in Headers */ = {isa = PBXBuildFile; fileRef = 6C98484A0F9A5D7800A2842D /* License.h */; };
@@ -133,6 +132,9 @@
 /* Begin PBXProject section */
 		089C1669FE841209C02AAC07 /* Project object */ = {
 			isa = PBXProject;
+			attributes = {
+				LastUpgradeCheck = 0460;
+			};
 			buildConfigurationList = 1DEB91DD08733DB10010E9CD /* Build configuration list for PBXProject "VoodooSDHC" */;
 			compatibilityVersion = "Xcode 3.2";
 			developmentRegion = English;
@@ -158,7 +160,6 @@
 			buildActionMask = 2147483647;
 			files = (
 				32D94FC80562CBF700B6AF17 /* InfoPlist.strings in Resources */,
-				05D69FBB0BF0D16100AA4006 /* Info.plist in Resources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -201,10 +202,10 @@
 			isa = XCBuildConfiguration;
 			buildSettings = {
 				ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
+				COMBINE_HIDPI_IMAGES = YES;
 				COPY_PHASE_STRIP = NO;
 				CURRENT_PROJECT_VERSION = 1.1d1;
 				GCC_DYNAMIC_NO_PIC = NO;
-				GCC_ENABLE_FIX_AND_CONTINUE = YES;
 				GCC_MODEL_TUNING = G5;
 				GCC_OPTIMIZATION_LEVEL = 0;
 				INFOPLIST_FILE = Info.plist;
@@ -213,6 +214,7 @@
 				MODULE_VERSION = 1.1d1;
 				ONLY_ACTIVE_ARCH = NO;
 				PRODUCT_NAME = VoodooSDHC;
+				SDKROOT = macosx10.7;
 				WRAPPER_EXTENSION = kext;
 				ZERO_LINK = YES;
 			};
@@ -221,7 +223,8 @@
 		1DEB91DB08733DB10010E9CD /* Release */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
-				ARCHS = "$(NATIVE_ARCH)";
+				ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
+				COMBINE_HIDPI_IMAGES = YES;
 				CURRENT_PROJECT_VERSION = 1.1d1;
 				GCC_GENERATE_DEBUGGING_SYMBOLS = NO;
 				GCC_MODEL_TUNING = G5;
@@ -230,6 +233,7 @@
 				MODULE_NAME = com.Voodoo.driver.VoodooSDHC;
 				MODULE_VERSION = 1.1d1;
 				PRODUCT_NAME = VoodooSDHC;
+				SDKROOT = macosx10.7;
 				WRAPPER_EXTENSION = kext;
 			};
 			name = Release;
@@ -243,8 +247,7 @@
 				GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS = NO;
 				GCC_WARN_PEDANTIC = NO;
 				GCC_WARN_UNUSED_VARIABLE = YES;
-				PREBINDING = NO;
-				SDKROOT = macosx10.6;
+				SDKROOT = macosx;
 				VALID_ARCHS = "i386 x86_64";
 			};
 			name = Debug;
@@ -254,8 +257,7 @@
 			buildSettings = {
 				GCC_WARN_ABOUT_RETURN_TYPE = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
-				PREBINDING = NO;
-				SDKROOT = macosx10.6;
+				SDKROOT = macosx;
 			};
 			name = Release;
 		};
