-
#include <lunaix/device.h>
#include <lunaix/fs.h>
#include <lunaix/fs/twifs.h>
#include <lunaix/spike.h>
#include <lunaix/syscall.h>
#include <lunaix/syscall_utils.h>
+#include <lunaix/owloysius.h>
-#include <usr/lunaix/device.h>
-
-#include <klibc/stdio.h>
+#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_prepare(struct device* dev, struct devclass* class)
+device_setname_vargs(struct device_meta* dev, char* fmt, va_list args)
{
- dev->magic = DEV_STRUCT_MAGIC;
- dev->dev_uid = devid++;
- dev->class = class ? class : &default_devclass;
-
- llist_init_head(&dev->children);
+ ksnprintfv(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
+ changeling_setname(dev_mobj(dev), dev->name_val);
}
-static void
-device_setname_vargs(struct device* dev, char* fmt, va_list args)
+void
+device_register_generic(struct device_meta* devm, struct devclass* class,
+ char* fmt, ...)
{
- size_t strlen =
- __ksprintf_internal(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
+ va_list args;
+ morph_t* morphed, *parent;
+
+ morphed = &devm->mobj;
+ va_start(args, fmt);
+
+ if (fmt) {
+ device_setname_vargs(devm, fmt, args);
+ }
+
+ if (class && morph_type_of(morphed, device_morpher))
+ {
+ struct device* dev = to_dev(devm);
+ dev->ident = (struct devident) {
+ .fn_grp = class->fn_grp,
+ .unique = DEV_UNIQUE(class->device, class->variant)
+ };
+ }
- dev->name = HSTR(dev->name_val, strlen);
+ parent = morphed->parent;
+ if (parent) {
+ changeling_attach(parent, morphed);
+ }
- hstr_rehash(&dev->name, HSTR_FULL_HASH);
+ va_end(args);
}
void
-device_setname(struct device* dev, char* fmt, ...)
+device_create(struct device* dev, struct device_meta* parent,
+ u32_t type, void* underlay)
{
- va_list args;
- va_start(args, fmt);
-
- device_setname_vargs(dev, fmt, args);
+ dev->underlay = underlay;
+ dev->dev_type = type;
- va_end(args);
+ llist_init_head(&dev->potentium);
+ mutex_init(&dev->lock);
+ iopoll_init_evt_q(&dev->pollers);
}
struct device*
-device_add_vargs(struct device* parent,
- void* underlay,
- char* name_fmt,
- u32_t type,
- struct devclass* class,
- va_list args)
+device_alloc(struct device_meta* parent, u32_t type, void* underlay)
{
struct device* dev = vzalloc(sizeof(struct device));
- device_prepare(dev, class);
-
- if (parent) {
- assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
- llist_append(&parent->children, &dev->siblings);
- } else {
- llist_append(&root_list, &dev->siblings);
+ if (!dev) {
+ return NULL;
}
- if (name_fmt) {
- device_setname_vargs(dev, name_fmt, args);
- }
-
- dev->parent = parent;
- dev->underlay = underlay;
- dev->dev_type = type;
+ device_create(dev, parent, type, underlay);
+ changeling_morph(dev_morph(parent), dev->mobj, NULL, device_morpher);
return dev;
}
-struct device*
-device_add(struct device* parent,
- struct devclass* class,
- void* underlay,
- u32_t type,
- char* name_fmt,
- ...)
+struct device_alias*
+device_alloc_alias(struct device_meta* parent, struct device_meta* aliased)
{
- va_list args;
- va_start(args, name_fmt);
+ struct device_alias* dev = vzalloc(sizeof(struct device_alias));
- struct device* dev =
- device_add_vargs(parent, underlay, name_fmt, type, class, args);
+ if (!dev) {
+ return NULL;
+ }
+
+ dev->alias = aliased;
+ changeling_ref(dev_mobj(aliased));
+ changeling_morph(dev_morph(parent), dev->mobj, NULL, devalias_morpher);
- va_end(args);
return dev;
}
-struct device*
-device_addsys(struct device* parent,
- struct devclass* class,
- void* underlay,
- char* name_fmt,
- ...)
+struct device_cat*
+device_alloc_cat(struct device_meta* parent)
{
- va_list args;
- va_start(args, name_fmt);
+ struct device_cat* dev = vzalloc(sizeof(struct device_cat));
- struct device* dev =
- device_add_vargs(parent, underlay, name_fmt, DEV_IFSYS, class, args);
+ if (!dev) {
+ return NULL;
+ }
+
+ changeling_morph(dev_morph(parent), dev->mobj, NULL, devcat_morpher);
- va_end(args);
return dev;
}
-struct device*
-device_addseq(struct device* parent,
- struct devclass* class,
- void* underlay,
- char* name_fmt,
- ...)
+void
+device_setname(struct device_meta* 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_IFSEQ, class, args);
+ device_setname_vargs(dev, fmt, args);
va_end(args);
- return dev;
}
-struct device*
-device_addvol(struct device* parent,
- struct devclass* class,
- 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);
- struct device* dev =
- device_add_vargs(parent, underlay, name_fmt, DEV_IFVOL, class, 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;
}
-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);
- struct device* dev =
- device_add_vargs(parent, NULL, name_fmt, DEV_IFCAT, NULL, 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;
}
-struct device*
-device_getbyid(struct llist_header* devlist, u32_t id)
+void
+device_remove(struct device_meta* dev)
{
- devlist = devlist ? devlist : &root_list;
- struct device *pos, *n;
- llist_for_each(pos, n, devlist, siblings)
- {
- if (pos->dev_uid == id) {
- return pos;
- }
+ changeling_isolate(&dev->mobj);
+ vfree(dev);
+}
+
+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;
}
- return NULL;
+ 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_getbyhname(struct device* root_dev, struct hstr* name)
+morph_t*
+resolve_device_morph(void* maybe_dev)
{
- struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
- struct device *pos, *n;
- llist_for_each(pos, n, devlist, siblings)
+ struct device_alias* da = NULL;
+ morph_t *morphed;
+
+ morphed = morphed_ptr(maybe_dev);
+
+ if (!is_changeling(morphed)) {
+ return NULL;
+ }
+
+ if (morph_type_of(morphed, device_morpher))
{
- if (HSTR_EQ(&pos->name, name)) {
- return pos;
- }
+ return morphed;
}
- return NULL;
-}
+ if (morph_type_of(morphed, devcat_morpher))
+ {
+ return morphed;
+ }
-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);
+ while(morph_type_of(morphed, devalias_morpher))
+ {
+ da = changeling_reveal(morphed, devalias_morpher);
+ morphed = &da->alias->mobj;
+ }
- return device_getbyhname(root_dev, &hname);
+ return da ? morphed : NULL;
}
void
-device_remove(struct device* dev)
+device_alert_poller(struct device* dev, int poll_evt)
{
- llist_delete(&dev->siblings);
- vfree(dev);
+ dev->poll_evflags = poll_evt;
+ iopoll_wake_pollers(&dev->pollers);
}
-struct device*
-device_getbyoffset(struct device* root_dev, int offset)
+void
+device_chain_loader(struct device_def* def, devdef_ldfn fn)
{
- 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)
- {
- if (off++ >= offset) {
- return pos;
- }
+ 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;
}
- return NULL;
+
+ def->load_chain = node;
}
-static inline void
-device_populate_info(struct device* dev, struct dev_info* devinfo)
+void
+device_chain_load_once(struct device_def* def)
{
- devinfo->dev_id.meta = dev->class->meta;
- devinfo->dev_id.device = dev->class->device;
- devinfo->dev_id.variant = dev->class->variant;
+ struct device_ldfn_chain *node, *next;
- if (!devinfo->dev_name.buf) {
+ 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;
}
- struct device_def* def = devdef_byclass(dev->class);
- size_t buflen = devinfo->dev_name.buf_len;
+ if (!def->create) {
+ return;
+ }
- strncpy(devinfo->dev_name.buf, def->name, buflen);
- devinfo->dev_name.buf[buflen - 1] = 0;
+ def->create(def, NULL);
+
}
-__DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
+__DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, sc_va_list, _args)
{
int errno = -1;
struct v_fd* fd_s;
+ va_list args;
+
+ convert_valist(&args, _args);
+
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;
+ struct device* dev = resolve_device(fd_s->file->inode->data);
+ if (!dev) {
+ errno = ENODEV;
goto done;
}
}
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);
-}
\ 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