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