X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/5ea8e2ba737f903db81d49b56778e883634512a5..ab281459f86543c6b8c11ecccc3e743c3d69577e:/lunaix-os/kernel/device/device.c diff --git a/lunaix-os/kernel/device/device.c b/lunaix-os/kernel/device/device.c index 0990c56..6b901a6 100644 --- a/lunaix-os/kernel/device/device.c +++ b/lunaix-os/kernel/device/device.c @@ -1,8 +1,11 @@ #include #include +#include #include +#include #include #include +#include static DEFINE_LLIST(root_list); @@ -19,11 +22,15 @@ device_add(struct device* parent, 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 = - __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; @@ -31,7 +38,7 @@ device_add(struct device* parent, dev->dev_type = type; hstr_rehash(&dev->name, HSTR_FULL_HASH); - llist_append(&root_list, &dev->siblings); + llist_init_head(&dev->children); return dev; } @@ -90,9 +97,9 @@ device_getbyid(struct llist_header* devlist, dev_t id) } struct device* -device_getbyhname(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) { @@ -105,12 +112,12 @@ device_getbyhname(struct llist_header* devlist, struct hstr* name) } struct device* -device_getbyname(struct llist_header* devlist, const char* name, size_t len) +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(devlist, &hname); + return device_getbyhname(root_dev, &hname); } void @@ -121,9 +128,9 @@ device_remove(struct device* dev) } 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) @@ -133,4 +140,29 @@ device_getbyoffset(struct llist_header* devlist, int offset) } } 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