1844590dce24ace0263102ff95a7551e02060dfb
[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 0x5645444c
89
90 #define DEV_MSKIF 0x00000003
91
92 #define DEV_IFVOL 0x0 // volumetric (block) device
93 #define DEV_IFSEQ 0x1 // sequential (character) device
94 #define DEV_IFCAT 0x2 // a device category (as device groupping)
95 #define DEV_IFSYS 0x3 // a system device
96
97 struct device
98 {
99     /* -- device structing -- */
100
101     u32_t magic;
102     struct llist_header siblings;
103     struct llist_header children;
104     struct device* parent;
105
106     /* -- device state -- */
107
108     mutex_t lock;
109
110     struct hstr name;
111     struct devident ident;
112
113     u32_t dev_uid;
114     int dev_type;
115     char name_val[DEVICE_NAME_SIZE];
116     void* underlay;
117
118     /* -- polling -- */
119     int poll_evflags;
120     poll_evt_q pollers;
121
122     struct
123     {
124         // TODO Think about where will they fit.
125         int (*acquire)(struct device* dev);
126         int (*release)(struct device* dev);
127
128         int (*read)(struct device*, void*, off_t, size_t);
129         int (*write)(struct device*, void*, off_t, size_t);
130         int (*read_async)(struct device*, void*, off_t, size_t);
131         int (*write_async)(struct device*, void*, off_t, size_t);
132
133         int (*read_page)(struct device*, void*, off_t);
134         int (*write_page)(struct device*, void*, off_t);
135
136         int (*exec_cmd)(struct device*, u32_t, va_list);
137         int (*poll)(struct device*);
138     } ops;
139 };
140
141 struct device_def
142 {
143     struct llist_header dev_list;
144     struct hlist_node hlist;
145     struct hlist_node hlist_if;
146     char* name;
147
148     struct devclass class;
149
150     /**
151      * @brief Called when the driver is required to initialize itself.
152      *
153      */
154     int (*init)(struct device_def*);
155
156     /**
157      * @brief Called when the driver is required to bind with a device. This is
158      * the case for a real-hardware-oriented driver
159      *
160      */
161     int (*bind)(struct device_def*, struct device*);
162
163     /**
164      * @brief Called when a driver is requested to detach from the device and
165      * free up all it's resources
166      *
167      */
168     int (*free)(struct device_def*, void* instance);
169 };
170
171 #define mark_device_doing_write(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLOUT
172 #define mark_device_done_write(dev_ptr) (dev_ptr)->poll_evflags |= _POLLOUT
173
174 #define mark_device_doing_read(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLIN
175 #define mark_device_done_read(dev_ptr) (dev_ptr)->poll_evflags |= _POLLIN
176
177 #define mark_device_hanging(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLHUP
178 #define mark_device_grounded(dev_ptr) (dev_ptr)->poll_evflags |= _POLLHUP
179
180 static inline u32_t
181 device_id_from_class(struct devclass* class)
182 {
183     return ((class->device & 0xffff) << 16) | ((class->variant & 0xffff));
184 }
185
186 void
187 device_scan_drivers();
188
189 void
190 device_setname_vargs(struct device* dev, char* fmt, va_list args);
191
192 void
193 device_setname(struct device* dev, char* fmt, ...);
194
195 void
196 device_register(struct device* dev, struct devclass* class, char* fmt, ...);
197
198 void
199 device_create(struct device* dev,
200               struct device* parent,
201               u32_t type,
202               void* underlay);
203
204 struct device*
205 device_alloc(struct device* parent, u32_t type, void* underlay);
206
207 static inline struct device*
208 device_allocsys(struct device* parent, void* underlay)
209 {
210     return device_alloc(parent, DEV_IFSYS, underlay);
211 }
212
213 static inline struct device*
214 device_allocseq(struct device* parent, void* underlay)
215 {
216     return device_alloc(parent, DEV_IFSEQ, underlay);
217 }
218
219 static inline struct device*
220 device_allocvol(struct device* parent, void* underlay)
221 {
222     return device_alloc(parent, DEV_IFVOL, underlay);
223 }
224
225 struct device*
226 device_addcat(struct device* parent, char* name_fmt, ...);
227
228 void
229 device_remove(struct device* dev);
230
231 struct device*
232 device_getbyid(struct llist_header* devlist, u32_t id);
233
234 struct device*
235 device_getbyhname(struct device* root_dev, struct hstr* name);
236
237 struct device*
238 device_getbyname(struct device* root_dev, const char* name, size_t len);
239
240 struct device*
241 device_getbyoffset(struct device* root_dev, int pos);
242
243 struct device*
244 device_cast(void* obj);
245
246 struct hbucket*
247 device_definitions_byif(int if_type);
248
249 struct device_def*
250 devdef_byident(struct devident* class);
251
252 void
253 device_populate_info(struct device* dev, struct dev_info* devinfo);
254
255 void
256 device_scan_drivers();
257
258 /*------ Load hooks ------*/
259
260 void
261 device_onbooot_load();
262
263 void
264 device_postboot_load();
265
266 void
267 device_sysconf_load();
268
269 static inline void
270 device_lock(struct device* dev)
271 {
272     mutex_lock(&dev->lock);
273 }
274
275 static inline void
276 device_unlock(struct device* dev)
277 {
278     mutex_unlock(&dev->lock);
279 }
280
281 static inline int
282 device_locked(struct device* dev)
283 {
284     return mutex_on_hold(&dev->lock);
285 }
286
287 #define devprintf_expand(devident) (devident)->fn_grp, (devident)->unique
288
289 #endif /* __LUNAIX_DEVICE_H */