xref: /linux/samples/rpmsg/rpmsg_client_sample.c (revision 44f57d78)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Remote processor messaging - sample client driver
4  *
5  * Copyright (C) 2011 Texas Instruments, Inc.
6  * Copyright (C) 2011 Google, Inc.
7  *
8  * Ohad Ben-Cohen <ohad@wizery.com>
9  * Brian Swetland <swetland@google.com>
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/rpmsg.h>
15 
16 #define MSG		"hello world!"
17 #define MSG_LIMIT	100
18 
19 struct instance_data {
20 	int rx_count;
21 };
22 
23 static int rpmsg_sample_cb(struct rpmsg_device *rpdev, void *data, int len,
24 						void *priv, u32 src)
25 {
26 	int ret;
27 	struct instance_data *idata = dev_get_drvdata(&rpdev->dev);
28 
29 	dev_info(&rpdev->dev, "incoming msg %d (src: 0x%x)\n",
30 		 ++idata->rx_count, src);
31 
32 	print_hex_dump(KERN_DEBUG, __func__, DUMP_PREFIX_NONE, 16, 1,
33 		       data, len,  true);
34 
35 	/* samples should not live forever */
36 	if (idata->rx_count >= MSG_LIMIT) {
37 		dev_info(&rpdev->dev, "goodbye!\n");
38 		return 0;
39 	}
40 
41 	/* send a new message now */
42 	ret = rpmsg_send(rpdev->ept, MSG, strlen(MSG));
43 	if (ret)
44 		dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
45 
46 	return 0;
47 }
48 
49 static int rpmsg_sample_probe(struct rpmsg_device *rpdev)
50 {
51 	int ret;
52 	struct instance_data *idata;
53 
54 	dev_info(&rpdev->dev, "new channel: 0x%x -> 0x%x!\n",
55 					rpdev->src, rpdev->dst);
56 
57 	idata = devm_kzalloc(&rpdev->dev, sizeof(*idata), GFP_KERNEL);
58 	if (!idata)
59 		return -ENOMEM;
60 
61 	dev_set_drvdata(&rpdev->dev, idata);
62 
63 	/* send a message to our remote processor */
64 	ret = rpmsg_send(rpdev->ept, MSG, strlen(MSG));
65 	if (ret) {
66 		dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
67 		return ret;
68 	}
69 
70 	return 0;
71 }
72 
73 static void rpmsg_sample_remove(struct rpmsg_device *rpdev)
74 {
75 	dev_info(&rpdev->dev, "rpmsg sample client driver is removed\n");
76 }
77 
78 static struct rpmsg_device_id rpmsg_driver_sample_id_table[] = {
79 	{ .name	= "rpmsg-client-sample" },
80 	{ },
81 };
82 MODULE_DEVICE_TABLE(rpmsg, rpmsg_driver_sample_id_table);
83 
84 static struct rpmsg_driver rpmsg_sample_client = {
85 	.drv.name	= KBUILD_MODNAME,
86 	.id_table	= rpmsg_driver_sample_id_table,
87 	.probe		= rpmsg_sample_probe,
88 	.callback	= rpmsg_sample_cb,
89 	.remove		= rpmsg_sample_remove,
90 };
91 module_rpmsg_driver(rpmsg_sample_client);
92 
93 MODULE_DESCRIPTION("Remote processor messaging sample client driver");
94 MODULE_LICENSE("GPL v2");
95