device.h 36 KB

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