2019-05-20 15:19:02 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2011-04-22 18:03:08 +08:00
|
|
|
/*
|
|
|
|
* PTP 1588 clock support - character device implementation.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 OMICRON electronics GmbH
|
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/posix-clock.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/sched.h>
|
2012-11-26 09:44:34 +08:00
|
|
|
#include <linux/slab.h>
|
2016-02-22 19:15:25 +08:00
|
|
|
#include <linux/timekeeping.h>
|
2011-04-22 18:03:08 +08:00
|
|
|
|
2018-10-16 21:06:41 +08:00
|
|
|
#include <linux/nospec.h>
|
|
|
|
|
2011-04-22 18:03:08 +08:00
|
|
|
#include "ptp_private.h"
|
|
|
|
|
2014-03-21 05:21:52 +08:00
|
|
|
static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
|
|
|
|
enum ptp_pin_function func, unsigned int chan)
|
|
|
|
{
|
|
|
|
struct ptp_clock_request rq;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
memset(&rq, 0, sizeof(rq));
|
|
|
|
|
|
|
|
switch (func) {
|
|
|
|
case PTP_PF_NONE:
|
|
|
|
break;
|
|
|
|
case PTP_PF_EXTTS:
|
|
|
|
rq.type = PTP_CLK_REQ_EXTTS;
|
|
|
|
rq.extts.index = chan;
|
|
|
|
err = ops->enable(ops, &rq, 0);
|
|
|
|
break;
|
|
|
|
case PTP_PF_PEROUT:
|
|
|
|
rq.type = PTP_CLK_REQ_PEROUT;
|
|
|
|
rq.perout.index = chan;
|
|
|
|
err = ops->enable(ops, &rq, 0);
|
|
|
|
break;
|
|
|
|
case PTP_PF_PHYSYNC:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
|
|
|
|
enum ptp_pin_function func, unsigned int chan)
|
|
|
|
{
|
|
|
|
struct ptp_clock_info *info = ptp->info;
|
|
|
|
struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin];
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* Check to see if any other pin previously had this function. */
|
|
|
|
for (i = 0; i < info->n_pins; i++) {
|
|
|
|
if (info->pin_config[i].func == func &&
|
|
|
|
info->pin_config[i].chan == chan) {
|
|
|
|
pin1 = &info->pin_config[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pin1 && i == pin)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Check the desired function and channel. */
|
|
|
|
switch (func) {
|
|
|
|
case PTP_PF_NONE:
|
|
|
|
break;
|
|
|
|
case PTP_PF_EXTTS:
|
|
|
|
if (chan >= info->n_ext_ts)
|
|
|
|
return -EINVAL;
|
|
|
|
break;
|
|
|
|
case PTP_PF_PEROUT:
|
|
|
|
if (chan >= info->n_per_out)
|
|
|
|
return -EINVAL;
|
|
|
|
break;
|
|
|
|
case PTP_PF_PHYSYNC:
|
2014-06-27 18:05:33 +08:00
|
|
|
if (chan != 0)
|
|
|
|
return -EINVAL;
|
2018-07-18 09:17:33 +08:00
|
|
|
break;
|
2014-03-21 05:21:52 +08:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->verify(info, pin, func, chan)) {
|
|
|
|
pr_err("driver cannot use function %u on pin %u\n", func, chan);
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable whatever function was previously assigned. */
|
|
|
|
if (pin1) {
|
|
|
|
ptp_disable_pinfunc(info, func, chan);
|
|
|
|
pin1->func = PTP_PF_NONE;
|
|
|
|
pin1->chan = 0;
|
|
|
|
}
|
|
|
|
ptp_disable_pinfunc(info, pin2->func, pin2->chan);
|
|
|
|
pin2->func = func;
|
|
|
|
pin2->chan = chan;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-22 18:03:08 +08:00
|
|
|
int ptp_open(struct posix_clock *pc, fmode_t fmode)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
|
2018-11-09 18:14:44 +08:00
|
|
|
struct ptp_sys_offset_extended *extoff = NULL;
|
2018-11-09 18:14:42 +08:00
|
|
|
struct ptp_sys_offset_precise precise_offset;
|
|
|
|
struct system_device_crosststamp xtstamp;
|
2011-04-22 18:03:08 +08:00
|
|
|
struct ptp_clock_info *ops = ptp->info;
|
2018-11-09 18:14:42 +08:00
|
|
|
struct ptp_sys_offset *sysoff = NULL;
|
2018-11-09 18:14:44 +08:00
|
|
|
struct ptp_system_timestamp sts;
|
2018-11-09 18:14:42 +08:00
|
|
|
struct ptp_clock_request req;
|
|
|
|
struct ptp_clock_caps caps;
|
2012-10-31 14:19:07 +08:00
|
|
|
struct ptp_clock_time *pct;
|
2018-11-09 18:14:42 +08:00
|
|
|
unsigned int i, pin_index;
|
|
|
|
struct ptp_pin_desc pd;
|
2015-03-30 05:11:52 +08:00
|
|
|
struct timespec64 ts;
|
2011-04-22 18:03:08 +08:00
|
|
|
int enable, err = 0;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
|
|
|
|
case PTP_CLOCK_GETCAPS:
|
2019-09-11 14:16:21 +08:00
|
|
|
case PTP_CLOCK_GETCAPS2:
|
2011-04-22 18:03:08 +08:00
|
|
|
memset(&caps, 0, sizeof(caps));
|
2019-09-11 14:16:21 +08:00
|
|
|
|
2011-04-22 18:03:08 +08:00
|
|
|
caps.max_adj = ptp->info->max_adj;
|
|
|
|
caps.n_alarm = ptp->info->n_alarm;
|
|
|
|
caps.n_ext_ts = ptp->info->n_ext_ts;
|
|
|
|
caps.n_per_out = ptp->info->n_per_out;
|
|
|
|
caps.pps = ptp->info->pps;
|
2014-03-21 05:21:52 +08:00
|
|
|
caps.n_pins = ptp->info->n_pins;
|
2016-02-22 19:15:25 +08:00
|
|
|
caps.cross_timestamping = ptp->info->getcrosststamp != NULL;
|
2020-05-02 11:35:37 +08:00
|
|
|
caps.adjust_phase = ptp->info->adjphase != NULL;
|
2011-05-30 03:53:12 +08:00
|
|
|
if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
|
|
|
|
err = -EFAULT;
|
2011-04-22 18:03:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PTP_EXTTS_REQUEST:
|
2019-09-11 14:16:21 +08:00
|
|
|
case PTP_EXTTS_REQUEST2:
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
2011-04-22 18:03:08 +08:00
|
|
|
if (copy_from_user(&req.extts, (void __user *)arg,
|
|
|
|
sizeof(req.extts))) {
|
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-15 02:44:55 +08:00
|
|
|
if (cmd == PTP_EXTTS_REQUEST2) {
|
2019-11-15 02:45:02 +08:00
|
|
|
/* Tell the drivers to check the flags carefully. */
|
|
|
|
req.extts.flags |= PTP_STRICT_FLAGS;
|
2019-11-15 02:44:55 +08:00
|
|
|
/* Make sure no reserved bit is set. */
|
|
|
|
if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
|
|
|
|
req.extts.rsv[0] || req.extts.rsv[1]) {
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Ensure one of the rising/falling edge bits is set. */
|
|
|
|
if ((req.extts.flags & PTP_ENABLE_FEATURE) &&
|
|
|
|
(req.extts.flags & PTP_EXTTS_EDGES) == 0) {
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2019-09-11 14:16:21 +08:00
|
|
|
} else if (cmd == PTP_EXTTS_REQUEST) {
|
ptp: correctly disable flags on old ioctls
Commit 415606588c61 ("PTP: introduce new versions of IOCTLs",
2019-09-13) introduced new versions of the PTP ioctls which actually
validate that the flags are acceptable values.
As part of this, it cleared the flags value using a bitwise
and+negation, in an attempt to prevent the old ioctl from accidentally
enabling new features.
This is incorrect for a couple of reasons. First, it results in
accidentally preventing previously working flags on the request ioctl.
By clearing the "valid" flags, we now no longer allow setting the
enable, rising edge, or falling edge flags.
Second, if we add new additional flags in the future, they must not be
set by the old ioctl. (Since the flag wasn't checked before, we could
potentially break userspace programs which sent garbage flag data.
The correct way to resolve this is to check for and clear all but the
originally valid flags.
Create defines indicating which flags are correctly checked and
interpreted by the original ioctls. Use these to clear any bits which
will not be correctly interpreted by the original ioctls.
In the future, new flags must be added to the VALID_FLAGS macros, but
*not* to the V1_VALID_FLAGS macros. In this way, new features may be
exposed over the v2 ioctls, but without breaking previous userspace
which happened to not clear the flags value properly. The old ioctl will
continue to behave the same way, while the new ioctl gains the benefit
of using the flags fields.
Cc: Richard Cochran <richardcochran@gmail.com>
Cc: Felipe Balbi <felipe.balbi@linux.intel.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Christopher Hall <christopher.s.hall@intel.com>
Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
Acked-by: Richard Cochran <richardcochran@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-09-26 10:28:19 +08:00
|
|
|
req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
|
2019-09-11 14:16:21 +08:00
|
|
|
req.extts.rsv[0] = 0;
|
|
|
|
req.extts.rsv[1] = 0;
|
|
|
|
}
|
2011-04-22 18:03:08 +08:00
|
|
|
if (req.extts.index >= ops->n_ext_ts) {
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
req.type = PTP_CLK_REQ_EXTTS;
|
|
|
|
enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
|
2020-03-29 22:55:10 +08:00
|
|
|
if (mutex_lock_interruptible(&ptp->pincfg_mux))
|
|
|
|
return -ERESTARTSYS;
|
2011-04-22 18:03:08 +08:00
|
|
|
err = ops->enable(ops, &req, enable);
|
2020-03-29 22:55:10 +08:00
|
|
|
mutex_unlock(&ptp->pincfg_mux);
|
2011-04-22 18:03:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PTP_PEROUT_REQUEST:
|
2019-09-11 14:16:21 +08:00
|
|
|
case PTP_PEROUT_REQUEST2:
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
2011-04-22 18:03:08 +08:00
|
|
|
if (copy_from_user(&req.perout, (void __user *)arg,
|
|
|
|
sizeof(req.perout))) {
|
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
ptp: add ability to configure duty cycle for periodic output
There are external event timestampers (PHCs with support for
PTP_EXTTS_REQUEST) that timestamp both event edges.
When those edges are very close (such as in the case of a short pulse),
there is a chance that the collected timestamp might be of the rising,
or of the falling edge, we never know.
There are also PHCs capable of generating periodic output with a
configurable duty cycle. This is good news, because we can space the
rising and falling edge out enough in time, that the risks to overrun
the 1-entry timestamp FIFO of the extts PHC are lower (example: the
perout PHC can be configured for a period of 1 second, and an "on" time
of 0.5 seconds, resulting in a duty cycle of 50%).
A flag is introduced for signaling that an on time is present in the
perout request structure, for preserving compatibility. Logically
speaking, the duty cycle cannot exceed 100% and the PTP core checks for
this.
PHC drivers that don't support this flag emit a periodic output of an
unspecified duty cycle, same as before.
The duty cycle is encoded as an "on" time, similar to the "start" and
"period" times, and reuses the reserved space while preserving overall
binary layout.
Pahole reported before:
struct ptp_perout_request {
struct ptp_clock_time start; /* 0 16 */
struct ptp_clock_time period; /* 16 16 */
unsigned int index; /* 32 4 */
unsigned int flags; /* 36 4 */
unsigned int rsv[4]; /* 40 16 */
/* size: 56, cachelines: 1, members: 5 */
/* last cacheline: 56 bytes */
};
And now:
struct ptp_perout_request {
struct ptp_clock_time start; /* 0 16 */
struct ptp_clock_time period; /* 16 16 */
unsigned int index; /* 32 4 */
unsigned int flags; /* 36 4 */
union {
struct ptp_clock_time on; /* 40 16 */
unsigned int rsv[4]; /* 40 16 */
}; /* 40 16 */
/* size: 56, cachelines: 1, members: 5 */
/* last cacheline: 56 bytes */
};
Signed-off-by: Vladimir Oltean <olteanv@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2020-07-17 06:45:29 +08:00
|
|
|
if (cmd == PTP_PEROUT_REQUEST2) {
|
|
|
|
struct ptp_perout_request *perout = &req.perout;
|
|
|
|
|
|
|
|
if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) {
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* The "on" field has undefined meaning if
|
|
|
|
* PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat
|
|
|
|
* it as reserved, which must be set to zero.
|
|
|
|
*/
|
|
|
|
if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) &&
|
|
|
|
(perout->rsv[0] || perout->rsv[1] ||
|
|
|
|
perout->rsv[2] || perout->rsv[3])) {
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (perout->flags & PTP_PEROUT_DUTY_CYCLE) {
|
|
|
|
/* The duty cycle must be subunitary. */
|
|
|
|
if (perout->on.sec > perout->period.sec ||
|
|
|
|
(perout->on.sec == perout->period.sec &&
|
|
|
|
perout->on.nsec > perout->period.nsec)) {
|
|
|
|
err = -ERANGE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 08:10:47 +08:00
|
|
|
if (perout->flags & PTP_PEROUT_PHASE) {
|
|
|
|
/*
|
|
|
|
* The phase should be specified modulo the
|
|
|
|
* period, therefore anything equal or larger
|
|
|
|
* than 1 period is invalid.
|
|
|
|
*/
|
|
|
|
if (perout->phase.sec > perout->period.sec ||
|
|
|
|
(perout->phase.sec == perout->period.sec &&
|
|
|
|
perout->phase.nsec >= perout->period.nsec)) {
|
|
|
|
err = -ERANGE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-09-11 14:16:21 +08:00
|
|
|
} else if (cmd == PTP_PEROUT_REQUEST) {
|
ptp: correctly disable flags on old ioctls
Commit 415606588c61 ("PTP: introduce new versions of IOCTLs",
2019-09-13) introduced new versions of the PTP ioctls which actually
validate that the flags are acceptable values.
As part of this, it cleared the flags value using a bitwise
and+negation, in an attempt to prevent the old ioctl from accidentally
enabling new features.
This is incorrect for a couple of reasons. First, it results in
accidentally preventing previously working flags on the request ioctl.
By clearing the "valid" flags, we now no longer allow setting the
enable, rising edge, or falling edge flags.
Second, if we add new additional flags in the future, they must not be
set by the old ioctl. (Since the flag wasn't checked before, we could
potentially break userspace programs which sent garbage flag data.
The correct way to resolve this is to check for and clear all but the
originally valid flags.
Create defines indicating which flags are correctly checked and
interpreted by the original ioctls. Use these to clear any bits which
will not be correctly interpreted by the original ioctls.
In the future, new flags must be added to the VALID_FLAGS macros, but
*not* to the V1_VALID_FLAGS macros. In this way, new features may be
exposed over the v2 ioctls, but without breaking previous userspace
which happened to not clear the flags value properly. The old ioctl will
continue to behave the same way, while the new ioctl gains the benefit
of using the flags fields.
Cc: Richard Cochran <richardcochran@gmail.com>
Cc: Felipe Balbi <felipe.balbi@linux.intel.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Christopher Hall <christopher.s.hall@intel.com>
Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
Acked-by: Richard Cochran <richardcochran@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-09-26 10:28:19 +08:00
|
|
|
req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
|
2019-09-11 14:16:21 +08:00
|
|
|
req.perout.rsv[0] = 0;
|
|
|
|
req.perout.rsv[1] = 0;
|
|
|
|
req.perout.rsv[2] = 0;
|
|
|
|
req.perout.rsv[3] = 0;
|
|
|
|
}
|
2011-04-22 18:03:08 +08:00
|
|
|
if (req.perout.index >= ops->n_per_out) {
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
req.type = PTP_CLK_REQ_PEROUT;
|
|
|
|
enable = req.perout.period.sec || req.perout.period.nsec;
|
2020-03-29 22:55:10 +08:00
|
|
|
if (mutex_lock_interruptible(&ptp->pincfg_mux))
|
|
|
|
return -ERESTARTSYS;
|
2011-04-22 18:03:08 +08:00
|
|
|
err = ops->enable(ops, &req, enable);
|
2020-03-29 22:55:10 +08:00
|
|
|
mutex_unlock(&ptp->pincfg_mux);
|
2011-04-22 18:03:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PTP_ENABLE_PPS:
|
2019-09-11 14:16:21 +08:00
|
|
|
case PTP_ENABLE_PPS2:
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
2011-04-22 18:03:08 +08:00
|
|
|
if (!capable(CAP_SYS_TIME))
|
|
|
|
return -EPERM;
|
|
|
|
req.type = PTP_CLK_REQ_PPS;
|
|
|
|
enable = arg ? 1 : 0;
|
2020-03-29 22:55:10 +08:00
|
|
|
if (mutex_lock_interruptible(&ptp->pincfg_mux))
|
|
|
|
return -ERESTARTSYS;
|
2011-04-22 18:03:08 +08:00
|
|
|
err = ops->enable(ops, &req, enable);
|
2020-03-29 22:55:10 +08:00
|
|
|
mutex_unlock(&ptp->pincfg_mux);
|
2011-04-22 18:03:08 +08:00
|
|
|
break;
|
|
|
|
|
2016-02-22 19:15:25 +08:00
|
|
|
case PTP_SYS_OFFSET_PRECISE:
|
2019-09-11 14:16:21 +08:00
|
|
|
case PTP_SYS_OFFSET_PRECISE2:
|
2016-02-22 19:15:25 +08:00
|
|
|
if (!ptp->info->getcrosststamp) {
|
|
|
|
err = -EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
|
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
|
2016-10-11 21:02:47 +08:00
|
|
|
memset(&precise_offset, 0, sizeof(precise_offset));
|
2016-02-22 19:15:25 +08:00
|
|
|
ts = ktime_to_timespec64(xtstamp.device);
|
|
|
|
precise_offset.device.sec = ts.tv_sec;
|
|
|
|
precise_offset.device.nsec = ts.tv_nsec;
|
|
|
|
ts = ktime_to_timespec64(xtstamp.sys_realtime);
|
|
|
|
precise_offset.sys_realtime.sec = ts.tv_sec;
|
|
|
|
precise_offset.sys_realtime.nsec = ts.tv_nsec;
|
|
|
|
ts = ktime_to_timespec64(xtstamp.sys_monoraw);
|
|
|
|
precise_offset.sys_monoraw.sec = ts.tv_sec;
|
|
|
|
precise_offset.sys_monoraw.nsec = ts.tv_nsec;
|
|
|
|
if (copy_to_user((void __user *)arg, &precise_offset,
|
|
|
|
sizeof(precise_offset)))
|
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
|
2018-11-09 18:14:44 +08:00
|
|
|
case PTP_SYS_OFFSET_EXTENDED:
|
2019-09-11 14:16:21 +08:00
|
|
|
case PTP_SYS_OFFSET_EXTENDED2:
|
2018-11-09 18:14:44 +08:00
|
|
|
if (!ptp->info->gettimex64) {
|
|
|
|
err = -EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extoff = memdup_user((void __user *)arg, sizeof(*extoff));
|
|
|
|
if (IS_ERR(extoff)) {
|
|
|
|
err = PTR_ERR(extoff);
|
|
|
|
extoff = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2019-01-07 23:22:29 +08:00
|
|
|
if (extoff->n_samples > PTP_MAX_SAMPLES
|
|
|
|
|| extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
|
2018-11-09 18:14:44 +08:00
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (i = 0; i < extoff->n_samples; i++) {
|
|
|
|
err = ptp->info->gettimex64(ptp->info, &ts, &sts);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
|
|
|
|
extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
|
|
|
|
extoff->ts[i][1].sec = ts.tv_sec;
|
|
|
|
extoff->ts[i][1].nsec = ts.tv_nsec;
|
|
|
|
extoff->ts[i][2].sec = sts.post_ts.tv_sec;
|
|
|
|
extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
|
|
|
|
}
|
|
|
|
if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
|
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
|
2012-10-31 14:19:07 +08:00
|
|
|
case PTP_SYS_OFFSET:
|
2019-09-11 14:16:21 +08:00
|
|
|
case PTP_SYS_OFFSET2:
|
2016-05-20 20:21:02 +08:00
|
|
|
sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
|
|
|
|
if (IS_ERR(sysoff)) {
|
|
|
|
err = PTR_ERR(sysoff);
|
2016-05-26 14:46:22 +08:00
|
|
|
sysoff = NULL;
|
2012-10-31 14:19:07 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-26 09:44:35 +08:00
|
|
|
if (sysoff->n_samples > PTP_MAX_SAMPLES) {
|
2012-10-31 14:19:07 +08:00
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-26 09:44:35 +08:00
|
|
|
pct = &sysoff->ts[0];
|
|
|
|
for (i = 0; i < sysoff->n_samples; i++) {
|
2018-06-18 22:20:39 +08:00
|
|
|
ktime_get_real_ts64(&ts);
|
2012-10-31 14:19:07 +08:00
|
|
|
pct->sec = ts.tv_sec;
|
|
|
|
pct->nsec = ts.tv_nsec;
|
|
|
|
pct++;
|
2018-11-09 18:14:45 +08:00
|
|
|
if (ops->gettimex64)
|
|
|
|
err = ops->gettimex64(ops, &ts, NULL);
|
|
|
|
else
|
|
|
|
err = ops->gettime64(ops, &ts);
|
2018-11-09 18:14:43 +08:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2012-10-31 14:19:07 +08:00
|
|
|
pct->sec = ts.tv_sec;
|
|
|
|
pct->nsec = ts.tv_nsec;
|
|
|
|
pct++;
|
|
|
|
}
|
2018-06-18 22:20:39 +08:00
|
|
|
ktime_get_real_ts64(&ts);
|
2012-10-31 14:19:07 +08:00
|
|
|
pct->sec = ts.tv_sec;
|
|
|
|
pct->nsec = ts.tv_nsec;
|
2012-11-26 09:44:35 +08:00
|
|
|
if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
|
2012-10-31 14:19:07 +08:00
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
|
2014-03-21 05:21:52 +08:00
|
|
|
case PTP_PIN_GETFUNC:
|
2019-09-11 14:16:21 +08:00
|
|
|
case PTP_PIN_GETFUNC2:
|
2014-03-21 05:21:52 +08:00
|
|
|
if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
|
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
2019-09-11 14:16:21 +08:00
|
|
|
if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
|
|
|
|
|| pd.rsv[3] || pd.rsv[4])
|
|
|
|
&& cmd == PTP_PIN_GETFUNC2) {
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
} else if (cmd == PTP_PIN_GETFUNC) {
|
|
|
|
pd.rsv[0] = 0;
|
|
|
|
pd.rsv[1] = 0;
|
|
|
|
pd.rsv[2] = 0;
|
|
|
|
pd.rsv[3] = 0;
|
|
|
|
pd.rsv[4] = 0;
|
|
|
|
}
|
2014-03-21 05:21:52 +08:00
|
|
|
pin_index = pd.index;
|
|
|
|
if (pin_index >= ops->n_pins) {
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2018-10-16 21:06:41 +08:00
|
|
|
pin_index = array_index_nospec(pin_index, ops->n_pins);
|
2014-03-21 05:21:52 +08:00
|
|
|
if (mutex_lock_interruptible(&ptp->pincfg_mux))
|
|
|
|
return -ERESTARTSYS;
|
|
|
|
pd = ops->pin_config[pin_index];
|
|
|
|
mutex_unlock(&ptp->pincfg_mux);
|
|
|
|
if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
|
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PTP_PIN_SETFUNC:
|
2019-09-11 14:16:21 +08:00
|
|
|
case PTP_PIN_SETFUNC2:
|
2014-03-21 05:21:52 +08:00
|
|
|
if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
|
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
2019-09-11 14:16:21 +08:00
|
|
|
if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
|
|
|
|
|| pd.rsv[3] || pd.rsv[4])
|
|
|
|
&& cmd == PTP_PIN_SETFUNC2) {
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
} else if (cmd == PTP_PIN_SETFUNC) {
|
|
|
|
pd.rsv[0] = 0;
|
|
|
|
pd.rsv[1] = 0;
|
|
|
|
pd.rsv[2] = 0;
|
|
|
|
pd.rsv[3] = 0;
|
|
|
|
pd.rsv[4] = 0;
|
|
|
|
}
|
2014-03-21 05:21:52 +08:00
|
|
|
pin_index = pd.index;
|
|
|
|
if (pin_index >= ops->n_pins) {
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2018-10-16 21:06:41 +08:00
|
|
|
pin_index = array_index_nospec(pin_index, ops->n_pins);
|
2014-03-21 05:21:52 +08:00
|
|
|
if (mutex_lock_interruptible(&ptp->pincfg_mux))
|
|
|
|
return -ERESTARTSYS;
|
|
|
|
err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
|
|
|
|
mutex_unlock(&ptp->pincfg_mux);
|
|
|
|
break;
|
|
|
|
|
2011-04-22 18:03:08 +08:00
|
|
|
default:
|
|
|
|
err = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-26 09:44:35 +08:00
|
|
|
|
2018-11-09 18:14:43 +08:00
|
|
|
out:
|
2018-11-09 18:14:44 +08:00
|
|
|
kfree(extoff);
|
2012-11-26 09:44:35 +08:00
|
|
|
kfree(sysoff);
|
2011-04-22 18:03:08 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-07-03 18:39:46 +08:00
|
|
|
__poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
|
2011-04-22 18:03:08 +08:00
|
|
|
{
|
|
|
|
struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
|
|
|
|
|
|
|
|
poll_wait(fp, &ptp->tsev_wq, wait);
|
|
|
|
|
2018-02-12 06:34:03 +08:00
|
|
|
return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0;
|
2011-04-22 18:03:08 +08:00
|
|
|
}
|
|
|
|
|
2012-11-26 09:44:34 +08:00
|
|
|
#define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
|
|
|
|
|
2011-04-22 18:03:08 +08:00
|
|
|
ssize_t ptp_read(struct posix_clock *pc,
|
|
|
|
uint rdflags, char __user *buf, size_t cnt)
|
|
|
|
{
|
|
|
|
struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
|
|
|
|
struct timestamp_event_queue *queue = &ptp->tsevq;
|
2012-11-26 09:44:34 +08:00
|
|
|
struct ptp_extts_event *event;
|
2011-04-22 18:03:08 +08:00
|
|
|
unsigned long flags;
|
|
|
|
size_t qcnt, i;
|
2012-11-26 09:44:34 +08:00
|
|
|
int result;
|
2011-04-22 18:03:08 +08:00
|
|
|
|
|
|
|
if (cnt % sizeof(struct ptp_extts_event) != 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-11-26 09:44:34 +08:00
|
|
|
if (cnt > EXTTS_BUFSIZE)
|
|
|
|
cnt = EXTTS_BUFSIZE;
|
2011-04-22 18:03:08 +08:00
|
|
|
|
|
|
|
cnt = cnt / sizeof(struct ptp_extts_event);
|
|
|
|
|
|
|
|
if (mutex_lock_interruptible(&ptp->tsevq_mux))
|
|
|
|
return -ERESTARTSYS;
|
|
|
|
|
|
|
|
if (wait_event_interruptible(ptp->tsev_wq,
|
|
|
|
ptp->defunct || queue_cnt(queue))) {
|
|
|
|
mutex_unlock(&ptp->tsevq_mux);
|
|
|
|
return -ERESTARTSYS;
|
|
|
|
}
|
|
|
|
|
2011-05-30 03:54:07 +08:00
|
|
|
if (ptp->defunct) {
|
|
|
|
mutex_unlock(&ptp->tsevq_mux);
|
2011-04-22 18:03:08 +08:00
|
|
|
return -ENODEV;
|
2011-05-30 03:54:07 +08:00
|
|
|
}
|
2011-04-22 18:03:08 +08:00
|
|
|
|
2012-11-26 09:44:34 +08:00
|
|
|
event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
|
|
|
|
if (!event) {
|
|
|
|
mutex_unlock(&ptp->tsevq_mux);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2011-04-22 18:03:08 +08:00
|
|
|
spin_lock_irqsave(&queue->lock, flags);
|
|
|
|
|
|
|
|
qcnt = queue_cnt(queue);
|
|
|
|
|
|
|
|
if (cnt > qcnt)
|
|
|
|
cnt = qcnt;
|
|
|
|
|
|
|
|
for (i = 0; i < cnt; i++) {
|
|
|
|
event[i] = queue->buf[queue->head];
|
|
|
|
queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&queue->lock, flags);
|
|
|
|
|
|
|
|
cnt = cnt * sizeof(struct ptp_extts_event);
|
|
|
|
|
|
|
|
mutex_unlock(&ptp->tsevq_mux);
|
|
|
|
|
2012-11-26 09:44:34 +08:00
|
|
|
result = cnt;
|
2011-05-30 03:54:07 +08:00
|
|
|
if (copy_to_user(buf, event, cnt))
|
2012-11-26 09:44:34 +08:00
|
|
|
result = -EFAULT;
|
2011-04-22 18:03:08 +08:00
|
|
|
|
2012-11-26 09:44:34 +08:00
|
|
|
kfree(event);
|
|
|
|
return result;
|
2011-04-22 18:03:08 +08:00
|
|
|
}
|