linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kai-Heng Feng <kai.heng.feng@canonical.com>
To: marcel@holtmann.org, johan.hedberg@gmail.com
Cc: drake@endlessm.com, linux-bluetooth@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Kai-Heng Feng <kai.heng.feng@canonical.com>
Subject: [PATCH 2/3] Bluetooth: btrtl: Let btrtl_initialize() always return error code
Date: Thu, 24 Jan 2019 23:23:09 +0800	[thread overview]
Message-ID: <20190124152310.29717-2-kai.heng.feng@canonical.com> (raw)
In-Reply-To: <20190124152310.29717-1-kai.heng.feng@canonical.com>

btrtl_initialize() may return an address or error code.

To make it more consistent, refactor btrtl_initialize() to always return
error code.

This requires "struct btrtl_device_info" to be allocated on stack. It's
a small structure, and simiar patterns can be found in other bluetooth
vendor helpers.

Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
---
 drivers/bluetooth/btrtl.c  | 43 +++++++++++++-------------------------
 drivers/bluetooth/btrtl.h  |  5 +++--
 drivers/bluetooth/hci_h5.c | 16 ++++++--------
 3 files changed, 23 insertions(+), 41 deletions(-)

diff --git a/drivers/bluetooth/btrtl.c b/drivers/bluetooth/btrtl.c
index a134a79bf0ef..c36f500d8313 100644
--- a/drivers/bluetooth/btrtl.c
+++ b/drivers/bluetooth/btrtl.c
@@ -516,10 +516,10 @@ void btrtl_free(struct btrtl_device_info *btrtl_dev)
 }
 EXPORT_SYMBOL_GPL(btrtl_free);
 
-struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
-					   const char *postfix)
+int btrtl_initialize(struct hci_dev *hdev,
+		     struct btrtl_device_info *btrtl_dev,
+		     const char *postfix)
 {
-	struct btrtl_device_info *btrtl_dev;
 	struct sk_buff *skb;
 	struct hci_rp_read_local_version *resp;
 	char cfg_name[40];
@@ -527,16 +527,9 @@ struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
 	u8 hci_ver;
 	int ret;
 
-	btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
-	if (!btrtl_dev) {
-		ret = -ENOMEM;
-		goto err_alloc;
-	}
-
 	skb = btrtl_read_local_version(hdev);
 	if (IS_ERR(skb)) {
-		ret = PTR_ERR(skb);
-		goto err_free;
+		return PTR_ERR(skb);
 	}
 
 	resp = (struct hci_rp_read_local_version *)skb->data;
@@ -555,14 +548,13 @@ struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
 	if (!btrtl_dev->ic_info) {
 		rtl_dev_err(hdev, "rtl: unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
 			    lmp_subver, hci_rev, hci_ver);
-		ret = -EINVAL;
-		goto err_free;
+		return -EINVAL;
 	}
 
 	if (btrtl_dev->ic_info->has_rom_version) {
 		ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
 		if (ret)
-			goto err_free;
+			return ret;
 	}
 
 	ret = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
@@ -570,7 +562,7 @@ struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
 	if (ret || !btrtl_dev->fw_len) {
 		rtl_dev_err(hdev, "firmware file %s not found\n",
 			    btrtl_dev->ic_info->fw_name);
-		goto err_free;
+		return -ENOENT;
 	}
 
 	if (btrtl_dev->ic_info->cfg_name) {
@@ -587,16 +579,11 @@ struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
 		    (ret || !btrtl_dev->cfg_len)) {
 			rtl_dev_err(hdev, "mandatory config file %s not found\n",
 				    btrtl_dev->ic_info->cfg_name);
-			goto err_free;
+			return -ENOENT;
 		}
 	}
 
-	return btrtl_dev;
-
-err_free:
-	btrtl_free(btrtl_dev);
-err_alloc:
-	return ERR_PTR(ret);
+	return 0;
 }
 EXPORT_SYMBOL_GPL(btrtl_initialize);
 
@@ -627,16 +614,14 @@ EXPORT_SYMBOL_GPL(btrtl_download_firmware);
 
 int btrtl_setup_realtek(struct hci_dev *hdev)
 {
-	struct btrtl_device_info *btrtl_dev;
+	struct btrtl_device_info btrtl_dev;
 	int ret;
 
-	btrtl_dev = btrtl_initialize(hdev, NULL);
-	if (IS_ERR(btrtl_dev))
-		return PTR_ERR(btrtl_dev);
-
-	ret = btrtl_download_firmware(hdev, btrtl_dev);
+	ret = btrtl_initialize(hdev, &btrtl_dev, NULL);
+	if (ret)
+		return ret;
 
-	btrtl_free(btrtl_dev);
+	ret = btrtl_download_firmware(hdev, &btrtl_dev);
 
 	return ret;
 }
diff --git a/drivers/bluetooth/btrtl.h b/drivers/bluetooth/btrtl.h
index f5e36f3993a8..df15480b7077 100644
--- a/drivers/bluetooth/btrtl.h
+++ b/drivers/bluetooth/btrtl.h
@@ -59,8 +59,9 @@ struct rtl_vendor_config {
 
 #if IS_ENABLED(CONFIG_BT_RTL)
 
-struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
-					   const char *postfix);
+int btrtl_initialize(struct hci_dev *hdev,
+		     struct btrtl_device_info *btrtl_dev,
+		     const char *postfix);
 void btrtl_free(struct btrtl_device_info *btrtl_dev);
 int btrtl_download_firmware(struct hci_dev *hdev,
 			    struct btrtl_device_info *btrtl_dev);
diff --git a/drivers/bluetooth/hci_h5.c b/drivers/bluetooth/hci_h5.c
index 069d1c8fde73..d1fb0a4d82ae 100644
--- a/drivers/bluetooth/hci_h5.c
+++ b/drivers/bluetooth/hci_h5.c
@@ -868,7 +868,7 @@ static int __maybe_unused h5_serdev_resume(struct device *dev)
 #ifdef CONFIG_BT_HCIUART_RTL
 static int h5_btrtl_setup(struct h5 *h5)
 {
-	struct btrtl_device_info *btrtl_dev;
+	struct btrtl_device_info btrtl_dev;
 	struct sk_buff *skb;
 	__le32 baudrate_data;
 	u32 device_baudrate;
@@ -876,23 +876,22 @@ static int h5_btrtl_setup(struct h5 *h5)
 	bool flow_control;
 	int err;
 
-	btrtl_dev = btrtl_initialize(h5->hu->hdev, h5->id);
-	if (IS_ERR(btrtl_dev))
-		return PTR_ERR(btrtl_dev);
+	err = btrtl_initialize(h5->hu->hdev, &btrtl_dev, h5->id);
+	if (err)
+		return err;
 
 	err = btrtl_get_uart_settings(h5->hu->hdev, btrtl_dev,
 				      &controller_baudrate, &device_baudrate,
 				      &flow_control);
 	if (err)
-		goto out_free;
+		return err;
 
 	baudrate_data = cpu_to_le32(device_baudrate);
 	skb = __hci_cmd_sync(h5->hu->hdev, 0xfc17, sizeof(baudrate_data),
 			     &baudrate_data, HCI_INIT_TIMEOUT);
 	if (IS_ERR(skb)) {
 		rtl_dev_err(h5->hu->hdev, "set baud rate command failed\n");
-		err = PTR_ERR(skb);
-		goto out_free;
+		return PTR_ERR(skb);
 	} else {
 		kfree_skb(skb);
 	}
@@ -906,9 +905,6 @@ static int h5_btrtl_setup(struct h5 *h5)
 	/* Give the device some time before the hci-core sends it a reset */
 	usleep_range(10000, 20000);
 
-out_free:
-	btrtl_free(btrtl_dev);
-
 	return err;
 }
 
-- 
2.17.1


  reply	other threads:[~2019-01-24 15:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-24 15:23 [PATCH 1/3] Bluetooth: btrtl: Let rtl_load_file() always return error code Kai-Heng Feng
2019-01-24 15:23 ` Kai-Heng Feng [this message]
2019-01-24 15:23 ` [PATCH 3/3] Bluetooth: btrtl: Skip initialization if firmware is already loaded Kai-Heng Feng
2019-01-25  0:55   ` Daniel Drake
2019-01-25 17:46     ` Kai-Heng Feng

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=20190124152310.29717-2-kai.heng.feng@canonical.com \
    --to=kai.heng.feng@canonical.com \
    --cc=drake@endlessm.com \
    --cc=johan.hedberg@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcel@holtmann.org \
    /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).