device.h 31 KB

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