blob: e8847c118ea38d5fd0631ea1636e8c2d12fc204e [file] [log] [blame]
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -07001/*
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 Gordeev7f7cce72011-01-12 17:00:52 -080022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070023
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 Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070034
35/*
Alexander Gordeev5e196d32011-01-12 17:00:51 -080036 * Local variables
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070037 */
38
Alexander Gordeev5e196d32011-01-12 17:00:51 -080039static DEFINE_SPINLOCK(pps_idr_lock);
40static DEFINE_IDR(pps_idr);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070041
42/*
43 * Local functions
44 */
45
46static 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 Giomettieae9d2b2009-06-17 16:28:37 -070064/* 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 Gordeev5e196d32011-01-12 17:00:51 -080072 * The function returns, in case of success, the PPS device. Otherwise NULL.
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070073 */
74
Alexander Gordeev5e196d32011-01-12 17:00:51 -080075struct pps_device *pps_register_source(struct pps_source_info *info,
76 int default_params)
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070077{
78 struct pps_device *pps;
79 int id;
80 int err;
81
82 /* Sanity checks */
83 if ((info->mode & default_params) != default_params) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -080084 pr_err("%s: unsupported default parameters\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070085 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 Gordeev7f7cce72011-01-12 17:00:52 -080091 pr_err("%s: echo function is not defined\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070092 info->name);
93 err = -EINVAL;
94 goto pps_register_source_exit;
95 }
96 if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -080097 pr_err("%s: unspecified time format\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070098 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 Giomettieae9d2b2009-06-17 16:28:37 -0700119
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 Gordeev7f7cce72011-01-12 17:00:52 -0800142 pr_err("%s: too many PPS sources in the system\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700143 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 Gordeev7f7cce72011-01-12 17:00:52 -0800154 pr_err("%s: unable to create char device\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700155 info->name);
156 goto free_idr;
157 }
158
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800159 dev_info(pps->dev, "new PPS source %s\n", info->name);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700160
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800161 return pps;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700162
163free_idr:
164 spin_lock_irq(&pps_idr_lock);
165 idr_remove(&pps_idr, id);
166 spin_unlock_irq(&pps_idr_lock);
167
168kfree_pps:
169 kfree(pps);
170
171pps_register_source_exit:
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800172 pr_err("%s: unable to register source\n", info->name);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700173
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800174 return NULL;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700175}
176EXPORT_SYMBOL(pps_register_source);
177
178/* pps_unregister_source - remove a PPS source from the system
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800179 * @pps: the PPS source
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700180 *
181 * This function is used to remove a previously registered PPS source from
182 * the system.
183 */
184
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800185void pps_unregister_source(struct pps_device *pps)
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700186{
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800187 unsigned int id = pps->id;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700188
189 pps_unregister_cdev(pps);
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800190
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 Giomettieae9d2b2009-06-17 16:28:37 -0700196}
197EXPORT_SYMBOL(pps_unregister_source);
198
199/* pps_event - register a PPS event into the system
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800200 * @pps: the PPS device
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700201 * @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 Gordeev5e196d32011-01-12 17:00:51 -0800208 * If an echo function is associated with the PPS device it will be called
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700209 * as:
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800210 * pps->info.echo(pps, event, data);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700211 */
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800212void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
213 void *data)
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700214{
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700215 unsigned long flags;
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800216 int captured = 0;
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800217 struct pps_ktime ts_real;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700218
219 if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800220 dev_err(pps->dev, "unknown event (%x)\n", event);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700221 return;
222 }
223
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800224 dev_dbg(pps->dev, "PPS event at %ld.%09ld\n",
225 ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800226
227 timespec_to_pps_ktime(&ts_real, ts->ts_real);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700228
229 spin_lock_irqsave(&pps->lock, flags);
230
231 /* Must call the echo function? */
232 if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800233 pps->info.echo(pps, event, data);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700234
235 /* Check the event */
236 pps->current_mode = pps->params.mode;
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800237 if ((event & PPS_CAPTUREASSERT) &
238 (pps->params.mode & PPS_CAPTUREASSERT)) {
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700239 /* We have to add an offset? */
240 if (pps->params.mode & PPS_OFFSETASSERT)
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800241 pps_add_offset(&ts_real,
242 &pps->params.assert_off_tu);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700243
244 /* Save the time stamp */
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800245 pps->assert_tu = ts_real;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700246 pps->assert_sequence++;
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800247 dev_dbg(pps->dev, "capture assert seq #%u\n",
248 pps->assert_sequence);
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800249
250 captured = ~0;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700251 }
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800252 if ((event & PPS_CAPTURECLEAR) &
253 (pps->params.mode & PPS_CAPTURECLEAR)) {
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700254 /* We have to add an offset? */
255 if (pps->params.mode & PPS_OFFSETCLEAR)
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800256 pps_add_offset(&ts_real,
257 &pps->params.clear_off_tu);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700258
259 /* Save the time stamp */
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800260 pps->clear_tu = ts_real;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700261 pps->clear_sequence++;
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800262 dev_dbg(pps->dev, "capture clear seq #%u\n",
263 pps->clear_sequence);
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800264
265 captured = ~0;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700266 }
267
Alexander Gordeev7a21a3c2011-01-12 17:00:49 -0800268 /* Wake up if captured something */
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800269 if (captured) {
Alexander Gordeev3003d552011-01-12 17:00:50 -0800270 pps->last_ev++;
271 wake_up_interruptible_all(&pps->queue);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700272
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800273 kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
274 }
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700275
276 spin_unlock_irqrestore(&pps->lock, flags);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700277}
278EXPORT_SYMBOL(pps_event);