device.h 39 KB

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