device.h 54 KB

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