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 	__poll_t (*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  * @dev:     Pointer to the clock's device.
86  * @rwsem:   Protects the 'zombie' field from concurrent access.
87  * @zombie:  If 'zombie' is true, then the hardware has disappeared.
88  *
89  * Drivers should embed their struct posix_clock within a private
90  * structure, obtaining a reference to it during callbacks using
91  * container_of().
92  *
93  * Drivers should supply an initialized but not exposed struct device
94  * to posix_clock_register(). It is used to manage lifetime of the
95  * driver's private structure. It's 'release' field should be set to
96  * a release function for this private structure.
97  */
98 struct posix_clock {
99 	struct posix_clock_operations ops;
100 	struct cdev cdev;
101 	struct device *dev;
102 	struct rw_semaphore rwsem;
103 	bool zombie;
104 };
105 
106 /**
107  * posix_clock_register() - register a new clock
108  * @clk:   Pointer to the clock. Caller must provide 'ops' field
109  * @dev:   Pointer to the initialized device. Caller must provide
110  *         'release' field
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, struct device *dev);
120 
121 /**
122  * posix_clock_unregister() - unregister a clock
123  * @clk: Clock instance previously registered via posix_clock_register()
124  *
125  * A clock driver calls this function to remove itself from the clock
126  * device subsystem. The posix_clock itself will remain (in an
127  * inactive state) until its reference count drops to zero, at which
128  * point it will be deallocated with its 'release' method.
129  */
130 void posix_clock_unregister(struct posix_clock *clk);
131 
132 #endif
133