Skip to content

Commit fbbe83c

Browse files
Tedd Ho-Jeong Anholtmann
authored andcommitted
Bluetooth: btintel: Create common function for firmware download
The firmware download flow for RAM SKU is same for both USB and UART and this patch creates a common function for both driver. Signed-off-by: Tedd Ho-Jeong An <tedd.an@linux.intel.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
1 parent faf174d commit fbbe83c

4 files changed

Lines changed: 99 additions & 154 deletions

File tree

‎drivers/bluetooth/btintel.c‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/module.h>
2525
#include <linux/firmware.h>
2626
#include <linux/regmap.h>
27+
#include <asm/unaligned.h>
2728

2829
#include <net/bluetooth/bluetooth.h>
2930
#include <net/bluetooth/hci_core.h>
@@ -640,6 +641,89 @@ int btintel_read_boot_params(struct hci_dev *hdev,
640641
}
641642
EXPORT_SYMBOL_GPL(btintel_read_boot_params);
642643

644+
int btintel_download_firmware(struct hci_dev *hdev, const struct firmware *fw,
645+
u32 *boot_param)
646+
{
647+
int err;
648+
const u8 *fw_ptr;
649+
u32 frag_len;
650+
651+
/* Start the firmware download transaction with the Init fragment
652+
* represented by the 128 bytes of CSS header.
653+
*/
654+
err = btintel_secure_send(hdev, 0x00, 128, fw->data);
655+
if (err < 0) {
656+
bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
657+
goto done;
658+
}
659+
660+
/* Send the 256 bytes of public key information from the firmware
661+
* as the PKey fragment.
662+
*/
663+
err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
664+
if (err < 0) {
665+
bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
666+
goto done;
667+
}
668+
669+
/* Send the 256 bytes of signature information from the firmware
670+
* as the Sign fragment.
671+
*/
672+
err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
673+
if (err < 0) {
674+
bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
675+
goto done;
676+
}
677+
678+
fw_ptr = fw->data + 644;
679+
frag_len = 0;
680+
681+
while (fw_ptr - fw->data < fw->size) {
682+
struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
683+
684+
/* Each SKU has a different reset parameter to use in the
685+
* HCI_Intel_Reset command and it is embedded in the firmware
686+
* data. So, instead of using static value per SKU, check
687+
* the firmware data and save it for later use.
688+
*/
689+
if (le16_to_cpu(cmd->opcode) == 0xfc0e) {
690+
/* The boot parameter is the first 32-bit value
691+
* and rest of 3 octets are reserved.
692+
*/
693+
*boot_param = get_unaligned_le32(fw_ptr + sizeof(*cmd));
694+
695+
bt_dev_dbg(hdev, "boot_param=0x%x", *boot_param);
696+
}
697+
698+
frag_len += sizeof(*cmd) + cmd->plen;
699+
700+
/* The parameter length of the secure send command requires
701+
* a 4 byte alignment. It happens so that the firmware file
702+
* contains proper Intel_NOP commands to align the fragments
703+
* as needed.
704+
*
705+
* Send set of commands with 4 byte alignment from the
706+
* firmware data buffer as a single Data fragement.
707+
*/
708+
if (!(frag_len % 4)) {
709+
err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
710+
if (err < 0) {
711+
bt_dev_err(hdev,
712+
"Failed to send firmware data (%d)",
713+
err);
714+
goto done;
715+
}
716+
717+
fw_ptr += frag_len;
718+
frag_len = 0;
719+
}
720+
}
721+
722+
done:
723+
return err;
724+
}
725+
EXPORT_SYMBOL_GPL(btintel_download_firmware);
726+
643727
MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
644728
MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
645729
MODULE_VERSION(VERSION);

‎drivers/bluetooth/btintel.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
100100
int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param);
101101
int btintel_read_boot_params(struct hci_dev *hdev,
102102
struct intel_boot_params *params);
103-
103+
int btintel_download_firmware(struct hci_dev *dev, const struct firmware *fw,
104+
u32 *boot_param);
104105
#else
105106

106107
static inline int btintel_check_bdaddr(struct hci_dev *hdev)
@@ -188,4 +189,11 @@ static inline int btintel_read_boot_params(struct hci_dev *hdev,
188189
{
189190
return -EOPNOTSUPP;
190191
}
192+
193+
static inline int btintel_download_firmware(struct hci_dev *dev,
194+
const struct firmware *fw,
195+
u32 *boot_param)
196+
{
197+
return -EOPNOTSUPP;
198+
}
191199
#endif

‎drivers/bluetooth/btusb.c‎

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,8 +2013,6 @@ static int btusb_setup_intel_new(struct hci_dev *hdev)
20132013
struct intel_version ver;
20142014
struct intel_boot_params params;
20152015
const struct firmware *fw;
2016-
const u8 *fw_ptr;
2017-
u32 frag_len;
20182016
u32 boot_param;
20192017
char fwname[64];
20202018
ktime_t calltime, delta, rettime;
@@ -2201,78 +2199,10 @@ static int btusb_setup_intel_new(struct hci_dev *hdev)
22012199

22022200
set_bit(BTUSB_DOWNLOADING, &data->flags);
22032201

2204-
/* Start the firmware download transaction with the Init fragment
2205-
* represented by the 128 bytes of CSS header.
2206-
*/
2207-
err = btintel_secure_send(hdev, 0x00, 128, fw->data);
2208-
if (err < 0) {
2209-
BT_ERR("%s: Failed to send firmware header (%d)",
2210-
hdev->name, err);
2211-
goto done;
2212-
}
2213-
2214-
/* Send the 256 bytes of public key information from the firmware
2215-
* as the PKey fragment.
2216-
*/
2217-
err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
2218-
if (err < 0) {
2219-
BT_ERR("%s: Failed to send firmware public key (%d)",
2220-
hdev->name, err);
2221-
goto done;
2222-
}
2223-
2224-
/* Send the 256 bytes of signature information from the firmware
2225-
* as the Sign fragment.
2226-
*/
2227-
err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
2228-
if (err < 0) {
2229-
BT_ERR("%s: Failed to send firmware signature (%d)",
2230-
hdev->name, err);
2202+
/* Start firmware downloading and get boot parameter */
2203+
err = btintel_download_firmware(hdev, fw, &boot_param);
2204+
if (err < 0)
22312205
goto done;
2232-
}
2233-
2234-
fw_ptr = fw->data + 644;
2235-
frag_len = 0;
2236-
2237-
while (fw_ptr - fw->data < fw->size) {
2238-
struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
2239-
2240-
/* Each SKU has a different reset parameter to use in the
2241-
* HCI_Intel_Reset command and it is embedded in the firmware
2242-
* data. So, instead of using static value per SKU, check
2243-
* the firmware data and save it for later use.
2244-
*/
2245-
if (cmd->opcode == 0xfc0e) {
2246-
/* The boot parameter is the first 32-bit value
2247-
* and rest of 3 octets are reserved.
2248-
*/
2249-
boot_param = get_unaligned_le32(fw_ptr + sizeof(*cmd));
2250-
2251-
bt_dev_dbg(hdev, "boot_param=0x%x", boot_param);
2252-
}
2253-
2254-
frag_len += sizeof(*cmd) + cmd->plen;
2255-
2256-
/* The parameter length of the secure send command requires
2257-
* a 4 byte alignment. It happens so that the firmware file
2258-
* contains proper Intel_NOP commands to align the fragments
2259-
* as needed.
2260-
*
2261-
* Send set of commands with 4 byte alignment from the
2262-
* firmware data buffer as a single Data fragement.
2263-
*/
2264-
if (!(frag_len % 4)) {
2265-
err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
2266-
if (err < 0) {
2267-
BT_ERR("%s: Failed to send firmware data (%d)",
2268-
hdev->name, err);
2269-
goto done;
2270-
}
2271-
2272-
fw_ptr += frag_len;
2273-
frag_len = 0;
2274-
}
2275-
}
22762206

22772207
set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
22782208

‎drivers/bluetooth/hci_intel.c‎

Lines changed: 3 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <linux/acpi.h>
3434
#include <linux/interrupt.h>
3535
#include <linux/pm_runtime.h>
36-
#include <asm/unaligned.h>
3736

3837
#include <net/bluetooth/bluetooth.h>
3938
#include <net/bluetooth/hci_core.h>
@@ -548,9 +547,7 @@ static int intel_setup(struct hci_uart *hu)
548547
struct intel_boot_params params;
549548
struct list_head *p;
550549
const struct firmware *fw;
551-
const u8 *fw_ptr;
552550
char fwname[64];
553-
u32 frag_len;
554551
u32 boot_param;
555552
ktime_t calltime, delta, rettime;
556553
unsigned long long duration;
@@ -761,84 +758,10 @@ static int intel_setup(struct hci_uart *hu)
761758

762759
set_bit(STATE_DOWNLOADING, &intel->flags);
763760

764-
/* Start the firmware download transaction with the Init fragment
765-
* represented by the 128 bytes of CSS header.
766-
*/
767-
err = btintel_secure_send(hdev, 0x00, 128, fw->data);
768-
if (err < 0) {
769-
bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
770-
goto done;
771-
}
772-
773-
/* Send the 256 bytes of public key information from the firmware
774-
* as the PKey fragment.
775-
*/
776-
err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
777-
if (err < 0) {
778-
bt_dev_err(hdev, "Failed to send firmware public key (%d)",
779-
err);
780-
goto done;
781-
}
782-
783-
/* Send the 256 bytes of signature information from the firmware
784-
* as the Sign fragment.
785-
*/
786-
err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
787-
if (err < 0) {
788-
bt_dev_err(hdev, "Failed to send firmware signature (%d)",
789-
err);
761+
/* Start firmware downloading and get boot parameter */
762+
err = btintel_download_firmware(hdev, fw, &boot_param);
763+
if (err < 0)
790764
goto done;
791-
}
792-
793-
fw_ptr = fw->data + 644;
794-
frag_len = 0;
795-
796-
while (fw_ptr - fw->data < fw->size) {
797-
struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
798-
799-
/* Each SKU has a different reset parameter to use in the
800-
* HCI_Intel_Reset command and it is embedded in the firmware
801-
* data. So, instead of using static value per SKU, check
802-
* the firmware data and save it for later use.
803-
*/
804-
if (cmd->opcode == 0xfc0e) {
805-
/* The boot parameter is the first 32-bit value
806-
* and rest of 3 octets are reserved.
807-
*/
808-
boot_param = get_unaligned_le32(fw_ptr + sizeof(*cmd));
809-
810-
bt_dev_dbg(hdev, "boot_param=0x%x", boot_param);
811-
}
812-
813-
frag_len += sizeof(*cmd) + cmd->plen;
814-
815-
bt_dev_dbg(hdev, "Patching %td/%zu", (fw_ptr - fw->data),
816-
fw->size);
817-
818-
/* The parameter length of the secure send command requires
819-
* a 4 byte alignment. It happens so that the firmware file
820-
* contains proper Intel_NOP commands to align the fragments
821-
* as needed.
822-
*
823-
* Send set of commands with 4 byte alignment from the
824-
* firmware data buffer as a single Data fragement.
825-
*/
826-
if (frag_len % 4)
827-
continue;
828-
829-
/* Send each command from the firmware data buffer as
830-
* a single Data fragment.
831-
*/
832-
err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
833-
if (err < 0) {
834-
bt_dev_err(hdev, "Failed to send firmware data (%d)",
835-
err);
836-
goto done;
837-
}
838-
839-
fw_ptr += frag_len;
840-
frag_len = 0;
841-
}
842765

843766
set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
844767

0 commit comments

Comments
 (0)