feat: owloysius - dynamic init function invocator
[lunaix-os.git] / lunaix-os / includes / lunaix / device.h
1 #ifndef __LUNAIX_DEVICE_H
2 #define __LUNAIX_DEVICE_H
3
4 #define DEVICE_NAME_SIZE 32
5
6 #include <lunaix/device_num.h>
7 #include <lunaix/ds/hashtable.h>
8 #include <lunaix/ds/hstr.h>
9 #include <lunaix/ds/ldga.h>
10 #include <lunaix/ds/llist.h>
11 #include <lunaix/ds/mutex.h>
12 #include <lunaix/iopoll.h>
13 #include <lunaix/types.h>
14
15 #include <usr/lunaix/device.h>
16
17 /**
18  * @brief Export a device definition (i.e., device driver metadata)
19  *
20  */
21 #define EXPORT_DEVICE(id, devdef, load_order)                                  \
22     export_ldga_el(devdefs, id, ptr_t, devdef);                                \
23     export_ldga_el_sfx(devdefs, id##_ldorder, ptr_t, devdef, load_order);
24
25 /**
26  * @brief Mark the device definition can be loaded on demand, all other loading
27  * options are extended from this
28  */
29 #define load_on_demand ld_ondemand
30
31 /**
32  * @brief Mark the device definition to be loaded as system configuration
33  * device. These kind of devices are defined to be the devices that talk to the
34  * system firmware to do config, or collecting crucial information about the
35  * system. For instances, ACPI, SoC components, and other **interconnection**
36  * buese (not USB!). Such device driver must only rely on basic memory
37  * management service, and must not try accessing subsystems other than the mm
38  * unit, for example, timer, interrupt, file-system, must not assumed exist.
39  *
40  */
41 #define load_sysconf ld_sysconf
42
43 /**
44  * @brief Mark the device definition should be loaded as time device, for
45  * example a real time clock device. Such device will be loaded and managed by
46  * clock subsystem
47  */
48 #define load_timedev ld_timedev
49
50 /**
51  * @brief Mark the device definition should be loaded automatically during the
52  * bootstrapping stage. Most of the driver do load there.
53  *
54  */
55 #define load_onboot ld_kboot
56
57 /**
58  * @brief Mark the device definition should be loaded automatically in
59  * the post boostrapping stage (i.e., the start up of proc0), where most of
60  * kernel sub-system are became ready to use. Do your load there if your driver
61  * depends on such condition
62  *
63  */
64 #define load_postboot ld_post
65
66 #define __foreach_exported_device_of(stage, index, pos)                        \
67     ldga_foreach(dev_##stage, struct device_def*, index, pos)
68 #define foreach_exported_device_of(stage, index, pos)                          \
69     __foreach_exported_device_of(stage, index, pos)
70
71 /**
72  * @brief Declare a device class
73  *
74  */
75 #define DEVCLASS(devif, devfn, dev)                                            \
76     (struct devclass)                                                          \
77     {                                                                          \
78         .fn_grp = DEV_FNGRP(devif, devfn), .device = (dev), .variant = 0       \
79     }
80
81 #define DEVCLASSV(devif, devfn, dev, devvar)                                   \
82     (struct devclass)                                                          \
83     {                                                                          \
84         .fn_grp = DEV_FNGRP(devif, devfn), .device = (dev),                    \
85         .variant = (devvar)                                                    \
86     }
87
88 #define DEV_STRUCT_MAGIC_MASK 0x56454440U
89 #define DEV_STRUCT 0xc
90 #define DEV_CAT 0xd
91 #define DEV_ALIAS 0xf
92
93 #define DEV_STRUCT_MAGIC (DEV_STRUCT_MAGIC_MASK | DEV_STRUCT)
94 #define DEV_CAT_MAGIC (DEV_STRUCT_MAGIC_MASK | DEV_CAT)
95 #define DEV_ALIAS_MAGIC (DEV_STRUCT_MAGIC_MASK | DEV_ALIAS)
96
97 #define DEV_MSKIF 0x00000003
98
99 #define DEV_IFVOL 0x0 // volumetric (block) device
100 #define DEV_IFSEQ 0x1 // sequential (character) device
101 #define DEV_IFSYS 0x3 // a system device
102
103 struct device_meta
104 {
105     u32_t magic;
106     struct llist_header siblings;
107     struct llist_header children;
108     struct device_meta* parent;
109     struct hstr name;
110     
111     u32_t dev_uid;
112     
113     char name_val[DEVICE_NAME_SIZE];
114 };
115
116 #define DEVICE_METADATA                             \
117     union {                                         \
118         struct device_meta meta;                    \
119         struct {                                    \
120             u32_t magic;                            \
121             struct llist_header siblings;           \
122             struct llist_header children;           \
123             struct device_meta* parent;             \
124             struct hstr name;                       \
125                                                     \
126             u32_t dev_uid;                          \
127                                                     \
128             char name_val[DEVICE_NAME_SIZE];        \
129         };                                          \
130     }                                              
131
132 #define dev_meta(dev) (&(dev)->meta)
133 #define to_dev(dev) (container_of(dev,struct device, meta))
134 #define to_catdev(dev) (container_of(dev,struct device_cat, meta))
135 #define to_aliasdev(dev) (container_of(dev,struct device_alias, meta))
136
137 struct device_alias {
138     DEVICE_METADATA;
139     struct device_meta* alias;
140 };
141
142 struct device_cat {
143     DEVICE_METADATA;
144 };
145
146 struct device
147 {
148     /* -- device structing -- */
149
150     DEVICE_METADATA;
151
152     /* -- device state -- */
153
154     mutex_t lock;
155
156     int dev_type;
157     struct devident ident;
158     void* underlay;
159
160     /* -- polling -- */
161     int poll_evflags;
162     poll_evt_q pollers;
163
164     struct
165     {
166         // TODO Think about where will they fit.
167         int (*acquire)(struct device* dev);
168         int (*release)(struct device* dev);
169
170         int (*read)(struct device*, void*, off_t, size_t);
171         int (*write)(struct device*, void*, off_t, size_t);
172         int (*read_async)(struct device*, void*, off_t, size_t);
173         int (*write_async)(struct device*, void*, off_t, size_t);
174
175         int (*read_page)(struct device*, void*, off_t);
176         int (*write_page)(struct device*, void*, off_t);
177
178         int (*exec_cmd)(struct device*, u32_t, va_list);
179         int (*poll)(struct device*);
180     } ops;
181 };
182
183 struct device_def
184 {
185     struct llist_header dev_list;
186     struct hlist_node hlist;
187     struct hlist_node hlist_if;
188     char* name;
189
190     struct devclass class;
191
192     /**
193      * @brief Called when the driver is required to initialize itself.
194      *
195      */
196     int (*init)(struct device_def*);
197
198     /**
199      * @brief Called when the driver is required to bind with a device. This is
200      * the case for a real-hardware-oriented driver
201      *
202      */
203     int (*bind)(struct device_def*, struct device*);
204
205     /**
206      * @brief Called when a driver is requested to detach from the device and
207      * free up all it's resources
208      *
209      */
210     int (*free)(struct device_def*, void* instance);
211 };
212
213 static inline bool must_inline
214 valid_device_ref(void* maybe_dev) {
215     if (!maybe_dev) 
216         return false;
217         
218     unsigned int magic = ((struct device_meta*)maybe_dev)->magic;
219     return (magic ^ DEV_STRUCT_MAGIC_MASK) <= 0xfU;
220 }
221
222 static inline bool must_inline
223 valid_device_subtype_ref(void* maybe_dev, unsigned int subtype) {
224     if (!maybe_dev) 
225         return false;
226     
227     unsigned int magic = ((struct device_meta*)maybe_dev)->magic;
228     return (magic ^ DEV_STRUCT_MAGIC_MASK) == subtype;
229 }
230
231 struct device*
232 resolve_device(void* maybe_dev);
233
234 struct device_meta*
235 resolve_device_meta(void* maybe_dev);
236
237 #define mark_device_doing_write(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLOUT
238 #define mark_device_done_write(dev_ptr) (dev_ptr)->poll_evflags |= _POLLOUT
239
240 #define mark_device_doing_read(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLIN
241 #define mark_device_done_read(dev_ptr) (dev_ptr)->poll_evflags |= _POLLIN
242
243 #define mark_device_hanging(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLHUP
244 #define mark_device_grounded(dev_ptr) (dev_ptr)->poll_evflags |= _POLLHUP
245
246 static inline u32_t
247 device_id_from_class(struct devclass* class)
248 {
249     return ((class->device & 0xffff) << 16) | ((class->variant & 0xffff));
250 }
251
252 void
253 device_scan_drivers();
254
255 void
256 device_setname_vargs(struct device_meta* dev, char* fmt, va_list args);
257
258 void
259 device_setname(struct device_meta* dev, char* fmt, ...);
260
261 void
262 device_register_generic(struct device_meta* dev, struct devclass* class, char* fmt, ...);
263
264 #define register_device(dev, class, fmt, ...) \
265             device_register_generic(dev_meta(dev), class, fmt, ## __VA_ARGS__)
266
267 void
268 device_create(struct device* dev,
269               struct device_meta* parent,
270               u32_t type,
271               void* underlay);
272
273 struct device*
274 device_alloc(struct device_meta* parent, u32_t type, void* underlay);
275
276 static inline struct device* must_inline
277 device_allocsys(struct device_meta* parent, void* underlay)
278 {
279     return device_alloc(parent, DEV_IFSYS, underlay);
280 }
281
282 static inline struct device* must_inline
283 device_allocseq(struct device_meta* parent, void* underlay)
284 {
285     return device_alloc(parent, DEV_IFSEQ, underlay);
286 }
287
288 static inline struct device* must_inline
289 device_allocvol(struct device_meta* parent, void* underlay)
290 {
291     return device_alloc(parent, DEV_IFVOL, underlay);
292 }
293
294 struct device_alias*
295 device_addalias(struct device_meta* parent, struct device_meta* aliased, char* name_fmt, ...);
296
297 struct device_cat*
298 device_addcat(struct device_meta* parent, char* name_fmt, ...);
299
300 void
301 device_remove(struct device_meta* dev);
302
303 struct device_meta*
304 device_getbyid(struct llist_header* devlist, u32_t id);
305
306 struct device_meta*
307 device_getbyhname(struct device_meta* root_dev, struct hstr* name);
308
309 struct device_meta*
310 device_getbyname(struct device_meta* root_dev, const char* name, size_t len);
311
312 struct device_meta*
313 device_getbyoffset(struct device_meta* root_dev, int pos);
314
315 struct hbucket*
316 device_definitions_byif(int if_type);
317
318 struct device_def*
319 devdef_byident(struct devident* class);
320
321 void
322 device_populate_info(struct device* dev, struct dev_info* devinfo);
323
324 void
325 device_scan_drivers();
326
327 /*------ Load hooks ------*/
328
329 void
330 device_onboot_load();
331
332 void
333 device_postboot_load();
334
335 void
336 device_sysconf_load();
337
338 static inline void
339 device_lock(struct device* dev)
340 {
341     mutex_lock(&dev->lock);
342 }
343
344 static inline void
345 device_unlock(struct device* dev)
346 {
347     mutex_unlock(&dev->lock);
348 }
349
350 static inline int
351 device_locked(struct device* dev)
352 {
353     return mutex_on_hold(&dev->lock);
354 }
355
356 #define devprintf_expand(devident) (devident)->fn_grp, (devident)->unique
357
358 #endif /* __LUNAIX_DEVICE_H */