-
Notifications
You must be signed in to change notification settings - Fork 0
/
evdi_sysfs.c
129 lines (109 loc) · 3.18 KB
/
evdi_sysfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// SPDX-License-Identifier: GPL-2.0-only
/*
* evdi_sysfs.c
*
* Copyright (c) 2020 DisplayLink (UK) Ltd.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/device.h>
#include <linux/slab.h>
#include "evdi_sysfs.h"
#include "evdi_params.h"
#include "evdi_debug.h"
#include "evdi_platform_drv.h"
static ssize_t version_show(__always_unused struct device *dev,
__always_unused struct device_attribute *attr,
char *buf)
{
return snprintf(buf, PAGE_SIZE, "%u.%u.%u\n", DRIVER_MAJOR,
DRIVER_MINOR, DRIVER_PATCH);
}
static ssize_t count_show(__always_unused struct device *dev,
__always_unused struct device_attribute *attr,
char *buf)
{
return snprintf(buf, PAGE_SIZE, "%u\n", evdi_platform_device_count(dev));
}
static ssize_t add_store(struct device *dev,
__always_unused struct device_attribute *attr,
const char *buf, size_t count)
{
unsigned int val;
int ret;
if (kstrtouint(buf, 10, &val)) {
EVDI_ERROR("Invalid device count \"%s\"\n", buf);
return -EINVAL;
}
ret = evdi_platform_add_devices(dev, val);
if (ret)
return ret;
return count;
}
static ssize_t remove_all_store(struct device *dev,
__always_unused struct device_attribute *attr,
__always_unused const char *buf,
size_t count)
{
evdi_platform_remove_all_devices(dev);
return count;
}
static ssize_t loglevel_show(__always_unused struct device *dev,
__always_unused struct device_attribute *attr,
char *buf)
{
return snprintf(buf, PAGE_SIZE, "%u\n", evdi_loglevel);
}
static ssize_t loglevel_store(__always_unused struct device *dev,
__always_unused struct device_attribute *attr,
const char *buf,
size_t count)
{
unsigned int val;
if (kstrtouint(buf, 10, &val)) {
EVDI_ERROR("Unable to parse %u\n", val);
return -EINVAL;
}
if (val > EVDI_LOGLEVEL_VERBOSE) {
EVDI_ERROR("Invalid loglevel %u\n", val);
return -EINVAL;
}
EVDI_INFO("Setting loglevel to %u\n", val);
evdi_loglevel = val;
return count;
}
static struct device_attribute evdi_device_attributes[] = {
__ATTR_RO(count),
__ATTR_RO(version),
__ATTR_RW(loglevel),
__ATTR_WO(add),
__ATTR_WO(remove_all)
};
void evdi_sysfs_init(struct device *root)
{
unsigned int i;
if (!PTR_ERR_OR_ZERO(root))
for (i = 0; i < ARRAY_SIZE(evdi_device_attributes); i++)
device_create_file(root, &evdi_device_attributes[i]);
}
void evdi_sysfs_exit(struct device *root)
{
unsigned int i;
if (PTR_ERR_OR_ZERO(root)) {
EVDI_ERROR("root device is null");
return;
}
for (i = 0; i < ARRAY_SIZE(evdi_device_attributes); i++)
device_remove_file(root, &evdi_device_attributes[i]);
}