device.h 40 KB

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