linux/drivers/usb/core
Alan Stern 85d07c5562 USB: core: Unite old scheme and new scheme descriptor reads
In preparation for reworking the usb_get_device_descriptor() routine,
it is desirable to unite the two different code paths responsible for
initially determining endpoint 0's maximum packet size in a newly
discovered USB device.  Making this determination presents a
chicken-and-egg sort of problem, in that the only way to learn the
maxpacket value is to get it from the device descriptor retrieved from
the device, but communicating with the device to retrieve a descriptor
requires us to know beforehand the ep0 maxpacket size.

In practice this problem is solved in two different ways, referred to
in hub.c as the "old scheme" and the "new scheme".  The old scheme
(which is the approach recommended by the USB-2 spec) involves asking
the device to send just the first eight bytes of its device
descriptor.  Such a transfer uses packets containing no more than
eight bytes each, and every USB device must have an ep0 maxpacket size
>= 8, so this should succeed.  Since the bMaxPacketSize0 field of the
device descriptor lies within the first eight bytes, this is all we
need.

The new scheme is an imitation of the technique used in an early
Windows USB implementation, giving it the happy advantage of working
with a wide variety of devices (some of them at the time would not
work with the old scheme, although that's probably less true now).  It
involves making an initial guess of the ep0 maxpacket size, asking the
device to send up to 64 bytes worth of its device descriptor (which is
only 18 bytes long), and then resetting the device to clear any error
condition that might have resulted from the guess being wrong.  The
initial guess is determined by the connection speed; it should be
correct in all cases other than full speed, for which the allowed
values are 8, 16, 32, and 64 (in this case the initial guess is 64).

The reason for this patch is that the old- and new-scheme parts of
hub_port_init() use different code paths, one involving
usb_get_device_descriptor() and one not, for their initial reads of
the device descriptor.  Since these reads have essentially the same
purpose and are made under essentially the same circumstances, this is
illogical.  It makes more sense to have both of them use a common
subroutine.

This subroutine does basically what the new scheme's code did, because
that approach is more general than the one used by the old scheme.  It
only needs to know how many bytes to transfer and whether or not it is
being called for the first iteration of a retry loop (in case of
certain time-out errors).  There are two main differences from the
former code:

	We initialize the bDescriptorType field of the transfer buffer
	to 0 before performing the transfer, to avoid possibly
	accessing an uninitialized value afterward.

	We read the device descriptor into a temporary buffer rather
	than storing it directly into udev->descriptor, which the old
	scheme implementation used to do.

Since the whole point of this first read of the device descriptor is
to determine the bMaxPacketSize0 value, that is what the new routine
returns (or an error code).  The value is stored in a local variable
rather than in udev->descriptor.  As a side effect, this necessitates
moving a section of code that checks the bcdUSB field for SuperSpeed
devices until after the full device descriptor has been retrieved.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Cc: Oliver Neukum <oneukum@suse.com>
Link: https://lore.kernel.org/r/495cb5d4-f956-4f4a-a875-1e67e9489510@rowland.harvard.edu
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2023-08-08 10:45:32 +02:00
..
buffer.c - Yosry Ahmed brought back some cgroup v1 stats in OOM logs. 2023-06-28 10:28:11 -07:00
config.c USB: core: Change configuration warnings to notices 2022-11-03 23:46:00 +09:00
devices.c usb: core: devices: drop redundant buffer overflow checks 2022-04-26 13:57:38 +02:00
devio.c usb: update the ctime as well when updating mtime after an ioctl 2023-06-13 11:58:50 +02:00
driver.c USB: mark all struct bus_type as const 2023-03-23 13:22:00 +01:00
endpoint.c usb: common: add function to get interval expressed in us unit 2021-03-10 09:37:17 +01:00
file.c USB: make usb class a const structure 2023-07-25 17:49:30 +02:00
generic.c usb: core: Fix file path that does not exist 2021-12-05 14:24:19 +01:00
hcd-pci.c USB: Extend pci resume function to handle PM events 2023-05-13 17:33:18 +09:00
hcd.c usb: core: hcd: Fix return value check in usb_hcd_setup_local_mem() 2022-11-29 08:56:09 +01:00
hub.c USB: core: Unite old scheme and new scheme descriptor reads 2023-08-08 10:45:32 +02:00
hub.h usb: core: add sysfs entry for usb device state 2023-06-13 11:58:26 +02:00
Kconfig USB: hub: Add Kconfig option to reduce number of port initialization retries 2020-10-02 11:29:02 +02:00
ledtrig-usbport.c usb: core: ledtrig-usbport: Demote obvious misuse of kerneldoc to standard comment blocks 2020-07-09 16:46:57 +02:00
Makefile usb: misc: Add onboard_usb_hub driver 2022-07-08 14:53:50 +02:00
message.c usb: Explicitly include correct DT includes 2023-07-25 18:20:02 +02:00
notify.c USB: core: Remove usbfs_mutex 2019-06-26 10:28:09 +08:00
of.c usb: Explicitly include correct DT includes 2023-07-25 18:20:02 +02:00
otg_productlist.h USB: OTG: rename product list of devices 2020-06-19 08:58:55 +02:00
phy.c usb: core: phy: add support for PHY calibration 2019-09-03 15:54:55 +02:00
phy.h usb: core: phy: add support for PHY calibration 2019-09-03 15:54:55 +02:00
port.c usb: core: add sysfs entry for usb device state 2023-06-13 11:58:26 +02:00
quirks.c USB: quirks: add quirk for Focusrite Scarlett 2023-07-25 17:41:22 +02:00
sysfs.c USB: core: Add wireless_status sysfs attribute 2023-04-03 13:30:08 +02:00
urb.c kmsan: handle memory sent to/from USB 2022-10-03 14:03:22 -07:00
usb-acpi.c usb: acpi: Switch to use acpi_evaluate_dsm_typed() 2023-03-16 12:15:10 +01:00
usb.c usb: Explicitly include correct DT includes 2023-07-25 18:20:02 +02:00
usb.h USB: make usb class a const structure 2023-07-25 17:49:30 +02:00