歡迎您光臨本站 註冊首頁

關於linux設備模型 device 的問題 求高人解答啊

←手機掃碼閱讀     火星人 @ 2014-03-26 , reply:0

#include
#include
#include
#include
#include


MODULE_LICENSE("GPL");

static void my_dev_release(struct device *dev)
{

}

struct device my_dev = {
.init_name= "my_dev",
.release = my_dev_release,
};


static ssize_t mydev_show(struct device *dev, char *buf)
{
int ret;
printk("<0> call the my_dev_show() !!!");
ret=sprintf(buf,"%s\n","this is my_dev");
printk("<0> the buf is : %s \n",buf);
return ret;

}


static DEVICE_ATTR(dev,S_IRUGO,mydev_show,NULL);

static int my_dev_init(void)
{
int ret;

ret=device_register(&my_dev); //設備註冊
ret=device_create_file(&my_dev,&dev_attr_dev); //創建my_dev下的屬性文件
return ret;
}

static void my_dev_exit(void)
{
device_unregister(&my_dev); //註銷 設備
}

module_init(my_dev_init);
module_exit(my_dev_exit);



這是一段比較短的代碼 是創建device的一個例子
我的linux系統是 3.1.0版本的 將內核模塊安裝后, 在/sys/devices/ 目錄下生成my_dev 目錄 在my_dev/目錄下
生成了 dev文件。
用cat 打開這個dev文件 正確的現象是 讀出this is my_dev 顯示到終端上
但是我實際執行的時候 cat /sys/devices/my_dev/dev 后什麼都沒有顯示, 過幾秒鐘后就死機了

printk("<0> call the my_dev_show() !!!");
printk("<0> the buf is : %s \n",buf);
這兩句是我加的 在實際cat 后 屏幕上出現
call the my_dev_show() !!!
the buf is :this is my_dev

證明正確的調用到了show 函數, buf也確實被正確賦值 但是就是不能顯示到終端上
甚至死機 求高手幫忙解答到底是什麼原因啊

[火星人 ] 關於linux設備模型 device 的問題 求高人解答啊已經有367次圍觀

http://coctec.com/docs/linux/show-post-183215.html