device.h 46 KB

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