device.h 39 KB

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