mm: update get_user_pages_longterm to migrate pages allocated from CMA region
[linux-2.6-block.git] / drivers / pps / kapi.c
CommitLineData
eae9d2ba
RG
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
7f7cce74 22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
eae9d2ba
RG
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>
717c0336 29#include <linux/timex.h>
eae9d2ba 30#include <linux/spinlock.h>
eae9d2ba
RG
31#include <linux/fs.h>
32#include <linux/pps_kernel.h>
5a0e3ad6 33#include <linux/slab.h>
eae9d2ba 34
717c0336
AG
35#include "kc.h"
36
eae9d2ba
RG
37/*
38 * 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
437c5341
JN
55static void pps_echo_client_default(struct pps_device *pps, int event,
56 void *data)
57{
58 dev_info(pps->dev, "echo %s %s\n",
59 event & PPS_CAPTUREASSERT ? "assert" : "",
60 event & PPS_CAPTURECLEAR ? "clear" : "");
61}
62
eae9d2ba
RG
63/*
64 * Exported functions
65 */
66
eae9d2ba
RG
67/* pps_register_source - add a PPS source in the system
68 * @info: the PPS info struct
69 * @default_params: the default PPS parameters of the new source
70 *
71 * This function is used to add a new PPS source in the system. The new
72 * source is described by info's fields and it will have, as default PPS
73 * parameters, the ones specified into default_params.
74 *
3b1ad360
Y
75 * The function returns, in case of success, the PPS device. Otherwise
76 * ERR_PTR(errno).
eae9d2ba
RG
77 */
78
5e196d34
AG
79struct pps_device *pps_register_source(struct pps_source_info *info,
80 int default_params)
eae9d2ba
RG
81{
82 struct pps_device *pps;
eae9d2ba
RG
83 int err;
84
85 /* Sanity checks */
86 if ((info->mode & default_params) != default_params) {
7f7cce74 87 pr_err("%s: unsupported default parameters\n",
eae9d2ba
RG
88 info->name);
89 err = -EINVAL;
90 goto pps_register_source_exit;
91 }
eae9d2ba 92 if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
7f7cce74 93 pr_err("%s: unspecified time format\n",
eae9d2ba
RG
94 info->name);
95 err = -EINVAL;
96 goto pps_register_source_exit;
97 }
98
99 /* Allocate memory for the new PPS source struct */
100 pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
101 if (pps == NULL) {
102 err = -ENOMEM;
103 goto pps_register_source_exit;
104 }
105
19dd2da3 106 /* These initializations must be done before calling idr_alloc()
eae9d2ba
RG
107 * in order to avoid reces into pps_event().
108 */
109 pps->params.api_version = PPS_API_VERS;
110 pps->params.mode = default_params;
111 pps->info = *info;
112
437c5341
JN
113 /* check for default echo function */
114 if ((pps->info.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) &&
115 pps->info.echo == NULL)
116 pps->info.echo = pps_echo_client_default;
117
eae9d2ba
RG
118 init_waitqueue_head(&pps->queue);
119 spin_lock_init(&pps->lock);
eae9d2ba 120
eae9d2ba
RG
121 /* Create the char device */
122 err = pps_register_cdev(pps);
123 if (err < 0) {
7f7cce74 124 pr_err("%s: unable to create char device\n",
eae9d2ba 125 info->name);
083e5866 126 goto kfree_pps;
eae9d2ba
RG
127 }
128
7f7cce74 129 dev_info(pps->dev, "new PPS source %s\n", info->name);
eae9d2ba 130
5e196d34 131 return pps;
eae9d2ba 132
eae9d2ba
RG
133kfree_pps:
134 kfree(pps);
135
136pps_register_source_exit:
7f7cce74 137 pr_err("%s: unable to register source\n", info->name);
eae9d2ba 138
3b1ad360 139 return ERR_PTR(err);
eae9d2ba
RG
140}
141EXPORT_SYMBOL(pps_register_source);
142
143/* pps_unregister_source - remove a PPS source from the system
5e196d34 144 * @pps: the PPS source
eae9d2ba
RG
145 *
146 * This function is used to remove a previously registered PPS source from
147 * the system.
148 */
149
5e196d34 150void pps_unregister_source(struct pps_device *pps)
eae9d2ba 151{
717c0336 152 pps_kc_remove(pps);
5e196d34 153 pps_unregister_cdev(pps);
eae9d2ba 154
083e5866
AG
155 /* don't have to kfree(pps) here because it will be done on
156 * device destruction */
eae9d2ba
RG
157}
158EXPORT_SYMBOL(pps_unregister_source);
159
160/* pps_event - register a PPS event into the system
5e196d34 161 * @pps: the PPS device
eae9d2ba
RG
162 * @ts: the event timestamp
163 * @event: the event type
164 * @data: userdef pointer
165 *
166 * This function is used by each PPS client in order to register a new
167 * PPS event into the system (it's usually called inside an IRQ handler).
168 *
5e196d34 169 * If an echo function is associated with the PPS device it will be called
eae9d2ba 170 * as:
5e196d34 171 * pps->info.echo(pps, event, data);
eae9d2ba 172 */
5e196d34
AG
173void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
174 void *data)
eae9d2ba 175{
eae9d2ba 176 unsigned long flags;
276b282e 177 int captured = 0;
99b0d365 178 struct pps_ktime ts_real = { .sec = 0, .nsec = 0, .flags = 0 };
eae9d2ba 179
29f347c9
AG
180 /* check event type */
181 BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
eae9d2ba 182
ade1bdff
AB
183 dev_dbg(pps->dev, "PPS event at %lld.%09ld\n",
184 (s64)ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
6f4229b5
AG
185
186 timespec_to_pps_ktime(&ts_real, ts->ts_real);
eae9d2ba
RG
187
188 spin_lock_irqsave(&pps->lock, flags);
189
190 /* Must call the echo function? */
191 if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
5e196d34 192 pps->info.echo(pps, event, data);
eae9d2ba
RG
193
194 /* Check the event */
195 pps->current_mode = pps->params.mode;
818b9eef 196 if (event & pps->params.mode & PPS_CAPTUREASSERT) {
eae9d2ba
RG
197 /* We have to add an offset? */
198 if (pps->params.mode & PPS_OFFSETASSERT)
6f4229b5
AG
199 pps_add_offset(&ts_real,
200 &pps->params.assert_off_tu);
eae9d2ba
RG
201
202 /* Save the time stamp */
6f4229b5 203 pps->assert_tu = ts_real;
eae9d2ba 204 pps->assert_sequence++;
5e196d34
AG
205 dev_dbg(pps->dev, "capture assert seq #%u\n",
206 pps->assert_sequence);
276b282e
RG
207
208 captured = ~0;
eae9d2ba 209 }
818b9eef 210 if (event & pps->params.mode & PPS_CAPTURECLEAR) {
eae9d2ba
RG
211 /* We have to add an offset? */
212 if (pps->params.mode & PPS_OFFSETCLEAR)
6f4229b5
AG
213 pps_add_offset(&ts_real,
214 &pps->params.clear_off_tu);
eae9d2ba
RG
215
216 /* Save the time stamp */
6f4229b5 217 pps->clear_tu = ts_real;
eae9d2ba 218 pps->clear_sequence++;
5e196d34
AG
219 dev_dbg(pps->dev, "capture clear seq #%u\n",
220 pps->clear_sequence);
276b282e
RG
221
222 captured = ~0;
eae9d2ba
RG
223 }
224
717c0336
AG
225 pps_kc_event(pps, ts, event);
226
7a21a3cc 227 /* Wake up if captured something */
276b282e 228 if (captured) {
3003d55b
AG
229 pps->last_ev++;
230 wake_up_interruptible_all(&pps->queue);
eae9d2ba 231
276b282e
RG
232 kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
233 }
eae9d2ba
RG
234
235 spin_unlock_irqrestore(&pps->lock, flags);
eae9d2ba
RG
236}
237EXPORT_SYMBOL(pps_event);