class.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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 = subsystem_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. subsystem_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. static decl_subsys(class_obj, &class_uevent_ops);
  379. static int class_device_add_attrs(struct class_device * cd)
  380. {
  381. int i;
  382. int error = 0;
  383. struct class * cls = cd->class;
  384. if (cls->class_dev_attrs) {
  385. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
  386. error = class_device_create_file(cd,
  387. &cls->class_dev_attrs[i]);
  388. if (error)
  389. goto Err;
  390. }
  391. }
  392. Done:
  393. return error;
  394. Err:
  395. while (--i >= 0)
  396. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  397. goto Done;
  398. }
  399. static void class_device_remove_attrs(struct class_device * cd)
  400. {
  401. int i;
  402. struct class * cls = cd->class;
  403. if (cls->class_dev_attrs) {
  404. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
  405. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  406. }
  407. }
  408. static int class_device_add_groups(struct class_device * cd)
  409. {
  410. int i;
  411. int error = 0;
  412. if (cd->groups) {
  413. for (i = 0; cd->groups[i]; i++) {
  414. error = sysfs_create_group(&cd->kobj, cd->groups[i]);
  415. if (error) {
  416. while (--i >= 0)
  417. sysfs_remove_group(&cd->kobj, cd->groups[i]);
  418. goto out;
  419. }
  420. }
  421. }
  422. out:
  423. return error;
  424. }
  425. static void class_device_remove_groups(struct class_device * cd)
  426. {
  427. int i;
  428. if (cd->groups) {
  429. for (i = 0; cd->groups[i]; i++) {
  430. sysfs_remove_group(&cd->kobj, cd->groups[i]);
  431. }
  432. }
  433. }
  434. static ssize_t show_dev(struct class_device *class_dev, char *buf)
  435. {
  436. return print_dev_t(buf, class_dev->devt);
  437. }
  438. static struct class_device_attribute class_devt_attr =
  439. __ATTR(dev, S_IRUGO, show_dev, NULL);
  440. static ssize_t store_uevent(struct class_device *class_dev,
  441. const char *buf, size_t count)
  442. {
  443. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  444. return count;
  445. }
  446. static struct class_device_attribute class_uevent_attr =
  447. __ATTR(uevent, S_IWUSR, NULL, store_uevent);
  448. void class_device_initialize(struct class_device *class_dev)
  449. {
  450. class_dev->kobj.kset = &class_obj_subsys;
  451. class_dev->kobj.ktype = &class_device_ktype;
  452. kobject_init(&class_dev->kobj);
  453. INIT_LIST_HEAD(&class_dev->node);
  454. }
  455. int class_device_add(struct class_device *class_dev)
  456. {
  457. struct class *parent_class = NULL;
  458. struct class_device *parent_class_dev = NULL;
  459. struct class_interface *class_intf;
  460. int error = -EINVAL;
  461. class_dev = class_device_get(class_dev);
  462. if (!class_dev)
  463. return -EINVAL;
  464. if (!strlen(class_dev->class_id))
  465. goto out1;
  466. parent_class = class_get(class_dev->class);
  467. if (!parent_class)
  468. goto out1;
  469. parent_class_dev = class_device_get(class_dev->parent);
  470. pr_debug("CLASS: registering class device: ID = '%s'\n",
  471. class_dev->class_id);
  472. /* first, register with generic layer. */
  473. error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
  474. if (error)
  475. goto out2;
  476. if (parent_class_dev)
  477. class_dev->kobj.parent = &parent_class_dev->kobj;
  478. else
  479. class_dev->kobj.parent = &parent_class->subsys.kobj;
  480. error = kobject_add(&class_dev->kobj);
  481. if (error)
  482. goto out2;
  483. /* add the needed attributes to this device */
  484. error = sysfs_create_link(&class_dev->kobj,
  485. &parent_class->subsys.kobj, "subsystem");
  486. if (error)
  487. goto out3;
  488. error = class_device_create_file(class_dev, &class_uevent_attr);
  489. if (error)
  490. goto out3;
  491. if (MAJOR(class_dev->devt)) {
  492. error = class_device_create_file(class_dev, &class_devt_attr);
  493. if (error)
  494. goto out4;
  495. }
  496. error = class_device_add_attrs(class_dev);
  497. if (error)
  498. goto out5;
  499. if (class_dev->dev) {
  500. error = sysfs_create_link(&class_dev->kobj,
  501. &class_dev->dev->kobj, "device");
  502. if (error)
  503. goto out6;
  504. }
  505. error = class_device_add_groups(class_dev);
  506. if (error)
  507. goto out7;
  508. error = make_deprecated_class_device_links(class_dev);
  509. if (error)
  510. goto out8;
  511. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  512. /* notify any interfaces this device is now here */
  513. down(&parent_class->sem);
  514. list_add_tail(&class_dev->node, &parent_class->children);
  515. list_for_each_entry(class_intf, &parent_class->interfaces, node) {
  516. if (class_intf->add)
  517. class_intf->add(class_dev, class_intf);
  518. }
  519. up(&parent_class->sem);
  520. goto out1;
  521. out8:
  522. class_device_remove_groups(class_dev);
  523. out7:
  524. if (class_dev->dev)
  525. sysfs_remove_link(&class_dev->kobj, "device");
  526. out6:
  527. class_device_remove_attrs(class_dev);
  528. out5:
  529. if (MAJOR(class_dev->devt))
  530. class_device_remove_file(class_dev, &class_devt_attr);
  531. out4:
  532. class_device_remove_file(class_dev, &class_uevent_attr);
  533. out3:
  534. kobject_del(&class_dev->kobj);
  535. out2:
  536. if(parent_class_dev)
  537. class_device_put(parent_class_dev);
  538. class_put(parent_class);
  539. out1:
  540. class_device_put(class_dev);
  541. return error;
  542. }
  543. int class_device_register(struct class_device *class_dev)
  544. {
  545. class_device_initialize(class_dev);
  546. return class_device_add(class_dev);
  547. }
  548. /**
  549. * class_device_create - creates a class device and registers it with sysfs
  550. * @cls: pointer to the struct class that this device should be registered to.
  551. * @parent: pointer to the parent struct class_device of this new device, if any.
  552. * @devt: the dev_t for the char device to be added.
  553. * @device: a pointer to a struct device that is assiociated with this class device.
  554. * @fmt: string for the class device's name
  555. *
  556. * This function can be used by char device classes. A struct
  557. * class_device will be created in sysfs, registered to the specified
  558. * class.
  559. * A "dev" file will be created, showing the dev_t for the device, if
  560. * the dev_t is not 0,0.
  561. * If a pointer to a parent struct class_device is passed in, the newly
  562. * created struct class_device will be a child of that device in sysfs.
  563. * The pointer to the struct class_device will be returned from the
  564. * call. Any further sysfs files that might be required can be created
  565. * using this pointer.
  566. *
  567. * Note: the struct class passed to this function must have previously
  568. * been created with a call to class_create().
  569. */
  570. struct class_device *class_device_create(struct class *cls,
  571. struct class_device *parent,
  572. dev_t devt,
  573. struct device *device,
  574. const char *fmt, ...)
  575. {
  576. va_list args;
  577. struct class_device *class_dev = NULL;
  578. int retval = -ENODEV;
  579. if (cls == NULL || IS_ERR(cls))
  580. goto error;
  581. class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
  582. if (!class_dev) {
  583. retval = -ENOMEM;
  584. goto error;
  585. }
  586. class_dev->devt = devt;
  587. class_dev->dev = device;
  588. class_dev->class = cls;
  589. class_dev->parent = parent;
  590. class_dev->release = class_device_create_release;
  591. class_dev->uevent = class_device_create_uevent;
  592. va_start(args, fmt);
  593. vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
  594. va_end(args);
  595. retval = class_device_register(class_dev);
  596. if (retval)
  597. goto error;
  598. return class_dev;
  599. error:
  600. kfree(class_dev);
  601. return ERR_PTR(retval);
  602. }
  603. void class_device_del(struct class_device *class_dev)
  604. {
  605. struct class *parent_class = class_dev->class;
  606. struct class_device *parent_device = class_dev->parent;
  607. struct class_interface *class_intf;
  608. if (parent_class) {
  609. down(&parent_class->sem);
  610. list_del_init(&class_dev->node);
  611. list_for_each_entry(class_intf, &parent_class->interfaces, node)
  612. if (class_intf->remove)
  613. class_intf->remove(class_dev, class_intf);
  614. up(&parent_class->sem);
  615. }
  616. if (class_dev->dev) {
  617. remove_deprecated_class_device_links(class_dev);
  618. sysfs_remove_link(&class_dev->kobj, "device");
  619. }
  620. sysfs_remove_link(&class_dev->kobj, "subsystem");
  621. class_device_remove_file(class_dev, &class_uevent_attr);
  622. if (MAJOR(class_dev->devt))
  623. class_device_remove_file(class_dev, &class_devt_attr);
  624. class_device_remove_attrs(class_dev);
  625. class_device_remove_groups(class_dev);
  626. kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
  627. kobject_del(&class_dev->kobj);
  628. class_device_put(parent_device);
  629. class_put(parent_class);
  630. }
  631. void class_device_unregister(struct class_device *class_dev)
  632. {
  633. pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
  634. class_dev->class_id);
  635. class_device_del(class_dev);
  636. class_device_put(class_dev);
  637. }
  638. /**
  639. * class_device_destroy - removes a class device that was created with class_device_create()
  640. * @cls: the pointer to the struct class that this device was registered * with.
  641. * @devt: the dev_t of the device that was previously registered.
  642. *
  643. * This call unregisters and cleans up a class device that was created with a
  644. * call to class_device_create()
  645. */
  646. void class_device_destroy(struct class *cls, dev_t devt)
  647. {
  648. struct class_device *class_dev = NULL;
  649. struct class_device *class_dev_tmp;
  650. down(&cls->sem);
  651. list_for_each_entry(class_dev_tmp, &cls->children, node) {
  652. if (class_dev_tmp->devt == devt) {
  653. class_dev = class_dev_tmp;
  654. break;
  655. }
  656. }
  657. up(&cls->sem);
  658. if (class_dev)
  659. class_device_unregister(class_dev);
  660. }
  661. struct class_device * class_device_get(struct class_device *class_dev)
  662. {
  663. if (class_dev)
  664. return to_class_dev(kobject_get(&class_dev->kobj));
  665. return NULL;
  666. }
  667. void class_device_put(struct class_device *class_dev)
  668. {
  669. if (class_dev)
  670. kobject_put(&class_dev->kobj);
  671. }
  672. int class_interface_register(struct class_interface *class_intf)
  673. {
  674. struct class *parent;
  675. struct class_device *class_dev;
  676. struct device *dev;
  677. if (!class_intf || !class_intf->class)
  678. return -ENODEV;
  679. parent = class_get(class_intf->class);
  680. if (!parent)
  681. return -EINVAL;
  682. down(&parent->sem);
  683. list_add_tail(&class_intf->node, &parent->interfaces);
  684. if (class_intf->add) {
  685. list_for_each_entry(class_dev, &parent->children, node)
  686. class_intf->add(class_dev, class_intf);
  687. }
  688. if (class_intf->add_dev) {
  689. list_for_each_entry(dev, &parent->devices, node)
  690. class_intf->add_dev(dev, class_intf);
  691. }
  692. up(&parent->sem);
  693. return 0;
  694. }
  695. void class_interface_unregister(struct class_interface *class_intf)
  696. {
  697. struct class * parent = class_intf->class;
  698. struct class_device *class_dev;
  699. struct device *dev;
  700. if (!parent)
  701. return;
  702. down(&parent->sem);
  703. list_del_init(&class_intf->node);
  704. if (class_intf->remove) {
  705. list_for_each_entry(class_dev, &parent->children, node)
  706. class_intf->remove(class_dev, class_intf);
  707. }
  708. if (class_intf->remove_dev) {
  709. list_for_each_entry(dev, &parent->devices, node)
  710. class_intf->remove_dev(dev, class_intf);
  711. }
  712. up(&parent->sem);
  713. class_put(parent);
  714. }
  715. int __init classes_init(void)
  716. {
  717. class_kset = kset_create_and_add("class", NULL, NULL);
  718. if (!class_kset)
  719. return -ENOMEM;
  720. /* ick, this is ugly, the things we go through to keep from showing up
  721. * in sysfs... */
  722. kset_init(&class_obj_subsys);
  723. if (!class_obj_subsys.kobj.parent)
  724. class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
  725. return 0;
  726. }
  727. EXPORT_SYMBOL_GPL(class_create_file);
  728. EXPORT_SYMBOL_GPL(class_remove_file);
  729. EXPORT_SYMBOL_GPL(class_register);
  730. EXPORT_SYMBOL_GPL(class_unregister);
  731. EXPORT_SYMBOL_GPL(class_create);
  732. EXPORT_SYMBOL_GPL(class_destroy);
  733. EXPORT_SYMBOL_GPL(class_device_register);
  734. EXPORT_SYMBOL_GPL(class_device_unregister);
  735. EXPORT_SYMBOL_GPL(class_device_initialize);
  736. EXPORT_SYMBOL_GPL(class_device_add);
  737. EXPORT_SYMBOL_GPL(class_device_del);
  738. EXPORT_SYMBOL_GPL(class_device_get);
  739. EXPORT_SYMBOL_GPL(class_device_put);
  740. EXPORT_SYMBOL_GPL(class_device_create);
  741. EXPORT_SYMBOL_GPL(class_device_destroy);
  742. EXPORT_SYMBOL_GPL(class_device_create_file);
  743. EXPORT_SYMBOL_GPL(class_device_remove_file);
  744. EXPORT_SYMBOL_GPL(class_device_create_bin_file);
  745. EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
  746. EXPORT_SYMBOL_GPL(class_interface_register);
  747. EXPORT_SYMBOL_GPL(class_interface_unregister);