Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Expose device speed #240

Open
RReverser opened this issue Sep 7, 2023 · 6 comments
Open

[Feature request] Expose device speed #240

RReverser opened this issue Sep 7, 2023 · 6 comments

Comments

@RReverser
Copy link
Contributor

For some applications (particularly, radio) it's important to know the device speed to choose the best packet size for communication.

They normally use libusb_get_device_speed, which, in turn, consults implementation backend to get device speed reported the OS. I implemented most functions in the WebUSB backend of libusb, but currently reporting device speed as "unknown" as it's not possible to determine it from the WebUSB API, and some apps fail on this as they expect speed to be well-defined.

I think (although not sure) it would be possible to infer speed from bMaxPacketSize and wMaxPacketSize if they were available, but right now only the latter is exposed (as USBEndpoint::packetSize) while the former is not.

@rektide
Copy link

rektide commented Sep 8, 2023

I've been using @RReverser's libgphoto2 hackery. I'd alter what sized media I try to capture/download/stream adaptively if I had this capability!

@reillyeon
Copy link
Collaborator

@beaufortfrancois I think both of these properties make sense to expose. My only concern is that we need to double-check that we can get the USB speed on all platforms.

Proposed WebIDL change:

enum USBSpeed {
  "low",
  "full"
  "high",
  "super",
};

interface USBDevice {
  // ... existing properties
  readonly attribute USBSpeed speed;
  readonly attribute octet packetSize;
}

@RReverser
Copy link
Contributor Author

My only concern is that we need to double-check that we can get the USB speed on all platforms.

FWIW libusb has "unknown" constant for this scenario. It should be possible to either return that or perhaps more idiomatic undefined on unsupported platforms.

@beaufortfrancois
Copy link
Contributor

For reference, here's what says https://www.beyondlogic.org/usbnutshell/usb5.shtml

Device descriptor

Offset Field Size Value Description
7 bMaxPacketSize 1 Number Maximum Packet Size for Zero Endpoint. Valid Sizes are 8, 16, 32, 64

The bMaxPacketSize field reports the maximum packet size for endpoint zero. All devices must support endpoint zero.

Endpoint descriptor

Offset Field Size Value Description
4 wMaxPacketSize 2 Number Maximum Packet Size this endpoint is capable of sending or receiving

wMaxPacketSize indicates the maximum payload size for this endpoint.

@RReverser
Copy link
Contributor Author

RReverser commented Sep 11, 2023

The bMaxPacketSize field reports the maximum packet size for endpoint zero. All devices must support endpoint zero.

I suspect that definition is somewhat outdated. It was true for older versions of USB, but not for USB 3.0 / SuperSpeed I think. I need to find a better reference link though.

@RReverser
Copy link
Contributor Author

Can't find a good reference link, but this patch discussion that pops up among first Google results is helpful:

For bMaxPacketSize0 we usually take what is specified in ep0->maxpacket.
This is fine in most cases, however on SuperSpeed bMaxPacketSize0
specifies the exponent instead of the actual size in bytes. The only
valid value on SS is 9 which denotes 512 bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants