All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 17/80] dm: usb: Move descriptor setup code into its own function
Date: Wed, 25 Mar 2015 12:22:05 -0600	[thread overview]
Message-ID: <1427307788-7496-18-git-send-email-sjg@chromium.org> (raw)
In-Reply-To: <1427307788-7496-1-git-send-email-sjg@chromium.org>

usb_new_device() is far too long and does far too much. As a first step, move
the code that does initial setup and reads a descriptor into its own function
called usb_setup_descriptor().

For XHCI the init order is different - we set up the device but don't
actually read the descriptor until after we set an address. Support this
option as a parameter to usb_setup_descriptor().

Avoid changing this torturous code more than necessary to make it easy to
review.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v2: None

 common/usb.c | 117 ++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 67 insertions(+), 50 deletions(-)

diff --git a/common/usb.c b/common/usb.c
index 4ab2213..d0debbc 100644
--- a/common/usb.c
+++ b/common/usb.c
@@ -891,35 +891,12 @@ int usb_legacy_port_reset(struct usb_device *hub, int portnr)
 	return 0;
 }
 
-/*
- * By the time we get here, the device has gotten a new device ID
- * and is in the default state. We need to identify the thing and
- * get the ball rolling..
- *
- * Returns 0 for success, != 0 for error.
- */
-int usb_new_device(struct usb_device *dev)
+static int usb_setup_descriptor(struct usb_device *dev, bool do_read)
 {
-	int addr, err;
-	int tmp;
+	__maybe_unused struct usb_device_descriptor *desc;
 	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, tmpbuf, USB_BUFSIZ);
 
 	/*
-	 * Allocate usb 3.0 device context.
-	 * USB 3.0 (xHCI) protocol tries to allocate device slot
-	 * and related data structures first. This call does that.
-	 * Refer to sec 4.3.2 in xHCI spec rev1.0
-	 */
-	if (usb_alloc_device(dev)) {
-		printf("Cannot allocate device context to get SLOT_ID\n");
-		return -1;
-	}
-
-	/* We still haven't set the Address yet */
-	addr = dev->devnum;
-	dev->devnum = 0;
-
-	/*
 	 * This is a Windows scheme of initialization sequence, with double
 	 * reset of the device (Linux uses the same sequence)
 	 * Some equipment is said to work only with such init sequence; this
@@ -934,40 +911,31 @@ int usb_new_device(struct usb_device *dev)
 	 * the maxpacket size is 8 or 16 the device may be waiting to transmit
 	 * some more, or keeps on retransmitting the 8 byte header. */
 
+	desc = (struct usb_device_descriptor *)tmpbuf;
 	dev->descriptor.bMaxPacketSize0 = 64;	    /* Start off at 64 bytes  */
 	/* Default to 64 byte max packet size */
 	dev->maxpacketsize = PACKET_SIZE_64;
 	dev->epmaxpacketin[0] = 64;
 	dev->epmaxpacketout[0] = 64;
 
-	/*
-	 * XHCI needs to issue a Address device command to setup
-	 * proper device context structures, before it can interact
-	 * with the device. So a get_descriptor will fail before any
-	 * of that is done for XHCI unlike EHCI.
-	 */
-#ifndef CONFIG_USB_XHCI
-	struct usb_device_descriptor *desc;
+	if (do_read) {
+		int err;
 
-	desc = (struct usb_device_descriptor *)tmpbuf;
-	err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, 64);
-	if (err < 0) {
-		debug("usb_new_device: usb_get_descriptor() failed\n");
-		return 1;
+		err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, 64);
+		if (err < sizeof(dev->descriptor)) {
+			if (err < 0) {
+				printf("unable to get device descriptor (error=%d)\n",
+				       err);
+				return err;
+			} else {
+				printf("USB device descriptor short read (expected %i, got %i)\n",
+				       (int)sizeof(dev->descriptor), err);
+				return -EIO;
+			}
+		}
+		memcpy(&dev->descriptor, tmpbuf, sizeof(dev->descriptor));
 	}
 
-	dev->descriptor.bMaxPacketSize0 = desc->bMaxPacketSize0;
-	/*
-	 * Fetch the device class, driver can use this info
-	 * to differentiate between HUB and DEVICE.
-	 */
-	dev->descriptor.bDeviceClass = desc->bDeviceClass;
-#endif
-
-	err = usb_legacy_port_reset(dev->parent, dev->portnr);
-	if (err)
-		return err;
-
 	dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
 	dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
 	switch (dev->descriptor.bMaxPacketSize0) {
@@ -984,6 +952,55 @@ int usb_new_device(struct usb_device *dev)
 		dev->maxpacketsize = PACKET_SIZE_64;
 		break;
 	}
+
+	return 0;
+}
+
+/*
+ * By the time we get here, the device has gotten a new device ID
+ * and is in the default state. We need to identify the thing and
+ * get the ball rolling..
+ *
+ * Returns 0 for success, != 0 for error.
+ */
+int usb_new_device(struct usb_device *dev)
+{
+	bool do_read = true;
+	int addr, err;
+	int tmp;
+	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, tmpbuf, USB_BUFSIZ);
+
+	/*
+	 * Allocate usb 3.0 device context.
+	 * USB 3.0 (xHCI) protocol tries to allocate device slot
+	 * and related data structures first. This call does that.
+	 * Refer to sec 4.3.2 in xHCI spec rev1.0
+	 */
+	if (usb_alloc_device(dev)) {
+		printf("Cannot allocate device context to get SLOT_ID\n");
+		return -1;
+	}
+
+	/* We still haven't set the Address yet */
+	addr = dev->devnum;
+	dev->devnum = 0;
+
+	/*
+	 * XHCI needs to issue a Address device command to setup
+	 * proper device context structures, before it can interact
+	 * with the device. So a get_descriptor will fail before any
+	 * of that is done for XHCI unlike EHCI.
+	 */
+#ifndef CONFIG_USB_XHCI
+	do_read = false;
+#endif
+	err = usb_setup_descriptor(dev, do_read);
+	if (err)
+		return err;
+	err = usb_legacy_port_reset(dev->parent, dev->portnr);
+	if (err)
+		return err;
+
 	dev->devnum = addr;
 
 	err = usb_set_address(dev); /* set address */
-- 
2.2.0.rc0.207.ga3a616c

  parent reply	other threads:[~2015-03-25 18:22 UTC|newest]

Thread overview: 178+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-25 18:21 [U-Boot] [PATCH v2 0/80] dm: Add USB support Simon Glass
2015-03-25 18:21 ` [U-Boot] [PATCH v2 01/80] linker_lists: Add a function to access a linker list entry Simon Glass
2015-04-07 18:39   ` Simon Glass
2015-03-25 18:21 ` [U-Boot] [PATCH v2 02/80] sandbox: Fix comment for os_open() Simon Glass
2015-04-07 18:39   ` Simon Glass
2015-03-25 18:21 ` [U-Boot] [PATCH v2 03/80] dm: test: bus: Use a local variable to simplify code Simon Glass
2015-04-07 18:39   ` Simon Glass
2015-03-25 18:21 ` [U-Boot] [PATCH v2 04/80] dm: exynos: snow: Move the keyboard to I2C Simon Glass
2015-04-07 18:39   ` Simon Glass
2015-03-25 18:21 ` [U-Boot] [PATCH v2 05/80] dm: core: Support allocating driver-private data for DMA Simon Glass
2015-04-07 18:39   ` Simon Glass
2015-03-25 18:21 ` [U-Boot] [PATCH v2 06/80] dm: core: Convert driver_bind() to use const Simon Glass
2015-04-07 18:39   ` Simon Glass
2015-03-25 18:21 ` [U-Boot] [PATCH v2 07/80] dm: core: Rename driver data function to dev_get_driver_data() Simon Glass
2015-04-07 18:40   ` Simon Glass
2015-03-25 18:21 ` [U-Boot] [PATCH v2 08/80] dm: core: Mark device as active before calling uclass probe() methods Simon Glass
2015-04-07 18:40   ` Simon Glass
2015-03-25 18:21 ` [U-Boot] [PATCH v2 09/80] dm: core: Add device children and sibling functions Simon Glass
2015-04-07 18:40   ` Simon Glass
2015-03-25 18:21 ` [U-Boot] [PATCH v2 10/80] dm: gpio: Add an implementation for gpio_get_number() Simon Glass
2015-04-07 18:40   ` Simon Glass
2015-03-25 18:21 ` [U-Boot] [PATCH v2 11/80] dm: usb: Add a uclass for USB controllers Simon Glass
2015-04-07 18:40   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 12/80] dm: usb: Adjust usb command to prepare for driver model Simon Glass
2015-04-07 18:40   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 13/80] dm: usb: Adjust usb_alloc_new_device() to return an error Simon Glass
2015-04-07 18:40   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 14/80] dm: usb: Convert 'usb' command to support driver model Simon Glass
2015-04-07 18:40   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 15/80] dm: usb: Drop the legacy USB init sequence Simon Glass
2015-03-26 18:22   ` Marek Vasut
2015-04-07 18:41     ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 16/80] dm: usb: Refactor port resets Simon Glass
2015-04-07 18:41   ` Simon Glass
2015-03-25 18:22 ` Simon Glass [this message]
2015-04-07 18:41   ` [U-Boot] [PATCH v2 17/80] dm: usb: Move descriptor setup code into its own function Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 18/80] dm: usb: Split out more code from usb_new_device() Simon Glass
2015-04-07 18:41   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 19/80] dm: usb: Complete the splitting up of usb_new_device() Simon Glass
2015-04-07 18:41   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 20/80] dm: usb: Convert core usb.c file to support driver model Simon Glass
2015-04-07 18:41   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 21/80] dm: usb: Split hub detection into its own function Simon Glass
2015-04-07 18:41   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 22/80] dm: usb: Add driver model support for hubs Simon Glass
2015-04-07 18:41   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 23/80] dm: usb: Move USB storage definitions to usb_defs.h Simon Glass
2015-04-07 18:42   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 24/80] dm: usb: Fix type problems in usb_stor_get_info() Simon Glass
2015-04-07 18:42   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 25/80] dm: usb: Simply device finding code in usb_storage Simon Glass
2015-04-07 18:42   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 26/80] dm: usb: Adjust usb_storage to work with sandbox Simon Glass
2015-04-07 18:42   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 27/80] dm: usb: Move storage device scanning into its own function Simon Glass
2015-04-07 18:43   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 28/80] dm: usb: Convert usb_storage to driver model Simon Glass
2015-04-07 18:43   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 29/80] dm: usb: Move all the EHCI weak functions together and declare them Simon Glass
2015-04-07 18:43   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 30/80] dm: usb: Pass EHCI controller pointer to ehci_get_port_speed() Simon Glass
2015-04-07 18:43   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 31/80] dm: usb: Allow ECHI to hold private data for the controller Simon Glass
2015-04-07 18:43   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 32/80] dm: usb: tegra: Store the controller type explicitly Simon Glass
2015-04-07 18:43   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 33/80] dm: usb: Pass EHCI controller pointer to ehci_powerup_fixup() Simon Glass
2015-04-07 18:43   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 34/80] dm: usb: tegra: Drop use of global controller variable Simon Glass
2015-04-07 18:44   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 35/80] dm: usb: Pass EHCI controller pointer to ehci_set_usbmode() Simon Glass
2015-04-07 18:44   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 36/80] dm: usb: Pass EHCI controller pointer to ehci_get_portsc_register() Simon Glass
2015-04-07 18:44   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 37/80] dm: usb: ehci: Use a function to find the controller from struct udevice Simon Glass
2015-04-07 18:44   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 38/80] dm: usb: Refactor EHCI init Simon Glass
2015-04-07 18:44   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 39/80] dm: usb: Drop the EHCI weak functions Simon Glass
2015-04-07 18:44   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 40/80] dm: usb: Change ehci_reset() to use a pointer Simon Glass
2015-04-07 18:44   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 41/80] dm: usb: Add driver model support to EHCI Simon Glass
2015-04-07 18:44   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 42/80] dm: usb: Allow USB drivers to be declared and auto-probed Simon Glass
2015-04-07 18:45   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 43/80] dm: usb: Bind generic USB devices when there is no driver Simon Glass
2015-04-07 18:45   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 44/80] dm: usb: Allow setting up a USB controller as a device/gadget Simon Glass
2015-04-07 18:45   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 45/80] dm: usb: Split out the keyboard probe into its own function Simon Glass
2015-04-07 18:45   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 46/80] dm: usb: Support driver model with USB keyboards Simon Glass
2015-04-07 18:45   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 47/80] dm: usb: tegra: Add vbus GPIOs for nyan Simon Glass
2015-04-07 19:09   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 48/80] dm: usb: Move struct usb_string to a common place Simon Glass
2015-04-07 19:09   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 49/80] dm: usb: sandbox: Add a uclass for USB device emulation Simon Glass
2015-04-07 19:09   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 50/80] dm: usb: sandbox: Reset emulation devices in usb stop() Simon Glass
2015-04-07 19:10   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 51/80] dm: usb: sandbox: Add an emulator for USB flash devices Simon Glass
2015-04-07 19:10   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 52/80] dm: usb: sandbox: Add an emulator for USB hub emulation Simon Glass
2015-04-07 19:10   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 53/80] dm: usb: sandbox: Add a driver for sandbox Simon Glass
2015-04-07 19:10   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 54/80] dm: usb: dts: sandbox: Add some sample USB devices to sandbox Simon Glass
2015-04-07 19:10   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 55/80] dm: usb: Add support for USB ethernet devices with driver model Simon Glass
2015-04-07 19:10   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 56/80] dm: usb: exynos: Add driver model support to exynos EHCI Simon Glass
2015-04-07 19:10   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 57/80] dm: usb: tegra: Remove the port_addr_clear_csc variable Simon Glass
2015-03-26  3:38   ` Jim Lin
2015-03-26  9:01     ` Jim Lin
2015-04-07 19:10       ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 58/80] dm: usb: tegra: Tidy up error handling and a static function Simon Glass
2015-04-07 19:10   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 59/80] dm: usb: tegra: Move most of init/uninit into a function Simon Glass
2015-04-07 19:10   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 60/80] dm: usb: tegra: Add driver model support to tegra EHCI Simon Glass
2015-04-07 19:10   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 61/80] dm: usb: xhci: Use a function to get xhci_ctrl Simon Glass
2015-04-07 19:11   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 62/80] dm: usb: xhci: Use explicit parameters for xhci_alloc_virt_device() Simon Glass
2015-04-07 19:11   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 63/80] dm: usb: xhci: Use explicit parameters for xhci_setup_addressable_virt_dev() Simon Glass
2015-04-07 19:11   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 64/80] dm: usb: xhci: Factor out common init/uninit Simon Glass
2015-04-07 19:11   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 65/80] dm: usb: Support driver model in XHCI Simon Glass
2015-04-07 19:11   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 66/80] dm: usb: Rename the XHCI HCD to U-Boot Simon Glass
2015-04-07 19:11   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 67/80] dm: usb: exynos: Adjust XHCI driver to support driver model Simon Glass
2015-04-07 19:11   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 68/80] dm: usb: exynos: Use driver model for USB Simon Glass
2015-04-07 19:11   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 69/80] dm: usb: exynos: Enable both USB ports on snow Simon Glass
2015-04-07 19:11   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 70/80] dm: usb: exynos: Enable both EHCI and XHCI " Simon Glass
2015-04-07 19:11   ` Simon Glass
2015-03-25 18:22 ` [U-Boot] [PATCH v2 71/80] dm: usb: tegra: Move to driver model for USB Simon Glass
2015-04-07 19:11   ` Simon Glass
2015-03-25 18:23 ` [U-Boot] [PATCH v2 72/80] dm: usb: Add a generic descriptor struct Simon Glass
2015-04-07 19:11   ` Simon Glass
2015-03-25 18:23 ` [U-Boot] [PATCH v2 73/80] dm: usb: Tidy up pipe value decoding Simon Glass
2015-04-07 19:12   ` Simon Glass
2015-03-25 18:23 ` [U-Boot] [PATCH v2 74/80] dm: usb: sandbox: Enable USB Simon Glass
2015-04-07 19:12   ` Simon Glass
2015-03-25 18:23 ` [U-Boot] [PATCH v2 75/80] dm: test: Correct printf() output nit in 'dm uclass' Simon Glass
2015-04-07 19:12   ` Simon Glass
2015-03-25 18:23 ` [U-Boot] [PATCH v2 76/80] dm: test: Allow 'dm test' to select a particular test to run Simon Glass
2015-04-07 19:12   ` Simon Glass
2015-03-25 18:23 ` [U-Boot] [PATCH v2 77/80] dm: usb: Add tests for the USB uclass Simon Glass
2015-04-07 19:12   ` Simon Glass
2015-04-21  5:24   ` Joe Hershberger
2015-04-21 13:14     ` Simon Glass
2015-04-21 16:05       ` Joe Hershberger
2015-04-21 16:19         ` Simon Glass
2015-04-21 16:57           ` Joe Hershberger
2015-04-21 17:00             ` Simon Glass
2015-04-21 20:10               ` Joe Hershberger
2015-04-21 20:14                 ` Simon Glass
2015-03-25 18:23 ` [U-Boot] [PATCH v2 78/80] dm: usb: tegra: Drop legacy USB code Simon Glass
2015-06-11 20:19   ` Simon Glass
2015-03-25 18:23 ` [U-Boot] [PATCH v2 79/80] dm: usb: exynos: " Simon Glass
2015-05-06 21:44   ` Simon Glass
2015-03-25 18:23 ` [U-Boot] [PATCH v2 80/80] dm: usb: Add a README for driver model Simon Glass
2015-04-07  3:24   ` Jim Lin
2015-04-08  1:40     ` Simon Glass
2015-03-26 19:40 ` [U-Boot] [PATCH v2 0/80] dm: Add USB support Marek Vasut
2015-04-05 23:38   ` Simon Glass
2015-04-06 13:13     ` Marek Vasut
2015-04-06 22:38       ` Simon Glass
2015-04-07 13:52         ` Marek Vasut

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1427307788-7496-18-git-send-email-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.