Index: apps/main.c
===================================================================
RCS file: /cvsroot/rockbox/apps/main.c,v
retrieving revision 1.187
diff -u -r1.187 main.c
--- apps/main.c	15 Aug 2006 22:54:05 -0000	1.187
+++ apps/main.c	27 Aug 2006 14:44:39 -0000
@@ -211,6 +211,7 @@
     init_threads();
     buffer_init();
     lcd_init();
+    ata_init();
 #ifdef HAVE_REMOTE_LCD
     lcd_remote_init();
 #endif
@@ -262,7 +263,6 @@
 }
 
 #else
-
 void init(void)
 {
     int rc;
@@ -370,7 +370,8 @@
 #endif
         panicf("ata: %d", rc);
     }
-
+
+    ata_accesslater_callback_init();
 #ifdef HAVE_EEPROM_SETTINGS
     eeprom_settings_init();
 #endif
Index: firmware/SOURCES
===================================================================
RCS file: /cvsroot/rockbox/firmware/SOURCES,v
retrieving revision 1.107
diff -u -r1.107 SOURCES
--- firmware/SOURCES	23 Aug 2006 17:49:19 -0000	1.107
+++ firmware/SOURCES	27 Aug 2006 14:44:45 -0000
@@ -10,6 +10,7 @@
 #ifndef SIMULATOR
 common/dir.c
 common/file.c
+ata_accesslater_callback.c
 #endif
 #ifdef HAVE_DIRCACHE
 common/dircache.c
Index: firmware/ata_accesslater_callback.c
===================================================================
RCS file: firmware/ata_accesslater_callback.c
diff -N firmware/ata_accesslater_callback.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ firmware/ata_accesslater_callback.c	27 Aug 2006 14:44:45 -0000
@@ -0,0 +1,73 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id:  $
+ *
+ * Copyright (C) 2006 Jonathan Gordon
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#include <stdbool.h>
+#include "system.h"
+#include "kernel.h"
+#include "thread.h"
+#include "string.h"
+#include "ata_accesslater_callback.h"
+
+static ata_accesslater_callback ata_accesslater_callback_funcs[MAX_ATA_CALLBACKS];
+bool register_ata_accesslater_func(ata_accesslater_callback function)
+{
+    int i;
+    for (i=0; i<MAX_ATA_CALLBACKS; i++)
+    {
+        if (ata_accesslater_callback_funcs[i] == NULL)
+        {   
+            ata_accesslater_callback_funcs[i] = function;
+            return true;
+        }
+    }
+    return false;
+}
+void unregister_ata_accesslater_func(ata_accesslater_callback func)
+{
+    int i;
+    for (i=0; i<MAX_ATA_CALLBACKS; i++)
+    {
+        if (ata_accesslater_callback_funcs[i] == func)
+            ata_accesslater_callback_funcs[i] = NULL;
+    }
+    return;
+}
+
+void call_ata_accesslater_callbacks(void)
+{
+    int i;
+    ata_accesslater_callback function;
+    for (i = 0; i < MAX_ATA_CALLBACKS; i++)
+    {
+        if (ata_accesslater_callback_funcs[i])
+        {
+            function = ata_accesslater_callback_funcs[i];
+            ata_accesslater_callback_funcs[i] = NULL;
+            function();
+        }
+    }
+}
+
+void ata_accesslater_callback_init(void)
+{
+    int i;
+    for (i=0; i<MAX_ATA_CALLBACKS; i++)
+    {
+        ata_accesslater_callback_funcs[i] = NULL;
+    }
+}
Index: firmware/drivers/ata.c
===================================================================
RCS file: /cvsroot/rockbox/firmware/drivers/ata.c,v
retrieving revision 1.177
diff -u -r1.177 ata.c
--- firmware/drivers/ata.c	12 Aug 2006 08:01:53 -0000	1.177
+++ firmware/drivers/ata.c	27 Aug 2006 14:44:47 -0000
@@ -29,7 +29,7 @@
 #include "power.h"
 #include "string.h"
 #include "hwcompat.h"
-
+#include "ata_accesslater_callback.h"
 #ifdef TARGET_TREE
 #include "ata-target.h"
 #endif
@@ -897,7 +897,7 @@
     mutex_unlock(&ata_mtx);
 
     /* only flush if reading went ok */
-    if ( (ret == 0) && delayed_write )
+    if (ret == 0)
         ata_flush();
 
     return ret;
@@ -1322,7 +1322,7 @@
     mutex_unlock(&ata_mtx);
 
     /* only flush if writing went ok */
-    if ( (ret == 0) && delayed_write )
+    if (ret == 0)
         ata_flush();
 
     return ret;
@@ -1345,6 +1345,7 @@
         delayed_write = false;
         ata_write_sectors(IF_MV2(0,) delayed_sector_num, 1, delayed_sector);
     }
+    call_ata_accesslater_callbacks();
 }
 
 
Index: firmware/export/ata_accesslater_callback.h
===================================================================
RCS file: firmware/export/ata_accesslater_callback.h
diff -N firmware/export/ata_accesslater_callback.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ firmware/export/ata_accesslater_callback.h	27 Aug 2006 14:44:47 -0000
@@ -0,0 +1,32 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id:  $
+ *
+ * Copyright (C) 2006 Jonathan Gordon
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#ifndef __ATACALLBACK_H__
+#define __ATACALLBACK_H__
+
+#include <stdbool.h>
+
+#define MAX_ATA_CALLBACKS 5
+extern void ata_accesslater_callback_init(void);
+typedef bool (*ata_accesslater_callback)(void);
+
+extern bool register_ata_accesslater_func(ata_accesslater_callback function);
+extern void unregister_ata_accesslater_func(ata_accesslater_callback function);
+extern void call_ata_accesslater_callbacks(void);
+
+#endif
Index: uisimulator/sdl/SOURCES
===================================================================
RCS file: /cvsroot/rockbox/uisimulator/sdl/SOURCES,v
retrieving revision 1.4
diff -u -r1.4 SOURCES
--- uisimulator/sdl/SOURCES	28 Jul 2006 07:35:45 -0000	1.4
+++ uisimulator/sdl/SOURCES	27 Aug 2006 14:44:49 -0000
@@ -1,3 +1,4 @@
+ata-sim.c
 button.c
 kernel.c
 #ifdef HAVE_LCD_BITMAP
Index: uisimulator/sdl/ata-sim.c
===================================================================
RCS file: uisimulator/sdl/ata-sim.c
diff -N uisimulator/sdl/ata-sim.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ uisimulator/sdl/ata-sim.c	27 Aug 2006 14:47:09 -0000
@@ -0,0 +1,91 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id:  $
+ *
+ * Copyright (C) 2006 Jonathan Gordon
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#include <stdbool.h>
+#include "system.h"
+#include "kernel.h"
+#include "thread.h"
+#include "string.h"
+#include "ata_accesslater_callback.h"
+#include "debug.h"
+void sim_ata_thread(void)
+{
+    while (1)
+    {
+        sleep(HZ*60);
+        call_ata_accesslater_callbacks();
+    }
+}
+
+static long sim_ata_stack[DEFAULT_STACK_SIZE/sizeof(long)];
+
+int ata_init(void)
+{
+    ata_accesslater_callback_init();
+    create_thread(sim_ata_thread, sim_ata_stack, sizeof(sim_ata_stack), "sim_ata thread");
+    return 1;
+}
+/* the rest is copied from ata_accesslaster_callback.c because gcc sucks! */
+static ata_accesslater_callback ata_accesslater_callback_funcs[MAX_ATA_CALLBACKS];
+bool register_ata_accesslater_func(ata_accesslater_callback function)
+{
+    int i;
+    for (i=0; i<MAX_ATA_CALLBACKS; i++)
+    {
+        if (ata_accesslater_callback_funcs[i] == NULL)
+        {   
+            ata_accesslater_callback_funcs[i] = function;
+            return true;
+        }
+    }
+    return false;
+}
+void unregister_ata_accesslater_func(ata_accesslater_callback func)
+{
+    int i;
+    for (i=0; i<MAX_ATA_CALLBACKS; i++)
+    {
+        if (ata_accesslater_callback_funcs[i] == func)
+            ata_accesslater_callback_funcs[i] = NULL;
+    }
+    return;
+}
+
+void call_ata_accesslater_callbacks(void)
+{
+    int i;
+    ata_accesslater_callback function;
+    for (i = 0; i < MAX_ATA_CALLBACKS; i++)
+    {
+        if (ata_accesslater_callback_funcs[i])
+        {
+            function = ata_accesslater_callback_funcs[i];
+            ata_accesslater_callback_funcs[i] = NULL;
+            function();
+        }
+    }
+}
+
+void ata_accesslater_callback_init(void)
+{
+    int i;
+    for (i=0; i<MAX_ATA_CALLBACKS; i++)
+    {
+        ata_accesslater_callback_funcs[i] = NULL;
+    }
+}
