pps.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * PPS core file
  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/uaccess.h>
  27. #include <linux/idr.h>
  28. #include <linux/mutex.h>
  29. #include <linux/cdev.h>
  30. #include <linux/poll.h>
  31. #include <linux/pps_kernel.h>
  32. #include <linux/slab.h>
  33. #include "kc.h"
  34. /*
  35. * Local variables
  36. */
  37. static dev_t pps_devt;
  38. static struct class *pps_class;
  39. static DEFINE_MUTEX(pps_idr_lock);
  40. static DEFINE_IDR(pps_idr);
  41. /*
  42. * Char device methods
  43. */
  44. static unsigned int pps_cdev_poll(struct file *file, poll_table *wait)
  45. {
  46. struct pps_device *pps = file->private_data;
  47. poll_wait(file, &pps->queue, wait);
  48. return POLLIN | POLLRDNORM;
  49. }
  50. static int pps_cdev_fasync(int fd, struct file *file, int on)
  51. {
  52. struct pps_device *pps = file->private_data;
  53. return fasync_helper(fd, file, on, &pps->async_queue);
  54. }
  55. static long pps_cdev_ioctl(struct file *file,
  56. unsigned int cmd, unsigned long arg)
  57. {
  58. struct pps_device *pps = file->private_data;
  59. struct pps_kparams params;
  60. void __user *uarg = (void __user *) arg;
  61. int __user *iuarg = (int __user *) arg;
  62. int err;
  63. switch (cmd) {
  64. case PPS_GETPARAMS:
  65. dev_dbg(pps->dev, "PPS_GETPARAMS\n");
  66. spin_lock_irq(&pps->lock);
  67. /* Get the current parameters */
  68. params = pps->params;
  69. spin_unlock_irq(&pps->lock);
  70. err = copy_to_user(uarg, &params, sizeof(struct pps_kparams));
  71. if (err)
  72. return -EFAULT;
  73. break;
  74. case PPS_SETPARAMS:
  75. dev_dbg(pps->dev, "PPS_SETPARAMS\n");
  76. /* Check the capabilities */
  77. if (!capable(CAP_SYS_TIME))
  78. return -EPERM;
  79. err = copy_from_user(&params, uarg, sizeof(struct pps_kparams));
  80. if (err)
  81. return -EFAULT;
  82. if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
  83. dev_dbg(pps->dev, "capture mode unspecified (%x)\n",
  84. params.mode);
  85. return -EINVAL;
  86. }
  87. /* Check for supported capabilities */
  88. if ((params.mode & ~pps->info.mode) != 0) {
  89. dev_dbg(pps->dev, "unsupported capabilities (%x)\n",
  90. params.mode);
  91. return -EINVAL;
  92. }
  93. spin_lock_irq(&pps->lock);
  94. /* Save the new parameters */
  95. pps->params = params;
  96. /* Restore the read only parameters */
  97. if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
  98. /* section 3.3 of RFC 2783 interpreted */
  99. dev_dbg(pps->dev, "time format unspecified (%x)\n",
  100. params.mode);
  101. pps->params.mode |= PPS_TSFMT_TSPEC;
  102. }
  103. if (pps->info.mode & PPS_CANWAIT)
  104. pps->params.mode |= PPS_CANWAIT;
  105. pps->params.api_version = PPS_API_VERS;
  106. spin_unlock_irq(&pps->lock);
  107. break;
  108. case PPS_GETCAP:
  109. dev_dbg(pps->dev, "PPS_GETCAP\n");
  110. err = put_user(pps->info.mode, iuarg);
  111. if (err)
  112. return -EFAULT;
  113. break;
  114. case PPS_FETCH: {
  115. struct pps_fdata fdata;
  116. unsigned int ev;
  117. dev_dbg(pps->dev, "PPS_FETCH\n");
  118. err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
  119. if (err)
  120. return -EFAULT;
  121. ev = pps->last_ev;
  122. /* Manage the timeout */
  123. if (fdata.timeout.flags & PPS_TIME_INVALID)
  124. err = wait_event_interruptible(pps->queue,
  125. ev != pps->last_ev);
  126. else {
  127. unsigned long ticks;
  128. dev_dbg(pps->dev, "timeout %lld.%09d\n",
  129. (long long) fdata.timeout.sec,
  130. fdata.timeout.nsec);
  131. ticks = fdata.timeout.sec * HZ;
  132. ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ);
  133. if (ticks != 0) {
  134. err = wait_event_interruptible_timeout(
  135. pps->queue,
  136. ev != pps->last_ev,
  137. ticks);
  138. if (err == 0)
  139. return -ETIMEDOUT;
  140. }
  141. }
  142. /* Check for pending signals */
  143. if (err == -ERESTARTSYS) {
  144. dev_dbg(pps->dev, "pending signal caught\n");
  145. return -EINTR;
  146. }
  147. /* Return the fetched timestamp */
  148. spin_lock_irq(&pps->lock);
  149. fdata.info.assert_sequence = pps->assert_sequence;
  150. fdata.info.clear_sequence = pps->clear_sequence;
  151. fdata.info.assert_tu = pps->assert_tu;
  152. fdata.info.clear_tu = pps->clear_tu;
  153. fdata.info.current_mode = pps->current_mode;
  154. spin_unlock_irq(&pps->lock);
  155. err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
  156. if (err)
  157. return -EFAULT;
  158. break;
  159. }
  160. case PPS_KC_BIND: {
  161. struct pps_bind_args bind_args;
  162. dev_dbg(pps->dev, "PPS_KC_BIND\n");
  163. /* Check the capabilities */
  164. if (!capable(CAP_SYS_TIME))
  165. return -EPERM;
  166. if (copy_from_user(&bind_args, uarg,
  167. sizeof(struct pps_bind_args)))
  168. return -EFAULT;
  169. /* Check for supported capabilities */
  170. if ((bind_args.edge & ~pps->info.mode) != 0) {
  171. dev_err(pps->dev, "unsupported capabilities (%x)\n",
  172. bind_args.edge);
  173. return -EINVAL;
  174. }
  175. /* Validate parameters roughly */
  176. if (bind_args.tsformat != PPS_TSFMT_TSPEC ||
  177. (bind_args.edge & ~PPS_CAPTUREBOTH) != 0 ||
  178. bind_args.consumer != PPS_KC_HARDPPS) {
  179. dev_err(pps->dev, "invalid kernel consumer bind"
  180. " parameters (%x)\n", bind_args.edge);
  181. return -EINVAL;
  182. }
  183. err = pps_kc_bind(pps, &bind_args);
  184. if (err < 0)
  185. return err;
  186. break;
  187. }
  188. default:
  189. return -ENOTTY;
  190. }
  191. return 0;
  192. }
  193. static int pps_cdev_open(struct inode *inode, struct file *file)
  194. {
  195. struct pps_device *pps = container_of(inode->i_cdev,
  196. struct pps_device, cdev);
  197. file->private_data = pps;
  198. return 0;
  199. }
  200. static int pps_cdev_release(struct inode *inode, struct file *file)
  201. {
  202. return 0;
  203. }
  204. /*
  205. * Char device stuff
  206. */
  207. static const struct file_operations pps_cdev_fops = {
  208. .owner = THIS_MODULE,
  209. .llseek = no_llseek,
  210. .poll = pps_cdev_poll,
  211. .fasync = pps_cdev_fasync,
  212. .unlocked_ioctl = pps_cdev_ioctl,
  213. .open = pps_cdev_open,
  214. .release = pps_cdev_release,
  215. };
  216. static void pps_device_destruct(struct device *dev)
  217. {
  218. struct pps_device *pps = dev_get_drvdata(dev);
  219. /* release id here to protect others from using it while it's
  220. * still in use */
  221. mutex_lock(&pps_idr_lock);
  222. idr_remove(&pps_idr, pps->id);
  223. mutex_unlock(&pps_idr_lock);
  224. kfree(dev);
  225. kfree(pps);
  226. }
  227. int pps_register_cdev(struct pps_device *pps)
  228. {
  229. int err;
  230. dev_t devt;
  231. mutex_lock(&pps_idr_lock);
  232. /* Get new ID for the new PPS source */
  233. if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0) {
  234. mutex_unlock(&pps_idr_lock);
  235. return -ENOMEM;
  236. }
  237. /* Now really allocate the PPS source.
  238. * After idr_get_new() calling the new source will be freely available
  239. * into the kernel.
  240. */
  241. err = idr_get_new(&pps_idr, pps, &pps->id);
  242. mutex_unlock(&pps_idr_lock);
  243. if (err < 0)
  244. return err;
  245. pps->id &= MAX_ID_MASK;
  246. if (pps->id >= PPS_MAX_SOURCES) {
  247. pr_err("%s: too many PPS sources in the system\n",
  248. pps->info.name);
  249. err = -EBUSY;
  250. goto free_idr;
  251. }
  252. devt = MKDEV(MAJOR(pps_devt), pps->id);
  253. cdev_init(&pps->cdev, &pps_cdev_fops);
  254. pps->cdev.owner = pps->info.owner;
  255. err = cdev_add(&pps->cdev, devt, 1);
  256. if (err) {
  257. pr_err("%s: failed to add char device %d:%d\n",
  258. pps->info.name, MAJOR(pps_devt), pps->id);
  259. goto free_idr;
  260. }
  261. pps->dev = device_create(pps_class, pps->info.dev, devt, pps,
  262. "pps%d", pps->id);
  263. if (IS_ERR(pps->dev))
  264. goto del_cdev;
  265. pps->dev->release = pps_device_destruct;
  266. pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
  267. MAJOR(pps_devt), pps->id);
  268. return 0;
  269. del_cdev:
  270. cdev_del(&pps->cdev);
  271. free_idr:
  272. mutex_lock(&pps_idr_lock);
  273. idr_remove(&pps_idr, pps->id);
  274. mutex_unlock(&pps_idr_lock);
  275. return err;
  276. }
  277. void pps_unregister_cdev(struct pps_device *pps)
  278. {
  279. device_destroy(pps_class, pps->dev->devt);
  280. cdev_del(&pps->cdev);
  281. }
  282. /*
  283. * Module stuff
  284. */
  285. static void __exit pps_exit(void)
  286. {
  287. class_destroy(pps_class);
  288. unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
  289. }
  290. static int __init pps_init(void)
  291. {
  292. int err;
  293. pps_class = class_create(THIS_MODULE, "pps");
  294. if (!pps_class) {
  295. pr_err("failed to allocate class\n");
  296. return -ENOMEM;
  297. }
  298. pps_class->dev_attrs = pps_attrs;
  299. err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
  300. if (err < 0) {
  301. pr_err("failed to allocate char device region\n");
  302. goto remove_class;
  303. }
  304. pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS);
  305. pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti "
  306. "<giometti@linux.it>\n", PPS_VERSION);
  307. return 0;
  308. remove_class:
  309. class_destroy(pps_class);
  310. return err;
  311. }
  312. subsys_initcall(pps_init);
  313. module_exit(pps_exit);
  314. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  315. MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
  316. MODULE_LICENSE("GPL");