1 /*
2  * Copyright (c) 2019 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Public API for PS/2 devices such as keyboard and mouse.
10  * Callers of this API are responsible for setting the typematic rate
11  * and decode keys using their desired scan code tables.
12  */
13 
14 #ifndef ZEPHYR_INCLUDE_DRIVERS_PS2_H_
15 #define ZEPHYR_INCLUDE_DRIVERS_PS2_H_
16 
17 #include <errno.h>
18 #include <zephyr/types.h>
19 #include <stddef.h>
20 #include <zephyr/device.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /**
27  * @brief PS/2 Driver APIs
28  * @defgroup ps2_interface PS/2 Driver APIs
29  * @ingroup io_interfaces
30  * @{
31  */
32 
33 /**
34  * @brief PS/2 callback called when user types or click a mouse.
35  *
36  * @param dev   Pointer to the device structure for the driver instance.
37  * @param data  Data byte passed pack to the user.
38  */
39 typedef void (*ps2_callback_t)(const struct device *dev, uint8_t data);
40 
41 /**
42  * @cond INTERNAL_HIDDEN
43  *
44  * PS2 driver API definition and system call entry points
45  *
46  * (Internal use only.)
47  */
48 typedef int (*ps2_config_t)(const struct device *dev,
49 			    ps2_callback_t callback_isr);
50 typedef int (*ps2_read_t)(const struct device *dev, uint8_t *value);
51 typedef int (*ps2_write_t)(const struct device *dev, uint8_t value);
52 typedef int (*ps2_disable_callback_t)(const struct device *dev);
53 typedef int (*ps2_enable_callback_t)(const struct device *dev);
54 
55 __subsystem struct ps2_driver_api {
56 	ps2_config_t config;
57 	ps2_read_t read;
58 	ps2_write_t write;
59 	ps2_disable_callback_t disable_callback;
60 	ps2_enable_callback_t enable_callback;
61 };
62 /**
63  * @endcond
64  */
65 
66 /**
67  * @brief Configure a ps2 instance.
68  *
69  * @param dev Pointer to the device structure for the driver instance.
70  * @param callback_isr called when PS/2 devices reply to a configuration
71  * command or when a mouse/keyboard send data to the client application.
72  *
73  * @retval 0 If successful.
74  * @retval Negative errno code if failure.
75  */
76 __syscall int ps2_config(const struct device *dev,
77 			 ps2_callback_t callback_isr);
78 
z_impl_ps2_config(const struct device * dev,ps2_callback_t callback_isr)79 static inline int z_impl_ps2_config(const struct device *dev,
80 				    ps2_callback_t callback_isr)
81 {
82 	const struct ps2_driver_api *api =
83 				(struct ps2_driver_api *)dev->api;
84 
85 	return api->config(dev, callback_isr);
86 }
87 
88 /**
89  * @brief Write to PS/2 device.
90  *
91  * @param dev Pointer to the device structure for the driver instance.
92  * @param value Data for the PS2 device.
93  *
94  * @retval 0 If successful.
95  * @retval Negative errno code if failure.
96  */
97 __syscall int ps2_write(const struct device *dev, uint8_t value);
98 
z_impl_ps2_write(const struct device * dev,uint8_t value)99 static inline int z_impl_ps2_write(const struct device *dev, uint8_t value)
100 {
101 	const struct ps2_driver_api *api =
102 			(const struct ps2_driver_api *)dev->api;
103 
104 	return api->write(dev, value);
105 }
106 
107 /**
108  * @brief Read slave-to-host values from PS/2 device.
109  * @param dev Pointer to the device structure for the driver instance.
110  * @param value Pointer used for reading the PS/2 device.
111  *
112  * @retval 0 If successful.
113  * @retval Negative errno code if failure.
114  */
115 __syscall int ps2_read(const struct device *dev,  uint8_t *value);
116 
z_impl_ps2_read(const struct device * dev,uint8_t * value)117 static inline int z_impl_ps2_read(const struct device *dev, uint8_t *value)
118 {
119 	const struct ps2_driver_api *api =
120 			(const struct ps2_driver_api *)dev->api;
121 
122 	return api->read(dev, value);
123 }
124 
125 /**
126  * @brief Enables callback.
127  * @param dev Pointer to the device structure for the driver instance.
128  *
129  * @retval 0 If successful.
130  * @retval Negative errno code if failure.
131  */
132 __syscall int ps2_enable_callback(const struct device *dev);
133 
z_impl_ps2_enable_callback(const struct device * dev)134 static inline int z_impl_ps2_enable_callback(const struct device *dev)
135 {
136 	const struct ps2_driver_api *api =
137 			(const struct ps2_driver_api *)dev->api;
138 
139 	if (api->enable_callback == NULL) {
140 		return -ENOSYS;
141 	}
142 
143 	return api->enable_callback(dev);
144 }
145 
146 /**
147  * @brief Disables callback.
148  * @param dev Pointer to the device structure for the driver instance.
149  *
150  * @retval 0 If successful.
151  * @retval Negative errno code if failure.
152  */
153 __syscall int ps2_disable_callback(const struct device *dev);
154 
z_impl_ps2_disable_callback(const struct device * dev)155 static inline int z_impl_ps2_disable_callback(const struct device *dev)
156 {
157 	const struct ps2_driver_api *api =
158 			(const struct ps2_driver_api *)dev->api;
159 
160 	if (api->disable_callback == NULL) {
161 		return -ENOSYS;
162 	}
163 
164 	return api->disable_callback(dev);
165 }
166 
167 #ifdef __cplusplus
168 }
169 #endif
170 
171 /**
172  * @}
173  */
174 
175 #include <syscalls/ps2.h>
176 
177 #endif /* ZEPHYR_INCLUDE_DRIVERS_PS2_H_ */
178