sys.c 11 KB

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