sys.c 11 KB

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