class.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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/genhd.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. return error;
  162. }
  163. EXPORT_SYMBOL_GPL(__class_register);
  164. void class_unregister(struct class *cls)
  165. {
  166. pr_debug("device class '%s': unregistering\n", cls->name);
  167. class_remove_groups(cls, cls->class_groups);
  168. kset_unregister(&cls->p->subsys);
  169. }
  170. static void class_create_release(struct class *cls)
  171. {
  172. pr_debug("%s called for %s\n", __func__, cls->name);
  173. kfree(cls);
  174. }
  175. /**
  176. * class_create - create a struct class structure
  177. * @owner: pointer to the module that is to "own" this struct class
  178. * @name: pointer to a string for the name of this class.
  179. * @key: the lock_class_key for this class; used by mutex lock debugging
  180. *
  181. * This is used to create a struct class pointer that can then be used
  182. * in calls to device_create().
  183. *
  184. * Returns &struct class pointer on success, or ERR_PTR() on error.
  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. /**
  225. * class_dev_iter_init - initialize class device iterator
  226. * @iter: class iterator to initialize
  227. * @class: the class we wanna iterate over
  228. * @start: the device to start iterating from, if any
  229. * @type: device_type of the devices to iterate over, NULL for all
  230. *
  231. * Initialize class iterator @iter such that it iterates over devices
  232. * of @class. If @start is set, the list iteration will start there,
  233. * otherwise if it is NULL, the iteration starts at the beginning of
  234. * the list.
  235. */
  236. void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
  237. struct device *start, const struct device_type *type)
  238. {
  239. struct klist_node *start_knode = NULL;
  240. if (start)
  241. start_knode = &start->p->knode_class;
  242. klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
  243. iter->type = type;
  244. }
  245. EXPORT_SYMBOL_GPL(class_dev_iter_init);
  246. /**
  247. * class_dev_iter_next - iterate to the next device
  248. * @iter: class iterator to proceed
  249. *
  250. * Proceed @iter to the next device and return it. Returns NULL if
  251. * iteration is complete.
  252. *
  253. * The returned device is referenced and won't be released till
  254. * iterator is proceed to the next device or exited. The caller is
  255. * free to do whatever it wants to do with the device including
  256. * calling back into class code.
  257. */
  258. struct device *class_dev_iter_next(struct class_dev_iter *iter)
  259. {
  260. struct klist_node *knode;
  261. struct device *dev;
  262. while (1) {
  263. knode = klist_next(&iter->ki);
  264. if (!knode)
  265. return NULL;
  266. dev = klist_class_to_dev(knode);
  267. if (!iter->type || iter->type == dev->type)
  268. return dev;
  269. }
  270. }
  271. EXPORT_SYMBOL_GPL(class_dev_iter_next);
  272. /**
  273. * class_dev_iter_exit - finish iteration
  274. * @iter: class iterator to finish
  275. *
  276. * Finish an iteration. Always call this function after iteration is
  277. * complete whether the iteration ran till the end or not.
  278. */
  279. void class_dev_iter_exit(struct class_dev_iter *iter)
  280. {
  281. klist_iter_exit(&iter->ki);
  282. }
  283. EXPORT_SYMBOL_GPL(class_dev_iter_exit);
  284. /**
  285. * class_for_each_device - device iterator
  286. * @class: the class we're iterating
  287. * @start: the device to start with in the list, if any.
  288. * @data: data for the callback
  289. * @fn: function to be called for each device
  290. *
  291. * Iterate over @class's list of devices, and call @fn for each,
  292. * passing it @data. If @start is set, the list iteration will start
  293. * there, otherwise if it is NULL, the iteration starts at the
  294. * beginning of the list.
  295. *
  296. * We check the return of @fn each time. If it returns anything
  297. * other than 0, we break out and return that value.
  298. *
  299. * @fn is allowed to do anything including calling back into class
  300. * code. There's no locking restriction.
  301. */
  302. int class_for_each_device(struct class *class, struct device *start,
  303. void *data, int (*fn)(struct device *, void *))
  304. {
  305. struct class_dev_iter iter;
  306. struct device *dev;
  307. int error = 0;
  308. if (!class)
  309. return -EINVAL;
  310. if (!class->p) {
  311. WARN(1, "%s called for class '%s' before it was initialized",
  312. __func__, class->name);
  313. return -EINVAL;
  314. }
  315. class_dev_iter_init(&iter, class, start, NULL);
  316. while ((dev = class_dev_iter_next(&iter))) {
  317. error = fn(dev, data);
  318. if (error)
  319. break;
  320. }
  321. class_dev_iter_exit(&iter);
  322. return error;
  323. }
  324. EXPORT_SYMBOL_GPL(class_for_each_device);
  325. /**
  326. * class_find_device - device iterator for locating a particular device
  327. * @class: the class we're iterating
  328. * @start: Device to begin with
  329. * @data: data for the match function
  330. * @match: function to check device
  331. *
  332. * This is similar to the class_for_each_dev() function above, but it
  333. * returns a reference to a device that is 'found' for later use, as
  334. * determined by the @match callback.
  335. *
  336. * The callback should return 0 if the device doesn't match and non-zero
  337. * if it does. If the callback returns non-zero, this function will
  338. * return to the caller and not iterate over any more devices.
  339. *
  340. * Note, you will need to drop the reference with put_device() after use.
  341. *
  342. * @match is allowed to do anything including calling back into class
  343. * code. There's no locking restriction.
  344. */
  345. struct device *class_find_device(struct class *class, struct device *start,
  346. const void *data,
  347. int (*match)(struct device *, const void *))
  348. {
  349. struct class_dev_iter iter;
  350. struct device *dev;
  351. if (!class)
  352. return NULL;
  353. if (!class->p) {
  354. WARN(1, "%s called for class '%s' before it was initialized",
  355. __func__, class->name);
  356. return NULL;
  357. }
  358. class_dev_iter_init(&iter, class, start, NULL);
  359. while ((dev = class_dev_iter_next(&iter))) {
  360. if (match(dev, data)) {
  361. get_device(dev);
  362. break;
  363. }
  364. }
  365. class_dev_iter_exit(&iter);
  366. return dev;
  367. }
  368. EXPORT_SYMBOL_GPL(class_find_device);
  369. int class_interface_register(struct class_interface *class_intf)
  370. {
  371. struct class *parent;
  372. struct class_dev_iter iter;
  373. struct device *dev;
  374. if (!class_intf || !class_intf->class)
  375. return -ENODEV;
  376. parent = class_get(class_intf->class);
  377. if (!parent)
  378. return -EINVAL;
  379. mutex_lock(&parent->p->mutex);
  380. list_add_tail(&class_intf->node, &parent->p->interfaces);
  381. if (class_intf->add_dev) {
  382. class_dev_iter_init(&iter, parent, NULL, NULL);
  383. while ((dev = class_dev_iter_next(&iter)))
  384. class_intf->add_dev(dev, class_intf);
  385. class_dev_iter_exit(&iter);
  386. }
  387. mutex_unlock(&parent->p->mutex);
  388. return 0;
  389. }
  390. void class_interface_unregister(struct class_interface *class_intf)
  391. {
  392. struct class *parent = class_intf->class;
  393. struct class_dev_iter iter;
  394. struct device *dev;
  395. if (!parent)
  396. return;
  397. mutex_lock(&parent->p->mutex);
  398. list_del_init(&class_intf->node);
  399. if (class_intf->remove_dev) {
  400. class_dev_iter_init(&iter, parent, NULL, NULL);
  401. while ((dev = class_dev_iter_next(&iter)))
  402. class_intf->remove_dev(dev, class_intf);
  403. class_dev_iter_exit(&iter);
  404. }
  405. mutex_unlock(&parent->p->mutex);
  406. class_put(parent);
  407. }
  408. ssize_t show_class_attr_string(struct class *class,
  409. struct class_attribute *attr, char *buf)
  410. {
  411. struct class_attribute_string *cs;
  412. cs = container_of(attr, struct class_attribute_string, attr);
  413. return sysfs_emit(buf, "%s\n", cs->str);
  414. }
  415. EXPORT_SYMBOL_GPL(show_class_attr_string);
  416. struct class_compat {
  417. struct kobject *kobj;
  418. };
  419. /**
  420. * class_compat_register - register a compatibility class
  421. * @name: the name of the class
  422. *
  423. * Compatibility class are meant as a temporary user-space compatibility
  424. * workaround when converting a family of class devices to a bus devices.
  425. */
  426. struct class_compat *class_compat_register(const char *name)
  427. {
  428. struct class_compat *cls;
  429. cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL);
  430. if (!cls)
  431. return NULL;
  432. cls->kobj = kobject_create_and_add(name, &class_kset->kobj);
  433. if (!cls->kobj) {
  434. kfree(cls);
  435. return NULL;
  436. }
  437. return cls;
  438. }
  439. EXPORT_SYMBOL_GPL(class_compat_register);
  440. /**
  441. * class_compat_unregister - unregister a compatibility class
  442. * @cls: the class to unregister
  443. */
  444. void class_compat_unregister(struct class_compat *cls)
  445. {
  446. kobject_put(cls->kobj);
  447. kfree(cls);
  448. }
  449. EXPORT_SYMBOL_GPL(class_compat_unregister);
  450. /**
  451. * class_compat_create_link - create a compatibility class device link to
  452. * a bus device
  453. * @cls: the compatibility class
  454. * @dev: the target bus device
  455. * @device_link: an optional device to which a "device" link should be created
  456. */
  457. int class_compat_create_link(struct class_compat *cls, struct device *dev,
  458. struct device *device_link)
  459. {
  460. int error;
  461. error = sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev));
  462. if (error)
  463. return error;
  464. /*
  465. * Optionally add a "device" link (typically to the parent), as a
  466. * class device would have one and we want to provide as much
  467. * backwards compatibility as possible.
  468. */
  469. if (device_link) {
  470. error = sysfs_create_link(&dev->kobj, &device_link->kobj,
  471. "device");
  472. if (error)
  473. sysfs_remove_link(cls->kobj, dev_name(dev));
  474. }
  475. return error;
  476. }
  477. EXPORT_SYMBOL_GPL(class_compat_create_link);
  478. /**
  479. * class_compat_remove_link - remove a compatibility class device link to
  480. * a bus device
  481. * @cls: the compatibility class
  482. * @dev: the target bus device
  483. * @device_link: an optional device to which a "device" link was previously
  484. * created
  485. */
  486. void class_compat_remove_link(struct class_compat *cls, struct device *dev,
  487. struct device *device_link)
  488. {
  489. if (device_link)
  490. sysfs_remove_link(&dev->kobj, "device");
  491. sysfs_remove_link(cls->kobj, dev_name(dev));
  492. }
  493. EXPORT_SYMBOL_GPL(class_compat_remove_link);
  494. int __init classes_init(void)
  495. {
  496. class_kset = kset_create_and_add("class", NULL, NULL);
  497. if (!class_kset)
  498. return -ENOMEM;
  499. return 0;
  500. }
  501. EXPORT_SYMBOL_GPL(class_create_file_ns);
  502. EXPORT_SYMBOL_GPL(class_remove_file_ns);
  503. EXPORT_SYMBOL_GPL(class_unregister);
  504. EXPORT_SYMBOL_GPL(class_destroy);
  505. EXPORT_SYMBOL_GPL(class_interface_register);
  506. EXPORT_SYMBOL_GPL(class_interface_unregister);