Svetlin Ankov | 8db0073 | 2016-01-13 14:07:48 -0700 | [diff] [blame] | 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 | |
| 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 devices=0x%X", |
| 24 | desc.name, &desc.slot, &desc.vid, &desc.pid, |
| 25 | &desc.cport, &desc.devices); |
| 26 | |
| 27 | if (num != 6) |
| 28 | return -EINVAL; |
| 29 | |
| 30 | num = gb_audio_manager_add(&desc); |
| 31 | if (num < 0) |
| 32 | return -EINVAL; |
| 33 | |
| 34 | return count; |
| 35 | } |
| 36 | |
| 37 | static struct kobj_attribute manager_add_attribute = |
| 38 | __ATTR(add, 0664, NULL, manager_sysfs_add_store); |
| 39 | |
| 40 | static ssize_t manager_sysfs_remove_store( |
| 41 | struct kobject *kobj, struct kobj_attribute *attr, |
| 42 | const char *buf, size_t count) |
| 43 | { |
| 44 | int id; |
| 45 | |
| 46 | int num = sscanf(buf, "%d", &id); |
| 47 | |
| 48 | if (num != 1) |
| 49 | return -EINVAL; |
| 50 | |
| 51 | num = gb_audio_manager_remove(id); |
| 52 | if (num) |
| 53 | return num; |
| 54 | |
| 55 | return count; |
| 56 | } |
| 57 | |
| 58 | static struct kobj_attribute manager_remove_attribute = |
| 59 | __ATTR(remove, 0664, NULL, manager_sysfs_remove_store); |
| 60 | |
| 61 | static ssize_t manager_sysfs_dump_store( |
| 62 | struct kobject *kobj, struct kobj_attribute *attr, |
| 63 | const char *buf, size_t count) |
| 64 | { |
| 65 | int id; |
| 66 | |
| 67 | int num = sscanf(buf, "%d", &id); |
| 68 | |
| 69 | if (num == 1) { |
| 70 | num = gb_audio_manager_dump_module(id); |
| 71 | if (num) |
| 72 | return num; |
| 73 | } else if (!strncmp("all", buf, 3)) |
| 74 | gb_audio_manager_dump_all(); |
| 75 | else |
| 76 | return -EINVAL; |
| 77 | |
| 78 | return count; |
| 79 | } |
| 80 | |
| 81 | static struct kobj_attribute manager_dump_attribute = |
| 82 | __ATTR(dump, 0664, NULL, manager_sysfs_dump_store); |
| 83 | |
| 84 | static void manager_sysfs_init_attribute( |
| 85 | struct kobject *kobj, 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 | |
| 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 | } |