rewrite the device subsystem interfaces (#48)
[lunaix-os.git] / lunaix-os / kernel / device / device.c
index b88f8f6c283ea311fb3e524059f67a6eabe3d143..9af8ac34fdd75b7b1a5545eacc4ed81967561493 100644 (file)
@@ -1,4 +1,3 @@
-
 #include <lunaix/device.h>
 #include <lunaix/fs.h>
 #include <lunaix/fs/twifs.h>
 #include <lunaix/device.h>
 #include <lunaix/fs.h>
 #include <lunaix/fs/twifs.h>
@@ -7,76 +6,61 @@
 #include <lunaix/spike.h>
 #include <lunaix/syscall.h>
 #include <lunaix/syscall_utils.h>
 #include <lunaix/spike.h>
 #include <lunaix/syscall.h>
 #include <lunaix/syscall_utils.h>
+#include <lunaix/owloysius.h>
 
 #include <klibc/strfmt.h>
 #include <klibc/string.h>
 
 static DEFINE_LLIST(root_list);
 
 
 #include <klibc/strfmt.h>
 #include <klibc/string.h>
 
 static DEFINE_LLIST(root_list);
 
-static volatile u32_t devid = 0;
-
-struct devclass default_devclass = {};
+morph_t* device_mobj_root;
 
 void
 device_setname_vargs(struct device_meta* dev, char* fmt, va_list args)
 {
 
 void
 device_setname_vargs(struct device_meta* dev, char* fmt, va_list args)
 {
-    size_t strlen = ksnprintfv(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
-
-    dev->name = HSTR(dev->name_val, strlen);
-
-    hstr_rehash(&dev->name, HSTR_FULL_HASH);
+    ksnprintfv(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
+    changeling_setname(dev_mobj(dev), dev->name_val);
 }
 
 void
 }
 
 void
-device_register_generic(struct device_meta* devm, struct devclass* class, char* fmt, ...)
+device_register_generic(struct device_meta* devm, struct devclass* class, 
+                        char* fmt, ...)
 {
     va_list args;
 {
     va_list args;
+    morph_t* morphed, *parent;
+
+    morphed = &devm->mobj;
     va_start(args, fmt);
 
     if (fmt) {
         device_setname_vargs(devm, fmt, args);
     }
 
     va_start(args, fmt);
 
     if (fmt) {
         device_setname_vargs(devm, fmt, args);
     }
 
-    if (class && valid_device_subtype_ref(devm, DEV_STRUCT)) {
+    if (class && morph_type_of(morphed, device_morpher)) 
+    {
         struct device* dev = to_dev(devm);
         struct device* dev = to_dev(devm);
-        dev->ident = (struct devident){ .fn_grp = class->fn_grp,
-                                        .unique = DEV_UNIQUE(class->device,
-                                                             class->variant) };
+        dev->ident = (struct devident) { 
+            .fn_grp = class->fn_grp,
+            .unique = DEV_UNIQUE(class->device, class->variant) 
+        };
     }
 
     }
 
-    devm->dev_uid = devid++;
-
-    struct device_meta* parent = devm->parent;
+    parent = morphed->parent;
     if (parent) {
     if (parent) {
-        assert(valid_device_subtype_ref(parent, DEV_CAT));
-        llist_append(&parent->children, &devm->siblings);
-    } else {
-        llist_append(&root_list, &devm->siblings);
+        changeling_attach(parent, morphed);
     }
 
     va_end(args);
 }
 
     }
 
     va_end(args);
 }
 
-static void
-device_init_meta(struct device_meta* dmeta, struct device_meta* parent, unsigned int subtype) 
-{
-    dmeta->magic = DEV_STRUCT_MAGIC_MASK | subtype;
-    dmeta->parent = parent;
-
-    llist_init_head(&dmeta->children);
-}
-
 void
 void
-device_create(struct device* dev,
-              struct device_meta* parent,
-              u32_t type,
-              void* underlay)
+device_create(struct device* dev, struct device_meta* parent,
+              u32_t type, void* underlay)
 {
 {
-    dev->magic = DEV_STRUCT_MAGIC;
     dev->underlay = underlay;
     dev->dev_type = type;
 
     dev->underlay = underlay;
     dev->dev_type = type;
 
-    device_init_meta(dev_meta(dev), parent, DEV_STRUCT);
+    llist_init_head(&dev->potentium);
     mutex_init(&dev->lock);
     iopoll_init_evt_q(&dev->pollers);
 }
     mutex_init(&dev->lock);
     iopoll_init_evt_q(&dev->pollers);
 }
@@ -91,6 +75,7 @@ device_alloc(struct device_meta* parent, u32_t type, void* underlay)
     }
 
     device_create(dev, parent, type, underlay);
     }
 
     device_create(dev, parent, type, underlay);
+    changeling_morph(dev_morph(parent), dev->mobj, NULL, device_morpher);
 
     return dev;
 }
 
     return dev;
 }
@@ -104,8 +89,9 @@ device_alloc_alias(struct device_meta* parent, struct device_meta* aliased)
         return NULL;
     }
 
         return NULL;
     }
 
-    device_init_meta(dev_meta(dev), parent, DEV_ALIAS);
     dev->alias = aliased;
     dev->alias = aliased;
+    changeling_ref(dev_mobj(aliased));
+    changeling_morph(dev_morph(parent), dev->mobj, NULL, devalias_morpher);
 
     return dev;
 }
 
     return dev;
 }
@@ -119,12 +105,11 @@ device_alloc_cat(struct device_meta* parent)
         return NULL;
     }
 
         return NULL;
     }
 
-    device_init_meta(dev_meta(dev), parent, DEV_CAT);
+    changeling_morph(dev_morph(parent), dev->mobj, NULL, devcat_morpher);
 
     return dev;
 }
 
 
     return dev;
 }
 
-
 void
 device_setname(struct device_meta* dev, char* fmt, ...)
 {
 void
 device_setname(struct device_meta* dev, char* fmt, ...)
 {
@@ -152,7 +137,8 @@ device_addcat(struct device_meta* parent, char* name_fmt, ...)
 }
 
 struct device_alias*
 }
 
 struct device_alias*
-device_addalias(struct device_meta* parent, struct device_meta* aliased, char* name_fmt, ...)
+device_addalias(struct device_meta* parent, 
+                struct device_meta* aliased, char* name_fmt, ...)
 {
     va_list args;
     va_start(args, name_fmt);
 {
     va_list args;
     va_start(args, name_fmt);
@@ -166,67 +152,13 @@ device_addalias(struct device_meta* parent, struct device_meta* aliased, char* n
     return dev;
 }
 
     return dev;
 }
 
-struct device_meta*
-device_getbyid(struct llist_header* devlist, u32_t id)
-{
-    devlist = devlist ? devlist : &root_list;
-    struct device_meta *pos, *n;
-    llist_for_each(pos, n, devlist, siblings)
-    {
-        if (pos->dev_uid == id) {
-            return pos;
-        }
-    }
-
-    return NULL;
-}
-
-struct device_meta*
-device_getbyhname(struct device_meta* root_dev, struct hstr* name)
-{
-    struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
-    struct device_meta *pos, *n;
-    llist_for_each(pos, n, devlist, siblings)
-    {
-        if (HSTR_EQ(&pos->name, name)) {
-            return pos;
-        }
-    }
-
-    return NULL;
-}
-
-struct device_meta*
-device_getbyname(struct device_meta* root_dev, const char* name, size_t len)
-{
-    struct hstr hname = HSTR(name, len);
-    hstr_rehash(&hname, HSTR_FULL_HASH);
-
-    return device_getbyhname(root_dev, &hname);
-}
-
 void
 device_remove(struct device_meta* dev)
 {
 void
 device_remove(struct device_meta* dev)
 {
-    llist_delete(&dev->siblings);
+    changeling_isolate(&dev->mobj);
     vfree(dev);
 }
 
     vfree(dev);
 }
 
-struct device_meta*
-device_getbyoffset(struct device_meta* root_dev, int offset)
-{
-    struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
-    struct device_meta *pos, *n;
-    int off = 0;
-    llist_for_each(pos, n, devlist, siblings)
-    {
-        if (off++ >= offset) {
-            return pos;
-        }
-    }
-    return NULL;
-}
-
 void
 device_populate_info(struct device* dev, struct dev_info* devinfo)
 {
 void
 device_populate_info(struct device* dev, struct dev_info* devinfo)
 {
@@ -244,44 +176,35 @@ device_populate_info(struct device* dev, struct dev_info* devinfo)
     devinfo->dev_name.buf[buflen - 1] = 0;
 }
 
     devinfo->dev_name.buf[buflen - 1] = 0;
 }
 
-struct device_meta*
-resolve_device_meta(void* maybe_dev) {
-    if (!valid_device_ref(maybe_dev)) {
+morph_t*
+resolve_device_morph(void* maybe_dev)
+{
+    struct device_alias* da = NULL;
+    morph_t *morphed;
+    
+    morphed = morphed_ptr(maybe_dev);
+
+    if (!is_changeling(morphed)) {
         return NULL;
     }
 
         return NULL;
     }
 
-    struct device_meta* dm = (struct device_meta*)maybe_dev;
-    unsigned int subtype = dm->magic ^ DEV_STRUCT_MAGIC_MASK;
-    
-    switch (subtype)
+    if (morph_type_of(morphed, device_morpher)) 
     {
     {
-        case DEV_STRUCT:
-        case DEV_CAT:
-            return dm;
-        
-        case DEV_ALIAS: {
-            struct device_meta* aliased_dm = dm;
-            
-            while(valid_device_subtype_ref(aliased_dm, DEV_ALIAS)) {
-                aliased_dm = to_aliasdev(aliased_dm)->alias;
-            }
-
-            return aliased_dm;
-        }
-        default:
-            return NULL;
+        return morphed;
     }
     }
-}
 
 
-struct device*
-resolve_device(void* maybe_dev) {
-    struct device_meta* dm = resolve_device_meta(maybe_dev);
-    
-    if (!valid_device_subtype_ref(dm, DEV_STRUCT)) {
-        return NULL;
+    if (morph_type_of(morphed, devcat_morpher)) 
+    {
+        return morphed;
+    }
+
+    while(morph_type_of(morphed, devalias_morpher)) 
+    {
+        da = changeling_reveal(morphed, devalias_morpher);
+        morphed = &da->alias->mobj;
     }
 
     }
 
-    return to_dev(dm);
+    return da ? morphed : NULL;
 }
 
 void
 }
 
 void
@@ -291,17 +214,72 @@ device_alert_poller(struct device* dev, int poll_evt)
     iopoll_wake_pollers(&dev->pollers);
 }
 
     iopoll_wake_pollers(&dev->pollers);
 }
 
-__DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
+void
+device_chain_loader(struct device_def* def, devdef_ldfn fn)
+{
+    struct device_ldfn_chain* node;
+
+    node = valloc(sizeof(*node));
+    node->load = fn;
+
+    if (!def->load_chain) {
+        node->chain = NULL;
+    }
+    else {
+        node->chain = def->load_chain;
+    }
+
+    def->load_chain = node;
+}
+
+void
+device_chain_load_once(struct device_def* def)
+{
+    struct device_ldfn_chain *node, *next;
+
+    if (def->load) {
+        def->load(def);
+    }
+
+    node = def->load_chain;
+    def->load_chain = NULL;
+    
+    while (node)
+    {
+        node->load(def);
+        next = node->chain;
+        vfree(node);
+
+        node = next;
+    }
+
+    if (def->flags.no_default_realm) {
+        return;
+    }
+
+    if (!def->create) {
+        return;
+    }
+
+    def->create(def, NULL);
+    
+}
+
+__DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, sc_va_list, _args)
 {
     int errno = -1;
     struct v_fd* fd_s;
 {
     int errno = -1;
     struct v_fd* fd_s;
+    va_list args;
+
+    convert_valist(&args, _args);
+
     if ((errno &= vfs_getfd(fd, &fd_s))) {
         goto done;
     }
 
     if ((errno &= vfs_getfd(fd, &fd_s))) {
         goto done;
     }
 
-    struct device* dev = (struct device*)fd_s->file->inode->data;
-    if (valid_device_subtype_ref(dev, DEV_STRUCT)) {
-        errno &= ENODEV;
+    struct device* dev = resolve_device(fd_s->file->inode->data);
+    if (!dev) {
+        errno = ENODEV;
         goto done;
     }
 
         goto done;
     }
 
@@ -312,12 +290,19 @@ __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
     }
 
     if (!dev->ops.exec_cmd) {
     }
 
     if (!dev->ops.exec_cmd) {
-        errno &= ENOTSUP;
+        errno = ENOTSUP;
         goto done;
     }
 
         goto done;
     }
 
-    errno &= dev->ops.exec_cmd(dev, req, args);
+    errno = dev->ops.exec_cmd(dev, req, args);
 
 done:
     return DO_STATUS_OR_RETURN(errno);
 
 done:
     return DO_STATUS_OR_RETURN(errno);
-}
\ No newline at end of file
+}
+
+static void
+__device_subsys_init()
+{
+    device_mobj_root = changeling_spawn(NULL, "devices");
+}
+owloysius_fetch_init(__device_subsys_init, on_sysconf);
\ No newline at end of file