[PATCH v10 13/16] staging: r8188eu: call new usb_read() from rtw_read{8,16,32}()

From: Fabio M. De Francesco
Date: Fri Sep 24 2021 - 08:28:24 EST


Create and call new usb_read() instead of usbctrl_vendorreq() from
inside rtw_read{8,16,32}().

In old code, rtw_read{8,16,32}() called usbctrl_vendorreq() which in
turn uses usb_control_msg() from within a "while" loop to build a control
URB, send it off and wait for completion. usbctrl_vendorreq() was used for
both receiving and sending messages, depending on the "requesttype"
argument which was passed by callers.

Compared to usbctrl_vendorreq(), which managed both reads and writes
from and to the USB endpoint, the new usb_read() manages only reads. For
this purpose it uses the newer USB Core usb_control_msg_recv() API. The
latter is preferred according both to a suggestion by Greg Kroah-Hartman
and also to its actual design.

Two noteworthy features of usb_control_msg_recv() are that (1) the data
pointer can be made to a reference on the stack because it does not have
the restriction that usb_control_msg() has where the data pointer must be
to dynamically allocated memory, and that (2) the whole message must be
properly received from the device in order for this function to be
successfuli (if a device returns less than the expected amount of data,
then the function will fail).

usbctrl_vendorreq() uses a "while" loop that we considered unnecessary
so that it is not in the new usb_read(). Furthermore, the latter has no
redundant checking, less obvious comments, and it manages errors before
success cases. All in all, usb_read() is simpler than
usbctrl_vendorreq() and uses less lines of code.

Suggested-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Co-developed-by: Pavel Skripkin <paskripkin@xxxxxxxxx>
Signed-off-by: Pavel Skripkin <paskripkin@xxxxxxxxx>
Signed-off-by: Fabio M. De Francesco <fmdefrancesco@xxxxxxxxx>
---
drivers/staging/r8188eu/hal/usb_ops_linux.c | 59 +++++++++++++++++++--
1 file changed, 56 insertions(+), 3 deletions(-)

diff --git a/drivers/staging/r8188eu/hal/usb_ops_linux.c b/drivers/staging/r8188eu/hal/usb_ops_linux.c
index b3f8a76b5db2..a3dcd366c00c 100644
--- a/drivers/staging/r8188eu/hal/usb_ops_linux.c
+++ b/drivers/staging/r8188eu/hal/usb_ops_linux.c
@@ -86,6 +86,59 @@ static int usbctrl_vendorreq(struct intf_hdl *intf, u16 value, void *data, u16 l
return status;
}

+static int usb_read(struct intf_hdl *intf, u16 value, void *data, u8 size)
+{
+ struct adapter *adapt = intf->padapter;
+ struct dvobj_priv *dvobjpriv = adapter_to_dvobj(adapt);
+ struct usb_device *udev = dvobjpriv->pusbdev;
+ int status;
+ u8 *io_buf; /* Pointer to I/O buffer */
+
+ mutex_lock(&dvobjpriv->usb_vendor_req_mutex);
+
+ if (adapt->bSurpriseRemoved || adapt->pwrctrlpriv.pnp_bstop_trx)
+ return -EPERM;
+
+ io_buf = dvobjpriv->usb_vendor_req_buf;
+
+ status = usb_control_msg_recv(udev, 0, REALTEK_USB_VENQT_CMD_REQ,
+ REALTEK_USB_VENQT_READ, value,
+ REALTEK_USB_VENQT_CMD_IDX, io_buf,
+ size, RTW_USB_CONTROL_MSG_TIMEOUT,
+ GFP_KERNEL);
+
+ if (status == -ESHUTDOWN ||
+ status == -ENODEV ||
+ status == -ENOENT) {
+ /*
+ * device or controller has been disabled due to
+ * some problem that could not be worked around,
+ * device or bus doesn’t exist, endpoint does not
+ * exist or is not enabled.
+ */
+ adapt->bSurpriseRemoved = true;
+ goto mutex_unlock;
+ }
+
+ if (status < 0) {
+ GET_HAL_DATA(adapt)->srestpriv.wifi_error_status =
+ USB_VEN_REQ_CMD_FAIL;
+
+ if (rtw_inc_and_chk_continual_urb_error(dvobjpriv))
+ adapt->bSurpriseRemoved = true;
+
+ goto mutex_unlock;
+ }
+
+ rtw_reset_continual_urb_error(dvobjpriv);
+ memcpy(data, io_buf, size);
+
+mutex_unlock:
+ mutex_unlock(&dvobjpriv->usb_vendor_req_mutex);
+
+ return status;
+}
+
u8 rtw_read8(struct adapter *adapter, u32 addr)
{
struct io_priv *io_priv = &adapter->iopriv;
@@ -93,7 +146,7 @@ u8 rtw_read8(struct adapter *adapter, u32 addr)
u16 value = addr & 0xffff;
u8 data;

- usbctrl_vendorreq(intf, value, &data, 1, REALTEK_USB_VENQT_READ);
+ usb_read(intf, value, &data, 1);

return data;
}
@@ -105,7 +158,7 @@ u16 rtw_read16(struct adapter *adapter, u32 addr)
u16 value = addr & 0xffff;
__le16 data;

- usbctrl_vendorreq(intf, value, &data, 2, REALTEK_USB_VENQT_READ);
+ usb_read(intf, value, &data, 2);

return le16_to_cpu(data);
}
@@ -117,7 +170,7 @@ u32 rtw_read32(struct adapter *adapter, u32 addr)
u16 value = addr & 0xffff;
__le32 data;

- usbctrl_vendorreq(intf, value, &data, 4, REALTEK_USB_VENQT_READ);
+ usb_read(intf, value, &data, 4);

return le32_to_cpu(data);
}
--
2.33.0