class.c 24 KB

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