Architectural Support: x86_64 (#37)
[lunaix-os.git] / lunaix-os / kernel / device / device.c
index 6b901a6593958ac2a38fd4ea35009fd62d67c141..d32ca32bada4c093e47e728ec8a6836db9d22d22 100644 (file)
@@ -1,4 +1,4 @@
-#include <klibc/stdio.h>
+
 #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>
 #include <lunaix/mm/valloc.h>
 #include <lunaix/spike.h>
 #include <lunaix/syscall.h>
 #include <lunaix/mm/valloc.h>
 #include <lunaix/spike.h>
 #include <lunaix/syscall.h>
+#include <lunaix/syscall_utils.h>
+
+#include <klibc/strfmt.h>
+#include <klibc/string.h>
 
 static DEFINE_LLIST(root_list);
 
 
 static DEFINE_LLIST(root_list);
 
-static volatile dev_t devid = 0;
+static volatile u32_t devid = 0;
 
 
-struct device*
-device_add(struct device* parent,
-           void* underlay,
-           char* name_fmt,
-           uint32_t type,
-           va_list args)
+struct devclass default_devclass = {};
+
+void
+device_setname_vargs(struct device_meta* dev, char* fmt, va_list args)
 {
 {
-    struct device* dev = vzalloc(sizeof(struct device));
+    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);
+}
+
+void
+device_register_generic(struct device_meta* devm, struct devclass* class, char* fmt, ...)
+{
+    va_list args;
+    va_start(args, fmt);
+
+    if (fmt) {
+        device_setname_vargs(devm, fmt, args);
+    }
+
+    if (class && valid_device_subtype_ref(devm, DEV_STRUCT)) {
+        struct device* dev = to_dev(devm);
+        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;
     if (parent) {
     if (parent) {
-        assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
-        llist_append(&parent->children, &dev->siblings);
+        assert(valid_device_subtype_ref(parent, DEV_CAT));
+        llist_append(&parent->children, &devm->siblings);
     } else {
     } else {
-        llist_append(&root_list, &dev->siblings);
+        llist_append(&root_list, &devm->siblings);
     }
 
     }
 
-    size_t strlen =
-      __ksprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, 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
+device_create(struct device* dev,
+              struct device_meta* parent,
+              u32_t type,
+              void* underlay)
+{
     dev->magic = DEV_STRUCT_MAGIC;
     dev->magic = DEV_STRUCT_MAGIC;
-    dev->dev_id = devid++;
-    dev->name = HSTR(dev->name_val, strlen);
-    dev->parent = parent;
     dev->underlay = underlay;
     dev->dev_type = type;
 
     dev->underlay = underlay;
     dev->dev_type = type;
 
-    hstr_rehash(&dev->name, HSTR_FULL_HASH);
-    llist_init_head(&dev->children);
+    device_init_meta(dev_meta(dev), parent, DEV_STRUCT);
+    llist_init_head(&dev->capabilities);
+    mutex_init(&dev->lock);
+    iopoll_init_evt_q(&dev->pollers);
+}
+
+struct device*
+device_alloc(struct device_meta* parent, u32_t type, void* underlay)
+{
+    struct device* dev = vzalloc(sizeof(struct device));
+
+    if (!dev) {
+        return NULL;
+    }
+
+    device_create(dev, parent, type, underlay);
 
     return dev;
 }
 
 
     return dev;
 }
 
-struct device*
-device_addseq(struct device* parent, void* underlay, char* name_fmt, ...)
+struct device_alias*
+device_alloc_alias(struct device_meta* parent, struct device_meta* aliased)
+{
+    struct device_alias* dev = vzalloc(sizeof(struct device_alias));
+
+    if (!dev) {
+        return NULL;
+    }
+
+    device_init_meta(dev_meta(dev), parent, DEV_ALIAS);
+    dev->alias = aliased;
+
+    return dev;
+}
+
+struct device_cat*
+device_alloc_cat(struct device_meta* parent)
+{
+    struct device_cat* dev = vzalloc(sizeof(struct device_cat));
+
+    if (!dev) {
+        return NULL;
+    }
+
+    device_init_meta(dev_meta(dev), parent, DEV_CAT);
+
+    return dev;
+}
+
+
+void
+device_setname(struct device_meta* dev, char* fmt, ...)
 {
     va_list args;
 {
     va_list args;
-    va_start(args, name_fmt);
+    va_start(args, fmt);
 
 
-    struct device* dev =
-      device_add(parent, underlay, name_fmt, DEV_IFSEQ, args);
+    device_setname_vargs(dev, fmt, args);
 
     va_end(args);
 
     va_end(args);
-    return dev;
 }
 
 }
 
-struct device*
-device_addvol(struct device* parent, void* underlay, char* name_fmt, ...)
+struct device_cat*
+device_addcat(struct device_meta* parent, char* name_fmt, ...)
 {
     va_list args;
     va_start(args, name_fmt);
 
 {
     va_list args;
     va_start(args, name_fmt);
 
-    struct device* dev =
-      device_add(parent, underlay, name_fmt, DEV_IFVOL, args);
+    struct device_cat* dev = device_alloc_cat(parent);
+
+    device_setname_vargs(dev_meta(dev), name_fmt, args);
+    device_register_generic(dev_meta(dev), NULL, NULL);
 
     va_end(args);
     return dev;
 }
 
 
     va_end(args);
     return dev;
 }
 
-struct device*
-device_addcat(struct device* parent, char* name_fmt, ...)
+struct device_alias*
+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);
 
-    struct device* dev = device_add(parent, NULL, name_fmt, DEV_IFCAT, args);
+    struct device_alias* dev = device_alloc_alias(parent, aliased);
+
+    device_setname_vargs(dev_meta(dev), name_fmt, args);
+    device_register_generic(dev_meta(dev), NULL, NULL);
 
     va_end(args);
     return dev;
 }
 
 
     va_end(args);
     return dev;
 }
 
-struct device*
-device_getbyid(struct llist_header* devlist, dev_t id)
+struct device_meta*
+device_getbyid(struct llist_header* devlist, u32_t id)
 {
     devlist = devlist ? devlist : &root_list;
 {
     devlist = devlist ? devlist : &root_list;
-    struct device *pos, *n;
+    struct device_meta *pos, *n;
     llist_for_each(pos, n, devlist, siblings)
     {
     llist_for_each(pos, n, devlist, siblings)
     {
-        if (pos->dev_id == id) {
+        if (pos->dev_uid == id) {
             return pos;
         }
     }
             return pos;
         }
     }
@@ -96,11 +182,11 @@ device_getbyid(struct llist_header* devlist, dev_t id)
     return NULL;
 }
 
     return NULL;
 }
 
-struct device*
-device_getbyhname(struct device* root_dev, struct hstr* name)
+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 llist_header* devlist = root_dev ? &root_dev->children : &root_list;
-    struct device *pos, *n;
+    struct device_meta *pos, *n;
     llist_for_each(pos, n, devlist, siblings)
     {
         if (HSTR_EQ(&pos->name, name)) {
     llist_for_each(pos, n, devlist, siblings)
     {
         if (HSTR_EQ(&pos->name, name)) {
@@ -111,8 +197,8 @@ device_getbyhname(struct device* root_dev, struct hstr* name)
     return NULL;
 }
 
     return NULL;
 }
 
-struct device*
-device_getbyname(struct device* root_dev, const char* name, size_t len)
+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);
 {
     struct hstr hname = HSTR(name, len);
     hstr_rehash(&hname, HSTR_FULL_HASH);
@@ -121,17 +207,17 @@ device_getbyname(struct device* root_dev, const char* name, size_t len)
 }
 
 void
 }
 
 void
-device_remove(struct device* dev)
+device_remove(struct device_meta* dev)
 {
     llist_delete(&dev->siblings);
     vfree(dev);
 }
 
 {
     llist_delete(&dev->siblings);
     vfree(dev);
 }
 
-struct device*
-device_getbyoffset(struct device* root_dev, int offset)
+struct device_meta*
+device_getbyoffset(struct device_meta* root_dev, int offset)
 {
     struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
 {
     struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
-    struct device *pos, *n;
+    struct device_meta *pos, *n;
     int off = 0;
     llist_for_each(pos, n, devlist, siblings)
     {
     int off = 0;
     llist_for_each(pos, n, devlist, siblings)
     {
@@ -142,26 +228,100 @@ device_getbyoffset(struct device* root_dev, int offset)
     return NULL;
 }
 
     return NULL;
 }
 
-__DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
+void
+device_populate_info(struct device* dev, struct dev_info* devinfo)
+{
+    devinfo->dev_id.group = dev->ident.fn_grp;
+    devinfo->dev_id.unique = dev->ident.unique;
+
+    if (!devinfo->dev_name.buf) {
+        return;
+    }
+
+    struct device_def* def = devdef_byident(&dev->ident);
+    size_t buflen = devinfo->dev_name.buf_len;
+
+    strncpy(devinfo->dev_name.buf, def->name, buflen);
+    devinfo->dev_name.buf[buflen - 1] = 0;
+}
+
+struct device_meta*
+resolve_device_meta(void* maybe_dev) {
+    if (!valid_device_ref(maybe_dev)) {
+        return NULL;
+    }
+
+    struct device_meta* dm = (struct device_meta*)maybe_dev;
+    unsigned int subtype = dm->magic ^ DEV_STRUCT_MAGIC_MASK;
+    
+    switch (subtype)
+    {
+        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;
+    }
+}
+
+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;
+    }
+
+    return to_dev(dm);
+}
+
+void
+device_alert_poller(struct device* dev, int poll_evt)
+{
+    dev->poll_evflags = poll_evt;
+    iopoll_wake_pollers(&dev->pollers);
+}
+
+__DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, sc_va_list, _args)
 {
 {
-    int errno;
+    int errno = -1;
     struct v_fd* fd_s;
     struct v_fd* fd_s;
-    if ((errno = vfs_getfd(fd, &fd_s))) {
+    va_list args;
+
+    convert_valist(&args, _args);
+
+    if ((errno &= vfs_getfd(fd, &fd_s))) {
         goto done;
     }
 
         goto done;
     }
 
-    struct device* dev = (struct device*)fd_s->file->inode->data;
-    if (dev->magic != DEV_STRUCT_MAGIC) {
+    struct device* dev = resolve_device(fd_s->file->inode->data);
+    if (!valid_device_subtype_ref(dev, DEV_STRUCT)) {
         errno = ENODEV;
         goto done;
     }
 
         errno = ENODEV;
         goto done;
     }
 
-    if (!dev->exec_cmd) {
-        errno = EINVAL;
+    if (req == DEVIOIDENT) {
+        struct dev_info* devinfo = va_arg(args, struct dev_info*);
+        device_populate_info(dev, devinfo);
+        errno = 0;
+    }
+
+    if (!dev->ops.exec_cmd) {
+        errno = ENOTSUP;
         goto done;
     }
 
         goto done;
     }
 
-    errno = dev->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);