kapi.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * kernel API
  4. *
  5. * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/sched.h>
  12. #include <linux/time.h>
  13. #include <linux/timex.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/fs.h>
  16. #include <linux/pps_kernel.h>
  17. #include <linux/slab.h>
  18. #include "kc.h"
  19. /*
  20. * Local functions
  21. */
  22. static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
  23. {
  24. ts->nsec += offset->nsec;
  25. while (ts->nsec >= NSEC_PER_SEC) {
  26. ts->nsec -= NSEC_PER_SEC;
  27. ts->sec++;
  28. }
  29. while (ts->nsec < 0) {
  30. ts->nsec += NSEC_PER_SEC;
  31. ts->sec--;
  32. }
  33. ts->sec += offset->sec;
  34. }
  35. static void pps_echo_client_default(struct pps_device *pps, int event,
  36. void *data)
  37. {
  38. dev_info(pps->dev, "echo %s %s\n",
  39. event & PPS_CAPTUREASSERT ? "assert" : "",
  40. event & PPS_CAPTURECLEAR ? "clear" : "");
  41. }
  42. /*
  43. * Exported functions
  44. */
  45. /* pps_register_source - add a PPS source in the system
  46. * @info: the PPS info struct
  47. * @default_params: the default PPS parameters of the new source
  48. *
  49. * This function is used to add a new PPS source in the system. The new
  50. * source is described by info's fields and it will have, as default PPS
  51. * parameters, the ones specified into default_params.
  52. *
  53. * The function returns, in case of success, the PPS device. Otherwise
  54. * ERR_PTR(errno).
  55. */
  56. struct pps_device *pps_register_source(struct pps_source_info *info,
  57. int default_params)
  58. {
  59. struct pps_device *pps;
  60. int err;
  61. /* Sanity checks */
  62. if ((info->mode & default_params) != default_params) {
  63. pr_err("%s: unsupported default parameters\n",
  64. info->name);
  65. err = -EINVAL;
  66. goto pps_register_source_exit;
  67. }
  68. if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
  69. pr_err("%s: unspecified time format\n",
  70. info->name);
  71. err = -EINVAL;
  72. goto pps_register_source_exit;
  73. }
  74. /* Allocate memory for the new PPS source struct */
  75. pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
  76. if (pps == NULL) {
  77. err = -ENOMEM;
  78. goto pps_register_source_exit;
  79. }
  80. /* These initializations must be done before calling idr_alloc()
  81. * in order to avoid reces into pps_event().
  82. */
  83. pps->params.api_version = PPS_API_VERS;
  84. pps->params.mode = default_params;
  85. pps->info = *info;
  86. /* check for default echo function */
  87. if ((pps->info.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) &&
  88. pps->info.echo == NULL)
  89. pps->info.echo = pps_echo_client_default;
  90. init_waitqueue_head(&pps->queue);
  91. spin_lock_init(&pps->lock);
  92. /* Create the char device */
  93. err = pps_register_cdev(pps);
  94. if (err < 0) {
  95. pr_err("%s: unable to create char device\n",
  96. info->name);
  97. goto kfree_pps;
  98. }
  99. dev_info(pps->dev, "new PPS source %s\n", info->name);
  100. return pps;
  101. kfree_pps:
  102. kfree(pps);
  103. pps_register_source_exit:
  104. pr_err("%s: unable to register source\n", info->name);
  105. return ERR_PTR(err);
  106. }
  107. EXPORT_SYMBOL(pps_register_source);
  108. /* pps_unregister_source - remove a PPS source from the system
  109. * @pps: the PPS source
  110. *
  111. * This function is used to remove a previously registered PPS source from
  112. * the system.
  113. */
  114. void pps_unregister_source(struct pps_device *pps)
  115. {
  116. pps_kc_remove(pps);
  117. pps_unregister_cdev(pps);
  118. /* don't have to kfree(pps) here because it will be done on
  119. * device destruction */
  120. }
  121. EXPORT_SYMBOL(pps_unregister_source);
  122. /* pps_event - register a PPS event into the system
  123. * @pps: the PPS device
  124. * @ts: the event timestamp
  125. * @event: the event type
  126. * @data: userdef pointer
  127. *
  128. * This function is used by each PPS client in order to register a new
  129. * PPS event into the system (it's usually called inside an IRQ handler).
  130. *
  131. * If an echo function is associated with the PPS device it will be called
  132. * as:
  133. * pps->info.echo(pps, event, data);
  134. */
  135. void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
  136. void *data)
  137. {
  138. unsigned long flags;
  139. int captured = 0;
  140. struct pps_ktime ts_real = { .sec = 0, .nsec = 0, .flags = 0 };
  141. /* check event type */
  142. BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
  143. dev_dbg(pps->dev, "PPS event at %lld.%09ld\n",
  144. (s64)ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
  145. timespec_to_pps_ktime(&ts_real, ts->ts_real);
  146. spin_lock_irqsave(&pps->lock, flags);
  147. /* Must call the echo function? */
  148. if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
  149. pps->info.echo(pps, event, data);
  150. /* Check the event */
  151. pps->current_mode = pps->params.mode;
  152. if (event & pps->params.mode & PPS_CAPTUREASSERT) {
  153. /* We have to add an offset? */
  154. if (pps->params.mode & PPS_OFFSETASSERT)
  155. pps_add_offset(&ts_real,
  156. &pps->params.assert_off_tu);
  157. /* Save the time stamp */
  158. pps->assert_tu = ts_real;
  159. pps->assert_sequence++;
  160. dev_dbg(pps->dev, "capture assert seq #%u\n",
  161. pps->assert_sequence);
  162. captured = ~0;
  163. }
  164. if (event & pps->params.mode & PPS_CAPTURECLEAR) {
  165. /* We have to add an offset? */
  166. if (pps->params.mode & PPS_OFFSETCLEAR)
  167. pps_add_offset(&ts_real,
  168. &pps->params.clear_off_tu);
  169. /* Save the time stamp */
  170. pps->clear_tu = ts_real;
  171. pps->clear_sequence++;
  172. dev_dbg(pps->dev, "capture clear seq #%u\n",
  173. pps->clear_sequence);
  174. captured = ~0;
  175. }
  176. pps_kc_event(pps, ts, event);
  177. /* Wake up if captured something */
  178. if (captured) {
  179. pps->last_ev++;
  180. wake_up_interruptible_all(&pps->queue);
  181. kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
  182. }
  183. spin_unlock_irqrestore(&pps->lock, flags);
  184. }
  185. EXPORT_SYMBOL(pps_event);