本文共 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/