1
0
Fork 0
remarkable-linux/drivers/rpmsg/imx_rpmsg_tty.c

253 lines
6.3 KiB
C
Raw Permalink Normal View History

/*
* Copyright (C) 2015-2016 Freescale Semiconductor, Inc.
* Copyright (C) 2017 NXP
*
* derived from the omap-rpmsg implementation.
* Remote processor messaging transport - tty driver
*
* The code contained herein is licensed under the GNU General Public
* License. You may obtain a copy of the GNU General Public License
* Version 2 or later at the following locations:
*
* http://www.opensource.org/licenses/gpl-license.html
* http://www.gnu.org/copyleft/gpl.html
*/
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/rpmsg.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/virtio.h>
/* this needs to be less then (RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) */
#define RPMSG_MAX_SIZE 256
#define MSG "hello world!"
/*
* struct rpmsgtty_port - Wrapper struct for imx rpmsg tty port.
* @port: TTY port data
*/
struct rpmsgtty_port {
struct tty_port port;
spinlock_t rx_lock;
struct rpmsg_device *rpdev;
struct tty_driver *rpmsgtty_driver;
};
static int rpmsg_tty_cb(struct rpmsg_device *rpdev, void *data, int len,
void *priv, u32 src)
{
int space;
unsigned char *cbuf;
struct rpmsgtty_port *cport = dev_get_drvdata(&rpdev->dev);
/* flush the recv-ed none-zero data to tty node */
if (len == 0)
return 0;
dev_dbg(&rpdev->dev, "msg(<- src 0x%x) len %d\n", src, len);
print_hex_dump(KERN_DEBUG, __func__, DUMP_PREFIX_NONE, 16, 1,
data, len, true);
spin_lock_bh(&cport->rx_lock);
space = tty_prepare_flip_string(&cport->port, &cbuf, len);
if (space <= 0) {
dev_err(&rpdev->dev, "No memory for tty_prepare_flip_string\n");
spin_unlock_bh(&cport->rx_lock);
return -ENOMEM;
}
memcpy(cbuf, data, len);
tty_flip_buffer_push(&cport->port);
spin_unlock_bh(&cport->rx_lock);
return 0;
}
static struct tty_port_operations rpmsgtty_port_ops = { };
static int rpmsgtty_install(struct tty_driver *driver, struct tty_struct *tty)
{
struct rpmsgtty_port *cport = driver->driver_state;
return tty_port_install(&cport->port, driver, tty);
}
static int rpmsgtty_open(struct tty_struct *tty, struct file *filp)
{
return tty_port_open(tty->port, tty, filp);
}
static void rpmsgtty_close(struct tty_struct *tty, struct file *filp)
{
return tty_port_close(tty->port, tty, filp);
}
static int rpmsgtty_write(struct tty_struct *tty, const unsigned char *buf,
int total)
{
int count, ret = 0;
const unsigned char *tbuf;
struct rpmsgtty_port *rptty_port = container_of(tty->port,
struct rpmsgtty_port, port);
struct rpmsg_device *rpdev = rptty_port->rpdev;
if (NULL == buf) {
pr_err("buf shouldn't be null.\n");
return -ENOMEM;
}
count = total;
tbuf = buf;
do {
/* send a message to our remote processor */
ret = rpmsg_send(rpdev->ept, (void *)tbuf,
count > RPMSG_MAX_SIZE ? RPMSG_MAX_SIZE : count);
if (ret) {
dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
return ret;
}
if (count > RPMSG_MAX_SIZE) {
count -= RPMSG_MAX_SIZE;
tbuf += RPMSG_MAX_SIZE;
} else {
count = 0;
}
} while (count > 0);
return total;
}
static int rpmsgtty_write_room(struct tty_struct *tty)
{
/* report the space in the rpmsg buffer */
return RPMSG_MAX_SIZE;
}
static const struct tty_operations imxrpmsgtty_ops = {
.install = rpmsgtty_install,
.open = rpmsgtty_open,
.close = rpmsgtty_close,
.write = rpmsgtty_write,
.write_room = rpmsgtty_write_room,
};
static int rpmsg_tty_probe(struct rpmsg_device *rpdev)
{
int ret;
struct rpmsgtty_port *cport;
struct tty_driver *rpmsgtty_driver;
dev_info(&rpdev->dev, "new channel: 0x%x -> 0x%x!\n",
rpdev->src, rpdev->dst);
cport = devm_kzalloc(&rpdev->dev, sizeof(*cport), GFP_KERNEL);
if (!cport)
return -ENOMEM;
rpmsgtty_driver = tty_alloc_driver(1, TTY_DRIVER_UNNUMBERED_NODE);
if (IS_ERR(rpmsgtty_driver)) {
kfree(cport);
return PTR_ERR(rpmsgtty_driver);
}
rpmsgtty_driver->driver_name = "rpmsg_tty";
MLK-19864 rpmsg: imx: fix the kernel panic during tty rpmsg demo Fix the kernel panic during tty rpmsg str echo demo. Root cause: The driver name may be flushed. Use the kasprintf to setup the rpmsg tty driver name to fix this issue. [ 35.494361] Unable to handle kernel paging request at virtual address ffff000021b4ba40 [ 35.502321] Mem abort info: [ 35.505117] Exception class = DABT (current EL), IL = 32 bits [ 35.511099] SET = 0, FnV = 0 [ 35.514188] EA = 0, S1PTW = 0 [ 35.517328] Data abort info: [ 35.520255] ISV = 0, ISS = 0x00000007 [ 35.524130] CM = 0, WnR = 0 [ 35.527140] swapper pgtable: 4k pages, 48-bit VAs, pgd = ffff0000096dd000 [ 35.533964] [ffff000021b4ba40] *pgd=000000097fffe803, *pud=000000097fffd803, *pmd=0000000973d20003, *pte=0000000000000000 [ 35.544976] Internal error: Oops: 96000007 [#1] PREEMPT SMP [ 35.550553] Modules linked in: imx_rpmsg_tty [ 35.554843] CPU: 2 PID: 3911 Comm: sh Not tainted 4.14.62-05098-gcff652e-dirty #41 [ 35.562419] Hardware name: Freescale i.MX8QM MEK (DT) [ 35.567479] task: ffff8008f3950000 task.stack: ffff000021b10000 [ 35.573413] PC is at string+0x28/0x98 [ 35.577080] LR is at vsnprintf+0x3c0/0x688 [ 35.581177] pc : [<ffff000008d5f1a0>] lr : [<ffff000008d61c70>] pstate: a0000145 [ 35.588577] sp : ffff000021b13970 [ 35.591894] x29: ffff000021b13970 x28: ffff80097365f56f [ 35.597219] x27: ffff0000092cf03a x26: ffff0000092cf03a [ 35.602544] x25: ffff000021b13a80 x24: ffff8008f365f570 [ 35.607869] x23: 00000000ffffffd8 x22: ffff000008fc08dc [ 35.613195] x21: 000000007fffffff x20: ffff000008fc08cc [ 35.618512] x19: ffff8008f365f570 x18: 0000000000000000 [ 35.623827] x17: 0000ffffbec93588 x16: ffff00000820b2c0 [ 35.629144] x15: 0000000000000000 x14: ffffffffffffffff [ 35.634461] x13: 0000000000000018 x12: 0101010101010101 [ 35.639777] x11: 0000000000000000 x10: ffff000021b13a80 [ 35.645085] x9 : ffff000021b13a80 x8 : ffff000021b13a80 [ 35.650402] x7 : ffff8008f365f570 x6 : fffffffffffffffe [ 35.655718] x5 : 00000000ffffffd0 x4 : ffff000021b4ba40 [ 35.661035] x3 : ffff0a00ffffff04 x2 : ffff80097365f56f [ 35.666354] x1 : ffff80097365f56f x0 : ffffffffffffffff [ 35.671672] Process sh (pid: 3911, stack limit = 0xffff000021b10000) [ 35.678022] Call trace: [ 35.680465] Exception stack(0xffff000021b13830 to 0xffff000021b13970) [ 35.686911] 3820: ffffffffffffffff ffff80097365f56f [ 35.694748] 3840: ffff80097365f56f ffff0a00ffffff04 ffff000021b4ba40 00000000ffffffd0 [ 35.702581] 3860: fffffffffffffffe ffff8008f365f570 ffff000021b13a80 ffff000021b13a80 [ 35.710409] 3880: ffff000021b13a80 0000000000000000 0101010101010101 0000000000000018 [ 35.718245] 38a0: ffffffffffffffff 0000000000000000 ffff00000820b2c0 0000ffffbec93588 [ 35.726081] 38c0: 0000000000000000 ffff8008f365f570 ffff000008fc08cc 000000007fffffff [ 35.733908] 38e0: ffff000008fc08dc 00000000ffffffd8 ffff8008f365f570 ffff000021b13a80 [ 35.741744] 3900: ffff0000092cf03a ffff0000092cf03a ffff80097365f56f ffff000021b13970 [ 35.749583] 3920: ffff000008d61c70 ffff000021b13970 ffff000008d5f1a0 00000000a0000145 [ 35.757417] 3940: ffff8008fff5fb00 0000000000001c00 0000ffffffffffff ffff000008d61930 [ 35.765254] 3960: ffff000021b13970 ffff000008d5f1a0 [ 35.770129] [<ffff000008d5f1a0>] string+0x28/0x98 [ 35.774837] [<ffff000008d61c70>] vsnprintf+0x3c0/0x688 [ 35.779980] [<ffff000008d620e4>] sprintf+0x4c/0x58 Signed-off-by: Richard Zhu <hongxing.zhu@nxp.com> Reviewed-by: Fugang Duan <fugang.duan@nxp.com>
2018-10-10 00:41:03 -06:00
rpmsgtty_driver->name = kasprintf(GFP_KERNEL, "ttyRPMSG%d", rpdev->dst);
rpmsgtty_driver->major = UNNAMED_MAJOR;
rpmsgtty_driver->minor_start = 0;
rpmsgtty_driver->type = TTY_DRIVER_TYPE_CONSOLE;
rpmsgtty_driver->init_termios = tty_std_termios;
tty_set_operations(rpmsgtty_driver, &imxrpmsgtty_ops);
tty_port_init(&cport->port);
cport->port.ops = &rpmsgtty_port_ops;
spin_lock_init(&cport->rx_lock);
cport->port.low_latency = cport->port.flags | ASYNC_LOW_LATENCY;
cport->rpdev = rpdev;
dev_set_drvdata(&rpdev->dev, cport);
rpmsgtty_driver->driver_state = cport;
cport->rpmsgtty_driver = rpmsgtty_driver;
ret = tty_register_driver(cport->rpmsgtty_driver);
if (ret < 0) {
pr_err("Couldn't install rpmsg tty driver: ret %d\n", ret);
goto error1;
} else {
pr_info("Install rpmsg tty driver!\n");
}
/*
* send a message to our remote processor, and tell remote
* processor about this channel
*/
ret = rpmsg_send(rpdev->ept, MSG, strlen(MSG));
if (ret) {
dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
goto error;
}
return 0;
error:
tty_unregister_driver(cport->rpmsgtty_driver);
error1:
put_tty_driver(cport->rpmsgtty_driver);
tty_port_destroy(&cport->port);
cport->rpmsgtty_driver = NULL;
kfree(cport);
return ret;
}
static void rpmsg_tty_remove(struct rpmsg_device *rpdev)
{
struct rpmsgtty_port *cport = dev_get_drvdata(&rpdev->dev);
dev_info(&rpdev->dev, "rpmsg tty driver is removed\n");
tty_unregister_driver(cport->rpmsgtty_driver);
MLK-19864 rpmsg: imx: fix the kernel panic during tty rpmsg demo Fix the kernel panic during tty rpmsg str echo demo. Root cause: The driver name may be flushed. Use the kasprintf to setup the rpmsg tty driver name to fix this issue. [ 35.494361] Unable to handle kernel paging request at virtual address ffff000021b4ba40 [ 35.502321] Mem abort info: [ 35.505117] Exception class = DABT (current EL), IL = 32 bits [ 35.511099] SET = 0, FnV = 0 [ 35.514188] EA = 0, S1PTW = 0 [ 35.517328] Data abort info: [ 35.520255] ISV = 0, ISS = 0x00000007 [ 35.524130] CM = 0, WnR = 0 [ 35.527140] swapper pgtable: 4k pages, 48-bit VAs, pgd = ffff0000096dd000 [ 35.533964] [ffff000021b4ba40] *pgd=000000097fffe803, *pud=000000097fffd803, *pmd=0000000973d20003, *pte=0000000000000000 [ 35.544976] Internal error: Oops: 96000007 [#1] PREEMPT SMP [ 35.550553] Modules linked in: imx_rpmsg_tty [ 35.554843] CPU: 2 PID: 3911 Comm: sh Not tainted 4.14.62-05098-gcff652e-dirty #41 [ 35.562419] Hardware name: Freescale i.MX8QM MEK (DT) [ 35.567479] task: ffff8008f3950000 task.stack: ffff000021b10000 [ 35.573413] PC is at string+0x28/0x98 [ 35.577080] LR is at vsnprintf+0x3c0/0x688 [ 35.581177] pc : [<ffff000008d5f1a0>] lr : [<ffff000008d61c70>] pstate: a0000145 [ 35.588577] sp : ffff000021b13970 [ 35.591894] x29: ffff000021b13970 x28: ffff80097365f56f [ 35.597219] x27: ffff0000092cf03a x26: ffff0000092cf03a [ 35.602544] x25: ffff000021b13a80 x24: ffff8008f365f570 [ 35.607869] x23: 00000000ffffffd8 x22: ffff000008fc08dc [ 35.613195] x21: 000000007fffffff x20: ffff000008fc08cc [ 35.618512] x19: ffff8008f365f570 x18: 0000000000000000 [ 35.623827] x17: 0000ffffbec93588 x16: ffff00000820b2c0 [ 35.629144] x15: 0000000000000000 x14: ffffffffffffffff [ 35.634461] x13: 0000000000000018 x12: 0101010101010101 [ 35.639777] x11: 0000000000000000 x10: ffff000021b13a80 [ 35.645085] x9 : ffff000021b13a80 x8 : ffff000021b13a80 [ 35.650402] x7 : ffff8008f365f570 x6 : fffffffffffffffe [ 35.655718] x5 : 00000000ffffffd0 x4 : ffff000021b4ba40 [ 35.661035] x3 : ffff0a00ffffff04 x2 : ffff80097365f56f [ 35.666354] x1 : ffff80097365f56f x0 : ffffffffffffffff [ 35.671672] Process sh (pid: 3911, stack limit = 0xffff000021b10000) [ 35.678022] Call trace: [ 35.680465] Exception stack(0xffff000021b13830 to 0xffff000021b13970) [ 35.686911] 3820: ffffffffffffffff ffff80097365f56f [ 35.694748] 3840: ffff80097365f56f ffff0a00ffffff04 ffff000021b4ba40 00000000ffffffd0 [ 35.702581] 3860: fffffffffffffffe ffff8008f365f570 ffff000021b13a80 ffff000021b13a80 [ 35.710409] 3880: ffff000021b13a80 0000000000000000 0101010101010101 0000000000000018 [ 35.718245] 38a0: ffffffffffffffff 0000000000000000 ffff00000820b2c0 0000ffffbec93588 [ 35.726081] 38c0: 0000000000000000 ffff8008f365f570 ffff000008fc08cc 000000007fffffff [ 35.733908] 38e0: ffff000008fc08dc 00000000ffffffd8 ffff8008f365f570 ffff000021b13a80 [ 35.741744] 3900: ffff0000092cf03a ffff0000092cf03a ffff80097365f56f ffff000021b13970 [ 35.749583] 3920: ffff000008d61c70 ffff000021b13970 ffff000008d5f1a0 00000000a0000145 [ 35.757417] 3940: ffff8008fff5fb00 0000000000001c00 0000ffffffffffff ffff000008d61930 [ 35.765254] 3960: ffff000021b13970 ffff000008d5f1a0 [ 35.770129] [<ffff000008d5f1a0>] string+0x28/0x98 [ 35.774837] [<ffff000008d61c70>] vsnprintf+0x3c0/0x688 [ 35.779980] [<ffff000008d620e4>] sprintf+0x4c/0x58 Signed-off-by: Richard Zhu <hongxing.zhu@nxp.com> Reviewed-by: Fugang Duan <fugang.duan@nxp.com>
2018-10-10 00:41:03 -06:00
kfree(cport->rpmsgtty_driver->name);
put_tty_driver(cport->rpmsgtty_driver);
tty_port_destroy(&cport->port);
cport->rpmsgtty_driver = NULL;
}
static struct rpmsg_device_id rpmsg_driver_tty_id_table[] = {
{ .name = "rpmsg-virtual-tty-channel-1" },
{ .name = "rpmsg-virtual-tty-channel" },
{ .name = "rpmsg-openamp-demo-channel" },
{ },
};
MODULE_DEVICE_TABLE(rpmsg, rpmsg_driver_tty_id_table);
static struct rpmsg_driver rpmsg_tty_driver = {
.drv.name = KBUILD_MODNAME,
.drv.owner = THIS_MODULE,
.id_table = rpmsg_driver_tty_id_table,
.probe = rpmsg_tty_probe,
.callback = rpmsg_tty_cb,
.remove = rpmsg_tty_remove,
};
static int __init init(void)
{
return register_rpmsg_driver(&rpmsg_tty_driver);
}
static void __exit fini(void)
{
unregister_rpmsg_driver(&rpmsg_tty_driver);
}
module_init(init);
module_exit(fini);
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("iMX virtio remote processor messaging tty driver");
MODULE_LICENSE("GPL v2");