Skip to content

Commit 6fe729c

Browse files
ndreysgregkh
authored andcommitted
serdev: Add serdev_device_write subroutine
Add serdev_device_write() a blocking call allowing to transfer arbitraty amount of data (potentially exceeding amount that serdev_device_write_buf can process in a single call) To support that, also add serdev_device_write_wakeup(). Drivers wanting to use full extent of serdev_device_write functionality are expected to provide serdev_device_write_wakeup() as a sole handler of .write_wakeup event or call it as a part of driver's custom .write_wakeup code. Because serdev_device_write() subroutine is a superset of serdev_device_write_buf() the patch re-impelements latter is terms of the former. For drivers wanting to just use serdev_device_write_buf() .write_wakeup handler is optional. Cc: cphealy@gmail.com Cc: Guenter Roeck <linux@roeck-us.net> Cc: linux-serial@vger.kernel.org Cc: linux-kernel@vger.kernel.org Reviewed-by: Rob Herring <robh@kernel.org> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e1dc9b0 commit 6fe729c

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

drivers/tty/serdev/core.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,41 @@ void serdev_device_close(struct serdev_device *serdev)
116116
}
117117
EXPORT_SYMBOL_GPL(serdev_device_close);
118118

119-
int serdev_device_write_buf(struct serdev_device *serdev,
120-
const unsigned char *buf, size_t count)
119+
void serdev_device_write_wakeup(struct serdev_device *serdev)
120+
{
121+
complete(&serdev->write_comp);
122+
}
123+
EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
124+
125+
int serdev_device_write(struct serdev_device *serdev,
126+
const unsigned char *buf, size_t count,
127+
unsigned long timeout)
121128
{
122129
struct serdev_controller *ctrl = serdev->ctrl;
130+
int ret;
123131

124-
if (!ctrl || !ctrl->ops->write_buf)
132+
if (!ctrl || !ctrl->ops->write_buf ||
133+
(timeout && !serdev->ops->write_wakeup))
125134
return -EINVAL;
126135

127-
return ctrl->ops->write_buf(ctrl, buf, count);
136+
mutex_lock(&serdev->write_lock);
137+
do {
138+
reinit_completion(&serdev->write_comp);
139+
140+
ret = ctrl->ops->write_buf(ctrl, buf, count);
141+
if (ret < 0)
142+
break;
143+
144+
buf += ret;
145+
count -= ret;
146+
147+
} while (count &&
148+
(timeout = wait_for_completion_timeout(&serdev->write_comp,
149+
timeout)));
150+
mutex_unlock(&serdev->write_lock);
151+
return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
128152
}
129-
EXPORT_SYMBOL_GPL(serdev_device_write_buf);
153+
EXPORT_SYMBOL_GPL(serdev_device_write);
130154

131155
void serdev_device_write_flush(struct serdev_device *serdev)
132156
{
@@ -232,6 +256,8 @@ struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
232256
serdev->dev.parent = &ctrl->dev;
233257
serdev->dev.bus = &serdev_bus_type;
234258
serdev->dev.type = &serdev_device_type;
259+
init_completion(&serdev->write_comp);
260+
mutex_init(&serdev->write_lock);
235261
return serdev;
236262
}
237263
EXPORT_SYMBOL_GPL(serdev_device_alloc);

include/linux/serdev.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ struct serdev_device_ops {
3939
* @nr: Device number on serdev bus.
4040
* @ctrl: serdev controller managing this device.
4141
* @ops: Device operations.
42+
* @write_comp Completion used by serdev_device_write() internally
43+
* @write_lock Lock to serialize access when writing data
4244
*/
4345
struct serdev_device {
4446
struct device dev;
4547
int nr;
4648
struct serdev_controller *ctrl;
4749
const struct serdev_device_ops *ops;
50+
struct completion write_comp;
51+
struct mutex write_lock;
4852
};
4953

5054
static inline struct serdev_device *to_serdev_device(struct device *d)
@@ -186,7 +190,8 @@ int serdev_device_open(struct serdev_device *);
186190
void serdev_device_close(struct serdev_device *);
187191
unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
188192
void serdev_device_set_flow_control(struct serdev_device *, bool);
189-
int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
193+
void serdev_device_write_wakeup(struct serdev_device *);
194+
int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, unsigned long);
190195
void serdev_device_write_flush(struct serdev_device *);
191196
int serdev_device_write_room(struct serdev_device *);
192197

@@ -223,7 +228,8 @@ static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev
223228
return 0;
224229
}
225230
static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
226-
static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsigned char *buf, size_t count)
231+
static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf,
232+
size_t count, unsigned long timeout)
227233
{
228234
return -ENODEV;
229235
}
@@ -259,4 +265,11 @@ static inline struct device *serdev_tty_port_register(struct tty_port *port,
259265
static inline void serdev_tty_port_unregister(struct tty_port *port) {}
260266
#endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
261267

268+
static inline int serdev_device_write_buf(struct serdev_device *serdev,
269+
const unsigned char *data,
270+
size_t count)
271+
{
272+
return serdev_device_write(serdev, data, count, 0);
273+
}
274+
262275
#endif /*_LINUX_SERDEV_H */

0 commit comments

Comments
 (0)