Commit 9bb83ce6 authored by Patrick Mochel's avatar Patrick Mochel

driverfs: Add struct attribute

driverfs can only handle passing struct device to read/write functions. In order to free it of this limitation,
we need a common data structure for driverfs to pass around. 

The only thing that driverfs really needs are the name and mode of the file, which are now located in struct
attribute.

struct device_attribute gets a struct attribute member, which holds the name and mode. With the DEVICE_ATTR 
macro, users of the structure require no modification.

device_create_file is modified to take a struct attribute parameter

a to_dev_attr() macro is introduced to convert between a struct attribute to a struct device_attribute
parent b74d2576
...@@ -30,7 +30,7 @@ int device_create_file(struct device * dev, struct device_attribute * entry) ...@@ -30,7 +30,7 @@ int device_create_file(struct device * dev, struct device_attribute * entry)
if (dev) { if (dev) {
get_device(dev); get_device(dev);
error = driverfs_create_file(entry,&dev->dir); error = driverfs_create_file(&entry->attr,&dev->dir);
put_device(dev); put_device(dev);
} }
return error; return error;
...@@ -46,7 +46,7 @@ void device_remove_file(struct device * dev, struct device_attribute * attr) ...@@ -46,7 +46,7 @@ void device_remove_file(struct device * dev, struct device_attribute * attr)
{ {
if (dev) { if (dev) {
get_device(dev); get_device(dev);
driverfs_remove_file(&dev->dir,attr->name); driverfs_remove_file(&dev->dir,attr->attr.name);
put_device(dev); put_device(dev);
} }
} }
......
...@@ -282,7 +282,7 @@ driverfs_read_file(struct file *file, char *buf, size_t count, loff_t *ppos) ...@@ -282,7 +282,7 @@ driverfs_read_file(struct file *file, char *buf, size_t count, loff_t *ppos)
struct device * dev; struct device * dev;
dir = file->f_dentry->d_parent->d_fsdata; dir = file->f_dentry->d_parent->d_fsdata;
entry = (struct device_attribute *)file->f_dentry->d_fsdata; entry = to_dev_attr(file->f_dentry->d_fsdata);
if (!entry) { if (!entry) {
DBG("%s: file entry is NULL\n",__FUNCTION__); DBG("%s: file entry is NULL\n",__FUNCTION__);
return -ENOENT; return -ENOENT;
...@@ -349,7 +349,7 @@ driverfs_write_file(struct file *file, const char *buf, size_t count, loff_t *pp ...@@ -349,7 +349,7 @@ driverfs_write_file(struct file *file, const char *buf, size_t count, loff_t *pp
dir = file->f_dentry->d_parent->d_fsdata; dir = file->f_dentry->d_parent->d_fsdata;
entry = (struct device_attribute *)file->f_dentry->d_fsdata; entry = to_dev_attr(file->f_dentry->d_fsdata);
if (!entry) { if (!entry) {
DBG("%s: file entry is NULL\n",__FUNCTION__); DBG("%s: file entry is NULL\n",__FUNCTION__);
return -ENOENT; return -ENOENT;
...@@ -618,7 +618,7 @@ driverfs_create_dir(struct driver_dir_entry * entry, ...@@ -618,7 +618,7 @@ driverfs_create_dir(struct driver_dir_entry * entry,
* @parent: directory to create it in * @parent: directory to create it in
*/ */
int int
driverfs_create_file(struct device_attribute * entry, driverfs_create_file(struct attribute * entry,
struct driver_dir_entry * parent) struct driver_dir_entry * parent)
{ {
struct dentry * dentry; struct dentry * dentry;
......
...@@ -32,23 +32,28 @@ struct driver_dir_entry { ...@@ -32,23 +32,28 @@ struct driver_dir_entry {
mode_t mode; mode_t mode;
}; };
struct attribute {
char * name;
mode_t mode;
};
struct device; struct device;
struct device_attribute { struct device_attribute {
char * name; struct attribute attr;
mode_t mode;
ssize_t (*show)(struct device * dev, char * buf, size_t count, loff_t off); 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); ssize_t (*store)(struct device * dev, const char * buf, size_t count, loff_t off);
}; };
#define DEVICE_ATTR(_name,_str,_mode,_show,_store) \ #define DEVICE_ATTR(_name,_str,_mode,_show,_store) \
struct device_attribute dev_attr_##_name = { \ struct device_attribute dev_attr_##_name = { \
.name = _str, \ .attr = {.name = _str, .mode = _mode }, \
.mode = _mode, \
.show = _show, \ .show = _show, \
.store = _store, \ .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 *);
...@@ -56,7 +61,7 @@ extern void ...@@ -56,7 +61,7 @@ extern void
driverfs_remove_dir(struct driver_dir_entry * entry); driverfs_remove_dir(struct driver_dir_entry * entry);
extern int extern int
driverfs_create_file(struct device_attribute * entry, driverfs_create_file(struct attribute * attr,
struct driver_dir_entry * parent); struct driver_dir_entry * parent);
extern int extern int
......
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