Use debugfs, which is better-suited than sysfs for the 'regs' node.
Also change the expected format from decimal to hexadecimal numbers.
---
 drivers/mfd/glamo-core.c       |   71 ++++++++++++++++++++++++++-------------
 include/linux/mfd/glamo-core.h |    1 +
 2 files changed, 48 insertions(+), 24 deletions(-)

diff --git a/drivers/mfd/glamo-core.c b/drivers/mfd/glamo-core.c
index 48f56fa..d716232 100644
--- a/drivers/mfd/glamo-core.c
+++ b/drivers/mfd/glamo-core.c
@@ -40,6 +40,8 @@
 #include <linux/mfd/glamo-core.h>
 #include <linux/io.h>
 #include <linux/slab.h>
+#include <linux/debugfs.h>
+#include <asm/uaccess.h>
 
 #include <linux/pm.h>
 
@@ -317,52 +319,69 @@ static irqreturn_t glamo_irq_demux_handler(int irq, void 
*data)
 }
 
 /*
-sysfs
+debugfs
 */
 
-static ssize_t regs_write(struct device *dev, struct device_attribute *attr,
-                               const char *buf, size_t count)
+static ssize_t debugfs_regs_write(struct file *file,
+                                 const char __user *user_buf,
+                                 size_t count, loff_t *ppos)
 {
-       struct glamo_core *glamo = dev_get_drvdata(dev);
+       struct glamo_core *glamo = file->private_data;
+       char buf[14];
        unsigned int reg;
        unsigned int val;
+       int buf_size;
+
+       buf_size = min(count, sizeof(buf) - 1);
+       if (copy_from_user(buf, user_buf, buf_size))
+               return -EFAULT;
+       if (sscanf(buf, "%x %x", &reg, &val) != 2)
+               return -EFAULT;
 
-       sscanf(buf, "%u %u", &reg, &val);
-       printk(KERN_INFO"reg 0x%02x <-- 0x%04x\n",
-               reg, val);
+       dev_info(&glamo->pdev->dev, "reg %#02x <-- %#04x\n", reg, val);
 
        glamo_reg_write(glamo, reg, val);
 
        return count;
 }
 
-static ssize_t regs_read(struct device *dev, struct device_attribute *attr,
-                       char *buf)
+static ssize_t debugfs_regs_read(struct file *file, char __user *user_buf,
+                                size_t count, loff_t *ppos)
 {
-       struct glamo_core *glamo = dev_get_drvdata(dev);
+       struct glamo_core *glamo = file->private_data;
        int i, n;
+       char buf[1024];
        char *end = buf;
+       const size_t bufsz = sizeof(buf);
        const struct reg_range *rr = reg_range;
 
-       spin_lock(&glamo->lock);
-
        for (i = 0; i < ARRAY_SIZE(reg_range); ++i, ++rr) {
                if (!rr->dump)
                        continue;
-               end += sprintf(end, "\n%s\n", rr->name);
+               end += scnprintf(end, bufsz + buf - end, "\n%s\n", rr->name);
                for (n = rr->start; n < rr->start + rr->count; n += 2) {
                        if ((n & 15) == 0)
-                               end += sprintf(end, "\n%04X:  ", n);
-                       end += sprintf(end, "%04x ", __reg_read(glamo, n));
+                               end += scnprintf(end, bufsz + buf - end, 
"\n%04X:  ", n);
+                       end += scnprintf(end, bufsz + buf - end, "%04x ", 
__reg_read(glamo, n));
                }
-               end += sprintf(end, "\n");
+               end += scnprintf(end, bufsz + buf - end, "\n");
        }
        spin_unlock(&glamo->lock);
 
-       return end - buf;
+       return simple_read_from_buffer(user_buf, count, ppos, buf, end - buf);
 }
 
-static DEVICE_ATTR(regs, 0644, regs_read, regs_write);
+static int debugfs_open_file(struct inode *inode, struct file *file)
+{
+       file->private_data = inode->i_private;
+       return 0;
+}
+
+static const struct file_operations debugfs_regs_ops = {
+       .write = debugfs_regs_write,
+       .read = debugfs_regs_read,
+       .open = debugfs_open_file,
+};
 
 struct glamo_engine_reg_set {
        uint16_t reg;
@@ -932,12 +951,13 @@ static int __devinit glamo_probe(struct platform_device 
*pdev)
 
        platform_set_drvdata(pdev, glamo);
 
-       /* sysfs */
-       ret = device_create_file(&pdev->dev, &dev_attr_regs);
-       if (ret < 0) {
-               dev_err(&pdev->dev, "Failed to create sysfs file\n");
-               goto err_iounmap;
-       }
+       /* debugfs */
+       glamo->debugfs_dir = debugfs_create_dir("glamo3362", NULL);
+       if (glamo->debugfs_dir)
+               debugfs_create_file("regs", S_IRUGO | S_IWUSR, debugfs_dir,
+                                   glamo, &debugfs_regs_ops);
+       else
+               dev_warn(&glamo->pdev->dev, "Failed to set up debugfs.\n");
 
        /* init the chip with canned register set */
        glamo_run_script(glamo, glamo_init_script,
@@ -1009,6 +1029,9 @@ static int __devexit glamo_remove(struct platform_device 
*pdev)
        int irq;
        int irq_base = glamo->irq_base;
 
+       if (glamo->debugfs_dir)
+               debugfs_remove_recursive(glamo->debugfs_dir);
+
        mfd_remove_devices(&pdev->dev);
 
        disable_irq(glamo->irq);
diff --git a/include/linux/mfd/glamo-core.h b/include/linux/mfd/glamo-core.h
index 34ec7c4..091d96a 100644
--- a/include/linux/mfd/glamo-core.h
+++ b/include/linux/mfd/glamo-core.h
@@ -37,6 +37,7 @@ struct glamo_core {
        enum glamo_engine_state engine_state[__NUM_GLAMO_ENGINES];
        spinlock_t lock;
        uint16_t saved_irq_mask;
+       struct dentry *debugfs_dir;
 };
 
 struct glamo_script {
-- 
1.7.1


Reply via email to