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