Merge tag 'rproc-v6.5' of git://git.kernel.org/pub/scm/linux/kernel/git/remoteproc...
[linux-2.6-block.git] / include / linux / device.h
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * device.h - generic, centralized driver model
4  *
5  * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
6  * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
7  * Copyright (c) 2008-2009 Novell Inc.
8  *
9  * See Documentation/driver-api/driver-model/ for more information.
10  */
11
12 #ifndef _DEVICE_H_
13 #define _DEVICE_H_
14
15 #include <linux/dev_printk.h>
16 #include <linux/energy_model.h>
17 #include <linux/ioport.h>
18 #include <linux/kobject.h>
19 #include <linux/klist.h>
20 #include <linux/list.h>
21 #include <linux/lockdep.h>
22 #include <linux/compiler.h>
23 #include <linux/types.h>
24 #include <linux/mutex.h>
25 #include <linux/pm.h>
26 #include <linux/atomic.h>
27 #include <linux/uidgid.h>
28 #include <linux/gfp.h>
29 #include <linux/overflow.h>
30 #include <linux/device/bus.h>
31 #include <linux/device/class.h>
32 #include <linux/device/driver.h>
33 #include <asm/device.h>
34
35 struct device;
36 struct device_private;
37 struct device_driver;
38 struct driver_private;
39 struct module;
40 struct class;
41 struct subsys_private;
42 struct device_node;
43 struct fwnode_handle;
44 struct iommu_ops;
45 struct iommu_group;
46 struct dev_pin_info;
47 struct dev_iommu;
48 struct msi_device_data;
49
50 /**
51  * struct subsys_interface - interfaces to device functions
52  * @name:       name of the device function
53  * @subsys:     subsystem of the devices to attach to
54  * @node:       the list of functions registered at the subsystem
55  * @add_dev:    device hookup to device function handler
56  * @remove_dev: device hookup to device function handler
57  *
58  * Simple interfaces attached to a subsystem. Multiple interfaces can
59  * attach to a subsystem and its devices. Unlike drivers, they do not
60  * exclusively claim or control devices. Interfaces usually represent
61  * a specific functionality of a subsystem/class of devices.
62  */
63 struct subsys_interface {
64         const char *name;
65         struct bus_type *subsys;
66         struct list_head node;
67         int (*add_dev)(struct device *dev, struct subsys_interface *sif);
68         void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
69 };
70
71 int subsys_interface_register(struct subsys_interface *sif);
72 void subsys_interface_unregister(struct subsys_interface *sif);
73
74 int subsys_system_register(struct bus_type *subsys,
75                            const struct attribute_group **groups);
76 int subsys_virtual_register(struct bus_type *subsys,
77                             const struct attribute_group **groups);
78
79 /*
80  * The type of device, "struct device" is embedded in. A class
81  * or bus can contain devices of different types
82  * like "partitions" and "disks", "mouse" and "event".
83  * This identifies the device type and carries type-specific
84  * information, equivalent to the kobj_type of a kobject.
85  * If "name" is specified, the uevent will contain it in
86  * the DEVTYPE variable.
87  */
88 struct device_type {
89         const char *name;
90         const struct attribute_group **groups;
91         int (*uevent)(const struct device *dev, struct kobj_uevent_env *env);
92         char *(*devnode)(const struct device *dev, umode_t *mode,
93                          kuid_t *uid, kgid_t *gid);
94         void (*release)(struct device *dev);
95
96         const struct dev_pm_ops *pm;
97 };
98
99 /**
100  * struct device_attribute - Interface for exporting device attributes.
101  * @attr: sysfs attribute definition.
102  * @show: Show handler.
103  * @store: Store handler.
104  */
105 struct device_attribute {
106         struct attribute        attr;
107         ssize_t (*show)(struct device *dev, struct device_attribute *attr,
108                         char *buf);
109         ssize_t (*store)(struct device *dev, struct device_attribute *attr,
110                          const char *buf, size_t count);
111 };
112
113 /**
114  * struct dev_ext_attribute - Exported device attribute with extra context.
115  * @attr: Exported device attribute.
116  * @var: Pointer to context.
117  */
118 struct dev_ext_attribute {
119         struct device_attribute attr;
120         void *var;
121 };
122
123 ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
124                           char *buf);
125 ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
126                            const char *buf, size_t count);
127 ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
128                         char *buf);
129 ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
130                          const char *buf, size_t count);
131 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
132                         char *buf);
133 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
134                          const char *buf, size_t count);
135
136 /**
137  * DEVICE_ATTR - Define a device attribute.
138  * @_name: Attribute name.
139  * @_mode: File mode.
140  * @_show: Show handler. Optional, but mandatory if attribute is readable.
141  * @_store: Store handler. Optional, but mandatory if attribute is writable.
142  *
143  * Convenience macro for defining a struct device_attribute.
144  *
145  * For example, ``DEVICE_ATTR(foo, 0644, foo_show, foo_store);`` expands to:
146  *
147  * .. code-block:: c
148  *
149  *      struct device_attribute dev_attr_foo = {
150  *              .attr   = { .name = "foo", .mode = 0644 },
151  *              .show   = foo_show,
152  *              .store  = foo_store,
153  *      };
154  */
155 #define DEVICE_ATTR(_name, _mode, _show, _store) \
156         struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
157
158 /**
159  * DEVICE_ATTR_PREALLOC - Define a preallocated device attribute.
160  * @_name: Attribute name.
161  * @_mode: File mode.
162  * @_show: Show handler. Optional, but mandatory if attribute is readable.
163  * @_store: Store handler. Optional, but mandatory if attribute is writable.
164  *
165  * Like DEVICE_ATTR(), but ``SYSFS_PREALLOC`` is set on @_mode.
166  */
167 #define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
168         struct device_attribute dev_attr_##_name = \
169                 __ATTR_PREALLOC(_name, _mode, _show, _store)
170
171 /**
172  * DEVICE_ATTR_RW - Define a read-write device attribute.
173  * @_name: Attribute name.
174  *
175  * Like DEVICE_ATTR(), but @_mode is 0644, @_show is <_name>_show,
176  * and @_store is <_name>_store.
177  */
178 #define DEVICE_ATTR_RW(_name) \
179         struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
180
181 /**
182  * DEVICE_ATTR_ADMIN_RW - Define an admin-only read-write device attribute.
183  * @_name: Attribute name.
184  *
185  * Like DEVICE_ATTR_RW(), but @_mode is 0600.
186  */
187 #define DEVICE_ATTR_ADMIN_RW(_name) \
188         struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
189
190 /**
191  * DEVICE_ATTR_RO - Define a readable device attribute.
192  * @_name: Attribute name.
193  *
194  * Like DEVICE_ATTR(), but @_mode is 0444 and @_show is <_name>_show.
195  */
196 #define DEVICE_ATTR_RO(_name) \
197         struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
198
199 /**
200  * DEVICE_ATTR_ADMIN_RO - Define an admin-only readable device attribute.
201  * @_name: Attribute name.
202  *
203  * Like DEVICE_ATTR_RO(), but @_mode is 0400.
204  */
205 #define DEVICE_ATTR_ADMIN_RO(_name) \
206         struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
207
208 /**
209  * DEVICE_ATTR_WO - Define an admin-only writable device attribute.
210  * @_name: Attribute name.
211  *
212  * Like DEVICE_ATTR(), but @_mode is 0200 and @_store is <_name>_store.
213  */
214 #define DEVICE_ATTR_WO(_name) \
215         struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
216
217 /**
218  * DEVICE_ULONG_ATTR - Define a device attribute backed by an unsigned long.
219  * @_name: Attribute name.
220  * @_mode: File mode.
221  * @_var: Identifier of unsigned long.
222  *
223  * Like DEVICE_ATTR(), but @_show and @_store are automatically provided
224  * such that reads and writes to the attribute from userspace affect @_var.
225  */
226 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
227         struct dev_ext_attribute dev_attr_##_name = \
228                 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
229
230 /**
231  * DEVICE_INT_ATTR - Define a device attribute backed by an int.
232  * @_name: Attribute name.
233  * @_mode: File mode.
234  * @_var: Identifier of int.
235  *
236  * Like DEVICE_ULONG_ATTR(), but @_var is an int.
237  */
238 #define DEVICE_INT_ATTR(_name, _mode, _var) \
239         struct dev_ext_attribute dev_attr_##_name = \
240                 { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
241
242 /**
243  * DEVICE_BOOL_ATTR - Define a device attribute backed by a bool.
244  * @_name: Attribute name.
245  * @_mode: File mode.
246  * @_var: Identifier of bool.
247  *
248  * Like DEVICE_ULONG_ATTR(), but @_var is a bool.
249  */
250 #define DEVICE_BOOL_ATTR(_name, _mode, _var) \
251         struct dev_ext_attribute dev_attr_##_name = \
252                 { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
253
254 #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
255         struct device_attribute dev_attr_##_name =              \
256                 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
257
258 int device_create_file(struct device *device,
259                        const struct device_attribute *entry);
260 void device_remove_file(struct device *dev,
261                         const struct device_attribute *attr);
262 bool device_remove_file_self(struct device *dev,
263                              const struct device_attribute *attr);
264 int __must_check device_create_bin_file(struct device *dev,
265                                         const struct bin_attribute *attr);
266 void device_remove_bin_file(struct device *dev,
267                             const struct bin_attribute *attr);
268
269 /* device resource management */
270 typedef void (*dr_release_t)(struct device *dev, void *res);
271 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
272
273 void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
274                           int nid, const char *name) __malloc;
275 #define devres_alloc(release, size, gfp) \
276         __devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
277 #define devres_alloc_node(release, size, gfp, nid) \
278         __devres_alloc_node(release, size, gfp, nid, #release)
279
280 void devres_for_each_res(struct device *dev, dr_release_t release,
281                          dr_match_t match, void *match_data,
282                          void (*fn)(struct device *, void *, void *),
283                          void *data);
284 void devres_free(void *res);
285 void devres_add(struct device *dev, void *res);
286 void *devres_find(struct device *dev, dr_release_t release,
287                   dr_match_t match, void *match_data);
288 void *devres_get(struct device *dev, void *new_res,
289                  dr_match_t match, void *match_data);
290 void *devres_remove(struct device *dev, dr_release_t release,
291                     dr_match_t match, void *match_data);
292 int devres_destroy(struct device *dev, dr_release_t release,
293                    dr_match_t match, void *match_data);
294 int devres_release(struct device *dev, dr_release_t release,
295                    dr_match_t match, void *match_data);
296
297 /* devres group */
298 void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp);
299 void devres_close_group(struct device *dev, void *id);
300 void devres_remove_group(struct device *dev, void *id);
301 int devres_release_group(struct device *dev, void *id);
302
303 /* managed devm_k.alloc/kfree for device drivers */
304 void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __alloc_size(2);
305 void *devm_krealloc(struct device *dev, void *ptr, size_t size,
306                     gfp_t gfp) __must_check __realloc_size(3);
307 __printf(3, 0) char *devm_kvasprintf(struct device *dev, gfp_t gfp,
308                                      const char *fmt, va_list ap) __malloc;
309 __printf(3, 4) char *devm_kasprintf(struct device *dev, gfp_t gfp,
310                                     const char *fmt, ...) __malloc;
311 static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
312 {
313         return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
314 }
315 static inline void *devm_kmalloc_array(struct device *dev,
316                                        size_t n, size_t size, gfp_t flags)
317 {
318         size_t bytes;
319
320         if (unlikely(check_mul_overflow(n, size, &bytes)))
321                 return NULL;
322
323         return devm_kmalloc(dev, bytes, flags);
324 }
325 static inline void *devm_kcalloc(struct device *dev,
326                                  size_t n, size_t size, gfp_t flags)
327 {
328         return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
329 }
330 static inline __realloc_size(3, 4) void * __must_check
331 devm_krealloc_array(struct device *dev, void *p, size_t new_n, size_t new_size, gfp_t flags)
332 {
333         size_t bytes;
334
335         if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
336                 return NULL;
337
338         return devm_krealloc(dev, p, bytes, flags);
339 }
340
341 void devm_kfree(struct device *dev, const void *p);
342 char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
343 const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
344 void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
345         __realloc_size(3);
346
347 unsigned long devm_get_free_pages(struct device *dev,
348                                   gfp_t gfp_mask, unsigned int order);
349 void devm_free_pages(struct device *dev, unsigned long addr);
350
351 void __iomem *devm_ioremap_resource(struct device *dev,
352                                     const struct resource *res);
353 void __iomem *devm_ioremap_resource_wc(struct device *dev,
354                                        const struct resource *res);
355
356 void __iomem *devm_of_iomap(struct device *dev,
357                             struct device_node *node, int index,
358                             resource_size_t *size);
359
360 /* allows to add/remove a custom action to devres stack */
361 void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
362 void devm_release_action(struct device *dev, void (*action)(void *), void *data);
363
364 int __devm_add_action(struct device *dev, void (*action)(void *), void *data, const char *name);
365 #define devm_add_action(release, action, data) \
366         __devm_add_action(release, action, data, #action)
367
368 static inline int __devm_add_action_or_reset(struct device *dev, void (*action)(void *),
369                                              void *data, const char *name)
370 {
371         int ret;
372
373         ret = __devm_add_action(dev, action, data, name);
374         if (ret)
375                 action(data);
376
377         return ret;
378 }
379 #define devm_add_action_or_reset(release, action, data) \
380         __devm_add_action_or_reset(release, action, data, #action)
381
382 /**
383  * devm_alloc_percpu - Resource-managed alloc_percpu
384  * @dev: Device to allocate per-cpu memory for
385  * @type: Type to allocate per-cpu memory for
386  *
387  * Managed alloc_percpu. Per-cpu memory allocated with this function is
388  * automatically freed on driver detach.
389  *
390  * RETURNS:
391  * Pointer to allocated memory on success, NULL on failure.
392  */
393 #define devm_alloc_percpu(dev, type)      \
394         ((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
395                                                       __alignof__(type)))
396
397 void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
398                                    size_t align);
399 void devm_free_percpu(struct device *dev, void __percpu *pdata);
400
401 struct device_dma_parameters {
402         /*
403          * a low level driver may set these to teach IOMMU code about
404          * sg limitations.
405          */
406         unsigned int max_segment_size;
407         unsigned int min_align_mask;
408         unsigned long segment_boundary_mask;
409 };
410
411 /**
412  * enum device_link_state - Device link states.
413  * @DL_STATE_NONE: The presence of the drivers is not being tracked.
414  * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
415  * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.
416  * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present).
417  * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present.
418  * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding.
419  */
420 enum device_link_state {
421         DL_STATE_NONE = -1,
422         DL_STATE_DORMANT = 0,
423         DL_STATE_AVAILABLE,
424         DL_STATE_CONSUMER_PROBE,
425         DL_STATE_ACTIVE,
426         DL_STATE_SUPPLIER_UNBIND,
427 };
428
429 /*
430  * Device link flags.
431  *
432  * STATELESS: The core will not remove this link automatically.
433  * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind.
434  * PM_RUNTIME: If set, the runtime PM framework will use this link.
435  * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation.
436  * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind.
437  * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds.
438  * MANAGED: The core tracks presence of supplier/consumer drivers (internal).
439  * SYNC_STATE_ONLY: Link only affects sync_state() behavior.
440  * INFERRED: Inferred from data (eg: firmware) and not from driver actions.
441  */
442 #define DL_FLAG_STATELESS               BIT(0)
443 #define DL_FLAG_AUTOREMOVE_CONSUMER     BIT(1)
444 #define DL_FLAG_PM_RUNTIME              BIT(2)
445 #define DL_FLAG_RPM_ACTIVE              BIT(3)
446 #define DL_FLAG_AUTOREMOVE_SUPPLIER     BIT(4)
447 #define DL_FLAG_AUTOPROBE_CONSUMER      BIT(5)
448 #define DL_FLAG_MANAGED                 BIT(6)
449 #define DL_FLAG_SYNC_STATE_ONLY         BIT(7)
450 #define DL_FLAG_INFERRED                BIT(8)
451 #define DL_FLAG_CYCLE                   BIT(9)
452
453 /**
454  * enum dl_dev_state - Device driver presence tracking information.
455  * @DL_DEV_NO_DRIVER: There is no driver attached to the device.
456  * @DL_DEV_PROBING: A driver is probing.
457  * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device.
458  * @DL_DEV_UNBINDING: The driver is unbinding from the device.
459  */
460 enum dl_dev_state {
461         DL_DEV_NO_DRIVER = 0,
462         DL_DEV_PROBING,
463         DL_DEV_DRIVER_BOUND,
464         DL_DEV_UNBINDING,
465 };
466
467 /**
468  * enum device_removable - Whether the device is removable. The criteria for a
469  * device to be classified as removable is determined by its subsystem or bus.
470  * @DEVICE_REMOVABLE_NOT_SUPPORTED: This attribute is not supported for this
471  *                                  device (default).
472  * @DEVICE_REMOVABLE_UNKNOWN:  Device location is Unknown.
473  * @DEVICE_FIXED: Device is not removable by the user.
474  * @DEVICE_REMOVABLE: Device is removable by the user.
475  */
476 enum device_removable {
477         DEVICE_REMOVABLE_NOT_SUPPORTED = 0, /* must be 0 */
478         DEVICE_REMOVABLE_UNKNOWN,
479         DEVICE_FIXED,
480         DEVICE_REMOVABLE,
481 };
482
483 /**
484  * struct dev_links_info - Device data related to device links.
485  * @suppliers: List of links to supplier devices.
486  * @consumers: List of links to consumer devices.
487  * @defer_sync: Hook to global list of devices that have deferred sync_state.
488  * @status: Driver status information.
489  */
490 struct dev_links_info {
491         struct list_head suppliers;
492         struct list_head consumers;
493         struct list_head defer_sync;
494         enum dl_dev_state status;
495 };
496
497 /**
498  * struct dev_msi_info - Device data related to MSI
499  * @domain:     The MSI interrupt domain associated to the device
500  * @data:       Pointer to MSI device data
501  */
502 struct dev_msi_info {
503 #ifdef CONFIG_GENERIC_MSI_IRQ
504         struct irq_domain       *domain;
505         struct msi_device_data  *data;
506 #endif
507 };
508
509 /**
510  * enum device_physical_location_panel - Describes which panel surface of the
511  * system's housing the device connection point resides on.
512  * @DEVICE_PANEL_TOP: Device connection point is on the top panel.
513  * @DEVICE_PANEL_BOTTOM: Device connection point is on the bottom panel.
514  * @DEVICE_PANEL_LEFT: Device connection point is on the left panel.
515  * @DEVICE_PANEL_RIGHT: Device connection point is on the right panel.
516  * @DEVICE_PANEL_FRONT: Device connection point is on the front panel.
517  * @DEVICE_PANEL_BACK: Device connection point is on the back panel.
518  * @DEVICE_PANEL_UNKNOWN: The panel with device connection point is unknown.
519  */
520 enum device_physical_location_panel {
521         DEVICE_PANEL_TOP,
522         DEVICE_PANEL_BOTTOM,
523         DEVICE_PANEL_LEFT,
524         DEVICE_PANEL_RIGHT,
525         DEVICE_PANEL_FRONT,
526         DEVICE_PANEL_BACK,
527         DEVICE_PANEL_UNKNOWN,
528 };
529
530 /**
531  * enum device_physical_location_vertical_position - Describes vertical
532  * position of the device connection point on the panel surface.
533  * @DEVICE_VERT_POS_UPPER: Device connection point is at upper part of panel.
534  * @DEVICE_VERT_POS_CENTER: Device connection point is at center part of panel.
535  * @DEVICE_VERT_POS_LOWER: Device connection point is at lower part of panel.
536  */
537 enum device_physical_location_vertical_position {
538         DEVICE_VERT_POS_UPPER,
539         DEVICE_VERT_POS_CENTER,
540         DEVICE_VERT_POS_LOWER,
541 };
542
543 /**
544  * enum device_physical_location_horizontal_position - Describes horizontal
545  * position of the device connection point on the panel surface.
546  * @DEVICE_HORI_POS_LEFT: Device connection point is at left part of panel.
547  * @DEVICE_HORI_POS_CENTER: Device connection point is at center part of panel.
548  * @DEVICE_HORI_POS_RIGHT: Device connection point is at right part of panel.
549  */
550 enum device_physical_location_horizontal_position {
551         DEVICE_HORI_POS_LEFT,
552         DEVICE_HORI_POS_CENTER,
553         DEVICE_HORI_POS_RIGHT,
554 };
555
556 /**
557  * struct device_physical_location - Device data related to physical location
558  * of the device connection point.
559  * @panel: Panel surface of the system's housing that the device connection
560  *         point resides on.
561  * @vertical_position: Vertical position of the device connection point within
562  *                     the panel.
563  * @horizontal_position: Horizontal position of the device connection point
564  *                       within the panel.
565  * @dock: Set if the device connection point resides in a docking station or
566  *        port replicator.
567  * @lid: Set if this device connection point resides on the lid of laptop
568  *       system.
569  */
570 struct device_physical_location {
571         enum device_physical_location_panel panel;
572         enum device_physical_location_vertical_position vertical_position;
573         enum device_physical_location_horizontal_position horizontal_position;
574         bool dock;
575         bool lid;
576 };
577
578 /**
579  * struct device - The basic device structure
580  * @parent:     The device's "parent" device, the device to which it is attached.
581  *              In most cases, a parent device is some sort of bus or host
582  *              controller. If parent is NULL, the device, is a top-level device,
583  *              which is not usually what you want.
584  * @p:          Holds the private data of the driver core portions of the device.
585  *              See the comment of the struct device_private for detail.
586  * @kobj:       A top-level, abstract class from which other classes are derived.
587  * @init_name:  Initial name of the device.
588  * @type:       The type of device.
589  *              This identifies the device type and carries type-specific
590  *              information.
591  * @mutex:      Mutex to synchronize calls to its driver.
592  * @bus:        Type of bus device is on.
593  * @driver:     Which driver has allocated this
594  * @platform_data: Platform data specific to the device.
595  *              Example: For devices on custom boards, as typical of embedded
596  *              and SOC based hardware, Linux often uses platform_data to point
597  *              to board-specific structures describing devices and how they
598  *              are wired.  That can include what ports are available, chip
599  *              variants, which GPIO pins act in what additional roles, and so
600  *              on.  This shrinks the "Board Support Packages" (BSPs) and
601  *              minimizes board-specific #ifdefs in drivers.
602  * @driver_data: Private pointer for driver specific info.
603  * @links:      Links to suppliers and consumers of this device.
604  * @power:      For device power management.
605  *              See Documentation/driver-api/pm/devices.rst for details.
606  * @pm_domain:  Provide callbacks that are executed during system suspend,
607  *              hibernation, system resume and during runtime PM transitions
608  *              along with subsystem-level and driver-level callbacks.
609  * @em_pd:      device's energy model performance domain
610  * @pins:       For device pin management.
611  *              See Documentation/driver-api/pin-control.rst for details.
612  * @msi:        MSI related data
613  * @numa_node:  NUMA node this device is close to.
614  * @dma_ops:    DMA mapping operations for this device.
615  * @dma_mask:   Dma mask (if dma'ble device).
616  * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
617  *              hardware supports 64-bit addresses for consistent allocations
618  *              such descriptors.
619  * @bus_dma_limit: Limit of an upstream bridge or bus which imposes a smaller
620  *              DMA limit than the device itself supports.
621  * @dma_range_map: map for DMA memory ranges relative to that of RAM
622  * @dma_parms:  A low level driver may set these to teach IOMMU code about
623  *              segment limitations.
624  * @dma_pools:  Dma pools (if dma'ble device).
625  * @dma_mem:    Internal for coherent mem override.
626  * @cma_area:   Contiguous memory area for dma allocations
627  * @dma_io_tlb_mem: Pointer to the swiotlb pool used.  Not for driver use.
628  * @archdata:   For arch-specific additions.
629  * @of_node:    Associated device tree node.
630  * @fwnode:     Associated device node supplied by platform firmware.
631  * @devt:       For creating the sysfs "dev".
632  * @id:         device instance
633  * @devres_lock: Spinlock to protect the resource of the device.
634  * @devres_head: The resources list of the device.
635  * @knode_class: The node used to add the device to the class list.
636  * @class:      The class of the device.
637  * @groups:     Optional attribute groups.
638  * @release:    Callback to free the device after all references have
639  *              gone away. This should be set by the allocator of the
640  *              device (i.e. the bus driver that discovered the device).
641  * @iommu_group: IOMMU group the device belongs to.
642  * @iommu:      Per device generic IOMMU runtime data
643  * @physical_location: Describes physical location of the device connection
644  *              point in the system housing.
645  * @removable:  Whether the device can be removed from the system. This
646  *              should be set by the subsystem / bus driver that discovered
647  *              the device.
648  *
649  * @offline_disabled: If set, the device is permanently online.
650  * @offline:    Set after successful invocation of bus type's .offline().
651  * @of_node_reused: Set if the device-tree node is shared with an ancestor
652  *              device.
653  * @state_synced: The hardware state of this device has been synced to match
654  *                the software state of this device by calling the driver/bus
655  *                sync_state() callback.
656  * @can_match:  The device has matched with a driver at least once or it is in
657  *              a bus (like AMBA) which can't check for matching drivers until
658  *              other devices probe successfully.
659  * @dma_coherent: this particular device is dma coherent, even if the
660  *              architecture supports non-coherent devices.
661  * @dma_ops_bypass: If set to %true then the dma_ops are bypassed for the
662  *              streaming DMA operations (->map_* / ->unmap_* / ->sync_*),
663  *              and optionall (if the coherent mask is large enough) also
664  *              for dma allocations.  This flag is managed by the dma ops
665  *              instance from ->dma_supported.
666  *
667  * At the lowest level, every device in a Linux system is represented by an
668  * instance of struct device. The device structure contains the information
669  * that the device model core needs to model the system. Most subsystems,
670  * however, track additional information about the devices they host. As a
671  * result, it is rare for devices to be represented by bare device structures;
672  * instead, that structure, like kobject structures, is usually embedded within
673  * a higher-level representation of the device.
674  */
675 struct device {
676         struct kobject kobj;
677         struct device           *parent;
678
679         struct device_private   *p;
680
681         const char              *init_name; /* initial name of the device */
682         const struct device_type *type;
683
684         const struct bus_type   *bus;   /* type of bus device is on */
685         struct device_driver *driver;   /* which driver has allocated this
686                                            device */
687         void            *platform_data; /* Platform specific data, device
688                                            core doesn't touch it */
689         void            *driver_data;   /* Driver data, set and get with
690                                            dev_set_drvdata/dev_get_drvdata */
691         struct mutex            mutex;  /* mutex to synchronize calls to
692                                          * its driver.
693                                          */
694
695         struct dev_links_info   links;
696         struct dev_pm_info      power;
697         struct dev_pm_domain    *pm_domain;
698
699 #ifdef CONFIG_ENERGY_MODEL
700         struct em_perf_domain   *em_pd;
701 #endif
702
703 #ifdef CONFIG_PINCTRL
704         struct dev_pin_info     *pins;
705 #endif
706         struct dev_msi_info     msi;
707 #ifdef CONFIG_DMA_OPS
708         const struct dma_map_ops *dma_ops;
709 #endif
710         u64             *dma_mask;      /* dma mask (if dma'able device) */
711         u64             coherent_dma_mask;/* Like dma_mask, but for
712                                              alloc_coherent mappings as
713                                              not all hardware supports
714                                              64 bit addresses for consistent
715                                              allocations such descriptors. */
716         u64             bus_dma_limit;  /* upstream dma constraint */
717         const struct bus_dma_region *dma_range_map;
718
719         struct device_dma_parameters *dma_parms;
720
721         struct list_head        dma_pools;      /* dma pools (if dma'ble) */
722
723 #ifdef CONFIG_DMA_DECLARE_COHERENT
724         struct dma_coherent_mem *dma_mem; /* internal for coherent mem
725                                              override */
726 #endif
727 #ifdef CONFIG_DMA_CMA
728         struct cma *cma_area;           /* contiguous memory area for dma
729                                            allocations */
730 #endif
731 #ifdef CONFIG_SWIOTLB
732         struct io_tlb_mem *dma_io_tlb_mem;
733 #endif
734         /* arch specific additions */
735         struct dev_archdata     archdata;
736
737         struct device_node      *of_node; /* associated device tree node */
738         struct fwnode_handle    *fwnode; /* firmware device node */
739
740 #ifdef CONFIG_NUMA
741         int             numa_node;      /* NUMA node this device is close to */
742 #endif
743         dev_t                   devt;   /* dev_t, creates the sysfs "dev" */
744         u32                     id;     /* device instance */
745
746         spinlock_t              devres_lock;
747         struct list_head        devres_head;
748
749         const struct class      *class;
750         const struct attribute_group **groups;  /* optional groups */
751
752         void    (*release)(struct device *dev);
753         struct iommu_group      *iommu_group;
754         struct dev_iommu        *iommu;
755
756         struct device_physical_location *physical_location;
757
758         enum device_removable   removable;
759
760         bool                    offline_disabled:1;
761         bool                    offline:1;
762         bool                    of_node_reused:1;
763         bool                    state_synced:1;
764         bool                    can_match:1;
765 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
766     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
767     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
768         bool                    dma_coherent:1;
769 #endif
770 #ifdef CONFIG_DMA_OPS_BYPASS
771         bool                    dma_ops_bypass : 1;
772 #endif
773 };
774
775 /**
776  * struct device_link - Device link representation.
777  * @supplier: The device on the supplier end of the link.
778  * @s_node: Hook to the supplier device's list of links to consumers.
779  * @consumer: The device on the consumer end of the link.
780  * @c_node: Hook to the consumer device's list of links to suppliers.
781  * @link_dev: device used to expose link details in sysfs
782  * @status: The state of the link (with respect to the presence of drivers).
783  * @flags: Link flags.
784  * @rpm_active: Whether or not the consumer device is runtime-PM-active.
785  * @kref: Count repeated addition of the same link.
786  * @rm_work: Work structure used for removing the link.
787  * @supplier_preactivated: Supplier has been made active before consumer probe.
788  */
789 struct device_link {
790         struct device *supplier;
791         struct list_head s_node;
792         struct device *consumer;
793         struct list_head c_node;
794         struct device link_dev;
795         enum device_link_state status;
796         u32 flags;
797         refcount_t rpm_active;
798         struct kref kref;
799         struct work_struct rm_work;
800         bool supplier_preactivated; /* Owned by consumer probe. */
801 };
802
803 #define kobj_to_dev(__kobj)     container_of_const(__kobj, struct device, kobj)
804
805 /**
806  * device_iommu_mapped - Returns true when the device DMA is translated
807  *                       by an IOMMU
808  * @dev: Device to perform the check on
809  */
810 static inline bool device_iommu_mapped(struct device *dev)
811 {
812         return (dev->iommu_group != NULL);
813 }
814
815 /* Get the wakeup routines, which depend on struct device */
816 #include <linux/pm_wakeup.h>
817
818 /**
819  * dev_name - Return a device's name.
820  * @dev: Device with name to get.
821  * Return: The kobject name of the device, or its initial name if unavailable.
822  */
823 static inline const char *dev_name(const struct device *dev)
824 {
825         /* Use the init name until the kobject becomes available */
826         if (dev->init_name)
827                 return dev->init_name;
828
829         return kobject_name(&dev->kobj);
830 }
831
832 /**
833  * dev_bus_name - Return a device's bus/class name, if at all possible
834  * @dev: struct device to get the bus/class name of
835  *
836  * Will return the name of the bus/class the device is attached to.  If it is
837  * not attached to a bus/class, an empty string will be returned.
838  */
839 static inline const char *dev_bus_name(const struct device *dev)
840 {
841         return dev->bus ? dev->bus->name : (dev->class ? dev->class->name : "");
842 }
843
844 __printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...);
845
846 #ifdef CONFIG_NUMA
847 static inline int dev_to_node(struct device *dev)
848 {
849         return dev->numa_node;
850 }
851 static inline void set_dev_node(struct device *dev, int node)
852 {
853         dev->numa_node = node;
854 }
855 #else
856 static inline int dev_to_node(struct device *dev)
857 {
858         return NUMA_NO_NODE;
859 }
860 static inline void set_dev_node(struct device *dev, int node)
861 {
862 }
863 #endif
864
865 static inline struct irq_domain *dev_get_msi_domain(const struct device *dev)
866 {
867 #ifdef CONFIG_GENERIC_MSI_IRQ
868         return dev->msi.domain;
869 #else
870         return NULL;
871 #endif
872 }
873
874 static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d)
875 {
876 #ifdef CONFIG_GENERIC_MSI_IRQ
877         dev->msi.domain = d;
878 #endif
879 }
880
881 static inline void *dev_get_drvdata(const struct device *dev)
882 {
883         return dev->driver_data;
884 }
885
886 static inline void dev_set_drvdata(struct device *dev, void *data)
887 {
888         dev->driver_data = data;
889 }
890
891 static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
892 {
893         return dev ? dev->power.subsys_data : NULL;
894 }
895
896 static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
897 {
898         return dev->kobj.uevent_suppress;
899 }
900
901 static inline void dev_set_uevent_suppress(struct device *dev, int val)
902 {
903         dev->kobj.uevent_suppress = val;
904 }
905
906 static inline int device_is_registered(struct device *dev)
907 {
908         return dev->kobj.state_in_sysfs;
909 }
910
911 static inline void device_enable_async_suspend(struct device *dev)
912 {
913         if (!dev->power.is_prepared)
914                 dev->power.async_suspend = true;
915 }
916
917 static inline void device_disable_async_suspend(struct device *dev)
918 {
919         if (!dev->power.is_prepared)
920                 dev->power.async_suspend = false;
921 }
922
923 static inline bool device_async_suspend_enabled(struct device *dev)
924 {
925         return !!dev->power.async_suspend;
926 }
927
928 static inline bool device_pm_not_required(struct device *dev)
929 {
930         return dev->power.no_pm;
931 }
932
933 static inline void device_set_pm_not_required(struct device *dev)
934 {
935         dev->power.no_pm = true;
936 }
937
938 static inline void dev_pm_syscore_device(struct device *dev, bool val)
939 {
940 #ifdef CONFIG_PM_SLEEP
941         dev->power.syscore = val;
942 #endif
943 }
944
945 static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags)
946 {
947         dev->power.driver_flags = flags;
948 }
949
950 static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags)
951 {
952         return !!(dev->power.driver_flags & flags);
953 }
954
955 static inline void device_lock(struct device *dev)
956 {
957         mutex_lock(&dev->mutex);
958 }
959
960 static inline int device_lock_interruptible(struct device *dev)
961 {
962         return mutex_lock_interruptible(&dev->mutex);
963 }
964
965 static inline int device_trylock(struct device *dev)
966 {
967         return mutex_trylock(&dev->mutex);
968 }
969
970 static inline void device_unlock(struct device *dev)
971 {
972         mutex_unlock(&dev->mutex);
973 }
974
975 static inline void device_lock_assert(struct device *dev)
976 {
977         lockdep_assert_held(&dev->mutex);
978 }
979
980 static inline struct device_node *dev_of_node(struct device *dev)
981 {
982         if (!IS_ENABLED(CONFIG_OF) || !dev)
983                 return NULL;
984         return dev->of_node;
985 }
986
987 static inline bool dev_has_sync_state(struct device *dev)
988 {
989         if (!dev)
990                 return false;
991         if (dev->driver && dev->driver->sync_state)
992                 return true;
993         if (dev->bus && dev->bus->sync_state)
994                 return true;
995         return false;
996 }
997
998 static inline void dev_set_removable(struct device *dev,
999                                      enum device_removable removable)
1000 {
1001         dev->removable = removable;
1002 }
1003
1004 static inline bool dev_is_removable(struct device *dev)
1005 {
1006         return dev->removable == DEVICE_REMOVABLE;
1007 }
1008
1009 static inline bool dev_removable_is_valid(struct device *dev)
1010 {
1011         return dev->removable != DEVICE_REMOVABLE_NOT_SUPPORTED;
1012 }
1013
1014 /*
1015  * High level routines for use by the bus drivers
1016  */
1017 int __must_check device_register(struct device *dev);
1018 void device_unregister(struct device *dev);
1019 void device_initialize(struct device *dev);
1020 int __must_check device_add(struct device *dev);
1021 void device_del(struct device *dev);
1022 int device_for_each_child(struct device *dev, void *data,
1023                           int (*fn)(struct device *dev, void *data));
1024 int device_for_each_child_reverse(struct device *dev, void *data,
1025                                   int (*fn)(struct device *dev, void *data));
1026 struct device *device_find_child(struct device *dev, void *data,
1027                                  int (*match)(struct device *dev, void *data));
1028 struct device *device_find_child_by_name(struct device *parent,
1029                                          const char *name);
1030 struct device *device_find_any_child(struct device *parent);
1031
1032 int device_rename(struct device *dev, const char *new_name);
1033 int device_move(struct device *dev, struct device *new_parent,
1034                 enum dpm_order dpm_order);
1035 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid);
1036 int device_is_dependent(struct device *dev, void *target);
1037
1038 static inline bool device_supports_offline(struct device *dev)
1039 {
1040         return dev->bus && dev->bus->offline && dev->bus->online;
1041 }
1042
1043 #define __device_lock_set_class(dev, name, key)                        \
1044 do {                                                                   \
1045         struct device *__d2 __maybe_unused = dev;                      \
1046         lock_set_class(&__d2->mutex.dep_map, name, key, 0, _THIS_IP_); \
1047 } while (0)
1048
1049 /**
1050  * device_lock_set_class - Specify a temporary lock class while a device
1051  *                         is attached to a driver
1052  * @dev: device to modify
1053  * @key: lock class key data
1054  *
1055  * This must be called with the device_lock() already held, for example
1056  * from driver ->probe(). Take care to only override the default
1057  * lockdep_no_validate class.
1058  */
1059 #ifdef CONFIG_LOCKDEP
1060 #define device_lock_set_class(dev, key)                                    \
1061 do {                                                                       \
1062         struct device *__d = dev;                                          \
1063         dev_WARN_ONCE(__d, !lockdep_match_class(&__d->mutex,               \
1064                                                 &__lockdep_no_validate__), \
1065                  "overriding existing custom lock class\n");               \
1066         __device_lock_set_class(__d, #key, key);                           \
1067 } while (0)
1068 #else
1069 #define device_lock_set_class(dev, key) __device_lock_set_class(dev, #key, key)
1070 #endif
1071
1072 /**
1073  * device_lock_reset_class - Return a device to the default lockdep novalidate state
1074  * @dev: device to modify
1075  *
1076  * This must be called with the device_lock() already held, for example
1077  * from driver ->remove().
1078  */
1079 #define device_lock_reset_class(dev) \
1080 do { \
1081         struct device *__d __maybe_unused = dev;                       \
1082         lock_set_novalidate_class(&__d->mutex.dep_map, "&dev->mutex",  \
1083                                   _THIS_IP_);                          \
1084 } while (0)
1085
1086 void lock_device_hotplug(void);
1087 void unlock_device_hotplug(void);
1088 int lock_device_hotplug_sysfs(void);
1089 int device_offline(struct device *dev);
1090 int device_online(struct device *dev);
1091 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1092 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1093 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
1094 void device_set_node(struct device *dev, struct fwnode_handle *fwnode);
1095
1096 static inline int dev_num_vf(struct device *dev)
1097 {
1098         if (dev->bus && dev->bus->num_vf)
1099                 return dev->bus->num_vf(dev);
1100         return 0;
1101 }
1102
1103 /*
1104  * Root device objects for grouping under /sys/devices
1105  */
1106 struct device *__root_device_register(const char *name, struct module *owner);
1107
1108 /* This is a macro to avoid include problems with THIS_MODULE */
1109 #define root_device_register(name) \
1110         __root_device_register(name, THIS_MODULE)
1111
1112 void root_device_unregister(struct device *root);
1113
1114 static inline void *dev_get_platdata(const struct device *dev)
1115 {
1116         return dev->platform_data;
1117 }
1118
1119 /*
1120  * Manual binding of a device to driver. See drivers/base/bus.c
1121  * for information on use.
1122  */
1123 int __must_check device_driver_attach(struct device_driver *drv,
1124                                       struct device *dev);
1125 int __must_check device_bind_driver(struct device *dev);
1126 void device_release_driver(struct device *dev);
1127 int  __must_check device_attach(struct device *dev);
1128 int __must_check driver_attach(struct device_driver *drv);
1129 void device_initial_probe(struct device *dev);
1130 int __must_check device_reprobe(struct device *dev);
1131
1132 bool device_is_bound(struct device *dev);
1133
1134 /*
1135  * Easy functions for dynamically creating devices on the fly
1136  */
1137 __printf(5, 6) struct device *
1138 device_create(const struct class *cls, struct device *parent, dev_t devt,
1139               void *drvdata, const char *fmt, ...);
1140 __printf(6, 7) struct device *
1141 device_create_with_groups(const struct class *cls, struct device *parent, dev_t devt,
1142                           void *drvdata, const struct attribute_group **groups,
1143                           const char *fmt, ...);
1144 void device_destroy(const struct class *cls, dev_t devt);
1145
1146 int __must_check device_add_groups(struct device *dev,
1147                                    const struct attribute_group **groups);
1148 void device_remove_groups(struct device *dev,
1149                           const struct attribute_group **groups);
1150
1151 static inline int __must_check device_add_group(struct device *dev,
1152                                         const struct attribute_group *grp)
1153 {
1154         const struct attribute_group *groups[] = { grp, NULL };
1155
1156         return device_add_groups(dev, groups);
1157 }
1158
1159 static inline void device_remove_group(struct device *dev,
1160                                        const struct attribute_group *grp)
1161 {
1162         const struct attribute_group *groups[] = { grp, NULL };
1163
1164         return device_remove_groups(dev, groups);
1165 }
1166
1167 int __must_check devm_device_add_groups(struct device *dev,
1168                                         const struct attribute_group **groups);
1169 int __must_check devm_device_add_group(struct device *dev,
1170                                        const struct attribute_group *grp);
1171
1172 /*
1173  * Platform "fixup" functions - allow the platform to have their say
1174  * about devices and actions that the general device layer doesn't
1175  * know about.
1176  */
1177 /* Notify platform of device discovery */
1178 extern int (*platform_notify)(struct device *dev);
1179
1180 extern int (*platform_notify_remove)(struct device *dev);
1181
1182
1183 /*
1184  * get_device - atomically increment the reference count for the device.
1185  *
1186  */
1187 struct device *get_device(struct device *dev);
1188 void put_device(struct device *dev);
1189 bool kill_device(struct device *dev);
1190
1191 #ifdef CONFIG_DEVTMPFS
1192 int devtmpfs_mount(void);
1193 #else
1194 static inline int devtmpfs_mount(void) { return 0; }
1195 #endif
1196
1197 /* drivers/base/power/shutdown.c */
1198 void device_shutdown(void);
1199
1200 /* debugging and troubleshooting/diagnostic helpers. */
1201 const char *dev_driver_string(const struct device *dev);
1202
1203 /* Device links interface. */
1204 struct device_link *device_link_add(struct device *consumer,
1205                                     struct device *supplier, u32 flags);
1206 void device_link_del(struct device_link *link);
1207 void device_link_remove(void *consumer, struct device *supplier);
1208 void device_links_supplier_sync_state_pause(void);
1209 void device_links_supplier_sync_state_resume(void);
1210
1211 __printf(3, 4) int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
1212
1213 /* Create alias, so I can be autoloaded. */
1214 #define MODULE_ALIAS_CHARDEV(major,minor) \
1215         MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1216 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1217         MODULE_ALIAS("char-major-" __stringify(major) "-*")
1218
1219 #endif /* _DEVICE_H_ */