class.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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/config.h>
  13. #include <linux/device.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/string.h>
  17. #include <linux/kdev_t.h>
  18. #include <linux/err.h>
  19. #include "base.h"
  20. #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
  21. #define to_class(obj) container_of(obj, struct class, subsys.kset.kobj)
  22. static ssize_t
  23. class_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  24. {
  25. struct class_attribute * class_attr = to_class_attr(attr);
  26. struct class * dc = to_class(kobj);
  27. ssize_t ret = -EIO;
  28. if (class_attr->show)
  29. ret = class_attr->show(dc, buf);
  30. return ret;
  31. }
  32. static ssize_t
  33. class_attr_store(struct kobject * kobj, struct attribute * attr,
  34. const char * buf, size_t count)
  35. {
  36. struct class_attribute * class_attr = to_class_attr(attr);
  37. struct class * dc = to_class(kobj);
  38. ssize_t ret = -EIO;
  39. if (class_attr->store)
  40. ret = class_attr->store(dc, buf, count);
  41. return ret;
  42. }
  43. static void class_release(struct kobject * kobj)
  44. {
  45. struct class *class = to_class(kobj);
  46. pr_debug("class '%s': release.\n", class->name);
  47. if (class->class_release)
  48. class->class_release(class);
  49. else
  50. pr_debug("class '%s' does not have a release() function, "
  51. "be careful\n", class->name);
  52. }
  53. static struct sysfs_ops class_sysfs_ops = {
  54. .show = class_attr_show,
  55. .store = class_attr_store,
  56. };
  57. static struct kobj_type ktype_class = {
  58. .sysfs_ops = &class_sysfs_ops,
  59. .release = class_release,
  60. };
  61. /* Hotplug events for classes go to the class_obj subsys */
  62. static decl_subsys(class, &ktype_class, NULL);
  63. int class_create_file(struct class * cls, const struct class_attribute * attr)
  64. {
  65. int error;
  66. if (cls) {
  67. error = sysfs_create_file(&cls->subsys.kset.kobj, &attr->attr);
  68. } else
  69. error = -EINVAL;
  70. return error;
  71. }
  72. void class_remove_file(struct class * cls, const struct class_attribute * attr)
  73. {
  74. if (cls)
  75. sysfs_remove_file(&cls->subsys.kset.kobj, &attr->attr);
  76. }
  77. struct class * class_get(struct class * cls)
  78. {
  79. if (cls)
  80. return container_of(subsys_get(&cls->subsys), struct class, subsys);
  81. return NULL;
  82. }
  83. void class_put(struct class * cls)
  84. {
  85. subsys_put(&cls->subsys);
  86. }
  87. static int add_class_attrs(struct class * cls)
  88. {
  89. int i;
  90. int error = 0;
  91. if (cls->class_attrs) {
  92. for (i = 0; attr_name(cls->class_attrs[i]); i++) {
  93. error = class_create_file(cls,&cls->class_attrs[i]);
  94. if (error)
  95. goto Err;
  96. }
  97. }
  98. Done:
  99. return error;
  100. Err:
  101. while (--i >= 0)
  102. class_remove_file(cls,&cls->class_attrs[i]);
  103. goto Done;
  104. }
  105. static void remove_class_attrs(struct class * cls)
  106. {
  107. int i;
  108. if (cls->class_attrs) {
  109. for (i = 0; attr_name(cls->class_attrs[i]); i++)
  110. class_remove_file(cls,&cls->class_attrs[i]);
  111. }
  112. }
  113. int class_register(struct class * cls)
  114. {
  115. int error;
  116. pr_debug("device class '%s': registering\n", cls->name);
  117. INIT_LIST_HEAD(&cls->children);
  118. INIT_LIST_HEAD(&cls->interfaces);
  119. init_MUTEX(&cls->sem);
  120. error = kobject_set_name(&cls->subsys.kset.kobj, "%s", cls->name);
  121. if (error)
  122. return error;
  123. subsys_set_kset(cls, class_subsys);
  124. error = subsystem_register(&cls->subsys);
  125. if (!error) {
  126. error = add_class_attrs(class_get(cls));
  127. class_put(cls);
  128. }
  129. return error;
  130. }
  131. void class_unregister(struct class * cls)
  132. {
  133. pr_debug("device class '%s': unregistering\n", cls->name);
  134. remove_class_attrs(cls);
  135. subsystem_unregister(&cls->subsys);
  136. }
  137. static void class_create_release(struct class *cls)
  138. {
  139. kfree(cls);
  140. }
  141. static void class_device_create_release(struct class_device *class_dev)
  142. {
  143. kfree(class_dev);
  144. }
  145. /**
  146. * class_create - create a struct class structure
  147. * @owner: pointer to the module that is to "own" this struct class
  148. * @name: pointer to a string for the name of this class.
  149. *
  150. * This is used to create a struct class pointer that can then be used
  151. * in calls to class_device_create().
  152. *
  153. * Note, the pointer created here is to be destroyed when finished by
  154. * making a call to class_destroy().
  155. */
  156. struct class *class_create(struct module *owner, char *name)
  157. {
  158. struct class *cls;
  159. int retval;
  160. cls = kzalloc(sizeof(*cls), GFP_KERNEL);
  161. if (!cls) {
  162. retval = -ENOMEM;
  163. goto error;
  164. }
  165. cls->name = name;
  166. cls->owner = owner;
  167. cls->class_release = class_create_release;
  168. cls->release = class_device_create_release;
  169. retval = class_register(cls);
  170. if (retval)
  171. goto error;
  172. return cls;
  173. error:
  174. kfree(cls);
  175. return ERR_PTR(retval);
  176. }
  177. /**
  178. * class_destroy - destroys a struct class structure
  179. * @cs: pointer to the struct class that is to be destroyed
  180. *
  181. * Note, the pointer to be destroyed must have been created with a call
  182. * to class_create().
  183. */
  184. void class_destroy(struct class *cls)
  185. {
  186. if ((cls == NULL) || (IS_ERR(cls)))
  187. return;
  188. class_unregister(cls);
  189. }
  190. /* Class Device Stuff */
  191. int class_device_create_file(struct class_device * class_dev,
  192. const struct class_device_attribute * attr)
  193. {
  194. int error = -EINVAL;
  195. if (class_dev)
  196. error = sysfs_create_file(&class_dev->kobj, &attr->attr);
  197. return error;
  198. }
  199. void class_device_remove_file(struct class_device * class_dev,
  200. const struct class_device_attribute * attr)
  201. {
  202. if (class_dev)
  203. sysfs_remove_file(&class_dev->kobj, &attr->attr);
  204. }
  205. int class_device_create_bin_file(struct class_device *class_dev,
  206. struct bin_attribute *attr)
  207. {
  208. int error = -EINVAL;
  209. if (class_dev)
  210. error = sysfs_create_bin_file(&class_dev->kobj, attr);
  211. return error;
  212. }
  213. void class_device_remove_bin_file(struct class_device *class_dev,
  214. struct bin_attribute *attr)
  215. {
  216. if (class_dev)
  217. sysfs_remove_bin_file(&class_dev->kobj, attr);
  218. }
  219. static ssize_t
  220. class_device_attr_show(struct kobject * kobj, struct attribute * attr,
  221. char * buf)
  222. {
  223. struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
  224. struct class_device * cd = to_class_dev(kobj);
  225. ssize_t ret = 0;
  226. if (class_dev_attr->show)
  227. ret = class_dev_attr->show(cd, buf);
  228. return ret;
  229. }
  230. static ssize_t
  231. class_device_attr_store(struct kobject * kobj, struct attribute * attr,
  232. const char * buf, size_t count)
  233. {
  234. struct class_device_attribute * class_dev_attr = to_class_dev_attr(attr);
  235. struct class_device * cd = to_class_dev(kobj);
  236. ssize_t ret = 0;
  237. if (class_dev_attr->store)
  238. ret = class_dev_attr->store(cd, buf, count);
  239. return ret;
  240. }
  241. static struct sysfs_ops class_dev_sysfs_ops = {
  242. .show = class_device_attr_show,
  243. .store = class_device_attr_store,
  244. };
  245. static void class_dev_release(struct kobject * kobj)
  246. {
  247. struct class_device *cd = to_class_dev(kobj);
  248. struct class * cls = cd->class;
  249. pr_debug("device class '%s': release.\n", cd->class_id);
  250. kfree(cd->devt_attr);
  251. cd->devt_attr = NULL;
  252. if (cls->release)
  253. cls->release(cd);
  254. else {
  255. printk(KERN_ERR "Device class '%s' does not have a release() function, "
  256. "it is broken and must be fixed.\n",
  257. cd->class_id);
  258. WARN_ON(1);
  259. }
  260. }
  261. static struct kobj_type ktype_class_device = {
  262. .sysfs_ops = &class_dev_sysfs_ops,
  263. .release = class_dev_release,
  264. };
  265. static int class_hotplug_filter(struct kset *kset, struct kobject *kobj)
  266. {
  267. struct kobj_type *ktype = get_ktype(kobj);
  268. if (ktype == &ktype_class_device) {
  269. struct class_device *class_dev = to_class_dev(kobj);
  270. if (class_dev->class)
  271. return 1;
  272. }
  273. return 0;
  274. }
  275. static const char *class_hotplug_name(struct kset *kset, struct kobject *kobj)
  276. {
  277. struct class_device *class_dev = to_class_dev(kobj);
  278. return class_dev->class->name;
  279. }
  280. static int class_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
  281. int num_envp, char *buffer, int buffer_size)
  282. {
  283. struct class_device *class_dev = to_class_dev(kobj);
  284. int i = 0;
  285. int length = 0;
  286. int retval = 0;
  287. pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
  288. if (class_dev->dev) {
  289. /* add physical device, backing this device */
  290. struct device *dev = class_dev->dev;
  291. char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
  292. add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
  293. &length, "PHYSDEVPATH=%s", path);
  294. kfree(path);
  295. if (dev->bus)
  296. add_hotplug_env_var(envp, num_envp, &i,
  297. buffer, buffer_size, &length,
  298. "PHYSDEVBUS=%s", dev->bus->name);
  299. if (dev->driver)
  300. add_hotplug_env_var(envp, num_envp, &i,
  301. buffer, buffer_size, &length,
  302. "PHYSDEVDRIVER=%s", dev->driver->name);
  303. }
  304. if (MAJOR(class_dev->devt)) {
  305. add_hotplug_env_var(envp, num_envp, &i,
  306. buffer, buffer_size, &length,
  307. "MAJOR=%u", MAJOR(class_dev->devt));
  308. add_hotplug_env_var(envp, num_envp, &i,
  309. buffer, buffer_size, &length,
  310. "MINOR=%u", MINOR(class_dev->devt));
  311. }
  312. /* terminate, set to next free slot, shrink available space */
  313. envp[i] = NULL;
  314. envp = &envp[i];
  315. num_envp -= i;
  316. buffer = &buffer[length];
  317. buffer_size -= length;
  318. if (class_dev->class->hotplug) {
  319. /* have the bus specific function add its stuff */
  320. retval = class_dev->class->hotplug (class_dev, envp, num_envp,
  321. buffer, buffer_size);
  322. if (retval) {
  323. pr_debug ("%s - hotplug() returned %d\n",
  324. __FUNCTION__, retval);
  325. }
  326. }
  327. return retval;
  328. }
  329. static struct kset_hotplug_ops class_hotplug_ops = {
  330. .filter = class_hotplug_filter,
  331. .name = class_hotplug_name,
  332. .hotplug = class_hotplug,
  333. };
  334. static decl_subsys(class_obj, &ktype_class_device, &class_hotplug_ops);
  335. static int class_device_add_attrs(struct class_device * cd)
  336. {
  337. int i;
  338. int error = 0;
  339. struct class * cls = cd->class;
  340. if (cls->class_dev_attrs) {
  341. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++) {
  342. error = class_device_create_file(cd,
  343. &cls->class_dev_attrs[i]);
  344. if (error)
  345. goto Err;
  346. }
  347. }
  348. Done:
  349. return error;
  350. Err:
  351. while (--i >= 0)
  352. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  353. goto Done;
  354. }
  355. static void class_device_remove_attrs(struct class_device * cd)
  356. {
  357. int i;
  358. struct class * cls = cd->class;
  359. if (cls->class_dev_attrs) {
  360. for (i = 0; attr_name(cls->class_dev_attrs[i]); i++)
  361. class_device_remove_file(cd,&cls->class_dev_attrs[i]);
  362. }
  363. }
  364. static ssize_t show_dev(struct class_device *class_dev, char *buf)
  365. {
  366. return print_dev_t(buf, class_dev->devt);
  367. }
  368. static ssize_t store_uevent(struct class_device *class_dev,
  369. const char *buf, size_t count)
  370. {
  371. kobject_hotplug(&class_dev->kobj, KOBJ_ADD);
  372. return count;
  373. }
  374. void class_device_initialize(struct class_device *class_dev)
  375. {
  376. kobj_set_kset_s(class_dev, class_obj_subsys);
  377. kobject_init(&class_dev->kobj);
  378. INIT_LIST_HEAD(&class_dev->node);
  379. }
  380. static char *make_class_name(struct class_device *class_dev)
  381. {
  382. char *name;
  383. int size;
  384. size = strlen(class_dev->class->name) +
  385. strlen(kobject_name(&class_dev->kobj)) + 2;
  386. name = kmalloc(size, GFP_KERNEL);
  387. if (!name)
  388. return ERR_PTR(-ENOMEM);
  389. strcpy(name, class_dev->class->name);
  390. strcat(name, ":");
  391. strcat(name, kobject_name(&class_dev->kobj));
  392. return name;
  393. }
  394. int class_device_add(struct class_device *class_dev)
  395. {
  396. struct class * parent = NULL;
  397. struct class_interface * class_intf;
  398. char *class_name = NULL;
  399. int error;
  400. class_dev = class_device_get(class_dev);
  401. if (!class_dev)
  402. return -EINVAL;
  403. if (!strlen(class_dev->class_id)) {
  404. error = -EINVAL;
  405. goto register_done;
  406. }
  407. parent = class_get(class_dev->class);
  408. pr_debug("CLASS: registering class device: ID = '%s'\n",
  409. class_dev->class_id);
  410. /* first, register with generic layer. */
  411. kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id);
  412. if (parent)
  413. class_dev->kobj.parent = &parent->subsys.kset.kobj;
  414. if ((error = kobject_add(&class_dev->kobj)))
  415. goto register_done;
  416. /* add the needed attributes to this device */
  417. class_dev->uevent_attr.attr.name = "uevent";
  418. class_dev->uevent_attr.attr.mode = S_IWUSR;
  419. class_dev->uevent_attr.attr.owner = parent->owner;
  420. class_dev->uevent_attr.store = store_uevent;
  421. class_device_create_file(class_dev, &class_dev->uevent_attr);
  422. if (MAJOR(class_dev->devt)) {
  423. struct class_device_attribute *attr;
  424. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  425. if (!attr) {
  426. error = -ENOMEM;
  427. kobject_del(&class_dev->kobj);
  428. goto register_done;
  429. }
  430. attr->attr.name = "dev";
  431. attr->attr.mode = S_IRUGO;
  432. attr->attr.owner = parent->owner;
  433. attr->show = show_dev;
  434. class_device_create_file(class_dev, attr);
  435. class_dev->devt_attr = attr;
  436. }
  437. class_device_add_attrs(class_dev);
  438. if (class_dev->dev) {
  439. class_name = make_class_name(class_dev);
  440. sysfs_create_link(&class_dev->kobj,
  441. &class_dev->dev->kobj, "device");
  442. sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
  443. class_name);
  444. }
  445. kobject_hotplug(&class_dev->kobj, KOBJ_ADD);
  446. /* notify any interfaces this device is now here */
  447. if (parent) {
  448. down(&parent->sem);
  449. list_add_tail(&class_dev->node, &parent->children);
  450. list_for_each_entry(class_intf, &parent->interfaces, node)
  451. if (class_intf->add)
  452. class_intf->add(class_dev, class_intf);
  453. up(&parent->sem);
  454. }
  455. register_done:
  456. if (error && parent)
  457. class_put(parent);
  458. class_device_put(class_dev);
  459. kfree(class_name);
  460. return error;
  461. }
  462. int class_device_register(struct class_device *class_dev)
  463. {
  464. class_device_initialize(class_dev);
  465. return class_device_add(class_dev);
  466. }
  467. /**
  468. * class_device_create - creates a class device and registers it with sysfs
  469. * @cs: pointer to the struct class that this device should be registered to.
  470. * @dev: the dev_t for the char device to be added.
  471. * @device: a pointer to a struct device that is assiociated with this class device.
  472. * @fmt: string for the class device's name
  473. *
  474. * This function can be used by char device classes. A struct
  475. * class_device will be created in sysfs, registered to the specified
  476. * class. A "dev" file will be created, showing the dev_t for the
  477. * device. The pointer to the struct class_device will be returned from
  478. * the call. Any further sysfs files that might be required can be
  479. * created using this pointer.
  480. *
  481. * Note: the struct class passed to this function must have previously
  482. * been created with a call to class_create().
  483. */
  484. struct class_device *class_device_create(struct class *cls, dev_t devt,
  485. struct device *device, char *fmt, ...)
  486. {
  487. va_list args;
  488. struct class_device *class_dev = NULL;
  489. int retval = -ENODEV;
  490. if (cls == NULL || IS_ERR(cls))
  491. goto error;
  492. class_dev = kzalloc(sizeof(*class_dev), GFP_KERNEL);
  493. if (!class_dev) {
  494. retval = -ENOMEM;
  495. goto error;
  496. }
  497. class_dev->devt = devt;
  498. class_dev->dev = device;
  499. class_dev->class = cls;
  500. va_start(args, fmt);
  501. vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args);
  502. va_end(args);
  503. retval = class_device_register(class_dev);
  504. if (retval)
  505. goto error;
  506. return class_dev;
  507. error:
  508. kfree(class_dev);
  509. return ERR_PTR(retval);
  510. }
  511. void class_device_del(struct class_device *class_dev)
  512. {
  513. struct class * parent = class_dev->class;
  514. struct class_interface * class_intf;
  515. char *class_name = NULL;
  516. if (parent) {
  517. down(&parent->sem);
  518. list_del_init(&class_dev->node);
  519. list_for_each_entry(class_intf, &parent->interfaces, node)
  520. if (class_intf->remove)
  521. class_intf->remove(class_dev, class_intf);
  522. up(&parent->sem);
  523. }
  524. if (class_dev->dev) {
  525. class_name = make_class_name(class_dev);
  526. sysfs_remove_link(&class_dev->kobj, "device");
  527. sysfs_remove_link(&class_dev->dev->kobj, class_name);
  528. }
  529. class_device_remove_file(class_dev, &class_dev->uevent_attr);
  530. if (class_dev->devt_attr)
  531. class_device_remove_file(class_dev, class_dev->devt_attr);
  532. class_device_remove_attrs(class_dev);
  533. kobject_hotplug(&class_dev->kobj, KOBJ_REMOVE);
  534. kobject_del(&class_dev->kobj);
  535. if (parent)
  536. class_put(parent);
  537. kfree(class_name);
  538. }
  539. void class_device_unregister(struct class_device *class_dev)
  540. {
  541. pr_debug("CLASS: Unregistering class device. ID = '%s'\n",
  542. class_dev->class_id);
  543. class_device_del(class_dev);
  544. class_device_put(class_dev);
  545. }
  546. /**
  547. * class_device_destroy - removes a class device that was created with class_device_create()
  548. * @cls: the pointer to the struct class that this device was registered * with.
  549. * @dev: the dev_t of the device that was previously registered.
  550. *
  551. * This call unregisters and cleans up a class device that was created with a
  552. * call to class_device_create()
  553. */
  554. void class_device_destroy(struct class *cls, dev_t devt)
  555. {
  556. struct class_device *class_dev = NULL;
  557. struct class_device *class_dev_tmp;
  558. down(&cls->sem);
  559. list_for_each_entry(class_dev_tmp, &cls->children, node) {
  560. if (class_dev_tmp->devt == devt) {
  561. class_dev = class_dev_tmp;
  562. break;
  563. }
  564. }
  565. up(&cls->sem);
  566. if (class_dev)
  567. class_device_unregister(class_dev);
  568. }
  569. int class_device_rename(struct class_device *class_dev, char *new_name)
  570. {
  571. int error = 0;
  572. char *old_class_name = NULL, *new_class_name = NULL;
  573. class_dev = class_device_get(class_dev);
  574. if (!class_dev)
  575. return -EINVAL;
  576. pr_debug("CLASS: renaming '%s' to '%s'\n", class_dev->class_id,
  577. new_name);
  578. if (class_dev->dev)
  579. old_class_name = make_class_name(class_dev);
  580. strlcpy(class_dev->class_id, new_name, KOBJ_NAME_LEN);
  581. error = kobject_rename(&class_dev->kobj, new_name);
  582. if (class_dev->dev) {
  583. new_class_name = make_class_name(class_dev);
  584. sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
  585. new_class_name);
  586. sysfs_remove_link(&class_dev->dev->kobj, old_class_name);
  587. }
  588. class_device_put(class_dev);
  589. kfree(old_class_name);
  590. kfree(new_class_name);
  591. return error;
  592. }
  593. struct class_device * class_device_get(struct class_device *class_dev)
  594. {
  595. if (class_dev)
  596. return to_class_dev(kobject_get(&class_dev->kobj));
  597. return NULL;
  598. }
  599. void class_device_put(struct class_device *class_dev)
  600. {
  601. kobject_put(&class_dev->kobj);
  602. }
  603. int class_interface_register(struct class_interface *class_intf)
  604. {
  605. struct class *parent;
  606. struct class_device *class_dev;
  607. if (!class_intf || !class_intf->class)
  608. return -ENODEV;
  609. parent = class_get(class_intf->class);
  610. if (!parent)
  611. return -EINVAL;
  612. down(&parent->sem);
  613. list_add_tail(&class_intf->node, &parent->interfaces);
  614. if (class_intf->add) {
  615. list_for_each_entry(class_dev, &parent->children, node)
  616. class_intf->add(class_dev, class_intf);
  617. }
  618. up(&parent->sem);
  619. return 0;
  620. }
  621. void class_interface_unregister(struct class_interface *class_intf)
  622. {
  623. struct class * parent = class_intf->class;
  624. struct class_device *class_dev;
  625. if (!parent)
  626. return;
  627. down(&parent->sem);
  628. list_del_init(&class_intf->node);
  629. if (class_intf->remove) {
  630. list_for_each_entry(class_dev, &parent->children, node)
  631. class_intf->remove(class_dev, class_intf);
  632. }
  633. up(&parent->sem);
  634. class_put(parent);
  635. }
  636. int __init classes_init(void)
  637. {
  638. int retval;
  639. retval = subsystem_register(&class_subsys);
  640. if (retval)
  641. return retval;
  642. /* ick, this is ugly, the things we go through to keep from showing up
  643. * in sysfs... */
  644. subsystem_init(&class_obj_subsys);
  645. if (!class_obj_subsys.kset.subsys)
  646. class_obj_subsys.kset.subsys = &class_obj_subsys;
  647. return 0;
  648. }
  649. EXPORT_SYMBOL_GPL(class_create_file);
  650. EXPORT_SYMBOL_GPL(class_remove_file);
  651. EXPORT_SYMBOL_GPL(class_register);
  652. EXPORT_SYMBOL_GPL(class_unregister);
  653. EXPORT_SYMBOL_GPL(class_get);
  654. EXPORT_SYMBOL_GPL(class_put);
  655. EXPORT_SYMBOL_GPL(class_create);
  656. EXPORT_SYMBOL_GPL(class_destroy);
  657. EXPORT_SYMBOL_GPL(class_device_register);
  658. EXPORT_SYMBOL_GPL(class_device_unregister);
  659. EXPORT_SYMBOL_GPL(class_device_initialize);
  660. EXPORT_SYMBOL_GPL(class_device_add);
  661. EXPORT_SYMBOL_GPL(class_device_del);
  662. EXPORT_SYMBOL_GPL(class_device_get);
  663. EXPORT_SYMBOL_GPL(class_device_put);
  664. EXPORT_SYMBOL_GPL(class_device_create);
  665. EXPORT_SYMBOL_GPL(class_device_destroy);
  666. EXPORT_SYMBOL_GPL(class_device_create_file);
  667. EXPORT_SYMBOL_GPL(class_device_remove_file);
  668. EXPORT_SYMBOL_GPL(class_device_create_bin_file);
  669. EXPORT_SYMBOL_GPL(class_device_remove_bin_file);
  670. EXPORT_SYMBOL_GPL(class_interface_register);
  671. EXPORT_SYMBOL_GPL(class_interface_unregister);