summaryrefslogtreecommitdiff
path: root/firmware/export/storage.h
diff options
context:
space:
mode:
authorMichael Sevakis <jethead71@rockbox.org>2017-03-15 01:51:54 -0400
committerMichael Sevakis <jethead71@rockbox.org>2017-10-26 14:35:41 -0400
commit1654efc31339972d0e6bd41a499fcffc0a45822e (patch)
tree5fb7f59ab918a3694608bb1138c2c52fb47698c3 /firmware/export/storage.h
parent7807934a271e9eb7b045cdcd89ba70fb59a91d69 (diff)
downloadrockbox-1654efc31339972d0e6bd41a499fcffc0a45822e.tar.gz
rockbox-1654efc31339972d0e6bd41a499fcffc0a45822e.zip
Unify storage threads into one
* Editing a bunch of drivers' thread routines in order to implement a new feature is tedious. * No matter the number of storage drivers, they share one thread. No extra threads needed for CONFIG_STORAGE_MULTI. * Each has an event callback called by the storage thread. * A default callback is provided to fake sleeping in order to trigger idle callbacks. It could also do other default processing. Changes to it will be part of driver code without editing each one. * Drivers may sleep and wake as they please as long as they give a low pulse on their storage bit to ask to go into sleep mode. Idle callback is called on its behalf and driver immediately put into sleep mode. * Drivers may indicate they are to continue receiving events in USB mode, otherwise they receve nothing until disconnect (they do receive SYS_USB_DISCONNECTED no matter what). * Rework a few things to keep the callback implementation sane and maintainable. ata.c was dreadful with all those bools; make it a state machine and easier to follow. Remove last_user_activity; it has no purpose that isn't served by keeping the disk active through last_disk_activity instead. * Even-out stack sizes partly because of a lack of a decent place to define them by driver or SoC or whatever; it doesn't seem too critical to do that anyway. Many are simply too large while at least one isn't really adequate. They may be individually overridden if necessary (figure out where). The thread uses the greatest size demanded. Newer file code is much more frugal with stack space. I barely see use crack 50% after idle callbacks (usually mid-40s). Card insert/eject doesn't demand much. * No forcing of idle callbacks. If it isn't necessary for one or more non-disk storage types, it really isn't any more necessary for disk storage. Besides, it makes the whole thing easier to implement. Change-Id: Id30c284d82a8af66e47f2cfe104c52cbd8aa7215
Diffstat (limited to 'firmware/export/storage.h')
-rw-r--r--firmware/export/storage.h78
1 files changed, 64 insertions, 14 deletions
diff --git a/firmware/export/storage.h b/firmware/export/storage.h
index 14cba09b35..8a4c95c05b 100644
--- a/firmware/export/storage.h
+++ b/firmware/export/storage.h
@@ -25,6 +25,7 @@
25#include <stdbool.h> 25#include <stdbool.h>
26#include "config.h" /* for HAVE_MULTIDRIVE or not */ 26#include "config.h" /* for HAVE_MULTIDRIVE or not */
27#include "mv.h" 27#include "mv.h"
28#include <kernel.h>
28 29
29#if (CONFIG_STORAGE & STORAGE_HOSTFS) || defined(SIMULATOR) 30#if (CONFIG_STORAGE & STORAGE_HOSTFS) || defined(SIMULATOR)
30#define HAVE_HOSTFS 31#define HAVE_HOSTFS
@@ -46,6 +47,57 @@
46#include "ramdisk.h" 47#include "ramdisk.h"
47#endif 48#endif
48 49
50enum
51{
52 Q_STORAGE_TICK = 1,
53 Q_STORAGE_SLEEP,
54 Q_STORAGE_SLEEPNOW,
55#ifdef STORAGE_CLOSE
56 Q_STORAGE_CLOSE,
57#endif
58};
59
60#define STG_EVENT_ASSERT_ACTIVE(type) \
61 ({ intptr_t __data = (data); \
62 *((unsigned int *)(__data)) |= (type); })
63
64static FORCE_INLINE int storage_event_default_handler(long id,
65 intptr_t data,
66 long last_activity,
67 unsigned int type)
68{
69 /* fake sleep in order to trigger storage idle sequence */
70 static long slept_at = -1;
71
72 if (id == Q_STORAGE_TICK) {
73 if (last_activity == slept_at ||
74 TIME_BEFORE(current_tick, last_activity + 3*HZ)) {
75 STG_EVENT_ASSERT_ACTIVE(type);
76 }
77 }
78 else if (id == Q_STORAGE_SLEEPNOW) {
79 slept_at = last_activity;
80 }
81
82 return 0;
83}
84
85#if (CONFIG_STORAGE & STORAGE_SD)
86int sd_event(long id, intptr_t data);
87#endif
88#if (CONFIG_STORAGE & STORAGE_MMC)
89int mmc_event(long id, intptr_t data);
90#endif
91#if (CONFIG_STORAGE & STORAGE_ATA)
92int ata_event(long id, intptr_t data);
93#endif
94#if (CONFIG_STORAGE & STORAGE_NAND)
95int nand_event(long id, intptr_t data);
96#endif
97#if (CONFIG_STORAGE & STORAGE_RAMDISK)
98int ramdisk_event(long id, intptr_t data);
99#endif
100
49struct storage_info 101struct storage_info
50{ 102{
51 unsigned int sector_size; 103 unsigned int sector_size;
@@ -55,13 +107,24 @@ struct storage_info
55 char *revision; 107 char *revision;
56}; 108};
57 109
110int storage_init(void) STORAGE_INIT_ATTR;
111void storage_close(void);
112
58#ifdef HAVE_HOSTFS 113#ifdef HAVE_HOSTFS
59#include "hostfs.h" 114#include "hostfs.h"
60/* stubs for the plugin api */ 115/* stubs for the plugin api */
61static inline void stub_storage_sleep(void) {} 116static inline void stub_storage_sleep(void) {}
62static inline void stub_storage_spin(void) {} 117static inline void stub_storage_spin(void) {}
63static inline void stub_storage_spindown(int timeout) { (void)timeout; } 118static inline void stub_storage_spindown(int timeout) { (void)timeout; }
119static inline int stub_storage_event(long id, intptr_t data)
120 { return 0; (void)id; (void)data; }
121#else /* ndef HAVE_HOSTFS */
122#if (CONFIG_STORAGE & STORAGE_ATA)
123void storage_sleep(void);
124#else
125static inline void storage_sleep(void) {}
64#endif 126#endif
127#endif /* HAVE_HOSTFS */
65 128
66#if !defined(CONFIG_STORAGE_MULTI) || defined(HAVE_HOSTFS) 129#if !defined(CONFIG_STORAGE_MULTI) || defined(HAVE_HOSTFS)
67/* storage_spindown, storage_sleep and storage_spin are passed as 130/* storage_spindown, storage_sleep and storage_spin are passed as
@@ -70,6 +133,7 @@ static inline void stub_storage_spindown(int timeout) { (void)timeout; }
70 #define storage_num_drives() NUM_DRIVES 133 #define storage_num_drives() NUM_DRIVES
71 #if defined(HAVE_HOSTFS) 134 #if defined(HAVE_HOSTFS)
72 #define STORAGE_FUNCTION(NAME) (stub_## NAME) 135 #define STORAGE_FUNCTION(NAME) (stub_## NAME)
136 #define storage_event stub_storage_event
73 #define storage_spindown stub_storage_spindown 137 #define storage_spindown stub_storage_spindown
74 #define storage_sleep stub_storage_sleep 138 #define storage_sleep stub_storage_sleep
75 #define storage_spin stub_storage_spin 139 #define storage_spin stub_storage_spin
@@ -97,15 +161,12 @@ static inline void stub_storage_spindown(int timeout) { (void)timeout; }
97 #elif (CONFIG_STORAGE & STORAGE_ATA) 161 #elif (CONFIG_STORAGE & STORAGE_ATA)
98 #define STORAGE_FUNCTION(NAME) (ata_## NAME) 162 #define STORAGE_FUNCTION(NAME) (ata_## NAME)
99 #define storage_spindown ata_spindown 163 #define storage_spindown ata_spindown
100 #define storage_sleep ata_sleep
101 #define storage_spin ata_spin 164 #define storage_spin ata_spin
102 165
103 #define storage_enable(on) ata_enable(on) 166 #define storage_enable(on) ata_enable(on)
104 #define storage_sleepnow() ata_sleepnow() 167 #define storage_sleepnow() ata_sleepnow()
105 #define storage_disk_is_active() ata_disk_is_active() 168 #define storage_disk_is_active() ata_disk_is_active()
106 #define storage_soft_reset() ata_soft_reset() 169 #define storage_soft_reset() ata_soft_reset()
107 #define storage_init() ata_init()
108 #define storage_close() ata_close()
109 #ifdef HAVE_STORAGE_FLUSH 170 #ifdef HAVE_STORAGE_FLUSH
110 #define storage_flush() (void)0 171 #define storage_flush() (void)0
111 #endif 172 #endif
@@ -124,15 +185,12 @@ static inline void stub_storage_spindown(int timeout) { (void)timeout; }
124 #elif (CONFIG_STORAGE & STORAGE_SD) 185 #elif (CONFIG_STORAGE & STORAGE_SD)
125 #define STORAGE_FUNCTION(NAME) (sd_## NAME) 186 #define STORAGE_FUNCTION(NAME) (sd_## NAME)
126 #define storage_spindown sd_spindown 187 #define storage_spindown sd_spindown
127 #define storage_sleep sd_sleep
128 #define storage_spin sd_spin 188 #define storage_spin sd_spin
129 189
130 #define storage_enable(on) sd_enable(on) 190 #define storage_enable(on) sd_enable(on)
131 #define storage_sleepnow() sd_sleepnow() 191 #define storage_sleepnow() sd_sleepnow()
132 #define storage_disk_is_active() 0 192 #define storage_disk_is_active() 0
133 #define storage_soft_reset() (void)0 193 #define storage_soft_reset() (void)0
134 #define storage_init() sd_init()
135 #define storage_close() sd_close()
136 #ifdef HAVE_STORAGE_FLUSH 194 #ifdef HAVE_STORAGE_FLUSH
137 #define storage_flush() (void)0 195 #define storage_flush() (void)0
138 #endif 196 #endif
@@ -151,14 +209,12 @@ static inline void stub_storage_spindown(int timeout) { (void)timeout; }
151 #elif (CONFIG_STORAGE & STORAGE_MMC) 209 #elif (CONFIG_STORAGE & STORAGE_MMC)
152 #define STORAGE_FUNCTION(NAME) (mmc_## NAME) 210 #define STORAGE_FUNCTION(NAME) (mmc_## NAME)
153 #define storage_spindown mmc_spindown 211 #define storage_spindown mmc_spindown
154 #define storage_sleep mmc_sleep
155 #define storage_spin mmc_spin 212 #define storage_spin mmc_spin
156 213
157 #define storage_enable(on) mmc_enable(on) 214 #define storage_enable(on) mmc_enable(on)
158 #define storage_sleepnow() mmc_sleepnow() 215 #define storage_sleepnow() mmc_sleepnow()
159 #define storage_disk_is_active() mmc_disk_is_active() 216 #define storage_disk_is_active() mmc_disk_is_active()
160 #define storage_soft_reset() (void)0 217 #define storage_soft_reset() (void)0
161 #define storage_init() mmc_init()
162 #ifdef HAVE_STORAGE_FLUSH 218 #ifdef HAVE_STORAGE_FLUSH
163 #define storage_flush() (void)0 219 #define storage_flush() (void)0
164 #endif 220 #endif
@@ -177,14 +233,12 @@ static inline void stub_storage_spindown(int timeout) { (void)timeout; }
177 #elif (CONFIG_STORAGE & STORAGE_NAND) 233 #elif (CONFIG_STORAGE & STORAGE_NAND)
178 #define STORAGE_FUNCTION(NAME) (nand_## NAME) 234 #define STORAGE_FUNCTION(NAME) (nand_## NAME)
179 #define storage_spindown nand_spindown 235 #define storage_spindown nand_spindown
180 #define storage_sleep nand_sleep
181 #define storage_spin nand_spin 236 #define storage_spin nand_spin
182 237
183 #define storage_enable(on) (void)0 238 #define storage_enable(on) (void)0
184 #define storage_sleepnow() nand_sleepnow() 239 #define storage_sleepnow() nand_sleepnow()
185 #define storage_disk_is_active() 0 240 #define storage_disk_is_active() 0
186 #define storage_soft_reset() (void)0 241 #define storage_soft_reset() (void)0
187 #define storage_init() nand_init()
188 #ifdef HAVE_STORAGE_FLUSH 242 #ifdef HAVE_STORAGE_FLUSH
189 #define storage_flush() nand_flush() 243 #define storage_flush() nand_flush()
190 #endif 244 #endif
@@ -203,14 +257,12 @@ static inline void stub_storage_spindown(int timeout) { (void)timeout; }
203 #elif (CONFIG_STORAGE & STORAGE_RAMDISK) 257 #elif (CONFIG_STORAGE & STORAGE_RAMDISK)
204 #define STORAGE_FUNCTION(NAME) (ramdisk_## NAME) 258 #define STORAGE_FUNCTION(NAME) (ramdisk_## NAME)
205 #define storage_spindown ramdisk_spindown 259 #define storage_spindown ramdisk_spindown
206 #define storage_sleep ramdisk_sleep
207 #define storage_spin ramdisk_spin 260 #define storage_spin ramdisk_spin
208 261
209 #define storage_enable(on) (void)0 262 #define storage_enable(on) (void)0
210 #define storage_sleepnow() ramdisk_sleepnow() 263 #define storage_sleepnow() ramdisk_sleepnow()
211 #define storage_disk_is_active() 0 264 #define storage_disk_is_active() 0
212 #define storage_soft_reset() (void)0 265 #define storage_soft_reset() (void)0
213 #define storage_init() ramdisk_init()
214 #ifdef HAVE_STORAGE_FLUSH 266 #ifdef HAVE_STORAGE_FLUSH
215 #define storage_flush() (void)0 267 #define storage_flush() (void)0
216 #endif 268 #endif
@@ -234,11 +286,9 @@ static inline void stub_storage_spindown(int timeout) { (void)timeout; }
234/* Multi-driver use normal functions */ 286/* Multi-driver use normal functions */
235 287
236void storage_enable(bool on); 288void storage_enable(bool on);
237void storage_sleep(void);
238void storage_sleepnow(void); 289void storage_sleepnow(void);
239bool storage_disk_is_active(void); 290bool storage_disk_is_active(void);
240int storage_soft_reset(void); 291int storage_soft_reset(void);
241int storage_init(void) STORAGE_INIT_ATTR;
242int storage_flush(void); 292int storage_flush(void);
243void storage_spin(void); 293void storage_spin(void);
244void storage_spindown(int seconds); 294void storage_spindown(int seconds);