posix-clock.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * posix-clock.c - support for dynamic clock devices
  3. *
  4. * Copyright (C) 2010 OMICRON electronics GmbH
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/device.h>
  21. #include <linux/export.h>
  22. #include <linux/file.h>
  23. #include <linux/posix-clock.h>
  24. #include <linux/slab.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/uaccess.h>
  27. #include "posix-timers.h"
  28. /*
  29. * Returns NULL if the posix_clock instance attached to 'fp' is old and stale.
  30. */
  31. static struct posix_clock *get_posix_clock(struct file *fp)
  32. {
  33. struct posix_clock *clk = fp->private_data;
  34. down_read(&clk->rwsem);
  35. if (!clk->zombie)
  36. return clk;
  37. up_read(&clk->rwsem);
  38. return NULL;
  39. }
  40. static void put_posix_clock(struct posix_clock *clk)
  41. {
  42. up_read(&clk->rwsem);
  43. }
  44. static ssize_t posix_clock_read(struct file *fp, char __user *buf,
  45. size_t count, loff_t *ppos)
  46. {
  47. struct posix_clock *clk = get_posix_clock(fp);
  48. int err = -EINVAL;
  49. if (!clk)
  50. return -ENODEV;
  51. if (clk->ops.read)
  52. err = clk->ops.read(clk, fp->f_flags, buf, count);
  53. put_posix_clock(clk);
  54. return err;
  55. }
  56. static unsigned int posix_clock_poll(struct file *fp, poll_table *wait)
  57. {
  58. struct posix_clock *clk = get_posix_clock(fp);
  59. unsigned int result = 0;
  60. if (!clk)
  61. return POLLERR;
  62. if (clk->ops.poll)
  63. result = clk->ops.poll(clk, fp, wait);
  64. put_posix_clock(clk);
  65. return result;
  66. }
  67. static long posix_clock_ioctl(struct file *fp,
  68. unsigned int cmd, unsigned long arg)
  69. {
  70. struct posix_clock *clk = get_posix_clock(fp);
  71. int err = -ENOTTY;
  72. if (!clk)
  73. return -ENODEV;
  74. if (clk->ops.ioctl)
  75. err = clk->ops.ioctl(clk, cmd, arg);
  76. put_posix_clock(clk);
  77. return err;
  78. }
  79. #ifdef CONFIG_COMPAT
  80. static long posix_clock_compat_ioctl(struct file *fp,
  81. unsigned int cmd, unsigned long arg)
  82. {
  83. struct posix_clock *clk = get_posix_clock(fp);
  84. int err = -ENOTTY;
  85. if (!clk)
  86. return -ENODEV;
  87. if (clk->ops.ioctl)
  88. err = clk->ops.ioctl(clk, cmd, arg);
  89. put_posix_clock(clk);
  90. return err;
  91. }
  92. #endif
  93. static int posix_clock_open(struct inode *inode, struct file *fp)
  94. {
  95. int err;
  96. struct posix_clock *clk =
  97. container_of(inode->i_cdev, struct posix_clock, cdev);
  98. down_read(&clk->rwsem);
  99. if (clk->zombie) {
  100. err = -ENODEV;
  101. goto out;
  102. }
  103. if (clk->ops.open)
  104. err = clk->ops.open(clk, fp->f_mode);
  105. else
  106. err = 0;
  107. if (!err) {
  108. get_device(clk->dev);
  109. fp->private_data = clk;
  110. }
  111. out:
  112. up_read(&clk->rwsem);
  113. return err;
  114. }
  115. static int posix_clock_release(struct inode *inode, struct file *fp)
  116. {
  117. struct posix_clock *clk = fp->private_data;
  118. int err = 0;
  119. if (clk->ops.release)
  120. err = clk->ops.release(clk);
  121. put_device(clk->dev);
  122. fp->private_data = NULL;
  123. return err;
  124. }
  125. static const struct file_operations posix_clock_file_operations = {
  126. .owner = THIS_MODULE,
  127. .llseek = no_llseek,
  128. .read = posix_clock_read,
  129. .poll = posix_clock_poll,
  130. .unlocked_ioctl = posix_clock_ioctl,
  131. .open = posix_clock_open,
  132. .release = posix_clock_release,
  133. #ifdef CONFIG_COMPAT
  134. .compat_ioctl = posix_clock_compat_ioctl,
  135. #endif
  136. };
  137. int posix_clock_register(struct posix_clock *clk, struct device *dev)
  138. {
  139. int err;
  140. init_rwsem(&clk->rwsem);
  141. cdev_init(&clk->cdev, &posix_clock_file_operations);
  142. err = cdev_device_add(&clk->cdev, dev);
  143. if (err) {
  144. pr_err("%s unable to add device %d:%d\n",
  145. dev_name(dev), MAJOR(dev->devt), MINOR(dev->devt));
  146. return err;
  147. }
  148. clk->cdev.owner = clk->ops.owner;
  149. clk->dev = dev;
  150. return 0;
  151. }
  152. EXPORT_SYMBOL_GPL(posix_clock_register);
  153. void posix_clock_unregister(struct posix_clock *clk)
  154. {
  155. cdev_device_del(&clk->cdev, clk->dev);
  156. down_write(&clk->rwsem);
  157. clk->zombie = true;
  158. up_write(&clk->rwsem);
  159. put_device(clk->dev);
  160. }
  161. EXPORT_SYMBOL_GPL(posix_clock_unregister);
  162. struct posix_clock_desc {
  163. struct file *fp;
  164. struct posix_clock *clk;
  165. };
  166. static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
  167. {
  168. struct file *fp = fget(CLOCKID_TO_FD(id));
  169. int err = -EINVAL;
  170. if (!fp)
  171. return err;
  172. if (fp->f_op->open != posix_clock_open || !fp->private_data)
  173. goto out;
  174. cd->fp = fp;
  175. cd->clk = get_posix_clock(fp);
  176. err = cd->clk ? 0 : -ENODEV;
  177. out:
  178. if (err)
  179. fput(fp);
  180. return err;
  181. }
  182. static void put_clock_desc(struct posix_clock_desc *cd)
  183. {
  184. put_posix_clock(cd->clk);
  185. fput(cd->fp);
  186. }
  187. static int pc_clock_adjtime(clockid_t id, struct timex *tx)
  188. {
  189. struct posix_clock_desc cd;
  190. int err;
  191. err = get_clock_desc(id, &cd);
  192. if (err)
  193. return err;
  194. if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
  195. err = -EACCES;
  196. goto out;
  197. }
  198. if (cd.clk->ops.clock_adjtime)
  199. err = cd.clk->ops.clock_adjtime(cd.clk, tx);
  200. else
  201. err = -EOPNOTSUPP;
  202. out:
  203. put_clock_desc(&cd);
  204. return err;
  205. }
  206. static int pc_clock_gettime(clockid_t id, struct timespec64 *ts)
  207. {
  208. struct posix_clock_desc cd;
  209. int err;
  210. err = get_clock_desc(id, &cd);
  211. if (err)
  212. return err;
  213. if (cd.clk->ops.clock_gettime)
  214. err = cd.clk->ops.clock_gettime(cd.clk, ts);
  215. else
  216. err = -EOPNOTSUPP;
  217. put_clock_desc(&cd);
  218. return err;
  219. }
  220. static int pc_clock_getres(clockid_t id, struct timespec64 *ts)
  221. {
  222. struct posix_clock_desc cd;
  223. int err;
  224. err = get_clock_desc(id, &cd);
  225. if (err)
  226. return err;
  227. if (cd.clk->ops.clock_getres)
  228. err = cd.clk->ops.clock_getres(cd.clk, ts);
  229. else
  230. err = -EOPNOTSUPP;
  231. put_clock_desc(&cd);
  232. return err;
  233. }
  234. static int pc_clock_settime(clockid_t id, const struct timespec64 *ts)
  235. {
  236. struct posix_clock_desc cd;
  237. int err;
  238. err = get_clock_desc(id, &cd);
  239. if (err)
  240. return err;
  241. if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
  242. err = -EACCES;
  243. goto out;
  244. }
  245. if (cd.clk->ops.clock_settime)
  246. err = cd.clk->ops.clock_settime(cd.clk, ts);
  247. else
  248. err = -EOPNOTSUPP;
  249. out:
  250. put_clock_desc(&cd);
  251. return err;
  252. }
  253. const struct k_clock clock_posix_dynamic = {
  254. .clock_getres = pc_clock_getres,
  255. .clock_set = pc_clock_settime,
  256. .clock_get = pc_clock_gettime,
  257. .clock_adj = pc_clock_adjtime,
  258. };