device.h 44 KB

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