[PATCH 2/3] Endianness fixes

Haavard Skinnemoen <[email protected]>
Newsgroups gmane.linux.usb.devel
Message-ID <[email protected]>
Values from descriptors and setup requests are little endian, so they
must be converted to cpu endian before doing things with them.

Signed-off-by: Haavard Skinnemoen <[email protected]>
---
 usb.c |   40 ++++++++++++++++++++++------------------
 1 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/usb.c b/usb.c
index 24a443d..835317a 100644
--- a/usb.c
+++ b/usb.c
@@ -603,12 +603,12 @@ static int iso_autoconfig ()
 	}
 	if (verbose) {
 		fprintf (stderr, "iso fs wMaxPacket %04x bInterval %02x\n",
-			fs_sink_desc.wMaxPacketSize,
+			__le16_to_cpu(fs_sink_desc.wMaxPacketSize),
 			fs_sink_desc.bInterval);
 		if (HIGHSPEED)
 			fprintf (stderr,
 				"iso hs wMaxPacket %04x bInterval %02x\n",
-				hs_sink_desc.wMaxPacketSize,
+				__le16_to_cpu(hs_sink_desc.wMaxPacketSize),
 				hs_sink_desc.bInterval);
 	}
 	return 0;
@@ -1285,13 +1285,17 @@ static void handle_control (int fd, struct usb_ctrlrequest *setup)
 {
 	int		status, tmp;
 	__u8		buf [256];
+	__u16		value, index, length;
+
+	value = __le16_to_cpu(setup->wValue);
+	index = __le16_to_cpu(setup->wIndex);
+	length = __le16_to_cpu(setup->wLength);
 
 	if (verbose)
 		fprintf (stderr, "SETUP %02x.%02x "
 				"v%04x i%04x %d\n",
 			setup->bRequestType, setup->bRequest,
-			setup->wValue, setup->wIndex,
-			setup->wLength);
+			value, index, length);
 
 	/*
 	if ((setup->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
@@ -1302,21 +1306,21 @@ static void handle_control (int fd, struct usb_ctrlrequest *setup)
 	case USB_REQ_GET_DESCRIPTOR:
 		if (setup->bRequestType != USB_DIR_IN)
 			goto stall;
-		switch (setup->wValue >> 8) {
+		switch (value >> 8) {
 		case USB_DT_STRING:
-			tmp = setup->wValue & 0x0ff;
+			tmp = value & 0x0ff;
 			if (verbose > 1)
 				fprintf (stderr,
 					"... get string %d lang %04x\n",
-					tmp, setup->wIndex);
-			if (tmp != 0 && setup->wIndex != strings.language)
+					tmp, index);
+			if (tmp != 0 && index != strings.language)
 				goto stall;
 			status = usb_gadget_get_string (&strings, tmp, buf);
 			if (status < 0)
 				goto stall;
 			tmp = status;
-			if (setup->wLength < tmp)
-				tmp = setup->wLength;
+			if (length < tmp)
+				tmp = length;
 			status = write (fd, buf, tmp);
 			if (status < 0) {
 				if (errno == EIDRM)
@@ -1336,7 +1340,7 @@ static void handle_control (int fd, struct usb_ctrlrequest *setup)
 		if (setup->bRequestType != USB_DIR_OUT)
 			goto stall;
 		if (verbose)
-			fprintf (stderr, "CONFIG #%d\n", setup->wValue);
+			fprintf (stderr, "CONFIG #%d\n", value);
 
 		/* Kernel is normally waiting for us to finish reconfiguring
 		 * the device.
@@ -1349,7 +1353,7 @@ static void handle_control (int fd, struct usb_ctrlrequest *setup)
 		 * the endpoints always active and don't rely on seeing any
 		 * config change events, either this or SET_INTERFACE.
 		 */
-		switch (setup->wValue) {
+		switch (value) {
 		case CONFIG_VALUE:
 			start_io ();
 			break;
@@ -1369,27 +1373,27 @@ static void handle_control (int fd, struct usb_ctrlrequest *setup)
 		return;
 	case USB_REQ_GET_INTERFACE:
 		if (setup->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
-				|| setup->wIndex != 0
-				|| setup->wLength > 1)
+				|| index != 0
+				|| length > 1)
 			goto stall;
 
 		/* only one altsetting in this driver */
 		buf [0] = 0;
-		status = write (fd, buf, setup->wLength);
+		status = write (fd, buf, length);
 		if (status < 0) {
 			if (errno == EIDRM)
 				fprintf (stderr, "GET_INTERFACE timeout\n");
 			else
 				perror ("write GET_INTERFACE data");
-		} else if (status != setup->wLength) {
+		} else if (status != length) {
 			fprintf (stderr, "short GET_INTERFACE write, %d\n",
 				status);
 		}
 		return;
 	case USB_REQ_SET_INTERFACE:
 		if (setup->bRequestType != USB_RECIP_INTERFACE
-				|| setup->wIndex != 0
-				|| setup->wValue != 0)
+				|| index != 0
+				|| value != 0)
 			goto stall;
 
 		/* just reset toggle/halt for the interface's endpoints */
-- 
1.5.3.1


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
[email protected]
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.