device.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. * @pm_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. * @devt: For creating the sysfs "dev".
  459. * @devres_lock: Spinlock to protect the resource of the device.
  460. * @devres_head: The resources list of the device.
  461. * @knode_class: The node used to add the device to the class list.
  462. * @class: The class of the device.
  463. * @groups: Optional attribute groups.
  464. * @release: Callback to free the device after all references have
  465. * gone away. This should be set by the allocator of the
  466. * device (i.e. the bus driver that discovered the device).
  467. *
  468. * At the lowest level, every device in a Linux system is represented by an
  469. * instance of struct device. The device structure contains the information
  470. * that the device model core needs to model the system. Most subsystems,
  471. * however, track additional information about the devices they host. As a
  472. * result, it is rare for devices to be represented by bare device structures;
  473. * instead, that structure, like kobject structures, is usually embedded within
  474. * a higher-level representation of the device.
  475. */
  476. struct device {
  477. struct device *parent;
  478. struct device_private *p;
  479. struct kobject kobj;
  480. const char *init_name; /* initial name of the device */
  481. const struct device_type *type;
  482. struct mutex mutex; /* mutex to synchronize calls to
  483. * its driver.
  484. */
  485. struct bus_type *bus; /* type of bus device is on */
  486. struct device_driver *driver; /* which driver has allocated this
  487. device */
  488. void *platform_data; /* Platform specific data, device
  489. core doesn't touch it */
  490. struct dev_pm_info power;
  491. struct dev_pm_domain *pm_domain;
  492. #ifdef CONFIG_NUMA
  493. int numa_node; /* NUMA node this device is close to */
  494. #endif
  495. u64 *dma_mask; /* dma mask (if dma'able device) */
  496. u64 coherent_dma_mask;/* Like dma_mask, but for
  497. alloc_coherent mappings as
  498. not all hardware supports
  499. 64 bit addresses for consistent
  500. allocations such descriptors. */
  501. struct device_dma_parameters *dma_parms;
  502. struct list_head dma_pools; /* dma pools (if dma'ble) */
  503. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  504. override */
  505. /* arch specific additions */
  506. struct dev_archdata archdata;
  507. struct device_node *of_node; /* associated device tree node */
  508. dev_t devt; /* dev_t, creates the sysfs "dev" */
  509. spinlock_t devres_lock;
  510. struct list_head devres_head;
  511. struct klist_node knode_class;
  512. struct class *class;
  513. const struct attribute_group **groups; /* optional groups */
  514. void (*release)(struct device *dev);
  515. };
  516. /* Get the wakeup routines, which depend on struct device */
  517. #include <linux/pm_wakeup.h>
  518. static inline const char *dev_name(const struct device *dev)
  519. {
  520. /* Use the init name until the kobject becomes available */
  521. if (dev->init_name)
  522. return dev->init_name;
  523. return kobject_name(&dev->kobj);
  524. }
  525. extern int dev_set_name(struct device *dev, const char *name, ...)
  526. __attribute__((format(printf, 2, 3)));
  527. #ifdef CONFIG_NUMA
  528. static inline int dev_to_node(struct device *dev)
  529. {
  530. return dev->numa_node;
  531. }
  532. static inline void set_dev_node(struct device *dev, int node)
  533. {
  534. dev->numa_node = node;
  535. }
  536. #else
  537. static inline int dev_to_node(struct device *dev)
  538. {
  539. return -1;
  540. }
  541. static inline void set_dev_node(struct device *dev, int node)
  542. {
  543. }
  544. #endif
  545. static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
  546. {
  547. return dev->kobj.uevent_suppress;
  548. }
  549. static inline void dev_set_uevent_suppress(struct device *dev, int val)
  550. {
  551. dev->kobj.uevent_suppress = val;
  552. }
  553. static inline int device_is_registered(struct device *dev)
  554. {
  555. return dev->kobj.state_in_sysfs;
  556. }
  557. static inline void device_enable_async_suspend(struct device *dev)
  558. {
  559. if (!dev->power.is_prepared)
  560. dev->power.async_suspend = true;
  561. }
  562. static inline void device_disable_async_suspend(struct device *dev)
  563. {
  564. if (!dev->power.is_prepared)
  565. dev->power.async_suspend = false;
  566. }
  567. static inline bool device_async_suspend_enabled(struct device *dev)
  568. {
  569. return !!dev->power.async_suspend;
  570. }
  571. static inline void device_lock(struct device *dev)
  572. {
  573. mutex_lock(&dev->mutex);
  574. }
  575. static inline int device_trylock(struct device *dev)
  576. {
  577. return mutex_trylock(&dev->mutex);
  578. }
  579. static inline void device_unlock(struct device *dev)
  580. {
  581. mutex_unlock(&dev->mutex);
  582. }
  583. void driver_init(void);
  584. /*
  585. * High level routines for use by the bus drivers
  586. */
  587. extern int __must_check device_register(struct device *dev);
  588. extern void device_unregister(struct device *dev);
  589. extern void device_initialize(struct device *dev);
  590. extern int __must_check device_add(struct device *dev);
  591. extern void device_del(struct device *dev);
  592. extern int device_for_each_child(struct device *dev, void *data,
  593. int (*fn)(struct device *dev, void *data));
  594. extern struct device *device_find_child(struct device *dev, void *data,
  595. int (*match)(struct device *dev, void *data));
  596. extern int device_rename(struct device *dev, const char *new_name);
  597. extern int device_move(struct device *dev, struct device *new_parent,
  598. enum dpm_order dpm_order);
  599. extern const char *device_get_devnode(struct device *dev,
  600. mode_t *mode, const char **tmp);
  601. extern void *dev_get_drvdata(const struct device *dev);
  602. extern int dev_set_drvdata(struct device *dev, void *data);
  603. /*
  604. * Root device objects for grouping under /sys/devices
  605. */
  606. extern struct device *__root_device_register(const char *name,
  607. struct module *owner);
  608. static inline struct device *root_device_register(const char *name)
  609. {
  610. return __root_device_register(name, THIS_MODULE);
  611. }
  612. extern void root_device_unregister(struct device *root);
  613. static inline void *dev_get_platdata(const struct device *dev)
  614. {
  615. return dev->platform_data;
  616. }
  617. /*
  618. * Manual binding of a device to driver. See drivers/base/bus.c
  619. * for information on use.
  620. */
  621. extern int __must_check device_bind_driver(struct device *dev);
  622. extern void device_release_driver(struct device *dev);
  623. extern int __must_check device_attach(struct device *dev);
  624. extern int __must_check driver_attach(struct device_driver *drv);
  625. extern int __must_check device_reprobe(struct device *dev);
  626. /*
  627. * Easy functions for dynamically creating devices on the fly
  628. */
  629. extern struct device *device_create_vargs(struct class *cls,
  630. struct device *parent,
  631. dev_t devt,
  632. void *drvdata,
  633. const char *fmt,
  634. va_list vargs);
  635. extern struct device *device_create(struct class *cls, struct device *parent,
  636. dev_t devt, void *drvdata,
  637. const char *fmt, ...)
  638. __attribute__((format(printf, 5, 6)));
  639. extern void device_destroy(struct class *cls, dev_t devt);
  640. /*
  641. * Platform "fixup" functions - allow the platform to have their say
  642. * about devices and actions that the general device layer doesn't
  643. * know about.
  644. */
  645. /* Notify platform of device discovery */
  646. extern int (*platform_notify)(struct device *dev);
  647. extern int (*platform_notify_remove)(struct device *dev);
  648. /*
  649. * get_device - atomically increment the reference count for the device.
  650. *
  651. */
  652. extern struct device *get_device(struct device *dev);
  653. extern void put_device(struct device *dev);
  654. extern void wait_for_device_probe(void);
  655. #ifdef CONFIG_DEVTMPFS
  656. extern int devtmpfs_create_node(struct device *dev);
  657. extern int devtmpfs_delete_node(struct device *dev);
  658. extern int devtmpfs_mount(const char *mntdir);
  659. #else
  660. static inline int devtmpfs_create_node(struct device *dev) { return 0; }
  661. static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
  662. static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
  663. #endif
  664. /* drivers/base/power/shutdown.c */
  665. extern void device_shutdown(void);
  666. /* debugging and troubleshooting/diagnostic helpers. */
  667. extern const char *dev_driver_string(const struct device *dev);
  668. #ifdef CONFIG_PRINTK
  669. extern int dev_printk(const char *level, const struct device *dev,
  670. const char *fmt, ...)
  671. __attribute__ ((format (printf, 3, 4)));
  672. extern int dev_emerg(const struct device *dev, const char *fmt, ...)
  673. __attribute__ ((format (printf, 2, 3)));
  674. extern int dev_alert(const struct device *dev, const char *fmt, ...)
  675. __attribute__ ((format (printf, 2, 3)));
  676. extern int dev_crit(const struct device *dev, const char *fmt, ...)
  677. __attribute__ ((format (printf, 2, 3)));
  678. extern int dev_err(const struct device *dev, const char *fmt, ...)
  679. __attribute__ ((format (printf, 2, 3)));
  680. extern int dev_warn(const struct device *dev, const char *fmt, ...)
  681. __attribute__ ((format (printf, 2, 3)));
  682. extern int dev_notice(const struct device *dev, const char *fmt, ...)
  683. __attribute__ ((format (printf, 2, 3)));
  684. extern int _dev_info(const struct device *dev, const char *fmt, ...)
  685. __attribute__ ((format (printf, 2, 3)));
  686. #else
  687. static inline int dev_printk(const char *level, const struct device *dev,
  688. const char *fmt, ...)
  689. __attribute__ ((format (printf, 3, 4)));
  690. static inline int dev_printk(const char *level, const struct device *dev,
  691. const char *fmt, ...)
  692. { return 0; }
  693. static inline int dev_emerg(const struct device *dev, const char *fmt, ...)
  694. __attribute__ ((format (printf, 2, 3)));
  695. static inline int dev_emerg(const struct device *dev, const char *fmt, ...)
  696. { return 0; }
  697. static inline int dev_crit(const struct device *dev, const char *fmt, ...)
  698. __attribute__ ((format (printf, 2, 3)));
  699. static inline int dev_crit(const struct device *dev, const char *fmt, ...)
  700. { return 0; }
  701. static inline int dev_alert(const struct device *dev, const char *fmt, ...)
  702. __attribute__ ((format (printf, 2, 3)));
  703. static inline int dev_alert(const struct device *dev, const char *fmt, ...)
  704. { return 0; }
  705. static inline int dev_err(const struct device *dev, const char *fmt, ...)
  706. __attribute__ ((format (printf, 2, 3)));
  707. static inline int dev_err(const struct device *dev, const char *fmt, ...)
  708. { return 0; }
  709. static inline int dev_warn(const struct device *dev, const char *fmt, ...)
  710. __attribute__ ((format (printf, 2, 3)));
  711. static inline int dev_warn(const struct device *dev, const char *fmt, ...)
  712. { return 0; }
  713. static inline int dev_notice(const struct device *dev, const char *fmt, ...)
  714. __attribute__ ((format (printf, 2, 3)));
  715. static inline int dev_notice(const struct device *dev, const char *fmt, ...)
  716. { return 0; }
  717. static inline int _dev_info(const struct device *dev, const char *fmt, ...)
  718. __attribute__ ((format (printf, 2, 3)));
  719. static inline int _dev_info(const struct device *dev, const char *fmt, ...)
  720. { return 0; }
  721. #endif
  722. /*
  723. * Stupid hackaround for existing uses of non-printk uses dev_info
  724. *
  725. * Note that the definition of dev_info below is actually _dev_info
  726. * and a macro is used to avoid redefining dev_info
  727. */
  728. #define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
  729. #if defined(DEBUG)
  730. #define dev_dbg(dev, format, arg...) \
  731. dev_printk(KERN_DEBUG, dev, format, ##arg)
  732. #elif defined(CONFIG_DYNAMIC_DEBUG)
  733. #define dev_dbg(dev, format, ...) \
  734. do { \
  735. dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
  736. } while (0)
  737. #else
  738. #define dev_dbg(dev, format, arg...) \
  739. ({ \
  740. if (0) \
  741. dev_printk(KERN_DEBUG, dev, format, ##arg); \
  742. 0; \
  743. })
  744. #endif
  745. #ifdef VERBOSE_DEBUG
  746. #define dev_vdbg dev_dbg
  747. #else
  748. #define dev_vdbg(dev, format, arg...) \
  749. ({ \
  750. if (0) \
  751. dev_printk(KERN_DEBUG, dev, format, ##arg); \
  752. 0; \
  753. })
  754. #endif
  755. /*
  756. * dev_WARN*() acts like dev_printk(), but with the key difference
  757. * of using a WARN/WARN_ON to get the message out, including the
  758. * file/line information and a backtrace.
  759. */
  760. #define dev_WARN(dev, format, arg...) \
  761. WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
  762. #define dev_WARN_ONCE(dev, condition, format, arg...) \
  763. WARN_ONCE(condition, "Device %s\n" format, \
  764. dev_driver_string(dev), ## arg)
  765. /* Create alias, so I can be autoloaded. */
  766. #define MODULE_ALIAS_CHARDEV(major,minor) \
  767. MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
  768. #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
  769. MODULE_ALIAS("char-major-" __stringify(major) "-*")
  770. #ifdef CONFIG_SYSFS_DEPRECATED
  771. extern long sysfs_deprecated;
  772. #else
  773. #define sysfs_deprecated 0
  774. #endif
  775. #endif /* _DEVICE_H_ */