Linux内核驱动头文件速查手册(老内核版)

适用范围:Linux 2.6.32 / 3.10 / 4.4 / 4.9 / 4.14 等老 LTS(嵌入式、工业设备、国产化平台常见)
⚠️ 关于版本分界:下表的分界版本以主线合并时间为准,各发行版可能打了 backport 补丁。
拿不准时一律用第 6 节的 grep 命令在你手上的内核源码里验证,不要凭本文档下结论。

新老内核区别

新内核文档直接套用到老内核,会遇到三类问题:

问题类型例子症状
头文件不存在linux/printk.h、linux/export.h、linux/device/class.hNo such file or directory
头文件存在但内容不同linux/sysfs.h 里没有 DEVICE_ATTR_RW、sysfs_emitundefined / 宏展开报错
头文件正确但结构体字段变了.ioctl vs .unlocked_ioctl、timer_list.function 签名unknown field / 函数指针类型不匹配

第二类和第三类最坑——编译报错信息指向的往往不是你真正要改的那一行。


头文件差异总表

以 2.6.32 为基准,列出老内核里不存在的头文件及其替代:

新内核头文件存在起始版本老内核替代方案
linux/printk.h5.18用 linux/kernel.h(printk、pr_* 都在里面)
linux/export.h3.2 左右用 linux/module.h(EXPORT_SYMBOL 在其中)
linux/device/class.h6.x用 linux/device.h
linux/device/driver.h6.x用 linux/device.h
linux/local_lock.h5.8用 linux/spinlock.h 或 get_cpu_var 本地锁
linux/atomic.h3.x 起较完整2.6.x 常用 asm/atomic.h
linux/sched/signal.h4.11用 linux/sched.h(send_sig 在其中)
linux/gpio/consumer.h3.13用 linux/gpio.h(整数编号 API)
linux/tasklet.h5.x用 linux/interrupt.h
linux/uaccess.h2.6.18 左右更早用 asm/uaccess.h(ARM 老代码很常见)

老内核里有、新内核里已删的头文件

头文件说明消失时间
linux/devfs_fs_kernel.hdevfs,已被 udev/sysfs 取代2.6.18
linux/config.h旧式内核配置宏2.6.19
asm/semaphore.h信号量实现随架构调整
linux/hardirq.h → 仍在,但 synchronize_irq 位置变了——

API 写法差异对照(最容易踩的坑)

这一节是本文档的核心。同一个头文件,字段名和函数签名都可能不一样。

字符设备:.ioctl → .unlocked_ioctl

老写法(2.6.32–2.6.35)新写法(2.6.36+)
字段名.ioctl.unlocked_ioctl
签名int (*)(struct inode *, struct file *, unsigned int, unsigned long)long (*)(struct file *, unsigned int, unsigned long)
返回值intlong
大内核锁内核自动持有 BKL需自己加锁
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* 2.6.32 写法 */
static int my_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case MY_CMD: return 0;
default: return -ENOTTY;
}
}

static const struct file_operations my_fops = {
.owner = THIS_MODULE,
.ioctl = my_ioctl, /* 字段名是 ioctl */
};

2.6.36 起 .ioctl 被移除,只剩 .unlocked_ioctl。写兼容代码用
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36) 包起来。

定时器:init_timer → timer_setup

老写法(≤ 4.14)新写法(4.15+)
初始化init_timer(&t)timer_setup(&t, fn, 0)
回调签名void (*)(unsigned long)void (*)(struct timer_list *)
传参方式t.data = (unsigned long)ptr回调里 from_timer(p, t, member)
立即初始化setup_timer(&t, fn, data)已废弃
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* 老内核写法 */
struct my_dev {
struct timer_list timer;
};

static void my_timer_fn(unsigned long data)
{
struct my_dev *dev = (struct my_dev *)data;
printk(KERN_INFO "tick\n");
}

static void init_my_timer(struct my_dev *dev)
{
init_timer(&dev->timer);
dev->timer.function = my_timer_fn;
dev->timer.data = (unsigned long)dev;
dev->timer.expires = jiffies + HZ;
add_timer(&dev->timer);
}

class_create 的参数

内核版本签名
2.6.26 – 6.3class_create(struct module *owner, const char *name)
6.4+class_create(const char *name)

老内核统一写 class_create(THIS_MODULE, "mychar")。

sysfs 属性

功能老写法(≤ 4.17)新写法(4.18+)
定义可读写属性DEVICE_ATTR(name, 0644, show, store)DEVICE_ATTR_RW(name)
定义只读属性DEVICE_ATTR(name, 0444, show, NULL)DEVICE_ATTR_RO(name)
输出字符串sprintf(buf, "%d\n", val)sysfs_emit(buf, "%d\n", val)(5.8+)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* 老内核写法 */
static ssize_t show_value(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "%d\n", my_value); /* 不是 sysfs_emit */
}

static ssize_t store_value(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
int val;
if (kstrtoint(buf, 10, &val)) return -EINVAL;
my_value = val;
return count;
}

static DEVICE_ATTR(value, 0644, show_value, store_value);

GPIO

老写法(≤ 3.12)新写法(3.13+)
头文件linux/gpio.hlinux/gpio/consumer.h
申请gpio_request(num, "name")gpiod_get(dev, "name", GPIOD_OUT_LOW)
方向gpio_direction_output(num, 1)由 gpiod_get 的 flags 决定
读写gpio_set_value(num, 1) / gpio_get_value(num)gpiod_set_value(desc, 1) / gpiod_get_value(desc)
转中断号gpio_to_irq(num)gpiod_to_irq(desc)
释放gpio_free(num)gpiod_put(desc)

老内核(2.6.32 / 3.10)基本只能用整数编号 API。

设备树属性读取

老写法(2.6.x / 早期 3.x)新写法(3.x 后期起)
读 u32of_get_property + be32_to_cpup 手工转换of_property_read_u32
读字符串of_get_property + strlenof_property_read_string
读 GPIOof_get_gpio / of_get_named_gpioof_get_named_gpio / gpiod_get_from_of_node
1
2
3
/* 老式写法:手工转换,注意大端 */
const __be32 *p = of_get_property(np, "reg-val", NULL);
if (p) val = be32_to_cpup(p);

中断标志与注册

老写法(≤ 2.6.21)新写法(2.6.22+)
共享中断SA_SHIRQIRQF_SHARED
快中断SA_INTERRUPTIRQF_DISABLED(后来也废了)
触发方式SA_TRIGGER_*IRQF_TRIGGER_*

2.6.32 已经用 IRQF_*,但网上很多古董教程还在教 SA_*,看到就知道是 2.6.18 之前的东西。

其他常用差异

功能老写法新写法分界
互斥锁初始化init_MUTEX(&sem)mutex_init(&mtx)2.6.25
静态定义互斥锁DECLARE_MUTEX(name)DEFINE_MUTEX(name)2.6.25
创建 proc 节点create_proc_entryproc_create3.10 删除旧的
proc 操作集struct file_operationsstruct proc_ops5.6
创建工作队列create_workqueuealloc_workqueue2.6.36 弃用
平台驱动注册module_init 里手动 platform_driver_registermodule_platform_driver(x)3.x
中断入口获取———

已从内核删除的 API 黑名单

在老代码里看到下面这些,说明这段代码至少有 8 年以上历史,直接改,不要试图让它编译通过:

已删除替代删除版本
init_MUTEX() / DECLARE_MUTEXmutex_init() / DEFINE_MUTEX()2.6.25
class_device_create()device_create()2.6.26
class_device 结构体device2.6.26
create_proc_entry()proc_create()3.10
create_proc_read_entry()proc_create + seq_file3.10
SA_SHIRQ / SA_INTERRUPTIRQF_SHARED / IRQF_DISABLED2.6.22
.ioctl 字段.unlocked_ioctl2.6.36
init_timer() + .functiontimer_setup()4.15
setup_timer()timer_setup()后续版本移除
sleep_on() / interruptible_sleep_on()wait_event_* 系列3.15 左右
MODULE_PARM()module_param()2.6.10
check_region()request_region()2.6.x
cli() / sti()local_irq_save() / local_irq_restore()2.6.x
kmem_cache_tstruct kmem_cache2.6.19
pci_module_init()pci_register_driver()2.6.x
devfs 相关全部sysfs + udev2.6.18

老内核版 include 模板与完整驱动

include 模板(2.6.32 可直接编译)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <linux/module.h>      /* module_init / MODULE_LICENSE / EXPORT_SYMBOL */
#include <linux/kernel.h> /* printk / pr_info / container_of / ARRAY_SIZE */
#include <linux/init.h> /* __init / __exit */
#include <linux/types.h> /* u8 / dev_t / loff_t */
#include <linux/errno.h> /* -EINVAL / -ENOMEM */
#include <linux/fs.h> /* file_operations / struct file / inode */
#include <linux/cdev.h> /* struct cdev */
#include <linux/device.h> /* class_create / device_create */
#include <linux/slab.h> /* kmalloc / kfree */
#include <linux/uaccess.h> /* copy_to_user(2.6.18+) */
#include <linux/ioctl.h> /* _IOR / _IOW */
#include <linux/kdev_t.h> /* MKDEV / MAJOR / MINOR */
#include <linux/string.h> /* memcpy / memset */
#include <linux/jiffies.h> /* HZ / jiffies */

没有 linux/printk.h、linux/export.h、linux/err.h(2.6.32 已有 linux/err.h,但内容较少)。
printk 和 EXPORT_SYMBOL 都从 linux/kernel.h / linux/module.h 拿。

完整可运行的字符设备(2.6.32 风格)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/errno.h>
#include <linux/kdev_t.h>

#define DEV_NAME "oldchar"
#define BUF_SIZE 256

static dev_t devno;
static struct cdev my_cdev;
static struct class *my_class;
static char *kbuf;
static int open_cnt;

static int oldchar_open(struct inode *inode, struct file *filp)
{
filp->private_data = kbuf;
open_cnt++;
printk(KERN_INFO "oldchar: open (cnt=%d)\n", open_cnt);
return 0;
}

static int oldchar_release(struct inode *inode, struct file *filp)
{
open_cnt--;
printk(KERN_INFO "oldchar: release (cnt=%d)\n", open_cnt);
return 0;
}

static ssize_t oldchar_read(struct file *filp, char __user *buf,
size_t count, loff_t *ppos)
{
if (*ppos >= BUF_SIZE)
return 0;

if (count > BUF_SIZE - *ppos)
count = BUF_SIZE - *ppos;

if (copy_to_user(buf, kbuf + *ppos, count))
return -EFAULT;

*ppos += count;
return count;
}

static ssize_t oldchar_write(struct file *filp, const char __user *buf,
size_t count, loff_t *ppos)
{
if (*ppos >= BUF_SIZE)
return -ENOSPC;

if (count > BUF_SIZE - *ppos)
count = BUF_SIZE - *ppos;

if (copy_from_user(kbuf + *ppos, buf, count))
return -EFAULT;

*ppos += count;
return count;
}

/* 注意:2.6.32 用 .ioctl,参数里有 struct inode * */
static int oldchar_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case 0:
printk(KERN_INFO "oldchar: ioctl reset\n");
return 0;
default:
return -ENOTTY;
}
}

static const struct file_operations oldchar_fops = {
.owner = THIS_MODULE,
.open = oldchar_open,
.release = oldchar_release,
.read = oldchar_read,
.write = oldchar_write,
.ioctl = oldchar_ioctl,
};

static int __init oldchar_init(void)
{
int ret;

kbuf = kzalloc(BUF_SIZE, GFP_KERNEL);
if (!kbuf)
return -ENOMEM;

ret = alloc_chrdev_region(&devno, 0, 1, DEV_NAME);
if (ret < 0)
goto err_kbuf;

cdev_init(&my_cdev, &oldchar_fops);
my_cdev.owner = THIS_MODULE;

ret = cdev_add(&my_cdev, devno, 1);
if (ret < 0)
goto err_region;

my_class = class_create(THIS_MODULE, DEV_NAME);
if (IS_ERR(my_class)) {
ret = PTR_ERR(my_class);
goto err_cdev;
}

device_create(my_class, NULL, devno, NULL, DEV_NAME);
printk(KERN_INFO "oldchar: loaded, major=%d minor=%d\n",
MAJOR(devno), MINOR(devno));
return 0;

err_cdev:
cdev_del(&my_cdev);
err_region:
unregister_chrdev_region(devno, 1);
err_kbuf:
kfree(kbuf);
return ret;
}

static void __exit oldchar_exit(void)
{
device_destroy(my_class, devno);
class_destroy(my_class);
cdev_del(&my_cdev);
unregister_chrdev_region(devno, 1);
kfree(kbuf);
printk(KERN_INFO "oldchar: unloaded\n");
}

module_init(oldchar_init);
module_exit(oldchar_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("you");
MODULE_DESCRIPTION("Legacy char device demo for 2.6.32+");

Makefile(老内核通用)

1
2
3
4
5
6
7
8
9
10
11
obj-m := oldchar.o

# 交叉编译时改成目标内核源码树路径
KDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

all:
$(MAKE) -C $(KDIR) M=$(PWD) modules

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

老内核(2.6.32 / 3.10)用 make modules 而不是 make M=... modules 也常见,但 M= 方式从 2.6.18 起就支持,可以放心用。

移植到 4.4 / 4.14 只需改这几处

位置2.6.32 写法4.4 / 4.14 写法
fops 字段.ioctl.unlocked_ioctl
ioctl 原型(struct inode *, struct file *, unsigned, unsigned long)(struct file *, unsigned, unsigned long)
ioctl 返回intlong
定时器(如有)init_timer + .functiontimer_setup
GPIO(如有)gpio_request / gpio_set_value可继续用,也可换 gpiod_*
头文件asm/uaccess.hlinux/uaccess.h
设备树(如有)of_get_propertyof_property_read_u32

测试脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/sh
# test_oldchar.sh —— 需要 root
set -e

make || exit 1
insmod oldchar.ko
echo "--- dmesg ---"
dmesg | tail -n 5

echo "--- 写入测试 ---"
echo "hello legacy" > /dev/oldchar # 注意:每次 open 会重置偏移
cat /dev/oldchar

echo "--- 卸载 ---"
rmmod oldchar
dmesg | tail -n 3

老内核上 /dev/oldchar 的权限默认是 root:root 600,非 root 测试需 chmod 666 /dev/oldchar。


如何确认你的内核支持什么

不要背版本号,直接问源码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 0. 先看版本
uname -r
cat /proc/version

# 1. 头文件在不在
ls /lib/modules/$(uname -r)/build/include/linux/ | grep -E "printk|export|device"

# 2. 结构体字段叫什么(最关键)
grep -n "unlocked_ioctl\|int (\*ioctl)" include/linux/fs.h

# 3. 定时器用哪套
grep -n "timer_setup\|setup_timer\|void (\*function)" include/linux/timer.h

# 4. class_create 几个参数
grep -n "class_create" include/linux/device.h

# 5. sysfs 宏有没有
grep -n "DEVICE_ATTR_RW\|sysfs_emit" include/linux/sysfs.h

# 6. GPIO 是哪套 API
ls include/linux/gpio/ 2>/dev/null || echo "只有 linux/gpio.h(≤3.12)"

# 7. 符号是否已导出(链接期报 undefined 时查这个)
grep -rn "EXPORT_SYMBOL(kzalloc)" mm/

判定口诀:能 grep 到字段名,就按它写;grep 不到,说明内核版本在该 API 之前。


老内核开发的三条现实建议

一、别用老教程的代码结构,只借用老 API 写法

网上 2.6 时代的教程普遍用 register_chrdev + mknod + .ioctl,这套东西:

  • 没有设备节点自动创建(要手动 mknod)
  • 主设备号占用 256 个
  • 与 sysfs 体系脱节

正确做法:结构用新的(alloc_chrdev_region + cdev_add + class_create),只把 API 签名退回老版本。这样 2.6.32 能跑,往上移植也几乎不用改。

二、用 LINUX_VERSION_CODE 做单份兼容代码

如果你要同时支持 2.6.32 和 4.14,不要维护两份代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <linux/version.h>

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
static long my_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
#else
static int my_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
#endif
{
/* 主体逻辑完全一样 */
return 0;
}

static const struct file_operations my_fops = {
.owner = THIS_MODULE,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
.unlocked_ioctl = my_ioctl,
#else
.ioctl = my_ioctl,
#endif
};

代价是代码里散布 #if,但对嵌入式项目来说,这比维护两份分支划算得多。

三、先确认目标内核的 config

老内核很多选项默认关闭,头文件存在但编译不过:

1
2
3
4
5
6
7
8
# 确认设备树支持
grep CONFIG_OF= /boot/config-$(uname -r)

# 确认 GPIO / sysfs / debugfs
grep -E "CONFIG_GPIO_SYSFS|CONFIG_SYSFS|CONFIG_DEBUG_FS" /boot/config-$(uname -r)

# 确认工作队列 / 高精度定时器
grep -E "CONFIG_HIGH_RES_TIMERS|CONFIG_SMP" /boot/config-$(uname -r)

CONFIG_SMP 关掉时,spin_lock 会退化成空操作——测试环境是单核,代码里的并发 bug 永远不会暴露,这是老平台驱动最常见的隐藏地雷。