blob: 891090f67e28580e35accbc7314e68aac153594a [file] [log] [blame]
Greg Kroah-Hartmanf91121b2014-09-11 08:22:06 -07001/*
2 * Greybus kernel "version" glue logic.
3 *
4 * Copyright 2014 Google Inc.
Alex Eldera46e9672014-12-12 12:08:42 -06005 * Copyright 2014 Linaro Ltd.
Greg Kroah-Hartmanf91121b2014-09-11 08:22:06 -07006 *
7 * Released under the GPLv2 only.
8 *
9 * Backports of newer kernel apis to allow the code to build properly on older
10 * kernel versions. Remove this file when merging to upstream, it should not be
11 * needed at all
12 */
13
14#ifndef __GREYBUS_KERNEL_VER_H
15#define __GREYBUS_KERNEL_VER_H
16
Viresh Kumare2cb6ca2015-03-27 16:32:56 +053017#include <linux/kernel.h>
18
Greg Kroah-Hartman4efe6062014-11-17 16:55:54 -080019#ifndef __ATTR_WO
20#define __ATTR_WO(_name) { \
Greg Kroah-Hartman99a4bd52015-05-01 21:04:47 +020021 .attr = { .name = __stringify(_name), .mode = S_IWUSR }, \
22 .store = _name##_store, \
Greg Kroah-Hartman4efe6062014-11-17 16:55:54 -080023}
24#endif
25
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -080026#ifndef __ATTR_RW
27#define __ATTR_RW(_name) __ATTR(_name, (S_IWUSR | S_IRUGO), \
Greg Kroah-Hartman99a4bd52015-05-01 21:04:47 +020028 _name##_show, _name##_store)
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -080029#endif
30
Greg Kroah-Hartmanf91121b2014-09-11 08:22:06 -070031#ifndef DEVICE_ATTR_RO
32#define DEVICE_ATTR_RO(_name) \
33 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
34#endif
35
Greg Kroah-Hartmanac4029f2014-11-17 16:03:34 -080036#ifndef DEVICE_ATTR_WO
37#define DEVICE_ATTR_WO(_name) \
38 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
39#endif
40
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -080041#ifndef DEVICE_ATTR_RW
42#define DEVICE_ATTR_RW(_name) \
43 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
44#endif
45
Alex Elder1cfc6672014-09-30 19:25:21 -050046#ifndef U8_MAX
47#define U8_MAX ((u8)~0U)
48#endif /* ! U8_MAX */
Greg Kroah-Hartmanf91121b2014-09-11 08:22:06 -070049
Alex Eldere88afa52014-10-01 21:54:15 -050050#ifndef U16_MAX
51#define U16_MAX ((u16)(~0U))
52#endif /* !U16_MAX */
53
Greg Kroah-Hartman213aefe2014-10-20 13:40:02 +080054/*
55 * The GPIO api sucks rocks in places, like removal, so work around their
56 * explicit requirements of catching the return value for kernels older than
57 * 3.17, which they explicitly changed in the 3.17 kernel. Consistency is
58 * overrated.
59 */
60#include <linux/version.h>
61#include <linux/gpio.h>
62
63#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)
64static inline void gb_gpiochip_remove(struct gpio_chip *chip)
65{
66 gpiochip_remove(chip);
67}
68#else
69static inline void gb_gpiochip_remove(struct gpio_chip *chip)
70{
71 int ret;
72 ret = gpiochip_remove(chip);
73}
74#endif
75
Greg Kroah-Hartmanf3489642014-10-28 09:27:50 +080076/*
77 * ATTRIBUTE_GROUPS showed up in 3.11-rc2, but we need to build on 3.10, so add
78 * it here.
79 */
80#if LINUX_VERSION_CODE < KERNEL_VERSION(3,11,0)
Greg Kroah-Hartman66c98982015-04-01 01:36:23 +020081#include <linux/sysfs.h>
82
Greg Kroah-Hartmanf3489642014-10-28 09:27:50 +080083#define ATTRIBUTE_GROUPS(name) \
84static const struct attribute_group name##_group = { \
85 .attrs = name##_attrs, \
86}; \
87static const struct attribute_group *name##_groups[] = { \
88 &name##_group, \
89 NULL, \
90}
Greg Kroah-Hartman66c98982015-04-01 01:36:23 +020091
92static inline int sysfs_create_groups(struct kobject *kobj,
93 const struct attribute_group **groups)
94{
95 int error = 0;
96 int i;
97
98 if (!groups)
99 return 0;
100
101 for (i = 0; groups[i]; i++) {
102 error = sysfs_create_group(kobj, groups[i]);
103 if (error) {
104 while (--i >= 0)
105 sysfs_remove_group(kobj, groups[i]);
106 break;
107 }
108 }
109 return error;
110}
111
112static inline void sysfs_remove_groups(struct kobject *kobj,
113 const struct attribute_group **groups)
114{
115 int i;
116
117 if (!groups)
118 return;
119 for (i = 0; groups[i]; i++)
120 sysfs_remove_group(kobj, groups[i]);
121}
Greg Kroah-Hartmanf3489642014-10-28 09:27:50 +0800122#endif
123
Greg Kroah-Hartmanf91121b2014-09-11 08:22:06 -0700124#endif /* __GREYBUS_KERNEL_VER_H */