sys.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * sys.c - pseudo-bus for system 'devices' (cpus, PICs, timers, etc)
  3. *
  4. * Copyright (c) 2002-3 Patrick Mochel
  5. * 2002-3 Open Source Development Lab
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. * This exports a 'system' bus type.
  10. * By default, a 'sys' bus gets added to the root of the system. There will
  11. * always be core system devices. Devices can use sysdev_register() to
  12. * add themselves as children of the system bus.
  13. */
  14. #include <linux/sysdev.h>
  15. #include <linux/err.h>
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/string.h>
  21. #include <linux/pm.h>
  22. #include <linux/device.h>
  23. #include <linux/mutex.h>
  24. #include "base.h"
  25. #define to_sysdev(k) container_of(k, struct sys_device, kobj)
  26. #define to_sysdev_attr(a) container_of(a, struct sysdev_attribute, attr)
  27. static ssize_t
  28. sysdev_show(struct kobject * kobj, struct attribute * attr, char * buffer)
  29. {
  30. struct sys_device * sysdev = to_sysdev(kobj);
  31. struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
  32. if (sysdev_attr->show)
  33. return sysdev_attr->show(sysdev, buffer);
  34. return -EIO;
  35. }
  36. static ssize_t
  37. sysdev_store(struct kobject * kobj, struct attribute * attr,
  38. const char * buffer, size_t count)
  39. {
  40. struct sys_device * sysdev = to_sysdev(kobj);
  41. struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
  42. if (sysdev_attr->store)
  43. return sysdev_attr->store(sysdev, buffer, count);
  44. return -EIO;
  45. }
  46. static struct sysfs_ops sysfs_ops = {
  47. .show = sysdev_show,
  48. .store = sysdev_store,
  49. };
  50. static struct kobj_type ktype_sysdev = {
  51. .sysfs_ops = &sysfs_ops,
  52. };
  53. int sysdev_create_file(struct sys_device * s, struct sysdev_attribute * a)
  54. {
  55. return sysfs_create_file(&s->kobj, &a->attr);
  56. }
  57. void sysdev_remove_file(struct sys_device * s, struct sysdev_attribute * a)
  58. {
  59. sysfs_remove_file(&s->kobj, &a->attr);
  60. }
  61. EXPORT_SYMBOL_GPL(sysdev_create_file);
  62. EXPORT_SYMBOL_GPL(sysdev_remove_file);
  63. #define to_sysdev_class(k) container_of(k, struct sysdev_class, kset.kobj)
  64. #define to_sysdev_class_attr(a) container_of(a, \
  65. struct sysdev_class_attribute, attr)
  66. static ssize_t sysdev_class_show(struct kobject *kobj, struct attribute *attr,
  67. char *buffer)
  68. {
  69. struct sysdev_class * class = to_sysdev_class(kobj);
  70. struct sysdev_class_attribute *class_attr = to_sysdev_class_attr(attr);
  71. if (class_attr->show)
  72. return class_attr->show(class, buffer);
  73. return -EIO;
  74. }
  75. static ssize_t sysdev_class_store(struct kobject *kobj, struct attribute *attr,
  76. const char *buffer, size_t count)
  77. {
  78. struct sysdev_class * class = to_sysdev_class(kobj);
  79. struct sysdev_class_attribute * class_attr = to_sysdev_class_attr(attr);
  80. if (class_attr->store)
  81. return class_attr->store(class, buffer, count);
  82. return -EIO;
  83. }
  84. static struct sysfs_ops sysfs_class_ops = {
  85. .show = sysdev_class_show,
  86. .store = sysdev_class_store,
  87. };
  88. static struct kobj_type ktype_sysdev_class = {
  89. .sysfs_ops = &sysfs_class_ops,
  90. };
  91. int sysdev_class_create_file(struct sysdev_class *c,
  92. struct sysdev_class_attribute *a)
  93. {
  94. return sysfs_create_file(&c->kset.kobj, &a->attr);
  95. }
  96. EXPORT_SYMBOL_GPL(sysdev_class_create_file);
  97. void sysdev_class_remove_file(struct sysdev_class *c,
  98. struct sysdev_class_attribute *a)
  99. {
  100. sysfs_remove_file(&c->kset.kobj, &a->attr);
  101. }
  102. EXPORT_SYMBOL_GPL(sysdev_class_remove_file);
  103. static struct kset *system_kset;
  104. int sysdev_class_register(struct sysdev_class * cls)
  105. {
  106. pr_debug("Registering sysdev class '%s'\n",
  107. kobject_name(&cls->kset.kobj));
  108. INIT_LIST_HEAD(&cls->drivers);
  109. memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
  110. cls->kset.kobj.parent = &system_kset->kobj;
  111. cls->kset.kobj.ktype = &ktype_sysdev_class;
  112. cls->kset.kobj.kset = system_kset;
  113. kobject_set_name(&cls->kset.kobj, cls->name);
  114. return kset_register(&cls->kset);
  115. }
  116. void sysdev_class_unregister(struct sysdev_class * cls)
  117. {
  118. pr_debug("Unregistering sysdev class '%s'\n",
  119. kobject_name(&cls->kset.kobj));
  120. kset_unregister(&cls->kset);
  121. }
  122. EXPORT_SYMBOL_GPL(sysdev_class_register);
  123. EXPORT_SYMBOL_GPL(sysdev_class_unregister);
  124. static DEFINE_MUTEX(sysdev_drivers_lock);
  125. /**
  126. * sysdev_driver_register - Register auxillary driver
  127. * @cls: Device class driver belongs to.
  128. * @drv: Driver.
  129. *
  130. * @drv is inserted into @cls->drivers to be
  131. * called on each operation on devices of that class. The refcount
  132. * of @cls is incremented.
  133. */
  134. int sysdev_driver_register(struct sysdev_class *cls, struct sysdev_driver *drv)
  135. {
  136. int err = 0;
  137. mutex_lock(&sysdev_drivers_lock);
  138. if (cls && kset_get(&cls->kset)) {
  139. list_add_tail(&drv->entry, &cls->drivers);
  140. /* If devices of this class already exist, tell the driver */
  141. if (drv->add) {
  142. struct sys_device *dev;
  143. list_for_each_entry(dev, &cls->kset.list, kobj.entry)
  144. drv->add(dev);
  145. }
  146. } else {
  147. err = -EINVAL;
  148. printk(KERN_ERR "%s: invalid device class\n", __FUNCTION__);
  149. WARN_ON(1);
  150. }
  151. mutex_unlock(&sysdev_drivers_lock);
  152. return err;
  153. }
  154. /**
  155. * sysdev_driver_unregister - Remove an auxillary driver.
  156. * @cls: Class driver belongs to.
  157. * @drv: Driver.
  158. */
  159. void sysdev_driver_unregister(struct sysdev_class * cls,
  160. struct sysdev_driver * drv)
  161. {
  162. mutex_lock(&sysdev_drivers_lock);
  163. list_del_init(&drv->entry);
  164. if (cls) {
  165. if (drv->remove) {
  166. struct sys_device *dev;
  167. list_for_each_entry(dev, &cls->kset.list, kobj.entry)
  168. drv->remove(dev);
  169. }
  170. kset_put(&cls->kset);
  171. }
  172. mutex_unlock(&sysdev_drivers_lock);
  173. }
  174. EXPORT_SYMBOL_GPL(sysdev_driver_register);
  175. EXPORT_SYMBOL_GPL(sysdev_driver_unregister);
  176. /**
  177. * sysdev_register - add a system device to the tree
  178. * @sysdev: device in question
  179. *
  180. */
  181. int sysdev_register(struct sys_device * sysdev)
  182. {
  183. int error;
  184. struct sysdev_class * cls = sysdev->cls;
  185. if (!cls)
  186. return -EINVAL;
  187. pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
  188. /* initialize the kobject to 0, in case it had previously been used */
  189. memset(&sysdev->kobj, 0x00, sizeof(struct kobject));
  190. /* Make sure the kset is set */
  191. sysdev->kobj.kset = &cls->kset;
  192. /* Register the object */
  193. error = kobject_init_and_add(&sysdev->kobj, &ktype_sysdev, NULL,
  194. "%s%d", kobject_name(&cls->kset.kobj),
  195. sysdev->id);
  196. if (!error) {
  197. struct sysdev_driver * drv;
  198. mutex_lock(&sysdev_drivers_lock);
  199. /* Generic notification is implicit, because it's that
  200. * code that should have called us.
  201. */
  202. /* Notify class auxillary drivers */
  203. list_for_each_entry(drv, &cls->drivers, entry) {
  204. if (drv->add)
  205. drv->add(sysdev);
  206. }
  207. mutex_unlock(&sysdev_drivers_lock);
  208. }
  209. kobject_uevent(&sysdev->kobj, KOBJ_ADD);
  210. return error;
  211. }
  212. void sysdev_unregister(struct sys_device * sysdev)
  213. {
  214. struct sysdev_driver * drv;
  215. mutex_lock(&sysdev_drivers_lock);
  216. list_for_each_entry(drv, &sysdev->cls->drivers, entry) {
  217. if (drv->remove)
  218. drv->remove(sysdev);
  219. }
  220. mutex_unlock(&sysdev_drivers_lock);
  221. kobject_put(&sysdev->kobj);
  222. }
  223. /**
  224. * sysdev_shutdown - Shut down all system devices.
  225. *
  226. * Loop over each class of system devices, and the devices in each
  227. * of those classes. For each device, we call the shutdown method for
  228. * each driver registered for the device - the auxillaries,
  229. * and the class driver.
  230. *
  231. * Note: The list is iterated in reverse order, so that we shut down
  232. * child devices before we shut down thier parents. The list ordering
  233. * is guaranteed by virtue of the fact that child devices are registered
  234. * after their parents.
  235. */
  236. void sysdev_shutdown(void)
  237. {
  238. struct sysdev_class * cls;
  239. pr_debug("Shutting Down System Devices\n");
  240. mutex_lock(&sysdev_drivers_lock);
  241. list_for_each_entry_reverse(cls, &system_kset->list, kset.kobj.entry) {
  242. struct sys_device * sysdev;
  243. pr_debug("Shutting down type '%s':\n",
  244. kobject_name(&cls->kset.kobj));
  245. list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
  246. struct sysdev_driver * drv;
  247. pr_debug(" %s\n", kobject_name(&sysdev->kobj));
  248. /* Call auxillary drivers first */
  249. list_for_each_entry(drv, &cls->drivers, entry) {
  250. if (drv->shutdown)
  251. drv->shutdown(sysdev);
  252. }
  253. /* Now call the generic one */
  254. if (cls->shutdown)
  255. cls->shutdown(sysdev);
  256. }
  257. }
  258. mutex_unlock(&sysdev_drivers_lock);
  259. }
  260. static void __sysdev_resume(struct sys_device *dev)
  261. {
  262. struct sysdev_class *cls = dev->cls;
  263. struct sysdev_driver *drv;
  264. /* First, call the class-specific one */
  265. if (cls->resume)
  266. cls->resume(dev);
  267. /* Call auxillary drivers next. */
  268. list_for_each_entry(drv, &cls->drivers, entry) {
  269. if (drv->resume)
  270. drv->resume(dev);
  271. }
  272. }
  273. /**
  274. * sysdev_suspend - Suspend all system devices.
  275. * @state: Power state to enter.
  276. *
  277. * We perform an almost identical operation as sys_device_shutdown()
  278. * above, though calling ->suspend() instead. Interrupts are disabled
  279. * when this called. Devices are responsible for both saving state and
  280. * quiescing or powering down the device.
  281. *
  282. * This is only called by the device PM core, so we let them handle
  283. * all synchronization.
  284. */
  285. int sysdev_suspend(pm_message_t state)
  286. {
  287. struct sysdev_class * cls;
  288. struct sys_device *sysdev, *err_dev;
  289. struct sysdev_driver *drv, *err_drv;
  290. int ret;
  291. pr_debug("Suspending System Devices\n");
  292. list_for_each_entry_reverse(cls, &system_kset->list, kset.kobj.entry) {
  293. pr_debug("Suspending type '%s':\n",
  294. kobject_name(&cls->kset.kobj));
  295. list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
  296. pr_debug(" %s\n", kobject_name(&sysdev->kobj));
  297. /* Call auxillary drivers first */
  298. list_for_each_entry(drv, &cls->drivers, entry) {
  299. if (drv->suspend) {
  300. ret = drv->suspend(sysdev, state);
  301. if (ret)
  302. goto aux_driver;
  303. }
  304. }
  305. /* Now call the generic one */
  306. if (cls->suspend) {
  307. ret = cls->suspend(sysdev, state);
  308. if (ret)
  309. goto cls_driver;
  310. }
  311. }
  312. }
  313. return 0;
  314. /* resume current sysdev */
  315. cls_driver:
  316. drv = NULL;
  317. printk(KERN_ERR "Class suspend failed for %s\n",
  318. kobject_name(&sysdev->kobj));
  319. aux_driver:
  320. if (drv)
  321. printk(KERN_ERR "Class driver suspend failed for %s\n",
  322. kobject_name(&sysdev->kobj));
  323. list_for_each_entry(err_drv, &cls->drivers, entry) {
  324. if (err_drv == drv)
  325. break;
  326. if (err_drv->resume)
  327. err_drv->resume(sysdev);
  328. }
  329. /* resume other sysdevs in current class */
  330. list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
  331. if (err_dev == sysdev)
  332. break;
  333. pr_debug(" %s\n", kobject_name(&err_dev->kobj));
  334. __sysdev_resume(err_dev);
  335. }
  336. /* resume other classes */
  337. list_for_each_entry_continue(cls, &system_kset->list, kset.kobj.entry) {
  338. list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
  339. pr_debug(" %s\n", kobject_name(&err_dev->kobj));
  340. __sysdev_resume(err_dev);
  341. }
  342. }
  343. return ret;
  344. }
  345. /**
  346. * sysdev_resume - Bring system devices back to life.
  347. *
  348. * Similar to sys_device_suspend(), but we iterate the list forwards
  349. * to guarantee that parent devices are resumed before their children.
  350. *
  351. * Note: Interrupts are disabled when called.
  352. */
  353. int sysdev_resume(void)
  354. {
  355. struct sysdev_class * cls;
  356. pr_debug("Resuming System Devices\n");
  357. list_for_each_entry(cls, &system_kset->list, kset.kobj.entry) {
  358. struct sys_device * sysdev;
  359. pr_debug("Resuming type '%s':\n",
  360. kobject_name(&cls->kset.kobj));
  361. list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
  362. pr_debug(" %s\n", kobject_name(&sysdev->kobj));
  363. __sysdev_resume(sysdev);
  364. }
  365. }
  366. return 0;
  367. }
  368. int __init system_bus_init(void)
  369. {
  370. system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
  371. if (!system_kset)
  372. return -ENOMEM;
  373. return 0;
  374. }
  375. EXPORT_SYMBOL_GPL(sysdev_register);
  376. EXPORT_SYMBOL_GPL(sysdev_unregister);