Merge tag 'intel-pinctrl-v6.9-1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / drivers / tty / rpmsg_tty.c
CommitLineData
7c0408d8
AP
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2021 STMicroelectronics - All Rights Reserved
4 *
5 * The rpmsg tty driver implements serial communication on the RPMsg bus to makes
6 * possible for user-space programs to send and receive rpmsg messages as a standard
7 * tty protocol.
8 *
9 * The remote processor can instantiate a new tty by requesting a "rpmsg-tty" RPMsg service.
10 * The "rpmsg-tty" service is directly used for data exchange. No flow control is implemented yet.
11 */
12
88af70be
AS
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
7c0408d8
AP
15#include <linux/module.h>
16#include <linux/rpmsg.h>
17#include <linux/slab.h>
18#include <linux/tty.h>
19#include <linux/tty_flip.h>
20
73a3d4f4 21#define RPMSG_TTY_NAME "ttyRPMSG"
7c0408d8
AP
22#define MAX_TTY_RPMSG 32
23
24static DEFINE_IDR(tty_idr); /* tty instance id */
25static DEFINE_MUTEX(idr_lock); /* protects tty_idr */
26
27static struct tty_driver *rpmsg_tty_driver;
28
29struct rpmsg_tty_port {
30 struct tty_port port; /* TTY port data */
31 int id; /* TTY rpmsg index */
32 struct rpmsg_device *rpdev; /* rpmsg device */
33};
34
35static int rpmsg_tty_cb(struct rpmsg_device *rpdev, void *data, int len, void *priv, u32 src)
36{
37 struct rpmsg_tty_port *cport = dev_get_drvdata(&rpdev->dev);
38 int copied;
39
40 if (!len)
41 return -EINVAL;
42 copied = tty_insert_flip_string(&cport->port, data, len);
43 if (copied != len)
44 dev_err_ratelimited(&rpdev->dev, "Trunc buffer: available space is %d\n", copied);
45 tty_flip_buffer_push(&cport->port);
46
47 return 0;
48}
49
50static int rpmsg_tty_install(struct tty_driver *driver, struct tty_struct *tty)
51{
52 struct rpmsg_tty_port *cport = idr_find(&tty_idr, tty->index);
db7f19c0 53 struct tty_port *port;
7c0408d8
AP
54
55 tty->driver_data = cport;
56
db7f19c0
AP
57 port = tty_port_get(&cport->port);
58 return tty_port_install(port, driver, tty);
59}
60
61static void rpmsg_tty_cleanup(struct tty_struct *tty)
62{
63 tty_port_put(tty->port);
7c0408d8
AP
64}
65
66static int rpmsg_tty_open(struct tty_struct *tty, struct file *filp)
67{
68 return tty_port_open(tty->port, tty, filp);
69}
70
71static void rpmsg_tty_close(struct tty_struct *tty, struct file *filp)
72{
73 return tty_port_close(tty->port, tty, filp);
74}
75
95713967
JSS
76static ssize_t rpmsg_tty_write(struct tty_struct *tty, const u8 *buf,
77 size_t len)
7c0408d8
AP
78{
79 struct rpmsg_tty_port *cport = tty->driver_data;
80 struct rpmsg_device *rpdev;
81 int msg_max_size, msg_size;
82 int ret;
83
84 rpdev = cport->rpdev;
85
86 msg_max_size = rpmsg_get_mtu(rpdev->ept);
87 if (msg_max_size < 0)
88 return msg_max_size;
89
95713967 90 msg_size = min_t(unsigned int, len, msg_max_size);
7c0408d8
AP
91
92 /*
93 * Use rpmsg_trysend instead of rpmsg_send to send the message so the caller is not
94 * hung until a rpmsg buffer is available. In such case rpmsg_trysend returns -ENOMEM.
95 */
96 ret = rpmsg_trysend(rpdev->ept, (void *)buf, msg_size);
97 if (ret) {
98 dev_dbg_ratelimited(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
99 return ret;
100 }
101
102 return msg_size;
103}
104
105static unsigned int rpmsg_tty_write_room(struct tty_struct *tty)
106{
107 struct rpmsg_tty_port *cport = tty->driver_data;
108 int size;
109
110 size = rpmsg_get_mtu(cport->rpdev->ept);
111 if (size < 0)
112 return 0;
113
114 return size;
115}
116
db7f19c0
AP
117static void rpmsg_tty_hangup(struct tty_struct *tty)
118{
119 tty_port_hangup(tty->port);
120}
121
7c0408d8
AP
122static const struct tty_operations rpmsg_tty_ops = {
123 .install = rpmsg_tty_install,
124 .open = rpmsg_tty_open,
125 .close = rpmsg_tty_close,
126 .write = rpmsg_tty_write,
127 .write_room = rpmsg_tty_write_room,
db7f19c0
AP
128 .hangup = rpmsg_tty_hangup,
129 .cleanup = rpmsg_tty_cleanup,
7c0408d8
AP
130};
131
132static struct rpmsg_tty_port *rpmsg_tty_alloc_cport(void)
133{
134 struct rpmsg_tty_port *cport;
408a5079 135 int ret;
7c0408d8
AP
136
137 cport = kzalloc(sizeof(*cport), GFP_KERNEL);
138 if (!cport)
139 return ERR_PTR(-ENOMEM);
140
141 mutex_lock(&idr_lock);
408a5079 142 ret = idr_alloc(&tty_idr, cport, 0, MAX_TTY_RPMSG, GFP_KERNEL);
7c0408d8
AP
143 mutex_unlock(&idr_lock);
144
408a5079 145 if (ret < 0) {
7c0408d8 146 kfree(cport);
408a5079 147 return ERR_PTR(ret);
7c0408d8
AP
148 }
149
408a5079 150 cport->id = ret;
0572da28 151
7c0408d8
AP
152 return cport;
153}
154
db7f19c0 155static void rpmsg_tty_destruct_port(struct tty_port *port)
7c0408d8 156{
db7f19c0
AP
157 struct rpmsg_tty_port *cport = container_of(port, struct rpmsg_tty_port, port);
158
7c0408d8
AP
159 mutex_lock(&idr_lock);
160 idr_remove(&tty_idr, cport->id);
161 mutex_unlock(&idr_lock);
162
163 kfree(cport);
164}
165
db7f19c0
AP
166static const struct tty_port_operations rpmsg_tty_port_ops = {
167 .destruct = rpmsg_tty_destruct_port,
168};
169
7c0408d8
AP
170
171static int rpmsg_tty_probe(struct rpmsg_device *rpdev)
172{
173 struct rpmsg_tty_port *cport;
174 struct device *dev = &rpdev->dev;
175 struct device *tty_dev;
176 int ret;
177
178 cport = rpmsg_tty_alloc_cport();
8673ef7b
AS
179 if (IS_ERR(cport))
180 return dev_err_probe(dev, PTR_ERR(cport), "Failed to alloc tty port\n");
7c0408d8
AP
181
182 tty_port_init(&cport->port);
183 cport->port.ops = &rpmsg_tty_port_ops;
184
185 tty_dev = tty_port_register_device(&cport->port, rpmsg_tty_driver,
186 cport->id, dev);
187 if (IS_ERR(tty_dev)) {
8673ef7b 188 ret = dev_err_probe(dev, PTR_ERR(tty_dev), "Failed to register tty port\n");
db7f19c0
AP
189 tty_port_put(&cport->port);
190 return ret;
7c0408d8
AP
191 }
192
193 cport->rpdev = rpdev;
194
195 dev_set_drvdata(dev, cport);
196
73a3d4f4 197 dev_dbg(dev, "New channel: 0x%x -> 0x%x: " RPMSG_TTY_NAME "%d\n",
7c0408d8
AP
198 rpdev->src, rpdev->dst, cport->id);
199
200 return 0;
7c0408d8
AP
201}
202
203static void rpmsg_tty_remove(struct rpmsg_device *rpdev)
204{
205 struct rpmsg_tty_port *cport = dev_get_drvdata(&rpdev->dev);
206
207 dev_dbg(&rpdev->dev, "Removing rpmsg tty device %d\n", cport->id);
208
209 /* User hang up to release the tty */
db7f19c0 210 tty_port_tty_hangup(&cport->port, false);
7c0408d8
AP
211
212 tty_unregister_device(rpmsg_tty_driver, cport->id);
213
db7f19c0 214 tty_port_put(&cport->port);
7c0408d8
AP
215}
216
217static struct rpmsg_device_id rpmsg_driver_tty_id_table[] = {
218 { .name = "rpmsg-tty" },
219 { },
220};
221MODULE_DEVICE_TABLE(rpmsg, rpmsg_driver_tty_id_table);
222
223static struct rpmsg_driver rpmsg_tty_rpmsg_drv = {
224 .drv.name = KBUILD_MODNAME,
225 .id_table = rpmsg_driver_tty_id_table,
226 .probe = rpmsg_tty_probe,
227 .callback = rpmsg_tty_cb,
228 .remove = rpmsg_tty_remove,
229};
230
231static int __init rpmsg_tty_init(void)
232{
408a5079 233 int ret;
7c0408d8
AP
234
235 rpmsg_tty_driver = tty_alloc_driver(MAX_TTY_RPMSG, TTY_DRIVER_REAL_RAW |
236 TTY_DRIVER_DYNAMIC_DEV);
237 if (IS_ERR(rpmsg_tty_driver))
238 return PTR_ERR(rpmsg_tty_driver);
239
240 rpmsg_tty_driver->driver_name = "rpmsg_tty";
73a3d4f4 241 rpmsg_tty_driver->name = RPMSG_TTY_NAME;
7c0408d8
AP
242 rpmsg_tty_driver->major = 0;
243 rpmsg_tty_driver->type = TTY_DRIVER_TYPE_CONSOLE;
244
245 /* Disable unused mode by default */
246 rpmsg_tty_driver->init_termios = tty_std_termios;
247 rpmsg_tty_driver->init_termios.c_lflag &= ~(ECHO | ICANON);
248 rpmsg_tty_driver->init_termios.c_oflag &= ~(OPOST | ONLCR);
249
250 tty_set_operations(rpmsg_tty_driver, &rpmsg_tty_ops);
251
408a5079
AS
252 ret = tty_register_driver(rpmsg_tty_driver);
253 if (ret < 0) {
88af70be 254 pr_err("Couldn't install driver: %d\n", ret);
7c0408d8
AP
255 goto error_put;
256 }
257
408a5079
AS
258 ret = register_rpmsg_driver(&rpmsg_tty_rpmsg_drv);
259 if (ret < 0) {
88af70be 260 pr_err("Couldn't register driver: %d\n", ret);
7c0408d8
AP
261 goto error_unregister;
262 }
263
264 return 0;
265
266error_unregister:
267 tty_unregister_driver(rpmsg_tty_driver);
268
269error_put:
270 tty_driver_kref_put(rpmsg_tty_driver);
271
408a5079 272 return ret;
7c0408d8
AP
273}
274
275static void __exit rpmsg_tty_exit(void)
276{
277 unregister_rpmsg_driver(&rpmsg_tty_rpmsg_drv);
278 tty_unregister_driver(rpmsg_tty_driver);
279 tty_driver_kref_put(rpmsg_tty_driver);
280 idr_destroy(&tty_idr);
281}
282
283module_init(rpmsg_tty_init);
284module_exit(rpmsg_tty_exit);
285
286MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@foss.st.com>");
287MODULE_DESCRIPTION("remote processor messaging tty driver");
288MODULE_LICENSE("GPL v2");