X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/6f6da1abb22dff69dbb710bd2fd9d95f083f2b43..2236410f4582ab45ae8c384dd6eeeef5d10aab15:/lunaix-os/kernel/device/device.c diff --git a/lunaix-os/kernel/device/device.c b/lunaix-os/kernel/device/device.c index d26808a..8765755 100644 --- a/lunaix-os/kernel/device/device.c +++ b/lunaix-os/kernel/device/device.c @@ -8,9 +8,7 @@ #include #include -#include - -#include +#include #include static DEFINE_LLIST(root_list); @@ -20,20 +18,10 @@ 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 = - __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); @@ -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;