device.h 32 KB

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