1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 ST Microelectronics
5 * Author: Lee Jones <lee.jones@linaro.org>
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/uaccess.h>
21 #include <linux/sched/signal.h>
23 #define MBOX_MAX_SIG_LEN 8
24 #define MBOX_MAX_MSG_LEN 128
25 #define MBOX_BYTES_PER_LINE 16
26 #define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2)
27 #define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * \
28 (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
30 static bool mbox_data_ready;
32 struct mbox_test_device {
34 void __iomem *tx_mmio;
35 void __iomem *rx_mmio;
36 struct mbox_chan *tx_channel;
37 struct mbox_chan *rx_channel;
43 wait_queue_head_t waitq;
44 struct fasync_struct *async_queue;
45 struct dentry *root_debugfs_dir;
48 static ssize_t mbox_test_signal_write(struct file *filp,
49 const char __user *userbuf,
50 size_t count, loff_t *ppos)
52 struct mbox_test_device *tdev = filp->private_data;
54 if (!tdev->tx_channel) {
55 dev_err(tdev->dev, "Channel cannot do Tx\n");
59 if (count > MBOX_MAX_SIG_LEN) {
61 "Signal length %zd greater than max allowed %d\n",
62 count, MBOX_MAX_SIG_LEN);
66 /* Only allocate memory if we need to */
68 tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
73 if (copy_from_user(tdev->signal, userbuf, count)) {
82 static const struct file_operations mbox_test_signal_ops = {
83 .write = mbox_test_signal_write,
85 .llseek = generic_file_llseek,
88 static int mbox_test_message_fasync(int fd, struct file *filp, int on)
90 struct mbox_test_device *tdev = filp->private_data;
92 return fasync_helper(fd, filp, on, &tdev->async_queue);
95 static ssize_t mbox_test_message_write(struct file *filp,
96 const char __user *userbuf,
97 size_t count, loff_t *ppos)
99 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 message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
120 mutex_lock(&tdev->mutex);
122 tdev->message = message;
123 ret = copy_from_user(tdev->message, userbuf, count);
130 * A separate signal is only of use if there is
131 * MMIO to subsequently pass the message through
133 if (tdev->tx_mmio && tdev->signal) {
134 print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
135 tdev->signal, MBOX_MAX_SIG_LEN);
139 data = tdev->message;
141 print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
142 tdev->message, MBOX_MAX_MSG_LEN);
144 ret = mbox_send_message(tdev->tx_channel, data);
146 dev_err(tdev->dev, "Failed to send message via mailbox\n");
150 kfree(tdev->message);
153 mutex_unlock(&tdev->mutex);
155 return ret < 0 ? ret : count;
158 static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
163 spin_lock_irqsave(&tdev->lock, flags);
164 data_ready = mbox_data_ready;
165 spin_unlock_irqrestore(&tdev->lock, flags);
170 static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
171 size_t count, loff_t *ppos)
173 struct mbox_test_device *tdev = filp->private_data;
179 DECLARE_WAITQUEUE(wait, current);
181 touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
185 if (!tdev->rx_channel) {
186 ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
187 ret = simple_read_from_buffer(userbuf, count, ppos,
192 add_wait_queue(&tdev->waitq, &wait);
195 __set_current_state(TASK_INTERRUPTIBLE);
197 if (mbox_test_message_data_ready(tdev))
200 if (filp->f_flags & O_NONBLOCK) {
205 if (signal_pending(current)) {
213 spin_lock_irqsave(&tdev->lock, flags);
215 ptr = tdev->rx_buffer;
216 while (l < MBOX_HEXDUMP_MAX_LEN) {
217 hex_dump_to_buffer(ptr,
219 MBOX_BYTES_PER_LINE, 1, touser + l,
220 MBOX_HEXDUMP_LINE_LEN, true);
222 ptr += MBOX_BYTES_PER_LINE;
223 l += MBOX_HEXDUMP_LINE_LEN;
224 *(touser + (l - 1)) = '\n';
226 *(touser + l) = '\0';
228 memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
229 mbox_data_ready = false;
231 spin_unlock_irqrestore(&tdev->lock, flags);
233 ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
235 __set_current_state(TASK_RUNNING);
236 remove_wait_queue(&tdev->waitq, &wait);
243 mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait)
245 struct mbox_test_device *tdev = filp->private_data;
247 poll_wait(filp, &tdev->waitq, wait);
249 if (mbox_test_message_data_ready(tdev))
250 return EPOLLIN | EPOLLRDNORM;
254 static const struct file_operations mbox_test_message_ops = {
255 .write = mbox_test_message_write,
256 .read = mbox_test_message_read,
257 .fasync = mbox_test_message_fasync,
258 .poll = mbox_test_message_poll,
260 .llseek = generic_file_llseek,
263 static int mbox_test_add_debugfs(struct platform_device *pdev,
264 struct mbox_test_device *tdev)
266 if (!debugfs_initialized())
269 tdev->root_debugfs_dir = debugfs_create_dir(dev_name(&pdev->dev), NULL);
270 if (!tdev->root_debugfs_dir) {
271 dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
275 debugfs_create_file("message", 0600, tdev->root_debugfs_dir,
276 tdev, &mbox_test_message_ops);
278 debugfs_create_file("signal", 0200, tdev->root_debugfs_dir,
279 tdev, &mbox_test_signal_ops);
284 static void mbox_test_receive_message(struct mbox_client *client, void *message)
286 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
289 spin_lock_irqsave(&tdev->lock, flags);
291 memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN);
292 print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS,
293 tdev->rx_buffer, MBOX_MAX_MSG_LEN);
294 } else if (message) {
295 print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS,
296 message, MBOX_MAX_MSG_LEN);
297 memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
299 mbox_data_ready = true;
300 spin_unlock_irqrestore(&tdev->lock, flags);
302 wake_up_interruptible(&tdev->waitq);
304 kill_fasync(&tdev->async_queue, SIGIO, POLL_IN);
307 static void mbox_test_prepare_message(struct mbox_client *client, void *message)
309 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
313 memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
315 memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
319 static void mbox_test_message_sent(struct mbox_client *client,
320 void *message, int r)
323 dev_warn(client->dev,
324 "Client: Message could not be sent: %d\n", r);
326 dev_info(client->dev,
327 "Client: Message sent\n");
330 static struct mbox_chan *
331 mbox_test_request_channel(struct platform_device *pdev, const char *name)
333 struct mbox_client *client;
334 struct mbox_chan *channel;
336 client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
338 return ERR_PTR(-ENOMEM);
340 client->dev = &pdev->dev;
341 client->rx_callback = mbox_test_receive_message;
342 client->tx_prepare = mbox_test_prepare_message;
343 client->tx_done = mbox_test_message_sent;
344 client->tx_block = true;
345 client->knows_txdone = false;
346 client->tx_tout = 500;
348 channel = mbox_request_channel_byname(client, name);
349 if (IS_ERR(channel)) {
350 dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
357 static int mbox_test_probe(struct platform_device *pdev)
359 struct mbox_test_device *tdev;
360 struct resource *res;
361 resource_size_t size;
364 tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
368 /* It's okay for MMIO to be NULL */
369 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
370 tdev->tx_mmio = devm_ioremap_resource(&pdev->dev, res);
371 if (PTR_ERR(tdev->tx_mmio) == -EBUSY) {
372 /* if reserved area in SRAM, try just ioremap */
373 size = resource_size(res);
374 tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size);
375 } else if (IS_ERR(tdev->tx_mmio)) {
376 tdev->tx_mmio = NULL;
379 /* If specified, second reg entry is Rx MMIO */
380 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
381 tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res);
382 if (PTR_ERR(tdev->rx_mmio) == -EBUSY) {
383 size = resource_size(res);
384 tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size);
385 } else if (IS_ERR(tdev->rx_mmio)) {
386 tdev->rx_mmio = tdev->tx_mmio;
389 tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
390 tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
392 if (!tdev->tx_channel && !tdev->rx_channel)
393 return -EPROBE_DEFER;
395 /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
396 if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio))
397 tdev->rx_channel = tdev->tx_channel;
399 tdev->dev = &pdev->dev;
400 platform_set_drvdata(pdev, tdev);
402 spin_lock_init(&tdev->lock);
403 mutex_init(&tdev->mutex);
405 if (tdev->rx_channel) {
406 tdev->rx_buffer = devm_kzalloc(&pdev->dev,
407 MBOX_MAX_MSG_LEN, GFP_KERNEL);
408 if (!tdev->rx_buffer)
412 ret = mbox_test_add_debugfs(pdev, tdev);
416 init_waitqueue_head(&tdev->waitq);
417 dev_info(&pdev->dev, "Successfully registered\n");
422 static int mbox_test_remove(struct platform_device *pdev)
424 struct mbox_test_device *tdev = platform_get_drvdata(pdev);
426 debugfs_remove_recursive(tdev->root_debugfs_dir);
428 if (tdev->tx_channel)
429 mbox_free_channel(tdev->tx_channel);
430 if (tdev->rx_channel)
431 mbox_free_channel(tdev->rx_channel);
436 static const struct of_device_id mbox_test_match[] = {
437 { .compatible = "mailbox-test" },
440 MODULE_DEVICE_TABLE(of, mbox_test_match);
442 static struct platform_driver mbox_test_driver = {
444 .name = "mailbox_test",
445 .of_match_table = mbox_test_match,
447 .probe = mbox_test_probe,
448 .remove = mbox_test_remove,
450 module_platform_driver(mbox_test_driver);
452 MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
453 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
454 MODULE_LICENSE("GPL v2");