From 77057f5a28c94d8ed1779b829d9ef0ad0a95db44 Mon Sep 17 00:00:00 2001 From: Patrick Mochel Date: Tue, 29 Oct 2002 23:15:24 -0800 Subject: driver model: convert devices to use kobjects and sysfs. - Define a device subsystem and register it on startup. - Fill it in with fields converted from driverfs-style fields. - Convert device_{create,remove}_file() to create files in sysfs. - Add default device attributes to device subsystem. - Make sure devices get proper kobject familial pointers and that the kobjects are registered. --- include/linux/device.h | 2 ++ include/linux/driverfs_fs.h | 7 ++----- include/linux/kobject.h | 4 +++- include/linux/sysfs.h | 12 +++++------- 4 files changed, 12 insertions(+), 13 deletions(-) (limited to 'include/linux') diff --git a/include/linux/device.h b/include/linux/device.h index 2e0948e96d68..ca7bf83886ed 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -29,6 +29,7 @@ #include #include #include +#include #define DEVICE_NAME_SIZE 80 #define DEVICE_ID_SIZE 32 @@ -275,6 +276,7 @@ struct device { struct list_head intf_list; struct device * parent; + struct kobject kobj; char name[DEVICE_NAME_SIZE]; /* descriptive ascii string */ char bus_id[BUS_ID_SIZE]; /* position on parent bus */ diff --git a/include/linux/driverfs_fs.h b/include/linux/driverfs_fs.h index b4270e947a1e..af7dc715855d 100644 --- a/include/linux/driverfs_fs.h +++ b/include/linux/driverfs_fs.h @@ -26,6 +26,8 @@ #ifndef _DRIVER_FS_H_ #define _DRIVER_FS_H_ +#include + struct driver_dir_entry; struct attribute; @@ -43,11 +45,6 @@ struct driver_dir_entry { struct driverfs_ops * ops; }; -struct attribute { - char * name; - mode_t mode; -}; - extern int driverfs_create_dir(struct driver_dir_entry *, struct driver_dir_entry *); diff --git a/include/linux/kobject.h b/include/linux/kobject.h index d2f0629a6189..40de16d0a227 100644 --- a/include/linux/kobject.h +++ b/include/linux/kobject.h @@ -12,8 +12,10 @@ #include #include +#define KOBJ_NAME_LEN 16 + struct kobject { - char name[16]; + char name[KOBJ_NAME_LEN]; atomic_t refcount; struct list_head entry; struct kobject * parent; diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index 7a46c9f0c308..29c3eb8d2c9e 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -9,20 +9,18 @@ #ifndef _SYSFS_H_ #define _SYSFS_H_ -struct driver_dir_entry; -struct attribute; struct kobject; -struct sysfs_ops { - ssize_t (*show)(struct kobject *, struct attribute *,char *, size_t, loff_t); - ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t, loff_t); -}; - struct attribute { char * name; mode_t mode; }; +struct sysfs_ops { + ssize_t (*show)(struct kobject *, struct attribute *,char *, size_t, loff_t); + ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t, loff_t); +}; + extern int sysfs_create_dir(struct kobject *); -- cgit v1.2.3 From 289609f07f4e96ef139af61bd2ee3d8f1592ba17 Mon Sep 17 00:00:00 2001 From: Patrick Mochel Date: Wed, 30 Oct 2002 00:04:57 -0800 Subject: driver model: convert bus drivers to use kobjects and sysfs. - create bus subsystem and register on start up. - add struct subsystem to struct bus type to get it in the object hierarchy. - add device and driver subsystems to struct bus type to give it hierarchy like it previously had. - convert bus show()/store() callbacks to know about struct kobject. - convert bus file creation/removal to use struct kobject. - create symlinks from to device directories, like they had in driverfs. --- drivers/base/bus.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++-- drivers/base/fs/bus.c | 78 ------------------------------------------- include/linux/device.h | 3 ++ 3 files changed, 91 insertions(+), 80 deletions(-) (limited to 'include/linux') diff --git a/drivers/base/bus.c b/drivers/base/bus.c index 8d0bbb5c9256..57fd83004240 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "base.h" static LIST_HEAD(bus_driver_list); @@ -19,6 +20,69 @@ static LIST_HEAD(bus_driver_list); #define to_dev(node) container_of(node,struct device,bus_list) #define to_drv(node) container_of(node,struct device_driver,bus_list) +#define to_bus_attr(_attr) container_of(_attr,struct bus_attribute,attr) +#define to_bus(obj) container_of(obj,struct bus_type,subsys.kobj) + +/* + * sysfs bindings for buses + */ + + +static ssize_t +bus_attr_show(struct kobject * kobj, struct attribute * attr, + char * buf, size_t count, loff_t off) +{ + struct bus_attribute * bus_attr = to_bus_attr(attr); + struct bus_type * bus = to_bus(kobj); + ssize_t ret = 0; + + if (bus_attr->show) + ret = bus_attr->show(bus,buf,count,off); + return ret; +} + +static ssize_t +bus_attr_store(struct kobject * kobj, struct attribute * attr, + const char * buf, size_t count, loff_t off) +{ + struct bus_attribute * bus_attr = to_bus_attr(attr); + struct bus_type * bus = to_bus(kobj); + ssize_t ret = 0; + + if (bus_attr->store) + ret = bus_attr->store(bus,buf,count,off); + return ret; +} + +static struct sysfs_ops bus_sysfs_ops = { + .show = bus_attr_show, + .store = bus_attr_store, +}; + +int bus_create_file(struct bus_type * bus, struct bus_attribute * attr) +{ + int error; + if (get_bus(bus)) { + error = sysfs_create_file(&bus->subsys.kobj,&attr->attr); + put_bus(bus); + } else + error = -EINVAL; + return error; +} + +void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr) +{ + if (get_bus(bus)) { + sysfs_remove_file(&bus->subsys.kobj,&attr->attr); + put_bus(bus); + } +} + +struct subsystem bus_subsys = { + .kobj = { .name = "bus" }, + .sysfs_ops = &bus_sysfs_ops, +}; + /** * bus_for_each_dev - walk list of devices and do something to each * @bus: bus in question @@ -206,7 +270,7 @@ int bus_add_device(struct device * dev) list_add_tail(&dev->bus_list,&dev->bus->devices); device_attach(dev); up_write(&dev->bus->rwsem); - device_bus_link(dev); + sysfs_create_link(&bus->devsubsys.kobj,&dev->kobj,dev->bus_id); } return 0; } @@ -221,9 +285,9 @@ int bus_add_device(struct device * dev) void bus_remove_device(struct device * dev) { if (dev->bus) { + sysfs_remove_link(&dev->bus->devsubsys.kobj,dev->bus_id); down_write(&dev->bus->rwsem); pr_debug("bus %s: remove device %s\n",dev->bus->name,dev->bus_id); - device_remove_symlink(&dev->bus->device_dir,dev->bus_id); device_detach(dev); list_del_init(&dev->bus_list); up_write(&dev->bus->rwsem); @@ -286,6 +350,18 @@ int bus_register(struct bus_type * bus) atomic_set(&bus->refcount,2); bus->present = 1; + strncpy(bus->subsys.kobj.name,bus->name,KOBJ_NAME_LEN); + bus->subsys.parent = &bus_subsys; + subsystem_register(&bus->subsys); + + snprintf(bus->devsubsys.kobj.name,KOBJ_NAME_LEN,"devices"); + bus->devsubsys.parent = &bus->subsys; + subsystem_register(&bus->devsubsys); + + snprintf(bus->drvsubsys.kobj.name,KOBJ_NAME_LEN,"drivers"); + bus->drvsubsys.parent = &bus->subsys; + subsystem_register(&bus->drvsubsys); + spin_lock(&device_lock); list_add_tail(&bus->node,&bus_driver_list); spin_unlock(&device_lock); @@ -309,6 +385,13 @@ void bus_unregister(struct bus_type * bus) put_bus(bus); } +static int __init bus_subsys_init(void) +{ + return subsystem_register(&bus_subsys); +} + +core_initcall(bus_subsys_init); + EXPORT_SYMBOL(bus_for_each_dev); EXPORT_SYMBOL(bus_for_each_drv); EXPORT_SYMBOL(bus_add_device); @@ -317,3 +400,6 @@ EXPORT_SYMBOL(bus_register); EXPORT_SYMBOL(bus_unregister); EXPORT_SYMBOL(get_bus); EXPORT_SYMBOL(put_bus); + +EXPORT_SYMBOL(bus_create_file); +EXPORT_SYMBOL(bus_remove_file); diff --git a/drivers/base/fs/bus.c b/drivers/base/fs/bus.c index bd6b92442f81..7c95233d9249 100644 --- a/drivers/base/fs/bus.c +++ b/drivers/base/fs/bus.c @@ -6,86 +6,10 @@ static struct driver_dir_entry bus_dir; -#define to_bus_attr(_attr) container_of(_attr,struct bus_attribute,attr) - -#define to_bus(dir) container_of(dir,struct bus_type,dir) - - -/* driverfs ops for device attribute files */ - -static int -bus_attr_open(struct driver_dir_entry * dir) -{ - struct bus_type * bus = to_bus(dir); - get_bus(bus); - return 0; -} - -static int -bus_attr_close(struct driver_dir_entry * dir) -{ - struct bus_type * bus = to_bus(dir); - put_bus(bus); - return 0; -} - -static ssize_t -bus_attr_show(struct driver_dir_entry * dir, struct attribute * attr, - char * buf, size_t count, loff_t off) -{ - struct bus_attribute * bus_attr = to_bus_attr(attr); - struct bus_type * bus = to_bus(dir); - ssize_t ret = 0; - - if (bus_attr->show) - ret = bus_attr->show(bus,buf,count,off); - return ret; -} - -static ssize_t -bus_attr_store(struct driver_dir_entry * dir, struct attribute * attr, - const char * buf, size_t count, loff_t off) -{ - struct bus_attribute * bus_attr = to_bus_attr(attr); - struct bus_type * bus = to_bus(dir); - ssize_t ret = 0; - - if (bus_attr->store) - ret = bus_attr->store(bus,buf,count,off); - return ret; -} - -static struct driverfs_ops bus_attr_ops = { - .open = bus_attr_open, - .close = bus_attr_close, - .show = bus_attr_show, - .store = bus_attr_store, -}; - -int bus_create_file(struct bus_type * bus, struct bus_attribute * attr) -{ - int error; - if (get_bus(bus)) { - error = driverfs_create_file(&attr->attr,&bus->dir); - put_bus(bus); - } else - error = -EINVAL; - return error; -} - -void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr) -{ - if (get_bus(bus)) { - driverfs_remove_file(&bus->dir,attr->attr.name); - put_bus(bus); - } -} - int bus_make_dir(struct bus_type * bus) { int error; bus->dir.name = bus->name; - bus->dir.ops = &bus_attr_ops; error = device_create_dir(&bus->dir,&bus_dir); if (!error) { @@ -119,5 +43,3 @@ static int __init bus_init(void) core_initcall(bus_init); -EXPORT_SYMBOL(bus_create_file); -EXPORT_SYMBOL(bus_remove_file); diff --git a/include/linux/device.h b/include/linux/device.h index ca7bf83886ed..66d4f313a809 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -66,6 +66,9 @@ struct bus_type { atomic_t refcount; u32 present; + struct subsystem subsys; + struct subsystem drvsubsys; + struct subsystem devsubsys; struct list_head node; struct list_head devices; struct list_head drivers; -- cgit v1.2.3 From 8de57ed6e13a1625695c016bf455ec7ebff68031 Mon Sep 17 00:00:00 2001 From: Patrick Mochel Date: Wed, 30 Oct 2002 00:35:49 -0800 Subject: driver model: convert drivers to use kobject and sysfs. - add kobject to struct device_driver and register it when drivers are registered (as member's of their bus's driver subsystem). - convert driverfs callbacks to know about struct kobject. - create links from drivers' directories to devices' directories. - don't even make driverfs directories for drivers anymore. --- drivers/base/bus.c | 46 ++++++++++++++++++++++++++-- drivers/base/driver.c | 32 +++++++++++++++++++- drivers/base/fs/driver.c | 78 ------------------------------------------------ include/linux/device.h | 1 + 4 files changed, 76 insertions(+), 81 deletions(-) (limited to 'include/linux') diff --git a/drivers/base/bus.c b/drivers/base/bus.c index 57fd83004240..8428b8f1acd3 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -24,7 +24,47 @@ static LIST_HEAD(bus_driver_list); #define to_bus(obj) container_of(obj,struct bus_type,subsys.kobj) /* - * sysfs bindings for buses + * sysfs bindings for drivers + */ + +#define to_drv_attr(_attr) container_of(_attr,struct driver_attribute,attr) +#define to_driver(obj) container_of(obj, struct device_driver, kobj) + + +static ssize_t +drv_attr_show(struct kobject * kobj, struct attribute * attr, + char * buf, size_t count, loff_t off) +{ + struct driver_attribute * drv_attr = to_drv_attr(attr); + struct device_driver * drv = to_driver(kobj); + ssize_t ret = 0; + + if (drv_attr->show) + ret = drv_attr->show(drv,buf,count,off); + return ret; +} + +static ssize_t +drv_attr_store(struct kobject * kobj, struct attribute * attr, + const char * buf, size_t count, loff_t off) +{ + struct driver_attribute * drv_attr = to_drv_attr(attr); + struct device_driver * drv = to_driver(kobj); + ssize_t ret = 0; + + if (drv_attr->store) + ret = drv_attr->store(drv,buf,count,off); + return ret; +} + +static struct sysfs_ops driver_sysfs_ops = { + .show = drv_attr_show, + .store = drv_attr_store, +}; + + +/* + * sysfs bindings for drivers */ @@ -156,6 +196,7 @@ static void attach(struct device * dev) pr_debug("bound device '%s' to driver '%s'\n", dev->bus_id,dev->driver->name); list_add_tail(&dev->driver_list,&dev->driver->devices); + sysfs_create_link(&dev->driver->kobj,&dev->kobj,dev->kobj.name); } static int bus_match(struct device * dev, struct device_driver * drv) @@ -226,6 +267,7 @@ static int driver_attach(struct device_driver * drv) static void detach(struct device * dev, struct device_driver * drv) { if (drv) { + sysfs_remove_link(&drv->kobj,dev->kobj.name); list_del_init(&dev->driver_list); devclass_remove_device(dev); if (drv->remove) @@ -304,7 +346,6 @@ int bus_add_driver(struct device_driver * drv) list_add_tail(&drv->bus_list,&bus->drivers); driver_attach(drv); up_write(&bus->rwsem); - driver_make_dir(drv); } return 0; } @@ -360,6 +401,7 @@ int bus_register(struct bus_type * bus) snprintf(bus->drvsubsys.kobj.name,KOBJ_NAME_LEN,"drivers"); bus->drvsubsys.parent = &bus->subsys; + bus->drvsubsys.sysfs_ops = &driver_sysfs_ops; subsystem_register(&bus->drvsubsys); spin_lock(&device_lock); diff --git a/drivers/base/driver.c b/drivers/base/driver.c index 4bf4a005b918..f940cc4c5165 100644 --- a/drivers/base/driver.c +++ b/drivers/base/driver.c @@ -12,6 +12,29 @@ #define to_dev(node) container_of(node,struct device,driver_list) +/* + * helpers for creating driver attributes in sysfs + */ + +int driver_create_file(struct device_driver * drv, struct driver_attribute * attr) +{ + int error; + if (get_driver(drv)) { + error = sysfs_create_file(&drv->kobj,&attr->attr); + put_driver(drv); + } else + error = -EINVAL; + return error; +} + +void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr) +{ + if (get_driver(drv)) { + sysfs_remove_file(&drv->kobj,&attr->attr); + put_driver(drv); + } +} + int driver_for_each_dev(struct device_driver * drv, void * data, int (*callback)(struct device *, void * )) { @@ -65,7 +88,6 @@ void put_driver(struct device_driver * drv) return; spin_unlock(&device_lock); BUG_ON(drv->present); - bus_remove_driver(drv); if (drv->release) drv->release(drv); put_bus(bus); @@ -84,6 +106,11 @@ int driver_register(struct device_driver * drv) pr_debug("driver %s:%s: registering\n",drv->bus->name,drv->name); + kobject_init(&drv->kobj); + strncpy(drv->kobj.name,drv->name,KOBJ_NAME_LEN); + drv->kobj.subsys = &drv->bus->drvsubsys; + kobject_register(&drv->kobj); + get_bus(drv->bus); atomic_set(&drv->refcount,2); rwlock_init(&drv->lock); @@ -108,3 +135,6 @@ EXPORT_SYMBOL(driver_register); EXPORT_SYMBOL(driver_unregister); EXPORT_SYMBOL(get_driver); EXPORT_SYMBOL(put_driver); + +EXPORT_SYMBOL(driver_create_file); +EXPORT_SYMBOL(driver_remove_file); diff --git a/drivers/base/fs/driver.c b/drivers/base/fs/driver.c index c70834d738fc..c259470c7bb6 100644 --- a/drivers/base/fs/driver.c +++ b/drivers/base/fs/driver.c @@ -4,80 +4,6 @@ #include #include "fs.h" -#define to_drv_attr(_attr) container_of(_attr,struct driver_attribute,attr) - -#define to_drv(d) container_of(d, struct device_driver, dir) - - -/* driverfs ops for device attribute files */ - -static int -drv_attr_open(struct driver_dir_entry * dir) -{ - struct device_driver * drv = to_drv(dir); - get_driver(drv); - return 0; -} - -static int -drv_attr_close(struct driver_dir_entry * dir) -{ - struct device_driver * drv = to_drv(dir); - put_driver(drv); - return 0; -} - -static ssize_t -drv_attr_show(struct driver_dir_entry * dir, struct attribute * attr, - char * buf, size_t count, loff_t off) -{ - struct driver_attribute * drv_attr = to_drv_attr(attr); - struct device_driver * drv = to_drv(dir); - ssize_t ret = 0; - - if (drv_attr->show) - ret = drv_attr->show(drv,buf,count,off); - return ret; -} - -static ssize_t -drv_attr_store(struct driver_dir_entry * dir, struct attribute * attr, - const char * buf, size_t count, loff_t off) -{ - struct driver_attribute * drv_attr = to_drv_attr(attr); - struct device_driver * drv = to_drv(dir); - ssize_t ret = 0; - - if (drv_attr->store) - ret = drv_attr->store(drv,buf,count,off); - return ret; -} - -static struct driverfs_ops drv_attr_ops = { - .open = drv_attr_open, - .close = drv_attr_close, - .show = drv_attr_show, - .store = drv_attr_store, -}; - -int driver_create_file(struct device_driver * drv, struct driver_attribute * attr) -{ - int error; - if (get_driver(drv)) { - error = driverfs_create_file(&attr->attr,&drv->dir); - put_driver(drv); - } else - error = -EINVAL; - return error; -} - -void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr) -{ - if (get_driver(drv)) { - driverfs_remove_file(&drv->dir,attr->attr.name); - put_driver(drv); - } -} /** * driver_make_dir - create a driverfs directory for a driver @@ -86,8 +12,6 @@ void driver_remove_file(struct device_driver * drv, struct driver_attribute * at int driver_make_dir(struct device_driver * drv) { drv->dir.name = drv->name; - drv->dir.ops = &drv_attr_ops; - return device_create_dir(&drv->dir,&drv->bus->driver_dir); } @@ -96,5 +20,3 @@ void driver_remove_dir(struct device_driver * drv) driverfs_remove_dir(&drv->dir); } -EXPORT_SYMBOL(driver_create_file); -EXPORT_SYMBOL(driver_remove_file); diff --git a/include/linux/device.h b/include/linux/device.h index 66d4f313a809..5ce759266658 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -123,6 +123,7 @@ struct device_driver { atomic_t refcount; u32 present; + struct kobject kobj; struct list_head bus_list; struct list_head class_list; struct list_head devices; -- cgit v1.2.3