class.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /*
  2. * class.c - basic device class management
  3. *
  4. * Copyright (c) 2002-3 Patrick Mochel
  5. * Copyright (c) 2002-3 Open Source Development Labs
  6. * Copyright (c) 2003-2004 Greg Kroah-Hartman
  7. * Copyright (c) 2003-2004 IBM Corp.
  8. *
  9. * This file is released under the GPLv2
  10. *
  11. */
  12. #include <linux/device.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/string.h>
  16. #include <linux/kdev_t.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include <linux/genhd.h>
  20. #include "base.h"
  21. #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
  22. #define to_class(obj) container_of(obj, struct class, subsys.kobj)
  23. static ssize_t
  24. class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  25. {
  26. struct class_attribute * class_attr = to_class_attr(attr);
  27. struct class * dc = to_class(kobj);
  28. ssize_t ret = -EIO;
  29. if (class_attr->show)
  30. ret = class_attr->show(dc, buf);
  31. return ret;
  32. }
  33. static ssize_t
  34. class_attr_store(struct kobject * kobj, struct attribute * attr,
  35. const char * buf, size_t count)
  36. {
  37. struct class_attribute * class_attr = to_class_attr(attr);
  38. struct class * dc = to_class(kobj);
  39. ssize_t ret = -EIO;
  40. if (class_attr->store)
  41. ret = class_attr->store(dc, buf, count);
  42. return ret;
  43. }
  44. static void class_release(struct kobject * kobj)
  45. {
  46. struct class *class = to_class(kobj);
  47. pr_debug("class '%s': release.\n", class->name);
  48. if (class->class_release)
  49. class->class_release(class);
  50. else
  51. pr_debug("class '%s' does not have a release() function, "
  52. "be careful\n", class->name);
  53. }
  54. static struct sysfs_ops class_sysfs_ops = {
  55. .show = class_attr_show,
  56. .store = class_attr_store,
  57. };
  58. static struct kobj_type class_ktype = {
  59. .sysfs_ops = &class_sysfs_ops,
  60. .release = class_release,
  61. };
  62. /* Hotplug events for classes go to the class_obj subsys */
  63. static struct kset *class_kset;
  64. int class_create_file(struct class * cls, const struct class_attribute * attr)
  65. {
  66. int error;
  67. if (cls) {
  68. error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
  69. } else
  70. error = -EINVAL;
  71. return error;
  72. }
  73. void class_remove_file(struct class * cls, const struct class_attribute * attr)
  74. {
  75. if (cls)
  76. sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
  77. }
  78. static struct class *class_get(struct class *cls)
  79. {
  80. if (cls)
  81. return container_of(kset_get(&cls->subsys), struct class, subsys);
  82. return NULL;
  83. }
  84. static void class_put(struct class * cls)
  85. {
  86. if (cls)
  87. kset_put(&cls->subsys);
  88. }
  89. static int add_class_attrs(struct class * cls)
  90. {
  91. int i;
  92. int error = 0;
  93. if (cls->class_attrs) {
  94. for (i = 0; attr_name(cls->class_attrs[i]); i++) {
  95. error = class_create_file(cls,&cls->class_attrs[i]);
  96. if (error)
  97. goto Err;
  98. }
  99. }
  100. Done:
  101. return error;
  102. Err:
  103. while (--i >= 0)
  104. class_remove_file(cls,&cls->class_attrs[i]);
  105. goto Done;
  106. }
  107. static void remove_class_attrs(struct class * cls)
  108. {
  109. int i;
  110. if (cls->class_attrs) {
  111. for (i = 0; attr_name(cls->class_attrs[i]); i++)
  112. class_remove_file(cls,&cls->class_attrs[i]);
  113. }
  114. }
  115. int class_register(struct class * cls)
  116. {
  117. int error;
  118. pr_debug("device class '%s': registering\n", cls->name);
  119. INIT_LIST_HEAD(&cls->children);
  120. INIT_LIST_HEAD(&cls->devices);
  121. INIT_LIST_HEAD(&cls->interfaces);
  122. kset_init(&cls->class_dirs);
  123. init_MUTEX(&cls->sem);
  124. error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name);
  125. if (error)
  126. return error;
  127. #ifdef CONFIG_SYSFS_DEPRECATED
  128. /* let the block class directory show up in the root of sysfs */
  129. if (cls != &block_class)
  130. cls->subsys.kobj.kset = class_kset;
  131. #else
  132. cls->subsys.kobj.kset = class_kset;
  133. #endif
  134. cls->subsys.kobj.ktype = &class_ktype;
  135. error = kset_register(&cls->subsys);
  136. if (!error) {
  137. error = add_class_attrs(class_get(cls));
  138. class_put(cls);
  139. }
  140. return error;
  141. }
  142. void class_unregister(struct class * cls)
  143. {
  144. pr_debug("device class '%s': unregistering\n", cls->name);
  145. remove_class_attrs(cls);
  146. kset_unregister(&cls->subsys);
  147. }
  148. static void class_create_release(struct class *cls)
  149. {
  150. pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
  151. kfree(cls);
  152. }
  153. static void class_device_create_release(struct class_device *class_dev)
  154. {
  155. pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
  156. kfree(class_dev);
  157. }
  158. /* needed to allow these devices to have parent class devices */
  159. static int class_device_create_uevent(struct class_device *class_dev,
  160. struct kobj_uevent_env *env)
  161. {
  162. pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
  163. return 0;
  164. }
  165. /**
  166. * class_create - create a struct class structure
  167. * @owner: pointer to the module that is to "own" this struct class
  168. * @name: pointer to a string for the name of this class.
  169. *
  170. * This is used to create a struct class pointer that can then be used
  171. * in calls to class_device_create().
  172. *
  173. * Note, the pointer created here is to be destroyed when finished by
  174. * making a call to class_destroy().
  175. */
  176. struct class *class_create(struct module *owner, const char *name)
  177. {
  178. struct class *cls;
  179. int retval;
  180. cls = kzalloc(sizeof(*cls), GFP_KERNEL);
  181. if (!cls) {
  182. retval = -ENOMEM;
  183. goto error;
  184. }
  185. cls->name = name;
  186. cls->owner = owner;
  187. cls->class_release = class_create_release;
  188. cls->release = class_device_create_release;
  189. retval = class_register(cls);
  190. if (retval)
  191. goto error;
  192. return cls;
  193. error:
  194. kfree(cls);
  195. return ERR_PTR(retval);
  196. }
  197. /**
  198. * class_destroy - destroys a struct class structure
  199. * @cls: pointer to the struct class that is to be destroyed
  200. *
  201. * Note, the pointer to be destroyed must have been created with a call
  202. * to class_create().
  203. */
  204. void class_destroy(struct class *cls)
  205. {
  206. if ((cls == NULL) || (IS_ERR(cls)))
  207. return;
  208. class_unregister(cls);
  209. }
  210. /* Class Device Stuff */
  211. int class_device_create_file(struct class_device * class_dev,
  212. const struct class_device_attribute * attr)
  213. {
  214. int error = -EINVAL;
  215. if (class_dev)
  216. error = sysfs_create_file(&class_dev->kobj, &attr->attr);
  217. return error;
  218. }
  219. void class_device_remove_file(struct class_device * class_dev,
  220. const struct class_device_attribute * attr)
  221. {
  222. if (class_dev)
  223. sysfs_remove_file(&class_dev->kobj, &attr->attr);
  224. }
  225. int class_device_create_bin_file(struct class_device *class_dev,
  226. struct bin_attribute *attr)
  227. {
  228. int error = -EINVAL;
  229. if (class_dev)
  230. error = sysfs_create_bin_file(&class_dev->kobj, attr);
  231. return error;
  232. }
  233. void class_device_remove_bin_file(struct class_device *class_dev,
  234. struct bin_attribute *attr)
  235. {
  236. if (class_dev)
  237. sysfs_remove_bin_file(&class_dev->kobj, attr);
  238. }
  239. static ssize_t
  240. class_device_attr_show(struct kobject * kobj, struct attribute * attr,
  241. char * buf)
  242. {
  243. struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
  244. struct class_device * cd = to_class_dev(kobj);
  245. ssize_t ret = 0;
  246. if (class_dev_attr->show)
  247. ret = class_dev_attr->show(cd, buf);
  248. return ret;
  249. }
  250. static ssize_t
  251. class_device_attr_store(struct kobject * kobj, struct attribute * attr,
  252. const char * buf, size_t count)
  253. {
  254. struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
  255. struct class_device * cd = to_class_dev(kobj);
  256. ssize_t ret = 0;
  257. if (class_dev_attr->store)
  258. ret = class_dev_attr->store(cd, buf, count);
  259. return ret;
  260. }
  261. static struct sysfs_ops class_dev_sysfs_ops = {
  262. .show = class_device_attr_show,
  263. .store = class_device_attr_store,
  264. };
  265. static void class_dev_release(struct kobject * kobj)
  266. {
  267. struct class_device *cd = to_class_dev(kobj);
  268. struct class * cls = cd->class;
  269. pr_debug("device class '%s': release.\n", cd->class_id);
  270. if (cd->release)
  271. cd->release(cd);
  272. else if (cls->release)
  273. cls->release(cd);
  274. else {
  275. printk(KERN_ERR "Class Device '%s' does not have a release() function, "
  276. "it is broken and must be fixed.\n",
  277. cd->class_id);
  278. WARN_ON(1);
  279. }
  280. }
  281. static struct kobj_type class_device_ktype = {
  282. .sysfs_ops = &class_dev_sysfs_ops,
  283. .release = class_dev_release,
  284. };
  285. static int class_uevent_filter(struct kset *kset, struct kobject *kobj)
  286. {
  287. struct kobj_type *ktype = get_ktype(kobj);
  288. if (ktype == &class_device_ktype) {
  289. struct class_device *class_dev = to_class_dev(kobj);
  290. if (class_dev->class)
  291. return 1;
  292. }
  293. return 0;
  294. }
  295. static const char *class_uevent_name(struct kset *kset, struct kobject *kobj)
  296. {
  297. struct class_device *class_dev = to_class_dev(kobj);
  298. return class_dev->class->name;
  299. }
  300. #ifdef CONFIG_SYSFS_DEPRECATED
  301. char *make_class_name(const char *name, struct kobject *kobj)
  302. {
  303. char *class_name;
  304. int size;
  305. size = strlen(name) + strlen(kobject_name(kobj)) + 2;
  306. class_name = kmalloc(size, GFP_KERNEL);
  307. if (!class_name)
  308. return NULL;
  309. strcpy(class_name, name);
  310. strcat(class_name, ":");
  311. strcat(class_name, kobject_name(kobj));
  312. return class_name;
  313. }
  314. static int make_deprecated_class_device_links(struct class_device *class_dev)
  315. {
  316. char *class_name;
  317. int error;
  318. if (!class_dev->dev)
  319. return 0;
  320. class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
  321. if (class_name)
  322. error = sysfs_create_link(&class_dev->dev->kobj,
  323. &class_dev->kobj, class_name);
  324. else
  325. error = -ENOMEM;
  326. kfree(class_name);
  327. return error;
  328. }
  329. static void remove_deprecated_class_device_links(struct class_device *class_dev)
  330. {
  331. char *class_name;
  332. if (!class_dev->dev)
  333. return;
  334. class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
  335. if (class_name)
  336. sysfs_remove_link(&class_dev->dev->kobj, class_name);
  337. kfree(class_name);
  338. }
  339. #else
  340. static inline int make_deprecated_class_device_links(struct class_device *cd)
  341. { return 0; }
  342. static void remove_deprecated_class_device_links(struct class_device *cd)
  343. { }
  344. #endif
  345. static int class_uevent(struct kset *kset, struct kobject *kobj,
  346. struct kobj_uevent_env *env)
  347. {
  348. struct class_device *class_dev = to_class_dev(kobj);
  349. struct device *dev = class_dev->dev;
  350. int retval = 0;
  351. pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
  352. if (MAJOR(class_dev->devt)) {
  353. add_uevent_var(env, "MAJOR=%u", MAJOR(class_dev->devt));
  354. add_uevent_var(env, "MINOR=%u", MINOR(class_dev->devt));
  355. }
  356. if (dev) {
  357. const char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
  358. if (path) {
  359. add_uevent_var(env, "PHYSDEVPATH=%s", path);
  360. kfree(path);
  361. }
  362. if (dev->bus)
  363. add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
  364. if (dev->driver)
  365. add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
  366. }
  367. if (class_dev->uevent) {
  368. /* have the class device specific function add its stuff */
  369. retval = class_dev->uevent(class_dev, env);
  370. if (retval)
  371. pr_debug("class_dev->uevent() returned %d\n", retval);
  372. } else if (class_dev->class->uevent) {
  373. /* have the class specific function add its stuff */
  374. retval = class_dev->class->uevent(class_dev, env);
  375. if (retval)
  376. pr_debug("class->uevent() returned %d\n", retval);
  377. }
  378. return retval;
  379. }
  380. static struct kset_uevent_ops class_uevent_ops = {
  381. .filter = class_uevent_filter,
  382. .name = class_uevent_name,
  383. .uevent = class_uevent,
  384. };
  385. /*
  386. * DO NOT copy how this is created, kset_create_and_add() should be
  387. * called, but this is a hold-over from the old-way and will be deleted
  388. * entirely soon.
  389. */
  390. static struct kset class_obj_subsys = {
  391. .uevent_ops = &class_uevent_ops,
  392. };
  393. static int class_device_add_attrs(struct class_device * cd)
  394. {
  395. int i;
  396. int error = 0;
  397. struct class * cls = cd->class;
  398. if (cls->class_dev_attrs) {
  399. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
  400. error = class_device_create_file(cd,
  401. &cls->class_dev_attrs[i]);
  402. if (error)
  403. goto Err;
  404. }
  405. }
  406. Done:
  407. return error;
  408. Err:
  409. while (--i >= 0)
  410. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  411. goto Done;
  412. }
  413. static void class_device_remove_attrs(struct class_device * cd)
  414. {
  415. int i;
  416. struct class * cls = cd->class;
  417. if (cls->class_dev_attrs) {
  418. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
  419. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  420. }
  421. }
  422. static int class_device_add_groups(struct class_device * cd)
  423. {
  424. int i;
  425. int error = 0;
  426. if (cd->groups) {
  427. for (i = 0; cd->groups[i]; i++) {
  428. error = sysfs_create_group(&cd->kobj, cd->groups[i]);
  429. if (error) {
  430. while (--i >= 0)
  431. sysfs_remove_group(&cd->kobj, cd->groups[i]);
  432. goto out;
  433. }
  434. }
  435. }
  436. out:
  437. return error;
  438. }
  439. static void class_device_remove_groups(struct class_device * cd)
  440. {
  441. int i;
  442. if (cd->groups) {
  443. for (i = 0; cd->groups[i]; i++) {
  444. sysfs_remove_group(&cd->kobj, cd->groups[i]);
  445. }
  446. }
  447. }
  448. static ssize_t show_dev(struct class_device *class_dev, char *buf)
  449. {
  450. return print_dev_t(buf, class_dev->devt);
  451. }
  452. static struct class_device_attribute class_devt_attr =
  453. __ATTR(dev, S_IRUGO, show_dev, NULL);
  454. static ssize_t store_uevent(struct class_device *class_dev,
  455. const char *buf, size_t count)
  456. {
  457. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  458. return count;
  459. }
  460. static struct class_device_attribute class_uevent_attr =
  461. __ATTR(uevent, S_IWUSR, NULL, store_uevent);
  462. void class_device_initialize(struct class_device *class_dev)
  463. {
  464. class_dev->kobj.kset = &class_obj_subsys;
  465. kobject_init(&class_dev->kobj, &class_device_ktype);
  466. INIT_LIST_HEAD(&class_dev->node);
  467. }
  468. int class_device_add(struct class_device *class_dev)
  469. {
  470. struct class *parent_class = NULL;
  471. struct class_device *parent_class_dev = NULL;
  472. struct class_interface *class_intf;
  473. int error = -EINVAL;
  474. class_dev = class_device_get(class_dev);
  475. if (!class_dev)
  476. return -EINVAL;
  477. if (!strlen(class_dev->class_id))
  478. goto out1;
  479. parent_class = class_get(class_dev->class);
  480. if (!parent_class)
  481. goto out1;
  482. parent_class_dev = class_device_get(class_dev->parent);
  483. pr_debug("CLASS: registering class device: ID = '%s'\n",
  484. class_dev->class_id);
  485. /* first, register with generic layer. */
  486. if (parent_class_dev)
  487. class_dev->kobj.parent = &parent_class_dev->kobj;
  488. else
  489. class_dev->kobj.parent = &parent_class->subsys.kobj;
  490. error = kobject_add(&class_dev->kobj, class_dev->kobj.parent,
  491. "%s", class_dev->class_id);
  492. if (error)
  493. goto out2;
  494. /* add the needed attributes to this device */
  495. error = sysfs_create_link(&class_dev->kobj,
  496. &parent_class->subsys.kobj, "subsystem");
  497. if (error)
  498. goto out3;
  499. error = class_device_create_file(class_dev, &class_uevent_attr);
  500. if (error)
  501. goto out3;
  502. if (MAJOR(class_dev->devt)) {
  503. error = class_device_create_file(class_dev, &class_devt_attr);
  504. if (error)
  505. goto out4;
  506. }
  507. error = class_device_add_attrs(class_dev);
  508. if (error)
  509. goto out5;
  510. if (class_dev->dev) {
  511. error = sysfs_create_link(&class_dev->kobj,
  512. &class_dev->dev->kobj, "device");
  513. if (error)
  514. goto out6;
  515. }
  516. error = class_device_add_groups(class_dev);
  517. if (error)
  518. goto out7;
  519. error = make_deprecated_class_device_links(class_dev);
  520. if (error)
  521. goto out8;
  522. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  523. /* notify any interfaces this device is now here */
  524. down(&parent_class->sem);
  525. list_add_tail(&class_dev->node, &parent_class->children);
  526. list_for_each_entry(class_intf, &parent_class->interfaces, node) {
  527. if (class_intf->add)
  528. class_intf->add(class_dev, class_intf);
  529. }
  530. up(&parent_class->sem);
  531. goto out1;
  532. out8:
  533. class_device_remove_groups(class_dev);
  534. out7:
  535. if (class_dev->dev)
  536. sysfs_remove_link(&class_dev->kobj, "device");
  537. out6:
  538. class_device_remove_attrs(class_dev);
  539. out5:
  540. if (MAJOR(class_dev->devt))
  541. class_device_remove_file(class_dev, &class_devt_attr);
  542. out4:
  543. class_device_remove_file(class_dev, &class_uevent_attr);
  544. out3:
  545. kobject_del(&class_dev->kobj);
  546. out2:
  547. if(parent_class_dev)
  548. class_device_put(parent_class_dev);
  549. class_put(parent_class);
  550. out1:
  551. class_device_put(class_dev);
  552. return error;
  553. }
  554. int class_device_register(struct class_device *class_dev)
  555. {
  556. class_device_initialize(class_dev);
  557. return class_device_add(class_dev);
  558. }
  559. /**
  560. * class_device_create - creates a class device and registers it with sysfs
  561. * @cls: pointer to the struct class that this device should be registered to.
  562. * @parent: pointer to the parent struct class_device of this new device, if any.
  563. * @devt: the dev_t for the char device to be added.
  564. * @device: a pointer to a struct device that is assiociated with this class device.
  565. * @fmt: string for the class device's name
  566. *
  567. * This function can be used by char device classes. A struct
  568. * class_device will be created in sysfs, registered to the specified
  569. * class.
  570. * A "dev" file will be created, showing the dev_t for the device, if
  571. * the dev_t is not 0,0.
  572. * If a pointer to a parent struct class_device is passed in, the newly
  573. * created struct class_device will be a child of that device in sysfs.
  574. * The pointer to the struct class_device will be returned from the
  575. * call. Any further sysfs files that might be required can be created
  576. * using this pointer.
  577. *
  578. * Note: the struct class passed to this function must have previously
  579. * been created with a call to class_create().
  580. */
  581. struct class_device *class_device_create(struct class *cls,
  582. struct class_device *parent,
  583. dev_t devt,
  584. struct device *device,
  585. const char *fmt, ...)
  586. {
  587. va_list args;
  588. struct class_device *class_dev = NULL;
  589. int retval = -ENODEV;
  590. if (cls == NULL || IS_ERR(cls))
  591. goto error;
  592. class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
  593. if (!class_dev) {
  594. retval = -ENOMEM;
  595. goto error;
  596. }
  597. class_dev->devt = devt;
  598. class_dev->dev = device;
  599. class_dev->class = cls;
  600. class_dev->parent = parent;
  601. class_dev->release = class_device_create_release;
  602. class_dev->uevent = class_device_create_uevent;
  603. va_start(args, fmt);
  604. vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
  605. va_end(args);
  606. retval = class_device_register(class_dev);
  607. if (retval)
  608. goto error;
  609. return class_dev;
  610. error:
  611. kfree(class_dev);
  612. return ERR_PTR(retval);
  613. }
  614. void class_device_del(struct class_device *class_dev)
  615. {
  616. struct class *parent_class = class_dev->class;
  617. struct class_device *parent_device = class_dev->parent;
  618. struct class_interface *class_intf;
  619. if (parent_class) {
  620. down(&parent_class->sem);
  621. list_del_init(&class_dev->node);
  622. list_for_each_entry(class_intf, &parent_class->interfaces, node)
  623. if (class_intf->remove)
  624. class_intf->remove(class_dev, class_intf);
  625. up(&parent_class->sem);
  626. }
  627. if (class_dev->dev) {
  628. remove_deprecated_class_device_links(class_dev);
  629. sysfs_remove_link(&class_dev->kobj, "device");
  630. }
  631. sysfs_remove_link(&class_dev->kobj, "subsystem");
  632. class_device_remove_file(class_dev, &class_uevent_attr);
  633. if (MAJOR(class_dev->devt))
  634. class_device_remove_file(class_dev, &class_devt_attr);
  635. class_device_remove_attrs(class_dev);
  636. class_device_remove_groups(class_dev);
  637. kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
  638. kobject_del(&class_dev->kobj);
  639. class_device_put(parent_device);
  640. class_put(parent_class);
  641. }
  642. void class_device_unregister(struct class_device *class_dev)
  643. {
  644. pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
  645. class_dev->class_id);
  646. class_device_del(class_dev);
  647. class_device_put(class_dev);
  648. }
  649. /**
  650. * class_device_destroy - removes a class device that was created with class_device_create()
  651. * @cls: the pointer to the struct class that this device was registered * with.
  652. * @devt: the dev_t of the device that was previously registered.
  653. *
  654. * This call unregisters and cleans up a class device that was created with a
  655. * call to class_device_create()
  656. */
  657. void class_device_destroy(struct class *cls, dev_t devt)
  658. {
  659. struct class_device *class_dev = NULL;
  660. struct class_device *class_dev_tmp;
  661. down(&cls->sem);
  662. list_for_each_entry(class_dev_tmp, &cls->children, node) {
  663. if (class_dev_tmp->devt == devt) {
  664. class_dev = class_dev_tmp;
  665. break;
  666. }
  667. }
  668. up(&cls->sem);
  669. if (class_dev)
  670. class_device_unregister(class_dev);
  671. }
  672. struct class_device * class_device_get(struct class_device *class_dev)
  673. {
  674. if (class_dev)
  675. return to_class_dev(kobject_get(&class_dev->kobj));
  676. return NULL;
  677. }
  678. void class_device_put(struct class_device *class_dev)
  679. {
  680. if (class_dev)
  681. kobject_put(&class_dev->kobj);
  682. }
  683. /**
  684. * class_for_each_device - device iterator
  685. * @class: the class we're iterating
  686. * @data: data for the callback
  687. * @fn: function to be called for each device
  688. *
  689. * Iterate over @class's list of devices, and call @fn for each,
  690. * passing it @data.
  691. *
  692. * We check the return of @fn each time. If it returns anything
  693. * other than 0, we break out and return that value.
  694. *
  695. * Note, we hold class->sem in this function, so it can not be
  696. * re-acquired in @fn, otherwise it will self-deadlocking. For
  697. * example, calls to add or remove class members would be verboten.
  698. */
  699. int class_for_each_device(struct class *class, void *data,
  700. int (*fn)(struct device *, void *))
  701. {
  702. struct device *dev;
  703. int error = 0;
  704. if (!class)
  705. return -EINVAL;
  706. down(&class->sem);
  707. list_for_each_entry(dev, &class->devices, node) {
  708. dev = get_device(dev);
  709. if (dev) {
  710. error = fn(dev, data);
  711. put_device(dev);
  712. } else
  713. error = -ENODEV;
  714. if (error)
  715. break;
  716. }
  717. up(&class->sem);
  718. return error;
  719. }
  720. EXPORT_SYMBOL_GPL(class_for_each_device);
  721. /**
  722. * class_find_device - device iterator for locating a particular device
  723. * @class: the class we're iterating
  724. * @data: data for the match function
  725. * @match: function to check device
  726. *
  727. * This is similar to the class_for_each_dev() function above, but it
  728. * returns a reference to a device that is 'found' for later use, as
  729. * determined by the @match callback.
  730. *
  731. * The callback should return 0 if the device doesn't match and non-zero
  732. * if it does. If the callback returns non-zero, this function will
  733. * return to the caller and not iterate over any more devices.
  734. * Note, you will need to drop the reference with put_device() after use.
  735. *
  736. * We hold class->sem in this function, so it can not be
  737. * re-acquired in @match, otherwise it will self-deadlocking. For
  738. * example, calls to add or remove class members would be verboten.
  739. */
  740. struct device *class_find_device(struct class *class, void *data,
  741. int (*match)(struct device *, void *))
  742. {
  743. struct device *dev;
  744. int found = 0;
  745. if (!class)
  746. return NULL;
  747. down(&class->sem);
  748. list_for_each_entry(dev, &class->devices, node) {
  749. dev = get_device(dev);
  750. if (dev) {
  751. if (match(dev, data)) {
  752. found = 1;
  753. break;
  754. } else
  755. put_device(dev);
  756. } else
  757. break;
  758. }
  759. up(&class->sem);
  760. return found ? dev : NULL;
  761. }
  762. EXPORT_SYMBOL_GPL(class_find_device);
  763. /**
  764. * class_find_child - device iterator for locating a particular class_device
  765. * @class: the class we're iterating
  766. * @data: data for the match function
  767. * @match: function to check class_device
  768. *
  769. * This function returns a reference to a class_device that is 'found' for
  770. * later use, as determined by the @match callback.
  771. *
  772. * The callback should return 0 if the class_device doesn't match and non-zero
  773. * if it does. If the callback returns non-zero, this function will
  774. * return to the caller and not iterate over any more class_devices.
  775. *
  776. * Note, you will need to drop the reference with class_device_put() after use.
  777. *
  778. * We hold class->sem in this function, so it can not be
  779. * re-acquired in @match, otherwise it will self-deadlocking. For
  780. * example, calls to add or remove class members would be verboten.
  781. */
  782. struct class_device *class_find_child(struct class *class, void *data,
  783. int (*match)(struct class_device *, void *))
  784. {
  785. struct class_device *dev;
  786. int found = 0;
  787. if (!class)
  788. return NULL;
  789. down(&class->sem);
  790. list_for_each_entry(dev, &class->children, node) {
  791. dev = class_device_get(dev);
  792. if (dev) {
  793. if (match(dev, data)) {
  794. found = 1;
  795. break;
  796. } else
  797. class_device_put(dev);
  798. } else
  799. break;
  800. }
  801. up(&class->sem);
  802. return found ? dev : NULL;
  803. }
  804. EXPORT_SYMBOL_GPL(class_find_child);
  805. int class_interface_register(struct class_interface *class_intf)
  806. {
  807. struct class *parent;
  808. struct class_device *class_dev;
  809. struct device *dev;
  810. if (!class_intf || !class_intf->class)
  811. return -ENODEV;
  812. parent = class_get(class_intf->class);
  813. if (!parent)
  814. return -EINVAL;
  815. down(&parent->sem);
  816. list_add_tail(&class_intf->node, &parent->interfaces);
  817. if (class_intf->add) {
  818. list_for_each_entry(class_dev, &parent->children, node)
  819. class_intf->add(class_dev, class_intf);
  820. }
  821. if (class_intf->add_dev) {
  822. list_for_each_entry(dev, &parent->devices, node)
  823. class_intf->add_dev(dev, class_intf);
  824. }
  825. up(&parent->sem);
  826. return 0;
  827. }
  828. void class_interface_unregister(struct class_interface *class_intf)
  829. {
  830. struct class * parent = class_intf->class;
  831. struct class_device *class_dev;
  832. struct device *dev;
  833. if (!parent)
  834. return;
  835. down(&parent->sem);
  836. list_del_init(&class_intf->node);
  837. if (class_intf->remove) {
  838. list_for_each_entry(class_dev, &parent->children, node)
  839. class_intf->remove(class_dev, class_intf);
  840. }
  841. if (class_intf->remove_dev) {
  842. list_for_each_entry(dev, &parent->devices, node)
  843. class_intf->remove_dev(dev, class_intf);
  844. }
  845. up(&parent->sem);
  846. class_put(parent);
  847. }
  848. int __init classes_init(void)
  849. {
  850. class_kset = kset_create_and_add("class", NULL, NULL);
  851. if (!class_kset)
  852. return -ENOMEM;
  853. /* ick, this is ugly, the things we go through to keep from showing up
  854. * in sysfs... */
  855. kset_init(&class_obj_subsys);
  856. kobject_set_name(&class_obj_subsys.kobj, "class_obj");
  857. if (!class_obj_subsys.kobj.parent)
  858. class_obj_subsys.kobj.parent = &class_obj_subsys.kobj;
  859. return 0;
  860. }
  861. EXPORT_SYMBOL_GPL(class_create_file);
  862. EXPORT_SYMBOL_GPL(class_remove_file);
  863. EXPORT_SYMBOL_GPL(class_register);
  864. EXPORT_SYMBOL_GPL(class_unregister);
  865. EXPORT_SYMBOL_GPL(class_create);
  866. EXPORT_SYMBOL_GPL(class_destroy);
  867. EXPORT_SYMBOL_GPL(class_device_register);
  868. EXPORT_SYMBOL_GPL(class_device_unregister);
  869. EXPORT_SYMBOL_GPL(class_device_initialize);
  870. EXPORT_SYMBOL_GPL(class_device_add);
  871. EXPORT_SYMBOL_GPL(class_device_del);
  872. EXPORT_SYMBOL_GPL(class_device_get);
  873. EXPORT_SYMBOL_GPL(class_device_put);
  874. EXPORT_SYMBOL_GPL(class_device_create);
  875. EXPORT_SYMBOL_GPL(class_device_destroy);
  876. EXPORT_SYMBOL_GPL(class_device_create_file);
  877. EXPORT_SYMBOL_GPL(class_device_remove_file);
  878. EXPORT_SYMBOL_GPL(class_device_create_bin_file);
  879. EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
  880. EXPORT_SYMBOL_GPL(class_interface_register);
  881. EXPORT_SYMBOL_GPL(class_interface_unregister);