Linux内核模块的Makefile?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4133338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:54:51  来源:igfitidea点击:

Makefile for Linux kernel module?

linuxmakefilelinux-kernellinux-device-driver

提问by matcheek

I was just reading The Linux Kernel Module Programming Guide and and got stuck on character device drivers example.

我只是在阅读 The Linux Kernel Module Programming Guide 并且被困在字符设备驱动程序示例上

Makefiles for previous examples were provided, but not for this one, so I'm trying to make one:

提供了先前示例的 Makefile,但没有提供此示例,因此我正在尝试制作一个:

obj-m += chardev.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

The output I'm getting is:

我得到的输出是:

maciej@jadwiga:~/Projects/os/chardev$ make
make -C /lib/modules/2.6.26-2-686/build M=/home/maciej/Projects/os/chardev modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.26-2-686'
  CC [M]  /home/maciej/Projects/os/chardev/chardev.o
/home/maciej/Projects/os/chardev/chardev.c: In function ‘cleanup_module':
/home/maciej/Projects/os/chardev/chardev.c:72: error: void value not ignored as it ought to be
make[2]: *** [/home/maciej/Projects/os/chardev/chardev.o] Error 1
make[1]: *** [_module_/home/maciej/Projects/os/chardev] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-2.6.26-2-686'
make: *** [all] Error 2

Can anybody help?

有人可以帮忙吗?

The file I want to compile:

我要编译的文件:

/*
 *  chardev.c: Creates a read-only char device that says how many times
 *  you've read from the dev file
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>    /* for put_user */

/*  
 *  Prototypes - this would normally go in a .h file
 */
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);

#define SUCCESS 0
#define DEVICE_NAME "chardev"   /* Dev name as it appears in /proc/devices   */
#define BUF_LEN 80      /* Max length of the message from the device */

/* 
 * Global variables are declared as static, so are global within the file. 
 */

static int Major;       /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open?  
                 * Used to prevent multiple access to device */
static char msg[BUF_LEN];   /* The msg the device will give when asked */
static char *msg_Ptr;

static struct file_operations fops = {
    .read = device_read,
    .write = device_write,
    .open = device_open,
    .release = device_release
};

/*
 * This function is called when the module is loaded
 */
int init_module(void)
{
        Major = register_chrdev(0, DEVICE_NAME, &fops);

    if (Major < 0) {
      printk(KERN_ALERT "Registering char device failed with %d\n", Major);
      return Major;
    }

    printk(KERN_INFO "I was assigned major number %d. To talk to\n", Major);
    printk(KERN_INFO "the driver, create a dev file with\n");
    printk(KERN_INFO "'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, Major);
    printk(KERN_INFO "Try various minor numbers. Try to cat and echo to\n");
    printk(KERN_INFO "the device file.\n");
    printk(KERN_INFO "Remove the device file and module when done.\n");

    return SUCCESS;
}

/*
 * This function is called when the module is unloaded
 */
void cleanup_module(void)
{
    /* 
     * Unregister the device 
     */
    int ret = unregister_chrdev(Major, DEVICE_NAME);
    if (ret < 0)
        printk(KERN_ALERT "Error in unregister_chrdev: %d\n", ret);
}

/*
 * Methods
 */

/* 
 * Called when a process tries to open the device file, like
 * "cat /dev/mycharfile"
 */
static int device_open(struct inode *inode, struct file *file)
{
    static int counter = 0;

    if (Device_Open)
        return -EBUSY;

    Device_Open++;
    sprintf(msg, "I already told you %d times Hello world!\n", counter++);
    msg_Ptr = msg;
    try_module_get(THIS_MODULE);

    return SUCCESS;
}

/* 
 * Called when a process closes the device file.
 */
static int device_release(struct inode *inode, struct file *file)
{
    Device_Open--;      /* We're now ready for our next caller */

    /* 
     * Decrement the usage count, or else once you opened the file, you'll
     * never get get rid of the module. 
     */
    module_put(THIS_MODULE);

    return 0;
}

/* 
 * Called when a process, which already opened the dev file, attempts to
 * read from it.
 */
static ssize_t device_read(struct file *filp,   /* see include/linux/fs.h   */
               char *buffer,    /* buffer to fill with data */
               size_t length,   /* length of the buffer     */
               loff_t * offset)
{
    /*
     * Number of bytes actually written to the buffer 
     */
    int bytes_read = 0;

    /*
     * If we're at the end of the message, 
     * return 0 signifying end of file 
     */
    if (*msg_Ptr == 0)
        return 0;

    /* 
     * Actually put the data into the buffer 
     */
    while (length && *msg_Ptr) {

        /* 
         * The buffer is in the user data segment, not the kernel 
         * segment so "*" assignment won't work.  We have to use 
         * put_user which copies data from the kernel data segment to
         * the user data segment. 
         */
        put_user(*(msg_Ptr++), buffer++);

        length--;
        bytes_read++;
    }

    /* 
     * Most read functions return the number of bytes put into the buffer
     */
    return bytes_read;
}

/*  
 * Called when a process writes to dev file: echo "hi" > /dev/hello 
 */
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
    printk(KERN_ALERT "Sorry, this operation isn't supported.\n");
    return -EINVAL;
}

采纳答案by Ignacio Vazquez-Abrams

obj-m += chardev.o

回答by ranganath

actually driver gets loaded by the macro MODULE_INIT(my_init); here "my_init" can be the user specified function and MUDULE_EXIT(my_cleanup); here my_exitcan be the user specified function . those two macros are missing in the above code.

实际上驱动程序是由宏 MODULE_INIT(my_init) 加载的;这里“ my_init”可以是用户指定的函数和MUDULE_EXIT(my_cleanup);这里my_exit可以是用户指定的函数。上面的代码中缺少这两个宏。

Makefile: the obj-m += chardev.ois to be replaced with obj-m := chardev.o.

Makefile:obj-m += chardev.o将替换为obj-m := chardev.o

I am also a beginner to LLD. If any mistake is there please let me know.

我也是 LLD 的初学者。如果有任何错误,请告诉我。

回答by Lam Hai Son

Because the function unregister_chrdev() don't return value. This is a helpful link. http://forum.kernelnewbies.org/read.php?17,1114

因为函数 unregister_chrdev() 不返回值。这是一个有用的链接。http://forum.kernelnewbies.org/read.php?17,1114