class.c 15 KB

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