1 #include <lunaix/device.h>
2 #include <lunaix/fs/twifs.h>
3 #include <lunaix/status.h>
5 #include <klibc/stdio.h>
7 static DECLARE_HASHTABLE(dev_registry, 32);
8 static DECLARE_HASHTABLE(dev_byif, 8);
9 static DEFINE_LLIST(dev_registry_flat);
11 static struct device* adhoc_devcat;
16 adhoc_devcat = device_addcat(NULL, "adhoc");
18 hashtable_init(dev_registry);
19 hashtable_init(dev_byif);
22 struct device_def* devdef;
23 ldga_foreach(devdefs, struct device_def*, idx, devdef)
25 u32_t hash = devclass_hash(devdef->class);
26 devdef->class.hash = hash;
28 hashtable_hash_in(dev_registry, &devdef->hlist, hash);
30 dev_byif, &devdef->hlist_if, DEV_IF(devdef->class.meta));
32 llist_append(&dev_registry_flat, &devdef->dev_list);
37 devclass_eq(struct devclass* c1, struct devclass* c2)
39 return c1->meta == c2->meta && c1->variant == c2->variant &&
40 c1->device == c2->device;
44 devdef_byclass(struct devclass* class)
46 u32_t hash = devclass_hash(*class);
49 struct device_def *pos, *n;
50 hashtable_hash_foreach(dev_registry, hash, pos, n, hlist)
52 if (pos->class.hash != hash) {
55 if (devclass_eq(class, &pos->class)) {
64 device_create_byclass(struct devclass* class,
70 struct device_def* devdef = devdef_byclass(class);
77 if (!devdef->init_for) {
84 struct device* dev = device_add(adhoc_devcat, NULL, type, NULL);
86 errno = devdef->init_for(devdef, dev);
87 if (err_code && !errno) {
105 device_definitions_byif(int if_type)
107 return &dev_byif[__hashkey(dev_byif, if_type)];
110 #define device_load_on_stage(stage) \
113 struct device_def* devdef; \
114 ldga_foreach(dev_ld_##stage, struct device_def*, idx, devdef) \
116 devdef->init(devdef); \
123 device_load_on_stage(early);
129 device_load_on_stage(aftertimer);
135 device_load_on_stage(post);
139 __devdb_db_gonext(struct twimap* mapping)
141 struct device_def* current = twimap_index(mapping, struct device_def*);
142 if (current->dev_list.next == &dev_registry_flat) {
146 list_entry(current->dev_list.next, struct device_def, dev_list);
151 __devdb_twifs_lsdb(struct twimap* mapping)
154 struct device_def* def = twimap_index(mapping, struct device_def*);
156 int meta = def->class.meta;
157 ksnprintf(flags, 32, "if=%x,fn=%x", DEV_IF(meta), DEV_FN(meta));
159 twimap_printf(mapping,
160 "%d:%d:%d %s (%s)\n",
171 struct twimap* map = twifs_mapping(NULL, NULL, "devtab");
172 map->read = __devdb_twifs_lsdb;
173 map->go_next = __devdb_db_gonext;
175 EXPORT_TWIFS_PLUGIN(devdb, devdb_twifs_plugin);