xref: /openbmc/linux/net/qrtr/mhi.c (revision 27ab1c1c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/mhi.h>
7 #include <linux/mod_devicetable.h>
8 #include <linux/module.h>
9 #include <linux/skbuff.h>
10 #include <net/sock.h>
11 
12 #include "qrtr.h"
13 
14 struct qrtr_mhi_dev {
15 	struct qrtr_endpoint ep;
16 	struct mhi_device *mhi_dev;
17 	struct device *dev;
18 };
19 
20 /* From MHI to QRTR */
21 static void qcom_mhi_qrtr_dl_callback(struct mhi_device *mhi_dev,
22 				      struct mhi_result *mhi_res)
23 {
24 	struct qrtr_mhi_dev *qdev = dev_get_drvdata(&mhi_dev->dev);
25 	int rc;
26 
27 	if (!qdev || mhi_res->transaction_status)
28 		return;
29 
30 	rc = qrtr_endpoint_post(&qdev->ep, mhi_res->buf_addr,
31 				mhi_res->bytes_xferd);
32 	if (rc == -EINVAL)
33 		dev_err(qdev->dev, "invalid ipcrouter packet\n");
34 }
35 
36 /* From QRTR to MHI */
37 static void qcom_mhi_qrtr_ul_callback(struct mhi_device *mhi_dev,
38 				      struct mhi_result *mhi_res)
39 {
40 	struct sk_buff *skb = mhi_res->buf_addr;
41 
42 	if (skb->sk)
43 		sock_put(skb->sk);
44 	consume_skb(skb);
45 }
46 
47 /* Send data over MHI */
48 static int qcom_mhi_qrtr_send(struct qrtr_endpoint *ep, struct sk_buff *skb)
49 {
50 	struct qrtr_mhi_dev *qdev = container_of(ep, struct qrtr_mhi_dev, ep);
51 	int rc;
52 
53 	rc = skb_linearize(skb);
54 	if (rc)
55 		goto free_skb;
56 
57 	rc = mhi_queue_skb(qdev->mhi_dev, DMA_TO_DEVICE, skb, skb->len,
58 			   MHI_EOT);
59 	if (rc)
60 		goto free_skb;
61 
62 	if (skb->sk)
63 		sock_hold(skb->sk);
64 
65 	return rc;
66 
67 free_skb:
68 	kfree_skb(skb);
69 
70 	return rc;
71 }
72 
73 static int qcom_mhi_qrtr_probe(struct mhi_device *mhi_dev,
74 			       const struct mhi_device_id *id)
75 {
76 	struct qrtr_mhi_dev *qdev;
77 	int rc;
78 
79 	/* start channels */
80 	rc = mhi_prepare_for_transfer(mhi_dev);
81 	if (rc)
82 		return rc;
83 
84 	qdev = devm_kzalloc(&mhi_dev->dev, sizeof(*qdev), GFP_KERNEL);
85 	if (!qdev)
86 		return -ENOMEM;
87 
88 	qdev->mhi_dev = mhi_dev;
89 	qdev->dev = &mhi_dev->dev;
90 	qdev->ep.xmit = qcom_mhi_qrtr_send;
91 
92 	dev_set_drvdata(&mhi_dev->dev, qdev);
93 	rc = qrtr_endpoint_register(&qdev->ep, QRTR_EP_NID_AUTO);
94 	if (rc)
95 		return rc;
96 
97 	dev_dbg(qdev->dev, "Qualcomm MHI QRTR driver probed\n");
98 
99 	return 0;
100 }
101 
102 static void qcom_mhi_qrtr_remove(struct mhi_device *mhi_dev)
103 {
104 	struct qrtr_mhi_dev *qdev = dev_get_drvdata(&mhi_dev->dev);
105 
106 	qrtr_endpoint_unregister(&qdev->ep);
107 	mhi_unprepare_from_transfer(mhi_dev);
108 	dev_set_drvdata(&mhi_dev->dev, NULL);
109 }
110 
111 static const struct mhi_device_id qcom_mhi_qrtr_id_table[] = {
112 	{ .chan = "IPCR" },
113 	{}
114 };
115 MODULE_DEVICE_TABLE(mhi, qcom_mhi_qrtr_id_table);
116 
117 static struct mhi_driver qcom_mhi_qrtr_driver = {
118 	.probe = qcom_mhi_qrtr_probe,
119 	.remove = qcom_mhi_qrtr_remove,
120 	.dl_xfer_cb = qcom_mhi_qrtr_dl_callback,
121 	.ul_xfer_cb = qcom_mhi_qrtr_ul_callback,
122 	.id_table = qcom_mhi_qrtr_id_table,
123 	.driver = {
124 		.name = "qcom_mhi_qrtr",
125 	},
126 };
127 
128 module_mhi_driver(qcom_mhi_qrtr_driver);
129 
130 MODULE_AUTHOR("Chris Lew <clew@codeaurora.org>");
131 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
132 MODULE_DESCRIPTION("Qualcomm IPC-Router MHI interface driver");
133 MODULE_LICENSE("GPL v2");
134