class.c 15 KB

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