]>
Commit | Line | Data |
---|---|---|
cd6484e1 RH |
1 | /* |
2 | * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 and | |
6 | * only version 2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | */ | |
13 | #ifndef _LINUX_SERDEV_H | |
14 | #define _LINUX_SERDEV_H | |
15 | ||
16 | #include <linux/types.h> | |
17 | #include <linux/device.h> | |
5659dab2 | 18 | #include <linux/termios.h> |
756db778 | 19 | #include <linux/delay.h> |
cd6484e1 RH |
20 | |
21 | struct serdev_controller; | |
22 | struct serdev_device; | |
23 | ||
24 | /* | |
25 | * serdev device structures | |
26 | */ | |
27 | ||
28 | /** | |
29 | * struct serdev_device_ops - Callback operations for a serdev device | |
30 | * @receive_buf: Function called with data received from device. | |
31 | * @write_wakeup: Function called when ready to transmit more data. | |
32 | */ | |
33 | struct serdev_device_ops { | |
34 | int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t); | |
35 | void (*write_wakeup)(struct serdev_device *); | |
36 | }; | |
37 | ||
38 | /** | |
39 | * struct serdev_device - Basic representation of an serdev device | |
40 | * @dev: Driver model representation of the device. | |
41 | * @nr: Device number on serdev bus. | |
42 | * @ctrl: serdev controller managing this device. | |
43 | * @ops: Device operations. | |
6fe729c4 AS |
44 | * @write_comp Completion used by serdev_device_write() internally |
45 | * @write_lock Lock to serialize access when writing data | |
cd6484e1 RH |
46 | */ |
47 | struct serdev_device { | |
48 | struct device dev; | |
49 | int nr; | |
50 | struct serdev_controller *ctrl; | |
51 | const struct serdev_device_ops *ops; | |
6fe729c4 AS |
52 | struct completion write_comp; |
53 | struct mutex write_lock; | |
cd6484e1 RH |
54 | }; |
55 | ||
56 | static inline struct serdev_device *to_serdev_device(struct device *d) | |
57 | { | |
58 | return container_of(d, struct serdev_device, dev); | |
59 | } | |
60 | ||
61 | /** | |
62 | * struct serdev_device_driver - serdev slave device driver | |
63 | * @driver: serdev device drivers should initialize name field of this | |
64 | * structure. | |
65 | * @probe: binds this driver to a serdev device. | |
66 | * @remove: unbinds this driver from the serdev device. | |
67 | */ | |
68 | struct serdev_device_driver { | |
69 | struct device_driver driver; | |
70 | int (*probe)(struct serdev_device *); | |
71 | void (*remove)(struct serdev_device *); | |
72 | }; | |
73 | ||
74 | static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d) | |
75 | { | |
76 | return container_of(d, struct serdev_device_driver, driver); | |
77 | } | |
78 | ||
79 | /* | |
80 | * serdev controller structures | |
81 | */ | |
82 | struct serdev_controller_ops { | |
83 | int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t); | |
84 | void (*write_flush)(struct serdev_controller *); | |
85 | int (*write_room)(struct serdev_controller *); | |
86 | int (*open)(struct serdev_controller *); | |
87 | void (*close)(struct serdev_controller *); | |
88 | void (*set_flow_control)(struct serdev_controller *, bool); | |
89 | unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int); | |
b3f80c8f | 90 | void (*wait_until_sent)(struct serdev_controller *, long); |
5659dab2 SR |
91 | int (*get_tiocm)(struct serdev_controller *); |
92 | int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int); | |
cd6484e1 RH |
93 | }; |
94 | ||
95 | /** | |
96 | * struct serdev_controller - interface to the serdev controller | |
97 | * @dev: Driver model representation of the device. | |
98 | * @nr: number identifier for this controller/bus. | |
99 | * @serdev: Pointer to slave device for this controller. | |
100 | * @ops: Controller operations. | |
101 | */ | |
102 | struct serdev_controller { | |
103 | struct device dev; | |
104 | unsigned int nr; | |
105 | struct serdev_device *serdev; | |
106 | const struct serdev_controller_ops *ops; | |
107 | }; | |
108 | ||
109 | static inline struct serdev_controller *to_serdev_controller(struct device *d) | |
110 | { | |
111 | return container_of(d, struct serdev_controller, dev); | |
112 | } | |
113 | ||
114 | static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev) | |
115 | { | |
116 | return dev_get_drvdata(&serdev->dev); | |
117 | } | |
118 | ||
119 | static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data) | |
120 | { | |
121 | dev_set_drvdata(&serdev->dev, data); | |
122 | } | |
123 | ||
124 | /** | |
125 | * serdev_device_put() - decrement serdev device refcount | |
126 | * @serdev serdev device. | |
127 | */ | |
128 | static inline void serdev_device_put(struct serdev_device *serdev) | |
129 | { | |
130 | if (serdev) | |
131 | put_device(&serdev->dev); | |
132 | } | |
133 | ||
134 | static inline void serdev_device_set_client_ops(struct serdev_device *serdev, | |
135 | const struct serdev_device_ops *ops) | |
136 | { | |
137 | serdev->ops = ops; | |
138 | } | |
139 | ||
140 | static inline | |
141 | void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl) | |
142 | { | |
143 | return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL; | |
144 | } | |
145 | ||
146 | static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl, | |
147 | void *data) | |
148 | { | |
149 | dev_set_drvdata(&ctrl->dev, data); | |
150 | } | |
151 | ||
152 | /** | |
153 | * serdev_controller_put() - decrement controller refcount | |
154 | * @ctrl serdev controller. | |
155 | */ | |
156 | static inline void serdev_controller_put(struct serdev_controller *ctrl) | |
157 | { | |
158 | if (ctrl) | |
159 | put_device(&ctrl->dev); | |
160 | } | |
161 | ||
162 | struct serdev_device *serdev_device_alloc(struct serdev_controller *); | |
163 | int serdev_device_add(struct serdev_device *); | |
164 | void serdev_device_remove(struct serdev_device *); | |
165 | ||
166 | struct serdev_controller *serdev_controller_alloc(struct device *, size_t); | |
167 | int serdev_controller_add(struct serdev_controller *); | |
168 | void serdev_controller_remove(struct serdev_controller *); | |
169 | ||
170 | static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl) | |
171 | { | |
172 | struct serdev_device *serdev = ctrl->serdev; | |
173 | ||
174 | if (!serdev || !serdev->ops->write_wakeup) | |
175 | return; | |
176 | ||
9d1d994d | 177 | serdev->ops->write_wakeup(serdev); |
cd6484e1 RH |
178 | } |
179 | ||
180 | static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl, | |
181 | const unsigned char *data, | |
182 | size_t count) | |
183 | { | |
184 | struct serdev_device *serdev = ctrl->serdev; | |
185 | ||
186 | if (!serdev || !serdev->ops->receive_buf) | |
187 | return -EINVAL; | |
188 | ||
9d1d994d | 189 | return serdev->ops->receive_buf(serdev, data, count); |
cd6484e1 RH |
190 | } |
191 | ||
192 | #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS) | |
193 | ||
194 | int serdev_device_open(struct serdev_device *); | |
195 | void serdev_device_close(struct serdev_device *); | |
196 | unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int); | |
197 | void serdev_device_set_flow_control(struct serdev_device *, bool); | |
b3f80c8f | 198 | void serdev_device_wait_until_sent(struct serdev_device *, long); |
5659dab2 SR |
199 | int serdev_device_get_tiocm(struct serdev_device *); |
200 | int serdev_device_set_tiocm(struct serdev_device *, int, int); | |
6fe729c4 AS |
201 | void serdev_device_write_wakeup(struct serdev_device *); |
202 | int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, unsigned long); | |
cd6484e1 RH |
203 | void serdev_device_write_flush(struct serdev_device *); |
204 | int serdev_device_write_room(struct serdev_device *); | |
205 | ||
206 | /* | |
207 | * serdev device driver functions | |
208 | */ | |
209 | int __serdev_device_driver_register(struct serdev_device_driver *, struct module *); | |
210 | #define serdev_device_driver_register(sdrv) \ | |
211 | __serdev_device_driver_register(sdrv, THIS_MODULE) | |
212 | ||
213 | /** | |
214 | * serdev_device_driver_unregister() - unregister an serdev client driver | |
215 | * @sdrv: the driver to unregister | |
216 | */ | |
217 | static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv) | |
218 | { | |
219 | if (sdrv) | |
220 | driver_unregister(&sdrv->driver); | |
221 | } | |
222 | ||
223 | #define module_serdev_device_driver(__serdev_device_driver) \ | |
224 | module_driver(__serdev_device_driver, serdev_device_driver_register, \ | |
225 | serdev_device_driver_unregister) | |
226 | ||
227 | #else | |
228 | ||
229 | static inline int serdev_device_open(struct serdev_device *sdev) | |
230 | { | |
231 | return -ENODEV; | |
232 | } | |
233 | static inline void serdev_device_close(struct serdev_device *sdev) {} | |
234 | static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate) | |
235 | { | |
236 | return 0; | |
237 | } | |
238 | static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {} | |
b3f80c8f | 239 | static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {} |
5659dab2 SR |
240 | static inline int serdev_device_get_tiocm(struct serdev_device *serdev) |
241 | { | |
242 | return -ENOTSUPP; | |
243 | } | |
244 | static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear) | |
245 | { | |
246 | return -ENOTSUPP; | |
247 | } | |
6fe729c4 AS |
248 | static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf, |
249 | size_t count, unsigned long timeout) | |
cd6484e1 RH |
250 | { |
251 | return -ENODEV; | |
252 | } | |
253 | static inline void serdev_device_write_flush(struct serdev_device *sdev) {} | |
254 | static inline int serdev_device_write_room(struct serdev_device *sdev) | |
255 | { | |
256 | return 0; | |
257 | } | |
258 | ||
259 | #define serdev_device_driver_register(x) | |
260 | #define serdev_device_driver_unregister(x) | |
261 | ||
262 | #endif /* CONFIG_SERIAL_DEV_BUS */ | |
263 | ||
756db778 SR |
264 | static inline bool serdev_device_get_cts(struct serdev_device *serdev) |
265 | { | |
266 | int status = serdev_device_get_tiocm(serdev); | |
267 | return !!(status & TIOCM_CTS); | |
268 | } | |
269 | ||
270 | static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms) | |
271 | { | |
272 | unsigned long timeout; | |
273 | bool signal; | |
274 | ||
275 | timeout = jiffies + msecs_to_jiffies(timeout_ms); | |
276 | while (time_is_after_jiffies(timeout)) { | |
277 | signal = serdev_device_get_cts(serdev); | |
278 | if (signal == state) | |
279 | return 0; | |
280 | usleep_range(1000, 2000); | |
281 | } | |
282 | ||
283 | return -ETIMEDOUT; | |
284 | } | |
285 | ||
286 | static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable) | |
287 | { | |
288 | if (enable) | |
289 | return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0); | |
290 | else | |
291 | return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS); | |
292 | } | |
293 | ||
bed35c6d RH |
294 | /* |
295 | * serdev hooks into TTY core | |
296 | */ | |
297 | struct tty_port; | |
298 | struct tty_driver; | |
299 | ||
300 | #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT | |
301 | struct device *serdev_tty_port_register(struct tty_port *port, | |
302 | struct device *parent, | |
303 | struct tty_driver *drv, int idx); | |
304 | void serdev_tty_port_unregister(struct tty_port *port); | |
305 | #else | |
306 | static inline struct device *serdev_tty_port_register(struct tty_port *port, | |
307 | struct device *parent, | |
308 | struct tty_driver *drv, int idx) | |
309 | { | |
310 | return ERR_PTR(-ENODEV); | |
311 | } | |
312 | static inline void serdev_tty_port_unregister(struct tty_port *port) {} | |
313 | #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */ | |
314 | ||
6fe729c4 AS |
315 | static inline int serdev_device_write_buf(struct serdev_device *serdev, |
316 | const unsigned char *data, | |
317 | size_t count) | |
318 | { | |
319 | return serdev_device_write(serdev, data, count, 0); | |
320 | } | |
321 | ||
cd6484e1 | 322 | #endif /*_LINUX_SERDEV_H */ |