posix-clock.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. int result = 0;
  60. if (!clk)
  61. return -ENODEV;
  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. int err;
  235. err = get_clock_desc(id, &cd);
  236. if (err)
  237. return err;
  238. if (cd.clk->ops.clock_gettime)
  239. err = cd.clk->ops.clock_gettime(cd.clk, ts);
  240. else
  241. err = -EOPNOTSUPP;
  242. put_clock_desc(&cd);
  243. return err;
  244. }
  245. static int pc_clock_getres(clockid_t id, struct timespec *ts)
  246. {
  247. struct posix_clock_desc cd;
  248. int err;
  249. err = get_clock_desc(id, &cd);
  250. if (err)
  251. return err;
  252. if (cd.clk->ops.clock_getres)
  253. err = cd.clk->ops.clock_getres(cd.clk, ts);
  254. else
  255. err = -EOPNOTSUPP;
  256. put_clock_desc(&cd);
  257. return err;
  258. }
  259. static int pc_clock_settime(clockid_t id, const struct timespec *ts)
  260. {
  261. struct posix_clock_desc cd;
  262. int err;
  263. err = get_clock_desc(id, &cd);
  264. if (err)
  265. return err;
  266. if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
  267. err = -EACCES;
  268. goto out;
  269. }
  270. if (cd.clk->ops.clock_settime)
  271. err = cd.clk->ops.clock_settime(cd.clk, ts);
  272. else
  273. err = -EOPNOTSUPP;
  274. out:
  275. put_clock_desc(&cd);
  276. return err;
  277. }
  278. static int pc_timer_create(struct k_itimer *kit)
  279. {
  280. clockid_t id = kit->it_clock;
  281. struct posix_clock_desc cd;
  282. int err;
  283. err = get_clock_desc(id, &cd);
  284. if (err)
  285. return err;
  286. if (cd.clk->ops.timer_create)
  287. err = cd.clk->ops.timer_create(cd.clk, kit);
  288. else
  289. err = -EOPNOTSUPP;
  290. put_clock_desc(&cd);
  291. return err;
  292. }
  293. static int pc_timer_delete(struct k_itimer *kit)
  294. {
  295. clockid_t id = kit->it_clock;
  296. struct posix_clock_desc cd;
  297. int err;
  298. err = get_clock_desc(id, &cd);
  299. if (err)
  300. return err;
  301. if (cd.clk->ops.timer_delete)
  302. err = cd.clk->ops.timer_delete(cd.clk, kit);
  303. else
  304. err = -EOPNOTSUPP;
  305. put_clock_desc(&cd);
  306. return err;
  307. }
  308. static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts)
  309. {
  310. clockid_t id = kit->it_clock;
  311. struct posix_clock_desc cd;
  312. if (get_clock_desc(id, &cd))
  313. return;
  314. if (cd.clk->ops.timer_gettime)
  315. cd.clk->ops.timer_gettime(cd.clk, kit, ts);
  316. put_clock_desc(&cd);
  317. }
  318. static int pc_timer_settime(struct k_itimer *kit, int flags,
  319. struct itimerspec *ts, struct itimerspec *old)
  320. {
  321. clockid_t id = kit->it_clock;
  322. struct posix_clock_desc cd;
  323. int err;
  324. err = get_clock_desc(id, &cd);
  325. if (err)
  326. return err;
  327. if (cd.clk->ops.timer_settime)
  328. err = cd.clk->ops.timer_settime(cd.clk, kit, flags, ts, old);
  329. else
  330. err = -EOPNOTSUPP;
  331. put_clock_desc(&cd);
  332. return err;
  333. }
  334. struct k_clock clock_posix_dynamic = {
  335. .clock_getres = pc_clock_getres,
  336. .clock_set = pc_clock_settime,
  337. .clock_get = pc_clock_gettime,
  338. .clock_adj = pc_clock_adjtime,
  339. .timer_create = pc_timer_create,
  340. .timer_set = pc_timer_settime,
  341. .timer_del = pc_timer_delete,
  342. .timer_get = pc_timer_gettime,
  343. };