Skip to content

Commit ea053b2

Browse files
dtorgregkh
authored andcommitted
ptp: create "pins" together with the rest of attributes
commit 85a66e5 upstream. Let's switch to using device_create_with_groups(), which will allow us to create "pins" attribute group together with the rest of ptp device attributes, and before userspace gets notified about ptp device creation. Signed-off-by: Dmitry Torokhov <[email protected]> Signed-off-by: David S. Miller <[email protected]> [bwh: Backported to 4.9: adjust context] Signed-off-by: Ben Hutchings <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 6347abb commit ea053b2

File tree

3 files changed

+24
-42
lines changed

3 files changed

+24
-42
lines changed

drivers/ptp/ptp_clock.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,17 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
214214
mutex_init(&ptp->pincfg_mux);
215215
init_waitqueue_head(&ptp->tsev_wq);
216216

217+
err = ptp_populate_pin_groups(ptp);
218+
if (err)
219+
goto no_pin_groups;
220+
217221
/* Create a new device in our class. */
218-
ptp->dev = device_create(ptp_class, parent, ptp->devid, ptp,
219-
"ptp%d", ptp->index);
222+
ptp->dev = device_create_with_groups(ptp_class, parent, ptp->devid,
223+
ptp, ptp->pin_attr_groups,
224+
"ptp%d", ptp->index);
220225
if (IS_ERR(ptp->dev))
221226
goto no_device;
222227

223-
err = ptp_populate_sysfs(ptp);
224-
if (err)
225-
goto no_sysfs;
226-
227228
/* Register a new PPS source. */
228229
if (info->pps) {
229230
struct pps_source_info pps;
@@ -251,10 +252,10 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
251252
if (ptp->pps_source)
252253
pps_unregister_source(ptp->pps_source);
253254
no_pps:
254-
ptp_cleanup_sysfs(ptp);
255-
no_sysfs:
256255
device_destroy(ptp_class, ptp->devid);
257256
no_device:
257+
ptp_cleanup_pin_groups(ptp);
258+
no_pin_groups:
258259
mutex_destroy(&ptp->tsevq_mux);
259260
mutex_destroy(&ptp->pincfg_mux);
260261
ida_simple_remove(&ptp_clocks_map, index);
@@ -273,8 +274,9 @@ int ptp_clock_unregister(struct ptp_clock *ptp)
273274
/* Release the clock's resources. */
274275
if (ptp->pps_source)
275276
pps_unregister_source(ptp->pps_source);
276-
ptp_cleanup_sysfs(ptp);
277+
277278
device_destroy(ptp_class, ptp->devid);
279+
ptp_cleanup_pin_groups(ptp);
278280

279281
posix_clock_unregister(&ptp->clock);
280282
return 0;

drivers/ptp/ptp_private.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct ptp_clock {
5454
struct device_attribute *pin_dev_attr;
5555
struct attribute **pin_attr;
5656
struct attribute_group pin_attr_group;
57+
/* 1st entry is a pointer to the real group, 2nd is NULL terminator */
58+
const struct attribute_group *pin_attr_groups[2];
5759
};
5860

5961
/*
@@ -94,8 +96,7 @@ uint ptp_poll(struct posix_clock *pc,
9496

9597
extern const struct attribute_group *ptp_groups[];
9698

97-
int ptp_cleanup_sysfs(struct ptp_clock *ptp);
98-
99-
int ptp_populate_sysfs(struct ptp_clock *ptp);
99+
int ptp_populate_pin_groups(struct ptp_clock *ptp);
100+
void ptp_cleanup_pin_groups(struct ptp_clock *ptp);
100101

101102
#endif

drivers/ptp/ptp_sysfs.c

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -268,25 +268,14 @@ static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
268268
return count;
269269
}
270270

271-
int ptp_cleanup_sysfs(struct ptp_clock *ptp)
271+
int ptp_populate_pin_groups(struct ptp_clock *ptp)
272272
{
273-
struct device *dev = ptp->dev;
274-
struct ptp_clock_info *info = ptp->info;
275-
276-
if (info->n_pins) {
277-
sysfs_remove_group(&dev->kobj, &ptp->pin_attr_group);
278-
kfree(ptp->pin_attr);
279-
kfree(ptp->pin_dev_attr);
280-
}
281-
return 0;
282-
}
283-
284-
static int ptp_populate_pins(struct ptp_clock *ptp)
285-
{
286-
struct device *dev = ptp->dev;
287273
struct ptp_clock_info *info = ptp->info;
288274
int err = -ENOMEM, i, n_pins = info->n_pins;
289275

276+
if (!n_pins)
277+
return 0;
278+
290279
ptp->pin_dev_attr = kzalloc(n_pins * sizeof(*ptp->pin_dev_attr),
291280
GFP_KERNEL);
292281
if (!ptp->pin_dev_attr)
@@ -310,28 +299,18 @@ static int ptp_populate_pins(struct ptp_clock *ptp)
310299
ptp->pin_attr_group.name = "pins";
311300
ptp->pin_attr_group.attrs = ptp->pin_attr;
312301

313-
err = sysfs_create_group(&dev->kobj, &ptp->pin_attr_group);
314-
if (err)
315-
goto no_group;
302+
ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
303+
316304
return 0;
317305

318-
no_group:
319-
kfree(ptp->pin_attr);
320306
no_pin_attr:
321307
kfree(ptp->pin_dev_attr);
322308
no_dev_attr:
323309
return err;
324310
}
325311

326-
int ptp_populate_sysfs(struct ptp_clock *ptp)
312+
void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
327313
{
328-
struct ptp_clock_info *info = ptp->info;
329-
int err;
330-
331-
if (info->n_pins) {
332-
err = ptp_populate_pins(ptp);
333-
if (err)
334-
return err;
335-
}
336-
return 0;
314+
kfree(ptp->pin_attr);
315+
kfree(ptp->pin_dev_attr);
337316
}

0 commit comments

Comments
 (0)