Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 2 | /* |
| 3 | * PTP 1588 clock support |
| 4 | * |
| 5 | * Copyright (C) 2010 OMICRON electronics GmbH |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 6 | */ |
Jiri Benc | 7356a76 | 2013-04-12 00:56:15 +0000 | [diff] [blame] | 7 | #include <linux/idr.h> |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 8 | #include <linux/device.h> |
| 9 | #include <linux/err.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/posix-clock.h> |
| 14 | #include <linux/pps_kernel.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/syscalls.h> |
| 17 | #include <linux/uaccess.h> |
Grygorii Strashko | d9535cb | 2017-07-28 17:30:02 -0500 | [diff] [blame] | 18 | #include <uapi/linux/sched/types.h> |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 19 | |
| 20 | #include "ptp_private.h" |
| 21 | |
| 22 | #define PTP_MAX_ALARMS 4 |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 23 | #define PTP_PPS_DEFAULTS (PPS_CAPTUREASSERT | PPS_OFFSETASSERT) |
| 24 | #define PTP_PPS_EVENT PPS_CAPTUREASSERT |
| 25 | #define PTP_PPS_MODE (PTP_PPS_DEFAULTS | PPS_CANWAIT | PPS_TSFMT_TSPEC) |
| 26 | |
Yangbo Lu | acb288e | 2021-06-30 16:11:55 +0800 | [diff] [blame] | 27 | struct class *ptp_class; |
| 28 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 29 | /* private globals */ |
| 30 | |
| 31 | static dev_t ptp_devt; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 32 | |
Jiri Benc | 7356a76 | 2013-04-12 00:56:15 +0000 | [diff] [blame] | 33 | static DEFINE_IDA(ptp_clocks_map); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 34 | |
| 35 | /* time stamp event queue operations */ |
| 36 | |
| 37 | static inline int queue_free(struct timestamp_event_queue *q) |
| 38 | { |
| 39 | return PTP_MAX_TIMESTAMPS - queue_cnt(q) - 1; |
| 40 | } |
| 41 | |
| 42 | static void enqueue_external_timestamp(struct timestamp_event_queue *queue, |
| 43 | struct ptp_clock_event *src) |
| 44 | { |
| 45 | struct ptp_extts_event *dst; |
| 46 | unsigned long flags; |
| 47 | s64 seconds; |
| 48 | u32 remainder; |
| 49 | |
| 50 | seconds = div_u64_rem(src->timestamp, 1000000000, &remainder); |
| 51 | |
| 52 | spin_lock_irqsave(&queue->lock, flags); |
| 53 | |
| 54 | dst = &queue->buf[queue->tail]; |
| 55 | dst->index = src->index; |
| 56 | dst->t.sec = seconds; |
| 57 | dst->t.nsec = remainder; |
| 58 | |
| 59 | if (!queue_free(queue)) |
| 60 | queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS; |
| 61 | |
| 62 | queue->tail = (queue->tail + 1) % PTP_MAX_TIMESTAMPS; |
| 63 | |
| 64 | spin_unlock_irqrestore(&queue->lock, flags); |
| 65 | } |
| 66 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 67 | /* posix clock implementation */ |
| 68 | |
Deepa Dinamani | d340266e | 2017-03-26 12:04:13 -0700 | [diff] [blame] | 69 | static int ptp_clock_getres(struct posix_clock *pc, struct timespec64 *tp) |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 70 | { |
Thomas Gleixner | d68fb11 | 2011-12-05 21:16:06 +0100 | [diff] [blame] | 71 | tp->tv_sec = 0; |
| 72 | tp->tv_nsec = 1; |
| 73 | return 0; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Deepa Dinamani | d340266e | 2017-03-26 12:04:13 -0700 | [diff] [blame] | 76 | static int ptp_clock_settime(struct posix_clock *pc, const struct timespec64 *tp) |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 77 | { |
| 78 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); |
Richard Cochran | d7d38f5 | 2015-03-29 23:11:53 +0200 | [diff] [blame] | 79 | |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 80 | if (ptp_vclock_in_use(ptp)) { |
| 81 | pr_err("ptp: virtual clock in use\n"); |
| 82 | return -EBUSY; |
| 83 | } |
| 84 | |
Deepa Dinamani | d340266e | 2017-03-26 12:04:13 -0700 | [diff] [blame] | 85 | return ptp->info->settime64(ptp->info, tp); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Deepa Dinamani | d340266e | 2017-03-26 12:04:13 -0700 | [diff] [blame] | 88 | static int ptp_clock_gettime(struct posix_clock *pc, struct timespec64 *tp) |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 89 | { |
| 90 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); |
Richard Cochran | d7d38f5 | 2015-03-29 23:11:53 +0200 | [diff] [blame] | 91 | int err; |
| 92 | |
Miroslav Lichvar | 916444d | 2018-11-09 11:14:45 +0100 | [diff] [blame] | 93 | if (ptp->info->gettimex64) |
| 94 | err = ptp->info->gettimex64(ptp->info, tp, NULL); |
| 95 | else |
| 96 | err = ptp->info->gettime64(ptp->info, tp); |
Richard Cochran | d7d38f5 | 2015-03-29 23:11:53 +0200 | [diff] [blame] | 97 | return err; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Deepa Dinamani | ead2541 | 2018-07-02 22:44:21 -0700 | [diff] [blame] | 100 | static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx) |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 101 | { |
| 102 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); |
| 103 | struct ptp_clock_info *ops; |
| 104 | int err = -EOPNOTSUPP; |
| 105 | |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 106 | if (ptp_vclock_in_use(ptp)) { |
| 107 | pr_err("ptp: virtual clock in use\n"); |
| 108 | return -EBUSY; |
| 109 | } |
| 110 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 111 | ops = ptp->info; |
| 112 | |
| 113 | if (tx->modes & ADJ_SETOFFSET) { |
Deepa Dinamani | d340266e | 2017-03-26 12:04:13 -0700 | [diff] [blame] | 114 | struct timespec64 ts; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 115 | ktime_t kt; |
| 116 | s64 delta; |
| 117 | |
| 118 | ts.tv_sec = tx->time.tv_sec; |
| 119 | ts.tv_nsec = tx->time.tv_usec; |
| 120 | |
| 121 | if (!(tx->modes & ADJ_NANO)) |
| 122 | ts.tv_nsec *= 1000; |
| 123 | |
| 124 | if ((unsigned long) ts.tv_nsec >= NSEC_PER_SEC) |
| 125 | return -EINVAL; |
| 126 | |
Deepa Dinamani | d340266e | 2017-03-26 12:04:13 -0700 | [diff] [blame] | 127 | kt = timespec64_to_ktime(ts); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 128 | delta = ktime_to_ns(kt); |
| 129 | err = ops->adjtime(ops, delta); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 130 | } else if (tx->modes & ADJ_FREQUENCY) { |
Jakub Kicinski | 475b92f | 2021-06-14 15:24:05 -0700 | [diff] [blame] | 131 | long ppb = scaled_ppm_to_ppb(tx->freq); |
Richard Cochran | d39a743 | 2014-04-27 15:01:27 +0200 | [diff] [blame] | 132 | if (ppb > ops->max_adj || ppb < -ops->max_adj) |
| 133 | return -ERANGE; |
Richard Cochran | d8d2635 | 2016-11-08 22:49:16 +0100 | [diff] [blame] | 134 | if (ops->adjfine) |
| 135 | err = ops->adjfine(ops, tx->freq); |
| 136 | else |
| 137 | err = ops->adjfreq(ops, ppb); |
Richard Cochran | 39a8cbd | 2012-09-22 07:02:01 +0000 | [diff] [blame] | 138 | ptp->dialed_frequency = tx->freq; |
Vincent Cheng | 184ecc9 | 2020-05-01 23:35:36 -0400 | [diff] [blame] | 139 | } else if (tx->modes & ADJ_OFFSET) { |
Richard Cochran | eabd5c9 | 2020-05-24 11:27:10 -0700 | [diff] [blame] | 140 | if (ops->adjphase) { |
| 141 | s32 offset = tx->offset; |
| 142 | |
| 143 | if (!(tx->modes & ADJ_NANO)) |
| 144 | offset *= NSEC_PER_USEC; |
| 145 | |
| 146 | err = ops->adjphase(ops, offset); |
| 147 | } |
Richard Cochran | 5c35bad | 2012-09-22 07:02:02 +0000 | [diff] [blame] | 148 | } else if (tx->modes == 0) { |
| 149 | tx->freq = ptp->dialed_frequency; |
| 150 | err = 0; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return err; |
| 154 | } |
| 155 | |
| 156 | static struct posix_clock_operations ptp_clock_ops = { |
| 157 | .owner = THIS_MODULE, |
| 158 | .clock_adjtime = ptp_clock_adjtime, |
| 159 | .clock_gettime = ptp_clock_gettime, |
| 160 | .clock_getres = ptp_clock_getres, |
| 161 | .clock_settime = ptp_clock_settime, |
| 162 | .ioctl = ptp_ioctl, |
| 163 | .open = ptp_open, |
| 164 | .poll = ptp_poll, |
| 165 | .read = ptp_read, |
| 166 | }; |
| 167 | |
Vladis Dronov | a33121e | 2019-12-27 03:26:27 +0100 | [diff] [blame] | 168 | static void ptp_clock_release(struct device *dev) |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 169 | { |
Vladis Dronov | a33121e | 2019-12-27 03:26:27 +0100 | [diff] [blame] | 170 | struct ptp_clock *ptp = container_of(dev, struct ptp_clock, dev); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 171 | |
Vladis Dronov | 7571858 | 2020-01-13 14:00:09 +0100 | [diff] [blame] | 172 | ptp_cleanup_pin_groups(ptp); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 173 | mutex_destroy(&ptp->tsevq_mux); |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 174 | mutex_destroy(&ptp->pincfg_mux); |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 175 | mutex_destroy(&ptp->n_vclocks_mux); |
Jiri Benc | 7356a76 | 2013-04-12 00:56:15 +0000 | [diff] [blame] | 176 | ida_simple_remove(&ptp_clocks_map, ptp->index); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 177 | kfree(ptp); |
| 178 | } |
| 179 | |
Grygorii Strashko | d9535cb | 2017-07-28 17:30:02 -0500 | [diff] [blame] | 180 | static void ptp_aux_kworker(struct kthread_work *work) |
| 181 | { |
| 182 | struct ptp_clock *ptp = container_of(work, struct ptp_clock, |
| 183 | aux_work.work); |
| 184 | struct ptp_clock_info *info = ptp->info; |
| 185 | long delay; |
| 186 | |
| 187 | delay = info->do_aux_work(info); |
| 188 | |
| 189 | if (delay >= 0) |
| 190 | kthread_queue_delayed_work(ptp->kworker, &ptp->aux_work, delay); |
| 191 | } |
| 192 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 193 | /* public interface */ |
| 194 | |
Richard Cochran | 1ef7615 | 2012-09-22 07:02:03 +0000 | [diff] [blame] | 195 | struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, |
| 196 | struct device *parent) |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 197 | { |
| 198 | struct ptp_clock *ptp; |
| 199 | int err = 0, index, major = MAJOR(ptp_devt); |
Yangbo Lu | 44c494c | 2021-06-30 16:11:54 +0800 | [diff] [blame] | 200 | size_t size; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 201 | |
| 202 | if (info->n_alarm > PTP_MAX_ALARMS) |
| 203 | return ERR_PTR(-EINVAL); |
| 204 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 205 | /* Initialize a clock structure. */ |
| 206 | err = -ENOMEM; |
| 207 | ptp = kzalloc(sizeof(struct ptp_clock), GFP_KERNEL); |
| 208 | if (ptp == NULL) |
| 209 | goto no_memory; |
| 210 | |
Jiri Benc | 7356a76 | 2013-04-12 00:56:15 +0000 | [diff] [blame] | 211 | index = ida_simple_get(&ptp_clocks_map, 0, MINORMASK + 1, GFP_KERNEL); |
| 212 | if (index < 0) { |
| 213 | err = index; |
| 214 | goto no_slot; |
| 215 | } |
| 216 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 217 | ptp->clock.ops = ptp_clock_ops; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 218 | ptp->info = info; |
| 219 | ptp->devid = MKDEV(major, index); |
| 220 | ptp->index = index; |
| 221 | spin_lock_init(&ptp->tsevq.lock); |
| 222 | mutex_init(&ptp->tsevq_mux); |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 223 | mutex_init(&ptp->pincfg_mux); |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 224 | mutex_init(&ptp->n_vclocks_mux); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 225 | init_waitqueue_head(&ptp->tsev_wq); |
| 226 | |
Grygorii Strashko | d9535cb | 2017-07-28 17:30:02 -0500 | [diff] [blame] | 227 | if (ptp->info->do_aux_work) { |
Grygorii Strashko | d9535cb | 2017-07-28 17:30:02 -0500 | [diff] [blame] | 228 | kthread_init_delayed_work(&ptp->aux_work, ptp_aux_kworker); |
Rasmus Villemoes | 822c5f7 | 2018-10-26 23:22:59 +0200 | [diff] [blame] | 229 | ptp->kworker = kthread_create_worker(0, "ptp%d", ptp->index); |
Grygorii Strashko | d9535cb | 2017-07-28 17:30:02 -0500 | [diff] [blame] | 230 | if (IS_ERR(ptp->kworker)) { |
| 231 | err = PTR_ERR(ptp->kworker); |
| 232 | pr_err("failed to create ptp aux_worker %d\n", err); |
| 233 | goto kworker_err; |
| 234 | } |
| 235 | } |
| 236 | |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 237 | /* PTP virtual clock is being registered under physical clock */ |
Yangbo Lu | 55eac20 | 2021-07-05 16:53:06 +0800 | [diff] [blame] | 238 | if (parent && parent->class && parent->class->name && |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 239 | strcmp(parent->class->name, "ptp") == 0) |
| 240 | ptp->is_virtual_clock = true; |
| 241 | |
Yangbo Lu | 44c494c | 2021-06-30 16:11:54 +0800 | [diff] [blame] | 242 | if (!ptp->is_virtual_clock) { |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 243 | ptp->max_vclocks = PTP_DEFAULT_MAX_VCLOCKS; |
| 244 | |
Yangbo Lu | 44c494c | 2021-06-30 16:11:54 +0800 | [diff] [blame] | 245 | size = sizeof(int) * ptp->max_vclocks; |
| 246 | ptp->vclock_index = kzalloc(size, GFP_KERNEL); |
| 247 | if (!ptp->vclock_index) { |
| 248 | err = -ENOMEM; |
| 249 | goto no_mem_for_vclocks; |
| 250 | } |
| 251 | } |
| 252 | |
Dmitry Torokhov | 85a66e5 | 2017-02-14 10:23:34 -0800 | [diff] [blame] | 253 | err = ptp_populate_pin_groups(ptp); |
| 254 | if (err) |
| 255 | goto no_pin_groups; |
| 256 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 257 | /* Register a new PPS source. */ |
| 258 | if (info->pps) { |
| 259 | struct pps_source_info pps; |
| 260 | memset(&pps, 0, sizeof(pps)); |
| 261 | snprintf(pps.name, PPS_MAX_NAME_LEN, "ptp%d", index); |
| 262 | pps.mode = PTP_PPS_MODE; |
| 263 | pps.owner = info->owner; |
| 264 | ptp->pps_source = pps_register_source(&pps, PTP_PPS_DEFAULTS); |
Dan Carpenter | b9d9359 | 2018-12-07 09:00:46 +0300 | [diff] [blame] | 265 | if (IS_ERR(ptp->pps_source)) { |
| 266 | err = PTR_ERR(ptp->pps_source); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 267 | pr_err("failed to register pps source\n"); |
| 268 | goto no_pps; |
| 269 | } |
Jonathan Lemon | debdd8e | 2021-07-08 11:04:08 -0700 | [diff] [blame] | 270 | ptp->pps_source->lookup_cookie = ptp; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Vladis Dronov | a33121e | 2019-12-27 03:26:27 +0100 | [diff] [blame] | 273 | /* Initialize a new device of our class in our clock structure. */ |
| 274 | device_initialize(&ptp->dev); |
| 275 | ptp->dev.devt = ptp->devid; |
| 276 | ptp->dev.class = ptp_class; |
| 277 | ptp->dev.parent = parent; |
| 278 | ptp->dev.groups = ptp->pin_attr_groups; |
| 279 | ptp->dev.release = ptp_clock_release; |
| 280 | dev_set_drvdata(&ptp->dev, ptp); |
| 281 | dev_set_name(&ptp->dev, "ptp%d", ptp->index); |
| 282 | |
| 283 | /* Create a posix clock and link it to the device. */ |
| 284 | err = posix_clock_register(&ptp->clock, &ptp->dev); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 285 | if (err) { |
Yang Yingliang | 4225fea | 2021-10-20 16:18:34 +0800 | [diff] [blame^] | 286 | if (ptp->pps_source) |
| 287 | pps_unregister_source(ptp->pps_source); |
| 288 | |
| 289 | kfree(ptp->vclock_index); |
| 290 | |
| 291 | if (ptp->kworker) |
| 292 | kthread_destroy_worker(ptp->kworker); |
| 293 | |
| 294 | put_device(&ptp->dev); |
| 295 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 296 | pr_err("failed to create posix clock\n"); |
Yang Yingliang | 4225fea | 2021-10-20 16:18:34 +0800 | [diff] [blame^] | 297 | return ERR_PTR(err); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 300 | return ptp; |
| 301 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 302 | no_pps: |
Dmitry Torokhov | 85a66e5 | 2017-02-14 10:23:34 -0800 | [diff] [blame] | 303 | ptp_cleanup_pin_groups(ptp); |
| 304 | no_pin_groups: |
Yangbo Lu | 44c494c | 2021-06-30 16:11:54 +0800 | [diff] [blame] | 305 | kfree(ptp->vclock_index); |
| 306 | no_mem_for_vclocks: |
Grygorii Strashko | d9535cb | 2017-07-28 17:30:02 -0500 | [diff] [blame] | 307 | if (ptp->kworker) |
| 308 | kthread_destroy_worker(ptp->kworker); |
| 309 | kworker_err: |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 310 | mutex_destroy(&ptp->tsevq_mux); |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 311 | mutex_destroy(&ptp->pincfg_mux); |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 312 | mutex_destroy(&ptp->n_vclocks_mux); |
Christophe Jaillet | b9118b7 | 2016-10-02 09:04:16 +0200 | [diff] [blame] | 313 | ida_simple_remove(&ptp_clocks_map, index); |
Jiri Benc | 7356a76 | 2013-04-12 00:56:15 +0000 | [diff] [blame] | 314 | no_slot: |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 315 | kfree(ptp); |
| 316 | no_memory: |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 317 | return ERR_PTR(err); |
| 318 | } |
| 319 | EXPORT_SYMBOL(ptp_clock_register); |
| 320 | |
| 321 | int ptp_clock_unregister(struct ptp_clock *ptp) |
| 322 | { |
Yangbo Lu | 73f3706 | 2021-06-30 16:11:53 +0800 | [diff] [blame] | 323 | if (ptp_vclock_in_use(ptp)) { |
| 324 | pr_err("ptp: virtual clock in use\n"); |
| 325 | return -EBUSY; |
| 326 | } |
| 327 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 328 | ptp->defunct = 1; |
| 329 | wake_up_interruptible(&ptp->tsev_wq); |
| 330 | |
Yangbo Lu | 44c494c | 2021-06-30 16:11:54 +0800 | [diff] [blame] | 331 | kfree(ptp->vclock_index); |
| 332 | |
Grygorii Strashko | d9535cb | 2017-07-28 17:30:02 -0500 | [diff] [blame] | 333 | if (ptp->kworker) { |
| 334 | kthread_cancel_delayed_work_sync(&ptp->aux_work); |
| 335 | kthread_destroy_worker(ptp->kworker); |
| 336 | } |
| 337 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 338 | /* Release the clock's resources. */ |
| 339 | if (ptp->pps_source) |
| 340 | pps_unregister_source(ptp->pps_source); |
Dmitry Torokhov | 85a66e5 | 2017-02-14 10:23:34 -0800 | [diff] [blame] | 341 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 342 | posix_clock_unregister(&ptp->clock); |
Vladis Dronov | 7571858 | 2020-01-13 14:00:09 +0100 | [diff] [blame] | 343 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | EXPORT_SYMBOL(ptp_clock_unregister); |
| 347 | |
| 348 | void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event) |
| 349 | { |
| 350 | struct pps_event_time evt; |
| 351 | |
| 352 | switch (event->type) { |
| 353 | |
| 354 | case PTP_CLOCK_ALARM: |
| 355 | break; |
| 356 | |
| 357 | case PTP_CLOCK_EXTTS: |
| 358 | enqueue_external_timestamp(&ptp->tsevq, event); |
| 359 | wake_up_interruptible(&ptp->tsev_wq); |
| 360 | break; |
| 361 | |
| 362 | case PTP_CLOCK_PPS: |
| 363 | pps_get_ts(&evt); |
| 364 | pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL); |
| 365 | break; |
Ben Hutchings | 220a60a | 2012-09-03 11:34:58 +0100 | [diff] [blame] | 366 | |
| 367 | case PTP_CLOCK_PPSUSR: |
| 368 | pps_event(ptp->pps_source, &event->pps_times, |
| 369 | PTP_PPS_EVENT, NULL); |
| 370 | break; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | EXPORT_SYMBOL(ptp_clock_event); |
| 374 | |
Richard Cochran | 995a909 | 2012-04-03 22:59:16 +0000 | [diff] [blame] | 375 | int ptp_clock_index(struct ptp_clock *ptp) |
| 376 | { |
| 377 | return ptp->index; |
| 378 | } |
| 379 | EXPORT_SYMBOL(ptp_clock_index); |
| 380 | |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 381 | int ptp_find_pin(struct ptp_clock *ptp, |
| 382 | enum ptp_pin_function func, unsigned int chan) |
| 383 | { |
| 384 | struct ptp_pin_desc *pin = NULL; |
| 385 | int i; |
| 386 | |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 387 | for (i = 0; i < ptp->info->n_pins; i++) { |
| 388 | if (ptp->info->pin_config[i].func == func && |
| 389 | ptp->info->pin_config[i].chan == chan) { |
| 390 | pin = &ptp->info->pin_config[i]; |
| 391 | break; |
| 392 | } |
| 393 | } |
Richard Cochran | 6092315 | 2014-03-20 22:21:52 +0100 | [diff] [blame] | 394 | |
| 395 | return pin ? i : -1; |
| 396 | } |
| 397 | EXPORT_SYMBOL(ptp_find_pin); |
| 398 | |
Richard Cochran | 62582a7 | 2020-03-29 07:55:10 -0700 | [diff] [blame] | 399 | int ptp_find_pin_unlocked(struct ptp_clock *ptp, |
| 400 | enum ptp_pin_function func, unsigned int chan) |
| 401 | { |
| 402 | int result; |
| 403 | |
| 404 | mutex_lock(&ptp->pincfg_mux); |
| 405 | |
| 406 | result = ptp_find_pin(ptp, func, chan); |
| 407 | |
| 408 | mutex_unlock(&ptp->pincfg_mux); |
| 409 | |
| 410 | return result; |
| 411 | } |
| 412 | EXPORT_SYMBOL(ptp_find_pin_unlocked); |
| 413 | |
Grygorii Strashko | d9535cb | 2017-07-28 17:30:02 -0500 | [diff] [blame] | 414 | int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay) |
| 415 | { |
| 416 | return kthread_mod_delayed_work(ptp->kworker, &ptp->aux_work, delay); |
| 417 | } |
| 418 | EXPORT_SYMBOL(ptp_schedule_worker); |
| 419 | |
Vladimir Oltean | 544fed4 | 2019-12-27 15:02:28 +0200 | [diff] [blame] | 420 | void ptp_cancel_worker_sync(struct ptp_clock *ptp) |
| 421 | { |
| 422 | kthread_cancel_delayed_work_sync(&ptp->aux_work); |
| 423 | } |
| 424 | EXPORT_SYMBOL(ptp_cancel_worker_sync); |
| 425 | |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 426 | /* module operations */ |
| 427 | |
| 428 | static void __exit ptp_exit(void) |
| 429 | { |
| 430 | class_destroy(ptp_class); |
Jiri Benc | 7356a76 | 2013-04-12 00:56:15 +0000 | [diff] [blame] | 431 | unregister_chrdev_region(ptp_devt, MINORMASK + 1); |
| 432 | ida_destroy(&ptp_clocks_map); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static int __init ptp_init(void) |
| 436 | { |
| 437 | int err; |
| 438 | |
| 439 | ptp_class = class_create(THIS_MODULE, "ptp"); |
| 440 | if (IS_ERR(ptp_class)) { |
| 441 | pr_err("ptp: failed to allocate class\n"); |
| 442 | return PTR_ERR(ptp_class); |
| 443 | } |
| 444 | |
Jiri Benc | 7356a76 | 2013-04-12 00:56:15 +0000 | [diff] [blame] | 445 | err = alloc_chrdev_region(&ptp_devt, 0, MINORMASK + 1, "ptp"); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 446 | if (err < 0) { |
| 447 | pr_err("ptp: failed to allocate device region\n"); |
| 448 | goto no_region; |
| 449 | } |
| 450 | |
Greg Kroah-Hartman | 3499116 | 2013-07-24 15:05:20 -0700 | [diff] [blame] | 451 | ptp_class->dev_groups = ptp_groups; |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 452 | pr_info("PTP clock support registered\n"); |
| 453 | return 0; |
| 454 | |
| 455 | no_region: |
| 456 | class_destroy(ptp_class); |
| 457 | return err; |
| 458 | } |
| 459 | |
| 460 | subsys_initcall(ptp_init); |
| 461 | module_exit(ptp_exit); |
| 462 | |
Richard Cochran | c2ec3ff | 2012-03-16 22:39:29 +0000 | [diff] [blame] | 463 | MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>"); |
Richard Cochran | d94ba80 | 2011-04-22 12:03:08 +0200 | [diff] [blame] | 464 | MODULE_DESCRIPTION("PTP clocks support"); |
| 465 | MODULE_LICENSE("GPL"); |