]> git.sur5r.net Git - groeck-nct6775/blob - compat.h
Update compat.h to improve support of older kernels.
[groeck-nct6775] / compat.h
1 #ifndef __COMPAT_H
2 #define __COMPAT_H
3
4 #include <linux/version.h>
5
6 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
7 #error This driver is for kernel versions 2.6.16 and later
8 #endif
9
10 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 21)
11 #if !(defined RHEL_MAJOR && RHEL_MAJOR == 5 && RHEL_MINOR >= 6)
12 /* Simplified version for compatibility */
13 struct i2c_board_info {
14         char            type[I2C_NAME_SIZE];
15         unsigned short  flags;
16         unsigned short  addr;
17 };
18 #endif
19 #endif
20
21 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 25)
22 /* Some older kernels have a different, useless struct i2c_device_id */
23 #define i2c_device_id i2c_device_id_compat
24 struct i2c_device_id {
25         char name[I2C_NAME_SIZE];
26         kernel_ulong_t driver_data      /* Data private to the driver */
27                         __attribute__((aligned(sizeof(kernel_ulong_t))));
28 };
29 #endif
30
31 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
32 static unsigned short empty_i2c[] =  { I2C_CLIENT_END };
33 static struct i2c_client_address_data addr_data = {
34         .normal_i2c     = normal_i2c,
35         .probe          = empty_i2c,
36         .ignore         = empty_i2c,
37 };
38 #endif
39
40 /* Red Hat EL5 includes backports of these functions, so we can't redefine
41  * our own. */
42 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
43 #if !(defined RHEL_MAJOR && RHEL_MAJOR == 5 && RHEL_MINOR >= 5)
44 static inline int strict_strtoul(const char *cp, unsigned int base,
45                                  unsigned long *res)
46 {
47         *res = simple_strtoul(cp, NULL, base);
48         return 0;
49 }
50
51 static inline int strict_strtol(const char *cp, unsigned int base, long *res)
52 {
53         *res = simple_strtol(cp, NULL, base);
54         return 0;
55 }
56 #endif
57 #endif
58
59 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 28)
60 /*
61  * Divide positive or negative dividend by positive divisor and round
62  * to closest integer. Result is undefined for negative divisors and
63  * for negative dividends if the divisor variable type is unsigned.
64  */
65 #define DIV_ROUND_CLOSEST(x, divisor)(                  \
66 {                                                       \
67         typeof(x) __x = x;                              \
68         typeof(divisor) __d = divisor;                  \
69         (((typeof(x))-1) > 0 ||                         \
70          ((typeof(divisor))-1) > 0 || (__x) > 0) ?      \
71                 (((__x) + ((__d) / 2)) / (__d)) :       \
72                 (((__x) - ((__d) / 2)) / (__d));        \
73 }                                                       \
74 )
75 #endif
76
77 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)
78 static inline s32
79 i2c_smbus_read_word_swapped(const struct i2c_client *client, u8 command)
80 {
81         s32 value = i2c_smbus_read_word_data(client, command);
82
83         return (value < 0) ? value : swab16(value);
84 }
85
86 static inline s32
87 i2c_smbus_write_word_swapped(const struct i2c_client *client,
88                              u8 command, u16 value)
89 {
90         return i2c_smbus_write_word_data(client, command, swab16(value));
91 }
92 #endif
93
94 #ifndef module_driver
95 /**
96  * module_driver() - Helper macro for drivers that don't do anything
97  * special in module init/exit. This eliminates a lot of boilerplate.
98  * Each module may only use this macro once, and calling it replaces
99  * module_init() and module_exit().
100  *
101  * @__driver: driver name
102  * @__register: register function for this driver type
103  * @__unregister: unregister function for this driver type
104  * @...: Additional arguments to be passed to __register and __unregister.
105  *
106  * Use this macro to construct bus specific macros for registering
107  * drivers, and do not use it on its own.
108  */
109 #define module_driver(__driver, __register, __unregister, ...) \
110 static int __init __driver##_init(void) \
111 { \
112         return __register(&(__driver) , ##__VA_ARGS__); \
113 } \
114 module_init(__driver##_init); \
115 static void __exit __driver##_exit(void) \
116 { \
117         __unregister(&(__driver) , ##__VA_ARGS__); \
118 } \
119 module_exit(__driver##_exit);
120 #endif
121
122 #ifndef module_i2c_driver
123 /**
124  * module_i2c_driver() - Helper macro for registering a I2C driver
125  * @__i2c_driver: i2c_driver struct
126  *
127  * Helper macro for I2C drivers which do not do anything special in module
128  * init/exit. This eliminates a lot of boilerplate. Each module may only
129  * use this macro once, and calling it replaces module_init() and module_exit()
130  */
131 #define module_i2c_driver(__i2c_driver) \
132         module_driver(__i2c_driver, i2c_add_driver, \
133                         i2c_del_driver)
134 #endif
135
136 #ifndef clamp_val
137 #define clamp_val SENSORS_LIMIT
138 #endif
139
140 #ifndef kstrtol
141 #define kstrtol strict_strtol
142 #endif
143 #ifndef kstrtoul
144 #define kstrtoul strict_strtoul
145 #endif
146
147 #ifndef request_muxed_region
148 #define request_muxed_region(a, b, c) (true)
149 #define release_region(a, b)
150 #endif
151
152 #endif /* __COMPAT_H */