class.c 24 KB

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