pps_gen.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PPS generators core file
  4. *
  5. * Copyright (C) 2024 Rodolfo Giometti <giometti@enneenne.com>
  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/uaccess.h>
  15. #include <linux/idr.h>
  16. #include <linux/cdev.h>
  17. #include <linux/poll.h>
  18. #include <linux/fs.h>
  19. #include <linux/pps_gen_kernel.h>
  20. #include <linux/slab.h>
  21. /*
  22. * Local variables
  23. */
  24. static dev_t pps_gen_devt;
  25. static struct class *pps_gen_class;
  26. static DEFINE_IDA(pps_gen_ida);
  27. /*
  28. * Char device methods
  29. */
  30. static __poll_t pps_gen_cdev_poll(struct file *file, poll_table *wait)
  31. {
  32. struct pps_gen_device *pps_gen = file->private_data;
  33. poll_wait(file, &pps_gen->queue, wait);
  34. return EPOLLIN | EPOLLRDNORM;
  35. }
  36. static int pps_gen_cdev_fasync(int fd, struct file *file, int on)
  37. {
  38. struct pps_gen_device *pps_gen = file->private_data;
  39. return fasync_helper(fd, file, on, &pps_gen->async_queue);
  40. }
  41. static long pps_gen_cdev_ioctl(struct file *file,
  42. unsigned int cmd, unsigned long arg)
  43. {
  44. struct pps_gen_device *pps_gen = file->private_data;
  45. void __user *uarg = (void __user *) arg;
  46. unsigned int __user *uiuarg = (unsigned int __user *) arg;
  47. unsigned int status;
  48. int ret;
  49. switch (cmd) {
  50. case PPS_GEN_SETENABLE:
  51. dev_dbg(pps_gen->dev, "PPS_GEN_SETENABLE\n");
  52. ret = get_user(status, uiuarg);
  53. if (ret)
  54. return -EFAULT;
  55. ret = pps_gen->info->enable(pps_gen, status);
  56. if (ret)
  57. return ret;
  58. pps_gen->enabled = status;
  59. break;
  60. case PPS_GEN_USESYSTEMCLOCK:
  61. dev_dbg(pps_gen->dev, "PPS_GEN_USESYSTEMCLOCK\n");
  62. ret = put_user(pps_gen->info->use_system_clock, uiuarg);
  63. if (ret)
  64. return -EFAULT;
  65. break;
  66. case PPS_GEN_FETCHEVENT: {
  67. struct pps_gen_event info;
  68. unsigned int ev = pps_gen->last_ev;
  69. dev_dbg(pps_gen->dev, "PPS_GEN_FETCHEVENT\n");
  70. ret = wait_event_interruptible(pps_gen->queue,
  71. ev != pps_gen->last_ev);
  72. if (ret == -ERESTARTSYS) {
  73. dev_dbg(pps_gen->dev, "pending signal caught\n");
  74. return -EINTR;
  75. }
  76. spin_lock_irq(&pps_gen->lock);
  77. info.sequence = pps_gen->sequence;
  78. info.event = pps_gen->event;
  79. spin_unlock_irq(&pps_gen->lock);
  80. ret = copy_to_user(uarg, &info, sizeof(struct pps_gen_event));
  81. if (ret)
  82. return -EFAULT;
  83. break;
  84. }
  85. default:
  86. return -ENOTTY;
  87. }
  88. return 0;
  89. }
  90. static int pps_gen_cdev_open(struct inode *inode, struct file *file)
  91. {
  92. struct pps_gen_device *pps_gen = container_of(inode->i_cdev,
  93. struct pps_gen_device, cdev);
  94. get_device(pps_gen->dev);
  95. file->private_data = pps_gen;
  96. return 0;
  97. }
  98. static int pps_gen_cdev_release(struct inode *inode, struct file *file)
  99. {
  100. struct pps_gen_device *pps_gen = file->private_data;
  101. put_device(pps_gen->dev);
  102. return 0;
  103. }
  104. /*
  105. * Char device stuff
  106. */
  107. static const struct file_operations pps_gen_cdev_fops = {
  108. .owner = THIS_MODULE,
  109. .poll = pps_gen_cdev_poll,
  110. .fasync = pps_gen_cdev_fasync,
  111. .unlocked_ioctl = pps_gen_cdev_ioctl,
  112. .open = pps_gen_cdev_open,
  113. .release = pps_gen_cdev_release,
  114. };
  115. static void pps_gen_device_destruct(struct device *dev)
  116. {
  117. struct pps_gen_device *pps_gen = dev_get_drvdata(dev);
  118. cdev_del(&pps_gen->cdev);
  119. pr_debug("deallocating pps-gen%d\n", pps_gen->id);
  120. ida_free(&pps_gen_ida, pps_gen->id);
  121. kfree(dev);
  122. kfree(pps_gen);
  123. }
  124. static int pps_gen_register_cdev(struct pps_gen_device *pps_gen)
  125. {
  126. int err;
  127. dev_t devt;
  128. err = ida_alloc_max(&pps_gen_ida, PPS_GEN_MAX_SOURCES - 1, GFP_KERNEL);
  129. if (err < 0) {
  130. if (err == -ENOSPC) {
  131. pr_err("too many PPS sources in the system\n");
  132. err = -EBUSY;
  133. }
  134. return err;
  135. }
  136. pps_gen->id = err;
  137. devt = MKDEV(MAJOR(pps_gen_devt), pps_gen->id);
  138. cdev_init(&pps_gen->cdev, &pps_gen_cdev_fops);
  139. pps_gen->cdev.owner = pps_gen->info->owner;
  140. err = cdev_add(&pps_gen->cdev, devt, 1);
  141. if (err) {
  142. pr_err("failed to add char device %d:%d\n",
  143. MAJOR(pps_gen_devt), pps_gen->id);
  144. goto free_ida;
  145. }
  146. pps_gen->dev = device_create(pps_gen_class, pps_gen->info->parent, devt,
  147. pps_gen, "pps-gen%d", pps_gen->id);
  148. if (IS_ERR(pps_gen->dev)) {
  149. err = PTR_ERR(pps_gen->dev);
  150. goto del_cdev;
  151. }
  152. pps_gen->dev->release = pps_gen_device_destruct;
  153. dev_set_drvdata(pps_gen->dev, pps_gen);
  154. pr_debug("generator got cdev (%d:%d)\n",
  155. MAJOR(pps_gen_devt), pps_gen->id);
  156. return 0;
  157. del_cdev:
  158. cdev_del(&pps_gen->cdev);
  159. free_ida:
  160. ida_free(&pps_gen_ida, pps_gen->id);
  161. return err;
  162. }
  163. static void pps_gen_unregister_cdev(struct pps_gen_device *pps_gen)
  164. {
  165. pr_debug("unregistering pps-gen%d\n", pps_gen->id);
  166. device_destroy(pps_gen_class, pps_gen->dev->devt);
  167. }
  168. /*
  169. * Exported functions
  170. */
  171. /**
  172. * pps_gen_register_source() - add a PPS generator in the system
  173. * @info: the PPS generator info struct
  174. *
  175. * This function is used to register a new PPS generator in the system.
  176. * When it returns successfully the new generator is up and running, and
  177. * it can be managed by the userspace.
  178. *
  179. * Return: the PPS generator device in case of success, and ERR_PTR(errno)
  180. * otherwise.
  181. */
  182. struct pps_gen_device *pps_gen_register_source(const struct pps_gen_source_info *info)
  183. {
  184. struct pps_gen_device *pps_gen;
  185. int err;
  186. pps_gen = kzalloc_obj(struct pps_gen_device);
  187. if (pps_gen == NULL) {
  188. err = -ENOMEM;
  189. goto pps_gen_register_source_exit;
  190. }
  191. pps_gen->info = info;
  192. pps_gen->enabled = false;
  193. init_waitqueue_head(&pps_gen->queue);
  194. spin_lock_init(&pps_gen->lock);
  195. /* Create the char device */
  196. err = pps_gen_register_cdev(pps_gen);
  197. if (err < 0) {
  198. pr_err(" unable to create char device\n");
  199. goto kfree_pps_gen;
  200. }
  201. return pps_gen;
  202. kfree_pps_gen:
  203. kfree(pps_gen);
  204. pps_gen_register_source_exit:
  205. pr_err("unable to register generator\n");
  206. return ERR_PTR(err);
  207. }
  208. EXPORT_SYMBOL(pps_gen_register_source);
  209. /**
  210. * pps_gen_unregister_source() - remove a PPS generator from the system
  211. * @pps_gen: the PPS generator device to be removed
  212. *
  213. * This function is used to deregister a PPS generator from the system. When
  214. * called, it disables the generator so no pulses are generated anymore.
  215. */
  216. void pps_gen_unregister_source(struct pps_gen_device *pps_gen)
  217. {
  218. pps_gen_unregister_cdev(pps_gen);
  219. }
  220. EXPORT_SYMBOL(pps_gen_unregister_source);
  221. /* pps_gen_event - register a PPS generator event into the system
  222. * @pps: the PPS generator device
  223. * @event: the event type
  224. * @data: userdef pointer
  225. *
  226. * This function is used by each PPS generator in order to register a new
  227. * PPS event into the system (it's usually called inside an IRQ handler).
  228. */
  229. void pps_gen_event(struct pps_gen_device *pps_gen,
  230. unsigned int event, void *data)
  231. {
  232. unsigned long flags;
  233. dev_dbg(pps_gen->dev, "PPS generator event %u\n", event);
  234. spin_lock_irqsave(&pps_gen->lock, flags);
  235. pps_gen->event = event;
  236. pps_gen->sequence++;
  237. pps_gen->last_ev++;
  238. wake_up_interruptible_all(&pps_gen->queue);
  239. kill_fasync(&pps_gen->async_queue, SIGIO, POLL_IN);
  240. spin_unlock_irqrestore(&pps_gen->lock, flags);
  241. }
  242. EXPORT_SYMBOL(pps_gen_event);
  243. /*
  244. * Module stuff
  245. */
  246. static void __exit pps_gen_exit(void)
  247. {
  248. class_destroy(pps_gen_class);
  249. unregister_chrdev_region(pps_gen_devt, PPS_GEN_MAX_SOURCES);
  250. }
  251. static int __init pps_gen_init(void)
  252. {
  253. int err;
  254. pps_gen_class = class_create("pps-gen");
  255. if (IS_ERR(pps_gen_class)) {
  256. pr_err("failed to allocate class\n");
  257. return PTR_ERR(pps_gen_class);
  258. }
  259. pps_gen_class->dev_groups = pps_gen_groups;
  260. err = alloc_chrdev_region(&pps_gen_devt, 0,
  261. PPS_GEN_MAX_SOURCES, "pps-gen");
  262. if (err < 0) {
  263. pr_err("failed to allocate char device region\n");
  264. goto remove_class;
  265. }
  266. return 0;
  267. remove_class:
  268. class_destroy(pps_gen_class);
  269. return err;
  270. }
  271. subsys_initcall(pps_gen_init);
  272. module_exit(pps_gen_exit);
  273. MODULE_AUTHOR("Rodolfo Giometti <giometti@enneenne.com>");
  274. MODULE_DESCRIPTION("LinuxPPS generators support");
  275. MODULE_LICENSE("GPL");