diff options
Diffstat (limited to 'drivers/ptp')
| -rw-r--r-- | drivers/ptp/Kconfig | 30 | ||||
| -rw-r--r-- | drivers/ptp/Makefile | 6 | ||||
| -rw-r--r-- | drivers/ptp/ptp_chardev.c | 159 | ||||
| -rw-r--r-- | drivers/ptp/ptp_clock.c | 343 | ||||
| -rw-r--r-- | drivers/ptp/ptp_private.h | 92 | ||||
| -rw-r--r-- | drivers/ptp/ptp_sysfs.c | 230 |
6 files changed, 860 insertions, 0 deletions
diff --git a/drivers/ptp/Kconfig b/drivers/ptp/Kconfig new file mode 100644 index 000000000000..70d4bb1cbdab --- /dev/null +++ b/drivers/ptp/Kconfig | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | # | ||
| 2 | # PTP clock support configuration | ||
| 3 | # | ||
| 4 | |||
| 5 | menu "PTP clock support" | ||
| 6 | |||
| 7 | comment "Enable Device Drivers -> PPS to see the PTP clock options." | ||
| 8 | depends on PPS=n | ||
| 9 | |||
| 10 | config PTP_1588_CLOCK | ||
| 11 | tristate "PTP clock support" | ||
| 12 | depends on EXPERIMENTAL | ||
| 13 | depends on PPS | ||
| 14 | help | ||
| 15 | The IEEE 1588 standard defines a method to precisely | ||
| 16 | synchronize distributed clocks over Ethernet networks. The | ||
| 17 | standard defines a Precision Time Protocol (PTP), which can | ||
| 18 | be used to achieve synchronization within a few dozen | ||
| 19 | microseconds. In addition, with the help of special hardware | ||
| 20 | time stamping units, it can be possible to achieve | ||
| 21 | synchronization to within a few hundred nanoseconds. | ||
| 22 | |||
| 23 | This driver adds support for PTP clocks as character | ||
| 24 | devices. If you want to use a PTP clock, then you should | ||
| 25 | also enable at least one clock driver as well. | ||
| 26 | |||
| 27 | To compile this driver as a module, choose M here: the module | ||
| 28 | will be called ptp. | ||
| 29 | |||
| 30 | endmenu | ||
diff --git a/drivers/ptp/Makefile b/drivers/ptp/Makefile new file mode 100644 index 000000000000..480e2afdc999 --- /dev/null +++ b/drivers/ptp/Makefile | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | # | ||
| 2 | # Makefile for PTP 1588 clock support. | ||
| 3 | # | ||
| 4 | |||
| 5 | ptp-y := ptp_clock.o ptp_chardev.o ptp_sysfs.o | ||
| 6 | obj-$(CONFIG_PTP_1588_CLOCK) += ptp.o | ||
diff --git a/drivers/ptp/ptp_chardev.c b/drivers/ptp/ptp_chardev.c new file mode 100644 index 000000000000..a8d03aeb4051 --- /dev/null +++ b/drivers/ptp/ptp_chardev.c | |||
| @@ -0,0 +1,159 @@ | |||
| 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 | } | ||
diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c new file mode 100644 index 000000000000..cf3f9997546d --- /dev/null +++ b/drivers/ptp/ptp_clock.c | |||
| @@ -0,0 +1,343 @@ | |||
| 1 | /* | ||
| 2 | * PTP 1588 clock support | ||
| 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/bitops.h> | ||
| 21 | #include <linux/device.h> | ||
| 22 | #include <linux/err.h> | ||
| 23 | #include <linux/init.h> | ||
| 24 | #include <linux/kernel.h> | ||
| 25 | #include <linux/module.h> | ||
| 26 | #include <linux/posix-clock.h> | ||
| 27 | #include <linux/pps_kernel.h> | ||
| 28 | #include <linux/slab.h> | ||
| 29 | #include <linux/syscalls.h> | ||
| 30 | #include <linux/uaccess.h> | ||
| 31 | |||
| 32 | #include "ptp_private.h" | ||
| 33 | |||
| 34 | #define PTP_MAX_ALARMS 4 | ||
| 35 | #define PTP_MAX_CLOCKS 8 | ||
| 36 | #define PTP_PPS_DEFAULTS (PPS_CAPTUREASSERT | PPS_OFFSETASSERT) | ||
| 37 | #define PTP_PPS_EVENT PPS_CAPTUREASSERT | ||
| 38 | #define PTP_PPS_MODE (PTP_PPS_DEFAULTS | PPS_CANWAIT | PPS_TSFMT_TSPEC) | ||
| 39 | |||
| 40 | /* private globals */ | ||
| 41 | |||
| 42 | static dev_t ptp_devt; | ||
| 43 | static struct class *ptp_class; | ||
| 44 | |||
| 45 | static DECLARE_BITMAP(ptp_clocks_map, PTP_MAX_CLOCKS); | ||
| 46 | static DEFINE_MUTEX(ptp_clocks_mutex); /* protects 'ptp_clocks_map' */ | ||
| 47 | |||
| 48 | /* time stamp event queue operations */ | ||
| 49 | |||
| 50 | static inline int queue_free(struct timestamp_event_queue *q) | ||
| 51 | { | ||
| 52 | return PTP_MAX_TIMESTAMPS - queue_cnt(q) - 1; | ||
| 53 | } | ||
| 54 | |||
| 55 | static void enqueue_external_timestamp(struct timestamp_event_queue *queue, | ||
| 56 | struct ptp_clock_event *src) | ||
| 57 | { | ||
| 58 | struct ptp_extts_event *dst; | ||
| 59 | unsigned long flags; | ||
| 60 | s64 seconds; | ||
| 61 | u32 remainder; | ||
| 62 | |||
| 63 | seconds = div_u64_rem(src->timestamp, 1000000000, &remainder); | ||
| 64 | |||
| 65 | spin_lock_irqsave(&queue->lock, flags); | ||
| 66 | |||
| 67 | dst = &queue->buf[queue->tail]; | ||
| 68 | dst->index = src->index; | ||
| 69 | dst->t.sec = seconds; | ||
| 70 | dst->t.nsec = remainder; | ||
| 71 | |||
| 72 | if (!queue_free(queue)) | ||
| 73 | queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS; | ||
| 74 | |||
| 75 | queue->tail = (queue->tail + 1) % PTP_MAX_TIMESTAMPS; | ||
| 76 | |||
| 77 | spin_unlock_irqrestore(&queue->lock, flags); | ||
| 78 | } | ||
| 79 | |||
| 80 | static s32 scaled_ppm_to_ppb(long ppm) | ||
| 81 | { | ||
| 82 | /* | ||
| 83 | * The 'freq' field in the 'struct timex' is in parts per | ||
| 84 | * million, but with a 16 bit binary fractional field. | ||
| 85 | * | ||
| 86 | * We want to calculate | ||
| 87 | * | ||
| 88 | * ppb = scaled_ppm * 1000 / 2^16 | ||
| 89 | * | ||
| 90 | * which simplifies to | ||
| 91 | * | ||
| 92 | * ppb = scaled_ppm * 125 / 2^13 | ||
| 93 | */ | ||
| 94 | s64 ppb = 1 + ppm; | ||
| 95 | ppb *= 125; | ||
| 96 | ppb >>= 13; | ||
| 97 | return (s32) ppb; | ||
| 98 | } | ||
| 99 | |||
| 100 | /* posix clock implementation */ | ||
| 101 | |||
| 102 | static int ptp_clock_getres(struct posix_clock *pc, struct timespec *tp) | ||
| 103 | { | ||
| 104 | return 1; /* always round timer functions to one nanosecond */ | ||
| 105 | } | ||
| 106 | |||
| 107 | static int ptp_clock_settime(struct posix_clock *pc, const struct timespec *tp) | ||
| 108 | { | ||
| 109 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); | ||
| 110 | return ptp->info->settime(ptp->info, tp); | ||
| 111 | } | ||
| 112 | |||
| 113 | static int ptp_clock_gettime(struct posix_clock *pc, struct timespec *tp) | ||
| 114 | { | ||
| 115 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); | ||
| 116 | return ptp->info->gettime(ptp->info, tp); | ||
| 117 | } | ||
| 118 | |||
| 119 | static int ptp_clock_adjtime(struct posix_clock *pc, struct timex *tx) | ||
| 120 | { | ||
| 121 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); | ||
| 122 | struct ptp_clock_info *ops; | ||
| 123 | int err = -EOPNOTSUPP; | ||
| 124 | |||
| 125 | ops = ptp->info; | ||
| 126 | |||
| 127 | if (tx->modes & ADJ_SETOFFSET) { | ||
| 128 | struct timespec ts; | ||
| 129 | ktime_t kt; | ||
| 130 | s64 delta; | ||
| 131 | |||
| 132 | ts.tv_sec = tx->time.tv_sec; | ||
| 133 | ts.tv_nsec = tx->time.tv_usec; | ||
| 134 | |||
| 135 | if (!(tx->modes & ADJ_NANO)) | ||
| 136 | ts.tv_nsec *= 1000; | ||
| 137 | |||
| 138 | if ((unsigned long) ts.tv_nsec >= NSEC_PER_SEC) | ||
| 139 | return -EINVAL; | ||
| 140 | |||
| 141 | kt = timespec_to_ktime(ts); | ||
| 142 | delta = ktime_to_ns(kt); | ||
| 143 | err = ops->adjtime(ops, delta); | ||
| 144 | |||
| 145 | } else if (tx->modes & ADJ_FREQUENCY) { | ||
| 146 | |||
| 147 | err = ops->adjfreq(ops, scaled_ppm_to_ppb(tx->freq)); | ||
| 148 | } | ||
| 149 | |||
| 150 | return err; | ||
| 151 | } | ||
| 152 | |||
| 153 | static struct posix_clock_operations ptp_clock_ops = { | ||
| 154 | .owner = THIS_MODULE, | ||
| 155 | .clock_adjtime = ptp_clock_adjtime, | ||
| 156 | .clock_gettime = ptp_clock_gettime, | ||
| 157 | .clock_getres = ptp_clock_getres, | ||
| 158 | .clock_settime = ptp_clock_settime, | ||
| 159 | .ioctl = ptp_ioctl, | ||
| 160 | .open = ptp_open, | ||
| 161 | .poll = ptp_poll, | ||
| 162 | .read = ptp_read, | ||
| 163 | }; | ||
| 164 | |||
| 165 | static void delete_ptp_clock(struct posix_clock *pc) | ||
| 166 | { | ||
| 167 | struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); | ||
| 168 | |||
| 169 | mutex_destroy(&ptp->tsevq_mux); | ||
| 170 | |||
| 171 | /* Remove the clock from the bit map. */ | ||
| 172 | mutex_lock(&ptp_clocks_mutex); | ||
| 173 | clear_bit(ptp->index, ptp_clocks_map); | ||
| 174 | mutex_unlock(&ptp_clocks_mutex); | ||
| 175 | |||
| 176 | kfree(ptp); | ||
| 177 | } | ||
| 178 | |||
| 179 | /* public interface */ | ||
| 180 | |||
| 181 | struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info) | ||
| 182 | { | ||
| 183 | struct ptp_clock *ptp; | ||
| 184 | int err = 0, index, major = MAJOR(ptp_devt); | ||
| 185 | |||
| 186 | if (info->n_alarm > PTP_MAX_ALARMS) | ||
| 187 | return ERR_PTR(-EINVAL); | ||
| 188 | |||
| 189 | /* Find a free clock slot and reserve it. */ | ||
| 190 | err = -EBUSY; | ||
| 191 | mutex_lock(&ptp_clocks_mutex); | ||
| 192 | index = find_first_zero_bit(ptp_clocks_map, PTP_MAX_CLOCKS); | ||
| 193 | if (index < PTP_MAX_CLOCKS) | ||
| 194 | set_bit(index, ptp_clocks_map); | ||
| 195 | else | ||
| 196 | goto no_slot; | ||
| 197 | |||
| 198 | /* Initialize a clock structure. */ | ||
| 199 | err = -ENOMEM; | ||
| 200 | ptp = kzalloc(sizeof(struct ptp_clock), GFP_KERNEL); | ||
| 201 | if (ptp == NULL) | ||
| 202 | goto no_memory; | ||
| 203 | |||
| 204 | ptp->clock.ops = ptp_clock_ops; | ||
| 205 | ptp->clock.release = delete_ptp_clock; | ||
| 206 | ptp->info = info; | ||
| 207 | ptp->devid = MKDEV(major, index); | ||
| 208 | ptp->index = index; | ||
| 209 | spin_lock_init(&ptp->tsevq.lock); | ||
| 210 | mutex_init(&ptp->tsevq_mux); | ||
| 211 | init_waitqueue_head(&ptp->tsev_wq); | ||
| 212 | |||
| 213 | /* Create a new device in our class. */ | ||
| 214 | ptp->dev = device_create(ptp_class, NULL, ptp->devid, ptp, | ||
| 215 | "ptp%d", ptp->index); | ||
| 216 | if (IS_ERR(ptp->dev)) | ||
| 217 | goto no_device; | ||
| 218 | |||
| 219 | dev_set_drvdata(ptp->dev, ptp); | ||
| 220 | |||
| 221 | err = ptp_populate_sysfs(ptp); | ||
| 222 | if (err) | ||
| 223 | goto no_sysfs; | ||
| 224 | |||
| 225 | /* Register a new PPS source. */ | ||
| 226 | if (info->pps) { | ||
| 227 | struct pps_source_info pps; | ||
| 228 | memset(&pps, 0, sizeof(pps)); | ||
| 229 | snprintf(pps.name, PPS_MAX_NAME_LEN, "ptp%d", index); | ||
| 230 | pps.mode = PTP_PPS_MODE; | ||
| 231 | pps.owner = info->owner; | ||
| 232 | ptp->pps_source = pps_register_source(&pps, PTP_PPS_DEFAULTS); | ||
| 233 | if (!ptp->pps_source) { | ||
| 234 | pr_err("failed to register pps source\n"); | ||
| 235 | goto no_pps; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | /* Create a posix clock. */ | ||
| 240 | err = posix_clock_register(&ptp->clock, ptp->devid); | ||
| 241 | if (err) { | ||
| 242 | pr_err("failed to create posix clock\n"); | ||
| 243 | goto no_clock; | ||
| 244 | } | ||
| 245 | |||
| 246 | mutex_unlock(&ptp_clocks_mutex); | ||
| 247 | return ptp; | ||
| 248 | |||
| 249 | no_clock: | ||
| 250 | if (ptp->pps_source) | ||
| 251 | pps_unregister_source(ptp->pps_source); | ||
| 252 | no_pps: | ||
| 253 | ptp_cleanup_sysfs(ptp); | ||
| 254 | no_sysfs: | ||
| 255 | device_destroy(ptp_class, ptp->devid); | ||
| 256 | no_device: | ||
| 257 | mutex_destroy(&ptp->tsevq_mux); | ||
| 258 | kfree(ptp); | ||
| 259 | no_memory: | ||
| 260 | clear_bit(index, ptp_clocks_map); | ||
| 261 | no_slot: | ||
| 262 | mutex_unlock(&ptp_clocks_mutex); | ||
| 263 | return ERR_PTR(err); | ||
| 264 | } | ||
| 265 | EXPORT_SYMBOL(ptp_clock_register); | ||
| 266 | |||
| 267 | int ptp_clock_unregister(struct ptp_clock *ptp) | ||
| 268 | { | ||
| 269 | ptp->defunct = 1; | ||
| 270 | wake_up_interruptible(&ptp->tsev_wq); | ||
| 271 | |||
| 272 | /* Release the clock's resources. */ | ||
| 273 | if (ptp->pps_source) | ||
| 274 | pps_unregister_source(ptp->pps_source); | ||
| 275 | ptp_cleanup_sysfs(ptp); | ||
| 276 | device_destroy(ptp_class, ptp->devid); | ||
| 277 | |||
| 278 | posix_clock_unregister(&ptp->clock); | ||
| 279 | return 0; | ||
| 280 | } | ||
| 281 | EXPORT_SYMBOL(ptp_clock_unregister); | ||
| 282 | |||
| 283 | void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event) | ||
| 284 | { | ||
| 285 | struct pps_event_time evt; | ||
| 286 | |||
| 287 | switch (event->type) { | ||
| 288 | |||
| 289 | case PTP_CLOCK_ALARM: | ||
| 290 | break; | ||
| 291 | |||
| 292 | case PTP_CLOCK_EXTTS: | ||
| 293 | enqueue_external_timestamp(&ptp->tsevq, event); | ||
| 294 | wake_up_interruptible(&ptp->tsev_wq); | ||
| 295 | break; | ||
| 296 | |||
| 297 | case PTP_CLOCK_PPS: | ||
| 298 | pps_get_ts(&evt); | ||
| 299 | pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL); | ||
| 300 | break; | ||
| 301 | } | ||
| 302 | } | ||
| 303 | EXPORT_SYMBOL(ptp_clock_event); | ||
| 304 | |||
| 305 | /* module operations */ | ||
| 306 | |||
| 307 | static void __exit ptp_exit(void) | ||
| 308 | { | ||
| 309 | class_destroy(ptp_class); | ||
| 310 | unregister_chrdev_region(ptp_devt, PTP_MAX_CLOCKS); | ||
| 311 | } | ||
| 312 | |||
| 313 | static int __init ptp_init(void) | ||
| 314 | { | ||
| 315 | int err; | ||
| 316 | |||
| 317 | ptp_class = class_create(THIS_MODULE, "ptp"); | ||
| 318 | if (IS_ERR(ptp_class)) { | ||
| 319 | pr_err("ptp: failed to allocate class\n"); | ||
| 320 | return PTR_ERR(ptp_class); | ||
| 321 | } | ||
| 322 | |||
| 323 | err = alloc_chrdev_region(&ptp_devt, 0, PTP_MAX_CLOCKS, "ptp"); | ||
| 324 | if (err < 0) { | ||
| 325 | pr_err("ptp: failed to allocate device region\n"); | ||
| 326 | goto no_region; | ||
| 327 | } | ||
| 328 | |||
| 329 | ptp_class->dev_attrs = ptp_dev_attrs; | ||
| 330 | pr_info("PTP clock support registered\n"); | ||
| 331 | return 0; | ||
| 332 | |||
| 333 | no_region: | ||
| 334 | class_destroy(ptp_class); | ||
| 335 | return err; | ||
| 336 | } | ||
| 337 | |||
| 338 | subsys_initcall(ptp_init); | ||
| 339 | module_exit(ptp_exit); | ||
| 340 | |||
| 341 | MODULE_AUTHOR("Richard Cochran <richard.cochran@omicron.at>"); | ||
| 342 | MODULE_DESCRIPTION("PTP clocks support"); | ||
| 343 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/ptp/ptp_private.h b/drivers/ptp/ptp_private.h new file mode 100644 index 000000000000..4d5b5082c3b1 --- /dev/null +++ b/drivers/ptp/ptp_private.h | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | /* | ||
| 2 | * PTP 1588 clock support - private declarations for the core module. | ||
| 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 | #ifndef _PTP_PRIVATE_H_ | ||
| 21 | #define _PTP_PRIVATE_H_ | ||
| 22 | |||
| 23 | #include <linux/cdev.h> | ||
| 24 | #include <linux/device.h> | ||
| 25 | #include <linux/mutex.h> | ||
| 26 | #include <linux/posix-clock.h> | ||
| 27 | #include <linux/ptp_clock.h> | ||
| 28 | #include <linux/ptp_clock_kernel.h> | ||
| 29 | #include <linux/time.h> | ||
| 30 | |||
| 31 | #define PTP_MAX_TIMESTAMPS 128 | ||
| 32 | #define PTP_BUF_TIMESTAMPS 30 | ||
| 33 | |||
| 34 | struct timestamp_event_queue { | ||
| 35 | struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS]; | ||
| 36 | int head; | ||
| 37 | int tail; | ||
| 38 | spinlock_t lock; | ||
| 39 | }; | ||
| 40 | |||
| 41 | struct ptp_clock { | ||
| 42 | struct posix_clock clock; | ||
| 43 | struct device *dev; | ||
| 44 | struct ptp_clock_info *info; | ||
| 45 | dev_t devid; | ||
| 46 | int index; /* index into clocks.map */ | ||
| 47 | struct pps_device *pps_source; | ||
| 48 | struct timestamp_event_queue tsevq; /* simple fifo for time stamps */ | ||
| 49 | struct mutex tsevq_mux; /* one process at a time reading the fifo */ | ||
| 50 | wait_queue_head_t tsev_wq; | ||
| 51 | int defunct; /* tells readers to go away when clock is being removed */ | ||
| 52 | }; | ||
| 53 | |||
| 54 | /* | ||
| 55 | * The function queue_cnt() is safe for readers to call without | ||
| 56 | * holding q->lock. Readers use this function to verify that the queue | ||
| 57 | * is nonempty before proceeding with a dequeue operation. The fact | ||
| 58 | * that a writer might concurrently increment the tail does not | ||
| 59 | * matter, since the queue remains nonempty nonetheless. | ||
| 60 | */ | ||
| 61 | static inline int queue_cnt(struct timestamp_event_queue *q) | ||
| 62 | { | ||
| 63 | int cnt = q->tail - q->head; | ||
| 64 | return cnt < 0 ? PTP_MAX_TIMESTAMPS + cnt : cnt; | ||
| 65 | } | ||
| 66 | |||
| 67 | /* | ||
| 68 | * see ptp_chardev.c | ||
| 69 | */ | ||
| 70 | |||
| 71 | long ptp_ioctl(struct posix_clock *pc, | ||
| 72 | unsigned int cmd, unsigned long arg); | ||
| 73 | |||
| 74 | int ptp_open(struct posix_clock *pc, fmode_t fmode); | ||
| 75 | |||
| 76 | ssize_t ptp_read(struct posix_clock *pc, | ||
| 77 | uint flags, char __user *buf, size_t cnt); | ||
| 78 | |||
| 79 | uint ptp_poll(struct posix_clock *pc, | ||
| 80 | struct file *fp, poll_table *wait); | ||
| 81 | |||
| 82 | /* | ||
| 83 | * see ptp_sysfs.c | ||
| 84 | */ | ||
| 85 | |||
| 86 | extern struct device_attribute ptp_dev_attrs[]; | ||
| 87 | |||
| 88 | int ptp_cleanup_sysfs(struct ptp_clock *ptp); | ||
| 89 | |||
| 90 | int ptp_populate_sysfs(struct ptp_clock *ptp); | ||
| 91 | |||
| 92 | #endif | ||
diff --git a/drivers/ptp/ptp_sysfs.c b/drivers/ptp/ptp_sysfs.c new file mode 100644 index 000000000000..2f93926ac976 --- /dev/null +++ b/drivers/ptp/ptp_sysfs.c | |||
| @@ -0,0 +1,230 @@ | |||
| 1 | /* | ||
| 2 | * PTP 1588 clock support - sysfs interface. | ||
| 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/capability.h> | ||
| 21 | |||
| 22 | #include "ptp_private.h" | ||
| 23 | |||
| 24 | static ssize_t clock_name_show(struct device *dev, | ||
| 25 | struct device_attribute *attr, char *page) | ||
| 26 | { | ||
| 27 | struct ptp_clock *ptp = dev_get_drvdata(dev); | ||
| 28 | return snprintf(page, PAGE_SIZE-1, "%s\n", ptp->info->name); | ||
| 29 | } | ||
| 30 | |||
| 31 | #define PTP_SHOW_INT(name) \ | ||
| 32 | static ssize_t name##_show(struct device *dev, \ | ||
| 33 | struct device_attribute *attr, char *page) \ | ||
| 34 | { \ | ||
| 35 | struct ptp_clock *ptp = dev_get_drvdata(dev); \ | ||
| 36 | return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->name); \ | ||
| 37 | } | ||
| 38 | |||
| 39 | PTP_SHOW_INT(max_adj); | ||
| 40 | PTP_SHOW_INT(n_alarm); | ||
| 41 | PTP_SHOW_INT(n_ext_ts); | ||
| 42 | PTP_SHOW_INT(n_per_out); | ||
| 43 | PTP_SHOW_INT(pps); | ||
| 44 | |||
| 45 | #define PTP_RO_ATTR(_var, _name) { \ | ||
| 46 | .attr = { .name = __stringify(_name), .mode = 0444 }, \ | ||
| 47 | .show = _var##_show, \ | ||
| 48 | } | ||
| 49 | |||
| 50 | struct device_attribute ptp_dev_attrs[] = { | ||
| 51 | PTP_RO_ATTR(clock_name, clock_name), | ||
| 52 | PTP_RO_ATTR(max_adj, max_adjustment), | ||
| 53 | PTP_RO_ATTR(n_alarm, n_alarms), | ||
| 54 | PTP_RO_ATTR(n_ext_ts, n_external_timestamps), | ||
| 55 | PTP_RO_ATTR(n_per_out, n_periodic_outputs), | ||
| 56 | PTP_RO_ATTR(pps, pps_available), | ||
| 57 | __ATTR_NULL, | ||
| 58 | }; | ||
| 59 | |||
| 60 | static ssize_t extts_enable_store(struct device *dev, | ||
| 61 | struct device_attribute *attr, | ||
| 62 | const char *buf, size_t count) | ||
| 63 | { | ||
| 64 | struct ptp_clock *ptp = dev_get_drvdata(dev); | ||
| 65 | struct ptp_clock_info *ops = ptp->info; | ||
| 66 | struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS }; | ||
| 67 | int cnt, enable; | ||
| 68 | int err = -EINVAL; | ||
| 69 | |||
| 70 | cnt = sscanf(buf, "%u %d", &req.extts.index, &enable); | ||
| 71 | if (cnt != 2) | ||
| 72 | goto out; | ||
| 73 | if (req.extts.index >= ops->n_ext_ts) | ||
| 74 | goto out; | ||
| 75 | |||
| 76 | err = ops->enable(ops, &req, enable ? 1 : 0); | ||
| 77 | if (err) | ||
| 78 | goto out; | ||
| 79 | |||
| 80 | return count; | ||
| 81 | out: | ||
| 82 | return err; | ||
| 83 | } | ||
| 84 | |||
| 85 | static ssize_t extts_fifo_show(struct device *dev, | ||
| 86 | struct device_attribute *attr, char *page) | ||
| 87 | { | ||
| 88 | struct ptp_clock *ptp = dev_get_drvdata(dev); | ||
| 89 | struct timestamp_event_queue *queue = &ptp->tsevq; | ||
| 90 | struct ptp_extts_event event; | ||
| 91 | unsigned long flags; | ||
| 92 | size_t qcnt; | ||
| 93 | int cnt = 0; | ||
| 94 | |||
| 95 | memset(&event, 0, sizeof(event)); | ||
| 96 | |||
| 97 | if (mutex_lock_interruptible(&ptp->tsevq_mux)) | ||
| 98 | return -ERESTARTSYS; | ||
| 99 | |||
| 100 | spin_lock_irqsave(&queue->lock, flags); | ||
| 101 | qcnt = queue_cnt(queue); | ||
| 102 | if (qcnt) { | ||
| 103 | event = queue->buf[queue->head]; | ||
| 104 | queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS; | ||
| 105 | } | ||
| 106 | spin_unlock_irqrestore(&queue->lock, flags); | ||
| 107 | |||
| 108 | if (!qcnt) | ||
| 109 | goto out; | ||
| 110 | |||
| 111 | cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n", | ||
| 112 | event.index, event.t.sec, event.t.nsec); | ||
| 113 | out: | ||
| 114 | mutex_unlock(&ptp->tsevq_mux); | ||
| 115 | return cnt; | ||
| 116 | } | ||
| 117 | |||
| 118 | static ssize_t period_store(struct device *dev, | ||
| 119 | struct device_attribute *attr, | ||
| 120 | const char *buf, size_t count) | ||
| 121 | { | ||
| 122 | struct ptp_clock *ptp = dev_get_drvdata(dev); | ||
| 123 | struct ptp_clock_info *ops = ptp->info; | ||
| 124 | struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT }; | ||
| 125 | int cnt, enable, err = -EINVAL; | ||
| 126 | |||
| 127 | cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index, | ||
| 128 | &req.perout.start.sec, &req.perout.start.nsec, | ||
| 129 | &req.perout.period.sec, &req.perout.period.nsec); | ||
| 130 | if (cnt != 5) | ||
| 131 | goto out; | ||
| 132 | if (req.perout.index >= ops->n_per_out) | ||
| 133 | goto out; | ||
| 134 | |||
| 135 | enable = req.perout.period.sec || req.perout.period.nsec; | ||
| 136 | err = ops->enable(ops, &req, enable); | ||
| 137 | if (err) | ||
| 138 | goto out; | ||
| 139 | |||
| 140 | return count; | ||
| 141 | out: | ||
| 142 | return err; | ||
| 143 | } | ||
| 144 | |||
| 145 | static ssize_t pps_enable_store(struct device *dev, | ||
| 146 | struct device_attribute *attr, | ||
| 147 | const char *buf, size_t count) | ||
| 148 | { | ||
| 149 | struct ptp_clock *ptp = dev_get_drvdata(dev); | ||
| 150 | struct ptp_clock_info *ops = ptp->info; | ||
| 151 | struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS }; | ||
| 152 | int cnt, enable; | ||
| 153 | int err = -EINVAL; | ||
| 154 | |||
| 155 | if (!capable(CAP_SYS_TIME)) | ||
| 156 | return -EPERM; | ||
| 157 | |||
| 158 | cnt = sscanf(buf, "%d", &enable); | ||
| 159 | if (cnt != 1) | ||
| 160 | goto out; | ||
| 161 | |||
| 162 | err = ops->enable(ops, &req, enable ? 1 : 0); | ||
| 163 | if (err) | ||
| 164 | goto out; | ||
| 165 | |||
| 166 | return count; | ||
| 167 | out: | ||
| 168 | return err; | ||
| 169 | } | ||
| 170 | |||
| 171 | static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store); | ||
| 172 | static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL); | ||
| 173 | static DEVICE_ATTR(period, 0220, NULL, period_store); | ||
| 174 | static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store); | ||
| 175 | |||
| 176 | int ptp_cleanup_sysfs(struct ptp_clock *ptp) | ||
| 177 | { | ||
| 178 | struct device *dev = ptp->dev; | ||
| 179 | struct ptp_clock_info *info = ptp->info; | ||
| 180 | |||
| 181 | if (info->n_ext_ts) { | ||
| 182 | device_remove_file(dev, &dev_attr_extts_enable); | ||
| 183 | device_remove_file(dev, &dev_attr_fifo); | ||
| 184 | } | ||
| 185 | if (info->n_per_out) | ||
| 186 | device_remove_file(dev, &dev_attr_period); | ||
| 187 | |||
| 188 | if (info->pps) | ||
| 189 | device_remove_file(dev, &dev_attr_pps_enable); | ||
| 190 | |||
| 191 | return 0; | ||
| 192 | } | ||
| 193 | |||
| 194 | int ptp_populate_sysfs(struct ptp_clock *ptp) | ||
| 195 | { | ||
| 196 | struct device *dev = ptp->dev; | ||
| 197 | struct ptp_clock_info *info = ptp->info; | ||
| 198 | int err; | ||
| 199 | |||
| 200 | if (info->n_ext_ts) { | ||
| 201 | err = device_create_file(dev, &dev_attr_extts_enable); | ||
| 202 | if (err) | ||
| 203 | goto out1; | ||
| 204 | err = device_create_file(dev, &dev_attr_fifo); | ||
| 205 | if (err) | ||
| 206 | goto out2; | ||
| 207 | } | ||
| 208 | if (info->n_per_out) { | ||
| 209 | err = device_create_file(dev, &dev_attr_period); | ||
| 210 | if (err) | ||
| 211 | goto out3; | ||
| 212 | } | ||
| 213 | if (info->pps) { | ||
| 214 | err = device_create_file(dev, &dev_attr_pps_enable); | ||
| 215 | if (err) | ||
| 216 | goto out4; | ||
| 217 | } | ||
| 218 | return 0; | ||
| 219 | out4: | ||
| 220 | if (info->n_per_out) | ||
| 221 | device_remove_file(dev, &dev_attr_period); | ||
| 222 | out3: | ||
| 223 | if (info->n_ext_ts) | ||
| 224 | device_remove_file(dev, &dev_attr_fifo); | ||
| 225 | out2: | ||
| 226 | if (info->n_ext_ts) | ||
| 227 | device_remove_file(dev, &dev_attr_extts_enable); | ||
| 228 | out1: | ||
| 229 | return err; | ||
| 230 | } | ||
