linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] Bluetooth: btrtl: Save firmware and config
@ 2019-08-05  3:07 Alex Lu
  2019-08-12 16:35 ` Marcel Holtmann
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Lu @ 2019-08-05  3:07 UTC (permalink / raw)
  To: Marcel Holtmann; +Cc: Johan Hedberg, linux-bluetooth, linux-kernel, Max Chou

From: Alex Lu <alex_lu@realsil.com.cn>

usb reset resume will cause downloading firmware again and
requesting firmware may be failed while host is resuming

Signed-off-by: Alex Lu <alex_lu@realsil.com.cn>
---
 drivers/bluetooth/btrtl.c | 101 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 97 insertions(+), 4 deletions(-)

diff --git a/drivers/bluetooth/btrtl.c b/drivers/bluetooth/btrtl.c
index 208feef63de4..416a5cb676e3 100644
--- a/drivers/bluetooth/btrtl.c
+++ b/drivers/bluetooth/btrtl.c
@@ -56,6 +56,8 @@ struct btrtl_device_info {
 	int cfg_len;
 };
 
+static struct btrtl_device_info dev_info;
+
 static const struct id_table ic_id_table[] = {
 	{ IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8723A, 0x0,
 	  .config_needed = false,
@@ -553,8 +555,23 @@ struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
 			goto err_free;
 	}
 
-	btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
-					  &btrtl_dev->fw_data);
+	if (dev_info.ic_info == NULL ||
+	    dev_info.ic_info != btrtl_dev->ic_info) {
+		btrtl_dev->fw_len = rtl_load_file(hdev,
+						  btrtl_dev->ic_info->fw_name,
+						  &btrtl_dev->fw_data);
+		/* Make sure that fw is stored later */
+		dev_info.ic_info = NULL;
+	} else {
+		if (dev_info.fw_len > 0)
+			btrtl_dev->fw_data = kmemdup(dev_info.fw_data,
+						     dev_info.fw_len,
+						     GFP_KERNEL);
+		if (btrtl_dev->fw_data)
+			btrtl_dev->fw_len = dev_info.fw_len;
+		else
+			btrtl_dev->fw_len = -ENOMEM;
+	}
 	if (btrtl_dev->fw_len < 0) {
 		rtl_dev_err(hdev, "firmware file %s not found\n",
 			    btrtl_dev->ic_info->fw_name);
@@ -570,8 +587,21 @@ struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
 			snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
 				 btrtl_dev->ic_info->cfg_name);
 		}
-		btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
-						   &btrtl_dev->cfg_data);
+
+		if (dev_info.ic_info == NULL ||
+		    dev_info.ic_info != btrtl_dev->ic_info) {
+			btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
+							&btrtl_dev->cfg_data);
+		} else {
+			if (dev_info.cfg_len > 0)
+				btrtl_dev->cfg_data = kmemdup(dev_info.cfg_data,
+							      dev_info.cfg_len,
+							      GFP_KERNEL);
+			if (btrtl_dev->cfg_data)
+				btrtl_dev->cfg_len = dev_info.cfg_len;
+			else
+				btrtl_dev->cfg_len = -ENOMEM;
+		}
 		if (btrtl_dev->ic_info->config_needed &&
 		    btrtl_dev->cfg_len <= 0) {
 			rtl_dev_err(hdev, "mandatory config file %s not found\n",
@@ -620,6 +650,49 @@ int btrtl_download_firmware(struct hci_dev *hdev,
 }
 EXPORT_SYMBOL_GPL(btrtl_download_firmware);
 
+static void dev_fw_free(void)
+{
+	kfree(dev_info.fw_data);
+	kfree(dev_info.cfg_data);
+	memset(&dev_info, 0, sizeof(dev_info));
+}
+
+static void dev_fw_dup(struct btrtl_device_info *btrtl_dev)
+{
+	dev_fw_free();
+
+	dev_info.ic_info = btrtl_dev->ic_info;
+	dev_info.rom_version = btrtl_dev->rom_version;
+
+	dev_info.fw_len = btrtl_dev->fw_len;
+	if (dev_info.fw_len > 0)
+		dev_info.fw_data = kmemdup(btrtl_dev->fw_data,
+					   btrtl_dev->fw_len, GFP_KERNEL);
+	if (!dev_info.fw_data) {
+		BT_ERR("Failed to save rtl firmware");
+		goto err_memdup;
+	}
+
+	dev_info.cfg_len = btrtl_dev->cfg_len;
+	if (dev_info.cfg_len > 0)
+		dev_info.cfg_data = kmemdup(btrtl_dev->cfg_data,
+					    btrtl_dev->cfg_len, GFP_KERNEL);
+	if (!dev_info.cfg_data) {
+		if (dev_info.ic_info->config_needed) {
+			BT_ERR("Failed to save mandatory rtl config file");
+			goto err_memdup;
+		} else {
+			dev_info.cfg_len = 0;
+		}
+		BT_WARN("Failed to save rtl config file");
+	}
+
+	return;
+
+err_memdup:
+	dev_fw_free();
+}
+
 int btrtl_setup_realtek(struct hci_dev *hdev)
 {
 	struct btrtl_device_info *btrtl_dev;
@@ -629,6 +702,9 @@ int btrtl_setup_realtek(struct hci_dev *hdev)
 	if (IS_ERR(btrtl_dev))
 		return PTR_ERR(btrtl_dev);
 
+	if (btrtl_dev->ic_info && dev_info.ic_info != btrtl_dev->ic_info)
+		dev_fw_dup(btrtl_dev);
+
 	ret = btrtl_download_firmware(hdev, btrtl_dev);
 
 	btrtl_free(btrtl_dev);
@@ -745,6 +821,23 @@ int btrtl_get_uart_settings(struct hci_dev *hdev,
 }
 EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
 
+static int btrtl_module_init(void)
+{
+	BT_INFO("btrtl: init");
+
+	return 0;
+}
+
+static void btrtl_module_exit(void)
+{
+	BT_INFO("btrtl: exit");
+
+	dev_fw_free();
+}
+
+module_init(btrtl_module_init);
+module_exit(btrtl_module_exit)
+
 MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
 MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
 MODULE_VERSION(VERSION);
-- 
2.19.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] Bluetooth: btrtl: Save firmware and config
  2019-08-05  3:07 [PATCH v2] Bluetooth: btrtl: Save firmware and config Alex Lu
@ 2019-08-12 16:35 ` Marcel Holtmann
  2019-08-14  7:31   ` 答复: " 陆朱伟
  0 siblings, 1 reply; 3+ messages in thread
From: Marcel Holtmann @ 2019-08-12 16:35 UTC (permalink / raw)
  To: Alex Lu; +Cc: Johan Hedberg, linux-bluetooth, linux-kernel, Max Chou

Hi Alex,

> usb reset resume will cause downloading firmware again and
> requesting firmware may be failed while host is resuming
> 
> Signed-off-by: Alex Lu <alex_lu@realsil.com.cn>
> ---
> drivers/bluetooth/btrtl.c | 101 ++++++++++++++++++++++++++++++++++++--
> 1 file changed, 97 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/bluetooth/btrtl.c b/drivers/bluetooth/btrtl.c
> index 208feef63de4..416a5cb676e3 100644
> --- a/drivers/bluetooth/btrtl.c
> +++ b/drivers/bluetooth/btrtl.c
> @@ -56,6 +56,8 @@ struct btrtl_device_info {
> 	int cfg_len;
> };
> 
> +static struct btrtl_device_info dev_info;
> +

No. We are are not using magic global variables. What happens if you attach more than one device? Also I assumed that request_firmware has a caching capability of sorts so that drivers don’t have to re-implement caching of the firmware.

Regards

Marcel


^ permalink raw reply	[flat|nested] 3+ messages in thread

* 答复: [PATCH v2] Bluetooth: btrtl: Save firmware and config
  2019-08-12 16:35 ` Marcel Holtmann
@ 2019-08-14  7:31   ` 陆朱伟
  0 siblings, 0 replies; 3+ messages in thread
From: 陆朱伟 @ 2019-08-14  7:31 UTC (permalink / raw)
  To: Marcel Holtmann; +Cc: Johan Hedberg, linux-bluetooth, linux-kernel, Max Chou

Hi Marcel,
It only saves the latest fw and config for only one device.
I will dig the caching capability of request_firmware first.

Thanks,
BRs,

> Subject: Re: [PATCH v2] Bluetooth: btrtl: Save firmware and config
> 
> Hi Alex,
> 
> > usb reset resume will cause downloading firmware again and
> > requesting firmware may be failed while host is resuming
> >
> > Signed-off-by: Alex Lu <alex_lu@realsil.com.cn>
> > ---
> > drivers/bluetooth/btrtl.c | 101
> ++++++++++++++++++++++++++++++++++++--
> > 1 file changed, 97 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/bluetooth/btrtl.c b/drivers/bluetooth/btrtl.c
> > index 208feef63de4..416a5cb676e3 100644
> > --- a/drivers/bluetooth/btrtl.c
> > +++ b/drivers/bluetooth/btrtl.c
> > @@ -56,6 +56,8 @@ struct btrtl_device_info {
> > 	int cfg_len;
> > };
> >
> > +static struct btrtl_device_info dev_info;
> > +
> 
> No. We are are not using magic global variables. What happens if you attach
> more than one device? Also I assumed that request_firmware has a caching
> capability of sorts so that drivers don’t have to re-implement caching of the
> firmware.
> 
> Regards
> 
> Marcel
> 
> 
> ------Please consider the environment before printing this e-mail.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-08-14  7:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-05  3:07 [PATCH v2] Bluetooth: btrtl: Save firmware and config Alex Lu
2019-08-12 16:35 ` Marcel Holtmann
2019-08-14  7:31   ` 答复: " 陆朱伟

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).