1
2
3
4
5
6
7
8
9
10
11
12
13#ifndef _LINUX_SERDEV_H
14#define _LINUX_SERDEV_H
15
16#include <linux/types.h>
17#include <linux/device.h>
18#include <linux/termios.h>
19#include <linux/delay.h>
20
21struct serdev_controller;
22struct serdev_device;
23
24
25
26
27
28
29
30
31
32
33struct 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
40
41
42
43
44
45
46
47struct serdev_device {
48 struct device dev;
49 int nr;
50 struct serdev_controller *ctrl;
51 const struct serdev_device_ops *ops;
52 struct completion write_comp;
53 struct mutex write_lock;
54};
55
56static inline struct serdev_device *to_serdev_device(struct device *d)
57{
58 return container_of(d, struct serdev_device, dev);
59}
60
61
62
63
64
65
66
67
68struct serdev_device_driver {
69 struct device_driver driver;
70 int (*probe)(struct serdev_device *);
71 void (*remove)(struct serdev_device *);
72};
73
74static 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
81
82struct 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);
90 void (*wait_until_sent)(struct serdev_controller *, long);
91 int (*get_tiocm)(struct serdev_controller *);
92 int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int);
93};
94
95
96
97
98
99
100
101
102struct serdev_controller {
103 struct device dev;
104 unsigned int nr;
105 struct serdev_device *serdev;
106 const struct serdev_controller_ops *ops;
107};
108
109static inline struct serdev_controller *to_serdev_controller(struct device *d)
110{
111 return container_of(d, struct serdev_controller, dev);
112}
113
114static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
115{
116 return dev_get_drvdata(&serdev->dev);
117}
118
119static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
120{
121 dev_set_drvdata(&serdev->dev, data);
122}
123
124
125
126
127
128static inline void serdev_device_put(struct serdev_device *serdev)
129{
130 if (serdev)
131 put_device(&serdev->dev);
132}
133
134static 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
140static inline
141void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
142{
143 return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
144}
145
146static 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
154
155
156static inline void serdev_controller_put(struct serdev_controller *ctrl)
157{
158 if (ctrl)
159 put_device(&ctrl->dev);
160}
161
162struct serdev_device *serdev_device_alloc(struct serdev_controller *);
163int serdev_device_add(struct serdev_device *);
164void serdev_device_remove(struct serdev_device *);
165
166struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
167int serdev_controller_add(struct serdev_controller *);
168void serdev_controller_remove(struct serdev_controller *);
169
170static 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
177 serdev->ops->write_wakeup(serdev);
178}
179
180static 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 0;
188
189 return serdev->ops->receive_buf(serdev, data, count);
190}
191
192#if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
193
194int serdev_device_open(struct serdev_device *);
195void serdev_device_close(struct serdev_device *);
196unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
197void serdev_device_set_flow_control(struct serdev_device *, bool);
198int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
199void serdev_device_wait_until_sent(struct serdev_device *, long);
200int serdev_device_get_tiocm(struct serdev_device *);
201int serdev_device_set_tiocm(struct serdev_device *, int, int);
202void serdev_device_write_wakeup(struct serdev_device *);
203int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, unsigned long);
204void serdev_device_write_flush(struct serdev_device *);
205int serdev_device_write_room(struct serdev_device *);
206
207
208
209
210int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
211#define serdev_device_driver_register(sdrv) \
212 __serdev_device_driver_register(sdrv, THIS_MODULE)
213
214
215
216
217
218static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
219{
220 if (sdrv)
221 driver_unregister(&sdrv->driver);
222}
223
224#define module_serdev_device_driver(__serdev_device_driver) \
225 module_driver(__serdev_device_driver, serdev_device_driver_register, \
226 serdev_device_driver_unregister)
227
228#else
229
230static inline int serdev_device_open(struct serdev_device *sdev)
231{
232 return -ENODEV;
233}
234static inline void serdev_device_close(struct serdev_device *sdev) {}
235static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
236{
237 return 0;
238}
239static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
240static inline int serdev_device_write_buf(struct serdev_device *serdev,
241 const unsigned char *buf,
242 size_t count)
243{
244 return -ENODEV;
245}
246static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
247static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
248{
249 return -ENOTSUPP;
250}
251static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
252{
253 return -ENOTSUPP;
254}
255static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf,
256 size_t count, unsigned long timeout)
257{
258 return -ENODEV;
259}
260static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
261static inline int serdev_device_write_room(struct serdev_device *sdev)
262{
263 return 0;
264}
265
266#define serdev_device_driver_register(x)
267#define serdev_device_driver_unregister(x)
268
269#endif
270
271static inline bool serdev_device_get_cts(struct serdev_device *serdev)
272{
273 int status = serdev_device_get_tiocm(serdev);
274 return !!(status & TIOCM_CTS);
275}
276
277static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms)
278{
279 unsigned long timeout;
280 bool signal;
281
282 timeout = jiffies + msecs_to_jiffies(timeout_ms);
283 while (time_is_after_jiffies(timeout)) {
284 signal = serdev_device_get_cts(serdev);
285 if (signal == state)
286 return 0;
287 usleep_range(1000, 2000);
288 }
289
290 return -ETIMEDOUT;
291}
292
293static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable)
294{
295 if (enable)
296 return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0);
297 else
298 return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS);
299}
300
301
302
303
304struct tty_port;
305struct tty_driver;
306
307#ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
308struct device *serdev_tty_port_register(struct tty_port *port,
309 struct device *parent,
310 struct tty_driver *drv, int idx);
311int serdev_tty_port_unregister(struct tty_port *port);
312#else
313static inline struct device *serdev_tty_port_register(struct tty_port *port,
314 struct device *parent,
315 struct tty_driver *drv, int idx)
316{
317 return ERR_PTR(-ENODEV);
318}
319static inline int serdev_tty_port_unregister(struct tty_port *port)
320{
321 return -ENODEV;
322}
323#endif
324
325#endif
326