X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/6f6da1abb22dff69dbb710bd2fd9d95f083f2b43..78cd005fac540973751b5a108c37a715bc64b5a2:/lunaix-os/includes/lunaix/device.h diff --git a/lunaix-os/includes/lunaix/device.h b/lunaix-os/includes/lunaix/device.h index af21cb3..affdb10 100644 --- a/lunaix-os/includes/lunaix/device.h +++ b/lunaix-os/includes/lunaix/device.h @@ -8,51 +8,80 @@ #include #include #include -#include +#include +#include #include +#include + /** - * @brief Export a device definition + * @brief Export a device definition (i.e., device driver metadata) * */ #define EXPORT_DEVICE(id, devdef, load_order) \ export_ldga_el(devdefs, id, ptr_t, devdef); \ export_ldga_el_sfx(devdefs, id##_ldorder, ptr_t, devdef, load_order); +/** + * @brief Mark the device definition can be loaded on demand, all other loading + * options are extended from this + */ #define load_on_demand ld_ondemand /** - * @brief Mark the device definition should be loaded automatically as earlier - * as possible in the kernel bootstrapping stage (before initialization of file - * systems). Load here if your driver is standalone and require no other than - * basic memory allocation services + * @brief Mark the device definition to be loaded as system configuration + * device. These kind of devices are defined to be the devices that talk to the + * system firmware to do config, or collecting crucial information about the + * system. For instances, ACPI, SoC components, and other **interconnection** + * buese (not USB!). Such device driver must only rely on basic memory + * management service, and must not try accessing subsystems other than the mm + * unit, for example, timer, interrupt, file-system, must not assumed exist. * */ -#define load_earlystage ld_early +#define load_sysconf ld_sysconf + +/** + * @brief Mark the device definition should be loaded as time device, for + * example a real time clock device. Such device will be loaded and managed by + * clock subsystem + */ +#define load_timedev ld_timedev /** - * @brief Mark the device definition should be loaded automatically after timer - * is ready. Load here if your driver require a basic timing service + * @brief Mark the device definition should be loaded automatically during the + * bootstrapping stage. Most of the driver do load there. * */ -#define load_timerstage ld_aftertimer +#define load_onboot ld_kboot /** * @brief Mark the device definition should be loaded automatically in - * the post boostrapping stage (i.e., the start up of proc0). Load here if your - * driver involves async mechanism + * the post boostrapping stage (i.e., the start up of proc0), where most of + * kernel sub-system are became ready to use. Do your load there if your driver + * depends on such condition * */ -#define load_poststage ld_post +#define load_postboot ld_post + +#define __foreach_exported_device_of(stage, index, pos) \ + ldga_foreach(dev_##stage, struct device_def*, index, pos) +#define foreach_exported_device_of(stage, index, pos) \ + __foreach_exported_device_of(stage, index, pos) /** * @brief Declare a device class * */ -#define DEVCLASS(devif, devfn, devkind, devvar) \ +#define DEVCLASS(devif, devfn, dev) \ + (struct devclass) \ + { \ + .fn_grp = DEV_FNGRP(devif, devfn), .device = (dev), .variant = 0 \ + } + +#define DEVCLASSV(devif, devfn, dev, devvar) \ (struct devclass) \ { \ - .meta = DEV_META(devif, devfn), .device = (devkind), \ + .fn_grp = DEV_FNGRP(devif, devfn), .device = (dev), \ .variant = (devvar) \ } @@ -65,29 +94,31 @@ #define DEV_IFCAT 0x2 // a device category (as device groupping) #define DEV_IFSYS 0x3 // a system device -struct devclass -{ - u32_t meta; - u32_t device; - u32_t variant; - u32_t hash; -}; - struct device { + /* -- device structing -- */ + u32_t magic; struct llist_header siblings; struct llist_header children; struct device* parent; - // TODO investigate event polling + + /* -- device state -- */ + + mutex_t lock; struct hstr name; - struct devclass* class; + struct devident ident; + u32_t dev_uid; int dev_type; char name_val[DEVICE_NAME_SIZE]; void* underlay; + /* -- polling -- */ + int poll_evflags; + poll_evt_q pollers; + struct { // TODO Think about where will they fit. @@ -99,6 +130,7 @@ struct device int (*read_page)(struct device* dev, void* buf, size_t offset); int (*write_page)(struct device* dev, void* buf, size_t offset); int (*exec_cmd)(struct device* dev, u32_t req, va_list args); + int (*poll)(struct device* dev); } ops; }; @@ -111,15 +143,26 @@ struct device_def struct devclass class; + /** + * @brief Called when the driver is required to initialize itself. + * + */ int (*init)(struct device_def*); - int (*init_for)(struct device_def*, struct device*); -}; -static inline u32_t devclass_hash(struct devclass class) -{ - return (((class.device & 0xffff) << 16) | (class.variant & 0xffff)) ^ - ~class.meta; -} + /** + * @brief Called when the driver is required to bind with a device. This is + * the case for a real-hardware-oriented driver + * + */ + int (*bind)(struct device_def*, struct device*); + + /** + * @brief Called when a driver is requested to detach from the device and + * free up all it's resources + * + */ + int (*free)(struct device_def*, void* instance); +}; static inline u32_t device_id_from_class(struct devclass* class) @@ -128,53 +171,43 @@ device_id_from_class(struct devclass* class) } void -device_register_all(); +device_scan_drivers(); void -device_prepare(struct device* dev, struct devclass* class); +device_setname_vargs(struct device* dev, char* fmt, va_list args); void device_setname(struct device* dev, char* fmt, ...); void -device_setname(struct device* dev, char* fmt, ...); +device_register(struct device* dev, struct devclass* class, char* fmt, ...); -struct device* -device_add_vargs(struct device* parent, - void* underlay, - char* name_fmt, - u32_t type, - struct devclass* class, - va_list args); +void +device_create(struct device* dev, + struct device* parent, + u32_t type, + void* underlay); struct device* -device_add(struct device* parent, - struct devclass* class, - void* underlay, - u32_t type, - char* name_fmt, - ...); +device_alloc(struct device* parent, u32_t type, void* underlay); -struct device* -device_addsys(struct device* parent, - struct devclass* class, - void* underlay, - char* name_fmt, - ...); +static inline struct device* +device_allocsys(struct device* parent, void* underlay) +{ + return device_alloc(parent, DEV_IFSYS, underlay); +} -struct device* -device_addseq(struct device* parent, - struct devclass* class, - void* underlay, - char* name_fmt, - ...); +static inline struct device* +device_allocseq(struct device* parent, void* underlay) +{ + return device_alloc(parent, DEV_IFSEQ, underlay); +} -struct device* -device_addvol(struct device* parent, - struct devclass* class, - void* underlay, - char* name_fmt, - ...); +static inline struct device* +device_allocvol(struct device* parent, void* underlay) +{ + return device_alloc(parent, DEV_IFVOL, underlay); +} struct device* device_addcat(struct device* parent, char* name_fmt, ...); @@ -195,29 +228,49 @@ struct device* device_getbyoffset(struct device* root_dev, int pos); struct device* -device_create_byclass(struct devclass* class, - u32_t type, - char* name, - int* err_code); +device_cast(void* obj); struct hbucket* device_definitions_byif(int if_type); struct device_def* -devdef_byclass(struct devclass* class); +devdef_byident(struct devident* class); void -device_register_all(); +device_populate_info(struct device* dev, struct dev_info* devinfo); + +void +device_scan_drivers(); /*------ Load hooks ------*/ void -device_earlystage(); +device_onbooot_load(); void -device_poststage(); +device_postboot_load(); void -device_timerstage(); +device_sysconf_load(); + +static inline void +device_lock(struct device* dev) +{ + mutex_lock(&dev->lock); +} + +static inline void +device_unlock(struct device* dev) +{ + mutex_unlock(&dev->lock); +} + +static inline int +device_locked(struct device* dev) +{ + return mutex_on_hold(&dev->lock); +} + +#define devprintf_expand(devident) (devident)->fn_grp, (devident)->unique #endif /* __LUNAIX_DEVICE_H */