feat: standard vga support (mode switching, framebuffer remapping)
[lunaix-os.git] / lunaix-os / kernel / device / device.c
index d26808a4646dbe453ff276e6f2836b6e1e203f6d..b25a9c203594bab14d2d2892ca04bbe39d5e5796 100644 (file)
@@ -8,8 +8,6 @@
 #include <lunaix/syscall.h>
 #include <lunaix/syscall_utils.h>
 
-#include <usr/lunaix/device.h>
-
 #include <klibc/stdio.h>
 #include <klibc/string.h>
 
@@ -20,16 +18,6 @@ static volatile u32_t devid = 0;
 struct devclass default_devclass = {};
 
 void
-device_prepare(struct device* dev, struct devclass* class)
-{
-    dev->magic = DEV_STRUCT_MAGIC;
-    dev->dev_uid = devid++;
-    dev->class = class ? class : &default_devclass;
-
-    llist_init_head(&dev->children);
-}
-
-static void
 device_setname_vargs(struct device* dev, char* fmt, va_list args)
 {
     size_t strlen =
@@ -41,28 +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,
-                 struct devclass* class,
-                 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, class);
+    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);
@@ -70,84 +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,
-           struct devclass* class,
-           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, class, args);
-
     va_end(args);
-    return dev;
 }
 
-struct device*
-device_addsys(struct device* parent,
-              struct devclass* class,
-              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_IFSYS, class, 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,
-              struct devclass* class,
-              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, class, args);
+    if (!dev) {
+        return NULL;
+    }
+
+    device_create(dev, parent, type, underlay);
 
-    va_end(args);
     return dev;
 }
 
-struct device*
-device_addvol(struct device* parent,
-              struct devclass* class,
-              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, class, args);
+    device_setname_vargs(dev, fmt, args);
 
     va_end(args);
-    return dev;
 }
 
 struct device*
@@ -156,8 +103,10 @@ 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, NULL, 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;
@@ -224,24 +173,34 @@ device_getbyoffset(struct device* root_dev, int offset)
     return NULL;
 }
 
-static inline void
+void
 device_populate_info(struct device* dev, struct dev_info* devinfo)
 {
-    devinfo->dev_id.meta = dev->class->meta;
-    devinfo->dev_id.device = dev->class->device;
-    devinfo->dev_id.variant = dev->class->variant;
+    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_byclass(dev->class);
+    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 = -1;