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/semaphore.h>
12 #include <lunaix/types.h>
15 * @brief Export a device definition
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);
22 #define load_on_demand ld_ondemand
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
31 #define load_earlystage ld_early
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
38 #define load_timerstage ld_aftertimer
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
46 #define load_poststage ld_post
49 * @brief Declare a device class
52 #define DEVCLASS(devif, devfn, devkind, devvar) \
55 .meta = DEV_META(devif, devfn), .device = (devkind), \
59 #define DEV_STRUCT_MAGIC 0x5645444c
61 #define DEV_MSKIF 0x00000003
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
68 typedef unsigned int dev_t;
81 struct llist_header siblings;
82 struct llist_header children;
83 struct device* parent;
84 // TODO investigate event polling
87 struct devclass class;
90 char name_val[DEVICE_NAME_SIZE];
95 // TODO Think about where will they fit.
96 int (*acquire)(struct device* dev);
97 int (*release)(struct device* dev);
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);
109 struct llist_header dev_list;
110 struct hlist_node hlist;
111 struct hlist_node hlist_if;
114 struct devclass class;
116 int (*init)(struct device_def*);
117 int (*init_for)(struct device_def*, struct device*);
120 static inline u32_t devclass_hash(struct devclass class)
122 return (((class.device & 0xffff) << 16) | (class.variant & 0xffff)) ^
127 device_register_all();
130 device_prepare(struct device* dev);
133 device_setname(struct device* dev, char* fmt, ...);
136 device_setname(struct device* dev, char* fmt, ...);
139 device_add_vargs(struct device* parent,
146 device_add(struct device* parent,
153 device_addsys(struct device* parent, void* underlay, char* name_fmt, ...);
156 device_addseq(struct device* parent, void* underlay, char* name_fmt, ...);
159 device_addvol(struct device* parent, void* underlay, char* name_fmt, ...);
162 device_addcat(struct device* parent, char* name_fmt, ...);
165 device_remove(struct device* dev);
168 device_getbyid(struct llist_header* devlist, dev_t id);
171 device_getbyhname(struct device* root_dev, struct hstr* name);
174 device_getbyname(struct device* root_dev, const char* name, size_t len);
177 device_getbyoffset(struct device* root_dev, int pos);
180 device_create_byclass(struct devclass* class,
186 device_definitions_byif(int if_type);
189 devdef_byclass(struct devclass* class);
192 device_register_all();
194 /*------ Load hooks ------*/
205 #endif /* __LUNAIX_DEVICE_H */