Maulik Shah | a2a8b39 | 2018-01-16 17:22:00 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved. |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/mailbox_client.h> |
| 18 | #include <linux/seq_file.h> |
| 19 | #include <linux/debugfs.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/mailbox/qmp.h> |
| 22 | #include <linux/uaccess.h> |
Maulik Shah | a2a8b39 | 2018-01-16 17:22:00 +0530 | [diff] [blame] | 23 | #include <linux/mailbox_controller.h> |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 24 | |
| 25 | #define MAX_MSG_SIZE 96 /* Imposed by the remote*/ |
| 26 | |
Maulik Shah | a2a8b39 | 2018-01-16 17:22:00 +0530 | [diff] [blame] | 27 | struct qmp_debugfs_data { |
| 28 | struct qmp_pkt pkt; |
| 29 | char buf[MAX_MSG_SIZE + 1]; |
| 30 | }; |
| 31 | |
| 32 | static struct qmp_debugfs_data data_pkt[MBOX_TX_QUEUE_LEN]; |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 33 | static struct mbox_chan *chan; |
| 34 | static struct mbox_client *cl; |
| 35 | |
Maulik Shah | a2a8b39 | 2018-01-16 17:22:00 +0530 | [diff] [blame] | 36 | static DEFINE_MUTEX(qmp_debugfs_mutex); |
| 37 | |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 38 | static ssize_t aop_msg_write(struct file *file, const char __user *userstr, |
| 39 | size_t len, loff_t *pos) |
| 40 | { |
Maulik Shah | a2a8b39 | 2018-01-16 17:22:00 +0530 | [diff] [blame] | 41 | static int count; |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 42 | int rc; |
| 43 | |
| 44 | if (!len || (len > MAX_MSG_SIZE)) |
| 45 | return len; |
| 46 | |
Maulik Shah | a2a8b39 | 2018-01-16 17:22:00 +0530 | [diff] [blame] | 47 | mutex_lock(&qmp_debugfs_mutex); |
| 48 | |
| 49 | if (count >= MBOX_TX_QUEUE_LEN) |
| 50 | count = 0; |
| 51 | |
| 52 | memset(&(data_pkt[count]), 0, sizeof(data_pkt[count])); |
| 53 | rc = copy_from_user(data_pkt[count].buf, userstr, len); |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 54 | if (rc) { |
| 55 | pr_err("%s copy from user failed, rc=%d\n", __func__, rc); |
Maulik Shah | a2a8b39 | 2018-01-16 17:22:00 +0530 | [diff] [blame] | 56 | mutex_unlock(&qmp_debugfs_mutex); |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 57 | return len; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Controller expects a 4 byte aligned buffer |
| 62 | */ |
Maulik Shah | a2a8b39 | 2018-01-16 17:22:00 +0530 | [diff] [blame] | 63 | data_pkt[count].pkt.size = (len + 0x3) & ~0x3; |
| 64 | data_pkt[count].pkt.data = data_pkt[count].buf; |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 65 | |
Maulik Shah | a2a8b39 | 2018-01-16 17:22:00 +0530 | [diff] [blame] | 66 | if (mbox_send_message(chan, &(data_pkt[count].pkt)) < 0) |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 67 | pr_err("Failed to send qmp request\n"); |
Maulik Shah | a2a8b39 | 2018-01-16 17:22:00 +0530 | [diff] [blame] | 68 | else |
| 69 | count++; |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 70 | |
Maulik Shah | a2a8b39 | 2018-01-16 17:22:00 +0530 | [diff] [blame] | 71 | mutex_unlock(&qmp_debugfs_mutex); |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 72 | return len; |
| 73 | } |
| 74 | |
| 75 | static const struct file_operations aop_msg_fops = { |
| 76 | .write = aop_msg_write, |
| 77 | }; |
| 78 | |
| 79 | static int qmp_msg_probe(struct platform_device *pdev) |
| 80 | { |
| 81 | struct dentry *file; |
| 82 | |
| 83 | cl = devm_kzalloc(&pdev->dev, sizeof(*cl), GFP_KERNEL); |
| 84 | if (!cl) |
| 85 | return -ENOMEM; |
| 86 | |
| 87 | cl->dev = &pdev->dev; |
| 88 | cl->tx_block = true; |
Maulik Shah | a2a8b39 | 2018-01-16 17:22:00 +0530 | [diff] [blame] | 89 | cl->tx_tout = 1000; |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 90 | cl->knows_txdone = false; |
| 91 | |
| 92 | chan = mbox_request_channel(cl, 0); |
| 93 | if (IS_ERR(chan)) { |
| 94 | dev_err(&pdev->dev, "Failed to mbox channel\n"); |
| 95 | return PTR_ERR(chan); |
| 96 | } |
| 97 | |
| 98 | file = debugfs_create_file("aop_send_message", 0220, NULL, NULL, |
| 99 | &aop_msg_fops); |
| 100 | if (!file) |
| 101 | goto err; |
| 102 | return 0; |
| 103 | err: |
| 104 | mbox_free_channel(chan); |
| 105 | chan = NULL; |
| 106 | return -ENOMEM; |
| 107 | } |
| 108 | |
| 109 | static const struct of_device_id aop_qmp_match_tbl[] = { |
| 110 | {.compatible = "qcom,debugfs-qmp-client"}, |
| 111 | {}, |
| 112 | }; |
| 113 | |
| 114 | static struct platform_driver aop_qmp_msg_driver = { |
| 115 | .probe = qmp_msg_probe, |
| 116 | .driver = { |
| 117 | .name = "debugfs-qmp-client", |
| 118 | .owner = THIS_MODULE, |
Maulik Shah | 4d5de1e | 2018-07-11 12:03:17 +0530 | [diff] [blame^] | 119 | .suppress_bind_attrs = true, |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 120 | .of_match_table = aop_qmp_match_tbl, |
| 121 | }, |
| 122 | }; |
Mahesh Sivasubramanian | a8b8790 | 2017-06-15 14:56:23 -0600 | [diff] [blame] | 123 | builtin_platform_driver(aop_qmp_msg_driver); |