blob: dae2a4e44f386d06f59b7313be74f44361b7c5c9 [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
Andy Shevchenko73a3d4f2021-10-25 16:51:48 +030021#define RPMSG_TTY_NAME "ttyRPMSG"
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +020022#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);
53
54 tty->driver_data = cport;
55
56 return tty_port_install(&cport->port, driver, tty);
57}
58
59static int rpmsg_tty_open(struct tty_struct *tty, struct file *filp)
60{
61 return tty_port_open(tty->port, tty, filp);
62}
63
64static void rpmsg_tty_close(struct tty_struct *tty, struct file *filp)
65{
66 return tty_port_close(tty->port, tty, filp);
67}
68
69static int rpmsg_tty_write(struct tty_struct *tty, const u8 *buf, int len)
70{
71 struct rpmsg_tty_port *cport = tty->driver_data;
72 struct rpmsg_device *rpdev;
73 int msg_max_size, msg_size;
74 int ret;
75
76 rpdev = cport->rpdev;
77
78 msg_max_size = rpmsg_get_mtu(rpdev->ept);
79 if (msg_max_size < 0)
80 return msg_max_size;
81
82 msg_size = min(len, msg_max_size);
83
84 /*
85 * Use rpmsg_trysend instead of rpmsg_send to send the message so the caller is not
86 * hung until a rpmsg buffer is available. In such case rpmsg_trysend returns -ENOMEM.
87 */
88 ret = rpmsg_trysend(rpdev->ept, (void *)buf, msg_size);
89 if (ret) {
90 dev_dbg_ratelimited(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
91 return ret;
92 }
93
94 return msg_size;
95}
96
97static unsigned int rpmsg_tty_write_room(struct tty_struct *tty)
98{
99 struct rpmsg_tty_port *cport = tty->driver_data;
100 int size;
101
102 size = rpmsg_get_mtu(cport->rpdev->ept);
103 if (size < 0)
104 return 0;
105
106 return size;
107}
108
109static const struct tty_operations rpmsg_tty_ops = {
110 .install = rpmsg_tty_install,
111 .open = rpmsg_tty_open,
112 .close = rpmsg_tty_close,
113 .write = rpmsg_tty_write,
114 .write_room = rpmsg_tty_write_room,
115};
116
117static struct rpmsg_tty_port *rpmsg_tty_alloc_cport(void)
118{
119 struct rpmsg_tty_port *cport;
Andy Shevchenko408a5072021-10-25 16:51:45 +0300120 int ret;
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200121
122 cport = kzalloc(sizeof(*cport), GFP_KERNEL);
123 if (!cport)
124 return ERR_PTR(-ENOMEM);
125
126 mutex_lock(&idr_lock);
Andy Shevchenko408a5072021-10-25 16:51:45 +0300127 ret = idr_alloc(&tty_idr, cport, 0, MAX_TTY_RPMSG, GFP_KERNEL);
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200128 mutex_unlock(&idr_lock);
129
Andy Shevchenko408a5072021-10-25 16:51:45 +0300130 if (ret < 0) {
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200131 kfree(cport);
Andy Shevchenko408a5072021-10-25 16:51:45 +0300132 return ERR_PTR(ret);
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200133 }
134
Andy Shevchenko408a5072021-10-25 16:51:45 +0300135 cport->id = ret;
Andy Shevchenko0572da22021-10-25 16:51:44 +0300136
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200137 return cport;
138}
139
140static void rpmsg_tty_release_cport(struct rpmsg_tty_port *cport)
141{
142 mutex_lock(&idr_lock);
143 idr_remove(&tty_idr, cport->id);
144 mutex_unlock(&idr_lock);
145
146 kfree(cport);
147}
148
149static const struct tty_port_operations rpmsg_tty_port_ops = { };
150
151static int rpmsg_tty_probe(struct rpmsg_device *rpdev)
152{
153 struct rpmsg_tty_port *cport;
154 struct device *dev = &rpdev->dev;
155 struct device *tty_dev;
156 int ret;
157
158 cport = rpmsg_tty_alloc_cport();
Andy Shevchenko8673ef72021-10-25 16:51:46 +0300159 if (IS_ERR(cport))
160 return dev_err_probe(dev, PTR_ERR(cport), "Failed to alloc tty port\n");
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200161
162 tty_port_init(&cport->port);
163 cport->port.ops = &rpmsg_tty_port_ops;
164
165 tty_dev = tty_port_register_device(&cport->port, rpmsg_tty_driver,
166 cport->id, dev);
167 if (IS_ERR(tty_dev)) {
Andy Shevchenko8673ef72021-10-25 16:51:46 +0300168 ret = dev_err_probe(dev, PTR_ERR(tty_dev), "Failed to register tty port\n");
169 goto err_destroy;
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200170 }
171
172 cport->rpdev = rpdev;
173
174 dev_set_drvdata(dev, cport);
175
Andy Shevchenko73a3d4f2021-10-25 16:51:48 +0300176 dev_dbg(dev, "New channel: 0x%x -> 0x%x: " RPMSG_TTY_NAME "%d\n",
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200177 rpdev->src, rpdev->dst, cport->id);
178
179 return 0;
180
181err_destroy:
182 tty_port_destroy(&cport->port);
183 rpmsg_tty_release_cport(cport);
184
185 return ret;
186}
187
188static void rpmsg_tty_remove(struct rpmsg_device *rpdev)
189{
190 struct rpmsg_tty_port *cport = dev_get_drvdata(&rpdev->dev);
191
192 dev_dbg(&rpdev->dev, "Removing rpmsg tty device %d\n", cport->id);
193
194 /* User hang up to release the tty */
195 if (tty_port_initialized(&cport->port))
196 tty_port_tty_hangup(&cport->port, false);
197
198 tty_unregister_device(rpmsg_tty_driver, cport->id);
199
200 tty_port_destroy(&cport->port);
201 rpmsg_tty_release_cport(cport);
202}
203
204static struct rpmsg_device_id rpmsg_driver_tty_id_table[] = {
205 { .name = "rpmsg-tty" },
206 { },
207};
208MODULE_DEVICE_TABLE(rpmsg, rpmsg_driver_tty_id_table);
209
210static struct rpmsg_driver rpmsg_tty_rpmsg_drv = {
211 .drv.name = KBUILD_MODNAME,
212 .id_table = rpmsg_driver_tty_id_table,
213 .probe = rpmsg_tty_probe,
214 .callback = rpmsg_tty_cb,
215 .remove = rpmsg_tty_remove,
216};
217
218static int __init rpmsg_tty_init(void)
219{
Andy Shevchenko408a5072021-10-25 16:51:45 +0300220 int ret;
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200221
222 rpmsg_tty_driver = tty_alloc_driver(MAX_TTY_RPMSG, TTY_DRIVER_REAL_RAW |
223 TTY_DRIVER_DYNAMIC_DEV);
224 if (IS_ERR(rpmsg_tty_driver))
225 return PTR_ERR(rpmsg_tty_driver);
226
227 rpmsg_tty_driver->driver_name = "rpmsg_tty";
Andy Shevchenko73a3d4f2021-10-25 16:51:48 +0300228 rpmsg_tty_driver->name = RPMSG_TTY_NAME;
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200229 rpmsg_tty_driver->major = 0;
230 rpmsg_tty_driver->type = TTY_DRIVER_TYPE_CONSOLE;
231
232 /* Disable unused mode by default */
233 rpmsg_tty_driver->init_termios = tty_std_termios;
234 rpmsg_tty_driver->init_termios.c_lflag &= ~(ECHO | ICANON);
235 rpmsg_tty_driver->init_termios.c_oflag &= ~(OPOST | ONLCR);
236
237 tty_set_operations(rpmsg_tty_driver, &rpmsg_tty_ops);
238
Andy Shevchenko408a5072021-10-25 16:51:45 +0300239 ret = tty_register_driver(rpmsg_tty_driver);
240 if (ret < 0) {
Andy Shevchenko88af70b2021-10-25 16:51:47 +0300241 pr_err("Couldn't install driver: %d\n", ret);
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200242 goto error_put;
243 }
244
Andy Shevchenko408a5072021-10-25 16:51:45 +0300245 ret = register_rpmsg_driver(&rpmsg_tty_rpmsg_drv);
246 if (ret < 0) {
Andy Shevchenko88af70b2021-10-25 16:51:47 +0300247 pr_err("Couldn't register driver: %d\n", ret);
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200248 goto error_unregister;
249 }
250
251 return 0;
252
253error_unregister:
254 tty_unregister_driver(rpmsg_tty_driver);
255
256error_put:
257 tty_driver_kref_put(rpmsg_tty_driver);
258
Andy Shevchenko408a5072021-10-25 16:51:45 +0300259 return ret;
Arnaud Pouliquen7c0408d2021-10-15 11:47:01 +0200260}
261
262static void __exit rpmsg_tty_exit(void)
263{
264 unregister_rpmsg_driver(&rpmsg_tty_rpmsg_drv);
265 tty_unregister_driver(rpmsg_tty_driver);
266 tty_driver_kref_put(rpmsg_tty_driver);
267 idr_destroy(&tty_idr);
268}
269
270module_init(rpmsg_tty_init);
271module_exit(rpmsg_tty_exit);
272
273MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@foss.st.com>");
274MODULE_DESCRIPTION("remote processor messaging tty driver");
275MODULE_LICENSE("GPL v2");