class.c 24 KB

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