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