refactor: add a async read/write variant to device ops, with allow async io to be...
[lunaix-os.git] / lunaix-os / includes / lunaix / device.h
index f65082126313c81202e0b587d7ae869849c73d01..1844590dce24ace0263102ff95a7551e02060dfb 100644 (file)
@@ -9,6 +9,7 @@
 #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>
 
 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;
@@ -111,17 +115,26 @@ 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_async)(struct device*, void*, off_t, size_t);
+        int (*write_async)(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;
 };
 
@@ -155,6 +168,15 @@ struct device_def
     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)
 {