Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, Sony Mobile Communications Inc. |
| 3 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 and |
| 7 | * only version 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/skbuff.h> |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 17 | #include <linux/rpmsg.h> |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "qrtr.h" |
| 20 | |
| 21 | struct qrtr_smd_dev { |
| 22 | struct qrtr_endpoint ep; |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 23 | struct rpmsg_endpoint *channel; |
Bjorn Andersson | f79a917 | 2016-05-13 14:36:11 -0700 | [diff] [blame] | 24 | struct device *dev; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | /* from smd to qrtr */ |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 28 | static int qcom_smd_qrtr_callback(struct rpmsg_device *rpdev, |
| 29 | void *data, int len, void *priv, u32 addr) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 30 | { |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 31 | struct qrtr_smd_dev *qdev = dev_get_drvdata(&rpdev->dev); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 32 | int rc; |
| 33 | |
| 34 | if (!qdev) |
| 35 | return -EAGAIN; |
| 36 | |
| 37 | rc = qrtr_endpoint_post(&qdev->ep, data, len); |
| 38 | if (rc == -EINVAL) { |
Bjorn Andersson | f79a917 | 2016-05-13 14:36:11 -0700 | [diff] [blame] | 39 | dev_err(qdev->dev, "invalid ipcrouter packet\n"); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 40 | /* return 0 to let smd drop the packet */ |
| 41 | rc = 0; |
| 42 | } |
| 43 | |
| 44 | return rc; |
| 45 | } |
| 46 | |
| 47 | /* from qrtr to smd */ |
| 48 | static int qcom_smd_qrtr_send(struct qrtr_endpoint *ep, struct sk_buff *skb) |
| 49 | { |
| 50 | struct qrtr_smd_dev *qdev = container_of(ep, struct qrtr_smd_dev, ep); |
| 51 | int rc; |
| 52 | |
| 53 | rc = skb_linearize(skb); |
| 54 | if (rc) |
| 55 | goto out; |
| 56 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 57 | rc = rpmsg_send(qdev->channel, skb->data, skb->len); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 58 | |
| 59 | out: |
| 60 | if (rc) |
| 61 | kfree_skb(skb); |
| 62 | else |
| 63 | consume_skb(skb); |
| 64 | return rc; |
| 65 | } |
| 66 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 67 | static int qcom_smd_qrtr_probe(struct rpmsg_device *rpdev) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 68 | { |
| 69 | struct qrtr_smd_dev *qdev; |
| 70 | int rc; |
| 71 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 72 | qdev = devm_kzalloc(&rpdev->dev, sizeof(*qdev), GFP_KERNEL); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 73 | if (!qdev) |
| 74 | return -ENOMEM; |
| 75 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 76 | qdev->channel = rpdev->ept; |
| 77 | qdev->dev = &rpdev->dev; |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 78 | qdev->ep.xmit = qcom_smd_qrtr_send; |
| 79 | |
| 80 | rc = qrtr_endpoint_register(&qdev->ep, QRTR_EP_NID_AUTO); |
| 81 | if (rc) |
| 82 | return rc; |
| 83 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 84 | dev_set_drvdata(&rpdev->dev, qdev); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 85 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 86 | dev_dbg(&rpdev->dev, "Qualcomm SMD QRTR driver probed\n"); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 91 | static void qcom_smd_qrtr_remove(struct rpmsg_device *rpdev) |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 92 | { |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 93 | struct qrtr_smd_dev *qdev = dev_get_drvdata(&rpdev->dev); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 94 | |
| 95 | qrtr_endpoint_unregister(&qdev->ep); |
| 96 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 97 | dev_set_drvdata(&rpdev->dev, NULL); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 100 | static const struct rpmsg_device_id qcom_smd_qrtr_smd_match[] = { |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 101 | { "IPCRTR" }, |
| 102 | {} |
| 103 | }; |
| 104 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 105 | static struct rpmsg_driver qcom_smd_qrtr_driver = { |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 106 | .probe = qcom_smd_qrtr_probe, |
| 107 | .remove = qcom_smd_qrtr_remove, |
| 108 | .callback = qcom_smd_qrtr_callback, |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 109 | .id_table = qcom_smd_qrtr_smd_match, |
| 110 | .drv = { |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 111 | .name = "qcom_smd_qrtr", |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 112 | }, |
| 113 | }; |
| 114 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 115 | module_rpmsg_driver(qcom_smd_qrtr_driver); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 116 | |
Ramon Fried | c77f5fb | 2018-02-25 09:49:37 +0200 | [diff] [blame^] | 117 | MODULE_ALIAS("rpmsg:IPCRTR"); |
Courtney Cavin | bdabad3 | 2016-05-06 07:09:08 -0700 | [diff] [blame] | 118 | MODULE_DESCRIPTION("Qualcomm IPC-Router SMD interface driver"); |
| 119 | MODULE_LICENSE("GPL v2"); |