• 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/ioport.h>
16 #include <linux/kobject.h>
17 #include <linux/klist.h>
18 #include <linux/list.h>
19 #include <linux/lockdep.h>
20 #include <linux/compiler.h>
21 #include <linux/types.h>
22 #include <linux/mutex.h>
23 #include <linux/pm.h>
24 #include <linux/atomic.h>
25 #include <linux/ratelimit.h>
26 #include <linux/uidgid.h>
27 #include <linux/gfp.h>
28 #include <linux/overflow.h>
29 #include <linux/android_kabi.h>
30 #include <asm/device.h>
31 
32 struct device;
33 struct device_private;
34 struct device_driver;
35 struct driver_private;
36 struct module;
37 struct class;
38 struct subsys_private;
39 struct bus_type;
40 struct device_node;
41 struct fwnode_handle;
42 struct iommu_ops;
43 struct iommu_group;
44 struct iommu_fwspec;
45 struct dev_pin_info;
46 struct iommu_param;
47 
48 struct bus_attribute {
49 	struct attribute	attr;
50 	ssize_t (*show)(struct bus_type *bus, char *buf);
51 	ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
52 };
53 
54 #define BUS_ATTR_RW(_name) \
55 	struct bus_attribute bus_attr_##_name = __ATTR_RW(_name)
56 #define BUS_ATTR_RO(_name) \
57 	struct bus_attribute bus_attr_##_name = __ATTR_RO(_name)
58 #define BUS_ATTR_WO(_name) \
59 	struct bus_attribute bus_attr_##_name = __ATTR_WO(_name)
60 
61 extern int __must_check bus_create_file(struct bus_type *,
62 					struct bus_attribute *);
63 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
64 
65 /**
66  * struct bus_type - The bus type of the device
67  *
68  * @name:	The name of the bus.
69  * @dev_name:	Used for subsystems to enumerate devices like ("foo%u", dev->id).
70  * @dev_root:	Default device to use as the parent.
71  * @bus_groups:	Default attributes of the bus.
72  * @dev_groups:	Default attributes of the devices on the bus.
73  * @drv_groups: Default attributes of the device drivers on the bus.
74  * @match:	Called, perhaps multiple times, whenever a new device or driver
75  *		is added for this bus. It should return a positive value if the
76  *		given device can be handled by the given driver and zero
77  *		otherwise. It may also return error code if determining that
78  *		the driver supports the device is not possible. In case of
79  *		-EPROBE_DEFER it will queue the device for deferred probing.
80  * @uevent:	Called when a device is added, removed, or a few other things
81  *		that generate uevents to add the environment variables.
82  * @probe:	Called when a new device or driver add to this bus, and callback
83  *		the specific driver's probe to initial the matched device.
84  * @sync_state:	Called to sync device state to software state after all the
85  *		state tracking consumers linked to this device (present at
86  *		the time of late_initcall) have successfully bound to a
87  *		driver. If the device has no consumers, this function will
88  *		be called at late_initcall_sync level. If the device has
89  *		consumers that are never bound to a driver, this function
90  *		will never get called until they do.
91  * @remove:	Called when a device removed from this bus.
92  * @shutdown:	Called at shut-down time to quiesce the device.
93  *
94  * @online:	Called to put the device back online (after offlining it).
95  * @offline:	Called to put the device offline for hot-removal. May fail.
96  *
97  * @suspend:	Called when a device on this bus wants to go to sleep mode.
98  * @resume:	Called to bring a device on this bus out of sleep mode.
99  * @num_vf:	Called to find out how many virtual functions a device on this
100  *		bus supports.
101  * @dma_configure:	Called to setup DMA configuration on a device on
102  *			this bus.
103  * @pm:		Power management operations of this bus, callback the specific
104  *		device driver's pm-ops.
105  * @iommu_ops:  IOMMU specific operations for this bus, used to attach IOMMU
106  *              driver implementations to a bus and allow the driver to do
107  *              bus-specific setup
108  * @p:		The private data of the driver core, only the driver core can
109  *		touch this.
110  * @lock_key:	Lock class key for use by the lock validator
111  * @need_parent_lock:	When probing or removing a device on this bus, the
112  *			device core should lock the device's parent.
113  *
114  * A bus is a channel between the processor and one or more devices. For the
115  * purposes of the device model, all devices are connected via a bus, even if
116  * it is an internal, virtual, "platform" bus. Buses can plug into each other.
117  * A USB controller is usually a PCI device, for example. The device model
118  * represents the actual connections between buses and the devices they control.
119  * A bus is represented by the bus_type structure. It contains the name, the
120  * default attributes, the bus' methods, PM operations, and the driver core's
121  * private data.
122  */
123 struct bus_type {
124 	const char		*name;
125 	const char		*dev_name;
126 	struct device		*dev_root;
127 	const struct attribute_group **bus_groups;
128 	const struct attribute_group **dev_groups;
129 	const struct attribute_group **drv_groups;
130 
131 	int (*match)(struct device *dev, struct device_driver *drv);
132 	int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
133 	int (*probe)(struct device *dev);
134 	void (*sync_state)(struct device *dev);
135 	int (*remove)(struct device *dev);
136 	void (*shutdown)(struct device *dev);
137 
138 	int (*online)(struct device *dev);
139 	int (*offline)(struct device *dev);
140 
141 	int (*suspend)(struct device *dev, pm_message_t state);
142 	int (*resume)(struct device *dev);
143 
144 	int (*num_vf)(struct device *dev);
145 
146 	int (*dma_configure)(struct device *dev);
147 
148 	const struct dev_pm_ops *pm;
149 
150 	const struct iommu_ops *iommu_ops;
151 
152 	struct subsys_private *p;
153 	struct lock_class_key lock_key;
154 
155 	bool need_parent_lock;
156 
157 	ANDROID_KABI_RESERVE(1);
158 	ANDROID_KABI_RESERVE(2);
159 	ANDROID_KABI_RESERVE(3);
160 	ANDROID_KABI_RESERVE(4);
161 };
162 
163 extern int __must_check bus_register(struct bus_type *bus);
164 
165 extern void bus_unregister(struct bus_type *bus);
166 
167 extern int __must_check bus_rescan_devices(struct bus_type *bus);
168 
169 /* iterator helpers for buses */
170 struct subsys_dev_iter {
171 	struct klist_iter		ki;
172 	const struct device_type	*type;
173 };
174 void subsys_dev_iter_init(struct subsys_dev_iter *iter,
175 			 struct bus_type *subsys,
176 			 struct device *start,
177 			 const struct device_type *type);
178 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter);
179 void subsys_dev_iter_exit(struct subsys_dev_iter *iter);
180 
181 int device_match_name(struct device *dev, const void *name);
182 int device_match_of_node(struct device *dev, const void *np);
183 int device_match_fwnode(struct device *dev, const void *fwnode);
184 int device_match_devt(struct device *dev, const void *pdevt);
185 int device_match_acpi_dev(struct device *dev, const void *adev);
186 int device_match_any(struct device *dev, const void *unused);
187 
188 int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
189 		     int (*fn)(struct device *dev, void *data));
190 struct device *bus_find_device(struct bus_type *bus, struct device *start,
191 			       const void *data,
192 			       int (*match)(struct device *dev, const void *data));
193 /**
194  * bus_find_device_by_name - device iterator for locating a particular device
195  * of a specific name.
196  * @bus: bus type
197  * @start: Device to begin with
198  * @name: name of the device to match
199  */
bus_find_device_by_name(struct bus_type * bus,struct device * start,const char * name)200 static inline struct device *bus_find_device_by_name(struct bus_type *bus,
201 						     struct device *start,
202 						     const char *name)
203 {
204 	return bus_find_device(bus, start, name, device_match_name);
205 }
206 
207 /**
208  * bus_find_device_by_of_node : device iterator for locating a particular device
209  * matching the of_node.
210  * @bus: bus type
211  * @np: of_node of the device to match.
212  */
213 static inline struct device *
bus_find_device_by_of_node(struct bus_type * bus,const struct device_node * np)214 bus_find_device_by_of_node(struct bus_type *bus, const struct device_node *np)
215 {
216 	return bus_find_device(bus, NULL, np, device_match_of_node);
217 }
218 
219 /**
220  * bus_find_device_by_fwnode : device iterator for locating a particular device
221  * matching the fwnode.
222  * @bus: bus type
223  * @fwnode: fwnode of the device to match.
224  */
225 static inline struct device *
bus_find_device_by_fwnode(struct bus_type * bus,const struct fwnode_handle * fwnode)226 bus_find_device_by_fwnode(struct bus_type *bus, const struct fwnode_handle *fwnode)
227 {
228 	return bus_find_device(bus, NULL, fwnode, device_match_fwnode);
229 }
230 
231 /**
232  * bus_find_device_by_devt : device iterator for locating a particular device
233  * matching the device type.
234  * @bus: bus type
235  * @devt: device type of the device to match.
236  */
bus_find_device_by_devt(struct bus_type * bus,dev_t devt)237 static inline struct device *bus_find_device_by_devt(struct bus_type *bus,
238 						     dev_t devt)
239 {
240 	return bus_find_device(bus, NULL, &devt, device_match_devt);
241 }
242 
243 /**
244  * bus_find_next_device - Find the next device after a given device in a
245  * given bus.
246  * @bus: bus type
247  * @cur: device to begin the search with.
248  */
249 static inline struct device *
bus_find_next_device(struct bus_type * bus,struct device * cur)250 bus_find_next_device(struct bus_type *bus,struct device *cur)
251 {
252 	return bus_find_device(bus, cur, NULL, device_match_any);
253 }
254 
255 #ifdef CONFIG_ACPI
256 struct acpi_device;
257 
258 /**
259  * bus_find_device_by_acpi_dev : device iterator for locating a particular device
260  * matching the ACPI COMPANION device.
261  * @bus: bus type
262  * @adev: ACPI COMPANION device to match.
263  */
264 static inline struct device *
bus_find_device_by_acpi_dev(struct bus_type * bus,const struct acpi_device * adev)265 bus_find_device_by_acpi_dev(struct bus_type *bus, const struct acpi_device *adev)
266 {
267 	return bus_find_device(bus, NULL, adev, device_match_acpi_dev);
268 }
269 #else
270 static inline struct device *
bus_find_device_by_acpi_dev(struct bus_type * bus,const void * adev)271 bus_find_device_by_acpi_dev(struct bus_type *bus, const void *adev)
272 {
273 	return NULL;
274 }
275 #endif
276 
277 struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
278 					struct device *hint);
279 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
280 		     void *data, int (*fn)(struct device_driver *, void *));
281 void bus_sort_breadthfirst(struct bus_type *bus,
282 			   int (*compare)(const struct device *a,
283 					  const struct device *b));
284 /*
285  * Bus notifiers: Get notified of addition/removal of devices
286  * and binding/unbinding of drivers to devices.
287  * In the long run, it should be a replacement for the platform
288  * notify hooks.
289  */
290 struct notifier_block;
291 
292 extern int bus_register_notifier(struct bus_type *bus,
293 				 struct notifier_block *nb);
294 extern int bus_unregister_notifier(struct bus_type *bus,
295 				   struct notifier_block *nb);
296 
297 /* All 4 notifers below get called with the target struct device *
298  * as an argument. Note that those functions are likely to be called
299  * with the device lock held in the core, so be careful.
300  */
301 #define BUS_NOTIFY_ADD_DEVICE		0x00000001 /* device added */
302 #define BUS_NOTIFY_DEL_DEVICE		0x00000002 /* device to be removed */
303 #define BUS_NOTIFY_REMOVED_DEVICE	0x00000003 /* device removed */
304 #define BUS_NOTIFY_BIND_DRIVER		0x00000004 /* driver about to be
305 						      bound */
306 #define BUS_NOTIFY_BOUND_DRIVER		0x00000005 /* driver bound to device */
307 #define BUS_NOTIFY_UNBIND_DRIVER	0x00000006 /* driver about to be
308 						      unbound */
309 #define BUS_NOTIFY_UNBOUND_DRIVER	0x00000007 /* driver is unbound
310 						      from the device */
311 #define BUS_NOTIFY_DRIVER_NOT_BOUND	0x00000008 /* driver fails to be bound */
312 
313 extern struct kset *bus_get_kset(struct bus_type *bus);
314 extern struct klist *bus_get_device_klist(struct bus_type *bus);
315 
316 /**
317  * enum probe_type - device driver probe type to try
318  *	Device drivers may opt in for special handling of their
319  *	respective probe routines. This tells the core what to
320  *	expect and prefer.
321  *
322  * @PROBE_DEFAULT_STRATEGY: Used by drivers that work equally well
323  *	whether probed synchronously or asynchronously.
324  * @PROBE_PREFER_ASYNCHRONOUS: Drivers for "slow" devices which
325  *	probing order is not essential for booting the system may
326  *	opt into executing their probes asynchronously.
327  * @PROBE_FORCE_SYNCHRONOUS: Use this to annotate drivers that need
328  *	their probe routines to run synchronously with driver and
329  *	device registration (with the exception of -EPROBE_DEFER
330  *	handling - re-probing always ends up being done asynchronously).
331  *
332  * Note that the end goal is to switch the kernel to use asynchronous
333  * probing by default, so annotating drivers with
334  * %PROBE_PREFER_ASYNCHRONOUS is a temporary measure that allows us
335  * to speed up boot process while we are validating the rest of the
336  * drivers.
337  */
338 enum probe_type {
339 	PROBE_DEFAULT_STRATEGY,
340 	PROBE_PREFER_ASYNCHRONOUS,
341 	PROBE_FORCE_SYNCHRONOUS,
342 };
343 
344 /**
345  * struct device_driver - The basic device driver structure
346  * @name:	Name of the device driver.
347  * @bus:	The bus which the device of this driver belongs to.
348  * @owner:	The module owner.
349  * @mod_name:	Used for built-in modules.
350  * @suppress_bind_attrs: Disables bind/unbind via sysfs.
351  * @probe_type:	Type of the probe (synchronous or asynchronous) to use.
352  * @of_match_table: The open firmware table.
353  * @acpi_match_table: The ACPI match table.
354  * @probe:	Called to query the existence of a specific device,
355  *		whether this driver can work with it, and bind the driver
356  *		to a specific device.
357  * @sync_state:	Called to sync device state to software state after all the
358  *		state tracking consumers linked to this device (present at
359  *		the time of late_initcall) have successfully bound to a
360  *		driver. If the device has no consumers, this function will
361  *		be called at late_initcall_sync level. If the device has
362  *		consumers that are never bound to a driver, this function
363  *		will never get called until they do.
364  * @remove:	Called when the device is removed from the system to
365  *		unbind a device from this driver.
366  * @shutdown:	Called at shut-down time to quiesce the device.
367  * @suspend:	Called to put the device to sleep mode. Usually to a
368  *		low power state.
369  * @resume:	Called to bring a device from sleep mode.
370  * @groups:	Default attributes that get created by the driver core
371  *		automatically.
372  * @dev_groups:	Additional attributes attached to device instance once the
373  *		it is bound to the driver.
374  * @pm:		Power management operations of the device which matched
375  *		this driver.
376  * @coredump:	Called when sysfs entry is written to. The device driver
377  *		is expected to call the dev_coredump API resulting in a
378  *		uevent.
379  * @p:		Driver core's private data, no one other than the driver
380  *		core can touch this.
381  *
382  * The device driver-model tracks all of the drivers known to the system.
383  * The main reason for this tracking is to enable the driver core to match
384  * up drivers with new devices. Once drivers are known objects within the
385  * system, however, a number of other things become possible. Device drivers
386  * can export information and configuration variables that are independent
387  * of any specific device.
388  */
389 struct device_driver {
390 	const char		*name;
391 	struct bus_type		*bus;
392 
393 	struct module		*owner;
394 	const char		*mod_name;	/* used for built-in modules */
395 
396 	bool suppress_bind_attrs;	/* disables bind/unbind via sysfs */
397 	enum probe_type probe_type;
398 
399 	const struct of_device_id	*of_match_table;
400 	const struct acpi_device_id	*acpi_match_table;
401 
402 	int (*probe) (struct device *dev);
403 	void (*sync_state)(struct device *dev);
404 	int (*remove) (struct device *dev);
405 	void (*shutdown) (struct device *dev);
406 	int (*suspend) (struct device *dev, pm_message_t state);
407 	int (*resume) (struct device *dev);
408 	const struct attribute_group **groups;
409 	const struct attribute_group **dev_groups;
410 
411 	const struct dev_pm_ops *pm;
412 	void (*coredump) (struct device *dev);
413 
414 	struct driver_private *p;
415 
416 	ANDROID_KABI_RESERVE(1);
417 	ANDROID_KABI_RESERVE(2);
418 	ANDROID_KABI_RESERVE(3);
419 	ANDROID_KABI_RESERVE(4);
420 };
421 
422 
423 extern int __must_check driver_register(struct device_driver *drv);
424 extern void driver_unregister(struct device_driver *drv);
425 
426 extern struct device_driver *driver_find(const char *name,
427 					 struct bus_type *bus);
428 extern int driver_probe_done(void);
429 extern void wait_for_device_probe(void);
430 
431 /* sysfs interface for exporting driver attributes */
432 
433 struct driver_attribute {
434 	struct attribute attr;
435 	ssize_t (*show)(struct device_driver *driver, char *buf);
436 	ssize_t (*store)(struct device_driver *driver, const char *buf,
437 			 size_t count);
438 };
439 
440 #define DRIVER_ATTR_RW(_name) \
441 	struct driver_attribute driver_attr_##_name = __ATTR_RW(_name)
442 #define DRIVER_ATTR_RO(_name) \
443 	struct driver_attribute driver_attr_##_name = __ATTR_RO(_name)
444 #define DRIVER_ATTR_WO(_name) \
445 	struct driver_attribute driver_attr_##_name = __ATTR_WO(_name)
446 
447 extern int __must_check driver_create_file(struct device_driver *driver,
448 					const struct driver_attribute *attr);
449 extern void driver_remove_file(struct device_driver *driver,
450 			       const struct driver_attribute *attr);
451 
452 int driver_set_override(struct device *dev, const char **override,
453 			const char *s, size_t len);
454 extern int __must_check driver_for_each_device(struct device_driver *drv,
455 					       struct device *start,
456 					       void *data,
457 					       int (*fn)(struct device *dev,
458 							 void *));
459 struct device *driver_find_device(struct device_driver *drv,
460 				  struct device *start, const void *data,
461 				  int (*match)(struct device *dev, const void *data));
462 
463 /**
464  * driver_find_device_by_name - device iterator for locating a particular device
465  * of a specific name.
466  * @drv: the driver we're iterating
467  * @name: name of the device to match
468  */
driver_find_device_by_name(struct device_driver * drv,const char * name)469 static inline struct device *driver_find_device_by_name(struct device_driver *drv,
470 							const char *name)
471 {
472 	return driver_find_device(drv, NULL, name, device_match_name);
473 }
474 
475 /**
476  * driver_find_device_by_of_node- device iterator for locating a particular device
477  * by of_node pointer.
478  * @drv: the driver we're iterating
479  * @np: of_node pointer to match.
480  */
481 static inline struct device *
driver_find_device_by_of_node(struct device_driver * drv,const struct device_node * np)482 driver_find_device_by_of_node(struct device_driver *drv,
483 			      const struct device_node *np)
484 {
485 	return driver_find_device(drv, NULL, np, device_match_of_node);
486 }
487 
488 /**
489  * driver_find_device_by_fwnode- device iterator for locating a particular device
490  * by fwnode pointer.
491  * @drv: the driver we're iterating
492  * @fwnode: fwnode pointer to match.
493  */
494 static inline struct device *
driver_find_device_by_fwnode(struct device_driver * drv,const struct fwnode_handle * fwnode)495 driver_find_device_by_fwnode(struct device_driver *drv,
496 			     const struct fwnode_handle *fwnode)
497 {
498 	return driver_find_device(drv, NULL, fwnode, device_match_fwnode);
499 }
500 
501 /**
502  * driver_find_device_by_devt- device iterator for locating a particular device
503  * by devt.
504  * @drv: the driver we're iterating
505  * @devt: devt pointer to match.
506  */
driver_find_device_by_devt(struct device_driver * drv,dev_t devt)507 static inline struct device *driver_find_device_by_devt(struct device_driver *drv,
508 							dev_t devt)
509 {
510 	return driver_find_device(drv, NULL, &devt, device_match_devt);
511 }
512 
driver_find_next_device(struct device_driver * drv,struct device * start)513 static inline struct device *driver_find_next_device(struct device_driver *drv,
514 						     struct device *start)
515 {
516 	return driver_find_device(drv, start, NULL, device_match_any);
517 }
518 
519 #ifdef CONFIG_ACPI
520 /**
521  * driver_find_device_by_acpi_dev : device iterator for locating a particular
522  * device matching the ACPI_COMPANION device.
523  * @drv: the driver we're iterating
524  * @adev: ACPI_COMPANION device to match.
525  */
526 static inline struct device *
driver_find_device_by_acpi_dev(struct device_driver * drv,const struct acpi_device * adev)527 driver_find_device_by_acpi_dev(struct device_driver *drv,
528 			       const struct acpi_device *adev)
529 {
530 	return driver_find_device(drv, NULL, adev, device_match_acpi_dev);
531 }
532 #else
533 static inline struct device *
driver_find_device_by_acpi_dev(struct device_driver * drv,const void * adev)534 driver_find_device_by_acpi_dev(struct device_driver *drv, const void *adev)
535 {
536 	return NULL;
537 }
538 #endif
539 
540 void driver_deferred_probe_add(struct device *dev);
541 int driver_deferred_probe_check_state(struct device *dev);
542 int driver_deferred_probe_check_state_continue(struct device *dev);
543 
544 /**
545  * struct subsys_interface - interfaces to device functions
546  * @name:       name of the device function
547  * @subsys:     subsytem of the devices to attach to
548  * @node:       the list of functions registered at the subsystem
549  * @add_dev:    device hookup to device function handler
550  * @remove_dev: device hookup to device function handler
551  *
552  * Simple interfaces attached to a subsystem. Multiple interfaces can
553  * attach to a subsystem and its devices. Unlike drivers, they do not
554  * exclusively claim or control devices. Interfaces usually represent
555  * a specific functionality of a subsystem/class of devices.
556  */
557 struct subsys_interface {
558 	const char *name;
559 	struct bus_type *subsys;
560 	struct list_head node;
561 	int (*add_dev)(struct device *dev, struct subsys_interface *sif);
562 	void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
563 };
564 
565 int subsys_interface_register(struct subsys_interface *sif);
566 void subsys_interface_unregister(struct subsys_interface *sif);
567 
568 int subsys_system_register(struct bus_type *subsys,
569 			   const struct attribute_group **groups);
570 int subsys_virtual_register(struct bus_type *subsys,
571 			    const struct attribute_group **groups);
572 
573 /**
574  * struct class - device classes
575  * @name:	Name of the class.
576  * @owner:	The module owner.
577  * @class_groups: Default attributes of this class.
578  * @dev_groups:	Default attributes of the devices that belong to the class.
579  * @dev_kobj:	The kobject that represents this class and links it into the hierarchy.
580  * @dev_uevent:	Called when a device is added, removed from this class, or a
581  *		few other things that generate uevents to add the environment
582  *		variables.
583  * @devnode:	Callback to provide the devtmpfs.
584  * @class_release: Called to release this class.
585  * @dev_release: Called to release the device.
586  * @shutdown_pre: Called at shut-down time before driver shutdown.
587  * @ns_type:	Callbacks so sysfs can detemine namespaces.
588  * @namespace:	Namespace of the device belongs to this class.
589  * @get_ownership: Allows class to specify uid/gid of the sysfs directories
590  *		for the devices belonging to the class. Usually tied to
591  *		device's namespace.
592  * @pm:		The default device power management operations of this class.
593  * @p:		The private data of the driver core, no one other than the
594  *		driver core can touch this.
595  *
596  * A class is a higher-level view of a device that abstracts out low-level
597  * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
598  * at the class level, they are all simply disks. Classes allow user space
599  * to work with devices based on what they do, rather than how they are
600  * connected or how they work.
601  */
602 struct class {
603 	const char		*name;
604 	struct module		*owner;
605 
606 	const struct attribute_group	**class_groups;
607 	const struct attribute_group	**dev_groups;
608 	struct kobject			*dev_kobj;
609 
610 	int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
611 	char *(*devnode)(struct device *dev, umode_t *mode);
612 
613 	void (*class_release)(struct class *class);
614 	void (*dev_release)(struct device *dev);
615 
616 	int (*shutdown_pre)(struct device *dev);
617 
618 	const struct kobj_ns_type_operations *ns_type;
619 	const void *(*namespace)(struct device *dev);
620 
621 	void (*get_ownership)(struct device *dev, kuid_t *uid, kgid_t *gid);
622 
623 	const struct dev_pm_ops *pm;
624 
625 	struct subsys_private *p;
626 
627 	ANDROID_KABI_RESERVE(1);
628 	ANDROID_KABI_RESERVE(2);
629 	ANDROID_KABI_RESERVE(3);
630 	ANDROID_KABI_RESERVE(4);
631 };
632 
633 struct class_dev_iter {
634 	struct klist_iter		ki;
635 	const struct device_type	*type;
636 };
637 
638 extern struct kobject *sysfs_dev_block_kobj;
639 extern struct kobject *sysfs_dev_char_kobj;
640 extern int __must_check __class_register(struct class *class,
641 					 struct lock_class_key *key);
642 extern void class_unregister(struct class *class);
643 
644 /* This is a #define to keep the compiler from merging different
645  * instances of the __key variable */
646 #define class_register(class)			\
647 ({						\
648 	static struct lock_class_key __key;	\
649 	__class_register(class, &__key);	\
650 })
651 
652 struct class_compat;
653 struct class_compat *class_compat_register(const char *name);
654 void class_compat_unregister(struct class_compat *cls);
655 int class_compat_create_link(struct class_compat *cls, struct device *dev,
656 			     struct device *device_link);
657 void class_compat_remove_link(struct class_compat *cls, struct device *dev,
658 			      struct device *device_link);
659 
660 extern void class_dev_iter_init(struct class_dev_iter *iter,
661 				struct class *class,
662 				struct device *start,
663 				const struct device_type *type);
664 extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
665 extern void class_dev_iter_exit(struct class_dev_iter *iter);
666 
667 extern int class_for_each_device(struct class *class, struct device *start,
668 				 void *data,
669 				 int (*fn)(struct device *dev, void *data));
670 extern struct device *class_find_device(struct class *class,
671 					struct device *start, const void *data,
672 					int (*match)(struct device *, const void *));
673 
674 /**
675  * class_find_device_by_name - device iterator for locating a particular device
676  * of a specific name.
677  * @class: class type
678  * @name: name of the device to match
679  */
class_find_device_by_name(struct class * class,const char * name)680 static inline struct device *class_find_device_by_name(struct class *class,
681 						       const char *name)
682 {
683 	return class_find_device(class, NULL, name, device_match_name);
684 }
685 
686 /**
687  * class_find_device_by_of_node : device iterator for locating a particular device
688  * matching the of_node.
689  * @class: class type
690  * @np: of_node of the device to match.
691  */
692 static inline struct device *
class_find_device_by_of_node(struct class * class,const struct device_node * np)693 class_find_device_by_of_node(struct class *class, const struct device_node *np)
694 {
695 	return class_find_device(class, NULL, np, device_match_of_node);
696 }
697 
698 /**
699  * class_find_device_by_fwnode : device iterator for locating a particular device
700  * matching the fwnode.
701  * @class: class type
702  * @fwnode: fwnode of the device to match.
703  */
704 static inline struct device *
class_find_device_by_fwnode(struct class * class,const struct fwnode_handle * fwnode)705 class_find_device_by_fwnode(struct class *class,
706 			    const struct fwnode_handle *fwnode)
707 {
708 	return class_find_device(class, NULL, fwnode, device_match_fwnode);
709 }
710 
711 /**
712  * class_find_device_by_devt : device iterator for locating a particular device
713  * matching the device type.
714  * @class: class type
715  * @devt: device type of the device to match.
716  */
class_find_device_by_devt(struct class * class,dev_t devt)717 static inline struct device *class_find_device_by_devt(struct class *class,
718 						       dev_t devt)
719 {
720 	return class_find_device(class, NULL, &devt, device_match_devt);
721 }
722 
723 #ifdef CONFIG_ACPI
724 struct acpi_device;
725 /**
726  * class_find_device_by_acpi_dev : device iterator for locating a particular
727  * device matching the ACPI_COMPANION device.
728  * @class: class type
729  * @adev: ACPI_COMPANION device to match.
730  */
731 static inline struct device *
class_find_device_by_acpi_dev(struct class * class,const struct acpi_device * adev)732 class_find_device_by_acpi_dev(struct class *class, const struct acpi_device *adev)
733 {
734 	return class_find_device(class, NULL, adev, device_match_acpi_dev);
735 }
736 #else
737 static inline struct device *
class_find_device_by_acpi_dev(struct class * class,const void * adev)738 class_find_device_by_acpi_dev(struct class *class, const void *adev)
739 {
740 	return NULL;
741 }
742 #endif
743 
744 struct class_attribute {
745 	struct attribute attr;
746 	ssize_t (*show)(struct class *class, struct class_attribute *attr,
747 			char *buf);
748 	ssize_t (*store)(struct class *class, struct class_attribute *attr,
749 			const char *buf, size_t count);
750 };
751 
752 #define CLASS_ATTR_RW(_name) \
753 	struct class_attribute class_attr_##_name = __ATTR_RW(_name)
754 #define CLASS_ATTR_RO(_name) \
755 	struct class_attribute class_attr_##_name = __ATTR_RO(_name)
756 #define CLASS_ATTR_WO(_name) \
757 	struct class_attribute class_attr_##_name = __ATTR_WO(_name)
758 
759 extern int __must_check class_create_file_ns(struct class *class,
760 					     const struct class_attribute *attr,
761 					     const void *ns);
762 extern void class_remove_file_ns(struct class *class,
763 				 const struct class_attribute *attr,
764 				 const void *ns);
765 
class_create_file(struct class * class,const struct class_attribute * attr)766 static inline int __must_check class_create_file(struct class *class,
767 					const struct class_attribute *attr)
768 {
769 	return class_create_file_ns(class, attr, NULL);
770 }
771 
class_remove_file(struct class * class,const struct class_attribute * attr)772 static inline void class_remove_file(struct class *class,
773 				     const struct class_attribute *attr)
774 {
775 	return class_remove_file_ns(class, attr, NULL);
776 }
777 
778 /* Simple class attribute that is just a static string */
779 struct class_attribute_string {
780 	struct class_attribute attr;
781 	char *str;
782 };
783 
784 /* Currently read-only only */
785 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
786 	{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
787 #define CLASS_ATTR_STRING(_name, _mode, _str) \
788 	struct class_attribute_string class_attr_##_name = \
789 		_CLASS_ATTR_STRING(_name, _mode, _str)
790 
791 extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
792                         char *buf);
793 
794 struct class_interface {
795 	struct list_head	node;
796 	struct class		*class;
797 
798 	int (*add_dev)		(struct device *, struct class_interface *);
799 	void (*remove_dev)	(struct device *, struct class_interface *);
800 };
801 
802 extern int __must_check class_interface_register(struct class_interface *);
803 extern void class_interface_unregister(struct class_interface *);
804 
805 extern struct class * __must_check __class_create(struct module *owner,
806 						  const char *name,
807 						  struct lock_class_key *key);
808 extern void class_destroy(struct class *cls);
809 
810 /* This is a #define to keep the compiler from merging different
811  * instances of the __key variable */
812 #define class_create(owner, name)		\
813 ({						\
814 	static struct lock_class_key __key;	\
815 	__class_create(owner, name, &__key);	\
816 })
817 
818 /*
819  * The type of device, "struct device" is embedded in. A class
820  * or bus can contain devices of different types
821  * like "partitions" and "disks", "mouse" and "event".
822  * This identifies the device type and carries type-specific
823  * information, equivalent to the kobj_type of a kobject.
824  * If "name" is specified, the uevent will contain it in
825  * the DEVTYPE variable.
826  */
827 struct device_type {
828 	const char *name;
829 	const struct attribute_group **groups;
830 	int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
831 	char *(*devnode)(struct device *dev, umode_t *mode,
832 			 kuid_t *uid, kgid_t *gid);
833 	void (*release)(struct device *dev);
834 
835 	const struct dev_pm_ops *pm;
836 };
837 
838 /* interface for exporting device attributes */
839 struct device_attribute {
840 	struct attribute	attr;
841 	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
842 			char *buf);
843 	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
844 			 const char *buf, size_t count);
845 };
846 
847 struct dev_ext_attribute {
848 	struct device_attribute attr;
849 	void *var;
850 };
851 
852 ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
853 			  char *buf);
854 ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
855 			   const char *buf, size_t count);
856 ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
857 			char *buf);
858 ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
859 			 const char *buf, size_t count);
860 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
861 			char *buf);
862 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
863 			 const char *buf, size_t count);
864 
865 #define DEVICE_ATTR(_name, _mode, _show, _store) \
866 	struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
867 #define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
868 	struct device_attribute dev_attr_##_name = \
869 		__ATTR_PREALLOC(_name, _mode, _show, _store)
870 #define DEVICE_ATTR_RW(_name) \
871 	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
872 #define DEVICE_ATTR_RO(_name) \
873 	struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
874 #define DEVICE_ATTR_WO(_name) \
875 	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
876 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
877 	struct dev_ext_attribute dev_attr_##_name = \
878 		{ __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
879 #define DEVICE_INT_ATTR(_name, _mode, _var) \
880 	struct dev_ext_attribute dev_attr_##_name = \
881 		{ __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
882 #define DEVICE_BOOL_ATTR(_name, _mode, _var) \
883 	struct dev_ext_attribute dev_attr_##_name = \
884 		{ __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
885 #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
886 	struct device_attribute dev_attr_##_name =		\
887 		__ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
888 
889 extern int device_create_file(struct device *device,
890 			      const struct device_attribute *entry);
891 extern void device_remove_file(struct device *dev,
892 			       const struct device_attribute *attr);
893 extern bool device_remove_file_self(struct device *dev,
894 				    const struct device_attribute *attr);
895 extern int __must_check device_create_bin_file(struct device *dev,
896 					const struct bin_attribute *attr);
897 extern void device_remove_bin_file(struct device *dev,
898 				   const struct bin_attribute *attr);
899 
900 /* device resource management */
901 typedef void (*dr_release_t)(struct device *dev, void *res);
902 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
903 
904 #ifdef CONFIG_DEBUG_DEVRES
905 extern void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
906 				 int nid, const char *name) __malloc;
907 #define devres_alloc(release, size, gfp) \
908 	__devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
909 #define devres_alloc_node(release, size, gfp, nid) \
910 	__devres_alloc_node(release, size, gfp, nid, #release)
911 #else
912 extern void *devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
913 			       int nid) __malloc;
devres_alloc(dr_release_t release,size_t size,gfp_t gfp)914 static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
915 {
916 	return devres_alloc_node(release, size, gfp, NUMA_NO_NODE);
917 }
918 #endif
919 
920 extern void devres_for_each_res(struct device *dev, dr_release_t release,
921 				dr_match_t match, void *match_data,
922 				void (*fn)(struct device *, void *, void *),
923 				void *data);
924 extern void devres_free(void *res);
925 extern void devres_add(struct device *dev, void *res);
926 extern void *devres_find(struct device *dev, dr_release_t release,
927 			 dr_match_t match, void *match_data);
928 extern void *devres_get(struct device *dev, void *new_res,
929 			dr_match_t match, void *match_data);
930 extern void *devres_remove(struct device *dev, dr_release_t release,
931 			   dr_match_t match, void *match_data);
932 extern int devres_destroy(struct device *dev, dr_release_t release,
933 			  dr_match_t match, void *match_data);
934 extern int devres_release(struct device *dev, dr_release_t release,
935 			  dr_match_t match, void *match_data);
936 
937 /* devres group */
938 extern void * __must_check devres_open_group(struct device *dev, void *id,
939 					     gfp_t gfp);
940 extern void devres_close_group(struct device *dev, void *id);
941 extern void devres_remove_group(struct device *dev, void *id);
942 extern int devres_release_group(struct device *dev, void *id);
943 
944 /* managed devm_k.alloc/kfree for device drivers */
945 extern void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc;
946 extern __printf(3, 0)
947 char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
948 		      va_list ap) __malloc;
949 extern __printf(3, 4)
950 char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...) __malloc;
devm_kzalloc(struct device * dev,size_t size,gfp_t gfp)951 static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
952 {
953 	return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
954 }
devm_kmalloc_array(struct device * dev,size_t n,size_t size,gfp_t flags)955 static inline void *devm_kmalloc_array(struct device *dev,
956 				       size_t n, size_t size, gfp_t flags)
957 {
958 	size_t bytes;
959 
960 	if (unlikely(check_mul_overflow(n, size, &bytes)))
961 		return NULL;
962 
963 	return devm_kmalloc(dev, bytes, flags);
964 }
devm_kcalloc(struct device * dev,size_t n,size_t size,gfp_t flags)965 static inline void *devm_kcalloc(struct device *dev,
966 				 size_t n, size_t size, gfp_t flags)
967 {
968 	return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
969 }
970 extern void devm_kfree(struct device *dev, const void *p);
971 extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
972 extern const char *devm_kstrdup_const(struct device *dev,
973 				      const char *s, gfp_t gfp);
974 extern void *devm_kmemdup(struct device *dev, const void *src, size_t len,
975 			  gfp_t gfp);
976 
977 extern unsigned long devm_get_free_pages(struct device *dev,
978 					 gfp_t gfp_mask, unsigned int order);
979 extern void devm_free_pages(struct device *dev, unsigned long addr);
980 
981 void __iomem *devm_ioremap_resource(struct device *dev,
982 				    const struct resource *res);
983 
984 void __iomem *devm_of_iomap(struct device *dev,
985 			    struct device_node *node, int index,
986 			    resource_size_t *size);
987 
988 /* allows to add/remove a custom action to devres stack */
989 int devm_add_action(struct device *dev, void (*action)(void *), void *data);
990 void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
991 void devm_release_action(struct device *dev, void (*action)(void *), void *data);
992 
devm_add_action_or_reset(struct device * dev,void (* action)(void *),void * data)993 static inline int devm_add_action_or_reset(struct device *dev,
994 					   void (*action)(void *), void *data)
995 {
996 	int ret;
997 
998 	ret = devm_add_action(dev, action, data);
999 	if (ret)
1000 		action(data);
1001 
1002 	return ret;
1003 }
1004 
1005 /**
1006  * devm_alloc_percpu - Resource-managed alloc_percpu
1007  * @dev: Device to allocate per-cpu memory for
1008  * @type: Type to allocate per-cpu memory for
1009  *
1010  * Managed alloc_percpu. Per-cpu memory allocated with this function is
1011  * automatically freed on driver detach.
1012  *
1013  * RETURNS:
1014  * Pointer to allocated memory on success, NULL on failure.
1015  */
1016 #define devm_alloc_percpu(dev, type)      \
1017 	((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
1018 						      __alignof__(type)))
1019 
1020 void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
1021 				   size_t align);
1022 void devm_free_percpu(struct device *dev, void __percpu *pdata);
1023 
1024 struct device_dma_parameters {
1025 	/*
1026 	 * a low level driver may set these to teach IOMMU code about
1027 	 * sg limitations.
1028 	 */
1029 	unsigned int max_segment_size;
1030 	unsigned long segment_boundary_mask;
1031 };
1032 
1033 /**
1034  * struct device_connection - Device Connection Descriptor
1035  * @fwnode: The device node of the connected device
1036  * @endpoint: The names of the two devices connected together
1037  * @id: Unique identifier for the connection
1038  * @list: List head, private, for internal use only
1039  *
1040  * NOTE: @fwnode is not used together with @endpoint. @fwnode is used when
1041  * platform firmware defines the connection. When the connection is registered
1042  * with device_connection_add() @endpoint is used instead.
1043  */
1044 struct device_connection {
1045 	struct fwnode_handle	*fwnode;
1046 	const char		*endpoint[2];
1047 	const char		*id;
1048 	struct list_head	list;
1049 };
1050 
1051 typedef void *(*devcon_match_fn_t)(struct device_connection *con, int ep,
1052 				   void *data);
1053 
1054 void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
1055 				   const char *con_id, void *data,
1056 				   devcon_match_fn_t match);
1057 void *device_connection_find_match(struct device *dev, const char *con_id,
1058 				   void *data, devcon_match_fn_t match);
1059 
1060 struct device *device_connection_find(struct device *dev, const char *con_id);
1061 
1062 void device_connection_add(struct device_connection *con);
1063 void device_connection_remove(struct device_connection *con);
1064 
1065 /**
1066  * device_connections_add - Add multiple device connections at once
1067  * @cons: Zero terminated array of device connection descriptors
1068  */
device_connections_add(struct device_connection * cons)1069 static inline void device_connections_add(struct device_connection *cons)
1070 {
1071 	struct device_connection *c;
1072 
1073 	for (c = cons; c->endpoint[0]; c++)
1074 		device_connection_add(c);
1075 }
1076 
1077 /**
1078  * device_connections_remove - Remove multiple device connections at once
1079  * @cons: Zero terminated array of device connection descriptors
1080  */
device_connections_remove(struct device_connection * cons)1081 static inline void device_connections_remove(struct device_connection *cons)
1082 {
1083 	struct device_connection *c;
1084 
1085 	for (c = cons; c->endpoint[0]; c++)
1086 		device_connection_remove(c);
1087 }
1088 
1089 /**
1090  * enum device_link_state - Device link states.
1091  * @DL_STATE_NONE: The presence of the drivers is not being tracked.
1092  * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
1093  * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.
1094  * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present).
1095  * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present.
1096  * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding.
1097  */
1098 enum device_link_state {
1099 	DL_STATE_NONE = -1,
1100 	DL_STATE_DORMANT = 0,
1101 	DL_STATE_AVAILABLE,
1102 	DL_STATE_CONSUMER_PROBE,
1103 	DL_STATE_ACTIVE,
1104 	DL_STATE_SUPPLIER_UNBIND,
1105 };
1106 
1107 /*
1108  * Device link flags.
1109  *
1110  * STATELESS: The core will not remove this link automatically.
1111  * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind.
1112  * PM_RUNTIME: If set, the runtime PM framework will use this link.
1113  * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation.
1114  * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind.
1115  * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds.
1116  * MANAGED: The core tracks presence of supplier/consumer drivers (internal).
1117  * SYNC_STATE_ONLY: Link only affects sync_state() behavior.
1118  */
1119 #define DL_FLAG_STATELESS		BIT(0)
1120 #define DL_FLAG_AUTOREMOVE_CONSUMER	BIT(1)
1121 #define DL_FLAG_PM_RUNTIME		BIT(2)
1122 #define DL_FLAG_RPM_ACTIVE		BIT(3)
1123 #define DL_FLAG_AUTOREMOVE_SUPPLIER	BIT(4)
1124 #define DL_FLAG_AUTOPROBE_CONSUMER	BIT(5)
1125 #define DL_FLAG_MANAGED			BIT(6)
1126 #define DL_FLAG_SYNC_STATE_ONLY		BIT(7)
1127 
1128 /**
1129  * enum dl_dev_state - Device driver presence tracking information.
1130  * @DL_DEV_NO_DRIVER: There is no driver attached to the device.
1131  * @DL_DEV_PROBING: A driver is probing.
1132  * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device.
1133  * @DL_DEV_UNBINDING: The driver is unbinding from the device.
1134  */
1135 enum dl_dev_state {
1136 	DL_DEV_NO_DRIVER = 0,
1137 	DL_DEV_PROBING,
1138 	DL_DEV_DRIVER_BOUND,
1139 	DL_DEV_UNBINDING,
1140 };
1141 
1142 /**
1143  * struct dev_links_info - Device data related to device links.
1144  * @suppliers: List of links to supplier devices.
1145  * @consumers: List of links to consumer devices.
1146  * @needs_suppliers: Hook to global list of devices waiting for suppliers.
1147  * @defer_hook: Hook to global list of devices that have deferred sync_state or
1148  *		deferred fw_devlink.
1149  * @need_for_probe: If needs_suppliers is on a list, this indicates if the
1150  *		    suppliers are needed for probe or not.
1151  * @status: Driver status information.
1152  */
1153 struct dev_links_info {
1154 	struct list_head suppliers;
1155 	struct list_head consumers;
1156 	struct list_head needs_suppliers;
1157 	struct list_head defer_hook;
1158 	bool need_for_probe;
1159 	enum dl_dev_state status;
1160 
1161 	ANDROID_KABI_RESERVE(1);
1162 	ANDROID_KABI_RESERVE(2);
1163 	ANDROID_KABI_RESERVE(3);
1164 	ANDROID_KABI_RESERVE(4);
1165 };
1166 
1167 /**
1168  * struct device - The basic device structure
1169  * @parent:	The device's "parent" device, the device to which it is attached.
1170  * 		In most cases, a parent device is some sort of bus or host
1171  * 		controller. If parent is NULL, the device, is a top-level device,
1172  * 		which is not usually what you want.
1173  * @p:		Holds the private data of the driver core portions of the device.
1174  * 		See the comment of the struct device_private for detail.
1175  * @kobj:	A top-level, abstract class from which other classes are derived.
1176  * @init_name:	Initial name of the device.
1177  * @type:	The type of device.
1178  * 		This identifies the device type and carries type-specific
1179  * 		information.
1180  * @mutex:	Mutex to synchronize calls to its driver.
1181  * @lockdep_mutex: An optional debug lock that a subsystem can use as a
1182  * 		peer lock to gain localized lockdep coverage of the device_lock.
1183  * @bus:	Type of bus device is on.
1184  * @driver:	Which driver has allocated this
1185  * @platform_data: Platform data specific to the device.
1186  * 		Example: For devices on custom boards, as typical of embedded
1187  * 		and SOC based hardware, Linux often uses platform_data to point
1188  * 		to board-specific structures describing devices and how they
1189  * 		are wired.  That can include what ports are available, chip
1190  * 		variants, which GPIO pins act in what additional roles, and so
1191  * 		on.  This shrinks the "Board Support Packages" (BSPs) and
1192  * 		minimizes board-specific #ifdefs in drivers.
1193  * @driver_data: Private pointer for driver specific info.
1194  * @links:	Links to suppliers and consumers of this device.
1195  * @power:	For device power management.
1196  *		See Documentation/driver-api/pm/devices.rst for details.
1197  * @pm_domain:	Provide callbacks that are executed during system suspend,
1198  * 		hibernation, system resume and during runtime PM transitions
1199  * 		along with subsystem-level and driver-level callbacks.
1200  * @pins:	For device pin management.
1201  *		See Documentation/driver-api/pinctl.rst for details.
1202  * @msi_list:	Hosts MSI descriptors
1203  * @msi_domain: The generic MSI domain this device is using.
1204  * @numa_node:	NUMA node this device is close to.
1205  * @dma_ops:    DMA mapping operations for this device.
1206  * @dma_mask:	Dma mask (if dma'ble device).
1207  * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
1208  * 		hardware supports 64-bit addresses for consistent allocations
1209  * 		such descriptors.
1210  * @bus_dma_mask: Mask of an upstream bridge or bus which imposes a smaller DMA
1211  *		limit than the device itself supports.
1212  * @dma_pfn_offset: offset of DMA memory range relatively of RAM
1213  * @dma_parms:	A low level driver may set these to teach IOMMU code about
1214  * 		segment limitations.
1215  * @dma_pools:	Dma pools (if dma'ble device).
1216  * @dma_mem:	Internal for coherent mem override.
1217  * @cma_area:	Contiguous memory area for dma allocations
1218  * @archdata:	For arch-specific additions.
1219  * @of_node:	Associated device tree node.
1220  * @fwnode:	Associated device node supplied by platform firmware.
1221  * @devt:	For creating the sysfs "dev".
1222  * @id:		device instance
1223  * @devres_lock: Spinlock to protect the resource of the device.
1224  * @devres_head: The resources list of the device.
1225  * @knode_class: The node used to add the device to the class list.
1226  * @class:	The class of the device.
1227  * @groups:	Optional attribute groups.
1228  * @release:	Callback to free the device after all references have
1229  * 		gone away. This should be set by the allocator of the
1230  * 		device (i.e. the bus driver that discovered the device).
1231  * @iommu_group: IOMMU group the device belongs to.
1232  * @iommu_fwspec: IOMMU-specific properties supplied by firmware.
1233  * @iommu_param: Per device generic IOMMU runtime data
1234  *
1235  * @offline_disabled: If set, the device is permanently online.
1236  * @offline:	Set after successful invocation of bus type's .offline().
1237  * @of_node_reused: Set if the device-tree node is shared with an ancestor
1238  *              device.
1239  * @state_synced: The hardware state of this device has been synced to match
1240  *		  the software state of this device by calling the driver/bus
1241  *		  sync_state() callback.
1242  * @dma_coherent: this particular device is dma coherent, even if the
1243  *		architecture supports non-coherent devices.
1244  *
1245  * At the lowest level, every device in a Linux system is represented by an
1246  * instance of struct device. The device structure contains the information
1247  * that the device model core needs to model the system. Most subsystems,
1248  * however, track additional information about the devices they host. As a
1249  * result, it is rare for devices to be represented by bare device structures;
1250  * instead, that structure, like kobject structures, is usually embedded within
1251  * a higher-level representation of the device.
1252  */
1253 struct device {
1254 	struct kobject kobj;
1255 	struct device		*parent;
1256 
1257 	struct device_private	*p;
1258 
1259 	const char		*init_name; /* initial name of the device */
1260 	const struct device_type *type;
1261 
1262 	struct bus_type	*bus;		/* type of bus device is on */
1263 	struct device_driver *driver;	/* which driver has allocated this
1264 					   device */
1265 	void		*platform_data;	/* Platform specific data, device
1266 					   core doesn't touch it */
1267 	void		*driver_data;	/* Driver data, set and get with
1268 					   dev_set_drvdata/dev_get_drvdata */
1269 #ifdef CONFIG_PROVE_LOCKING
1270 	struct mutex		lockdep_mutex;
1271 #endif
1272 	struct mutex		mutex;	/* mutex to synchronize calls to
1273 					 * its driver.
1274 					 */
1275 
1276 	struct dev_links_info	links;
1277 	struct dev_pm_info	power;
1278 	struct dev_pm_domain	*pm_domain;
1279 
1280 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
1281 	struct irq_domain	*msi_domain;
1282 #endif
1283 #ifdef CONFIG_PINCTRL
1284 	struct dev_pin_info	*pins;
1285 #endif
1286 #ifdef CONFIG_GENERIC_MSI_IRQ
1287 	struct list_head	msi_list;
1288 #endif
1289 
1290 	const struct dma_map_ops *dma_ops;
1291 	u64		*dma_mask;	/* dma mask (if dma'able device) */
1292 	u64		coherent_dma_mask;/* Like dma_mask, but for
1293 					     alloc_coherent mappings as
1294 					     not all hardware supports
1295 					     64 bit addresses for consistent
1296 					     allocations such descriptors. */
1297 	u64		bus_dma_mask;	/* upstream dma_mask constraint */
1298 	unsigned long	dma_pfn_offset;
1299 
1300 	struct device_dma_parameters *dma_parms;
1301 
1302 	struct list_head	dma_pools;	/* dma pools (if dma'ble) */
1303 
1304 #ifdef CONFIG_DMA_DECLARE_COHERENT
1305 	struct dma_coherent_mem	*dma_mem; /* internal for coherent mem
1306 					     override */
1307 #endif
1308 #ifdef CONFIG_DMA_CMA
1309 	struct cma *cma_area;		/* contiguous memory area for dma
1310 					   allocations */
1311 #endif
1312 	/* arch specific additions */
1313 	struct dev_archdata	archdata;
1314 
1315 	struct device_node	*of_node; /* associated device tree node */
1316 	struct fwnode_handle	*fwnode; /* firmware device node */
1317 
1318 #ifdef CONFIG_NUMA
1319 	int		numa_node;	/* NUMA node this device is close to */
1320 #endif
1321 	dev_t			devt;	/* dev_t, creates the sysfs "dev" */
1322 	u32			id;	/* device instance */
1323 
1324 	spinlock_t		devres_lock;
1325 	struct list_head	devres_head;
1326 
1327 	struct class		*class;
1328 	const struct attribute_group **groups;	/* optional groups */
1329 
1330 	void	(*release)(struct device *dev);
1331 	struct iommu_group	*iommu_group;
1332 	struct iommu_fwspec	*iommu_fwspec;
1333 	struct iommu_param	*iommu_param;
1334 
1335 	bool			offline_disabled:1;
1336 	bool			offline:1;
1337 	bool			of_node_reused:1;
1338 	bool			state_synced:1;
1339 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
1340     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
1341     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
1342 	bool			dma_coherent:1;
1343 #endif
1344 	ANDROID_KABI_RESERVE(1);
1345 	ANDROID_KABI_RESERVE(2);
1346 	ANDROID_KABI_RESERVE(3);
1347 	ANDROID_KABI_RESERVE(4);
1348 	ANDROID_KABI_RESERVE(5);
1349 	ANDROID_KABI_RESERVE(6);
1350 	ANDROID_KABI_RESERVE(7);
1351 	ANDROID_KABI_RESERVE(8);
1352 };
1353 
1354 /**
1355  * struct device_link - Device link representation.
1356  * @supplier: The device on the supplier end of the link.
1357  * @s_node: Hook to the supplier device's list of links to consumers.
1358  * @consumer: The device on the consumer end of the link.
1359  * @c_node: Hook to the consumer device's list of links to suppliers.
1360  * @link_dev: device used to expose link details in sysfs
1361  * @status: The state of the link (with respect to the presence of drivers).
1362  * @flags: Link flags.
1363  * @rpm_active: Whether or not the consumer device is runtime-PM-active.
1364  * @kref: Count repeated addition of the same link.
1365  * @rcu_head: An RCU head to use for deferred execution of SRCU callbacks.
1366  * @supplier_preactivated: Supplier has been made active before consumer probe.
1367  */
1368 struct device_link {
1369 	struct device *supplier;
1370 	struct list_head s_node;
1371 	struct device *consumer;
1372 	struct list_head c_node;
1373 	struct device link_dev;
1374 	enum device_link_state status;
1375 	u32 flags;
1376 	refcount_t rpm_active;
1377 	struct kref kref;
1378 #ifdef CONFIG_SRCU
1379 	struct rcu_head rcu_head;
1380 #endif
1381 	bool supplier_preactivated; /* Owned by consumer probe. */
1382 
1383 	ANDROID_KABI_RESERVE(1);
1384 	ANDROID_KABI_RESERVE(2);
1385 	ANDROID_KABI_RESERVE(3);
1386 	ANDROID_KABI_RESERVE(4);
1387 };
1388 
kobj_to_dev(struct kobject * kobj)1389 static inline struct device *kobj_to_dev(struct kobject *kobj)
1390 {
1391 	return container_of(kobj, struct device, kobj);
1392 }
1393 
1394 /**
1395  * device_iommu_mapped - Returns true when the device DMA is translated
1396  *			 by an IOMMU
1397  * @dev: Device to perform the check on
1398  */
device_iommu_mapped(struct device * dev)1399 static inline bool device_iommu_mapped(struct device *dev)
1400 {
1401 	return (dev->iommu_group != NULL);
1402 }
1403 
1404 /* Get the wakeup routines, which depend on struct device */
1405 #include <linux/pm_wakeup.h>
1406 
dev_name(const struct device * dev)1407 static inline const char *dev_name(const struct device *dev)
1408 {
1409 	/* Use the init name until the kobject becomes available */
1410 	if (dev->init_name)
1411 		return dev->init_name;
1412 
1413 	return kobject_name(&dev->kobj);
1414 }
1415 
1416 extern __printf(2, 3)
1417 int dev_set_name(struct device *dev, const char *name, ...);
1418 
1419 #ifdef CONFIG_NUMA
dev_to_node(struct device * dev)1420 static inline int dev_to_node(struct device *dev)
1421 {
1422 	return dev->numa_node;
1423 }
set_dev_node(struct device * dev,int node)1424 static inline void set_dev_node(struct device *dev, int node)
1425 {
1426 	dev->numa_node = node;
1427 }
1428 #else
dev_to_node(struct device * dev)1429 static inline int dev_to_node(struct device *dev)
1430 {
1431 	return NUMA_NO_NODE;
1432 }
set_dev_node(struct device * dev,int node)1433 static inline void set_dev_node(struct device *dev, int node)
1434 {
1435 }
1436 #endif
1437 
dev_get_msi_domain(const struct device * dev)1438 static inline struct irq_domain *dev_get_msi_domain(const struct device *dev)
1439 {
1440 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
1441 	return dev->msi_domain;
1442 #else
1443 	return NULL;
1444 #endif
1445 }
1446 
dev_set_msi_domain(struct device * dev,struct irq_domain * d)1447 static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d)
1448 {
1449 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
1450 	dev->msi_domain = d;
1451 #endif
1452 }
1453 
dev_get_drvdata(const struct device * dev)1454 static inline void *dev_get_drvdata(const struct device *dev)
1455 {
1456 	return dev->driver_data;
1457 }
1458 
dev_set_drvdata(struct device * dev,void * data)1459 static inline void dev_set_drvdata(struct device *dev, void *data)
1460 {
1461 	dev->driver_data = data;
1462 }
1463 
dev_to_psd(struct device * dev)1464 static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
1465 {
1466 	return dev ? dev->power.subsys_data : NULL;
1467 }
1468 
dev_get_uevent_suppress(const struct device * dev)1469 static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
1470 {
1471 	return dev->kobj.uevent_suppress;
1472 }
1473 
dev_set_uevent_suppress(struct device * dev,int val)1474 static inline void dev_set_uevent_suppress(struct device *dev, int val)
1475 {
1476 	dev->kobj.uevent_suppress = val;
1477 }
1478 
device_is_registered(struct device * dev)1479 static inline int device_is_registered(struct device *dev)
1480 {
1481 	return dev->kobj.state_in_sysfs;
1482 }
1483 
device_enable_async_suspend(struct device * dev)1484 static inline void device_enable_async_suspend(struct device *dev)
1485 {
1486 	if (!dev->power.is_prepared)
1487 		dev->power.async_suspend = true;
1488 }
1489 
device_disable_async_suspend(struct device * dev)1490 static inline void device_disable_async_suspend(struct device *dev)
1491 {
1492 	if (!dev->power.is_prepared)
1493 		dev->power.async_suspend = false;
1494 }
1495 
device_async_suspend_enabled(struct device * dev)1496 static inline bool device_async_suspend_enabled(struct device *dev)
1497 {
1498 	return !!dev->power.async_suspend;
1499 }
1500 
device_pm_not_required(struct device * dev)1501 static inline bool device_pm_not_required(struct device *dev)
1502 {
1503 	return dev->power.no_pm;
1504 }
1505 
device_set_pm_not_required(struct device * dev)1506 static inline void device_set_pm_not_required(struct device *dev)
1507 {
1508 	dev->power.no_pm = true;
1509 }
1510 
dev_pm_syscore_device(struct device * dev,bool val)1511 static inline void dev_pm_syscore_device(struct device *dev, bool val)
1512 {
1513 #ifdef CONFIG_PM_SLEEP
1514 	dev->power.syscore = val;
1515 #endif
1516 }
1517 
dev_pm_set_driver_flags(struct device * dev,u32 flags)1518 static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags)
1519 {
1520 	dev->power.driver_flags = flags;
1521 }
1522 
dev_pm_test_driver_flags(struct device * dev,u32 flags)1523 static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags)
1524 {
1525 	return !!(dev->power.driver_flags & flags);
1526 }
1527 
device_lock(struct device * dev)1528 static inline void device_lock(struct device *dev)
1529 {
1530 	mutex_lock(&dev->mutex);
1531 }
1532 
device_lock_interruptible(struct device * dev)1533 static inline int device_lock_interruptible(struct device *dev)
1534 {
1535 	return mutex_lock_interruptible(&dev->mutex);
1536 }
1537 
device_trylock(struct device * dev)1538 static inline int device_trylock(struct device *dev)
1539 {
1540 	return mutex_trylock(&dev->mutex);
1541 }
1542 
device_unlock(struct device * dev)1543 static inline void device_unlock(struct device *dev)
1544 {
1545 	mutex_unlock(&dev->mutex);
1546 }
1547 
device_lock_assert(struct device * dev)1548 static inline void device_lock_assert(struct device *dev)
1549 {
1550 	lockdep_assert_held(&dev->mutex);
1551 }
1552 
dev_of_node(struct device * dev)1553 static inline struct device_node *dev_of_node(struct device *dev)
1554 {
1555 	if (!IS_ENABLED(CONFIG_OF) || !dev)
1556 		return NULL;
1557 	return dev->of_node;
1558 }
1559 
dev_has_sync_state(struct device * dev)1560 static inline bool dev_has_sync_state(struct device *dev)
1561 {
1562 	if (!dev)
1563 		return false;
1564 	if (dev->driver && dev->driver->sync_state)
1565 		return true;
1566 	if (dev->bus && dev->bus->sync_state)
1567 		return true;
1568 	return false;
1569 }
1570 
1571 void driver_init(void);
1572 
1573 /*
1574  * High level routines for use by the bus drivers
1575  */
1576 extern int __must_check device_register(struct device *dev);
1577 extern void device_unregister(struct device *dev);
1578 extern void device_initialize(struct device *dev);
1579 extern int __must_check device_add(struct device *dev);
1580 extern void device_del(struct device *dev);
1581 extern int device_for_each_child(struct device *dev, void *data,
1582 		     int (*fn)(struct device *dev, void *data));
1583 extern int device_for_each_child_reverse(struct device *dev, void *data,
1584 		     int (*fn)(struct device *dev, void *data));
1585 extern struct device *device_find_child(struct device *dev, void *data,
1586 				int (*match)(struct device *dev, void *data));
1587 extern struct device *device_find_child_by_name(struct device *parent,
1588 						const char *name);
1589 extern int device_rename(struct device *dev, const char *new_name);
1590 extern int device_move(struct device *dev, struct device *new_parent,
1591 		       enum dpm_order dpm_order);
1592 extern const char *device_get_devnode(struct device *dev,
1593 				      umode_t *mode, kuid_t *uid, kgid_t *gid,
1594 				      const char **tmp);
1595 
device_supports_offline(struct device * dev)1596 static inline bool device_supports_offline(struct device *dev)
1597 {
1598 	return dev->bus && dev->bus->offline && dev->bus->online;
1599 }
1600 
1601 extern void lock_device_hotplug(void);
1602 extern void unlock_device_hotplug(void);
1603 extern int lock_device_hotplug_sysfs(void);
1604 extern int device_offline(struct device *dev);
1605 extern int device_online(struct device *dev);
1606 extern void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1607 extern void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1608 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
1609 void device_set_node(struct device *dev, struct fwnode_handle *fwnode);
1610 
dev_num_vf(struct device * dev)1611 static inline int dev_num_vf(struct device *dev)
1612 {
1613 	if (dev->bus && dev->bus->num_vf)
1614 		return dev->bus->num_vf(dev);
1615 	return 0;
1616 }
1617 
1618 /*
1619  * Root device objects for grouping under /sys/devices
1620  */
1621 extern struct device *__root_device_register(const char *name,
1622 					     struct module *owner);
1623 
1624 /* This is a macro to avoid include problems with THIS_MODULE */
1625 #define root_device_register(name) \
1626 	__root_device_register(name, THIS_MODULE)
1627 
1628 extern void root_device_unregister(struct device *root);
1629 
dev_get_platdata(const struct device * dev)1630 static inline void *dev_get_platdata(const struct device *dev)
1631 {
1632 	return dev->platform_data;
1633 }
1634 
1635 /*
1636  * Manual binding of a device to driver. See drivers/base/bus.c
1637  * for information on use.
1638  */
1639 extern int __must_check device_bind_driver(struct device *dev);
1640 extern void device_release_driver(struct device *dev);
1641 extern int  __must_check device_attach(struct device *dev);
1642 extern int __must_check driver_attach(struct device_driver *drv);
1643 extern void device_initial_probe(struct device *dev);
1644 extern int __must_check device_reprobe(struct device *dev);
1645 
1646 extern bool device_is_bound(struct device *dev);
1647 
1648 /*
1649  * Easy functions for dynamically creating devices on the fly
1650  */
1651 extern __printf(5, 0)
1652 struct device *device_create_vargs(struct class *cls, struct device *parent,
1653 				   dev_t devt, void *drvdata,
1654 				   const char *fmt, va_list vargs);
1655 extern __printf(5, 6)
1656 struct device *device_create(struct class *cls, struct device *parent,
1657 			     dev_t devt, void *drvdata,
1658 			     const char *fmt, ...);
1659 extern __printf(6, 7)
1660 struct device *device_create_with_groups(struct class *cls,
1661 			     struct device *parent, dev_t devt, void *drvdata,
1662 			     const struct attribute_group **groups,
1663 			     const char *fmt, ...);
1664 extern void device_destroy(struct class *cls, dev_t devt);
1665 
1666 extern int __must_check device_add_groups(struct device *dev,
1667 					const struct attribute_group **groups);
1668 extern void device_remove_groups(struct device *dev,
1669 				 const struct attribute_group **groups);
1670 
device_add_group(struct device * dev,const struct attribute_group * grp)1671 static inline int __must_check device_add_group(struct device *dev,
1672 					const struct attribute_group *grp)
1673 {
1674 	const struct attribute_group *groups[] = { grp, NULL };
1675 
1676 	return device_add_groups(dev, groups);
1677 }
1678 
device_remove_group(struct device * dev,const struct attribute_group * grp)1679 static inline void device_remove_group(struct device *dev,
1680 				       const struct attribute_group *grp)
1681 {
1682 	const struct attribute_group *groups[] = { grp, NULL };
1683 
1684 	return device_remove_groups(dev, groups);
1685 }
1686 
1687 extern int __must_check devm_device_add_groups(struct device *dev,
1688 					const struct attribute_group **groups);
1689 extern void devm_device_remove_groups(struct device *dev,
1690 				      const struct attribute_group **groups);
1691 extern int __must_check devm_device_add_group(struct device *dev,
1692 					const struct attribute_group *grp);
1693 extern void devm_device_remove_group(struct device *dev,
1694 				     const struct attribute_group *grp);
1695 
1696 /*
1697  * Platform "fixup" functions - allow the platform to have their say
1698  * about devices and actions that the general device layer doesn't
1699  * know about.
1700  */
1701 /* Notify platform of device discovery */
1702 extern int (*platform_notify)(struct device *dev);
1703 
1704 extern int (*platform_notify_remove)(struct device *dev);
1705 
1706 
1707 /*
1708  * get_device - atomically increment the reference count for the device.
1709  *
1710  */
1711 extern struct device *get_device(struct device *dev);
1712 extern void put_device(struct device *dev);
1713 extern bool kill_device(struct device *dev);
1714 
1715 #ifdef CONFIG_DEVTMPFS
1716 extern int devtmpfs_create_node(struct device *dev);
1717 extern int devtmpfs_delete_node(struct device *dev);
1718 extern int devtmpfs_mount(const char *mntdir);
1719 #else
devtmpfs_create_node(struct device * dev)1720 static inline int devtmpfs_create_node(struct device *dev) { return 0; }
devtmpfs_delete_node(struct device * dev)1721 static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
devtmpfs_mount(const char * mountpoint)1722 static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
1723 #endif
1724 
1725 /* drivers/base/power/shutdown.c */
1726 extern void device_shutdown(void);
1727 
1728 /* debugging and troubleshooting/diagnostic helpers. */
1729 extern const char *dev_driver_string(const struct device *dev);
1730 
1731 /* Device links interface. */
1732 struct device_link *device_link_add(struct device *consumer,
1733 				    struct device *supplier, u32 flags);
1734 void device_link_del(struct device_link *link);
1735 void device_link_remove(void *consumer, struct device *supplier);
1736 void device_links_supplier_sync_state_pause(void);
1737 void device_links_supplier_sync_state_resume(void);
1738 
1739 #ifndef dev_fmt
1740 #define dev_fmt(fmt) fmt
1741 #endif
1742 
1743 #ifdef CONFIG_PRINTK
1744 
1745 __printf(3, 0) __cold
1746 int dev_vprintk_emit(int level, const struct device *dev,
1747 		     const char *fmt, va_list args);
1748 __printf(3, 4) __cold
1749 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
1750 
1751 __printf(3, 4) __cold
1752 void dev_printk(const char *level, const struct device *dev,
1753 		const char *fmt, ...);
1754 __printf(2, 3) __cold
1755 void _dev_emerg(const struct device *dev, const char *fmt, ...);
1756 __printf(2, 3) __cold
1757 void _dev_alert(const struct device *dev, const char *fmt, ...);
1758 __printf(2, 3) __cold
1759 void _dev_crit(const struct device *dev, const char *fmt, ...);
1760 __printf(2, 3) __cold
1761 void _dev_err(const struct device *dev, const char *fmt, ...);
1762 __printf(2, 3) __cold
1763 void _dev_warn(const struct device *dev, const char *fmt, ...);
1764 __printf(2, 3) __cold
1765 void _dev_notice(const struct device *dev, const char *fmt, ...);
1766 __printf(2, 3) __cold
1767 void _dev_info(const struct device *dev, const char *fmt, ...);
1768 
1769 #else
1770 
1771 static inline __printf(3, 0)
dev_vprintk_emit(int level,const struct device * dev,const char * fmt,va_list args)1772 int dev_vprintk_emit(int level, const struct device *dev,
1773 		     const char *fmt, va_list args)
1774 { return 0; }
1775 static inline __printf(3, 4)
dev_printk_emit(int level,const struct device * dev,const char * fmt,...)1776 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
1777 { return 0; }
1778 
__dev_printk(const char * level,const struct device * dev,struct va_format * vaf)1779 static inline void __dev_printk(const char *level, const struct device *dev,
1780 				struct va_format *vaf)
1781 {}
1782 static inline __printf(3, 4)
dev_printk(const char * level,const struct device * dev,const char * fmt,...)1783 void dev_printk(const char *level, const struct device *dev,
1784 		 const char *fmt, ...)
1785 {}
1786 
1787 static inline __printf(2, 3)
_dev_emerg(const struct device * dev,const char * fmt,...)1788 void _dev_emerg(const struct device *dev, const char *fmt, ...)
1789 {}
1790 static inline __printf(2, 3)
_dev_crit(const struct device * dev,const char * fmt,...)1791 void _dev_crit(const struct device *dev, const char *fmt, ...)
1792 {}
1793 static inline __printf(2, 3)
_dev_alert(const struct device * dev,const char * fmt,...)1794 void _dev_alert(const struct device *dev, const char *fmt, ...)
1795 {}
1796 static inline __printf(2, 3)
_dev_err(const struct device * dev,const char * fmt,...)1797 void _dev_err(const struct device *dev, const char *fmt, ...)
1798 {}
1799 static inline __printf(2, 3)
_dev_warn(const struct device * dev,const char * fmt,...)1800 void _dev_warn(const struct device *dev, const char *fmt, ...)
1801 {}
1802 static inline __printf(2, 3)
_dev_notice(const struct device * dev,const char * fmt,...)1803 void _dev_notice(const struct device *dev, const char *fmt, ...)
1804 {}
1805 static inline __printf(2, 3)
_dev_info(const struct device * dev,const char * fmt,...)1806 void _dev_info(const struct device *dev, const char *fmt, ...)
1807 {}
1808 
1809 #endif
1810 
1811 /*
1812  * #defines for all the dev_<level> macros to prefix with whatever
1813  * possible use of #define dev_fmt(fmt) ...
1814  */
1815 
1816 #define dev_emerg(dev, fmt, ...)					\
1817 	_dev_emerg(dev, dev_fmt(fmt), ##__VA_ARGS__)
1818 #define dev_crit(dev, fmt, ...)						\
1819 	_dev_crit(dev, dev_fmt(fmt), ##__VA_ARGS__)
1820 #define dev_alert(dev, fmt, ...)					\
1821 	_dev_alert(dev, dev_fmt(fmt), ##__VA_ARGS__)
1822 #define dev_err(dev, fmt, ...)						\
1823 	_dev_err(dev, dev_fmt(fmt), ##__VA_ARGS__)
1824 #define dev_warn(dev, fmt, ...)						\
1825 	_dev_warn(dev, dev_fmt(fmt), ##__VA_ARGS__)
1826 #define dev_notice(dev, fmt, ...)					\
1827 	_dev_notice(dev, dev_fmt(fmt), ##__VA_ARGS__)
1828 #define dev_info(dev, fmt, ...)						\
1829 	_dev_info(dev, dev_fmt(fmt), ##__VA_ARGS__)
1830 
1831 #if defined(CONFIG_DYNAMIC_DEBUG) || \
1832 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
1833 #define dev_dbg(dev, fmt, ...)						\
1834 	dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
1835 #elif defined(DEBUG)
1836 #define dev_dbg(dev, fmt, ...)						\
1837 	dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
1838 #else
1839 #define dev_dbg(dev, fmt, ...)						\
1840 ({									\
1841 	if (0)								\
1842 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
1843 })
1844 #endif
1845 
1846 #ifdef CONFIG_PRINTK
1847 #define dev_level_once(dev_level, dev, fmt, ...)			\
1848 do {									\
1849 	static bool __print_once __read_mostly;				\
1850 									\
1851 	if (!__print_once) {						\
1852 		__print_once = true;					\
1853 		dev_level(dev, fmt, ##__VA_ARGS__);			\
1854 	}								\
1855 } while (0)
1856 #else
1857 #define dev_level_once(dev_level, dev, fmt, ...)			\
1858 do {									\
1859 	if (0)								\
1860 		dev_level(dev, fmt, ##__VA_ARGS__);			\
1861 } while (0)
1862 #endif
1863 
1864 #define dev_emerg_once(dev, fmt, ...)					\
1865 	dev_level_once(dev_emerg, dev, fmt, ##__VA_ARGS__)
1866 #define dev_alert_once(dev, fmt, ...)					\
1867 	dev_level_once(dev_alert, dev, fmt, ##__VA_ARGS__)
1868 #define dev_crit_once(dev, fmt, ...)					\
1869 	dev_level_once(dev_crit, dev, fmt, ##__VA_ARGS__)
1870 #define dev_err_once(dev, fmt, ...)					\
1871 	dev_level_once(dev_err, dev, fmt, ##__VA_ARGS__)
1872 #define dev_warn_once(dev, fmt, ...)					\
1873 	dev_level_once(dev_warn, dev, fmt, ##__VA_ARGS__)
1874 #define dev_notice_once(dev, fmt, ...)					\
1875 	dev_level_once(dev_notice, dev, fmt, ##__VA_ARGS__)
1876 #define dev_info_once(dev, fmt, ...)					\
1877 	dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__)
1878 #define dev_dbg_once(dev, fmt, ...)					\
1879 	dev_level_once(dev_dbg, dev, fmt, ##__VA_ARGS__)
1880 
1881 #define dev_level_ratelimited(dev_level, dev, fmt, ...)			\
1882 do {									\
1883 	static DEFINE_RATELIMIT_STATE(_rs,				\
1884 				      DEFAULT_RATELIMIT_INTERVAL,	\
1885 				      DEFAULT_RATELIMIT_BURST);		\
1886 	if (__ratelimit(&_rs))						\
1887 		dev_level(dev, fmt, ##__VA_ARGS__);			\
1888 } while (0)
1889 
1890 #define dev_emerg_ratelimited(dev, fmt, ...)				\
1891 	dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
1892 #define dev_alert_ratelimited(dev, fmt, ...)				\
1893 	dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
1894 #define dev_crit_ratelimited(dev, fmt, ...)				\
1895 	dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
1896 #define dev_err_ratelimited(dev, fmt, ...)				\
1897 	dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
1898 #define dev_warn_ratelimited(dev, fmt, ...)				\
1899 	dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
1900 #define dev_notice_ratelimited(dev, fmt, ...)				\
1901 	dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
1902 #define dev_info_ratelimited(dev, fmt, ...)				\
1903 	dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
1904 #if defined(CONFIG_DYNAMIC_DEBUG) || \
1905 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
1906 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
1907 #define dev_dbg_ratelimited(dev, fmt, ...)				\
1908 do {									\
1909 	static DEFINE_RATELIMIT_STATE(_rs,				\
1910 				      DEFAULT_RATELIMIT_INTERVAL,	\
1911 				      DEFAULT_RATELIMIT_BURST);		\
1912 	DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);			\
1913 	if (DYNAMIC_DEBUG_BRANCH(descriptor) &&				\
1914 	    __ratelimit(&_rs))						\
1915 		__dynamic_dev_dbg(&descriptor, dev, dev_fmt(fmt),	\
1916 				  ##__VA_ARGS__);			\
1917 } while (0)
1918 #elif defined(DEBUG)
1919 #define dev_dbg_ratelimited(dev, fmt, ...)				\
1920 do {									\
1921 	static DEFINE_RATELIMIT_STATE(_rs,				\
1922 				      DEFAULT_RATELIMIT_INTERVAL,	\
1923 				      DEFAULT_RATELIMIT_BURST);		\
1924 	if (__ratelimit(&_rs))						\
1925 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
1926 } while (0)
1927 #else
1928 #define dev_dbg_ratelimited(dev, fmt, ...)				\
1929 do {									\
1930 	if (0)								\
1931 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
1932 } while (0)
1933 #endif
1934 
1935 #ifdef VERBOSE_DEBUG
1936 #define dev_vdbg	dev_dbg
1937 #else
1938 #define dev_vdbg(dev, fmt, ...)						\
1939 ({									\
1940 	if (0)								\
1941 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
1942 })
1943 #endif
1944 
1945 /*
1946  * dev_WARN*() acts like dev_printk(), but with the key difference of
1947  * using WARN/WARN_ONCE to include file/line information and a backtrace.
1948  */
1949 #define dev_WARN(dev, format, arg...) \
1950 	WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg);
1951 
1952 #define dev_WARN_ONCE(dev, condition, format, arg...) \
1953 	WARN_ONCE(condition, "%s %s: " format, \
1954 			dev_driver_string(dev), dev_name(dev), ## arg)
1955 
1956 extern __printf(3, 4)
1957 int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
1958 
1959 /* Create alias, so I can be autoloaded. */
1960 #define MODULE_ALIAS_CHARDEV(major,minor) \
1961 	MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1962 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1963 	MODULE_ALIAS("char-major-" __stringify(major) "-*")
1964 
1965 #ifdef CONFIG_SYSFS_DEPRECATED
1966 extern long sysfs_deprecated;
1967 #else
1968 #define sysfs_deprecated 0
1969 #endif
1970 
1971 /**
1972  * module_driver() - Helper macro for drivers that don't do anything
1973  * special in module init/exit. This eliminates a lot of boilerplate.
1974  * Each module may only use this macro once, and calling it replaces
1975  * module_init() and module_exit().
1976  *
1977  * @__driver: driver name
1978  * @__register: register function for this driver type
1979  * @__unregister: unregister function for this driver type
1980  * @...: Additional arguments to be passed to __register and __unregister.
1981  *
1982  * Use this macro to construct bus specific macros for registering
1983  * drivers, and do not use it on its own.
1984  */
1985 #define module_driver(__driver, __register, __unregister, ...) \
1986 static int __init __driver##_init(void) \
1987 { \
1988 	return __register(&(__driver) , ##__VA_ARGS__); \
1989 } \
1990 module_init(__driver##_init); \
1991 static void __exit __driver##_exit(void) \
1992 { \
1993 	__unregister(&(__driver) , ##__VA_ARGS__); \
1994 } \
1995 module_exit(__driver##_exit);
1996 
1997 /**
1998  * builtin_driver() - Helper macro for drivers that don't do anything
1999  * special in init and have no exit. This eliminates some boilerplate.
2000  * Each driver may only use this macro once, and calling it replaces
2001  * device_initcall (or in some cases, the legacy __initcall).  This is
2002  * meant to be a direct parallel of module_driver() above but without
2003  * the __exit stuff that is not used for builtin cases.
2004  *
2005  * @__driver: driver name
2006  * @__register: register function for this driver type
2007  * @...: Additional arguments to be passed to __register
2008  *
2009  * Use this macro to construct bus specific macros for registering
2010  * drivers, and do not use it on its own.
2011  */
2012 #define builtin_driver(__driver, __register, ...) \
2013 static int __init __driver##_init(void) \
2014 { \
2015 	return __register(&(__driver) , ##__VA_ARGS__); \
2016 } \
2017 device_initcall(__driver##_init);
2018 
2019 #endif /* _DEVICE_H_ */
2020