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>
20 #include <linux/platform_device.h>
21 #include <linux/poll.h>
22 #include <linux/slab.h>
23 #include <linux/uaccess.h>
24 #include <linux/sched/signal.h>
26 #define MBOX_MAX_SIG_LEN 8
27 #define MBOX_MAX_MSG_LEN 128
28 #define MBOX_BYTES_PER_LINE 16
29 #define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2)
30 #define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * \
31 (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
33 static struct dentry
*root_debugfs_dir
;
35 struct mbox_test_device
{
37 void __iomem
*tx_mmio
;
38 void __iomem
*rx_mmio
;
39 struct mbox_chan
*tx_channel
;
40 struct mbox_chan
*rx_channel
;
45 wait_queue_head_t waitq
;
46 struct fasync_struct
*async_queue
;
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 tdev
->message
= kzalloc(MBOX_MAX_MSG_LEN
, GFP_KERNEL
);
120 ret
= copy_from_user(tdev
->message
, userbuf
, count
);
127 * A separate signal is only of use if there is
128 * MMIO to subsequently pass the message through
130 if (tdev
->tx_mmio
&& tdev
->signal
) {
131 print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS
,
132 tdev
->signal
, MBOX_MAX_SIG_LEN
);
136 data
= tdev
->message
;
138 print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS
,
139 tdev
->message
, MBOX_MAX_MSG_LEN
);
141 ret
= mbox_send_message(tdev
->tx_channel
, data
);
143 dev_err(tdev
->dev
, "Failed to send message via mailbox\n");
147 kfree(tdev
->message
);
150 return ret
< 0 ? ret
: count
;
153 static bool mbox_test_message_data_ready(struct mbox_test_device
*tdev
)
158 spin_lock_irqsave(&tdev
->lock
, flags
);
159 data
= tdev
->rx_buffer
[0];
160 spin_unlock_irqrestore(&tdev
->lock
, flags
);
167 static ssize_t
mbox_test_message_read(struct file
*filp
, char __user
*userbuf
,
168 size_t count
, loff_t
*ppos
)
170 struct mbox_test_device
*tdev
= filp
->private_data
;
176 DECLARE_WAITQUEUE(wait
, current
);
178 touser
= kzalloc(MBOX_HEXDUMP_MAX_LEN
+ 1, GFP_KERNEL
);
182 if (!tdev
->rx_channel
) {
183 ret
= snprintf(touser
, 20, "<NO RX CAPABILITY>\n");
184 ret
= simple_read_from_buffer(userbuf
, count
, ppos
,
189 add_wait_queue(&tdev
->waitq
, &wait
);
192 __set_current_state(TASK_INTERRUPTIBLE
);
194 if (mbox_test_message_data_ready(tdev
))
197 if (filp
->f_flags
& O_NONBLOCK
) {
202 if (signal_pending(current
)) {
210 spin_lock_irqsave(&tdev
->lock
, flags
);
212 ptr
= tdev
->rx_buffer
;
213 while (l
< MBOX_HEXDUMP_MAX_LEN
) {
214 hex_dump_to_buffer(ptr
,
216 MBOX_BYTES_PER_LINE
, 1, touser
+ l
,
217 MBOX_HEXDUMP_LINE_LEN
, true);
219 ptr
+= MBOX_BYTES_PER_LINE
;
220 l
+= MBOX_HEXDUMP_LINE_LEN
;
221 *(touser
+ (l
- 1)) = '\n';
223 *(touser
+ l
) = '\0';
225 memset(tdev
->rx_buffer
, 0, MBOX_MAX_MSG_LEN
);
227 spin_unlock_irqrestore(&tdev
->lock
, flags
);
229 ret
= simple_read_from_buffer(userbuf
, count
, ppos
, touser
, MBOX_HEXDUMP_MAX_LEN
);
231 __set_current_state(TASK_RUNNING
);
232 remove_wait_queue(&tdev
->waitq
, &wait
);
239 mbox_test_message_poll(struct file
*filp
, struct poll_table_struct
*wait
)
241 struct mbox_test_device
*tdev
= filp
->private_data
;
243 poll_wait(filp
, &tdev
->waitq
, wait
);
245 if (mbox_test_message_data_ready(tdev
))
246 return POLLIN
| POLLRDNORM
;
250 static const struct file_operations mbox_test_message_ops
= {
251 .write
= mbox_test_message_write
,
252 .read
= mbox_test_message_read
,
253 .fasync
= mbox_test_message_fasync
,
254 .poll
= mbox_test_message_poll
,
256 .llseek
= generic_file_llseek
,
259 static int mbox_test_add_debugfs(struct platform_device
*pdev
,
260 struct mbox_test_device
*tdev
)
262 if (!debugfs_initialized())
265 root_debugfs_dir
= debugfs_create_dir("mailbox", NULL
);
266 if (!root_debugfs_dir
) {
267 dev_err(&pdev
->dev
, "Failed to create Mailbox debugfs\n");
271 debugfs_create_file("message", 0600, root_debugfs_dir
,
272 tdev
, &mbox_test_message_ops
);
274 debugfs_create_file("signal", 0200, root_debugfs_dir
,
275 tdev
, &mbox_test_signal_ops
);
280 static void mbox_test_receive_message(struct mbox_client
*client
, void *message
)
282 struct mbox_test_device
*tdev
= dev_get_drvdata(client
->dev
);
285 spin_lock_irqsave(&tdev
->lock
, flags
);
287 memcpy_fromio(tdev
->rx_buffer
, tdev
->rx_mmio
, MBOX_MAX_MSG_LEN
);
288 print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS
,
289 tdev
->rx_buffer
, MBOX_MAX_MSG_LEN
);
290 } else if (message
) {
291 print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS
,
292 message
, MBOX_MAX_MSG_LEN
);
293 memcpy(tdev
->rx_buffer
, message
, MBOX_MAX_MSG_LEN
);
295 spin_unlock_irqrestore(&tdev
->lock
, flags
);
297 wake_up_interruptible(&tdev
->waitq
);
299 kill_fasync(&tdev
->async_queue
, SIGIO
, POLL_IN
);
302 static void mbox_test_prepare_message(struct mbox_client
*client
, void *message
)
304 struct mbox_test_device
*tdev
= dev_get_drvdata(client
->dev
);
308 memcpy_toio(tdev
->tx_mmio
, tdev
->message
, MBOX_MAX_MSG_LEN
);
310 memcpy_toio(tdev
->tx_mmio
, message
, MBOX_MAX_MSG_LEN
);
314 static void mbox_test_message_sent(struct mbox_client
*client
,
315 void *message
, int r
)
318 dev_warn(client
->dev
,
319 "Client: Message could not be sent: %d\n", r
);
321 dev_info(client
->dev
,
322 "Client: Message sent\n");
325 static struct mbox_chan
*
326 mbox_test_request_channel(struct platform_device
*pdev
, const char *name
)
328 struct mbox_client
*client
;
329 struct mbox_chan
*channel
;
331 client
= devm_kzalloc(&pdev
->dev
, sizeof(*client
), GFP_KERNEL
);
333 return ERR_PTR(-ENOMEM
);
335 client
->dev
= &pdev
->dev
;
336 client
->rx_callback
= mbox_test_receive_message
;
337 client
->tx_prepare
= mbox_test_prepare_message
;
338 client
->tx_done
= mbox_test_message_sent
;
339 client
->tx_block
= true;
340 client
->knows_txdone
= false;
341 client
->tx_tout
= 500;
343 channel
= mbox_request_channel_byname(client
, name
);
344 if (IS_ERR(channel
)) {
345 dev_warn(&pdev
->dev
, "Failed to request %s channel\n", name
);
352 static int mbox_test_probe(struct platform_device
*pdev
)
354 struct mbox_test_device
*tdev
;
355 struct resource
*res
;
356 resource_size_t size
;
359 tdev
= devm_kzalloc(&pdev
->dev
, sizeof(*tdev
), GFP_KERNEL
);
363 /* It's okay for MMIO to be NULL */
364 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
365 size
= resource_size(res
);
366 tdev
->tx_mmio
= devm_ioremap_resource(&pdev
->dev
, res
);
367 if (PTR_ERR(tdev
->tx_mmio
) == -EBUSY
)
368 /* if reserved area in SRAM, try just ioremap */
369 tdev
->tx_mmio
= devm_ioremap(&pdev
->dev
, res
->start
, size
);
370 else if (IS_ERR(tdev
->tx_mmio
))
371 tdev
->tx_mmio
= NULL
;
373 /* If specified, second reg entry is Rx MMIO */
374 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
375 size
= resource_size(res
);
376 tdev
->rx_mmio
= devm_ioremap_resource(&pdev
->dev
, res
);
377 if (PTR_ERR(tdev
->rx_mmio
) == -EBUSY
)
378 tdev
->rx_mmio
= devm_ioremap(&pdev
->dev
, res
->start
, size
);
379 else if (IS_ERR(tdev
->rx_mmio
))
380 tdev
->rx_mmio
= tdev
->tx_mmio
;
382 tdev
->tx_channel
= mbox_test_request_channel(pdev
, "tx");
383 tdev
->rx_channel
= mbox_test_request_channel(pdev
, "rx");
385 if (!tdev
->tx_channel
&& !tdev
->rx_channel
)
386 return -EPROBE_DEFER
;
388 /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
389 if (!tdev
->rx_channel
&& (tdev
->rx_mmio
!= tdev
->tx_mmio
))
390 tdev
->rx_channel
= tdev
->tx_channel
;
392 tdev
->dev
= &pdev
->dev
;
393 platform_set_drvdata(pdev
, tdev
);
395 spin_lock_init(&tdev
->lock
);
397 if (tdev
->rx_channel
) {
398 tdev
->rx_buffer
= devm_kzalloc(&pdev
->dev
,
399 MBOX_MAX_MSG_LEN
, GFP_KERNEL
);
400 if (!tdev
->rx_buffer
)
404 ret
= mbox_test_add_debugfs(pdev
, tdev
);
408 init_waitqueue_head(&tdev
->waitq
);
409 dev_info(&pdev
->dev
, "Successfully registered\n");
414 static int mbox_test_remove(struct platform_device
*pdev
)
416 struct mbox_test_device
*tdev
= platform_get_drvdata(pdev
);
418 debugfs_remove_recursive(root_debugfs_dir
);
420 if (tdev
->tx_channel
)
421 mbox_free_channel(tdev
->tx_channel
);
422 if (tdev
->rx_channel
)
423 mbox_free_channel(tdev
->rx_channel
);
428 static const struct of_device_id mbox_test_match
[] = {
429 { .compatible
= "mailbox-test" },
432 MODULE_DEVICE_TABLE(of
, mbox_test_match
);
434 static struct platform_driver mbox_test_driver
= {
436 .name
= "mailbox_test",
437 .of_match_table
= mbox_test_match
,
439 .probe
= mbox_test_probe
,
440 .remove
= mbox_test_remove
,
442 module_platform_driver(mbox_test_driver
);
444 MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
445 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
446 MODULE_LICENSE("GPL v2");