12
#include <drivers/block_dev.h>
13
#include <drivers/flash/flash.h>
15
#include <mem/misc/pool.h>
17
#include <lib/libds/indexator.h>
19
#include <framework/mod/options.h>
21
#define MAX_FLASHDEV_QUANTITY OPTION_GET(NUMBER, dev_quantity)
23
POOL_DEF(flash_pool, struct flash_dev, MAX_FLASHDEV_QUANTITY);
24
INDEX_DEF(flash_idx, 0, MAX_FLASHDEV_QUANTITY);
26
static struct flash_dev *flashdev_tab[MAX_FLASHDEV_QUANTITY];
28
struct flash_dev *flash_alloc(void) {
29
struct flash_dev *flash;
32
if (NULL == (flash = pool_alloc(&flash_pool))) {
33
return err2ptr(ENOMEM);
36
idx = index_alloc(&flash_idx, INDEX_MIN);
37
if (idx == INDEX_NONE) {
38
pool_free(&flash_pool, flash);
39
return err2ptr(ENOMEM);
42
memset(flash, 0, sizeof(*flash));
45
flashdev_tab[idx] = flash;
50
int flash_free(struct flash_dev *dev) {
53
index_free(&flash_idx, dev->idx);
54
flashdev_tab[dev->idx] = 0;
55
pool_free(&flash_pool, dev);
60
static int flash_initialized = 0;
61
struct flash_dev *flash_by_id(int idx) {
62
if (idx < 0 || idx >= MAX_FLASHDEV_QUANTITY) {
66
if (!flash_initialized) {
70
return flashdev_tab[idx];
73
int flash_max_id(void) {
74
return MAX_FLASHDEV_QUANTITY;
77
ARRAY_SPREAD_DEF(const struct flash_dev_module, __flash_dev_registry);
78
int flash_devs_init(void) {
80
const struct flash_dev_module *fdev_module;
82
if (flash_initialized) {
86
flash_initialized = 1;
89
array_spread_foreach_ptr(fdev_module, __flash_dev_registry) {
90
if (fdev_module->dev_drv->flash_init != NULL) {
91
ret = fdev_module->dev_drv->flash_init(NULL, NULL);