renamed and cleaned up export header files to match linux convention
[lunaix-os.git] / lunaix-os / kernel / device / devdb.c
1 #include <lunaix/device.h>
2 #include <lunaix/fs/twifs.h>
3 #include <lunaix/status.h>
4 #include <lunaix/syslog.h>
5
6 #include <klibc/hash.h>
7
8 #include <klibc/strfmt.h>
9
10 LOG_MODULE("devdb")
11
12 static DECLARE_HASHTABLE(dev_registry, 32);
13 static DECLARE_HASHTABLE(dev_byif, 8);
14 static DEFINE_LLIST(dev_registry_flat);
15
16 static struct device_cat* adhoc_devcat;
17
18 static inline u32_t
19 hash_dev(u32_t fngrp, u32_t dev)
20 {
21     return (hash_32(fngrp, 16) << 16) | (hash_32(dev, 16));
22 }
23
24 void
25 device_scan_drivers()
26 {
27     adhoc_devcat = device_addcat(NULL, "adhoc");
28
29     hashtable_init(dev_registry);
30     hashtable_init(dev_byif);
31
32     int idx = 0, errno;
33     struct device_def* devdef;
34     ldga_foreach(devdefs, struct device_def*, idx, devdef)
35     {
36         struct devclass* devc = &devdef->class;
37         u32_t hash = hash_dev(devc->fn_grp, devc->device);
38         devdef->class_hash = hash;
39
40         if (!devdef->name) {
41             devdef->name = "<unspecified>";
42         }
43
44         errno = 0;
45         if (devdef->ad_tabulam) {
46             errno = devdef->ad_tabulam(devdef);
47         }
48
49         if (errno) {
50             ERROR("driver unable to register %xh:%xh.%d (err=%d)",
51                     devdef->class.fn_grp, 
52                     devdef->class.device,
53                     devdef->class.variant, errno);
54             continue;
55         }
56
57         hashtable_hash_in(dev_registry, &devdef->hlist, hash);
58         hashtable_hash_in(dev_byif, &devdef->hlist_if, DEV_VN(devc->fn_grp));
59
60         llist_append(&dev_registry_flat, &devdef->dev_list);
61     }
62
63     INFO("%d drivers registered", idx + 1);
64 }
65
66 static int
67 devclass_eq(struct devclass* c1, struct devclass* c2)
68 {
69     return c1->fn_grp == c2->fn_grp && c1->device == c2->device;
70 }
71
72 struct device_def*
73 devdef_byclass(struct devclass* devc)
74 {
75     u32_t hash = hash_dev(devc->fn_grp, devc->device);
76     int errno;
77
78     struct device_def *pos, *n;
79     hashtable_hash_foreach(dev_registry, hash, pos, n, hlist)
80     {
81         if (pos->class_hash != hash) {
82             continue;
83         }
84         if (devclass_eq(devc, &pos->class)) {
85             break;
86         }
87     }
88
89     return pos;
90 }
91
92 struct device_def*
93 devdef_byident(struct devident* ident)
94 {
95     struct devclass derived = { .device = DEV_KIND_FROM(ident->unique),
96                                 .fn_grp = ident->fn_grp };
97     return devdef_byclass(&derived);
98 }
99
100 struct hbucket*
101 device_definitions_byif(int if_type)
102 {
103     return &dev_byif[__hashkey(dev_byif, if_type)];
104 }
105
106 #define __device_load_on_stage(stage)                                          \
107     ({                                                                         \
108         int idx = 0;                                                           \
109         struct device_def* devdef;                                             \
110         ldga_foreach(dev_##stage, struct device_def*, idx, devdef)             \
111         {                                                                      \
112             device_chain_load_once(devdef);                                    \
113         }                                                                      \
114     })
115 #define device_load_on_stage(stage) __device_load_on_stage(stage)
116
117 void
118 device_onboot_load()
119 {
120     device_load_on_stage(load_onboot);
121 }
122
123 void
124 device_postboot_load()
125 {
126     device_load_on_stage(load_postboot);
127 }
128
129 void
130 device_sysconf_load()
131 {
132     device_load_on_stage(load_sysconf);
133 }
134
135 static int
136 __twimap_gonext_devtab(struct twimap* mapping)
137 {
138     struct device_def* current = twimap_index(mapping, struct device_def*);
139     if (current->dev_list.next == &dev_registry_flat) {
140         return 0;
141     }
142     mapping->index =
143       list_entry(current->dev_list.next, struct device_def, dev_list);
144     return 1;
145 }
146
147 static void
148 __twimap_read_devtab(struct twimap* mapping)
149 {
150     char flags[64];
151     struct device_def* def = twimap_index(mapping, struct device_def*);
152
153     int meta = def->class.fn_grp;
154     ksnprintf(flags, 64, "vn=%x, fn=%x", DEV_VN(meta), DEV_FN(meta));
155
156     twimap_printf(mapping,
157                   "%08xh:%04d \"%s\" %s\n",
158                   def->class.fn_grp,
159                   def->class.device,
160                   def->name,
161                   flags);
162 }
163
164 static void
165 __twimap_reset_devtab(struct twimap* map)
166 {
167     map->index =
168       container_of(dev_registry_flat.next, struct device_def, dev_list);
169 }
170
171 static void
172 devdb_twifs_plugin()
173 {
174     twimap_export_list(NULL, devtab, FSACL_aR, NULL);
175 }
176 EXPORT_TWIFS_PLUGIN(devdb, devdb_twifs_plugin);