Skip to content

Commit 4156065

Browse files
Felipe Balbidavem330
authored andcommitted
PTP: introduce new versions of IOCTLs
The current version of the IOCTL have a small problem which prevents us from extending the API by making use of reserved fields. In these new IOCTLs, we are now making sure that flags and rsv fields are zero which will allow us to extend the API in the future. Reviewed-by: Richard Cochran <[email protected]> Signed-off-by: Felipe Balbi <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 6cd476d commit 4156065

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

drivers/ptp/ptp_chardev.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
126126
switch (cmd) {
127127

128128
case PTP_CLOCK_GETCAPS:
129+
case PTP_CLOCK_GETCAPS2:
129130
memset(&caps, 0, sizeof(caps));
131+
130132
caps.max_adj = ptp->info->max_adj;
131133
caps.n_alarm = ptp->info->n_alarm;
132134
caps.n_ext_ts = ptp->info->n_ext_ts;
@@ -139,11 +141,24 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
139141
break;
140142

141143
case PTP_EXTTS_REQUEST:
144+
case PTP_EXTTS_REQUEST2:
145+
memset(&req, 0, sizeof(req));
146+
142147
if (copy_from_user(&req.extts, (void __user *)arg,
143148
sizeof(req.extts))) {
144149
err = -EFAULT;
145150
break;
146151
}
152+
if (((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
153+
req.extts.rsv[0] || req.extts.rsv[1]) &&
154+
cmd == PTP_EXTTS_REQUEST2) {
155+
err = -EINVAL;
156+
break;
157+
} else if (cmd == PTP_EXTTS_REQUEST) {
158+
req.extts.flags &= ~PTP_EXTTS_VALID_FLAGS;
159+
req.extts.rsv[0] = 0;
160+
req.extts.rsv[1] = 0;
161+
}
147162
if (req.extts.index >= ops->n_ext_ts) {
148163
err = -EINVAL;
149164
break;
@@ -154,11 +169,27 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
154169
break;
155170

156171
case PTP_PEROUT_REQUEST:
172+
case PTP_PEROUT_REQUEST2:
173+
memset(&req, 0, sizeof(req));
174+
157175
if (copy_from_user(&req.perout, (void __user *)arg,
158176
sizeof(req.perout))) {
159177
err = -EFAULT;
160178
break;
161179
}
180+
if (((req.perout.flags & ~PTP_PEROUT_VALID_FLAGS) ||
181+
req.perout.rsv[0] || req.perout.rsv[1] ||
182+
req.perout.rsv[2] || req.perout.rsv[3]) &&
183+
cmd == PTP_PEROUT_REQUEST2) {
184+
err = -EINVAL;
185+
break;
186+
} else if (cmd == PTP_PEROUT_REQUEST) {
187+
req.perout.flags &= ~PTP_PEROUT_VALID_FLAGS;
188+
req.perout.rsv[0] = 0;
189+
req.perout.rsv[1] = 0;
190+
req.perout.rsv[2] = 0;
191+
req.perout.rsv[3] = 0;
192+
}
162193
if (req.perout.index >= ops->n_per_out) {
163194
err = -EINVAL;
164195
break;
@@ -169,6 +200,9 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
169200
break;
170201

171202
case PTP_ENABLE_PPS:
203+
case PTP_ENABLE_PPS2:
204+
memset(&req, 0, sizeof(req));
205+
172206
if (!capable(CAP_SYS_TIME))
173207
return -EPERM;
174208
req.type = PTP_CLK_REQ_PPS;
@@ -177,6 +211,7 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
177211
break;
178212

179213
case PTP_SYS_OFFSET_PRECISE:
214+
case PTP_SYS_OFFSET_PRECISE2:
180215
if (!ptp->info->getcrosststamp) {
181216
err = -EOPNOTSUPP;
182217
break;
@@ -201,6 +236,7 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
201236
break;
202237

203238
case PTP_SYS_OFFSET_EXTENDED:
239+
case PTP_SYS_OFFSET_EXTENDED2:
204240
if (!ptp->info->gettimex64) {
205241
err = -EOPNOTSUPP;
206242
break;
@@ -232,6 +268,7 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
232268
break;
233269

234270
case PTP_SYS_OFFSET:
271+
case PTP_SYS_OFFSET2:
235272
sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
236273
if (IS_ERR(sysoff)) {
237274
err = PTR_ERR(sysoff);
@@ -266,10 +303,23 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
266303
break;
267304

268305
case PTP_PIN_GETFUNC:
306+
case PTP_PIN_GETFUNC2:
269307
if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
270308
err = -EFAULT;
271309
break;
272310
}
311+
if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
312+
|| pd.rsv[3] || pd.rsv[4])
313+
&& cmd == PTP_PIN_GETFUNC2) {
314+
err = -EINVAL;
315+
break;
316+
} else if (cmd == PTP_PIN_GETFUNC) {
317+
pd.rsv[0] = 0;
318+
pd.rsv[1] = 0;
319+
pd.rsv[2] = 0;
320+
pd.rsv[3] = 0;
321+
pd.rsv[4] = 0;
322+
}
273323
pin_index = pd.index;
274324
if (pin_index >= ops->n_pins) {
275325
err = -EINVAL;
@@ -285,10 +335,23 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
285335
break;
286336

287337
case PTP_PIN_SETFUNC:
338+
case PTP_PIN_SETFUNC2:
288339
if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
289340
err = -EFAULT;
290341
break;
291342
}
343+
if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
344+
|| pd.rsv[3] || pd.rsv[4])
345+
&& cmd == PTP_PIN_SETFUNC2) {
346+
err = -EINVAL;
347+
break;
348+
} else if (cmd == PTP_PIN_SETFUNC) {
349+
pd.rsv[0] = 0;
350+
pd.rsv[1] = 0;
351+
pd.rsv[2] = 0;
352+
pd.rsv[3] = 0;
353+
pd.rsv[4] = 0;
354+
}
292355
pin_index = pd.index;
293356
if (pin_index >= ops->n_pins) {
294357
err = -EINVAL;

include/uapi/linux/ptp_clock.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,20 @@
2525
#include <linux/ioctl.h>
2626
#include <linux/types.h>
2727

28-
/* PTP_xxx bits, for the flags field within the request structures. */
28+
/*
29+
* Bits of the ptp_extts_request.flags field:
30+
*/
2931
#define PTP_ENABLE_FEATURE (1<<0)
3032
#define PTP_RISING_EDGE (1<<1)
3133
#define PTP_FALLING_EDGE (1<<2)
34+
#define PTP_EXTTS_VALID_FLAGS (PTP_ENABLE_FEATURE | \
35+
PTP_RISING_EDGE | \
36+
PTP_FALLING_EDGE)
37+
38+
/*
39+
* Bits of the ptp_perout_request.flags field:
40+
*/
41+
#define PTP_PEROUT_VALID_FLAGS (0)
3242

3343
/*
3444
* struct ptp_clock_time - represents a time value
@@ -149,6 +159,18 @@ struct ptp_pin_desc {
149159
#define PTP_SYS_OFFSET_EXTENDED \
150160
_IOWR(PTP_CLK_MAGIC, 9, struct ptp_sys_offset_extended)
151161

162+
#define PTP_CLOCK_GETCAPS2 _IOR(PTP_CLK_MAGIC, 10, struct ptp_clock_caps)
163+
#define PTP_EXTTS_REQUEST2 _IOW(PTP_CLK_MAGIC, 11, struct ptp_extts_request)
164+
#define PTP_PEROUT_REQUEST2 _IOW(PTP_CLK_MAGIC, 12, struct ptp_perout_request)
165+
#define PTP_ENABLE_PPS2 _IOW(PTP_CLK_MAGIC, 13, int)
166+
#define PTP_SYS_OFFSET2 _IOW(PTP_CLK_MAGIC, 14, struct ptp_sys_offset)
167+
#define PTP_PIN_GETFUNC2 _IOWR(PTP_CLK_MAGIC, 15, struct ptp_pin_desc)
168+
#define PTP_PIN_SETFUNC2 _IOW(PTP_CLK_MAGIC, 16, struct ptp_pin_desc)
169+
#define PTP_SYS_OFFSET_PRECISE2 \
170+
_IOWR(PTP_CLK_MAGIC, 17, struct ptp_sys_offset_precise)
171+
#define PTP_SYS_OFFSET_EXTENDED2 \
172+
_IOWR(PTP_CLK_MAGIC, 18, struct ptp_sys_offset_extended)
173+
152174
struct ptp_extts_event {
153175
struct ptp_clock_time t; /* Time event occured. */
154176
unsigned int index; /* Which channel produced the event. */

0 commit comments

Comments
 (0)