posix-clock.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * posix-clock.h - 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. #ifndef _LINUX_POSIX_CLOCK_H_
  21. #define _LINUX_POSIX_CLOCK_H_
  22. #include <linux/cdev.h>
  23. #include <linux/fs.h>
  24. #include <linux/poll.h>
  25. #include <linux/posix-timers.h>
  26. #include <linux/rwsem.h>
  27. struct posix_clock;
  28. /**
  29. * struct posix_clock_operations - functional interface to the clock
  30. *
  31. * Every posix clock is represented by a character device. Drivers may
  32. * optionally offer extended capabilities by implementing the
  33. * character device methods. The character device file operations are
  34. * first handled by the clock device layer, then passed on to the
  35. * driver by calling these functions.
  36. *
  37. * @owner: The clock driver should set to THIS_MODULE
  38. * @clock_adjtime: Adjust the clock
  39. * @clock_gettime: Read the current time
  40. * @clock_getres: Get the clock resolution
  41. * @clock_settime: Set the current time value
  42. * @timer_create: Create a new timer
  43. * @timer_delete: Remove a previously created timer
  44. * @timer_gettime: Get remaining time and interval of a timer
  45. * @timer_settime: Set a timer's initial expiration and interval
  46. * @fasync: Optional character device fasync method
  47. * @mmap: Optional character device mmap method
  48. * @open: Optional character device open method
  49. * @release: Optional character device release method
  50. * @ioctl: Optional character device ioctl method
  51. * @read: Optional character device read method
  52. * @poll: Optional character device poll method
  53. */
  54. struct posix_clock_operations {
  55. struct module *owner;
  56. int (*clock_adjtime)(struct posix_clock *pc, struct timex *tx);
  57. int (*clock_gettime)(struct posix_clock *pc, struct timespec64 *ts);
  58. int (*clock_getres) (struct posix_clock *pc, struct timespec64 *ts);
  59. int (*clock_settime)(struct posix_clock *pc,
  60. const struct timespec64 *ts);
  61. int (*timer_create) (struct posix_clock *pc, struct k_itimer *kit);
  62. int (*timer_delete) (struct posix_clock *pc, struct k_itimer *kit);
  63. void (*timer_gettime)(struct posix_clock *pc,
  64. struct k_itimer *kit, struct itimerspec64 *tsp);
  65. int (*timer_settime)(struct posix_clock *pc,
  66. struct k_itimer *kit, int flags,
  67. struct itimerspec64 *tsp, struct itimerspec64 *old);
  68. /*
  69. * Optional character device methods:
  70. */
  71. int (*fasync) (struct posix_clock *pc,
  72. int fd, struct file *file, int on);
  73. long (*ioctl) (struct posix_clock *pc,
  74. unsigned int cmd, unsigned long arg);
  75. int (*mmap) (struct posix_clock *pc,
  76. struct vm_area_struct *vma);
  77. int (*open) (struct posix_clock *pc, fmode_t f_mode);
  78. uint (*poll) (struct posix_clock *pc,
  79. struct file *file, poll_table *wait);
  80. int (*release) (struct posix_clock *pc);
  81. ssize_t (*read) (struct posix_clock *pc,
  82. uint flags, char __user *buf, size_t cnt);
  83. };
  84. /**
  85. * struct posix_clock - represents a dynamic posix clock
  86. *
  87. * @ops: Functional interface to the clock
  88. * @cdev: Character device instance for this clock
  89. * @kref: Reference count.
  90. * @rwsem: Protects the 'zombie' field from concurrent access.
  91. * @zombie: If 'zombie' is true, then the hardware has disappeared.
  92. * @release: A function to free the structure when the reference count reaches
  93. * zero. May be NULL if structure is statically allocated.
  94. *
  95. * Drivers should embed their struct posix_clock within a private
  96. * structure, obtaining a reference to it during callbacks using
  97. * container_of().
  98. */
  99. struct posix_clock {
  100. struct posix_clock_operations ops;
  101. struct cdev cdev;
  102. struct kref kref;
  103. struct rw_semaphore rwsem;
  104. bool zombie;
  105. void (*release)(struct posix_clock *clk);
  106. };
  107. /**
  108. * posix_clock_register() - register a new clock
  109. * @clk: Pointer to the clock. Caller must provide 'ops' and 'release'
  110. * @devid: Allocated device id
  111. *
  112. * A clock driver calls this function to register itself with the
  113. * clock device subsystem. If 'clk' points to dynamically allocated
  114. * memory, then the caller must provide a 'release' function to free
  115. * that memory.
  116. *
  117. * Returns zero on success, non-zero otherwise.
  118. */
  119. int posix_clock_register(struct posix_clock *clk, dev_t devid);
  120. /**
  121. * posix_clock_unregister() - unregister a clock
  122. * @clk: Clock instance previously registered via posix_clock_register()
  123. *
  124. * A clock driver calls this function to remove itself from the clock
  125. * device subsystem. The posix_clock itself will remain (in an
  126. * inactive state) until its reference count drops to zero, at which
  127. * point it will be deallocated with its 'release' method.
  128. */
  129. void posix_clock_unregister(struct posix_clock *clk);
  130. #endif