Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * kernel API
0004  *
0005  * Copyright (C) 2005-2009   Rodolfo Giometti <giometti@linux.it>
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  * Local functions
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  * Exported functions
0051  */
0052 
0053 /* pps_register_source - add a PPS source in the system
0054  * @info: the PPS info struct
0055  * @default_params: the default PPS parameters of the new source
0056  *
0057  * This function is used to add a new PPS source in the system. The new
0058  * source is described by info's fields and it will have, as default PPS
0059  * parameters, the ones specified into default_params.
0060  *
0061  * The function returns, in case of success, the PPS device. Otherwise
0062  * ERR_PTR(errno).
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     /* Sanity checks */
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     /* Allocate memory for the new PPS source struct */
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     /* These initializations must be done before calling idr_alloc()
0093      * in order to avoid reces into pps_event().
0094      */
0095     pps->params.api_version = PPS_API_VERS;
0096     pps->params.mode = default_params;
0097     pps->info = *info;
0098 
0099     /* check for default echo function */
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     /* Create the char device */
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 /* pps_unregister_source - remove a PPS source from the system
0130  * @pps: the PPS source
0131  *
0132  * This function is used to remove a previously registered PPS source from
0133  * the system.
0134  */
0135 
0136 void pps_unregister_source(struct pps_device *pps)
0137 {
0138     pps_kc_remove(pps);
0139     pps_unregister_cdev(pps);
0140 
0141     /* don't have to kfree(pps) here because it will be done on
0142      * device destruction */
0143 }
0144 EXPORT_SYMBOL(pps_unregister_source);
0145 
0146 /* pps_event - register a PPS event into the system
0147  * @pps: the PPS device
0148  * @ts: the event timestamp
0149  * @event: the event type
0150  * @data: userdef pointer
0151  *
0152  * This function is used by each PPS client in order to register a new
0153  * PPS event into the system (it's usually called inside an IRQ handler).
0154  *
0155  * If an echo function is associated with the PPS device it will be called
0156  * as:
0157  *  pps->info.echo(pps, event, data);
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     /* check event type */
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     /* Must call the echo function? */
0177     if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
0178         pps->info.echo(pps, event, data);
0179 
0180     /* Check the event */
0181     pps->current_mode = pps->params.mode;
0182     if (event & pps->params.mode & PPS_CAPTUREASSERT) {
0183         /* We have to add an offset? */
0184         if (pps->params.mode & PPS_OFFSETASSERT)
0185             pps_add_offset(&ts_real,
0186                     &pps->params.assert_off_tu);
0187 
0188         /* Save the time stamp */
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         /* We have to add an offset? */
0198         if (pps->params.mode & PPS_OFFSETCLEAR)
0199             pps_add_offset(&ts_real,
0200                     &pps->params.clear_off_tu);
0201 
0202         /* Save the time stamp */
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     /* Wake up if captured something */
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);