1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 ST Microelectronics
8 #include <linux/debugfs.h>
12 #include <linux/kernel.h>
13 #include <linux/mailbox_client.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
17 #include <linux/platform_device.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/uaccess.h>
22 #include <linux/sched/signal.h>
24 #define MBOX_MAX_SIG_LEN 8
25 #define MBOX_MAX_MSG_LEN 128
26 #define MBOX_BYTES_PER_LINE 16
27 #define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2)
28 #define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * \
29 (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
31 static bool mbox_data_ready;
33 struct mbox_test_device {
35 void __iomem *tx_mmio;
36 void __iomem *rx_mmio;
37 struct mbox_chan *tx_channel;
38 struct mbox_chan *rx_channel;
44 wait_queue_head_t waitq;
45 struct fasync_struct *async_queue;
46 struct dentry *root_debugfs_dir;
49 static ssize_t mbox_test_signal_write(struct file *filp,
50 const char __user *userbuf,
51 size_t count, loff_t *ppos)
53 struct mbox_test_device *tdev = filp->private_data;
55 if (!tdev->tx_channel) {
56 dev_err(tdev->dev, "Channel cannot do Tx\n");
60 if (count > MBOX_MAX_SIG_LEN) {
62 "Signal length %zd greater than max allowed %d\n",
63 count, MBOX_MAX_SIG_LEN);
67 /* Only allocate memory if we need to */
69 tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
74 if (copy_from_user(tdev->signal, userbuf, count)) {
83 static const struct file_operations mbox_test_signal_ops = {
84 .write = mbox_test_signal_write,
86 .llseek = generic_file_llseek,
89 static int mbox_test_message_fasync(int fd, struct file *filp, int on)
91 struct mbox_test_device *tdev = filp->private_data;
93 return fasync_helper(fd, filp, on, &tdev->async_queue);
96 static ssize_t mbox_test_message_write(struct file *filp,
97 const char __user *userbuf,
98 size_t count, loff_t *ppos)
100 struct mbox_test_device *tdev = filp->private_data;
104 if (!tdev->tx_channel) {
105 dev_err(tdev->dev, "Channel cannot do Tx\n");
109 if (count > MBOX_MAX_MSG_LEN) {
111 "Message length %zd greater than max allowed %d\n",
112 count, MBOX_MAX_MSG_LEN);
116 mutex_lock(&tdev->mutex);
118 tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
122 ret = copy_from_user(tdev->message, userbuf, count);
129 * A separate signal is only of use if there is
130 * MMIO to subsequently pass the message through
132 if (tdev->tx_mmio && tdev->signal) {
133 print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
134 tdev->signal, MBOX_MAX_SIG_LEN);
138 data = tdev->message;
140 print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
141 tdev->message, MBOX_MAX_MSG_LEN);
143 ret = mbox_send_message(tdev->tx_channel, data);
145 dev_err(tdev->dev, "Failed to send message via mailbox\n");
149 kfree(tdev->message);
152 mutex_unlock(&tdev->mutex);
154 return ret < 0 ? ret : count;
157 static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
162 spin_lock_irqsave(&tdev->lock, flags);
163 data_ready = mbox_data_ready;
164 spin_unlock_irqrestore(&tdev->lock, flags);
169 static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
170 size_t count, loff_t *ppos)
172 struct mbox_test_device *tdev = filp->private_data;
178 DECLARE_WAITQUEUE(wait, current);
180 touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
184 if (!tdev->rx_channel) {
185 ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
186 ret = simple_read_from_buffer(userbuf, count, ppos,
191 add_wait_queue(&tdev->waitq, &wait);
194 __set_current_state(TASK_INTERRUPTIBLE);
196 if (mbox_test_message_data_ready(tdev))
199 if (filp->f_flags & O_NONBLOCK) {
204 if (signal_pending(current)) {
212 spin_lock_irqsave(&tdev->lock, flags);
214 ptr = tdev->rx_buffer;
215 while (l < MBOX_HEXDUMP_MAX_LEN) {
216 hex_dump_to_buffer(ptr,
218 MBOX_BYTES_PER_LINE, 1, touser + l,
219 MBOX_HEXDUMP_LINE_LEN, true);
221 ptr += MBOX_BYTES_PER_LINE;
222 l += MBOX_HEXDUMP_LINE_LEN;
223 *(touser + (l - 1)) = '\n';
225 *(touser + l) = '\0';
227 memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
228 mbox_data_ready = false;
230 spin_unlock_irqrestore(&tdev->lock, flags);
232 ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
234 __set_current_state(TASK_RUNNING);
235 remove_wait_queue(&tdev->waitq, &wait);
242 mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait)
244 struct mbox_test_device *tdev = filp->private_data;
246 poll_wait(filp, &tdev->waitq, wait);
248 if (mbox_test_message_data_ready(tdev))
249 return EPOLLIN | EPOLLRDNORM;
253 static const struct file_operations mbox_test_message_ops = {
254 .write = mbox_test_message_write,
255 .read = mbox_test_message_read,
256 .fasync = mbox_test_message_fasync,
257 .poll = mbox_test_message_poll,
259 .llseek = generic_file_llseek,
262 static int mbox_test_add_debugfs(struct platform_device *pdev,
263 struct mbox_test_device *tdev)
265 if (!debugfs_initialized())
268 tdev->root_debugfs_dir = debugfs_create_dir(dev_name(&pdev->dev), NULL);
269 if (!tdev->root_debugfs_dir) {
270 dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
274 debugfs_create_file("message", 0600, tdev->root_debugfs_dir,
275 tdev, &mbox_test_message_ops);
277 debugfs_create_file("signal", 0200, tdev->root_debugfs_dir,
278 tdev, &mbox_test_signal_ops);
283 static void mbox_test_receive_message(struct mbox_client *client, void *message)
285 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
288 spin_lock_irqsave(&tdev->lock, flags);
290 memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN);
291 print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS,
292 tdev->rx_buffer, MBOX_MAX_MSG_LEN);
293 } else if (message) {
294 print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS,
295 message, MBOX_MAX_MSG_LEN);
296 memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
298 mbox_data_ready = true;
299 spin_unlock_irqrestore(&tdev->lock, flags);
301 wake_up_interruptible(&tdev->waitq);
303 kill_fasync(&tdev->async_queue, SIGIO, POLL_IN);
306 static void mbox_test_prepare_message(struct mbox_client *client, void *message)
308 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
312 memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
314 memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
318 static void mbox_test_message_sent(struct mbox_client *client,
319 void *message, int r)
322 dev_warn(client->dev,
323 "Client: Message could not be sent: %d\n", r);
325 dev_info(client->dev,
326 "Client: Message sent\n");
329 static struct mbox_chan *
330 mbox_test_request_channel(struct platform_device *pdev, const char *name)
332 struct mbox_client *client;
333 struct mbox_chan *channel;
335 client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
337 return ERR_PTR(-ENOMEM);
339 client->dev = &pdev->dev;
340 client->rx_callback = mbox_test_receive_message;
341 client->tx_prepare = mbox_test_prepare_message;
342 client->tx_done = mbox_test_message_sent;
343 client->tx_block = true;
344 client->knows_txdone = false;
345 client->tx_tout = 500;
347 channel = mbox_request_channel_byname(client, name);
348 if (IS_ERR(channel)) {
349 dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
356 static int mbox_test_probe(struct platform_device *pdev)
358 struct mbox_test_device *tdev;
359 struct resource *res;
360 resource_size_t size;
363 tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
367 /* It's okay for MMIO to be NULL */
368 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
369 tdev->tx_mmio = devm_ioremap_resource(&pdev->dev, res);
370 if (PTR_ERR(tdev->tx_mmio) == -EBUSY) {
371 /* if reserved area in SRAM, try just ioremap */
372 size = resource_size(res);
373 tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size);
374 } else if (IS_ERR(tdev->tx_mmio)) {
375 tdev->tx_mmio = NULL;
378 /* If specified, second reg entry is Rx MMIO */
379 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
380 tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res);
381 if (PTR_ERR(tdev->rx_mmio) == -EBUSY) {
382 size = resource_size(res);
383 tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size);
384 } else if (IS_ERR(tdev->rx_mmio)) {
385 tdev->rx_mmio = tdev->tx_mmio;
388 tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
389 tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
391 if (!tdev->tx_channel && !tdev->rx_channel)
392 return -EPROBE_DEFER;
394 /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
395 if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio))
396 tdev->rx_channel = tdev->tx_channel;
398 tdev->dev = &pdev->dev;
399 platform_set_drvdata(pdev, tdev);
401 spin_lock_init(&tdev->lock);
402 mutex_init(&tdev->mutex);
404 if (tdev->rx_channel) {
405 tdev->rx_buffer = devm_kzalloc(&pdev->dev,
406 MBOX_MAX_MSG_LEN, GFP_KERNEL);
407 if (!tdev->rx_buffer)
411 ret = mbox_test_add_debugfs(pdev, tdev);
415 init_waitqueue_head(&tdev->waitq);
416 dev_info(&pdev->dev, "Successfully registered\n");
421 static int mbox_test_remove(struct platform_device *pdev)
423 struct mbox_test_device *tdev = platform_get_drvdata(pdev);
425 debugfs_remove_recursive(tdev->root_debugfs_dir);
427 if (tdev->tx_channel)
428 mbox_free_channel(tdev->tx_channel);
429 if (tdev->rx_channel)
430 mbox_free_channel(tdev->rx_channel);
435 static const struct of_device_id mbox_test_match[] = {
436 { .compatible = "mailbox-test" },
439 MODULE_DEVICE_TABLE(of, mbox_test_match);
441 static struct platform_driver mbox_test_driver = {
443 .name = "mailbox_test",
444 .of_match_table = mbox_test_match,
446 .probe = mbox_test_probe,
447 .remove = mbox_test_remove,
449 module_platform_driver(mbox_test_driver);
451 MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
453 MODULE_LICENSE("GPL v2");