github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/machine/usb/usb.go (about)

     1  package usb
     2  
     3  var (
     4  	// TODO: allow setting these
     5  	STRING_LANGUAGE = [2]uint16{(3 << 8) | (2 + 2), 0x0409} // English
     6  )
     7  
     8  const (
     9  	DescriptorConfigCDC = 1 << iota
    10  	DescriptorConfigHID
    11  	DescriptorConfigMIDI
    12  	DescriptorConfigJoystick
    13  )
    14  
    15  const (
    16  	IMANUFACTURER = 1
    17  	IPRODUCT      = 2
    18  	ISERIAL       = 3
    19  
    20  	ENDPOINT_TYPE_DISABLE     = 0xFF
    21  	ENDPOINT_TYPE_CONTROL     = 0x00
    22  	ENDPOINT_TYPE_ISOCHRONOUS = 0x01
    23  	ENDPOINT_TYPE_BULK        = 0x02
    24  	ENDPOINT_TYPE_INTERRUPT   = 0x03
    25  
    26  	EndpointOut = 0x00
    27  	EndpointIn  = 0x80
    28  
    29  	EndpointPacketSize = 64 // 64 for Full Speed, EPT size max is 1024
    30  
    31  	// standard requests
    32  	GET_STATUS        = 0
    33  	CLEAR_FEATURE     = 1
    34  	SET_FEATURE       = 3
    35  	SET_ADDRESS       = 5
    36  	GET_DESCRIPTOR    = 6
    37  	SET_DESCRIPTOR    = 7
    38  	GET_CONFIGURATION = 8
    39  	SET_CONFIGURATION = 9
    40  	GET_INTERFACE     = 10
    41  	SET_INTERFACE     = 11
    42  
    43  	// non standard requests
    44  	GET_REPORT      = 1
    45  	GET_IDLE        = 2
    46  	GET_PROTOCOL    = 3
    47  	SET_REPORT      = 9
    48  	SET_IDLE        = 10
    49  	SET_PROTOCOL    = 11
    50  	SET_REPORT_TYPE = 33
    51  
    52  	DEVICE_CLASS_COMMUNICATIONS  = 0x02
    53  	DEVICE_CLASS_HUMAN_INTERFACE = 0x03
    54  	DEVICE_CLASS_STORAGE         = 0x08
    55  	DEVICE_CLASS_VENDOR_SPECIFIC = 0xFF
    56  
    57  	CONFIG_POWERED_MASK  = 0x40
    58  	CONFIG_BUS_POWERED   = 0x80
    59  	CONFIG_SELF_POWERED  = 0xC0
    60  	CONFIG_REMOTE_WAKEUP = 0x20
    61  
    62  	// Interface
    63  	NumberOfInterfaces = 3
    64  	CDC_ACM_INTERFACE  = 0 // CDC ACM
    65  	CDC_DATA_INTERFACE = 1 // CDC Data
    66  	CDC_FIRST_ENDPOINT = 1
    67  	HID_INTERFACE      = 2 // HID
    68  
    69  	// Endpoint
    70  	CONTROL_ENDPOINT  = 0
    71  	CDC_ENDPOINT_ACM  = 1
    72  	CDC_ENDPOINT_OUT  = 2
    73  	CDC_ENDPOINT_IN   = 3
    74  	HID_ENDPOINT_IN   = 4 // for Interrupt In
    75  	HID_ENDPOINT_OUT  = 5 // for Interrupt Out
    76  	MIDI_ENDPOINT_IN  = 6 // for Bulk In
    77  	MIDI_ENDPOINT_OUT = 7 // for Bulk Out
    78  	NumberOfEndpoints = 8
    79  
    80  	// bmRequestType
    81  	REQUEST_HOSTTODEVICE = 0x00
    82  	REQUEST_DEVICETOHOST = 0x80
    83  	REQUEST_DIRECTION    = 0x80
    84  
    85  	REQUEST_STANDARD = 0x00
    86  	REQUEST_CLASS    = 0x20
    87  	REQUEST_VENDOR   = 0x40
    88  	REQUEST_TYPE     = 0x60
    89  
    90  	REQUEST_DEVICE    = 0x00
    91  	REQUEST_INTERFACE = 0x01
    92  	REQUEST_ENDPOINT  = 0x02
    93  	REQUEST_OTHER     = 0x03
    94  	REQUEST_RECIPIENT = 0x1F
    95  
    96  	REQUEST_DEVICETOHOST_CLASS_INTERFACE    = (REQUEST_DEVICETOHOST | REQUEST_CLASS | REQUEST_INTERFACE)
    97  	REQUEST_HOSTTODEVICE_CLASS_INTERFACE    = (REQUEST_HOSTTODEVICE | REQUEST_CLASS | REQUEST_INTERFACE)
    98  	REQUEST_DEVICETOHOST_STANDARD_INTERFACE = (REQUEST_DEVICETOHOST | REQUEST_STANDARD | REQUEST_INTERFACE)
    99  )
   100  
   101  type Setup struct {
   102  	BmRequestType uint8
   103  	BRequest      uint8
   104  	WValueL       uint8
   105  	WValueH       uint8
   106  	WIndex        uint16
   107  	WLength       uint16
   108  }
   109  
   110  func NewSetup(data []byte) Setup {
   111  	u := Setup{}
   112  	u.BmRequestType = uint8(data[0])
   113  	u.BRequest = uint8(data[1])
   114  	u.WValueL = uint8(data[2])
   115  	u.WValueH = uint8(data[3])
   116  	u.WIndex = uint16(data[4]) | (uint16(data[5]) << 8)
   117  	u.WLength = uint16(data[6]) | (uint16(data[7]) << 8)
   118  	return u
   119  }
   120  
   121  var (
   122  	// VendorID aka VID is the officially assigned vendor number
   123  	// for this USB device. Only set this if you know what you are doing,
   124  	// since changing it can make it difficult to reflash some devices.
   125  	VendorID uint16
   126  
   127  	// ProductID aka PID is the product number associated with the officially assigned
   128  	// vendor number for this USB device. Only set this if you know what you are doing,
   129  	// since changing it can make it difficult to reflash some devices.
   130  	ProductID uint16
   131  
   132  	// Manufacturer is the manufacturer name displayed for this USB device.
   133  	Manufacturer string
   134  
   135  	// Product is the product name displayed for this USB device.
   136  	Product string
   137  
   138  	// Serial is the serial value displayed for this USB device. Assign a value to
   139  	// transmit the serial to the host when requested.
   140  	Serial string
   141  )