Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * PTP 1588 clock support - character device implementation.
0004  *
0005  * Copyright (C) 2010 OMICRON electronics GmbH
0006  */
0007 #include <linux/module.h>
0008 #include <linux/posix-clock.h>
0009 #include <linux/poll.h>
0010 #include <linux/sched.h>
0011 #include <linux/slab.h>
0012 #include <linux/timekeeping.h>
0013 
0014 #include <linux/nospec.h>
0015 
0016 #include "ptp_private.h"
0017 
0018 static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
0019                    enum ptp_pin_function func, unsigned int chan)
0020 {
0021     struct ptp_clock_request rq;
0022     int err = 0;
0023 
0024     memset(&rq, 0, sizeof(rq));
0025 
0026     switch (func) {
0027     case PTP_PF_NONE:
0028         break;
0029     case PTP_PF_EXTTS:
0030         rq.type = PTP_CLK_REQ_EXTTS;
0031         rq.extts.index = chan;
0032         err = ops->enable(ops, &rq, 0);
0033         break;
0034     case PTP_PF_PEROUT:
0035         rq.type = PTP_CLK_REQ_PEROUT;
0036         rq.perout.index = chan;
0037         err = ops->enable(ops, &rq, 0);
0038         break;
0039     case PTP_PF_PHYSYNC:
0040         break;
0041     default:
0042         return -EINVAL;
0043     }
0044 
0045     return err;
0046 }
0047 
0048 int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
0049             enum ptp_pin_function func, unsigned int chan)
0050 {
0051     struct ptp_clock_info *info = ptp->info;
0052     struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin];
0053     unsigned int i;
0054 
0055     /* Check to see if any other pin previously had this function. */
0056     for (i = 0; i < info->n_pins; i++) {
0057         if (info->pin_config[i].func == func &&
0058             info->pin_config[i].chan == chan) {
0059             pin1 = &info->pin_config[i];
0060             break;
0061         }
0062     }
0063     if (pin1 && i == pin)
0064         return 0;
0065 
0066     /* Check the desired function and channel. */
0067     switch (func) {
0068     case PTP_PF_NONE:
0069         break;
0070     case PTP_PF_EXTTS:
0071         if (chan >= info->n_ext_ts)
0072             return -EINVAL;
0073         break;
0074     case PTP_PF_PEROUT:
0075         if (chan >= info->n_per_out)
0076             return -EINVAL;
0077         break;
0078     case PTP_PF_PHYSYNC:
0079         if (chan != 0)
0080             return -EINVAL;
0081         break;
0082     default:
0083         return -EINVAL;
0084     }
0085 
0086     if (info->verify(info, pin, func, chan)) {
0087         pr_err("driver cannot use function %u on pin %u\n", func, chan);
0088         return -EOPNOTSUPP;
0089     }
0090 
0091     /* Disable whatever function was previously assigned. */
0092     if (pin1) {
0093         ptp_disable_pinfunc(info, func, chan);
0094         pin1->func = PTP_PF_NONE;
0095         pin1->chan = 0;
0096     }
0097     ptp_disable_pinfunc(info, pin2->func, pin2->chan);
0098     pin2->func = func;
0099     pin2->chan = chan;
0100 
0101     return 0;
0102 }
0103 
0104 int ptp_open(struct posix_clock *pc, fmode_t fmode)
0105 {
0106     return 0;
0107 }
0108 
0109 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
0110 {
0111     struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
0112     struct ptp_sys_offset_extended *extoff = NULL;
0113     struct ptp_sys_offset_precise precise_offset;
0114     struct system_device_crosststamp xtstamp;
0115     struct ptp_clock_info *ops = ptp->info;
0116     struct ptp_sys_offset *sysoff = NULL;
0117     struct ptp_system_timestamp sts;
0118     struct ptp_clock_request req;
0119     struct ptp_clock_caps caps;
0120     struct ptp_clock_time *pct;
0121     unsigned int i, pin_index;
0122     struct ptp_pin_desc pd;
0123     struct timespec64 ts;
0124     int enable, err = 0;
0125 
0126     switch (cmd) {
0127 
0128     case PTP_CLOCK_GETCAPS:
0129     case PTP_CLOCK_GETCAPS2:
0130         memset(&caps, 0, sizeof(caps));
0131 
0132         caps.max_adj = ptp->info->max_adj;
0133         caps.n_alarm = ptp->info->n_alarm;
0134         caps.n_ext_ts = ptp->info->n_ext_ts;
0135         caps.n_per_out = ptp->info->n_per_out;
0136         caps.pps = ptp->info->pps;
0137         caps.n_pins = ptp->info->n_pins;
0138         caps.cross_timestamping = ptp->info->getcrosststamp != NULL;
0139         caps.adjust_phase = ptp->info->adjphase != NULL;
0140         if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
0141             err = -EFAULT;
0142         break;
0143 
0144     case PTP_EXTTS_REQUEST:
0145     case PTP_EXTTS_REQUEST2:
0146         memset(&req, 0, sizeof(req));
0147 
0148         if (copy_from_user(&req.extts, (void __user *)arg,
0149                    sizeof(req.extts))) {
0150             err = -EFAULT;
0151             break;
0152         }
0153         if (cmd == PTP_EXTTS_REQUEST2) {
0154             /* Tell the drivers to check the flags carefully. */
0155             req.extts.flags |= PTP_STRICT_FLAGS;
0156             /* Make sure no reserved bit is set. */
0157             if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
0158                 req.extts.rsv[0] || req.extts.rsv[1]) {
0159                 err = -EINVAL;
0160                 break;
0161             }
0162             /* Ensure one of the rising/falling edge bits is set. */
0163             if ((req.extts.flags & PTP_ENABLE_FEATURE) &&
0164                 (req.extts.flags & PTP_EXTTS_EDGES) == 0) {
0165                 err = -EINVAL;
0166                 break;
0167             }
0168         } else if (cmd == PTP_EXTTS_REQUEST) {
0169             req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
0170             req.extts.rsv[0] = 0;
0171             req.extts.rsv[1] = 0;
0172         }
0173         if (req.extts.index >= ops->n_ext_ts) {
0174             err = -EINVAL;
0175             break;
0176         }
0177         req.type = PTP_CLK_REQ_EXTTS;
0178         enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
0179         if (mutex_lock_interruptible(&ptp->pincfg_mux))
0180             return -ERESTARTSYS;
0181         err = ops->enable(ops, &req, enable);
0182         mutex_unlock(&ptp->pincfg_mux);
0183         break;
0184 
0185     case PTP_PEROUT_REQUEST:
0186     case PTP_PEROUT_REQUEST2:
0187         memset(&req, 0, sizeof(req));
0188 
0189         if (copy_from_user(&req.perout, (void __user *)arg,
0190                    sizeof(req.perout))) {
0191             err = -EFAULT;
0192             break;
0193         }
0194         if (cmd == PTP_PEROUT_REQUEST2) {
0195             struct ptp_perout_request *perout = &req.perout;
0196 
0197             if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) {
0198                 err = -EINVAL;
0199                 break;
0200             }
0201             /*
0202              * The "on" field has undefined meaning if
0203              * PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat
0204              * it as reserved, which must be set to zero.
0205              */
0206             if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) &&
0207                 (perout->rsv[0] || perout->rsv[1] ||
0208                  perout->rsv[2] || perout->rsv[3])) {
0209                 err = -EINVAL;
0210                 break;
0211             }
0212             if (perout->flags & PTP_PEROUT_DUTY_CYCLE) {
0213                 /* The duty cycle must be subunitary. */
0214                 if (perout->on.sec > perout->period.sec ||
0215                     (perout->on.sec == perout->period.sec &&
0216                      perout->on.nsec > perout->period.nsec)) {
0217                     err = -ERANGE;
0218                     break;
0219                 }
0220             }
0221             if (perout->flags & PTP_PEROUT_PHASE) {
0222                 /*
0223                  * The phase should be specified modulo the
0224                  * period, therefore anything equal or larger
0225                  * than 1 period is invalid.
0226                  */
0227                 if (perout->phase.sec > perout->period.sec ||
0228                     (perout->phase.sec == perout->period.sec &&
0229                      perout->phase.nsec >= perout->period.nsec)) {
0230                     err = -ERANGE;
0231                     break;
0232                 }
0233             }
0234         } else if (cmd == PTP_PEROUT_REQUEST) {
0235             req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
0236             req.perout.rsv[0] = 0;
0237             req.perout.rsv[1] = 0;
0238             req.perout.rsv[2] = 0;
0239             req.perout.rsv[3] = 0;
0240         }
0241         if (req.perout.index >= ops->n_per_out) {
0242             err = -EINVAL;
0243             break;
0244         }
0245         req.type = PTP_CLK_REQ_PEROUT;
0246         enable = req.perout.period.sec || req.perout.period.nsec;
0247         if (mutex_lock_interruptible(&ptp->pincfg_mux))
0248             return -ERESTARTSYS;
0249         err = ops->enable(ops, &req, enable);
0250         mutex_unlock(&ptp->pincfg_mux);
0251         break;
0252 
0253     case PTP_ENABLE_PPS:
0254     case PTP_ENABLE_PPS2:
0255         memset(&req, 0, sizeof(req));
0256 
0257         if (!capable(CAP_SYS_TIME))
0258             return -EPERM;
0259         req.type = PTP_CLK_REQ_PPS;
0260         enable = arg ? 1 : 0;
0261         if (mutex_lock_interruptible(&ptp->pincfg_mux))
0262             return -ERESTARTSYS;
0263         err = ops->enable(ops, &req, enable);
0264         mutex_unlock(&ptp->pincfg_mux);
0265         break;
0266 
0267     case PTP_SYS_OFFSET_PRECISE:
0268     case PTP_SYS_OFFSET_PRECISE2:
0269         if (!ptp->info->getcrosststamp) {
0270             err = -EOPNOTSUPP;
0271             break;
0272         }
0273         err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
0274         if (err)
0275             break;
0276 
0277         memset(&precise_offset, 0, sizeof(precise_offset));
0278         ts = ktime_to_timespec64(xtstamp.device);
0279         precise_offset.device.sec = ts.tv_sec;
0280         precise_offset.device.nsec = ts.tv_nsec;
0281         ts = ktime_to_timespec64(xtstamp.sys_realtime);
0282         precise_offset.sys_realtime.sec = ts.tv_sec;
0283         precise_offset.sys_realtime.nsec = ts.tv_nsec;
0284         ts = ktime_to_timespec64(xtstamp.sys_monoraw);
0285         precise_offset.sys_monoraw.sec = ts.tv_sec;
0286         precise_offset.sys_monoraw.nsec = ts.tv_nsec;
0287         if (copy_to_user((void __user *)arg, &precise_offset,
0288                  sizeof(precise_offset)))
0289             err = -EFAULT;
0290         break;
0291 
0292     case PTP_SYS_OFFSET_EXTENDED:
0293     case PTP_SYS_OFFSET_EXTENDED2:
0294         if (!ptp->info->gettimex64) {
0295             err = -EOPNOTSUPP;
0296             break;
0297         }
0298         extoff = memdup_user((void __user *)arg, sizeof(*extoff));
0299         if (IS_ERR(extoff)) {
0300             err = PTR_ERR(extoff);
0301             extoff = NULL;
0302             break;
0303         }
0304         if (extoff->n_samples > PTP_MAX_SAMPLES
0305             || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
0306             err = -EINVAL;
0307             break;
0308         }
0309         for (i = 0; i < extoff->n_samples; i++) {
0310             err = ptp->info->gettimex64(ptp->info, &ts, &sts);
0311             if (err)
0312                 goto out;
0313             extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
0314             extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
0315             extoff->ts[i][1].sec = ts.tv_sec;
0316             extoff->ts[i][1].nsec = ts.tv_nsec;
0317             extoff->ts[i][2].sec = sts.post_ts.tv_sec;
0318             extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
0319         }
0320         if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
0321             err = -EFAULT;
0322         break;
0323 
0324     case PTP_SYS_OFFSET:
0325     case PTP_SYS_OFFSET2:
0326         sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
0327         if (IS_ERR(sysoff)) {
0328             err = PTR_ERR(sysoff);
0329             sysoff = NULL;
0330             break;
0331         }
0332         if (sysoff->n_samples > PTP_MAX_SAMPLES) {
0333             err = -EINVAL;
0334             break;
0335         }
0336         pct = &sysoff->ts[0];
0337         for (i = 0; i < sysoff->n_samples; i++) {
0338             ktime_get_real_ts64(&ts);
0339             pct->sec = ts.tv_sec;
0340             pct->nsec = ts.tv_nsec;
0341             pct++;
0342             if (ops->gettimex64)
0343                 err = ops->gettimex64(ops, &ts, NULL);
0344             else
0345                 err = ops->gettime64(ops, &ts);
0346             if (err)
0347                 goto out;
0348             pct->sec = ts.tv_sec;
0349             pct->nsec = ts.tv_nsec;
0350             pct++;
0351         }
0352         ktime_get_real_ts64(&ts);
0353         pct->sec = ts.tv_sec;
0354         pct->nsec = ts.tv_nsec;
0355         if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
0356             err = -EFAULT;
0357         break;
0358 
0359     case PTP_PIN_GETFUNC:
0360     case PTP_PIN_GETFUNC2:
0361         if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
0362             err = -EFAULT;
0363             break;
0364         }
0365         if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
0366                 || pd.rsv[3] || pd.rsv[4])
0367             && cmd == PTP_PIN_GETFUNC2) {
0368             err = -EINVAL;
0369             break;
0370         } else if (cmd == PTP_PIN_GETFUNC) {
0371             pd.rsv[0] = 0;
0372             pd.rsv[1] = 0;
0373             pd.rsv[2] = 0;
0374             pd.rsv[3] = 0;
0375             pd.rsv[4] = 0;
0376         }
0377         pin_index = pd.index;
0378         if (pin_index >= ops->n_pins) {
0379             err = -EINVAL;
0380             break;
0381         }
0382         pin_index = array_index_nospec(pin_index, ops->n_pins);
0383         if (mutex_lock_interruptible(&ptp->pincfg_mux))
0384             return -ERESTARTSYS;
0385         pd = ops->pin_config[pin_index];
0386         mutex_unlock(&ptp->pincfg_mux);
0387         if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
0388             err = -EFAULT;
0389         break;
0390 
0391     case PTP_PIN_SETFUNC:
0392     case PTP_PIN_SETFUNC2:
0393         if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
0394             err = -EFAULT;
0395             break;
0396         }
0397         if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
0398                 || pd.rsv[3] || pd.rsv[4])
0399             && cmd == PTP_PIN_SETFUNC2) {
0400             err = -EINVAL;
0401             break;
0402         } else if (cmd == PTP_PIN_SETFUNC) {
0403             pd.rsv[0] = 0;
0404             pd.rsv[1] = 0;
0405             pd.rsv[2] = 0;
0406             pd.rsv[3] = 0;
0407             pd.rsv[4] = 0;
0408         }
0409         pin_index = pd.index;
0410         if (pin_index >= ops->n_pins) {
0411             err = -EINVAL;
0412             break;
0413         }
0414         pin_index = array_index_nospec(pin_index, ops->n_pins);
0415         if (mutex_lock_interruptible(&ptp->pincfg_mux))
0416             return -ERESTARTSYS;
0417         err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
0418         mutex_unlock(&ptp->pincfg_mux);
0419         break;
0420 
0421     default:
0422         err = -ENOTTY;
0423         break;
0424     }
0425 
0426 out:
0427     kfree(extoff);
0428     kfree(sysoff);
0429     return err;
0430 }
0431 
0432 __poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
0433 {
0434     struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
0435 
0436     poll_wait(fp, &ptp->tsev_wq, wait);
0437 
0438     return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0;
0439 }
0440 
0441 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
0442 
0443 ssize_t ptp_read(struct posix_clock *pc,
0444          uint rdflags, char __user *buf, size_t cnt)
0445 {
0446     struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
0447     struct timestamp_event_queue *queue = &ptp->tsevq;
0448     struct ptp_extts_event *event;
0449     unsigned long flags;
0450     size_t qcnt, i;
0451     int result;
0452 
0453     if (cnt % sizeof(struct ptp_extts_event) != 0)
0454         return -EINVAL;
0455 
0456     if (cnt > EXTTS_BUFSIZE)
0457         cnt = EXTTS_BUFSIZE;
0458 
0459     cnt = cnt / sizeof(struct ptp_extts_event);
0460 
0461     if (mutex_lock_interruptible(&ptp->tsevq_mux))
0462         return -ERESTARTSYS;
0463 
0464     if (wait_event_interruptible(ptp->tsev_wq,
0465                      ptp->defunct || queue_cnt(queue))) {
0466         mutex_unlock(&ptp->tsevq_mux);
0467         return -ERESTARTSYS;
0468     }
0469 
0470     if (ptp->defunct) {
0471         mutex_unlock(&ptp->tsevq_mux);
0472         return -ENODEV;
0473     }
0474 
0475     event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
0476     if (!event) {
0477         mutex_unlock(&ptp->tsevq_mux);
0478         return -ENOMEM;
0479     }
0480 
0481     spin_lock_irqsave(&queue->lock, flags);
0482 
0483     qcnt = queue_cnt(queue);
0484 
0485     if (cnt > qcnt)
0486         cnt = qcnt;
0487 
0488     for (i = 0; i < cnt; i++) {
0489         event[i] = queue->buf[queue->head];
0490         queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
0491     }
0492 
0493     spin_unlock_irqrestore(&queue->lock, flags);
0494 
0495     cnt = cnt * sizeof(struct ptp_extts_event);
0496 
0497     mutex_unlock(&ptp->tsevq_mux);
0498 
0499     result = cnt;
0500     if (copy_to_user(buf, event, cnt))
0501         result = -EFAULT;
0502 
0503     kfree(event);
0504     return result;
0505 }