[PATCH v6] ehci: fix EHCI host controller initialization sequence

2021-01-10 Thread Eugene Korenevsky
According to EHCI spec, EHCI HC clears USBSTS.HCHalted whenever
USBCMD.RS=1.

However, it is a good practice to wait some time after setting USBCMD.RS
(approximately 100ms) until USBSTS.HCHalted become zero.

Without this waiting, VirtualBox's EHCI virtual HC accidentally hangs
(see BugLink).

BugLink: https://bugzilla.kernel.org/show_bug.cgi?id=211095 
Acked-by: Alan Stern 
Signed-off-by: Eugene Korenevsky 
---
v1: initial patch
v2: add BugLink tag
v3: add Revieved-By tags (incorrect), restore `msleep(5)`
v4: remove Reviewed-By tags, restore reading from USBCMD prior to msleep,
adjust description
v5: add `patch changelog`
v6: add `Acked-by: Alan Stern ` tag

 drivers/usb/host/ehci-hcd.c | 12 
 1 file changed, 12 insertions(+)

diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c
index e358ae17d51e..1926b328b6aa 100644
--- a/drivers/usb/host/ehci-hcd.c
+++ b/drivers/usb/host/ehci-hcd.c
@@ -574,6 +574,7 @@ static int ehci_run (struct usb_hcd *hcd)
struct ehci_hcd *ehci = hcd_to_ehci (hcd);
u32 temp;
u32 hcc_params;
+   int rc;
 
hcd->uses_new_polling = 1;
 
@@ -629,9 +630,20 @@ static int ehci_run (struct usb_hcd *hcd)
down_write(_cf_port_reset_rwsem);
ehci->rh_state = EHCI_RH_RUNNING;
ehci_writel(ehci, FLAG_CF, >regs->configured_flag);
+
+   /* Wait until HC become operational */
ehci_readl(ehci, >regs->command); /* unblock posted writes */
msleep(5);
+   rc = ehci_handshake(ehci, >regs->status, STS_HALT, 0, 100 * 1000);
+
up_write(_cf_port_reset_rwsem);
+
+   if (rc) {
+   ehci_err(ehci, "USB %x.%x, controller refused to start: %d\n",
+((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f), rc);
+   return rc;
+   }
+
ehci->last_periodic_enable = ktime_get_real();
 
temp = HC_VERSION(ehci, ehci_readl(ehci, >caps->hc_capbase));
-- 
2.20.1



[PATCH] [PATCH v7] USB hub_probe: rework ugly goto-into-compound-statement

2017-06-25 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is
legacy. Anyway such code is not appropriate for Linux kernel.

Signed-off-by: Eugene Korenevsky <ekorenev...@gmail.com>
---
Changes in v7: rename hub_check_descriptor_sanity -> hub_descriptor_is_sane
Changes in v6: more pedantic conversion from `int` to `bool`; fix comment
Changes in v5: make `bool` a return type of `hub_check_descriptor_sanity()`
Changes in v4: fix typo
Changes in v3: extract the code to static function
Changes in v2: fix spaces instead of tab, add missing 'Signed-off-by'

 drivers/usb/core/hub.c | 38 +-
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index b8bb20d7acdb..0484cd348156 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1661,10 +1661,28 @@ static void hub_disconnect(struct usb_interface *intf)
kref_put(>kref, hub_release);
 }
 
+static bool hub_descriptor_is_sane(struct usb_host_interface *desc)
+{
+   /* Some hubs have a subclass of 1, which AFAICT according to the */
+   /*  specs is not defined, but it works */
+   if (desc->desc.bInterfaceSubClass != 0 &&
+   desc->desc.bInterfaceSubClass != 1)
+   return false;
+
+   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
+   if (desc->desc.bNumEndpoints != 1)
+   return false;
+
+   /* If the first endpoint is not interrupt IN, we'd better punt! */
+   if (!usb_endpoint_is_int_in(>endpoint[0].desc))
+   return false;
+
+return true;
+}
+
 static int hub_probe(struct usb_interface *intf, const struct usb_device_id 
*id)
 {
struct usb_host_interface *desc;
-   struct usb_endpoint_descriptor *endpoint;
struct usb_device *hdev;
struct usb_hub *hub;
 
@@ -1739,25 +1757,11 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
-   /* Some hubs have a subclass of 1, which AFAICT according to the */
-   /*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+   if (!hub_descriptor_is_sane(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
@@ -1784,7 +1788,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
hub->quirk_check_port_auto_suspend = 1;
 
-   if (hub_configure(hub, endpoint) >= 0)
+   if (hub_configure(hub, >endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.13.1



[PATCH] [PATCH v7] USB hub_probe: rework ugly goto-into-compound-statement

2017-06-25 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is
legacy. Anyway such code is not appropriate for Linux kernel.

Signed-off-by: Eugene Korenevsky 
---
Changes in v7: rename hub_check_descriptor_sanity -> hub_descriptor_is_sane
Changes in v6: more pedantic conversion from `int` to `bool`; fix comment
Changes in v5: make `bool` a return type of `hub_check_descriptor_sanity()`
Changes in v4: fix typo
Changes in v3: extract the code to static function
Changes in v2: fix spaces instead of tab, add missing 'Signed-off-by'

 drivers/usb/core/hub.c | 38 +-
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index b8bb20d7acdb..0484cd348156 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1661,10 +1661,28 @@ static void hub_disconnect(struct usb_interface *intf)
kref_put(>kref, hub_release);
 }
 
+static bool hub_descriptor_is_sane(struct usb_host_interface *desc)
+{
+   /* Some hubs have a subclass of 1, which AFAICT according to the */
+   /*  specs is not defined, but it works */
+   if (desc->desc.bInterfaceSubClass != 0 &&
+   desc->desc.bInterfaceSubClass != 1)
+   return false;
+
+   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
+   if (desc->desc.bNumEndpoints != 1)
+   return false;
+
+   /* If the first endpoint is not interrupt IN, we'd better punt! */
+   if (!usb_endpoint_is_int_in(>endpoint[0].desc))
+   return false;
+
+return true;
+}
+
 static int hub_probe(struct usb_interface *intf, const struct usb_device_id 
*id)
 {
struct usb_host_interface *desc;
-   struct usb_endpoint_descriptor *endpoint;
struct usb_device *hdev;
struct usb_hub *hub;
 
@@ -1739,25 +1757,11 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
-   /* Some hubs have a subclass of 1, which AFAICT according to the */
-   /*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+   if (!hub_descriptor_is_sane(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
@@ -1784,7 +1788,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
hub->quirk_check_port_auto_suspend = 1;
 
-   if (hub_configure(hub, endpoint) >= 0)
+   if (hub_configure(hub, >endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.13.1



[PATCH v6] USB hub_probe: rework ugly goto-into-compound-statement

2016-11-18 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is
legacy. Anyway such code is not appropriate for Linux kernel.

Signed-off-by: Eugene Korenevsky <ekorenev...@gmail.com>
---
Changes in v6: more pedantic conversion from `int` to `bool`; fix comment
Changes in v5: make `bool` a return type of `hub_check_descriptor_sanity()`
Changes in v4: fix typo
Changes in v3: extract the code to static function
Changes in v2: fix spaces instead of tab, add missing 'Signed-off-by'

 drivers/usb/core/hub.c | 38 +-
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..dbebfe4 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1722,10 +1722,28 @@ static void hub_disconnect(struct usb_interface *intf)
kref_put(>kref, hub_release);
 }
 
+static bool hub_check_descriptor_sanity(struct usb_host_interface *desc)
+{
+   /* Some hubs have a subclass of 1, which AFAICT according to the */
+   /*  specs is not defined, but it works */
+   if (desc->desc.bInterfaceSubClass != 0 &&
+   desc->desc.bInterfaceSubClass != 1)
+   return false;
+
+   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
+   if (desc->desc.bNumEndpoints != 1)
+   return false;
+
+   /* If the first endpoint is not interrupt IN, we'd better punt! */
+   if (!usb_endpoint_is_int_in(>endpoint[0].desc))
+   return false;
+
+return true;
+}
+
 static int hub_probe(struct usb_interface *intf, const struct usb_device_id 
*id)
 {
struct usb_host_interface *desc;
-   struct usb_endpoint_descriptor *endpoint;
struct usb_device *hdev;
struct usb_hub *hub;
 
@@ -1800,25 +1818,11 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
-   /* Some hubs have a subclass of 1, which AFAICT according to the */
-   /*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+   if (!hub_check_descriptor_sanity(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
@@ -1845,7 +1849,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
hub->quirk_check_port_auto_suspend = 1;
 
-   if (hub_configure(hub, endpoint) >= 0)
+   if (hub_configure(hub, >endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2




[PATCH v6] USB hub_probe: rework ugly goto-into-compound-statement

2016-11-18 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is
legacy. Anyway such code is not appropriate for Linux kernel.

Signed-off-by: Eugene Korenevsky 
---
Changes in v6: more pedantic conversion from `int` to `bool`; fix comment
Changes in v5: make `bool` a return type of `hub_check_descriptor_sanity()`
Changes in v4: fix typo
Changes in v3: extract the code to static function
Changes in v2: fix spaces instead of tab, add missing 'Signed-off-by'

 drivers/usb/core/hub.c | 38 +-
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..dbebfe4 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1722,10 +1722,28 @@ static void hub_disconnect(struct usb_interface *intf)
kref_put(>kref, hub_release);
 }
 
+static bool hub_check_descriptor_sanity(struct usb_host_interface *desc)
+{
+   /* Some hubs have a subclass of 1, which AFAICT according to the */
+   /*  specs is not defined, but it works */
+   if (desc->desc.bInterfaceSubClass != 0 &&
+   desc->desc.bInterfaceSubClass != 1)
+   return false;
+
+   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
+   if (desc->desc.bNumEndpoints != 1)
+   return false;
+
+   /* If the first endpoint is not interrupt IN, we'd better punt! */
+   if (!usb_endpoint_is_int_in(>endpoint[0].desc))
+   return false;
+
+return true;
+}
+
 static int hub_probe(struct usb_interface *intf, const struct usb_device_id 
*id)
 {
struct usb_host_interface *desc;
-   struct usb_endpoint_descriptor *endpoint;
struct usb_device *hdev;
struct usb_hub *hub;
 
@@ -1800,25 +1818,11 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
-   /* Some hubs have a subclass of 1, which AFAICT according to the */
-   /*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+   if (!hub_check_descriptor_sanity(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
@@ -1845,7 +1849,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
hub->quirk_check_port_auto_suspend = 1;
 
-   if (hub_configure(hub, endpoint) >= 0)
+   if (hub_configure(hub, >endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2




Re: [PATCH v5] USB hub_probe: rework ugly goto-into-compound-statement

2016-11-18 Thread Eugene Korenevsky
> Ok, I'm going to be really pedantic here and ask that you spell this
> last statement out:
>if (usb...)
> return true;
>return false;


> 
> Also, the comment should say:
>   /* If the first endpoint is not interrupt IN, we... */
> 

It's better to inverse the condition and return false:

```
if (!usb...)
return false;
return true;
```

This is exactly what is said in the comment ("If the first endpoint...
we'd better punt!"). And does not break the composition of the entire
function (all `if` bodies return false, last statement is `return
true`).

-- 
Eugene



Re: [PATCH v5] USB hub_probe: rework ugly goto-into-compound-statement

2016-11-18 Thread Eugene Korenevsky
> Ok, I'm going to be really pedantic here and ask that you spell this
> last statement out:
>if (usb...)
> return true;
>return false;


> 
> Also, the comment should say:
>   /* If the first endpoint is not interrupt IN, we... */
> 

It's better to inverse the condition and return false:

```
if (!usb...)
return false;
return true;
```

This is exactly what is said in the comment ("If the first endpoint...
we'd better punt!"). And does not break the composition of the entire
function (all `if` bodies return false, last statement is `return
true`).

-- 
Eugene



[PATCH v5] USB hub_probe: rework ugly goto-into-compound-statement

2016-11-13 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is
legacy. Anyway such code is not appropriate for Linux kernel.

Signed-off-by: Eugene Korenevsky <ekorenev...@gmail.com>
---
Changes in v5: make `bool` a return type of `hub_check_descriptor_sanity()`
Changes in v4: fix typo
Changes in v3: extract the code to static function
Changes in v2: fix spaces instead of tab, add missing 'Signed-off-by'

 drivers/usb/core/hub.c | 35 ++-
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..1a316a1 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1722,10 +1722,25 @@ static void hub_disconnect(struct usb_interface *intf)
kref_put(>kref, hub_release);
 }
 
+static bool hub_check_descriptor_sanity(struct usb_host_interface *desc)
+{
+   /* Some hubs have a subclass of 1, which AFAICT according to the */
+   /*  specs is not defined, but it works */
+   if (desc->desc.bInterfaceSubClass != 0 &&
+   desc->desc.bInterfaceSubClass != 1)
+   return false;
+
+   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
+   if (desc->desc.bNumEndpoints != 1)
+   return false;
+
+   /* If it's not an interrupt in endpoint, we'd better punt! */
+   return usb_endpoint_is_int_in(>endpoint[0].desc) != 0;
+}
+
 static int hub_probe(struct usb_interface *intf, const struct usb_device_id 
*id)
 {
struct usb_host_interface *desc;
-   struct usb_endpoint_descriptor *endpoint;
struct usb_device *hdev;
struct usb_hub *hub;
 
@@ -1800,25 +1815,11 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
-   /* Some hubs have a subclass of 1, which AFAICT according to the */
-   /*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+   if (!hub_check_descriptor_sanity(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
@@ -1845,7 +1846,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
hub->quirk_check_port_auto_suspend = 1;
 
-   if (hub_configure(hub, endpoint) >= 0)
+   if (hub_configure(hub, >endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2



[PATCH v5] USB hub_probe: rework ugly goto-into-compound-statement

2016-11-13 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is
legacy. Anyway such code is not appropriate for Linux kernel.

Signed-off-by: Eugene Korenevsky 
---
Changes in v5: make `bool` a return type of `hub_check_descriptor_sanity()`
Changes in v4: fix typo
Changes in v3: extract the code to static function
Changes in v2: fix spaces instead of tab, add missing 'Signed-off-by'

 drivers/usb/core/hub.c | 35 ++-
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..1a316a1 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1722,10 +1722,25 @@ static void hub_disconnect(struct usb_interface *intf)
kref_put(>kref, hub_release);
 }
 
+static bool hub_check_descriptor_sanity(struct usb_host_interface *desc)
+{
+   /* Some hubs have a subclass of 1, which AFAICT according to the */
+   /*  specs is not defined, but it works */
+   if (desc->desc.bInterfaceSubClass != 0 &&
+   desc->desc.bInterfaceSubClass != 1)
+   return false;
+
+   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
+   if (desc->desc.bNumEndpoints != 1)
+   return false;
+
+   /* If it's not an interrupt in endpoint, we'd better punt! */
+   return usb_endpoint_is_int_in(>endpoint[0].desc) != 0;
+}
+
 static int hub_probe(struct usb_interface *intf, const struct usb_device_id 
*id)
 {
struct usb_host_interface *desc;
-   struct usb_endpoint_descriptor *endpoint;
struct usb_device *hdev;
struct usb_hub *hub;
 
@@ -1800,25 +1815,11 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
-   /* Some hubs have a subclass of 1, which AFAICT according to the */
-   /*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+   if (!hub_check_descriptor_sanity(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
@@ -1845,7 +1846,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
hub->quirk_check_port_auto_suspend = 1;
 
-   if (hub_configure(hub, endpoint) >= 0)
+   if (hub_configure(hub, >endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2



[PATCH v4] USB hub_probe: rework ugly goto-into-compound-statement

2016-11-09 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is
legacy. Anyway such code is not appropriate for Linux kernel.

Changes since v3: fix typo
Changes since v2: extract the code to static function
Changes since v1: fix spaces instead of tab, add missing 'Signed-off-by'

Signed-off-by: Eugene Korenevsky <ekorenev...@gmail.com>
---
 drivers/usb/core/hub.c | 35 ++-
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..b770c8d 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1722,10 +1722,25 @@ static void hub_disconnect(struct usb_interface *intf)
kref_put(>kref, hub_release);
 }
 
+static int hub_check_descriptor_sanity(struct usb_host_interface *desc)
+{
+   /* Some hubs have a subclass of 1, which AFAICT according to the */
+   /*  specs is not defined, but it works */
+   if (desc->desc.bInterfaceSubClass != 0 &&
+   desc->desc.bInterfaceSubClass != 1)
+   return 0;
+
+   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
+   if (desc->desc.bNumEndpoints != 1)
+   return 0;
+
+   /* If it's not an interrupt in endpoint, we'd better punt! */
+   return usb_endpoint_is_int_in(>endpoint[0].desc);
+}
+
 static int hub_probe(struct usb_interface *intf, const struct usb_device_id 
*id)
 {
struct usb_host_interface *desc;
-   struct usb_endpoint_descriptor *endpoint;
struct usb_device *hdev;
struct usb_hub *hub;
 
@@ -1800,25 +1815,11 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
-   /* Some hubs have a subclass of 1, which AFAICT according to the */
-   /*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+   if (!hub_check_descriptor_sanity(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
@@ -1845,7 +1846,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
hub->quirk_check_port_auto_suspend = 1;
 
-   if (hub_configure(hub, endpoint) >= 0)
+   if (hub_configure(hub, >endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2




[PATCH v4] USB hub_probe: rework ugly goto-into-compound-statement

2016-11-09 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is
legacy. Anyway such code is not appropriate for Linux kernel.

Changes since v3: fix typo
Changes since v2: extract the code to static function
Changes since v1: fix spaces instead of tab, add missing 'Signed-off-by'

Signed-off-by: Eugene Korenevsky 
---
 drivers/usb/core/hub.c | 35 ++-
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..b770c8d 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1722,10 +1722,25 @@ static void hub_disconnect(struct usb_interface *intf)
kref_put(>kref, hub_release);
 }
 
+static int hub_check_descriptor_sanity(struct usb_host_interface *desc)
+{
+   /* Some hubs have a subclass of 1, which AFAICT according to the */
+   /*  specs is not defined, but it works */
+   if (desc->desc.bInterfaceSubClass != 0 &&
+   desc->desc.bInterfaceSubClass != 1)
+   return 0;
+
+   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
+   if (desc->desc.bNumEndpoints != 1)
+   return 0;
+
+   /* If it's not an interrupt in endpoint, we'd better punt! */
+   return usb_endpoint_is_int_in(>endpoint[0].desc);
+}
+
 static int hub_probe(struct usb_interface *intf, const struct usb_device_id 
*id)
 {
struct usb_host_interface *desc;
-   struct usb_endpoint_descriptor *endpoint;
struct usb_device *hdev;
struct usb_hub *hub;
 
@@ -1800,25 +1815,11 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
-   /* Some hubs have a subclass of 1, which AFAICT according to the */
-   /*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+   if (!hub_check_descriptor_sanity(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
@@ -1845,7 +1846,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
hub->quirk_check_port_auto_suspend = 1;
 
-   if (hub_configure(hub, endpoint) >= 0)
+   if (hub_configure(hub, >endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2




[PATCH v3] USB hub_probe: rework ugly goto-into-compound-statement

2016-11-08 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is
legacy. Anyway such code is not appropriate for Linux kernel.

Changes since v2: extract the code to static function
Changes since v1: fix spaces instead of tab, add missing 'Signed-off-by'

Signed-off-by: Eugene Korenevsky <ekorenev...@gmail.com>
---
 drivers/usb/core/hub.c | 35 ++-
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..aca8e86 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1722,10 +1722,25 @@ static void hub_disconnect(struct usb_interface *intf)
kref_put(>kref, hub_release);
 }
 
+static int hub_check_descriptor_sanity(struct usb_host_interface *desc)
+{
+   /* Some hubs have a subclass of 1, which AFAICT according to the */
+   /*  specs is not defined, but it works */
+   if (desc->desc.bInterfaceSubClass != 1 &&
+   desc->desc.bInterfaceSubClass != 2)
+   return 0;
+
+   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
+   if (desc->desc.bNumEndpoints != 1)
+   return 0;
+
+   /* If it's not an interrupt in endpoint, we'd better punt! */
+   return usb_endpoint_is_int_in(>endpoint[0].desc);
+}
+
 static int hub_probe(struct usb_interface *intf, const struct usb_device_id 
*id)
 {
struct usb_host_interface *desc;
-   struct usb_endpoint_descriptor *endpoint;
struct usb_device *hdev;
struct usb_hub *hub;
 
@@ -1800,25 +1815,11 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
-   /* Some hubs have a subclass of 1, which AFAICT according to the */
-   /*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+   if (!hub_check_descriptor_sanity(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
@@ -1845,7 +1846,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
hub->quirk_check_port_auto_suspend = 1;
 
-   if (hub_configure(hub, endpoint) >= 0)
+   if (hub_configure(hub, >endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2


-- 
Eugene



[PATCH v3] USB hub_probe: rework ugly goto-into-compound-statement

2016-11-08 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is
legacy. Anyway such code is not appropriate for Linux kernel.

Changes since v2: extract the code to static function
Changes since v1: fix spaces instead of tab, add missing 'Signed-off-by'

Signed-off-by: Eugene Korenevsky 
---
 drivers/usb/core/hub.c | 35 ++-
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..aca8e86 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1722,10 +1722,25 @@ static void hub_disconnect(struct usb_interface *intf)
kref_put(>kref, hub_release);
 }
 
+static int hub_check_descriptor_sanity(struct usb_host_interface *desc)
+{
+   /* Some hubs have a subclass of 1, which AFAICT according to the */
+   /*  specs is not defined, but it works */
+   if (desc->desc.bInterfaceSubClass != 1 &&
+   desc->desc.bInterfaceSubClass != 2)
+   return 0;
+
+   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
+   if (desc->desc.bNumEndpoints != 1)
+   return 0;
+
+   /* If it's not an interrupt in endpoint, we'd better punt! */
+   return usb_endpoint_is_int_in(>endpoint[0].desc);
+}
+
 static int hub_probe(struct usb_interface *intf, const struct usb_device_id 
*id)
 {
struct usb_host_interface *desc;
-   struct usb_endpoint_descriptor *endpoint;
struct usb_device *hdev;
struct usb_hub *hub;
 
@@ -1800,25 +1815,11 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
-   /* Some hubs have a subclass of 1, which AFAICT according to the */
-   /*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+   if (!hub_check_descriptor_sanity(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
@@ -1845,7 +1846,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
hub->quirk_check_port_auto_suspend = 1;
 
-   if (hub_configure(hub, endpoint) >= 0)
+   if (hub_configure(hub, >endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2


-- 
Eugene



[PATCH v3 2/2] USB hub_probe: put initialization before usage

2016-11-07 Thread Eugene Korenevsky
Minor optimization: move initialization immediately before usage.
This gives a chance for more accurate register allocation by the compiler.

Signed-off-by: Eugene Korenevsky <ekorenev...@gmail.com>
---
 drivers/usb/core/hub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 7a20980..c7f6b5f 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1742,7 +1742,6 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
struct usb_device *hdev;
struct usb_hub *hub;
 
-   desc = intf->cur_altsetting;
hdev = interface_to_usbdev(intf);
 
/*
@@ -1813,6 +1812,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
+   desc = intf->cur_altsetting;
if (!hub_check_descriptor_sanity(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
-- 
2.10.2



[PATCH v3 2/2] USB hub_probe: put initialization before usage

2016-11-07 Thread Eugene Korenevsky
Minor optimization: move initialization immediately before usage.
This gives a chance for more accurate register allocation by the compiler.

Signed-off-by: Eugene Korenevsky 
---
 drivers/usb/core/hub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 7a20980..c7f6b5f 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1742,7 +1742,6 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
struct usb_device *hdev;
struct usb_hub *hub;
 
-   desc = intf->cur_altsetting;
hdev = interface_to_usbdev(intf);
 
/*
@@ -1813,6 +1812,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
+   desc = intf->cur_altsetting;
if (!hub_check_descriptor_sanity(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
-- 
2.10.2



[PATCH v3 1/2] USB hub_probe: rework ugly goto-into-compound-statement

2016-11-07 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is
legacy. Anyway such code is not appropriate for Linux kernel.

Signed-off-by: Eugene Korenevsky <ekorenev...@gmail.com>
---
 drivers/usb/core/hub.c | 33 -
 1 file changed, 16 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..7a20980 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1722,10 +1722,23 @@ static void hub_disconnect(struct usb_interface *intf)
kref_put(>kref, hub_release);
 }
 
+static int hub_check_descriptor_sanity(struct usb_host_interface *desc)
+{
+   /* Some hubs have a subclass of 1, which AFAICT according to the */
+   /*  specs is not defined, but it works */
+   if (desc->desc.bInterfaceSubClass != 1 &&
+   desc->desc.bInterfaceSubClass != 2)
+   return 0;
+   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
+   if (desc->desc.bNumEndpoints != 1)
+   return 0;
+   /* If it's not an interrupt in endpoint, we'd better punt! */
+   return usb_endpoint_is_int_in(>endpoint[0].desc);
+}
+
 static int hub_probe(struct usb_interface *intf, const struct usb_device_id 
*id)
 {
struct usb_host_interface *desc;
-   struct usb_endpoint_descriptor *endpoint;
struct usb_device *hdev;
struct usb_hub *hub;
 
@@ -1800,25 +1813,11 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
-   /* Some hubs have a subclass of 1, which AFAICT according to the */
-   /*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+   if (!hub_check_descriptor_sanity(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
@@ -1845,7 +1844,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
hub->quirk_check_port_auto_suspend = 1;
 
-   if (hub_configure(hub, endpoint) >= 0)
+   if (hub_configure(hub, >endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2



[PATCH v3 1/2] USB hub_probe: rework ugly goto-into-compound-statement

2016-11-07 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is
legacy. Anyway such code is not appropriate for Linux kernel.

Signed-off-by: Eugene Korenevsky 
---
 drivers/usb/core/hub.c | 33 -
 1 file changed, 16 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..7a20980 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1722,10 +1722,23 @@ static void hub_disconnect(struct usb_interface *intf)
kref_put(>kref, hub_release);
 }
 
+static int hub_check_descriptor_sanity(struct usb_host_interface *desc)
+{
+   /* Some hubs have a subclass of 1, which AFAICT according to the */
+   /*  specs is not defined, but it works */
+   if (desc->desc.bInterfaceSubClass != 1 &&
+   desc->desc.bInterfaceSubClass != 2)
+   return 0;
+   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
+   if (desc->desc.bNumEndpoints != 1)
+   return 0;
+   /* If it's not an interrupt in endpoint, we'd better punt! */
+   return usb_endpoint_is_int_in(>endpoint[0].desc);
+}
+
 static int hub_probe(struct usb_interface *intf, const struct usb_device_id 
*id)
 {
struct usb_host_interface *desc;
-   struct usb_endpoint_descriptor *endpoint;
struct usb_device *hdev;
struct usb_hub *hub;
 
@@ -1800,25 +1813,11 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
}
 #endif
 
-   /* Some hubs have a subclass of 1, which AFAICT according to the */
-   /*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+   if (!hub_check_descriptor_sanity(desc)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
@@ -1845,7 +1844,7 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
hub->quirk_check_port_auto_suspend = 1;
 
-   if (hub_configure(hub, endpoint) >= 0)
+   if (hub_configure(hub, >endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2



Re: [PATCH v2] USB hub_probe: remove ugly goto-into-compound-statement

2016-11-07 Thread Eugene Korenevsky
On Mon, Nov 07, 2016 at 10:09:17AM +0100, Greg Kroah-Hartman wrote:

> > Rework smelling code (goto inside compound statement). Perhaps this is 
> > legacy.
> > Anyway such code is not appropriate for Linux kernel.
> > 
> > Signed-off-by: Eugene Korenevsky <ekorenev...@gmail.com>
> > ---
> >  drivers/usb/core/hub.c | 24 +++-
> >  1 file changed, 11 insertions(+), 13 deletions(-)
> 
> What changed from v1?

Fixed faults: missed 'Signed-off-by', spaces instead of tab.


> > diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
> > index cbb1467..4081672 100644
> > --- a/drivers/usb/core/hub.c
> > +++ b/drivers/usb/core/hub.c
> > @@ -1802,23 +1802,21 @@ static int hub_probe(struct usb_interface *intf, 
> > const struct usb_device_id *id)
> >  
> > /* Some hubs have a subclass of 1, which AFAICT according to the */
> > /*  specs is not defined, but it works */
> > -   if ((desc->desc.bInterfaceSubClass != 0) &&
> > -   (desc->desc.bInterfaceSubClass != 1)) {
> > -descriptor_error:
> > +
> > +   /* Reject in following cases:
> > +* - Interface subclass is not 0 or 1
> > +* - Multiple endpoints
> > +* - Not an interrupt in endpoint
> > +*/
> > +   endpoint = >endpoint[0].desc;
> > +   if ((desc->desc.bInterfaceSubClass != 0 &&
> > +desc->desc.bInterfaceSubClass != 1) ||
> > +   desc->desc.bNumEndpoints != 1 ||
> > +   !usb_endpoint_is_int_in(endpoint)) {
> > dev_err(>dev, "bad descriptor, ignoring hub\n");
> > return -EIO;
> > }
> >  
> > -   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
> > -   if (desc->desc.bNumEndpoints != 1)
> > -   goto descriptor_error;
> > -
> > -   endpoint = >endpoint[0].desc;
> > -
> > -   /* If it's not an interrupt in endpoint, we'd better punt! */
> > -   if (!usb_endpoint_is_int_in(endpoint))
> > -   goto descriptor_error;
> > -
> 
> As "horrible" as the original code might be, it's much easier to read
> and follow, which is the key thing here, right?  What's so bad about a
> goto backwards?

OK, this patch is still not perfect. But jumping *back* into
*compound* *statement* hurts reader's eyes. Really. Maybe it's better to
extract this code to static function, compiler will inline it. See v3
patchset.

-- 
Eugene



Re: [PATCH v2] USB hub_probe: remove ugly goto-into-compound-statement

2016-11-07 Thread Eugene Korenevsky
On Mon, Nov 07, 2016 at 10:09:17AM +0100, Greg Kroah-Hartman wrote:

> > Rework smelling code (goto inside compound statement). Perhaps this is 
> > legacy.
> > Anyway such code is not appropriate for Linux kernel.
> > 
> > Signed-off-by: Eugene Korenevsky 
> > ---
> >  drivers/usb/core/hub.c | 24 +++-
> >  1 file changed, 11 insertions(+), 13 deletions(-)
> 
> What changed from v1?

Fixed faults: missed 'Signed-off-by', spaces instead of tab.


> > diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
> > index cbb1467..4081672 100644
> > --- a/drivers/usb/core/hub.c
> > +++ b/drivers/usb/core/hub.c
> > @@ -1802,23 +1802,21 @@ static int hub_probe(struct usb_interface *intf, 
> > const struct usb_device_id *id)
> >  
> > /* Some hubs have a subclass of 1, which AFAICT according to the */
> > /*  specs is not defined, but it works */
> > -   if ((desc->desc.bInterfaceSubClass != 0) &&
> > -   (desc->desc.bInterfaceSubClass != 1)) {
> > -descriptor_error:
> > +
> > +   /* Reject in following cases:
> > +* - Interface subclass is not 0 or 1
> > +* - Multiple endpoints
> > +* - Not an interrupt in endpoint
> > +*/
> > +   endpoint = >endpoint[0].desc;
> > +   if ((desc->desc.bInterfaceSubClass != 0 &&
> > +desc->desc.bInterfaceSubClass != 1) ||
> > +   desc->desc.bNumEndpoints != 1 ||
> > +   !usb_endpoint_is_int_in(endpoint)) {
> > dev_err(>dev, "bad descriptor, ignoring hub\n");
> > return -EIO;
> > }
> >  
> > -   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
> > -   if (desc->desc.bNumEndpoints != 1)
> > -   goto descriptor_error;
> > -
> > -   endpoint = >endpoint[0].desc;
> > -
> > -   /* If it's not an interrupt in endpoint, we'd better punt! */
> > -   if (!usb_endpoint_is_int_in(endpoint))
> > -   goto descriptor_error;
> > -
> 
> As "horrible" as the original code might be, it's much easier to read
> and follow, which is the key thing here, right?  What's so bad about a
> goto backwards?

OK, this patch is still not perfect. But jumping *back* into
*compound* *statement* hurts reader's eyes. Really. Maybe it's better to
extract this code to static function, compiler will inline it. See v3
patchset.

-- 
Eugene



[PATCH v2] USB hub_probe: remove ugly goto-into-compound-statement

2016-11-02 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is legacy.
Anyway such code is not appropriate for Linux kernel.

Signed-off-by: Eugene Korenevsky <ekorenev...@gmail.com>
---
 drivers/usb/core/hub.c | 24 +++-
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..4081672 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1802,23 +1802,21 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
 
/* Some hubs have a subclass of 1, which AFAICT according to the */
/*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+
+   /* Reject in following cases:
+* - Interface subclass is not 0 or 1
+* - Multiple endpoints
+* - Not an interrupt in endpoint
+*/
+   endpoint = >endpoint[0].desc;
+   if ((desc->desc.bInterfaceSubClass != 0 &&
+desc->desc.bInterfaceSubClass != 1) ||
+   desc->desc.bNumEndpoints != 1 ||
+   !usb_endpoint_is_int_in(endpoint)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
-- 
2.10.2



[PATCH v2] USB hub_probe: remove ugly goto-into-compound-statement

2016-11-02 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is legacy.
Anyway such code is not appropriate for Linux kernel.

Signed-off-by: Eugene Korenevsky 
---
 drivers/usb/core/hub.c | 24 +++-
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..4081672 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1802,23 +1802,21 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
 
/* Some hubs have a subclass of 1, which AFAICT according to the */
/*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+
+   /* Reject in following cases:
+* - Interface subclass is not 0 or 1
+* - Multiple endpoints
+* - Not an interrupt in endpoint
+*/
+   endpoint = >endpoint[0].desc;
+   if ((desc->desc.bInterfaceSubClass != 0 &&
+desc->desc.bInterfaceSubClass != 1) ||
+   desc->desc.bNumEndpoints != 1 ||
+   !usb_endpoint_is_int_in(endpoint)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
-- 
2.10.2



[PATCH] USB hub_probe: refactor ugly goto-into-compound-statement

2016-11-02 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is legacy.
Anyway such code is not appropriate for Linux kernel.

---
 drivers/usb/core/hub.c | 24 +++-
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..abd786b 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1802,23 +1802,21 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
 
/* Some hubs have a subclass of 1, which AFAICT according to the */
/*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+
+   /* Reject in following cases:
+* - Interface subclass is not 0 or 1
+* - Multiple endpoints
+* - Not an interrupt in endpoint
+*/
+   endpoint = >endpoint[0].desc;
+   if ((desc->desc.bInterfaceSubClass != 0 &&
+desc->desc.bInterfaceSubClass != 1) ||
+desc->desc.bNumEndpoints != 1 ||
+!usb_endpoint_is_int_in(endpoint)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
-- 
2.10.2



[PATCH] USB hub_probe: refactor ugly goto-into-compound-statement

2016-11-02 Thread Eugene Korenevsky
Rework smelling code (goto inside compound statement). Perhaps this is legacy.
Anyway such code is not appropriate for Linux kernel.

---
 drivers/usb/core/hub.c | 24 +++-
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index cbb1467..abd786b 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1802,23 +1802,21 @@ static int hub_probe(struct usb_interface *intf, const 
struct usb_device_id *id)
 
/* Some hubs have a subclass of 1, which AFAICT according to the */
/*  specs is not defined, but it works */
-   if ((desc->desc.bInterfaceSubClass != 0) &&
-   (desc->desc.bInterfaceSubClass != 1)) {
-descriptor_error:
+
+   /* Reject in following cases:
+* - Interface subclass is not 0 or 1
+* - Multiple endpoints
+* - Not an interrupt in endpoint
+*/
+   endpoint = >endpoint[0].desc;
+   if ((desc->desc.bInterfaceSubClass != 0 &&
+desc->desc.bInterfaceSubClass != 1) ||
+desc->desc.bNumEndpoints != 1 ||
+!usb_endpoint_is_int_in(endpoint)) {
dev_err(>dev, "bad descriptor, ignoring hub\n");
return -EIO;
}
 
-   /* Multiple endpoints? What kind of mutant ninja-hub is this? */
-   if (desc->desc.bNumEndpoints != 1)
-   goto descriptor_error;
-
-   endpoint = >endpoint[0].desc;
-
-   /* If it's not an interrupt in endpoint, we'd better punt! */
-   if (!usb_endpoint_is_int_in(endpoint))
-   goto descriptor_error;
-
/* We found a hub */
dev_info(>dev, "USB hub found\n");
 
-- 
2.10.2



[PATCH] EFI loader: remove pointless "if" statement

2016-11-02 Thread Eugene Korenevsky
An obvious cleanup. "if" statement does nothing and should be removed.

---
 arch/x86/boot/compressed/eboot.c | 6 +-
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
index cc69e37..202f2aa 100644
--- a/arch/x86/boot/compressed/eboot.c
+++ b/arch/x86/boot/compressed/eboot.c
@@ -1054,11 +1054,7 @@ static efi_status_t exit_boot(struct boot_params 
*boot_params,
/* Historic? */
boot_params->alt_mem_k = 32 * 1024;
 
-   status = setup_e820(boot_params, e820ext, e820ext_size);
-   if (status != EFI_SUCCESS)
-   return status;
-
-   return EFI_SUCCESS;
+   return setup_e820(boot_params, e820ext, e820ext_size);
 }
 
 /*
-- 
2.10.2



[PATCH] EFI loader: remove pointless "if" statement

2016-11-02 Thread Eugene Korenevsky
An obvious cleanup. "if" statement does nothing and should be removed.

---
 arch/x86/boot/compressed/eboot.c | 6 +-
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
index cc69e37..202f2aa 100644
--- a/arch/x86/boot/compressed/eboot.c
+++ b/arch/x86/boot/compressed/eboot.c
@@ -1054,11 +1054,7 @@ static efi_status_t exit_boot(struct boot_params 
*boot_params,
/* Historic? */
boot_params->alt_mem_k = 32 * 1024;
 
-   status = setup_e820(boot_params, e820ext, e820ext_size);
-   if (status != EFI_SUCCESS)
-   return status;
-
-   return EFI_SUCCESS;
+   return setup_e820(boot_params, e820ext, e820ext_size);
 }
 
 /*
-- 
2.10.2



Re: [PATCH v2] EFI loader: remove redundant code

2016-11-02 Thread Eugene Korenevsky
> > *e820ext is always NULL in 'alloc_e820ext()' (see the code of 
> > 'exit_boot()').
> > Without loss of generality we can replace freeing with returning
> > EFI_INVALID_PARAMETER. So if the caller would ever incorrectly pass non-NULL
> > *e820ext, he will obtain a returned error code.
> >
> 
> What exactly are you trying to fix here? Adding new artificial failure
> modes is hardly worth it when all you are doing is cleaning up code
> that by itself is correct to begin with, but is simply never called.

This code (free_pool, assignments) is dead whether it is correct or
not. So it is to be removed.
The check gives some assurance that memory is not leaked if the calling
code is changed.


> > @@ -956,11 +956,8 @@ static efi_status_t alloc_e820ext(u32 nr_desc, struct 
> > setup_data **e820ext,
> > size = sizeof(struct setup_data) +
> > sizeof(struct e820entry) * nr_desc;
> >
> > -   if (*e820ext) {
> > -   efi_call_early(free_pool, *e820ext);
> > -   *e820ext = NULL;
> > -   *e820ext_size = 0;
> > -   }
> > +   if (*e820ext)
> > +   return EFI_INVALID_PARAMETER;
> >
> > status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
> > size, (void **)e820ext);
> > --


-- 
Eugene



Re: [PATCH v2] EFI loader: remove redundant code

2016-11-02 Thread Eugene Korenevsky
> > *e820ext is always NULL in 'alloc_e820ext()' (see the code of 
> > 'exit_boot()').
> > Without loss of generality we can replace freeing with returning
> > EFI_INVALID_PARAMETER. So if the caller would ever incorrectly pass non-NULL
> > *e820ext, he will obtain a returned error code.
> >
> 
> What exactly are you trying to fix here? Adding new artificial failure
> modes is hardly worth it when all you are doing is cleaning up code
> that by itself is correct to begin with, but is simply never called.

This code (free_pool, assignments) is dead whether it is correct or
not. So it is to be removed.
The check gives some assurance that memory is not leaked if the calling
code is changed.


> > @@ -956,11 +956,8 @@ static efi_status_t alloc_e820ext(u32 nr_desc, struct 
> > setup_data **e820ext,
> > size = sizeof(struct setup_data) +
> > sizeof(struct e820entry) * nr_desc;
> >
> > -   if (*e820ext) {
> > -   efi_call_early(free_pool, *e820ext);
> > -   *e820ext = NULL;
> > -   *e820ext_size = 0;
> > -   }
> > +   if (*e820ext)
> > +   return EFI_INVALID_PARAMETER;
> >
> > status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
> > size, (void **)e820ext);
> > --


-- 
Eugene



Re: [PATCH] EFI loader: remove dead code

2016-11-01 Thread Eugene Korenevsky
Applied your notice. Sent v2 patch.

--
Eugene



Re: [PATCH] EFI loader: remove dead code

2016-11-01 Thread Eugene Korenevsky
Applied your notice. Sent v2 patch.

--
Eugene



[PATCH v2] EFI loader: remove redundant code

2016-11-01 Thread Eugene Korenevsky
*e820ext is always NULL in 'alloc_e820ext()' (see the code of 'exit_boot()').
Without loss of generality we can replace freeing with returning
EFI_INVALID_PARAMETER. So if the caller would ever incorrectly pass non-NULL
*e820ext, he will obtain a returned error code.

---
 arch/x86/boot/compressed/eboot.c | 7 ++-
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
index cc69e37..6cc66c7 100644
--- a/arch/x86/boot/compressed/eboot.c
+++ b/arch/x86/boot/compressed/eboot.c
@@ -956,11 +956,8 @@ static efi_status_t alloc_e820ext(u32 nr_desc, struct 
setup_data **e820ext,
size = sizeof(struct setup_data) +
sizeof(struct e820entry) * nr_desc;
 
-   if (*e820ext) {
-   efi_call_early(free_pool, *e820ext);
-   *e820ext = NULL;
-   *e820ext_size = 0;
-   }
+   if (*e820ext)
+   return EFI_INVALID_PARAMETER;
 
status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
size, (void **)e820ext);
-- 
2.10.2



[PATCH v2] EFI loader: remove redundant code

2016-11-01 Thread Eugene Korenevsky
*e820ext is always NULL in 'alloc_e820ext()' (see the code of 'exit_boot()').
Without loss of generality we can replace freeing with returning
EFI_INVALID_PARAMETER. So if the caller would ever incorrectly pass non-NULL
*e820ext, he will obtain a returned error code.

---
 arch/x86/boot/compressed/eboot.c | 7 ++-
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
index cc69e37..6cc66c7 100644
--- a/arch/x86/boot/compressed/eboot.c
+++ b/arch/x86/boot/compressed/eboot.c
@@ -956,11 +956,8 @@ static efi_status_t alloc_e820ext(u32 nr_desc, struct 
setup_data **e820ext,
size = sizeof(struct setup_data) +
sizeof(struct e820entry) * nr_desc;
 
-   if (*e820ext) {
-   efi_call_early(free_pool, *e820ext);
-   *e820ext = NULL;
-   *e820ext_size = 0;
-   }
+   if (*e820ext)
+   return EFI_INVALID_PARAMETER;
 
status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
size, (void **)e820ext);
-- 
2.10.2



[PATCH] EFI loader: remove dead code

2016-11-01 Thread Eugene Korenevsky
*e820ext is always NULL in 'alloc_e820ext()' (see the code of 'exit_boot()').
Therefore the 'if' condition is always false and the entire 'if' statement is
pointless. Remove it.

---
 arch/x86/boot/compressed/eboot.c | 6 --
 1 file changed, 6 deletions(-)

diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
index cc69e37..edfd4d6 100644
--- a/arch/x86/boot/compressed/eboot.c
+++ b/arch/x86/boot/compressed/eboot.c
@@ -956,12 +956,6 @@ static efi_status_t alloc_e820ext(u32 nr_desc, struct 
setup_data **e820ext,
size = sizeof(struct setup_data) +
sizeof(struct e820entry) * nr_desc;
 
-   if (*e820ext) {
-   efi_call_early(free_pool, *e820ext);
-   *e820ext = NULL;
-   *e820ext_size = 0;
-   }
-
status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
size, (void **)e820ext);
if (status == EFI_SUCCESS)
-- 
2.10.2



[PATCH] EFI loader: remove dead code

2016-11-01 Thread Eugene Korenevsky
*e820ext is always NULL in 'alloc_e820ext()' (see the code of 'exit_boot()').
Therefore the 'if' condition is always false and the entire 'if' statement is
pointless. Remove it.

---
 arch/x86/boot/compressed/eboot.c | 6 --
 1 file changed, 6 deletions(-)

diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
index cc69e37..edfd4d6 100644
--- a/arch/x86/boot/compressed/eboot.c
+++ b/arch/x86/boot/compressed/eboot.c
@@ -956,12 +956,6 @@ static efi_status_t alloc_e820ext(u32 nr_desc, struct 
setup_data **e820ext,
size = sizeof(struct setup_data) +
sizeof(struct e820entry) * nr_desc;
 
-   if (*e820ext) {
-   efi_call_early(free_pool, *e820ext);
-   *e820ext = NULL;
-   *e820ext_size = 0;
-   }
-
status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
size, (void **)e820ext);
if (status == EFI_SUCCESS)
-- 
2.10.2