class.c 15 KB

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