GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / mailbox / mailbox-test.c
1 /*
2  * Copyright (C) 2015 ST Microelectronics
3  *
4  * Author: Lee Jones <lee.jones@linaro.org>
5  *
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.
10  */
11
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/mutex.h>
20 #include <linux/of.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>
26
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))
33
34 static bool mbox_data_ready;
35 static struct dentry *root_debugfs_dir;
36
37 struct mbox_test_device {
38         struct device           *dev;
39         void __iomem            *tx_mmio;
40         void __iomem            *rx_mmio;
41         struct mbox_chan        *tx_channel;
42         struct mbox_chan        *rx_channel;
43         char                    *rx_buffer;
44         char                    *signal;
45         char                    *message;
46         spinlock_t              lock;
47         struct mutex            mutex;
48         wait_queue_head_t       waitq;
49         struct fasync_struct    *async_queue;
50 };
51
52 static ssize_t mbox_test_signal_write(struct file *filp,
53                                        const char __user *userbuf,
54                                        size_t count, loff_t *ppos)
55 {
56         struct mbox_test_device *tdev = filp->private_data;
57
58         if (!tdev->tx_channel) {
59                 dev_err(tdev->dev, "Channel cannot do Tx\n");
60                 return -EINVAL;
61         }
62
63         if (count > MBOX_MAX_SIG_LEN) {
64                 dev_err(tdev->dev,
65                         "Signal length %zd greater than max allowed %d\n",
66                         count, MBOX_MAX_SIG_LEN);
67                 return -EINVAL;
68         }
69
70         /* Only allocate memory if we need to */
71         if (!tdev->signal) {
72                 tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
73                 if (!tdev->signal)
74                         return -ENOMEM;
75         }
76
77         if (copy_from_user(tdev->signal, userbuf, count)) {
78                 kfree(tdev->signal);
79                 tdev->signal = NULL;
80                 return -EFAULT;
81         }
82
83         return count;
84 }
85
86 static const struct file_operations mbox_test_signal_ops = {
87         .write  = mbox_test_signal_write,
88         .open   = simple_open,
89         .llseek = generic_file_llseek,
90 };
91
92 static int mbox_test_message_fasync(int fd, struct file *filp, int on)
93 {
94         struct mbox_test_device *tdev = filp->private_data;
95
96         return fasync_helper(fd, filp, on, &tdev->async_queue);
97 }
98
99 static ssize_t mbox_test_message_write(struct file *filp,
100                                        const char __user *userbuf,
101                                        size_t count, loff_t *ppos)
102 {
103         struct mbox_test_device *tdev = filp->private_data;
104         char *message;
105         void *data;
106         int ret;
107
108         if (!tdev->tx_channel) {
109                 dev_err(tdev->dev, "Channel cannot do Tx\n");
110                 return -EINVAL;
111         }
112
113         if (count > MBOX_MAX_MSG_LEN) {
114                 dev_err(tdev->dev,
115                         "Message length %zd greater than max allowed %d\n",
116                         count, MBOX_MAX_MSG_LEN);
117                 return -EINVAL;
118         }
119
120         message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
121         if (!message)
122                 return -ENOMEM;
123
124         mutex_lock(&tdev->mutex);
125
126         tdev->message = message;
127         ret = copy_from_user(tdev->message, userbuf, count);
128         if (ret) {
129                 ret = -EFAULT;
130                 goto out;
131         }
132
133         /*
134          * A separate signal is only of use if there is
135          * MMIO to subsequently pass the message through
136          */
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);
140
141                 data = tdev->signal;
142         } else
143                 data = tdev->message;
144
145         print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
146                              tdev->message, MBOX_MAX_MSG_LEN);
147
148         ret = mbox_send_message(tdev->tx_channel, data);
149         if (ret < 0)
150                 dev_err(tdev->dev, "Failed to send message via mailbox\n");
151
152 out:
153         kfree(tdev->signal);
154         kfree(tdev->message);
155         tdev->signal = NULL;
156
157         mutex_unlock(&tdev->mutex);
158
159         return ret < 0 ? ret : count;
160 }
161
162 static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
163 {
164         bool data_ready;
165         unsigned long flags;
166
167         spin_lock_irqsave(&tdev->lock, flags);
168         data_ready = mbox_data_ready;
169         spin_unlock_irqrestore(&tdev->lock, flags);
170
171         return data_ready;
172 }
173
174 static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
175                                       size_t count, loff_t *ppos)
176 {
177         struct mbox_test_device *tdev = filp->private_data;
178         unsigned long flags;
179         char *touser, *ptr;
180         int l = 0;
181         int ret;
182
183         DECLARE_WAITQUEUE(wait, current);
184
185         touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
186         if (!touser)
187                 return -ENOMEM;
188
189         if (!tdev->rx_channel) {
190                 ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
191                 ret = simple_read_from_buffer(userbuf, count, ppos,
192                                               touser, ret);
193                 goto kfree_err;
194         }
195
196         add_wait_queue(&tdev->waitq, &wait);
197
198         do {
199                 __set_current_state(TASK_INTERRUPTIBLE);
200
201                 if (mbox_test_message_data_ready(tdev))
202                         break;
203
204                 if (filp->f_flags & O_NONBLOCK) {
205                         ret = -EAGAIN;
206                         goto waitq_err;
207                 }
208
209                 if (signal_pending(current)) {
210                         ret = -ERESTARTSYS;
211                         goto waitq_err;
212                 }
213                 schedule();
214
215         } while (1);
216
217         spin_lock_irqsave(&tdev->lock, flags);
218
219         ptr = tdev->rx_buffer;
220         while (l < MBOX_HEXDUMP_MAX_LEN) {
221                 hex_dump_to_buffer(ptr,
222                                    MBOX_BYTES_PER_LINE,
223                                    MBOX_BYTES_PER_LINE, 1, touser + l,
224                                    MBOX_HEXDUMP_LINE_LEN, true);
225
226                 ptr += MBOX_BYTES_PER_LINE;
227                 l += MBOX_HEXDUMP_LINE_LEN;
228                 *(touser + (l - 1)) = '\n';
229         }
230         *(touser + l) = '\0';
231
232         memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
233         mbox_data_ready = false;
234
235         spin_unlock_irqrestore(&tdev->lock, flags);
236
237         ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
238 waitq_err:
239         __set_current_state(TASK_RUNNING);
240         remove_wait_queue(&tdev->waitq, &wait);
241 kfree_err:
242         kfree(touser);
243         return ret;
244 }
245
246 static unsigned int
247 mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait)
248 {
249         struct mbox_test_device *tdev = filp->private_data;
250
251         poll_wait(filp, &tdev->waitq, wait);
252
253         if (mbox_test_message_data_ready(tdev))
254                 return POLLIN | POLLRDNORM;
255         return 0;
256 }
257
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,
263         .open   = simple_open,
264         .llseek = generic_file_llseek,
265 };
266
267 static int mbox_test_add_debugfs(struct platform_device *pdev,
268                                  struct mbox_test_device *tdev)
269 {
270         if (!debugfs_initialized())
271                 return 0;
272
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");
276                 return -EINVAL;
277         }
278
279         debugfs_create_file("message", 0600, root_debugfs_dir,
280                             tdev, &mbox_test_message_ops);
281
282         debugfs_create_file("signal", 0200, root_debugfs_dir,
283                             tdev, &mbox_test_signal_ops);
284
285         return 0;
286 }
287
288 static void mbox_test_receive_message(struct mbox_client *client, void *message)
289 {
290         struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
291         unsigned long flags;
292
293         spin_lock_irqsave(&tdev->lock, flags);
294         if (tdev->rx_mmio) {
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);
302         }
303         mbox_data_ready = true;
304         spin_unlock_irqrestore(&tdev->lock, flags);
305
306         wake_up_interruptible(&tdev->waitq);
307
308         kill_fasync(&tdev->async_queue, SIGIO, POLL_IN);
309 }
310
311 static void mbox_test_prepare_message(struct mbox_client *client, void *message)
312 {
313         struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
314
315         if (tdev->tx_mmio) {
316                 if (tdev->signal)
317                         memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
318                 else
319                         memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
320         }
321 }
322
323 static void mbox_test_message_sent(struct mbox_client *client,
324                                    void *message, int r)
325 {
326         if (r)
327                 dev_warn(client->dev,
328                          "Client: Message could not be sent: %d\n", r);
329         else
330                 dev_info(client->dev,
331                          "Client: Message sent\n");
332 }
333
334 static struct mbox_chan *
335 mbox_test_request_channel(struct platform_device *pdev, const char *name)
336 {
337         struct mbox_client *client;
338         struct mbox_chan *channel;
339
340         client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
341         if (!client)
342                 return ERR_PTR(-ENOMEM);
343
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;
351
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);
355                 return NULL;
356         }
357
358         return channel;
359 }
360
361 static int mbox_test_probe(struct platform_device *pdev)
362 {
363         struct mbox_test_device *tdev;
364         struct resource *res;
365         resource_size_t size;
366         int ret;
367
368         tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
369         if (!tdev)
370                 return -ENOMEM;
371
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;
381         }
382
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;
391         }
392
393         tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
394         tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
395
396         if (!tdev->tx_channel && !tdev->rx_channel)
397                 return -EPROBE_DEFER;
398
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;
402
403         tdev->dev = &pdev->dev;
404         platform_set_drvdata(pdev, tdev);
405
406         spin_lock_init(&tdev->lock);
407         mutex_init(&tdev->mutex);
408
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)
413                         return -ENOMEM;
414         }
415
416         ret = mbox_test_add_debugfs(pdev, tdev);
417         if (ret)
418                 return ret;
419
420         init_waitqueue_head(&tdev->waitq);
421         dev_info(&pdev->dev, "Successfully registered\n");
422
423         return 0;
424 }
425
426 static int mbox_test_remove(struct platform_device *pdev)
427 {
428         struct mbox_test_device *tdev = platform_get_drvdata(pdev);
429
430         debugfs_remove_recursive(root_debugfs_dir);
431
432         if (tdev->tx_channel)
433                 mbox_free_channel(tdev->tx_channel);
434         if (tdev->rx_channel)
435                 mbox_free_channel(tdev->rx_channel);
436
437         return 0;
438 }
439
440 static const struct of_device_id mbox_test_match[] = {
441         { .compatible = "mailbox-test" },
442         {},
443 };
444 MODULE_DEVICE_TABLE(of, mbox_test_match);
445
446 static struct platform_driver mbox_test_driver = {
447         .driver = {
448                 .name = "mailbox_test",
449                 .of_match_table = mbox_test_match,
450         },
451         .probe  = mbox_test_probe,
452         .remove = mbox_test_remove,
453 };
454 module_platform_driver(mbox_test_driver);
455
456 MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
457 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
458 MODULE_LICENSE("GPL v2");