device.h 35 KB

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