device.h 35 KB

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