class.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * class.c - basic device class management
  4. *
  5. * Copyright (c) 2002-3 Patrick Mochel
  6. * Copyright (c) 2002-3 Open Source Development Labs
  7. * Copyright (c) 2003-2004 Greg Kroah-Hartman
  8. * Copyright (c) 2003-2004 IBM Corp.
  9. *
  10. * This file is released under the GPLv2
  11. *
  12. */
  13. #include <linux/device.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/string.h>
  17. #include <linux/kdev_t.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/genhd.h>
  21. #include <linux/mutex.h>
  22. #include "base.h"
  23. #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
  24. static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
  25. char *buf)
  26. {
  27. struct class_attribute *class_attr = to_class_attr(attr);
  28. struct subsys_private *cp = to_subsys_private(kobj);
  29. ssize_t ret = -EIO;
  30. if (class_attr->show)
  31. ret = class_attr->show(cp->class, class_attr, buf);
  32. return ret;
  33. }
  34. static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
  35. const char *buf, size_t count)
  36. {
  37. struct class_attribute *class_attr = to_class_attr(attr);
  38. struct subsys_private *cp = to_subsys_private(kobj);
  39. ssize_t ret = -EIO;
  40. if (class_attr->store)
  41. ret = class_attr->store(cp->class, class_attr, buf, count);
  42. return ret;
  43. }
  44. static void class_release(struct kobject *kobj)
  45. {
  46. struct subsys_private *cp = to_subsys_private(kobj);
  47. struct class *class = cp->class;
  48. pr_debug("class '%s': release.\n", class->name);
  49. if (class->class_release)
  50. class->class_release(class);
  51. else
  52. pr_debug("class '%s' does not have a release() function, "
  53. "be careful\n", class->name);
  54. kfree(cp);
  55. }
  56. static const struct kobj_ns_type_operations *class_child_ns_type(struct kobject *kobj)
  57. {
  58. struct subsys_private *cp = to_subsys_private(kobj);
  59. struct class *class = cp->class;
  60. return class->ns_type;
  61. }
  62. static const struct sysfs_ops class_sysfs_ops = {
  63. .show = class_attr_show,
  64. .store = class_attr_store,
  65. };
  66. static struct kobj_type class_ktype = {
  67. .sysfs_ops = &class_sysfs_ops,
  68. .release = class_release,
  69. .child_ns_type = class_child_ns_type,
  70. };
  71. /* Hotplug events for classes go to the class subsys */
  72. static struct kset *class_kset;
  73. int class_create_file_ns(struct class *cls, const struct class_attribute *attr,
  74. const void *ns)
  75. {
  76. int error;
  77. if (cls)
  78. error = sysfs_create_file_ns(&cls->p->subsys.kobj,
  79. &attr->attr, ns);
  80. else
  81. error = -EINVAL;
  82. return error;
  83. }
  84. void class_remove_file_ns(struct class *cls, const struct class_attribute *attr,
  85. const void *ns)
  86. {
  87. if (cls)
  88. sysfs_remove_file_ns(&cls->p->subsys.kobj, &attr->attr, ns);
  89. }
  90. static struct class *class_get(struct class *cls)
  91. {
  92. if (cls)
  93. kset_get(&cls->p->subsys);
  94. return cls;
  95. }
  96. static void class_put(struct class *cls)
  97. {
  98. if (cls)
  99. kset_put(&cls->p->subsys);
  100. }
  101. static void klist_class_dev_get(struct klist_node *n)
  102. {
  103. struct device *dev = container_of(n, struct device, knode_class);
  104. get_device(dev);
  105. }
  106. static void klist_class_dev_put(struct klist_node *n)
  107. {
  108. struct device *dev = container_of(n, struct device, knode_class);
  109. put_device(dev);
  110. }
  111. static int class_add_groups(struct class *cls,
  112. const struct attribute_group **groups)
  113. {
  114. return sysfs_create_groups(&cls->p->subsys.kobj, groups);
  115. }
  116. static void class_remove_groups(struct class *cls,
  117. const struct attribute_group **groups)
  118. {
  119. return sysfs_remove_groups(&cls->p->subsys.kobj, groups);
  120. }
  121. int __class_register(struct class *cls, struct lock_class_key *key)
  122. {
  123. struct subsys_private *cp;
  124. int error;
  125. pr_debug("device class '%s': registering\n", cls->name);
  126. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  127. if (!cp)
  128. return -ENOMEM;
  129. klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put);
  130. INIT_LIST_HEAD(&cp->interfaces);
  131. kset_init(&cp->glue_dirs);
  132. __mutex_init(&cp->mutex, "subsys mutex", key);
  133. error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name);
  134. if (error) {
  135. kfree(cp);
  136. return error;
  137. }
  138. /* set the default /sys/dev directory for devices of this class */
  139. if (!cls->dev_kobj)
  140. cls->dev_kobj = sysfs_dev_char_kobj;
  141. #if defined(CONFIG_BLOCK)
  142. /* let the block class directory show up in the root of sysfs */
  143. if (!sysfs_deprecated || cls != &block_class)
  144. cp->subsys.kobj.kset = class_kset;
  145. #else
  146. cp->subsys.kobj.kset = class_kset;
  147. #endif
  148. cp->subsys.kobj.ktype = &class_ktype;
  149. cp->class = cls;
  150. cls->p = cp;
  151. error = kset_register(&cp->subsys);
  152. if (error) {
  153. kfree(cp);
  154. return error;
  155. }
  156. error = class_add_groups(class_get(cls), cls->class_groups);
  157. class_put(cls);
  158. return error;
  159. }
  160. EXPORT_SYMBOL_GPL(__class_register);
  161. void class_unregister(struct class *cls)
  162. {
  163. pr_debug("device class '%s': unregistering\n", cls->name);
  164. class_remove_groups(cls, cls->class_groups);
  165. kset_unregister(&cls->p->subsys);
  166. }
  167. static void class_create_release(struct class *cls)
  168. {
  169. pr_debug("%s called for %s\n", __func__, cls->name);
  170. kfree(cls);
  171. }
  172. /**
  173. * class_create - create a struct class structure
  174. * @owner: pointer to the module that is to "own" this struct class
  175. * @name: pointer to a string for the name of this class.
  176. * @key: the lock_class_key for this class; used by mutex lock debugging
  177. *
  178. * This is used to create a struct class pointer that can then be used
  179. * in calls to device_create().
  180. *
  181. * Returns &struct class pointer on success, or ERR_PTR() on error.
  182. *
  183. * Note, the pointer created here is to be destroyed when finished by
  184. * making a call to class_destroy().
  185. */
  186. struct class *__class_create(struct module *owner, const char *name,
  187. struct lock_class_key *key)
  188. {
  189. struct class *cls;
  190. int retval;
  191. cls = kzalloc(sizeof(*cls), GFP_KERNEL);
  192. if (!cls) {
  193. retval = -ENOMEM;
  194. goto error;
  195. }
  196. cls->name = name;
  197. cls->owner = owner;
  198. cls->class_release = class_create_release;
  199. retval = __class_register(cls, key);
  200. if (retval)
  201. goto error;
  202. return cls;
  203. error:
  204. kfree(cls);
  205. return ERR_PTR(retval);
  206. }
  207. EXPORT_SYMBOL_GPL(__class_create);
  208. /**
  209. * class_destroy - destroys a struct class structure
  210. * @cls: pointer to the struct class that is to be destroyed
  211. *
  212. * Note, the pointer to be destroyed must have been created with a call
  213. * to class_create().
  214. */
  215. void class_destroy(struct class *cls)
  216. {
  217. if ((cls == NULL) || (IS_ERR(cls)))
  218. return;
  219. class_unregister(cls);
  220. }
  221. /**
  222. * class_dev_iter_init - initialize class device iterator
  223. * @iter: class iterator to initialize
  224. * @class: the class we wanna iterate over
  225. * @start: the device to start iterating from, if any
  226. * @type: device_type of the devices to iterate over, NULL for all
  227. *
  228. * Initialize class iterator @iter such that it iterates over devices
  229. * of @class. If @start is set, the list iteration will start there,
  230. * otherwise if it is NULL, the iteration starts at the beginning of
  231. * the list.
  232. */
  233. void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
  234. struct device *start, const struct device_type *type)
  235. {
  236. struct klist_node *start_knode = NULL;
  237. if (start)
  238. start_knode = &start->knode_class;
  239. klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
  240. iter->type = type;
  241. }
  242. EXPORT_SYMBOL_GPL(class_dev_iter_init);
  243. /**
  244. * class_dev_iter_next - iterate to the next device
  245. * @iter: class iterator to proceed
  246. *
  247. * Proceed @iter to the next device and return it. Returns NULL if
  248. * iteration is complete.
  249. *
  250. * The returned device is referenced and won't be released till
  251. * iterator is proceed to the next device or exited. The caller is
  252. * free to do whatever it wants to do with the device including
  253. * calling back into class code.
  254. */
  255. struct device *class_dev_iter_next(struct class_dev_iter *iter)
  256. {
  257. struct klist_node *knode;
  258. struct device *dev;
  259. while (1) {
  260. knode = klist_next(&iter->ki);
  261. if (!knode)
  262. return NULL;
  263. dev = container_of(knode, struct device, knode_class);
  264. if (!iter->type || iter->type == dev->type)
  265. return dev;
  266. }
  267. }
  268. EXPORT_SYMBOL_GPL(class_dev_iter_next);
  269. /**
  270. * class_dev_iter_exit - finish iteration
  271. * @iter: class iterator to finish
  272. *
  273. * Finish an iteration. Always call this function after iteration is
  274. * complete whether the iteration ran till the end or not.
  275. */
  276. void class_dev_iter_exit(struct class_dev_iter *iter)
  277. {
  278. klist_iter_exit(&iter->ki);
  279. }
  280. EXPORT_SYMBOL_GPL(class_dev_iter_exit);
  281. /**
  282. * class_for_each_device - device iterator
  283. * @class: the class we're iterating
  284. * @start: the device to start with in the list, if any.
  285. * @data: data for the callback
  286. * @fn: function to be called for each device
  287. *
  288. * Iterate over @class's list of devices, and call @fn for each,
  289. * passing it @data. If @start is set, the list iteration will start
  290. * there, otherwise if it is NULL, the iteration starts at the
  291. * beginning of the list.
  292. *
  293. * We check the return of @fn each time. If it returns anything
  294. * other than 0, we break out and return that value.
  295. *
  296. * @fn is allowed to do anything including calling back into class
  297. * code. There's no locking restriction.
  298. */
  299. int class_for_each_device(struct class *class, struct device *start,
  300. void *data, int (*fn)(struct device *, void *))
  301. {
  302. struct class_dev_iter iter;
  303. struct device *dev;
  304. int error = 0;
  305. if (!class)
  306. return -EINVAL;
  307. if (!class->p) {
  308. WARN(1, "%s called for class '%s' before it was initialized",
  309. __func__, class->name);
  310. return -EINVAL;
  311. }
  312. class_dev_iter_init(&iter, class, start, NULL);
  313. while ((dev = class_dev_iter_next(&iter))) {
  314. error = fn(dev, data);
  315. if (error)
  316. break;
  317. }
  318. class_dev_iter_exit(&iter);
  319. return error;
  320. }
  321. EXPORT_SYMBOL_GPL(class_for_each_device);
  322. /**
  323. * class_find_device - device iterator for locating a particular device
  324. * @class: the class we're iterating
  325. * @start: Device to begin with
  326. * @data: data for the match function
  327. * @match: function to check device
  328. *
  329. * This is similar to the class_for_each_dev() function above, but it
  330. * returns a reference to a device that is 'found' for later use, as
  331. * determined by the @match callback.
  332. *
  333. * The callback should return 0 if the device doesn't match and non-zero
  334. * if it does. If the callback returns non-zero, this function will
  335. * return to the caller and not iterate over any more devices.
  336. *
  337. * Note, you will need to drop the reference with put_device() after use.
  338. *
  339. * @match is allowed to do anything including calling back into class
  340. * code. There's no locking restriction.
  341. */
  342. struct device *class_find_device(struct class *class, struct device *start,
  343. const void *data,
  344. int (*match)(struct device *, const void *))
  345. {
  346. struct class_dev_iter iter;
  347. struct device *dev;
  348. if (!class)
  349. return NULL;
  350. if (!class->p) {
  351. WARN(1, "%s called for class '%s' before it was initialized",
  352. __func__, class->name);
  353. return NULL;
  354. }
  355. class_dev_iter_init(&iter, class, start, NULL);
  356. while ((dev = class_dev_iter_next(&iter))) {
  357. if (match(dev, data)) {
  358. get_device(dev);
  359. break;
  360. }
  361. }
  362. class_dev_iter_exit(&iter);
  363. return dev;
  364. }
  365. EXPORT_SYMBOL_GPL(class_find_device);
  366. int class_interface_register(struct class_interface *class_intf)
  367. {
  368. struct class *parent;
  369. struct class_dev_iter iter;
  370. struct device *dev;
  371. if (!class_intf || !class_intf->class)
  372. return -ENODEV;
  373. parent = class_get(class_intf->class);
  374. if (!parent)
  375. return -EINVAL;
  376. mutex_lock(&parent->p->mutex);
  377. list_add_tail(&class_intf->node, &parent->p->interfaces);
  378. if (class_intf->add_dev) {
  379. class_dev_iter_init(&iter, parent, NULL, NULL);
  380. while ((dev = class_dev_iter_next(&iter)))
  381. class_intf->add_dev(dev, class_intf);
  382. class_dev_iter_exit(&iter);
  383. }
  384. mutex_unlock(&parent->p->mutex);
  385. return 0;
  386. }
  387. void class_interface_unregister(struct class_interface *class_intf)
  388. {
  389. struct class *parent = class_intf->class;
  390. struct class_dev_iter iter;
  391. struct device *dev;
  392. if (!parent)
  393. return;
  394. mutex_lock(&parent->p->mutex);
  395. list_del_init(&class_intf->node);
  396. if (class_intf->remove_dev) {
  397. class_dev_iter_init(&iter, parent, NULL, NULL);
  398. while ((dev = class_dev_iter_next(&iter)))
  399. class_intf->remove_dev(dev, class_intf);
  400. class_dev_iter_exit(&iter);
  401. }
  402. mutex_unlock(&parent->p->mutex);
  403. class_put(parent);
  404. }
  405. ssize_t show_class_attr_string(struct class *class,
  406. struct class_attribute *attr, char *buf)
  407. {
  408. struct class_attribute_string *cs;
  409. cs = container_of(attr, struct class_attribute_string, attr);
  410. return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
  411. }
  412. EXPORT_SYMBOL_GPL(show_class_attr_string);
  413. struct class_compat {
  414. struct kobject *kobj;
  415. };
  416. /**
  417. * class_compat_register - register a compatibility class
  418. * @name: the name of the class
  419. *
  420. * Compatibility class are meant as a temporary user-space compatibility
  421. * workaround when converting a family of class devices to a bus devices.
  422. */
  423. struct class_compat *class_compat_register(const char *name)
  424. {
  425. struct class_compat *cls;
  426. cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL);
  427. if (!cls)
  428. return NULL;
  429. cls->kobj = kobject_create_and_add(name, &class_kset->kobj);
  430. if (!cls->kobj) {
  431. kfree(cls);
  432. return NULL;
  433. }
  434. return cls;
  435. }
  436. EXPORT_SYMBOL_GPL(class_compat_register);
  437. /**
  438. * class_compat_unregister - unregister a compatibility class
  439. * @cls: the class to unregister
  440. */
  441. void class_compat_unregister(struct class_compat *cls)
  442. {
  443. kobject_put(cls->kobj);
  444. kfree(cls);
  445. }
  446. EXPORT_SYMBOL_GPL(class_compat_unregister);
  447. /**
  448. * class_compat_create_link - create a compatibility class device link to
  449. * a bus device
  450. * @cls: the compatibility class
  451. * @dev: the target bus device
  452. * @device_link: an optional device to which a "device" link should be created
  453. */
  454. int class_compat_create_link(struct class_compat *cls, struct device *dev,
  455. struct device *device_link)
  456. {
  457. int error;
  458. error = sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev));
  459. if (error)
  460. return error;
  461. /*
  462. * Optionally add a "device" link (typically to the parent), as a
  463. * class device would have one and we want to provide as much
  464. * backwards compatibility as possible.
  465. */
  466. if (device_link) {
  467. error = sysfs_create_link(&dev->kobj, &device_link->kobj,
  468. "device");
  469. if (error)
  470. sysfs_remove_link(cls->kobj, dev_name(dev));
  471. }
  472. return error;
  473. }
  474. EXPORT_SYMBOL_GPL(class_compat_create_link);
  475. /**
  476. * class_compat_remove_link - remove a compatibility class device link to
  477. * a bus device
  478. * @cls: the compatibility class
  479. * @dev: the target bus device
  480. * @device_link: an optional device to which a "device" link was previously
  481. * created
  482. */
  483. void class_compat_remove_link(struct class_compat *cls, struct device *dev,
  484. struct device *device_link)
  485. {
  486. if (device_link)
  487. sysfs_remove_link(&dev->kobj, "device");
  488. sysfs_remove_link(cls->kobj, dev_name(dev));
  489. }
  490. EXPORT_SYMBOL_GPL(class_compat_remove_link);
  491. int __init classes_init(void)
  492. {
  493. class_kset = kset_create_and_add("class", NULL, NULL);
  494. if (!class_kset)
  495. return -ENOMEM;
  496. return 0;
  497. }
  498. EXPORT_SYMBOL_GPL(class_create_file_ns);
  499. EXPORT_SYMBOL_GPL(class_remove_file_ns);
  500. EXPORT_SYMBOL_GPL(class_unregister);
  501. EXPORT_SYMBOL_GPL(class_destroy);
  502. EXPORT_SYMBOL_GPL(class_interface_register);
  503. EXPORT_SYMBOL_GPL(class_interface_unregister);