QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1480|回复: 10

内核模块的编译和使用问题请教。

[复制链接]
发表于 2006-9-20 21:26:22 | 显示全部楼层 |阅读模式
斑竹大哥,我需要对一个设备做驱动程序,系统调用接口和硬件程序都做好了,但是研究和实验了7天,不知道怎样加入到内核或者动态加入到内核中去,希望应用程序能够通过这个系统调用使用设备。方法和步骤是什么呀?

谢谢!
 楼主| 发表于 2006-9-21 08:19:46 | 显示全部楼层
用 insmod h.o 后出现
“couldn't find the kernel version the modules was comiled for”
不知道怎样办?
h.o是有h.c编译成的,h.c主要程序为
int init_module()
{
     printk("hello!");
     return 1;
}
回复

使用道具 举报

发表于 2006-9-21 09:16:38 | 显示全部楼层
编译环境的内核版本跟加载环境的内核版本是不是一样的?
回复

使用道具 举报

 楼主| 发表于 2006-9-21 15:39:49 | 显示全部楼层
一样的。当场编译当场加载。
回复

使用道具 举报

发表于 2006-9-22 08:55:18 | 显示全部楼层
2.4还是2.6?你是怎么编译的?
回复

使用道具 举报

 楼主| 发表于 2006-9-22 09:09:40 | 显示全部楼层
大哥,这个问题解决了!
我是2.4.20-8,编译模块时候生成了2.4.20-8custom,所以必须要重新生成内核并启动这个内核,这样版本都是2.4.20-8custom。(不过为什么要加custom?)
另外,出现的问题是
1、pintk("\nHello!");这个语句好象没有什么反映,因为运行insmod h.o后屏幕什么也没有。
2、put_user或copy_to_user函数也不起作用,核外应用程序没有读到任何希望的数值,而且read(...)的返回值总是不是希望的。
望大哥指点!
回复

使用道具 举报

 楼主| 发表于 2006-9-22 10:38:48 | 显示全部楼层
接上面的另外一个问题:驱动程序接口要填写如下结构(fs.h里面的定义)

struct file_operations {
        struct module *owner;
        loff_t (*llseek) (struct file *, loff_t, int);
        ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
        ssize_t (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
        ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
        ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t, loff_t);
        int (*readdir) (struct file *, void *, filldir_t);
        unsigned int (*poll) (struct file *, struct poll_table_struct *);
        int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
        int (*mmap) (struct file *, struct vm_area_struct *);
        int (*open) (struct inode *, struct file *);
        int (*flush) (struct file *);
        int (*release) (struct inode *, struct file *);
        int (*fsync) (struct file *, struct dentry *, int datasync);
        int (*aio_fsync) (struct kiocb *, int datasync);
        int (*fasync) (int, struct file *, int);
        int (*lock) (struct file *, int, struct file_lock *);
        ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
        ........
        ........
我填写
struct file_operations test_fops = {
        NULL,
        NULL,
        ssize_t test_read,
        ssize_t test_test,
        NULL, /* test_readdir */
        NULL,
        NULL, /* test_ioctl */
        NULL, /* test_mmap */
        int test_open,
        NULL,
        int test_release,
        NULL, /* test_fsync */
        NULL, /* test_fasync */
        /* nothing more, fill with NULLs */
};

其中
static int test_read(struct inode *node, struct file *file, char *buf, int count)
{
...
...
为什么出现
warning: initialization from incompatible pointer type
这样的警告?
以上填写正确吗?
回复

使用道具 举报

发表于 2006-9-22 12:25:29 | 显示全部楼层
模块加载对内核版本要做检查,2.4.20-8custom和2.4.20-8会被认为是不同的版本,加--force参数应该可以强行加载,另外也可以在内核源码里把custom去掉,好像是在include/linux/version.h里面

dmesg可以看到printk打印的信息。或者在控制台执行insmod,也能看到。

我一般这样写:
static struct file_operations fop = {
open: test_open,
      release:test_release,
      ioctl:test_ioctl,
      mmap:test_mmap,
};
这样就不用考虑那些NULL填充了。
回复

使用道具 举报

 楼主| 发表于 2006-9-22 17:29:21 | 显示全部楼层
谢谢大哥!
1、我在控制台上运行的呀,怎么没有显示?(控制台是root登陆的终端吗?)
2、驱动程序的参数是什么?我看了示例是
    static int test_read(struct inode *node,  struct file *file,  char *buf,  int ount)
但是fs.h里面定义为
    ssize_t  (*read) (struct file *,  char __user *,  size_t,  loff_t *);
不知道怎么会不一致?怎样选择参数。
3、能否告诉参数的含义?

谢谢!
回复

使用道具 举报

 楼主| 发表于 2006-9-22 20:21:35 | 显示全部楼层
谢谢班主大哥,基本上通过了.只是必需dmesg才看见printk的信息.为什么不直接看见?

再次谢谢!
回复

使用道具 举报

发表于 2006-9-23 20:08:10 | 显示全部楼层
你看看grub配置文件里设的启动参数是不是有CONSOLE=xxxx,把这个去掉试试
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-5-2 04:08 , Processed in 0.058432 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表