Thomas Gleixner | 97fb5e8 | 2019-05-29 07:17:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015, Sony Mobile Communications Inc. |
| 4 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/skbuff.h> |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 9 | #include <linux/rpmsg.h> |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 10 | |
| 11 | #include "qrtr.h" |
| 12 | |
| 13 | struct qrtr_smd_dev { |
| 14 | struct qrtr_endpoint ep; |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 15 | struct rpmsg_endpoint *channel; |
Bjorn Andersson | f79a917 | 2016-05-13 14:36:11 -0700 | [diff] [blame] | 16 | struct device *dev; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 17 | }; |
| 18 | |
| 19 | /* from smd to qrtr */ |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 20 | static int qcom_smd_qrtr_callback(struct rpmsg_device *rpdev, |
| 21 | void *data, int len, void *priv, u32 addr) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 22 | { |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 23 | struct qrtr_smd_dev *qdev = dev_get_drvdata(&rpdev->dev); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 24 | int rc; |
| 25 | |
| 26 | if (!qdev) |
| 27 | return -EAGAIN; |
| 28 | |
| 29 | rc = qrtr_endpoint_post(&qdev->ep, data, len); |
| 30 | if (rc == -EINVAL) { |
Bjorn Andersson | f79a917 | 2016-05-13 14:36:11 -0700 | [diff] [blame] | 31 | dev_err(qdev->dev, "invalid ipcrouter packet\n"); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 32 | /* return 0 to let smd drop the packet */ |
| 33 | rc = 0; |
| 34 | } |
| 35 | |
| 36 | return rc; |
| 37 | } |
| 38 | |
| 39 | /* from qrtr to smd */ |
| 40 | static 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 Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 49 | rc = rpmsg_send(qdev->channel, skb->data, skb->len); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 50 | |
| 51 | out: |
| 52 | if (rc) |
| 53 | kfree_skb(skb); |
| 54 | else |
| 55 | consume_skb(skb); |
| 56 | return rc; |
| 57 | } |
| 58 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 59 | static int qcom_smd_qrtr_probe(struct rpmsg_device *rpdev) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 60 | { |
| 61 | struct qrtr_smd_dev *qdev; |
| 62 | int rc; |
| 63 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 64 | qdev = devm_kzalloc(&rpdev->dev, sizeof(*qdev), GFP_KERNEL); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 65 | if (!qdev) |
| 66 | return -ENOMEM; |
| 67 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 68 | qdev->channel = rpdev->ept; |
| 69 | qdev->dev = &rpdev->dev; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 70 | 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 Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 76 | dev_set_drvdata(&rpdev->dev, qdev); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 77 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 78 | dev_dbg(&rpdev->dev, "Qualcomm SMD QRTR driver probed\n"); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 83 | static void qcom_smd_qrtr_remove(struct rpmsg_device *rpdev) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 84 | { |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 85 | struct qrtr_smd_dev *qdev = dev_get_drvdata(&rpdev->dev); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 86 | |
| 87 | qrtr_endpoint_unregister(&qdev->ep); |
| 88 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 89 | dev_set_drvdata(&rpdev->dev, NULL); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 92 | static const struct rpmsg_device_id qcom_smd_qrtr_smd_match[] = { |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 93 | { "IPCRTR" }, |
| 94 | {} |
| 95 | }; |
| 96 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 97 | static struct rpmsg_driver qcom_smd_qrtr_driver = { |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 98 | .probe = qcom_smd_qrtr_probe, |
| 99 | .remove = qcom_smd_qrtr_remove, |
| 100 | .callback = qcom_smd_qrtr_callback, |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 101 | .id_table = qcom_smd_qrtr_smd_match, |
| 102 | .drv = { |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 103 | .name = "qcom_smd_qrtr", |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 104 | }, |
| 105 | }; |
| 106 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 107 | module_rpmsg_driver(qcom_smd_qrtr_driver); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 108 | |
Ramon Fried | c77f5fb | 2018-02-25 09:49:37 +0200 | [diff] [blame] | 109 | MODULE_ALIAS("rpmsg:IPCRTR"); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 110 | MODULE_DESCRIPTION("Qualcomm IPC-Router SMD interface driver"); |
| 111 | MODULE_LICENSE("GPL v2"); |