1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Greybus operations
4  *
5  * Copyright 2015-2016 Google Inc.
6  */
7 
8 #include <linux/string.h>
9 #include <linux/sysfs.h>
10 
11 #include "audio_manager.h"
12 #include "audio_manager_private.h"
13 
manager_sysfs_add_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)14 static ssize_t manager_sysfs_add_store(struct kobject *kobj,
15 				       struct kobj_attribute *attr,
16 				       const char *buf, size_t count)
17 {
18 	struct gb_audio_manager_module_descriptor desc = { {0} };
19 
20 	int num = sscanf(buf,
21 			"name=%" GB_AUDIO_MANAGER_MODULE_NAME_LEN_SSCANF
22 			"s vid=%d pid=%d intf_id=%d i/p devices=0x%X o/p devices=0x%X",
23 			desc.name, &desc.vid, &desc.pid, &desc.intf_id,
24 			&desc.ip_devices, &desc.op_devices);
25 
26 	if (num != 7)
27 		return -EINVAL;
28 
29 	num = gb_audio_manager_add(&desc);
30 	if (num < 0)
31 		return -EINVAL;
32 
33 	return count;
34 }
35 
36 static struct kobj_attribute manager_add_attribute =
37 	__ATTR(add, 0664, NULL, manager_sysfs_add_store);
38 
manager_sysfs_remove_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)39 static ssize_t manager_sysfs_remove_store(struct kobject *kobj,
40 					  struct kobj_attribute *attr,
41 					  const char *buf, size_t count)
42 {
43 	int id;
44 
45 	int num = kstrtoint(buf, 10, &id);
46 
47 	if (num != 1)
48 		return -EINVAL;
49 
50 	num = gb_audio_manager_remove(id);
51 	if (num)
52 		return num;
53 
54 	return count;
55 }
56 
57 static struct kobj_attribute manager_remove_attribute =
58 	__ATTR(remove, 0664, NULL, manager_sysfs_remove_store);
59 
manager_sysfs_dump_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)60 static ssize_t manager_sysfs_dump_store(struct kobject *kobj,
61 					struct kobj_attribute *attr,
62 					const char *buf, size_t count)
63 {
64 	int id;
65 
66 	int num = kstrtoint(buf, 10, &id);
67 
68 	if (num == 1) {
69 		num = gb_audio_manager_dump_module(id);
70 		if (num)
71 			return num;
72 	} else if (!strncmp("all", buf, 3)) {
73 		gb_audio_manager_dump_all();
74 	} else {
75 		return -EINVAL;
76 	}
77 
78 	return count;
79 }
80 
81 static struct kobj_attribute manager_dump_attribute =
82 	__ATTR(dump, 0664, NULL, manager_sysfs_dump_store);
83 
manager_sysfs_init_attribute(struct kobject * kobj,struct kobj_attribute * kattr)84 static void manager_sysfs_init_attribute(struct kobject *kobj,
85 					 struct kobj_attribute *kattr)
86 {
87 	int err;
88 
89 	err = sysfs_create_file(kobj, &kattr->attr);
90 	if (err) {
91 		pr_warn("creating the sysfs entry for %s failed: %d\n",
92 			kattr->attr.name, err);
93 	}
94 }
95 
gb_audio_manager_sysfs_init(struct kobject * kobj)96 void gb_audio_manager_sysfs_init(struct kobject *kobj)
97 {
98 	manager_sysfs_init_attribute(kobj, &manager_add_attribute);
99 	manager_sysfs_init_attribute(kobj, &manager_remove_attribute);
100 	manager_sysfs_init_attribute(kobj, &manager_dump_attribute);
101 }
102