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;
29 devdef->name = "<unspecified>";
32 hashtable_hash_in(dev_registry, &devdef->hlist, hash);
34 dev_byif, &devdef->hlist_if, DEV_IF(devdef->class.meta));
36 llist_append(&dev_registry_flat, &devdef->dev_list);
41 devclass_eq(struct devclass* c1, struct devclass* c2)
43 return c1->meta == c2->meta && c1->variant == c2->variant &&
44 c1->device == c2->device;
48 devdef_byclass(struct devclass* class)
50 u32_t hash = devclass_hash(*class);
53 struct device_def *pos, *n;
54 hashtable_hash_foreach(dev_registry, hash, pos, n, hlist)
56 if (pos->class.hash != hash) {
59 if (devclass_eq(class, &pos->class)) {
68 device_create_byclass(struct devclass* class,
74 struct device_def* devdef = devdef_byclass(class);
81 if (!devdef->init_for) {
88 struct device* dev = device_add(adhoc_devcat, class, NULL, type, NULL);
90 errno = devdef->init_for(devdef, dev);
91 if (err_code && !errno) {
109 device_definitions_byif(int if_type)
111 return &dev_byif[__hashkey(dev_byif, if_type)];
114 #define device_load_on_stage(stage) \
117 struct device_def* devdef; \
118 ldga_foreach(dev_ld_##stage, struct device_def*, idx, devdef) \
120 devdef->init(devdef); \
127 device_load_on_stage(early);
133 device_load_on_stage(aftertimer);
139 device_load_on_stage(post);
143 __devdb_db_gonext(struct twimap* mapping)
145 struct device_def* current = twimap_index(mapping, struct device_def*);
146 if (current->dev_list.next == &dev_registry_flat) {
150 list_entry(current->dev_list.next, struct device_def, dev_list);
155 __devdb_twifs_lsdb(struct twimap* mapping)
158 struct device_def* def = twimap_index(mapping, struct device_def*);
160 int meta = def->class.meta;
161 ksnprintf(flags, 64, "if=%x,fn=%x", DEV_IF(meta), DEV_FN(meta));
163 twimap_printf(mapping,
164 "%xh:%d:%d \"%s\" %s\n",
173 __devdb_reset(struct twimap* map)
176 container_of(dev_registry_flat.next, struct device_def, dev_list);
182 struct twimap* map = twifs_mapping(NULL, NULL, "devtab");
183 map->reset = __devdb_reset;
184 map->read = __devdb_twifs_lsdb;
185 map->go_next = __devdb_db_gonext;
187 EXPORT_TWIFS_PLUGIN(devdb, devdb_twifs_plugin);