blob: c91bf030fbc7d973a90a1f88e90e66df42ab2bdd [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Courtney Cavinbdabad32016-05-06 07:09:08 -07002/*
3 * Copyright (c) 2015, Sony Mobile Communications Inc.
4 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
Courtney Cavinbdabad32016-05-06 07:09:08 -07005 */
6
7#include <linux/module.h>
8#include <linux/skbuff.h>
Bjorn Andersson5052de82017-03-27 22:26:33 -07009#include <linux/rpmsg.h>
Courtney Cavinbdabad32016-05-06 07:09:08 -070010
11#include "qrtr.h"
12
13struct qrtr_smd_dev {
14 struct qrtr_endpoint ep;
Bjorn Andersson5052de82017-03-27 22:26:33 -070015 struct rpmsg_endpoint *channel;
Bjorn Anderssonf79a9172016-05-13 14:36:11 -070016 struct device *dev;
Courtney Cavinbdabad32016-05-06 07:09:08 -070017};
18
19/* from smd to qrtr */
Bjorn Andersson5052de82017-03-27 22:26:33 -070020static int qcom_smd_qrtr_callback(struct rpmsg_device *rpdev,
21 void *data, int len, void *priv, u32 addr)
Courtney Cavinbdabad32016-05-06 07:09:08 -070022{
Bjorn Andersson5052de82017-03-27 22:26:33 -070023 struct qrtr_smd_dev *qdev = dev_get_drvdata(&rpdev->dev);
Courtney Cavinbdabad32016-05-06 07:09:08 -070024 int rc;
25
26 if (!qdev)
27 return -EAGAIN;
28
29 rc = qrtr_endpoint_post(&qdev->ep, data, len);
30 if (rc == -EINVAL) {
Bjorn Anderssonf79a9172016-05-13 14:36:11 -070031 dev_err(qdev->dev, "invalid ipcrouter packet\n");
Courtney Cavinbdabad32016-05-06 07:09:08 -070032 /* return 0 to let smd drop the packet */
33 rc = 0;
34 }
35
36 return rc;
37}
38
39/* from qrtr to smd */
40static int qcom_smd_qrtr_send(struct qrtr_endpoint *ep, struct sk_buff *skb)
41{
42 struct qrtr_smd_dev *qdev = container_of(ep, struct qrtr_smd_dev, ep);
43 int rc;
44
45 rc = skb_linearize(skb);
46 if (rc)
47 goto out;
48
Bjorn Andersson5052de82017-03-27 22:26:33 -070049 rc = rpmsg_send(qdev->channel, skb->data, skb->len);
Courtney Cavinbdabad32016-05-06 07:09:08 -070050
51out:
52 if (rc)
53 kfree_skb(skb);
54 else
55 consume_skb(skb);
56 return rc;
57}
58
Bjorn Andersson5052de82017-03-27 22:26:33 -070059static int qcom_smd_qrtr_probe(struct rpmsg_device *rpdev)
Courtney Cavinbdabad32016-05-06 07:09:08 -070060{
61 struct qrtr_smd_dev *qdev;
62 int rc;
63
Bjorn Andersson5052de82017-03-27 22:26:33 -070064 qdev = devm_kzalloc(&rpdev->dev, sizeof(*qdev), GFP_KERNEL);
Courtney Cavinbdabad32016-05-06 07:09:08 -070065 if (!qdev)
66 return -ENOMEM;
67
Bjorn Andersson5052de82017-03-27 22:26:33 -070068 qdev->channel = rpdev->ept;
69 qdev->dev = &rpdev->dev;
Courtney Cavinbdabad32016-05-06 07:09:08 -070070 qdev->ep.xmit = qcom_smd_qrtr_send;
71
72 rc = qrtr_endpoint_register(&qdev->ep, QRTR_EP_NID_AUTO);
73 if (rc)
74 return rc;
75
Bjorn Andersson5052de82017-03-27 22:26:33 -070076 dev_set_drvdata(&rpdev->dev, qdev);
Courtney Cavinbdabad32016-05-06 07:09:08 -070077
Bjorn Andersson5052de82017-03-27 22:26:33 -070078 dev_dbg(&rpdev->dev, "Qualcomm SMD QRTR driver probed\n");
Courtney Cavinbdabad32016-05-06 07:09:08 -070079
80 return 0;
81}
82
Bjorn Andersson5052de82017-03-27 22:26:33 -070083static void qcom_smd_qrtr_remove(struct rpmsg_device *rpdev)
Courtney Cavinbdabad32016-05-06 07:09:08 -070084{
Bjorn Andersson5052de82017-03-27 22:26:33 -070085 struct qrtr_smd_dev *qdev = dev_get_drvdata(&rpdev->dev);
Courtney Cavinbdabad32016-05-06 07:09:08 -070086
87 qrtr_endpoint_unregister(&qdev->ep);
88
Bjorn Andersson5052de82017-03-27 22:26:33 -070089 dev_set_drvdata(&rpdev->dev, NULL);
Courtney Cavinbdabad32016-05-06 07:09:08 -070090}
91
Bjorn Andersson5052de82017-03-27 22:26:33 -070092static const struct rpmsg_device_id qcom_smd_qrtr_smd_match[] = {
Courtney Cavinbdabad32016-05-06 07:09:08 -070093 { "IPCRTR" },
94 {}
95};
96
Bjorn Andersson5052de82017-03-27 22:26:33 -070097static struct rpmsg_driver qcom_smd_qrtr_driver = {
Courtney Cavinbdabad32016-05-06 07:09:08 -070098 .probe = qcom_smd_qrtr_probe,
99 .remove = qcom_smd_qrtr_remove,
100 .callback = qcom_smd_qrtr_callback,
Bjorn Andersson5052de82017-03-27 22:26:33 -0700101 .id_table = qcom_smd_qrtr_smd_match,
102 .drv = {
Courtney Cavinbdabad32016-05-06 07:09:08 -0700103 .name = "qcom_smd_qrtr",
Courtney Cavinbdabad32016-05-06 07:09:08 -0700104 },
105};
106
Bjorn Andersson5052de82017-03-27 22:26:33 -0700107module_rpmsg_driver(qcom_smd_qrtr_driver);
Courtney Cavinbdabad32016-05-06 07:09:08 -0700108
Ramon Friedc77f5fb2018-02-25 09:49:37 +0200109MODULE_ALIAS("rpmsg:IPCRTR");
Courtney Cavinbdabad32016-05-06 07:09:08 -0700110MODULE_DESCRIPTION("Qualcomm IPC-Router SMD interface driver");
111MODULE_LICENSE("GPL v2");