staging: ks7010: separate dissimilar checks
[linux/fpc-iii.git] / drivers / mailbox / mailbox-test.c
blob97fb956bb6e04a328b12abdf37eb9db3fb321bb3
1 /*
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>
14 #include <linux/fs.h>
15 #include <linux/io.h>
16 #include <linux/kernel.h>
17 #include <linux/mailbox_client.h>
18 #include <linux/module.h>
19 #include <linux/of.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 {
36 struct device *dev;
37 void __iomem *tx_mmio;
38 void __iomem *rx_mmio;
39 struct mbox_chan *tx_channel;
40 struct mbox_chan *rx_channel;
41 char *rx_buffer;
42 char *signal;
43 char *message;
44 spinlock_t lock;
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");
57 return -EINVAL;
60 if (count > MBOX_MAX_SIG_LEN) {
61 dev_err(tdev->dev,
62 "Signal length %zd greater than max allowed %d\n",
63 count, MBOX_MAX_SIG_LEN);
64 return -EINVAL;
67 /* Only allocate memory if we need to */
68 if (!tdev->signal) {
69 tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
70 if (!tdev->signal)
71 return -ENOMEM;
74 if (copy_from_user(tdev->signal, userbuf, count)) {
75 kfree(tdev->signal);
76 tdev->signal = NULL;
77 return -EFAULT;
80 return count;
83 static const struct file_operations mbox_test_signal_ops = {
84 .write = mbox_test_signal_write,
85 .open = simple_open,
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;
101 void *data;
102 int ret;
104 if (!tdev->tx_channel) {
105 dev_err(tdev->dev, "Channel cannot do Tx\n");
106 return -EINVAL;
109 if (count > MBOX_MAX_MSG_LEN) {
110 dev_err(tdev->dev,
111 "Message length %zd greater than max allowed %d\n",
112 count, MBOX_MAX_MSG_LEN);
113 return -EINVAL;
116 tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
117 if (!tdev->message)
118 return -ENOMEM;
120 ret = copy_from_user(tdev->message, userbuf, count);
121 if (ret) {
122 ret = -EFAULT;
123 goto out;
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);
134 data = tdev->signal;
135 } else
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);
142 if (ret < 0)
143 dev_err(tdev->dev, "Failed to send message via mailbox\n");
145 out:
146 kfree(tdev->signal);
147 kfree(tdev->message);
148 tdev->signal = NULL;
150 return ret < 0 ? ret : count;
153 static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
155 unsigned char data;
156 unsigned long flags;
158 spin_lock_irqsave(&tdev->lock, flags);
159 data = tdev->rx_buffer[0];
160 spin_unlock_irqrestore(&tdev->lock, flags);
162 if (data != '\0')
163 return true;
164 return false;
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;
171 unsigned long flags;
172 char *touser, *ptr;
173 int l = 0;
174 int ret;
176 DECLARE_WAITQUEUE(wait, current);
178 touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
179 if (!touser)
180 return -ENOMEM;
182 if (!tdev->rx_channel) {
183 ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
184 ret = simple_read_from_buffer(userbuf, count, ppos,
185 touser, ret);
186 goto kfree_err;
189 add_wait_queue(&tdev->waitq, &wait);
191 do {
192 __set_current_state(TASK_INTERRUPTIBLE);
194 if (mbox_test_message_data_ready(tdev))
195 break;
197 if (filp->f_flags & O_NONBLOCK) {
198 ret = -EAGAIN;
199 goto waitq_err;
202 if (signal_pending(current)) {
203 ret = -ERESTARTSYS;
204 goto waitq_err;
206 schedule();
208 } while (1);
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,
215 MBOX_BYTES_PER_LINE,
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);
230 waitq_err:
231 __set_current_state(TASK_RUNNING);
232 remove_wait_queue(&tdev->waitq, &wait);
233 kfree_err:
234 kfree(touser);
235 return ret;
238 static unsigned int
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;
247 return 0;
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,
255 .open = simple_open,
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())
263 return 0;
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");
268 return -EINVAL;
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);
277 return 0;
280 static void mbox_test_receive_message(struct mbox_client *client, void *message)
282 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
283 unsigned long flags;
285 spin_lock_irqsave(&tdev->lock, flags);
286 if (tdev->rx_mmio) {
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);
306 if (tdev->tx_mmio) {
307 if (tdev->signal)
308 memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
309 else
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)
317 if (r)
318 dev_warn(client->dev,
319 "Client: Message could not be sent: %d\n", r);
320 else
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);
332 if (!client)
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);
346 return NULL;
349 return channel;
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;
357 int ret;
359 tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
360 if (!tdev)
361 return -ENOMEM;
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)
401 return -ENOMEM;
404 ret = mbox_test_add_debugfs(pdev, tdev);
405 if (ret)
406 return ret;
408 init_waitqueue_head(&tdev->waitq);
409 dev_info(&pdev->dev, "Successfully registered\n");
411 return 0;
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);
425 return 0;
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 = {
435 .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");