日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

怎样在Linux驱动中创建proc节点(示例)

發布時間:2023/12/20 linux 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 怎样在Linux驱动中创建proc节点(示例) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、定義proc節點的讀、寫函數

static int tp_switch_writeproc(struct file *file,const char *buffer,

?????????????????????????? unsigned long count,void *data)

{

? ? ? ??sscanf(buffer,"%d", &tp_dbg);

? ? ? ? printk("tpd: proc-->tp_dbg = %d\n", tp_dbg);

? ? ? ? return count;

}

?

static int tp_switch_readproc(char *page, char **start, off_t off,

??????????????????????????????????? int count,int *eof, void *data)

{

? ? ? ? int len;

? ? ? ? unsigned char tmp =tp_dbg&0x0F;

? ? ? ? len = sprintf(page,"%c\n", tmp);

? ? ? ??return 2;

}

?

二、驅動加載時創建proc節點的入口

#include <linux/proc_fs.h>
static struct proc_dir_entry *tp_root; ?
static struct proc_dir_entry *debug_entry; ?
#define USER_ROOT_DIR "tp_debug" ?
#define USER_ENTRY1 ? "debug_switch"

staticint goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)

{

? ? ? ? … …

? ? ? ? init_debug_port();?? //創建proc調試節點

? ? ? ? … …

}


static int init_debug_port(void)

{

? ? ? ? pt_root =proc_mkdir(USER_ROOT_DIR, NULL);?

? ? ? ? if (NULL==pt_root)?

? ? ? ? {?

? ? ? ? ? ? ? ??printk(KERN_ALERT"Create dir /proc/%s error!\n",?USER_ROOT_DIR);?

? ? ? ? ? ? ? ? return -1;?

? ? ? ? }?

? ? ? ? printk(KERN_INFO"Create dir /proc/%s\n", USER_ROOT_DIR);?

?

? ? ? ? // Create a test entryunder USER_ROOT_DIR?

? ? ? ? pt_entry1 =create_proc_entry(USER_ENTRY1, 0666, pt_root);?

? ? ? ? if (NULL ==pt_entry1)?

? ? ? ? {?

? ? ? ? ? ? ? ??printk(KERN_ALERT"Create entry %s under /proc/%s error!\n",?

??????????????????????????????????????????? USER_ENTRY1,USER_ROOT_DIR);?

? ? ? ? ? ? ? ? goto err_out;?

? ? ? ? ?}

?

? ? ? ? ?pt_entry1->write_proc= tp_switch_writeproc;

? ? ? ? ?pt_entry1->read_proc =tp_switch_readproc;

? ? ? ? ?printk(KERN_INFO"Create /proc/%s/%s\n",?

?????????????????? USER_ROOT_DIR,USER_ENTRY1);??

?

? ? ? ? ?return 0;?

err_out:?

? ? ? ? ?pt_entry1->read_proc =NULL;?

? ? ? ? ?pt_entry1->write_proc= NULL;

? ? ? ? ?remove_proc_entry(USER_ROOT_DIR,pt_root);?

? ? ? ? ?return -1;?

}

?

三、卸載驅動時刪除proc節點

static void remove_debug_port(void)

{

? ? ? ? remove_proc_entry(USER_ENTRY1,pt_root);?

? ? ? ? remove_proc_entry(USER_ROOT_DIR,NULL);

}

?

static int goodix_ts_remove(struct i2c_client *client)

{

? ? ? ? … …

? ? ? ? remove_debug_port();

? ? ? ? … …

}

總結

以上是生活随笔為你收集整理的怎样在Linux驱动中创建proc节点(示例)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。