class.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. #include <linux/device/class.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/string.h>
  15. #include <linux/kdev_t.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/mutex.h>
  20. #include "base.h"
  21. /* /sys/class */
  22. static struct kset *class_kset;
  23. #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
  24. /**
  25. * class_to_subsys - Turn a struct class into a struct subsys_private
  26. *
  27. * @class: pointer to the struct bus_type to look up
  28. *
  29. * The driver core internals need to work on the subsys_private structure, not
  30. * the external struct class pointer. This function walks the list of
  31. * registered classes in the system and finds the matching one and returns the
  32. * internal struct subsys_private that relates to that class.
  33. *
  34. * Note, the reference count of the return value is INCREMENTED if it is not
  35. * NULL. A call to subsys_put() must be done when finished with the pointer in
  36. * order for it to be properly freed.
  37. */
  38. struct subsys_private *class_to_subsys(const struct class *class)
  39. {
  40. struct subsys_private *sp = NULL;
  41. struct kobject *kobj;
  42. if (!class || !class_kset)
  43. return NULL;
  44. spin_lock(&class_kset->list_lock);
  45. if (list_empty(&class_kset->list))
  46. goto done;
  47. list_for_each_entry(kobj, &class_kset->list, entry) {
  48. struct kset *kset = container_of(kobj, struct kset, kobj);
  49. sp = container_of_const(kset, struct subsys_private, subsys);
  50. if (sp->class == class)
  51. goto done;
  52. }
  53. sp = NULL;
  54. done:
  55. sp = subsys_get(sp);
  56. spin_unlock(&class_kset->list_lock);
  57. return sp;
  58. }
  59. static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
  60. char *buf)
  61. {
  62. struct class_attribute *class_attr = to_class_attr(attr);
  63. struct subsys_private *cp = to_subsys_private(kobj);
  64. ssize_t ret = -EIO;
  65. if (class_attr->show)
  66. ret = class_attr->show(cp->class, class_attr, buf);
  67. return ret;
  68. }
  69. static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
  70. const char *buf, size_t count)
  71. {
  72. struct class_attribute *class_attr = to_class_attr(attr);
  73. struct subsys_private *cp = to_subsys_private(kobj);
  74. ssize_t ret = -EIO;
  75. if (class_attr->store)
  76. ret = class_attr->store(cp->class, class_attr, buf, count);
  77. return ret;
  78. }
  79. static void class_release(struct kobject *kobj)
  80. {
  81. struct subsys_private *cp = to_subsys_private(kobj);
  82. const struct class *class = cp->class;
  83. pr_debug("class '%s': release.\n", class->name);
  84. if (class->class_release)
  85. class->class_release(class);
  86. else
  87. pr_debug("class '%s' does not have a release() function, "
  88. "be careful\n", class->name);
  89. lockdep_unregister_key(&cp->lock_key);
  90. kfree(cp);
  91. }
  92. static const struct kobj_ns_type_operations *class_child_ns_type(const struct kobject *kobj)
  93. {
  94. const struct subsys_private *cp = to_subsys_private(kobj);
  95. const struct class *class = cp->class;
  96. return class->ns_type;
  97. }
  98. static const struct sysfs_ops class_sysfs_ops = {
  99. .show = class_attr_show,
  100. .store = class_attr_store,
  101. };
  102. static const struct kobj_type class_ktype = {
  103. .sysfs_ops = &class_sysfs_ops,
  104. .release = class_release,
  105. .child_ns_type = class_child_ns_type,
  106. };
  107. int class_create_file_ns(const struct class *cls, const struct class_attribute *attr,
  108. const void *ns)
  109. {
  110. struct subsys_private *sp = class_to_subsys(cls);
  111. int error;
  112. if (!sp)
  113. return -EINVAL;
  114. error = sysfs_create_file_ns(&sp->subsys.kobj, &attr->attr, ns);
  115. subsys_put(sp);
  116. return error;
  117. }
  118. EXPORT_SYMBOL_GPL(class_create_file_ns);
  119. void class_remove_file_ns(const struct class *cls, const struct class_attribute *attr,
  120. const void *ns)
  121. {
  122. struct subsys_private *sp = class_to_subsys(cls);
  123. if (!sp)
  124. return;
  125. sysfs_remove_file_ns(&sp->subsys.kobj, &attr->attr, ns);
  126. subsys_put(sp);
  127. }
  128. EXPORT_SYMBOL_GPL(class_remove_file_ns);
  129. static struct device *klist_class_to_dev(struct klist_node *n)
  130. {
  131. struct device_private *p = to_device_private_class(n);
  132. return p->device;
  133. }
  134. static void klist_class_dev_get(struct klist_node *n)
  135. {
  136. struct device *dev = klist_class_to_dev(n);
  137. get_device(dev);
  138. }
  139. static void klist_class_dev_put(struct klist_node *n)
  140. {
  141. struct device *dev = klist_class_to_dev(n);
  142. put_device(dev);
  143. }
  144. int class_register(const struct class *cls)
  145. {
  146. struct subsys_private *cp;
  147. struct lock_class_key *key;
  148. int error;
  149. pr_debug("device class '%s': registering\n", cls->name);
  150. if (cls->ns_type && !cls->namespace) {
  151. pr_err("%s: class '%s' does not have namespace\n",
  152. __func__, cls->name);
  153. return -EINVAL;
  154. }
  155. if (!cls->ns_type && cls->namespace) {
  156. pr_err("%s: class '%s' does not have ns_type\n",
  157. __func__, cls->name);
  158. return -EINVAL;
  159. }
  160. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  161. if (!cp)
  162. return -ENOMEM;
  163. klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put);
  164. INIT_LIST_HEAD(&cp->interfaces);
  165. kset_init(&cp->glue_dirs);
  166. key = &cp->lock_key;
  167. lockdep_register_key(key);
  168. __mutex_init(&cp->mutex, "subsys mutex", key);
  169. error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name);
  170. if (error)
  171. goto err_out;
  172. cp->subsys.kobj.kset = class_kset;
  173. cp->subsys.kobj.ktype = &class_ktype;
  174. cp->class = cls;
  175. error = kset_register(&cp->subsys);
  176. if (error)
  177. goto err_out;
  178. error = sysfs_create_groups(&cp->subsys.kobj, cls->class_groups);
  179. if (error) {
  180. kobject_del(&cp->subsys.kobj);
  181. kfree_const(cp->subsys.kobj.name);
  182. goto err_out;
  183. }
  184. return 0;
  185. err_out:
  186. lockdep_unregister_key(key);
  187. kfree(cp);
  188. return error;
  189. }
  190. EXPORT_SYMBOL_GPL(class_register);
  191. void class_unregister(const struct class *cls)
  192. {
  193. struct subsys_private *sp = class_to_subsys(cls);
  194. if (!sp)
  195. return;
  196. pr_debug("device class '%s': unregistering\n", cls->name);
  197. sysfs_remove_groups(&sp->subsys.kobj, cls->class_groups);
  198. kset_unregister(&sp->subsys);
  199. subsys_put(sp);
  200. }
  201. EXPORT_SYMBOL_GPL(class_unregister);
  202. static void class_create_release(const struct class *cls)
  203. {
  204. pr_debug("%s called for %s\n", __func__, cls->name);
  205. kfree(cls);
  206. }
  207. /**
  208. * class_create - create a struct class structure
  209. * @name: pointer to a string for the name of this class.
  210. *
  211. * This is used to create a struct class pointer that can then be used
  212. * in calls to device_create().
  213. *
  214. * Returns &struct class pointer on success, or ERR_PTR() on error.
  215. *
  216. * Note, the pointer created here is to be destroyed when finished by
  217. * making a call to class_destroy().
  218. */
  219. struct class *class_create(const char *name)
  220. {
  221. struct class *cls;
  222. int retval;
  223. cls = kzalloc(sizeof(*cls), GFP_KERNEL);
  224. if (!cls) {
  225. retval = -ENOMEM;
  226. goto error;
  227. }
  228. cls->name = name;
  229. cls->class_release = class_create_release;
  230. retval = class_register(cls);
  231. if (retval)
  232. goto error;
  233. return cls;
  234. error:
  235. kfree(cls);
  236. return ERR_PTR(retval);
  237. }
  238. EXPORT_SYMBOL_GPL(class_create);
  239. /**
  240. * class_destroy - destroys a struct class structure
  241. * @cls: pointer to the struct class that is to be destroyed
  242. *
  243. * Note, the pointer to be destroyed must have been created with a call
  244. * to class_create().
  245. */
  246. void class_destroy(const struct class *cls)
  247. {
  248. if (IS_ERR_OR_NULL(cls))
  249. return;
  250. class_unregister(cls);
  251. }
  252. EXPORT_SYMBOL_GPL(class_destroy);
  253. /**
  254. * class_dev_iter_init - initialize class device iterator
  255. * @iter: class iterator to initialize
  256. * @class: the class we wanna iterate over
  257. * @start: the device to start iterating from, if any
  258. * @type: device_type of the devices to iterate over, NULL for all
  259. *
  260. * Initialize class iterator @iter such that it iterates over devices
  261. * of @class. If @start is set, the list iteration will start there,
  262. * otherwise if it is NULL, the iteration starts at the beginning of
  263. * the list.
  264. */
  265. void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,
  266. const struct device *start, const struct device_type *type)
  267. {
  268. struct subsys_private *sp = class_to_subsys(class);
  269. struct klist_node *start_knode = NULL;
  270. memset(iter, 0, sizeof(*iter));
  271. if (!sp) {
  272. pr_crit("%s: class %p was not registered yet\n",
  273. __func__, class);
  274. return;
  275. }
  276. if (start)
  277. start_knode = &start->p->knode_class;
  278. klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode);
  279. iter->type = type;
  280. iter->sp = sp;
  281. }
  282. EXPORT_SYMBOL_GPL(class_dev_iter_init);
  283. /**
  284. * class_dev_iter_next - iterate to the next device
  285. * @iter: class iterator to proceed
  286. *
  287. * Proceed @iter to the next device and return it. Returns NULL if
  288. * iteration is complete.
  289. *
  290. * The returned device is referenced and won't be released till
  291. * iterator is proceed to the next device or exited. The caller is
  292. * free to do whatever it wants to do with the device including
  293. * calling back into class code.
  294. */
  295. struct device *class_dev_iter_next(struct class_dev_iter *iter)
  296. {
  297. struct klist_node *knode;
  298. struct device *dev;
  299. if (!iter->sp)
  300. return NULL;
  301. while (1) {
  302. knode = klist_next(&iter->ki);
  303. if (!knode)
  304. return NULL;
  305. dev = klist_class_to_dev(knode);
  306. if (!iter->type || iter->type == dev->type)
  307. return dev;
  308. }
  309. }
  310. EXPORT_SYMBOL_GPL(class_dev_iter_next);
  311. /**
  312. * class_dev_iter_exit - finish iteration
  313. * @iter: class iterator to finish
  314. *
  315. * Finish an iteration. Always call this function after iteration is
  316. * complete whether the iteration ran till the end or not.
  317. */
  318. void class_dev_iter_exit(struct class_dev_iter *iter)
  319. {
  320. klist_iter_exit(&iter->ki);
  321. subsys_put(iter->sp);
  322. }
  323. EXPORT_SYMBOL_GPL(class_dev_iter_exit);
  324. /**
  325. * class_for_each_device - device iterator
  326. * @class: the class we're iterating
  327. * @start: the device to start with in the list, if any.
  328. * @data: data for the callback
  329. * @fn: function to be called for each device
  330. *
  331. * Iterate over @class's list of devices, and call @fn for each,
  332. * passing it @data. If @start is set, the list iteration will start
  333. * there, otherwise if it is NULL, the iteration starts at the
  334. * beginning of the list.
  335. *
  336. * We check the return of @fn each time. If it returns anything
  337. * other than 0, we break out and return that value.
  338. *
  339. * @fn is allowed to do anything including calling back into class
  340. * code. There's no locking restriction.
  341. */
  342. int class_for_each_device(const struct class *class, const struct device *start,
  343. void *data, int (*fn)(struct device *, void *))
  344. {
  345. struct subsys_private *sp = class_to_subsys(class);
  346. struct class_dev_iter iter;
  347. struct device *dev;
  348. int error = 0;
  349. if (!class)
  350. return -EINVAL;
  351. if (!sp) {
  352. WARN(1, "%s called for class '%s' before it was initialized",
  353. __func__, class->name);
  354. return -EINVAL;
  355. }
  356. class_dev_iter_init(&iter, class, start, NULL);
  357. while ((dev = class_dev_iter_next(&iter))) {
  358. error = fn(dev, data);
  359. if (error)
  360. break;
  361. }
  362. class_dev_iter_exit(&iter);
  363. subsys_put(sp);
  364. return error;
  365. }
  366. EXPORT_SYMBOL_GPL(class_for_each_device);
  367. /**
  368. * class_find_device - device iterator for locating a particular device
  369. * @class: the class we're iterating
  370. * @start: Device to begin with
  371. * @data: data for the match function
  372. * @match: function to check device
  373. *
  374. * This is similar to the class_for_each_dev() function above, but it
  375. * returns a reference to a device that is 'found' for later use, as
  376. * determined by the @match callback.
  377. *
  378. * The callback should return 0 if the device doesn't match and non-zero
  379. * if it does. If the callback returns non-zero, this function will
  380. * return to the caller and not iterate over any more devices.
  381. *
  382. * Note, you will need to drop the reference with put_device() after use.
  383. *
  384. * @match is allowed to do anything including calling back into class
  385. * code. There's no locking restriction.
  386. */
  387. struct device *class_find_device(const struct class *class, const struct device *start,
  388. const void *data, device_match_t match)
  389. {
  390. struct subsys_private *sp = class_to_subsys(class);
  391. struct class_dev_iter iter;
  392. struct device *dev;
  393. if (!class)
  394. return NULL;
  395. if (!sp) {
  396. WARN(1, "%s called for class '%s' before it was initialized",
  397. __func__, class->name);
  398. return NULL;
  399. }
  400. class_dev_iter_init(&iter, class, start, NULL);
  401. while ((dev = class_dev_iter_next(&iter))) {
  402. if (match(dev, data)) {
  403. get_device(dev);
  404. break;
  405. }
  406. }
  407. class_dev_iter_exit(&iter);
  408. subsys_put(sp);
  409. return dev;
  410. }
  411. EXPORT_SYMBOL_GPL(class_find_device);
  412. int class_interface_register(struct class_interface *class_intf)
  413. {
  414. struct subsys_private *sp;
  415. const struct class *parent;
  416. struct class_dev_iter iter;
  417. struct device *dev;
  418. if (!class_intf || !class_intf->class)
  419. return -ENODEV;
  420. parent = class_intf->class;
  421. sp = class_to_subsys(parent);
  422. if (!sp)
  423. return -EINVAL;
  424. /*
  425. * Reference in sp is now incremented and will be dropped when
  426. * the interface is removed in the call to class_interface_unregister()
  427. */
  428. mutex_lock(&sp->mutex);
  429. list_add_tail(&class_intf->node, &sp->interfaces);
  430. if (class_intf->add_dev) {
  431. class_dev_iter_init(&iter, parent, NULL, NULL);
  432. while ((dev = class_dev_iter_next(&iter)))
  433. class_intf->add_dev(dev);
  434. class_dev_iter_exit(&iter);
  435. }
  436. mutex_unlock(&sp->mutex);
  437. return 0;
  438. }
  439. EXPORT_SYMBOL_GPL(class_interface_register);
  440. void class_interface_unregister(struct class_interface *class_intf)
  441. {
  442. struct subsys_private *sp;
  443. const struct class *parent = class_intf->class;
  444. struct class_dev_iter iter;
  445. struct device *dev;
  446. if (!parent)
  447. return;
  448. sp = class_to_subsys(parent);
  449. if (!sp)
  450. return;
  451. mutex_lock(&sp->mutex);
  452. list_del_init(&class_intf->node);
  453. if (class_intf->remove_dev) {
  454. class_dev_iter_init(&iter, parent, NULL, NULL);
  455. while ((dev = class_dev_iter_next(&iter)))
  456. class_intf->remove_dev(dev);
  457. class_dev_iter_exit(&iter);
  458. }
  459. mutex_unlock(&sp->mutex);
  460. /*
  461. * Decrement the reference count twice, once for the class_to_subsys()
  462. * call in the start of this function, and the second one from the
  463. * reference increment in class_interface_register()
  464. */
  465. subsys_put(sp);
  466. subsys_put(sp);
  467. }
  468. EXPORT_SYMBOL_GPL(class_interface_unregister);
  469. ssize_t show_class_attr_string(const struct class *class,
  470. const struct class_attribute *attr, char *buf)
  471. {
  472. struct class_attribute_string *cs;
  473. cs = container_of(attr, struct class_attribute_string, attr);
  474. return sysfs_emit(buf, "%s\n", cs->str);
  475. }
  476. EXPORT_SYMBOL_GPL(show_class_attr_string);
  477. struct class_compat {
  478. struct kobject *kobj;
  479. };
  480. /**
  481. * class_compat_register - register a compatibility class
  482. * @name: the name of the class
  483. *
  484. * Compatibility class are meant as a temporary user-space compatibility
  485. * workaround when converting a family of class devices to a bus devices.
  486. */
  487. struct class_compat *class_compat_register(const char *name)
  488. {
  489. struct class_compat *cls;
  490. cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL);
  491. if (!cls)
  492. return NULL;
  493. cls->kobj = kobject_create_and_add(name, &class_kset->kobj);
  494. if (!cls->kobj) {
  495. kfree(cls);
  496. return NULL;
  497. }
  498. return cls;
  499. }
  500. EXPORT_SYMBOL_GPL(class_compat_register);
  501. /**
  502. * class_compat_unregister - unregister a compatibility class
  503. * @cls: the class to unregister
  504. */
  505. void class_compat_unregister(struct class_compat *cls)
  506. {
  507. kobject_put(cls->kobj);
  508. kfree(cls);
  509. }
  510. EXPORT_SYMBOL_GPL(class_compat_unregister);
  511. /**
  512. * class_compat_create_link - create a compatibility class device link to
  513. * a bus device
  514. * @cls: the compatibility class
  515. * @dev: the target bus device
  516. * @device_link: an optional device to which a "device" link should be created
  517. */
  518. int class_compat_create_link(struct class_compat *cls, struct device *dev,
  519. struct device *device_link)
  520. {
  521. int error;
  522. error = sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev));
  523. if (error)
  524. return error;
  525. /*
  526. * Optionally add a "device" link (typically to the parent), as a
  527. * class device would have one and we want to provide as much
  528. * backwards compatibility as possible.
  529. */
  530. if (device_link) {
  531. error = sysfs_create_link(&dev->kobj, &device_link->kobj,
  532. "device");
  533. if (error)
  534. sysfs_remove_link(cls->kobj, dev_name(dev));
  535. }
  536. return error;
  537. }
  538. EXPORT_SYMBOL_GPL(class_compat_create_link);
  539. /**
  540. * class_compat_remove_link - remove a compatibility class device link to
  541. * a bus device
  542. * @cls: the compatibility class
  543. * @dev: the target bus device
  544. * @device_link: an optional device to which a "device" link was previously
  545. * created
  546. */
  547. void class_compat_remove_link(struct class_compat *cls, struct device *dev,
  548. struct device *device_link)
  549. {
  550. if (device_link)
  551. sysfs_remove_link(&dev->kobj, "device");
  552. sysfs_remove_link(cls->kobj, dev_name(dev));
  553. }
  554. EXPORT_SYMBOL_GPL(class_compat_remove_link);
  555. /**
  556. * class_is_registered - determine if at this moment in time, a class is
  557. * registered in the driver core or not.
  558. * @class: the class to check
  559. *
  560. * Returns a boolean to state if the class is registered in the driver core
  561. * or not. Note that the value could switch right after this call is made,
  562. * so only use this in places where you "know" it is safe to do so (usually
  563. * to determine if the specific class has been registered yet or not).
  564. *
  565. * Be careful in using this.
  566. */
  567. bool class_is_registered(const struct class *class)
  568. {
  569. struct subsys_private *sp = class_to_subsys(class);
  570. bool is_initialized = false;
  571. if (sp) {
  572. is_initialized = true;
  573. subsys_put(sp);
  574. }
  575. return is_initialized;
  576. }
  577. EXPORT_SYMBOL_GPL(class_is_registered);
  578. int __init classes_init(void)
  579. {
  580. class_kset = kset_create_and_add("class", NULL, NULL);
  581. if (!class_kset)
  582. return -ENOMEM;
  583. return 0;
  584. }