X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/8fce4520de1f257819b16f9253fa28dcdae743f4..8b8f49b713d64065775fe538232f8639083601bd:/lunaix-os/kernel/device/device.c diff --git a/lunaix-os/kernel/device/device.c b/lunaix-os/kernel/device/device.c index ae855ef..b25a9c2 100644 --- a/lunaix-os/kernel/device/device.c +++ b/lunaix-os/kernel/device/device.c @@ -8,25 +8,16 @@ #include #include -#include - #include #include static DEFINE_LLIST(root_list); -static volatile dev_t devid = 0; +static volatile u32_t devid = 0; -void -device_prepare(struct device* dev) -{ - dev->magic = DEV_STRUCT_MAGIC; - dev->dev_id = devid++; +struct devclass default_devclass = {}; - llist_init_head(&dev->children); -} - -static void +void device_setname_vargs(struct device* dev, char* fmt, va_list args) { size_t strlen = @@ -38,27 +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, - 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); + 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); @@ -66,71 +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, - 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, ...) +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_IFSEQ, 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, 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, args); + if (!dev) { + return NULL; + } + + device_create(dev, parent, type, underlay); - va_end(args); return dev; } -struct device* -device_addvol(struct device* parent, 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, args); + device_setname_vargs(dev, fmt, args); va_end(args); - return dev; } struct device* @@ -139,21 +103,23 @@ 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, 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; } struct device* -device_getbyid(struct llist_header* devlist, dev_t id) +device_getbyid(struct llist_header* devlist, u32_t id) { devlist = devlist ? devlist : &root_list; struct device *pos, *n; llist_for_each(pos, n, devlist, siblings) { - if (pos->dev_id == id) { + if (pos->dev_uid == id) { return pos; } } @@ -207,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;