regression: elf loading
[lunaix-os.git] / lunaix-os / kernel / device / device.c
index aafdf0e5694b30d3566b2339dcbc2b89941aeb40..3647f10a87f778395cb4efba6f4080b622cd0f63 100644 (file)
@@ -1,29 +1,37 @@
 #include <klibc/stdio.h>
 #include <lunaix/device.h>
 #include <klibc/stdio.h>
 #include <lunaix/device.h>
+#include <lunaix/fs.h>
 #include <lunaix/fs/twifs.h>
 #include <lunaix/fs/twifs.h>
+#include <lunaix/ioctl.h>
 #include <lunaix/mm/valloc.h>
 #include <lunaix/spike.h>
 #include <lunaix/mm/valloc.h>
 #include <lunaix/spike.h>
+#include <lunaix/syscall.h>
+#include <lunaix/syscall_utils.h>
 
 static DEFINE_LLIST(root_list);
 
 static volatile dev_t devid = 0;
 
 struct device*
 
 static DEFINE_LLIST(root_list);
 
 static volatile dev_t devid = 0;
 
 struct device*
-__device_add(struct device* parent,
-             void* underlay,
-             char* name_fmt,
-             uint32_t type,
-             va_list args)
+device_add(struct device* parent,
+           void* underlay,
+           char* name_fmt,
+           u32_t type,
+           va_list args)
 {
     struct device* dev = vzalloc(sizeof(struct device));
 
     if (parent) {
         assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
 {
     struct device* dev = vzalloc(sizeof(struct device));
 
     if (parent) {
         assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
+        llist_append(&parent->children, &dev->siblings);
+    } else {
+        llist_append(&root_list, &dev->siblings);
     }
 
     size_t strlen =
     }
 
     size_t strlen =
-      __sprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args);
+      __ksprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args);
 
 
+    dev->magic = DEV_STRUCT_MAGIC;
     dev->dev_id = devid++;
     dev->name = HSTR(dev->name_val, strlen);
     dev->parent = parent;
     dev->dev_id = devid++;
     dev->name = HSTR(dev->name_val, strlen);
     dev->parent = parent;
@@ -31,7 +39,7 @@ __device_add(struct device* parent,
     dev->dev_type = type;
 
     hstr_rehash(&dev->name, HSTR_FULL_HASH);
     dev->dev_type = type;
 
     hstr_rehash(&dev->name, HSTR_FULL_HASH);
-    llist_append(&root_list, &dev->siblings);
+    llist_init_head(&dev->children);
 
     return dev;
 }
 
     return dev;
 }
@@ -43,7 +51,7 @@ device_addseq(struct device* parent, void* underlay, char* name_fmt, ...)
     va_start(args, name_fmt);
 
     struct device* dev =
     va_start(args, name_fmt);
 
     struct device* dev =
-      __device_add(parent, underlay, name_fmt, DEV_IFSEQ, args);
+      device_add(parent, underlay, name_fmt, DEV_IFSEQ, args);
 
     va_end(args);
     return dev;
 
     va_end(args);
     return dev;
@@ -56,7 +64,7 @@ device_addvol(struct device* parent, void* underlay, char* name_fmt, ...)
     va_start(args, name_fmt);
 
     struct device* dev =
     va_start(args, name_fmt);
 
     struct device* dev =
-      __device_add(parent, underlay, name_fmt, DEV_IFVOL, args);
+      device_add(parent, underlay, name_fmt, DEV_IFVOL, args);
 
     va_end(args);
     return dev;
 
     va_end(args);
     return dev;
@@ -68,7 +76,7 @@ device_addcat(struct device* 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, NULL, name_fmt, DEV_IFCAT, args);
+    struct device* dev = device_add(parent, NULL, name_fmt, DEV_IFCAT, args);
 
     va_end(args);
     return dev;
 
     va_end(args);
     return dev;
@@ -90,9 +98,9 @@ device_getbyid(struct llist_header* devlist, dev_t id)
 }
 
 struct device*
 }
 
 struct device*
-device_getbyname(struct llist_header* devlist, struct hstr* name)
+device_getbyhname(struct device* root_dev, struct hstr* name)
 {
 {
-    devlist = devlist ? devlist : &root_list;
+    struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
     struct device *pos, *n;
     llist_for_each(pos, n, devlist, siblings)
     {
     struct device *pos, *n;
     llist_for_each(pos, n, devlist, siblings)
     {
@@ -104,6 +112,15 @@ device_getbyname(struct llist_header* devlist, struct hstr* name)
     return NULL;
 }
 
     return NULL;
 }
 
+struct device*
+device_getbyname(struct device* 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* dev)
 {
 void
 device_remove(struct device* dev)
 {
@@ -112,9 +129,9 @@ device_remove(struct device* dev)
 }
 
 struct device*
 }
 
 struct device*
-device_getbyoffset(struct llist_header* devlist, int offset)
+device_getbyoffset(struct device* root_dev, int offset)
 {
 {
-    devlist = devlist ? devlist : &root_list;
+    struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
     struct device *pos, *n;
     int off = 0;
     llist_for_each(pos, n, devlist, siblings)
     struct device *pos, *n;
     int off = 0;
     llist_for_each(pos, n, devlist, siblings)
@@ -124,4 +141,29 @@ device_getbyoffset(struct llist_header* devlist, int offset)
         }
     }
     return NULL;
         }
     }
     return NULL;
+}
+
+__DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
+{
+    int errno;
+    struct v_fd* fd_s;
+    if ((errno = vfs_getfd(fd, &fd_s))) {
+        goto done;
+    }
+
+    struct device* dev = (struct device*)fd_s->file->inode->data;
+    if (dev->magic != DEV_STRUCT_MAGIC) {
+        errno = ENODEV;
+        goto done;
+    }
+
+    if (!dev->exec_cmd) {
+        errno = EINVAL;
+        goto done;
+    }
+
+    errno = dev->exec_cmd(dev, req, args);
+
+done:
+    return DO_STATUS_OR_RETURN(errno);
 }
\ No newline at end of file
 }
\ No newline at end of file