class.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. * @key: the lock_class_key for this class; used by mutex lock debugging
  172. *
  173. * This is used to create a struct class pointer that can then be used
  174. * in calls to device_create().
  175. *
  176. * Note, the pointer created here is to be destroyed when finished by
  177. * making a call to class_destroy().
  178. */
  179. struct class *__class_create(struct module *owner, const char *name,
  180. struct lock_class_key *key)
  181. {
  182. struct class *cls;
  183. int retval;
  184. cls = kzalloc(sizeof(*cls), GFP_KERNEL);
  185. if (!cls) {
  186. retval = -ENOMEM;
  187. goto error;
  188. }
  189. cls->name = name;
  190. cls->owner = owner;
  191. cls->class_release = class_create_release;
  192. retval = __class_register(cls, key);
  193. if (retval)
  194. goto error;
  195. return cls;
  196. error:
  197. kfree(cls);
  198. return ERR_PTR(retval);
  199. }
  200. EXPORT_SYMBOL_GPL(__class_create);
  201. /**
  202. * class_destroy - destroys a struct class structure
  203. * @cls: pointer to the struct class that is to be destroyed
  204. *
  205. * Note, the pointer to be destroyed must have been created with a call
  206. * to class_create().
  207. */
  208. void class_destroy(struct class *cls)
  209. {
  210. if ((cls == NULL) || (IS_ERR(cls)))
  211. return;
  212. class_unregister(cls);
  213. }
  214. #ifdef CONFIG_SYSFS_DEPRECATED
  215. char *make_class_name(const char *name, struct kobject *kobj)
  216. {
  217. char *class_name;
  218. int size;
  219. size = strlen(name) + strlen(kobject_name(kobj)) + 2;
  220. class_name = kmalloc(size, GFP_KERNEL);
  221. if (!class_name)
  222. return NULL;
  223. strcpy(class_name, name);
  224. strcat(class_name, ":");
  225. strcat(class_name, kobject_name(kobj));
  226. return class_name;
  227. }
  228. #endif
  229. /**
  230. * class_for_each_device - device iterator
  231. * @class: the class we're iterating
  232. * @start: the device to start with in the list, if any.
  233. * @data: data for the callback
  234. * @fn: function to be called for each device
  235. *
  236. * Iterate over @class's list of devices, and call @fn for each,
  237. * passing it @data. If @start is set, the list iteration will start
  238. * there, otherwise if it is NULL, the iteration starts at the
  239. * beginning of the list.
  240. *
  241. * We check the return of @fn each time. If it returns anything
  242. * other than 0, we break out and return that value.
  243. *
  244. * Note, we hold class->class_mutex in this function, so it can not be
  245. * re-acquired in @fn, otherwise it will self-deadlocking. For
  246. * example, calls to add or remove class members would be verboten.
  247. */
  248. int class_for_each_device(struct class *class, struct device *start,
  249. void *data, int (*fn)(struct device *, void *))
  250. {
  251. struct device *dev;
  252. int error = 0;
  253. if (!class)
  254. return -EINVAL;
  255. mutex_lock(&class->p->class_mutex);
  256. list_for_each_entry(dev, &class->p->class_devices, node) {
  257. if (start) {
  258. if (start == dev)
  259. start = NULL;
  260. continue;
  261. }
  262. dev = get_device(dev);
  263. error = fn(dev, data);
  264. put_device(dev);
  265. if (error)
  266. break;
  267. }
  268. mutex_unlock(&class->p->class_mutex);
  269. return error;
  270. }
  271. EXPORT_SYMBOL_GPL(class_for_each_device);
  272. /**
  273. * class_find_device - device iterator for locating a particular device
  274. * @class: the class we're iterating
  275. * @start: Device to begin with
  276. * @data: data for the match function
  277. * @match: function to check device
  278. *
  279. * This is similar to the class_for_each_dev() function above, but it
  280. * returns a reference to a device that is 'found' for later use, as
  281. * determined by the @match callback.
  282. *
  283. * The callback should return 0 if the device doesn't match and non-zero
  284. * if it does. If the callback returns non-zero, this function will
  285. * return to the caller and not iterate over any more devices.
  286. *
  287. * Note, you will need to drop the reference with put_device() after use.
  288. *
  289. * We hold class->class_mutex in this function, so it can not be
  290. * re-acquired in @match, otherwise it will self-deadlocking. For
  291. * example, calls to add or remove class members would be verboten.
  292. */
  293. struct device *class_find_device(struct class *class, struct device *start,
  294. void *data,
  295. int (*match)(struct device *, void *))
  296. {
  297. struct device *dev;
  298. int found = 0;
  299. if (!class)
  300. return NULL;
  301. mutex_lock(&class->p->class_mutex);
  302. list_for_each_entry(dev, &class->p->class_devices, node) {
  303. if (start) {
  304. if (start == dev)
  305. start = NULL;
  306. continue;
  307. }
  308. dev = get_device(dev);
  309. if (match(dev, data)) {
  310. found = 1;
  311. break;
  312. } else
  313. put_device(dev);
  314. }
  315. mutex_unlock(&class->p->class_mutex);
  316. return found ? dev : NULL;
  317. }
  318. EXPORT_SYMBOL_GPL(class_find_device);
  319. int class_interface_register(struct class_interface *class_intf)
  320. {
  321. struct class *parent;
  322. struct device *dev;
  323. if (!class_intf || !class_intf->class)
  324. return -ENODEV;
  325. parent = class_get(class_intf->class);
  326. if (!parent)
  327. return -EINVAL;
  328. mutex_lock(&parent->p->class_mutex);
  329. list_add_tail(&class_intf->node, &parent->p->class_interfaces);
  330. if (class_intf->add_dev) {
  331. list_for_each_entry(dev, &parent->p->class_devices, node)
  332. class_intf->add_dev(dev, class_intf);
  333. }
  334. mutex_unlock(&parent->p->class_mutex);
  335. return 0;
  336. }
  337. void class_interface_unregister(struct class_interface *class_intf)
  338. {
  339. struct class *parent = class_intf->class;
  340. struct device *dev;
  341. if (!parent)
  342. return;
  343. mutex_lock(&parent->p->class_mutex);
  344. list_del_init(&class_intf->node);
  345. if (class_intf->remove_dev) {
  346. list_for_each_entry(dev, &parent->p->class_devices, node)
  347. class_intf->remove_dev(dev, class_intf);
  348. }
  349. mutex_unlock(&parent->p->class_mutex);
  350. class_put(parent);
  351. }
  352. int __init classes_init(void)
  353. {
  354. class_kset = kset_create_and_add("class", NULL, NULL);
  355. if (!class_kset)
  356. return -ENOMEM;
  357. return 0;
  358. }
  359. EXPORT_SYMBOL_GPL(class_create_file);
  360. EXPORT_SYMBOL_GPL(class_remove_file);
  361. EXPORT_SYMBOL_GPL(class_unregister);
  362. EXPORT_SYMBOL_GPL(class_destroy);
  363. EXPORT_SYMBOL_GPL(class_interface_register);
  364. EXPORT_SYMBOL_GPL(class_interface_unregister);