device.h 38 KB

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