device.h 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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. * This file is released under the GPLv2
  10. *
  11. * See Documentation/driver-model/ for more information.
  12. */
  13. #ifndef _DEVICE_H_
  14. #define _DEVICE_H_
  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/pinctrl/devinfo.h>
  24. #include <linux/pm.h>
  25. #include <linux/atomic.h>
  26. #include <linux/ratelimit.h>
  27. #include <linux/uidgid.h>
  28. #include <linux/gfp.h>
  29. #include <asm/device.h>
  30. struct device;
  31. struct device_private;
  32. struct device_driver;
  33. struct driver_private;
  34. struct module;
  35. struct class;
  36. struct subsys_private;
  37. struct bus_type;
  38. struct device_node;
  39. struct fwnode_handle;
  40. struct iommu_ops;
  41. struct iommu_group;
  42. struct iommu_fwspec;
  43. struct bus_attribute {
  44. struct attribute attr;
  45. ssize_t (*show)(struct bus_type *bus, char *buf);
  46. ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
  47. };
  48. #define BUS_ATTR(_name, _mode, _show, _store) \
  49. struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
  50. #define BUS_ATTR_RW(_name) \
  51. struct bus_attribute bus_attr_##_name = __ATTR_RW(_name)
  52. #define BUS_ATTR_RO(_name) \
  53. struct bus_attribute bus_attr_##_name = __ATTR_RO(_name)
  54. extern int __must_check bus_create_file(struct bus_type *,
  55. struct bus_attribute *);
  56. extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
  57. /**
  58. * struct bus_type - The bus type of the device
  59. *
  60. * @name: The name of the bus.
  61. * @dev_name: Used for subsystems to enumerate devices like ("foo%u", dev->id).
  62. * @dev_root: Default device to use as the parent.
  63. * @bus_groups: Default attributes of the bus.
  64. * @dev_groups: Default attributes of the devices on the bus.
  65. * @drv_groups: Default attributes of the device drivers on the bus.
  66. * @match: Called, perhaps multiple times, whenever a new device or driver
  67. * is added for this bus. It should return a positive value if the
  68. * given device can be handled by the given driver and zero
  69. * otherwise. It may also return error code if determining that
  70. * the driver supports the device is not possible. In case of
  71. * -EPROBE_DEFER it will queue the device for deferred probing.
  72. * @uevent: Called when a device is added, removed, or a few other things
  73. * that generate uevents to add the environment variables.
  74. * @probe: Called when a new device or driver add to this bus, and callback
  75. * the specific driver's probe to initial the matched device.
  76. * @remove: Called when a device removed from this bus.
  77. * @shutdown: Called at shut-down time to quiesce the device.
  78. *
  79. * @online: Called to put the device back online (after offlining it).
  80. * @offline: Called to put the device offline for hot-removal. May fail.
  81. *
  82. * @suspend: Called when a device on this bus wants to go to sleep mode.
  83. * @resume: Called to bring a device on this bus out of sleep mode.
  84. * @num_vf: Called to find out how many virtual functions a device on this
  85. * bus supports.
  86. * @pm: Power management operations of this bus, callback the specific
  87. * device driver's pm-ops.
  88. * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU
  89. * driver implementations to a bus and allow the driver to do
  90. * bus-specific setup
  91. * @p: The private data of the driver core, only the driver core can
  92. * touch this.
  93. * @lock_key: Lock class key for use by the lock validator
  94. * @force_dma: Assume devices on this bus should be set up by dma_configure()
  95. * even if DMA capability is not explicitly described by firmware.
  96. *
  97. * A bus is a channel between the processor and one or more devices. For the
  98. * purposes of the device model, all devices are connected via a bus, even if
  99. * it is an internal, virtual, "platform" bus. Buses can plug into each other.
  100. * A USB controller is usually a PCI device, for example. The device model
  101. * represents the actual connections between buses and the devices they control.
  102. * A bus is represented by the bus_type structure. It contains the name, the
  103. * default attributes, the bus' methods, PM operations, and the driver core's
  104. * private data.
  105. */
  106. struct bus_type {
  107. const char *name;
  108. const char *dev_name;
  109. struct device *dev_root;
  110. const struct attribute_group **bus_groups;
  111. const struct attribute_group **dev_groups;
  112. const struct attribute_group **drv_groups;
  113. int (*match)(struct device *dev, struct device_driver *drv);
  114. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  115. int (*probe)(struct device *dev);
  116. int (*remove)(struct device *dev);
  117. void (*shutdown)(struct device *dev);
  118. int (*online)(struct device *dev);
  119. int (*offline)(struct device *dev);
  120. int (*suspend)(struct device *dev, pm_message_t state);
  121. int (*resume)(struct device *dev);
  122. int (*num_vf)(struct device *dev);
  123. const struct dev_pm_ops *pm;
  124. const struct iommu_ops *iommu_ops;
  125. struct subsys_private *p;
  126. struct lock_class_key lock_key;
  127. bool force_dma;
  128. };
  129. extern int __must_check bus_register(struct bus_type *bus);
  130. extern void bus_unregister(struct bus_type *bus);
  131. extern int __must_check bus_rescan_devices(struct bus_type *bus);
  132. /* iterator helpers for buses */
  133. struct subsys_dev_iter {
  134. struct klist_iter ki;
  135. const struct device_type *type;
  136. };
  137. void subsys_dev_iter_init(struct subsys_dev_iter *iter,
  138. struct bus_type *subsys,
  139. struct device *start,
  140. const struct device_type *type);
  141. struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter);
  142. void subsys_dev_iter_exit(struct subsys_dev_iter *iter);
  143. int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
  144. int (*fn)(struct device *dev, void *data));
  145. struct device *bus_find_device(struct bus_type *bus, struct device *start,
  146. void *data,
  147. int (*match)(struct device *dev, void *data));
  148. struct device *bus_find_device_by_name(struct bus_type *bus,
  149. struct device *start,
  150. const char *name);
  151. struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
  152. struct device *hint);
  153. int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
  154. void *data, int (*fn)(struct device_driver *, void *));
  155. void bus_sort_breadthfirst(struct bus_type *bus,
  156. int (*compare)(const struct device *a,
  157. const struct device *b));
  158. /*
  159. * Bus notifiers: Get notified of addition/removal of devices
  160. * and binding/unbinding of drivers to devices.
  161. * In the long run, it should be a replacement for the platform
  162. * notify hooks.
  163. */
  164. struct notifier_block;
  165. extern int bus_register_notifier(struct bus_type *bus,
  166. struct notifier_block *nb);
  167. extern int bus_unregister_notifier(struct bus_type *bus,
  168. struct notifier_block *nb);
  169. /* All 4 notifers below get called with the target struct device *
  170. * as an argument. Note that those functions are likely to be called
  171. * with the device lock held in the core, so be careful.
  172. */
  173. #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
  174. #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device to be removed */
  175. #define BUS_NOTIFY_REMOVED_DEVICE 0x00000003 /* device removed */
  176. #define BUS_NOTIFY_BIND_DRIVER 0x00000004 /* driver about to be
  177. bound */
  178. #define BUS_NOTIFY_BOUND_DRIVER 0x00000005 /* driver bound to device */
  179. #define BUS_NOTIFY_UNBIND_DRIVER 0x00000006 /* driver about to be
  180. unbound */
  181. #define BUS_NOTIFY_UNBOUND_DRIVER 0x00000007 /* driver is unbound
  182. from the device */
  183. #define BUS_NOTIFY_DRIVER_NOT_BOUND 0x00000008 /* driver fails to be bound */
  184. extern struct kset *bus_get_kset(struct bus_type *bus);
  185. extern struct klist *bus_get_device_klist(struct bus_type *bus);
  186. /**
  187. * enum probe_type - device driver probe type to try
  188. * Device drivers may opt in for special handling of their
  189. * respective probe routines. This tells the core what to
  190. * expect and prefer.
  191. *
  192. * @PROBE_DEFAULT_STRATEGY: Used by drivers that work equally well
  193. * whether probed synchronously or asynchronously.
  194. * @PROBE_PREFER_ASYNCHRONOUS: Drivers for "slow" devices which
  195. * probing order is not essential for booting the system may
  196. * opt into executing their probes asynchronously.
  197. * @PROBE_FORCE_SYNCHRONOUS: Use this to annotate drivers that need
  198. * their probe routines to run synchronously with driver and
  199. * device registration (with the exception of -EPROBE_DEFER
  200. * handling - re-probing always ends up being done asynchronously).
  201. *
  202. * Note that the end goal is to switch the kernel to use asynchronous
  203. * probing by default, so annotating drivers with
  204. * %PROBE_PREFER_ASYNCHRONOUS is a temporary measure that allows us
  205. * to speed up boot process while we are validating the rest of the
  206. * drivers.
  207. */
  208. enum probe_type {
  209. PROBE_DEFAULT_STRATEGY,
  210. PROBE_PREFER_ASYNCHRONOUS,
  211. PROBE_FORCE_SYNCHRONOUS,
  212. };
  213. /**
  214. * struct device_driver - The basic device driver structure
  215. * @name: Name of the device driver.
  216. * @bus: The bus which the device of this driver belongs to.
  217. * @owner: The module owner.
  218. * @mod_name: Used for built-in modules.
  219. * @suppress_bind_attrs: Disables bind/unbind via sysfs.
  220. * @probe_type: Type of the probe (synchronous or asynchronous) to use.
  221. * @of_match_table: The open firmware table.
  222. * @acpi_match_table: The ACPI match table.
  223. * @probe: Called to query the existence of a specific device,
  224. * whether this driver can work with it, and bind the driver
  225. * to a specific device.
  226. * @remove: Called when the device is removed from the system to
  227. * unbind a device from this driver.
  228. * @shutdown: Called at shut-down time to quiesce the device.
  229. * @suspend: Called to put the device to sleep mode. Usually to a
  230. * low power state.
  231. * @resume: Called to bring a device from sleep mode.
  232. * @groups: Default attributes that get created by the driver core
  233. * automatically.
  234. * @pm: Power management operations of the device which matched
  235. * this driver.
  236. * @p: Driver core's private data, no one other than the driver
  237. * core can touch this.
  238. *
  239. * The device driver-model tracks all of the drivers known to the system.
  240. * The main reason for this tracking is to enable the driver core to match
  241. * up drivers with new devices. Once drivers are known objects within the
  242. * system, however, a number of other things become possible. Device drivers
  243. * can export information and configuration variables that are independent
  244. * of any specific device.
  245. */
  246. struct device_driver {
  247. const char *name;
  248. struct bus_type *bus;
  249. struct module *owner;
  250. const char *mod_name; /* used for built-in modules */
  251. bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
  252. enum probe_type probe_type;
  253. const struct of_device_id *of_match_table;
  254. const struct acpi_device_id *acpi_match_table;
  255. int (*probe) (struct device *dev);
  256. int (*remove) (struct device *dev);
  257. void (*shutdown) (struct device *dev);
  258. int (*suspend) (struct device *dev, pm_message_t state);
  259. int (*resume) (struct device *dev);
  260. const struct attribute_group **groups;
  261. const struct dev_pm_ops *pm;
  262. struct driver_private *p;
  263. };
  264. extern int __must_check driver_register(struct device_driver *drv);
  265. extern void driver_unregister(struct device_driver *drv);
  266. extern struct device_driver *driver_find(const char *name,
  267. struct bus_type *bus);
  268. extern int driver_probe_done(void);
  269. extern void wait_for_device_probe(void);
  270. /* sysfs interface for exporting driver attributes */
  271. struct driver_attribute {
  272. struct attribute attr;
  273. ssize_t (*show)(struct device_driver *driver, char *buf);
  274. ssize_t (*store)(struct device_driver *driver, const char *buf,
  275. size_t count);
  276. };
  277. #define DRIVER_ATTR_RW(_name) \
  278. struct driver_attribute driver_attr_##_name = __ATTR_RW(_name)
  279. #define DRIVER_ATTR_RO(_name) \
  280. struct driver_attribute driver_attr_##_name = __ATTR_RO(_name)
  281. #define DRIVER_ATTR_WO(_name) \
  282. struct driver_attribute driver_attr_##_name = __ATTR_WO(_name)
  283. extern int __must_check driver_create_file(struct device_driver *driver,
  284. const struct driver_attribute *attr);
  285. extern void driver_remove_file(struct device_driver *driver,
  286. const struct driver_attribute *attr);
  287. extern int __must_check driver_for_each_device(struct device_driver *drv,
  288. struct device *start,
  289. void *data,
  290. int (*fn)(struct device *dev,
  291. void *));
  292. struct device *driver_find_device(struct device_driver *drv,
  293. struct device *start, void *data,
  294. int (*match)(struct device *dev, void *data));
  295. /**
  296. * struct subsys_interface - interfaces to device functions
  297. * @name: name of the device function
  298. * @subsys: subsytem of the devices to attach to
  299. * @node: the list of functions registered at the subsystem
  300. * @add_dev: device hookup to device function handler
  301. * @remove_dev: device hookup to device function handler
  302. *
  303. * Simple interfaces attached to a subsystem. Multiple interfaces can
  304. * attach to a subsystem and its devices. Unlike drivers, they do not
  305. * exclusively claim or control devices. Interfaces usually represent
  306. * a specific functionality of a subsystem/class of devices.
  307. */
  308. struct subsys_interface {
  309. const char *name;
  310. struct bus_type *subsys;
  311. struct list_head node;
  312. int (*add_dev)(struct device *dev, struct subsys_interface *sif);
  313. void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
  314. };
  315. int subsys_interface_register(struct subsys_interface *sif);
  316. void subsys_interface_unregister(struct subsys_interface *sif);
  317. int subsys_system_register(struct bus_type *subsys,
  318. const struct attribute_group **groups);
  319. int subsys_virtual_register(struct bus_type *subsys,
  320. const struct attribute_group **groups);
  321. /**
  322. * struct class - device classes
  323. * @name: Name of the class.
  324. * @owner: The module owner.
  325. * @class_groups: Default attributes of this class.
  326. * @dev_groups: Default attributes of the devices that belong to the class.
  327. * @dev_kobj: The kobject that represents this class and links it into the hierarchy.
  328. * @dev_uevent: Called when a device is added, removed from this class, or a
  329. * few other things that generate uevents to add the environment
  330. * variables.
  331. * @devnode: Callback to provide the devtmpfs.
  332. * @class_release: Called to release this class.
  333. * @dev_release: Called to release the device.
  334. * @shutdown_pre: Called at shut-down time before driver shutdown.
  335. * @ns_type: Callbacks so sysfs can detemine namespaces.
  336. * @namespace: Namespace of the device belongs to this class.
  337. * @pm: The default device power management operations of this class.
  338. * @p: The private data of the driver core, no one other than the
  339. * driver core can touch this.
  340. *
  341. * A class is a higher-level view of a device that abstracts out low-level
  342. * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
  343. * at the class level, they are all simply disks. Classes allow user space
  344. * to work with devices based on what they do, rather than how they are
  345. * connected or how they work.
  346. */
  347. struct class {
  348. const char *name;
  349. struct module *owner;
  350. const struct attribute_group **class_groups;
  351. const struct attribute_group **dev_groups;
  352. struct kobject *dev_kobj;
  353. int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
  354. char *(*devnode)(struct device *dev, umode_t *mode);
  355. void (*class_release)(struct class *class);
  356. void (*dev_release)(struct device *dev);
  357. int (*shutdown_pre)(struct device *dev);
  358. const struct kobj_ns_type_operations *ns_type;
  359. const void *(*namespace)(struct device *dev);
  360. const struct dev_pm_ops *pm;
  361. struct subsys_private *p;
  362. };
  363. struct class_dev_iter {
  364. struct klist_iter ki;
  365. const struct device_type *type;
  366. };
  367. extern struct kobject *sysfs_dev_block_kobj;
  368. extern struct kobject *sysfs_dev_char_kobj;
  369. extern int __must_check __class_register(struct class *class,
  370. struct lock_class_key *key);
  371. extern void class_unregister(struct class *class);
  372. /* This is a #define to keep the compiler from merging different
  373. * instances of the __key variable */
  374. #define class_register(class) \
  375. ({ \
  376. static struct lock_class_key __key; \
  377. __class_register(class, &__key); \
  378. })
  379. struct class_compat;
  380. struct class_compat *class_compat_register(const char *name);
  381. void class_compat_unregister(struct class_compat *cls);
  382. int class_compat_create_link(struct class_compat *cls, struct device *dev,
  383. struct device *device_link);
  384. void class_compat_remove_link(struct class_compat *cls, struct device *dev,
  385. struct device *device_link);
  386. extern void class_dev_iter_init(struct class_dev_iter *iter,
  387. struct class *class,
  388. struct device *start,
  389. const struct device_type *type);
  390. extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
  391. extern void class_dev_iter_exit(struct class_dev_iter *iter);
  392. extern int class_for_each_device(struct class *class, struct device *start,
  393. void *data,
  394. int (*fn)(struct device *dev, void *data));
  395. extern struct device *class_find_device(struct class *class,
  396. struct device *start, const void *data,
  397. int (*match)(struct device *, const void *));
  398. struct class_attribute {
  399. struct attribute attr;
  400. ssize_t (*show)(struct class *class, struct class_attribute *attr,
  401. char *buf);
  402. ssize_t (*store)(struct class *class, struct class_attribute *attr,
  403. const char *buf, size_t count);
  404. };
  405. #define CLASS_ATTR_RW(_name) \
  406. struct class_attribute class_attr_##_name = __ATTR_RW(_name)
  407. #define CLASS_ATTR_RO(_name) \
  408. struct class_attribute class_attr_##_name = __ATTR_RO(_name)
  409. #define CLASS_ATTR_WO(_name) \
  410. struct class_attribute class_attr_##_name = __ATTR_WO(_name)
  411. extern int __must_check class_create_file_ns(struct class *class,
  412. const struct class_attribute *attr,
  413. const void *ns);
  414. extern void class_remove_file_ns(struct class *class,
  415. const struct class_attribute *attr,
  416. const void *ns);
  417. static inline int __must_check class_create_file(struct class *class,
  418. const struct class_attribute *attr)
  419. {
  420. return class_create_file_ns(class, attr, NULL);
  421. }
  422. static inline void class_remove_file(struct class *class,
  423. const struct class_attribute *attr)
  424. {
  425. return class_remove_file_ns(class, attr, NULL);
  426. }
  427. /* Simple class attribute that is just a static string */
  428. struct class_attribute_string {
  429. struct class_attribute attr;
  430. char *str;
  431. };
  432. /* Currently read-only only */
  433. #define _CLASS_ATTR_STRING(_name, _mode, _str) \
  434. { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
  435. #define CLASS_ATTR_STRING(_name, _mode, _str) \
  436. struct class_attribute_string class_attr_##_name = \
  437. _CLASS_ATTR_STRING(_name, _mode, _str)
  438. extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
  439. char *buf);
  440. struct class_interface {
  441. struct list_head node;
  442. struct class *class;
  443. int (*add_dev) (struct device *, struct class_interface *);
  444. void (*remove_dev) (struct device *, struct class_interface *);
  445. };
  446. extern int __must_check class_interface_register(struct class_interface *);
  447. extern void class_interface_unregister(struct class_interface *);
  448. extern struct class * __must_check __class_create(struct module *owner,
  449. const char *name,
  450. struct lock_class_key *key);
  451. extern void class_destroy(struct class *cls);
  452. /* This is a #define to keep the compiler from merging different
  453. * instances of the __key variable */
  454. #define class_create(owner, name) \
  455. ({ \
  456. static struct lock_class_key __key; \
  457. __class_create(owner, name, &__key); \
  458. })
  459. /*
  460. * The type of device, "struct device" is embedded in. A class
  461. * or bus can contain devices of different types
  462. * like "partitions" and "disks", "mouse" and "event".
  463. * This identifies the device type and carries type-specific
  464. * information, equivalent to the kobj_type of a kobject.
  465. * If "name" is specified, the uevent will contain it in
  466. * the DEVTYPE variable.
  467. */
  468. struct device_type {
  469. const char *name;
  470. const struct attribute_group **groups;
  471. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  472. char *(*devnode)(struct device *dev, umode_t *mode,
  473. kuid_t *uid, kgid_t *gid);
  474. void (*release)(struct device *dev);
  475. const struct dev_pm_ops *pm;
  476. };
  477. /* interface for exporting device attributes */
  478. struct device_attribute {
  479. struct attribute attr;
  480. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  481. char *buf);
  482. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  483. const char *buf, size_t count);
  484. };
  485. struct dev_ext_attribute {
  486. struct device_attribute attr;
  487. void *var;
  488. };
  489. ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
  490. char *buf);
  491. ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
  492. const char *buf, size_t count);
  493. ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
  494. char *buf);
  495. ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
  496. const char *buf, size_t count);
  497. ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
  498. char *buf);
  499. ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
  500. const char *buf, size_t count);
  501. #define DEVICE_ATTR(_name, _mode, _show, _store) \
  502. struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
  503. #define DEVICE_ATTR_RW(_name) \
  504. struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
  505. #define DEVICE_ATTR_RO(_name) \
  506. struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
  507. #define DEVICE_ATTR_WO(_name) \
  508. struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
  509. #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
  510. struct dev_ext_attribute dev_attr_##_name = \
  511. { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
  512. #define DEVICE_INT_ATTR(_name, _mode, _var) \
  513. struct dev_ext_attribute dev_attr_##_name = \
  514. { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
  515. #define DEVICE_BOOL_ATTR(_name, _mode, _var) \
  516. struct dev_ext_attribute dev_attr_##_name = \
  517. { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
  518. #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
  519. struct device_attribute dev_attr_##_name = \
  520. __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
  521. extern int device_create_file(struct device *device,
  522. const struct device_attribute *entry);
  523. extern void device_remove_file(struct device *dev,
  524. const struct device_attribute *attr);
  525. extern bool device_remove_file_self(struct device *dev,
  526. const struct device_attribute *attr);
  527. extern int __must_check device_create_bin_file(struct device *dev,
  528. const struct bin_attribute *attr);
  529. extern void device_remove_bin_file(struct device *dev,
  530. const struct bin_attribute *attr);
  531. /* device resource management */
  532. typedef void (*dr_release_t)(struct device *dev, void *res);
  533. typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
  534. #ifdef CONFIG_DEBUG_DEVRES
  535. extern void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
  536. int nid, const char *name) __malloc;
  537. #define devres_alloc(release, size, gfp) \
  538. __devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
  539. #define devres_alloc_node(release, size, gfp, nid) \
  540. __devres_alloc_node(release, size, gfp, nid, #release)
  541. #else
  542. extern void *devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
  543. int nid) __malloc;
  544. static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
  545. {
  546. return devres_alloc_node(release, size, gfp, NUMA_NO_NODE);
  547. }
  548. #endif
  549. extern void devres_for_each_res(struct device *dev, dr_release_t release,
  550. dr_match_t match, void *match_data,
  551. void (*fn)(struct device *, void *, void *),
  552. void *data);
  553. extern void devres_free(void *res);
  554. extern void devres_add(struct device *dev, void *res);
  555. extern void *devres_find(struct device *dev, dr_release_t release,
  556. dr_match_t match, void *match_data);
  557. extern void *devres_get(struct device *dev, void *new_res,
  558. dr_match_t match, void *match_data);
  559. extern void *devres_remove(struct device *dev, dr_release_t release,
  560. dr_match_t match, void *match_data);
  561. extern int devres_destroy(struct device *dev, dr_release_t release,
  562. dr_match_t match, void *match_data);
  563. extern int devres_release(struct device *dev, dr_release_t release,
  564. dr_match_t match, void *match_data);
  565. /* devres group */
  566. extern void * __must_check devres_open_group(struct device *dev, void *id,
  567. gfp_t gfp);
  568. extern void devres_close_group(struct device *dev, void *id);
  569. extern void devres_remove_group(struct device *dev, void *id);
  570. extern int devres_release_group(struct device *dev, void *id);
  571. /* managed devm_k.alloc/kfree for device drivers */
  572. extern void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc;
  573. extern __printf(3, 0)
  574. char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
  575. va_list ap) __malloc;
  576. extern __printf(3, 4)
  577. char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...) __malloc;
  578. static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
  579. {
  580. return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
  581. }
  582. static inline void *devm_kmalloc_array(struct device *dev,
  583. size_t n, size_t size, gfp_t flags)
  584. {
  585. if (size != 0 && n > SIZE_MAX / size)
  586. return NULL;
  587. return devm_kmalloc(dev, n * size, flags);
  588. }
  589. static inline void *devm_kcalloc(struct device *dev,
  590. size_t n, size_t size, gfp_t flags)
  591. {
  592. return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
  593. }
  594. extern void devm_kfree(struct device *dev, void *p);
  595. extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
  596. extern void *devm_kmemdup(struct device *dev, const void *src, size_t len,
  597. gfp_t gfp);
  598. extern unsigned long devm_get_free_pages(struct device *dev,
  599. gfp_t gfp_mask, unsigned int order);
  600. extern void devm_free_pages(struct device *dev, unsigned long addr);
  601. void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res);
  602. /* allows to add/remove a custom action to devres stack */
  603. int devm_add_action(struct device *dev, void (*action)(void *), void *data);
  604. void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
  605. static inline int devm_add_action_or_reset(struct device *dev,
  606. void (*action)(void *), void *data)
  607. {
  608. int ret;
  609. ret = devm_add_action(dev, action, data);
  610. if (ret)
  611. action(data);
  612. return ret;
  613. }
  614. /**
  615. * devm_alloc_percpu - Resource-managed alloc_percpu
  616. * @dev: Device to allocate per-cpu memory for
  617. * @type: Type to allocate per-cpu memory for
  618. *
  619. * Managed alloc_percpu. Per-cpu memory allocated with this function is
  620. * automatically freed on driver detach.
  621. *
  622. * RETURNS:
  623. * Pointer to allocated memory on success, NULL on failure.
  624. */
  625. #define devm_alloc_percpu(dev, type) \
  626. ((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
  627. __alignof__(type)))
  628. void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
  629. size_t align);
  630. void devm_free_percpu(struct device *dev, void __percpu *pdata);
  631. struct device_dma_parameters {
  632. /*
  633. * a low level driver may set these to teach IOMMU code about
  634. * sg limitations.
  635. */
  636. unsigned int max_segment_size;
  637. unsigned long segment_boundary_mask;
  638. };
  639. /**
  640. * enum device_link_state - Device link states.
  641. * @DL_STATE_NONE: The presence of the drivers is not being tracked.
  642. * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
  643. * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.
  644. * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present).
  645. * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present.
  646. * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding.
  647. */
  648. enum device_link_state {
  649. DL_STATE_NONE = -1,
  650. DL_STATE_DORMANT = 0,
  651. DL_STATE_AVAILABLE,
  652. DL_STATE_CONSUMER_PROBE,
  653. DL_STATE_ACTIVE,
  654. DL_STATE_SUPPLIER_UNBIND,
  655. };
  656. /*
  657. * Device link flags.
  658. *
  659. * STATELESS: The core won't track the presence of supplier/consumer drivers.
  660. * AUTOREMOVE: Remove this link automatically on consumer driver unbind.
  661. * PM_RUNTIME: If set, the runtime PM framework will use this link.
  662. * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation.
  663. */
  664. #define DL_FLAG_STATELESS BIT(0)
  665. #define DL_FLAG_AUTOREMOVE BIT(1)
  666. #define DL_FLAG_PM_RUNTIME BIT(2)
  667. #define DL_FLAG_RPM_ACTIVE BIT(3)
  668. /**
  669. * struct device_link - Device link representation.
  670. * @supplier: The device on the supplier end of the link.
  671. * @s_node: Hook to the supplier device's list of links to consumers.
  672. * @consumer: The device on the consumer end of the link.
  673. * @c_node: Hook to the consumer device's list of links to suppliers.
  674. * @status: The state of the link (with respect to the presence of drivers).
  675. * @flags: Link flags.
  676. * @rpm_active: Whether or not the consumer device is runtime-PM-active.
  677. * @rcu_head: An RCU head to use for deferred execution of SRCU callbacks.
  678. */
  679. struct device_link {
  680. struct device *supplier;
  681. struct list_head s_node;
  682. struct device *consumer;
  683. struct list_head c_node;
  684. enum device_link_state status;
  685. u32 flags;
  686. bool rpm_active;
  687. #ifdef CONFIG_SRCU
  688. struct rcu_head rcu_head;
  689. #endif
  690. };
  691. /**
  692. * enum dl_dev_state - Device driver presence tracking information.
  693. * @DL_DEV_NO_DRIVER: There is no driver attached to the device.
  694. * @DL_DEV_PROBING: A driver is probing.
  695. * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device.
  696. * @DL_DEV_UNBINDING: The driver is unbinding from the device.
  697. */
  698. enum dl_dev_state {
  699. DL_DEV_NO_DRIVER = 0,
  700. DL_DEV_PROBING,
  701. DL_DEV_DRIVER_BOUND,
  702. DL_DEV_UNBINDING,
  703. };
  704. /**
  705. * struct dev_links_info - Device data related to device links.
  706. * @suppliers: List of links to supplier devices.
  707. * @consumers: List of links to consumer devices.
  708. * @status: Driver status information.
  709. */
  710. struct dev_links_info {
  711. struct list_head suppliers;
  712. struct list_head consumers;
  713. enum dl_dev_state status;
  714. };
  715. /**
  716. * struct device - The basic device structure
  717. * @parent: The device's "parent" device, the device to which it is attached.
  718. * In most cases, a parent device is some sort of bus or host
  719. * controller. If parent is NULL, the device, is a top-level device,
  720. * which is not usually what you want.
  721. * @p: Holds the private data of the driver core portions of the device.
  722. * See the comment of the struct device_private for detail.
  723. * @kobj: A top-level, abstract class from which other classes are derived.
  724. * @init_name: Initial name of the device.
  725. * @type: The type of device.
  726. * This identifies the device type and carries type-specific
  727. * information.
  728. * @mutex: Mutex to synchronize calls to its driver.
  729. * @bus: Type of bus device is on.
  730. * @driver: Which driver has allocated this
  731. * @platform_data: Platform data specific to the device.
  732. * Example: For devices on custom boards, as typical of embedded
  733. * and SOC based hardware, Linux often uses platform_data to point
  734. * to board-specific structures describing devices and how they
  735. * are wired. That can include what ports are available, chip
  736. * variants, which GPIO pins act in what additional roles, and so
  737. * on. This shrinks the "Board Support Packages" (BSPs) and
  738. * minimizes board-specific #ifdefs in drivers.
  739. * @driver_data: Private pointer for driver specific info.
  740. * @links: Links to suppliers and consumers of this device.
  741. * @power: For device power management.
  742. * See Documentation/driver-api/pm/devices.rst for details.
  743. * @pm_domain: Provide callbacks that are executed during system suspend,
  744. * hibernation, system resume and during runtime PM transitions
  745. * along with subsystem-level and driver-level callbacks.
  746. * @pins: For device pin management.
  747. * See Documentation/driver-api/pinctl.rst for details.
  748. * @msi_list: Hosts MSI descriptors
  749. * @msi_domain: The generic MSI domain this device is using.
  750. * @numa_node: NUMA node this device is close to.
  751. * @dma_ops: DMA mapping operations for this device.
  752. * @dma_mask: Dma mask (if dma'ble device).
  753. * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
  754. * hardware supports 64-bit addresses for consistent allocations
  755. * such descriptors.
  756. * @dma_pfn_offset: offset of DMA memory range relatively of RAM
  757. * @dma_parms: A low level driver may set these to teach IOMMU code about
  758. * segment limitations.
  759. * @dma_pools: Dma pools (if dma'ble device).
  760. * @dma_mem: Internal for coherent mem override.
  761. * @cma_area: Contiguous memory area for dma allocations
  762. * @archdata: For arch-specific additions.
  763. * @of_node: Associated device tree node.
  764. * @fwnode: Associated device node supplied by platform firmware.
  765. * @devt: For creating the sysfs "dev".
  766. * @id: device instance
  767. * @devres_lock: Spinlock to protect the resource of the device.
  768. * @devres_head: The resources list of the device.
  769. * @knode_class: The node used to add the device to the class list.
  770. * @class: The class of the device.
  771. * @groups: Optional attribute groups.
  772. * @release: Callback to free the device after all references have
  773. * gone away. This should be set by the allocator of the
  774. * device (i.e. the bus driver that discovered the device).
  775. * @iommu_group: IOMMU group the device belongs to.
  776. * @iommu_fwspec: IOMMU-specific properties supplied by firmware.
  777. *
  778. * @offline_disabled: If set, the device is permanently online.
  779. * @offline: Set after successful invocation of bus type's .offline().
  780. * @of_node_reused: Set if the device-tree node is shared with an ancestor
  781. * device.
  782. *
  783. * At the lowest level, every device in a Linux system is represented by an
  784. * instance of struct device. The device structure contains the information
  785. * that the device model core needs to model the system. Most subsystems,
  786. * however, track additional information about the devices they host. As a
  787. * result, it is rare for devices to be represented by bare device structures;
  788. * instead, that structure, like kobject structures, is usually embedded within
  789. * a higher-level representation of the device.
  790. */
  791. struct device {
  792. struct device *parent;
  793. struct device_private *p;
  794. struct kobject kobj;
  795. const char *init_name; /* initial name of the device */
  796. const struct device_type *type;
  797. struct mutex mutex; /* mutex to synchronize calls to
  798. * its driver.
  799. */
  800. struct bus_type *bus; /* type of bus device is on */
  801. struct device_driver *driver; /* which driver has allocated this
  802. device */
  803. void *platform_data; /* Platform specific data, device
  804. core doesn't touch it */
  805. void *driver_data; /* Driver data, set and get with
  806. dev_set/get_drvdata */
  807. struct dev_links_info links;
  808. struct dev_pm_info power;
  809. struct dev_pm_domain *pm_domain;
  810. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  811. struct irq_domain *msi_domain;
  812. #endif
  813. #ifdef CONFIG_PINCTRL
  814. struct dev_pin_info *pins;
  815. #endif
  816. #ifdef CONFIG_GENERIC_MSI_IRQ
  817. struct list_head msi_list;
  818. #endif
  819. #ifdef CONFIG_NUMA
  820. int numa_node; /* NUMA node this device is close to */
  821. #endif
  822. const struct dma_map_ops *dma_ops;
  823. u64 *dma_mask; /* dma mask (if dma'able device) */
  824. u64 coherent_dma_mask;/* Like dma_mask, but for
  825. alloc_coherent mappings as
  826. not all hardware supports
  827. 64 bit addresses for consistent
  828. allocations such descriptors. */
  829. unsigned long dma_pfn_offset;
  830. struct device_dma_parameters *dma_parms;
  831. struct list_head dma_pools; /* dma pools (if dma'ble) */
  832. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  833. override */
  834. #ifdef CONFIG_DMA_CMA
  835. struct cma *cma_area; /* contiguous memory area for dma
  836. allocations */
  837. #endif
  838. /* arch specific additions */
  839. struct dev_archdata archdata;
  840. struct device_node *of_node; /* associated device tree node */
  841. struct fwnode_handle *fwnode; /* firmware device node */
  842. dev_t devt; /* dev_t, creates the sysfs "dev" */
  843. u32 id; /* device instance */
  844. spinlock_t devres_lock;
  845. struct list_head devres_head;
  846. struct klist_node knode_class;
  847. struct class *class;
  848. const struct attribute_group **groups; /* optional groups */
  849. void (*release)(struct device *dev);
  850. struct iommu_group *iommu_group;
  851. struct iommu_fwspec *iommu_fwspec;
  852. bool offline_disabled:1;
  853. bool offline:1;
  854. bool of_node_reused:1;
  855. };
  856. static inline struct device *kobj_to_dev(struct kobject *kobj)
  857. {
  858. return container_of(kobj, struct device, kobj);
  859. }
  860. /* Get the wakeup routines, which depend on struct device */
  861. #include <linux/pm_wakeup.h>
  862. static inline const char *dev_name(const struct device *dev)
  863. {
  864. /* Use the init name until the kobject becomes available */
  865. if (dev->init_name)
  866. return dev->init_name;
  867. return kobject_name(&dev->kobj);
  868. }
  869. extern __printf(2, 3)
  870. int dev_set_name(struct device *dev, const char *name, ...);
  871. #ifdef CONFIG_NUMA
  872. static inline int dev_to_node(struct device *dev)
  873. {
  874. return dev->numa_node;
  875. }
  876. static inline void set_dev_node(struct device *dev, int node)
  877. {
  878. dev->numa_node = node;
  879. }
  880. #else
  881. static inline int dev_to_node(struct device *dev)
  882. {
  883. return -1;
  884. }
  885. static inline void set_dev_node(struct device *dev, int node)
  886. {
  887. }
  888. #endif
  889. static inline struct irq_domain *dev_get_msi_domain(const struct device *dev)
  890. {
  891. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  892. return dev->msi_domain;
  893. #else
  894. return NULL;
  895. #endif
  896. }
  897. static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d)
  898. {
  899. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  900. dev->msi_domain = d;
  901. #endif
  902. }
  903. static inline void *dev_get_drvdata(const struct device *dev)
  904. {
  905. return dev->driver_data;
  906. }
  907. static inline void dev_set_drvdata(struct device *dev, void *data)
  908. {
  909. dev->driver_data = data;
  910. }
  911. static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
  912. {
  913. return dev ? dev->power.subsys_data : NULL;
  914. }
  915. static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
  916. {
  917. return dev->kobj.uevent_suppress;
  918. }
  919. static inline void dev_set_uevent_suppress(struct device *dev, int val)
  920. {
  921. dev->kobj.uevent_suppress = val;
  922. }
  923. static inline int device_is_registered(struct device *dev)
  924. {
  925. return dev->kobj.state_in_sysfs;
  926. }
  927. static inline void device_enable_async_suspend(struct device *dev)
  928. {
  929. if (!dev->power.is_prepared)
  930. dev->power.async_suspend = true;
  931. }
  932. static inline void device_disable_async_suspend(struct device *dev)
  933. {
  934. if (!dev->power.is_prepared)
  935. dev->power.async_suspend = false;
  936. }
  937. static inline bool device_async_suspend_enabled(struct device *dev)
  938. {
  939. return !!dev->power.async_suspend;
  940. }
  941. static inline void dev_pm_syscore_device(struct device *dev, bool val)
  942. {
  943. #ifdef CONFIG_PM_SLEEP
  944. dev->power.syscore = val;
  945. #endif
  946. }
  947. static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags)
  948. {
  949. dev->power.driver_flags = flags;
  950. }
  951. static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags)
  952. {
  953. return !!(dev->power.driver_flags & flags);
  954. }
  955. static inline void device_lock(struct device *dev)
  956. {
  957. mutex_lock(&dev->mutex);
  958. }
  959. static inline int device_lock_interruptible(struct device *dev)
  960. {
  961. return mutex_lock_interruptible(&dev->mutex);
  962. }
  963. static inline int device_trylock(struct device *dev)
  964. {
  965. return mutex_trylock(&dev->mutex);
  966. }
  967. static inline void device_unlock(struct device *dev)
  968. {
  969. mutex_unlock(&dev->mutex);
  970. }
  971. static inline void device_lock_assert(struct device *dev)
  972. {
  973. lockdep_assert_held(&dev->mutex);
  974. }
  975. static inline struct device_node *dev_of_node(struct device *dev)
  976. {
  977. if (!IS_ENABLED(CONFIG_OF))
  978. return NULL;
  979. return dev->of_node;
  980. }
  981. void driver_init(void);
  982. /*
  983. * High level routines for use by the bus drivers
  984. */
  985. extern int __must_check device_register(struct device *dev);
  986. extern void device_unregister(struct device *dev);
  987. extern void device_initialize(struct device *dev);
  988. extern int __must_check device_add(struct device *dev);
  989. extern void device_del(struct device *dev);
  990. extern int device_for_each_child(struct device *dev, void *data,
  991. int (*fn)(struct device *dev, void *data));
  992. extern int device_for_each_child_reverse(struct device *dev, void *data,
  993. int (*fn)(struct device *dev, void *data));
  994. extern struct device *device_find_child(struct device *dev, void *data,
  995. int (*match)(struct device *dev, void *data));
  996. extern int device_rename(struct device *dev, const char *new_name);
  997. extern int device_move(struct device *dev, struct device *new_parent,
  998. enum dpm_order dpm_order);
  999. extern const char *device_get_devnode(struct device *dev,
  1000. umode_t *mode, kuid_t *uid, kgid_t *gid,
  1001. const char **tmp);
  1002. static inline bool device_supports_offline(struct device *dev)
  1003. {
  1004. return dev->bus && dev->bus->offline && dev->bus->online;
  1005. }
  1006. extern void lock_device_hotplug(void);
  1007. extern void unlock_device_hotplug(void);
  1008. extern int lock_device_hotplug_sysfs(void);
  1009. extern int device_offline(struct device *dev);
  1010. extern int device_online(struct device *dev);
  1011. extern void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
  1012. extern void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
  1013. void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
  1014. static inline int dev_num_vf(struct device *dev)
  1015. {
  1016. if (dev->bus && dev->bus->num_vf)
  1017. return dev->bus->num_vf(dev);
  1018. return 0;
  1019. }
  1020. /*
  1021. * Root device objects for grouping under /sys/devices
  1022. */
  1023. extern struct device *__root_device_register(const char *name,
  1024. struct module *owner);
  1025. /* This is a macro to avoid include problems with THIS_MODULE */
  1026. #define root_device_register(name) \
  1027. __root_device_register(name, THIS_MODULE)
  1028. extern void root_device_unregister(struct device *root);
  1029. static inline void *dev_get_platdata(const struct device *dev)
  1030. {
  1031. return dev->platform_data;
  1032. }
  1033. /*
  1034. * Manual binding of a device to driver. See drivers/base/bus.c
  1035. * for information on use.
  1036. */
  1037. extern int __must_check device_bind_driver(struct device *dev);
  1038. extern void device_release_driver(struct device *dev);
  1039. extern int __must_check device_attach(struct device *dev);
  1040. extern int __must_check driver_attach(struct device_driver *drv);
  1041. extern void device_initial_probe(struct device *dev);
  1042. extern int __must_check device_reprobe(struct device *dev);
  1043. extern bool device_is_bound(struct device *dev);
  1044. /*
  1045. * Easy functions for dynamically creating devices on the fly
  1046. */
  1047. extern __printf(5, 0)
  1048. struct device *device_create_vargs(struct class *cls, struct device *parent,
  1049. dev_t devt, void *drvdata,
  1050. const char *fmt, va_list vargs);
  1051. extern __printf(5, 6)
  1052. struct device *device_create(struct class *cls, struct device *parent,
  1053. dev_t devt, void *drvdata,
  1054. const char *fmt, ...);
  1055. extern __printf(6, 7)
  1056. struct device *device_create_with_groups(struct class *cls,
  1057. struct device *parent, dev_t devt, void *drvdata,
  1058. const struct attribute_group **groups,
  1059. const char *fmt, ...);
  1060. extern void device_destroy(struct class *cls, dev_t devt);
  1061. extern int __must_check device_add_groups(struct device *dev,
  1062. const struct attribute_group **groups);
  1063. extern void device_remove_groups(struct device *dev,
  1064. const struct attribute_group **groups);
  1065. static inline int __must_check device_add_group(struct device *dev,
  1066. const struct attribute_group *grp)
  1067. {
  1068. const struct attribute_group *groups[] = { grp, NULL };
  1069. return device_add_groups(dev, groups);
  1070. }
  1071. static inline void device_remove_group(struct device *dev,
  1072. const struct attribute_group *grp)
  1073. {
  1074. const struct attribute_group *groups[] = { grp, NULL };
  1075. return device_remove_groups(dev, groups);
  1076. }
  1077. extern int __must_check devm_device_add_groups(struct device *dev,
  1078. const struct attribute_group **groups);
  1079. extern void devm_device_remove_groups(struct device *dev,
  1080. const struct attribute_group **groups);
  1081. extern int __must_check devm_device_add_group(struct device *dev,
  1082. const struct attribute_group *grp);
  1083. extern void devm_device_remove_group(struct device *dev,
  1084. const struct attribute_group *grp);
  1085. /*
  1086. * Platform "fixup" functions - allow the platform to have their say
  1087. * about devices and actions that the general device layer doesn't
  1088. * know about.
  1089. */
  1090. /* Notify platform of device discovery */
  1091. extern int (*platform_notify)(struct device *dev);
  1092. extern int (*platform_notify_remove)(struct device *dev);
  1093. /*
  1094. * get_device - atomically increment the reference count for the device.
  1095. *
  1096. */
  1097. extern struct device *get_device(struct device *dev);
  1098. extern void put_device(struct device *dev);
  1099. #ifdef CONFIG_DEVTMPFS
  1100. extern int devtmpfs_create_node(struct device *dev);
  1101. extern int devtmpfs_delete_node(struct device *dev);
  1102. extern int devtmpfs_mount(const char *mntdir);
  1103. #else
  1104. static inline int devtmpfs_create_node(struct device *dev) { return 0; }
  1105. static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
  1106. static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
  1107. #endif
  1108. /* drivers/base/power/shutdown.c */
  1109. extern void device_shutdown(void);
  1110. /* debugging and troubleshooting/diagnostic helpers. */
  1111. extern const char *dev_driver_string(const struct device *dev);
  1112. /* Device links interface. */
  1113. struct device_link *device_link_add(struct device *consumer,
  1114. struct device *supplier, u32 flags);
  1115. void device_link_del(struct device_link *link);
  1116. #ifdef CONFIG_PRINTK
  1117. extern __printf(3, 0)
  1118. int dev_vprintk_emit(int level, const struct device *dev,
  1119. const char *fmt, va_list args);
  1120. extern __printf(3, 4)
  1121. int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
  1122. extern __printf(3, 4)
  1123. void dev_printk(const char *level, const struct device *dev,
  1124. const char *fmt, ...);
  1125. extern __printf(2, 3)
  1126. void dev_emerg(const struct device *dev, const char *fmt, ...);
  1127. extern __printf(2, 3)
  1128. void dev_alert(const struct device *dev, const char *fmt, ...);
  1129. extern __printf(2, 3)
  1130. void dev_crit(const struct device *dev, const char *fmt, ...);
  1131. extern __printf(2, 3)
  1132. void dev_err(const struct device *dev, const char *fmt, ...);
  1133. extern __printf(2, 3)
  1134. void dev_warn(const struct device *dev, const char *fmt, ...);
  1135. extern __printf(2, 3)
  1136. void dev_notice(const struct device *dev, const char *fmt, ...);
  1137. extern __printf(2, 3)
  1138. void _dev_info(const struct device *dev, const char *fmt, ...);
  1139. #else
  1140. static inline __printf(3, 0)
  1141. int dev_vprintk_emit(int level, const struct device *dev,
  1142. const char *fmt, va_list args)
  1143. { return 0; }
  1144. static inline __printf(3, 4)
  1145. int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
  1146. { return 0; }
  1147. static inline void __dev_printk(const char *level, const struct device *dev,
  1148. struct va_format *vaf)
  1149. {}
  1150. static inline __printf(3, 4)
  1151. void dev_printk(const char *level, const struct device *dev,
  1152. const char *fmt, ...)
  1153. {}
  1154. static inline __printf(2, 3)
  1155. void dev_emerg(const struct device *dev, const char *fmt, ...)
  1156. {}
  1157. static inline __printf(2, 3)
  1158. void dev_crit(const struct device *dev, const char *fmt, ...)
  1159. {}
  1160. static inline __printf(2, 3)
  1161. void dev_alert(const struct device *dev, const char *fmt, ...)
  1162. {}
  1163. static inline __printf(2, 3)
  1164. void dev_err(const struct device *dev, const char *fmt, ...)
  1165. {}
  1166. static inline __printf(2, 3)
  1167. void dev_warn(const struct device *dev, const char *fmt, ...)
  1168. {}
  1169. static inline __printf(2, 3)
  1170. void dev_notice(const struct device *dev, const char *fmt, ...)
  1171. {}
  1172. static inline __printf(2, 3)
  1173. void _dev_info(const struct device *dev, const char *fmt, ...)
  1174. {}
  1175. #endif
  1176. /*
  1177. * Stupid hackaround for existing uses of non-printk uses dev_info
  1178. *
  1179. * Note that the definition of dev_info below is actually _dev_info
  1180. * and a macro is used to avoid redefining dev_info
  1181. */
  1182. #define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
  1183. #if defined(CONFIG_DYNAMIC_DEBUG)
  1184. #define dev_dbg(dev, format, ...) \
  1185. do { \
  1186. dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
  1187. } while (0)
  1188. #elif defined(DEBUG)
  1189. #define dev_dbg(dev, format, arg...) \
  1190. dev_printk(KERN_DEBUG, dev, format, ##arg)
  1191. #else
  1192. #define dev_dbg(dev, format, arg...) \
  1193. ({ \
  1194. if (0) \
  1195. dev_printk(KERN_DEBUG, dev, format, ##arg); \
  1196. })
  1197. #endif
  1198. #ifdef CONFIG_PRINTK
  1199. #define dev_level_once(dev_level, dev, fmt, ...) \
  1200. do { \
  1201. static bool __print_once __read_mostly; \
  1202. \
  1203. if (!__print_once) { \
  1204. __print_once = true; \
  1205. dev_level(dev, fmt, ##__VA_ARGS__); \
  1206. } \
  1207. } while (0)
  1208. #else
  1209. #define dev_level_once(dev_level, dev, fmt, ...) \
  1210. do { \
  1211. if (0) \
  1212. dev_level(dev, fmt, ##__VA_ARGS__); \
  1213. } while (0)
  1214. #endif
  1215. #define dev_emerg_once(dev, fmt, ...) \
  1216. dev_level_once(dev_emerg, dev, fmt, ##__VA_ARGS__)
  1217. #define dev_alert_once(dev, fmt, ...) \
  1218. dev_level_once(dev_alert, dev, fmt, ##__VA_ARGS__)
  1219. #define dev_crit_once(dev, fmt, ...) \
  1220. dev_level_once(dev_crit, dev, fmt, ##__VA_ARGS__)
  1221. #define dev_err_once(dev, fmt, ...) \
  1222. dev_level_once(dev_err, dev, fmt, ##__VA_ARGS__)
  1223. #define dev_warn_once(dev, fmt, ...) \
  1224. dev_level_once(dev_warn, dev, fmt, ##__VA_ARGS__)
  1225. #define dev_notice_once(dev, fmt, ...) \
  1226. dev_level_once(dev_notice, dev, fmt, ##__VA_ARGS__)
  1227. #define dev_info_once(dev, fmt, ...) \
  1228. dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__)
  1229. #define dev_dbg_once(dev, fmt, ...) \
  1230. dev_level_once(dev_dbg, dev, fmt, ##__VA_ARGS__)
  1231. #define dev_level_ratelimited(dev_level, dev, fmt, ...) \
  1232. do { \
  1233. static DEFINE_RATELIMIT_STATE(_rs, \
  1234. DEFAULT_RATELIMIT_INTERVAL, \
  1235. DEFAULT_RATELIMIT_BURST); \
  1236. if (__ratelimit(&_rs)) \
  1237. dev_level(dev, fmt, ##__VA_ARGS__); \
  1238. } while (0)
  1239. #define dev_emerg_ratelimited(dev, fmt, ...) \
  1240. dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
  1241. #define dev_alert_ratelimited(dev, fmt, ...) \
  1242. dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
  1243. #define dev_crit_ratelimited(dev, fmt, ...) \
  1244. dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
  1245. #define dev_err_ratelimited(dev, fmt, ...) \
  1246. dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
  1247. #define dev_warn_ratelimited(dev, fmt, ...) \
  1248. dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
  1249. #define dev_notice_ratelimited(dev, fmt, ...) \
  1250. dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
  1251. #define dev_info_ratelimited(dev, fmt, ...) \
  1252. dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
  1253. #if defined(CONFIG_DYNAMIC_DEBUG)
  1254. /* descriptor check is first to prevent flooding with "callbacks suppressed" */
  1255. #define dev_dbg_ratelimited(dev, fmt, ...) \
  1256. do { \
  1257. static DEFINE_RATELIMIT_STATE(_rs, \
  1258. DEFAULT_RATELIMIT_INTERVAL, \
  1259. DEFAULT_RATELIMIT_BURST); \
  1260. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  1261. if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
  1262. __ratelimit(&_rs)) \
  1263. __dynamic_dev_dbg(&descriptor, dev, fmt, \
  1264. ##__VA_ARGS__); \
  1265. } while (0)
  1266. #elif defined(DEBUG)
  1267. #define dev_dbg_ratelimited(dev, fmt, ...) \
  1268. do { \
  1269. static DEFINE_RATELIMIT_STATE(_rs, \
  1270. DEFAULT_RATELIMIT_INTERVAL, \
  1271. DEFAULT_RATELIMIT_BURST); \
  1272. if (__ratelimit(&_rs)) \
  1273. dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); \
  1274. } while (0)
  1275. #else
  1276. #define dev_dbg_ratelimited(dev, fmt, ...) \
  1277. do { \
  1278. if (0) \
  1279. dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); \
  1280. } while (0)
  1281. #endif
  1282. #ifdef VERBOSE_DEBUG
  1283. #define dev_vdbg dev_dbg
  1284. #else
  1285. #define dev_vdbg(dev, format, arg...) \
  1286. ({ \
  1287. if (0) \
  1288. dev_printk(KERN_DEBUG, dev, format, ##arg); \
  1289. })
  1290. #endif
  1291. /*
  1292. * dev_WARN*() acts like dev_printk(), but with the key difference of
  1293. * using WARN/WARN_ONCE to include file/line information and a backtrace.
  1294. */
  1295. #define dev_WARN(dev, format, arg...) \
  1296. WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg);
  1297. #define dev_WARN_ONCE(dev, condition, format, arg...) \
  1298. WARN_ONCE(condition, "%s %s: " format, \
  1299. dev_driver_string(dev), dev_name(dev), ## arg)
  1300. /* Create alias, so I can be autoloaded. */
  1301. #define MODULE_ALIAS_CHARDEV(major,minor) \
  1302. MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
  1303. #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
  1304. MODULE_ALIAS("char-major-" __stringify(major) "-*")
  1305. #ifdef CONFIG_SYSFS_DEPRECATED
  1306. extern long sysfs_deprecated;
  1307. #else
  1308. #define sysfs_deprecated 0
  1309. #endif
  1310. /**
  1311. * module_driver() - Helper macro for drivers that don't do anything
  1312. * special in module init/exit. This eliminates a lot of boilerplate.
  1313. * Each module may only use this macro once, and calling it replaces
  1314. * module_init() and module_exit().
  1315. *
  1316. * @__driver: driver name
  1317. * @__register: register function for this driver type
  1318. * @__unregister: unregister function for this driver type
  1319. * @...: Additional arguments to be passed to __register and __unregister.
  1320. *
  1321. * Use this macro to construct bus specific macros for registering
  1322. * drivers, and do not use it on its own.
  1323. */
  1324. #define module_driver(__driver, __register, __unregister, ...) \
  1325. static int __init __driver##_init(void) \
  1326. { \
  1327. return __register(&(__driver) , ##__VA_ARGS__); \
  1328. } \
  1329. module_init(__driver##_init); \
  1330. static void __exit __driver##_exit(void) \
  1331. { \
  1332. __unregister(&(__driver) , ##__VA_ARGS__); \
  1333. } \
  1334. module_exit(__driver##_exit);
  1335. /**
  1336. * builtin_driver() - Helper macro for drivers that don't do anything
  1337. * special in init and have no exit. This eliminates some boilerplate.
  1338. * Each driver may only use this macro once, and calling it replaces
  1339. * device_initcall (or in some cases, the legacy __initcall). This is
  1340. * meant to be a direct parallel of module_driver() above but without
  1341. * the __exit stuff that is not used for builtin cases.
  1342. *
  1343. * @__driver: driver name
  1344. * @__register: register function for this driver type
  1345. * @...: Additional arguments to be passed to __register
  1346. *
  1347. * Use this macro to construct bus specific macros for registering
  1348. * drivers, and do not use it on its own.
  1349. */
  1350. #define builtin_driver(__driver, __register, ...) \
  1351. static int __init __driver##_init(void) \
  1352. { \
  1353. return __register(&(__driver) , ##__VA_ARGS__); \
  1354. } \
  1355. device_initcall(__driver##_init);
  1356. #endif /* _DEVICE_H_ */