usb: typec: mux: fix static inline syntax error
[platform/kernel/linux-starfive.git] / drivers / mailbox / mailbox-test.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015 ST Microelectronics
4  *
5  * Author: Lee Jones <lee.jones@linaro.org>
6  */
7
8 #include <linux/debugfs.h>
9 #include <linux/err.h>
10 #include <linux/fs.h>
11 #include <linux/io.h>
12 #include <linux/kernel.h>
13 #include <linux/mailbox_client.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/of.h>
17 #include <linux/platform_device.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/uaccess.h>
22 #include <linux/sched/signal.h>
23
24 #define MBOX_MAX_SIG_LEN        8
25 #define MBOX_MAX_MSG_LEN        128
26 #define MBOX_BYTES_PER_LINE     16
27 #define MBOX_HEXDUMP_LINE_LEN   ((MBOX_BYTES_PER_LINE * 4) + 2)
28 #define MBOX_HEXDUMP_MAX_LEN    (MBOX_HEXDUMP_LINE_LEN *                \
29                                  (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
30
31 static bool mbox_data_ready;
32
33 struct mbox_test_device {
34         struct device           *dev;
35         void __iomem            *tx_mmio;
36         void __iomem            *rx_mmio;
37         struct mbox_chan        *tx_channel;
38         struct mbox_chan        *rx_channel;
39         char                    *rx_buffer;
40         char                    *signal;
41         char                    *message;
42         spinlock_t              lock;
43         struct mutex            mutex;
44         wait_queue_head_t       waitq;
45         struct fasync_struct    *async_queue;
46         struct dentry           *root_debugfs_dir;
47 };
48
49 static ssize_t mbox_test_signal_write(struct file *filp,
50                                        const char __user *userbuf,
51                                        size_t count, loff_t *ppos)
52 {
53         struct mbox_test_device *tdev = filp->private_data;
54
55         if (!tdev->tx_channel) {
56                 dev_err(tdev->dev, "Channel cannot do Tx\n");
57                 return -EINVAL;
58         }
59
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;
65         }
66
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;
72         }
73
74         if (copy_from_user(tdev->signal, userbuf, count)) {
75                 kfree(tdev->signal);
76                 tdev->signal = NULL;
77                 return -EFAULT;
78         }
79
80         return count;
81 }
82
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,
87 };
88
89 static int mbox_test_message_fasync(int fd, struct file *filp, int on)
90 {
91         struct mbox_test_device *tdev = filp->private_data;
92
93         return fasync_helper(fd, filp, on, &tdev->async_queue);
94 }
95
96 static ssize_t mbox_test_message_write(struct file *filp,
97                                        const char __user *userbuf,
98                                        size_t count, loff_t *ppos)
99 {
100         struct mbox_test_device *tdev = filp->private_data;
101         void *data;
102         int ret;
103
104         if (!tdev->tx_channel) {
105                 dev_err(tdev->dev, "Channel cannot do Tx\n");
106                 return -EINVAL;
107         }
108
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;
114         }
115
116         mutex_lock(&tdev->mutex);
117
118         tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
119         if (!tdev->message)
120                 return -ENOMEM;
121
122         ret = copy_from_user(tdev->message, userbuf, count);
123         if (ret) {
124                 ret = -EFAULT;
125                 goto out;
126         }
127
128         /*
129          * A separate signal is only of use if there is
130          * MMIO to subsequently pass the message through
131          */
132         if (tdev->tx_mmio && tdev->signal) {
133                 print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
134                                      tdev->signal, MBOX_MAX_SIG_LEN);
135
136                 data = tdev->signal;
137         } else
138                 data = tdev->message;
139
140         print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
141                              tdev->message, MBOX_MAX_MSG_LEN);
142
143         ret = mbox_send_message(tdev->tx_channel, data);
144         if (ret < 0)
145                 dev_err(tdev->dev, "Failed to send message via mailbox\n");
146
147 out:
148         kfree(tdev->signal);
149         kfree(tdev->message);
150         tdev->signal = NULL;
151
152         mutex_unlock(&tdev->mutex);
153
154         return ret < 0 ? ret : count;
155 }
156
157 static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
158 {
159         bool data_ready;
160         unsigned long flags;
161
162         spin_lock_irqsave(&tdev->lock, flags);
163         data_ready = mbox_data_ready;
164         spin_unlock_irqrestore(&tdev->lock, flags);
165
166         return data_ready;
167 }
168
169 static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
170                                       size_t count, loff_t *ppos)
171 {
172         struct mbox_test_device *tdev = filp->private_data;
173         unsigned long flags;
174         char *touser, *ptr;
175         int l = 0;
176         int ret;
177
178         DECLARE_WAITQUEUE(wait, current);
179
180         touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
181         if (!touser)
182                 return -ENOMEM;
183
184         if (!tdev->rx_channel) {
185                 ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
186                 ret = simple_read_from_buffer(userbuf, count, ppos,
187                                               touser, ret);
188                 goto kfree_err;
189         }
190
191         add_wait_queue(&tdev->waitq, &wait);
192
193         do {
194                 __set_current_state(TASK_INTERRUPTIBLE);
195
196                 if (mbox_test_message_data_ready(tdev))
197                         break;
198
199                 if (filp->f_flags & O_NONBLOCK) {
200                         ret = -EAGAIN;
201                         goto waitq_err;
202                 }
203
204                 if (signal_pending(current)) {
205                         ret = -ERESTARTSYS;
206                         goto waitq_err;
207                 }
208                 schedule();
209
210         } while (1);
211
212         spin_lock_irqsave(&tdev->lock, flags);
213
214         ptr = tdev->rx_buffer;
215         while (l < MBOX_HEXDUMP_MAX_LEN) {
216                 hex_dump_to_buffer(ptr,
217                                    MBOX_BYTES_PER_LINE,
218                                    MBOX_BYTES_PER_LINE, 1, touser + l,
219                                    MBOX_HEXDUMP_LINE_LEN, true);
220
221                 ptr += MBOX_BYTES_PER_LINE;
222                 l += MBOX_HEXDUMP_LINE_LEN;
223                 *(touser + (l - 1)) = '\n';
224         }
225         *(touser + l) = '\0';
226
227         memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
228         mbox_data_ready = false;
229
230         spin_unlock_irqrestore(&tdev->lock, flags);
231
232         ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
233 waitq_err:
234         __set_current_state(TASK_RUNNING);
235         remove_wait_queue(&tdev->waitq, &wait);
236 kfree_err:
237         kfree(touser);
238         return ret;
239 }
240
241 static __poll_t
242 mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait)
243 {
244         struct mbox_test_device *tdev = filp->private_data;
245
246         poll_wait(filp, &tdev->waitq, wait);
247
248         if (mbox_test_message_data_ready(tdev))
249                 return EPOLLIN | EPOLLRDNORM;
250         return 0;
251 }
252
253 static const struct file_operations mbox_test_message_ops = {
254         .write  = mbox_test_message_write,
255         .read   = mbox_test_message_read,
256         .fasync = mbox_test_message_fasync,
257         .poll   = mbox_test_message_poll,
258         .open   = simple_open,
259         .llseek = generic_file_llseek,
260 };
261
262 static int mbox_test_add_debugfs(struct platform_device *pdev,
263                                  struct mbox_test_device *tdev)
264 {
265         if (!debugfs_initialized())
266                 return 0;
267
268         tdev->root_debugfs_dir = debugfs_create_dir(dev_name(&pdev->dev), NULL);
269         if (!tdev->root_debugfs_dir) {
270                 dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
271                 return -EINVAL;
272         }
273
274         debugfs_create_file("message", 0600, tdev->root_debugfs_dir,
275                             tdev, &mbox_test_message_ops);
276
277         debugfs_create_file("signal", 0200, tdev->root_debugfs_dir,
278                             tdev, &mbox_test_signal_ops);
279
280         return 0;
281 }
282
283 static void mbox_test_receive_message(struct mbox_client *client, void *message)
284 {
285         struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
286         unsigned long flags;
287
288         spin_lock_irqsave(&tdev->lock, flags);
289         if (tdev->rx_mmio) {
290                 memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN);
291                 print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS,
292                                      tdev->rx_buffer, MBOX_MAX_MSG_LEN);
293         } else if (message) {
294                 print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS,
295                                      message, MBOX_MAX_MSG_LEN);
296                 memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
297         }
298         mbox_data_ready = true;
299         spin_unlock_irqrestore(&tdev->lock, flags);
300
301         wake_up_interruptible(&tdev->waitq);
302
303         kill_fasync(&tdev->async_queue, SIGIO, POLL_IN);
304 }
305
306 static void mbox_test_prepare_message(struct mbox_client *client, void *message)
307 {
308         struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
309
310         if (tdev->tx_mmio) {
311                 if (tdev->signal)
312                         memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
313                 else
314                         memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
315         }
316 }
317
318 static void mbox_test_message_sent(struct mbox_client *client,
319                                    void *message, int r)
320 {
321         if (r)
322                 dev_warn(client->dev,
323                          "Client: Message could not be sent: %d\n", r);
324         else
325                 dev_info(client->dev,
326                          "Client: Message sent\n");
327 }
328
329 static struct mbox_chan *
330 mbox_test_request_channel(struct platform_device *pdev, const char *name)
331 {
332         struct mbox_client *client;
333         struct mbox_chan *channel;
334
335         client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
336         if (!client)
337                 return ERR_PTR(-ENOMEM);
338
339         client->dev             = &pdev->dev;
340         client->rx_callback     = mbox_test_receive_message;
341         client->tx_prepare      = mbox_test_prepare_message;
342         client->tx_done         = mbox_test_message_sent;
343         client->tx_block        = true;
344         client->knows_txdone    = false;
345         client->tx_tout         = 500;
346
347         channel = mbox_request_channel_byname(client, name);
348         if (IS_ERR(channel)) {
349                 dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
350                 return NULL;
351         }
352
353         return channel;
354 }
355
356 static int mbox_test_probe(struct platform_device *pdev)
357 {
358         struct mbox_test_device *tdev;
359         struct resource *res;
360         resource_size_t size;
361         int ret;
362
363         tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
364         if (!tdev)
365                 return -ENOMEM;
366
367         /* It's okay for MMIO to be NULL */
368         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
369         tdev->tx_mmio = devm_ioremap_resource(&pdev->dev, res);
370         if (PTR_ERR(tdev->tx_mmio) == -EBUSY) {
371                 /* if reserved area in SRAM, try just ioremap */
372                 size = resource_size(res);
373                 tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size);
374         } else if (IS_ERR(tdev->tx_mmio)) {
375                 tdev->tx_mmio = NULL;
376         }
377
378         /* If specified, second reg entry is Rx MMIO */
379         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
380         tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res);
381         if (PTR_ERR(tdev->rx_mmio) == -EBUSY) {
382                 size = resource_size(res);
383                 tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size);
384         } else if (IS_ERR(tdev->rx_mmio)) {
385                 tdev->rx_mmio = tdev->tx_mmio;
386         }
387
388         tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
389         tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
390
391         if (!tdev->tx_channel && !tdev->rx_channel)
392                 return -EPROBE_DEFER;
393
394         /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
395         if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio))
396                 tdev->rx_channel = tdev->tx_channel;
397
398         tdev->dev = &pdev->dev;
399         platform_set_drvdata(pdev, tdev);
400
401         spin_lock_init(&tdev->lock);
402         mutex_init(&tdev->mutex);
403
404         if (tdev->rx_channel) {
405                 tdev->rx_buffer = devm_kzalloc(&pdev->dev,
406                                                MBOX_MAX_MSG_LEN, GFP_KERNEL);
407                 if (!tdev->rx_buffer)
408                         return -ENOMEM;
409         }
410
411         ret = mbox_test_add_debugfs(pdev, tdev);
412         if (ret)
413                 return ret;
414
415         init_waitqueue_head(&tdev->waitq);
416         dev_info(&pdev->dev, "Successfully registered\n");
417
418         return 0;
419 }
420
421 static int mbox_test_remove(struct platform_device *pdev)
422 {
423         struct mbox_test_device *tdev = platform_get_drvdata(pdev);
424
425         debugfs_remove_recursive(tdev->root_debugfs_dir);
426
427         if (tdev->tx_channel)
428                 mbox_free_channel(tdev->tx_channel);
429         if (tdev->rx_channel)
430                 mbox_free_channel(tdev->rx_channel);
431
432         return 0;
433 }
434
435 static const struct of_device_id mbox_test_match[] = {
436         { .compatible = "mailbox-test" },
437         {},
438 };
439 MODULE_DEVICE_TABLE(of, mbox_test_match);
440
441 static struct platform_driver mbox_test_driver = {
442         .driver = {
443                 .name = "mailbox_test",
444                 .of_match_table = mbox_test_match,
445         },
446         .probe  = mbox_test_probe,
447         .remove = mbox_test_remove,
448 };
449 module_platform_driver(mbox_test_driver);
450
451 MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
452 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
453 MODULE_LICENSE("GPL v2");