device.h 36 KB

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