只是在创建 Linux 驱动程序方面迈出了第一步。终于得到了这个工作:
#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");
}
仍在测试中,所以到目前为止内容/目的并不是那么重要,但是......在安装 GetMonth.ko 之后,我可以在 lsmod-List 中看到它。我还看到了 kprintft 工作和 /proc 文档。
但是我很震惊在 /dev 中没有看到我的驱动程序的任何内容?!
怎么可能?不是每个驱动程序都在这个目录中创建一个文件吗?