建立一個簡單的debugfs檔案系統節點
阿新 • • 發佈:2019-02-09
有時為了除錯方便,需要建立一個檔案節點,供上層呼叫,下面是一個較簡單的例子;
可以在其基礎上,稍加修改名字,即可使用。
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/ 下。