博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
创建一个简单的debugfs文件系统节点
阅读量:4283 次
发布时间:2019-05-27

本文共 1275 字,大约阅读时间需要 4 分钟。

有时为了调试方便,需要创建一个文件节点,供上层调用,下面是一个较简单的例子;

可以在其基础上,稍加修改名字,即可使用。

static ssize_t usb_hnp_show(struct file *file, char __user *user_buf,				     size_t count, loff_t *ppos){//	char *buf;	ssize_t ret;	struct usb_device *udev = global_usb_device;	if (udev == NULL)	{		printk(KERN_ERR "%s:the usb_device is NULL\n", __func__);		return -EFAULT;	}	dev_err(&udev->dev, "zsm %s\n", __func__);	return ret;}static ssize_t usb_hnp_store(struct file *file,				      const char __user *user_buf, size_t count,				      loff_t *ppos){	char buf[32];	ssize_t buf_size;	struct usb_device *udev = global_usb_device;	if (udev == NULL)	{		printk(KERN_ERR "%s:the usb_device is NULL\n", __func__);		return -EFAULT;	}	dev_err(&udev->dev, "zsm %s\n", __func__);	buf_size = min(count, (size_t)(sizeof(buf)-1));	if (copy_from_user(buf, user_buf, buf_size)) {		dev_err(&udev->dev, "Failed to copy from user\n");		return -EFAULT;	}	buf[buf_size] = 0;	return buf_size;}static const struct file_operations usb_hnp_node_fops = {	.owner = THIS_MODULE,	.open = simple_open,	.read = usb_hnp_show,	.write = usb_hnp_store,};
注册部分代码可以放在一个probe函数里,如下:

struct dentry *usb_hnp_dentry;	usb_hnp_dentry = debugfs_create_file("enable_usb_hnp",					S_IRUGO, NULL, NULL, &usb_hnp_node_fops);

编译运行后,生成的目录在

/sys/kernel/debug/ 下。

转载地址:http://pnngi.baihongyu.com/

你可能感兴趣的文章
svn : how to set the executable bit on a file?
查看>>
vim 取代指令
查看>>
git 修改過檔案後,如何commit上server
查看>>
git log 應用
查看>>
Git 版本控制系統(3) 還沒 push 前可以做的事
查看>>
Git 基礎 - 檢視提交的歷史記錄
查看>>
wifi 連ap command
查看>>
git clean reset checkout
查看>>
[轉載]6個超強網站讓你查到最道地的英文
查看>>
HUB 與 Switch 差別
查看>>
linux產生 core dump文件方法及設置
查看>>
How to pass macro definition from “makefile” command line arguments to C source code?
查看>>
英文句型
查看>>
mtd and /dev/mtd*相關資料
查看>>
cp: cannot create symbolic link to fat format of usb: Operation not permitted
查看>>
MTD bad Block issue
查看>>
How to change network interface name
查看>>
ubifs and ubi and mtd
查看>>
shell script set 用法
查看>>
英文序數寫法與唸法 Ordinal Numbers(轉載)
查看>>