class.c 15 KB

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