hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/mailbox/mailbox-test.c
....@@ -1,12 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Copyright (C) 2015 ST Microelectronics
34 *
45 * Author: Lee Jones <lee.jones@linaro.org>
5
- *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version.
106 */
117
128 #include <linux/debugfs.h>
....@@ -16,6 +12,7 @@
1612 #include <linux/kernel.h>
1713 #include <linux/mailbox_client.h>
1814 #include <linux/module.h>
15
+#include <linux/mutex.h>
1916 #include <linux/of.h>
2017 #include <linux/platform_device.h>
2118 #include <linux/poll.h>
....@@ -31,7 +28,6 @@
3128 (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
3229
3330 static bool mbox_data_ready;
34
-static struct dentry *root_debugfs_dir;
3531
3632 struct mbox_test_device {
3733 struct device *dev;
....@@ -43,8 +39,10 @@
4339 char *signal;
4440 char *message;
4541 spinlock_t lock;
42
+ struct mutex mutex;
4643 wait_queue_head_t waitq;
4744 struct fasync_struct *async_queue;
45
+ struct dentry *root_debugfs_dir;
4846 };
4947
5048 static ssize_t mbox_test_signal_write(struct file *filp,
....@@ -99,6 +97,7 @@
9997 size_t count, loff_t *ppos)
10098 {
10199 struct mbox_test_device *tdev = filp->private_data;
100
+ char *message;
102101 void *data;
103102 int ret;
104103
....@@ -114,10 +113,13 @@
114113 return -EINVAL;
115114 }
116115
117
- tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
118
- if (!tdev->message)
116
+ message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
117
+ if (!message)
119118 return -ENOMEM;
120119
120
+ mutex_lock(&tdev->mutex);
121
+
122
+ tdev->message = message;
121123 ret = copy_from_user(tdev->message, userbuf, count);
122124 if (ret) {
123125 ret = -EFAULT;
....@@ -147,6 +149,8 @@
147149 kfree(tdev->signal);
148150 kfree(tdev->message);
149151 tdev->signal = NULL;
152
+
153
+ mutex_unlock(&tdev->mutex);
150154
151155 return ret < 0 ? ret : count;
152156 }
....@@ -262,16 +266,16 @@
262266 if (!debugfs_initialized())
263267 return 0;
264268
265
- root_debugfs_dir = debugfs_create_dir("mailbox", NULL);
266
- if (!root_debugfs_dir) {
269
+ tdev->root_debugfs_dir = debugfs_create_dir(dev_name(&pdev->dev), NULL);
270
+ if (!tdev->root_debugfs_dir) {
267271 dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
268272 return -EINVAL;
269273 }
270274
271
- debugfs_create_file("message", 0600, root_debugfs_dir,
275
+ debugfs_create_file("message", 0600, tdev->root_debugfs_dir,
272276 tdev, &mbox_test_message_ops);
273277
274
- debugfs_create_file("signal", 0200, root_debugfs_dir,
278
+ debugfs_create_file("signal", 0200, tdev->root_debugfs_dir,
275279 tdev, &mbox_test_signal_ops);
276280
277281 return 0;
....@@ -396,6 +400,7 @@
396400 platform_set_drvdata(pdev, tdev);
397401
398402 spin_lock_init(&tdev->lock);
403
+ mutex_init(&tdev->mutex);
399404
400405 if (tdev->rx_channel) {
401406 tdev->rx_buffer = devm_kzalloc(&pdev->dev,
....@@ -418,7 +423,7 @@
418423 {
419424 struct mbox_test_device *tdev = platform_get_drvdata(pdev);
420425
421
- debugfs_remove_recursive(root_debugfs_dir);
426
+ debugfs_remove_recursive(tdev->root_debugfs_dir);
422427
423428 if (tdev->tx_channel)
424429 mbox_free_channel(tdev->tx_channel);