device.h 37 KB

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