class.c 9.9 KB

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