posix-clock.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. static void delete_clock(struct kref *kref);
  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 int posix_clock_fasync(int fd, struct file *fp, int on)
  68. {
  69. struct posix_clock *clk = get_posix_clock(fp);
  70. int err = 0;
  71. if (!clk)
  72. return -ENODEV;
  73. if (clk->ops.fasync)
  74. err = clk->ops.fasync(clk, fd, fp, on);
  75. put_posix_clock(clk);
  76. return err;
  77. }
  78. static int posix_clock_mmap(struct file *fp, struct vm_area_struct *vma)
  79. {
  80. struct posix_clock *clk = get_posix_clock(fp);
  81. int err = -ENODEV;
  82. if (!clk)
  83. return -ENODEV;
  84. if (clk->ops.mmap)
  85. err = clk->ops.mmap(clk, vma);
  86. put_posix_clock(clk);
  87. return err;
  88. }
  89. static long posix_clock_ioctl(struct file *fp,
  90. unsigned int cmd, unsigned long arg)
  91. {
  92. struct posix_clock *clk = get_posix_clock(fp);
  93. int err = -ENOTTY;
  94. if (!clk)
  95. return -ENODEV;
  96. if (clk->ops.ioctl)
  97. err = clk->ops.ioctl(clk, cmd, arg);
  98. put_posix_clock(clk);
  99. return err;
  100. }
  101. #ifdef CONFIG_COMPAT
  102. static long posix_clock_compat_ioctl(struct file *fp,
  103. unsigned int cmd, unsigned long arg)
  104. {
  105. struct posix_clock *clk = get_posix_clock(fp);
  106. int err = -ENOTTY;
  107. if (!clk)
  108. return -ENODEV;
  109. if (clk->ops.ioctl)
  110. err = clk->ops.ioctl(clk, cmd, arg);
  111. put_posix_clock(clk);
  112. return err;
  113. }
  114. #endif
  115. static int posix_clock_open(struct inode *inode, struct file *fp)
  116. {
  117. int err;
  118. struct posix_clock *clk =
  119. container_of(inode->i_cdev, struct posix_clock, cdev);
  120. down_read(&clk->rwsem);
  121. if (clk->zombie) {
  122. err = -ENODEV;
  123. goto out;
  124. }
  125. if (clk->ops.open)
  126. err = clk->ops.open(clk, fp->f_mode);
  127. else
  128. err = 0;
  129. if (!err) {
  130. kref_get(&clk->kref);
  131. fp->private_data = clk;
  132. }
  133. out:
  134. up_read(&clk->rwsem);
  135. return err;
  136. }
  137. static int posix_clock_release(struct inode *inode, struct file *fp)
  138. {
  139. struct posix_clock *clk = fp->private_data;
  140. int err = 0;
  141. if (clk->ops.release)
  142. err = clk->ops.release(clk);
  143. kref_put(&clk->kref, delete_clock);
  144. fp->private_data = NULL;
  145. return err;
  146. }
  147. static const struct file_operations posix_clock_file_operations = {
  148. .owner = THIS_MODULE,
  149. .llseek = no_llseek,
  150. .read = posix_clock_read,
  151. .poll = posix_clock_poll,
  152. .unlocked_ioctl = posix_clock_ioctl,
  153. .open = posix_clock_open,
  154. .release = posix_clock_release,
  155. .fasync = posix_clock_fasync,
  156. .mmap = posix_clock_mmap,
  157. #ifdef CONFIG_COMPAT
  158. .compat_ioctl = posix_clock_compat_ioctl,
  159. #endif
  160. };
  161. int posix_clock_register(struct posix_clock *clk, dev_t devid)
  162. {
  163. int err;
  164. kref_init(&clk->kref);
  165. init_rwsem(&clk->rwsem);
  166. cdev_init(&clk->cdev, &posix_clock_file_operations);
  167. clk->cdev.owner = clk->ops.owner;
  168. err = cdev_add(&clk->cdev, devid, 1);
  169. return err;
  170. }
  171. EXPORT_SYMBOL_GPL(posix_clock_register);
  172. static void delete_clock(struct kref *kref)
  173. {
  174. struct posix_clock *clk = container_of(kref, struct posix_clock, kref);
  175. if (clk->release)
  176. clk->release(clk);
  177. }
  178. void posix_clock_unregister(struct posix_clock *clk)
  179. {
  180. cdev_del(&clk->cdev);
  181. down_write(&clk->rwsem);
  182. clk->zombie = true;
  183. up_write(&clk->rwsem);
  184. kref_put(&clk->kref, delete_clock);
  185. }
  186. EXPORT_SYMBOL_GPL(posix_clock_unregister);
  187. struct posix_clock_desc {
  188. struct file *fp;
  189. struct posix_clock *clk;
  190. };
  191. static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
  192. {
  193. struct file *fp = fget(CLOCKID_TO_FD(id));
  194. int err = -EINVAL;
  195. if (!fp)
  196. return err;
  197. if (fp->f_op->open != posix_clock_open || !fp->private_data)
  198. goto out;
  199. cd->fp = fp;
  200. cd->clk = get_posix_clock(fp);
  201. err = cd->clk ? 0 : -ENODEV;
  202. out:
  203. if (err)
  204. fput(fp);
  205. return err;
  206. }
  207. static void put_clock_desc(struct posix_clock_desc *cd)
  208. {
  209. put_posix_clock(cd->clk);
  210. fput(cd->fp);
  211. }
  212. static int pc_clock_adjtime(clockid_t id, struct timex *tx)
  213. {
  214. struct posix_clock_desc cd;
  215. int err;
  216. err = get_clock_desc(id, &cd);
  217. if (err)
  218. return err;
  219. if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
  220. err = -EACCES;
  221. goto out;
  222. }
  223. if (cd.clk->ops.clock_adjtime)
  224. err = cd.clk->ops.clock_adjtime(cd.clk, tx);
  225. else
  226. err = -EOPNOTSUPP;
  227. out:
  228. put_clock_desc(&cd);
  229. return err;
  230. }
  231. static int pc_clock_gettime(clockid_t id, struct timespec *ts)
  232. {
  233. struct posix_clock_desc cd;
  234. struct timespec64 ts64;
  235. int err;
  236. err = get_clock_desc(id, &cd);
  237. if (err)
  238. return err;
  239. if (cd.clk->ops.clock_gettime) {
  240. err = cd.clk->ops.clock_gettime(cd.clk, &ts64);
  241. *ts = timespec64_to_timespec(ts64);
  242. }
  243. else
  244. err = -EOPNOTSUPP;
  245. put_clock_desc(&cd);
  246. return err;
  247. }
  248. static int pc_clock_getres(clockid_t id, struct timespec *ts)
  249. {
  250. struct posix_clock_desc cd;
  251. struct timespec64 ts64;
  252. int err;
  253. err = get_clock_desc(id, &cd);
  254. if (err)
  255. return err;
  256. if (cd.clk->ops.clock_getres) {
  257. err = cd.clk->ops.clock_getres(cd.clk, &ts64);
  258. *ts = timespec64_to_timespec(ts64);
  259. }
  260. else
  261. err = -EOPNOTSUPP;
  262. put_clock_desc(&cd);
  263. return err;
  264. }
  265. static int pc_clock_settime(clockid_t id, const struct timespec *ts)
  266. {
  267. struct timespec64 ts64 = timespec_to_timespec64(*ts);
  268. struct posix_clock_desc cd;
  269. int err;
  270. err = get_clock_desc(id, &cd);
  271. if (err)
  272. return err;
  273. if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
  274. err = -EACCES;
  275. goto out;
  276. }
  277. if (cd.clk->ops.clock_settime)
  278. err = cd.clk->ops.clock_settime(cd.clk, &ts64);
  279. else
  280. err = -EOPNOTSUPP;
  281. out:
  282. put_clock_desc(&cd);
  283. return err;
  284. }
  285. static int pc_timer_create(struct k_itimer *kit)
  286. {
  287. clockid_t id = kit->it_clock;
  288. struct posix_clock_desc cd;
  289. int err;
  290. err = get_clock_desc(id, &cd);
  291. if (err)
  292. return err;
  293. if (cd.clk->ops.timer_create)
  294. err = cd.clk->ops.timer_create(cd.clk, kit);
  295. else
  296. err = -EOPNOTSUPP;
  297. put_clock_desc(&cd);
  298. return err;
  299. }
  300. static int pc_timer_delete(struct k_itimer *kit)
  301. {
  302. clockid_t id = kit->it_clock;
  303. struct posix_clock_desc cd;
  304. int err;
  305. err = get_clock_desc(id, &cd);
  306. if (err)
  307. return err;
  308. if (cd.clk->ops.timer_delete)
  309. err = cd.clk->ops.timer_delete(cd.clk, kit);
  310. else
  311. err = -EOPNOTSUPP;
  312. put_clock_desc(&cd);
  313. return err;
  314. }
  315. static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts)
  316. {
  317. clockid_t id = kit->it_clock;
  318. struct posix_clock_desc cd;
  319. struct itimerspec64 ts64;
  320. if (get_clock_desc(id, &cd))
  321. return;
  322. if (cd.clk->ops.timer_gettime) {
  323. cd.clk->ops.timer_gettime(cd.clk, kit, &ts64);
  324. *ts = itimerspec64_to_itimerspec(&ts64);
  325. }
  326. put_clock_desc(&cd);
  327. }
  328. static int pc_timer_settime(struct k_itimer *kit, int flags,
  329. struct itimerspec *ts, struct itimerspec *old)
  330. {
  331. struct itimerspec64 ts64 = itimerspec_to_itimerspec64(ts);
  332. clockid_t id = kit->it_clock;
  333. struct posix_clock_desc cd;
  334. struct itimerspec64 old64;
  335. int err;
  336. err = get_clock_desc(id, &cd);
  337. if (err)
  338. return err;
  339. if (cd.clk->ops.timer_settime) {
  340. err = cd.clk->ops.timer_settime(cd.clk, kit, flags, &ts64, &old64);
  341. if (old)
  342. *old = itimerspec64_to_itimerspec(&old64);
  343. }
  344. else
  345. err = -EOPNOTSUPP;
  346. put_clock_desc(&cd);
  347. return err;
  348. }
  349. struct k_clock clock_posix_dynamic = {
  350. .clock_getres = pc_clock_getres,
  351. .clock_set = pc_clock_settime,
  352. .clock_get = pc_clock_gettime,
  353. .clock_adj = pc_clock_adjtime,
  354. .timer_create = pc_timer_create,
  355. .timer_set = pc_timer_settime,
  356. .timer_del = pc_timer_delete,
  357. .timer_get = pc_timer_gettime,
  358. };