posix-clock.h 5.3 KB

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