sys.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 <linux/mutex.h>
  24. #include <linux/interrupt.h>
  25. #include "base.h"
  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, sysdev_attr, 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, sysdev_attr, 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. static struct kset *system_kset;
  105. int sysdev_class_register(struct sysdev_class *cls)
  106. {
  107. pr_debug("Registering sysdev class '%s'\n", cls->name);
  108. INIT_LIST_HEAD(&cls->drivers);
  109. memset(&cls->kset.kobj, 0x00, sizeof(struct kobject));
  110. cls->kset.kobj.parent = &system_kset->kobj;
  111. cls->kset.kobj.ktype = &ktype_sysdev_class;
  112. cls->kset.kobj.kset = system_kset;
  113. kobject_set_name(&cls->kset.kobj, cls->name);
  114. return kset_register(&cls->kset);
  115. }
  116. void sysdev_class_unregister(struct sysdev_class *cls)
  117. {
  118. pr_debug("Unregistering sysdev class '%s'\n",
  119. kobject_name(&cls->kset.kobj));
  120. kset_unregister(&cls->kset);
  121. }
  122. EXPORT_SYMBOL_GPL(sysdev_class_register);
  123. EXPORT_SYMBOL_GPL(sysdev_class_unregister);
  124. static DEFINE_MUTEX(sysdev_drivers_lock);
  125. /**
  126. * sysdev_driver_register - Register auxillary driver
  127. * @cls: Device class driver belongs to.
  128. * @drv: Driver.
  129. *
  130. * @drv is inserted into @cls->drivers to be
  131. * called on each operation on devices of that class. The refcount
  132. * of @cls is incremented.
  133. */
  134. int sysdev_driver_register(struct sysdev_class *cls, struct sysdev_driver *drv)
  135. {
  136. int err = 0;
  137. if (!cls) {
  138. WARN(1, KERN_WARNING "sysdev: invalid class passed to "
  139. "sysdev_driver_register!\n");
  140. return -EINVAL;
  141. }
  142. /* Check whether this driver has already been added to a class. */
  143. if (drv->entry.next && !list_empty(&drv->entry))
  144. WARN(1, KERN_WARNING "sysdev: class %s: driver (%p) has already"
  145. " been registered to a class, something is wrong, but "
  146. "will forge on!\n", cls->name, drv);
  147. mutex_lock(&sysdev_drivers_lock);
  148. if (cls && kset_get(&cls->kset)) {
  149. list_add_tail(&drv->entry, &cls->drivers);
  150. /* If devices of this class already exist, tell the driver */
  151. if (drv->add) {
  152. struct sys_device *dev;
  153. list_for_each_entry(dev, &cls->kset.list, kobj.entry)
  154. drv->add(dev);
  155. }
  156. } else {
  157. err = -EINVAL;
  158. WARN(1, KERN_ERR "%s: invalid device class\n", __func__);
  159. }
  160. mutex_unlock(&sysdev_drivers_lock);
  161. return err;
  162. }
  163. /**
  164. * sysdev_driver_unregister - Remove an auxillary driver.
  165. * @cls: Class driver belongs to.
  166. * @drv: Driver.
  167. */
  168. void sysdev_driver_unregister(struct sysdev_class *cls,
  169. struct sysdev_driver *drv)
  170. {
  171. mutex_lock(&sysdev_drivers_lock);
  172. list_del_init(&drv->entry);
  173. if (cls) {
  174. if (drv->remove) {
  175. struct sys_device *dev;
  176. list_for_each_entry(dev, &cls->kset.list, kobj.entry)
  177. drv->remove(dev);
  178. }
  179. kset_put(&cls->kset);
  180. }
  181. mutex_unlock(&sysdev_drivers_lock);
  182. }
  183. EXPORT_SYMBOL_GPL(sysdev_driver_register);
  184. EXPORT_SYMBOL_GPL(sysdev_driver_unregister);
  185. /**
  186. * sysdev_register - add a system device to the tree
  187. * @sysdev: device in question
  188. *
  189. */
  190. int sysdev_register(struct sys_device *sysdev)
  191. {
  192. int error;
  193. struct sysdev_class *cls = sysdev->cls;
  194. if (!cls)
  195. return -EINVAL;
  196. pr_debug("Registering sys device of class '%s'\n",
  197. kobject_name(&cls->kset.kobj));
  198. /* initialize the kobject to 0, in case it had previously been used */
  199. memset(&sysdev->kobj, 0x00, sizeof(struct kobject));
  200. /* Make sure the kset is set */
  201. sysdev->kobj.kset = &cls->kset;
  202. /* Register the object */
  203. error = kobject_init_and_add(&sysdev->kobj, &ktype_sysdev, NULL,
  204. "%s%d", kobject_name(&cls->kset.kobj),
  205. sysdev->id);
  206. if (!error) {
  207. struct sysdev_driver *drv;
  208. pr_debug("Registering sys device '%s'\n",
  209. kobject_name(&sysdev->kobj));
  210. mutex_lock(&sysdev_drivers_lock);
  211. /* Generic notification is implicit, because it's that
  212. * code that should have called us.
  213. */
  214. /* Notify class auxillary drivers */
  215. list_for_each_entry(drv, &cls->drivers, entry) {
  216. if (drv->add)
  217. drv->add(sysdev);
  218. }
  219. mutex_unlock(&sysdev_drivers_lock);
  220. }
  221. kobject_uevent(&sysdev->kobj, KOBJ_ADD);
  222. return error;
  223. }
  224. void sysdev_unregister(struct sys_device *sysdev)
  225. {
  226. struct sysdev_driver *drv;
  227. mutex_lock(&sysdev_drivers_lock);
  228. list_for_each_entry(drv, &sysdev->cls->drivers, entry) {
  229. if (drv->remove)
  230. drv->remove(sysdev);
  231. }
  232. mutex_unlock(&sysdev_drivers_lock);
  233. kobject_put(&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 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 their 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. mutex_lock(&sysdev_drivers_lock);
  253. list_for_each_entry_reverse(cls, &system_kset->list, 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 auxillary drivers first */
  261. list_for_each_entry(drv, &cls->drivers, entry) {
  262. if (drv->shutdown)
  263. drv->shutdown(sysdev);
  264. }
  265. /* Now call the generic one */
  266. if (cls->shutdown)
  267. cls->shutdown(sysdev);
  268. }
  269. }
  270. mutex_unlock(&sysdev_drivers_lock);
  271. }
  272. static void __sysdev_resume(struct sys_device *dev)
  273. {
  274. struct sysdev_class *cls = dev->cls;
  275. struct sysdev_driver *drv;
  276. /* First, call the class-specific one */
  277. if (cls->resume)
  278. cls->resume(dev);
  279. WARN_ONCE(!irqs_disabled(),
  280. "Interrupts enabled after %pF\n", cls->resume);
  281. /* Call auxillary drivers next. */
  282. list_for_each_entry(drv, &cls->drivers, entry) {
  283. if (drv->resume)
  284. drv->resume(dev);
  285. WARN_ONCE(!irqs_disabled(),
  286. "Interrupts enabled after %pF\n", drv->resume);
  287. }
  288. }
  289. /**
  290. * sysdev_suspend - Suspend all system devices.
  291. * @state: Power state to enter.
  292. *
  293. * We perform an almost identical operation as sysdev_shutdown()
  294. * above, though calling ->suspend() instead. Interrupts are disabled
  295. * when this called. Devices are responsible for both saving state and
  296. * quiescing or powering down the device.
  297. *
  298. * This is only called by the device PM core, so we let them handle
  299. * all synchronization.
  300. */
  301. int sysdev_suspend(pm_message_t state)
  302. {
  303. struct sysdev_class *cls;
  304. struct sys_device *sysdev, *err_dev;
  305. struct sysdev_driver *drv, *err_drv;
  306. int ret;
  307. pr_debug("Checking wake-up interrupts\n");
  308. /* Return error code if there are any wake-up interrupts pending */
  309. ret = check_wakeup_irqs();
  310. if (ret)
  311. return ret;
  312. WARN_ONCE(!irqs_disabled(),
  313. "Interrupts enabled while suspending system devices\n");
  314. pr_debug("Suspending System Devices\n");
  315. list_for_each_entry_reverse(cls, &system_kset->list, 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 auxillary drivers first */
  321. list_for_each_entry(drv, &cls->drivers, entry) {
  322. if (drv->suspend) {
  323. ret = drv->suspend(sysdev, state);
  324. if (ret)
  325. goto aux_driver;
  326. }
  327. WARN_ONCE(!irqs_disabled(),
  328. "Interrupts enabled after %pF\n",
  329. drv->suspend);
  330. }
  331. /* Now call the generic one */
  332. if (cls->suspend) {
  333. ret = cls->suspend(sysdev, state);
  334. if (ret)
  335. goto cls_driver;
  336. WARN_ONCE(!irqs_disabled(),
  337. "Interrupts enabled after %pF\n",
  338. cls->suspend);
  339. }
  340. }
  341. }
  342. return 0;
  343. /* resume current sysdev */
  344. cls_driver:
  345. drv = NULL;
  346. printk(KERN_ERR "Class suspend failed for %s\n",
  347. kobject_name(&sysdev->kobj));
  348. aux_driver:
  349. if (drv)
  350. printk(KERN_ERR "Class driver suspend failed for %s\n",
  351. kobject_name(&sysdev->kobj));
  352. list_for_each_entry(err_drv, &cls->drivers, entry) {
  353. if (err_drv == drv)
  354. break;
  355. if (err_drv->resume)
  356. err_drv->resume(sysdev);
  357. }
  358. /* resume other sysdevs in current class */
  359. list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
  360. if (err_dev == sysdev)
  361. break;
  362. pr_debug(" %s\n", kobject_name(&err_dev->kobj));
  363. __sysdev_resume(err_dev);
  364. }
  365. /* resume other classes */
  366. list_for_each_entry_continue(cls, &system_kset->list, kset.kobj.entry) {
  367. list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
  368. pr_debug(" %s\n", kobject_name(&err_dev->kobj));
  369. __sysdev_resume(err_dev);
  370. }
  371. }
  372. return ret;
  373. }
  374. EXPORT_SYMBOL_GPL(sysdev_suspend);
  375. /**
  376. * sysdev_resume - Bring system devices back to life.
  377. *
  378. * Similar to sysdev_suspend(), but we iterate the list forwards
  379. * to guarantee that parent devices are resumed before their children.
  380. *
  381. * Note: Interrupts are disabled when called.
  382. */
  383. int sysdev_resume(void)
  384. {
  385. struct sysdev_class *cls;
  386. WARN_ONCE(!irqs_disabled(),
  387. "Interrupts enabled while resuming system devices\n");
  388. pr_debug("Resuming System Devices\n");
  389. list_for_each_entry(cls, &system_kset->list, kset.kobj.entry) {
  390. struct sys_device *sysdev;
  391. pr_debug("Resuming type '%s':\n",
  392. kobject_name(&cls->kset.kobj));
  393. list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
  394. pr_debug(" %s\n", kobject_name(&sysdev->kobj));
  395. __sysdev_resume(sysdev);
  396. }
  397. }
  398. return 0;
  399. }
  400. EXPORT_SYMBOL_GPL(sysdev_resume);
  401. int __init system_bus_init(void)
  402. {
  403. system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
  404. if (!system_kset)
  405. return -ENOMEM;
  406. return 0;
  407. }
  408. EXPORT_SYMBOL_GPL(sysdev_register);
  409. EXPORT_SYMBOL_GPL(sysdev_unregister);
  410. #define to_ext_attr(x) container_of(x, struct sysdev_ext_attribute, attr)
  411. ssize_t sysdev_store_ulong(struct sys_device *sysdev,
  412. struct sysdev_attribute *attr,
  413. const char *buf, size_t size)
  414. {
  415. struct sysdev_ext_attribute *ea = to_ext_attr(attr);
  416. char *end;
  417. unsigned long new = simple_strtoul(buf, &end, 0);
  418. if (end == buf)
  419. return -EINVAL;
  420. *(unsigned long *)(ea->var) = new;
  421. /* Always return full write size even if we didn't consume all */
  422. return size;
  423. }
  424. EXPORT_SYMBOL_GPL(sysdev_store_ulong);
  425. ssize_t sysdev_show_ulong(struct sys_device *sysdev,
  426. struct sysdev_attribute *attr,
  427. char *buf)
  428. {
  429. struct sysdev_ext_attribute *ea = to_ext_attr(attr);
  430. return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
  431. }
  432. EXPORT_SYMBOL_GPL(sysdev_show_ulong);
  433. ssize_t sysdev_store_int(struct sys_device *sysdev,
  434. struct sysdev_attribute *attr,
  435. const char *buf, size_t size)
  436. {
  437. struct sysdev_ext_attribute *ea = to_ext_attr(attr);
  438. char *end;
  439. long new = simple_strtol(buf, &end, 0);
  440. if (end == buf || new > INT_MAX || new < INT_MIN)
  441. return -EINVAL;
  442. *(int *)(ea->var) = new;
  443. /* Always return full write size even if we didn't consume all */
  444. return size;
  445. }
  446. EXPORT_SYMBOL_GPL(sysdev_store_int);
  447. ssize_t sysdev_show_int(struct sys_device *sysdev,
  448. struct sysdev_attribute *attr,
  449. char *buf)
  450. {
  451. struct sysdev_ext_attribute *ea = to_ext_attr(attr);
  452. return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
  453. }
  454. EXPORT_SYMBOL_GPL(sysdev_show_int);