blob: cba1b43f7519b554e20ed787577a8f1f3d04c011 [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>
Alexander Gordeev717c0332011-01-12 17:00:58 -080029#include <linux/timex.h>
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070030#include <linux/spinlock.h>
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070031#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
Alexander Gordeev717c0332011-01-12 17:00:58 -080035#include "kc.h"
36
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070037/*
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070038 * Local functions
39 */
40
41static 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
55/*
56 * Exported functions
57 */
58
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070059/* pps_register_source - add a PPS source in the system
60 * @info: the PPS info struct
61 * @default_params: the default PPS parameters of the new source
62 *
63 * This function is used to add a new PPS source in the system. The new
64 * source is described by info's fields and it will have, as default PPS
65 * parameters, the ones specified into default_params.
66 *
Alexander Gordeev5e196d32011-01-12 17:00:51 -080067 * The function returns, in case of success, the PPS device. Otherwise NULL.
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070068 */
69
Alexander Gordeev5e196d32011-01-12 17:00:51 -080070struct pps_device *pps_register_source(struct pps_source_info *info,
71 int default_params)
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070072{
73 struct pps_device *pps;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070074 int err;
75
76 /* Sanity checks */
77 if ((info->mode & default_params) != default_params) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -080078 pr_err("%s: unsupported default parameters\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070079 info->name);
80 err = -EINVAL;
81 goto pps_register_source_exit;
82 }
83 if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
84 info->echo == NULL) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -080085 pr_err("%s: echo function is not defined\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070086 info->name);
87 err = -EINVAL;
88 goto pps_register_source_exit;
89 }
90 if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -080091 pr_err("%s: unspecified time format\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070092 info->name);
93 err = -EINVAL;
94 goto pps_register_source_exit;
95 }
96
97 /* Allocate memory for the new PPS source struct */
98 pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
99 if (pps == NULL) {
100 err = -ENOMEM;
101 goto pps_register_source_exit;
102 }
103
104 /* These initializations must be done before calling idr_get_new()
105 * in order to avoid reces into pps_event().
106 */
107 pps->params.api_version = PPS_API_VERS;
108 pps->params.mode = default_params;
109 pps->info = *info;
110
111 init_waitqueue_head(&pps->queue);
112 spin_lock_init(&pps->lock);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700113
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700114 /* Create the char device */
115 err = pps_register_cdev(pps);
116 if (err < 0) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800117 pr_err("%s: unable to create char device\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700118 info->name);
Alexander Gordeev083e5862011-01-12 17:00:53 -0800119 goto kfree_pps;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700120 }
121
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800122 dev_info(pps->dev, "new PPS source %s\n", info->name);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700123
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800124 return pps;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700125
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700126kfree_pps:
127 kfree(pps);
128
129pps_register_source_exit:
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800130 pr_err("%s: unable to register source\n", info->name);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700131
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800132 return NULL;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700133}
134EXPORT_SYMBOL(pps_register_source);
135
136/* pps_unregister_source - remove a PPS source from the system
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800137 * @pps: the PPS source
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700138 *
139 * This function is used to remove a previously registered PPS source from
140 * the system.
141 */
142
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800143void pps_unregister_source(struct pps_device *pps)
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700144{
Alexander Gordeev717c0332011-01-12 17:00:58 -0800145 pps_kc_remove(pps);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700146 pps_unregister_cdev(pps);
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800147
Alexander Gordeev083e5862011-01-12 17:00:53 -0800148 /* don't have to kfree(pps) here because it will be done on
149 * device destruction */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700150}
151EXPORT_SYMBOL(pps_unregister_source);
152
153/* pps_event - register a PPS event into the system
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800154 * @pps: the PPS device
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700155 * @ts: the event timestamp
156 * @event: the event type
157 * @data: userdef pointer
158 *
159 * This function is used by each PPS client in order to register a new
160 * PPS event into the system (it's usually called inside an IRQ handler).
161 *
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800162 * If an echo function is associated with the PPS device it will be called
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700163 * as:
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800164 * pps->info.echo(pps, event, data);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700165 */
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800166void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
167 void *data)
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700168{
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700169 unsigned long flags;
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800170 int captured = 0;
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800171 struct pps_ktime ts_real;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700172
Alexander Gordeev29f347c2011-01-12 17:00:54 -0800173 /* check event type */
174 BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700175
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800176 dev_dbg(pps->dev, "PPS event at %ld.%09ld\n",
177 ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800178
179 timespec_to_pps_ktime(&ts_real, ts->ts_real);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700180
181 spin_lock_irqsave(&pps->lock, flags);
182
183 /* Must call the echo function? */
184 if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800185 pps->info.echo(pps, event, data);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700186
187 /* Check the event */
188 pps->current_mode = pps->params.mode;
Alexander Gordeev818b9ee2011-01-12 17:00:54 -0800189 if (event & pps->params.mode & PPS_CAPTUREASSERT) {
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700190 /* We have to add an offset? */
191 if (pps->params.mode & PPS_OFFSETASSERT)
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800192 pps_add_offset(&ts_real,
193 &pps->params.assert_off_tu);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700194
195 /* Save the time stamp */
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800196 pps->assert_tu = ts_real;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700197 pps->assert_sequence++;
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800198 dev_dbg(pps->dev, "capture assert seq #%u\n",
199 pps->assert_sequence);
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800200
201 captured = ~0;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700202 }
Alexander Gordeev818b9ee2011-01-12 17:00:54 -0800203 if (event & pps->params.mode & PPS_CAPTURECLEAR) {
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700204 /* We have to add an offset? */
205 if (pps->params.mode & PPS_OFFSETCLEAR)
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800206 pps_add_offset(&ts_real,
207 &pps->params.clear_off_tu);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700208
209 /* Save the time stamp */
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800210 pps->clear_tu = ts_real;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700211 pps->clear_sequence++;
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800212 dev_dbg(pps->dev, "capture clear seq #%u\n",
213 pps->clear_sequence);
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800214
215 captured = ~0;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700216 }
217
Alexander Gordeev717c0332011-01-12 17:00:58 -0800218 pps_kc_event(pps, ts, event);
219
Alexander Gordeev7a21a3c2011-01-12 17:00:49 -0800220 /* Wake up if captured something */
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800221 if (captured) {
Alexander Gordeev3003d552011-01-12 17:00:50 -0800222 pps->last_ev++;
223 wake_up_interruptible_all(&pps->queue);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700224
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800225 kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
226 }
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700227
228 spin_unlock_irqrestore(&pps->lock, flags);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700229}
230EXPORT_SYMBOL(pps_event);