-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathkernel_compat.h
More file actions
318 lines (281 loc) · 9.36 KB
/
kernel_compat.h
File metadata and controls
318 lines (281 loc) · 9.36 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Kernel version compatibility shims for building against Linux 4.9 - 6.6+
*
* This header provides compatibility wrappers for kernel APIs that changed
* or were removed across kernel versions, enabling the OSAL layer and
* wrapper modules to compile against newer kernels while the binary blobs
* remain unchanged (they only call OSAL via opaque void* wrappers).
*/
#ifndef KERNEL_COMPAT_H
#define KERNEL_COMPAT_H
#include <linux/version.h>
/*
* On 6.4+, EXPORT_ALIAS is a no-op so the LOG symbol alias of HI_LOG
* is not created. Map LOG to HI_LOG at the preprocessor level.
* This is applied globally via -include in the top-level Kbuild.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
#define LOG HI_LOG
#endif
/*
* Timer API: init_timer() removed in 4.15, timer callback signature changed.
* 4.15+: timer_setup() + void(*)(struct timer_list*) callback
* Pre-4.15: init_timer() + void(*)(unsigned long) callback + .data field
*
* Since OSAL wraps timers behind opaque void* and the blobs use the old
* callback signature void(*)(unsigned long), we use from_timer() in the
* shim callback to recover the original context.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0)
#define COMPAT_TIMER_SETUP 1
#endif
/*
* access_ok() lost its first (type) parameter in 5.0.
* Pre-5.0: access_ok(type, addr, size)
* 5.0+: access_ok(addr, size)
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
#define compat_access_ok(type, addr, size) access_ok(addr, size)
#else
#define compat_access_ok(type, addr, size) access_ok(type, addr, size)
#endif
/*
* ioremap_nocache() removed in 5.6 (ioremap is already uncached on most archs).
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
#define ioremap_nocache ioremap
#endif
/*
* set_fs()/get_fs()/get_ds() removed in 5.10 (UACCESS_BUFFER / addr_limit).
* Use kernel_read()/kernel_write() instead of vfs_read() with set_fs().
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0)
#define COMPAT_NO_SET_FS 1
#endif
/*
* mmap_sem renamed to mmap_lock in 5.8, with helper functions.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
#define compat_mmap_read_lock(mm) mmap_read_lock(mm)
#define compat_mmap_read_unlock(mm) mmap_read_unlock(mm)
#else
#define compat_mmap_read_lock(mm) down_read(&(mm)->mmap_sem)
#define compat_mmap_read_unlock(mm) up_read(&(mm)->mmap_sem)
#endif
/*
* do_gettimeofday() removed in 5.0.
* Use ktime_get_real_ts64() + timespec64 instead.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
#define COMPAT_NO_DO_GETTIMEOFDAY 1
#include <linux/ktime.h>
#include <linux/timekeeping.h>
#endif
/*
* rtc_time_to_tm/rtc_tm_to_time removed in 5.6 in favor of
* rtc_time64_to_tm/rtc_tm_to_time64.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
#define compat_rtc_time_to_tm rtc_time64_to_tm
/* rtc_tm_to_time64() returns time64_t instead of storing via pointer */
#define compat_rtc_tm_to_time(tm, timep) do { *(timep) = rtc_tm_to_time64(tm); } while (0)
#else
#define compat_rtc_time_to_tm rtc_time_to_tm
#define compat_rtc_tm_to_time rtc_tm_to_time
#endif
/*
* register_sysctl_table() removed in 6.6.
* Use register_sysctl() instead.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
#define COMPAT_NO_SYSCTL_TABLE 1
#endif
/*
* register_sysctl_paths() removed in 6.6.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
#define COMPAT_NO_SYSCTL_PATHS 1
#endif
/*
* struct ctl_path removed in 6.6.
*/
/*
* ARM32 cache flush APIs:
* __flush_dcache_area renamed to dcache_clean_inval_poc in 5.15+ (arm64)
* __cpuc_flush_dcache_area still available on ARM32 through 6.x
* __cpuc_flush_kern_all still available on ARM32 through 6.x
* outer_flush_range/outer_flush_all still available when CONFIG_CACHE_L2X0
*
* Note: ARM32 cache flush functions are arch-specific and remain stable.
* ARM64 changes are handled separately.
*/
#ifdef CONFIG_64BIT
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
/* __flush_dcache_area was renamed */
#define compat_flush_dcache_area(addr, size) \
dcache_clean_inval_poc((unsigned long)(addr), \
(unsigned long)(addr) + (size))
#else
#define compat_flush_dcache_area(addr, size) __flush_dcache_area(addr, size)
#endif
#endif /* CONFIG_64BIT */
/*
* strlcpy() deprecated in 6.1, removed in 6.8. Use strscpy() instead.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
#define compat_strlcpy(dst, src, size) strscpy(dst, src, size)
#else
#define compat_strlcpy(dst, src, size) strlcpy(dst, src, size)
#endif
/*
* PDE_DATA() renamed to pde_data() in 5.17.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 17, 0)
#define compat_pde_data(inode) pde_data(inode)
#elif defined(CONFIG_PROC_FS)
#define compat_pde_data(inode) PDE_DATA(inode)
#endif
/*
* vm_flags field became read-only in 6.3 (use vm_flags_set/vm_flags_clear).
* For read-only checks this doesn't matter.
*/
/*
* sched_clock() moved to <linux/sched/clock.h> in 4.11+.
* signal_pending() moved to <linux/sched/signal.h> in 4.11+.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
#include <linux/sched/clock.h>
#include <linux/sched/signal.h>
#endif
/*
* struct bus_type: dev_attrs removed in 3.12, use dev_groups instead.
* The field was technically present but deprecated since 3.11.
*/
/*
* linux/dma-contiguous.h merged into linux/dma-map-ops.h in 5.x.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
#include <linux/dma-map-ops.h>
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
#include <linux/dma-contiguous.h>
#endif
/*
* DEFINE_SEMAPHORE gained a count parameter in 6.6.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
#define compat_DEFINE_SEMAPHORE(name) DEFINE_SEMAPHORE(name, 1)
#else
#define compat_DEFINE_SEMAPHORE(name) DEFINE_SEMAPHORE(name)
#endif
/*
* vm_flags became read-only in 6.3. Use vm_flags_set()/vm_flags_clear().
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
#define compat_vm_flags_set(vma, flags) vm_flags_set(vma, flags)
#else
#define compat_vm_flags_set(vma, flags) ((vma)->vm_flags |= (flags))
#endif
/*
* get_random_int() renamed to get_random_u32() in 6.1.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
#define compat_get_random_int() get_random_u32()
#else
#define compat_get_random_int() get_random_int()
#endif
/*
* struct file_operations replaced by struct proc_ops for proc files in 5.6.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
#define COMPAT_USE_PROC_OPS 1
#endif
/*
* dma_alloc_from_contiguous() gained a 4th 'no_warn' parameter in 4.15.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0)
#define compat_dma_alloc_from_contiguous(dev, count, align) \
dma_alloc_from_contiguous(dev, count, align, false)
#else
#define compat_dma_alloc_from_contiguous(dev, count, align) \
dma_alloc_from_contiguous(dev, count, align)
#endif
/*
* spi_busnum_to_master() removed in 6.4. Provide inline fallback.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
#include <linux/spi/spi.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 0, 0)
extern const struct class spi_controller_class;
#define compat_spi_class spi_controller_class
#else
extern struct class spi_master_class;
#define compat_spi_class spi_master_class
#endif
static inline struct spi_controller *compat_spi_busnum_to_controller(u16 bus_num)
{
struct device *dev;
char name[32];
snprintf(name, sizeof(name), "spi%u", bus_num);
dev = class_find_device_by_name(&compat_spi_class, name);
if (!dev)
return NULL;
return container_of(dev, struct spi_controller, dev);
}
#define spi_busnum_to_controller compat_spi_busnum_to_controller
#endif
/* Inserted before #endif */
/*
* i2c_new_device renamed to i2c_new_client_device in 5.x.
*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 2, 0)
#define i2c_new_client_device i2c_new_device
#endif
/*
* spi_busnum_to_master renamed to spi_busnum_to_controller in 5.x.
* (6.4+ removal handled separately above)
*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
#define spi_busnum_to_controller spi_busnum_to_master
#endif
/*
* Linux 7.0+ API changes
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 0, 0)
/* platform_device.h no longer implicitly included */
#include <linux/platform_device.h>
/* del_timer moved to linux/timer.h (explicit include needed) */
#include <linux/timer.h>
#endif /* >= 7.0 */
/* del_timer renamed to timer_delete in 7.0 */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 0, 0)
#define del_timer timer_delete
#define del_timer_sync timer_delete_sync
#endif
/* platform_driver.remove returns void since 6.11 */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 11, 0)
#define compat_platform_remove_ret void
#define compat_platform_remove_return return
#else
#define compat_platform_remove_ret int
#define compat_platform_remove_return return 0
#endif
/* spi_master typedef removed in 7.0 */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 0, 0)
#define spi_master spi_controller
#endif
/* from_timer macro removed in 7.0, use timer_container_of */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 0, 0)
#include <linux/timer.h>
#define from_timer(var, callback_timer, timer_fieldname) \
container_of(callback_timer, typeof(*var), timer_fieldname)
#endif
/* 7.0: register_sysctl() validates sentinel entries, causing spurious errors.
* Use register_sysctl_sz() with explicit count to exclude sentinel. */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 0, 0)
#define compat_register_sysctl(path, table) \
register_sysctl_sz(path, table, ARRAY_SIZE(table) - 1)
#else
#define compat_register_sysctl(path, table) \
register_sysctl(path, table)
#endif
#endif /* KERNEL_COMPAT_H */