class.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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.kset.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 ktype_class = {
  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, &ktype_class, 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.kset.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.kset.kobj, &attr->attr);
  76. }
  77. static struct class *class_get(struct class *cls)
  78. {
  79. if (cls)
  80. return container_of(subsys_get(&cls->subsys), struct class, subsys);
  81. return NULL;
  82. }
  83. static void class_put(struct class * cls)
  84. {
  85. if (cls)
  86. subsys_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. init_MUTEX(&cls->sem);
  122. error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name);
  123. if (error)
  124. return error;
  125. subsys_set_kset(cls, class_subsys);
  126. error = subsystem_register(&cls->subsys);
  127. if (!error) {
  128. error = add_class_attrs(class_get(cls));
  129. class_put(cls);
  130. }
  131. return error;
  132. }
  133. void class_unregister(struct class * cls)
  134. {
  135. pr_debug("device class '%s': unregistering\n", cls->name);
  136. remove_class_attrs(cls);
  137. subsystem_unregister(&cls->subsys);
  138. }
  139. static void class_create_release(struct class *cls)
  140. {
  141. pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
  142. kfree(cls);
  143. }
  144. static void class_device_create_release(struct class_device *class_dev)
  145. {
  146. pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
  147. kfree(class_dev);
  148. }
  149. /* needed to allow these devices to have parent class devices */
  150. static int class_device_create_uevent(struct class_device *class_dev,
  151. char **envp, int num_envp,
  152. char *buffer, int buffer_size)
  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, 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. * @cs: 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. kfree(cd->devt_attr);
  263. cd->devt_attr = NULL;
  264. if (cd->release)
  265. cd->release(cd);
  266. else if (cls->release)
  267. cls->release(cd);
  268. else {
  269. printk(KERN_ERR "Class Device '%s' does not have a release() function, "
  270. "it is broken and must be fixed.\n",
  271. cd->class_id);
  272. WARN_ON(1);
  273. }
  274. }
  275. static struct kobj_type ktype_class_device = {
  276. .sysfs_ops = &class_dev_sysfs_ops,
  277. .release = class_dev_release,
  278. };
  279. static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
  280. {
  281. struct kobj_type *ktype = get_ktype(kobj);
  282. if (ktype == &ktype_class_device) {
  283. struct class_device *class_dev = to_class_dev(kobj);
  284. if (class_dev->class)
  285. return 1;
  286. }
  287. return 0;
  288. }
  289. static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
  290. {
  291. struct class_device *class_dev = to_class_dev(kobj);
  292. return class_dev->class->name;
  293. }
  294. static int class_uevent(struct kset *kset, struct kobject *kobj, char **envp,
  295. int num_envp, char *buffer, int buffer_size)
  296. {
  297. struct class_device *class_dev = to_class_dev(kobj);
  298. int i = 0;
  299. int length = 0;
  300. int retval = 0;
  301. pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
  302. if (class_dev->dev) {
  303. /* add physical device, backing this device */
  304. struct device *dev = class_dev->dev;
  305. char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
  306. add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
  307. &length, "PHYSDEVPATH=%s", path);
  308. kfree(path);
  309. if (dev->bus)
  310. add_uevent_var(envp, num_envp, &i,
  311. buffer, buffer_size, &length,
  312. "PHYSDEVBUS=%s", dev->bus->name);
  313. if (dev->driver)
  314. add_uevent_var(envp, num_envp, &i,
  315. buffer, buffer_size, &length,
  316. "PHYSDEVDRIVER=%s", dev->driver->name);
  317. }
  318. if (MAJOR(class_dev->devt)) {
  319. add_uevent_var(envp, num_envp, &i,
  320. buffer, buffer_size, &length,
  321. "MAJOR=%u", MAJOR(class_dev->devt));
  322. add_uevent_var(envp, num_envp, &i,
  323. buffer, buffer_size, &length,
  324. "MINOR=%u", MINOR(class_dev->devt));
  325. }
  326. /* terminate, set to next free slot, shrink available space */
  327. envp[i] = NULL;
  328. envp = &envp[i];
  329. num_envp -= i;
  330. buffer = &buffer[length];
  331. buffer_size -= length;
  332. if (class_dev->uevent) {
  333. /* have the class device specific function add its stuff */
  334. retval = class_dev->uevent(class_dev, envp, num_envp,
  335. buffer, buffer_size);
  336. if (retval)
  337. pr_debug("class_dev->uevent() returned %d\n", retval);
  338. } else if (class_dev->class->uevent) {
  339. /* have the class specific function add its stuff */
  340. retval = class_dev->class->uevent(class_dev, envp, num_envp,
  341. buffer, buffer_size);
  342. if (retval)
  343. pr_debug("class->uevent() returned %d\n", retval);
  344. }
  345. return retval;
  346. }
  347. static struct kset_uevent_ops class_uevent_ops = {
  348. .filter = class_uevent_filter,
  349. .name = class_uevent_name,
  350. .uevent = class_uevent,
  351. };
  352. static decl_subsys(class_obj, &ktype_class_device, &class_uevent_ops);
  353. static int class_device_add_attrs(struct class_device * cd)
  354. {
  355. int i;
  356. int error = 0;
  357. struct class * cls = cd->class;
  358. if (cls->class_dev_attrs) {
  359. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
  360. error = class_device_create_file(cd,
  361. &cls->class_dev_attrs[i]);
  362. if (error)
  363. goto Err;
  364. }
  365. }
  366. Done:
  367. return error;
  368. Err:
  369. while (--i >= 0)
  370. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  371. goto Done;
  372. }
  373. static void class_device_remove_attrs(struct class_device * cd)
  374. {
  375. int i;
  376. struct class * cls = cd->class;
  377. if (cls->class_dev_attrs) {
  378. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
  379. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  380. }
  381. }
  382. static int class_device_add_groups(struct class_device * cd)
  383. {
  384. int i;
  385. int error = 0;
  386. if (cd->groups) {
  387. for (i = 0; cd->groups[i]; i++) {
  388. error = sysfs_create_group(&cd->kobj, cd->groups[i]);
  389. if (error) {
  390. while (--i >= 0)
  391. sysfs_remove_group(&cd->kobj, cd->groups[i]);
  392. goto out;
  393. }
  394. }
  395. }
  396. out:
  397. return error;
  398. }
  399. static void class_device_remove_groups(struct class_device * cd)
  400. {
  401. int i;
  402. if (cd->groups) {
  403. for (i = 0; cd->groups[i]; i++) {
  404. sysfs_remove_group(&cd->kobj, cd->groups[i]);
  405. }
  406. }
  407. }
  408. static ssize_t show_dev(struct class_device *class_dev, char *buf)
  409. {
  410. return print_dev_t(buf, class_dev->devt);
  411. }
  412. static ssize_t store_uevent(struct class_device *class_dev,
  413. const char *buf, size_t count)
  414. {
  415. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  416. return count;
  417. }
  418. void class_device_initialize(struct class_device *class_dev)
  419. {
  420. kobj_set_kset_s(class_dev, class_obj_subsys);
  421. kobject_init(&class_dev->kobj);
  422. INIT_LIST_HEAD(&class_dev->node);
  423. }
  424. char *make_class_name(const char *name, struct kobject *kobj)
  425. {
  426. char *class_name;
  427. int size;
  428. size = strlen(name) + strlen(kobject_name(kobj)) + 2;
  429. class_name = kmalloc(size, GFP_KERNEL);
  430. if (!class_name)
  431. return ERR_PTR(-ENOMEM);
  432. strcpy(class_name, name);
  433. strcat(class_name, ":");
  434. strcat(class_name, kobject_name(kobj));
  435. return class_name;
  436. }
  437. int class_device_add(struct class_device *class_dev)
  438. {
  439. struct class *parent_class = NULL;
  440. struct class_device *parent_class_dev = NULL;
  441. struct class_interface *class_intf;
  442. char *class_name = NULL;
  443. int error = -EINVAL;
  444. class_dev = class_device_get(class_dev);
  445. if (!class_dev)
  446. return -EINVAL;
  447. if (!strlen(class_dev->class_id))
  448. goto out1;
  449. parent_class = class_get(class_dev->class);
  450. if (!parent_class)
  451. goto out1;
  452. parent_class_dev = class_device_get(class_dev->parent);
  453. pr_debug("CLASS: registering class device: ID = '%s'\n",
  454. class_dev->class_id);
  455. /* first, register with generic layer. */
  456. error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
  457. if (error)
  458. goto out2;
  459. if (parent_class_dev)
  460. class_dev->kobj.parent = &parent_class_dev->kobj;
  461. else
  462. class_dev->kobj.parent = &parent_class->subsys.kset.kobj;
  463. error = kobject_add(&class_dev->kobj);
  464. if (error)
  465. goto out2;
  466. /* add the needed attributes to this device */
  467. sysfs_create_link(&class_dev->kobj, &parent_class->subsys.kset.kobj, "subsystem");
  468. class_dev->uevent_attr.attr.name = "uevent";
  469. class_dev->uevent_attr.attr.mode = S_IWUSR;
  470. class_dev->uevent_attr.attr.owner = parent_class->owner;
  471. class_dev->uevent_attr.store = store_uevent;
  472. error = class_device_create_file(class_dev, &class_dev->uevent_attr);
  473. if (error)
  474. goto out3;
  475. if (MAJOR(class_dev->devt)) {
  476. struct class_device_attribute *attr;
  477. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  478. if (!attr) {
  479. error = -ENOMEM;
  480. goto out4;
  481. }
  482. attr->attr.name = "dev";
  483. attr->attr.mode = S_IRUGO;
  484. attr->attr.owner = parent_class->owner;
  485. attr->show = show_dev;
  486. error = class_device_create_file(class_dev, attr);
  487. if (error) {
  488. kfree(attr);
  489. goto out4;
  490. }
  491. class_dev->devt_attr = attr;
  492. }
  493. error = class_device_add_attrs(class_dev);
  494. if (error)
  495. goto out5;
  496. if (class_dev->dev) {
  497. class_name = make_class_name(class_dev->class->name,
  498. &class_dev->kobj);
  499. error = sysfs_create_link(&class_dev->kobj,
  500. &class_dev->dev->kobj, "device");
  501. if (error)
  502. goto out6;
  503. error = sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
  504. class_name);
  505. if (error)
  506. goto out7;
  507. }
  508. error = class_device_add_groups(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. if (class_dev->dev)
  523. sysfs_remove_link(&class_dev->kobj, class_name);
  524. out7:
  525. if (class_dev->dev)
  526. sysfs_remove_link(&class_dev->kobj, "device");
  527. out6:
  528. class_device_remove_attrs(class_dev);
  529. out5:
  530. if (class_dev->devt_attr)
  531. class_device_remove_file(class_dev, class_dev->devt_attr);
  532. out4:
  533. class_device_remove_file(class_dev, &class_dev->uevent_attr);
  534. out3:
  535. kobject_del(&class_dev->kobj);
  536. out2:
  537. if(parent_class_dev)
  538. class_device_put(parent_class_dev);
  539. class_put(parent_class);
  540. out1:
  541. class_device_put(class_dev);
  542. kfree(class_name);
  543. return error;
  544. }
  545. int class_device_register(struct class_device *class_dev)
  546. {
  547. class_device_initialize(class_dev);
  548. return class_device_add(class_dev);
  549. }
  550. /**
  551. * class_device_create - creates a class device and registers it with sysfs
  552. * @cs: pointer to the struct class that this device should be registered to.
  553. * @parent: pointer to the parent struct class_device of this new device, if any.
  554. * @dev: the dev_t for the char device to be added.
  555. * @device: a pointer to a struct device that is assiociated with this class device.
  556. * @fmt: string for the class device's name
  557. *
  558. * This function can be used by char device classes. A struct
  559. * class_device will be created in sysfs, registered to the specified
  560. * class.
  561. * A "dev" file will be created, showing the dev_t for the device, if
  562. * the dev_t is not 0,0.
  563. * If a pointer to a parent struct class_device is passed in, the newly
  564. * created struct class_device will be a child of that device in sysfs.
  565. * The pointer to the struct class_device will be returned from the
  566. * call. Any further sysfs files that might be required can be created
  567. * using this pointer.
  568. *
  569. * Note: the struct class passed to this function must have previously
  570. * been created with a call to class_create().
  571. */
  572. struct class_device *class_device_create(struct class *cls,
  573. struct class_device *parent,
  574. dev_t devt,
  575. struct device *device, char *fmt, ...)
  576. {
  577. va_list args;
  578. struct class_device *class_dev = NULL;
  579. int retval = -ENODEV;
  580. if (cls == NULL || IS_ERR(cls))
  581. goto error;
  582. class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
  583. if (!class_dev) {
  584. retval = -ENOMEM;
  585. goto error;
  586. }
  587. class_dev->devt = devt;
  588. class_dev->dev = device;
  589. class_dev->class = cls;
  590. class_dev->parent = parent;
  591. class_dev->release = class_device_create_release;
  592. class_dev->uevent = class_device_create_uevent;
  593. va_start(args, fmt);
  594. vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
  595. va_end(args);
  596. retval = class_device_register(class_dev);
  597. if (retval)
  598. goto error;
  599. return class_dev;
  600. error:
  601. kfree(class_dev);
  602. return ERR_PTR(retval);
  603. }
  604. void class_device_del(struct class_device *class_dev)
  605. {
  606. struct class *parent_class = class_dev->class;
  607. struct class_device *parent_device = class_dev->parent;
  608. struct class_interface *class_intf;
  609. char *class_name = NULL;
  610. if (parent_class) {
  611. down(&parent_class->sem);
  612. list_del_init(&class_dev->node);
  613. list_for_each_entry(class_intf, &parent_class->interfaces, node)
  614. if (class_intf->remove)
  615. class_intf->remove(class_dev, class_intf);
  616. up(&parent_class->sem);
  617. }
  618. if (class_dev->dev) {
  619. class_name = make_class_name(class_dev->class->name,
  620. &class_dev->kobj);
  621. sysfs_remove_link(&class_dev->kobj, "device");
  622. sysfs_remove_link(&class_dev->dev->kobj, class_name);
  623. }
  624. sysfs_remove_link(&class_dev->kobj, "subsystem");
  625. class_device_remove_file(class_dev, &class_dev->uevent_attr);
  626. if (class_dev->devt_attr)
  627. class_device_remove_file(class_dev, class_dev->devt_attr);
  628. class_device_remove_attrs(class_dev);
  629. class_device_remove_groups(class_dev);
  630. kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
  631. kobject_del(&class_dev->kobj);
  632. class_device_put(parent_device);
  633. class_put(parent_class);
  634. kfree(class_name);
  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. * @dev: 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. int class_device_rename(struct class_device *class_dev, char *new_name)
  667. {
  668. int error = 0;
  669. char *old_class_name = NULL, *new_class_name = NULL;
  670. class_dev = class_device_get(class_dev);
  671. if (!class_dev)
  672. return -EINVAL;
  673. pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
  674. new_name);
  675. if (class_dev->dev)
  676. old_class_name = make_class_name(class_dev->class->name,
  677. &class_dev->kobj);
  678. strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
  679. error = kobject_rename(&class_dev->kobj, new_name);
  680. if (class_dev->dev) {
  681. new_class_name = make_class_name(class_dev->class->name,
  682. &class_dev->kobj);
  683. sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
  684. new_class_name);
  685. sysfs_remove_link(&class_dev->dev->kobj, old_class_name);
  686. }
  687. class_device_put(class_dev);
  688. kfree(old_class_name);
  689. kfree(new_class_name);
  690. return error;
  691. }
  692. struct class_device * class_device_get(struct class_device *class_dev)
  693. {
  694. if (class_dev)
  695. return to_class_dev(kobject_get(&class_dev->kobj));
  696. return NULL;
  697. }
  698. void class_device_put(struct class_device *class_dev)
  699. {
  700. if (class_dev)
  701. kobject_put(&class_dev->kobj);
  702. }
  703. int class_interface_register(struct class_interface *class_intf)
  704. {
  705. struct class *parent;
  706. struct class_device *class_dev;
  707. if (!class_intf || !class_intf->class)
  708. return -ENODEV;
  709. parent = class_get(class_intf->class);
  710. if (!parent)
  711. return -EINVAL;
  712. down(&parent->sem);
  713. list_add_tail(&class_intf->node, &parent->interfaces);
  714. if (class_intf->add) {
  715. list_for_each_entry(class_dev, &parent->children, node)
  716. class_intf->add(class_dev, class_intf);
  717. }
  718. up(&parent->sem);
  719. return 0;
  720. }
  721. void class_interface_unregister(struct class_interface *class_intf)
  722. {
  723. struct class * parent = class_intf->class;
  724. struct class_device *class_dev;
  725. if (!parent)
  726. return;
  727. down(&parent->sem);
  728. list_del_init(&class_intf->node);
  729. if (class_intf->remove) {
  730. list_for_each_entry(class_dev, &parent->children, node)
  731. class_intf->remove(class_dev, class_intf);
  732. }
  733. up(&parent->sem);
  734. class_put(parent);
  735. }
  736. int __init classes_init(void)
  737. {
  738. int retval;
  739. retval = subsystem_register(&class_subsys);
  740. if (retval)
  741. return retval;
  742. /* ick, this is ugly, the things we go through to keep from showing up
  743. * in sysfs... */
  744. subsystem_init(&class_obj_subsys);
  745. if (!class_obj_subsys.kset.subsys)
  746. class_obj_subsys.kset.subsys = &class_obj_subsys;
  747. return 0;
  748. }
  749. EXPORT_SYMBOL_GPL(class_create_file);
  750. EXPORT_SYMBOL_GPL(class_remove_file);
  751. EXPORT_SYMBOL_GPL(class_register);
  752. EXPORT_SYMBOL_GPL(class_unregister);
  753. EXPORT_SYMBOL_GPL(class_create);
  754. EXPORT_SYMBOL_GPL(class_destroy);
  755. EXPORT_SYMBOL_GPL(class_device_register);
  756. EXPORT_SYMBOL_GPL(class_device_unregister);
  757. EXPORT_SYMBOL_GPL(class_device_initialize);
  758. EXPORT_SYMBOL_GPL(class_device_add);
  759. EXPORT_SYMBOL_GPL(class_device_del);
  760. EXPORT_SYMBOL_GPL(class_device_get);
  761. EXPORT_SYMBOL_GPL(class_device_put);
  762. EXPORT_SYMBOL_GPL(class_device_create);
  763. EXPORT_SYMBOL_GPL(class_device_destroy);
  764. EXPORT_SYMBOL_GPL(class_device_create_file);
  765. EXPORT_SYMBOL_GPL(class_device_remove_file);
  766. EXPORT_SYMBOL_GPL(class_device_create_bin_file);
  767. EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
  768. EXPORT_SYMBOL_GPL(class_interface_register);
  769. EXPORT_SYMBOL_GPL(class_interface_unregister);