feat: better rtc framework which aims to remove single rtc restrictions.
[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/ds/hstr.h>
7 #include <lunaix/ds/ldga.h>
8 #include <lunaix/ds/llist.h>
9 #include <lunaix/ds/semaphore.h>
10 #include <lunaix/types.h>
11
12 /**
13  * @brief Export pseudo device
14  *
15  */
16 #define EXPORT_PSEUDODEV(id, init_fn)                                          \
17     export_ldga_el(pseudo_dev, id, ptr_t, init_fn)
18
19 #define DEV_STRUCT_MAGIC 0x5645444c
20
21 #define DEV_MSKIF 0x00000003
22
23 #define DEV_IFVOL 0x0 // volumetric (block) device
24 #define DEV_IFSEQ 0x1 // sequential (character) device
25 #define DEV_IFCAT 0x2 // a device category (as device groupping)
26 #define DEV_IFSYS 0x3 // a system device
27
28 typedef unsigned int dev_t;
29
30 struct device
31 {
32     u32_t magic;
33     struct llist_header siblings;
34     struct llist_header children;
35     struct device* parent;
36     // TODO investigate event polling
37
38     struct hstr name;
39     dev_t dev_id;
40     int dev_type;
41     char name_val[DEVICE_NAME_SIZE];
42     void* underlay;
43
44     struct
45     {
46         int (*read)(struct device* dev, void* buf, size_t offset, size_t len);
47         int (*write)(struct device* dev, void* buf, size_t offset, size_t len);
48         int (*read_page)(struct device* dev, void* buf, size_t offset);
49         int (*write_page)(struct device* dev, void* buf, size_t offset);
50         int (*exec_cmd)(struct device* dev, u32_t req, va_list args);
51     } ops;
52 };
53
54 struct device*
55 device_add(struct device* parent,
56            void* underlay,
57            char* name_fmt,
58            u32_t type,
59            va_list args);
60
61 struct device*
62 device_addsys(struct device* parent, void* underlay, char* name_fmt, ...);
63
64 struct device*
65 device_addseq(struct device* parent, void* underlay, char* name_fmt, ...);
66
67 struct device*
68 device_addvol(struct device* parent, void* underlay, char* name_fmt, ...);
69
70 struct device*
71 device_addcat(struct device* parent, char* name_fmt, ...);
72
73 void
74 device_remove(struct device* dev);
75
76 struct device*
77 device_getbyid(struct llist_header* devlist, dev_t id);
78
79 struct device*
80 device_getbyhname(struct device* root_dev, struct hstr* name);
81
82 struct device*
83 device_getbyname(struct device* root_dev, const char* name, size_t len);
84
85 struct device*
86 device_getbyoffset(struct device* root_dev, int pos);
87
88 void
89 device_install_pseudo();
90
91 #endif /* __LUNAIX_DEVICE_H */