sys.c 9.4 KB

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