class.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * The class-specific portions of the driver model
  4. *
  5. * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
  6. * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
  7. * Copyright (c) 2008-2009 Novell Inc.
  8. * Copyright (c) 2012-2019 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  9. * Copyright (c) 2012-2019 Linux Foundation
  10. *
  11. * See Documentation/driver-api/driver-model/ for more information.
  12. */
  13. #ifndef _DEVICE_CLASS_H_
  14. #define _DEVICE_CLASS_H_
  15. #include <linux/kobject.h>
  16. #include <linux/klist.h>
  17. #include <linux/pm.h>
  18. #include <linux/device/bus.h>
  19. #include <linux/android_kabi.h>
  20. struct device;
  21. struct fwnode_handle;
  22. /**
  23. * struct class - device classes
  24. * @name: Name of the class.
  25. * @owner: The module owner.
  26. * @class_groups: Default attributes of this class.
  27. * @dev_groups: Default attributes of the devices that belong to the class.
  28. * @dev_kobj: The kobject that represents this class and links it into the hierarchy.
  29. * @dev_uevent: Called when a device is added, removed from this class, or a
  30. * few other things that generate uevents to add the environment
  31. * variables.
  32. * @devnode: Callback to provide the devtmpfs.
  33. * @class_release: Called to release this class.
  34. * @dev_release: Called to release the device.
  35. * @shutdown_pre: Called at shut-down time before driver shutdown.
  36. * @ns_type: Callbacks so sysfs can detemine namespaces.
  37. * @namespace: Namespace of the device belongs to this class.
  38. * @get_ownership: Allows class to specify uid/gid of the sysfs directories
  39. * for the devices belonging to the class. Usually tied to
  40. * device's namespace.
  41. * @pm: The default device power management operations of this class.
  42. * @p: The private data of the driver core, no one other than the
  43. * driver core can touch this.
  44. *
  45. * A class is a higher-level view of a device that abstracts out low-level
  46. * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
  47. * at the class level, they are all simply disks. Classes allow user space
  48. * to work with devices based on what they do, rather than how they are
  49. * connected or how they work.
  50. */
  51. struct class {
  52. const char *name;
  53. struct module *owner;
  54. const struct attribute_group **class_groups;
  55. const struct attribute_group **dev_groups;
  56. struct kobject *dev_kobj;
  57. int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
  58. char *(*devnode)(struct device *dev, umode_t *mode);
  59. void (*class_release)(struct class *class);
  60. void (*dev_release)(struct device *dev);
  61. int (*shutdown_pre)(struct device *dev);
  62. const struct kobj_ns_type_operations *ns_type;
  63. const void *(*namespace)(struct device *dev);
  64. void (*get_ownership)(struct device *dev, kuid_t *uid, kgid_t *gid);
  65. const struct dev_pm_ops *pm;
  66. struct subsys_private *p;
  67. ANDROID_KABI_RESERVE(1);
  68. ANDROID_KABI_RESERVE(2);
  69. ANDROID_KABI_RESERVE(3);
  70. ANDROID_KABI_RESERVE(4);
  71. };
  72. struct class_dev_iter {
  73. struct klist_iter ki;
  74. const struct device_type *type;
  75. };
  76. extern struct kobject *sysfs_dev_block_kobj;
  77. extern struct kobject *sysfs_dev_char_kobj;
  78. extern int __must_check __class_register(struct class *class,
  79. struct lock_class_key *key);
  80. extern void class_unregister(struct class *class);
  81. /* This is a #define to keep the compiler from merging different
  82. * instances of the __key variable */
  83. #define class_register(class) \
  84. ({ \
  85. static struct lock_class_key __key; \
  86. __class_register(class, &__key); \
  87. })
  88. struct class_compat;
  89. struct class_compat *class_compat_register(const char *name);
  90. void class_compat_unregister(struct class_compat *cls);
  91. int class_compat_create_link(struct class_compat *cls, struct device *dev,
  92. struct device *device_link);
  93. void class_compat_remove_link(struct class_compat *cls, struct device *dev,
  94. struct device *device_link);
  95. extern void class_dev_iter_init(struct class_dev_iter *iter,
  96. struct class *class,
  97. struct device *start,
  98. const struct device_type *type);
  99. extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
  100. extern void class_dev_iter_exit(struct class_dev_iter *iter);
  101. extern int class_for_each_device(struct class *class, struct device *start,
  102. void *data,
  103. int (*fn)(struct device *dev, void *data));
  104. extern struct device *class_find_device(struct class *class,
  105. struct device *start, const void *data,
  106. int (*match)(struct device *, const void *));
  107. /**
  108. * class_find_device_by_name - device iterator for locating a particular device
  109. * of a specific name.
  110. * @class: class type
  111. * @name: name of the device to match
  112. */
  113. static inline struct device *class_find_device_by_name(struct class *class,
  114. const char *name)
  115. {
  116. return class_find_device(class, NULL, name, device_match_name);
  117. }
  118. /**
  119. * class_find_device_by_of_node : device iterator for locating a particular device
  120. * matching the of_node.
  121. * @class: class type
  122. * @np: of_node of the device to match.
  123. */
  124. static inline struct device *
  125. class_find_device_by_of_node(struct class *class, const struct device_node *np)
  126. {
  127. return class_find_device(class, NULL, np, device_match_of_node);
  128. }
  129. /**
  130. * class_find_device_by_fwnode : device iterator for locating a particular device
  131. * matching the fwnode.
  132. * @class: class type
  133. * @fwnode: fwnode of the device to match.
  134. */
  135. static inline struct device *
  136. class_find_device_by_fwnode(struct class *class,
  137. const struct fwnode_handle *fwnode)
  138. {
  139. return class_find_device(class, NULL, fwnode, device_match_fwnode);
  140. }
  141. /**
  142. * class_find_device_by_devt : device iterator for locating a particular device
  143. * matching the device type.
  144. * @class: class type
  145. * @devt: device type of the device to match.
  146. */
  147. static inline struct device *class_find_device_by_devt(struct class *class,
  148. dev_t devt)
  149. {
  150. return class_find_device(class, NULL, &devt, device_match_devt);
  151. }
  152. #ifdef CONFIG_ACPI
  153. struct acpi_device;
  154. /**
  155. * class_find_device_by_acpi_dev : device iterator for locating a particular
  156. * device matching the ACPI_COMPANION device.
  157. * @class: class type
  158. * @adev: ACPI_COMPANION device to match.
  159. */
  160. static inline struct device *
  161. class_find_device_by_acpi_dev(struct class *class, const struct acpi_device *adev)
  162. {
  163. return class_find_device(class, NULL, adev, device_match_acpi_dev);
  164. }
  165. #else
  166. static inline struct device *
  167. class_find_device_by_acpi_dev(struct class *class, const void *adev)
  168. {
  169. return NULL;
  170. }
  171. #endif
  172. struct class_attribute {
  173. struct attribute attr;
  174. ssize_t (*show)(struct class *class, struct class_attribute *attr,
  175. char *buf);
  176. ssize_t (*store)(struct class *class, struct class_attribute *attr,
  177. const char *buf, size_t count);
  178. };
  179. #define CLASS_ATTR_RW(_name) \
  180. struct class_attribute class_attr_##_name = __ATTR_RW(_name)
  181. #define CLASS_ATTR_RO(_name) \
  182. struct class_attribute class_attr_##_name = __ATTR_RO(_name)
  183. #define CLASS_ATTR_WO(_name) \
  184. struct class_attribute class_attr_##_name = __ATTR_WO(_name)
  185. extern int __must_check class_create_file_ns(struct class *class,
  186. const struct class_attribute *attr,
  187. const void *ns);
  188. extern void class_remove_file_ns(struct class *class,
  189. const struct class_attribute *attr,
  190. const void *ns);
  191. static inline int __must_check class_create_file(struct class *class,
  192. const struct class_attribute *attr)
  193. {
  194. return class_create_file_ns(class, attr, NULL);
  195. }
  196. static inline void class_remove_file(struct class *class,
  197. const struct class_attribute *attr)
  198. {
  199. return class_remove_file_ns(class, attr, NULL);
  200. }
  201. /* Simple class attribute that is just a static string */
  202. struct class_attribute_string {
  203. struct class_attribute attr;
  204. char *str;
  205. };
  206. /* Currently read-only only */
  207. #define _CLASS_ATTR_STRING(_name, _mode, _str) \
  208. { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
  209. #define CLASS_ATTR_STRING(_name, _mode, _str) \
  210. struct class_attribute_string class_attr_##_name = \
  211. _CLASS_ATTR_STRING(_name, _mode, _str)
  212. extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
  213. char *buf);
  214. struct class_interface {
  215. struct list_head node;
  216. struct class *class;
  217. int (*add_dev) (struct device *, struct class_interface *);
  218. void (*remove_dev) (struct device *, struct class_interface *);
  219. };
  220. extern int __must_check class_interface_register(struct class_interface *);
  221. extern void class_interface_unregister(struct class_interface *);
  222. extern struct class * __must_check __class_create(struct module *owner,
  223. const char *name,
  224. struct lock_class_key *key);
  225. extern void class_destroy(struct class *cls);
  226. /* This is a #define to keep the compiler from merging different
  227. * instances of the __key variable */
  228. #define class_create(owner, name) \
  229. ({ \
  230. static struct lock_class_key __key; \
  231. __class_create(owner, name, &__key); \
  232. })
  233. #endif /* _DEVICE_CLASS_H_ */