class.c 15 KB

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