feat: nearly complete POSIX.1-2008 compliant terminal interface implementation
[lunaix-os.git] / lunaix-os / includes / lunaix / device.h
index fac62a9b54f179c5d42d1ed598c96fe81234e330..a3b6f9403db071c1403215557245a35dad290242 100644 (file)
@@ -9,44 +9,64 @@
 #include <lunaix/ds/ldga.h>
 #include <lunaix/ds/llist.h>
 #include <lunaix/ds/mutex.h>
+#include <lunaix/iopoll.h>
 #include <lunaix/types.h>
 
 #include <usr/lunaix/device.h>
 
 /**
- * @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
-#define load_pci_probe 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
 
 struct device
 {
+    /* -- device structing -- */
+
     u32_t magic;
     struct llist_header siblings;
     struct llist_header children;
     struct device* parent;
-    mutex_t lock;
 
-    // TODO investigate event polling
+    /* -- device state -- */
+
+    mutex_t lock;
 
     struct hstr name;
     struct devident ident;
@@ -92,17 +115,24 @@ struct device
     char name_val[DEVICE_NAME_SIZE];
     void* underlay;
 
+    /* -- polling -- */
+    int poll_evflags;
+    poll_evt_q pollers;
+
     struct
     {
         // TODO Think about where will they fit.
         int (*acquire)(struct device* dev);
         int (*release)(struct device* dev);
 
-        int (*read)(struct device* dev, void* buf, size_t offset, size_t len);
-        int (*write)(struct device* dev, void* buf, size_t offset, size_t len);
-        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 (*read)(struct device*, void*, off_t, size_t);
+        int (*write)(struct device*, void*, off_t, size_t);
+
+        int (*read_page)(struct device*, void*, off_t);
+        int (*write_page)(struct device*, void*, off_t);
+
+        int (*exec_cmd)(struct device*, u32_t, va_list);
+        int (*poll)(struct device*);
     } ops;
 };
 
@@ -115,11 +145,36 @@ struct device_def
 
     struct devclass class;
 
+    /**
+     * @brief Called when the driver is required to initialize itself.
+     *
+     */
     int (*init)(struct device_def*);
+
+    /**
+     * @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);
 };
 
+#define mark_device_doing_write(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLOUT
+#define mark_device_done_write(dev_ptr) (dev_ptr)->poll_evflags |= _POLLOUT
+
+#define mark_device_doing_read(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLIN
+#define mark_device_done_read(dev_ptr) (dev_ptr)->poll_evflags |= _POLLIN
+
+#define mark_device_hanging(dev_ptr) (dev_ptr)->poll_evflags &= ~_POLLHUP
+#define mark_device_grounded(dev_ptr) (dev_ptr)->poll_evflags |= _POLLHUP
+
 static inline u32_t
 device_id_from_class(struct devclass* class)
 {
@@ -201,13 +256,13 @@ 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)