blob: a327e0311da213b6bb9c95286d28db66815f83e5 [file] [log] [blame]
Maulik Shaha2a8b392018-01-16 17:22:00 +05301/* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -06002 *
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 Shaha2a8b392018-01-16 17:22:00 +053023#include <linux/mailbox_controller.h>
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -060024
25#define MAX_MSG_SIZE 96 /* Imposed by the remote*/
26
Maulik Shaha2a8b392018-01-16 17:22:00 +053027struct qmp_debugfs_data {
28 struct qmp_pkt pkt;
29 char buf[MAX_MSG_SIZE + 1];
30};
31
32static struct qmp_debugfs_data data_pkt[MBOX_TX_QUEUE_LEN];
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -060033static struct mbox_chan *chan;
34static struct mbox_client *cl;
35
Maulik Shaha2a8b392018-01-16 17:22:00 +053036static DEFINE_MUTEX(qmp_debugfs_mutex);
37
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -060038static ssize_t aop_msg_write(struct file *file, const char __user *userstr,
39 size_t len, loff_t *pos)
40{
Maulik Shaha2a8b392018-01-16 17:22:00 +053041 static int count;
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -060042 int rc;
43
44 if (!len || (len > MAX_MSG_SIZE))
45 return len;
46
Maulik Shaha2a8b392018-01-16 17:22:00 +053047 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 Sivasubramaniana8b87902017-06-15 14:56:23 -060054 if (rc) {
55 pr_err("%s copy from user failed, rc=%d\n", __func__, rc);
Maulik Shaha2a8b392018-01-16 17:22:00 +053056 mutex_unlock(&qmp_debugfs_mutex);
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -060057 return len;
58 }
59
60 /*
61 * Controller expects a 4 byte aligned buffer
62 */
Maulik Shaha2a8b392018-01-16 17:22:00 +053063 data_pkt[count].pkt.size = (len + 0x3) & ~0x3;
64 data_pkt[count].pkt.data = data_pkt[count].buf;
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -060065
Maulik Shaha2a8b392018-01-16 17:22:00 +053066 if (mbox_send_message(chan, &(data_pkt[count].pkt)) < 0)
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -060067 pr_err("Failed to send qmp request\n");
Maulik Shaha2a8b392018-01-16 17:22:00 +053068 else
69 count++;
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -060070
Maulik Shaha2a8b392018-01-16 17:22:00 +053071 mutex_unlock(&qmp_debugfs_mutex);
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -060072 return len;
73}
74
75static const struct file_operations aop_msg_fops = {
76 .write = aop_msg_write,
77};
78
79static 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 Shaha2a8b392018-01-16 17:22:00 +053089 cl->tx_tout = 1000;
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -060090 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;
103err:
104 mbox_free_channel(chan);
105 chan = NULL;
106 return -ENOMEM;
107}
108
109static const struct of_device_id aop_qmp_match_tbl[] = {
110 {.compatible = "qcom,debugfs-qmp-client"},
111 {},
112};
113
114static struct platform_driver aop_qmp_msg_driver = {
115 .probe = qmp_msg_probe,
116 .driver = {
117 .name = "debugfs-qmp-client",
118 .owner = THIS_MODULE,
Maulik Shah4d5de1e2018-07-11 12:03:17 +0530119 .suppress_bind_attrs = true,
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -0600120 .of_match_table = aop_qmp_match_tbl,
121 },
122};
Mahesh Sivasubramaniana8b87902017-06-15 14:56:23 -0600123builtin_platform_driver(aop_qmp_msg_driver);