Skip to contentSkip to conversation

usb: gadget: aspeed_udc: fix handling of tx_len == 0

PATCH Superseded Dan Carpenter 4 participants 13 messages Jun 22, 2022 – Jun 28, 2022

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: aspeed_udc: fix handling of tx_len == 0

The bug is that we should still enter this loop if "tx_len" is zero.

After adding the "last" variable, then the "chunk >= 0" condition is no longer required but I left it for readability.

Fixes c09b1f372e74 ("usb: gadget: aspeed_udc: cleanup loop in ast_dma_descriptor_setup()")
---
 drivers/usb/gadget/udc/aspeed_udc.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/usb/gadget/udc/aspeed_udc.c +6 -3
index d75a4e070bf7..01968e2167f9 100644
@@ -476,6 +476,7 @@ static int ast_dma_descriptor_setup(struct ast_udc_ep *ep, u32 dma_buf,
 {
 	struct ast_udc_dev *udc = ep->udc;
 	struct device *dev = &udc->pdev->dev;
+	bool last = false;
 	int chunk, count;
 	u32 offset;
 
@@ -493,14 +494,16 @@ static int ast_dma_descriptor_setup(struct ast_udc_ep *ep, u32 dma_buf,
 	       "tx_len", tx_len);
 
 	/* Create Descriptor Lists */
-	while (chunk > 0 && count < AST_UDC_DESCS_COUNT) {
+	while (chunk >= 0 && !last && count < AST_UDC_DESCS_COUNT) {
 
 		ep->descs[ep->descs_wptr].des_0 = dma_buf + offset;
 
-		if (chunk > ep->chunk_max)
+		if (chunk > ep->chunk_max) {
 			ep->descs[ep->descs_wptr].des_1 = ep->chunk_max;
-		else
+		} else {
 			ep->descs[ep->descs_wptr].des_1 = chunk;
+			last = true;
+		}
 
 		chunk -= ep->chunk_max;
 
Signature · 1 line
2.35.1
maintainer
#1 lore raw
Quoted 5 lines

The bug is that we should still enter this loop if "tx_len" is zero.

After adding the "last" variable, then the "chunk >= 0" condition is no longer required but I left it for readability.

Use either "chunk >=0" or "last".
I think the former is more simpler.

Quoted 44 lines
Fixes c09b1f372e74 ("usb: gadget: aspeed_udc: cleanup loop in

ast_dma_descriptor_setup()")

---
 drivers/usb/gadget/udc/aspeed_udc.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/usb/gadget/udc/aspeed_udc.c +6 -4
index d75a4e070bf7..01968e2167f9 100644
@@ -476,6 +476,7 @@ static int ast_dma_descriptor_setup(struct ast_udc_ep
 *ep, u32 dma_buf,  {
 	struct ast_udc_dev *udc = ep->udc;
 	struct device *dev = &udc->pdev->dev;
+	bool last = false;
 	int chunk, count;
 	u32 offset;
 
@@ -493,14 +494,16 @@ static int ast_dma_descriptor_setup(struct
 ast_udc_ep *ep, u32 dma_buf,
 	       "tx_len", tx_len);
 
 	/* Create Descriptor Lists */
-	while (chunk > 0 && count < AST_UDC_DESCS_COUNT) {
+	while (chunk >= 0 && !last && count < AST_UDC_DESCS_COUNT) {
 
 		ep->descs[ep->descs_wptr].des_0 = dma_buf + offset;
 
-		if (chunk > ep->chunk_max)
+		if (chunk > ep->chunk_max) {
 			ep->descs[ep->descs_wptr].des_1 = ep->chunk_max;
-		else
+		} else {
 			ep->descs[ep->descs_wptr].des_1 = chunk;
+			last = true;
+		}
 
 		chunk -= ep->chunk_max;
 
--

2.35.1

#2 lore raw
Quoted 8 lines · Neal Liu
Quoted 5 lines

The bug is that we should still enter this loop if "tx_len" is zero.

After adding the "last" variable, then the "chunk >= 0" condition is no longer required but I left it for readability.

Use either "chunk >=0" or "last".
I think the former is more simpler.

chunk >= 0 doesn't work. last works but I think this way is more readable.

regards,
dan carpenter

#3 lore raw
Quoted 7 lines · Dan Carpenter
Quoted 6 lines · Neal Liu
Quoted 5 lines

The bug is that we should still enter this loop if "tx_len" is zero.

After adding the "last" variable, then the "chunk >= 0" condition is no longer required but I left it for readability.

Quoted 5 lines
Quoted 2 lines

Use either "chunk >=0" or "last".
I think the former is more simpler.

chunk >= 0 doesn't work. last works but I think this way is more readable.

Fine, I can remove the chunk >= 0. But you can see why your idea of removing the "last" doesn't work, right? I mean maybe it does work and there was a bug in the original code? Could you please look at that so we're for sure writing correct code?

regards,
dan carpenter

maintainer
#4 lore raw
Quoted 11 lines · Dan Carpenter
Quoted 10 lines · Neal Liu
Quoted 8 lines
Quoted 5 lines

The bug is that we should still enter this loop if "tx_len" is zero.

After adding the "last" variable, then the "chunk >= 0" condition is no longer required but I left it for readability.

Use either "chunk >=0" or "last".
I think the former is more simpler.

Quoted 8 lines
Quoted 2 lines

chunk >= 0 doesn't work. last works but I think this way is more readable.

Fine, I can remove the chunk >= 0. But you can see why your idea of removing the "last" doesn't work, right? I mean maybe it does work and there was a bug in the original code? Could you please look at that so we're for sure writing correct code?

Why removing the "last" doesn't work? If "chunk == 0", it would go through while loop once, and chunk will be negative (chunk -= ep->chunk_max).

Best Regards,
-Neal

#5 lore raw
Quoted 14 lines · Neal Liu
Quoted 14 lines · Dan Carpenter
Quoted 12 lines · Neal Liu
Quoted 8 lines
Quoted 5 lines

The bug is that we should still enter this loop if "tx_len" is zero.

After adding the "last" variable, then the "chunk >= 0" condition is no longer required but I left it for readability.

Use either "chunk >=0" or "last".
I think the former is more simpler.

chunk >= 0 doesn't work. last works but I think this way is more readable.

Quoted 8 lines
Quoted 5 lines

Fine, I can remove the chunk >= 0. But you can see why your idea of removing the "last" doesn't work, right? I mean maybe it does work and there was a bug in the original code? Could you please look at that so we're for sure writing correct code?

Why removing the "last" doesn't work? If "chunk == 0", it would go through while loop once, and chunk will be negative (chunk -= ep->chunk_max).

chunk -= ep->chunk_max could set chunk to zero.

regards,
dan carpenter

maintainer Reviewed-by
#6 lore raw
Quoted 22 lines · Neal Liu
Quoted 20 lines
Quoted 19 lines · Dan Carpenter
Quoted 7 lines · Neal Liu
Quoted 6 lines
Quoted 5 lines

The bug is that we should still enter this loop if "tx_len" is zero.

After adding the "last" variable, then the "chunk >= 0" condition is no longer required but I left it for readability.

Quoted 5 lines
Quoted 2 lines

Use either "chunk >=0" or "last".
I think the former is more simpler.

chunk >= 0 doesn't work. last works but I think this way is more readable.

Fine, I can remove the chunk >= 0. But you can see why your idea of removing the "last" doesn't work, right? I mean maybe it does work and there was a bug in the original code? Could you please look at that so we're for sure writing correct code?

Quoted 6 lines
Quoted 1 line

Why removing the "last" doesn't work? If "chunk == 0", it would go through

while loop once, and chunk will be negative (chunk -= ep->chunk_max).

Quoted 1 line

chunk -= ep->chunk_max could set chunk to zero.

Looks reasonable. Feel free to add:

Thanks

#7 lore raw
In reply to Dan Carpenter
Quoted 9 lines · Dan Carpenter
Quoted 8 lines · Neal Liu
Quoted 7 lines

The bug is that we should still enter this loop if "tx_len" is zero.

After adding the "last" variable, then the "chunk >= 0" condition is no longer
required but I left it for readability.

Quoted 5 lines
Quoted 2 lines

Use either "chunk >=0" or "last".
I think the former is more simpler.

chunk >= 0 doesn't work. last works but I think this way is more readable.

Hrm... what is that driver ? I've missed it ... is the code lifted from aspeed-vhub ? If yes, should we instead make it a common code base ? And if there are bug fixes on one they might apply to the other as well...

Neal, is that "UDC" IP block the same that resides under the vhub ? If yes then this really needs to be a common driver instead, using the code existing in aspeed-vhub, simply making it able to work without a parent vhub pointer.

Cheers,
Ben.

#8 lore raw
Quoted 13 lines · Benjamin
Quoted 12 lines · Dan Carpenter
Quoted 10 lines · Neal Liu
Quoted 7 lines

The bug is that we should still enter this loop if "tx_len" is zero.

After adding the "last" variable, then the "chunk >= 0" condition is no longer
required but I left it for readability.

Use either "chunk >=0" or "last".
I think the former is more simpler.

Quoted 7 lines
Quoted 2 lines

chunk >= 0 doesn't work. last works but I think this way is more readable.

Hrm... what is that driver ? I've missed it ... is the code lifted from aspeed-vhub ? If yes, should we instead make it a common code base ? And if there are bug fixes on one they might apply to the other as well...

No, I'm the person who introduced the bug so it's unique to this driver.

regards,
dan carpenter

#9 lore raw

(switching back to my normal "community" email)

Quoted 10 lines · Dan Carpenter
Quoted 6 lines

Hrm... what is that driver ? I've missed it ... is the code lifted from
aspeed-vhub ? If yes, should we instead make it a common code base ?
And if there are bug fixes on one they might apply to the other as well...

No, I'm the person who introduced the bug so it's unique to this driver.

Ok thanks. That said, the code looks fairly similar to the vhub code, so my comment stands, if this is the same IP block, which it appears to be, the driver should be common.

IE. The vhub is made of a virtual hub with a bunch of UDCs underneath, this appears to address the ast2600 "new" standalone (no hub) variant of said UDC if I'm not mistaken.

The way I structured the code in vhub, it shouldn't be overly difficult to make it standalone. I wrote (and maintain) aspeed-vhub and would be happy to work on this if I got sent an ast2600 board.

Cheers,
Ben.

maintainer
#10 lore raw
Quoted 17 lines

(switching back to my normal "community" email)

Quoted 9 lines · Dan Carpenter
Quoted 5 lines

Hrm... what is that driver ? I've missed it ... is the code lifted from aspeed-vhub ? If yes, should we instead make it a common code base ?
And if there are bug fixes on one they might apply to the other as well...

No, I'm the person who introduced the bug so it's unique to this driver.

Ok thanks. That said, the code looks fairly similar to the vhub code, so my comment stands, if this is the same IP block, which it appears to be, the driver should be common.

Quoted 8 lines

IE. The vhub is made of a virtual hub with a bunch of UDCs underneath, this appears to address the ast2600 "new" standalone (no hub) variant of said UDC if I'm not mistaken.

The way I structured the code in vhub, it shouldn't be overly difficult to make it standalone. I wrote (and maintain) aspeed-vhub and would be happy to work on this if I got sent an ast2600 board.

Hi Ben, This UDC is the independent IP. The ast2600 board can run aspeed-vhub & aspeed_udc simultaneously with different USB port. I think it's no need to restruct the code in vhub.

#11 lore raw
Quoted 4 lines · Neal Liu
Quoted 1 line

Hi Ben, This UDC is the independent IP. The ast2600 board can run aspeed-vhub & aspeed_udc simultaneously with different USB port. I think it's no need to restruct the code in vhub.

But is it a copy of the same base IP block ? IE, is the fundamental HW interface of the independent UDC operating the same way with the same register layout as one of the ports of the vHUB ?

I don't like having multiple drivers for the same hardware... if it's different enough, then let's keep it separate, but if not, we should definitely split the udc from the existing vhub code so that the same driver can operate standalone or beneath a vhub.

Cheers,
Ben.

maintainer
#12 lore raw
Quoted 6 lines · Neal Liu
Quoted 4 lines
Quoted 1 line

Hi Ben, This UDC is the independent IP. The ast2600 board can run aspeed-vhub & aspeed_udc simultaneously with different USB port. I think it's no need to restruct the code in vhub.

Quoted 9 lines

But is it a copy of the same base IP block ? IE, is the fundamental HW interface of the independent UDC operating the same way with the same register layout as one of the ports of the vHUB ?

I don't like having multiple drivers for the same hardware... if it's different enough, then let's keep it separate, but if not, we should definitely split the udc from the existing vhub code so that the same driver can operate standalone or beneath a vhub.

Based on ast2600 hardware design, it's similar, but not exactly the same. I need more time to extract the differences and evaluate it if it could utilize vhub.

Quoted 2 lines

Cheers,
Ben.