Switch from GPLv2 to GPLv2+
[fw/altos] / src / drivers / ao_bufio.c
index 9a5e801ba5830390baaee986774082861b9a3b3a..f212da53b36088491f6c91bf1f5d8287d57f9bd6 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 #include "ao.h"
 #endif
 
+/* Include bufio commands */
+#ifndef AO_FAT_TEST
+#define BUFIO_COMMANDS 0
+#endif
+
 #include "ao_sdcard.h"
 #include "ao_bufio.h"
 
-#define AO_NUM_BUF             4
+#define AO_NUM_BUF             16
 #define AO_BUFSIZ              512
 
 struct ao_bufio {
@@ -40,7 +46,7 @@ static uint8_t                ao_bufio_mutex;
 #if 0
 #define DBG(...) printf(__VA_ARGS__)
 #else
-#define DBG(...)
+#define DBG(...) (void) 0
 #endif
 
 static inline void
@@ -205,10 +211,13 @@ ao_bufio_get(uint32_t block)
                                bufio->block = 0xffffffff;
                                bufio = NULL;
                        }
-               }
+               } else
+                       ao_panic(AO_PANIC_BUFIO);
        }
        if (bufio) {
                bufio->busy++;
+               if (!bufio->busy)
+                       ao_panic(AO_PANIC_BUFIO);
                buf = ao_bufio_to_buf(bufio);
        }
        ao_bufio_unlock();
@@ -227,6 +236,9 @@ ao_bufio_put(uint8_t *buf, uint8_t write)
        ao_bufio_lock();
        bufio = ao_buf_to_bufio(buf);
        
+       if (!bufio->busy)
+               ao_panic(AO_PANIC_BUFIO);
+
        DBG ("idle buffer %d write %d\n", ao_bufio_to_num(bufio), write);
        bufio->dirty |= write;
        if (!--bufio->busy) {
@@ -262,6 +274,7 @@ ao_bufio_flush(void)
        ao_bufio_unlock();
 }
 
+#if BUFIO_COMMANDS
 static void
 ao_bufio_test_read(void)
 {
@@ -284,15 +297,26 @@ static const struct ao_cmds ao_bufio_cmds[] = {
        { ao_bufio_test_read,   "q\0Test bufio read" },
        { 0, NULL },
 };
+#endif
 
 void
-ao_bufio_init(void)
+ao_bufio_setup(void)
 {
        int b;
 
-       for (b = 0; b < AO_NUM_BUF; b++)
+       for (b = 0; b < AO_NUM_BUF; b++) {
+               ao_bufio[b].dirty = 0;
+               ao_bufio[b].busy = 0;
                ao_bufio[b].block = 0xffffffff;
-       ao_sdcard_init();
+       }
+}
 
+void
+ao_bufio_init(void)
+{
+       ao_bufio_setup();
+       ao_sdcard_init();
+#if BUFIO_COMMANDS
        ao_cmd_register(&ao_bufio_cmds[0]);
+#endif
 }