2 * Copyright (C) 2015 ST Microelectronics
4 * Author: Lee Jones <lee.jones@linaro.org>
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.
12 #include <linux/debugfs.h>
13 #include <linux/err.h>
16 #include <linux/kernel.h>
17 #include <linux/mailbox_client.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
21 #include <linux/platform_device.h>
22 #include <linux/poll.h>
23 #include <linux/slab.h>
24 #include <linux/uaccess.h>
25 #include <linux/sched/signal.h>
27 #define MBOX_MAX_SIG_LEN 8
28 #define MBOX_MAX_MSG_LEN 128
29 #define MBOX_BYTES_PER_LINE 16
30 #define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2)
31 #define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * \
32 (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
34 static bool mbox_data_ready;
35 static struct dentry *root_debugfs_dir;
37 struct mbox_test_device {
39 void __iomem *tx_mmio;
40 void __iomem *rx_mmio;
41 struct mbox_chan *tx_channel;
42 struct mbox_chan *rx_channel;
48 wait_queue_head_t waitq;
49 struct fasync_struct *async_queue;
52 static ssize_t mbox_test_signal_write(struct file *filp,
53 const char __user *userbuf,
54 size_t count, loff_t *ppos)
56 struct mbox_test_device *tdev = filp->private_data;
58 if (!tdev->tx_channel) {
59 dev_err(tdev->dev, "Channel cannot do Tx\n");
63 if (count > MBOX_MAX_SIG_LEN) {
65 "Signal length %zd greater than max allowed %d\n",
66 count, MBOX_MAX_SIG_LEN);
70 /* Only allocate memory if we need to */
72 tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
77 if (copy_from_user(tdev->signal, userbuf, count)) {
86 static const struct file_operations mbox_test_signal_ops = {
87 .write = mbox_test_signal_write,
89 .llseek = generic_file_llseek,
92 static int mbox_test_message_fasync(int fd, struct file *filp, int on)
94 struct mbox_test_device *tdev = filp->private_data;
96 return fasync_helper(fd, filp, on, &tdev->async_queue);
99 static ssize_t mbox_test_message_write(struct file *filp,
100 const char __user *userbuf,
101 size_t count, loff_t *ppos)
103 struct mbox_test_device *tdev = filp->private_data;
108 if (!tdev->tx_channel) {
109 dev_err(tdev->dev, "Channel cannot do Tx\n");
113 if (count > MBOX_MAX_MSG_LEN) {
115 "Message length %zd greater than max allowed %d\n",
116 count, MBOX_MAX_MSG_LEN);
120 message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
124 mutex_lock(&tdev->mutex);
126 tdev->message = message;
127 ret = copy_from_user(tdev->message, userbuf, count);
134 * A separate signal is only of use if there is
135 * MMIO to subsequently pass the message through
137 if (tdev->tx_mmio && tdev->signal) {
138 print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
139 tdev->signal, MBOX_MAX_SIG_LEN);
143 data = tdev->message;
145 print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
146 tdev->message, MBOX_MAX_MSG_LEN);
148 ret = mbox_send_message(tdev->tx_channel, data);
150 dev_err(tdev->dev, "Failed to send message via mailbox\n");
154 kfree(tdev->message);
157 mutex_unlock(&tdev->mutex);
159 return ret < 0 ? ret : count;
162 static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
167 spin_lock_irqsave(&tdev->lock, flags);
168 data_ready = mbox_data_ready;
169 spin_unlock_irqrestore(&tdev->lock, flags);
174 static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
175 size_t count, loff_t *ppos)
177 struct mbox_test_device *tdev = filp->private_data;
183 DECLARE_WAITQUEUE(wait, current);
185 touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
189 if (!tdev->rx_channel) {
190 ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
191 ret = simple_read_from_buffer(userbuf, count, ppos,
196 add_wait_queue(&tdev->waitq, &wait);
199 __set_current_state(TASK_INTERRUPTIBLE);
201 if (mbox_test_message_data_ready(tdev))
204 if (filp->f_flags & O_NONBLOCK) {
209 if (signal_pending(current)) {
217 spin_lock_irqsave(&tdev->lock, flags);
219 ptr = tdev->rx_buffer;
220 while (l < MBOX_HEXDUMP_MAX_LEN) {
221 hex_dump_to_buffer(ptr,
223 MBOX_BYTES_PER_LINE, 1, touser + l,
224 MBOX_HEXDUMP_LINE_LEN, true);
226 ptr += MBOX_BYTES_PER_LINE;
227 l += MBOX_HEXDUMP_LINE_LEN;
228 *(touser + (l - 1)) = '\n';
230 *(touser + l) = '\0';
232 memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
233 mbox_data_ready = false;
235 spin_unlock_irqrestore(&tdev->lock, flags);
237 ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
239 __set_current_state(TASK_RUNNING);
240 remove_wait_queue(&tdev->waitq, &wait);
247 mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait)
249 struct mbox_test_device *tdev = filp->private_data;
251 poll_wait(filp, &tdev->waitq, wait);
253 if (mbox_test_message_data_ready(tdev))
254 return POLLIN | POLLRDNORM;
258 static const struct file_operations mbox_test_message_ops = {
259 .write = mbox_test_message_write,
260 .read = mbox_test_message_read,
261 .fasync = mbox_test_message_fasync,
262 .poll = mbox_test_message_poll,
264 .llseek = generic_file_llseek,
267 static int mbox_test_add_debugfs(struct platform_device *pdev,
268 struct mbox_test_device *tdev)
270 if (!debugfs_initialized())
273 root_debugfs_dir = debugfs_create_dir("mailbox", NULL);
274 if (!root_debugfs_dir) {
275 dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
279 debugfs_create_file("message", 0600, root_debugfs_dir,
280 tdev, &mbox_test_message_ops);
282 debugfs_create_file("signal", 0200, root_debugfs_dir,
283 tdev, &mbox_test_signal_ops);
288 static void mbox_test_receive_message(struct mbox_client *client, void *message)
290 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
293 spin_lock_irqsave(&tdev->lock, flags);
295 memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN);
296 print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS,
297 tdev->rx_buffer, MBOX_MAX_MSG_LEN);
298 } else if (message) {
299 print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS,
300 message, MBOX_MAX_MSG_LEN);
301 memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
303 mbox_data_ready = true;
304 spin_unlock_irqrestore(&tdev->lock, flags);
306 wake_up_interruptible(&tdev->waitq);
308 kill_fasync(&tdev->async_queue, SIGIO, POLL_IN);
311 static void mbox_test_prepare_message(struct mbox_client *client, void *message)
313 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
317 memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
319 memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
323 static void mbox_test_message_sent(struct mbox_client *client,
324 void *message, int r)
327 dev_warn(client->dev,
328 "Client: Message could not be sent: %d\n", r);
330 dev_info(client->dev,
331 "Client: Message sent\n");
334 static struct mbox_chan *
335 mbox_test_request_channel(struct platform_device *pdev, const char *name)
337 struct mbox_client *client;
338 struct mbox_chan *channel;
340 client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
342 return ERR_PTR(-ENOMEM);
344 client->dev = &pdev->dev;
345 client->rx_callback = mbox_test_receive_message;
346 client->tx_prepare = mbox_test_prepare_message;
347 client->tx_done = mbox_test_message_sent;
348 client->tx_block = true;
349 client->knows_txdone = false;
350 client->tx_tout = 500;
352 channel = mbox_request_channel_byname(client, name);
353 if (IS_ERR(channel)) {
354 dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
361 static int mbox_test_probe(struct platform_device *pdev)
363 struct mbox_test_device *tdev;
364 struct resource *res;
365 resource_size_t size;
368 tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
372 /* It's okay for MMIO to be NULL */
373 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
374 tdev->tx_mmio = devm_ioremap_resource(&pdev->dev, res);
375 if (PTR_ERR(tdev->tx_mmio) == -EBUSY) {
376 /* if reserved area in SRAM, try just ioremap */
377 size = resource_size(res);
378 tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size);
379 } else if (IS_ERR(tdev->tx_mmio)) {
380 tdev->tx_mmio = NULL;
383 /* If specified, second reg entry is Rx MMIO */
384 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
385 tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res);
386 if (PTR_ERR(tdev->rx_mmio) == -EBUSY) {
387 size = resource_size(res);
388 tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size);
389 } else if (IS_ERR(tdev->rx_mmio)) {
390 tdev->rx_mmio = tdev->tx_mmio;
393 tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
394 tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
396 if (!tdev->tx_channel && !tdev->rx_channel)
397 return -EPROBE_DEFER;
399 /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
400 if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio))
401 tdev->rx_channel = tdev->tx_channel;
403 tdev->dev = &pdev->dev;
404 platform_set_drvdata(pdev, tdev);
406 spin_lock_init(&tdev->lock);
407 mutex_init(&tdev->mutex);
409 if (tdev->rx_channel) {
410 tdev->rx_buffer = devm_kzalloc(&pdev->dev,
411 MBOX_MAX_MSG_LEN, GFP_KERNEL);
412 if (!tdev->rx_buffer)
416 ret = mbox_test_add_debugfs(pdev, tdev);
420 init_waitqueue_head(&tdev->waitq);
421 dev_info(&pdev->dev, "Successfully registered\n");
426 static int mbox_test_remove(struct platform_device *pdev)
428 struct mbox_test_device *tdev = platform_get_drvdata(pdev);
430 debugfs_remove_recursive(root_debugfs_dir);
432 if (tdev->tx_channel)
433 mbox_free_channel(tdev->tx_channel);
434 if (tdev->rx_channel)
435 mbox_free_channel(tdev->rx_channel);
440 static const struct of_device_id mbox_test_match[] = {
441 { .compatible = "mailbox-test" },
444 MODULE_DEVICE_TABLE(of, mbox_test_match);
446 static struct platform_driver mbox_test_driver = {
448 .name = "mailbox_test",
449 .of_match_table = mbox_test_match,
451 .probe = mbox_test_probe,
452 .remove = mbox_test_remove,
454 module_platform_driver(mbox_test_driver);
456 MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
457 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
458 MODULE_LICENSE("GPL v2");