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.
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.
13 #ifndef _LINUX_SERDEV_H
14 #define _LINUX_SERDEV_H
16 #include <linux/types.h>
17 #include <linux/device.h>
19 struct serdev_controller;
23 * serdev device structures
27 * struct serdev_device_ops - Callback operations for a serdev device
28 * @receive_buf: Function called with data received from device.
29 * @write_wakeup: Function called when ready to transmit more data.
31 struct serdev_device_ops {
32 int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t);
33 void (*write_wakeup)(struct serdev_device *);
37 * struct serdev_device - Basic representation of an serdev device
38 * @dev: Driver model representation of the device.
39 * @nr: Device number on serdev bus.
40 * @ctrl: serdev controller managing this device.
41 * @ops: Device operations.
42 * @write_comp Completion used by serdev_device_write() internally
43 * @write_lock Lock to serialize access when writing data
45 struct serdev_device {
48 struct serdev_controller *ctrl;
49 const struct serdev_device_ops *ops;
50 struct completion write_comp;
51 struct mutex write_lock;
54 static inline struct serdev_device *to_serdev_device(struct device *d)
56 return container_of(d, struct serdev_device, dev);
60 * struct serdev_device_driver - serdev slave device driver
61 * @driver: serdev device drivers should initialize name field of this
63 * @probe: binds this driver to a serdev device.
64 * @remove: unbinds this driver from the serdev device.
66 struct serdev_device_driver {
67 struct device_driver driver;
68 int (*probe)(struct serdev_device *);
69 void (*remove)(struct serdev_device *);
72 static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d)
74 return container_of(d, struct serdev_device_driver, driver);
78 * serdev controller structures
80 struct serdev_controller_ops {
81 int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t);
82 void (*write_flush)(struct serdev_controller *);
83 int (*write_room)(struct serdev_controller *);
84 int (*open)(struct serdev_controller *);
85 void (*close)(struct serdev_controller *);
86 void (*set_flow_control)(struct serdev_controller *, bool);
87 unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
91 * struct serdev_controller - interface to the serdev controller
92 * @dev: Driver model representation of the device.
93 * @nr: number identifier for this controller/bus.
94 * @serdev: Pointer to slave device for this controller.
95 * @ops: Controller operations.
97 struct serdev_controller {
100 struct serdev_device *serdev;
101 const struct serdev_controller_ops *ops;
104 static inline struct serdev_controller *to_serdev_controller(struct device *d)
106 return container_of(d, struct serdev_controller, dev);
109 static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
111 return dev_get_drvdata(&serdev->dev);
114 static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
116 dev_set_drvdata(&serdev->dev, data);
120 * serdev_device_put() - decrement serdev device refcount
121 * @serdev serdev device.
123 static inline void serdev_device_put(struct serdev_device *serdev)
126 put_device(&serdev->dev);
129 static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
130 const struct serdev_device_ops *ops)
136 void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
138 return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
141 static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
144 dev_set_drvdata(&ctrl->dev, data);
148 * serdev_controller_put() - decrement controller refcount
149 * @ctrl serdev controller.
151 static inline void serdev_controller_put(struct serdev_controller *ctrl)
154 put_device(&ctrl->dev);
157 struct serdev_device *serdev_device_alloc(struct serdev_controller *);
158 int serdev_device_add(struct serdev_device *);
159 void serdev_device_remove(struct serdev_device *);
161 struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
162 int serdev_controller_add(struct serdev_controller *);
163 void serdev_controller_remove(struct serdev_controller *);
165 static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
167 struct serdev_device *serdev = ctrl->serdev;
169 if (!serdev || !serdev->ops->write_wakeup)
172 serdev->ops->write_wakeup(serdev);
175 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
176 const unsigned char *data,
179 struct serdev_device *serdev = ctrl->serdev;
181 if (!serdev || !serdev->ops->receive_buf)
184 return serdev->ops->receive_buf(serdev, data, count);
187 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
189 int serdev_device_open(struct serdev_device *);
190 void serdev_device_close(struct serdev_device *);
191 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
192 void serdev_device_set_flow_control(struct serdev_device *, bool);
193 void serdev_device_write_wakeup(struct serdev_device *);
194 int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, unsigned long);
195 void serdev_device_write_flush(struct serdev_device *);
196 int serdev_device_write_room(struct serdev_device *);
199 * serdev device driver functions
201 int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
202 #define serdev_device_driver_register(sdrv) \
203 __serdev_device_driver_register(sdrv, THIS_MODULE)
206 * serdev_device_driver_unregister() - unregister an serdev client driver
207 * @sdrv: the driver to unregister
209 static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
212 driver_unregister(&sdrv->driver);
215 #define module_serdev_device_driver(__serdev_device_driver) \
216 module_driver(__serdev_device_driver, serdev_device_driver_register, \
217 serdev_device_driver_unregister)
221 static inline int serdev_device_open(struct serdev_device *sdev)
225 static inline void serdev_device_close(struct serdev_device *sdev) {}
226 static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
230 static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
231 static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf,
232 size_t count, unsigned long timeout)
236 static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
237 static inline int serdev_device_write_room(struct serdev_device *sdev)
242 #define serdev_device_driver_register(x)
243 #define serdev_device_driver_unregister(x)
245 #endif /* CONFIG_SERIAL_DEV_BUS */
248 * serdev hooks into TTY core
253 #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
254 struct device *serdev_tty_port_register(struct tty_port *port,
255 struct device *parent,
256 struct tty_driver *drv, int idx);
257 void serdev_tty_port_unregister(struct tty_port *port);
259 static inline struct device *serdev_tty_port_register(struct tty_port *port,
260 struct device *parent,
261 struct tty_driver *drv, int idx)
263 return ERR_PTR(-ENODEV);
265 static inline void serdev_tty_port_unregister(struct tty_port *port) {}
266 #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
268 static inline int serdev_device_write_buf(struct serdev_device *serdev,
269 const unsigned char *data,
272 return serdev_device_write(serdev, data, count, 0);
275 #endif /*_LINUX_SERDEV_H */