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/types.h>
14 #include <usr/lunaix/device.h>
17 * @brief Export a device definition
20 #define EXPORT_DEVICE(id, devdef, load_order) \
21 export_ldga_el(devdefs, id, ptr_t, devdef); \
22 export_ldga_el_sfx(devdefs, id##_ldorder, ptr_t, devdef, load_order);
24 #define load_on_demand ld_ondemand
25 #define load_pci_probe ld_ondemand
28 * @brief Mark the device definition should be loaded automatically as earlier
29 * as possible in the kernel bootstrapping stage (before initialization of file
30 * systems). Load here if your driver is standalone and require no other than
31 * basic memory allocation services
34 #define load_earlystage ld_early
37 * @brief Mark the device definition should be loaded automatically after timer
38 * is ready. Load here if your driver require a basic timing service
41 #define load_timerstage ld_aftertimer
44 * @brief Mark the device definition should be loaded automatically in
45 * the post boostrapping stage (i.e., the start up of proc0). Load here if your
46 * driver involves async mechanism
49 #define load_poststage ld_post
52 * @brief Declare a device class
55 #define DEVCLASS(devif, devfn, dev) \
58 .fn_grp = DEV_FNGRP(devif, devfn), .device = (dev), .variant = 0 \
61 #define DEVCLASSV(devif, devfn, dev, devvar) \
64 .fn_grp = DEV_FNGRP(devif, devfn), .device = (dev), \
68 #define DEV_STRUCT_MAGIC 0x5645444c
70 #define DEV_MSKIF 0x00000003
72 #define DEV_IFVOL 0x0 // volumetric (block) device
73 #define DEV_IFSEQ 0x1 // sequential (character) device
74 #define DEV_IFCAT 0x2 // a device category (as device groupping)
75 #define DEV_IFSYS 0x3 // a system device
80 struct llist_header siblings;
81 struct llist_header children;
82 struct device* parent;
85 // TODO investigate event polling
88 struct devident ident;
92 char name_val[DEVICE_NAME_SIZE];
97 // TODO Think about where will they fit.
98 int (*acquire)(struct device* dev);
99 int (*release)(struct device* dev);
101 int (*read)(struct device* dev, void* buf, size_t offset, size_t len);
102 int (*write)(struct device* dev, void* buf, size_t offset, size_t len);
103 int (*read_page)(struct device* dev, void* buf, size_t offset);
104 int (*write_page)(struct device* dev, void* buf, size_t offset);
105 int (*exec_cmd)(struct device* dev, u32_t req, va_list args);
111 struct llist_header dev_list;
112 struct hlist_node hlist;
113 struct hlist_node hlist_if;
116 struct devclass class;
118 int (*init)(struct device_def*);
119 int (*init_for)(struct device_def*, struct device*);
123 device_id_from_class(struct devclass* class)
125 return ((class->device & 0xffff) << 16) | ((class->variant & 0xffff));
129 device_scan_drivers();
132 device_setname_vargs(struct device* dev, char* fmt, va_list args);
135 device_setname(struct device* dev, char* fmt, ...);
138 device_register(struct device* dev, struct devclass* class, char* fmt, ...);
141 device_create(struct device* dev,
142 struct device* parent,
147 device_alloc(struct device* parent, u32_t type, void* underlay);
149 static inline struct device*
150 device_allocsys(struct device* parent, void* underlay)
152 return device_alloc(parent, DEV_IFSYS, underlay);
155 static inline struct device*
156 device_allocseq(struct device* parent, void* underlay)
158 return device_alloc(parent, DEV_IFSEQ, underlay);
161 static inline struct device*
162 device_allocvol(struct device* parent, void* underlay)
164 return device_alloc(parent, DEV_IFVOL, underlay);
168 device_addcat(struct device* parent, char* name_fmt, ...);
171 device_remove(struct device* dev);
174 device_getbyid(struct llist_header* devlist, u32_t id);
177 device_getbyhname(struct device* root_dev, struct hstr* name);
180 device_getbyname(struct device* root_dev, const char* name, size_t len);
183 device_getbyoffset(struct device* root_dev, int pos);
186 device_cast(void* obj);
189 device_definitions_byif(int if_type);
192 devdef_byident(struct devident* class);
195 device_populate_info(struct device* dev, struct dev_info* devinfo);
198 device_scan_drivers();
200 /*------ Load hooks ------*/
212 device_lock(struct device* dev)
214 mutex_lock(&dev->lock);
218 device_unlock(struct device* dev)
220 mutex_unlock(&dev->lock);
224 device_locked(struct device* dev)
226 return mutex_on_hold(&dev->lock);
229 #endif /* __LUNAIX_DEVICE_H */