[PATCH v4 18/18] staging: r8188eu: Shorten calls chain of rtw_write8/16/32/N()

From: Fabio M. De Francesco
Date: Mon Sep 13 2021 - 14:13:46 EST


Shorten the calls chain of rtw_write8/16/32() down to the actual writes.
For this purpose unify the four usb_write8/16/32/N() into the new
usb_write(); make the latter parameterizable with 'size'; embed most of
the code of usbctrl_vendorreq() into usb_write() and use in it the new
usb_control_msg_send() API of USB Core.

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

v3->v4:
Make some changes according to a first review of Greg
Kroah-Hartman; remove unnecessary 'while' loop and a couple of
'if' test; handle the errors returned by usb_control_msg_send().

v2->v3: Fix the version number of the patch.

v1->v2: Replace parameter REALTEK_USB_VENQT_READ with REALTEK_USB_VENQT_WRITE
in usb_control_msg_send(). More in-depth explanation at
https://lore.kernel.org/lkml/2791328.7pjKATJfGa@localhost.localdomain/T/#m1fc1ab2f7c1f463049ad88d5df5bb1b107b37260

drivers/staging/r8188eu/hal/usb_ops_linux.c | 111 +++++++-------------
1 file changed, 39 insertions(+), 72 deletions(-)

diff --git a/drivers/staging/r8188eu/hal/usb_ops_linux.c b/drivers/staging/r8188eu/hal/usb_ops_linux.c
index 75475b0083db..cf27c84c3c90 100644
--- a/drivers/staging/r8188eu/hal/usb_ops_linux.c
+++ b/drivers/staging/r8188eu/hal/usb_ops_linux.c
@@ -8,88 +8,57 @@
#include "../include/recv_osdep.h"
#include "../include/rtl8188e_hal.h"

-static int usbctrl_vendorreq(struct intf_hdl *intfhdl, u16 value, void *data, u16 len, u8 requesttype)
+static int usb_read(struct intf_hdl *intfhdl, u16 addr, void *data, u8 size)
{
- int status = 0;
- u8 *io_buf;
- unsigned int pipe;
- int vendorreq_times = 0;
+ int status;
+ u8 *io_buf; /* pointer to I/O buffer */
struct adapter *adapt = intfhdl->padapter;
struct dvobj_priv *dvobjpriv = adapter_to_dvobj(adapt);
struct usb_device *udev = dvobjpriv->pusbdev;

- if ((adapt->bSurpriseRemoved) || (adapt->pwrctrlpriv.pnp_bstop_trx)) {
+ if (adapt->bSurpriseRemoved || adapt->pwrctrlpriv.pnp_bstop_trx) {
status = -EPERM;
goto exit;
}

- if (len > MAX_VENDOR_REQ_CMD_SIZE) {
- DBG_88E("[%s] Buffer len error ,vendor request failed\n", __func__);
- status = -EINVAL;
- goto exit;
- }
-
mutex_lock(&dvobjpriv->usb_vendor_req_mutex);

- /* Acquire IO memory for vendorreq */
io_buf = dvobjpriv->usb_vendor_req_buf;

- if (requesttype == REALTEK_USB_VENQT_READ)
- /* read in */
- pipe = usb_rcvctrlpipe(udev, 0);
- else
- /* write out */
- pipe = usb_sndctrlpipe(udev, 0);
-
- while (++vendorreq_times <= MAX_USBCTRL_VENDORREQ_TIMES) {
- if (requesttype == REALTEK_USB_VENQT_READ)
- memset(io_buf, 0, len);
- else
- memcpy(io_buf, data, len);
-
- status = usb_control_msg(udev, pipe, REALTEK_USB_VENQT_CMD_REQ,
- requesttype, value, REALTEK_USB_VENQT_CMD_IDX,
- io_buf, len, RTW_USB_CONTROL_MSG_TIMEOUT);
-
- if (status == len) {
- /* success */
- rtw_reset_continual_urb_error(dvobjpriv);
- if (requesttype == REALTEK_USB_VENQT_READ)
- memcpy(data, io_buf, len);
- } else {
- /* errors */
- if (status < 0) {
- if (status == (-ESHUTDOWN || -ENODEV)) {
- adapt->bSurpriseRemoved = true;
- } else {
- struct hal_data_8188e *haldata = GET_HAL_DATA(adapt);
- haldata->srestpriv.wifi_error_status = USB_VEN_REQ_CMD_FAIL;
- }
- } else {
- /* status != len && status >= 0 */
- if (status > 0) {
- if (requesttype == REALTEK_USB_VENQT_READ)
- memcpy(data, io_buf, len);
- }
- }
-
- if (rtw_inc_and_chk_continual_urb_error(dvobjpriv)) {
- adapt->bSurpriseRemoved = true;
- break;
- }
-
- }
- /* firmware download is checksummed, don't retry */
- if ((value >= FW_8188E_START_ADDRESS && value <= FW_8188E_END_ADDRESS) || status == len)
- break;
+ status = usb_control_msg_recv(udev, 0, REALTEK_USB_VENQT_CMD_REQ,
+ REALTEK_USB_VENQT_READ, addr,
+ REALTEK_USB_VENQT_CMD_IDX, io_buf,
+ size, RTW_USB_CONTROL_MSG_TIMEOUT,
+ GFP_KERNEL);
+ if (!status) {
+ /* Success this control transfer. */
+ rtw_reset_continual_urb_error(dvobjpriv);
+ memcpy(data, io_buf, size);
+ goto mutex_unlock;
}
-
+ /* error cases */
+ if (status == (-ESHUTDOWN || -ENODEV || -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;
+ }
+ 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;
+ }
+mutex_unlock:
mutex_unlock(&dvobjpriv->usb_vendor_req_mutex);
exit:
return status;
}

-static int usb_read(struct intf_hdl *intfhdl, u16 addr, void *data, u8 size)
+static int usb_write(struct intf_hdl *intfhdl, u16 addr, void *data, u8 size)
{
int status;
u8 *io_buf; /* pointer to I/O buffer */
@@ -105,16 +74,16 @@ static int usb_read(struct intf_hdl *intfhdl, u16 addr, void *data, u8 size)
mutex_lock(&dvobjpriv->usb_vendor_req_mutex);

io_buf = dvobjpriv->usb_vendor_req_buf;
+ memcpy(io_buf, data, size);

- status = usb_control_msg_recv(udev, 0, REALTEK_USB_VENQT_CMD_REQ,
- REALTEK_USB_VENQT_READ, addr,
+ status = usb_control_msg_send(udev, 0, REALTEK_USB_VENQT_CMD_REQ,
+ REALTEK_USB_VENQT_WRITE, addr,
REALTEK_USB_VENQT_CMD_IDX, io_buf,
size, RTW_USB_CONTROL_MSG_TIMEOUT,
GFP_KERNEL);
if (!status) {
/* Success this control transfer. */
rtw_reset_continual_urb_error(dvobjpriv);
- memcpy(data, io_buf, size);
goto mutex_unlock;
}
/* error cases */
@@ -182,7 +151,7 @@ int rtw_write8(struct adapter *adapter, u32 addr, u8 val)
struct io_priv *io_priv = &adapter->iopriv;
struct intf_hdl *intfhdl = &io_priv->intf;

- ret = usbctrl_vendorreq(intfhdl, address, &val, 1, REALTEK_USB_VENQT_WRITE);
+ ret = usb_write(intfhdl, address, &val, 1);

return RTW_STATUS_CODE(ret);
}
@@ -195,7 +164,7 @@ int rtw_write16(struct adapter *adapter, u32 addr, u16 val)
struct io_priv *io_priv = &adapter->iopriv;
struct intf_hdl *intfhdl = &io_priv->intf;

- ret = usbctrl_vendorreq(intfhdl, address, &data, 2, REALTEK_USB_VENQT_WRITE);
+ ret = usb_write(intfhdl, address, &data, 2);

return RTW_STATUS_CODE(ret);
}
@@ -208,7 +177,7 @@ int rtw_write32(struct adapter *adapter, u32 addr, u32 val)
struct io_priv *io_priv = &adapter->iopriv;
struct intf_hdl *intfhdl = &io_priv->intf;

- ret = usbctrl_vendorreq(intfhdl, address, &data, 4, REALTEK_USB_VENQT_WRITE);
+ ret = usb_write(intfhdl, address, &data, 4);

return RTW_STATUS_CODE(ret);
}
@@ -218,15 +187,13 @@ int rtw_writeN(struct adapter *adapter, u32 addr, u32 len, u8 *data)
int ret;
u16 address = (u16)(addr & 0xffff);
u16 length = (u16)(len & 0xffff);
- u8 buf[VENDOR_CMD_MAX_DATA_LEN] = {0};
struct io_priv *io_priv = &adapter->iopriv;
struct intf_hdl *intfhdl = &io_priv->intf;

if (length > VENDOR_CMD_MAX_DATA_LEN)
return _FAIL;

- memcpy(buf, data, length);
- ret = usbctrl_vendorreq(intfhdl, address, buf, length, REALTEK_USB_VENQT_WRITE);
+ ret = usb_write(intfhdl, address, data, length);

return RTW_STATUS_CODE(ret);
}
--
2.33.0