class.c 15 KB

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