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

gateware.usb.usb2.request: add claim to RequestHandlerInterface #225

Merged
merged 1 commit into from
Mar 7, 2024

Conversation

mndza
Copy link
Contributor

@mndza mndza commented Dec 4, 2023

This commit adds a handled() method to USBRequestHandler. This method takes a SetupPacket as an argument and returns an Amaranth boolean indicating whether a request has been taken care of.

These signals from the various request handlers are subsequently combined to trigger a stall for unhandled cases.

@martinling
Copy link
Member

Hi @mndza,

@miek and I don't think this one depends on the other PRs mentioned. Do you see a reason for this PR to wait for the rest?

@zyp how do you feel about this change from the point of view of OrbTrace, or other downstream users?

@zyp
Copy link
Contributor

zyp commented Dec 7, 2023

From an Orbtrace perspective, this would be a welcome improvement. We've got interface handlers that'll stall nonexistent requests on the interfaces they handle, but nothing that'll stall requests that's not directed to any of the handled interfaces.

@mndza
Copy link
Contributor Author

mndza commented Dec 10, 2023

Hi @mndza,

@miek and I don't think this one depends on the other PRs mentioned. Do you see a reason for this PR to wait for the rest?

I agree that this PR should be independent. I'm removing any references to the other PRs. Let me know if there are any other concerns or adjustments needed.

@martinling martinling self-requested a review December 19, 2023 17:59
Copy link
Member

@martinling martinling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty happy with this as it stands, and it's a good improvement over the status quo, but I do see one issue:

With this change, the logic for which requests should be handled ends up appearing twice in each request handler: once in handled, and again in elaborate.

This seems like a bit of a recipe for trouble, because it would be easy for the two to get out of sync, such that a handler implementation is lying about which requests it does and doesn't handle. That could lead to some quite hard-to-find bugs.

Given that this is already going to be a breaking change to downstream users of USBRequestHandler, I wonder if we should take the opportunity to change the API a little bit further, such that a request handler won't see a request at all unless its handled precondition is met. That would require some further changes, but could make handlers simpler and less error-prone to write.

@zyp
Copy link
Contributor

zyp commented Dec 20, 2023

It'd be nice to have a request dispatcher that you could just register request handlers with, automatically generating both the handled logic and the dispatch logic in elaborate.

@mndza mndza marked this pull request as draft January 10, 2024 12:46
Introduce a new `claim` signal to the `RequestHandlerInterface`.
If a `USBRequestHandler` wants to manage an incoming request, it must
assert this signal to gain control of the remaining interface outputs.

Additionally, this commit simplifies the logic within
`USBRequestHandlerMultiplexer` and provides a fallback mechanism for
unhandled requests.
@mndza
Copy link
Contributor Author

mndza commented Jan 15, 2024

I've just pushed a different approach for this PR.

Instead of a handled method, there's a new claim signal in RequestHandlerInterface.

When a USBRequestHandler wants to manage an incoming request, it must assert this signal. This lets the handler drive the rest of interface outputs.

It makes the logic in USBRequestHandlerMultiplexer simpler, and introduces a fallback mechanism for unhandled requests: when no handler claims a request, or more than 1 does, the multiplexer selects this fallback.

@mndza mndza changed the title gateware.usb.usb2.request: USBRequestHandler defines managed requests gateware.usb.usb2.request: add claim to RequestHandlerInterface Jan 15, 2024
@mndza mndza marked this pull request as ready for review January 15, 2024 15:39
@antoinevg antoinevg mentioned this pull request Jan 16, 2024
Copy link
Member

@martinling martinling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with this, and it's being used successfully on the update-dependencies branch of the Cynthion factory test.

@mossmann mossmann merged commit cb9e929 into greatscottgadgets:main Mar 7, 2024
2 of 4 checks passed
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

Successfully merging this pull request may close these issues.

5 participants