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
103 struct capability_meta
105 struct llist_header caps;
106 unsigned int cap_type;
109 #define CAPABILITY_META \
111 struct capability_meta cap_meta; \
113 struct llist_header caps; \
114 unsigned int cap_type; \
118 #define get_capability(cap, cap_type) \
119 container_of((cap), cap_type, cap_meta)
120 #define cap_meta(cap) (&(cap)->cap_meta)
122 typedef struct llist_header capability_list_t;
128 struct llist_header siblings;
129 struct llist_header children;
130 struct device_meta* parent;
135 char name_val[DEVICE_NAME_SIZE];
138 #define DEVICE_METADATA \
140 struct device_meta meta; \
143 struct llist_header siblings; \
144 struct llist_header children; \
145 struct device_meta* parent; \
150 char name_val[DEVICE_NAME_SIZE]; \
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))
159 struct device_alias {
161 struct device_meta* alias;
170 /* -- device structing -- */
174 capability_list_t capabilities;
176 /* -- device state -- */
181 struct devident ident;
190 // TODO Think about where will they fit.
191 int (*acquire)(struct device* dev);
192 int (*release)(struct device* dev);
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);
199 int (*read_page)(struct device*, void*, off_t);
200 int (*write_page)(struct device*, void*, off_t);
202 int (*exec_cmd)(struct device*, u32_t, va_list);
203 int (*poll)(struct device*);
209 struct llist_header dev_list;
210 struct hlist_node hlist;
211 struct hlist_node hlist_if;
214 struct devclass class;
217 * @brief Called when the driver is required to initialize itself.
220 int (*init)(struct device_def*);
223 * @brief Called when the driver is required to bind with a device. This is
224 * the case for a real-hardware-oriented driver
227 int (*bind)(struct device_def*, struct device*);
230 * @brief Called when a driver is requested to detach from the device and
231 * free up all it's resources
234 int (*free)(struct device_def*, void* instance);
237 static inline bool must_inline
238 valid_device_ref(void* maybe_dev) {
242 unsigned int magic = ((struct device_meta*)maybe_dev)->magic;
243 return (magic ^ DEV_STRUCT_MAGIC_MASK) <= 0xfU;
246 static inline bool must_inline
247 valid_device_subtype_ref(void* maybe_dev, unsigned int subtype) {
251 unsigned int magic = ((struct device_meta*)maybe_dev)->magic;
252 return (magic ^ DEV_STRUCT_MAGIC_MASK) == subtype;
256 resolve_device(void* maybe_dev);
259 resolve_device_meta(void* maybe_dev);
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
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
267 #define mark_device_hanging(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLHUP
268 #define mark_device_grounded(dev_ptr) (dev_ptr)->poll_evflags |= _POLLHUP
271 device_id_from_class(struct devclass* class)
273 return ((class->device & 0xffff) << 16) | ((class->variant & 0xffff));
277 device_scan_drivers();
280 device_setname_vargs(struct device_meta* dev, char* fmt, va_list args);
283 device_setname(struct device_meta* dev, char* fmt, ...);
286 device_register_generic(struct device_meta* dev, struct devclass* class, char* fmt, ...);
288 #define register_device(dev, class, fmt, ...) \
289 device_register_generic(dev_meta(dev), class, fmt, ## __VA_ARGS__)
292 device_create(struct device* dev,
293 struct device_meta* parent,
298 device_alloc(struct device_meta* parent, u32_t type, void* underlay);
300 static inline struct device* must_inline
301 device_allocsys(struct device_meta* parent, void* underlay)
303 return device_alloc(parent, DEV_IFSYS, underlay);
306 static inline struct device* must_inline
307 device_allocseq(struct device_meta* parent, void* underlay)
309 return device_alloc(parent, DEV_IFSEQ, underlay);
312 static inline struct device* must_inline
313 device_allocvol(struct device_meta* parent, void* underlay)
315 return device_alloc(parent, DEV_IFVOL, underlay);
319 device_addalias(struct device_meta* parent, struct device_meta* aliased, char* name_fmt, ...);
322 device_addcat(struct device_meta* parent, char* name_fmt, ...);
325 device_remove(struct device_meta* dev);
328 device_getbyid(struct llist_header* devlist, u32_t id);
331 device_getbyhname(struct device_meta* root_dev, struct hstr* name);
334 device_getbyname(struct device_meta* root_dev, const char* name, size_t len);
337 device_getbyoffset(struct device_meta* root_dev, int pos);
340 device_definitions_byif(int if_type);
343 devdef_byident(struct devident* class);
346 device_populate_info(struct device* dev, struct dev_info* devinfo);
349 device_scan_drivers();
351 /*------ Capability ------*/
353 struct capability_meta*
354 alloc_capability(int cap, unsigned int size);
356 #define new_capability(cap_type, cap_impl)\
357 ((cap_impl*)alloc_capability((cap_type), sizeof(cap_impl)))
359 #define new_capability_marker(cap_type)\
360 (alloc_capability((cap_type), sizeof(struct capability_meta)))
363 device_grant_capability(struct device* dev, struct capability_meta* cap);
365 struct capability_meta*
366 device_get_capability(struct device* dev, unsigned int cap_type);
367 /*------ Load hooks ------*/
370 device_onboot_load();
373 device_postboot_load();
376 device_sysconf_load();
379 device_lock(struct device* dev)
381 mutex_lock(&dev->lock);
385 device_unlock(struct device* dev)
387 mutex_unlock(&dev->lock);
391 device_locked(struct device* dev)
393 return mutex_on_hold(&dev->lock);
396 #define devprintf_expand(devident) (devident)->fn_grp, (devident)->unique
398 #endif /* __LUNAIX_DEVICE_H */