Skip to content

Commit

Permalink
gateware.usb.usb2.request: stall when handlers step on each other
Browse files Browse the repository at this point in the history
This commit changes the fallback mechanism to also stall when multiple
request handlers want to manage a setup packet at the same time.
  • Loading branch information
mndza committed Dec 6, 2023
1 parent a89405b commit 161c67c
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions luna/gateware/usb/usb2/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
""" Low-level USB transciever gateware -- control transfer components. """

import unittest
import operator
from functools import reduce

from amaranth import Signal, Module, Elaboratable
from usb_protocol.emitters import DeviceDescriptorCollection
Expand Down Expand Up @@ -130,14 +128,12 @@ def elaborate(self, platform):


#
# We know if a setup packet is processed with the handled() method from the request handlers.
# We use the `handled()` method in the request handlers to check if a setup packet is processed.
# Emit a stall when a request is not handled by any registered request handlers or when
# handled by more than one.
#
if len(self._request_handlers) > 0:
# Emit a stall when the request is not taken care of by the registered request handlers
handle_conditions = [handler.handled for handler in self._request_handlers]
stall_condition = lambda setup: reduce(operator.and_, (~cond(setup) for cond in handle_conditions))
else:
stall_condition = lambda setup: True
handle_conditions = [handler.handled for handler in self._request_handlers]
stall_condition = lambda setup: sum((cond(setup) for cond in handle_conditions)) != 1
self.add_request_handler(StallOnlyRequestHandler(stall_condition))

#
Expand Down

0 comments on commit 161c67c

Please sign in to comment.