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 (*bind)(struct device_def*, struct device*);
120 int (*free)(struct device_def*, void* instance);
124 device_id_from_class(struct devclass* class)
126 return ((class->device & 0xffff) << 16) | ((class->variant & 0xffff));
130 device_scan_drivers();
133 device_setname_vargs(struct device* dev, char* fmt, va_list args);
136 device_setname(struct device* dev, char* fmt, ...);
139 device_register(struct device* dev, struct devclass* class, char* fmt, ...);
142 device_create(struct device* dev,
143 struct device* parent,
148 device_alloc(struct device* parent, u32_t type, void* underlay);
150 static inline struct device*
151 device_allocsys(struct device* parent, void* underlay)
153 return device_alloc(parent, DEV_IFSYS, underlay);
156 static inline struct device*
157 device_allocseq(struct device* parent, void* underlay)
159 return device_alloc(parent, DEV_IFSEQ, underlay);
162 static inline struct device*
163 device_allocvol(struct device* parent, void* underlay)
165 return device_alloc(parent, DEV_IFVOL, underlay);
169 device_addcat(struct device* parent, char* name_fmt, ...);
172 device_remove(struct device* dev);
175 device_getbyid(struct llist_header* devlist, u32_t id);
178 device_getbyhname(struct device* root_dev, struct hstr* name);
181 device_getbyname(struct device* root_dev, const char* name, size_t len);
184 device_getbyoffset(struct device* root_dev, int pos);
187 device_cast(void* obj);
190 device_definitions_byif(int if_type);
193 devdef_byident(struct devident* class);
196 device_populate_info(struct device* dev, struct dev_info* devinfo);
199 device_scan_drivers();
201 /*------ Load hooks ------*/
213 device_lock(struct device* dev)
215 mutex_lock(&dev->lock);
219 device_unlock(struct device* dev)
221 mutex_unlock(&dev->lock);
225 device_locked(struct device* dev)
227 return mutex_on_hold(&dev->lock);
230 #define devprintf_expand(devident) (devident)->fn_grp, (devident)->unique
232 #endif /* __LUNAIX_DEVICE_H */