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