device.h 31 KB

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