Skip to content

Commit

Permalink
cores/usb/generic_device: option to add microsoft descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
povauboin committed Sep 26, 2024
1 parent 4f22e38 commit c57ca24
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion lambdalib/cores/usb/generic_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from amaranth import *

from usb_protocol.emitters import DeviceDescriptorCollection
from usb_protocol.emitters.descriptors.standard import get_string_descriptor
from usb_protocol.emitters.descriptors.microsoft10 import MicrosoftOS10DescriptorCollection
from usb_protocol.types.descriptors.microsoft10 import RegistryTypes
from luna.usb2 import USBDevice, USBStreamOutEndpoint, USBStreamInEndpoint
from luna.gateware.usb.request.windows import MicrosoftOS10RequestHandler

from .endpoint_cdc import *
from lambdalib.interface import stream
Expand All @@ -29,6 +33,7 @@ def __init__(self, pins,
ep_sizes=None,
max_packet_size=None,
with_cdc=True,
with_microsoft_os_1_0=False,
**kwargs):

self.pins = pins
Expand All @@ -55,6 +60,7 @@ def __init__(self, pins,
self.ep_sizes = ep_sizes
self.control_ep_handlers = []
self.with_cdc = with_cdc
self.with_microsoft_os_1_0 = with_microsoft_os_1_0

self.kwargs = kwargs

Expand Down Expand Up @@ -107,6 +113,28 @@ def create_descriptors(self):

return descriptors

def add_microsoft_os_1_0(self, descriptors):
""" Add Microsoft OS 1.0 descriptors for Windows compatibility. """

descriptors.add_descriptor(get_string_descriptor("MSFT100\xee"), index=0xee)

msft_descriptors = MicrosoftOS10DescriptorCollection()

with msft_descriptors.ExtendedCompatIDDescriptor() as c:
with c.Function() as f:
f.bFirstInterfaceNumber = 0
f.compatibleID = "WINUSB"

with msft_descriptors.ExtendedPropertiesDescriptor() as d:
with d.Property() as p:
# Windows defined ClassGUID for "USBDevice"
p.dwPropertyDataType = RegistryTypes.REG_SZ
p.PropertyName = "DeviceInterfaceGUID"
p.PropertyData = "{88bae032-5a81-49f0-bc3d-a4ff138216d6}"

msft_handler = MicrosoftOS10RequestHandler(msft_descriptors, request_code=0xee)
self.add_control_ep_handler(msft_handler)

def add_control_ep_handler(self, handler):
self.control_ep_handlers.append(handler)

Expand All @@ -117,7 +145,17 @@ def elaborate(self, platform):

# Add our standard control endpoint to the device.
descriptors = self.create_descriptors()
control_ep = usb.add_standard_control_endpoint(descriptors)

# Optionally add handlers for Microsoft descriptors.
if self.with_microsoft_os_1_0:
self.add_microsoft_os_1_0(descriptors)

control_ep = usb.add_standard_control_endpoint(
descriptors,
# Windows compatible descriptors cannot be build in block ram
# because MSFT string at index 0xee is not contiguous.
avoid_blockram=self.with_microsoft_os_1_0,
)

# Add optional custom requests handlers (vendor)
for handler in self.control_ep_handlers:
Expand Down

0 comments on commit c57ca24

Please sign in to comment.