device.h 48 KB

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