class.c 9.6 KB

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