class.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. static void klist_class_dev_get(struct klist_node *n)
  117. {
  118. struct device *dev = container_of(n, struct device, knode_class);
  119. get_device(dev);
  120. }
  121. static void klist_class_dev_put(struct klist_node *n)
  122. {
  123. struct device *dev = container_of(n, struct device, knode_class);
  124. put_device(dev);
  125. }
  126. int __class_register(struct class *cls, struct lock_class_key *key)
  127. {
  128. struct class_private *cp;
  129. int error;
  130. pr_debug("device class '%s': registering\n", cls->name);
  131. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  132. if (!cp)
  133. return -ENOMEM;
  134. klist_init(&cp->class_devices, klist_class_dev_get, klist_class_dev_put);
  135. INIT_LIST_HEAD(&cp->class_interfaces);
  136. kset_init(&cp->class_dirs);
  137. __mutex_init(&cp->class_mutex, "struct class mutex", key);
  138. error = kobject_set_name(&cp->class_subsys.kobj, "%s", cls->name);
  139. if (error) {
  140. kfree(cp);
  141. return error;
  142. }
  143. /* set the default /sys/dev directory for devices of this class */
  144. if (!cls->dev_kobj)
  145. cls->dev_kobj = sysfs_dev_char_kobj;
  146. #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK)
  147. /* let the block class directory show up in the root of sysfs */
  148. if (cls != &block_class)
  149. cp->class_subsys.kobj.kset = class_kset;
  150. #else
  151. cp->class_subsys.kobj.kset = class_kset;
  152. #endif
  153. cp->class_subsys.kobj.ktype = &class_ktype;
  154. cp->class = cls;
  155. cls->p = cp;
  156. error = kset_register(&cp->class_subsys);
  157. if (error) {
  158. kfree(cp);
  159. return error;
  160. }
  161. error = add_class_attrs(class_get(cls));
  162. class_put(cls);
  163. return error;
  164. }
  165. EXPORT_SYMBOL_GPL(__class_register);
  166. void class_unregister(struct class *cls)
  167. {
  168. pr_debug("device class '%s': unregistering\n", cls->name);
  169. remove_class_attrs(cls);
  170. kset_unregister(&cls->p->class_subsys);
  171. }
  172. static void class_create_release(struct class *cls)
  173. {
  174. pr_debug("%s called for %s\n", __func__, cls->name);
  175. kfree(cls);
  176. }
  177. /**
  178. * class_create - create a struct class structure
  179. * @owner: pointer to the module that is to "own" this struct class
  180. * @name: pointer to a string for the name of this class.
  181. * @key: the lock_class_key for this class; used by mutex lock debugging
  182. *
  183. * This is used to create a struct class pointer that can then be used
  184. * in calls to device_create().
  185. *
  186. * Note, the pointer created here is to be destroyed when finished by
  187. * making a call to class_destroy().
  188. */
  189. struct class *__class_create(struct module *owner, const char *name,
  190. struct lock_class_key *key)
  191. {
  192. struct class *cls;
  193. int retval;
  194. cls = kzalloc(sizeof(*cls), GFP_KERNEL);
  195. if (!cls) {
  196. retval = -ENOMEM;
  197. goto error;
  198. }
  199. cls->name = name;
  200. cls->owner = owner;
  201. cls->class_release = class_create_release;
  202. retval = __class_register(cls, key);
  203. if (retval)
  204. goto error;
  205. return cls;
  206. error:
  207. kfree(cls);
  208. return ERR_PTR(retval);
  209. }
  210. EXPORT_SYMBOL_GPL(__class_create);
  211. /**
  212. * class_destroy - destroys a struct class structure
  213. * @cls: pointer to the struct class that is to be destroyed
  214. *
  215. * Note, the pointer to be destroyed must have been created with a call
  216. * to class_create().
  217. */
  218. void class_destroy(struct class *cls)
  219. {
  220. if ((cls == NULL) || (IS_ERR(cls)))
  221. return;
  222. class_unregister(cls);
  223. }
  224. #ifdef CONFIG_SYSFS_DEPRECATED
  225. char *make_class_name(const char *name, struct kobject *kobj)
  226. {
  227. char *class_name;
  228. int size;
  229. size = strlen(name) + strlen(kobject_name(kobj)) + 2;
  230. class_name = kmalloc(size, GFP_KERNEL);
  231. if (!class_name)
  232. return NULL;
  233. strcpy(class_name, name);
  234. strcat(class_name, ":");
  235. strcat(class_name, kobject_name(kobj));
  236. return class_name;
  237. }
  238. #endif
  239. /**
  240. * class_dev_iter_init - initialize class device iterator
  241. * @iter: class iterator to initialize
  242. * @class: the class we wanna iterate over
  243. * @start: the device to start iterating from, if any
  244. * @type: device_type of the devices to iterate over, NULL for all
  245. *
  246. * Initialize class iterator @iter such that it iterates over devices
  247. * of @class. If @start is set, the list iteration will start there,
  248. * otherwise if it is NULL, the iteration starts at the beginning of
  249. * the list.
  250. */
  251. void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
  252. struct device *start, const struct device_type *type)
  253. {
  254. struct klist_node *start_knode = NULL;
  255. if (start)
  256. start_knode = &start->knode_class;
  257. klist_iter_init_node(&class->p->class_devices, &iter->ki, start_knode);
  258. iter->type = type;
  259. }
  260. EXPORT_SYMBOL_GPL(class_dev_iter_init);
  261. /**
  262. * class_dev_iter_next - iterate to the next device
  263. * @iter: class iterator to proceed
  264. *
  265. * Proceed @iter to the next device and return it. Returns NULL if
  266. * iteration is complete.
  267. *
  268. * The returned device is referenced and won't be released till
  269. * iterator is proceed to the next device or exited. The caller is
  270. * free to do whatever it wants to do with the device including
  271. * calling back into class code.
  272. */
  273. struct device *class_dev_iter_next(struct class_dev_iter *iter)
  274. {
  275. struct klist_node *knode;
  276. struct device *dev;
  277. while (1) {
  278. knode = klist_next(&iter->ki);
  279. if (!knode)
  280. return NULL;
  281. dev = container_of(knode, struct device, knode_class);
  282. if (!iter->type || iter->type == dev->type)
  283. return dev;
  284. }
  285. }
  286. EXPORT_SYMBOL_GPL(class_dev_iter_next);
  287. /**
  288. * class_dev_iter_exit - finish iteration
  289. * @iter: class iterator to finish
  290. *
  291. * Finish an iteration. Always call this function after iteration is
  292. * complete whether the iteration ran till the end or not.
  293. */
  294. void class_dev_iter_exit(struct class_dev_iter *iter)
  295. {
  296. klist_iter_exit(&iter->ki);
  297. }
  298. EXPORT_SYMBOL_GPL(class_dev_iter_exit);
  299. /**
  300. * class_for_each_device - device iterator
  301. * @class: the class we're iterating
  302. * @start: the device to start with in the list, if any.
  303. * @data: data for the callback
  304. * @fn: function to be called for each device
  305. *
  306. * Iterate over @class's list of devices, and call @fn for each,
  307. * passing it @data. If @start is set, the list iteration will start
  308. * there, otherwise if it is NULL, the iteration starts at the
  309. * beginning of the list.
  310. *
  311. * We check the return of @fn each time. If it returns anything
  312. * other than 0, we break out and return that value.
  313. *
  314. * @fn is allowed to do anything including calling back into class
  315. * code. There's no locking restriction.
  316. */
  317. int class_for_each_device(struct class *class, struct device *start,
  318. void *data, int (*fn)(struct device *, void *))
  319. {
  320. struct class_dev_iter iter;
  321. struct device *dev;
  322. int error = 0;
  323. if (!class)
  324. return -EINVAL;
  325. if (!class->p) {
  326. WARN(1, "%s called for class '%s' before it was initialized",
  327. __func__, class->name);
  328. return -EINVAL;
  329. }
  330. class_dev_iter_init(&iter, class, start, NULL);
  331. while ((dev = class_dev_iter_next(&iter))) {
  332. error = fn(dev, data);
  333. if (error)
  334. break;
  335. }
  336. class_dev_iter_exit(&iter);
  337. return error;
  338. }
  339. EXPORT_SYMBOL_GPL(class_for_each_device);
  340. /**
  341. * class_find_device - device iterator for locating a particular device
  342. * @class: the class we're iterating
  343. * @start: Device to begin with
  344. * @data: data for the match function
  345. * @match: function to check device
  346. *
  347. * This is similar to the class_for_each_dev() function above, but it
  348. * returns a reference to a device that is 'found' for later use, as
  349. * determined by the @match callback.
  350. *
  351. * The callback should return 0 if the device doesn't match and non-zero
  352. * if it does. If the callback returns non-zero, this function will
  353. * return to the caller and not iterate over any more devices.
  354. *
  355. * Note, you will need to drop the reference with put_device() after use.
  356. *
  357. * @fn is allowed to do anything including calling back into class
  358. * code. There's no locking restriction.
  359. */
  360. struct device *class_find_device(struct class *class, struct device *start,
  361. void *data,
  362. int (*match)(struct device *, void *))
  363. {
  364. struct class_dev_iter iter;
  365. struct device *dev;
  366. if (!class)
  367. return NULL;
  368. if (!class->p) {
  369. WARN(1, "%s called for class '%s' before it was initialized",
  370. __func__, class->name);
  371. return NULL;
  372. }
  373. class_dev_iter_init(&iter, class, start, NULL);
  374. while ((dev = class_dev_iter_next(&iter))) {
  375. if (match(dev, data)) {
  376. get_device(dev);
  377. break;
  378. }
  379. }
  380. class_dev_iter_exit(&iter);
  381. return dev;
  382. }
  383. EXPORT_SYMBOL_GPL(class_find_device);
  384. int class_interface_register(struct class_interface *class_intf)
  385. {
  386. struct class *parent;
  387. struct class_dev_iter iter;
  388. struct device *dev;
  389. if (!class_intf || !class_intf->class)
  390. return -ENODEV;
  391. parent = class_get(class_intf->class);
  392. if (!parent)
  393. return -EINVAL;
  394. mutex_lock(&parent->p->class_mutex);
  395. list_add_tail(&class_intf->node, &parent->p->class_interfaces);
  396. if (class_intf->add_dev) {
  397. class_dev_iter_init(&iter, parent, NULL, NULL);
  398. while ((dev = class_dev_iter_next(&iter)))
  399. class_intf->add_dev(dev, class_intf);
  400. class_dev_iter_exit(&iter);
  401. }
  402. mutex_unlock(&parent->p->class_mutex);
  403. return 0;
  404. }
  405. void class_interface_unregister(struct class_interface *class_intf)
  406. {
  407. struct class *parent = class_intf->class;
  408. struct class_dev_iter iter;
  409. struct device *dev;
  410. if (!parent)
  411. return;
  412. mutex_lock(&parent->p->class_mutex);
  413. list_del_init(&class_intf->node);
  414. if (class_intf->remove_dev) {
  415. class_dev_iter_init(&iter, parent, NULL, NULL);
  416. while ((dev = class_dev_iter_next(&iter)))
  417. class_intf->remove_dev(dev, class_intf);
  418. class_dev_iter_exit(&iter);
  419. }
  420. mutex_unlock(&parent->p->class_mutex);
  421. class_put(parent);
  422. }
  423. int __init classes_init(void)
  424. {
  425. class_kset = kset_create_and_add("class", NULL, NULL);
  426. if (!class_kset)
  427. return -ENOMEM;
  428. return 0;
  429. }
  430. EXPORT_SYMBOL_GPL(class_create_file);
  431. EXPORT_SYMBOL_GPL(class_remove_file);
  432. EXPORT_SYMBOL_GPL(class_unregister);
  433. EXPORT_SYMBOL_GPL(class_destroy);
  434. EXPORT_SYMBOL_GPL(class_interface_register);
  435. EXPORT_SYMBOL_GPL(class_interface_unregister);