只是我在Linux驅動程序創建的第一步。得到這個工作最後:Linux驅動程序 - > insmod - > lsmod顯示驅動程序,但在/ dev中沒有條目?
#include <linux/version.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
#define DRIVER_AUTHOR "DirtyDiddy"
#define DRIVER_DESC "Ein Treiber der zuordnet Hexzahl -> Monatsname"
static unsigned int GM_major = 0;
static unsigned int GM_minor = 0;
static size_t count_w4proc = 0;
static size_t count_r4proc = 0;
MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
/*Listing 15*/
int GM_read_proc(char* page, char** start, off_t offset, int count, int* eof, void* data)
{
int len = 0, min;
len = snprintf(page, count, "Major number = %d \n"
"Minor number = %d \n"
"geschrieben = %d \n"
"gelesen = %d \n",
GM_major, GM_minor, count_w4proc, count_r4proc);
*eof = 1;
min = (len > count ? count : len);
return min;
}
/*Listing 16*/
int GM_open(struct inode* inode, struct file* filp)
{
GM_minor = iminor(inode);
printk(KERN_INFO "GM_open: Minor number = %d\n", GM_minor);
return 0;
}
int GM_close(struct inode* inode, struct file* filp)
{
printk(KERN_INFO "GM_close\n");
return 0;
}
/*Listing 17*/
ssize_t GM_read(struct file* filp, char*buf, size_t count, loff_t* t)
{
char kbuf[10];
unsigned long not_copied;
kbuf[0] =0x00;
printk(KERN_INFO "gm_read: count = %d\n", count);
not_copied = copy_to_user(buf, kbuf, 10);
if(not_copied != 0) return (-EFAULT);
count_r4proc++;
return 10;
}
/*Listing 18*/
ssize_t GM_write(struct file* filp, char*buf, size_t count, loff_t* t)
{
printk(KERN_INFO "gm_write: count = %d\n", count);
count_r4proc++;
return count;
}
/*Listing 19*/
struct file_operations GM_fops = {
.owner = THIS_MODULE,
.read = GM_read,
.write = GM_write,
.open = GM_open,
.release= GM_close
};
/*Listing 20*/
int init_module(void)
{
int result;
printk(KERN_INFO "init_module_gm\n");
result = register_chrdev(GM_major, "GetMonth", &GM_fops);
if(result < 0)
{
printk(KERN_INFO "GM_init_module: kein Zugriff auf Major number\n");
return result;
}
printk(KERN_INFO "GM_init_module: register _chrdev ok\n");
if(GM_major == 0) GM_major = result;
printk(KERN_INFO "GM_init_module: Major number = %d \n", GM_major);
create_proc_read_entry("GetMonth", 0, NULL, GM_read_proc, NULL);
return 0;
}
/*Listing 21*/
void cleanup_module(void)
{
printk(KERN_INFO "cleanup_module_gm : ungregister\n");
unregister_chrdev(GM_major, "GetMonth");
remove_proc_entry("GetMonth", NULL);
printk(KERN_INFO "GM_cleanup: Ende \n");
}
還在測試,所以內容/目的不是那麼重要,到目前爲止,但... insmoding的GetMonth.ko後,我可以看到它在lsmod的名單。我還看到了kprinft工作和/ proc文檔。
但是我很震驚沒有在/ dev中看到我的驅動程序?
那該怎麼辦?是不是每個驅動程序都在這個目錄下創建一個文件
來吧,沒有人多年來這樣做。我們有* udev *負責。 – 0andriy
@ 0andriy對不起,我對內核模塊編程也有點新,而且我的老師告訴我們要用'mknod'(udev可以失控告訴他) – user5954246
也許他是一個老學校的傢伙,他沒有得到(didn'喜歡)* udev *。很難找到現在不使用某種* udev *的發行版。 – 0andriy