X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/642855f81fd03b9fd6540ac99c665b57b4b38cc8..8fce4520de1f257819b16f9253fa28dcdae743f4:/lunaix-os/kernel/device/device.c diff --git a/lunaix-os/kernel/device/device.c b/lunaix-os/kernel/device/device.c index 3647f10..ae855ef 100644 --- a/lunaix-os/kernel/device/device.c +++ b/lunaix-os/kernel/device/device.c @@ -1,4 +1,4 @@ -#include + #include #include #include @@ -8,19 +8,57 @@ #include #include +#include + +#include +#include + static DEFINE_LLIST(root_list); static volatile dev_t devid = 0; +void +device_prepare(struct device* dev) +{ + dev->magic = DEV_STRUCT_MAGIC; + dev->dev_id = devid++; + + llist_init_head(&dev->children); +} + +static 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); + + dev->name = HSTR(dev->name_val, strlen); + + hstr_rehash(&dev->name, HSTR_FULL_HASH); +} + +void +device_setname(struct device* dev, char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + + device_setname_vargs(dev, fmt, args); + + va_end(args); +} + struct device* -device_add(struct device* parent, - void* underlay, - char* name_fmt, - u32_t type, - va_list args) +device_add_vargs(struct device* parent, + void* underlay, + char* name_fmt, + u32_t type, + va_list args) { struct device* dev = vzalloc(sizeof(struct device)); + device_prepare(dev); + if (parent) { assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT); llist_append(&parent->children, &dev->siblings); @@ -28,19 +66,44 @@ device_add(struct device* parent, llist_append(&root_list, &dev->siblings); } - size_t strlen = - __ksprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args); + if (name_fmt) { + device_setname_vargs(dev, name_fmt, args); + } - dev->magic = DEV_STRUCT_MAGIC; - dev->dev_id = devid++; - dev->name = HSTR(dev->name_val, strlen); dev->parent = parent; dev->underlay = underlay; dev->dev_type = type; - hstr_rehash(&dev->name, HSTR_FULL_HASH); - llist_init_head(&dev->children); + 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, ...) +{ + va_list args; + va_start(args, name_fmt); + + struct device* dev = + device_add_vargs(parent, underlay, name_fmt, DEV_IFSEQ, args); + va_end(args); return dev; } @@ -51,7 +114,7 @@ device_addseq(struct device* parent, void* underlay, char* name_fmt, ...) va_start(args, name_fmt); struct device* dev = - device_add(parent, underlay, name_fmt, DEV_IFSEQ, args); + device_add_vargs(parent, underlay, name_fmt, DEV_IFSEQ, args); va_end(args); return dev; @@ -64,7 +127,7 @@ device_addvol(struct device* parent, void* underlay, char* name_fmt, ...) va_start(args, name_fmt); struct device* dev = - device_add(parent, underlay, name_fmt, DEV_IFVOL, args); + device_add_vargs(parent, underlay, name_fmt, DEV_IFVOL, args); va_end(args); return dev; @@ -76,7 +139,8 @@ device_addcat(struct device* parent, char* 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_vargs(parent, NULL, name_fmt, DEV_IFCAT, args); va_end(args); return dev; @@ -143,26 +207,50 @@ device_getbyoffset(struct device* root_dev, int offset) return NULL; } +static inline 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; + + if (!devinfo->dev_name.buf) { + return; + } + + struct device_def* def = devdef_byclass(&dev->class); + size_t buflen = devinfo->dev_name.buf_len; + + strncpy(devinfo->dev_name.buf, def->name, buflen); + devinfo->dev_name.buf[buflen - 1] = 0; +} + __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 (!dev->exec_cmd) { - errno = EINVAL; + 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; goto done; } - errno = dev->exec_cmd(dev, req, args); + errno &= dev->ops.exec_cmd(dev, req, args); done: return DO_STATUS_OR_RETURN(errno);