The reference subject is at https://github.com/ataradov/usb-sniffer.git ― this is a USB high-speed device implemented on a Cypress FX2 MCU, with the USB capture function implemented using a Lattice CPLD plus a Microchip USB PHY chip. Now we're using one low-cost domestic FPGA plus a USB PHY chip to replace the Cypress MCU and the Lattice CPLD. The FPGA implements the USB device controller, USB standard transfers and vendor transfers, and the USB capture function from the CPLD, all in pure digital logic ― no more MCU or its firmware needed.
Step one in cloning: grab all the various USB descriptors of this device. The firmware of usb-sniffer.git is open source, so its USB descriptors can be dug out of the source code. But here we use a USB protocol analyzer to capture the enumeration process and get its USB descriptors instead ― this approach is more universal, letting you clone any USB 2.0 device. Figure 1 is the captured enumeration of the USB Sniffer by the Windows OS:
This is a USB high-speed (HS) device, with 8 USB descriptors in total:
Device Descriptor
Configuration Descriptor
BOS Descriptor (Binary Device Object Store Descriptor)
Microsoft OS Descriptor Set
4 string descriptors (Language ID, Manufacture, Product, Serial Number)
Among these, the BOS descriptor and the Microsoft OS Descriptor Set are the key to driver-free operation on the Windows OS.
1.1 Copy the USB device descriptor. The copy steps are shown in Figure 2:
In the "USB Overview" and "Field Details" views, select "GetDescriptor (Device)";
In the "Raw Data" view, right-click and select "Copy as Hex Array" ― that copies the USB device descriptor into the system clipboard:
{ 0x12, 0x01, 0x10, 0x02, 0x00, 0x00, 0x00, 0x40, 0x66, 0x66, 0x20, 0x66, 0x00, 0x01, 0x01, 0x02, 0x03, 0x01 }
Besides idVendor and idProduct, the key fields here also include:
bcdUSB: as a BCD value, it can't be 0x0200 (2.0), otherwise the Microsoft OS Descriptor Set later on will fail to work ― that's the beauty of cloning blindly.
bMaxPacketSize0: the FIFO depth of EP0 in the hardware-implemented USB device controller is 64 bytes.
1.2 Keep copying the other USB descriptors. Figure 3 is the USB configuration descriptor, which internally contains the USB interface descriptor and the USB endpoint descriptor. The interface descriptor indicates that the USB Sniffer is a vendor-specific USB device class, and the endpoint descriptor defines one endpoint: EP2 is Bulk IN, with a FIFO depth of 512 bytes.
Figure 4 is the USB BOS descriptor, whose device capabilities are defined by the Platform (Microsoft, in this case) ― specifically, it's about introducing the later MS OS 2.0 Descriptor Set.
Figure 5 is the Microsoft OS Descriptor Set. Note this isn't a descriptor defined by the USB spec, but a vendor-defined descriptor from Microsoft ― you can tell from its Setup request type being Vendor. The Microsoft OS Descriptor Set also comes in versions: 1.0 and 2.0; the Setup request here with wIndex=7 indicates the Microsoft OS 2.0 Descriptor Set. We just clone this Microsoft OS Descriptor Set blindly, for driver-free operation of the USB device on the Windows OS. The contents of this descriptor set are very long; for the specific fields, you can refer to the Tipasig USB protocol analyzer's parsing.
Figure 6 is the USB string descriptor. In the Setup request, wValue=0x0302, where byte 0x03 is the string descriptor and byte 0x02 is the string index pointing to the product name (0=wLANGID, 1=iManufacture, 2=iProduct, 3=iSerialNumber). Also, strings in USB are all UTF16-encoded, i.e. ASCII 'U' is encoded in the string descriptor as 'U', 0x00; ASCII 'r' is encoded as 'r', 0x00; ...
1.3 Paste the USB descriptors into the USB device controller IP ― as shown in Figure 7. "descriptors" is all the descriptors of the USB Sniffer device after collection, and this will be passed as a parameter to the USB device controller IP.
Instantiate the USB device controller IP ― Figure 8 shows the configuration parameters of the USB device controller IP: besides descriptors, there's also bus and speed. descriptors not only defines the device's number of endpoints, types, and FIFO depth, but will also be instantiated as a ROM block for enumeration use; bus="ulpi" specifies that the bus interface uses ULPI, to communicate with the actual USB PHY chip; speed="high" specifies that the device's Monitor port is implemented as a USB high-speed device. This IP is implemented in SpinalHDL ― I won't go into detail here, just showing off how easy it is to configure.
Note that you also have to implement the logic for a vendor USB control transfer, whose implementation inherits from a generic USB control transfer Handler ― I'll skip that here too.
case class SnifferVendorRequestHandler() extends ControlRequestHandler
Copy&Paste the CPLD digital logic from usb-sniffer.git ― Figure 9 shows how to integrate the UsbCapture component (module) from the Lattice CPLD. Note there are two clock domains in the system: monitorCl