0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/sched.h>
0014 #include <linux/time.h>
0015 #include <linux/timex.h>
0016 #include <linux/spinlock.h>
0017 #include <linux/fs.h>
0018 #include <linux/pps_kernel.h>
0019 #include <linux/slab.h>
0020
0021 #include "kc.h"
0022
0023
0024
0025
0026
0027 static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
0028 {
0029 ts->nsec += offset->nsec;
0030 while (ts->nsec >= NSEC_PER_SEC) {
0031 ts->nsec -= NSEC_PER_SEC;
0032 ts->sec++;
0033 }
0034 while (ts->nsec < 0) {
0035 ts->nsec += NSEC_PER_SEC;
0036 ts->sec--;
0037 }
0038 ts->sec += offset->sec;
0039 }
0040
0041 static void pps_echo_client_default(struct pps_device *pps, int event,
0042 void *data)
0043 {
0044 dev_info(pps->dev, "echo %s %s\n",
0045 event & PPS_CAPTUREASSERT ? "assert" : "",
0046 event & PPS_CAPTURECLEAR ? "clear" : "");
0047 }
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065 struct pps_device *pps_register_source(struct pps_source_info *info,
0066 int default_params)
0067 {
0068 struct pps_device *pps;
0069 int err;
0070
0071
0072 if ((info->mode & default_params) != default_params) {
0073 pr_err("%s: unsupported default parameters\n",
0074 info->name);
0075 err = -EINVAL;
0076 goto pps_register_source_exit;
0077 }
0078 if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
0079 pr_err("%s: unspecified time format\n",
0080 info->name);
0081 err = -EINVAL;
0082 goto pps_register_source_exit;
0083 }
0084
0085
0086 pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
0087 if (pps == NULL) {
0088 err = -ENOMEM;
0089 goto pps_register_source_exit;
0090 }
0091
0092
0093
0094
0095 pps->params.api_version = PPS_API_VERS;
0096 pps->params.mode = default_params;
0097 pps->info = *info;
0098
0099
0100 if ((pps->info.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) &&
0101 pps->info.echo == NULL)
0102 pps->info.echo = pps_echo_client_default;
0103
0104 init_waitqueue_head(&pps->queue);
0105 spin_lock_init(&pps->lock);
0106
0107
0108 err = pps_register_cdev(pps);
0109 if (err < 0) {
0110 pr_err("%s: unable to create char device\n",
0111 info->name);
0112 goto kfree_pps;
0113 }
0114
0115 dev_info(pps->dev, "new PPS source %s\n", info->name);
0116
0117 return pps;
0118
0119 kfree_pps:
0120 kfree(pps);
0121
0122 pps_register_source_exit:
0123 pr_err("%s: unable to register source\n", info->name);
0124
0125 return ERR_PTR(err);
0126 }
0127 EXPORT_SYMBOL(pps_register_source);
0128
0129
0130
0131
0132
0133
0134
0135
0136 void pps_unregister_source(struct pps_device *pps)
0137 {
0138 pps_kc_remove(pps);
0139 pps_unregister_cdev(pps);
0140
0141
0142
0143 }
0144 EXPORT_SYMBOL(pps_unregister_source);
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159 void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
0160 void *data)
0161 {
0162 unsigned long flags;
0163 int captured = 0;
0164 struct pps_ktime ts_real = { .sec = 0, .nsec = 0, .flags = 0 };
0165
0166
0167 BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
0168
0169 dev_dbg(pps->dev, "PPS event at %lld.%09ld\n",
0170 (s64)ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
0171
0172 timespec_to_pps_ktime(&ts_real, ts->ts_real);
0173
0174 spin_lock_irqsave(&pps->lock, flags);
0175
0176
0177 if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
0178 pps->info.echo(pps, event, data);
0179
0180
0181 pps->current_mode = pps->params.mode;
0182 if (event & pps->params.mode & PPS_CAPTUREASSERT) {
0183
0184 if (pps->params.mode & PPS_OFFSETASSERT)
0185 pps_add_offset(&ts_real,
0186 &pps->params.assert_off_tu);
0187
0188
0189 pps->assert_tu = ts_real;
0190 pps->assert_sequence++;
0191 dev_dbg(pps->dev, "capture assert seq #%u\n",
0192 pps->assert_sequence);
0193
0194 captured = ~0;
0195 }
0196 if (event & pps->params.mode & PPS_CAPTURECLEAR) {
0197
0198 if (pps->params.mode & PPS_OFFSETCLEAR)
0199 pps_add_offset(&ts_real,
0200 &pps->params.clear_off_tu);
0201
0202
0203 pps->clear_tu = ts_real;
0204 pps->clear_sequence++;
0205 dev_dbg(pps->dev, "capture clear seq #%u\n",
0206 pps->clear_sequence);
0207
0208 captured = ~0;
0209 }
0210
0211 pps_kc_event(pps, ts, event);
0212
0213
0214 if (captured) {
0215 pps->last_ev++;
0216 wake_up_interruptible_all(&pps->queue);
0217
0218 kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
0219 }
0220
0221 spin_unlock_irqrestore(&pps->lock, flags);
0222 }
0223 EXPORT_SYMBOL(pps_event);