Fix mount problem


git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1835 42af7a65-404d-4744-a932-0658087f49c3
diff --git a/ChangeLog b/ChangeLog
index 95fced4..a21520a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -749,5 +749,7 @@
 	* configs/mcu123-lpc214x/src: Corrected some logic in the LPC2148 SPI receive block
 	  logic.  Re-verified SDC ver1.x support with 1Gb Toshiba SDC, 1Gb PNY SDC, 2Gb SanDisk SDC,
 	  and 4Gb Kingston SDHC.
+	* fs/fs_mount.c: Corrected error handling that could cause a deadlock on certain
+	  mount() failures.
 
 
diff --git a/Documentation/NuttX.html b/Documentation/NuttX.html
index 035d5d0..4e651ca 100644
--- a/Documentation/NuttX.html
+++ b/Documentation/NuttX.html
@@ -8,7 +8,7 @@
   <tr align="center" bgcolor="#e4e4e4">
     <td>
       <h1><big><font color="#3c34ec"><i>NuttX RTOS</i></font></big></h1>
-      <p>Last Updated: May 28, 2009</p>
+      <p>Last Updated: May 29, 2009</p>
     </td>
   </tr>
 </table>
@@ -1439,6 +1439,8 @@
 	* configs/mcu123-lpc214x/src: Corrected some logic in the LPC2148 SPI receive block
 	  logic.  Re-verified SDC ver1.x support with 1Gb Toshiba SDC, 1Gb PNY SDC, 2Gb SanDisk SDC,
 	  and 4Gb Kingston SDHC.
+	* fs/fs_mount.c: Corrected error handling that could cause a deadlock on certain
+	  mount() failures.
 
 pascal-0.1.3 2009-xx-xx Gregory Nutt &lt;spudmonkey@racsa.co.cr&gt;
 
diff --git a/drivers/mmcsd/mmcsd_spi.c b/drivers/mmcsd/mmcsd_spi.c
index dafcc4f..cadf8bb 100644
--- a/drivers/mmcsd/mmcsd_spi.c
+++ b/drivers/mmcsd/mmcsd_spi.c
@@ -431,8 +431,17 @@
   FAR struct spi_dev_s *spi = slot->spi;
   uint32 result;
   ubyte response = 0xff;
+  int ret;
   int i;
 
+  /* Wait until the card is not busy */
+
+  ret = mmcsd_waitready(slot);
+  if (ret != OK)
+    {
+      return ret;
+    }
+
   /* Send command code */
 
   SPI_SEND(spi, cmd->cmd);
@@ -1012,15 +1021,34 @@
     }
 #endif
 
-  /* Select the slave */
+  /* Verify that an MMC/SD card has been inserted */
 
+  ret = -ENODEV;
   mmcsd_semtake(&slot->sem);
-  SPI_SELECT(spi, SPIDEV_MMCSD, TRUE);
+  if ((SPI_STATUS(spi, SPIDEV_MMCSD) & SPI_STATUS_PRESENT) != 0)
+    {
+      /* Yes.. a card is present.  Has it been initialized? */
 
-  /* Verify that the MMC/SD card is alive and ready for business */
+      if (slot->type == MMCSD_CARDTYPE_UNKNOWN)
+        {
+          /* Ininitialize for the media in the slot */
 
-  ret = mmcsd_waitready(slot);
-  SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
+          ret = mmcsd_mediainitialize(slot);
+          if (ret < 0)
+            {
+              fvdbg("Failed to initialize card\n");
+              goto errout_with_sem;
+            }
+        }
+
+      /* Make sure that the card is ready */    
+
+      SPI_SELECT(spi, SPIDEV_MMCSD, TRUE);
+      ret = mmcsd_waitready(slot);
+      SPI_SELECT(spi, SPIDEV_MMCSD, FALSE);
+    }
+
+errout_with_sem:
   mmcsd_semgive(&slot->sem);
   return ret;
 }
@@ -1084,7 +1112,7 @@
     }
 #endif
 
-  /* Verify that card is availabled */
+  /* Verify that card is available */
 
   if (slot->state & MMCSD_SLOTSTATUS_NOTREADY)
     {
@@ -1237,7 +1265,7 @@
     }
 #endif
 
-  /* Verify that card is availabled */
+  /* Verify that card is available */
 
   if (slot->state & MMCSD_SLOTSTATUS_NOTREADY)
     {
diff --git a/examples/nsh/nsh_fscmds.c b/examples/nsh/nsh_fscmds.c
index 301abdd..5b51a4d 100644
--- a/examples/nsh/nsh_fscmds.c
+++ b/examples/nsh/nsh_fscmds.c
@@ -1,7 +1,7 @@
 /****************************************************************************
  * examples/nsh/nsh_fscmds.c
  *
- *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
diff --git a/fs/fat/fs_fat32.c b/fs/fat/fs_fat32.c
index a0a2774..a28d57a 100644
--- a/fs/fat/fs_fat32.c
+++ b/fs/fat/fs_fat32.c
@@ -1,7 +1,7 @@
 /****************************************************************************
  * fs_fat32.c
  *
- *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * References:
@@ -1482,7 +1482,7 @@
     }
 
   if (blkdriver->u.i_bops->open &&
-       blkdriver->u.i_bops->open(blkdriver) != OK)
+      blkdriver->u.i_bops->open(blkdriver) != OK)
     {
       return -ENODEV;
     }
diff --git a/fs/fs_mount.c b/fs/fs_mount.c
index d0806ba..22af7a1 100644
--- a/fs/fs_mount.c
+++ b/fs/fs_mount.c
@@ -1,7 +1,7 @@
 /****************************************************************************
  * fs/fs_mount.c
  *
- *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -231,7 +231,7 @@
 
       fdbg("Bind method failed: %d\n", status);
       errcode = -status;
-      goto errout_with_blkdrvr;
+      goto errout_with_mountpt;
   }
 
   /* We have it, now populate it with driver specific information. */
@@ -256,13 +256,16 @@
 
   /* A lot of goto's!  But they make the error handling much simpler */
 
-errout_with_blkdrvr:
-  inode_release(blkdrvr_inode);
 errout_with_mountpt:
+  inode_semgive();
+  inode_release(blkdrvr_inode);
   inode_release(mountpt_inode);
+  goto errout;
+
 errout_with_semaphore:
   inode_semgive();
   inode_release(blkdrvr_inode);
+
 errout:
   errno = errcode;
   return ERROR;