posix-clock.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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/file.h>
  22. #include <linux/mutex.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. mutex_lock(&clk->mutex);
  35. if (!clk->zombie)
  36. return clk;
  37. mutex_unlock(&clk->mutex);
  38. return NULL;
  39. }
  40. static void put_posix_clock(struct posix_clock *clk)
  41. {
  42. mutex_unlock(&clk->mutex);
  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. mutex_lock(&clk->mutex);
  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. mutex_unlock(&clk->mutex);
  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. mutex_init(&clk->mutex);
  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. if (err)
  170. goto no_cdev;
  171. return err;
  172. no_cdev:
  173. mutex_destroy(&clk->mutex);
  174. return err;
  175. }
  176. EXPORT_SYMBOL_GPL(posix_clock_register);
  177. static void delete_clock(struct kref *kref)
  178. {
  179. struct posix_clock *clk = container_of(kref, struct posix_clock, kref);
  180. mutex_destroy(&clk->mutex);
  181. if (clk->release)
  182. clk->release(clk);
  183. }
  184. void posix_clock_unregister(struct posix_clock *clk)
  185. {
  186. cdev_del(&clk->cdev);
  187. mutex_lock(&clk->mutex);
  188. clk->zombie = true;
  189. mutex_unlock(&clk->mutex);
  190. kref_put(&clk->kref, delete_clock);
  191. }
  192. EXPORT_SYMBOL_GPL(posix_clock_unregister);
  193. struct posix_clock_desc {
  194. struct file *fp;
  195. struct posix_clock *clk;
  196. };
  197. static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
  198. {
  199. struct file *fp = fget(CLOCKID_TO_FD(id));
  200. int err = -EINVAL;
  201. if (!fp)
  202. return err;
  203. if (fp->f_op->open != posix_clock_open || !fp->private_data)
  204. goto out;
  205. cd->fp = fp;
  206. cd->clk = get_posix_clock(fp);
  207. err = cd->clk ? 0 : -ENODEV;
  208. out:
  209. if (err)
  210. fput(fp);
  211. return err;
  212. }
  213. static void put_clock_desc(struct posix_clock_desc *cd)
  214. {
  215. put_posix_clock(cd->clk);
  216. fput(cd->fp);
  217. }
  218. static int pc_clock_adjtime(clockid_t id, struct timex *tx)
  219. {
  220. struct posix_clock_desc cd;
  221. int err;
  222. err = get_clock_desc(id, &cd);
  223. if (err)
  224. return err;
  225. if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
  226. err = -EACCES;
  227. goto out;
  228. }
  229. if (cd.clk->ops.clock_adjtime)
  230. err = cd.clk->ops.clock_adjtime(cd.clk, tx);
  231. else
  232. err = -EOPNOTSUPP;
  233. out:
  234. put_clock_desc(&cd);
  235. return err;
  236. }
  237. static int pc_clock_gettime(clockid_t id, struct timespec *ts)
  238. {
  239. struct posix_clock_desc cd;
  240. int err;
  241. err = get_clock_desc(id, &cd);
  242. if (err)
  243. return err;
  244. if (cd.clk->ops.clock_gettime)
  245. err = cd.clk->ops.clock_gettime(cd.clk, ts);
  246. else
  247. err = -EOPNOTSUPP;
  248. put_clock_desc(&cd);
  249. return err;
  250. }
  251. static int pc_clock_getres(clockid_t id, struct timespec *ts)
  252. {
  253. struct posix_clock_desc cd;
  254. int err;
  255. err = get_clock_desc(id, &cd);
  256. if (err)
  257. return err;
  258. if (cd.clk->ops.clock_getres)
  259. err = cd.clk->ops.clock_getres(cd.clk, ts);
  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 posix_clock_desc cd;
  268. int err;
  269. err = get_clock_desc(id, &cd);
  270. if (err)
  271. return err;
  272. if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
  273. err = -EACCES;
  274. goto out;
  275. }
  276. if (cd.clk->ops.clock_settime)
  277. err = cd.clk->ops.clock_settime(cd.clk, ts);
  278. else
  279. err = -EOPNOTSUPP;
  280. out:
  281. put_clock_desc(&cd);
  282. return err;
  283. }
  284. static int pc_timer_create(struct k_itimer *kit)
  285. {
  286. clockid_t id = kit->it_clock;
  287. struct posix_clock_desc cd;
  288. int err;
  289. err = get_clock_desc(id, &cd);
  290. if (err)
  291. return err;
  292. if (cd.clk->ops.timer_create)
  293. err = cd.clk->ops.timer_create(cd.clk, kit);
  294. else
  295. err = -EOPNOTSUPP;
  296. put_clock_desc(&cd);
  297. return err;
  298. }
  299. static int pc_timer_delete(struct k_itimer *kit)
  300. {
  301. clockid_t id = kit->it_clock;
  302. struct posix_clock_desc cd;
  303. int err;
  304. err = get_clock_desc(id, &cd);
  305. if (err)
  306. return err;
  307. if (cd.clk->ops.timer_delete)
  308. err = cd.clk->ops.timer_delete(cd.clk, kit);
  309. else
  310. err = -EOPNOTSUPP;
  311. put_clock_desc(&cd);
  312. return err;
  313. }
  314. static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts)
  315. {
  316. clockid_t id = kit->it_clock;
  317. struct posix_clock_desc cd;
  318. if (get_clock_desc(id, &cd))
  319. return;
  320. if (cd.clk->ops.timer_gettime)
  321. cd.clk->ops.timer_gettime(cd.clk, kit, ts);
  322. put_clock_desc(&cd);
  323. }
  324. static int pc_timer_settime(struct k_itimer *kit, int flags,
  325. struct itimerspec *ts, struct itimerspec *old)
  326. {
  327. clockid_t id = kit->it_clock;
  328. struct posix_clock_desc cd;
  329. int err;
  330. err = get_clock_desc(id, &cd);
  331. if (err)
  332. return err;
  333. if (cd.clk->ops.timer_settime)
  334. err = cd.clk->ops.timer_settime(cd.clk, kit, flags, ts, old);
  335. else
  336. err = -EOPNOTSUPP;
  337. put_clock_desc(&cd);
  338. return err;
  339. }
  340. struct k_clock clock_posix_dynamic = {
  341. .clock_getres = pc_clock_getres,
  342. .clock_set = pc_clock_settime,
  343. .clock_get = pc_clock_gettime,
  344. .clock_adj = pc_clock_adjtime,
  345. .timer_create = pc_timer_create,
  346. .timer_set = pc_timer_settime,
  347. .timer_del = pc_timer_delete,
  348. .timer_get = pc_timer_gettime,
  349. };