2 * bin.c - binary file operations for sysfs.
7 #include <linux/errno.h>
9 #include <linux/kobject.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
13 #include <asm/uaccess.h>
18 fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
20 struct bin_attribute * attr = dentry->d_fsdata;
21 struct kobject * kobj = dentry->d_parent->d_fsdata;
23 return attr->read(kobj, buffer, off, count);
27 read(struct file * file, char __user * userbuf, size_t count, loff_t * off)
29 char *buffer = file->private_data;
30 struct dentry *dentry = file->f_dentry;
31 int size = dentry->d_inode->i_size;
35 if (count > PAGE_SIZE)
41 if (offs + count > size)
45 ret = fill_read(dentry, buffer, offs, count);
50 if (copy_to_user(userbuf, buffer + offs, count) != 0)
53 pr_debug("offs = %lld, *off = %lld, count = %zd\n", offs, *off, count);
61 flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
63 struct bin_attribute *attr = dentry->d_fsdata;
64 struct kobject *kobj = dentry->d_parent->d_fsdata;
66 return attr->write(kobj, buffer, offset, count);
69 static ssize_t write(struct file * file, const char __user * userbuf,
70 size_t count, loff_t * off)
72 char *buffer = file->private_data;
73 struct dentry *dentry = file->f_dentry;
74 int size = dentry->d_inode->i_size;
77 if (count > PAGE_SIZE)
82 if (offs + count > size)
86 if (copy_from_user(buffer + offs, userbuf, count))
89 count = flush_write(dentry, buffer, offs, count);
95 static int open(struct inode * inode, struct file * file)
97 struct kobject * kobj = kobject_get(file->f_dentry->d_parent->d_fsdata);
98 struct bin_attribute * attr = file->f_dentry->d_fsdata;
105 if ((file->f_mode & FMODE_WRITE) && !attr->write)
107 if ((file->f_mode & FMODE_READ) && !attr->read)
111 file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
112 if (!file->private_data)
123 static int release(struct inode * inode, struct file * file)
125 struct kobject * kobj = file->f_dentry->d_parent->d_fsdata;
126 u8 * buffer = file->private_data;
134 static struct file_operations bin_fops = {
137 .llseek = generic_file_llseek,
143 * sysfs_create_bin_file - create binary file for object.
145 * @attr: attribute descriptor.
149 int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
151 struct dentry * dentry;
152 struct dentry * parent;
158 parent = kobj->dentry;
160 down(&parent->d_inode->i_sem);
161 dentry = sysfs_get_dentry(parent,attr->attr.name);
162 if (!IS_ERR(dentry)) {
163 dentry->d_fsdata = (void *)attr;
164 error = sysfs_create(dentry,
165 (attr->attr.mode & S_IALLUGO) | S_IFREG,
168 dentry->d_inode->i_size = attr->size;
169 dentry->d_inode->i_fop = &bin_fops;
172 error = PTR_ERR(dentry);
173 up(&parent->d_inode->i_sem);
179 * sysfs_remove_bin_file - remove binary file for object.
181 * @attr: attribute descriptor.
185 int sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
187 sysfs_hash_and_remove(kobj->dentry,attr->attr.name);
191 EXPORT_SYMBOL(sysfs_create_bin_file);
192 EXPORT_SYMBOL(sysfs_remove_bin_file);