sys.c 12 KB

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