device.h 48 KB

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