kapi.c 6.8 KB

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