refactor: make pci device driver loading passive, pci bus scanner will not load them...
[lunaix-os.git] / lunaix-os / kernel / device / device.c
index b794334628ca696d4f44f0f8ab15722ac5fa5aaa..87657551eb50a0e5a3ac42a257d712ad8e011344 100644 (file)
@@ -1,4 +1,4 @@
-#include <klibc/stdio.h>
+
 #include <lunaix/device.h>
 #include <lunaix/fs.h>
 #include <lunaix/fs/twifs.h>
@@ -8,24 +8,20 @@
 #include <lunaix/syscall.h>
 #include <lunaix/syscall_utils.h>
 
-static DEFINE_LLIST(root_list);
+#include <klibc/strfmt.h>
+#include <klibc/string.h>
 
-static volatile dev_t devid = 0;
+static DEFINE_LLIST(root_list);
 
-void
-device_prepare(struct device* dev)
-{
-    dev->magic = DEV_STRUCT_MAGIC;
-    dev->dev_id = devid++;
+static volatile u32_t devid = 0;
 
-    llist_init_head(&dev->children);
-}
+struct devclass default_devclass = {};
 
-static void
+void
 device_setname_vargs(struct device* dev, char* fmt, va_list args)
 {
     size_t strlen =
-      __ksprintf_internal(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
+      ksnprintfv(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
 
     dev->name = HSTR(dev->name_val, strlen);
 
@@ -33,27 +29,24 @@ device_setname_vargs(struct device* dev, char* fmt, va_list args)
 }
 
 void
-device_setname(struct device* dev, char* fmt, ...)
+device_register(struct device* dev, struct devclass* class, char* fmt, ...)
 {
     va_list args;
     va_start(args, fmt);
 
-    device_setname_vargs(dev, fmt, args);
-
-    va_end(args);
-}
+    if (fmt) {
+        device_setname_vargs(dev, fmt, args);
+    }
 
-struct device*
-device_add_vargs(struct device* parent,
-                 void* underlay,
-                 char* name_fmt,
-                 u32_t type,
-                 va_list args)
-{
-    struct device* dev = vzalloc(sizeof(struct device));
+    if (class) {
+        dev->ident = (struct devident){ .fn_grp = class->fn_grp,
+                                        .unique = DEV_UNIQUE(class->device,
+                                                             class->variant) };
+    }
 
-    device_prepare(dev);
+    dev->dev_uid = devid++;
 
+    struct device* parent = dev->parent;
     if (parent) {
         assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
         llist_append(&parent->children, &dev->siblings);
@@ -61,71 +54,47 @@ device_add_vargs(struct device* parent,
         llist_append(&root_list, &dev->siblings);
     }
 
-    if (name_fmt) {
-        device_setname_vargs(dev, name_fmt, args);
-    }
-
-    dev->parent = parent;
-    dev->underlay = underlay;
-    dev->dev_type = type;
-
-    return dev;
-}
-
-struct device*
-device_add(struct device* parent,
-           void* underlay,
-           u32_t type,
-           char* name_fmt,
-           ...)
-{
-    va_list args;
-    va_start(args, name_fmt);
-
-    struct device* dev =
-      device_add_vargs(parent, underlay, name_fmt, type, args);
-
     va_end(args);
-    return dev;
 }
 
-struct device*
-device_addsys(struct device* parent, void* underlay, char* name_fmt, ...)
+void
+device_create(struct device* dev,
+              struct device* parent,
+              u32_t type,
+              void* underlay)
 {
-    va_list args;
-    va_start(args, name_fmt);
-
-    struct device* dev =
-      device_add_vargs(parent, underlay, name_fmt, DEV_IFSEQ, args);
+    dev->magic = DEV_STRUCT_MAGIC;
+    dev->underlay = underlay;
+    dev->dev_type = type;
+    dev->parent = parent;
 
-    va_end(args);
-    return dev;
+    llist_init_head(&dev->children);
+    mutex_init(&dev->lock);
 }
 
 struct device*
-device_addseq(struct device* parent, void* underlay, char* name_fmt, ...)
+device_alloc(struct device* parent, u32_t type, void* underlay)
 {
-    va_list args;
-    va_start(args, name_fmt);
+    struct device* dev = vzalloc(sizeof(struct device));
 
-    struct device* dev =
-      device_add_vargs(parent, underlay, name_fmt, DEV_IFSEQ, args);
+    if (!dev) {
+        return NULL;
+    }
+
+    device_create(dev, parent, type, underlay);
 
-    va_end(args);
     return dev;
 }
 
-struct device*
-device_addvol(struct device* parent, void* underlay, char* name_fmt, ...)
+void
+device_setname(struct device* dev, char* fmt, ...)
 {
     va_list args;
-    va_start(args, name_fmt);
+    va_start(args, fmt);
 
-    struct device* dev =
-      device_add_vargs(parent, underlay, name_fmt, DEV_IFVOL, args);
+    device_setname_vargs(dev, fmt, args);
 
     va_end(args);
-    return dev;
 }
 
 struct device*
@@ -134,21 +103,23 @@ device_addcat(struct device* parent, char* name_fmt, ...)
     va_list args;
     va_start(args, name_fmt);
 
-    struct device* dev =
-      device_add_vargs(parent, NULL, name_fmt, DEV_IFCAT, args);
+    struct device* dev = device_alloc(parent, DEV_IFCAT, NULL);
+
+    device_setname_vargs(dev, name_fmt, args);
+    device_register(dev, NULL, NULL);
 
     va_end(args);
     return dev;
 }
 
 struct device*
-device_getbyid(struct llist_header* devlist, dev_t id)
+device_getbyid(struct llist_header* devlist, u32_t id)
 {
     devlist = devlist ? devlist : &root_list;
     struct device *pos, *n;
     llist_for_each(pos, n, devlist, siblings)
     {
-        if (pos->dev_id == id) {
+        if (pos->dev_uid == id) {
             return pos;
         }
     }
@@ -202,26 +173,60 @@ device_getbyoffset(struct device* root_dev, int offset)
     return NULL;
 }
 
+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*
+device_cast(void* obj)
+{
+    struct device* dev = (struct device*)obj;
+    if (dev && dev->magic == DEV_STRUCT_MAGIC) {
+        return dev;
+    }
+
+    return NULL;
+}
+
 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
 {
-    int errno;
+    int errno = -1;
     struct v_fd* fd_s;
-    if ((errno = vfs_getfd(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;
+        errno &= ENODEV;
         goto done;
     }
 
+    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;
+        errno &= ENOTSUP;
         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);