sys.c 14 KB

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