class.c 22 KB

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