device.h 39 KB

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