[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(&hub->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(&desc->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(&intf->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 = &desc->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(&intf->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, &desc->endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.13.1

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[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(&hub->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(&desc->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(&intf->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 = &desc->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(&intf->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, &desc->endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2


--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


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

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[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(&hub->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(&desc->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(&intf->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 = &desc->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(&intf->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, &desc->endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[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(&hub->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(&desc->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(&intf->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 = &desc->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(&intf->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, &desc->endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2


--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[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(&hub->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(&desc->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(&intf->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 = &desc->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(&intf->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, &desc->endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2


-- 
Eugene

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[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(&intf->dev, "bad descriptor, ignoring hub\n");
return -EIO;
-- 
2.10.2

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[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(&hub->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(&desc->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(&intf->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 = &desc->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(&intf->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, &desc->endpoint[0].desc) >= 0)
return 0;
 
hub_disconnect(intf);
-- 
2.10.2

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


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 = &desc->endpoint[0].desc;
> > +   if ((desc->desc.bInterfaceSubClass != 0 &&
> > +desc->desc.bInterfaceSubClass != 1) ||
> > +   desc->desc.bNumEndpoints != 1 ||
> > +   !usb_endpoint_is_int_in(endpoint)) {
> > dev_err(&intf->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 = &desc->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

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[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 = &desc->endpoint[0].desc;
+   if ((desc->desc.bInterfaceSubClass != 0 &&
+desc->desc.bInterfaceSubClass != 1) ||
+   desc->desc.bNumEndpoints != 1 ||
+   !usb_endpoint_is_int_in(endpoint)) {
dev_err(&intf->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 = &desc->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(&intf->dev, "USB hub found\n");
 
-- 
2.10.2

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[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 = &desc->endpoint[0].desc;
+   if ((desc->desc.bInterfaceSubClass != 0 &&
+desc->desc.bInterfaceSubClass != 1) ||
+desc->desc.bNumEndpoints != 1 ||
+!usb_endpoint_is_int_in(endpoint)) {
dev_err(&intf->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 = &desc->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(&intf->dev, "USB hub found\n");
 
-- 
2.10.2

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html