sys.c 15 KB

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