Skip to contentSkip to conversation

usb: Use get_unaligned() in usb_endpoint_maxp() for wMaxPacketSize

PATCH New Vivek Gautam 7 participants 8 messages May 13, 2013 – May 14, 2013 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 usb: Use get_unaligned() in usb_endpoint_maxp() for wMaxPacketSize

Use unaligned access to fetch wMaxPacketSize in usb_endpoint_maxp() api.
In its absence we see following data abort message:
==============================================================
data abort

MAYBE you should read doc/README.arm-unaligned-accesses

pc : [<bf794e24>]          lr : [<bf794e1c>]

sp : bf37c7b0 ip : 0000002f fp : 00000000
r10: 00000000 r9 : 00000002 r8 : bf37fecc
r7 : 00000001 r6 : bf7d8931 r5 : bf7d891c r4 : bf7d8800
r3 : bf7d65b0 r2 : 00000002 r1 : bf7d65b4 r0 : 00000027
Flags: nZCv IRQs off FIQs off Mode SVC_32
Resetting CPU ...

resetting ...
==============================================================

Cc Ilya Yanok <[email protected]>
Cc Marek Vasut <[email protected]>

---

Based on u-boot-usb/next. Tested with 'u-boot-usb/master' branch.

 include/linux/usb/ch9.h |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
include/linux/usb/ch9.h +2 -1
index d1d732c..bd48704 100644
@@ -35,6 +35,7 @@
 
 #include <linux/types.h>	/* __u8 etc */
 #include <asm/byteorder.h>	/* le16_to_cpu */
+#include <asm/unaligned.h>	/* get_unaligned() */
 
 /*-------------------------------------------------------------------------*/
 
@@ -596,7 +597,7 @@ static inline int usb_endpoint_is_isoc_out(
  */
 static inline int usb_endpoint_maxp(const struct usb_endpoint_descriptor *epd)
 {
-	return __le16_to_cpu(epd->wMaxPacketSize);
+	return __le16_to_cpu(get_unaligned(&epd->wMaxPacketSize));
 }
 
 static inline int usb_endpoint_interrupt_type(
Signature · 1 line
1.7.6.5
#1 lore raw

PATCH 2/2 usb: gadget: Use unaligned access for wMaxPacketSize

Use get_unaligned() while fetching wMaxPacketSize to avoid voilating any alignment rules.

Cc Lukasz Majewski <[email protected]>
Cc Piotr Wilczek <[email protected]>
Cc Kyungmin Park <[email protected]>
Cc Lukasz Dalek <[email protected]>
Cc Marek Vasut <[email protected]>

---

Just did a build test on u-boot-usb/master branch. Need to be tested further.

Based on u-boot-usb/next.

 drivers/usb/gadget/f_mass_storage.c |    3 ++-
 drivers/usb/gadget/pxa25x_udc.c     |   13 +++++++------
 2 files changed, 9 insertions(+), 7 deletions(-)
drivers/usb/gadget/f_mass_storage.c +2 -1
index c28866f..45bc132 100644
@@ -2261,7 +2261,8 @@ reset:
 	if (rc)
 		goto reset;
 	fsg->bulk_out_enabled = 1;
-	common->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
+	common->bulk_out_maxpacket =
+				le16_to_cpu(get_unaligned(&d->wMaxPacketSize));
 	clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
 
 	/* Allocate the requests */
drivers/usb/gadget/pxa25x_udc.c +7 -6
index 9ce98f0..085503d 100644
@@ -314,7 +314,8 @@ static int pxa25x_ep_enable(struct usb_ep *_ep,
 	if (!_ep || !desc || ep->desc || _ep->name == ep0name
 			|| desc->bDescriptorType != USB_DT_ENDPOINT
 			|| ep->bEndpointAddress != desc->bEndpointAddress
-			|| ep->fifo_size < le16_to_cpu(desc->wMaxPacketSize)) {
+			|| ep->fifo_size <
+			   le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
 		printf("%s, bad ep or descriptor\n", __func__);
 		return -EINVAL;
 	}
@@ -329,9 +330,9 @@ static int pxa25x_ep_enable(struct usb_ep *_ep,
 
 	/* hardware _could_ do smaller, but driver doesn't */
 	if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
-				&& le16_to_cpu(desc->wMaxPacketSize)
+			&& le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))
 						!= BULK_FIFO_SIZE)
-			|| !desc->wMaxPacketSize) {
+			|| !get_unaligned(&desc->wMaxPacketSize)) {
 		printf("%s, bad %s maxpacket\n", __func__, _ep->name);
 		return -ERANGE;
 	}
@@ -345,7 +346,7 @@ static int pxa25x_ep_enable(struct usb_ep *_ep,
 	ep->desc = desc;
 	ep->stopped = 0;
 	ep->pio_irqs = 0;
-	ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
+	ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
 
 	/* flush fifo (mostly for OUT buffers) */
 	pxa25x_ep_fifo_flush(_ep);
@@ -485,7 +486,7 @@ write_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
 {
 	unsigned max;
 
-	max = le16_to_cpu(ep->desc->wMaxPacketSize);
+	max = le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize));
 	do {
 		unsigned count;
 		int is_last, is_short;
@@ -766,7 +767,7 @@ pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
 	 */
 	if (unlikely(ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
 			&& req->req.length >
-			le16_to_cpu(ep->desc->wMaxPacketSize)))
+			le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize))))
 		return -EMSGSIZE;
 
 	debug_cond(NOISY, "%s queue req %p, len %d buf %p\n",
Signature · 1 line
1.7.6.5
#2 lore raw

PATCH 2/2 usb: gadget: Use unaligned access for wMaxPacketSize

Quoted 3 lines · Vivek Gautam

Use get_unaligned() while fetching wMaxPacketSize to avoid voilating any alignment rules.

It's another story, can we get performance gain with unaligned access feature? In case of kernel, we got some gains.

Anyway, good job!

Quoted 103 lines
Cc Lukasz Majewski <[email protected]>
Cc Piotr Wilczek <[email protected]>
Cc Kyungmin Park <[email protected]>
Cc Lukasz Dalek <[email protected]>
Cc Marek Vasut <[email protected]>

---

Just did a build test on u-boot-usb/master branch. Need to be tested further.

Based on u-boot-usb/next.

 drivers/usb/gadget/f_mass_storage.c |    3 ++-
 drivers/usb/gadget/pxa25x_udc.c     |   13 +++++++------
 2 files changed, 9 insertions(+), 7 deletions(-)
drivers/usb/gadget/f_mass_storage.c +2 -1
index c28866f..45bc132 100644
@@ -2261,7 +2261,8 @@ reset:
        if (rc)
                goto reset;
        fsg->bulk_out_enabled = 1;
-       common->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
+       common->bulk_out_maxpacket =
+
 le16_to_cpu(get_unaligned(&d->wMaxPacketSize));
        clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
 
        /* Allocate the requests */
drivers/usb/gadget/pxa25x_udc.c +2 -1
index 9ce98f0..085503d 100644
@@ -314,7 +314,8 @@ static int pxa25x_ep_enable(struct usb_ep *_ep,
        if (!_ep || !desc || ep->desc || _ep->name == ep0name
                        || desc->bDescriptorType != USB_DT_ENDPOINT
                        || ep->bEndpointAddress != desc->bEndpointAddress
-                       || ep->fifo_size <
 le16_to_cpu(desc->wMaxPacketSize)) {
+                       || ep->fifo_size <
+
 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
                printf("%s, bad ep or descriptor\n", __func__);
                return -EINVAL;
        }
drivers/usb/gadget/pxa25x_udc.c +2 -2
@@ -329,9 +330,9 @@ static int pxa25x_ep_enable(struct usb_ep *_ep,
 
        /* hardware _could_ do smaller, but driver doesn't */
        if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
-                               && le16_to_cpu(desc->wMaxPacketSize)
+                       &&
 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))
                                                != BULK_FIFO_SIZE)
-                       || !desc->wMaxPacketSize) {
+                       || !get_unaligned(&desc->wMaxPacketSize)) {
                printf("%s, bad %s maxpacket\n", __func__, _ep->name);
                return -ERANGE;
        }
drivers/usb/gadget/pxa25x_udc.c +1 -1
@@ -345,7 +346,7 @@ static int pxa25x_ep_enable(struct usb_ep *_ep,
        ep->desc = desc;
        ep->stopped = 0;
        ep->pio_irqs = 0;
-       ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
+       ep->ep.maxpacket =
 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
 
        /* flush fifo (mostly for OUT buffers) */
        pxa25x_ep_fifo_flush(_ep);
drivers/usb/gadget/pxa25x_udc.c +1 -1
@@ -485,7 +486,7 @@ write_fifo(struct pxa25x_ep *ep, struct pxa25x_request
 *req)
 {
        unsigned max;
 
-       max = le16_to_cpu(ep->desc->wMaxPacketSize);
+       max = le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize));
        do {
                unsigned count;
                int is_last, is_short;
drivers/usb/gadget/pxa25x_udc.c +1 -1
@@ -766,7 +767,7 @@ pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request
 *_req, gfp_t gfp_flags)
         */
        if (unlikely(ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
                        && req->req.length >
-                       le16_to_cpu(ep->desc->wMaxPacketSize)))
+
 le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize))))
                return -EMSGSIZE;

debug_cond(NOISY, "%s queue req %p, len %d buf %p\n",
--
1.7.6.5

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

#3 lore raw

PATCH 2/2 usb: gadget: Use unaligned access for wMaxPacketSize

Hi,

Quoted 4 lines · Kyungmin Park
Quoted 2 lines · Vivek Gautam

Use get_unaligned() while fetching wMaxPacketSize to avoid voilating any alignment rules.

typo here s/voilating/violating

Quoted 4 lines
Quoted 1 line

It's another story, can we get performance gain with unaligned access feature? In case of kernel, we got some gains.

This change was necessary since we get data abort if we try to fetch wMaxPacketSize
which violates the natural alignment rules being a member of "struct usb_endpoint_descriptor".

I am not sure of the performance gains with this. However, if you can guide me how to
measure that, we can get the numbers if any.

Quoted 2 lines

Anyway, good job!

Thanks !

Quoted 112 lines
Quoted 94 lines
Cc Lukasz Majewski <[email protected]>
Cc Piotr Wilczek <[email protected]>
Cc Kyungmin Park <[email protected]>
Cc Lukasz Dalek <[email protected]>
Cc Marek Vasut <[email protected]>

---

Just did a build test on u-boot-usb/master branch. Need to be tested further.

Based on u-boot-usb/next.

 drivers/usb/gadget/f_mass_storage.c |    3 ++-
 drivers/usb/gadget/pxa25x_udc.c     |   13 +++++++------
 2 files changed, 9 insertions(+), 7 deletions(-)
drivers/usb/gadget/f_mass_storage.c +2 -1
index c28866f..45bc132 100644
@@ -2261,7 +2261,8 @@ reset:
        if (rc)
                goto reset;
        fsg->bulk_out_enabled = 1;
-       common->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
+       common->bulk_out_maxpacket =
+
 le16_to_cpu(get_unaligned(&d->wMaxPacketSize));
        clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
 
        /* Allocate the requests */
drivers/usb/gadget/pxa25x_udc.c +2 -1
index 9ce98f0..085503d 100644
@@ -314,7 +314,8 @@ static int pxa25x_ep_enable(struct usb_ep *_ep,
        if (!_ep || !desc || ep->desc || _ep->name == ep0name
                        || desc->bDescriptorType != USB_DT_ENDPOINT
                        || ep->bEndpointAddress != desc->bEndpointAddress
-                       || ep->fifo_size <
 le16_to_cpu(desc->wMaxPacketSize)) {
+                       || ep->fifo_size <
+
 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
                printf("%s, bad ep or descriptor\n", __func__);
                return -EINVAL;
        }
drivers/usb/gadget/pxa25x_udc.c +2 -2
@@ -329,9 +330,9 @@ static int pxa25x_ep_enable(struct usb_ep *_ep,
 
        /* hardware _could_ do smaller, but driver doesn't */
        if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
-                               && le16_to_cpu(desc->wMaxPacketSize)
+                       &&
 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))
                                                != BULK_FIFO_SIZE)
-                       || !desc->wMaxPacketSize) {
+                       || !get_unaligned(&desc->wMaxPacketSize)) {
                printf("%s, bad %s maxpacket\n", __func__, _ep->name);
                return -ERANGE;
        }
drivers/usb/gadget/pxa25x_udc.c +1 -1
@@ -345,7 +346,7 @@ static int pxa25x_ep_enable(struct usb_ep *_ep,
        ep->desc = desc;
        ep->stopped = 0;
        ep->pio_irqs = 0;
-       ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
+       ep->ep.maxpacket =
 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
 
        /* flush fifo (mostly for OUT buffers) */
        pxa25x_ep_fifo_flush(_ep);
drivers/usb/gadget/pxa25x_udc.c +1 -1
@@ -485,7 +486,7 @@ write_fifo(struct pxa25x_ep *ep, struct pxa25x_request
 *req)
 {
        unsigned max;
 
-       max = le16_to_cpu(ep->desc->wMaxPacketSize);
+       max = le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize));
        do {
                unsigned count;
                int is_last, is_short;
drivers/usb/gadget/pxa25x_udc.c +1 -1
@@ -766,7 +767,7 @@ pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request
 *_req, gfp_t gfp_flags)
         */
        if (unlikely(ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
                        && req->req.length >
-                       le16_to_cpu(ep->desc->wMaxPacketSize)))
+
 le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize))))
                return -EMSGSIZE;
Quoted 9 lines

debug_cond(NOISY, "%s queue req %p, len %d buf %p\n",
--
1.7.6.5

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

Signature · 2 lines
Best Regards
Vivek
#4 lore raw

PATCH 2/2 usb: gadget: Use unaligned access for wMaxPacketSize

In reply to Kyungmin Park

Hi Kyungmin,

Quoted 8 lines · Kyungmin Park
Quoted 3 lines · Vivek Gautam

Use get_unaligned() while fetching wMaxPacketSize to avoid voilating any alignment rules.

It's another story, can we get performance gain with unaligned access feature? In case of kernel, we got some gains.

Please do not forget that U-Boot does not run on ARMv7+ only; it runs on many ARM generations and on many non-ARM architectures too. Some of these targets have a severe penalty on unaligned access; some cannot even perform unaligned accesses.

File drivers/usb/gadget/f_mass_storage.c is generic and must build and run for such targets too.

The best approach is to properly align fields wherever possible, and when not possible, to use unaligned access macros (and if posisble detect unaligned accesses at runtime).

Amicalement,

Signature · 1 line
Albert.
#5 lore raw

PATCH 2/2 usb: gadget: Use unaligned access for wMaxPacketSize

In reply to Vivek Gautam
Quoted 11 lines · Vivek Gautam

Use get_unaligned() while fetching wMaxPacketSize to avoid voilating any alignment rules.

Cc Lukasz Majewski <[email protected]>
Cc Piotr Wilczek <[email protected]>
Cc Kyungmin Park <[email protected]>
Cc Lukasz Dalek <[email protected]>
Cc Marek Vasut <[email protected]>

---

Quoted 8 lines

Just did a build test on u-boot-usb/master branch. Need to be tested further.

Based on u-boot-usb/next.

  drivers/usb/gadget/f_mass_storage.c |    3 ++-
  drivers/usb/gadget/pxa25x_udc.c     |   13 +++++++------
  2 files changed, 9 insertions(+), 7 deletions(-)

Tested pxa25x_udc.c on u-boot/master on h2200 (PXA255) and works fine.

Yours sincerely,
Lukasz Dalek

#6 lore raw

PATCH 2/2 usb: gadget: Use unaligned access for wMaxPacketSize

In reply to Vivek Gautam
Quoted 21 lines

-----Original Message-----
From: Vivek Gautam [mailto:gautam.vivek at samsung.com]
Sent: Monday, May 13, 2013 12:24 PM
To: u-boot at lists.denx.de
Cc: gautam.vivek at samsung.com; Lukasz Majewski; Piotr Wilczek; Kyungmin Park; Lukasz Dalek; Marek Vasut
Subject: [PATCH 2/2] usb: gadget: Use unaligned access for wMaxPacketSize

Use get_unaligned() while fetching wMaxPacketSize to avoid voilating any alignment rules.

Cc Lukasz Majewski <[email protected]>
Cc Piotr Wilczek <[email protected]>
Cc Kyungmin Park <[email protected]>
Cc Lukasz Dalek <[email protected]>
Cc Marek Vasut <[email protected]>

---

Quoted 9 lines

Just did a build test on u-boot-usb/master branch. Need to be tested further.

Based on u-boot-usb/next.

 drivers/usb/gadget/f_mass_storage.c |    3 ++-
 drivers/usb/gadget/pxa25x_udc.c     |   13 +++++++------
 2 files changed, 9 insertions(+), 7 deletions(-)

Tested 'f_mass_storage.c' on u-boot/samsung. It works fine with or without this patch on Trats2.

Best regards,
Piotr Wilczek

#7 lore raw

PATCH 2/2 usb: gadget: Use unaligned access for wMaxPacketSize

In reply to Vivek Gautam

Dear Vivek Gautam,

Quoted 9 lines

Use get_unaligned() while fetching wMaxPacketSize to avoid voilating any alignment rules.

Cc Lukasz Majewski <[email protected]>
Cc Piotr Wilczek <[email protected]>
Cc Kyungmin Park <[email protected]>
Cc Lukasz Dalek <[email protected]>
Cc Marek Vasut <[email protected]>

I'll pick both, thanks.

Best regards,
Marek Vasut