vfio/mdev: consolidate all the description sysfs into the core code
[linux-2.6-microblaze.git] / drivers / vfio / mdev / mdev_sysfs.c
index b7f87c3..658b3bf 100644 (file)
 
 #include "mdev_private.h"
 
-/* Static functions */
+struct mdev_type_attribute {
+       struct attribute attr;
+       ssize_t (*show)(struct mdev_type *mtype,
+                       struct mdev_type_attribute *attr, char *buf);
+       ssize_t (*store)(struct mdev_type *mtype,
+                        struct mdev_type_attribute *attr, const char *buf,
+                        size_t count);
+};
+
+#define MDEV_TYPE_ATTR_RO(_name) \
+       struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
+#define MDEV_TYPE_ATTR_WO(_name) \
+       struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
 
 static ssize_t mdev_type_attr_show(struct kobject *kobj,
                                     struct attribute *__attr, char *buf)
@@ -100,16 +112,35 @@ static ssize_t available_instances_show(struct mdev_type *mtype,
 }
 static MDEV_TYPE_ATTR_RO(available_instances);
 
+static ssize_t description_show(struct mdev_type *mtype,
+                               struct mdev_type_attribute *attr,
+                               char *buf)
+{
+       return mtype->parent->mdev_driver->show_description(mtype, buf);
+}
+static MDEV_TYPE_ATTR_RO(description);
+
 static struct attribute *mdev_types_core_attrs[] = {
        &mdev_type_attr_create.attr,
        &mdev_type_attr_device_api.attr,
        &mdev_type_attr_name.attr,
        &mdev_type_attr_available_instances.attr,
+       &mdev_type_attr_description.attr,
        NULL,
 };
 
+static umode_t mdev_types_core_is_visible(struct kobject *kobj,
+                                         struct attribute *attr, int n)
+{
+       if (attr == &mdev_type_attr_description.attr &&
+           !to_mdev_type(kobj)->parent->mdev_driver->show_description)
+               return 0;
+       return attr->mode;
+}
+
 static struct attribute_group mdev_type_core_group = {
        .attrs = mdev_types_core_attrs,
+       .is_visible = mdev_types_core_is_visible,
 };
 
 static const struct attribute_group *mdev_type_groups[] = {
@@ -155,13 +186,8 @@ static int mdev_type_add(struct mdev_parent *parent, struct mdev_type *type)
                goto attr_devices_failed;
        }
 
-       ret = sysfs_create_files(&type->kobj, parent->mdev_driver->types_attrs);
-       if (ret)
-               goto attrs_failed;
        return 0;
 
-attrs_failed:
-       kobject_put(type->devices_kobj);
 attr_devices_failed:
        kobject_del(&type->kobj);
        kobject_put(&type->kobj);
@@ -170,8 +196,6 @@ attr_devices_failed:
 
 static void mdev_type_remove(struct mdev_type *type)
 {
-       sysfs_remove_files(&type->kobj, type->parent->mdev_driver->types_attrs);
-
        kobject_put(type->devices_kobj);
        kobject_del(&type->kobj);
        kobject_put(&type->kobj);