mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bernard Zhao <zhaojunkui2008@126.com>
To: "Wolfgang Grandegger" <wg@grandegger.com>,
	"Marc Kleine-Budde" <mkl@pengutronix.de>,
	"David S. Miller" <davem@davemloft.net>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Vincent Mailhol" <mailhol.vincent@wanadoo.fr>,
	"Stefan Mätje" <stefan.maetje@esd.eu>,
	"Bernard Zhao" <zhaojunkui2008@126.com>,
	linux-can@vger.kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: bernard@vivo.com
Subject: [PATCH v2] usb/peak_usb: cleanup code
Date: Wed, 11 May 2022 06:02:38 -0700	[thread overview]
Message-ID: <20220511130240.790771-1-zhaojunkui2008@126.com> (raw)

The variable fi and bi only used in branch if (!dev->prev_siblings)
, fi & bi not kmalloc in else branch, so move kfree into branch
if (!dev->prev_siblings),this change is to cleanup the code a bit.

Signed-off-by: Bernard Zhao <zhaojunkui2008@126.com>

---
Changes since V1:
* move all the content of the if (!dev->prev_siblings) to a new
function.
---
 drivers/net/can/usb/peak_usb/pcan_usb_pro.c | 57 +++++++++++++--------
 1 file changed, 36 insertions(+), 21 deletions(-)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
index ebe087f258e3..5e472fe086a8 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
@@ -841,32 +841,28 @@ static int pcan_usb_pro_stop(struct peak_usb_device *dev)
 	return 0;
 }
 
-/*
- * called when probing to initialize a device object.
- */
-static int pcan_usb_pro_init(struct peak_usb_device *dev)
+static int pcan_usb_pro_init_first_channel(struct peak_usb_device *dev, struct pcan_usb_pro_interface **usb_if)
 {
-	struct pcan_usb_pro_device *pdev =
-			container_of(dev, struct pcan_usb_pro_device, dev);
-	struct pcan_usb_pro_interface *usb_if = NULL;
-	struct pcan_usb_pro_fwinfo *fi = NULL;
-	struct pcan_usb_pro_blinfo *bi = NULL;
+	struct pcan_usb_pro_interface *pusb_if = NULL;
 	int err;
 
 	/* do this for 1st channel only */
 	if (!dev->prev_siblings) {
+		struct pcan_usb_pro_fwinfo *fi = NULL;
+		struct pcan_usb_pro_blinfo *bi = NULL;
+
 		/* allocate netdevices common structure attached to first one */
-		usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
+		pusb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
 				 GFP_KERNEL);
 		fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL);
 		bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL);
-		if (!usb_if || !fi || !bi) {
+		if (!pusb_if || !fi || !bi) {
 			err = -ENOMEM;
 			goto err_out;
 		}
 
 		/* number of ts msgs to ignore before taking one into account */
-		usb_if->cm_ignore_count = 5;
+		pusb_if->cm_ignore_count = 5;
 
 		/*
 		 * explicit use of dev_xxx() instead of netdev_xxx() here:
@@ -903,18 +899,14 @@ static int pcan_usb_pro_init(struct peak_usb_device *dev)
 		     pcan_usb_pro.name,
 		     bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo,
 		     pcan_usb_pro.ctrl_count);
+
+		kfree(bi);
+		kfree(fi);
 	} else {
-		usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
+		pusb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
 	}
 
-	pdev->usb_if = usb_if;
-	usb_if->dev[dev->ctrl_idx] = dev;
-
-	/* set LED in default state (end of init phase) */
-	pcan_usb_pro_set_led(dev, PCAN_USBPRO_LED_DEVICE, 1);
-
-	kfree(bi);
-	kfree(fi);
+	*usb_if = pusb_if;
 
 	return 0;
 
@@ -926,6 +918,29 @@ static int pcan_usb_pro_init(struct peak_usb_device *dev)
 	return err;
 }
 
+/*
+ * called when probing to initialize a device object.
+ */
+static int pcan_usb_pro_init(struct peak_usb_device *dev)
+{
+	struct pcan_usb_pro_device *pdev =
+			container_of(dev, struct pcan_usb_pro_device, dev);
+	struct pcan_usb_pro_interface *usb_if = NULL;
+	int err;
+
+	err = pcan_usb_pro_init_first_channel(dev, &usb_if);
+	if (err)
+		return err;
+
+	pdev->usb_if = usb_if;
+	usb_if->dev[dev->ctrl_idx] = dev;
+
+	/* set LED in default state (end of init phase) */
+	pcan_usb_pro_set_led(dev, PCAN_USBPRO_LED_DEVICE, 1);
+
+	return 0;
+}
+
 static void pcan_usb_pro_exit(struct peak_usb_device *dev)
 {
 	struct pcan_usb_pro_device *pdev =
-- 
2.33.1


             reply	other threads:[~2022-05-11 13:04 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-11 13:02 Bernard Zhao [this message]
2022-05-11 14:28 ` Vincent MAILHOL
2022-05-12  1:15   ` z
2022-05-11 19:42 ` kernel test robot
2022-05-11 20:43 ` 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=20220511130240.790771-1-zhaojunkui2008@126.com \
    --to=zhaojunkui2008@126.com \
    --cc=bernard@vivo.com \
    --cc=davem@davemloft.net \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mailhol.vincent@wanadoo.fr \
    --cc=mkl@pengutronix.de \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=stefan.maetje@esd.eu \
    --cc=wg@grandegger.com \
    /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

all inboxes | Powered by JetHome®