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
79 struct llist_header siblings;
80 struct llist_header children;
81 struct device* parent;
82 // TODO investigate event polling
85 struct devclass* class;
88 char name_val[DEVICE_NAME_SIZE];
93 // TODO Think about where will they fit.
94 int (*acquire)(struct device* dev);
95 int (*release)(struct device* dev);
97 int (*read)(struct device* dev, void* buf, size_t offset, size_t len);
98 int (*write)(struct device* dev, void* buf, size_t offset, size_t len);
99 int (*read_page)(struct device* dev, void* buf, size_t offset);
100 int (*write_page)(struct device* dev, void* buf, size_t offset);
101 int (*exec_cmd)(struct device* dev, u32_t req, va_list args);
107 struct llist_header dev_list;
108 struct hlist_node hlist;
109 struct hlist_node hlist_if;
112 struct devclass class;
114 int (*init)(struct device_def*);
115 int (*init_for)(struct device_def*, struct device*);
118 static inline u32_t devclass_hash(struct devclass class)
120 return (((class.device & 0xffff) << 16) | (class.variant & 0xffff)) ^
125 device_id_from_class(struct devclass* class)
127 return ((class->device & 0xffff) << 16) | ((class->variant & 0xffff));
131 device_register_all();
134 device_prepare(struct device* dev, struct devclass* class);
137 device_setname(struct device* dev, char* fmt, ...);
140 device_setname(struct device* dev, char* fmt, ...);
143 device_add_vargs(struct device* parent,
147 struct devclass* class,
151 device_add(struct device* parent,
152 struct devclass* class,
159 device_addsys(struct device* parent,
160 struct devclass* class,
166 device_addseq(struct device* parent,
167 struct devclass* class,
173 device_addvol(struct device* parent,
174 struct devclass* class,
180 device_addcat(struct device* parent, char* name_fmt, ...);
183 device_remove(struct device* dev);
186 device_getbyid(struct llist_header* devlist, u32_t id);
189 device_getbyhname(struct device* root_dev, struct hstr* name);
192 device_getbyname(struct device* root_dev, const char* name, size_t len);
195 device_getbyoffset(struct device* root_dev, int pos);
198 device_create_byclass(struct devclass* class,
204 device_definitions_byif(int if_type);
207 devdef_byclass(struct devclass* class);
210 device_register_all();
212 /*------ Load hooks ------*/
223 #endif /* __LUNAIX_DEVICE_H */