device.h 39 KB

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