A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Alan Korr
11 * Copyright (C) 2008 by Frank Gevaerts
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22#ifndef __STORAGE_H__
23#define __STORAGE_H__
24
25#include <stdbool.h>
26#include "config.h" /* for HAVE_MULTIDRIVE or not */
27#include "mv.h"
28#include <kernel.h>
29
30#if (CONFIG_STORAGE & STORAGE_HOSTFS) || defined(SIMULATOR)
31#define HAVE_HOSTFS
32#endif
33
34#if (CONFIG_STORAGE & STORAGE_SD)
35#include "sd.h"
36#endif
37#if (CONFIG_STORAGE & STORAGE_USB)
38// TODO: Doesn't matter until we're native
39#endif
40#if (CONFIG_STORAGE & STORAGE_MMC)
41#include "mmc.h"
42#endif
43#if (CONFIG_STORAGE & STORAGE_ATA)
44#include "ata.h"
45#endif
46#if (CONFIG_STORAGE & STORAGE_NAND)
47#include "nand.h"
48#endif
49#if (CONFIG_STORAGE & STORAGE_RAMDISK)
50#include "ramdisk.h"
51#endif
52
53enum
54{
55 Q_STORAGE_TICK = 1,
56 Q_STORAGE_SLEEP,
57 Q_STORAGE_SLEEPNOW,
58#ifdef STORAGE_CLOSE
59 Q_STORAGE_CLOSE,
60#endif
61};
62
63#define STG_EVENT_ASSERT_ACTIVE(type) \
64 ({ intptr_t __data = (data); \
65 *((unsigned int *)(__data)) |= (type); })
66
67static FORCE_INLINE int storage_event_default_handler(long id,
68 intptr_t data,
69 long last_activity,
70 unsigned int type)
71{
72 /* fake sleep in order to trigger storage idle sequence */
73 static long slept_at = -1;
74
75 if (id == Q_STORAGE_TICK) {
76 if (last_activity == slept_at ||
77 TIME_BEFORE(current_tick, last_activity + 3*HZ)) {
78 STG_EVENT_ASSERT_ACTIVE(type);
79 }
80 }
81 else if (id == Q_STORAGE_SLEEPNOW) {
82 slept_at = last_activity;
83 }
84
85 return 0;
86}
87
88#if (CONFIG_STORAGE & STORAGE_SD)
89int sd_event(long id, intptr_t data);
90#endif
91#if (CONFIG_STORAGE & STORAGE_MMC)
92int mmc_event(long id, intptr_t data);
93#endif
94#if (CONFIG_STORAGE & STORAGE_ATA)
95int ata_event(long id, intptr_t data);
96#endif
97#if (CONFIG_STORAGE & STORAGE_NAND)
98int nand_event(long id, intptr_t data);
99#endif
100#if (CONFIG_STORAGE & STORAGE_RAMDISK)
101int ramdisk_event(long id, intptr_t data);
102#endif
103#if (CONFIG_STORAGE & STORAGE_USB)
104// int usb_event(long id, intptr_t data); // TODO: Implement
105#endif
106
107struct storage_info
108{
109 unsigned int sector_size;
110 sector_t num_sectors;
111#ifdef MAX_PHYS_SECTOR_SIZE
112 uint16_t phys_sector_mult;
113#endif
114 char *vendor;
115 char *product;
116 char *revision;
117};
118
119int storage_init(void) STORAGE_INIT_ATTR;
120void storage_close(void);
121
122#ifdef HAVE_HOSTFS
123#include "hostfs.h"
124/* stubs for the plugin api */
125static inline void stub_storage_sleep(void) {}
126static inline void stub_storage_spin(void) {}
127static inline void stub_storage_spindown(int timeout) { (void)timeout; }
128static inline int stub_storage_event(long id, intptr_t data)
129 { return 0; (void)id; (void)data; }
130static inline void storage_sleep(void) {};
131#else /* ndef HAVE_HOSTFS */
132#if (CONFIG_STORAGE & STORAGE_ATA)
133void storage_sleep(void);
134#else
135static inline void storage_sleep(void) {};
136#endif
137#endif /* HAVE_HOSTFS */
138
139#if !defined(CONFIG_STORAGE_MULTI) || defined(HAVE_HOSTFS)
140/* storage_spindown, storage_sleep and storage_spin are passed as
141 * pointers, which doesn't work with argument-macros.
142 */
143 #define storage_num_drives() NUM_DRIVES
144 #if defined(HAVE_HOSTFS)
145 #define STORAGE_FUNCTION(NAME) (stub_storage_## NAME)
146 #define storage_event stub_storage_event
147 #define storage_spindown(sec) stub_storage_spindown(sec)
148 #define storage_sleep() stub_storage_sleep()
149 #define storage_spin() stub_storage_spin()
150
151 #define storage_enable(on)
152 #define storage_sleepnow()
153 #define storage_disk_is_active() 0
154 #define storage_soft_reset()
155 #define storage_init() hostfs_init()
156 #ifdef HAVE_STORAGE_FLUSH
157 #define storage_flush() hostfs_flush()
158 #endif
159 #define storage_last_disk_activity() (-1)
160 #define storage_spinup_time() 0
161 #define storage_get_identify() (NULL) /* not actually called anywher */
162
163 #ifdef STORAGE_GET_INFO
164 #error storage_get_info not implemented
165 #endif
166 #ifdef HAVE_HOTSWAP
167 #define storage_removable(drive) hostfs_removable(IF_MD(drive))
168 #define storage_present(drive) hostfs_present(IF_MD(drive))
169 #endif
170 #define storage_driver_type(drive) hostfs_driver_type(IF_MV(drive))
171 #elif (CONFIG_STORAGE & STORAGE_ATA)
172 #define STORAGE_FUNCTION(NAME) (ata_## NAME)
173 #define storage_spindown(sec) ata_spindown(sec)
174 #define storage_spin() ata_spin()
175 #define storage_enable(on) ata_enable(on)
176 #define storage_sleepnow() ata_sleepnow()
177 #define storage_disk_is_active() ata_disk_is_active()
178 #define storage_soft_reset() ata_soft_reset()
179 #ifdef HAVE_STORAGE_FLUSH
180 #define storage_flush() ata_flush()
181 #endif
182 #define storage_last_disk_activity() ata_last_disk_activity()
183 #define storage_spinup_time() ata_spinup_time()
184 #define storage_get_identify() ata_get_identify()
185
186 #ifdef STORAGE_GET_INFO
187 #define storage_get_info(drive, info) ata_get_info(IF_MD(drive,) info)
188 #endif
189 #ifdef HAVE_HOTSWAP
190 #define storage_removable(drive) ata_removable(IF_MD(drive))
191 #define storage_present(drive) ata_present(IF_MD(drive))
192 #endif
193 #define storage_driver_type(drive) (STORAGE_ATA_NUM)
194 #elif (CONFIG_STORAGE & STORAGE_SD)
195 #define STORAGE_FUNCTION(NAME) (sd_## NAME)
196 #define storage_spindown(sec) sd_spindown(sec)
197 #define storage_spin() sd_spin()
198
199 #define storage_enable(on) sd_enable(on)
200 #define storage_sleepnow() do {} while (0)
201 #define storage_disk_is_active() 0
202 #define storage_soft_reset() (void)0
203 #ifdef HAVE_STORAGE_FLUSH
204 #define storage_flush() (void)0
205 #endif
206 #define storage_last_disk_activity() sd_last_disk_activity()
207 #define storage_spinup_time() 0
208 #define storage_get_identify() sd_get_identify()
209
210 #ifdef STORAGE_GET_INFO
211 #define storage_get_info(drive, info) sd_get_info(IF_MD(drive,) info)
212 #endif
213 #ifdef HAVE_HOTSWAP
214 #define storage_removable(drive) sd_removable(IF_MD(drive))
215 #define storage_present(drive) sd_present(IF_MD(drive))
216 #endif
217 #define storage_driver_type(drive) (STORAGE_SD_NUM)
218 #elif (CONFIG_STORAGE & STORAGE_MMC)
219 #define STORAGE_FUNCTION(NAME) (mmc_## NAME)
220 #define storage_spindown(sec) mmc_spindown(sec)
221 #define storage_spin() mmc_spin()
222
223 #define storage_enable(on) mmc_enable(on)
224 #define storage_sleepnow() mmc_sleepnow()
225 #define storage_disk_is_active() mmc_disk_is_active()
226 #define storage_soft_reset() (void)0
227 #ifdef HAVE_STORAGE_FLUSH
228 #define storage_flush() (void)0
229 #endif
230 #define storage_last_disk_activity() mmc_last_disk_activity()
231 #define storage_spinup_time() 0
232 #define storage_get_identify() mmc_get_identify()
233
234 #ifdef STORAGE_GET_INFO
235 #define storage_get_info(drive, info) mmc_get_info(IF_MD(drive,) info)
236 #endif
237 #ifdef HAVE_HOTSWAP
238 #define storage_removable(drive) mmc_removable(IF_MD(drive))
239 #define storage_present(drive) mmc_present(IF_MD(drive))
240 #endif
241 #define storage_driver_type(drive) (STORAGE_MMC_NUM)
242 #elif (CONFIG_STORAGE & STORAGE_NAND)
243 #define STORAGE_FUNCTION(NAME) (nand_## NAME)
244 #define storage_spindown(sec) nand_spindown(sec)
245 #define storage_spin() nand_spin()
246
247 #define storage_enable(on) (void)0
248 #define storage_sleepnow() nand_sleepnow()
249 #define storage_disk_is_active() 0
250 #define storage_soft_reset() (void)0
251 #ifdef HAVE_STORAGE_FLUSH
252 #define storage_flush() nand_flush()
253 #endif
254 #define storage_last_disk_activity() nand_last_disk_activity()
255 #define storage_spinup_time() 0
256 #define storage_get_identify() nand_get_identify()
257
258 #ifdef STORAGE_GET_INFO
259 #define storage_get_info(drive, info) nand_get_info(IF_MD(drive,) info)
260 #endif
261 #ifdef HAVE_HOTSWAP
262 #define storage_removable(drive) nand_removable(IF_MD(drive))
263 #define storage_present(drive) nand_present(IF_MD(drive))
264 #endif
265 #define storage_driver_type(drive) (STORAGE_NAND_NUM)
266 #elif (CONFIG_STORAGE & STORAGE_RAMDISK)
267 #define STORAGE_FUNCTION(NAME) (ramdisk_## NAME)
268 #define storage_spindown(sec) ramdisk_spindown(sec)
269 #define storage_spin() ramdisk_spin()
270
271 #define storage_enable(on) (void)0
272 #define storage_sleepnow() ramdisk_sleepnow()
273 #define storage_disk_is_active() 0
274 #define storage_soft_reset() (void)0
275 #ifdef HAVE_STORAGE_FLUSH
276 #define storage_flush() (void)0
277 #endif
278 #define storage_last_disk_activity() ramdisk_last_disk_activity()
279 #define storage_spinup_time() 0
280 #define storage_get_identify() ramdisk_get_identify()
281
282 #ifdef STORAGE_GET_INFO
283 #define storage_get_info(drive, info) ramdisk_get_info(IF_MD(drive,) info)
284 #endif
285 #ifdef HAVE_HOTSWAP
286 #define storage_removable(drive) ramdisk_removable(IF_MD(drive))
287 #define storage_present(drive) ramdisk_present(IF_MD(drive))
288 #endif
289 #define storage_driver_type(drive) (STORAGE_RAMDISK_NUM)
290 #elif (CONFIG_STORAGE & STORAGE_USB)
291 // TODO: Eventually fix me
292 #else
293 //#error No storage driver!
294 #endif
295#else /* CONFIG_STORAGE_MULTI || !HAVE_HOSTFS */
296
297/* Multi-driver use normal functions */
298#define STORAGE_FUNCTION(NAME) (storage_## NAME)
299
300void storage_enable(bool on);
301void storage_sleepnow(void);
302bool storage_disk_is_active(void);
303int storage_soft_reset(void);
304int storage_flush(void);
305void storage_spin(void);
306void storage_spindown(int seconds);
307long storage_last_disk_activity(void);
308int storage_spinup_time(void);
309int storage_num_drives(void);
310#ifdef STORAGE_GET_INFO
311void storage_get_info(int drive, struct storage_info *info);
312#endif
313#ifdef HAVE_HOTSWAP
314bool storage_removable(int drive);
315bool storage_present(int drive);
316#else
317#define storage_removable(x) 0
318#define storage_present(x) 1
319#endif
320int storage_driver_type(int drive);
321
322#endif /* NOT CONFIG_STORAGE_MULTI and NOT SIMULATOR*/
323
324int storage_read_sectors(IF_MD(int drive,) sector_t start, int count, void* buf);
325int storage_write_sectors(IF_MD(int drive,) sector_t start, int count, const void* buf);
326#endif