Commit 137973c1 authored by Patrick Mochel's avatar Patrick Mochel

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.
parent 9bb83ce6
...@@ -17,6 +17,62 @@ ...@@ -17,6 +17,62 @@
extern struct device_attribute * device_default_files[]; 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 * device_create_file - create a driverfs file for a device
* @dev: device requesting file * @dev: device requesting file
...@@ -159,6 +215,7 @@ int device_make_dir(struct device * dev) ...@@ -159,6 +215,7 @@ int device_make_dir(struct device * dev)
if (dev->parent) if (dev->parent)
parent = &dev->parent->dir; parent = &dev->parent->dir;
dev->dir.name = dev->bus_id; dev->dir.name = dev->bus_id;
dev->dir.ops = &dev_attr_ops;
if ((error = device_create_dir(&dev->dir,parent))) if ((error = device_create_dir(&dev->dir,parent)))
return error; return error;
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
#include <linux/namei.h> #include <linux/namei.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/device.h> #include <linux/driverfs_fs.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
...@@ -275,26 +275,18 @@ static int driverfs_rmdir(struct inode *dir, struct dentry *dentry) ...@@ -275,26 +275,18 @@ static int driverfs_rmdir(struct inode *dir, struct dentry *dentry)
static ssize_t static ssize_t
driverfs_read_file(struct file *file, char *buf, size_t count, loff_t *ppos) 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; struct driver_dir_entry * dir;
unsigned char *page; unsigned char *page;
ssize_t retval = 0; ssize_t retval = 0;
struct device * dev;
dir = file->f_dentry->d_parent->d_fsdata; dir = file->f_dentry->d_parent->d_fsdata;
entry = to_dev_attr(file->f_dentry->d_fsdata); if (!dir->ops->show)
if (!entry) {
DBG("%s: file entry is NULL\n",__FUNCTION__);
return -ENOENT;
}
if (!entry->show)
return 0; return 0;
if (count > PAGE_SIZE) if (count > PAGE_SIZE)
count = PAGE_SIZE; count = PAGE_SIZE;
dev = to_device(dir);
page = (unsigned char*)__get_free_page(GFP_KERNEL); page = (unsigned char*)__get_free_page(GFP_KERNEL);
if (!page) if (!page)
return -ENOMEM; return -ENOMEM;
...@@ -302,7 +294,7 @@ driverfs_read_file(struct file *file, char *buf, size_t count, loff_t *ppos) ...@@ -302,7 +294,7 @@ driverfs_read_file(struct file *file, char *buf, size_t count, loff_t *ppos)
while (count > 0) { while (count > 0) {
ssize_t len; 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) {
if (len < 0) if (len < 0)
...@@ -341,24 +333,13 @@ driverfs_read_file(struct file *file, char *buf, size_t count, loff_t *ppos) ...@@ -341,24 +333,13 @@ driverfs_read_file(struct file *file, char *buf, size_t count, loff_t *ppos)
static ssize_t static ssize_t
driverfs_write_file(struct file *file, const char *buf, size_t count, loff_t *ppos) 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 driver_dir_entry * dir;
struct device * dev;
ssize_t retval = 0; ssize_t retval = 0;
char * page; char * page;
dir = file->f_dentry->d_parent->d_fsdata; 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); page = (char *)__get_free_page(GFP_KERNEL);
if (!page) if (!page)
return -ENOMEM; return -ENOMEM;
...@@ -372,7 +353,7 @@ driverfs_write_file(struct file *file, const char *buf, size_t count, loff_t *pp ...@@ -372,7 +353,7 @@ driverfs_write_file(struct file *file, const char *buf, size_t count, loff_t *pp
while (count > 0) { while (count > 0) {
ssize_t len; 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) {
if (len < 0) if (len < 0)
...@@ -418,24 +399,28 @@ driverfs_file_lseek(struct file *file, loff_t offset, int orig) ...@@ -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) static int driverfs_open_file(struct inode * inode, struct file * filp)
{ {
struct driver_dir_entry * dir; struct driver_dir_entry * dir;
struct device * dev; int error = 0;
dir = (struct driver_dir_entry *)filp->f_dentry->d_parent->d_fsdata; dir = (struct driver_dir_entry *)filp->f_dentry->d_parent->d_fsdata;
if (!dir) if (dir) {
return -EFAULT; struct attribute * attr = filp->f_dentry->d_fsdata;
dev = to_device(dir); if (attr && dir->ops) {
get_device(dev); if (dir->ops->open)
return 0; error = dir->ops->open(dir);
goto Done;
}
}
error = -EINVAL;
Done:
return error;
} }
static int driverfs_release(struct inode * inode, struct file * filp) static int driverfs_release(struct inode * inode, struct file * filp)
{ {
struct driver_dir_entry * dir; struct driver_dir_entry * dir;
struct device * dev;
dir = (struct driver_dir_entry *)filp->f_dentry->d_parent->d_fsdata; dir = (struct driver_dir_entry *)filp->f_dentry->d_parent->d_fsdata;
dev = to_device(dir); if (dir->ops->close)
put_device(dev); dir->ops->close(dir);
return 0; return 0;
} }
......
...@@ -160,8 +160,6 @@ struct device { ...@@ -160,8 +160,6 @@ struct device {
void (*release)(struct device * dev); void (*release)(struct device * dev);
}; };
#define to_device(d) container_of(d, struct device, dir)
static inline struct device * static inline struct device *
list_to_dev(struct list_head *node) list_to_dev(struct list_head *node)
{ {
...@@ -179,6 +177,22 @@ g_list_to_dev(struct list_head *g_list) ...@@ -179,6 +177,22 @@ g_list_to_dev(struct list_head *g_list)
*/ */
extern int device_register(struct device * dev); 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 int device_create_file(struct device *device, struct device_attribute * entry);
extern void device_remove_file(struct device * dev, struct device_attribute * attr); extern void device_remove_file(struct device * dev, struct device_attribute * attr);
......
...@@ -26,10 +26,21 @@ ...@@ -26,10 +26,21 @@
#ifndef _DRIVER_FS_H_ #ifndef _DRIVER_FS_H_
#define _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 { struct driver_dir_entry {
char * name; char * name;
struct dentry * dentry; struct dentry * dentry;
mode_t mode; mode_t mode;
struct driverfs_ops * ops;
}; };
struct attribute { struct attribute {
...@@ -37,23 +48,6 @@ struct attribute { ...@@ -37,23 +48,6 @@ struct attribute {
mode_t mode; 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 extern int
driverfs_create_dir(struct driver_dir_entry *, struct driver_dir_entry *); driverfs_create_dir(struct driver_dir_entry *, struct driver_dir_entry *);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment