Skip to content

Commit 0cc4b56

Browse files
gregkhVudentz
authored andcommitted
Bluetooth: btmtk: Fix short read errors in btmtk_usb_reg_read()
If btmtk_usb_reg_read() gets a "short" read from a device, it will accidentally treat that as a "real" read and populate the returned value with some unknown and probably totally invalid data. Fix this logic error up by calling usb_control_msg_recv() which guarantees a "full" read happens, and then simplify the error checking for when btmtk_usb_reg_read() is called (it's really just btmtk_usb_id_get() that calls btmtk_usb_reg_read(), so fix up those return sites. Cc: stable <stable@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
1 parent b186c18 commit 0cc4b56

1 file changed

Lines changed: 15 additions & 21 deletions

File tree

drivers/bluetooth/btmtk.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -827,27 +827,21 @@ static int btmtk_usb_uhw_reg_read(struct hci_dev *hdev, u32 reg, u32 *val)
827827
static int btmtk_usb_reg_read(struct hci_dev *hdev, u32 reg, u32 *val)
828828
{
829829
struct btmtk_data *data = hci_get_priv(hdev);
830-
int pipe, err, size = sizeof(u32);
831-
void *buf;
832-
833-
buf = kzalloc(size, GFP_KERNEL);
834-
if (!buf)
835-
return -ENOMEM;
830+
u8 buf[sizeof(u32)];
831+
int err;
836832

837-
pipe = usb_rcvctrlpipe(data->udev, 0);
838-
err = usb_control_msg(data->udev, pipe, 0x63,
839-
USB_TYPE_VENDOR | USB_DIR_IN,
840-
reg >> 16, reg & 0xffff,
841-
buf, size, USB_CTRL_GET_TIMEOUT);
833+
*val = 0;
834+
err = usb_control_msg_recv(data->udev, 0, 0x63,
835+
USB_TYPE_VENDOR | USB_DIR_IN,
836+
reg >> 16, reg & 0xffff,
837+
buf, sizeof(buf), USB_CTRL_GET_TIMEOUT,
838+
GFP_KERNEL);
842839
if (err < 0)
843-
goto err_free_buf;
840+
return err;
844841

845842
*val = get_unaligned_le32(buf);
846843

847-
err_free_buf:
848-
kfree(buf);
849-
850-
return err;
844+
return 0;
851845
}
852846

853847
static int btmtk_usb_id_get(struct hci_dev *hdev, u32 reg, u32 *id)
@@ -974,7 +968,7 @@ int btmtk_usb_subsys_reset(struct hci_dev *hdev, u32 dev_id)
974968
}
975969

976970
err = btmtk_usb_id_get(hdev, 0x70010200, &val);
977-
if (err < 0 || (!val && dev_id != 0x6639))
971+
if (err || (!val && dev_id != 0x6639))
978972
bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
979973

980974
return err;
@@ -1318,24 +1312,24 @@ int btmtk_usb_setup(struct hci_dev *hdev)
13181312
calltime = ktime_get();
13191313

13201314
err = btmtk_usb_id_get(hdev, 0x80000008, &dev_id);
1321-
if (err < 0) {
1315+
if (err) {
13221316
bt_dev_err(hdev, "Failed to get device id (%d)", err);
13231317
return err;
13241318
}
13251319

13261320
if (!dev_id || dev_id != 0x7663) {
13271321
err = btmtk_usb_id_get(hdev, 0x70010200, &dev_id);
1328-
if (err < 0) {
1322+
if (err) {
13291323
bt_dev_err(hdev, "Failed to get device id (%d)", err);
13301324
return err;
13311325
}
13321326
err = btmtk_usb_id_get(hdev, 0x80021004, &fw_version);
1333-
if (err < 0) {
1327+
if (err) {
13341328
bt_dev_err(hdev, "Failed to get fw version (%d)", err);
13351329
return err;
13361330
}
13371331
err = btmtk_usb_id_get(hdev, 0x70010020, &fw_flavor);
1338-
if (err < 0) {
1332+
if (err) {
13391333
bt_dev_err(hdev, "Failed to get fw flavor (%d)", err);
13401334
return err;
13411335
}

0 commit comments

Comments
 (0)