class.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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->children);
  120. INIT_LIST_HEAD(&cls->devices);
  121. INIT_LIST_HEAD(&cls->interfaces);
  122. kset_init(&cls->class_dirs);
  123. init_MUTEX(&cls->sem);
  124. error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name);
  125. if (error)
  126. return error;
  127. #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK)
  128. /* let the block class directory show up in the root of sysfs */
  129. if (cls != &block_class)
  130. cls->subsys.kobj.kset = class_kset;
  131. #else
  132. cls->subsys.kobj.kset = class_kset;
  133. #endif
  134. cls->subsys.kobj.ktype = &class_ktype;
  135. error = kset_register(&cls->subsys);
  136. if (!error) {
  137. error = add_class_attrs(class_get(cls));
  138. class_put(cls);
  139. }
  140. return error;
  141. }
  142. void class_unregister(struct class *cls)
  143. {
  144. pr_debug("device class '%s': unregistering\n", cls->name);
  145. remove_class_attrs(cls);
  146. kset_unregister(&cls->subsys);
  147. }
  148. static void class_create_release(struct class *cls)
  149. {
  150. pr_debug("%s called for %s\n", __func__, cls->name);
  151. kfree(cls);
  152. }
  153. /**
  154. * class_create - create a struct class structure
  155. * @owner: pointer to the module that is to "own" this struct class
  156. * @name: pointer to a string for the name of this class.
  157. *
  158. * This is used to create a struct class pointer that can then be used
  159. * in calls to device_create().
  160. *
  161. * Note, the pointer created here is to be destroyed when finished by
  162. * making a call to class_destroy().
  163. */
  164. struct class *class_create(struct module *owner, const char *name)
  165. {
  166. struct class *cls;
  167. int retval;
  168. cls = kzalloc(sizeof(*cls), GFP_KERNEL);
  169. if (!cls) {
  170. retval = -ENOMEM;
  171. goto error;
  172. }
  173. cls->name = name;
  174. cls->owner = owner;
  175. cls->class_release = class_create_release;
  176. retval = class_register(cls);
  177. if (retval)
  178. goto error;
  179. return cls;
  180. error:
  181. kfree(cls);
  182. return ERR_PTR(retval);
  183. }
  184. /**
  185. * class_destroy - destroys a struct class structure
  186. * @cls: pointer to the struct class that is to be destroyed
  187. *
  188. * Note, the pointer to be destroyed must have been created with a call
  189. * to class_create().
  190. */
  191. void class_destroy(struct class *cls)
  192. {
  193. if ((cls == NULL) || (IS_ERR(cls)))
  194. return;
  195. class_unregister(cls);
  196. }
  197. #ifdef CONFIG_SYSFS_DEPRECATED
  198. char *make_class_name(const char *name, struct kobject *kobj)
  199. {
  200. char *class_name;
  201. int size;
  202. size = strlen(name) + strlen(kobject_name(kobj)) + 2;
  203. class_name = kmalloc(size, GFP_KERNEL);
  204. if (!class_name)
  205. return NULL;
  206. strcpy(class_name, name);
  207. strcat(class_name, ":");
  208. strcat(class_name, kobject_name(kobj));
  209. return class_name;
  210. }
  211. #endif
  212. /**
  213. * class_for_each_device - device iterator
  214. * @class: the class we're iterating
  215. * @data: data for the callback
  216. * @fn: function to be called for each device
  217. *
  218. * Iterate over @class's list of devices, and call @fn for each,
  219. * passing it @data.
  220. *
  221. * We check the return of @fn each time. If it returns anything
  222. * other than 0, we break out and return that value.
  223. *
  224. * Note, we hold class->sem in this function, so it can not be
  225. * re-acquired in @fn, otherwise it will self-deadlocking. For
  226. * example, calls to add or remove class members would be verboten.
  227. */
  228. int class_for_each_device(struct class *class, void *data,
  229. int (*fn)(struct device *, void *))
  230. {
  231. struct device *dev;
  232. int error = 0;
  233. if (!class)
  234. return -EINVAL;
  235. down(&class->sem);
  236. list_for_each_entry(dev, &class->devices, node) {
  237. dev = get_device(dev);
  238. if (dev) {
  239. error = fn(dev, data);
  240. put_device(dev);
  241. } else
  242. error = -ENODEV;
  243. if (error)
  244. break;
  245. }
  246. up(&class->sem);
  247. return error;
  248. }
  249. EXPORT_SYMBOL_GPL(class_for_each_device);
  250. /**
  251. * class_find_device - device iterator for locating a particular device
  252. * @class: the class we're iterating
  253. * @data: data for the match function
  254. * @match: function to check device
  255. *
  256. * This is similar to the class_for_each_dev() function above, but it
  257. * returns a reference to a device that is 'found' for later use, as
  258. * determined by the @match callback.
  259. *
  260. * The callback should return 0 if the device doesn't match and non-zero
  261. * if it does. If the callback returns non-zero, this function will
  262. * return to the caller and not iterate over any more devices.
  263. *
  264. * Note, you will need to drop the reference with put_device() after use.
  265. *
  266. * We hold class->sem in this function, so it can not be
  267. * re-acquired in @match, otherwise it will self-deadlocking. For
  268. * example, calls to add or remove class members would be verboten.
  269. */
  270. struct device *class_find_device(struct class *class, void *data,
  271. int (*match)(struct device *, void *))
  272. {
  273. struct device *dev;
  274. int found = 0;
  275. if (!class)
  276. return NULL;
  277. down(&class->sem);
  278. list_for_each_entry(dev, &class->devices, node) {
  279. dev = get_device(dev);
  280. if (dev) {
  281. if (match(dev, data)) {
  282. found = 1;
  283. break;
  284. } else
  285. put_device(dev);
  286. } else
  287. break;
  288. }
  289. up(&class->sem);
  290. return found ? dev : NULL;
  291. }
  292. EXPORT_SYMBOL_GPL(class_find_device);
  293. int class_interface_register(struct class_interface *class_intf)
  294. {
  295. struct class *parent;
  296. struct device *dev;
  297. if (!class_intf || !class_intf->class)
  298. return -ENODEV;
  299. parent = class_get(class_intf->class);
  300. if (!parent)
  301. return -EINVAL;
  302. down(&parent->sem);
  303. list_add_tail(&class_intf->node, &parent->interfaces);
  304. if (class_intf->add_dev) {
  305. list_for_each_entry(dev, &parent->devices, node)
  306. class_intf->add_dev(dev, class_intf);
  307. }
  308. up(&parent->sem);
  309. return 0;
  310. }
  311. void class_interface_unregister(struct class_interface *class_intf)
  312. {
  313. struct class *parent = class_intf->class;
  314. struct device *dev;
  315. if (!parent)
  316. return;
  317. down(&parent->sem);
  318. list_del_init(&class_intf->node);
  319. if (class_intf->remove_dev) {
  320. list_for_each_entry(dev, &parent->devices, node)
  321. class_intf->remove_dev(dev, class_intf);
  322. }
  323. up(&parent->sem);
  324. class_put(parent);
  325. }
  326. int __init classes_init(void)
  327. {
  328. class_kset = kset_create_and_add("class", NULL, NULL);
  329. if (!class_kset)
  330. return -ENOMEM;
  331. return 0;
  332. }
  333. EXPORT_SYMBOL_GPL(class_create_file);
  334. EXPORT_SYMBOL_GPL(class_remove_file);
  335. EXPORT_SYMBOL_GPL(class_register);
  336. EXPORT_SYMBOL_GPL(class_unregister);
  337. EXPORT_SYMBOL_GPL(class_create);
  338. EXPORT_SYMBOL_GPL(class_destroy);
  339. EXPORT_SYMBOL_GPL(class_interface_register);
  340. EXPORT_SYMBOL_GPL(class_interface_unregister);