Heng30的博客
搜索 分类 关于 订阅

Linux驱动如何创建platform_device?

2025-03-28

struct platform_device 是Linux内核中用于表示平台设备的重要数据结构,它是Linux设备模型的一部分,特别用于那些不通过传统总线(如 PCI、USB 等)连接的设备。

设备代码

// device.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>

#define LED1_CTL 0
#define LED2_CTL 1
#define SIGNAL_USER 3

// 这里的地址需要根据嵌入式开发板或外部设备进行设置
#define ADDR_START 0x0
#define DEV_NAME "my_device_001"

struct resource my_resource[] = {
    [0] =
        {
            .start = ADDR_START,     // 需要根据实际情况填写
            .end = ADDR_START + 0x0, // 需要根据实际情况填写
            .name = "LED1",
            .flags = IORESOURCE_MEM,
            .desc = LED1_CTL,
        },
    [1] =
        {
            .start = ADDR_START + 0x0, // 需要根据实际情况填写
            .end = ADDR_START + 0x0,   // 需要根据实际情况填写
            .name = "LED2",
            .flags = IORESOURCE_MEM,
            .desc = LED2_CTL,
        },
    [2] =
        {
            .start = 67, // 需要根据实际情况填写
            .end = 67,   // 需要根据实际情况填写
            .name = "SIGNAL_USER",
            .flags = IORESOURCE_IRQ,
            .desc = SIGNAL_USER,
        },
};

static void _release(struct device *dev) {
    pr_info("platform_device_release\n");
}

struct platform_device my_platform_dev = {
    .name = DEV_NAME,
    .id = PLATFORM_DEVID_AUTO,
    .resource = my_resource,
    .num_resources = 3,
    .dev =
        {
            .release = _release,
        },
};

static int __init platform_device_init(void) {
    pr_info("platform_device_init\n");

    // 会在`/sys/bus/platform/devices/`目录下创建对应设备
    int ret = platform_device_register(&my_platform_dev);
    if (ret < 0) {
        pr_err("platform_device_register failed\n");
        return -1;
    }
    return 0;
}

static void __exit platform_device_exit(void) {
    pr_info("platform_device_exit\n");
    platform_device_unregister(&my_platform_dev);
}

module_init(platform_device_init);
module_exit(platform_device_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("heng30");
MODULE_VERSION("v0.0.1");
MODULE_DESCRIPTION("A simple platform_device kernel module");

驱动代码

// driver.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>

// 这个名称要与`struct platform_device`定义的一致
#define PLATFORM_DEVICE_NAME "my_device_001"

// 当安装该驱动并名称匹配成功后,平台设备驱动会自动调用该函数
static int _probe(struct platform_device *device) {
    pr_info("platform_driver_probe\n");
    //TODO:初始化逻辑,创建设备号,设备类,设备文件等操作
    return 0;
}

// 当平台设备驱动卸载时,会自动调用该函数
static void _remove(struct platform_device *device) {
    pr_info("platform_driver_remove\n");
}

struct platform_driver my_platform_driver = {
    .probe = _probe,
    .remove = _remove,
    .driver =
        {
            .name = PLATFORM_DEVICE_NAME,
        },
};

static int __init platform_driver_init(void) {
    pr_info("platform_driver_init\n");

    int ret = platform_driver_register(&my_platform_driver);
    if (ret < 0) {
        pr_info("platform_driver_register failed\n");
        return -1;
    }
    return 0;
}

static void __exit platform_driver_exit(void) {
    pr_info("platform_driver_exit\n");
    platform_driver_unregister(&my_platform_driver);
}

module_init(platform_driver_init);
module_exit(platform_driver_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("heng30");
MODULE_VERSION("v0.0.1");
MODULE_DESCRIPTION("A simple platform_driver kernel module");

编译脚本

#!/bin/sh

top-dir = $(shell pwd)
kernel-version = $(shell uname -r)
kernel-dir ?= /lib/modules/$(kernel-version)/build

obj-m += driver.o device.o

all:
        make -C $(kernel-dir) modules M=$(top-dir)

clean:
        rm -f *.o *.ko *.mod *.mod.c *.order *.symvers *.dtbo
        make -C $(kernel-dir) clean m=$(top-dir)

测试

  • 编译:make

  • 安装驱动:

insmod device.ko
insmod driver.ko
  • 卸载驱动:
rmmod driver.ko
rmmod device.ko
  • 查看输出:dmesg
[318391.882255] platform_device_init
[318398.132449] platform_driver_init
[318398.132537] platform_driver_probe
[318413.135957] platform_driver_exit
[318413.136000] platform_driver_remove
[318420.380824] platform_device_exit
[318420.380953] platform_device_release