device.h 54 KB

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