posix-clock.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * posix-clock.h - support for dynamic clock devices
  4. *
  5. * Copyright (C) 2010 OMICRON electronics GmbH
  6. */
  7. #ifndef _LINUX_POSIX_CLOCK_H_
  8. #define _LINUX_POSIX_CLOCK_H_
  9. #include <linux/cdev.h>
  10. #include <linux/fs.h>
  11. #include <linux/poll.h>
  12. #include <linux/posix-timers.h>
  13. #include <linux/rwsem.h>
  14. struct posix_clock;
  15. /**
  16. * struct posix_clock_operations - functional interface to the clock
  17. *
  18. * Every posix clock is represented by a character device. Drivers may
  19. * optionally offer extended capabilities by implementing the
  20. * character device methods. The character device file operations are
  21. * first handled by the clock device layer, then passed on to the
  22. * driver by calling these functions.
  23. *
  24. * @owner: The clock driver should set to THIS_MODULE
  25. * @clock_adjtime: Adjust the clock
  26. * @clock_gettime: Read the current time
  27. * @clock_getres: Get the clock resolution
  28. * @clock_settime: Set the current time value
  29. * @open: Optional character device open method
  30. * @release: Optional character device release method
  31. * @ioctl: Optional character device ioctl method
  32. * @read: Optional character device read method
  33. * @poll: Optional character device poll method
  34. */
  35. struct posix_clock_operations {
  36. struct module *owner;
  37. int (*clock_adjtime)(struct posix_clock *pc, struct __kernel_timex *tx);
  38. int (*clock_gettime)(struct posix_clock *pc, struct timespec64 *ts);
  39. int (*clock_getres) (struct posix_clock *pc, struct timespec64 *ts);
  40. int (*clock_settime)(struct posix_clock *pc,
  41. const struct timespec64 *ts);
  42. /*
  43. * Optional character device methods:
  44. */
  45. long (*ioctl) (struct posix_clock *pc,
  46. unsigned int cmd, unsigned long arg);
  47. int (*open) (struct posix_clock *pc, fmode_t f_mode);
  48. __poll_t (*poll) (struct posix_clock *pc,
  49. struct file *file, poll_table *wait);
  50. int (*release) (struct posix_clock *pc);
  51. ssize_t (*read) (struct posix_clock *pc,
  52. uint flags, char __user *buf, size_t cnt);
  53. };
  54. /**
  55. * struct posix_clock - represents a dynamic posix clock
  56. *
  57. * @ops: Functional interface to the clock
  58. * @cdev: Character device instance for this clock
  59. * @dev: Pointer to the clock's device.
  60. * @rwsem: Protects the 'zombie' field from concurrent access.
  61. * @zombie: If 'zombie' is true, then the hardware has disappeared.
  62. *
  63. * Drivers should embed their struct posix_clock within a private
  64. * structure, obtaining a reference to it during callbacks using
  65. * container_of().
  66. *
  67. * Drivers should supply an initialized but not exposed struct device
  68. * to posix_clock_register(). It is used to manage lifetime of the
  69. * driver's private structure. It's 'release' field should be set to
  70. * a release function for this private structure.
  71. */
  72. struct posix_clock {
  73. struct posix_clock_operations ops;
  74. struct cdev cdev;
  75. struct device *dev;
  76. struct rw_semaphore rwsem;
  77. bool zombie;
  78. };
  79. /**
  80. * posix_clock_register() - register a new clock
  81. * @clk: Pointer to the clock. Caller must provide 'ops' field
  82. * @dev: Pointer to the initialized device. Caller must provide
  83. * 'release' field
  84. *
  85. * A clock driver calls this function to register itself with the
  86. * clock device subsystem. If 'clk' points to dynamically allocated
  87. * memory, then the caller must provide a 'release' function to free
  88. * that memory.
  89. *
  90. * Returns zero on success, non-zero otherwise.
  91. */
  92. int posix_clock_register(struct posix_clock *clk, struct device *dev);
  93. /**
  94. * posix_clock_unregister() - unregister a clock
  95. * @clk: Clock instance previously registered via posix_clock_register()
  96. *
  97. * A clock driver calls this function to remove itself from the clock
  98. * device subsystem. The posix_clock itself will remain (in an
  99. * inactive state) until its reference count drops to zero, at which
  100. * point it will be deallocated with its 'release' method.
  101. */
  102. void posix_clock_unregister(struct posix_clock *clk);
  103. #endif