class.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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 "base.h"
  20. #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
  21. #define to_class(obj) container_of(obj, struct class, subsys.kobj)
  22. static ssize_t
  23. class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  24. {
  25. struct class_attribute * class_attr = to_class_attr(attr);
  26. struct class * dc = to_class(kobj);
  27. ssize_t ret = -EIO;
  28. if (class_attr->show)
  29. ret = class_attr->show(dc, buf);
  30. return ret;
  31. }
  32. static ssize_t
  33. 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), struct class, subsys);
  81. return NULL;
  82. }
  83. static void class_put(struct class * cls)
  84. {
  85. if (cls)
  86. kset_put(&cls->subsys);
  87. }
  88. static int add_class_attrs(struct class * cls)
  89. {
  90. int i;
  91. int error = 0;
  92. if (cls->class_attrs) {
  93. for (i = 0; attr_name(cls->class_attrs[i]); i++) {
  94. error = class_create_file(cls,&cls->class_attrs[i]);
  95. if (error)
  96. goto Err;
  97. }
  98. }
  99. Done:
  100. return error;
  101. Err:
  102. while (--i >= 0)
  103. class_remove_file(cls,&cls->class_attrs[i]);
  104. goto Done;
  105. }
  106. static void remove_class_attrs(struct class * cls)
  107. {
  108. int i;
  109. if (cls->class_attrs) {
  110. for (i = 0; attr_name(cls->class_attrs[i]); i++)
  111. class_remove_file(cls,&cls->class_attrs[i]);
  112. }
  113. }
  114. int class_register(struct class * cls)
  115. {
  116. int error;
  117. pr_debug("device class '%s': registering\n", cls->name);
  118. INIT_LIST_HEAD(&cls->children);
  119. INIT_LIST_HEAD(&cls->devices);
  120. INIT_LIST_HEAD(&cls->interfaces);
  121. kset_init(&cls->class_dirs);
  122. init_MUTEX(&cls->sem);
  123. error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name);
  124. if (error)
  125. return error;
  126. cls->subsys.kobj.kset = class_kset;
  127. cls->subsys.kobj.ktype = &class_ktype;
  128. error = kset_register(&cls->subsys);
  129. if (!error) {
  130. error = add_class_attrs(class_get(cls));
  131. class_put(cls);
  132. }
  133. return error;
  134. }
  135. void class_unregister(struct class * cls)
  136. {
  137. pr_debug("device class '%s': unregistering\n", cls->name);
  138. remove_class_attrs(cls);
  139. kset_unregister(&cls->subsys);
  140. }
  141. static void class_create_release(struct class *cls)
  142. {
  143. pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
  144. kfree(cls);
  145. }
  146. static void class_device_create_release(struct class_device *class_dev)
  147. {
  148. pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
  149. kfree(class_dev);
  150. }
  151. /* needed to allow these devices to have parent class devices */
  152. static int class_device_create_uevent(struct class_device *class_dev,
  153. struct kobj_uevent_env *env)
  154. {
  155. pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
  156. return 0;
  157. }
  158. /**
  159. * class_create - create a struct class structure
  160. * @owner: pointer to the module that is to "own" this struct class
  161. * @name: pointer to a string for the name of this class.
  162. *
  163. * This is used to create a struct class pointer that can then be used
  164. * in calls to class_device_create().
  165. *
  166. * Note, the pointer created here is to be destroyed when finished by
  167. * making a call to class_destroy().
  168. */
  169. struct class *class_create(struct module *owner, const char *name)
  170. {
  171. struct class *cls;
  172. int retval;
  173. cls = kzalloc(sizeof(*cls), GFP_KERNEL);
  174. if (!cls) {
  175. retval = -ENOMEM;
  176. goto error;
  177. }
  178. cls->name = name;
  179. cls->owner = owner;
  180. cls->class_release = class_create_release;
  181. cls->release = class_device_create_release;
  182. retval = class_register(cls);
  183. if (retval)
  184. goto error;
  185. return cls;
  186. error:
  187. kfree(cls);
  188. return ERR_PTR(retval);
  189. }
  190. /**
  191. * class_destroy - destroys a struct class structure
  192. * @cls: pointer to the struct class that is to be destroyed
  193. *
  194. * Note, the pointer to be destroyed must have been created with a call
  195. * to class_create().
  196. */
  197. void class_destroy(struct class *cls)
  198. {
  199. if ((cls == NULL) || (IS_ERR(cls)))
  200. return;
  201. class_unregister(cls);
  202. }
  203. /* Class Device Stuff */
  204. int class_device_create_file(struct class_device * class_dev,
  205. const struct class_device_attribute * attr)
  206. {
  207. int error = -EINVAL;
  208. if (class_dev)
  209. error = sysfs_create_file(&class_dev->kobj, &attr->attr);
  210. return error;
  211. }
  212. void class_device_remove_file(struct class_device * class_dev,
  213. const struct class_device_attribute * attr)
  214. {
  215. if (class_dev)
  216. sysfs_remove_file(&class_dev->kobj, &attr->attr);
  217. }
  218. int class_device_create_bin_file(struct class_device *class_dev,
  219. struct bin_attribute *attr)
  220. {
  221. int error = -EINVAL;
  222. if (class_dev)
  223. error = sysfs_create_bin_file(&class_dev->kobj, attr);
  224. return error;
  225. }
  226. void class_device_remove_bin_file(struct class_device *class_dev,
  227. struct bin_attribute *attr)
  228. {
  229. if (class_dev)
  230. sysfs_remove_bin_file(&class_dev->kobj, attr);
  231. }
  232. static ssize_t
  233. class_device_attr_show(struct kobject * kobj, struct attribute * attr,
  234. char * buf)
  235. {
  236. struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
  237. struct class_device * cd = to_class_dev(kobj);
  238. ssize_t ret = 0;
  239. if (class_dev_attr->show)
  240. ret = class_dev_attr->show(cd, buf);
  241. return ret;
  242. }
  243. static ssize_t
  244. class_device_attr_store(struct kobject * kobj, struct attribute * attr,
  245. const char * buf, size_t count)
  246. {
  247. struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
  248. struct class_device * cd = to_class_dev(kobj);
  249. ssize_t ret = 0;
  250. if (class_dev_attr->store)
  251. ret = class_dev_attr->store(cd, buf, count);
  252. return ret;
  253. }
  254. static struct sysfs_ops class_dev_sysfs_ops = {
  255. .show = class_device_attr_show,
  256. .store = class_device_attr_store,
  257. };
  258. static void class_dev_release(struct kobject * kobj)
  259. {
  260. struct class_device *cd = to_class_dev(kobj);
  261. struct class * cls = cd->class;
  262. pr_debug("device class '%s': release.\n", cd->class_id);
  263. if (cd->release)
  264. cd->release(cd);
  265. else if (cls->release)
  266. cls->release(cd);
  267. else {
  268. printk(KERN_ERR "Class Device '%s' does not have a release() function, "
  269. "it is broken and must be fixed.\n",
  270. cd->class_id);
  271. WARN_ON(1);
  272. }
  273. }
  274. static struct kobj_type class_device_ktype = {
  275. .sysfs_ops = &class_dev_sysfs_ops,
  276. .release = class_dev_release,
  277. };
  278. static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
  279. {
  280. struct kobj_type *ktype = get_ktype(kobj);
  281. if (ktype == &class_device_ktype) {
  282. struct class_device *class_dev = to_class_dev(kobj);
  283. if (class_dev->class)
  284. return 1;
  285. }
  286. return 0;
  287. }
  288. static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
  289. {
  290. struct class_device *class_dev = to_class_dev(kobj);
  291. return class_dev->class->name;
  292. }
  293. #ifdef CONFIG_SYSFS_DEPRECATED
  294. char *make_class_name(const char *name, struct kobject *kobj)
  295. {
  296. char *class_name;
  297. int size;
  298. size = strlen(name) + strlen(kobject_name(kobj)) + 2;
  299. class_name = kmalloc(size, GFP_KERNEL);
  300. if (!class_name)
  301. return NULL;
  302. strcpy(class_name, name);
  303. strcat(class_name, ":");
  304. strcat(class_name, kobject_name(kobj));
  305. return class_name;
  306. }
  307. static int make_deprecated_class_device_links(struct class_device *class_dev)
  308. {
  309. char *class_name;
  310. int error;
  311. if (!class_dev->dev)
  312. return 0;
  313. class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
  314. if (class_name)
  315. error = sysfs_create_link(&class_dev->dev->kobj,
  316. &class_dev->kobj, class_name);
  317. else
  318. error = -ENOMEM;
  319. kfree(class_name);
  320. return error;
  321. }
  322. static void remove_deprecated_class_device_links(struct class_device *class_dev)
  323. {
  324. char *class_name;
  325. if (!class_dev->dev)
  326. return;
  327. class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
  328. if (class_name)
  329. sysfs_remove_link(&class_dev->dev->kobj, class_name);
  330. kfree(class_name);
  331. }
  332. #else
  333. static inline int make_deprecated_class_device_links(struct class_device *cd)
  334. { return 0; }
  335. static void remove_deprecated_class_device_links(struct class_device *cd)
  336. { }
  337. #endif
  338. static int class_uevent(struct kset *kset, struct kobject *kobj,
  339. struct kobj_uevent_env *env)
  340. {
  341. struct class_device *class_dev = to_class_dev(kobj);
  342. struct device *dev = class_dev->dev;
  343. int retval = 0;
  344. pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
  345. if (MAJOR(class_dev->devt)) {
  346. add_uevent_var(env, "MAJOR=%u", MAJOR(class_dev->devt));
  347. add_uevent_var(env, "MINOR=%u", MINOR(class_dev->devt));
  348. }
  349. if (dev) {
  350. const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
  351. if (path) {
  352. add_uevent_var(env, "PHYSDEVPATH=%s", path);
  353. kfree(path);
  354. }
  355. if (dev->bus)
  356. add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
  357. if (dev->driver)
  358. add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
  359. }
  360. if (class_dev->uevent) {
  361. /* have the class device specific function add its stuff */
  362. retval = class_dev->uevent(class_dev, env);
  363. if (retval)
  364. pr_debug("class_dev->uevent() returned %d\n", retval);
  365. } else if (class_dev->class->uevent) {
  366. /* have the class specific function add its stuff */
  367. retval = class_dev->class->uevent(class_dev, env);
  368. if (retval)
  369. pr_debug("class->uevent() returned %d\n", retval);
  370. }
  371. return retval;
  372. }
  373. static struct kset_uevent_ops class_uevent_ops = {
  374. .filter = class_uevent_filter,
  375. .name = class_uevent_name,
  376. .uevent = class_uevent,
  377. };
  378. /*
  379. * DO NOT copy how this is created, kset_create_and_add() should be
  380. * called, but this is a hold-over from the old-way and will be deleted
  381. * entirely soon.
  382. */
  383. static struct kset class_obj_subsys = {
  384. .kobj = { .k_name = "class_obj", },
  385. .uevent_ops = &class_uevent_ops,
  386. };
  387. static int class_device_add_attrs(struct class_device * cd)
  388. {
  389. int i;
  390. int error = 0;
  391. struct class * cls = cd->class;
  392. if (cls->class_dev_attrs) {
  393. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
  394. error = class_device_create_file(cd,
  395. &cls->class_dev_attrs[i]);
  396. if (error)
  397. goto Err;
  398. }
  399. }
  400. Done:
  401. return error;
  402. Err:
  403. while (--i >= 0)
  404. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  405. goto Done;
  406. }
  407. static void class_device_remove_attrs(struct class_device * cd)
  408. {
  409. int i;
  410. struct class * cls = cd->class;
  411. if (cls->class_dev_attrs) {
  412. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
  413. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  414. }
  415. }
  416. static int class_device_add_groups(struct class_device * cd)
  417. {
  418. int i;
  419. int error = 0;
  420. if (cd->groups) {
  421. for (i = 0; cd->groups[i]; i++) {
  422. error = sysfs_create_group(&cd->kobj, cd->groups[i]);
  423. if (error) {
  424. while (--i >= 0)
  425. sysfs_remove_group(&cd->kobj, cd->groups[i]);
  426. goto out;
  427. }
  428. }
  429. }
  430. out:
  431. return error;
  432. }
  433. static void class_device_remove_groups(struct class_device * cd)
  434. {
  435. int i;
  436. if (cd->groups) {
  437. for (i = 0; cd->groups[i]; i++) {
  438. sysfs_remove_group(&cd->kobj, cd->groups[i]);
  439. }
  440. }
  441. }
  442. static ssize_t show_dev(struct class_device *class_dev, char *buf)
  443. {
  444. return print_dev_t(buf, class_dev->devt);
  445. }
  446. static struct class_device_attribute class_devt_attr =
  447. __ATTR(dev, S_IRUGO, show_dev, NULL);
  448. static ssize_t store_uevent(struct class_device *class_dev,
  449. const char *buf, size_t count)
  450. {
  451. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  452. return count;
  453. }
  454. static struct class_device_attribute class_uevent_attr =
  455. __ATTR(uevent, S_IWUSR, NULL, store_uevent);
  456. void class_device_initialize(struct class_device *class_dev)
  457. {
  458. class_dev->kobj.kset = &class_obj_subsys;
  459. kobject_init_ng(&class_dev->kobj, &class_device_ktype);
  460. INIT_LIST_HEAD(&class_dev->node);
  461. }
  462. int class_device_add(struct class_device *class_dev)
  463. {
  464. struct class *parent_class = NULL;
  465. struct class_device *parent_class_dev = NULL;
  466. struct class_interface *class_intf;
  467. int error = -EINVAL;
  468. class_dev = class_device_get(class_dev);
  469. if (!class_dev)
  470. return -EINVAL;
  471. if (!strlen(class_dev->class_id))
  472. goto out1;
  473. parent_class = class_get(class_dev->class);
  474. if (!parent_class)
  475. goto out1;
  476. parent_class_dev = class_device_get(class_dev->parent);
  477. pr_debug("CLASS: registering class device: ID = '%s'\n",
  478. class_dev->class_id);
  479. /* first, register with generic layer. */
  480. if (parent_class_dev)
  481. class_dev->kobj.parent = &parent_class_dev->kobj;
  482. else
  483. class_dev->kobj.parent = &parent_class->subsys.kobj;
  484. error = kobject_add_ng(&class_dev->kobj, class_dev->kobj.parent,
  485. "%s", class_dev->class_id);
  486. if (error)
  487. goto out2;
  488. /* add the needed attributes to this device */
  489. error = sysfs_create_link(&class_dev->kobj,
  490. &parent_class->subsys.kobj, "subsystem");
  491. if (error)
  492. goto out3;
  493. error = class_device_create_file(class_dev, &class_uevent_attr);
  494. if (error)
  495. goto out3;
  496. if (MAJOR(class_dev->devt)) {
  497. error = class_device_create_file(class_dev, &class_devt_attr);
  498. if (error)
  499. goto out4;
  500. }
  501. error = class_device_add_attrs(class_dev);
  502. if (error)
  503. goto out5;
  504. if (class_dev->dev) {
  505. error = sysfs_create_link(&class_dev->kobj,
  506. &class_dev->dev->kobj, "device");
  507. if (error)
  508. goto out6;
  509. }
  510. error = class_device_add_groups(class_dev);
  511. if (error)
  512. goto out7;
  513. error = make_deprecated_class_device_links(class_dev);
  514. if (error)
  515. goto out8;
  516. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  517. /* notify any interfaces this device is now here */
  518. down(&parent_class->sem);
  519. list_add_tail(&class_dev->node, &parent_class->children);
  520. list_for_each_entry(class_intf, &parent_class->interfaces, node) {
  521. if (class_intf->add)
  522. class_intf->add(class_dev, class_intf);
  523. }
  524. up(&parent_class->sem);
  525. goto out1;
  526. out8:
  527. class_device_remove_groups(class_dev);
  528. out7:
  529. if (class_dev->dev)
  530. sysfs_remove_link(&class_dev->kobj, "device");
  531. out6:
  532. class_device_remove_attrs(class_dev);
  533. out5:
  534. if (MAJOR(class_dev->devt))
  535. class_device_remove_file(class_dev, &class_devt_attr);
  536. out4:
  537. class_device_remove_file(class_dev, &class_uevent_attr);
  538. out3:
  539. kobject_del(&class_dev->kobj);
  540. out2:
  541. if(parent_class_dev)
  542. class_device_put(parent_class_dev);
  543. class_put(parent_class);
  544. out1:
  545. class_device_put(class_dev);
  546. return error;
  547. }
  548. int class_device_register(struct class_device *class_dev)
  549. {
  550. class_device_initialize(class_dev);
  551. return class_device_add(class_dev);
  552. }
  553. /**
  554. * class_device_create - creates a class device and registers it with sysfs
  555. * @cls: pointer to the struct class that this device should be registered to.
  556. * @parent: pointer to the parent struct class_device of this new device, if any.
  557. * @devt: the dev_t for the char device to be added.
  558. * @device: a pointer to a struct device that is assiociated with this class device.
  559. * @fmt: string for the class device's name
  560. *
  561. * This function can be used by char device classes. A struct
  562. * class_device will be created in sysfs, registered to the specified
  563. * class.
  564. * A "dev" file will be created, showing the dev_t for the device, if
  565. * the dev_t is not 0,0.
  566. * If a pointer to a parent struct class_device is passed in, the newly
  567. * created struct class_device will be a child of that device in sysfs.
  568. * The pointer to the struct class_device will be returned from the
  569. * call. Any further sysfs files that might be required can be created
  570. * using this pointer.
  571. *
  572. * Note: the struct class passed to this function must have previously
  573. * been created with a call to class_create().
  574. */
  575. struct class_device *class_device_create(struct class *cls,
  576. struct class_device *parent,
  577. dev_t devt,
  578. struct device *device,
  579. const char *fmt, ...)
  580. {
  581. va_list args;
  582. struct class_device *class_dev = NULL;
  583. int retval = -ENODEV;
  584. if (cls == NULL || IS_ERR(cls))
  585. goto error;
  586. class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
  587. if (!class_dev) {
  588. retval = -ENOMEM;
  589. goto error;
  590. }
  591. class_dev->devt = devt;
  592. class_dev->dev = device;
  593. class_dev->class = cls;
  594. class_dev->parent = parent;
  595. class_dev->release = class_device_create_release;
  596. class_dev->uevent = class_device_create_uevent;
  597. va_start(args, fmt);
  598. vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
  599. va_end(args);
  600. retval = class_device_register(class_dev);
  601. if (retval)
  602. goto error;
  603. return class_dev;
  604. error:
  605. kfree(class_dev);
  606. return ERR_PTR(retval);
  607. }
  608. void class_device_del(struct class_device *class_dev)
  609. {
  610. struct class *parent_class = class_dev->class;
  611. struct class_device *parent_device = class_dev->parent;
  612. struct class_interface *class_intf;
  613. if (parent_class) {
  614. down(&parent_class->sem);
  615. list_del_init(&class_dev->node);
  616. list_for_each_entry(class_intf, &parent_class->interfaces, node)
  617. if (class_intf->remove)
  618. class_intf->remove(class_dev, class_intf);
  619. up(&parent_class->sem);
  620. }
  621. if (class_dev->dev) {
  622. remove_deprecated_class_device_links(class_dev);
  623. sysfs_remove_link(&class_dev->kobj, "device");
  624. }
  625. sysfs_remove_link(&class_dev->kobj, "subsystem");
  626. class_device_remove_file(class_dev, &class_uevent_attr);
  627. if (MAJOR(class_dev->devt))
  628. class_device_remove_file(class_dev, &class_devt_attr);
  629. class_device_remove_attrs(class_dev);
  630. class_device_remove_groups(class_dev);
  631. kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
  632. kobject_del(&class_dev->kobj);
  633. class_device_put(parent_device);
  634. class_put(parent_class);
  635. }
  636. void class_device_unregister(struct class_device *class_dev)
  637. {
  638. pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
  639. class_dev->class_id);
  640. class_device_del(class_dev);
  641. class_device_put(class_dev);
  642. }
  643. /**
  644. * class_device_destroy - removes a class device that was created with class_device_create()
  645. * @cls: the pointer to the struct class that this device was registered * with.
  646. * @devt: the dev_t of the device that was previously registered.
  647. *
  648. * This call unregisters and cleans up a class device that was created with a
  649. * call to class_device_create()
  650. */
  651. void class_device_destroy(struct class *cls, dev_t devt)
  652. {
  653. struct class_device *class_dev = NULL;
  654. struct class_device *class_dev_tmp;
  655. down(&cls->sem);
  656. list_for_each_entry(class_dev_tmp, &cls->children, node) {
  657. if (class_dev_tmp->devt == devt) {
  658. class_dev = class_dev_tmp;
  659. break;
  660. }
  661. }
  662. up(&cls->sem);
  663. if (class_dev)
  664. class_device_unregister(class_dev);
  665. }
  666. struct class_device * class_device_get(struct class_device *class_dev)
  667. {
  668. if (class_dev)
  669. return to_class_dev(kobject_get(&class_dev->kobj));
  670. return NULL;
  671. }
  672. void class_device_put(struct class_device *class_dev)
  673. {
  674. if (class_dev)
  675. kobject_put(&class_dev->kobj);
  676. }
  677. int class_interface_register(struct class_interface *class_intf)
  678. {
  679. struct class *parent;
  680. struct class_device *class_dev;
  681. struct device *dev;
  682. if (!class_intf || !class_intf->class)
  683. return -ENODEV;
  684. parent = class_get(class_intf->class);
  685. if (!parent)
  686. return -EINVAL;
  687. down(&parent->sem);
  688. list_add_tail(&class_intf->node, &parent->interfaces);
  689. if (class_intf->add) {
  690. list_for_each_entry(class_dev, &parent->children, node)
  691. class_intf->add(class_dev, class_intf);
  692. }
  693. if (class_intf->add_dev) {
  694. list_for_each_entry(dev, &parent->devices, node)
  695. class_intf->add_dev(dev, class_intf);
  696. }
  697. up(&parent->sem);
  698. return 0;
  699. }
  700. void class_interface_unregister(struct class_interface *class_intf)
  701. {
  702. struct class * parent = class_intf->class;
  703. struct class_device *class_dev;
  704. struct device *dev;
  705. if (!parent)
  706. return;
  707. down(&parent->sem);
  708. list_del_init(&class_intf->node);
  709. if (class_intf->remove) {
  710. list_for_each_entry(class_dev, &parent->children, node)
  711. class_intf->remove(class_dev, class_intf);
  712. }
  713. if (class_intf->remove_dev) {
  714. list_for_each_entry(dev, &parent->devices, node)
  715. class_intf->remove_dev(dev, class_intf);
  716. }
  717. up(&parent->sem);
  718. class_put(parent);
  719. }
  720. int __init classes_init(void)
  721. {
  722. class_kset = kset_create_and_add("class", NULL, NULL);
  723. if (!class_kset)
  724. return -ENOMEM;
  725. /* ick, this is ugly, the things we go through to keep from showing up
  726. * in sysfs... */
  727. kset_init(&class_obj_subsys);
  728. if (!class_obj_subsys.kobj.parent)
  729. class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
  730. return 0;
  731. }
  732. EXPORT_SYMBOL_GPL(class_create_file);
  733. EXPORT_SYMBOL_GPL(class_remove_file);
  734. EXPORT_SYMBOL_GPL(class_register);
  735. EXPORT_SYMBOL_GPL(class_unregister);
  736. EXPORT_SYMBOL_GPL(class_create);
  737. EXPORT_SYMBOL_GPL(class_destroy);
  738. EXPORT_SYMBOL_GPL(class_device_register);
  739. EXPORT_SYMBOL_GPL(class_device_unregister);
  740. EXPORT_SYMBOL_GPL(class_device_initialize);
  741. EXPORT_SYMBOL_GPL(class_device_add);
  742. EXPORT_SYMBOL_GPL(class_device_del);
  743. EXPORT_SYMBOL_GPL(class_device_get);
  744. EXPORT_SYMBOL_GPL(class_device_put);
  745. EXPORT_SYMBOL_GPL(class_device_create);
  746. EXPORT_SYMBOL_GPL(class_device_destroy);
  747. EXPORT_SYMBOL_GPL(class_device_create_file);
  748. EXPORT_SYMBOL_GPL(class_device_remove_file);
  749. EXPORT_SYMBOL_GPL(class_device_create_bin_file);
  750. EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
  751. EXPORT_SYMBOL_GPL(class_interface_register);
  752. EXPORT_SYMBOL_GPL(class_interface_unregister);