Skip to content

Commit 6347abb

Browse files
dtorgregkh
authored andcommitted
ptp: use is_visible method to hide unused attributes
commit af59e71 upstream. Instead of creating selected attributes after the device is created (and after userspace potentially seen uevent), lets use attribute group is_visible() method to control which attributes are shown. This will allow us to create all attributes (except "pins" group, which will be taken care of later) before userspace gets notified about new ptp class device. Signed-off-by: Dmitry Torokhov <[email protected]> Signed-off-by: David S. Miller <[email protected]> Signed-off-by: Ben Hutchings <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent bca1732 commit 6347abb

File tree

1 file changed

+55
-70
lines changed

1 file changed

+55
-70
lines changed

drivers/ptp/ptp_sysfs.c

Lines changed: 55 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,6 @@ PTP_SHOW_INT(n_periodic_outputs, n_per_out);
4646
PTP_SHOW_INT(n_programmable_pins, n_pins);
4747
PTP_SHOW_INT(pps_available, pps);
4848

49-
static struct attribute *ptp_attrs[] = {
50-
&dev_attr_clock_name.attr,
51-
&dev_attr_max_adjustment.attr,
52-
&dev_attr_n_alarms.attr,
53-
&dev_attr_n_external_timestamps.attr,
54-
&dev_attr_n_periodic_outputs.attr,
55-
&dev_attr_n_programmable_pins.attr,
56-
&dev_attr_pps_available.attr,
57-
NULL,
58-
};
59-
60-
static const struct attribute_group ptp_group = {
61-
.attrs = ptp_attrs,
62-
};
63-
64-
const struct attribute_group *ptp_groups[] = {
65-
&ptp_group,
66-
NULL,
67-
};
68-
69-
7049
static ssize_t extts_enable_store(struct device *dev,
7150
struct device_attribute *attr,
7251
const char *buf, size_t count)
@@ -91,6 +70,7 @@ static ssize_t extts_enable_store(struct device *dev,
9170
out:
9271
return err;
9372
}
73+
static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
9474

9575
static ssize_t extts_fifo_show(struct device *dev,
9676
struct device_attribute *attr, char *page)
@@ -124,6 +104,7 @@ static ssize_t extts_fifo_show(struct device *dev,
124104
mutex_unlock(&ptp->tsevq_mux);
125105
return cnt;
126106
}
107+
static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
127108

128109
static ssize_t period_store(struct device *dev,
129110
struct device_attribute *attr,
@@ -151,6 +132,7 @@ static ssize_t period_store(struct device *dev,
151132
out:
152133
return err;
153134
}
135+
static DEVICE_ATTR(period, 0220, NULL, period_store);
154136

155137
static ssize_t pps_enable_store(struct device *dev,
156138
struct device_attribute *attr,
@@ -177,6 +159,57 @@ static ssize_t pps_enable_store(struct device *dev,
177159
out:
178160
return err;
179161
}
162+
static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
163+
164+
static struct attribute *ptp_attrs[] = {
165+
&dev_attr_clock_name.attr,
166+
167+
&dev_attr_max_adjustment.attr,
168+
&dev_attr_n_alarms.attr,
169+
&dev_attr_n_external_timestamps.attr,
170+
&dev_attr_n_periodic_outputs.attr,
171+
&dev_attr_n_programmable_pins.attr,
172+
&dev_attr_pps_available.attr,
173+
174+
&dev_attr_extts_enable.attr,
175+
&dev_attr_fifo.attr,
176+
&dev_attr_period.attr,
177+
&dev_attr_pps_enable.attr,
178+
NULL
179+
};
180+
181+
static umode_t ptp_is_attribute_visible(struct kobject *kobj,
182+
struct attribute *attr, int n)
183+
{
184+
struct device *dev = kobj_to_dev(kobj);
185+
struct ptp_clock *ptp = dev_get_drvdata(dev);
186+
struct ptp_clock_info *info = ptp->info;
187+
umode_t mode = attr->mode;
188+
189+
if (attr == &dev_attr_extts_enable.attr ||
190+
attr == &dev_attr_fifo.attr) {
191+
if (!info->n_ext_ts)
192+
mode = 0;
193+
} else if (attr == &dev_attr_period.attr) {
194+
if (!info->n_per_out)
195+
mode = 0;
196+
} else if (attr == &dev_attr_pps_enable.attr) {
197+
if (!info->pps)
198+
mode = 0;
199+
}
200+
201+
return mode;
202+
}
203+
204+
static const struct attribute_group ptp_group = {
205+
.is_visible = ptp_is_attribute_visible,
206+
.attrs = ptp_attrs,
207+
};
208+
209+
const struct attribute_group *ptp_groups[] = {
210+
&ptp_group,
211+
NULL
212+
};
180213

181214
static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
182215
{
@@ -235,26 +268,11 @@ static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
235268
return count;
236269
}
237270

238-
static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
239-
static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
240-
static DEVICE_ATTR(period, 0220, NULL, period_store);
241-
static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
242-
243271
int ptp_cleanup_sysfs(struct ptp_clock *ptp)
244272
{
245273
struct device *dev = ptp->dev;
246274
struct ptp_clock_info *info = ptp->info;
247275

248-
if (info->n_ext_ts) {
249-
device_remove_file(dev, &dev_attr_extts_enable);
250-
device_remove_file(dev, &dev_attr_fifo);
251-
}
252-
if (info->n_per_out)
253-
device_remove_file(dev, &dev_attr_period);
254-
255-
if (info->pps)
256-
device_remove_file(dev, &dev_attr_pps_enable);
257-
258276
if (info->n_pins) {
259277
sysfs_remove_group(&dev->kobj, &ptp->pin_attr_group);
260278
kfree(ptp->pin_attr);
@@ -307,46 +325,13 @@ static int ptp_populate_pins(struct ptp_clock *ptp)
307325

308326
int ptp_populate_sysfs(struct ptp_clock *ptp)
309327
{
310-
struct device *dev = ptp->dev;
311328
struct ptp_clock_info *info = ptp->info;
312329
int err;
313330

314-
if (info->n_ext_ts) {
315-
err = device_create_file(dev, &dev_attr_extts_enable);
316-
if (err)
317-
goto out1;
318-
err = device_create_file(dev, &dev_attr_fifo);
319-
if (err)
320-
goto out2;
321-
}
322-
if (info->n_per_out) {
323-
err = device_create_file(dev, &dev_attr_period);
324-
if (err)
325-
goto out3;
326-
}
327-
if (info->pps) {
328-
err = device_create_file(dev, &dev_attr_pps_enable);
329-
if (err)
330-
goto out4;
331-
}
332331
if (info->n_pins) {
333332
err = ptp_populate_pins(ptp);
334333
if (err)
335-
goto out5;
334+
return err;
336335
}
337336
return 0;
338-
out5:
339-
if (info->pps)
340-
device_remove_file(dev, &dev_attr_pps_enable);
341-
out4:
342-
if (info->n_per_out)
343-
device_remove_file(dev, &dev_attr_period);
344-
out3:
345-
if (info->n_ext_ts)
346-
device_remove_file(dev, &dev_attr_fifo);
347-
out2:
348-
if (info->n_ext_ts)
349-
device_remove_file(dev, &dev_attr_extts_enable);
350-
out1:
351-
return err;
352337
}

0 commit comments

Comments
 (0)