Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) 2015 ST Microelectronics
0004  *
0005  * Author: Lee Jones <lee.jones@linaro.org>
0006  */
0007 
0008 #include <linux/debugfs.h>
0009 #include <linux/err.h>
0010 #include <linux/fs.h>
0011 #include <linux/io.h>
0012 #include <linux/kernel.h>
0013 #include <linux/mailbox_client.h>
0014 #include <linux/module.h>
0015 #include <linux/of.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/poll.h>
0018 #include <linux/slab.h>
0019 #include <linux/uaccess.h>
0020 #include <linux/sched/signal.h>
0021 
0022 #define MBOX_MAX_SIG_LEN    8
0023 #define MBOX_MAX_MSG_LEN    128
0024 #define MBOX_BYTES_PER_LINE 16
0025 #define MBOX_HEXDUMP_LINE_LEN   ((MBOX_BYTES_PER_LINE * 4) + 2)
0026 #define MBOX_HEXDUMP_MAX_LEN    (MBOX_HEXDUMP_LINE_LEN *        \
0027                  (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
0028 
0029 static bool mbox_data_ready;
0030 
0031 struct mbox_test_device {
0032     struct device       *dev;
0033     void __iomem        *tx_mmio;
0034     void __iomem        *rx_mmio;
0035     struct mbox_chan    *tx_channel;
0036     struct mbox_chan    *rx_channel;
0037     char            *rx_buffer;
0038     char            *signal;
0039     char            *message;
0040     spinlock_t      lock;
0041     wait_queue_head_t   waitq;
0042     struct fasync_struct    *async_queue;
0043     struct dentry       *root_debugfs_dir;
0044 };
0045 
0046 static ssize_t mbox_test_signal_write(struct file *filp,
0047                        const char __user *userbuf,
0048                        size_t count, loff_t *ppos)
0049 {
0050     struct mbox_test_device *tdev = filp->private_data;
0051 
0052     if (!tdev->tx_channel) {
0053         dev_err(tdev->dev, "Channel cannot do Tx\n");
0054         return -EINVAL;
0055     }
0056 
0057     if (count > MBOX_MAX_SIG_LEN) {
0058         dev_err(tdev->dev,
0059             "Signal length %zd greater than max allowed %d\n",
0060             count, MBOX_MAX_SIG_LEN);
0061         return -EINVAL;
0062     }
0063 
0064     /* Only allocate memory if we need to */
0065     if (!tdev->signal) {
0066         tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
0067         if (!tdev->signal)
0068             return -ENOMEM;
0069     }
0070 
0071     if (copy_from_user(tdev->signal, userbuf, count)) {
0072         kfree(tdev->signal);
0073         tdev->signal = NULL;
0074         return -EFAULT;
0075     }
0076 
0077     return count;
0078 }
0079 
0080 static const struct file_operations mbox_test_signal_ops = {
0081     .write  = mbox_test_signal_write,
0082     .open   = simple_open,
0083     .llseek = generic_file_llseek,
0084 };
0085 
0086 static int mbox_test_message_fasync(int fd, struct file *filp, int on)
0087 {
0088     struct mbox_test_device *tdev = filp->private_data;
0089 
0090     return fasync_helper(fd, filp, on, &tdev->async_queue);
0091 }
0092 
0093 static ssize_t mbox_test_message_write(struct file *filp,
0094                        const char __user *userbuf,
0095                        size_t count, loff_t *ppos)
0096 {
0097     struct mbox_test_device *tdev = filp->private_data;
0098     void *data;
0099     int ret;
0100 
0101     if (!tdev->tx_channel) {
0102         dev_err(tdev->dev, "Channel cannot do Tx\n");
0103         return -EINVAL;
0104     }
0105 
0106     if (count > MBOX_MAX_MSG_LEN) {
0107         dev_err(tdev->dev,
0108             "Message length %zd greater than max allowed %d\n",
0109             count, MBOX_MAX_MSG_LEN);
0110         return -EINVAL;
0111     }
0112 
0113     tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
0114     if (!tdev->message)
0115         return -ENOMEM;
0116 
0117     ret = copy_from_user(tdev->message, userbuf, count);
0118     if (ret) {
0119         ret = -EFAULT;
0120         goto out;
0121     }
0122 
0123     /*
0124      * A separate signal is only of use if there is
0125      * MMIO to subsequently pass the message through
0126      */
0127     if (tdev->tx_mmio && tdev->signal) {
0128         print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
0129                      tdev->signal, MBOX_MAX_SIG_LEN);
0130 
0131         data = tdev->signal;
0132     } else
0133         data = tdev->message;
0134 
0135     print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
0136                  tdev->message, MBOX_MAX_MSG_LEN);
0137 
0138     ret = mbox_send_message(tdev->tx_channel, data);
0139     if (ret < 0)
0140         dev_err(tdev->dev, "Failed to send message via mailbox\n");
0141 
0142 out:
0143     kfree(tdev->signal);
0144     kfree(tdev->message);
0145     tdev->signal = NULL;
0146 
0147     return ret < 0 ? ret : count;
0148 }
0149 
0150 static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
0151 {
0152     bool data_ready;
0153     unsigned long flags;
0154 
0155     spin_lock_irqsave(&tdev->lock, flags);
0156     data_ready = mbox_data_ready;
0157     spin_unlock_irqrestore(&tdev->lock, flags);
0158 
0159     return data_ready;
0160 }
0161 
0162 static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
0163                       size_t count, loff_t *ppos)
0164 {
0165     struct mbox_test_device *tdev = filp->private_data;
0166     unsigned long flags;
0167     char *touser, *ptr;
0168     int l = 0;
0169     int ret;
0170 
0171     DECLARE_WAITQUEUE(wait, current);
0172 
0173     touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
0174     if (!touser)
0175         return -ENOMEM;
0176 
0177     if (!tdev->rx_channel) {
0178         ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
0179         ret = simple_read_from_buffer(userbuf, count, ppos,
0180                           touser, ret);
0181         goto kfree_err;
0182     }
0183 
0184     add_wait_queue(&tdev->waitq, &wait);
0185 
0186     do {
0187         __set_current_state(TASK_INTERRUPTIBLE);
0188 
0189         if (mbox_test_message_data_ready(tdev))
0190             break;
0191 
0192         if (filp->f_flags & O_NONBLOCK) {
0193             ret = -EAGAIN;
0194             goto waitq_err;
0195         }
0196 
0197         if (signal_pending(current)) {
0198             ret = -ERESTARTSYS;
0199             goto waitq_err;
0200         }
0201         schedule();
0202 
0203     } while (1);
0204 
0205     spin_lock_irqsave(&tdev->lock, flags);
0206 
0207     ptr = tdev->rx_buffer;
0208     while (l < MBOX_HEXDUMP_MAX_LEN) {
0209         hex_dump_to_buffer(ptr,
0210                    MBOX_BYTES_PER_LINE,
0211                    MBOX_BYTES_PER_LINE, 1, touser + l,
0212                    MBOX_HEXDUMP_LINE_LEN, true);
0213 
0214         ptr += MBOX_BYTES_PER_LINE;
0215         l += MBOX_HEXDUMP_LINE_LEN;
0216         *(touser + (l - 1)) = '\n';
0217     }
0218     *(touser + l) = '\0';
0219 
0220     memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
0221     mbox_data_ready = false;
0222 
0223     spin_unlock_irqrestore(&tdev->lock, flags);
0224 
0225     ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
0226 waitq_err:
0227     __set_current_state(TASK_RUNNING);
0228     remove_wait_queue(&tdev->waitq, &wait);
0229 kfree_err:
0230     kfree(touser);
0231     return ret;
0232 }
0233 
0234 static __poll_t
0235 mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait)
0236 {
0237     struct mbox_test_device *tdev = filp->private_data;
0238 
0239     poll_wait(filp, &tdev->waitq, wait);
0240 
0241     if (mbox_test_message_data_ready(tdev))
0242         return EPOLLIN | EPOLLRDNORM;
0243     return 0;
0244 }
0245 
0246 static const struct file_operations mbox_test_message_ops = {
0247     .write  = mbox_test_message_write,
0248     .read   = mbox_test_message_read,
0249     .fasync = mbox_test_message_fasync,
0250     .poll   = mbox_test_message_poll,
0251     .open   = simple_open,
0252     .llseek = generic_file_llseek,
0253 };
0254 
0255 static int mbox_test_add_debugfs(struct platform_device *pdev,
0256                  struct mbox_test_device *tdev)
0257 {
0258     if (!debugfs_initialized())
0259         return 0;
0260 
0261     tdev->root_debugfs_dir = debugfs_create_dir(dev_name(&pdev->dev), NULL);
0262     if (!tdev->root_debugfs_dir) {
0263         dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
0264         return -EINVAL;
0265     }
0266 
0267     debugfs_create_file("message", 0600, tdev->root_debugfs_dir,
0268                 tdev, &mbox_test_message_ops);
0269 
0270     debugfs_create_file("signal", 0200, tdev->root_debugfs_dir,
0271                 tdev, &mbox_test_signal_ops);
0272 
0273     return 0;
0274 }
0275 
0276 static void mbox_test_receive_message(struct mbox_client *client, void *message)
0277 {
0278     struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
0279     unsigned long flags;
0280 
0281     spin_lock_irqsave(&tdev->lock, flags);
0282     if (tdev->rx_mmio) {
0283         memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN);
0284         print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS,
0285                      tdev->rx_buffer, MBOX_MAX_MSG_LEN);
0286     } else if (message) {
0287         print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS,
0288                      message, MBOX_MAX_MSG_LEN);
0289         memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
0290     }
0291     mbox_data_ready = true;
0292     spin_unlock_irqrestore(&tdev->lock, flags);
0293 
0294     wake_up_interruptible(&tdev->waitq);
0295 
0296     kill_fasync(&tdev->async_queue, SIGIO, POLL_IN);
0297 }
0298 
0299 static void mbox_test_prepare_message(struct mbox_client *client, void *message)
0300 {
0301     struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
0302 
0303     if (tdev->tx_mmio) {
0304         if (tdev->signal)
0305             memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
0306         else
0307             memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
0308     }
0309 }
0310 
0311 static void mbox_test_message_sent(struct mbox_client *client,
0312                    void *message, int r)
0313 {
0314     if (r)
0315         dev_warn(client->dev,
0316              "Client: Message could not be sent: %d\n", r);
0317     else
0318         dev_info(client->dev,
0319              "Client: Message sent\n");
0320 }
0321 
0322 static struct mbox_chan *
0323 mbox_test_request_channel(struct platform_device *pdev, const char *name)
0324 {
0325     struct mbox_client *client;
0326     struct mbox_chan *channel;
0327 
0328     client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
0329     if (!client)
0330         return ERR_PTR(-ENOMEM);
0331 
0332     client->dev     = &pdev->dev;
0333     client->rx_callback = mbox_test_receive_message;
0334     client->tx_prepare  = mbox_test_prepare_message;
0335     client->tx_done     = mbox_test_message_sent;
0336     client->tx_block    = true;
0337     client->knows_txdone    = false;
0338     client->tx_tout     = 500;
0339 
0340     channel = mbox_request_channel_byname(client, name);
0341     if (IS_ERR(channel)) {
0342         dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
0343         return NULL;
0344     }
0345 
0346     return channel;
0347 }
0348 
0349 static int mbox_test_probe(struct platform_device *pdev)
0350 {
0351     struct mbox_test_device *tdev;
0352     struct resource *res;
0353     resource_size_t size;
0354     int ret;
0355 
0356     tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
0357     if (!tdev)
0358         return -ENOMEM;
0359 
0360     /* It's okay for MMIO to be NULL */
0361     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0362     tdev->tx_mmio = devm_ioremap_resource(&pdev->dev, res);
0363     if (PTR_ERR(tdev->tx_mmio) == -EBUSY) {
0364         /* if reserved area in SRAM, try just ioremap */
0365         size = resource_size(res);
0366         tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size);
0367     } else if (IS_ERR(tdev->tx_mmio)) {
0368         tdev->tx_mmio = NULL;
0369     }
0370 
0371     /* If specified, second reg entry is Rx MMIO */
0372     res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
0373     tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res);
0374     if (PTR_ERR(tdev->rx_mmio) == -EBUSY) {
0375         size = resource_size(res);
0376         tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size);
0377     } else if (IS_ERR(tdev->rx_mmio)) {
0378         tdev->rx_mmio = tdev->tx_mmio;
0379     }
0380 
0381     tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
0382     tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
0383 
0384     if (!tdev->tx_channel && !tdev->rx_channel)
0385         return -EPROBE_DEFER;
0386 
0387     /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
0388     if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio))
0389         tdev->rx_channel = tdev->tx_channel;
0390 
0391     tdev->dev = &pdev->dev;
0392     platform_set_drvdata(pdev, tdev);
0393 
0394     spin_lock_init(&tdev->lock);
0395 
0396     if (tdev->rx_channel) {
0397         tdev->rx_buffer = devm_kzalloc(&pdev->dev,
0398                            MBOX_MAX_MSG_LEN, GFP_KERNEL);
0399         if (!tdev->rx_buffer)
0400             return -ENOMEM;
0401     }
0402 
0403     ret = mbox_test_add_debugfs(pdev, tdev);
0404     if (ret)
0405         return ret;
0406 
0407     init_waitqueue_head(&tdev->waitq);
0408     dev_info(&pdev->dev, "Successfully registered\n");
0409 
0410     return 0;
0411 }
0412 
0413 static int mbox_test_remove(struct platform_device *pdev)
0414 {
0415     struct mbox_test_device *tdev = platform_get_drvdata(pdev);
0416 
0417     debugfs_remove_recursive(tdev->root_debugfs_dir);
0418 
0419     if (tdev->tx_channel)
0420         mbox_free_channel(tdev->tx_channel);
0421     if (tdev->rx_channel)
0422         mbox_free_channel(tdev->rx_channel);
0423 
0424     return 0;
0425 }
0426 
0427 static const struct of_device_id mbox_test_match[] = {
0428     { .compatible = "mailbox-test" },
0429     {},
0430 };
0431 MODULE_DEVICE_TABLE(of, mbox_test_match);
0432 
0433 static struct platform_driver mbox_test_driver = {
0434     .driver = {
0435         .name = "mailbox_test",
0436         .of_match_table = mbox_test_match,
0437     },
0438     .probe  = mbox_test_probe,
0439     .remove = mbox_test_remove,
0440 };
0441 module_platform_driver(mbox_test_driver);
0442 
0443 MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
0444 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
0445 MODULE_LICENSE("GPL v2");