Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * File attributes for Mediated devices
  4 *
  5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  6 *     Author: Neo Jia <cjia@nvidia.com>
  7 *             Kirti Wankhede <kwankhede@nvidia.com>
  8 */
  9
 10#include <linux/sysfs.h>
 11#include <linux/ctype.h>
 
 12#include <linux/slab.h>
 
 13#include <linux/mdev.h>
 14
 15#include "mdev_private.h"
 16
 17struct mdev_type_attribute {
 18	struct attribute attr;
 19	ssize_t (*show)(struct mdev_type *mtype,
 20			struct mdev_type_attribute *attr, char *buf);
 21	ssize_t (*store)(struct mdev_type *mtype,
 22			 struct mdev_type_attribute *attr, const char *buf,
 23			 size_t count);
 24};
 25
 26#define MDEV_TYPE_ATTR_RO(_name) \
 27	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
 28#define MDEV_TYPE_ATTR_WO(_name) \
 29	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
 30
 31static ssize_t mdev_type_attr_show(struct kobject *kobj,
 32				     struct attribute *__attr, char *buf)
 33{
 34	struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
 35	struct mdev_type *type = to_mdev_type(kobj);
 36	ssize_t ret = -EIO;
 37
 38	if (attr->show)
 39		ret = attr->show(type, attr, buf);
 40	return ret;
 41}
 42
 43static ssize_t mdev_type_attr_store(struct kobject *kobj,
 44				      struct attribute *__attr,
 45				      const char *buf, size_t count)
 46{
 47	struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
 48	struct mdev_type *type = to_mdev_type(kobj);
 49	ssize_t ret = -EIO;
 50
 51	if (attr->store)
 52		ret = attr->store(type, attr, buf, count);
 53	return ret;
 54}
 55
 56static const struct sysfs_ops mdev_type_sysfs_ops = {
 57	.show = mdev_type_attr_show,
 58	.store = mdev_type_attr_store,
 59};
 60
 61static ssize_t create_store(struct mdev_type *mtype,
 62			    struct mdev_type_attribute *attr, const char *buf,
 63			    size_t count)
 64{
 65	char *str;
 66	guid_t uuid;
 67	int ret;
 68
 69	if ((count < UUID_STRING_LEN) || (count > UUID_STRING_LEN + 1))
 70		return -EINVAL;
 71
 72	str = kstrndup(buf, count, GFP_KERNEL);
 73	if (!str)
 74		return -ENOMEM;
 75
 76	ret = guid_parse(str, &uuid);
 77	kfree(str);
 78	if (ret)
 79		return ret;
 80
 81	ret = mdev_device_create(mtype, &uuid);
 82	if (ret)
 83		return ret;
 84
 85	return count;
 86}
 87static MDEV_TYPE_ATTR_WO(create);
 88
 89static ssize_t device_api_show(struct mdev_type *mtype,
 90			       struct mdev_type_attribute *attr, char *buf)
 91{
 92	return sysfs_emit(buf, "%s\n", mtype->parent->mdev_driver->device_api);
 93}
 94static MDEV_TYPE_ATTR_RO(device_api);
 95
 96static ssize_t name_show(struct mdev_type *mtype,
 97			 struct mdev_type_attribute *attr, char *buf)
 98{
 99	return sysfs_emit(buf, "%s\n",
100		mtype->pretty_name ? mtype->pretty_name : mtype->sysfs_name);
101}
102
103static MDEV_TYPE_ATTR_RO(name);
104
105static ssize_t available_instances_show(struct mdev_type *mtype,
106					struct mdev_type_attribute *attr,
107					char *buf)
108{
109	struct mdev_driver *drv = mtype->parent->mdev_driver;
110
111	if (drv->get_available)
112		return sysfs_emit(buf, "%u\n", drv->get_available(mtype));
113	return sysfs_emit(buf, "%u\n",
114			  atomic_read(&mtype->parent->available_instances));
115}
116static MDEV_TYPE_ATTR_RO(available_instances);
117
118static ssize_t description_show(struct mdev_type *mtype,
119				struct mdev_type_attribute *attr,
120				char *buf)
121{
122	return mtype->parent->mdev_driver->show_description(mtype, buf);
123}
124static MDEV_TYPE_ATTR_RO(description);
125
126static struct attribute *mdev_types_core_attrs[] = {
127	&mdev_type_attr_create.attr,
128	&mdev_type_attr_device_api.attr,
129	&mdev_type_attr_name.attr,
130	&mdev_type_attr_available_instances.attr,
131	&mdev_type_attr_description.attr,
132	NULL,
133};
134
135static umode_t mdev_types_core_is_visible(struct kobject *kobj,
136					  struct attribute *attr, int n)
137{
138	if (attr == &mdev_type_attr_description.attr &&
139	    !to_mdev_type(kobj)->parent->mdev_driver->show_description)
140		return 0;
141	return attr->mode;
142}
143
144static struct attribute_group mdev_type_core_group = {
145	.attrs = mdev_types_core_attrs,
146	.is_visible = mdev_types_core_is_visible,
147};
148
149static const struct attribute_group *mdev_type_groups[] = {
150	&mdev_type_core_group,
151	NULL,
152};
153
154static void mdev_type_release(struct kobject *kobj)
155{
156	struct mdev_type *type = to_mdev_type(kobj);
157
158	pr_debug("Releasing group %s\n", kobj->name);
159	/* Pairs with the get in add_mdev_supported_type() */
160	put_device(type->parent->dev);
161}
162
163static const struct kobj_type mdev_type_ktype = {
164	.sysfs_ops	= &mdev_type_sysfs_ops,
165	.release	= mdev_type_release,
166	.default_groups	= mdev_type_groups,
167};
168
169static int mdev_type_add(struct mdev_parent *parent, struct mdev_type *type)
 
170{
 
171	int ret;
172
 
 
 
 
 
 
 
 
 
173	type->kobj.kset = parent->mdev_types_kset;
174	type->parent = parent;
175	/* Pairs with the put in mdev_type_release() */
176	get_device(parent->dev);
177
178	ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
179				   "%s-%s", dev_driver_string(parent->dev),
180				   type->sysfs_name);
181	if (ret) {
182		kobject_put(&type->kobj);
183		return ret;
184	}
185
 
 
 
 
186	type->devices_kobj = kobject_create_and_add("devices", &type->kobj);
187	if (!type->devices_kobj) {
188		ret = -ENOMEM;
189		goto attr_devices_failed;
190	}
191
192	return 0;
 
 
 
 
 
193
 
 
 
 
 
 
194attr_devices_failed:
 
 
195	kobject_del(&type->kobj);
196	kobject_put(&type->kobj);
197	return ret;
198}
199
200static void mdev_type_remove(struct mdev_type *type)
201{
 
 
202	kobject_put(type->devices_kobj);
 
203	kobject_del(&type->kobj);
204	kobject_put(&type->kobj);
205}
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207/* mdev sysfs functions */
208void parent_remove_sysfs_files(struct mdev_parent *parent)
209{
210	int i;
 
 
 
 
 
211
212	for (i = 0; i < parent->nr_types; i++)
213		mdev_type_remove(parent->types[i]);
214	kset_unregister(parent->mdev_types_kset);
215}
216
217int parent_create_sysfs_files(struct mdev_parent *parent)
218{
219	int ret, i;
220
221	parent->mdev_types_kset = kset_create_and_add("mdev_supported_types",
222					       NULL, &parent->dev->kobj);
 
223	if (!parent->mdev_types_kset)
224		return -ENOMEM;
225
226	for (i = 0; i < parent->nr_types; i++) {
227		ret = mdev_type_add(parent, parent->types[i]);
228		if (ret)
229			goto out_err;
230	}
231	return 0;
 
 
 
 
 
 
 
232
233out_err:
234	while (--i >= 0)
235		mdev_type_remove(parent->types[i]);
236	kset_unregister(parent->mdev_types_kset);
237	return ret;
238}
239
240static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
241			    const char *buf, size_t count)
242{
243	struct mdev_device *mdev = to_mdev_device(dev);
244	unsigned long val;
245
246	if (kstrtoul(buf, 0, &val) < 0)
247		return -EINVAL;
248
249	if (val && device_remove_file_self(dev, attr)) {
250		int ret;
251
252		ret = mdev_device_remove(mdev);
253		if (ret)
254			return ret;
255	}
256
257	return count;
258}
259
260static DEVICE_ATTR_WO(remove);
261
262static struct attribute *mdev_device_attrs[] = {
263	&dev_attr_remove.attr,
264	NULL,
265};
266
267static const struct attribute_group mdev_device_group = {
268	.attrs = mdev_device_attrs,
269};
270
271const struct attribute_group *mdev_device_groups[] = {
272	&mdev_device_group,
273	NULL
274};
275
276int mdev_create_sysfs_files(struct mdev_device *mdev)
277{
278	struct mdev_type *type = mdev->type;
279	struct kobject *kobj = &mdev->dev.kobj;
280	int ret;
281
282	ret = sysfs_create_link(type->devices_kobj, kobj, dev_name(&mdev->dev));
283	if (ret)
284		return ret;
285
286	ret = sysfs_create_link(kobj, &type->kobj, "mdev_type");
287	if (ret)
288		goto type_link_failed;
 
 
 
 
 
289	return ret;
290
 
 
291type_link_failed:
292	sysfs_remove_link(mdev->type->devices_kobj, dev_name(&mdev->dev));
293	return ret;
294}
295
296void mdev_remove_sysfs_files(struct mdev_device *mdev)
297{
298	struct kobject *kobj = &mdev->dev.kobj;
299
300	sysfs_remove_link(kobj, "mdev_type");
301	sysfs_remove_link(mdev->type->devices_kobj, dev_name(&mdev->dev));
302}
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * File attributes for Mediated devices
  4 *
  5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  6 *     Author: Neo Jia <cjia@nvidia.com>
  7 *             Kirti Wankhede <kwankhede@nvidia.com>
  8 */
  9
 10#include <linux/sysfs.h>
 11#include <linux/ctype.h>
 12#include <linux/device.h>
 13#include <linux/slab.h>
 14#include <linux/uuid.h>
 15#include <linux/mdev.h>
 16
 17#include "mdev_private.h"
 18
 19/* Static functions */
 
 
 
 
 
 
 
 
 
 
 
 
 20
 21static ssize_t mdev_type_attr_show(struct kobject *kobj,
 22				     struct attribute *__attr, char *buf)
 23{
 24	struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
 25	struct mdev_type *type = to_mdev_type(kobj);
 26	ssize_t ret = -EIO;
 27
 28	if (attr->show)
 29		ret = attr->show(kobj, type->parent->dev, buf);
 30	return ret;
 31}
 32
 33static ssize_t mdev_type_attr_store(struct kobject *kobj,
 34				      struct attribute *__attr,
 35				      const char *buf, size_t count)
 36{
 37	struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
 38	struct mdev_type *type = to_mdev_type(kobj);
 39	ssize_t ret = -EIO;
 40
 41	if (attr->store)
 42		ret = attr->store(&type->kobj, type->parent->dev, buf, count);
 43	return ret;
 44}
 45
 46static const struct sysfs_ops mdev_type_sysfs_ops = {
 47	.show = mdev_type_attr_show,
 48	.store = mdev_type_attr_store,
 49};
 50
 51static ssize_t create_store(struct kobject *kobj, struct device *dev,
 52			    const char *buf, size_t count)
 
 53{
 54	char *str;
 55	guid_t uuid;
 56	int ret;
 57
 58	if ((count < UUID_STRING_LEN) || (count > UUID_STRING_LEN + 1))
 59		return -EINVAL;
 60
 61	str = kstrndup(buf, count, GFP_KERNEL);
 62	if (!str)
 63		return -ENOMEM;
 64
 65	ret = guid_parse(str, &uuid);
 66	kfree(str);
 67	if (ret)
 68		return ret;
 69
 70	ret = mdev_device_create(kobj, dev, &uuid);
 71	if (ret)
 72		return ret;
 73
 74	return count;
 75}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 76
 77MDEV_TYPE_ATTR_WO(create);
 
 
 
 
 
 
 
 
 78
 79static void mdev_type_release(struct kobject *kobj)
 80{
 81	struct mdev_type *type = to_mdev_type(kobj);
 82
 83	pr_debug("Releasing group %s\n", kobj->name);
 84	kfree(type);
 
 85}
 86
 87static struct kobj_type mdev_type_ktype = {
 88	.sysfs_ops = &mdev_type_sysfs_ops,
 89	.release = mdev_type_release,
 
 90};
 91
 92static struct mdev_type *add_mdev_supported_type(struct mdev_parent *parent,
 93						 struct attribute_group *group)
 94{
 95	struct mdev_type *type;
 96	int ret;
 97
 98	if (!group->name) {
 99		pr_err("%s: Type name empty!\n", __func__);
100		return ERR_PTR(-EINVAL);
101	}
102
103	type = kzalloc(sizeof(*type), GFP_KERNEL);
104	if (!type)
105		return ERR_PTR(-ENOMEM);
106
107	type->kobj.kset = parent->mdev_types_kset;
 
 
 
108
109	ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
110				   "%s-%s", dev_driver_string(parent->dev),
111				   group->name);
112	if (ret) {
113		kfree(type);
114		return ERR_PTR(ret);
115	}
116
117	ret = sysfs_create_file(&type->kobj, &mdev_type_attr_create.attr);
118	if (ret)
119		goto attr_create_failed;
120
121	type->devices_kobj = kobject_create_and_add("devices", &type->kobj);
122	if (!type->devices_kobj) {
123		ret = -ENOMEM;
124		goto attr_devices_failed;
125	}
126
127	ret = sysfs_create_files(&type->kobj,
128				 (const struct attribute **)group->attrs);
129	if (ret) {
130		ret = -ENOMEM;
131		goto attrs_failed;
132	}
133
134	type->group = group;
135	type->parent = parent;
136	return type;
137
138attrs_failed:
139	kobject_put(type->devices_kobj);
140attr_devices_failed:
141	sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
142attr_create_failed:
143	kobject_del(&type->kobj);
144	kobject_put(&type->kobj);
145	return ERR_PTR(ret);
146}
147
148static void remove_mdev_supported_type(struct mdev_type *type)
149{
150	sysfs_remove_files(&type->kobj,
151			   (const struct attribute **)type->group->attrs);
152	kobject_put(type->devices_kobj);
153	sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
154	kobject_del(&type->kobj);
155	kobject_put(&type->kobj);
156}
157
158static int add_mdev_supported_type_groups(struct mdev_parent *parent)
159{
160	int i;
161
162	for (i = 0; parent->ops->supported_type_groups[i]; i++) {
163		struct mdev_type *type;
164
165		type = add_mdev_supported_type(parent,
166					parent->ops->supported_type_groups[i]);
167		if (IS_ERR(type)) {
168			struct mdev_type *ltype, *tmp;
169
170			list_for_each_entry_safe(ltype, tmp, &parent->type_list,
171						  next) {
172				list_del(&ltype->next);
173				remove_mdev_supported_type(ltype);
174			}
175			return PTR_ERR(type);
176		}
177		list_add(&type->next, &parent->type_list);
178	}
179	return 0;
180}
181
182/* mdev sysfs functions */
183void parent_remove_sysfs_files(struct mdev_parent *parent)
184{
185	struct mdev_type *type, *tmp;
186
187	list_for_each_entry_safe(type, tmp, &parent->type_list, next) {
188		list_del(&type->next);
189		remove_mdev_supported_type(type);
190	}
191
192	sysfs_remove_groups(&parent->dev->kobj, parent->ops->dev_attr_groups);
 
193	kset_unregister(parent->mdev_types_kset);
194}
195
196int parent_create_sysfs_files(struct mdev_parent *parent)
197{
198	int ret;
199
200	parent->mdev_types_kset = kset_create_and_add("mdev_supported_types",
201					       NULL, &parent->dev->kobj);
202
203	if (!parent->mdev_types_kset)
204		return -ENOMEM;
205
206	INIT_LIST_HEAD(&parent->type_list);
207
208	ret = sysfs_create_groups(&parent->dev->kobj,
209				  parent->ops->dev_attr_groups);
210	if (ret)
211		goto create_err;
212
213	ret = add_mdev_supported_type_groups(parent);
214	if (ret)
215		sysfs_remove_groups(&parent->dev->kobj,
216				    parent->ops->dev_attr_groups);
217	else
218		return ret;
219
220create_err:
 
 
221	kset_unregister(parent->mdev_types_kset);
222	return ret;
223}
224
225static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
226			    const char *buf, size_t count)
227{
 
228	unsigned long val;
229
230	if (kstrtoul(buf, 0, &val) < 0)
231		return -EINVAL;
232
233	if (val && device_remove_file_self(dev, attr)) {
234		int ret;
235
236		ret = mdev_device_remove(dev);
237		if (ret)
238			return ret;
239	}
240
241	return count;
242}
243
244static DEVICE_ATTR_WO(remove);
245
246static const struct attribute *mdev_device_attrs[] = {
247	&dev_attr_remove.attr,
248	NULL,
249};
250
251int  mdev_create_sysfs_files(struct device *dev, struct mdev_type *type)
 
 
 
 
 
 
 
 
 
252{
 
 
253	int ret;
254
255	ret = sysfs_create_link(type->devices_kobj, &dev->kobj, dev_name(dev));
256	if (ret)
257		return ret;
258
259	ret = sysfs_create_link(&dev->kobj, &type->kobj, "mdev_type");
260	if (ret)
261		goto type_link_failed;
262
263	ret = sysfs_create_files(&dev->kobj, mdev_device_attrs);
264	if (ret)
265		goto create_files_failed;
266
267	return ret;
268
269create_files_failed:
270	sysfs_remove_link(&dev->kobj, "mdev_type");
271type_link_failed:
272	sysfs_remove_link(type->devices_kobj, dev_name(dev));
273	return ret;
274}
275
276void mdev_remove_sysfs_files(struct device *dev, struct mdev_type *type)
277{
278	sysfs_remove_files(&dev->kobj, mdev_device_attrs);
279	sysfs_remove_link(&dev->kobj, "mdev_type");
280	sysfs_remove_link(type->devices_kobj, dev_name(dev));
 
281}