Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 289176
b: refs/heads/master
c: e553f18
h: refs/heads/master
v: v3
  • Loading branch information
Michael Hennerich authored and Greg Kroah-Hartman committed Mar 3, 2012
1 parent 81ef362 commit 4ff6e6b
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 3 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 4abf6f8b29e37f492078173a9d4cb808ce327ec0
refs/heads/master: e553f182d55bd268fea3f106368e2344141c212a
27 changes: 26 additions & 1 deletion trunk/drivers/staging/iio/iio.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ struct iio_info {
struct iio_trigger *trig);
int (*update_scan_mode)(struct iio_dev *indio_dev,
const unsigned long *scan_mask);
int (*debugfs_reg_access)(struct iio_dev *indio_dev,
unsigned reg, unsigned writeval,
unsigned *readval);
};

/**
Expand Down Expand Up @@ -332,7 +335,9 @@ struct iio_buffer_setup_ops {
* @groups: [INTERN] attribute groups
* @groupcounter: [INTERN] index of next attribute group
* @flags: [INTERN] file ops related flags including busy flag.
**/
* @debugfs_dentry: [INTERN] device specific debugfs dentry.
* @cached_reg_addr: [INTERN] cached register address for debugfs reads.
*/
struct iio_dev {
int id;

Expand Down Expand Up @@ -366,6 +371,10 @@ struct iio_dev {
int groupcounter;

unsigned long flags;
#if defined(CONFIG_DEBUG_FS)
struct dentry *debugfs_dentry;
unsigned cached_reg_addr;
#endif
};

/**
Expand Down Expand Up @@ -443,4 +452,20 @@ static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
& (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE);
};

/**
* iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
* @indio_dev: IIO device info structure for device
**/
#if defined(CONFIG_DEBUG_FS)
static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
{
return indio_dev->debugfs_dentry;
};
#else
static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
{
return NULL;
};
#endif

#endif /* _INDUSTRIAL_IO_H_ */
137 changes: 136 additions & 1 deletion trunk/drivers/staging/iio/industrialio-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/anon_inodes.h>
#include <linux/debugfs.h>
#include "iio.h"
#include "iio_core.h"
#include "iio_core_trigger.h"
Expand All @@ -39,6 +40,8 @@ struct bus_type iio_bus_type = {
};
EXPORT_SYMBOL(iio_bus_type);

static struct dentry *iio_debugfs_dentry;

static const char * const iio_data_type_name[] = {
[IIO_RAW] = "raw",
[IIO_PROCESSED] = "input",
Expand Down Expand Up @@ -129,6 +132,8 @@ static int __init iio_init(void)
goto error_unregister_bus_type;
}

iio_debugfs_dentry = debugfs_create_dir("iio", NULL);

return 0;

error_unregister_bus_type:
Expand All @@ -142,6 +147,84 @@ static void __exit iio_exit(void)
if (iio_devt)
unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
bus_unregister(&iio_bus_type);
debugfs_remove(iio_debugfs_dentry);
}

#if defined(CONFIG_DEBUG_FS)
static int iio_debugfs_open(struct inode *inode, struct file *file)
{
if (inode->i_private)
file->private_data = inode->i_private;

return 0;
}

static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
size_t count, loff_t *ppos)
{
struct iio_dev *indio_dev = file->private_data;
char buf[20];
unsigned val = 0;
ssize_t len;
int ret;

ret = indio_dev->info->debugfs_reg_access(indio_dev,
indio_dev->cached_reg_addr,
0, &val);
if (ret)
dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);

len = snprintf(buf, sizeof(buf), "0x%X\n", val);

return simple_read_from_buffer(userbuf, count, ppos, buf, len);
}

static ssize_t iio_debugfs_write_reg(struct file *file,
const char __user *userbuf, size_t count, loff_t *ppos)
{
struct iio_dev *indio_dev = file->private_data;
unsigned reg, val;
char buf[80];
int ret;

count = min_t(size_t, count, (sizeof(buf)-1));
if (copy_from_user(buf, userbuf, count))
return -EFAULT;

buf[count] = 0;

ret = sscanf(buf, "%i %i", &reg, &val);

switch (ret) {
case 1:
indio_dev->cached_reg_addr = reg;
break;
case 2:
indio_dev->cached_reg_addr = reg;
ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
val, NULL);
if (ret) {
dev_err(indio_dev->dev.parent, "%s: write failed\n",
__func__);
return ret;
}
break;
default:
return -EINVAL;
}

return count;
}

static const struct file_operations iio_debugfs_reg_fops = {
.open = iio_debugfs_open,
.read = iio_debugfs_read_reg,
.write = iio_debugfs_write_reg,
};

static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
{
debugfs_remove_recursive(indio_dev->debugfs_dentry);
}

static ssize_t iio_read_channel_ext_info(struct device *dev,
Expand Down Expand Up @@ -171,6 +254,49 @@ static ssize_t iio_write_channel_ext_info(struct device *dev,
return ext_info->write(indio_dev, this_attr->c, buf, len);
}

static int iio_device_register_debugfs(struct iio_dev *indio_dev)
{
struct dentry *d;

if (indio_dev->info->debugfs_reg_access == NULL)
return 0;

if (IS_ERR(iio_debugfs_dentry))
return 0;

indio_dev->debugfs_dentry =
debugfs_create_dir(dev_name(&indio_dev->dev),
iio_debugfs_dentry);
if (IS_ERR(indio_dev->debugfs_dentry))
return PTR_ERR(indio_dev->debugfs_dentry);

if (indio_dev->debugfs_dentry == NULL) {
dev_warn(indio_dev->dev.parent,
"Failed to create debugfs directory\n");
return -EFAULT;
}

d = debugfs_create_file("direct_reg_access", 0644,
indio_dev->debugfs_dentry,
indio_dev, &iio_debugfs_reg_fops);
if (!d) {
iio_device_unregister_debugfs(indio_dev);
return -ENOMEM;
}

return 0;
}
#else
static int iio_device_register_debugfs(struct iio_dev *indio_dev)
{
return 0;
}

static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
{
}
#endif /* CONFIG_DEBUG_FS */

static ssize_t iio_read_channel_info(struct device *dev,
struct device_attribute *attr,
char *buf)
Expand Down Expand Up @@ -618,6 +744,7 @@ static void iio_dev_release(struct device *device)
iio_device_unregister_trigger_consumer(indio_dev);
iio_device_unregister_eventset(indio_dev);
iio_device_unregister_sysfs(indio_dev);
iio_device_unregister_debugfs(indio_dev);
}

static struct device_type iio_dev_type = {
Expand Down Expand Up @@ -734,11 +861,17 @@ int iio_device_register(struct iio_dev *indio_dev)
/* configure elements for the chrdev */
indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);

ret = iio_device_register_debugfs(indio_dev);
if (ret) {
dev_err(indio_dev->dev.parent,
"Failed to register debugfs interfaces\n");
goto error_ret;
}
ret = iio_device_register_sysfs(indio_dev);
if (ret) {
dev_err(indio_dev->dev.parent,
"Failed to register sysfs interfaces\n");
goto error_ret;
goto error_unreg_debugfs;
}
ret = iio_device_register_eventset(indio_dev);
if (ret) {
Expand All @@ -765,6 +898,8 @@ int iio_device_register(struct iio_dev *indio_dev)
iio_device_unregister_eventset(indio_dev);
error_free_sysfs:
iio_device_unregister_sysfs(indio_dev);
error_unreg_debugfs:
iio_device_unregister_debugfs(indio_dev);
error_ret:
return ret;
}
Expand Down

0 comments on commit 4ff6e6b

Please sign in to comment.