class.c 9.7 KB

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