File Index
Files and directories
Sign in
FILE

serdev.h

File preview with syntax highlighting.

linux-6.19.14/include/linux/serdev.h C
  1/* SPDX-License-Identifier: GPL-2.0-only */
  2/*
  3 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
  4 */
  5#ifndef _LINUX_SERDEV_H
  6#define _LINUX_SERDEV_H
  7
  8#include <linux/types.h>
  9#include <linux/device.h>
 10#include <linux/iopoll.h>
 11#include <linux/uaccess.h>
 12#include <linux/termios.h>
 13#include <linux/delay.h>
 14
 15struct serdev_controller;
 16struct serdev_device;
 17
 18/*
 19 * serdev device structures
 20 */
 21
 22/**
 23 * struct serdev_device_ops - Callback operations for a serdev device
 24 * @receive_buf:	Function called with data received from device;
 25 *			returns number of bytes accepted; may sleep.
 26 * @write_wakeup:	Function called when ready to transmit more data; must
 27 *			not sleep.
 28 */
 29struct serdev_device_ops {
 30	size_t (*receive_buf)(struct serdev_device *, const u8 *, size_t);
 31	void (*write_wakeup)(struct serdev_device *);
 32};
 33
 34/**
 35 * struct serdev_device - Basic representation of an serdev device
 36 * @dev:	Driver model representation of the device.
 37 * @nr:		Device number on serdev bus.
 38 * @ctrl:	serdev controller managing this device.
 39 * @ops:	Device operations.
 40 * @write_comp	Completion used by serdev_device_write() internally
 41 * @write_lock	Lock to serialize access when writing data
 42 */
 43struct serdev_device {
 44	struct device dev;
 45	int nr;
 46	struct serdev_controller *ctrl;
 47	const struct serdev_device_ops *ops;
 48	struct completion write_comp;
 49	struct mutex write_lock;
 50};
 51
 52static inline struct serdev_device *to_serdev_device(struct device *d)
 53{
 54	return container_of(d, struct serdev_device, dev);
 55}
 56
 57/**
 58 * struct serdev_device_driver - serdev slave device driver
 59 * @driver:	serdev device drivers should initialize name field of this
 60 *		structure.
 61 * @probe:	binds this driver to a serdev device.
 62 * @remove:	unbinds this driver from the serdev device.
 63 */
 64struct serdev_device_driver {
 65	struct device_driver driver;
 66	int	(*probe)(struct serdev_device *);
 67	void	(*remove)(struct serdev_device *);
 68};
 69
 70static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d)
 71{
 72	return container_of(d, struct serdev_device_driver, driver);
 73}
 74
 75enum serdev_parity {
 76	SERDEV_PARITY_NONE,
 77	SERDEV_PARITY_EVEN,
 78	SERDEV_PARITY_ODD,
 79};
 80
 81/*
 82 * serdev controller structures
 83 */
 84struct serdev_controller_ops {
 85	ssize_t (*write_buf)(struct serdev_controller *, const u8 *, size_t);
 86	void (*write_flush)(struct serdev_controller *);
 87	int (*open)(struct serdev_controller *);
 88	void (*close)(struct serdev_controller *);
 89	void (*set_flow_control)(struct serdev_controller *, bool);
 90	int (*set_parity)(struct serdev_controller *, enum serdev_parity);
 91	unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
 92	void (*wait_until_sent)(struct serdev_controller *, long);
 93	int (*get_tiocm)(struct serdev_controller *);
 94	int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int);
 95	int (*break_ctl)(struct serdev_controller *ctrl, unsigned int break_state);
 96};
 97
 98/**
 99 * struct serdev_controller - interface to the serdev controller
100 * @dev:	Driver model representation of the device.
101 * @host:	Serial port hardware controller device
102 * @nr:		number identifier for this controller/bus.
103 * @serdev:	Pointer to slave device for this controller.
104 * @ops:	Controller operations.
105 */
106struct serdev_controller {
107	struct device		dev;
108	struct device		*host;
109	unsigned int		nr;
110	struct serdev_device	*serdev;
111	const struct serdev_controller_ops *ops;
112};
113
114static inline struct serdev_controller *to_serdev_controller(struct device *d)
115{
116	return container_of(d, struct serdev_controller, dev);
117}
118
119static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
120{
121	return dev_get_drvdata(&serdev->dev);
122}
123
124static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
125{
126	dev_set_drvdata(&serdev->dev, data);
127}
128
129/**
130 * serdev_device_put() - decrement serdev device refcount
131 * @serdev	serdev device.
132 */
133static inline void serdev_device_put(struct serdev_device *serdev)
134{
135	if (serdev)
136		put_device(&serdev->dev);
137}
138
139static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
140					      const struct serdev_device_ops *ops)
141{
142	serdev->ops = ops;
143}
144
145static inline
146void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
147{
148	return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
149}
150
151static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
152					       void *data)
153{
154	dev_set_drvdata(&ctrl->dev, data);
155}
156
157/**
158 * serdev_controller_put() - decrement controller refcount
159 * @ctrl	serdev controller.
160 */
161static inline void serdev_controller_put(struct serdev_controller *ctrl)
162{
163	if (ctrl)
164		put_device(&ctrl->dev);
165}
166
167struct serdev_device *serdev_device_alloc(struct serdev_controller *);
168int serdev_device_add(struct serdev_device *);
169void serdev_device_remove(struct serdev_device *);
170
171struct serdev_controller *serdev_controller_alloc(struct device *host,
172						  struct device *parent,
173						  size_t size);
174int serdev_controller_add(struct serdev_controller *);
175void serdev_controller_remove(struct serdev_controller *);
176
177static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
178{
179	struct serdev_device *serdev = ctrl->serdev;
180
181	if (!serdev || !serdev->ops->write_wakeup)
182		return;
183
184	serdev->ops->write_wakeup(serdev);
185}
186
187static inline size_t serdev_controller_receive_buf(struct serdev_controller *ctrl,
188						   const u8 *data,
189						   size_t count)
190{
191	struct serdev_device *serdev = ctrl->serdev;
192
193	if (!serdev || !serdev->ops->receive_buf)
194		return 0;
195
196	return serdev->ops->receive_buf(serdev, data, count);
197}
198
199#if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
200
201int serdev_device_open(struct serdev_device *);
202void serdev_device_close(struct serdev_device *);
203int devm_serdev_device_open(struct device *, struct serdev_device *);
204unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
205void serdev_device_set_flow_control(struct serdev_device *, bool);
206int serdev_device_write_buf(struct serdev_device *, const u8 *, size_t);
207void serdev_device_wait_until_sent(struct serdev_device *, long);
208int serdev_device_get_tiocm(struct serdev_device *);
209int serdev_device_set_tiocm(struct serdev_device *, int, int);
210int serdev_device_break_ctl(struct serdev_device *serdev, int break_state);
211void serdev_device_write_wakeup(struct serdev_device *);
212ssize_t serdev_device_write(struct serdev_device *, const u8 *, size_t, long);
213void serdev_device_write_flush(struct serdev_device *);
214
215/*
216 * serdev device driver functions
217 */
218int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
219#define serdev_device_driver_register(sdrv) \
220	__serdev_device_driver_register(sdrv, THIS_MODULE)
221
222/**
223 * serdev_device_driver_unregister() - unregister an serdev client driver
224 * @sdrv:	the driver to unregister
225 */
226static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
227{
228	if (sdrv)
229		driver_unregister(&sdrv->driver);
230}
231
232#define module_serdev_device_driver(__serdev_device_driver) \
233	module_driver(__serdev_device_driver, serdev_device_driver_register, \
234			serdev_device_driver_unregister)
235
236#else
237
238static inline int serdev_device_open(struct serdev_device *sdev)
239{
240	return -ENODEV;
241}
242static inline void serdev_device_close(struct serdev_device *sdev) {}
243static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
244{
245	return 0;
246}
247static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
248static inline int serdev_device_write_buf(struct serdev_device *serdev,
249					  const u8 *buf,
250					  size_t count)
251{
252	return -ENODEV;
253}
254static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
255static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
256{
257	return -EOPNOTSUPP;
258}
259static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
260{
261	return -EOPNOTSUPP;
262}
263static inline int serdev_device_break_ctl(struct serdev_device *serdev, int break_state)
264{
265	return -EOPNOTSUPP;
266}
267static inline ssize_t serdev_device_write(struct serdev_device *sdev,
268					  const u8 *buf, size_t count,
269					  unsigned long timeout)
270{
271	return -ENODEV;
272}
273static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
274
275#define serdev_device_driver_register(x)
276#define serdev_device_driver_unregister(x)
277
278#endif /* CONFIG_SERIAL_DEV_BUS */
279
280static inline bool serdev_device_get_cts(struct serdev_device *serdev)
281{
282	int status = serdev_device_get_tiocm(serdev);
283	return !!(status & TIOCM_CTS);
284}
285
286static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms)
287{
288	bool signal;
289
290	return readx_poll_timeout(serdev_device_get_cts, serdev, signal, signal == state,
291				  2000, timeout_ms * 1000);
292}
293
294static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable)
295{
296	if (enable)
297		return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0);
298	else
299		return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS);
300}
301
302int serdev_device_set_parity(struct serdev_device *serdev,
303			     enum serdev_parity parity);
304
305/*
306 * serdev hooks into TTY core
307 */
308struct tty_port;
309struct tty_driver;
310
311#ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
312struct device *serdev_tty_port_register(struct tty_port *port,
313					struct device *host,
314					struct device *parent,
315					struct tty_driver *drv, int idx);
316int serdev_tty_port_unregister(struct tty_port *port);
317#else
318static inline struct device *serdev_tty_port_register(struct tty_port *port,
319					   struct device *host,
320					   struct device *parent,
321					   struct tty_driver *drv, int idx)
322{
323	return ERR_PTR(-ENODEV);
324}
325static inline int serdev_tty_port_unregister(struct tty_port *port)
326{
327	return -ENODEV;
328}
329#endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
330
331struct acpi_resource;
332struct acpi_resource_uart_serialbus;
333
334#ifdef CONFIG_ACPI
335bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
336				   struct acpi_resource_uart_serialbus **uart);
337#else
338static inline bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
339						 struct acpi_resource_uart_serialbus **uart)
340{
341	return false;
342}
343#endif /* CONFIG_ACPI */
344
345#endif /*_LINUX_SERDEV_H */
~/ Read-only file preview.