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 #include <linux/slab.h> 25 26 #include "ptp_private.h" 27 28 static int ptp_disable_pinfunc(struct ptp_clock_info *ops, 29 enum ptp_pin_function func, unsigned int chan) 30 { 31 struct ptp_clock_request rq; 32 int err = 0; 33 34 memset(&rq, 0, sizeof(rq)); 35 36 switch (func) { 37 case PTP_PF_NONE: 38 break; 39 case PTP_PF_EXTTS: 40 rq.type = PTP_CLK_REQ_EXTTS; 41 rq.extts.index = chan; 42 err = ops->enable(ops, &rq, 0); 43 break; 44 case PTP_PF_PEROUT: 45 rq.type = PTP_CLK_REQ_PEROUT; 46 rq.perout.index = chan; 47 err = ops->enable(ops, &rq, 0); 48 break; 49 case PTP_PF_PHYSYNC: 50 break; 51 default: 52 return -EINVAL; 53 } 54 55 return err; 56 } 57 58 int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin, 59 enum ptp_pin_function func, unsigned int chan) 60 { 61 struct ptp_clock_info *info = ptp->info; 62 struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin]; 63 unsigned int i; 64 65 /* Check to see if any other pin previously had this function. */ 66 for (i = 0; i < info->n_pins; i++) { 67 if (info->pin_config[i].func == func && 68 info->pin_config[i].chan == chan) { 69 pin1 = &info->pin_config[i]; 70 break; 71 } 72 } 73 if (pin1 && i == pin) 74 return 0; 75 76 /* Check the desired function and channel. */ 77 switch (func) { 78 case PTP_PF_NONE: 79 break; 80 case PTP_PF_EXTTS: 81 if (chan >= info->n_ext_ts) 82 return -EINVAL; 83 break; 84 case PTP_PF_PEROUT: 85 if (chan >= info->n_per_out) 86 return -EINVAL; 87 break; 88 case PTP_PF_PHYSYNC: 89 pr_err("sorry, cannot reassign the calibration pin\n"); 90 return -EINVAL; 91 default: 92 return -EINVAL; 93 } 94 95 if (pin2->func == PTP_PF_PHYSYNC) { 96 pr_err("sorry, cannot reprogram the calibration pin\n"); 97 return -EINVAL; 98 } 99 100 if (info->verify(info, pin, func, chan)) { 101 pr_err("driver cannot use function %u on pin %u\n", func, chan); 102 return -EOPNOTSUPP; 103 } 104 105 /* Disable whatever function was previously assigned. */ 106 if (pin1) { 107 ptp_disable_pinfunc(info, func, chan); 108 pin1->func = PTP_PF_NONE; 109 pin1->chan = 0; 110 } 111 ptp_disable_pinfunc(info, pin2->func, pin2->chan); 112 pin2->func = func; 113 pin2->chan = chan; 114 115 return 0; 116 } 117 118 int ptp_open(struct posix_clock *pc, fmode_t fmode) 119 { 120 return 0; 121 } 122 123 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg) 124 { 125 struct ptp_clock_caps caps; 126 struct ptp_clock_request req; 127 struct ptp_sys_offset *sysoff = NULL; 128 struct ptp_pin_desc pd; 129 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); 130 struct ptp_clock_info *ops = ptp->info; 131 struct ptp_clock_time *pct; 132 struct timespec ts; 133 int enable, err = 0; 134 unsigned int i, pin_index; 135 136 switch (cmd) { 137 138 case PTP_CLOCK_GETCAPS: 139 memset(&caps, 0, sizeof(caps)); 140 caps.max_adj = ptp->info->max_adj; 141 caps.n_alarm = ptp->info->n_alarm; 142 caps.n_ext_ts = ptp->info->n_ext_ts; 143 caps.n_per_out = ptp->info->n_per_out; 144 caps.pps = ptp->info->pps; 145 caps.n_pins = ptp->info->n_pins; 146 if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) 147 err = -EFAULT; 148 break; 149 150 case PTP_EXTTS_REQUEST: 151 if (copy_from_user(&req.extts, (void __user *)arg, 152 sizeof(req.extts))) { 153 err = -EFAULT; 154 break; 155 } 156 if (req.extts.index >= ops->n_ext_ts) { 157 err = -EINVAL; 158 break; 159 } 160 req.type = PTP_CLK_REQ_EXTTS; 161 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0; 162 err = ops->enable(ops, &req, enable); 163 break; 164 165 case PTP_PEROUT_REQUEST: 166 if (copy_from_user(&req.perout, (void __user *)arg, 167 sizeof(req.perout))) { 168 err = -EFAULT; 169 break; 170 } 171 if (req.perout.index >= ops->n_per_out) { 172 err = -EINVAL; 173 break; 174 } 175 req.type = PTP_CLK_REQ_PEROUT; 176 enable = req.perout.period.sec || req.perout.period.nsec; 177 err = ops->enable(ops, &req, enable); 178 break; 179 180 case PTP_ENABLE_PPS: 181 if (!capable(CAP_SYS_TIME)) 182 return -EPERM; 183 req.type = PTP_CLK_REQ_PPS; 184 enable = arg ? 1 : 0; 185 err = ops->enable(ops, &req, enable); 186 break; 187 188 case PTP_SYS_OFFSET: 189 sysoff = kmalloc(sizeof(*sysoff), GFP_KERNEL); 190 if (!sysoff) { 191 err = -ENOMEM; 192 break; 193 } 194 if (copy_from_user(sysoff, (void __user *)arg, 195 sizeof(*sysoff))) { 196 err = -EFAULT; 197 break; 198 } 199 if (sysoff->n_samples > PTP_MAX_SAMPLES) { 200 err = -EINVAL; 201 break; 202 } 203 pct = &sysoff->ts[0]; 204 for (i = 0; i < sysoff->n_samples; i++) { 205 getnstimeofday(&ts); 206 pct->sec = ts.tv_sec; 207 pct->nsec = ts.tv_nsec; 208 pct++; 209 ptp->info->gettime(ptp->info, &ts); 210 pct->sec = ts.tv_sec; 211 pct->nsec = ts.tv_nsec; 212 pct++; 213 } 214 getnstimeofday(&ts); 215 pct->sec = ts.tv_sec; 216 pct->nsec = ts.tv_nsec; 217 if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff))) 218 err = -EFAULT; 219 break; 220 221 case PTP_PIN_GETFUNC: 222 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { 223 err = -EFAULT; 224 break; 225 } 226 pin_index = pd.index; 227 if (pin_index >= ops->n_pins) { 228 err = -EINVAL; 229 break; 230 } 231 if (mutex_lock_interruptible(&ptp->pincfg_mux)) 232 return -ERESTARTSYS; 233 pd = ops->pin_config[pin_index]; 234 mutex_unlock(&ptp->pincfg_mux); 235 if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd))) 236 err = -EFAULT; 237 break; 238 239 case PTP_PIN_SETFUNC: 240 if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { 241 err = -EFAULT; 242 break; 243 } 244 pin_index = pd.index; 245 if (pin_index >= ops->n_pins) { 246 err = -EINVAL; 247 break; 248 } 249 if (mutex_lock_interruptible(&ptp->pincfg_mux)) 250 return -ERESTARTSYS; 251 err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); 252 mutex_unlock(&ptp->pincfg_mux); 253 break; 254 255 default: 256 err = -ENOTTY; 257 break; 258 } 259 260 kfree(sysoff); 261 return err; 262 } 263 264 unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait) 265 { 266 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); 267 268 poll_wait(fp, &ptp->tsev_wq, wait); 269 270 return queue_cnt(&ptp->tsevq) ? POLLIN : 0; 271 } 272 273 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event)) 274 275 ssize_t ptp_read(struct posix_clock *pc, 276 uint rdflags, char __user *buf, size_t cnt) 277 { 278 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); 279 struct timestamp_event_queue *queue = &ptp->tsevq; 280 struct ptp_extts_event *event; 281 unsigned long flags; 282 size_t qcnt, i; 283 int result; 284 285 if (cnt % sizeof(struct ptp_extts_event) != 0) 286 return -EINVAL; 287 288 if (cnt > EXTTS_BUFSIZE) 289 cnt = EXTTS_BUFSIZE; 290 291 cnt = cnt / sizeof(struct ptp_extts_event); 292 293 if (mutex_lock_interruptible(&ptp->tsevq_mux)) 294 return -ERESTARTSYS; 295 296 if (wait_event_interruptible(ptp->tsev_wq, 297 ptp->defunct || queue_cnt(queue))) { 298 mutex_unlock(&ptp->tsevq_mux); 299 return -ERESTARTSYS; 300 } 301 302 if (ptp->defunct) { 303 mutex_unlock(&ptp->tsevq_mux); 304 return -ENODEV; 305 } 306 307 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL); 308 if (!event) { 309 mutex_unlock(&ptp->tsevq_mux); 310 return -ENOMEM; 311 } 312 313 spin_lock_irqsave(&queue->lock, flags); 314 315 qcnt = queue_cnt(queue); 316 317 if (cnt > qcnt) 318 cnt = qcnt; 319 320 for (i = 0; i < cnt; i++) { 321 event[i] = queue->buf[queue->head]; 322 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS; 323 } 324 325 spin_unlock_irqrestore(&queue->lock, flags); 326 327 cnt = cnt * sizeof(struct ptp_extts_event); 328 329 mutex_unlock(&ptp->tsevq_mux); 330 331 result = cnt; 332 if (copy_to_user(buf, event, cnt)) 333 result = -EFAULT; 334 335 kfree(event); 336 return result; 337 } 338