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