dim2_sysfs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * dim2_sysfs.c - MediaLB sysfs information
  3. *
  4. * Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * This file is licensed under GPLv2.
  12. */
  13. /* Author: Andrey Shvetsov <andrey.shvetsov@k2l.de> */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/kernel.h>
  16. #include "dim2_sysfs.h"
  17. struct bus_attr {
  18. struct attribute attr;
  19. ssize_t (*show)(struct medialb_bus *bus, char *buf);
  20. ssize_t (*store)(struct medialb_bus *bus, const char *buf,
  21. size_t count);
  22. };
  23. static ssize_t state_show(struct medialb_bus *bus, char *buf)
  24. {
  25. bool state = dim2_sysfs_get_state_cb();
  26. return sprintf(buf, "%s\n", state ? "locked" : "");
  27. }
  28. static struct bus_attr state_attr = __ATTR_RO(state);
  29. static struct attribute *bus_default_attrs[] = {
  30. &state_attr.attr,
  31. NULL,
  32. };
  33. static struct attribute_group bus_attr_group = {
  34. .attrs = bus_default_attrs,
  35. };
  36. static void bus_kobj_release(struct kobject *kobj)
  37. {
  38. }
  39. static ssize_t bus_kobj_attr_show(struct kobject *kobj, struct attribute *attr,
  40. char *buf)
  41. {
  42. struct medialb_bus *bus =
  43. container_of(kobj, struct medialb_bus, kobj_group);
  44. struct bus_attr *xattr = container_of(attr, struct bus_attr, attr);
  45. if (!xattr->show)
  46. return -EIO;
  47. return xattr->show(bus, buf);
  48. }
  49. static ssize_t bus_kobj_attr_store(struct kobject *kobj, struct attribute *attr,
  50. const char *buf, size_t count)
  51. {
  52. ssize_t ret;
  53. struct medialb_bus *bus =
  54. container_of(kobj, struct medialb_bus, kobj_group);
  55. struct bus_attr *xattr = container_of(attr, struct bus_attr, attr);
  56. if (!xattr->store)
  57. return -EIO;
  58. ret = xattr->store(bus, buf, count);
  59. return ret;
  60. }
  61. static struct sysfs_ops const bus_kobj_sysfs_ops = {
  62. .show = bus_kobj_attr_show,
  63. .store = bus_kobj_attr_store,
  64. };
  65. static struct kobj_type bus_ktype = {
  66. .release = bus_kobj_release,
  67. .sysfs_ops = &bus_kobj_sysfs_ops,
  68. };
  69. int dim2_sysfs_probe(struct medialb_bus *bus, struct kobject *parent_kobj)
  70. {
  71. int err;
  72. kobject_init(&bus->kobj_group, &bus_ktype);
  73. err = kobject_add(&bus->kobj_group, parent_kobj, "bus");
  74. if (err) {
  75. pr_err("kobject_add() failed: %d\n", err);
  76. goto err_kobject_add;
  77. }
  78. err = sysfs_create_group(&bus->kobj_group, &bus_attr_group);
  79. if (err) {
  80. pr_err("sysfs_create_group() failed: %d\n", err);
  81. goto err_create_group;
  82. }
  83. return 0;
  84. err_create_group:
  85. kobject_put(&bus->kobj_group);
  86. err_kobject_add:
  87. return err;
  88. }
  89. void dim2_sysfs_destroy(struct medialb_bus *bus)
  90. {
  91. kobject_put(&bus->kobj_group);
  92. }