class.c 10.0 KB

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