device.h 37 KB

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