Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 2 | /* |
| 3 | * kernel API |
| 4 | * |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 5 | * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it> |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/time.h> |
Alexander Gordeev | 717c033 | 2011-01-12 17:00:58 -0800 | [diff] [blame] | 15 | #include <linux/timex.h> |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 16 | #include <linux/spinlock.h> |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 17 | #include <linux/fs.h> |
| 18 | #include <linux/pps_kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 20 | |
Alexander Gordeev | 717c033 | 2011-01-12 17:00:58 -0800 | [diff] [blame] | 21 | #include "kc.h" |
| 22 | |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 23 | /* |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 24 | * Local functions |
| 25 | */ |
| 26 | |
| 27 | static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset) |
| 28 | { |
| 29 | ts->nsec += offset->nsec; |
| 30 | while (ts->nsec >= NSEC_PER_SEC) { |
| 31 | ts->nsec -= NSEC_PER_SEC; |
| 32 | ts->sec++; |
| 33 | } |
| 34 | while (ts->nsec < 0) { |
| 35 | ts->nsec += NSEC_PER_SEC; |
| 36 | ts->sec--; |
| 37 | } |
| 38 | ts->sec += offset->sec; |
| 39 | } |
| 40 | |
James Nuss | 437c534 | 2011-11-02 13:39:34 -0700 | [diff] [blame] | 41 | static void pps_echo_client_default(struct pps_device *pps, int event, |
| 42 | void *data) |
| 43 | { |
| 44 | dev_info(pps->dev, "echo %s %s\n", |
| 45 | event & PPS_CAPTUREASSERT ? "assert" : "", |
| 46 | event & PPS_CAPTURECLEAR ? "clear" : ""); |
| 47 | } |
| 48 | |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 49 | /* |
| 50 | * Exported functions |
| 51 | */ |
| 52 | |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 53 | /* pps_register_source - add a PPS source in the system |
| 54 | * @info: the PPS info struct |
| 55 | * @default_params: the default PPS parameters of the new source |
| 56 | * |
| 57 | * This function is used to add a new PPS source in the system. The new |
| 58 | * source is described by info's fields and it will have, as default PPS |
| 59 | * parameters, the ones specified into default_params. |
| 60 | * |
YueHaibing | 3b1ad360 | 2018-11-26 18:24:22 +0800 | [diff] [blame] | 61 | * The function returns, in case of success, the PPS device. Otherwise |
| 62 | * ERR_PTR(errno). |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 63 | */ |
| 64 | |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 65 | struct pps_device *pps_register_source(struct pps_source_info *info, |
| 66 | int default_params) |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 67 | { |
| 68 | struct pps_device *pps; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 69 | int err; |
| 70 | |
| 71 | /* Sanity checks */ |
| 72 | if ((info->mode & default_params) != default_params) { |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame] | 73 | pr_err("%s: unsupported default parameters\n", |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 74 | info->name); |
| 75 | err = -EINVAL; |
| 76 | goto pps_register_source_exit; |
| 77 | } |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 78 | if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) { |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame] | 79 | pr_err("%s: unspecified time format\n", |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 80 | info->name); |
| 81 | err = -EINVAL; |
| 82 | goto pps_register_source_exit; |
| 83 | } |
| 84 | |
| 85 | /* Allocate memory for the new PPS source struct */ |
| 86 | pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL); |
| 87 | if (pps == NULL) { |
| 88 | err = -ENOMEM; |
| 89 | goto pps_register_source_exit; |
| 90 | } |
| 91 | |
Tejun Heo | 19dd2da | 2013-02-27 17:04:38 -0800 | [diff] [blame] | 92 | /* These initializations must be done before calling idr_alloc() |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 93 | * in order to avoid reces into pps_event(). |
| 94 | */ |
| 95 | pps->params.api_version = PPS_API_VERS; |
| 96 | pps->params.mode = default_params; |
| 97 | pps->info = *info; |
| 98 | |
James Nuss | 437c534 | 2011-11-02 13:39:34 -0700 | [diff] [blame] | 99 | /* check for default echo function */ |
| 100 | if ((pps->info.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) && |
| 101 | pps->info.echo == NULL) |
| 102 | pps->info.echo = pps_echo_client_default; |
| 103 | |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 104 | init_waitqueue_head(&pps->queue); |
| 105 | spin_lock_init(&pps->lock); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 106 | |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 107 | /* Create the char device */ |
| 108 | err = pps_register_cdev(pps); |
| 109 | if (err < 0) { |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame] | 110 | pr_err("%s: unable to create char device\n", |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 111 | info->name); |
Alexander Gordeev | 083e586 | 2011-01-12 17:00:53 -0800 | [diff] [blame] | 112 | goto kfree_pps; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame] | 115 | dev_info(pps->dev, "new PPS source %s\n", info->name); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 116 | |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 117 | return pps; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 118 | |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 119 | kfree_pps: |
| 120 | kfree(pps); |
| 121 | |
| 122 | pps_register_source_exit: |
Alexander Gordeev | 7f7cce7 | 2011-01-12 17:00:52 -0800 | [diff] [blame] | 123 | pr_err("%s: unable to register source\n", info->name); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 124 | |
YueHaibing | 3b1ad360 | 2018-11-26 18:24:22 +0800 | [diff] [blame] | 125 | return ERR_PTR(err); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 126 | } |
| 127 | EXPORT_SYMBOL(pps_register_source); |
| 128 | |
| 129 | /* pps_unregister_source - remove a PPS source from the system |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 130 | * @pps: the PPS source |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 131 | * |
| 132 | * This function is used to remove a previously registered PPS source from |
| 133 | * the system. |
| 134 | */ |
| 135 | |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 136 | void pps_unregister_source(struct pps_device *pps) |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 137 | { |
Alexander Gordeev | 717c033 | 2011-01-12 17:00:58 -0800 | [diff] [blame] | 138 | pps_kc_remove(pps); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 139 | pps_unregister_cdev(pps); |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 140 | |
Alexander Gordeev | 083e586 | 2011-01-12 17:00:53 -0800 | [diff] [blame] | 141 | /* don't have to kfree(pps) here because it will be done on |
| 142 | * device destruction */ |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 143 | } |
| 144 | EXPORT_SYMBOL(pps_unregister_source); |
| 145 | |
| 146 | /* pps_event - register a PPS event into the system |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 147 | * @pps: the PPS device |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 148 | * @ts: the event timestamp |
| 149 | * @event: the event type |
| 150 | * @data: userdef pointer |
| 151 | * |
| 152 | * This function is used by each PPS client in order to register a new |
| 153 | * PPS event into the system (it's usually called inside an IRQ handler). |
| 154 | * |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 155 | * If an echo function is associated with the PPS device it will be called |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 156 | * as: |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 157 | * pps->info.echo(pps, event, data); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 158 | */ |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 159 | void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, |
| 160 | void *data) |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 161 | { |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 162 | unsigned long flags; |
Rodolfo Giometti | 276b282 | 2009-11-11 14:26:54 -0800 | [diff] [blame] | 163 | int captured = 0; |
Alexander Gordeev | 99b0d36 | 2011-02-25 14:44:30 -0800 | [diff] [blame] | 164 | struct pps_ktime ts_real = { .sec = 0, .nsec = 0, .flags = 0 }; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 165 | |
Alexander Gordeev | 29f347c | 2011-01-12 17:00:54 -0800 | [diff] [blame] | 166 | /* check event type */ |
| 167 | BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 168 | |
Arnd Bergmann | ade1bdf | 2015-09-28 22:21:31 +0200 | [diff] [blame] | 169 | dev_dbg(pps->dev, "PPS event at %lld.%09ld\n", |
| 170 | (s64)ts->ts_real.tv_sec, ts->ts_real.tv_nsec); |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 171 | |
| 172 | timespec_to_pps_ktime(&ts_real, ts->ts_real); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 173 | |
| 174 | spin_lock_irqsave(&pps->lock, flags); |
| 175 | |
| 176 | /* Must call the echo function? */ |
| 177 | if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR))) |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 178 | pps->info.echo(pps, event, data); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 179 | |
| 180 | /* Check the event */ |
| 181 | pps->current_mode = pps->params.mode; |
Alexander Gordeev | 818b9ee | 2011-01-12 17:00:54 -0800 | [diff] [blame] | 182 | if (event & pps->params.mode & PPS_CAPTUREASSERT) { |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 183 | /* We have to add an offset? */ |
| 184 | if (pps->params.mode & PPS_OFFSETASSERT) |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 185 | pps_add_offset(&ts_real, |
| 186 | &pps->params.assert_off_tu); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 187 | |
| 188 | /* Save the time stamp */ |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 189 | pps->assert_tu = ts_real; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 190 | pps->assert_sequence++; |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 191 | dev_dbg(pps->dev, "capture assert seq #%u\n", |
| 192 | pps->assert_sequence); |
Rodolfo Giometti | 276b282 | 2009-11-11 14:26:54 -0800 | [diff] [blame] | 193 | |
| 194 | captured = ~0; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 195 | } |
Alexander Gordeev | 818b9ee | 2011-01-12 17:00:54 -0800 | [diff] [blame] | 196 | if (event & pps->params.mode & PPS_CAPTURECLEAR) { |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 197 | /* We have to add an offset? */ |
| 198 | if (pps->params.mode & PPS_OFFSETCLEAR) |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 199 | pps_add_offset(&ts_real, |
| 200 | &pps->params.clear_off_tu); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 201 | |
| 202 | /* Save the time stamp */ |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 203 | pps->clear_tu = ts_real; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 204 | pps->clear_sequence++; |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 205 | dev_dbg(pps->dev, "capture clear seq #%u\n", |
| 206 | pps->clear_sequence); |
Rodolfo Giometti | 276b282 | 2009-11-11 14:26:54 -0800 | [diff] [blame] | 207 | |
| 208 | captured = ~0; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Alexander Gordeev | 717c033 | 2011-01-12 17:00:58 -0800 | [diff] [blame] | 211 | pps_kc_event(pps, ts, event); |
| 212 | |
Alexander Gordeev | 7a21a3c | 2011-01-12 17:00:49 -0800 | [diff] [blame] | 213 | /* Wake up if captured something */ |
Rodolfo Giometti | 276b282 | 2009-11-11 14:26:54 -0800 | [diff] [blame] | 214 | if (captured) { |
Alexander Gordeev | 3003d55 | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 215 | pps->last_ev++; |
| 216 | wake_up_interruptible_all(&pps->queue); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 217 | |
Rodolfo Giometti | 276b282 | 2009-11-11 14:26:54 -0800 | [diff] [blame] | 218 | kill_fasync(&pps->async_queue, SIGIO, POLL_IN); |
| 219 | } |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 220 | |
| 221 | spin_unlock_irqrestore(&pps->lock, flags); |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 222 | } |
| 223 | EXPORT_SYMBOL(pps_event); |