]> Git Repo - linux.git/blob - include/linux/property.h
HID: hid-sensor-custom: Fix big on-stack allocation in hid_sensor_custom_get_known()
[linux.git] / include / linux / property.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * property.h - Unified device property interface.
4  *
5  * Copyright (C) 2014, Intel Corporation
6  * Authors: Rafael J. Wysocki <[email protected]>
7  *          Mika Westerberg <[email protected]>
8  */
9
10 #ifndef _LINUX_PROPERTY_H_
11 #define _LINUX_PROPERTY_H_
12
13 #include <linux/bits.h>
14 #include <linux/fwnode.h>
15 #include <linux/types.h>
16
17 struct device;
18 struct net_device;
19
20 enum dev_prop_type {
21         DEV_PROP_U8,
22         DEV_PROP_U16,
23         DEV_PROP_U32,
24         DEV_PROP_U64,
25         DEV_PROP_STRING,
26         DEV_PROP_REF,
27 };
28
29 enum dev_dma_attr {
30         DEV_DMA_NOT_SUPPORTED,
31         DEV_DMA_NON_COHERENT,
32         DEV_DMA_COHERENT,
33 };
34
35 struct fwnode_handle *dev_fwnode(const struct device *dev);
36
37 bool device_property_present(struct device *dev, const char *propname);
38 int device_property_read_u8_array(struct device *dev, const char *propname,
39                                   u8 *val, size_t nval);
40 int device_property_read_u16_array(struct device *dev, const char *propname,
41                                    u16 *val, size_t nval);
42 int device_property_read_u32_array(struct device *dev, const char *propname,
43                                    u32 *val, size_t nval);
44 int device_property_read_u64_array(struct device *dev, const char *propname,
45                                    u64 *val, size_t nval);
46 int device_property_read_string_array(struct device *dev, const char *propname,
47                                       const char **val, size_t nval);
48 int device_property_read_string(struct device *dev, const char *propname,
49                                 const char **val);
50 int device_property_match_string(struct device *dev,
51                                  const char *propname, const char *string);
52
53 bool fwnode_property_present(const struct fwnode_handle *fwnode,
54                              const char *propname);
55 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
56                                   const char *propname, u8 *val,
57                                   size_t nval);
58 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
59                                    const char *propname, u16 *val,
60                                    size_t nval);
61 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
62                                    const char *propname, u32 *val,
63                                    size_t nval);
64 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
65                                    const char *propname, u64 *val,
66                                    size_t nval);
67 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
68                                       const char *propname, const char **val,
69                                       size_t nval);
70 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
71                                 const char *propname, const char **val);
72 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
73                                  const char *propname, const char *string);
74
75 bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
76
77 static inline
78 bool fwnode_device_is_compatible(const struct fwnode_handle *fwnode, const char *compat)
79 {
80         return fwnode_property_match_string(fwnode, "compatible", compat) >= 0;
81 }
82
83 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
84                                        const char *prop, const char *nargs_prop,
85                                        unsigned int nargs, unsigned int index,
86                                        struct fwnode_reference_args *args);
87
88 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
89                                             const char *name,
90                                             unsigned int index);
91
92 const char *fwnode_get_name(const struct fwnode_handle *fwnode);
93 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
94
95 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
96 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode);
97
98 #define fwnode_for_each_parent_node(fwnode, parent)             \
99         for (parent = fwnode_get_parent(fwnode); parent;        \
100              parent = fwnode_get_next_parent(parent))
101
102 struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode);
103 unsigned int fwnode_count_parents(const struct fwnode_handle *fwn);
104 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn,
105                                             unsigned int depth);
106 bool fwnode_is_ancestor_of(struct fwnode_handle *ancestor, struct fwnode_handle *child);
107 struct fwnode_handle *fwnode_get_next_child_node(
108         const struct fwnode_handle *fwnode, struct fwnode_handle *child);
109 struct fwnode_handle *fwnode_get_next_available_child_node(
110         const struct fwnode_handle *fwnode, struct fwnode_handle *child);
111
112 #define fwnode_for_each_child_node(fwnode, child)                       \
113         for (child = fwnode_get_next_child_node(fwnode, NULL); child;   \
114              child = fwnode_get_next_child_node(fwnode, child))
115
116 #define fwnode_for_each_available_child_node(fwnode, child)                    \
117         for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
118              child = fwnode_get_next_available_child_node(fwnode, child))
119
120 struct fwnode_handle *device_get_next_child_node(
121         struct device *dev, struct fwnode_handle *child);
122
123 #define device_for_each_child_node(dev, child)                          \
124         for (child = device_get_next_child_node(dev, NULL); child;      \
125              child = device_get_next_child_node(dev, child))
126
127 struct fwnode_handle *fwnode_get_named_child_node(
128         const struct fwnode_handle *fwnode, const char *childname);
129 struct fwnode_handle *device_get_named_child_node(struct device *dev,
130                                                   const char *childname);
131
132 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
133 void fwnode_handle_put(struct fwnode_handle *fwnode);
134
135 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index);
136 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name);
137
138 unsigned int device_get_child_node_count(struct device *dev);
139
140 static inline bool device_property_read_bool(struct device *dev,
141                                              const char *propname)
142 {
143         return device_property_present(dev, propname);
144 }
145
146 static inline int device_property_read_u8(struct device *dev,
147                                           const char *propname, u8 *val)
148 {
149         return device_property_read_u8_array(dev, propname, val, 1);
150 }
151
152 static inline int device_property_read_u16(struct device *dev,
153                                            const char *propname, u16 *val)
154 {
155         return device_property_read_u16_array(dev, propname, val, 1);
156 }
157
158 static inline int device_property_read_u32(struct device *dev,
159                                            const char *propname, u32 *val)
160 {
161         return device_property_read_u32_array(dev, propname, val, 1);
162 }
163
164 static inline int device_property_read_u64(struct device *dev,
165                                            const char *propname, u64 *val)
166 {
167         return device_property_read_u64_array(dev, propname, val, 1);
168 }
169
170 static inline int device_property_count_u8(struct device *dev, const char *propname)
171 {
172         return device_property_read_u8_array(dev, propname, NULL, 0);
173 }
174
175 static inline int device_property_count_u16(struct device *dev, const char *propname)
176 {
177         return device_property_read_u16_array(dev, propname, NULL, 0);
178 }
179
180 static inline int device_property_count_u32(struct device *dev, const char *propname)
181 {
182         return device_property_read_u32_array(dev, propname, NULL, 0);
183 }
184
185 static inline int device_property_count_u64(struct device *dev, const char *propname)
186 {
187         return device_property_read_u64_array(dev, propname, NULL, 0);
188 }
189
190 static inline int device_property_string_array_count(struct device *dev,
191                                                      const char *propname)
192 {
193         return device_property_read_string_array(dev, propname, NULL, 0);
194 }
195
196 static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
197                                              const char *propname)
198 {
199         return fwnode_property_present(fwnode, propname);
200 }
201
202 static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
203                                           const char *propname, u8 *val)
204 {
205         return fwnode_property_read_u8_array(fwnode, propname, val, 1);
206 }
207
208 static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
209                                            const char *propname, u16 *val)
210 {
211         return fwnode_property_read_u16_array(fwnode, propname, val, 1);
212 }
213
214 static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
215                                            const char *propname, u32 *val)
216 {
217         return fwnode_property_read_u32_array(fwnode, propname, val, 1);
218 }
219
220 static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
221                                            const char *propname, u64 *val)
222 {
223         return fwnode_property_read_u64_array(fwnode, propname, val, 1);
224 }
225
226 static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
227                                            const char *propname)
228 {
229         return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
230 }
231
232 static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
233                                             const char *propname)
234 {
235         return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
236 }
237
238 static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
239                                             const char *propname)
240 {
241         return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
242 }
243
244 static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
245                                             const char *propname)
246 {
247         return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
248 }
249
250 static inline int
251 fwnode_property_string_array_count(const struct fwnode_handle *fwnode,
252                                    const char *propname)
253 {
254         return fwnode_property_read_string_array(fwnode, propname, NULL, 0);
255 }
256
257 struct software_node;
258
259 /**
260  * struct software_node_ref_args - Reference property with additional arguments
261  * @node: Reference to a software node
262  * @nargs: Number of elements in @args array
263  * @args: Integer arguments
264  */
265 struct software_node_ref_args {
266         const struct software_node *node;
267         unsigned int nargs;
268         u64 args[NR_FWNODE_REFERENCE_ARGS];
269 };
270
271 #define SOFTWARE_NODE_REFERENCE(_ref_, ...)                     \
272 (const struct software_node_ref_args) {                         \
273         .node = _ref_,                                          \
274         .nargs = ARRAY_SIZE(((u64[]){ 0, ##__VA_ARGS__ })) - 1, \
275         .args = { __VA_ARGS__ },                                \
276 }
277
278 /**
279  * struct property_entry - "Built-in" device property representation.
280  * @name: Name of the property.
281  * @length: Length of data making up the value.
282  * @is_inline: True when the property value is stored inline.
283  * @type: Type of the data in unions.
284  * @pointer: Pointer to the property when it is not stored inline.
285  * @value: Value of the property when it is stored inline.
286  */
287 struct property_entry {
288         const char *name;
289         size_t length;
290         bool is_inline;
291         enum dev_prop_type type;
292         union {
293                 const void *pointer;
294                 union {
295                         u8 u8_data[sizeof(u64) / sizeof(u8)];
296                         u16 u16_data[sizeof(u64) / sizeof(u16)];
297                         u32 u32_data[sizeof(u64) / sizeof(u32)];
298                         u64 u64_data[sizeof(u64) / sizeof(u64)];
299                         const char *str[sizeof(u64) / sizeof(char *)];
300                 } value;
301         };
302 };
303
304 /*
305  * Note: the below initializers for the anonymous union are carefully
306  * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
307  * and structs.
308  */
309
310 #define __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_)                           \
311         sizeof(((struct property_entry *)NULL)->value._elem_[0])
312
313 #define __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, _elsize_, _Type_,     \
314                                           _val_, _len_)                 \
315 (struct property_entry) {                                               \
316         .name = _name_,                                                 \
317         .length = (_len_) * (_elsize_),                                 \
318         .type = DEV_PROP_##_Type_,                                      \
319         { .pointer = _val_ },                                           \
320 }
321
322 #define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _elem_, _Type_, _val_, _len_)\
323         __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_,                       \
324                                 __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_),  \
325                                 _Type_, _val_, _len_)
326
327 #define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_)               \
328         __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8_data, U8, _val_, _len_)
329 #define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_)              \
330         __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16_data, U16, _val_, _len_)
331 #define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_)              \
332         __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32_data, U32, _val_, _len_)
333 #define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_)              \
334         __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64_data, U64, _val_, _len_)
335 #define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_)           \
336         __PROPERTY_ENTRY_ARRAY_LEN(_name_, str, STRING, _val_, _len_)
337 #define PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, _len_)              \
338         __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_,                       \
339                                 sizeof(struct software_node_ref_args),  \
340                                 REF, _val_, _len_)
341
342 #define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_)                          \
343         PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
344 #define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_)                         \
345         PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
346 #define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_)                         \
347         PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
348 #define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_)                         \
349         PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
350 #define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_)                      \
351         PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
352 #define PROPERTY_ENTRY_REF_ARRAY(_name_, _val_)                 \
353         PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
354
355 #define __PROPERTY_ENTRY_ELEMENT(_name_, _elem_, _Type_, _val_)         \
356 (struct property_entry) {                                               \
357         .name = _name_,                                                 \
358         .length = __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_),                \
359         .is_inline = true,                                              \
360         .type = DEV_PROP_##_Type_,                                      \
361         { .value = { ._elem_[0] = _val_ } },                            \
362 }
363
364 #define PROPERTY_ENTRY_U8(_name_, _val_)                                \
365         __PROPERTY_ENTRY_ELEMENT(_name_, u8_data, U8, _val_)
366 #define PROPERTY_ENTRY_U16(_name_, _val_)                               \
367         __PROPERTY_ENTRY_ELEMENT(_name_, u16_data, U16, _val_)
368 #define PROPERTY_ENTRY_U32(_name_, _val_)                               \
369         __PROPERTY_ENTRY_ELEMENT(_name_, u32_data, U32, _val_)
370 #define PROPERTY_ENTRY_U64(_name_, _val_)                               \
371         __PROPERTY_ENTRY_ELEMENT(_name_, u64_data, U64, _val_)
372 #define PROPERTY_ENTRY_STRING(_name_, _val_)                            \
373         __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
374
375 #define PROPERTY_ENTRY_BOOL(_name_)             \
376 (struct property_entry) {                       \
377         .name = _name_,                         \
378         .is_inline = true,                      \
379 }
380
381 #define PROPERTY_ENTRY_REF(_name_, _ref_, ...)                          \
382 (struct property_entry) {                                               \
383         .name = _name_,                                                 \
384         .length = sizeof(struct software_node_ref_args),                \
385         .type = DEV_PROP_REF,                                           \
386         { .pointer = &SOFTWARE_NODE_REFERENCE(_ref_, ##__VA_ARGS__), }, \
387 }
388
389 struct property_entry *
390 property_entries_dup(const struct property_entry *properties);
391
392 void property_entries_free(const struct property_entry *properties);
393
394 bool device_dma_supported(struct device *dev);
395
396 enum dev_dma_attr device_get_dma_attr(struct device *dev);
397
398 const void *device_get_match_data(const struct device *dev);
399
400 int device_get_phy_mode(struct device *dev);
401 int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
402
403 void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index);
404
405 struct fwnode_handle *fwnode_graph_get_next_endpoint(
406         const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
407 struct fwnode_handle *
408 fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
409 struct fwnode_handle *fwnode_graph_get_remote_port_parent(
410         const struct fwnode_handle *fwnode);
411 struct fwnode_handle *fwnode_graph_get_remote_port(
412         const struct fwnode_handle *fwnode);
413 struct fwnode_handle *fwnode_graph_get_remote_endpoint(
414         const struct fwnode_handle *fwnode);
415
416 static inline bool fwnode_graph_is_endpoint(struct fwnode_handle *fwnode)
417 {
418         return fwnode_property_present(fwnode, "remote-endpoint");
419 }
420
421 /*
422  * Fwnode lookup flags
423  *
424  * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
425  *                              closest endpoint ID greater than the specified
426  *                              one.
427  * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
428  *                                endpoint of the given endpoint belongs to,
429  *                                may be disabled, or that the endpoint is not
430  *                                connected.
431  */
432 #define FWNODE_GRAPH_ENDPOINT_NEXT      BIT(0)
433 #define FWNODE_GRAPH_DEVICE_DISABLED    BIT(1)
434
435 struct fwnode_handle *
436 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
437                                 u32 port, u32 endpoint, unsigned long flags);
438 unsigned int fwnode_graph_get_endpoint_count(struct fwnode_handle *fwnode,
439                                              unsigned long flags);
440
441 #define fwnode_graph_for_each_endpoint(fwnode, child)                   \
442         for (child = NULL;                                              \
443              (child = fwnode_graph_get_next_endpoint(fwnode, child)); )
444
445 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
446                                 struct fwnode_endpoint *endpoint);
447
448 typedef void *(*devcon_match_fn_t)(struct fwnode_handle *fwnode, const char *id,
449                                    void *data);
450
451 void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
452                                    const char *con_id, void *data,
453                                    devcon_match_fn_t match);
454
455 static inline void *device_connection_find_match(struct device *dev,
456                                                  const char *con_id, void *data,
457                                                  devcon_match_fn_t match)
458 {
459         return fwnode_connection_find_match(dev_fwnode(dev), con_id, data, match);
460 }
461
462 int fwnode_connection_find_matches(struct fwnode_handle *fwnode,
463                                    const char *con_id, void *data,
464                                    devcon_match_fn_t match,
465                                    void **matches, unsigned int matches_len);
466
467 /* -------------------------------------------------------------------------- */
468 /* Software fwnode support - when HW description is incomplete or missing */
469
470 /**
471  * struct software_node - Software node description
472  * @name: Name of the software node
473  * @parent: Parent of the software node
474  * @properties: Array of device properties
475  */
476 struct software_node {
477         const char *name;
478         const struct software_node *parent;
479         const struct property_entry *properties;
480 };
481
482 bool is_software_node(const struct fwnode_handle *fwnode);
483 const struct software_node *
484 to_software_node(const struct fwnode_handle *fwnode);
485 struct fwnode_handle *software_node_fwnode(const struct software_node *node);
486
487 const struct software_node *
488 software_node_find_by_name(const struct software_node *parent,
489                            const char *name);
490
491 int software_node_register_nodes(const struct software_node *nodes);
492 void software_node_unregister_nodes(const struct software_node *nodes);
493
494 int software_node_register_node_group(const struct software_node **node_group);
495 void software_node_unregister_node_group(const struct software_node **node_group);
496
497 int software_node_register(const struct software_node *node);
498 void software_node_unregister(const struct software_node *node);
499
500 struct fwnode_handle *
501 fwnode_create_software_node(const struct property_entry *properties,
502                             const struct fwnode_handle *parent);
503 void fwnode_remove_software_node(struct fwnode_handle *fwnode);
504
505 int device_add_software_node(struct device *dev, const struct software_node *node);
506 void device_remove_software_node(struct device *dev);
507
508 int device_create_managed_software_node(struct device *dev,
509                                         const struct property_entry *properties,
510                                         const struct software_node *parent);
511
512 #endif /* _LINUX_PROPERTY_H_ */
This page took 0.063325 seconds and 4 git commands to generate.