staging: pi433: enforce tx_cfg to be set before any message can be sent
authorPaulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com>
Fri, 14 Jan 2022 22:16:43 +0000 (11:16 +1300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 26 Jan 2022 12:01:48 +0000 (13:01 +0100)
this driver relies on exposing a char device to userspace to tx
messages. Every message can be sent using different trasmitter settings
such so the tx_cfg must be written before sending any messages.
Failing to do so will cause the message to fail silently depending on
printk/dynamic_debug settings which makes it hard to troubleshoot.

This patch add a control variable that will get initialized once tx_cfg
is set for the fd used when interacting with the char device.

Signed-off-by: Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com>
Link: https://lore.kernel.org/r/20220114221643.GA7843@mail.google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/pi433/pi433_if.c

index 523a1bca0c5888f968aef2a30c9a3fe38aa73d74..17ff51f6a9dafee8105ab64fdccb1ff9273b90d2 100644 (file)
@@ -108,6 +108,9 @@ struct pi433_device {
 struct pi433_instance {
        struct pi433_device     *device;
        struct pi433_tx_cfg     tx_cfg;
+
+       /* control flags */
+       bool                    tx_cfg_initialized;
 };
 
 /*-------------------------------------------------------------------------*/
@@ -823,6 +826,16 @@ pi433_write(struct file *filp, const char __user *buf,
        if (count > MAX_MSG_SIZE)
                return -EMSGSIZE;
 
+       /*
+        * check if tx_cfg has been initialized otherwise we won't be able to
+        * config the RF trasmitter correctly due to invalid settings
+        */
+       if (!instance->tx_cfg_initialized) {
+               dev_notice_once(device->dev,
+                               "write: failed due to unconfigured tx_cfg (see PI433_IOC_WR_TX_CFG)");
+               return -EINVAL;
+       }
+
        /*
         * write the following sequence into fifo:
         * - tx_cfg
@@ -897,6 +910,7 @@ pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
                        return -EFAULT;
                mutex_lock(&device->tx_fifo_lock);
                memcpy(&instance->tx_cfg, &tx_cfg, sizeof(struct pi433_tx_cfg));
+               instance->tx_cfg_initialized = true;
                mutex_unlock(&device->tx_fifo_lock);
                break;
        case PI433_IOC_RD_RX_CFG:
@@ -949,8 +963,6 @@ static int pi433_open(struct inode *inode, struct file *filp)
 
        /* setup instance data*/
        instance->device = device;
-       instance->tx_cfg.bit_rate = 4711;
-       // TODO: fill instance->tx_cfg;
 
        /* instance data as context */
        filp->private_data = instance;