kapi.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. static void pps_echo_client_default(struct pps_device *pps, int event,
  50. void *data)
  51. {
  52. dev_info(pps->dev, "echo %s %s\n",
  53. event & PPS_CAPTUREASSERT ? "assert" : "",
  54. event & PPS_CAPTURECLEAR ? "clear" : "");
  55. }
  56. /*
  57. * Exported functions
  58. */
  59. /* pps_register_source - add a PPS source in the system
  60. * @info: the PPS info struct
  61. * @default_params: the default PPS parameters of the new source
  62. *
  63. * This function is used to add a new PPS source in the system. The new
  64. * source is described by info's fields and it will have, as default PPS
  65. * parameters, the ones specified into default_params.
  66. *
  67. * The function returns, in case of success, the PPS device. Otherwise NULL.
  68. */
  69. struct pps_device *pps_register_source(struct pps_source_info *info,
  70. int default_params)
  71. {
  72. struct pps_device *pps;
  73. int err;
  74. /* Sanity checks */
  75. if ((info->mode & default_params) != default_params) {
  76. pr_err("%s: unsupported default parameters\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_alloc()
  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. /* check for default echo function */
  100. if ((pps->info.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) &&
  101. pps->info.echo == NULL)
  102. pps->info.echo = pps_echo_client_default;
  103. init_waitqueue_head(&pps->queue);
  104. spin_lock_init(&pps->lock);
  105. /* Create the char device */
  106. err = pps_register_cdev(pps);
  107. if (err < 0) {
  108. pr_err("%s: unable to create char device\n",
  109. info->name);
  110. goto kfree_pps;
  111. }
  112. dev_info(pps->dev, "new PPS source %s\n", info->name);
  113. return pps;
  114. kfree_pps:
  115. kfree(pps);
  116. pps_register_source_exit:
  117. pr_err("%s: unable to register source\n", info->name);
  118. return NULL;
  119. }
  120. EXPORT_SYMBOL(pps_register_source);
  121. /* pps_unregister_source - remove a PPS source from the system
  122. * @pps: the PPS source
  123. *
  124. * This function is used to remove a previously registered PPS source from
  125. * the system.
  126. */
  127. void pps_unregister_source(struct pps_device *pps)
  128. {
  129. pps_kc_remove(pps);
  130. pps_unregister_cdev(pps);
  131. /* don't have to kfree(pps) here because it will be done on
  132. * device destruction */
  133. }
  134. EXPORT_SYMBOL(pps_unregister_source);
  135. /* pps_event - register a PPS event into the system
  136. * @pps: the PPS device
  137. * @ts: the event timestamp
  138. * @event: the event type
  139. * @data: userdef pointer
  140. *
  141. * This function is used by each PPS client in order to register a new
  142. * PPS event into the system (it's usually called inside an IRQ handler).
  143. *
  144. * If an echo function is associated with the PPS device it will be called
  145. * as:
  146. * pps->info.echo(pps, event, data);
  147. */
  148. void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
  149. void *data)
  150. {
  151. unsigned long flags;
  152. int captured = 0;
  153. struct pps_ktime ts_real = { .sec = 0, .nsec = 0, .flags = 0 };
  154. /* check event type */
  155. BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
  156. dev_dbg(pps->dev, "PPS event at %lld.%09ld\n",
  157. (s64)ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
  158. timespec_to_pps_ktime(&ts_real, ts->ts_real);
  159. spin_lock_irqsave(&pps->lock, flags);
  160. /* Must call the echo function? */
  161. if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
  162. pps->info.echo(pps, event, data);
  163. /* Check the event */
  164. pps->current_mode = pps->params.mode;
  165. if (event & pps->params.mode & PPS_CAPTUREASSERT) {
  166. /* We have to add an offset? */
  167. if (pps->params.mode & PPS_OFFSETASSERT)
  168. pps_add_offset(&ts_real,
  169. &pps->params.assert_off_tu);
  170. /* Save the time stamp */
  171. pps->assert_tu = ts_real;
  172. pps->assert_sequence++;
  173. dev_dbg(pps->dev, "capture assert seq #%u\n",
  174. pps->assert_sequence);
  175. captured = ~0;
  176. }
  177. if (event & pps->params.mode & PPS_CAPTURECLEAR) {
  178. /* We have to add an offset? */
  179. if (pps->params.mode & PPS_OFFSETCLEAR)
  180. pps_add_offset(&ts_real,
  181. &pps->params.clear_off_tu);
  182. /* Save the time stamp */
  183. pps->clear_tu = ts_real;
  184. pps->clear_sequence++;
  185. dev_dbg(pps->dev, "capture clear seq #%u\n",
  186. pps->clear_sequence);
  187. captured = ~0;
  188. }
  189. pps_kc_event(pps, ts, event);
  190. /* Wake up if captured something */
  191. if (captured) {
  192. pps->last_ev++;
  193. wake_up_interruptible_all(&pps->queue);
  194. kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
  195. }
  196. spin_unlock_irqrestore(&pps->lock, flags);
  197. }
  198. EXPORT_SYMBOL(pps_event);