device.h 44 KB

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