class.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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
  24. class_attr_show(struct kobject * kobj, struct attribute * attr, 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
  34. class_attr_store(struct kobject * kobj, struct attribute * attr,
  35. const char * buf, size_t count)
  36. {
  37. struct class_attribute * class_attr = to_class_attr(attr);
  38. struct class * dc = to_class(kobj);
  39. ssize_t ret = -EIO;
  40. if (class_attr->store)
  41. ret = class_attr->store(dc, buf, count);
  42. return ret;
  43. }
  44. static void class_release(struct kobject * kobj)
  45. {
  46. struct class *class = to_class(kobj);
  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. }
  54. static struct sysfs_ops class_sysfs_ops = {
  55. .show = class_attr_show,
  56. .store = class_attr_store,
  57. };
  58. static struct kobj_type class_ktype = {
  59. .sysfs_ops = &class_sysfs_ops,
  60. .release = class_release,
  61. };
  62. /* Hotplug events for classes go to the class_obj subsys */
  63. static struct kset *class_kset;
  64. int class_create_file(struct class * cls, const struct class_attribute * attr)
  65. {
  66. int error;
  67. if (cls) {
  68. error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
  69. } else
  70. error = -EINVAL;
  71. return error;
  72. }
  73. void class_remove_file(struct class * cls, const struct class_attribute * attr)
  74. {
  75. if (cls)
  76. sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
  77. }
  78. static struct class *class_get(struct class *cls)
  79. {
  80. if (cls)
  81. return container_of(kset_get(&cls->subsys), 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 Err;
  98. }
  99. }
  100. Done:
  101. return error;
  102. Err:
  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. #ifdef CONFIG_SYSFS_DEPRECATED
  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
  240. class_device_attr_show(struct kobject * kobj, struct attribute * attr,
  241. char * buf)
  242. {
  243. struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
  244. struct class_device * cd = to_class_dev(kobj);
  245. ssize_t ret = 0;
  246. if (class_dev_attr->show)
  247. ret = class_dev_attr->show(cd, buf);
  248. return ret;
  249. }
  250. static ssize_t
  251. class_device_attr_store(struct kobject * kobj, struct attribute * attr,
  252. const char * buf, size_t count)
  253. {
  254. struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
  255. struct class_device * cd = to_class_dev(kobj);
  256. ssize_t ret = 0;
  257. if (class_dev_attr->store)
  258. ret = class_dev_attr->store(cd, buf, count);
  259. return ret;
  260. }
  261. static struct sysfs_ops class_dev_sysfs_ops = {
  262. .show = class_device_attr_show,
  263. .store = class_device_attr_store,
  264. };
  265. static void class_dev_release(struct kobject * kobj)
  266. {
  267. struct class_device *cd = to_class_dev(kobj);
  268. struct class * cls = cd->class;
  269. pr_debug("device class '%s': release.\n", cd->class_id);
  270. if (cd->release)
  271. cd->release(cd);
  272. else if (cls->release)
  273. cls->release(cd);
  274. else {
  275. printk(KERN_ERR "Class Device '%s' does not have a release() function, "
  276. "it is broken and must be fixed.\n",
  277. cd->class_id);
  278. WARN_ON(1);
  279. }
  280. }
  281. static struct kobj_type class_device_ktype = {
  282. .sysfs_ops = &class_dev_sysfs_ops,
  283. .release = class_dev_release,
  284. };
  285. static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
  286. {
  287. struct kobj_type *ktype = get_ktype(kobj);
  288. if (ktype == &class_device_ktype) {
  289. struct class_device *class_dev = to_class_dev(kobj);
  290. if (class_dev->class)
  291. return 1;
  292. }
  293. return 0;
  294. }
  295. static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
  296. {
  297. struct class_device *class_dev = to_class_dev(kobj);
  298. return class_dev->class->name;
  299. }
  300. #ifdef CONFIG_SYSFS_DEPRECATED
  301. char *make_class_name(const char *name, struct kobject *kobj)
  302. {
  303. char *class_name;
  304. int size;
  305. size = strlen(name) + strlen(kobject_name(kobj)) + 2;
  306. class_name = kmalloc(size, GFP_KERNEL);
  307. if (!class_name)
  308. return NULL;
  309. strcpy(class_name, name);
  310. strcat(class_name, ":");
  311. strcat(class_name, kobject_name(kobj));
  312. return class_name;
  313. }
  314. static int make_deprecated_class_device_links(struct class_device *class_dev)
  315. {
  316. char *class_name;
  317. int error;
  318. if (!class_dev->dev)
  319. return 0;
  320. class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
  321. if (class_name)
  322. error = sysfs_create_link(&class_dev->dev->kobj,
  323. &class_dev->kobj, class_name);
  324. else
  325. error = -ENOMEM;
  326. kfree(class_name);
  327. return error;
  328. }
  329. static void remove_deprecated_class_device_links(struct class_device *class_dev)
  330. {
  331. char *class_name;
  332. if (!class_dev->dev)
  333. return;
  334. class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
  335. if (class_name)
  336. sysfs_remove_link(&class_dev->dev->kobj, class_name);
  337. kfree(class_name);
  338. }
  339. #else
  340. static inline int make_deprecated_class_device_links(struct class_device *cd)
  341. { return 0; }
  342. static void remove_deprecated_class_device_links(struct class_device *cd)
  343. { }
  344. #endif
  345. static int class_uevent(struct kset *kset, struct kobject *kobj,
  346. struct kobj_uevent_env *env)
  347. {
  348. struct class_device *class_dev = to_class_dev(kobj);
  349. struct device *dev = class_dev->dev;
  350. int retval = 0;
  351. pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
  352. if (MAJOR(class_dev->devt)) {
  353. add_uevent_var(env, "MAJOR=%u", MAJOR(class_dev->devt));
  354. add_uevent_var(env, "MINOR=%u", MINOR(class_dev->devt));
  355. }
  356. if (dev) {
  357. const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
  358. if (path) {
  359. add_uevent_var(env, "PHYSDEVPATH=%s", path);
  360. kfree(path);
  361. }
  362. if (dev->bus)
  363. add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
  364. if (dev->driver)
  365. add_uevent_var(env, "PHYSDEVDRIVER=%s", 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. .kobj = { .k_name = "class_obj", },
  392. .uevent_ops = &class_uevent_ops,
  393. };
  394. static int class_device_add_attrs(struct class_device * cd)
  395. {
  396. int i;
  397. int error = 0;
  398. struct class * cls = cd->class;
  399. if (cls->class_dev_attrs) {
  400. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
  401. error = class_device_create_file(cd,
  402. &cls->class_dev_attrs[i]);
  403. if (error)
  404. goto Err;
  405. }
  406. }
  407. Done:
  408. return error;
  409. Err:
  410. while (--i >= 0)
  411. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  412. goto Done;
  413. }
  414. static void class_device_remove_attrs(struct class_device * cd)
  415. {
  416. int i;
  417. struct class * cls = cd->class;
  418. if (cls->class_dev_attrs) {
  419. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
  420. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  421. }
  422. }
  423. static int class_device_add_groups(struct class_device * cd)
  424. {
  425. int i;
  426. int error = 0;
  427. if (cd->groups) {
  428. for (i = 0; cd->groups[i]; i++) {
  429. error = sysfs_create_group(&cd->kobj, cd->groups[i]);
  430. if (error) {
  431. while (--i >= 0)
  432. sysfs_remove_group(&cd->kobj, 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. }
  448. }
  449. static ssize_t show_dev(struct class_device *class_dev, char *buf)
  450. {
  451. return print_dev_t(buf, class_dev->devt);
  452. }
  453. static struct class_device_attribute class_devt_attr =
  454. __ATTR(dev, S_IRUGO, show_dev, NULL);
  455. static ssize_t store_uevent(struct class_device *class_dev,
  456. const char *buf, size_t count)
  457. {
  458. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  459. return count;
  460. }
  461. static struct class_device_attribute class_uevent_attr =
  462. __ATTR(uevent, S_IWUSR, NULL, store_uevent);
  463. void class_device_initialize(struct class_device *class_dev)
  464. {
  465. class_dev->kobj.kset = &class_obj_subsys;
  466. kobject_init(&class_dev->kobj, &class_device_ktype);
  467. INIT_LIST_HEAD(&class_dev->node);
  468. }
  469. int class_device_add(struct class_device *class_dev)
  470. {
  471. struct class *parent_class = NULL;
  472. struct class_device *parent_class_dev = NULL;
  473. struct class_interface *class_intf;
  474. int error = -EINVAL;
  475. class_dev = class_device_get(class_dev);
  476. if (!class_dev)
  477. return -EINVAL;
  478. if (!strlen(class_dev->class_id))
  479. goto out1;
  480. parent_class = class_get(class_dev->class);
  481. if (!parent_class)
  482. goto out1;
  483. parent_class_dev = class_device_get(class_dev->parent);
  484. pr_debug("CLASS: registering class device: ID = '%s'\n",
  485. class_dev->class_id);
  486. /* first, register with generic layer. */
  487. if (parent_class_dev)
  488. class_dev->kobj.parent = &parent_class_dev->kobj;
  489. else
  490. class_dev->kobj.parent = &parent_class->subsys.kobj;
  491. error = kobject_add(&class_dev->kobj, class_dev->kobj.parent,
  492. "%s", class_dev->class_id);
  493. if (error)
  494. goto out2;
  495. /* add the needed attributes to this device */
  496. error = sysfs_create_link(&class_dev->kobj,
  497. &parent_class->subsys.kobj, "subsystem");
  498. if (error)
  499. goto out3;
  500. error = class_device_create_file(class_dev, &class_uevent_attr);
  501. if (error)
  502. goto out3;
  503. if (MAJOR(class_dev->devt)) {
  504. error = class_device_create_file(class_dev, &class_devt_attr);
  505. if (error)
  506. goto out4;
  507. }
  508. error = class_device_add_attrs(class_dev);
  509. if (error)
  510. goto out5;
  511. if (class_dev->dev) {
  512. error = sysfs_create_link(&class_dev->kobj,
  513. &class_dev->dev->kobj, "device");
  514. if (error)
  515. goto out6;
  516. }
  517. error = class_device_add_groups(class_dev);
  518. if (error)
  519. goto out7;
  520. error = make_deprecated_class_device_links(class_dev);
  521. if (error)
  522. goto out8;
  523. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  524. /* notify any interfaces this device is now here */
  525. down(&parent_class->sem);
  526. list_add_tail(&class_dev->node, &parent_class->children);
  527. list_for_each_entry(class_intf, &parent_class->interfaces, node) {
  528. if (class_intf->add)
  529. class_intf->add(class_dev, class_intf);
  530. }
  531. up(&parent_class->sem);
  532. goto out1;
  533. out8:
  534. class_device_remove_groups(class_dev);
  535. out7:
  536. if (class_dev->dev)
  537. sysfs_remove_link(&class_dev->kobj, "device");
  538. out6:
  539. class_device_remove_attrs(class_dev);
  540. out5:
  541. if (MAJOR(class_dev->devt))
  542. class_device_remove_file(class_dev, &class_devt_attr);
  543. out4:
  544. class_device_remove_file(class_dev, &class_uevent_attr);
  545. out3:
  546. kobject_del(&class_dev->kobj);
  547. out2:
  548. if(parent_class_dev)
  549. class_device_put(parent_class_dev);
  550. class_put(parent_class);
  551. out1:
  552. class_device_put(class_dev);
  553. return error;
  554. }
  555. int class_device_register(struct class_device *class_dev)
  556. {
  557. class_device_initialize(class_dev);
  558. return class_device_add(class_dev);
  559. }
  560. /**
  561. * class_device_create - creates a class device and registers it with sysfs
  562. * @cls: pointer to the struct class that this device should be registered to.
  563. * @parent: pointer to the parent struct class_device of this new device, if any.
  564. * @devt: the dev_t for the char device to be added.
  565. * @device: a pointer to a struct device that is assiociated with this class 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. int class_interface_register(struct class_interface *class_intf)
  685. {
  686. struct class *parent;
  687. struct class_device *class_dev;
  688. struct device *dev;
  689. if (!class_intf || !class_intf->class)
  690. return -ENODEV;
  691. parent = class_get(class_intf->class);
  692. if (!parent)
  693. return -EINVAL;
  694. down(&parent->sem);
  695. list_add_tail(&class_intf->node, &parent->interfaces);
  696. if (class_intf->add) {
  697. list_for_each_entry(class_dev, &parent->children, node)
  698. class_intf->add(class_dev, class_intf);
  699. }
  700. if (class_intf->add_dev) {
  701. list_for_each_entry(dev, &parent->devices, node)
  702. class_intf->add_dev(dev, class_intf);
  703. }
  704. up(&parent->sem);
  705. return 0;
  706. }
  707. void class_interface_unregister(struct class_interface *class_intf)
  708. {
  709. struct class * parent = class_intf->class;
  710. struct class_device *class_dev;
  711. struct device *dev;
  712. if (!parent)
  713. return;
  714. down(&parent->sem);
  715. list_del_init(&class_intf->node);
  716. if (class_intf->remove) {
  717. list_for_each_entry(class_dev, &parent->children, node)
  718. class_intf->remove(class_dev, class_intf);
  719. }
  720. if (class_intf->remove_dev) {
  721. list_for_each_entry(dev, &parent->devices, node)
  722. class_intf->remove_dev(dev, class_intf);
  723. }
  724. up(&parent->sem);
  725. class_put(parent);
  726. }
  727. int __init classes_init(void)
  728. {
  729. class_kset = kset_create_and_add("class", NULL, NULL);
  730. if (!class_kset)
  731. return -ENOMEM;
  732. /* ick, this is ugly, the things we go through to keep from showing up
  733. * in sysfs... */
  734. kset_init(&class_obj_subsys);
  735. if (!class_obj_subsys.kobj.parent)
  736. class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
  737. return 0;
  738. }
  739. EXPORT_SYMBOL_GPL(class_create_file);
  740. EXPORT_SYMBOL_GPL(class_remove_file);
  741. EXPORT_SYMBOL_GPL(class_register);
  742. EXPORT_SYMBOL_GPL(class_unregister);
  743. EXPORT_SYMBOL_GPL(class_create);
  744. EXPORT_SYMBOL_GPL(class_destroy);
  745. EXPORT_SYMBOL_GPL(class_device_register);
  746. EXPORT_SYMBOL_GPL(class_device_unregister);
  747. EXPORT_SYMBOL_GPL(class_device_initialize);
  748. EXPORT_SYMBOL_GPL(class_device_add);
  749. EXPORT_SYMBOL_GPL(class_device_del);
  750. EXPORT_SYMBOL_GPL(class_device_get);
  751. EXPORT_SYMBOL_GPL(class_device_put);
  752. EXPORT_SYMBOL_GPL(class_device_create);
  753. EXPORT_SYMBOL_GPL(class_device_destroy);
  754. EXPORT_SYMBOL_GPL(class_device_create_file);
  755. EXPORT_SYMBOL_GPL(class_device_remove_file);
  756. EXPORT_SYMBOL_GPL(class_device_create_bin_file);
  757. EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
  758. EXPORT_SYMBOL_GPL(class_interface_register);
  759. EXPORT_SYMBOL_GPL(class_interface_unregister);