天天看點

【Linux裝置驅動】--0x01簡單的字元裝置子產品源檔案Makefile

源檔案

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>

static int g_simple_cdev_major_no;
static struct class *g_simple_cdev_class;
static struct device *g_simple_cdev_device;

static int simple_cdev_open(struct inode *inode, struct file *file)
{
    return 0;
}

static int simple_cdev_release(struct inode *inode, struct file *file)
{
    return 0;
}

static int simple_cdev_mmap(struct file *file, struct vm_area_struct *vma)
{
    return 0;
}

/* File operations struct for character device */
static const struct file_operations g_simple_cdev_fops = {
    .owner   = THIS_MODULE,
    .open    = simple_cdev_open,
    .release = simple_cdev_release,
    .mmap    = simple_cdev_mmap,
};

static int __init simple_cdev_init(void)
{
    g_simple_cdev_major_no = register_chrdev(0, "simple_cdev", &g_simple_cdev_fops);
    g_simple_cdev_class  = class_create(THIS_MODULE, "simple_cdev");
    g_simple_cdev_device = device_create(g_simple_cdev_class, NULL,
        MKDEV(g_simple_cdev_major_no, 0), NULL, "simple_cdev");

    return 0;
}

static void __exit simple_cdev_exit(void)
{
    printk(KERN_INFO "Goodbye world\n");
}

MODULE_LICENSE("GPL");
module_init(simple_cdev_init);
module_exit(simple_cdev_exit);           
  1. 由regiser_chrdev自動申請某個裝置号
  2. 由class_create和device_create自動生成字元裝置結點/dev/simple_cdev,裝置号從regiset_chrdev擷取

Makefile

obj-m     := simple_cdev.o
PWD       := $(shell pwd)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build

build: 
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

install:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) clean
           

繼續閱讀