device.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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_free(void *res);
  466. extern void devres_add(struct device *dev, void *res);
  467. extern void *devres_find(struct device *dev, dr_release_t release,
  468. dr_match_t match, void *match_data);
  469. extern void *devres_get(struct device *dev, void *new_res,
  470. dr_match_t match, void *match_data);
  471. extern void *devres_remove(struct device *dev, dr_release_t release,
  472. dr_match_t match, void *match_data);
  473. extern int devres_destroy(struct device *dev, dr_release_t release,
  474. dr_match_t match, void *match_data);
  475. extern int devres_release(struct device *dev, dr_release_t release,
  476. dr_match_t match, void *match_data);
  477. /* devres group */
  478. extern void * __must_check devres_open_group(struct device *dev, void *id,
  479. gfp_t gfp);
  480. extern void devres_close_group(struct device *dev, void *id);
  481. extern void devres_remove_group(struct device *dev, void *id);
  482. extern int devres_release_group(struct device *dev, void *id);
  483. /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
  484. extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
  485. extern void devm_kfree(struct device *dev, void *p);
  486. void __iomem *devm_request_and_ioremap(struct device *dev,
  487. struct resource *res);
  488. struct device_dma_parameters {
  489. /*
  490. * a low level driver may set these to teach IOMMU code about
  491. * sg limitations.
  492. */
  493. unsigned int max_segment_size;
  494. unsigned long segment_boundary_mask;
  495. };
  496. /**
  497. * struct device - The basic device structure
  498. * @parent: The device's "parent" device, the device to which it is attached.
  499. * In most cases, a parent device is some sort of bus or host
  500. * controller. If parent is NULL, the device, is a top-level device,
  501. * which is not usually what you want.
  502. * @p: Holds the private data of the driver core portions of the device.
  503. * See the comment of the struct device_private for detail.
  504. * @kobj: A top-level, abstract class from which other classes are derived.
  505. * @init_name: Initial name of the device.
  506. * @type: The type of device.
  507. * This identifies the device type and carries type-specific
  508. * information.
  509. * @mutex: Mutex to synchronize calls to its driver.
  510. * @bus: Type of bus device is on.
  511. * @driver: Which driver has allocated this
  512. * @platform_data: Platform data specific to the device.
  513. * Example: For devices on custom boards, as typical of embedded
  514. * and SOC based hardware, Linux often uses platform_data to point
  515. * to board-specific structures describing devices and how they
  516. * are wired. That can include what ports are available, chip
  517. * variants, which GPIO pins act in what additional roles, and so
  518. * on. This shrinks the "Board Support Packages" (BSPs) and
  519. * minimizes board-specific #ifdefs in drivers.
  520. * @power: For device power management.
  521. * See Documentation/power/devices.txt for details.
  522. * @pm_domain: Provide callbacks that are executed during system suspend,
  523. * hibernation, system resume and during runtime PM transitions
  524. * along with subsystem-level and driver-level callbacks.
  525. * @numa_node: NUMA node this device is close to.
  526. * @dma_mask: Dma mask (if dma'ble device).
  527. * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
  528. * hardware supports 64-bit addresses for consistent allocations
  529. * such descriptors.
  530. * @dma_parms: A low level driver may set these to teach IOMMU code about
  531. * segment limitations.
  532. * @dma_pools: Dma pools (if dma'ble device).
  533. * @dma_mem: Internal for coherent mem override.
  534. * @archdata: For arch-specific additions.
  535. * @of_node: Associated device tree node.
  536. * @devt: For creating the sysfs "dev".
  537. * @id: device instance
  538. * @devres_lock: Spinlock to protect the resource of the device.
  539. * @devres_head: The resources list of the device.
  540. * @knode_class: The node used to add the device to the class list.
  541. * @class: The class of the device.
  542. * @groups: Optional attribute groups.
  543. * @release: Callback to free the device after all references have
  544. * gone away. This should be set by the allocator of the
  545. * device (i.e. the bus driver that discovered the device).
  546. *
  547. * At the lowest level, every device in a Linux system is represented by an
  548. * instance of struct device. The device structure contains the information
  549. * that the device model core needs to model the system. Most subsystems,
  550. * however, track additional information about the devices they host. As a
  551. * result, it is rare for devices to be represented by bare device structures;
  552. * instead, that structure, like kobject structures, is usually embedded within
  553. * a higher-level representation of the device.
  554. */
  555. struct device {
  556. struct device *parent;
  557. struct device_private *p;
  558. struct kobject kobj;
  559. const char *init_name; /* initial name of the device */
  560. const struct device_type *type;
  561. struct mutex mutex; /* mutex to synchronize calls to
  562. * its driver.
  563. */
  564. struct bus_type *bus; /* type of bus device is on */
  565. struct device_driver *driver; /* which driver has allocated this
  566. device */
  567. void *platform_data; /* Platform specific data, device
  568. core doesn't touch it */
  569. struct dev_pm_info power;
  570. struct dev_pm_domain *pm_domain;
  571. #ifdef CONFIG_NUMA
  572. int numa_node; /* NUMA node this device is close to */
  573. #endif
  574. u64 *dma_mask; /* dma mask (if dma'able device) */
  575. u64 coherent_dma_mask;/* Like dma_mask, but for
  576. alloc_coherent mappings as
  577. not all hardware supports
  578. 64 bit addresses for consistent
  579. allocations such descriptors. */
  580. struct device_dma_parameters *dma_parms;
  581. struct list_head dma_pools; /* dma pools (if dma'ble) */
  582. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  583. override */
  584. #ifdef CONFIG_CMA
  585. struct cma *cma_area; /* contiguous memory area for dma
  586. allocations */
  587. #endif
  588. /* arch specific additions */
  589. struct dev_archdata archdata;
  590. struct device_node *of_node; /* associated device tree node */
  591. dev_t devt; /* dev_t, creates the sysfs "dev" */
  592. u32 id; /* device instance */
  593. spinlock_t devres_lock;
  594. struct list_head devres_head;
  595. struct klist_node knode_class;
  596. struct class *class;
  597. const struct attribute_group **groups; /* optional groups */
  598. void (*release)(struct device *dev);
  599. struct iommu_group *iommu_group;
  600. };
  601. static inline struct device *kobj_to_dev(struct kobject *kobj)
  602. {
  603. return container_of(kobj, struct device, kobj);
  604. }
  605. /* Get the wakeup routines, which depend on struct device */
  606. #include <linux/pm_wakeup.h>
  607. static inline const char *dev_name(const struct device *dev)
  608. {
  609. /* Use the init name until the kobject becomes available */
  610. if (dev->init_name)
  611. return dev->init_name;
  612. return kobject_name(&dev->kobj);
  613. }
  614. extern __printf(2, 3)
  615. int dev_set_name(struct device *dev, const char *name, ...);
  616. #ifdef CONFIG_NUMA
  617. static inline int dev_to_node(struct device *dev)
  618. {
  619. return dev->numa_node;
  620. }
  621. static inline void set_dev_node(struct device *dev, int node)
  622. {
  623. dev->numa_node = node;
  624. }
  625. #else
  626. static inline int dev_to_node(struct device *dev)
  627. {
  628. return -1;
  629. }
  630. static inline void set_dev_node(struct device *dev, int node)
  631. {
  632. }
  633. #endif
  634. static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
  635. {
  636. return dev ? dev->power.subsys_data : NULL;
  637. }
  638. static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
  639. {
  640. return dev->kobj.uevent_suppress;
  641. }
  642. static inline void dev_set_uevent_suppress(struct device *dev, int val)
  643. {
  644. dev->kobj.uevent_suppress = val;
  645. }
  646. static inline int device_is_registered(struct device *dev)
  647. {
  648. return dev->kobj.state_in_sysfs;
  649. }
  650. static inline void device_enable_async_suspend(struct device *dev)
  651. {
  652. if (!dev->power.is_prepared)
  653. dev->power.async_suspend = true;
  654. }
  655. static inline void device_disable_async_suspend(struct device *dev)
  656. {
  657. if (!dev->power.is_prepared)
  658. dev->power.async_suspend = false;
  659. }
  660. static inline bool device_async_suspend_enabled(struct device *dev)
  661. {
  662. return !!dev->power.async_suspend;
  663. }
  664. static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
  665. {
  666. dev->power.ignore_children = enable;
  667. }
  668. static inline void device_lock(struct device *dev)
  669. {
  670. mutex_lock(&dev->mutex);
  671. }
  672. static inline int device_trylock(struct device *dev)
  673. {
  674. return mutex_trylock(&dev->mutex);
  675. }
  676. static inline void device_unlock(struct device *dev)
  677. {
  678. mutex_unlock(&dev->mutex);
  679. }
  680. void driver_init(void);
  681. /*
  682. * High level routines for use by the bus drivers
  683. */
  684. extern int __must_check device_register(struct device *dev);
  685. extern void device_unregister(struct device *dev);
  686. extern void device_initialize(struct device *dev);
  687. extern int __must_check device_add(struct device *dev);
  688. extern void device_del(struct device *dev);
  689. extern int device_for_each_child(struct device *dev, void *data,
  690. int (*fn)(struct device *dev, void *data));
  691. extern struct device *device_find_child(struct device *dev, void *data,
  692. int (*match)(struct device *dev, void *data));
  693. extern int device_rename(struct device *dev, const char *new_name);
  694. extern int device_move(struct device *dev, struct device *new_parent,
  695. enum dpm_order dpm_order);
  696. extern const char *device_get_devnode(struct device *dev,
  697. umode_t *mode, const char **tmp);
  698. extern void *dev_get_drvdata(const struct device *dev);
  699. extern int dev_set_drvdata(struct device *dev, void *data);
  700. /*
  701. * Root device objects for grouping under /sys/devices
  702. */
  703. extern struct device *__root_device_register(const char *name,
  704. struct module *owner);
  705. /*
  706. * This is a macro to avoid include problems with THIS_MODULE,
  707. * just as per what is done for device_schedule_callback() above.
  708. */
  709. #define root_device_register(name) \
  710. __root_device_register(name, THIS_MODULE)
  711. extern void root_device_unregister(struct device *root);
  712. static inline void *dev_get_platdata(const struct device *dev)
  713. {
  714. return dev->platform_data;
  715. }
  716. /*
  717. * Manual binding of a device to driver. See drivers/base/bus.c
  718. * for information on use.
  719. */
  720. extern int __must_check device_bind_driver(struct device *dev);
  721. extern void device_release_driver(struct device *dev);
  722. extern int __must_check device_attach(struct device *dev);
  723. extern int __must_check driver_attach(struct device_driver *drv);
  724. extern int __must_check device_reprobe(struct device *dev);
  725. /*
  726. * Easy functions for dynamically creating devices on the fly
  727. */
  728. extern struct device *device_create_vargs(struct class *cls,
  729. struct device *parent,
  730. dev_t devt,
  731. void *drvdata,
  732. const char *fmt,
  733. va_list vargs);
  734. extern __printf(5, 6)
  735. struct device *device_create(struct class *cls, struct device *parent,
  736. dev_t devt, void *drvdata,
  737. const char *fmt, ...);
  738. extern void device_destroy(struct class *cls, dev_t devt);
  739. /*
  740. * Platform "fixup" functions - allow the platform to have their say
  741. * about devices and actions that the general device layer doesn't
  742. * know about.
  743. */
  744. /* Notify platform of device discovery */
  745. extern int (*platform_notify)(struct device *dev);
  746. extern int (*platform_notify_remove)(struct device *dev);
  747. /*
  748. * get_device - atomically increment the reference count for the device.
  749. *
  750. */
  751. extern struct device *get_device(struct device *dev);
  752. extern void put_device(struct device *dev);
  753. #ifdef CONFIG_DEVTMPFS
  754. extern int devtmpfs_create_node(struct device *dev);
  755. extern int devtmpfs_delete_node(struct device *dev);
  756. extern int devtmpfs_mount(const char *mntdir);
  757. #else
  758. static inline int devtmpfs_create_node(struct device *dev) { return 0; }
  759. static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
  760. static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
  761. #endif
  762. /* drivers/base/power/shutdown.c */
  763. extern void device_shutdown(void);
  764. /* debugging and troubleshooting/diagnostic helpers. */
  765. extern const char *dev_driver_string(const struct device *dev);
  766. #ifdef CONFIG_PRINTK
  767. extern int __dev_printk(const char *level, const struct device *dev,
  768. struct va_format *vaf);
  769. extern __printf(3, 4)
  770. int dev_printk(const char *level, const struct device *dev,
  771. const char *fmt, ...)
  772. ;
  773. extern __printf(2, 3)
  774. int dev_emerg(const struct device *dev, const char *fmt, ...);
  775. extern __printf(2, 3)
  776. int dev_alert(const struct device *dev, const char *fmt, ...);
  777. extern __printf(2, 3)
  778. int dev_crit(const struct device *dev, const char *fmt, ...);
  779. extern __printf(2, 3)
  780. int dev_err(const struct device *dev, const char *fmt, ...);
  781. extern __printf(2, 3)
  782. int dev_warn(const struct device *dev, const char *fmt, ...);
  783. extern __printf(2, 3)
  784. int dev_notice(const struct device *dev, const char *fmt, ...);
  785. extern __printf(2, 3)
  786. int _dev_info(const struct device *dev, const char *fmt, ...);
  787. #else
  788. static inline int __dev_printk(const char *level, const struct device *dev,
  789. struct va_format *vaf)
  790. { return 0; }
  791. static inline __printf(3, 4)
  792. int dev_printk(const char *level, const struct device *dev,
  793. const char *fmt, ...)
  794. { return 0; }
  795. static inline __printf(2, 3)
  796. int dev_emerg(const struct device *dev, const char *fmt, ...)
  797. { return 0; }
  798. static inline __printf(2, 3)
  799. int dev_crit(const struct device *dev, const char *fmt, ...)
  800. { return 0; }
  801. static inline __printf(2, 3)
  802. int dev_alert(const struct device *dev, const char *fmt, ...)
  803. { return 0; }
  804. static inline __printf(2, 3)
  805. int dev_err(const struct device *dev, const char *fmt, ...)
  806. { return 0; }
  807. static inline __printf(2, 3)
  808. int dev_warn(const struct device *dev, const char *fmt, ...)
  809. { return 0; }
  810. static inline __printf(2, 3)
  811. int dev_notice(const struct device *dev, const char *fmt, ...)
  812. { return 0; }
  813. static inline __printf(2, 3)
  814. int _dev_info(const struct device *dev, const char *fmt, ...)
  815. { return 0; }
  816. #endif
  817. #define dev_level_ratelimited(dev_level, dev, fmt, ...) \
  818. do { \
  819. static DEFINE_RATELIMIT_STATE(_rs, \
  820. DEFAULT_RATELIMIT_INTERVAL, \
  821. DEFAULT_RATELIMIT_BURST); \
  822. if (__ratelimit(&_rs)) \
  823. dev_level(dev, fmt, ##__VA_ARGS__); \
  824. } while (0)
  825. #define dev_emerg_ratelimited(dev, fmt, ...) \
  826. dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
  827. #define dev_alert_ratelimited(dev, fmt, ...) \
  828. dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
  829. #define dev_crit_ratelimited(dev, fmt, ...) \
  830. dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
  831. #define dev_err_ratelimited(dev, fmt, ...) \
  832. dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
  833. #define dev_warn_ratelimited(dev, fmt, ...) \
  834. dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
  835. #define dev_notice_ratelimited(dev, fmt, ...) \
  836. dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
  837. #define dev_info_ratelimited(dev, fmt, ...) \
  838. dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
  839. #define dev_dbg_ratelimited(dev, fmt, ...) \
  840. dev_level_ratelimited(dev_dbg, dev, fmt, ##__VA_ARGS__)
  841. /*
  842. * Stupid hackaround for existing uses of non-printk uses dev_info
  843. *
  844. * Note that the definition of dev_info below is actually _dev_info
  845. * and a macro is used to avoid redefining dev_info
  846. */
  847. #define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
  848. #if defined(CONFIG_DYNAMIC_DEBUG)
  849. #define dev_dbg(dev, format, ...) \
  850. do { \
  851. dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
  852. } while (0)
  853. #elif defined(DEBUG)
  854. #define dev_dbg(dev, format, arg...) \
  855. dev_printk(KERN_DEBUG, dev, format, ##arg)
  856. #else
  857. #define dev_dbg(dev, format, arg...) \
  858. ({ \
  859. if (0) \
  860. dev_printk(KERN_DEBUG, dev, format, ##arg); \
  861. 0; \
  862. })
  863. #endif
  864. #ifdef VERBOSE_DEBUG
  865. #define dev_vdbg dev_dbg
  866. #else
  867. #define dev_vdbg(dev, format, arg...) \
  868. ({ \
  869. if (0) \
  870. dev_printk(KERN_DEBUG, dev, format, ##arg); \
  871. 0; \
  872. })
  873. #endif
  874. /*
  875. * dev_WARN*() acts like dev_printk(), but with the key difference
  876. * of using a WARN/WARN_ON to get the message out, including the
  877. * file/line information and a backtrace.
  878. */
  879. #define dev_WARN(dev, format, arg...) \
  880. WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
  881. #define dev_WARN_ONCE(dev, condition, format, arg...) \
  882. WARN_ONCE(condition, "Device %s\n" format, \
  883. dev_driver_string(dev), ## arg)
  884. /* Create alias, so I can be autoloaded. */
  885. #define MODULE_ALIAS_CHARDEV(major,minor) \
  886. MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
  887. #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
  888. MODULE_ALIAS("char-major-" __stringify(major) "-*")
  889. #ifdef CONFIG_SYSFS_DEPRECATED
  890. extern long sysfs_deprecated;
  891. #else
  892. #define sysfs_deprecated 0
  893. #endif
  894. /**
  895. * module_driver() - Helper macro for drivers that don't do anything
  896. * special in module init/exit. This eliminates a lot of boilerplate.
  897. * Each module may only use this macro once, and calling it replaces
  898. * module_init() and module_exit().
  899. *
  900. * @__driver: driver name
  901. * @__register: register function for this driver type
  902. * @__unregister: unregister function for this driver type
  903. * @...: Additional arguments to be passed to __register and __unregister.
  904. *
  905. * Use this macro to construct bus specific macros for registering
  906. * drivers, and do not use it on its own.
  907. */
  908. #define module_driver(__driver, __register, __unregister, ...) \
  909. static int __init __driver##_init(void) \
  910. { \
  911. return __register(&(__driver) , ##__VA_ARGS__); \
  912. } \
  913. module_init(__driver##_init); \
  914. static void __exit __driver##_exit(void) \
  915. { \
  916. __unregister(&(__driver) , ##__VA_ARGS__); \
  917. } \
  918. module_exit(__driver##_exit);
  919. #endif /* _DEVICE_H_ */