Home Home > GIT Browse
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Mochel <mochel@osdl.org>2002-07-31 22:07:33 -0700
committerPatrick Mochel <mochel@osdl.org>2002-07-31 22:07:33 -0700
commit137973c1a3ee75e3d28af550a5e33737ac9acc5b (patch)
treed9834c4900a6939f86074f8e893cd80bcb304379
parent9bb83ce6ec5908758f0999362c0297720ca9b5ac (diff)
driverfs: define struct driverfs_ops and remove struct device dependencies
In order to read/write attributes, you have to deal directly with the object that owns them. driverfs really wants to be generic and not deal directly with those objects. So, we create an intermediate layer that subsystems must implement that converts between the generic objects and the specific objects that own the attributes. This allows allows attributes to be exported for any object type. In doing so, it places the responsibility on the subsystems to do the following: - define their own object-specific attribute structures - define their own driverfs_ops - set the ops pointer in struct driver_dir_entry when creating an object's directory - do object reference counting on open() and close() - call the show() and store() callbacks of their attribute structure - convert between the generic objects and the specific objects from the struct driver_dir_entry and struct attribute pointers (using container_of) The implementation of this layer for struct device is intended to be used as an example of the interface. Because this layer of abstraction is now in place, we can move the device attribute structure into include/linux/device.h, and driverfs should be free of references to it completely.
-rw-r--r--drivers/base/fs.c57
-rw-r--r--fs/driverfs/inode.c55
-rw-r--r--include/linux/device.h18
-rw-r--r--include/linux/driverfs_fs.h28
4 files changed, 104 insertions, 54 deletions
diff --git a/drivers/base/fs.c b/drivers/base/fs.c
index a50586b8c45a..62df2c48be3f 100644
--- a/drivers/base/fs.c
+++ b/drivers/base/fs.c
@@ -17,6 +17,62 @@
extern struct device_attribute * device_default_files[];
+#define to_dev_attr(_attr) container_of(_attr,struct device_attribute,attr)
+
+#define to_device(d) container_of(d, struct device, dir)
+
+
+/* driverfs ops for device attribute files */
+
+static int
+dev_attr_open(struct driver_dir_entry * dir)
+{
+ struct device * dev = to_device(dir);
+ get_device(dev);
+ return 0;
+}
+
+static int
+dev_attr_close(struct driver_dir_entry * dir)
+{
+ struct device * dev = to_device(dir);
+ put_device(dev);
+ return 0;
+}
+
+static ssize_t
+dev_attr_show(struct driver_dir_entry * dir, struct attribute * attr,
+ char * buf, size_t count, loff_t off)
+{
+ struct device_attribute * dev_attr = to_dev_attr(attr);
+ struct device * dev = to_device(dir);
+ ssize_t ret = 0;
+
+ if (dev_attr->show)
+ ret = dev_attr->show(dev,buf,count,off);
+ return ret;
+}
+
+static ssize_t
+dev_attr_store(struct driver_dir_entry * dir, struct attribute * attr,
+ const char * buf, size_t count, loff_t off)
+{
+ struct device_attribute * dev_attr = to_dev_attr(attr);
+ struct device * dev = to_device(dir);
+ ssize_t ret = 0;
+
+ if (dev_attr->store)
+ ret = dev_attr->store(dev,buf,count,off);
+ return ret;
+}
+
+static struct driverfs_ops dev_attr_ops = {
+ open: dev_attr_open,
+ close: dev_attr_close,
+ show: dev_attr_show,
+ store: dev_attr_store,
+};
+
/**
* device_create_file - create a driverfs file for a device
* @dev: device requesting file
@@ -159,6 +215,7 @@ int device_make_dir(struct device * dev)
if (dev->parent)
parent = &dev->parent->dir;
dev->dir.name = dev->bus_id;
+ dev->dir.ops = &dev_attr_ops;
if ((error = device_create_dir(&dev->dir,parent)))
return error;
diff --git a/fs/driverfs/inode.c b/fs/driverfs/inode.c
index 7df47ce2f4d3..49b27f479355 100644
--- a/fs/driverfs/inode.c
+++ b/fs/driverfs/inode.c
@@ -32,7 +32,7 @@
#include <linux/namei.h>
#include <linux/module.h>
#include <linux/slab.h>
-#include <linux/device.h>
+#include <linux/driverfs_fs.h>
#include <asm/uaccess.h>
@@ -275,26 +275,18 @@ static int driverfs_rmdir(struct inode *dir, struct dentry *dentry)
static ssize_t
driverfs_read_file(struct file *file, char *buf, size_t count, loff_t *ppos)
{
- struct device_attribute * entry;
+ struct attribute * attr = file->f_dentry->d_fsdata;
struct driver_dir_entry * dir;
unsigned char *page;
ssize_t retval = 0;
- struct device * dev;
dir = file->f_dentry->d_parent->d_fsdata;
- entry = to_dev_attr(file->f_dentry->d_fsdata);
- if (!entry) {
- DBG("%s: file entry is NULL\n",__FUNCTION__);
- return -ENOENT;
- }
- if (!entry->show)
+ if (!dir->ops->show)
return 0;
if (count > PAGE_SIZE)
count = PAGE_SIZE;
- dev = to_device(dir);
-
page = (unsigned char*)__get_free_page(GFP_KERNEL);
if (!page)
return -ENOMEM;
@@ -302,7 +294,7 @@ driverfs_read_file(struct file *file, char *buf, size_t count, loff_t *ppos)
while (count > 0) {
ssize_t len;
- len = entry->show(dev,page,count,*ppos);
+ len = dir->ops->show(dir,attr,page,count,*ppos);
if (len <= 0) {
if (len < 0)
@@ -341,24 +333,13 @@ driverfs_read_file(struct file *file, char *buf, size_t count, loff_t *ppos)
static ssize_t
driverfs_write_file(struct file *file, const char *buf, size_t count, loff_t *ppos)
{
- struct device_attribute * entry;
+ struct attribute * attr = file->f_dentry->d_fsdata;
struct driver_dir_entry * dir;
- struct device * dev;
ssize_t retval = 0;
char * page;
dir = file->f_dentry->d_parent->d_fsdata;
- entry = to_dev_attr(file->f_dentry->d_fsdata);
- if (!entry) {
- DBG("%s: file entry is NULL\n",__FUNCTION__);
- return -ENOENT;
- }
- if (!entry->store)
- return 0;
-
- dev = to_device(dir);
-
page = (char *)__get_free_page(GFP_KERNEL);
if (!page)
return -ENOMEM;
@@ -372,7 +353,7 @@ driverfs_write_file(struct file *file, const char *buf, size_t count, loff_t *pp
while (count > 0) {
ssize_t len;
- len = entry->store(dev,page + retval,count,*ppos);
+ len = dir->ops->store(dir,attr,page + retval,count,*ppos);
if (len <= 0) {
if (len < 0)
@@ -418,24 +399,28 @@ driverfs_file_lseek(struct file *file, loff_t offset, int orig)
static int driverfs_open_file(struct inode * inode, struct file * filp)
{
struct driver_dir_entry * dir;
- struct device * dev;
+ int error = 0;
dir = (struct driver_dir_entry *)filp->f_dentry->d_parent->d_fsdata;
- if (!dir)
- return -EFAULT;
- dev = to_device(dir);
- get_device(dev);
- return 0;
+ if (dir) {
+ struct attribute * attr = filp->f_dentry->d_fsdata;
+ if (attr && dir->ops) {
+ if (dir->ops->open)
+ error = dir->ops->open(dir);
+ goto Done;
+ }
+ }
+ error = -EINVAL;
+ Done:
+ return error;
}
static int driverfs_release(struct inode * inode, struct file * filp)
{
struct driver_dir_entry * dir;
- struct device * dev;
-
dir = (struct driver_dir_entry *)filp->f_dentry->d_parent->d_fsdata;
- dev = to_device(dir);
- put_device(dev);
+ if (dir->ops->close)
+ dir->ops->close(dir);
return 0;
}
diff --git a/include/linux/device.h b/include/linux/device.h
index 670be49d1718..5dfa05c2dd3a 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -160,8 +160,6 @@ struct device {
void (*release)(struct device * dev);
};
-#define to_device(d) container_of(d, struct device, dir)
-
static inline struct device *
list_to_dev(struct list_head *node)
{
@@ -179,6 +177,22 @@ g_list_to_dev(struct list_head *g_list)
*/
extern int device_register(struct device * dev);
+
+/* driverfs interface for exporting device attributes */
+
+struct device_attribute {
+ struct attribute attr;
+ ssize_t (*show)(struct device * dev, char * buf, size_t count, loff_t off);
+ ssize_t (*store)(struct device * dev, const char * buf, size_t count, loff_t off);
+};
+
+#define DEVICE_ATTR(_name,_str,_mode,_show,_store) \
+struct device_attribute dev_attr_##_name = { \
+ .attr = {.name = _str, .mode = _mode }, \
+ .show = _show, \
+ .store = _store, \
+};
+
extern int device_create_file(struct device *device, struct device_attribute * entry);
extern void device_remove_file(struct device * dev, struct device_attribute * attr);
diff --git a/include/linux/driverfs_fs.h b/include/linux/driverfs_fs.h
index d00ee61fce07..d859f8c2e041 100644
--- a/include/linux/driverfs_fs.h
+++ b/include/linux/driverfs_fs.h
@@ -26,10 +26,21 @@
#ifndef _DRIVER_FS_H_
#define _DRIVER_FS_H_
+struct driver_dir_entry;
+struct attribute;
+
+struct driverfs_ops {
+ int (*open)(struct driver_dir_entry *);
+ int (*close)(struct driver_dir_entry *);
+ ssize_t (*show)(struct driver_dir_entry *, struct attribute *,char *, size_t, loff_t);
+ ssize_t (*store)(struct driver_dir_entry *,struct attribute *,const char *, size_t, loff_t);
+};
+
struct driver_dir_entry {
char * name;
struct dentry * dentry;
mode_t mode;
+ struct driverfs_ops * ops;
};
struct attribute {
@@ -37,23 +48,6 @@ struct attribute {
mode_t mode;
};
-struct device;
-
-struct device_attribute {
- struct attribute attr;
- ssize_t (*show)(struct device * dev, char * buf, size_t count, loff_t off);
- ssize_t (*store)(struct device * dev, const char * buf, size_t count, loff_t off);
-};
-
-#define DEVICE_ATTR(_name,_str,_mode,_show,_store) \
-struct device_attribute dev_attr_##_name = { \
- .attr = {.name = _str, .mode = _mode }, \
- .show = _show, \
- .store = _store, \
-};
-
-#define to_dev_attr(_attr) container_of(_attr,struct device_attribute,attr)
-
extern int
driverfs_create_dir(struct driver_dir_entry *, struct driver_dir_entry *);