| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (C) 2015 ST Microelectronics |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * 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. |
|---|
| 10 | 6 | */ |
|---|
| 11 | 7 | |
|---|
| 12 | 8 | #include <linux/debugfs.h> |
|---|
| .. | .. |
|---|
| 16 | 12 | #include <linux/kernel.h> |
|---|
| 17 | 13 | #include <linux/mailbox_client.h> |
|---|
| 18 | 14 | #include <linux/module.h> |
|---|
| 15 | +#include <linux/mutex.h> |
|---|
| 19 | 16 | #include <linux/of.h> |
|---|
| 20 | 17 | #include <linux/platform_device.h> |
|---|
| 21 | 18 | #include <linux/poll.h> |
|---|
| .. | .. |
|---|
| 31 | 28 | (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE)) |
|---|
| 32 | 29 | |
|---|
| 33 | 30 | static bool mbox_data_ready; |
|---|
| 34 | | -static struct dentry *root_debugfs_dir; |
|---|
| 35 | 31 | |
|---|
| 36 | 32 | struct mbox_test_device { |
|---|
| 37 | 33 | struct device *dev; |
|---|
| .. | .. |
|---|
| 43 | 39 | char *signal; |
|---|
| 44 | 40 | char *message; |
|---|
| 45 | 41 | spinlock_t lock; |
|---|
| 42 | + struct mutex mutex; |
|---|
| 46 | 43 | wait_queue_head_t waitq; |
|---|
| 47 | 44 | struct fasync_struct *async_queue; |
|---|
| 45 | + struct dentry *root_debugfs_dir; |
|---|
| 48 | 46 | }; |
|---|
| 49 | 47 | |
|---|
| 50 | 48 | static ssize_t mbox_test_signal_write(struct file *filp, |
|---|
| .. | .. |
|---|
| 99 | 97 | size_t count, loff_t *ppos) |
|---|
| 100 | 98 | { |
|---|
| 101 | 99 | struct mbox_test_device *tdev = filp->private_data; |
|---|
| 100 | + char *message; |
|---|
| 102 | 101 | void *data; |
|---|
| 103 | 102 | int ret; |
|---|
| 104 | 103 | |
|---|
| .. | .. |
|---|
| 114 | 113 | return -EINVAL; |
|---|
| 115 | 114 | } |
|---|
| 116 | 115 | |
|---|
| 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) |
|---|
| 119 | 118 | return -ENOMEM; |
|---|
| 120 | 119 | |
|---|
| 120 | + mutex_lock(&tdev->mutex); |
|---|
| 121 | + |
|---|
| 122 | + tdev->message = message; |
|---|
| 121 | 123 | ret = copy_from_user(tdev->message, userbuf, count); |
|---|
| 122 | 124 | if (ret) { |
|---|
| 123 | 125 | ret = -EFAULT; |
|---|
| .. | .. |
|---|
| 147 | 149 | kfree(tdev->signal); |
|---|
| 148 | 150 | kfree(tdev->message); |
|---|
| 149 | 151 | tdev->signal = NULL; |
|---|
| 152 | + |
|---|
| 153 | + mutex_unlock(&tdev->mutex); |
|---|
| 150 | 154 | |
|---|
| 151 | 155 | return ret < 0 ? ret : count; |
|---|
| 152 | 156 | } |
|---|
| .. | .. |
|---|
| 262 | 266 | if (!debugfs_initialized()) |
|---|
| 263 | 267 | return 0; |
|---|
| 264 | 268 | |
|---|
| 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) { |
|---|
| 267 | 271 | dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n"); |
|---|
| 268 | 272 | return -EINVAL; |
|---|
| 269 | 273 | } |
|---|
| 270 | 274 | |
|---|
| 271 | | - debugfs_create_file("message", 0600, root_debugfs_dir, |
|---|
| 275 | + debugfs_create_file("message", 0600, tdev->root_debugfs_dir, |
|---|
| 272 | 276 | tdev, &mbox_test_message_ops); |
|---|
| 273 | 277 | |
|---|
| 274 | | - debugfs_create_file("signal", 0200, root_debugfs_dir, |
|---|
| 278 | + debugfs_create_file("signal", 0200, tdev->root_debugfs_dir, |
|---|
| 275 | 279 | tdev, &mbox_test_signal_ops); |
|---|
| 276 | 280 | |
|---|
| 277 | 281 | return 0; |
|---|
| .. | .. |
|---|
| 396 | 400 | platform_set_drvdata(pdev, tdev); |
|---|
| 397 | 401 | |
|---|
| 398 | 402 | spin_lock_init(&tdev->lock); |
|---|
| 403 | + mutex_init(&tdev->mutex); |
|---|
| 399 | 404 | |
|---|
| 400 | 405 | if (tdev->rx_channel) { |
|---|
| 401 | 406 | tdev->rx_buffer = devm_kzalloc(&pdev->dev, |
|---|
| .. | .. |
|---|
| 418 | 423 | { |
|---|
| 419 | 424 | struct mbox_test_device *tdev = platform_get_drvdata(pdev); |
|---|
| 420 | 425 | |
|---|
| 421 | | - debugfs_remove_recursive(root_debugfs_dir); |
|---|
| 426 | + debugfs_remove_recursive(tdev->root_debugfs_dir); |
|---|
| 422 | 427 | |
|---|
| 423 | 428 | if (tdev->tx_channel) |
|---|
| 424 | 429 | mbox_free_channel(tdev->tx_channel); |
|---|