aboutsummaryrefslogtreecommitdiffstats
path: root/fs/debugfs
diff options
context:
space:
mode:
authorArend van Spriel <arend@broadcom.com>2014-11-09 05:31:58 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-11-26 22:38:37 -0500
commit98210b7f73f1db182bd9a558a031093cd166e907 (patch)
tree93b0c862e237d963e9ad9a8d2ff8e5b0e222b8fd /fs/debugfs
parent6df43c9b4d87b6767debdfa8318c5374415e8fc0 (diff)
debugfs: add helper function to create device related seq_file
This patch adds a helper function that simplifies adding a so-called single_open sequence file for device drivers. The calling device driver needs to provide a read function and a device pointer. The field struct seq_file::private will reference the device pointer upon call to the read function so the driver can obtain his data from it and do its task of providing the file content using seq_printf() calls and alike. Using this helper function also gets rid of the need to specify file operations per debugfs file. Signed-off-by: Arend van Spriel <arend@broadcom.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'fs/debugfs')
-rw-r--r--fs/debugfs/file.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c
index 76c08c2beb2f..a2f8ec70fe8d 100644
--- a/fs/debugfs/file.c
+++ b/fs/debugfs/file.c
@@ -22,6 +22,7 @@
22#include <linux/io.h> 22#include <linux/io.h>
23#include <linux/slab.h> 23#include <linux/slab.h>
24#include <linux/atomic.h> 24#include <linux/atomic.h>
25#include <linux/device.h>
25 26
26static ssize_t default_read_file(struct file *file, char __user *buf, 27static ssize_t default_read_file(struct file *file, char __user *buf,
27 size_t count, loff_t *ppos) 28 size_t count, loff_t *ppos)
@@ -761,3 +762,56 @@ struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
761EXPORT_SYMBOL_GPL(debugfs_create_regset32); 762EXPORT_SYMBOL_GPL(debugfs_create_regset32);
762 763
763#endif /* CONFIG_HAS_IOMEM */ 764#endif /* CONFIG_HAS_IOMEM */
765
766struct debugfs_devm_entry {
767 int (*read)(struct seq_file *seq, void *data);
768 struct device *dev;
769};
770
771static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
772{
773 struct debugfs_devm_entry *entry = inode->i_private;
774
775 return single_open(f, entry->read, entry->dev);
776}
777
778static const struct file_operations debugfs_devm_entry_ops = {
779 .owner = THIS_MODULE,
780 .open = debugfs_devm_entry_open,
781 .release = single_release,
782 .read = seq_read,
783 .llseek = seq_lseek
784};
785
786/**
787 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
788 *
789 * @dev: device related to this debugfs file.
790 * @name: name of the debugfs file.
791 * @parent: a pointer to the parent dentry for this file. This should be a
792 * directory dentry if set. If this parameter is %NULL, then the
793 * file will be created in the root of the debugfs filesystem.
794 * @read_fn: function pointer called to print the seq_file content.
795 */
796struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
797 struct dentry *parent,
798 int (*read_fn)(struct seq_file *s,
799 void *data))
800{
801 struct debugfs_devm_entry *entry;
802
803 if (IS_ERR(parent))
804 return ERR_PTR(-ENOENT);
805
806 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
807 if (!entry)
808 return ERR_PTR(-ENOMEM);
809
810 entry->read = read_fn;
811 entry->dev = dev;
812
813 return debugfs_create_file(name, S_IRUGO, parent, entry,
814 &debugfs_devm_entry_ops);
815}
816EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
817