Skip to content

Commit e553f18

Browse files
mhennerichgregkh
authored andcommitted
staging: iio: core: Introduce debugfs support, add support for direct register access
Changes since V1: Exclude iio debugfs code in case CONFIG_DEBUG_FS isn't enabled. Introduce helper function iio_get_debugfs_dentry. Document additions to struct iio_dev iio_debugfs_read_reg: Use snprintf. Use a shorter fixed length. Introduce len instead of pointer math. iio_debugfs_write_reg: Fix return value use PT_ERR. Changes since V2: Use debugfs_remove. Fix whitespace damage. Signed-off-by: Michael Hennerich <michael.hennerich@analog.com> Acked-by: Jonathan Cameron <jic23@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4abf6f8 commit e553f18

2 files changed

Lines changed: 162 additions & 2 deletions

File tree

‎drivers/staging/iio/iio.h‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ struct iio_info {
286286
struct iio_trigger *trig);
287287
int (*update_scan_mode)(struct iio_dev *indio_dev,
288288
const unsigned long *scan_mask);
289+
int (*debugfs_reg_access)(struct iio_dev *indio_dev,
290+
unsigned reg, unsigned writeval,
291+
unsigned *readval);
289292
};
290293

291294
/**
@@ -332,7 +335,9 @@ struct iio_buffer_setup_ops {
332335
* @groups: [INTERN] attribute groups
333336
* @groupcounter: [INTERN] index of next attribute group
334337
* @flags: [INTERN] file ops related flags including busy flag.
335-
**/
338+
* @debugfs_dentry: [INTERN] device specific debugfs dentry.
339+
* @cached_reg_addr: [INTERN] cached register address for debugfs reads.
340+
*/
336341
struct iio_dev {
337342
int id;
338343

@@ -366,6 +371,10 @@ struct iio_dev {
366371
int groupcounter;
367372

368373
unsigned long flags;
374+
#if defined(CONFIG_DEBUG_FS)
375+
struct dentry *debugfs_dentry;
376+
unsigned cached_reg_addr;
377+
#endif
369378
};
370379

371380
/**
@@ -443,4 +452,20 @@ static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
443452
& (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE);
444453
};
445454

455+
/**
456+
* iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
457+
* @indio_dev: IIO device info structure for device
458+
**/
459+
#if defined(CONFIG_DEBUG_FS)
460+
static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
461+
{
462+
return indio_dev->debugfs_dentry;
463+
};
464+
#else
465+
static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
466+
{
467+
return NULL;
468+
};
469+
#endif
470+
446471
#endif /* _INDUSTRIAL_IO_H_ */

‎drivers/staging/iio/industrialio-core.c‎

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/cdev.h>
2323
#include <linux/slab.h>
2424
#include <linux/anon_inodes.h>
25+
#include <linux/debugfs.h>
2526
#include "iio.h"
2627
#include "iio_core.h"
2728
#include "iio_core_trigger.h"
@@ -39,6 +40,8 @@ struct bus_type iio_bus_type = {
3940
};
4041
EXPORT_SYMBOL(iio_bus_type);
4142

43+
static struct dentry *iio_debugfs_dentry;
44+
4245
static const char * const iio_data_type_name[] = {
4346
[IIO_RAW] = "raw",
4447
[IIO_PROCESSED] = "input",
@@ -129,6 +132,8 @@ static int __init iio_init(void)
129132
goto error_unregister_bus_type;
130133
}
131134

135+
iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
136+
132137
return 0;
133138

134139
error_unregister_bus_type:
@@ -142,6 +147,84 @@ static void __exit iio_exit(void)
142147
if (iio_devt)
143148
unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
144149
bus_unregister(&iio_bus_type);
150+
debugfs_remove(iio_debugfs_dentry);
151+
}
152+
153+
#if defined(CONFIG_DEBUG_FS)
154+
static int iio_debugfs_open(struct inode *inode, struct file *file)
155+
{
156+
if (inode->i_private)
157+
file->private_data = inode->i_private;
158+
159+
return 0;
160+
}
161+
162+
static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
163+
size_t count, loff_t *ppos)
164+
{
165+
struct iio_dev *indio_dev = file->private_data;
166+
char buf[20];
167+
unsigned val = 0;
168+
ssize_t len;
169+
int ret;
170+
171+
ret = indio_dev->info->debugfs_reg_access(indio_dev,
172+
indio_dev->cached_reg_addr,
173+
0, &val);
174+
if (ret)
175+
dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
176+
177+
len = snprintf(buf, sizeof(buf), "0x%X\n", val);
178+
179+
return simple_read_from_buffer(userbuf, count, ppos, buf, len);
180+
}
181+
182+
static ssize_t iio_debugfs_write_reg(struct file *file,
183+
const char __user *userbuf, size_t count, loff_t *ppos)
184+
{
185+
struct iio_dev *indio_dev = file->private_data;
186+
unsigned reg, val;
187+
char buf[80];
188+
int ret;
189+
190+
count = min_t(size_t, count, (sizeof(buf)-1));
191+
if (copy_from_user(buf, userbuf, count))
192+
return -EFAULT;
193+
194+
buf[count] = 0;
195+
196+
ret = sscanf(buf, "%i %i", &reg, &val);
197+
198+
switch (ret) {
199+
case 1:
200+
indio_dev->cached_reg_addr = reg;
201+
break;
202+
case 2:
203+
indio_dev->cached_reg_addr = reg;
204+
ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
205+
val, NULL);
206+
if (ret) {
207+
dev_err(indio_dev->dev.parent, "%s: write failed\n",
208+
__func__);
209+
return ret;
210+
}
211+
break;
212+
default:
213+
return -EINVAL;
214+
}
215+
216+
return count;
217+
}
218+
219+
static const struct file_operations iio_debugfs_reg_fops = {
220+
.open = iio_debugfs_open,
221+
.read = iio_debugfs_read_reg,
222+
.write = iio_debugfs_write_reg,
223+
};
224+
225+
static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
226+
{
227+
debugfs_remove_recursive(indio_dev->debugfs_dentry);
145228
}
146229

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

257+
static int iio_device_register_debugfs(struct iio_dev *indio_dev)
258+
{
259+
struct dentry *d;
260+
261+
if (indio_dev->info->debugfs_reg_access == NULL)
262+
return 0;
263+
264+
if (IS_ERR(iio_debugfs_dentry))
265+
return 0;
266+
267+
indio_dev->debugfs_dentry =
268+
debugfs_create_dir(dev_name(&indio_dev->dev),
269+
iio_debugfs_dentry);
270+
if (IS_ERR(indio_dev->debugfs_dentry))
271+
return PTR_ERR(indio_dev->debugfs_dentry);
272+
273+
if (indio_dev->debugfs_dentry == NULL) {
274+
dev_warn(indio_dev->dev.parent,
275+
"Failed to create debugfs directory\n");
276+
return -EFAULT;
277+
}
278+
279+
d = debugfs_create_file("direct_reg_access", 0644,
280+
indio_dev->debugfs_dentry,
281+
indio_dev, &iio_debugfs_reg_fops);
282+
if (!d) {
283+
iio_device_unregister_debugfs(indio_dev);
284+
return -ENOMEM;
285+
}
286+
287+
return 0;
288+
}
289+
#else
290+
static int iio_device_register_debugfs(struct iio_dev *indio_dev)
291+
{
292+
return 0;
293+
}
294+
295+
static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
296+
{
297+
}
298+
#endif /* CONFIG_DEBUG_FS */
299+
174300
static ssize_t iio_read_channel_info(struct device *dev,
175301
struct device_attribute *attr,
176302
char *buf)
@@ -618,6 +744,7 @@ static void iio_dev_release(struct device *device)
618744
iio_device_unregister_trigger_consumer(indio_dev);
619745
iio_device_unregister_eventset(indio_dev);
620746
iio_device_unregister_sysfs(indio_dev);
747+
iio_device_unregister_debugfs(indio_dev);
621748
}
622749

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

864+
ret = iio_device_register_debugfs(indio_dev);
865+
if (ret) {
866+
dev_err(indio_dev->dev.parent,
867+
"Failed to register debugfs interfaces\n");
868+
goto error_ret;
869+
}
737870
ret = iio_device_register_sysfs(indio_dev);
738871
if (ret) {
739872
dev_err(indio_dev->dev.parent,
740873
"Failed to register sysfs interfaces\n");
741-
goto error_ret;
874+
goto error_unreg_debugfs;
742875
}
743876
ret = iio_device_register_eventset(indio_dev);
744877
if (ret) {
@@ -765,6 +898,8 @@ int iio_device_register(struct iio_dev *indio_dev)
765898
iio_device_unregister_eventset(indio_dev);
766899
error_free_sysfs:
767900
iio_device_unregister_sysfs(indio_dev);
901+
error_unreg_debugfs:
902+
iio_device_unregister_debugfs(indio_dev);
768903
error_ret:
769904
return ret;
770905
}

0 commit comments

Comments
 (0)