1 /* 2 * PTP 1588 clock support - character device implementation. 3 * 4 * Copyright (C) 2010 OMICRON electronics GmbH 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 */ 20 #include <linux/module.h> 21 #include <linux/posix-clock.h> 22 #include <linux/poll.h> 23 #include <linux/sched.h> 24 25 #include "ptp_private.h" 26 27 int ptp_open(struct posix_clock *pc, fmode_t fmode) 28 { 29 return 0; 30 } 31 32 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg) 33 { 34 struct ptp_clock_caps caps; 35 struct ptp_clock_request req; 36 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); 37 struct ptp_clock_info *ops = ptp->info; 38 int enable, err = 0; 39 40 switch (cmd) { 41 42 case PTP_CLOCK_GETCAPS: 43 memset(&caps, 0, sizeof(caps)); 44 caps.max_adj = ptp->info->max_adj; 45 caps.n_alarm = ptp->info->n_alarm; 46 caps.n_ext_ts = ptp->info->n_ext_ts; 47 caps.n_per_out = ptp->info->n_per_out; 48 caps.pps = ptp->info->pps; 49 err = copy_to_user((void __user *)arg, &caps, sizeof(caps)); 50 break; 51 52 case PTP_EXTTS_REQUEST: 53 if (copy_from_user(&req.extts, (void __user *)arg, 54 sizeof(req.extts))) { 55 err = -EFAULT; 56 break; 57 } 58 if (req.extts.index >= ops->n_ext_ts) { 59 err = -EINVAL; 60 break; 61 } 62 req.type = PTP_CLK_REQ_EXTTS; 63 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0; 64 err = ops->enable(ops, &req, enable); 65 break; 66 67 case PTP_PEROUT_REQUEST: 68 if (copy_from_user(&req.perout, (void __user *)arg, 69 sizeof(req.perout))) { 70 err = -EFAULT; 71 break; 72 } 73 if (req.perout.index >= ops->n_per_out) { 74 err = -EINVAL; 75 break; 76 } 77 req.type = PTP_CLK_REQ_PEROUT; 78 enable = req.perout.period.sec || req.perout.period.nsec; 79 err = ops->enable(ops, &req, enable); 80 break; 81 82 case PTP_ENABLE_PPS: 83 if (!capable(CAP_SYS_TIME)) 84 return -EPERM; 85 req.type = PTP_CLK_REQ_PPS; 86 enable = arg ? 1 : 0; 87 err = ops->enable(ops, &req, enable); 88 break; 89 90 default: 91 err = -ENOTTY; 92 break; 93 } 94 return err; 95 } 96 97 unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait) 98 { 99 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); 100 101 poll_wait(fp, &ptp->tsev_wq, wait); 102 103 return queue_cnt(&ptp->tsevq) ? POLLIN : 0; 104 } 105 106 ssize_t ptp_read(struct posix_clock *pc, 107 uint rdflags, char __user *buf, size_t cnt) 108 { 109 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); 110 struct timestamp_event_queue *queue = &ptp->tsevq; 111 struct ptp_extts_event event[PTP_BUF_TIMESTAMPS]; 112 unsigned long flags; 113 size_t qcnt, i; 114 115 if (cnt % sizeof(struct ptp_extts_event) != 0) 116 return -EINVAL; 117 118 if (cnt > sizeof(event)) 119 cnt = sizeof(event); 120 121 cnt = cnt / sizeof(struct ptp_extts_event); 122 123 if (mutex_lock_interruptible(&ptp->tsevq_mux)) 124 return -ERESTARTSYS; 125 126 if (wait_event_interruptible(ptp->tsev_wq, 127 ptp->defunct || queue_cnt(queue))) { 128 mutex_unlock(&ptp->tsevq_mux); 129 return -ERESTARTSYS; 130 } 131 132 if (ptp->defunct) 133 return -ENODEV; 134 135 spin_lock_irqsave(&queue->lock, flags); 136 137 qcnt = queue_cnt(queue); 138 139 if (cnt > qcnt) 140 cnt = qcnt; 141 142 for (i = 0; i < cnt; i++) { 143 event[i] = queue->buf[queue->head]; 144 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS; 145 } 146 147 spin_unlock_irqrestore(&queue->lock, flags); 148 149 cnt = cnt * sizeof(struct ptp_extts_event); 150 151 mutex_unlock(&ptp->tsevq_mux); 152 153 if (copy_to_user(buf, event, cnt)) { 154 mutex_unlock(&ptp->tsevq_mux); 155 return -EFAULT; 156 } 157 158 return cnt; 159 } 160