Skip to content

Commit afd84fb

Browse files
committed
thermal: sysfs: Get to trips via attribute pointers
The _store() and _show() functions for sysfs attributes corresponding to trip point parameters (type, temperature and hysteresis) read the trip ID from the attribute name and then use the trip ID as the index in the given thermal zone's trips table to get to the trip object they want. Instead of doing this, make them use the attribute pointer they get as the second argument to get to the trip object embedded in the same struct thermal_trip_desc as the struct device_attribute pointed to by it, which is much more straightforward and less overhead. Signed-off-by: Rafael J. Wysocki <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 66b2633 commit afd84fb

File tree

1 file changed

+20
-34
lines changed

1 file changed

+20
-34
lines changed

drivers/thermal/thermal_sysfs.c

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1414

15+
#include <linux/container_of.h>
1516
#include <linux/sysfs.h>
1617
#include <linux/device.h>
1718
#include <linux/err.h>
@@ -78,39 +79,38 @@ mode_store(struct device *dev, struct device_attribute *attr,
7879
return count;
7980
}
8081

82+
#define thermal_trip_of_attr(_ptr_, _attr_) \
83+
({ \
84+
struct thermal_trip_desc *td; \
85+
\
86+
td = container_of(_ptr_, struct thermal_trip_desc, \
87+
trip_attrs._attr_.attr); \
88+
&td->trip; \
89+
})
90+
8191
static ssize_t
8292
trip_point_type_show(struct device *dev, struct device_attribute *attr,
8393
char *buf)
8494
{
85-
struct thermal_zone_device *tz = to_thermal_zone(dev);
86-
int trip_id;
87-
88-
if (sscanf(attr->attr.name, "trip_point_%d_type", &trip_id) != 1)
89-
return -EINVAL;
95+
struct thermal_trip *trip = thermal_trip_of_attr(attr, type);
9096

91-
return sprintf(buf, "%s\n", thermal_trip_type_name(tz->trips[trip_id].trip.type));
97+
return sprintf(buf, "%s\n", thermal_trip_type_name(trip->type));
9298
}
9399

94100
static ssize_t
95101
trip_point_temp_store(struct device *dev, struct device_attribute *attr,
96102
const char *buf, size_t count)
97103
{
104+
struct thermal_trip *trip = thermal_trip_of_attr(attr, temp);
98105
struct thermal_zone_device *tz = to_thermal_zone(dev);
99-
struct thermal_trip *trip;
100-
int trip_id, ret;
101-
int temp;
106+
int ret, temp;
102107

103108
ret = kstrtoint(buf, 10, &temp);
104109
if (ret)
105110
return -EINVAL;
106111

107-
if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
108-
return -EINVAL;
109-
110112
mutex_lock(&tz->lock);
111113

112-
trip = &tz->trips[trip_id].trip;
113-
114114
if (temp != trip->temperature) {
115115
if (tz->ops.set_trip_temp) {
116116
ret = tz->ops.set_trip_temp(tz, trip, temp);
@@ -133,35 +133,25 @@ static ssize_t
133133
trip_point_temp_show(struct device *dev, struct device_attribute *attr,
134134
char *buf)
135135
{
136-
struct thermal_zone_device *tz = to_thermal_zone(dev);
137-
int trip_id;
136+
struct thermal_trip *trip = thermal_trip_of_attr(attr, temp);
138137

139-
if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
140-
return -EINVAL;
141-
142-
return sprintf(buf, "%d\n", READ_ONCE(tz->trips[trip_id].trip.temperature));
138+
return sprintf(buf, "%d\n", READ_ONCE(trip->temperature));
143139
}
144140

145141
static ssize_t
146142
trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
147143
const char *buf, size_t count)
148144
{
145+
struct thermal_trip *trip = thermal_trip_of_attr(attr, hyst);
149146
struct thermal_zone_device *tz = to_thermal_zone(dev);
150-
struct thermal_trip *trip;
151-
int trip_id, ret;
152-
int hyst;
147+
int ret, hyst;
153148

154149
ret = kstrtoint(buf, 10, &hyst);
155150
if (ret || hyst < 0)
156151
return -EINVAL;
157152

158-
if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
159-
return -EINVAL;
160-
161153
mutex_lock(&tz->lock);
162154

163-
trip = &tz->trips[trip_id].trip;
164-
165155
if (hyst != trip->hysteresis) {
166156
WRITE_ONCE(trip->hysteresis, hyst);
167157

@@ -177,13 +167,9 @@ static ssize_t
177167
trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
178168
char *buf)
179169
{
180-
struct thermal_zone_device *tz = to_thermal_zone(dev);
181-
int trip_id;
182-
183-
if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
184-
return -EINVAL;
170+
struct thermal_trip *trip = thermal_trip_of_attr(attr, hyst);
185171

186-
return sprintf(buf, "%d\n", READ_ONCE(tz->trips[trip_id].trip.hysteresis));
172+
return sprintf(buf, "%d\n", READ_ONCE(trip->hysteresis));
187173
}
188174

189175
static ssize_t

0 commit comments

Comments
 (0)