1 #ifndef __LUNAIX_DEVICE_H
2 #define __LUNAIX_DEVICE_H
4 #define DEVICE_NAME_SIZE 32
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>
15 #include <usr/lunaix/device.h>
18 * @brief Export a device definition (i.e., device driver metadata)
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);
26 * @brief Mark the device definition can be loaded on demand, all other loading
27 * options are extended from this
29 #define load_on_demand ld_ondemand
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.
41 #define load_sysconf ld_sysconf
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
48 #define load_timedev ld_timedev
51 * @brief Mark the device definition should be loaded automatically during the
52 * bootstrapping stage. Most of the driver do load there.
55 #define load_onboot ld_kboot
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
64 #define load_postboot ld_post
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)
72 * @brief Declare a device class
75 #define DEVCLASS(devif, devfn, dev) \
78 .fn_grp = DEV_FNGRP(devif, devfn), .device = (dev), .variant = 0 \
81 #define DEVCLASSV(devif, devfn, dev, devvar) \
84 .fn_grp = DEV_FNGRP(devif, devfn), .device = (dev), \
88 #define DEV_STRUCT_MAGIC_MASK 0x56454440U
89 #define DEV_STRUCT 0xc
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)
97 #define DEV_MSKIF 0x00000003
99 #define DEV_IFVOL 0x0 // volumetric (block) device
100 #define DEV_IFSEQ 0x1 // sequential (character) device
101 #define DEV_IFSYS 0x3 // a system device
106 struct llist_header siblings;
107 struct llist_header children;
108 struct device_meta* parent;
113 char name_val[DEVICE_NAME_SIZE];
116 #define DEVICE_METADATA \
118 struct device_meta meta; \
121 struct llist_header siblings; \
122 struct llist_header children; \
123 struct device_meta* parent; \
128 char name_val[DEVICE_NAME_SIZE]; \
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))
137 struct device_alias {
139 struct device_meta* alias;
148 /* -- device structing -- */
152 /* -- device state -- */
157 struct devident ident;
166 // TODO Think about where will they fit.
167 int (*acquire)(struct device* dev);
168 int (*release)(struct device* dev);
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);
175 int (*read_page)(struct device*, void*, off_t);
176 int (*write_page)(struct device*, void*, off_t);
178 int (*exec_cmd)(struct device*, u32_t, va_list);
179 int (*poll)(struct device*);
185 struct llist_header dev_list;
186 struct hlist_node hlist;
187 struct hlist_node hlist_if;
190 struct devclass class;
193 * @brief Called when the driver is required to initialize itself.
196 int (*init)(struct device_def*);
199 * @brief Called when the driver is required to bind with a device. This is
200 * the case for a real-hardware-oriented driver
203 int (*bind)(struct device_def*, struct device*);
206 * @brief Called when a driver is requested to detach from the device and
207 * free up all it's resources
210 int (*free)(struct device_def*, void* instance);
213 static inline bool must_inline
214 valid_device_ref(void* maybe_dev) {
218 unsigned int magic = ((struct device_meta*)maybe_dev)->magic;
219 return (magic ^ DEV_STRUCT_MAGIC_MASK) <= 0xfU;
222 static inline bool must_inline
223 valid_device_subtype_ref(void* maybe_dev, unsigned int subtype) {
227 unsigned int magic = ((struct device_meta*)maybe_dev)->magic;
228 return (magic ^ DEV_STRUCT_MAGIC_MASK) == subtype;
232 resolve_device(void* maybe_dev);
235 resolve_device_meta(void* maybe_dev);
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
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
243 #define mark_device_hanging(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLHUP
244 #define mark_device_grounded(dev_ptr) (dev_ptr)->poll_evflags |= _POLLHUP
247 device_id_from_class(struct devclass* class)
249 return ((class->device & 0xffff) << 16) | ((class->variant & 0xffff));
253 device_scan_drivers();
256 device_setname_vargs(struct device_meta* dev, char* fmt, va_list args);
259 device_setname(struct device_meta* dev, char* fmt, ...);
262 device_register_generic(struct device_meta* dev, struct devclass* class, char* fmt, ...);
264 #define register_device(dev, class, fmt, ...) \
265 device_register_generic(dev_meta(dev), class, fmt, ## __VA_ARGS__)
268 device_create(struct device* dev,
269 struct device_meta* parent,
274 device_alloc(struct device_meta* parent, u32_t type, void* underlay);
276 static inline struct device* must_inline
277 device_allocsys(struct device_meta* parent, void* underlay)
279 return device_alloc(parent, DEV_IFSYS, underlay);
282 static inline struct device* must_inline
283 device_allocseq(struct device_meta* parent, void* underlay)
285 return device_alloc(parent, DEV_IFSEQ, underlay);
288 static inline struct device* must_inline
289 device_allocvol(struct device_meta* parent, void* underlay)
291 return device_alloc(parent, DEV_IFVOL, underlay);
295 device_addalias(struct device_meta* parent, struct device_meta* aliased, char* name_fmt, ...);
298 device_addcat(struct device_meta* parent, char* name_fmt, ...);
301 device_remove(struct device_meta* dev);
304 device_getbyid(struct llist_header* devlist, u32_t id);
307 device_getbyhname(struct device_meta* root_dev, struct hstr* name);
310 device_getbyname(struct device_meta* root_dev, const char* name, size_t len);
313 device_getbyoffset(struct device_meta* root_dev, int pos);
316 device_definitions_byif(int if_type);
319 devdef_byident(struct devident* class);
322 device_populate_info(struct device* dev, struct dev_info* devinfo);
325 device_scan_drivers();
327 /*------ Load hooks ------*/
330 device_onboot_load();
333 device_postboot_load();
336 device_sysconf_load();
339 device_lock(struct device* dev)
341 mutex_lock(&dev->lock);
345 device_unlock(struct device* dev)
347 mutex_unlock(&dev->lock);
351 device_locked(struct device* dev)
353 return mutex_on_hold(&dev->lock);
356 #define devprintf_expand(devident) (devident)->fn_grp, (devident)->unique
358 #endif /* __LUNAIX_DEVICE_H */