class.c 15 KB

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