class.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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. extern struct subsystem devices_subsys;
  21. #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
  22. #define to_class(obj) container_of(obj, struct class, subsys.kset.kobj)
  23. static ssize_t
  24. class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  25. {
  26. struct class_attribute * class_attr = to_class_attr(attr);
  27. struct class * dc = to_class(kobj);
  28. ssize_t ret = -EIO;
  29. if (class_attr->show)
  30. ret = class_attr->show(dc, buf);
  31. return ret;
  32. }
  33. static ssize_t
  34. class_attr_store(struct kobject * kobj, struct attribute * attr,
  35. const char * buf, size_t count)
  36. {
  37. struct class_attribute * class_attr = to_class_attr(attr);
  38. struct class * dc = to_class(kobj);
  39. ssize_t ret = -EIO;
  40. if (class_attr->store)
  41. ret = class_attr->store(dc, buf, count);
  42. return ret;
  43. }
  44. static void class_release(struct kobject * kobj)
  45. {
  46. struct class *class = to_class(kobj);
  47. pr_debug("class '%s': release.\n", class->name);
  48. if (class->class_release)
  49. class->class_release(class);
  50. else
  51. pr_debug("class '%s' does not have a release() function, "
  52. "be careful\n", class->name);
  53. }
  54. static struct sysfs_ops class_sysfs_ops = {
  55. .show = class_attr_show,
  56. .store = class_attr_store,
  57. };
  58. static struct kobj_type ktype_class = {
  59. .sysfs_ops = &class_sysfs_ops,
  60. .release = class_release,
  61. };
  62. /* Hotplug events for classes go to the class_obj subsys */
  63. static decl_subsys(class, &ktype_class, NULL);
  64. int class_create_file(struct class * cls, const struct class_attribute * attr)
  65. {
  66. int error;
  67. if (cls) {
  68. error = sysfs_create_file(&cls->subsys.kset.kobj, &attr->attr);
  69. } else
  70. error = -EINVAL;
  71. return error;
  72. }
  73. void class_remove_file(struct class * cls, const struct class_attribute * attr)
  74. {
  75. if (cls)
  76. sysfs_remove_file(&cls->subsys.kset.kobj, &attr->attr);
  77. }
  78. static struct class *class_get(struct class *cls)
  79. {
  80. if (cls)
  81. return container_of(subsys_get(&cls->subsys), struct class, subsys);
  82. return NULL;
  83. }
  84. static void class_put(struct class * cls)
  85. {
  86. if (cls)
  87. subsys_put(&cls->subsys);
  88. }
  89. static int add_class_attrs(struct class * cls)
  90. {
  91. int i;
  92. int error = 0;
  93. if (cls->class_attrs) {
  94. for (i = 0; attr_name(cls->class_attrs[i]); i++) {
  95. error = class_create_file(cls,&cls->class_attrs[i]);
  96. if (error)
  97. goto Err;
  98. }
  99. }
  100. Done:
  101. return error;
  102. Err:
  103. while (--i >= 0)
  104. class_remove_file(cls,&cls->class_attrs[i]);
  105. goto Done;
  106. }
  107. static void remove_class_attrs(struct class * cls)
  108. {
  109. int i;
  110. if (cls->class_attrs) {
  111. for (i = 0; attr_name(cls->class_attrs[i]); i++)
  112. class_remove_file(cls,&cls->class_attrs[i]);
  113. }
  114. }
  115. int class_register(struct class * cls)
  116. {
  117. int error;
  118. pr_debug("device class '%s': registering\n", cls->name);
  119. INIT_LIST_HEAD(&cls->children);
  120. INIT_LIST_HEAD(&cls->devices);
  121. INIT_LIST_HEAD(&cls->interfaces);
  122. init_MUTEX(&cls->sem);
  123. error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name);
  124. if (error)
  125. return error;
  126. subsys_set_kset(cls, class_subsys);
  127. error = subsystem_register(&cls->subsys);
  128. if (!error) {
  129. error = add_class_attrs(class_get(cls));
  130. class_put(cls);
  131. }
  132. return error;
  133. }
  134. void class_unregister(struct class * cls)
  135. {
  136. pr_debug("device class '%s': unregistering\n", cls->name);
  137. remove_class_attrs(cls);
  138. subsystem_unregister(&cls->subsys);
  139. }
  140. static void class_create_release(struct class *cls)
  141. {
  142. pr_debug("%s called for %s\n", __FUNCTION__, cls->name);
  143. kfree(cls);
  144. }
  145. static void class_device_create_release(struct class_device *class_dev)
  146. {
  147. pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
  148. kfree(class_dev);
  149. }
  150. /* needed to allow these devices to have parent class devices */
  151. static int class_device_create_uevent(struct class_device *class_dev,
  152. char **envp, int num_envp,
  153. char *buffer, int buffer_size)
  154. {
  155. pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id);
  156. return 0;
  157. }
  158. /**
  159. * class_create - create a struct class structure
  160. * @owner: pointer to the module that is to "own" this struct class
  161. * @name: pointer to a string for the name of this class.
  162. *
  163. * This is used to create a struct class pointer that can then be used
  164. * in calls to class_device_create().
  165. *
  166. * Note, the pointer created here is to be destroyed when finished by
  167. * making a call to class_destroy().
  168. */
  169. struct class *class_create(struct module *owner, 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 ERR_PTR(-ENOMEM);
  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. error = sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
  341. class_name);
  342. kfree(class_name);
  343. return error;
  344. }
  345. static void remove_deprecated_class_device_links(struct class_device *class_dev)
  346. {
  347. char *class_name;
  348. if (!class_dev->dev)
  349. return;
  350. class_name = make_class_name(class_dev->class->name, &class_dev->kobj);
  351. sysfs_remove_link(&class_dev->dev->kobj, class_name);
  352. kfree(class_name);
  353. }
  354. #else
  355. static inline int deprecated_class_uevent(char **envp, int num_envp,
  356. int *cur_index, char *buffer,
  357. int buffer_size, int *cur_len,
  358. struct class_device *class_dev)
  359. { return 0; }
  360. static inline int make_deprecated_class_device_links(struct class_device *cd)
  361. { return 0; }
  362. static void remove_deprecated_class_device_links(struct class_device *cd)
  363. { }
  364. #endif
  365. static int class_uevent(struct kset *kset, struct kobject *kobj, char **envp,
  366. int num_envp, char *buffer, int buffer_size)
  367. {
  368. struct class_device *class_dev = to_class_dev(kobj);
  369. int i = 0;
  370. int length = 0;
  371. int retval = 0;
  372. pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
  373. deprecated_class_uevent(envp, num_envp, &i, buffer, buffer_size,
  374. &length, class_dev);
  375. if (MAJOR(class_dev->devt)) {
  376. add_uevent_var(envp, num_envp, &i,
  377. buffer, buffer_size, &length,
  378. "MAJOR=%u", MAJOR(class_dev->devt));
  379. add_uevent_var(envp, num_envp, &i,
  380. buffer, buffer_size, &length,
  381. "MINOR=%u", MINOR(class_dev->devt));
  382. }
  383. /* terminate, set to next free slot, shrink available space */
  384. envp[i] = NULL;
  385. envp = &envp[i];
  386. num_envp -= i;
  387. buffer = &buffer[length];
  388. buffer_size -= length;
  389. if (class_dev->uevent) {
  390. /* have the class device specific function add its stuff */
  391. retval = class_dev->uevent(class_dev, envp, num_envp,
  392. buffer, buffer_size);
  393. if (retval)
  394. pr_debug("class_dev->uevent() returned %d\n", retval);
  395. } else if (class_dev->class->uevent) {
  396. /* have the class specific function add its stuff */
  397. retval = class_dev->class->uevent(class_dev, envp, num_envp,
  398. buffer, buffer_size);
  399. if (retval)
  400. pr_debug("class->uevent() returned %d\n", retval);
  401. }
  402. return retval;
  403. }
  404. static struct kset_uevent_ops class_uevent_ops = {
  405. .filter = class_uevent_filter,
  406. .name = class_uevent_name,
  407. .uevent = class_uevent,
  408. };
  409. static decl_subsys(class_obj, &ktype_class_device, &class_uevent_ops);
  410. static int class_device_add_attrs(struct class_device * cd)
  411. {
  412. int i;
  413. int error = 0;
  414. struct class * cls = cd->class;
  415. if (cls->class_dev_attrs) {
  416. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
  417. error = class_device_create_file(cd,
  418. &cls->class_dev_attrs[i]);
  419. if (error)
  420. goto Err;
  421. }
  422. }
  423. Done:
  424. return error;
  425. Err:
  426. while (--i >= 0)
  427. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  428. goto Done;
  429. }
  430. static void class_device_remove_attrs(struct class_device * cd)
  431. {
  432. int i;
  433. struct class * cls = cd->class;
  434. if (cls->class_dev_attrs) {
  435. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
  436. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  437. }
  438. }
  439. static int class_device_add_groups(struct class_device * cd)
  440. {
  441. int i;
  442. int error = 0;
  443. if (cd->groups) {
  444. for (i = 0; cd->groups[i]; i++) {
  445. error = sysfs_create_group(&cd->kobj, cd->groups[i]);
  446. if (error) {
  447. while (--i >= 0)
  448. sysfs_remove_group(&cd->kobj, cd->groups[i]);
  449. goto out;
  450. }
  451. }
  452. }
  453. out:
  454. return error;
  455. }
  456. static void class_device_remove_groups(struct class_device * cd)
  457. {
  458. int i;
  459. if (cd->groups) {
  460. for (i = 0; cd->groups[i]; i++) {
  461. sysfs_remove_group(&cd->kobj, cd->groups[i]);
  462. }
  463. }
  464. }
  465. static ssize_t show_dev(struct class_device *class_dev, char *buf)
  466. {
  467. return print_dev_t(buf, class_dev->devt);
  468. }
  469. static ssize_t store_uevent(struct class_device *class_dev,
  470. const char *buf, size_t count)
  471. {
  472. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  473. return count;
  474. }
  475. void class_device_initialize(struct class_device *class_dev)
  476. {
  477. kobj_set_kset_s(class_dev, class_obj_subsys);
  478. kobject_init(&class_dev->kobj);
  479. INIT_LIST_HEAD(&class_dev->node);
  480. }
  481. int class_device_add(struct class_device *class_dev)
  482. {
  483. struct class *parent_class = NULL;
  484. struct class_device *parent_class_dev = NULL;
  485. struct class_interface *class_intf;
  486. int error = -EINVAL;
  487. class_dev = class_device_get(class_dev);
  488. if (!class_dev)
  489. return -EINVAL;
  490. if (!strlen(class_dev->class_id))
  491. goto out1;
  492. parent_class = class_get(class_dev->class);
  493. if (!parent_class)
  494. goto out1;
  495. parent_class_dev = class_device_get(class_dev->parent);
  496. pr_debug("CLASS: registering class device: ID = '%s'\n",
  497. class_dev->class_id);
  498. /* first, register with generic layer. */
  499. error = kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
  500. if (error)
  501. goto out2;
  502. if (parent_class_dev)
  503. class_dev->kobj.parent = &parent_class_dev->kobj;
  504. else
  505. class_dev->kobj.parent = &parent_class->subsys.kset.kobj;
  506. error = kobject_add(&class_dev->kobj);
  507. if (error)
  508. goto out2;
  509. /* add the needed attributes to this device */
  510. error = sysfs_create_link(&class_dev->kobj,
  511. &parent_class->subsys.kset.kobj, "subsystem");
  512. if (error)
  513. goto out3;
  514. class_dev->uevent_attr.attr.name = "uevent";
  515. class_dev->uevent_attr.attr.mode = S_IWUSR;
  516. class_dev->uevent_attr.attr.owner = parent_class->owner;
  517. class_dev->uevent_attr.store = store_uevent;
  518. error = class_device_create_file(class_dev, &class_dev->uevent_attr);
  519. if (error)
  520. goto out3;
  521. if (MAJOR(class_dev->devt)) {
  522. struct class_device_attribute *attr;
  523. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  524. if (!attr) {
  525. error = -ENOMEM;
  526. goto out4;
  527. }
  528. attr->attr.name = "dev";
  529. attr->attr.mode = S_IRUGO;
  530. attr->attr.owner = parent_class->owner;
  531. attr->show = show_dev;
  532. error = class_device_create_file(class_dev, attr);
  533. if (error) {
  534. kfree(attr);
  535. goto out4;
  536. }
  537. class_dev->devt_attr = attr;
  538. }
  539. error = class_device_add_attrs(class_dev);
  540. if (error)
  541. goto out5;
  542. if (class_dev->dev) {
  543. error = sysfs_create_link(&class_dev->kobj,
  544. &class_dev->dev->kobj, "device");
  545. if (error)
  546. goto out6;
  547. }
  548. error = class_device_add_groups(class_dev);
  549. if (error)
  550. goto out7;
  551. error = make_deprecated_class_device_links(class_dev);
  552. if (error)
  553. goto out8;
  554. kobject_uevent(&class_dev->kobj, KOBJ_ADD);
  555. /* notify any interfaces this device is now here */
  556. down(&parent_class->sem);
  557. list_add_tail(&class_dev->node, &parent_class->children);
  558. list_for_each_entry(class_intf, &parent_class->interfaces, node) {
  559. if (class_intf->add)
  560. class_intf->add(class_dev, class_intf);
  561. }
  562. up(&parent_class->sem);
  563. goto out1;
  564. out8:
  565. class_device_remove_groups(class_dev);
  566. out7:
  567. if (class_dev->dev)
  568. sysfs_remove_link(&class_dev->kobj, "device");
  569. out6:
  570. class_device_remove_attrs(class_dev);
  571. out5:
  572. if (class_dev->devt_attr)
  573. class_device_remove_file(class_dev, class_dev->devt_attr);
  574. out4:
  575. class_device_remove_file(class_dev, &class_dev->uevent_attr);
  576. out3:
  577. kobject_del(&class_dev->kobj);
  578. out2:
  579. if(parent_class_dev)
  580. class_device_put(parent_class_dev);
  581. class_put(parent_class);
  582. out1:
  583. class_device_put(class_dev);
  584. return error;
  585. }
  586. int class_device_register(struct class_device *class_dev)
  587. {
  588. class_device_initialize(class_dev);
  589. return class_device_add(class_dev);
  590. }
  591. /**
  592. * class_device_create - creates a class device and registers it with sysfs
  593. * @cls: pointer to the struct class that this device should be registered to.
  594. * @parent: pointer to the parent struct class_device of this new device, if any.
  595. * @devt: the dev_t for the char device to be added.
  596. * @device: a pointer to a struct device that is assiociated with this class device.
  597. * @fmt: string for the class device's name
  598. *
  599. * This function can be used by char device classes. A struct
  600. * class_device will be created in sysfs, registered to the specified
  601. * class.
  602. * A "dev" file will be created, showing the dev_t for the device, if
  603. * the dev_t is not 0,0.
  604. * If a pointer to a parent struct class_device is passed in, the newly
  605. * created struct class_device will be a child of that device in sysfs.
  606. * The pointer to the struct class_device will be returned from the
  607. * call. Any further sysfs files that might be required can be created
  608. * using this pointer.
  609. *
  610. * Note: the struct class passed to this function must have previously
  611. * been created with a call to class_create().
  612. */
  613. struct class_device *class_device_create(struct class *cls,
  614. struct class_device *parent,
  615. dev_t devt,
  616. struct device *device,
  617. const char *fmt, ...)
  618. {
  619. va_list args;
  620. struct class_device *class_dev = NULL;
  621. int retval = -ENODEV;
  622. if (cls == NULL || IS_ERR(cls))
  623. goto error;
  624. class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
  625. if (!class_dev) {
  626. retval = -ENOMEM;
  627. goto error;
  628. }
  629. class_dev->devt = devt;
  630. class_dev->dev = device;
  631. class_dev->class = cls;
  632. class_dev->parent = parent;
  633. class_dev->release = class_device_create_release;
  634. class_dev->uevent = class_device_create_uevent;
  635. va_start(args, fmt);
  636. vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
  637. va_end(args);
  638. retval = class_device_register(class_dev);
  639. if (retval)
  640. goto error;
  641. return class_dev;
  642. error:
  643. kfree(class_dev);
  644. return ERR_PTR(retval);
  645. }
  646. void class_device_del(struct class_device *class_dev)
  647. {
  648. struct class *parent_class = class_dev->class;
  649. struct class_device *parent_device = class_dev->parent;
  650. struct class_interface *class_intf;
  651. if (parent_class) {
  652. down(&parent_class->sem);
  653. list_del_init(&class_dev->node);
  654. list_for_each_entry(class_intf, &parent_class->interfaces, node)
  655. if (class_intf->remove)
  656. class_intf->remove(class_dev, class_intf);
  657. up(&parent_class->sem);
  658. }
  659. if (class_dev->dev) {
  660. remove_deprecated_class_device_links(class_dev);
  661. sysfs_remove_link(&class_dev->kobj, "device");
  662. }
  663. sysfs_remove_link(&class_dev->kobj, "subsystem");
  664. class_device_remove_file(class_dev, &class_dev->uevent_attr);
  665. if (class_dev->devt_attr)
  666. class_device_remove_file(class_dev, class_dev->devt_attr);
  667. class_device_remove_attrs(class_dev);
  668. class_device_remove_groups(class_dev);
  669. kobject_uevent(&class_dev->kobj, KOBJ_REMOVE);
  670. kobject_del(&class_dev->kobj);
  671. class_device_put(parent_device);
  672. class_put(parent_class);
  673. }
  674. void class_device_unregister(struct class_device *class_dev)
  675. {
  676. pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
  677. class_dev->class_id);
  678. class_device_del(class_dev);
  679. class_device_put(class_dev);
  680. }
  681. /**
  682. * class_device_destroy - removes a class device that was created with class_device_create()
  683. * @cls: the pointer to the struct class that this device was registered * with.
  684. * @devt: the dev_t of the device that was previously registered.
  685. *
  686. * This call unregisters and cleans up a class device that was created with a
  687. * call to class_device_create()
  688. */
  689. void class_device_destroy(struct class *cls, dev_t devt)
  690. {
  691. struct class_device *class_dev = NULL;
  692. struct class_device *class_dev_tmp;
  693. down(&cls->sem);
  694. list_for_each_entry(class_dev_tmp, &cls->children, node) {
  695. if (class_dev_tmp->devt == devt) {
  696. class_dev = class_dev_tmp;
  697. break;
  698. }
  699. }
  700. up(&cls->sem);
  701. if (class_dev)
  702. class_device_unregister(class_dev);
  703. }
  704. int class_device_rename(struct class_device *class_dev, char *new_name)
  705. {
  706. int error = 0;
  707. char *old_class_name = NULL, *new_class_name = NULL;
  708. class_dev = class_device_get(class_dev);
  709. if (!class_dev)
  710. return -EINVAL;
  711. pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
  712. new_name);
  713. #ifdef CONFIG_SYSFS_DEPRECATED
  714. if (class_dev->dev)
  715. old_class_name = make_class_name(class_dev->class->name,
  716. &class_dev->kobj);
  717. #endif
  718. strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
  719. error = kobject_rename(&class_dev->kobj, new_name);
  720. #ifdef CONFIG_SYSFS_DEPRECATED
  721. if (class_dev->dev) {
  722. new_class_name = make_class_name(class_dev->class->name,
  723. &class_dev->kobj);
  724. sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
  725. new_class_name);
  726. sysfs_remove_link(&class_dev->dev->kobj, old_class_name);
  727. }
  728. #endif
  729. class_device_put(class_dev);
  730. kfree(old_class_name);
  731. kfree(new_class_name);
  732. return error;
  733. }
  734. struct class_device * class_device_get(struct class_device *class_dev)
  735. {
  736. if (class_dev)
  737. return to_class_dev(kobject_get(&class_dev->kobj));
  738. return NULL;
  739. }
  740. void class_device_put(struct class_device *class_dev)
  741. {
  742. if (class_dev)
  743. kobject_put(&class_dev->kobj);
  744. }
  745. int class_interface_register(struct class_interface *class_intf)
  746. {
  747. struct class *parent;
  748. struct class_device *class_dev;
  749. struct device *dev;
  750. if (!class_intf || !class_intf->class)
  751. return -ENODEV;
  752. parent = class_get(class_intf->class);
  753. if (!parent)
  754. return -EINVAL;
  755. down(&parent->sem);
  756. list_add_tail(&class_intf->node, &parent->interfaces);
  757. if (class_intf->add) {
  758. list_for_each_entry(class_dev, &parent->children, node)
  759. class_intf->add(class_dev, class_intf);
  760. }
  761. if (class_intf->add_dev) {
  762. list_for_each_entry(dev, &parent->devices, node)
  763. class_intf->add_dev(dev, class_intf);
  764. }
  765. up(&parent->sem);
  766. return 0;
  767. }
  768. void class_interface_unregister(struct class_interface *class_intf)
  769. {
  770. struct class * parent = class_intf->class;
  771. struct class_device *class_dev;
  772. struct device *dev;
  773. if (!parent)
  774. return;
  775. down(&parent->sem);
  776. list_del_init(&class_intf->node);
  777. if (class_intf->remove) {
  778. list_for_each_entry(class_dev, &parent->children, node)
  779. class_intf->remove(class_dev, class_intf);
  780. }
  781. if (class_intf->remove_dev) {
  782. list_for_each_entry(dev, &parent->devices, node)
  783. class_intf->remove_dev(dev, class_intf);
  784. }
  785. up(&parent->sem);
  786. class_put(parent);
  787. }
  788. int __init classes_init(void)
  789. {
  790. int retval;
  791. retval = subsystem_register(&class_subsys);
  792. if (retval)
  793. return retval;
  794. /* ick, this is ugly, the things we go through to keep from showing up
  795. * in sysfs... */
  796. subsystem_init(&class_obj_subsys);
  797. if (!class_obj_subsys.kset.subsys)
  798. class_obj_subsys.kset.subsys = &class_obj_subsys;
  799. return 0;
  800. }
  801. EXPORT_SYMBOL_GPL(class_create_file);
  802. EXPORT_SYMBOL_GPL(class_remove_file);
  803. EXPORT_SYMBOL_GPL(class_register);
  804. EXPORT_SYMBOL_GPL(class_unregister);
  805. EXPORT_SYMBOL_GPL(class_create);
  806. EXPORT_SYMBOL_GPL(class_destroy);
  807. EXPORT_SYMBOL_GPL(class_device_register);
  808. EXPORT_SYMBOL_GPL(class_device_unregister);
  809. EXPORT_SYMBOL_GPL(class_device_initialize);
  810. EXPORT_SYMBOL_GPL(class_device_add);
  811. EXPORT_SYMBOL_GPL(class_device_del);
  812. EXPORT_SYMBOL_GPL(class_device_get);
  813. EXPORT_SYMBOL_GPL(class_device_put);
  814. EXPORT_SYMBOL_GPL(class_device_create);
  815. EXPORT_SYMBOL_GPL(class_device_destroy);
  816. EXPORT_SYMBOL_GPL(class_device_create_file);
  817. EXPORT_SYMBOL_GPL(class_device_remove_file);
  818. EXPORT_SYMBOL_GPL(class_device_create_bin_file);
  819. EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
  820. EXPORT_SYMBOL_GPL(class_interface_register);
  821. EXPORT_SYMBOL_GPL(class_interface_unregister);