7-ps2_keyboard.md and 8-multitasking.md (#29)
[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 capability_meta
104 {
105     struct llist_header caps;
106     unsigned int cap_type;
107 };
108
109 #define CAPABILITY_META                     \
110     union {                                 \
111         struct capability_meta cap_meta;    \
112         struct {                            \
113             struct llist_header caps;       \
114             unsigned int cap_type;          \
115         };                                  \
116     }
117
118 #define get_capability(cap, cap_type)       \
119     container_of((cap), cap_type, cap_meta)
120 #define cap_meta(cap) (&(cap)->cap_meta)
121
122 typedef struct llist_header capability_list_t;
123
124
125 struct device_meta
126 {
127     u32_t magic;
128     struct llist_header siblings;
129     struct llist_header children;
130     struct device_meta* parent;
131     struct hstr name;
132     
133     u32_t dev_uid;
134     
135     char name_val[DEVICE_NAME_SIZE];
136 };
137
138 #define DEVICE_METADATA                             \
139     union {                                         \
140         struct device_meta meta;                    \
141         struct {                                    \
142             u32_t magic;                            \
143             struct llist_header siblings;           \
144             struct llist_header children;           \
145             struct device_meta* parent;             \
146             struct hstr name;                       \
147                                                     \
148             u32_t dev_uid;                          \
149                                                     \
150             char name_val[DEVICE_NAME_SIZE];        \
151         };                                          \
152     }                                              
153
154 #define dev_meta(dev) (&(dev)->meta)
155 #define to_dev(dev) (container_of(dev,struct device, meta))
156 #define to_catdev(dev) (container_of(dev,struct device_cat, meta))
157 #define to_aliasdev(dev) (container_of(dev,struct device_alias, meta))
158
159 struct device_alias {
160     DEVICE_METADATA;
161     struct device_meta* alias;
162 };
163
164 struct device_cat {
165     DEVICE_METADATA;
166 };
167
168 struct device
169 {
170     /* -- device structing -- */
171
172     DEVICE_METADATA;
173
174     capability_list_t capabilities;
175
176     /* -- device state -- */
177
178     mutex_t lock;
179
180     int dev_type;
181     struct devident ident;
182     void* underlay;
183
184     /* -- polling -- */
185     int poll_evflags;
186     poll_evt_q pollers;
187
188     struct
189     {
190         // TODO Think about where will they fit.
191         int (*acquire)(struct device* dev);
192         int (*release)(struct device* dev);
193
194         int (*read)(struct device*, void*, off_t, size_t);
195         int (*write)(struct device*, void*, off_t, size_t);
196         int (*read_async)(struct device*, void*, off_t, size_t);
197         int (*write_async)(struct device*, void*, off_t, size_t);
198
199         int (*read_page)(struct device*, void*, off_t);
200         int (*write_page)(struct device*, void*, off_t);
201
202         int (*exec_cmd)(struct device*, u32_t, va_list);
203         int (*poll)(struct device*);
204     } ops;
205 };
206
207 struct device_def
208 {
209     struct llist_header dev_list;
210     struct hlist_node hlist;
211     struct hlist_node hlist_if;
212     char* name;
213
214     struct devclass class;
215
216     /**
217      * @brief Called when the driver is required to initialize itself.
218      *
219      */
220     int (*init)(struct device_def*);
221
222     /**
223      * @brief Called when the driver is required to bind with a device. This is
224      * the case for a real-hardware-oriented driver
225      *
226      */
227     int (*bind)(struct device_def*, struct device*);
228
229     /**
230      * @brief Called when a driver is requested to detach from the device and
231      * free up all it's resources
232      *
233      */
234     int (*free)(struct device_def*, void* instance);
235 };
236
237 static inline bool must_inline
238 valid_device_ref(void* maybe_dev) {
239     if (!maybe_dev) 
240         return false;
241         
242     unsigned int magic = ((struct device_meta*)maybe_dev)->magic;
243     return (magic ^ DEV_STRUCT_MAGIC_MASK) <= 0xfU;
244 }
245
246 static inline bool must_inline
247 valid_device_subtype_ref(void* maybe_dev, unsigned int subtype) {
248     if (!maybe_dev) 
249         return false;
250     
251     unsigned int magic = ((struct device_meta*)maybe_dev)->magic;
252     return (magic ^ DEV_STRUCT_MAGIC_MASK) == subtype;
253 }
254
255 struct device*
256 resolve_device(void* maybe_dev);
257
258 struct device_meta*
259 resolve_device_meta(void* maybe_dev);
260
261 #define mark_device_doing_write(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLOUT
262 #define mark_device_done_write(dev_ptr) (dev_ptr)->poll_evflags |= _POLLOUT
263
264 #define mark_device_doing_read(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLIN
265 #define mark_device_done_read(dev_ptr) (dev_ptr)->poll_evflags |= _POLLIN
266
267 #define mark_device_hanging(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLHUP
268 #define mark_device_grounded(dev_ptr) (dev_ptr)->poll_evflags |= _POLLHUP
269
270 static inline u32_t
271 device_id_from_class(struct devclass* class)
272 {
273     return ((class->device & 0xffff) << 16) | ((class->variant & 0xffff));
274 }
275
276 void
277 device_scan_drivers();
278
279 void
280 device_setname_vargs(struct device_meta* dev, char* fmt, va_list args);
281
282 void
283 device_setname(struct device_meta* dev, char* fmt, ...);
284
285 void
286 device_register_generic(struct device_meta* dev, struct devclass* class, char* fmt, ...);
287
288 #define register_device(dev, class, fmt, ...) \
289             device_register_generic(dev_meta(dev), class, fmt, ## __VA_ARGS__)
290
291 void
292 device_create(struct device* dev,
293               struct device_meta* parent,
294               u32_t type,
295               void* underlay);
296
297 struct device*
298 device_alloc(struct device_meta* parent, u32_t type, void* underlay);
299
300 static inline struct device* must_inline
301 device_allocsys(struct device_meta* parent, void* underlay)
302 {
303     return device_alloc(parent, DEV_IFSYS, underlay);
304 }
305
306 static inline struct device* must_inline
307 device_allocseq(struct device_meta* parent, void* underlay)
308 {
309     return device_alloc(parent, DEV_IFSEQ, underlay);
310 }
311
312 static inline struct device* must_inline
313 device_allocvol(struct device_meta* parent, void* underlay)
314 {
315     return device_alloc(parent, DEV_IFVOL, underlay);
316 }
317
318 struct device_alias*
319 device_addalias(struct device_meta* parent, struct device_meta* aliased, char* name_fmt, ...);
320
321 struct device_cat*
322 device_addcat(struct device_meta* parent, char* name_fmt, ...);
323
324 void
325 device_remove(struct device_meta* dev);
326
327 struct device_meta*
328 device_getbyid(struct llist_header* devlist, u32_t id);
329
330 struct device_meta*
331 device_getbyhname(struct device_meta* root_dev, struct hstr* name);
332
333 struct device_meta*
334 device_getbyname(struct device_meta* root_dev, const char* name, size_t len);
335
336 struct device_meta*
337 device_getbyoffset(struct device_meta* root_dev, int pos);
338
339 struct hbucket*
340 device_definitions_byif(int if_type);
341
342 struct device_def*
343 devdef_byident(struct devident* class);
344
345 void
346 device_populate_info(struct device* dev, struct dev_info* devinfo);
347
348 void
349 device_scan_drivers();
350
351 /*------ Capability ------*/
352
353 struct capability_meta*
354 alloc_capability(int cap, unsigned int size);
355
356 #define new_capability(cap_type, cap_impl)\
357     ((cap_impl*)alloc_capability((cap_type), sizeof(cap_impl)))
358
359 #define new_capability_marker(cap_type)\
360     (alloc_capability((cap_type), sizeof(struct capability_meta)))
361
362 void
363 device_grant_capability(struct device* dev, struct capability_meta* cap);
364
365 struct capability_meta*
366 device_get_capability(struct device* dev, unsigned int cap_type);
367 /*------ Load hooks ------*/
368
369 void
370 device_onboot_load();
371
372 void
373 device_postboot_load();
374
375 void
376 device_sysconf_load();
377
378 static inline void
379 device_lock(struct device* dev)
380 {
381     mutex_lock(&dev->lock);
382 }
383
384 static inline void
385 device_unlock(struct device* dev)
386 {
387     mutex_unlock(&dev->lock);
388 }
389
390 static inline int
391 device_locked(struct device* dev)
392 {
393     return mutex_on_hold(&dev->lock);
394 }
395
396 #define devprintf_expand(devident) (devident)->fn_grp, (devident)->unique
397
398 #endif /* __LUNAIX_DEVICE_H */