kapi.c 5.9 KB

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