使用sys接口来调试驱动

 在调试cyttsp4驱动时,发现sys接口非常有用,便将相关的代码抽取出来。

 代码如下sys.c

#include <linux/module.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/device.h>

#define DEVICE_NAME "select"
static struct miscdevice poll_dev;

ssize_t sys_read(struct device *dev, struct device_attribute *attr,char *buf)
{
	printk("%s\n",__FUNCTION__); 
	return 0;
}

ssize_t sys_write(struct device *dev, struct device_attribute *attr,const char *buf, size_t count)
{
	printk("%s\n",__FUNCTION__);
	return count;
}

static struct device_attribute attributes[] = {
	__ATTR(sys_iface_one,S_IRUGO, sys_read, NULL),
	__ATTR(sys_iface_two,S_IWUSR, NULL, sys_write),
	__ATTR(sys_iface_three,S_IRUSR | S_IWUSR, sys_read,sys_write),
};

static int add_sysfs_interfaces(struct device *dev)
{
	int i;
	for (i = 0; i < ARRAY_SIZE(attributes); i++)
		if (device_create_file(dev, attributes + i))
			goto undo;

	return 0;
undo:
	for (i--; i >= 0 ; i--)
		device_remove_file(dev, attributes + i);
	dev_err(dev, "%s: failed to create sysfs interface\n", __func__);
	return -ENODEV;
}

static int dev_open(struct inode *inode, struct file *file)
{
	return 0;
}

static int dev_release(struct inode *node, struct file *filp)
{
	return 0;
}

static struct file_operations poll_dev_fops={
	.owner          = THIS_MODULE,
	.open           = dev_open,
	.release        = dev_release,
};


static struct miscdevice poll_dev = {
	.minor          = MISC_DYNAMIC_MINOR,
	.name           = DEVICE_NAME,
	.fops           = &poll_dev_fops,
};

static int __init poll_init(void) 
{	
	int ret;
	printk("%s\n",__func__);
	ret = misc_register(&poll_dev);
	add_sysfs_interfaces(poll_dev.this_device);
	return ret;
}

static void __exit poll_exit(void)
{
	misc_deregister(&poll_dev);
	printk("%s\n",__func__);
}

module_init(poll_init);
module_exit(poll_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("www");

 

安装驱动后,在Ubuntu下路径/sys/devices/virtual/misc会出现如下文件,就可以用echo和cat来读写文件了。

drwxr-xr-x  3 root root    0 9月   4 20:22 .
drwxr-xr-x 24 root root    0 9月   4 20:22 ..
-r--r--r--  1 root root 4096 9月   4 20:22 dev
drwxr-xr-x  2 root root    0 9月   4 20:22 power
lrwxrwxrwx  1 root root    0 9月   4 20:22 subsystem -> ../../../../class/misc
-r--r--r--  1 root root 4096 9月   4 20:22 sys_iface_one
-rw-------  1 root root 4096 9月   4 20:20 sys_iface_three
--w-------  1 root root 4096 9月   4 20:21 sys_iface_two
-rw-r--r--  1 root root 4096 9月   4 20:22 uevent


在我们调试驱动的时候,只需在sys_write和sys_read中填充要跑的函数,通过读写就可以达到调试的目的。

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值