class.c 23 KB

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