hc
2025-02-14 bbb9540dc49f70f6b703d1c8d1b85fa5f602d86e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// SPDX-License-Identifier: GPL-2.0
/*
 * Rockchip Remote Processors Messaging Test.
 *
 * Copyright (c) 2022 Rockchip Electronics Co. Ltd.
 * Author: Hongming Zou <hongming.zou@rock-chips.com>
 */
 
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/rpmsg.h>
#include <linux/rpmsg/rockchip_rpmsg.h>
#include <linux/time.h>
#include <linux/virtio.h>
 
#define LINUX_TEST_MSG_1 "Announce master ept id!"
#define LINUX_TEST_MSG_2 "Rockchip rpmsg linux test pingpong!"
#define MSG_LIMIT       10000
 
/* different processor cores may need to adjust the value of this definition */
#define LINUX_RPMSG_COMPENSATION    (1)    //ms
 
struct rpmsg_info_t {
   int rx_count;
};
 
static int rockchip_rpmsg_test_cb(struct rpmsg_device *rp, void *payload,
                 int payload_len, void *priv, u32 src)
{
   int ret;
   uint32_t remote_ept_id;
   struct rpmsg_info_t *info = dev_get_drvdata(&rp->dev);
 
   remote_ept_id = src;
   dev_info(&rp->dev, "rx msg %s rx_count %d(remote_ept_id: 0x%x)\n",
           (char *)payload, ++info->rx_count, remote_ept_id);
 
   /* test should not live forever */
   if (info->rx_count >= MSG_LIMIT) {
       dev_info(&rp->dev, "Rockchip rpmsg test exit!\n");
       return 0;
   }
 
   mdelay(LINUX_RPMSG_COMPENSATION);
   /* send a new message now */
   ret = rpmsg_sendto(rp->ept, LINUX_TEST_MSG_2, strlen(LINUX_TEST_MSG_2), remote_ept_id);
   if (ret)
       dev_err(&rp->dev, "rpmsg_send failed: %d\n", ret);
       return ret;
}
 
static int rockchip_rpmsg_test_probe(struct rpmsg_device *rp)
{
   int ret;
   uint32_t master_ept_id, remote_ept_id;
   struct rpmsg_info_t *info;
 
   master_ept_id = rp->src;
   remote_ept_id = rp->dst;
   dev_info(&rp->dev, "new channel: 0x%x -> 0x%x!\n", master_ept_id, remote_ept_id);
 
   info = devm_kzalloc(&rp->dev, sizeof(*info), GFP_KERNEL);
   if (!info)
       return -ENOMEM;
 
   dev_set_drvdata(&rp->dev, info);
 
   /*
    * send a message to our remote processor, and tell remote
    * processor about this channel
    */
   ret = rpmsg_send(rp->ept, LINUX_TEST_MSG_1, strlen(LINUX_TEST_MSG_1));
   if (ret) {
       dev_err(&rp->dev, "rpmsg_send failed: %d\n", ret);
       return ret;
   }
   mdelay(LINUX_RPMSG_COMPENSATION);
   ret = rpmsg_sendto(rp->ept, LINUX_TEST_MSG_2, strlen(LINUX_TEST_MSG_2), remote_ept_id);
   if (ret) {
       dev_err(&rp->dev, "rpmsg_send failed: %d\n", ret);
       return ret;
   }
 
   return 0;
}
 
static void rockchip_rpmsg_test_remove(struct rpmsg_device *rp)
{
   dev_info(&rp->dev, "rockchip rpmsg test is removed\n");
}
 
static struct rpmsg_device_id rockchip_rpmsg_test_id_table[] = {
   { .name = "rpmsg-ap3-ch0" },
   { .name = "rpmsg-mcu0-test" },
   { /* sentinel */ },
};
 
static struct rpmsg_driver rockchip_rpmsg_test = {
   .drv.name    = KBUILD_MODNAME,
   .drv.owner    = THIS_MODULE,
   .id_table    = rockchip_rpmsg_test_id_table,
   .probe        = rockchip_rpmsg_test_probe,
   .callback    = rockchip_rpmsg_test_cb,
   .remove        = rockchip_rpmsg_test_remove,
};
 
static int __init init(void)
{
   return register_rpmsg_driver(&rockchip_rpmsg_test);
}
 
static void __exit fini(void)
{
   unregister_rpmsg_driver(&rockchip_rpmsg_test);
}
module_init(init);
module_exit(fini);
 
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Rockchip Remote Processors Messaging Test");
MODULE_AUTHOR("Hongming Zou <hongming.zou@rock-chips.com>");