2014-06-03 10:39:40

by Vivek Gautam

[permalink] [raw]
Subject: [PATCH] usb: dwc3: Keeping 'resource' related code together

Putting together the code related to getting the 'IORESOURCE_MEM'
and assigning the same to dwc->xhci_resources, for increasing
the readability.

Signed-off-by: Vivek Gautam <[email protected]>
---

Based on 'next' branch of Felipe's usb tree.
Also cleanly applies to 'usb-next' branch of Greg's usb tree.

drivers/usb/dwc3/core.c | 38 +++++++++++++++++++-------------------
1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index 3f59c12..4ca925d 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -656,6 +656,25 @@ static int dwc3_probe(struct platform_device *pdev)
return -ENODEV;
}

+ dwc->xhci_resources[0].start = res->start;
+ dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
+ DWC3_XHCI_REGS_END;
+ dwc->xhci_resources[0].flags = res->flags;
+ dwc->xhci_resources[0].name = res->name;
+
+ res->start += DWC3_GLOBALS_REGS_START;
+
+ /*
+ * Request memory region but exclude xHCI regs,
+ * since it will be requested by the xhci-plat driver.
+ */
+ regs = devm_ioremap_resource(dev, res);
+ if (IS_ERR(regs))
+ return PTR_ERR(regs);
+
+ dwc->regs = regs;
+ dwc->regs_size = resource_size(res);
+
if (node) {
dwc->maximum_speed = of_usb_get_maximum_speed(node);

@@ -676,28 +695,9 @@ static int dwc3_probe(struct platform_device *pdev)
if (ret)
return ret;

- dwc->xhci_resources[0].start = res->start;
- dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
- DWC3_XHCI_REGS_END;
- dwc->xhci_resources[0].flags = res->flags;
- dwc->xhci_resources[0].name = res->name;
-
- res->start += DWC3_GLOBALS_REGS_START;
-
- /*
- * Request memory region but exclude xHCI regs,
- * since it will be requested by the xhci-plat driver.
- */
- regs = devm_ioremap_resource(dev, res);
- if (IS_ERR(regs))
- return PTR_ERR(regs);
-
spin_lock_init(&dwc->lock);
platform_set_drvdata(pdev, dwc);

- dwc->regs = regs;
- dwc->regs_size = resource_size(res);
-
dev->dma_mask = dev->parent->dma_mask;
dev->dma_parms = dev->parent->dma_parms;
dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
--
1.7.10.4


2014-06-03 19:11:59

by Paul Zimmerman

[permalink] [raw]
Subject: RE: [PATCH] usb: dwc3: Keeping 'resource' related code together

> From: [email protected] [mailto:[email protected]] On Behalf Of Vivek Gautam
> Sent: Tuesday, June 03, 2014 3:40 AM
>
> Putting together the code related to getting the 'IORESOURCE_MEM'
> and assigning the same to dwc->xhci_resources, for increasing
> the readability.
>
> Signed-off-by: Vivek Gautam <[email protected]>
> ---
>
> Based on 'next' branch of Felipe's usb tree.
> Also cleanly applies to 'usb-next' branch of Greg's usb tree.
>
> drivers/usb/dwc3/core.c | 38 +++++++++++++++++++-------------------
> 1 file changed, 19 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
> index 3f59c12..4ca925d 100644
> --- a/drivers/usb/dwc3/core.c
> +++ b/drivers/usb/dwc3/core.c
> @@ -656,6 +656,25 @@ static int dwc3_probe(struct platform_device *pdev)
> return -ENODEV;
> }
>
> + dwc->xhci_resources[0].start = res->start;
> + dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
> + DWC3_XHCI_REGS_END;
> + dwc->xhci_resources[0].flags = res->flags;
> + dwc->xhci_resources[0].name = res->name;
> +
> + res->start += DWC3_GLOBALS_REGS_START;
> +
> + /*
> + * Request memory region but exclude xHCI regs,
> + * since it will be requested by the xhci-plat driver.
> + */
> + regs = devm_ioremap_resource(dev, res);
> + if (IS_ERR(regs))
> + return PTR_ERR(regs);
> +
> + dwc->regs = regs;
> + dwc->regs_size = resource_size(res);
> +
> if (node) {
> dwc->maximum_speed = of_usb_get_maximum_speed(node);

I think this will break if the following dwc3_core_get_phy() call returns
-EPROBE_DEFER. Since you have incremented res->start above, the next time
this probe function is called, res->start will have the wrong value.
Provided I'm reading the code correctly of course :)

I think you could fix that by decrementing res->start back to its
original value just after the call to resource_size() above.

--
Paul

2014-06-04 06:25:34

by Vivek Gautam

[permalink] [raw]
Subject: Re: [PATCH] usb: dwc3: Keeping 'resource' related code together

Hi,


On Wed, Jun 4, 2014 at 12:41 AM, Paul Zimmerman
<[email protected]> wrote:
>> From: [email protected] [mailto:[email protected]] On Behalf Of Vivek Gautam
>> Sent: Tuesday, June 03, 2014 3:40 AM
>>
>> Putting together the code related to getting the 'IORESOURCE_MEM'
>> and assigning the same to dwc->xhci_resources, for increasing
>> the readability.
>>
>> Signed-off-by: Vivek Gautam <[email protected]>
>> ---
>>
>> Based on 'next' branch of Felipe's usb tree.
>> Also cleanly applies to 'usb-next' branch of Greg's usb tree.
>>
>> drivers/usb/dwc3/core.c | 38 +++++++++++++++++++-------------------
>> 1 file changed, 19 insertions(+), 19 deletions(-)
>>
>> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
>> index 3f59c12..4ca925d 100644
>> --- a/drivers/usb/dwc3/core.c
>> +++ b/drivers/usb/dwc3/core.c
>> @@ -656,6 +656,25 @@ static int dwc3_probe(struct platform_device *pdev)
>> return -ENODEV;
>> }
>>
>> + dwc->xhci_resources[0].start = res->start;
>> + dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
>> + DWC3_XHCI_REGS_END;
>> + dwc->xhci_resources[0].flags = res->flags;
>> + dwc->xhci_resources[0].name = res->name;
>> +
>> + res->start += DWC3_GLOBALS_REGS_START;
>> +
>> + /*
>> + * Request memory region but exclude xHCI regs,
>> + * since it will be requested by the xhci-plat driver.
>> + */
>> + regs = devm_ioremap_resource(dev, res);
>> + if (IS_ERR(regs))
>> + return PTR_ERR(regs);
>> +
>> + dwc->regs = regs;
>> + dwc->regs_size = resource_size(res);
>> +
>> if (node) {
>> dwc->maximum_speed = of_usb_get_maximum_speed(node);
>
> I think this will break if the following dwc3_core_get_phy() call returns
> -EPROBE_DEFER. Since you have incremented res->start above, the next time
> this probe function is called, res->start will have the wrong value.
> Provided I'm reading the code correctly of course :)

True, my fault. This will surely throw an error in requesting memory
region the next time probe
is called.
Thanks for catching this.

>
> I think you could fix that by decrementing res->start back to its
> original value just after the call to resource_size() above.

Sure will put
res->start -= DWC3_GLOBALS_REGS_START;
right after the call to resource_size() call.



--
Best Regards
Vivek Gautam
Samsung R&D Institute, Bangalore
India

2014-06-04 09:04:55

by Vivek Gautam

[permalink] [raw]
Subject: [PATCH v2] usb: dwc3: Keeping 'resource' related code together

Putting together the code related to getting the 'IORESOURCE_MEM'
and assigning the same to dwc->xhci_resources, for increasing
the readability.

Signed-off-by: Vivek Gautam <[email protected]>
---

Changes from v1:
- Fixed issue with 'res->start' as pointed out by Paul Zimmerman
by decrementing it back to its original value.

drivers/usb/dwc3/core.c | 44 +++++++++++++++++++++++++-------------------
1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index 3f59c12..8b1acad 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -656,6 +656,31 @@ static int dwc3_probe(struct platform_device *pdev)
return -ENODEV;
}

+ dwc->xhci_resources[0].start = res->start;
+ dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
+ DWC3_XHCI_REGS_END;
+ dwc->xhci_resources[0].flags = res->flags;
+ dwc->xhci_resources[0].name = res->name;
+
+ res->start += DWC3_GLOBALS_REGS_START;
+
+ /*
+ * Request memory region but exclude xHCI regs,
+ * since it will be requested by the xhci-plat driver.
+ */
+ regs = devm_ioremap_resource(dev, res);
+ if (IS_ERR(regs))
+ return PTR_ERR(regs);
+
+ dwc->regs = regs;
+ dwc->regs_size = resource_size(res);
+ /*
+ * restore res->start back to its original value so that,
+ * in case the probe is deferred, we don't end up getting error in
+ * request the memory region the next time probe is called.
+ */
+ res->start -= DWC3_GLOBALS_REGS_START;
+
if (node) {
dwc->maximum_speed = of_usb_get_maximum_speed(node);

@@ -676,28 +701,9 @@ static int dwc3_probe(struct platform_device *pdev)
if (ret)
return ret;

- dwc->xhci_resources[0].start = res->start;
- dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
- DWC3_XHCI_REGS_END;
- dwc->xhci_resources[0].flags = res->flags;
- dwc->xhci_resources[0].name = res->name;
-
- res->start += DWC3_GLOBALS_REGS_START;
-
- /*
- * Request memory region but exclude xHCI regs,
- * since it will be requested by the xhci-plat driver.
- */
- regs = devm_ioremap_resource(dev, res);
- if (IS_ERR(regs))
- return PTR_ERR(regs);
-
spin_lock_init(&dwc->lock);
platform_set_drvdata(pdev, dwc);

- dwc->regs = regs;
- dwc->regs_size = resource_size(res);
-
dev->dma_mask = dev->parent->dma_mask;
dev->dma_parms = dev->parent->dma_parms;
dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
--
1.7.10.4