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