class.c 22 KB

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