^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2015 ST Microelectronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author: Lee Jones <lee.jones@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mailbox_client.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define MBOX_MAX_SIG_LEN 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define MBOX_MAX_MSG_LEN 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define MBOX_BYTES_PER_LINE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static bool mbox_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct mbox_test_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) void __iomem *tx_mmio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) void __iomem *rx_mmio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct mbox_chan *tx_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct mbox_chan *rx_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) char *rx_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) char *signal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) char *message;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) wait_queue_head_t waitq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct fasync_struct *async_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct dentry *root_debugfs_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static ssize_t mbox_test_signal_write(struct file *filp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) const char __user *userbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct mbox_test_device *tdev = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!tdev->tx_channel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) dev_err(tdev->dev, "Channel cannot do Tx\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (count > MBOX_MAX_SIG_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) dev_err(tdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) "Signal length %zd greater than max allowed %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) count, MBOX_MAX_SIG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* Only allocate memory if we need to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (!tdev->signal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (!tdev->signal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (copy_from_user(tdev->signal, userbuf, count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) kfree(tdev->signal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) tdev->signal = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static const struct file_operations mbox_test_signal_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .write = mbox_test_signal_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .open = simple_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int mbox_test_message_fasync(int fd, struct file *filp, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct mbox_test_device *tdev = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return fasync_helper(fd, filp, on, &tdev->async_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static ssize_t mbox_test_message_write(struct file *filp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) const char __user *userbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct mbox_test_device *tdev = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (!tdev->tx_channel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) dev_err(tdev->dev, "Channel cannot do Tx\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (count > MBOX_MAX_MSG_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) dev_err(tdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) "Message length %zd greater than max allowed %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) count, MBOX_MAX_MSG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (!tdev->message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ret = copy_from_user(tdev->message, userbuf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * A separate signal is only of use if there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * MMIO to subsequently pass the message through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (tdev->tx_mmio && tdev->signal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) tdev->signal, MBOX_MAX_SIG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) data = tdev->signal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) data = tdev->message;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) tdev->message, MBOX_MAX_MSG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ret = mbox_send_message(tdev->tx_channel, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) dev_err(tdev->dev, "Failed to send message via mailbox\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) kfree(tdev->signal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) kfree(tdev->message);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) tdev->signal = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return ret < 0 ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) bool data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) spin_lock_irqsave(&tdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) data_ready = mbox_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) spin_unlock_irqrestore(&tdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct mbox_test_device *tdev = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) char *touser, *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!touser)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (!tdev->rx_channel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret = simple_read_from_buffer(userbuf, count, ppos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) touser, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) goto kfree_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) add_wait_queue(&tdev->waitq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) __set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (mbox_test_message_data_ready(tdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (filp->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) goto waitq_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) goto waitq_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) } while (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) spin_lock_irqsave(&tdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ptr = tdev->rx_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) while (l < MBOX_HEXDUMP_MAX_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) hex_dump_to_buffer(ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) MBOX_BYTES_PER_LINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) MBOX_BYTES_PER_LINE, 1, touser + l,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) MBOX_HEXDUMP_LINE_LEN, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) ptr += MBOX_BYTES_PER_LINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) l += MBOX_HEXDUMP_LINE_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) *(touser + (l - 1)) = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) *(touser + l) = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) mbox_data_ready = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) spin_unlock_irqrestore(&tdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) waitq_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) remove_wait_queue(&tdev->waitq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) kfree_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) kfree(touser);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static __poll_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct mbox_test_device *tdev = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) poll_wait(filp, &tdev->waitq, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (mbox_test_message_data_ready(tdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static const struct file_operations mbox_test_message_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) .write = mbox_test_message_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) .read = mbox_test_message_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) .fasync = mbox_test_message_fasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) .poll = mbox_test_message_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) .open = simple_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static int mbox_test_add_debugfs(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct mbox_test_device *tdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (!debugfs_initialized())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) tdev->root_debugfs_dir = debugfs_create_dir(dev_name(&pdev->dev), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (!tdev->root_debugfs_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) debugfs_create_file("message", 0600, tdev->root_debugfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) tdev, &mbox_test_message_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) debugfs_create_file("signal", 0200, tdev->root_debugfs_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) tdev, &mbox_test_signal_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static void mbox_test_receive_message(struct mbox_client *client, void *message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) spin_lock_irqsave(&tdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (tdev->rx_mmio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) tdev->rx_buffer, MBOX_MAX_MSG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) } else if (message) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) message, MBOX_MAX_MSG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) mbox_data_ready = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) spin_unlock_irqrestore(&tdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) wake_up_interruptible(&tdev->waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) kill_fasync(&tdev->async_queue, SIGIO, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static void mbox_test_prepare_message(struct mbox_client *client, void *message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (tdev->tx_mmio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (tdev->signal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static void mbox_test_message_sent(struct mbox_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) void *message, int r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) dev_warn(client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) "Client: Message could not be sent: %d\n", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) dev_info(client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) "Client: Message sent\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static struct mbox_chan *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) mbox_test_request_channel(struct platform_device *pdev, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) struct mbox_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct mbox_chan *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (!client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) client->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) client->rx_callback = mbox_test_receive_message;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) client->tx_prepare = mbox_test_prepare_message;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) client->tx_done = mbox_test_message_sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) client->tx_block = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) client->knows_txdone = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) client->tx_tout = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) channel = mbox_request_channel_byname(client, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (IS_ERR(channel)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static int mbox_test_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) struct mbox_test_device *tdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) resource_size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (!tdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /* It's okay for MMIO to be NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) tdev->tx_mmio = devm_ioremap_resource(&pdev->dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (PTR_ERR(tdev->tx_mmio) == -EBUSY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) /* if reserved area in SRAM, try just ioremap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) size = resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) } else if (IS_ERR(tdev->tx_mmio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) tdev->tx_mmio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /* If specified, second reg entry is Rx MMIO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (PTR_ERR(tdev->rx_mmio) == -EBUSY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) size = resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) } else if (IS_ERR(tdev->rx_mmio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) tdev->rx_mmio = tdev->tx_mmio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (!tdev->tx_channel && !tdev->rx_channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) tdev->rx_channel = tdev->tx_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) tdev->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) platform_set_drvdata(pdev, tdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) spin_lock_init(&tdev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (tdev->rx_channel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) tdev->rx_buffer = devm_kzalloc(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) MBOX_MAX_MSG_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (!tdev->rx_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) ret = mbox_test_add_debugfs(pdev, tdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) init_waitqueue_head(&tdev->waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) dev_info(&pdev->dev, "Successfully registered\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) static int mbox_test_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct mbox_test_device *tdev = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) debugfs_remove_recursive(tdev->root_debugfs_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (tdev->tx_channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) mbox_free_channel(tdev->tx_channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (tdev->rx_channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) mbox_free_channel(tdev->rx_channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) static const struct of_device_id mbox_test_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) { .compatible = "mailbox-test" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) MODULE_DEVICE_TABLE(of, mbox_test_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static struct platform_driver mbox_test_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) .name = "mailbox_test",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) .of_match_table = mbox_test_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) .probe = mbox_test_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) .remove = mbox_test_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) module_platform_driver(mbox_test_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) MODULE_LICENSE("GPL v2");