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