net/mlx5: Fix use-after-free in self-healing flow
[linux-2.6-block.git] / include / linux / property.h
CommitLineData
b31384fa
RW
1/*
2 * property.h - Unified device property interface.
3 *
4 * Copyright (C) 2014, Intel Corporation
5 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#ifndef _LINUX_PROPERTY_H_
14#define _LINUX_PROPERTY_H_
15
ce793486 16#include <linux/fwnode.h>
b31384fa
RW
17#include <linux/types.h>
18
19struct device;
20
21enum dev_prop_type {
22 DEV_PROP_U8,
23 DEV_PROP_U16,
24 DEV_PROP_U32,
25 DEV_PROP_U64,
26 DEV_PROP_STRING,
27 DEV_PROP_MAX,
28};
29
1b9863c6
SS
30enum dev_dma_attr {
31 DEV_DMA_NOT_SUPPORTED,
32 DEV_DMA_NON_COHERENT,
33 DEV_DMA_COHERENT,
34};
35
e44bb0cb
SA
36struct fwnode_handle *dev_fwnode(struct device *dev);
37
b31384fa
RW
38bool device_property_present(struct device *dev, const char *propname);
39int device_property_read_u8_array(struct device *dev, const char *propname,
40 u8 *val, size_t nval);
41int device_property_read_u16_array(struct device *dev, const char *propname,
42 u16 *val, size_t nval);
43int device_property_read_u32_array(struct device *dev, const char *propname,
44 u32 *val, size_t nval);
45int device_property_read_u64_array(struct device *dev, const char *propname,
46 u64 *val, size_t nval);
47int device_property_read_string_array(struct device *dev, const char *propname,
48 const char **val, size_t nval);
49int device_property_read_string(struct device *dev, const char *propname,
50 const char **val);
3f5c8d31
MW
51int device_property_match_string(struct device *dev,
52 const char *propname, const char *string);
b31384fa 53
37ba983c
SA
54bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
55bool fwnode_property_present(const struct fwnode_handle *fwnode,
56 const char *propname);
57int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
58 const char *propname, u8 *val,
59 size_t nval);
37ba983c 60int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
61 const char *propname, u16 *val,
62 size_t nval);
37ba983c 63int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
64 const char *propname, u32 *val,
65 size_t nval);
37ba983c 66int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
67 const char *propname, u64 *val,
68 size_t nval);
37ba983c 69int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
70 const char *propname, const char **val,
71 size_t nval);
37ba983c 72int fwnode_property_read_string(const struct fwnode_handle *fwnode,
8a0662d9 73 const char *propname, const char **val);
37ba983c 74int fwnode_property_match_string(const struct fwnode_handle *fwnode,
3f5c8d31 75 const char *propname, const char *string);
3e3119d3
SA
76int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
77 const char *prop, const char *nargs_prop,
78 unsigned int nargs, unsigned int index,
79 struct fwnode_reference_args *args);
8a0662d9 80
37ba983c
SA
81struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
82struct fwnode_handle *fwnode_get_next_parent(
83 struct fwnode_handle *fwnode);
84struct fwnode_handle *fwnode_get_next_child_node(
85 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
3395de96
MW
86struct fwnode_handle *fwnode_get_next_available_child_node(
87 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
34055190
MW
88
89#define fwnode_for_each_child_node(fwnode, child) \
90 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
91 child = fwnode_get_next_child_node(fwnode, child))
afaf26fd 92
3395de96
MW
93#define fwnode_for_each_available_child_node(fwnode, child) \
94 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
95 child = fwnode_get_next_available_child_node(fwnode, child))
96
37ba983c
SA
97struct fwnode_handle *device_get_next_child_node(
98 struct device *dev, struct fwnode_handle *child);
8a0662d9 99
1d656fb7
AS
100#define device_for_each_child_node(dev, child) \
101 for (child = device_get_next_child_node(dev, NULL); child; \
8a0662d9
RW
102 child = device_get_next_child_node(dev, child))
103
37ba983c
SA
104struct fwnode_handle *fwnode_get_named_child_node(
105 const struct fwnode_handle *fwnode, const char *childname);
613e9721
AT
106struct fwnode_handle *device_get_named_child_node(struct device *dev,
107 const char *childname);
108
cf89a31c 109struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
8a0662d9
RW
110void fwnode_handle_put(struct fwnode_handle *fwnode);
111
7c6c57f2
MW
112int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index);
113
8a0662d9
RW
114unsigned int device_get_child_node_count(struct device *dev);
115
b31384fa
RW
116static inline bool device_property_read_bool(struct device *dev,
117 const char *propname)
118{
119 return device_property_present(dev, propname);
120}
121
122static inline int device_property_read_u8(struct device *dev,
123 const char *propname, u8 *val)
124{
125 return device_property_read_u8_array(dev, propname, val, 1);
126}
127
128static inline int device_property_read_u16(struct device *dev,
129 const char *propname, u16 *val)
130{
131 return device_property_read_u16_array(dev, propname, val, 1);
132}
133
134static inline int device_property_read_u32(struct device *dev,
135 const char *propname, u32 *val)
136{
137 return device_property_read_u32_array(dev, propname, val, 1);
138}
139
140static inline int device_property_read_u64(struct device *dev,
141 const char *propname, u64 *val)
142{
143 return device_property_read_u64_array(dev, propname, val, 1);
144}
145
37ba983c 146static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
8a0662d9
RW
147 const char *propname)
148{
149 return fwnode_property_present(fwnode, propname);
150}
151
37ba983c 152static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
8a0662d9
RW
153 const char *propname, u8 *val)
154{
155 return fwnode_property_read_u8_array(fwnode, propname, val, 1);
156}
157
37ba983c 158static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
8a0662d9
RW
159 const char *propname, u16 *val)
160{
161 return fwnode_property_read_u16_array(fwnode, propname, val, 1);
162}
163
37ba983c 164static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
8a0662d9
RW
165 const char *propname, u32 *val)
166{
167 return fwnode_property_read_u32_array(fwnode, propname, val, 1);
168}
169
37ba983c 170static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
8a0662d9
RW
171 const char *propname, u64 *val)
172{
173 return fwnode_property_read_u64_array(fwnode, propname, val, 1);
174}
175
16ba08d5
RW
176/**
177 * struct property_entry - "Built-in" device property representation.
178 * @name: Name of the property.
318a1971 179 * @length: Length of data making up the value.
66586bab 180 * @is_array: True when the property is an array.
63dcc709 181 * @type: Type of the data in unions.
66586bab
AS
182 * @pointer: Pointer to the property (an array of items of the given type).
183 * @value: Value of the property (when it is a single item of the given type).
16ba08d5
RW
184 */
185struct property_entry {
186 const char *name;
318a1971 187 size_t length;
66586bab 188 bool is_array;
63dcc709 189 enum dev_prop_type type;
16ba08d5 190 union {
66586bab 191 union {
9426998c
DT
192 const u8 *u8_data;
193 const u16 *u16_data;
194 const u32 *u32_data;
195 const u64 *u64_data;
196 const char * const *str;
66586bab
AS
197 } pointer;
198 union {
66586bab
AS
199 u8 u8_data;
200 u16 u16_data;
201 u32 u32_data;
202 u64 u64_data;
203 const char *str;
204 } value;
205 };
16ba08d5
RW
206};
207
d76eebfa
AM
208/*
209 * Note: the below four initializers for the anonymous union are carefully
210 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
211 * and structs.
212 */
213
63dcc709
AS
214#define PROPERTY_ENTRY_INTEGER_ARRAY(_name_, _type_, _Type_, _val_) \
215(struct property_entry) { \
216 .name = _name_, \
217 .length = ARRAY_SIZE(_val_) * sizeof(_type_), \
218 .is_array = true, \
219 .type = DEV_PROP_##_Type_, \
220 { .pointer = { ._type_##_data = _val_ } }, \
a85f4204
HK
221}
222
223#define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
63dcc709 224 PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u8, U8, _val_)
a85f4204 225#define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
63dcc709 226 PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u16, U16, _val_)
a85f4204 227#define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
63dcc709 228 PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u32, U32, _val_)
a85f4204 229#define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
63dcc709 230 PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u64, U64, _val_)
a85f4204
HK
231
232#define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
c505cbd4 233(struct property_entry) { \
a85f4204
HK
234 .name = _name_, \
235 .length = ARRAY_SIZE(_val_) * sizeof(const char *), \
236 .is_array = true, \
63dcc709 237 .type = DEV_PROP_STRING, \
d76eebfa 238 { .pointer = { .str = _val_ } }, \
a85f4204
HK
239}
240
63dcc709
AS
241#define PROPERTY_ENTRY_INTEGER(_name_, _type_, _Type_, _val_) \
242(struct property_entry) { \
243 .name = _name_, \
244 .length = sizeof(_type_), \
245 .type = DEV_PROP_##_Type_, \
246 { .value = { ._type_##_data = _val_ } }, \
a85f4204
HK
247}
248
249#define PROPERTY_ENTRY_U8(_name_, _val_) \
63dcc709 250 PROPERTY_ENTRY_INTEGER(_name_, u8, U8, _val_)
a85f4204 251#define PROPERTY_ENTRY_U16(_name_, _val_) \
63dcc709 252 PROPERTY_ENTRY_INTEGER(_name_, u16, U16, _val_)
a85f4204 253#define PROPERTY_ENTRY_U32(_name_, _val_) \
63dcc709 254 PROPERTY_ENTRY_INTEGER(_name_, u32, U32, _val_)
a85f4204 255#define PROPERTY_ENTRY_U64(_name_, _val_) \
63dcc709 256 PROPERTY_ENTRY_INTEGER(_name_, u64, U64, _val_)
a85f4204
HK
257
258#define PROPERTY_ENTRY_STRING(_name_, _val_) \
c505cbd4 259(struct property_entry) { \
a85f4204
HK
260 .name = _name_, \
261 .length = sizeof(_val_), \
63dcc709 262 .type = DEV_PROP_STRING, \
aace7367 263 { .value = { .str = _val_ } }, \
a85f4204
HK
264}
265
266#define PROPERTY_ENTRY_BOOL(_name_) \
c505cbd4 267(struct property_entry) { \
a85f4204
HK
268 .name = _name_, \
269}
270
2d479e1f
DT
271struct property_entry *
272property_entries_dup(const struct property_entry *properties);
273
274void property_entries_free(const struct property_entry *properties);
275
f4d05266 276int device_add_properties(struct device *dev,
bec84da8 277 const struct property_entry *properties);
f4d05266 278void device_remove_properties(struct device *dev);
16ba08d5 279
e5e55864
SS
280bool device_dma_supported(struct device *dev);
281
282enum dev_dma_attr device_get_dma_attr(struct device *dev);
b283f157 283
67dcc26d 284const void *device_get_match_data(struct device *dev);
e5e55864 285
4c96b7dc
JL
286int device_get_phy_mode(struct device *dev);
287
288void *device_get_mac_address(struct device *dev, char *addr, int alen);
289
b28f263b 290int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
babe2dbb
MW
291void *fwnode_get_mac_address(struct fwnode_handle *fwnode,
292 char *addr, int alen);
07bb80d4 293struct fwnode_handle *fwnode_graph_get_next_endpoint(
37ba983c 294 const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
6a71d8d7 295struct fwnode_handle *
37ba983c 296fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
07bb80d4 297struct fwnode_handle *fwnode_graph_get_remote_port_parent(
37ba983c 298 const struct fwnode_handle *fwnode);
07bb80d4 299struct fwnode_handle *fwnode_graph_get_remote_port(
37ba983c 300 const struct fwnode_handle *fwnode);
07bb80d4 301struct fwnode_handle *fwnode_graph_get_remote_endpoint(
37ba983c
SA
302 const struct fwnode_handle *fwnode);
303struct fwnode_handle *
304fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port,
305 u32 endpoint);
07bb80d4 306
5e4b1b70
SA
307#define fwnode_graph_for_each_endpoint(fwnode, child) \
308 for (child = NULL; \
309 (child = fwnode_graph_get_next_endpoint(fwnode, child)); )
310
37ba983c 311int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
2bd5452d
SA
312 struct fwnode_endpoint *endpoint);
313
b31384fa 314#endif /* _LINUX_PROPERTY_H_ */