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