regression: test serial port r/w.
[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/semaphore.h>
12 #include <lunaix/types.h>
13
14 /**
15  * @brief Export a device definition
16  *
17  */
18 #define EXPORT_DEVICE(id, devdef, load_order)                                  \
19     export_ldga_el(devdefs, id, ptr_t, devdef);                                \
20     export_ldga_el_sfx(devdefs, id##_ldorder, ptr_t, devdef, load_order);
21
22 #define load_on_demand ld_ondemand
23
24 /**
25  * @brief Mark the device definition should be loaded automatically as earlier
26  * as possible in the kernel bootstrapping stage (before initialization of file
27  * systems). Load here if your driver is standalone and require no other than
28  * basic memory allocation services
29  *
30  */
31 #define load_earlystage ld_early
32
33 /**
34  * @brief Mark the device definition should be loaded automatically after timer
35  * is ready. Load here if your driver require a basic timing service
36  *
37  */
38 #define load_timerstage ld_aftertimer
39
40 /**
41  * @brief Mark the device definition should be loaded automatically in
42  * the post boostrapping stage (i.e., the start up of proc0). Load here if your
43  * driver involves async mechanism
44  *
45  */
46 #define load_poststage ld_post
47
48 /**
49  * @brief Declare a device class
50  *
51  */
52 #define DEVCLASS(devif, devfn, devkind, devvar)                                \
53     (struct devclass)                                                          \
54     {                                                                          \
55         .meta = DEV_META(devif, devfn), .device = (devkind),                   \
56         .variant = (devvar)                                                    \
57     }
58
59 #define DEV_STRUCT_MAGIC 0x5645444c
60
61 #define DEV_MSKIF 0x00000003
62
63 #define DEV_IFVOL 0x0 // volumetric (block) device
64 #define DEV_IFSEQ 0x1 // sequential (character) device
65 #define DEV_IFCAT 0x2 // a device category (as device groupping)
66 #define DEV_IFSYS 0x3 // a system device
67
68 typedef unsigned int dev_t;
69
70 struct devclass
71 {
72     u32_t meta;
73     u32_t device;
74     u32_t variant;
75     u32_t hash;
76 };
77
78 struct device
79 {
80     u32_t magic;
81     struct llist_header siblings;
82     struct llist_header children;
83     struct device* parent;
84     // TODO investigate event polling
85
86     struct hstr name;
87     struct devclass class;
88     dev_t dev_id;
89     int dev_type;
90     char name_val[DEVICE_NAME_SIZE];
91     void* underlay;
92
93     struct
94     {
95         // TODO Think about where will they fit.
96         int (*acquire)(struct device* dev);
97         int (*release)(struct device* dev);
98
99         int (*read)(struct device* dev, void* buf, size_t offset, size_t len);
100         int (*write)(struct device* dev, void* buf, size_t offset, size_t len);
101         int (*read_page)(struct device* dev, void* buf, size_t offset);
102         int (*write_page)(struct device* dev, void* buf, size_t offset);
103         int (*exec_cmd)(struct device* dev, u32_t req, va_list args);
104     } ops;
105 };
106
107 struct device_def
108 {
109     struct llist_header dev_list;
110     struct hlist_node hlist;
111     struct hlist_node hlist_if;
112     char* name;
113
114     struct devclass class;
115
116     int (*init)(struct device_def*);
117     int (*init_for)(struct device_def*, struct device*);
118 };
119
120 static inline u32_t devclass_hash(struct devclass class)
121 {
122     return (((class.device & 0xffff) << 16) | (class.variant & 0xffff)) ^
123            ~class.meta;
124 }
125
126 void
127 device_register_all();
128
129 void
130 device_prepare(struct device* dev);
131
132 void
133 device_setname(struct device* dev, char* fmt, ...);
134
135 void
136 device_setname(struct device* dev, char* fmt, ...);
137
138 struct device*
139 device_add_vargs(struct device* parent,
140                  void* underlay,
141                  char* name_fmt,
142                  u32_t type,
143                  va_list args);
144
145 struct device*
146 device_add(struct device* parent,
147            void* underlay,
148            u32_t type,
149            char* name_fmt,
150            ...);
151
152 struct device*
153 device_addsys(struct device* parent, void* underlay, char* name_fmt, ...);
154
155 struct device*
156 device_addseq(struct device* parent, void* underlay, char* name_fmt, ...);
157
158 struct device*
159 device_addvol(struct device* parent, void* underlay, char* name_fmt, ...);
160
161 struct device*
162 device_addcat(struct device* parent, char* name_fmt, ...);
163
164 void
165 device_remove(struct device* dev);
166
167 struct device*
168 device_getbyid(struct llist_header* devlist, dev_t id);
169
170 struct device*
171 device_getbyhname(struct device* root_dev, struct hstr* name);
172
173 struct device*
174 device_getbyname(struct device* root_dev, const char* name, size_t len);
175
176 struct device*
177 device_getbyoffset(struct device* root_dev, int pos);
178
179 struct device*
180 device_create_byclass(struct devclass* class,
181                       u32_t type,
182                       char* name,
183                       int* err_code);
184
185 struct hbucket*
186 device_definitions_byif(int if_type);
187
188 struct device_def*
189 devdef_byclass(struct devclass* class);
190
191 void
192 device_register_all();
193
194 /*------ Load hooks ------*/
195
196 void
197 device_earlystage();
198
199 void
200 device_poststage();
201
202 void
203 device_timerstage();
204
205 #endif /* __LUNAIX_DEVICE_H */