sys.c 12 KB

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