class.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. * Note, the pointer created here is to be destroyed when finished by
  188. * making a call to class_destroy().
  189. */
  190. struct class *__class_create(struct module *owner, const char *name,
  191. struct lock_class_key *key)
  192. {
  193. struct class *cls;
  194. int retval;
  195. cls = kzalloc(sizeof(*cls), GFP_KERNEL);
  196. if (!cls) {
  197. retval = -ENOMEM;
  198. goto error;
  199. }
  200. cls->name = name;
  201. cls->owner = owner;
  202. cls->class_release = class_create_release;
  203. retval = __class_register(cls, key);
  204. if (retval)
  205. goto error;
  206. return cls;
  207. error:
  208. kfree(cls);
  209. return ERR_PTR(retval);
  210. }
  211. EXPORT_SYMBOL_GPL(__class_create);
  212. /**
  213. * class_destroy - destroys a struct class structure
  214. * @cls: pointer to the struct class that is to be destroyed
  215. *
  216. * Note, the pointer to be destroyed must have been created with a call
  217. * to class_create().
  218. */
  219. void class_destroy(struct class *cls)
  220. {
  221. if ((cls == NULL) || (IS_ERR(cls)))
  222. return;
  223. class_unregister(cls);
  224. }
  225. #ifdef CONFIG_SYSFS_DEPRECATED
  226. char *make_class_name(const char *name, struct kobject *kobj)
  227. {
  228. char *class_name;
  229. int size;
  230. size = strlen(name) + strlen(kobject_name(kobj)) + 2;
  231. class_name = kmalloc(size, GFP_KERNEL);
  232. if (!class_name)
  233. return NULL;
  234. strcpy(class_name, name);
  235. strcat(class_name, ":");
  236. strcat(class_name, kobject_name(kobj));
  237. return class_name;
  238. }
  239. #endif
  240. /**
  241. * class_dev_iter_init - initialize class device iterator
  242. * @iter: class iterator to initialize
  243. * @class: the class we wanna iterate over
  244. * @start: the device to start iterating from, if any
  245. * @type: device_type of the devices to iterate over, NULL for all
  246. *
  247. * Initialize class iterator @iter such that it iterates over devices
  248. * of @class. If @start is set, the list iteration will start there,
  249. * otherwise if it is NULL, the iteration starts at the beginning of
  250. * the list.
  251. */
  252. void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
  253. struct device *start, const struct device_type *type)
  254. {
  255. struct klist_node *start_knode = NULL;
  256. if (start)
  257. start_knode = &start->knode_class;
  258. klist_iter_init_node(&class->p->class_devices, &iter->ki, start_knode);
  259. iter->type = type;
  260. }
  261. EXPORT_SYMBOL_GPL(class_dev_iter_init);
  262. /**
  263. * class_dev_iter_next - iterate to the next device
  264. * @iter: class iterator to proceed
  265. *
  266. * Proceed @iter to the next device and return it. Returns NULL if
  267. * iteration is complete.
  268. *
  269. * The returned device is referenced and won't be released till
  270. * iterator is proceed to the next device or exited. The caller is
  271. * free to do whatever it wants to do with the device including
  272. * calling back into class code.
  273. */
  274. struct device *class_dev_iter_next(struct class_dev_iter *iter)
  275. {
  276. struct klist_node *knode;
  277. struct device *dev;
  278. while (1) {
  279. knode = klist_next(&iter->ki);
  280. if (!knode)
  281. return NULL;
  282. dev = container_of(knode, struct device, knode_class);
  283. if (!iter->type || iter->type == dev->type)
  284. return dev;
  285. }
  286. }
  287. EXPORT_SYMBOL_GPL(class_dev_iter_next);
  288. /**
  289. * class_dev_iter_exit - finish iteration
  290. * @iter: class iterator to finish
  291. *
  292. * Finish an iteration. Always call this function after iteration is
  293. * complete whether the iteration ran till the end or not.
  294. */
  295. void class_dev_iter_exit(struct class_dev_iter *iter)
  296. {
  297. klist_iter_exit(&iter->ki);
  298. }
  299. EXPORT_SYMBOL_GPL(class_dev_iter_exit);
  300. /**
  301. * class_for_each_device - device iterator
  302. * @class: the class we're iterating
  303. * @start: the device to start with in the list, if any.
  304. * @data: data for the callback
  305. * @fn: function to be called for each device
  306. *
  307. * Iterate over @class's list of devices, and call @fn for each,
  308. * passing it @data. If @start is set, the list iteration will start
  309. * there, otherwise if it is NULL, the iteration starts at the
  310. * beginning of the list.
  311. *
  312. * We check the return of @fn each time. If it returns anything
  313. * other than 0, we break out and return that value.
  314. *
  315. * @fn is allowed to do anything including calling back into class
  316. * code. There's no locking restriction.
  317. */
  318. int class_for_each_device(struct class *class, struct device *start,
  319. void *data, int (*fn)(struct device *, void *))
  320. {
  321. struct class_dev_iter iter;
  322. struct device *dev;
  323. int error = 0;
  324. if (!class)
  325. return -EINVAL;
  326. if (!class->p) {
  327. WARN(1, "%s called for class '%s' before it was initialized",
  328. __func__, class->name);
  329. return -EINVAL;
  330. }
  331. class_dev_iter_init(&iter, class, start, NULL);
  332. while ((dev = class_dev_iter_next(&iter))) {
  333. error = fn(dev, data);
  334. if (error)
  335. break;
  336. }
  337. class_dev_iter_exit(&iter);
  338. return error;
  339. }
  340. EXPORT_SYMBOL_GPL(class_for_each_device);
  341. /**
  342. * class_find_device - device iterator for locating a particular device
  343. * @class: the class we're iterating
  344. * @start: Device to begin with
  345. * @data: data for the match function
  346. * @match: function to check device
  347. *
  348. * This is similar to the class_for_each_dev() function above, but it
  349. * returns a reference to a device that is 'found' for later use, as
  350. * determined by the @match callback.
  351. *
  352. * The callback should return 0 if the device doesn't match and non-zero
  353. * if it does. If the callback returns non-zero, this function will
  354. * return to the caller and not iterate over any more devices.
  355. *
  356. * Note, you will need to drop the reference with put_device() after use.
  357. *
  358. * @fn is allowed to do anything including calling back into class
  359. * code. There's no locking restriction.
  360. */
  361. struct device *class_find_device(struct class *class, struct device *start,
  362. void *data,
  363. int (*match)(struct device *, void *))
  364. {
  365. struct class_dev_iter iter;
  366. struct device *dev;
  367. if (!class)
  368. return NULL;
  369. if (!class->p) {
  370. WARN(1, "%s called for class '%s' before it was initialized",
  371. __func__, class->name);
  372. return NULL;
  373. }
  374. class_dev_iter_init(&iter, class, start, NULL);
  375. while ((dev = class_dev_iter_next(&iter))) {
  376. if (match(dev, data)) {
  377. get_device(dev);
  378. break;
  379. }
  380. }
  381. class_dev_iter_exit(&iter);
  382. return dev;
  383. }
  384. EXPORT_SYMBOL_GPL(class_find_device);
  385. int class_interface_register(struct class_interface *class_intf)
  386. {
  387. struct class *parent;
  388. struct class_dev_iter iter;
  389. struct device *dev;
  390. if (!class_intf || !class_intf->class)
  391. return -ENODEV;
  392. parent = class_get(class_intf->class);
  393. if (!parent)
  394. return -EINVAL;
  395. mutex_lock(&parent->p->class_mutex);
  396. list_add_tail(&class_intf->node, &parent->p->class_interfaces);
  397. if (class_intf->add_dev) {
  398. class_dev_iter_init(&iter, parent, NULL, NULL);
  399. while ((dev = class_dev_iter_next(&iter)))
  400. class_intf->add_dev(dev, class_intf);
  401. class_dev_iter_exit(&iter);
  402. }
  403. mutex_unlock(&parent->p->class_mutex);
  404. return 0;
  405. }
  406. void class_interface_unregister(struct class_interface *class_intf)
  407. {
  408. struct class *parent = class_intf->class;
  409. struct class_dev_iter iter;
  410. struct device *dev;
  411. if (!parent)
  412. return;
  413. mutex_lock(&parent->p->class_mutex);
  414. list_del_init(&class_intf->node);
  415. if (class_intf->remove_dev) {
  416. class_dev_iter_init(&iter, parent, NULL, NULL);
  417. while ((dev = class_dev_iter_next(&iter)))
  418. class_intf->remove_dev(dev, class_intf);
  419. class_dev_iter_exit(&iter);
  420. }
  421. mutex_unlock(&parent->p->class_mutex);
  422. class_put(parent);
  423. }
  424. ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
  425. char *buf)
  426. {
  427. struct class_attribute_string *cs;
  428. cs = container_of(attr, struct class_attribute_string, attr);
  429. return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
  430. }
  431. EXPORT_SYMBOL_GPL(show_class_attr_string);
  432. struct class_compat {
  433. struct kobject *kobj;
  434. };
  435. /**
  436. * class_compat_register - register a compatibility class
  437. * @name: the name of the class
  438. *
  439. * Compatibility class are meant as a temporary user-space compatibility
  440. * workaround when converting a family of class devices to a bus devices.
  441. */
  442. struct class_compat *class_compat_register(const char *name)
  443. {
  444. struct class_compat *cls;
  445. cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL);
  446. if (!cls)
  447. return NULL;
  448. cls->kobj = kobject_create_and_add(name, &class_kset->kobj);
  449. if (!cls->kobj) {
  450. kfree(cls);
  451. return NULL;
  452. }
  453. return cls;
  454. }
  455. EXPORT_SYMBOL_GPL(class_compat_register);
  456. /**
  457. * class_compat_unregister - unregister a compatibility class
  458. * @cls: the class to unregister
  459. */
  460. void class_compat_unregister(struct class_compat *cls)
  461. {
  462. kobject_put(cls->kobj);
  463. kfree(cls);
  464. }
  465. EXPORT_SYMBOL_GPL(class_compat_unregister);
  466. /**
  467. * class_compat_create_link - create a compatibility class device link to
  468. * a bus device
  469. * @cls: the compatibility class
  470. * @dev: the target bus device
  471. * @device_link: an optional device to which a "device" link should be created
  472. */
  473. int class_compat_create_link(struct class_compat *cls, struct device *dev,
  474. struct device *device_link)
  475. {
  476. int error;
  477. error = sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev));
  478. if (error)
  479. return error;
  480. /*
  481. * Optionally add a "device" link (typically to the parent), as a
  482. * class device would have one and we want to provide as much
  483. * backwards compatibility as possible.
  484. */
  485. if (device_link) {
  486. error = sysfs_create_link(&dev->kobj, &device_link->kobj,
  487. "device");
  488. if (error)
  489. sysfs_remove_link(cls->kobj, dev_name(dev));
  490. }
  491. return error;
  492. }
  493. EXPORT_SYMBOL_GPL(class_compat_create_link);
  494. /**
  495. * class_compat_remove_link - remove a compatibility class device link to
  496. * a bus device
  497. * @cls: the compatibility class
  498. * @dev: the target bus device
  499. * @device_link: an optional device to which a "device" link was previously
  500. * created
  501. */
  502. void class_compat_remove_link(struct class_compat *cls, struct device *dev,
  503. struct device *device_link)
  504. {
  505. if (device_link)
  506. sysfs_remove_link(&dev->kobj, "device");
  507. sysfs_remove_link(cls->kobj, dev_name(dev));
  508. }
  509. EXPORT_SYMBOL_GPL(class_compat_remove_link);
  510. int __init classes_init(void)
  511. {
  512. class_kset = kset_create_and_add("class", NULL, NULL);
  513. if (!class_kset)
  514. return -ENOMEM;
  515. return 0;
  516. }
  517. EXPORT_SYMBOL_GPL(class_create_file);
  518. EXPORT_SYMBOL_GPL(class_remove_file);
  519. EXPORT_SYMBOL_GPL(class_unregister);
  520. EXPORT_SYMBOL_GPL(class_destroy);
  521. EXPORT_SYMBOL_GPL(class_interface_register);
  522. EXPORT_SYMBOL_GPL(class_interface_unregister);