device.h 52 KB

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