class.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * class.c - basic device class management
  3. *
  4. * Copyright (c) 2002-3 Patrick Mochel
  5. * Copyright (c) 2002-3 Open Source Development Labs
  6. * Copyright (c) 2003-2004 Greg Kroah-Hartman
  7. * Copyright (c) 2003-2004 IBM Corp.
  8. *
  9. * This file is released under the GPLv2
  10. *
  11. */
  12. #include <linux/device.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/string.h>
  16. #include <linux/kdev_t.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include <linux/genhd.h>
  20. #include "base.h"
  21. #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
  22. #define to_class(obj) container_of(obj, struct class, subsys.kobj)
  23. static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
  24. char *buf)
  25. {
  26. struct class_attribute *class_attr = to_class_attr(attr);
  27. struct class *dc = to_class(kobj);
  28. ssize_t ret = -EIO;
  29. if (class_attr->show)
  30. ret = class_attr->show(dc, buf);
  31. return ret;
  32. }
  33. static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
  34. const char *buf, size_t count)
  35. {
  36. struct class_attribute *class_attr = to_class_attr(attr);
  37. struct class *dc = to_class(kobj);
  38. ssize_t ret = -EIO;
  39. if (class_attr->store)
  40. ret = class_attr->store(dc, buf, count);
  41. return ret;
  42. }
  43. static void class_release(struct kobject *kobj)
  44. {
  45. struct class *class = to_class(kobj);
  46. pr_debug("class '%s': release.\n", class->name);
  47. if (class->class_release)
  48. class->class_release(class);
  49. else
  50. pr_debug("class '%s' does not have a release() function, "
  51. "be careful\n", class->name);
  52. }
  53. static struct sysfs_ops class_sysfs_ops = {
  54. .show = class_attr_show,
  55. .store = class_attr_store,
  56. };
  57. static struct kobj_type class_ktype = {
  58. .sysfs_ops = &class_sysfs_ops,
  59. .release = class_release,
  60. };
  61. /* Hotplug events for classes go to the class_obj subsys */
  62. static struct kset *class_kset;
  63. int class_create_file(struct class *cls, const struct class_attribute *attr)
  64. {
  65. int error;
  66. if (cls)
  67. error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
  68. else
  69. error = -EINVAL;
  70. return error;
  71. }
  72. void class_remove_file(struct class *cls, const struct class_attribute *attr)
  73. {
  74. if (cls)
  75. sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
  76. }
  77. static struct class *class_get(struct class *cls)
  78. {
  79. if (cls)
  80. return container_of(kset_get(&cls->subsys),
  81. struct class, subsys);
  82. return NULL;
  83. }
  84. static void class_put(struct class *cls)
  85. {
  86. if (cls)
  87. kset_put(&cls->subsys);
  88. }
  89. static int add_class_attrs(struct class *cls)
  90. {
  91. int i;
  92. int error = 0;
  93. if (cls->class_attrs) {
  94. for (i = 0; attr_name(cls->class_attrs[i]); i++) {
  95. error = class_create_file(cls, &cls->class_attrs[i]);
  96. if (error)
  97. goto error;
  98. }
  99. }
  100. done:
  101. return error;
  102. error:
  103. while (--i >= 0)
  104. class_remove_file(cls, &cls->class_attrs[i]);
  105. goto done;
  106. }
  107. static void remove_class_attrs(struct class *cls)
  108. {
  109. int i;
  110. if (cls->class_attrs) {
  111. for (i = 0; attr_name(cls->class_attrs[i]); i++)
  112. class_remove_file(cls, &cls->class_attrs[i]);
  113. }
  114. }
  115. int class_register(struct class *cls)
  116. {
  117. int error;
  118. pr_debug("device class '%s': registering\n", cls->name);
  119. INIT_LIST_HEAD(&cls->devices);
  120. INIT_LIST_HEAD(&cls->interfaces);
  121. kset_init(&cls->class_dirs);
  122. init_MUTEX(&cls->sem);
  123. error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name);
  124. if (error)
  125. return error;
  126. #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK)
  127. /* let the block class directory show up in the root of sysfs */
  128. if (cls != &block_class)
  129. cls->subsys.kobj.kset = class_kset;
  130. #else
  131. cls->subsys.kobj.kset = class_kset;
  132. #endif
  133. cls->subsys.kobj.ktype = &class_ktype;
  134. error = kset_register(&cls->subsys);
  135. if (!error) {
  136. error = add_class_attrs(class_get(cls));
  137. class_put(cls);
  138. }
  139. return error;
  140. }
  141. void class_unregister(struct class *cls)
  142. {
  143. pr_debug("device class '%s': unregistering\n", cls->name);
  144. remove_class_attrs(cls);
  145. kset_unregister(&cls->subsys);
  146. }
  147. static void class_create_release(struct class *cls)
  148. {
  149. pr_debug("%s called for %s\n", __func__, cls->name);
  150. kfree(cls);
  151. }
  152. /**
  153. * class_create - create a struct class structure
  154. * @owner: pointer to the module that is to "own" this struct class
  155. * @name: pointer to a string for the name of this class.
  156. *
  157. * This is used to create a struct class pointer that can then be used
  158. * in calls to device_create().
  159. *
  160. * Note, the pointer created here is to be destroyed when finished by
  161. * making a call to class_destroy().
  162. */
  163. struct class *class_create(struct module *owner, const char *name)
  164. {
  165. struct class *cls;
  166. int retval;
  167. cls = kzalloc(sizeof(*cls), GFP_KERNEL);
  168. if (!cls) {
  169. retval = -ENOMEM;
  170. goto error;
  171. }
  172. cls->name = name;
  173. cls->owner = owner;
  174. cls->class_release = class_create_release;
  175. retval = class_register(cls);
  176. if (retval)
  177. goto error;
  178. return cls;
  179. error:
  180. kfree(cls);
  181. return ERR_PTR(retval);
  182. }
  183. /**
  184. * class_destroy - destroys a struct class structure
  185. * @cls: pointer to the struct class that is to be destroyed
  186. *
  187. * Note, the pointer to be destroyed must have been created with a call
  188. * to class_create().
  189. */
  190. void class_destroy(struct class *cls)
  191. {
  192. if ((cls == NULL) || (IS_ERR(cls)))
  193. return;
  194. class_unregister(cls);
  195. }
  196. #ifdef CONFIG_SYSFS_DEPRECATED
  197. char *make_class_name(const char *name, struct kobject *kobj)
  198. {
  199. char *class_name;
  200. int size;
  201. size = strlen(name) + strlen(kobject_name(kobj)) + 2;
  202. class_name = kmalloc(size, GFP_KERNEL);
  203. if (!class_name)
  204. return NULL;
  205. strcpy(class_name, name);
  206. strcat(class_name, ":");
  207. strcat(class_name, kobject_name(kobj));
  208. return class_name;
  209. }
  210. #endif
  211. /**
  212. * class_for_each_device - device iterator
  213. * @class: the class we're iterating
  214. * @data: data for the callback
  215. * @fn: function to be called for each device
  216. *
  217. * Iterate over @class's list of devices, and call @fn for each,
  218. * passing it @data.
  219. *
  220. * We check the return of @fn each time. If it returns anything
  221. * other than 0, we break out and return that value.
  222. *
  223. * Note, we hold class->sem in this function, so it can not be
  224. * re-acquired in @fn, otherwise it will self-deadlocking. For
  225. * example, calls to add or remove class members would be verboten.
  226. */
  227. int class_for_each_device(struct class *class, void *data,
  228. int (*fn)(struct device *, void *))
  229. {
  230. struct device *dev;
  231. int error = 0;
  232. if (!class)
  233. return -EINVAL;
  234. down(&class->sem);
  235. list_for_each_entry(dev, &class->devices, node) {
  236. dev = get_device(dev);
  237. if (dev) {
  238. error = fn(dev, data);
  239. put_device(dev);
  240. } else
  241. error = -ENODEV;
  242. if (error)
  243. break;
  244. }
  245. up(&class->sem);
  246. return error;
  247. }
  248. EXPORT_SYMBOL_GPL(class_for_each_device);
  249. /**
  250. * class_find_device - device iterator for locating a particular device
  251. * @class: the class we're iterating
  252. * @data: data for the match function
  253. * @match: function to check device
  254. *
  255. * This is similar to the class_for_each_dev() function above, but it
  256. * returns a reference to a device that is 'found' for later use, as
  257. * determined by the @match callback.
  258. *
  259. * The callback should return 0 if the device doesn't match and non-zero
  260. * if it does. If the callback returns non-zero, this function will
  261. * return to the caller and not iterate over any more devices.
  262. *
  263. * Note, you will need to drop the reference with put_device() after use.
  264. *
  265. * We hold class->sem in this function, so it can not be
  266. * re-acquired in @match, otherwise it will self-deadlocking. For
  267. * example, calls to add or remove class members would be verboten.
  268. */
  269. struct device *class_find_device(struct class *class, void *data,
  270. int (*match)(struct device *, void *))
  271. {
  272. struct device *dev;
  273. int found = 0;
  274. if (!class)
  275. return NULL;
  276. down(&class->sem);
  277. list_for_each_entry(dev, &class->devices, node) {
  278. dev = get_device(dev);
  279. if (dev) {
  280. if (match(dev, data)) {
  281. found = 1;
  282. break;
  283. } else
  284. put_device(dev);
  285. } else
  286. break;
  287. }
  288. up(&class->sem);
  289. return found ? dev : NULL;
  290. }
  291. EXPORT_SYMBOL_GPL(class_find_device);
  292. int class_interface_register(struct class_interface *class_intf)
  293. {
  294. struct class *parent;
  295. struct device *dev;
  296. if (!class_intf || !class_intf->class)
  297. return -ENODEV;
  298. parent = class_get(class_intf->class);
  299. if (!parent)
  300. return -EINVAL;
  301. down(&parent->sem);
  302. list_add_tail(&class_intf->node, &parent->interfaces);
  303. if (class_intf->add_dev) {
  304. list_for_each_entry(dev, &parent->devices, node)
  305. class_intf->add_dev(dev, class_intf);
  306. }
  307. up(&parent->sem);
  308. return 0;
  309. }
  310. void class_interface_unregister(struct class_interface *class_intf)
  311. {
  312. struct class *parent = class_intf->class;
  313. struct device *dev;
  314. if (!parent)
  315. return;
  316. down(&parent->sem);
  317. list_del_init(&class_intf->node);
  318. if (class_intf->remove_dev) {
  319. list_for_each_entry(dev, &parent->devices, node)
  320. class_intf->remove_dev(dev, class_intf);
  321. }
  322. up(&parent->sem);
  323. class_put(parent);
  324. }
  325. int __init classes_init(void)
  326. {
  327. class_kset = kset_create_and_add("class", NULL, NULL);
  328. if (!class_kset)
  329. return -ENOMEM;
  330. return 0;
  331. }
  332. EXPORT_SYMBOL_GPL(class_create_file);
  333. EXPORT_SYMBOL_GPL(class_remove_file);
  334. EXPORT_SYMBOL_GPL(class_register);
  335. EXPORT_SYMBOL_GPL(class_unregister);
  336. EXPORT_SYMBOL_GPL(class_create);
  337. EXPORT_SYMBOL_GPL(class_destroy);
  338. EXPORT_SYMBOL_GPL(class_interface_register);
  339. EXPORT_SYMBOL_GPL(class_interface_unregister);