linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Yaşar Arabacı" <yasar11732@gmail.com>
To: gregkh@linuxfoundation.org
Cc: paulo.miguel.almeida.rodenas@gmail.com, dan.carpenter@oracle.com,
	alexandre.belloni@bootlin.com, realwakka@gmail.com,
	u.kleine-koenig@pengutronix.de, linux-staging@lists.linux.dev,
	linux-kernel@vger.kernel.org,
	"Yaşar Arabacı" <yasar11732@gmail.com>
Subject: [PATCH] Staging: pi433: Don't use ioctl for per-client configuration
Date: Sun, 15 May 2022 13:47:11 +0300	[thread overview]
Message-ID: <20220515104711.94567-1-yasar11732@gmail.com> (raw)

Currently this driver uses ioctl for reading/writing per-device and
per-client configuration. Per-client configuration can be handled by
usespace and sent to driver with each write() call. Doing so does not
introduce extra overhead because we copy tx config to fifo for each
transmit anyway. This way, we don't have to introduce new ioctl's.

This has not been tested as I don't have access to hardware.

Signed-off-by: Yaşar Arabacı <yasar11732@gmail.com>
---
 drivers/staging/pi433/pi433_if.c | 63 ++++++--------------------------
 drivers/staging/pi433/pi433_if.h |  7 +---
 2 files changed, 13 insertions(+), 57 deletions(-)

diff --git a/drivers/staging/pi433/pi433_if.c b/drivers/staging/pi433/pi433_if.c
index 941aaa7eab2e..07cd9054560a 100644
--- a/drivers/staging/pi433/pi433_if.c
+++ b/drivers/staging/pi433/pi433_if.c
@@ -109,10 +109,6 @@ struct pi433_device {
 
 struct pi433_instance {
 	struct pi433_device	*device;
-	struct pi433_tx_cfg	tx_cfg;
-
-	/* control flags */
-	bool			tx_cfg_initialized;
 };
 
 /*-------------------------------------------------------------------------*/
@@ -574,12 +570,6 @@ static int pi433_tx_thread(void *data)
 		if (kthread_should_stop())
 			return 0;
 
-		/*
-		 * get data from fifo in the following order:
-		 * - tx_cfg
-		 * - size of message
-		 * - message
-		 */
 		retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
 		if (retval != sizeof(tx_cfg)) {
 			dev_dbg(device->dev,
@@ -588,13 +578,7 @@ static int pi433_tx_thread(void *data)
 			continue;
 		}
 
-		retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
-		if (retval != sizeof(size_t)) {
-			dev_dbg(device->dev,
-				"reading msg size from fifo failed: got %d, expected %d\n",
-				retval, (unsigned int)sizeof(size_t));
-			continue;
-		}
+		size = tx_cfg.payload_size;
 
 		/* use fixed message length, if requested */
 		if (tx_cfg.fixed_message_length != 0)
@@ -811,6 +795,7 @@ pi433_write(struct file *filp, const char __user *buf,
 	    size_t count, loff_t *f_pos)
 {
 	struct pi433_instance	*instance;
+	struct pi433_tx_cfg *tx_cfg;
 	struct pi433_device	*device;
 	int                     retval;
 	unsigned int		required, available, copied;
@@ -822,18 +807,16 @@ pi433_write(struct file *filp, const char __user *buf,
 	 * check, whether internal buffer (tx thread) is big enough
 	 * for requested size
 	 */
-	if (count > MAX_MSG_SIZE)
+	if (unlikely(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)\n");
-		return -EINVAL;
-	}
+	if (unlikely(count < sizeof(struct pi433_tx_cfg)))
+		return -EMSGSIZE;
+
+	tx_cfg = (struct pi433_tx_cfg *)buf;
+
+	if (unlikely(count != sizeof(struct pi433_tx_cfg)) + tx_cfg->payload_size)
+		return -EMSGSIZE;
 
 	/*
 	 * write the following sequence into fifo:
@@ -843,24 +826,14 @@ pi433_write(struct file *filp, const char __user *buf,
 	 */
 	mutex_lock(&device->tx_fifo_lock);
 
-	required = sizeof(instance->tx_cfg) + sizeof(size_t) + count;
 	available = kfifo_avail(&device->tx_fifo);
-	if (required > available) {
+	if (count > available) {
 		dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available\n",
 			required, available);
 		mutex_unlock(&device->tx_fifo_lock);
 		return -EAGAIN;
 	}
 
-	retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
-			  sizeof(instance->tx_cfg));
-	if (retval != sizeof(instance->tx_cfg))
-		goto abort;
-
-	retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
-	if (retval != sizeof(size_t))
-		goto abort;
-
 	retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
 	if (retval || copied != count)
 		goto abort;
@@ -884,7 +857,6 @@ static long pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
 	struct pi433_instance	*instance;
 	struct pi433_device	*device;
-	struct pi433_tx_cfg	tx_cfg;
 	void __user *argp = (void __user *)arg;
 
 	/* Check type and command number */
@@ -898,19 +870,6 @@ static long pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 		return -ESHUTDOWN;
 
 	switch (cmd) {
-	case PI433_IOC_RD_TX_CFG:
-		if (copy_to_user(argp, &instance->tx_cfg,
-				 sizeof(struct pi433_tx_cfg)))
-			return -EFAULT;
-		break;
-	case PI433_IOC_WR_TX_CFG:
-		if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
-			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:
 		if (copy_to_user(argp, &device->rx_cfg,
 				 sizeof(struct pi433_rx_cfg)))
diff --git a/drivers/staging/pi433/pi433_if.h b/drivers/staging/pi433/pi433_if.h
index 25ee0b77a32c..5dd2a3c6e661 100644
--- a/drivers/staging/pi433/pi433_if.h
+++ b/drivers/staging/pi433/pi433_if.h
@@ -75,6 +75,8 @@ struct pi433_tx_cfg {
 
 	__u8			sync_pattern[8];
 	__u8			address_byte;
+	__u32			payload_size;
+	__u8			payload[];
 };
 
 /**
@@ -135,11 +137,6 @@ struct pi433_rx_cfg {
 
 #define PI433_IOC_MAGIC	'r'
 
-#define PI433_IOC_RD_TX_CFG                                             \
-	_IOR(PI433_IOC_MAGIC, PI433_TX_CFG_IOCTL_NR, char[sizeof(struct pi433_tx_cfg)])
-#define PI433_IOC_WR_TX_CFG                                             \
-	_IOW(PI433_IOC_MAGIC, PI433_TX_CFG_IOCTL_NR, char[sizeof(struct pi433_tx_cfg)])
-
 #define PI433_IOC_RD_RX_CFG                                             \
 	_IOR(PI433_IOC_MAGIC, PI433_RX_CFG_IOCTL_NR, char[sizeof(struct pi433_rx_cfg)])
 #define PI433_IOC_WR_RX_CFG                                             \
-- 
2.36.1


             reply	other threads:[~2022-05-15 10:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-15 10:47 Yaşar Arabacı [this message]
2022-05-15 10:54 ` [PATCH] Staging: pi433: Don't use ioctl for per-client configuration Greg KH
2022-05-15 10:55 ` Greg KH
2022-05-16  7:33 ` Dan Carpenter
2022-05-16 17:23   ` Yaşar Arabacı
2022-05-16 11:23 ` kernel test robot
2022-05-16 15:30 ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220515104711.94567-1-yasar11732@gmail.com \
    --to=yasar11732@gmail.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=dan.carpenter@oracle.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=paulo.miguel.almeida.rodenas@gmail.com \
    --cc=realwakka@gmail.com \
    --cc=u.kleine-koenig@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).