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