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