blob: 98d4012ca595eb3238ba1919073fe8c34077b075 [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
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 Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070033
34/*
Alexander Gordeev5e196d32011-01-12 17:00:51 -080035 * Local variables
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070036 */
37
Alexander Gordeev5e196d32011-01-12 17:00:51 -080038static DEFINE_SPINLOCK(pps_idr_lock);
39static DEFINE_IDR(pps_idr);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070040
41/*
42 * Local functions
43 */
44
45static 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 Giomettieae9d2b2009-06-17 16:28:37 -070063/* 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 Gordeev5e196d32011-01-12 17:00:51 -080071 * The function returns, in case of success, the PPS device. Otherwise NULL.
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070072 */
73
Alexander Gordeev5e196d32011-01-12 17:00:51 -080074struct pps_device *pps_register_source(struct pps_source_info *info,
75 int default_params)
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070076{
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 Giomettieae9d2b2009-06-17 16:28:37 -0700118
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 Gordeev5e196d32011-01-12 17:00:51 -0800160 return pps;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700161
162free_idr:
163 spin_lock_irq(&pps_idr_lock);
164 idr_remove(&pps_idr, id);
165 spin_unlock_irq(&pps_idr_lock);
166
167kfree_pps:
168 kfree(pps);
169
170pps_register_source_exit:
171 printk(KERN_ERR "pps: %s: unable to register source\n", info->name);
172
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800173 return NULL;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700174}
175EXPORT_SYMBOL(pps_register_source);
176
177/* pps_unregister_source - remove a PPS source from the system
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800178 * @pps: the PPS source
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700179 *
180 * This function is used to remove a previously registered PPS source from
181 * the system.
182 */
183
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800184void pps_unregister_source(struct pps_device *pps)
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700185{
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800186 unsigned int id = pps->id;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700187
188 pps_unregister_cdev(pps);
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800189
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 Giomettieae9d2b2009-06-17 16:28:37 -0700195}
196EXPORT_SYMBOL(pps_unregister_source);
197
198/* pps_event - register a PPS event into the system
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800199 * @pps: the PPS device
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700200 * @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 Gordeev5e196d32011-01-12 17:00:51 -0800207 * If an echo function is associated with the PPS device it will be called
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700208 * as:
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800209 * pps->info.echo(pps, event, data);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700210 */
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800211void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
212 void *data)
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700213{
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700214 unsigned long flags;
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800215 int captured = 0;
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800216 struct pps_ktime ts_real;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700217
218 if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800219 dev_err(pps->dev, "unknown event (%x)\n", event);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700220 return;
221 }
222
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800223 dev_dbg(pps->dev, "PPS event at %ld.%09ld\n",
224 ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800225
226 timespec_to_pps_ktime(&ts_real, ts->ts_real);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700227
228 spin_lock_irqsave(&pps->lock, flags);
229
230 /* Must call the echo function? */
231 if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800232 pps->info.echo(pps, event, data);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700233
234 /* Check the event */
235 pps->current_mode = pps->params.mode;
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800236 if ((event & PPS_CAPTUREASSERT) &
237 (pps->params.mode & PPS_CAPTUREASSERT)) {
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700238 /* We have to add an offset? */
239 if (pps->params.mode & PPS_OFFSETASSERT)
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800240 pps_add_offset(&ts_real,
241 &pps->params.assert_off_tu);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700242
243 /* Save the time stamp */
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800244 pps->assert_tu = ts_real;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700245 pps->assert_sequence++;
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800246 dev_dbg(pps->dev, "capture assert seq #%u\n",
247 pps->assert_sequence);
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800248
249 captured = ~0;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700250 }
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800251 if ((event & PPS_CAPTURECLEAR) &
252 (pps->params.mode & PPS_CAPTURECLEAR)) {
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700253 /* We have to add an offset? */
254 if (pps->params.mode & PPS_OFFSETCLEAR)
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800255 pps_add_offset(&ts_real,
256 &pps->params.clear_off_tu);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700257
258 /* Save the time stamp */
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800259 pps->clear_tu = ts_real;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700260 pps->clear_sequence++;
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800261 dev_dbg(pps->dev, "capture clear seq #%u\n",
262 pps->clear_sequence);
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800263
264 captured = ~0;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700265 }
266
Alexander Gordeev7a21a3c2011-01-12 17:00:49 -0800267 /* Wake up if captured something */
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800268 if (captured) {
Alexander Gordeev3003d552011-01-12 17:00:50 -0800269 pps->last_ev++;
270 wake_up_interruptible_all(&pps->queue);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700271
Rodolfo Giometti276b2822009-11-11 14:26:54 -0800272 kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
273 }
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700274
275 spin_unlock_irqrestore(&pps->lock, flags);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700276}
277EXPORT_SYMBOL(pps_event);