blob: 4bee6c5bbb53fb69c6067a52fdcac68040b1db29 [file] [log] [blame]
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +02001// 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
Andy Shevchenko88af70b2021-10-25 16:51:47 +030013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +020015#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
21#define MAX_TTY_RPMSG 32
22
23static DEFINE_IDR(tty_idr); /* tty instance id */
24static DEFINE_MUTEX(idr_lock); /* protects tty_idr */
25
26static struct tty_driver *rpmsg_tty_driver;
27
28struct rpmsg_tty_port {
29 struct tty_port port; /* TTY port data */
30 int id; /* TTY rpmsg index */
31 struct rpmsg_device *rpdev; /* rpmsg device */
32};
33
34static int rpmsg_tty_cb(struct rpmsg_device *rpdev, void *data, int len, void *priv, u32 src)
35{
36 struct rpmsg_tty_port *cport = dev_get_drvdata(&rpdev->dev);
37 int copied;
38
39 if (!len)
40 return -EINVAL;
41 copied = tty_insert_flip_string(&cport->port, data, len);
42 if (copied != len)
43 dev_err_ratelimited(&rpdev->dev, "Trunc buffer: available space is %d\n", copied);
44 tty_flip_buffer_push(&cport->port);
45
46 return 0;
47}
48
49static int rpmsg_tty_install(struct tty_driver *driver, struct tty_struct *tty)
50{
51 struct rpmsg_tty_port *cport = idr_find(&tty_idr, tty->index);
52
53 tty->driver_data = cport;
54
55 return tty_port_install(&cport->port, driver, tty);
56}
57
58static int rpmsg_tty_open(struct tty_struct *tty, struct file *filp)
59{
60 return tty_port_open(tty->port, tty, filp);
61}
62
63static void rpmsg_tty_close(struct tty_struct *tty, struct file *filp)
64{
65 return tty_port_close(tty->port, tty, filp);
66}
67
68static int rpmsg_tty_write(struct tty_struct *tty, const u8 *buf, int len)
69{
70 struct rpmsg_tty_port *cport = tty->driver_data;
71 struct rpmsg_device *rpdev;
72 int msg_max_size, msg_size;
73 int ret;
74
75 rpdev = cport->rpdev;
76
77 msg_max_size = rpmsg_get_mtu(rpdev->ept);
78 if (msg_max_size < 0)
79 return msg_max_size;
80
81 msg_size = min(len, msg_max_size);
82
83 /*
84 * Use rpmsg_trysend instead of rpmsg_send to send the message so the caller is not
85 * hung until a rpmsg buffer is available. In such case rpmsg_trysend returns -ENOMEM.
86 */
87 ret = rpmsg_trysend(rpdev->ept, (void *)buf, msg_size);
88 if (ret) {
89 dev_dbg_ratelimited(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
90 return ret;
91 }
92
93 return msg_size;
94}
95
96static unsigned int rpmsg_tty_write_room(struct tty_struct *tty)
97{
98 struct rpmsg_tty_port *cport = tty->driver_data;
99 int size;
100
101 size = rpmsg_get_mtu(cport->rpdev->ept);
102 if (size < 0)
103 return 0;
104
105 return size;
106}
107
108static const struct tty_operations rpmsg_tty_ops = {
109 .install = rpmsg_tty_install,
110 .open = rpmsg_tty_open,
111 .close = rpmsg_tty_close,
112 .write = rpmsg_tty_write,
113 .write_room = rpmsg_tty_write_room,
114};
115
116static struct rpmsg_tty_port *rpmsg_tty_alloc_cport(void)
117{
118 struct rpmsg_tty_port *cport;
Andy Shevchenko408a5072021-10-25 16:51:45 +0300119 int ret;
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200120
121 cport = kzalloc(sizeof(*cport), GFP_KERNEL);
122 if (!cport)
123 return ERR_PTR(-ENOMEM);
124
125 mutex_lock(&idr_lock);
Andy Shevchenko408a5072021-10-25 16:51:45 +0300126 ret = idr_alloc(&tty_idr, cport, 0, MAX_TTY_RPMSG, GFP_KERNEL);
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200127 mutex_unlock(&idr_lock);
128
Andy Shevchenko408a5072021-10-25 16:51:45 +0300129 if (ret < 0) {
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200130 kfree(cport);
Andy Shevchenko408a5072021-10-25 16:51:45 +0300131 return ERR_PTR(ret);
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200132 }
133
Andy Shevchenko408a5072021-10-25 16:51:45 +0300134 cport->id = ret;
Andy Shevchenko0572da22021-10-25 16:51:44 +0300135
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200136 return cport;
137}
138
139static void rpmsg_tty_release_cport(struct rpmsg_tty_port *cport)
140{
141 mutex_lock(&idr_lock);
142 idr_remove(&tty_idr, cport->id);
143 mutex_unlock(&idr_lock);
144
145 kfree(cport);
146}
147
148static const struct tty_port_operations rpmsg_tty_port_ops = { };
149
150static int rpmsg_tty_probe(struct rpmsg_device *rpdev)
151{
152 struct rpmsg_tty_port *cport;
153 struct device *dev = &rpdev->dev;
154 struct device *tty_dev;
155 int ret;
156
157 cport = rpmsg_tty_alloc_cport();
Andy Shevchenko8673ef72021-10-25 16:51:46 +0300158 if (IS_ERR(cport))
159 return dev_err_probe(dev, PTR_ERR(cport), "Failed to alloc tty port\n");
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200160
161 tty_port_init(&cport->port);
162 cport->port.ops = &rpmsg_tty_port_ops;
163
164 tty_dev = tty_port_register_device(&cport->port, rpmsg_tty_driver,
165 cport->id, dev);
166 if (IS_ERR(tty_dev)) {
Andy Shevchenko8673ef72021-10-25 16:51:46 +0300167 ret = dev_err_probe(dev, PTR_ERR(tty_dev), "Failed to register tty port\n");
168 goto err_destroy;
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200169 }
170
171 cport->rpdev = rpdev;
172
173 dev_set_drvdata(dev, cport);
174
175 dev_dbg(dev, "New channel: 0x%x -> 0x%x : ttyRPMSG%d\n",
176 rpdev->src, rpdev->dst, cport->id);
177
178 return 0;
179
180err_destroy:
181 tty_port_destroy(&cport->port);
182 rpmsg_tty_release_cport(cport);
183
184 return ret;
185}
186
187static void rpmsg_tty_remove(struct rpmsg_device *rpdev)
188{
189 struct rpmsg_tty_port *cport = dev_get_drvdata(&rpdev->dev);
190
191 dev_dbg(&rpdev->dev, "Removing rpmsg tty device %d\n", cport->id);
192
193 /* User hang up to release the tty */
194 if (tty_port_initialized(&cport->port))
195 tty_port_tty_hangup(&cport->port, false);
196
197 tty_unregister_device(rpmsg_tty_driver, cport->id);
198
199 tty_port_destroy(&cport->port);
200 rpmsg_tty_release_cport(cport);
201}
202
203static struct rpmsg_device_id rpmsg_driver_tty_id_table[] = {
204 { .name = "rpmsg-tty" },
205 { },
206};
207MODULE_DEVICE_TABLE(rpmsg, rpmsg_driver_tty_id_table);
208
209static struct rpmsg_driver rpmsg_tty_rpmsg_drv = {
210 .drv.name = KBUILD_MODNAME,
211 .id_table = rpmsg_driver_tty_id_table,
212 .probe = rpmsg_tty_probe,
213 .callback = rpmsg_tty_cb,
214 .remove = rpmsg_tty_remove,
215};
216
217static int __init rpmsg_tty_init(void)
218{
Andy Shevchenko408a5072021-10-25 16:51:45 +0300219 int ret;
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200220
221 rpmsg_tty_driver = tty_alloc_driver(MAX_TTY_RPMSG, TTY_DRIVER_REAL_RAW |
222 TTY_DRIVER_DYNAMIC_DEV);
223 if (IS_ERR(rpmsg_tty_driver))
224 return PTR_ERR(rpmsg_tty_driver);
225
226 rpmsg_tty_driver->driver_name = "rpmsg_tty";
227 rpmsg_tty_driver->name = "ttyRPMSG";
228 rpmsg_tty_driver->major = 0;
229 rpmsg_tty_driver->type = TTY_DRIVER_TYPE_CONSOLE;
230
231 /* Disable unused mode by default */
232 rpmsg_tty_driver->init_termios = tty_std_termios;
233 rpmsg_tty_driver->init_termios.c_lflag &= ~(ECHO | ICANON);
234 rpmsg_tty_driver->init_termios.c_oflag &= ~(OPOST | ONLCR);
235
236 tty_set_operations(rpmsg_tty_driver, &rpmsg_tty_ops);
237
Andy Shevchenko408a5072021-10-25 16:51:45 +0300238 ret = tty_register_driver(rpmsg_tty_driver);
239 if (ret < 0) {
Andy Shevchenko88af70b2021-10-25 16:51:47 +0300240 pr_err("Couldn't install driver: %d\n", ret);
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200241 goto error_put;
242 }
243
Andy Shevchenko408a5072021-10-25 16:51:45 +0300244 ret = register_rpmsg_driver(&rpmsg_tty_rpmsg_drv);
245 if (ret < 0) {
Andy Shevchenko88af70b2021-10-25 16:51:47 +0300246 pr_err("Couldn't register driver: %d\n", ret);
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200247 goto error_unregister;
248 }
249
250 return 0;
251
252error_unregister:
253 tty_unregister_driver(rpmsg_tty_driver);
254
255error_put:
256 tty_driver_kref_put(rpmsg_tty_driver);
257
Andy Shevchenko408a5072021-10-25 16:51:45 +0300258 return ret;
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200259}
260
261static void __exit rpmsg_tty_exit(void)
262{
263 unregister_rpmsg_driver(&rpmsg_tty_rpmsg_drv);
264 tty_unregister_driver(rpmsg_tty_driver);
265 tty_driver_kref_put(rpmsg_tty_driver);
266 idr_destroy(&tty_idr);
267}
268
269module_init(rpmsg_tty_init);
270module_exit(rpmsg_tty_exit);
271
272MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@foss.st.com>");
273MODULE_DESCRIPTION("remote processor messaging tty driver");
274MODULE_LICENSE("GPL v2");