class.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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 "base.h"
  21. #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
  22. #define to_class(obj) container_of(obj, struct class, subsys.kobj)
  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 *dc = to_class(kobj);
  28. ssize_t ret = -EIO;
  29. if (class_attr->show)
  30. ret = class_attr->show(dc, 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 *dc = to_class(kobj);
  38. ssize_t ret = -EIO;
  39. if (class_attr->store)
  40. ret = class_attr->store(dc, buf, count);
  41. return ret;
  42. }
  43. static void class_release(struct kobject *kobj)
  44. {
  45. struct class *class = to_class(kobj);
  46. pr_debug("class '%s': release.\n", class->name);
  47. if (class->class_release)
  48. class->class_release(class);
  49. else
  50. pr_debug("class '%s' does not have a release() function, "
  51. "be careful\n", class->name);
  52. }
  53. static struct sysfs_ops class_sysfs_ops = {
  54. .show = class_attr_show,
  55. .store = class_attr_store,
  56. };
  57. static struct kobj_type class_ktype = {
  58. .sysfs_ops = &class_sysfs_ops,
  59. .release = class_release,
  60. };
  61. /* Hotplug events for classes go to the class_obj subsys */
  62. static struct kset *class_kset;
  63. int class_create_file(struct class *cls, const struct class_attribute *attr)
  64. {
  65. int error;
  66. if (cls)
  67. error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
  68. else
  69. error = -EINVAL;
  70. return error;
  71. }
  72. void class_remove_file(struct class *cls, const struct class_attribute *attr)
  73. {
  74. if (cls)
  75. sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
  76. }
  77. static struct class *class_get(struct class *cls)
  78. {
  79. if (cls)
  80. return container_of(kset_get(&cls->subsys),
  81. struct class, subsys);
  82. return NULL;
  83. }
  84. static void class_put(struct class *cls)
  85. {
  86. if (cls)
  87. kset_put(&cls->subsys);
  88. }
  89. static int add_class_attrs(struct class *cls)
  90. {
  91. int i;
  92. int error = 0;
  93. if (cls->class_attrs) {
  94. for (i = 0; attr_name(cls->class_attrs[i]); i++) {
  95. error = class_create_file(cls, &cls->class_attrs[i]);
  96. if (error)
  97. goto error;
  98. }
  99. }
  100. done:
  101. return error;
  102. error:
  103. while (--i >= 0)
  104. class_remove_file(cls, &cls->class_attrs[i]);
  105. goto done;
  106. }
  107. static void remove_class_attrs(struct class *cls)
  108. {
  109. int i;
  110. if (cls->class_attrs) {
  111. for (i = 0; attr_name(cls->class_attrs[i]); i++)
  112. class_remove_file(cls, &cls->class_attrs[i]);
  113. }
  114. }
  115. int class_register(struct class *cls)
  116. {
  117. int error;
  118. pr_debug("device class '%s': registering\n", cls->name);
  119. INIT_LIST_HEAD(&cls->children);
  120. INIT_LIST_HEAD(&cls->devices);
  121. INIT_LIST_HEAD(&cls->interfaces);
  122. kset_init(&cls->class_dirs);
  123. init_MUTEX(&cls->sem);
  124. error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name);
  125. if (error)
  126. return error;
  127. #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK)
  128. /* let the block class directory show up in the root of sysfs */
  129. if (cls != &block_class)
  130. cls->subsys.kobj.kset = class_kset;
  131. #else
  132. cls->subsys.kobj.kset = class_kset;
  133. #endif
  134. cls->subsys.kobj.ktype = &class_ktype;
  135. error = kset_register(&cls->subsys);
  136. if (!error) {
  137. error = add_class_attrs(class_get(cls));
  138. class_put(cls);
  139. }
  140. return error;
  141. }
  142. void class_unregister(struct class *cls)
  143. {
  144. pr_debug("device class '%s': unregistering\n", cls->name);
  145. remove_class_attrs(cls);
  146. kset_unregister(&cls->subsys);
  147. }
  148. static void class_create_release(struct class *cls)
  149. {
  150. pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
  151. kfree(cls);
  152. }
  153. static void class_device_create_release(struct class_device *class_dev)
  154. {
  155. pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
  156. kfree(class_dev);
  157. }
  158. /* needed to allow these devices to have parent class devices */
  159. static int class_device_create_uevent(struct class_device *class_dev,
  160. struct kobj_uevent_env *env)
  161. {
  162. pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
  163. return 0;
  164. }
  165. /**
  166. * class_create - create a struct class structure
  167. * @owner: pointer to the module that is to "own" this struct class
  168. * @name: pointer to a string for the name of this class.
  169. *
  170. * This is used to create a struct class pointer that can then be used
  171. * in calls to class_device_create().
  172. *
  173. * Note, the pointer created here is to be destroyed when finished by
  174. * making a call to class_destroy().
  175. */
  176. struct class *class_create(struct module *owner, const char *name)
  177. {
  178. struct class *cls;
  179. int retval;
  180. cls = kzalloc(sizeof(*cls), GFP_KERNEL);
  181. if (!cls) {
  182. retval = -ENOMEM;
  183. goto error;
  184. }
  185. cls->name = name;
  186. cls->owner = owner;
  187. cls->class_release = class_create_release;
  188. cls->release = class_device_create_release;
  189. retval = class_register(cls);
  190. if (retval)
  191. goto error;
  192. return cls;
  193. error:
  194. kfree(cls);
  195. return ERR_PTR(retval);
  196. }
  197. /**
  198. * class_destroy - destroys a struct class structure
  199. * @cls: pointer to the struct class that is to be destroyed
  200. *
  201. * Note, the pointer to be destroyed must have been created with a call
  202. * to class_create().
  203. */
  204. void class_destroy(struct class *cls)
  205. {
  206. if ((cls == NULL) || (IS_ERR(cls)))
  207. return;
  208. class_unregister(cls);
  209. }
  210. /* Class Device Stuff */
  211. int class_device_create_file(struct class_device *class_dev,
  212. const struct class_device_attribute *attr)
  213. {
  214. int error = -EINVAL;
  215. if (class_dev)
  216. error = sysfs_create_file(&class_dev->kobj, &attr->attr);
  217. return error;
  218. }
  219. void class_device_remove_file(struct class_device *class_dev,
  220. const struct class_device_attribute *attr)
  221. {
  222. if (class_dev)
  223. sysfs_remove_file(&class_dev->kobj, &attr->attr);
  224. }
  225. int class_device_create_bin_file(struct class_device *class_dev,
  226. struct bin_attribute *attr)
  227. {
  228. int error = -EINVAL;
  229. if (class_dev)
  230. error = sysfs_create_bin_file(&class_dev->kobj, attr);
  231. return error;
  232. }
  233. void class_device_remove_bin_file(struct class_device *class_dev,
  234. struct bin_attribute *attr)
  235. {
  236. if (class_dev)
  237. sysfs_remove_bin_file(&class_dev->kobj, attr);
  238. }
  239. static ssize_t class_device_attr_show(struct kobject *kobj,
  240. struct attribute *attr, char *buf)
  241. {
  242. struct class_device_attribute *class_dev_attr = to_class_dev_attr(attr);
  243. struct class_device *cd = to_class_dev(kobj);
  244. ssize_t ret = 0;
  245. if (class_dev_attr->show)
  246. ret = class_dev_attr->show(cd, buf);
  247. return ret;
  248. }
  249. static ssize_t class_device_attr_store(struct kobject *kobj,
  250. struct attribute *attr,
  251. const char *buf, size_t count)
  252. {
  253. struct class_device_attribute *class_dev_attr = to_class_dev_attr(attr);
  254. struct class_device *cd = to_class_dev(kobj);
  255. ssize_t ret = 0;
  256. if (class_dev_attr->store)
  257. ret = class_dev_attr->store(cd, buf, count);
  258. return ret;
  259. }
  260. static struct sysfs_ops class_dev_sysfs_ops = {
  261. .show = class_device_attr_show,
  262. .store = class_device_attr_store,
  263. };
  264. static void class_dev_release(struct kobject *kobj)
  265. {
  266. struct class_device *cd = to_class_dev(kobj);
  267. struct class *cls = cd->class;
  268. pr_debug("device class '%s': release.\n", cd->class_id);
  269. if (cd->release)
  270. cd->release(cd);
  271. else if (cls->release)
  272. cls->release(cd);
  273. else {
  274. printk(KERN_ERR "Class Device '%s' does not have a release() "
  275. "function, it is broken and must be fixed.\n",
  276. cd->class_id);
  277. WARN_ON(1);
  278. }
  279. }
  280. static struct kobj_type class_device_ktype = {
  281. .sysfs_ops = &class_dev_sysfs_ops,
  282. .release = class_dev_release,
  283. };
  284. static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
  285. {
  286. struct kobj_type *ktype = get_ktype(kobj);
  287. if (ktype == &class_device_ktype) {
  288. struct class_device *class_dev = to_class_dev(kobj);
  289. if (class_dev->class)
  290. return 1;
  291. }
  292. return 0;
  293. }
  294. static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
  295. {
  296. struct class_device *class_dev = to_class_dev(kobj);
  297. return class_dev->class->name;
  298. }
  299. #ifdef CONFIG_SYSFS_DEPRECATED
  300. char *make_class_name(const char *name, struct kobject *kobj)
  301. {
  302. char *class_name;
  303. int size;
  304. size = strlen(name) + strlen(kobject_name(kobj)) + 2;
  305. class_name = kmalloc(size, GFP_KERNEL);
  306. if (!class_name)
  307. return NULL;
  308. strcpy(class_name, name);
  309. strcat(class_name, ":");
  310. strcat(class_name, kobject_name(kobj));
  311. return class_name;
  312. }
  313. static int make_deprecated_class_device_links(struct class_device *class_dev)
  314. {
  315. char *class_name;
  316. int error;
  317. if (!class_dev->dev)
  318. return 0;
  319. class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
  320. if (class_name)
  321. error = sysfs_create_link(&class_dev->dev->kobj,
  322. &class_dev->kobj, class_name);
  323. else
  324. error = -ENOMEM;
  325. kfree(class_name);
  326. return error;
  327. }
  328. static void remove_deprecated_class_device_links(struct class_device *class_dev)
  329. {
  330. char *class_name;
  331. if (!class_dev->dev)
  332. return;
  333. class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
  334. if (class_name)
  335. sysfs_remove_link(&class_dev->dev->kobj, class_name);
  336. kfree(class_name);
  337. }
  338. #else
  339. static inline int make_deprecated_class_device_links(struct class_device *cd)
  340. { return 0; }
  341. static void remove_deprecated_class_device_links(struct class_device *cd)
  342. { }
  343. #endif
  344. static int class_uevent(struct kset *kset, struct kobject *kobj,
  345. struct kobj_uevent_env *env)
  346. {
  347. struct class_device *class_dev = to_class_dev(kobj);
  348. struct device *dev = class_dev->dev;
  349. int retval = 0;
  350. pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
  351. if (MAJOR(class_dev->devt)) {
  352. add_uevent_var(env, "MAJOR=%u", MAJOR(class_dev->devt));
  353. add_uevent_var(env, "MINOR=%u", MINOR(class_dev->devt));
  354. }
  355. if (dev) {
  356. const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
  357. if (path) {
  358. add_uevent_var(env, "PHYSDEVPATH=%s", path);
  359. kfree(path);
  360. }
  361. if (dev->bus)
  362. add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
  363. if (dev->driver)
  364. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  365. dev->driver->name);
  366. }
  367. if (class_dev->uevent) {
  368. /* have the class device specific function add its stuff */
  369. retval = class_dev->uevent(class_dev, env);
  370. if (retval)
  371. pr_debug("class_dev->uevent() returned %d\n", retval);
  372. } else if (class_dev->class->uevent) {
  373. /* have the class specific function add its stuff */
  374. retval = class_dev->class->uevent(class_dev, env);
  375. if (retval)
  376. pr_debug("class->uevent() returned %d\n", retval);
  377. }
  378. return retval;
  379. }
  380. static struct kset_uevent_ops class_uevent_ops = {
  381. .filter = class_uevent_filter,
  382. .name = class_uevent_name,
  383. .uevent = class_uevent,
  384. };
  385. /*
  386. * DO NOT copy how this is created, kset_create_and_add() should be
  387. * called, but this is a hold-over from the old-way and will be deleted
  388. * entirely soon.
  389. */
  390. static struct kset class_obj_subsys = {
  391. .uevent_ops = &class_uevent_ops,
  392. };
  393. static int class_device_add_attrs(struct class_device *cd)
  394. {
  395. int i;
  396. int error = 0;
  397. struct class *cls = cd->class;
  398. if (cls->class_dev_attrs) {
  399. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
  400. error = class_device_create_file(cd,
  401. &cls->class_dev_attrs[i]);
  402. if (error)
  403. goto err;
  404. }
  405. }
  406. done:
  407. return error;
  408. err:
  409. while (--i >= 0)
  410. class_device_remove_file(cd, &cls->class_dev_attrs[i]);
  411. goto done;
  412. }
  413. static void class_device_remove_attrs(struct class_device *cd)
  414. {
  415. int i;
  416. struct class *cls = cd->class;
  417. if (cls->class_dev_attrs) {
  418. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
  419. class_device_remove_file(cd, &cls->class_dev_attrs[i]);
  420. }
  421. }
  422. static int class_device_add_groups(struct class_device *cd)
  423. {
  424. int i;
  425. int error = 0;
  426. if (cd->groups) {
  427. for (i = 0; cd->groups[i]; i++) {
  428. error = sysfs_create_group(&cd->kobj, cd->groups[i]);
  429. if (error) {
  430. while (--i >= 0)
  431. sysfs_remove_group(&cd->kobj,
  432. cd->groups[i]);
  433. goto out;
  434. }
  435. }
  436. }
  437. out:
  438. return error;
  439. }
  440. static void class_device_remove_groups(struct class_device *cd)
  441. {
  442. int i;
  443. if (cd->groups)
  444. for (i = 0; cd->groups[i]; i++)
  445. sysfs_remove_group(&cd->kobj, cd->groups[i]);
  446. }
  447. static ssize_t show_dev(struct class_device *class_dev, char *buf)
  448. {
  449. return print_dev_t(buf, class_dev->devt);
  450. }
  451. static struct class_device_attribute class_devt_attr =
  452. __ATTR(dev, S_IRUGO, show_dev, NULL);
  453. static ssize_t store_uevent(struct class_device *class_dev,
  454. const char *buf, size_t count)
  455. {
  456. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  457. return count;
  458. }
  459. static struct class_device_attribute class_uevent_attr =
  460. __ATTR(uevent, S_IWUSR, NULL, store_uevent);
  461. void class_device_initialize(struct class_device *class_dev)
  462. {
  463. class_dev->kobj.kset = &class_obj_subsys;
  464. kobject_init(&class_dev->kobj, &class_device_ktype);
  465. INIT_LIST_HEAD(&class_dev->node);
  466. }
  467. int class_device_add(struct class_device *class_dev)
  468. {
  469. struct class *parent_class = NULL;
  470. struct class_device *parent_class_dev = NULL;
  471. struct class_interface *class_intf;
  472. int error = -EINVAL;
  473. class_dev = class_device_get(class_dev);
  474. if (!class_dev)
  475. return -EINVAL;
  476. if (!strlen(class_dev->class_id))
  477. goto out1;
  478. parent_class = class_get(class_dev->class);
  479. if (!parent_class)
  480. goto out1;
  481. parent_class_dev = class_device_get(class_dev->parent);
  482. pr_debug("CLASS: registering class device: ID = '%s'\n",
  483. class_dev->class_id);
  484. /* first, register with generic layer. */
  485. if (parent_class_dev)
  486. class_dev->kobj.parent = &parent_class_dev->kobj;
  487. else
  488. class_dev->kobj.parent = &parent_class->subsys.kobj;
  489. error = kobject_add(&class_dev->kobj, class_dev->kobj.parent,
  490. "%s", class_dev->class_id);
  491. if (error)
  492. goto out2;
  493. /* add the needed attributes to this device */
  494. error = sysfs_create_link(&class_dev->kobj,
  495. &parent_class->subsys.kobj, "subsystem");
  496. if (error)
  497. goto out3;
  498. error = class_device_create_file(class_dev, &class_uevent_attr);
  499. if (error)
  500. goto out3;
  501. if (MAJOR(class_dev->devt)) {
  502. error = class_device_create_file(class_dev, &class_devt_attr);
  503. if (error)
  504. goto out4;
  505. }
  506. error = class_device_add_attrs(class_dev);
  507. if (error)
  508. goto out5;
  509. if (class_dev->dev) {
  510. error = sysfs_create_link(&class_dev->kobj,
  511. &class_dev->dev->kobj, "device");
  512. if (error)
  513. goto out6;
  514. }
  515. error = class_device_add_groups(class_dev);
  516. if (error)
  517. goto out7;
  518. error = make_deprecated_class_device_links(class_dev);
  519. if (error)
  520. goto out8;
  521. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  522. /* notify any interfaces this device is now here */
  523. down(&parent_class->sem);
  524. list_add_tail(&class_dev->node, &parent_class->children);
  525. list_for_each_entry(class_intf, &parent_class->interfaces, node) {
  526. if (class_intf->add)
  527. class_intf->add(class_dev, class_intf);
  528. }
  529. up(&parent_class->sem);
  530. goto out1;
  531. out8:
  532. class_device_remove_groups(class_dev);
  533. out7:
  534. if (class_dev->dev)
  535. sysfs_remove_link(&class_dev->kobj, "device");
  536. out6:
  537. class_device_remove_attrs(class_dev);
  538. out5:
  539. if (MAJOR(class_dev->devt))
  540. class_device_remove_file(class_dev, &class_devt_attr);
  541. out4:
  542. class_device_remove_file(class_dev, &class_uevent_attr);
  543. out3:
  544. kobject_del(&class_dev->kobj);
  545. out2:
  546. if (parent_class_dev)
  547. class_device_put(parent_class_dev);
  548. class_put(parent_class);
  549. out1:
  550. class_device_put(class_dev);
  551. return error;
  552. }
  553. int class_device_register(struct class_device *class_dev)
  554. {
  555. class_device_initialize(class_dev);
  556. return class_device_add(class_dev);
  557. }
  558. /**
  559. * class_device_create - creates a class device and registers it with sysfs
  560. * @cls: pointer to the struct class that this device should be registered to.
  561. * @parent: pointer to the parent struct class_device of this new device, if
  562. * any.
  563. * @devt: the dev_t for the char device to be added.
  564. * @device: a pointer to a struct device that is assiociated with this class
  565. * device.
  566. * @fmt: string for the class device's name
  567. *
  568. * This function can be used by char device classes. A struct
  569. * class_device will be created in sysfs, registered to the specified
  570. * class.
  571. * A "dev" file will be created, showing the dev_t for the device, if
  572. * the dev_t is not 0,0.
  573. * If a pointer to a parent struct class_device is passed in, the newly
  574. * created struct class_device will be a child of that device in sysfs.
  575. * The pointer to the struct class_device will be returned from the
  576. * call. Any further sysfs files that might be required can be created
  577. * using this pointer.
  578. *
  579. * Note: the struct class passed to this function must have previously
  580. * been created with a call to class_create().
  581. */
  582. struct class_device *class_device_create(struct class *cls,
  583. struct class_device *parent,
  584. dev_t devt,
  585. struct device *device,
  586. const char *fmt, ...)
  587. {
  588. va_list args;
  589. struct class_device *class_dev = NULL;
  590. int retval = -ENODEV;
  591. if (cls == NULL || IS_ERR(cls))
  592. goto error;
  593. class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
  594. if (!class_dev) {
  595. retval = -ENOMEM;
  596. goto error;
  597. }
  598. class_dev->devt = devt;
  599. class_dev->dev = device;
  600. class_dev->class = cls;
  601. class_dev->parent = parent;
  602. class_dev->release = class_device_create_release;
  603. class_dev->uevent = class_device_create_uevent;
  604. va_start(args, fmt);
  605. vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
  606. va_end(args);
  607. retval = class_device_register(class_dev);
  608. if (retval)
  609. goto error;
  610. return class_dev;
  611. error:
  612. kfree(class_dev);
  613. return ERR_PTR(retval);
  614. }
  615. void class_device_del(struct class_device *class_dev)
  616. {
  617. struct class *parent_class = class_dev->class;
  618. struct class_device *parent_device = class_dev->parent;
  619. struct class_interface *class_intf;
  620. if (parent_class) {
  621. down(&parent_class->sem);
  622. list_del_init(&class_dev->node);
  623. list_for_each_entry(class_intf, &parent_class->interfaces, node)
  624. if (class_intf->remove)
  625. class_intf->remove(class_dev, class_intf);
  626. up(&parent_class->sem);
  627. }
  628. if (class_dev->dev) {
  629. remove_deprecated_class_device_links(class_dev);
  630. sysfs_remove_link(&class_dev->kobj, "device");
  631. }
  632. sysfs_remove_link(&class_dev->kobj, "subsystem");
  633. class_device_remove_file(class_dev, &class_uevent_attr);
  634. if (MAJOR(class_dev->devt))
  635. class_device_remove_file(class_dev, &class_devt_attr);
  636. class_device_remove_attrs(class_dev);
  637. class_device_remove_groups(class_dev);
  638. kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
  639. kobject_del(&class_dev->kobj);
  640. class_device_put(parent_device);
  641. class_put(parent_class);
  642. }
  643. void class_device_unregister(struct class_device *class_dev)
  644. {
  645. pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
  646. class_dev->class_id);
  647. class_device_del(class_dev);
  648. class_device_put(class_dev);
  649. }
  650. /**
  651. * class_device_destroy - removes a class device that was created with class_device_create()
  652. * @cls: the pointer to the struct class that this device was registered * with.
  653. * @devt: the dev_t of the device that was previously registered.
  654. *
  655. * This call unregisters and cleans up a class device that was created with a
  656. * call to class_device_create()
  657. */
  658. void class_device_destroy(struct class *cls, dev_t devt)
  659. {
  660. struct class_device *class_dev = NULL;
  661. struct class_device *class_dev_tmp;
  662. down(&cls->sem);
  663. list_for_each_entry(class_dev_tmp, &cls->children, node) {
  664. if (class_dev_tmp->devt == devt) {
  665. class_dev = class_dev_tmp;
  666. break;
  667. }
  668. }
  669. up(&cls->sem);
  670. if (class_dev)
  671. class_device_unregister(class_dev);
  672. }
  673. struct class_device *class_device_get(struct class_device *class_dev)
  674. {
  675. if (class_dev)
  676. return to_class_dev(kobject_get(&class_dev->kobj));
  677. return NULL;
  678. }
  679. void class_device_put(struct class_device *class_dev)
  680. {
  681. if (class_dev)
  682. kobject_put(&class_dev->kobj);
  683. }
  684. /**
  685. * class_for_each_device - device iterator
  686. * @class: the class we're iterating
  687. * @data: data for the callback
  688. * @fn: function to be called for each device
  689. *
  690. * Iterate over @class's list of devices, and call @fn for each,
  691. * passing it @data.
  692. *
  693. * We check the return of @fn each time. If it returns anything
  694. * other than 0, we break out and return that value.
  695. *
  696. * Note, we hold class->sem in this function, so it can not be
  697. * re-acquired in @fn, otherwise it will self-deadlocking. For
  698. * example, calls to add or remove class members would be verboten.
  699. */
  700. int class_for_each_device(struct class *class, void *data,
  701. int (*fn)(struct device *, void *))
  702. {
  703. struct device *dev;
  704. int error = 0;
  705. if (!class)
  706. return -EINVAL;
  707. down(&class->sem);
  708. list_for_each_entry(dev, &class->devices, node) {
  709. dev = get_device(dev);
  710. if (dev) {
  711. error = fn(dev, data);
  712. put_device(dev);
  713. } else
  714. error = -ENODEV;
  715. if (error)
  716. break;
  717. }
  718. up(&class->sem);
  719. return error;
  720. }
  721. EXPORT_SYMBOL_GPL(class_for_each_device);
  722. /**
  723. * class_find_device - device iterator for locating a particular device
  724. * @class: the class we're iterating
  725. * @data: data for the match function
  726. * @match: function to check device
  727. *
  728. * This is similar to the class_for_each_dev() function above, but it
  729. * returns a reference to a device that is 'found' for later use, as
  730. * determined by the @match callback.
  731. *
  732. * The callback should return 0 if the device doesn't match and non-zero
  733. * if it does. If the callback returns non-zero, this function will
  734. * return to the caller and not iterate over any more devices.
  735. *
  736. * Note, you will need to drop the reference with put_device() after use.
  737. *
  738. * We hold class->sem in this function, so it can not be
  739. * re-acquired in @match, otherwise it will self-deadlocking. For
  740. * example, calls to add or remove class members would be verboten.
  741. */
  742. struct device *class_find_device(struct class *class, void *data,
  743. int (*match)(struct device *, void *))
  744. {
  745. struct device *dev;
  746. int found = 0;
  747. if (!class)
  748. return NULL;
  749. down(&class->sem);
  750. list_for_each_entry(dev, &class->devices, node) {
  751. dev = get_device(dev);
  752. if (dev) {
  753. if (match(dev, data)) {
  754. found = 1;
  755. break;
  756. } else
  757. put_device(dev);
  758. } else
  759. break;
  760. }
  761. up(&class->sem);
  762. return found ? dev : NULL;
  763. }
  764. EXPORT_SYMBOL_GPL(class_find_device);
  765. /**
  766. * class_find_child - device iterator for locating a particular class_device
  767. * @class: the class we're iterating
  768. * @data: data for the match function
  769. * @match: function to check class_device
  770. *
  771. * This function returns a reference to a class_device that is 'found' for
  772. * later use, as determined by the @match callback.
  773. *
  774. * The callback should return 0 if the class_device doesn't match and non-zero
  775. * if it does. If the callback returns non-zero, this function will
  776. * return to the caller and not iterate over any more class_devices.
  777. *
  778. * Note, you will need to drop the reference with class_device_put() after use.
  779. *
  780. * We hold class->sem in this function, so it can not be
  781. * re-acquired in @match, otherwise it will self-deadlocking. For
  782. * example, calls to add or remove class members would be verboten.
  783. */
  784. struct class_device *class_find_child(struct class *class, void *data,
  785. int (*match)(struct class_device *, void *))
  786. {
  787. struct class_device *dev;
  788. int found = 0;
  789. if (!class)
  790. return NULL;
  791. down(&class->sem);
  792. list_for_each_entry(dev, &class->children, node) {
  793. dev = class_device_get(dev);
  794. if (dev) {
  795. if (match(dev, data)) {
  796. found = 1;
  797. break;
  798. } else
  799. class_device_put(dev);
  800. } else
  801. break;
  802. }
  803. up(&class->sem);
  804. return found ? dev : NULL;
  805. }
  806. EXPORT_SYMBOL_GPL(class_find_child);
  807. int class_interface_register(struct class_interface *class_intf)
  808. {
  809. struct class *parent;
  810. struct class_device *class_dev;
  811. struct device *dev;
  812. if (!class_intf || !class_intf->class)
  813. return -ENODEV;
  814. parent = class_get(class_intf->class);
  815. if (!parent)
  816. return -EINVAL;
  817. down(&parent->sem);
  818. list_add_tail(&class_intf->node, &parent->interfaces);
  819. if (class_intf->add) {
  820. list_for_each_entry(class_dev, &parent->children, node)
  821. class_intf->add(class_dev, class_intf);
  822. }
  823. if (class_intf->add_dev) {
  824. list_for_each_entry(dev, &parent->devices, node)
  825. class_intf->add_dev(dev, class_intf);
  826. }
  827. up(&parent->sem);
  828. return 0;
  829. }
  830. void class_interface_unregister(struct class_interface *class_intf)
  831. {
  832. struct class *parent = class_intf->class;
  833. struct class_device *class_dev;
  834. struct device *dev;
  835. if (!parent)
  836. return;
  837. down(&parent->sem);
  838. list_del_init(&class_intf->node);
  839. if (class_intf->remove) {
  840. list_for_each_entry(class_dev, &parent->children, node)
  841. class_intf->remove(class_dev, class_intf);
  842. }
  843. if (class_intf->remove_dev) {
  844. list_for_each_entry(dev, &parent->devices, node)
  845. class_intf->remove_dev(dev, class_intf);
  846. }
  847. up(&parent->sem);
  848. class_put(parent);
  849. }
  850. int __init classes_init(void)
  851. {
  852. class_kset = kset_create_and_add("class", NULL, NULL);
  853. if (!class_kset)
  854. return -ENOMEM;
  855. /* ick, this is ugly, the things we go through to keep from showing up
  856. * in sysfs... */
  857. kset_init(&class_obj_subsys);
  858. kobject_set_name(&class_obj_subsys.kobj, "class_obj");
  859. if (!class_obj_subsys.kobj.parent)
  860. class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
  861. return 0;
  862. }
  863. EXPORT_SYMBOL_GPL(class_create_file);
  864. EXPORT_SYMBOL_GPL(class_remove_file);
  865. EXPORT_SYMBOL_GPL(class_register);
  866. EXPORT_SYMBOL_GPL(class_unregister);
  867. EXPORT_SYMBOL_GPL(class_create);
  868. EXPORT_SYMBOL_GPL(class_destroy);
  869. EXPORT_SYMBOL_GPL(class_device_register);
  870. EXPORT_SYMBOL_GPL(class_device_unregister);
  871. EXPORT_SYMBOL_GPL(class_device_initialize);
  872. EXPORT_SYMBOL_GPL(class_device_add);
  873. EXPORT_SYMBOL_GPL(class_device_del);
  874. EXPORT_SYMBOL_GPL(class_device_get);
  875. EXPORT_SYMBOL_GPL(class_device_put);
  876. EXPORT_SYMBOL_GPL(class_device_create);
  877. EXPORT_SYMBOL_GPL(class_device_destroy);
  878. EXPORT_SYMBOL_GPL(class_device_create_file);
  879. EXPORT_SYMBOL_GPL(class_device_remove_file);
  880. EXPORT_SYMBOL_GPL(class_device_create_bin_file);
  881. EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
  882. EXPORT_SYMBOL_GPL(class_interface_register);
  883. EXPORT_SYMBOL_GPL(class_interface_unregister);