github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/soc/nxp/usb/descriptor_cdc.go (about)

     1  // USB descriptor support
     2  // https://github.com/usbarmory/tamago
     3  //
     4  // Copyright (c) WithSecure Corporation
     5  // https://foundry.withsecure.com
     6  //
     7  // Use of this source code is governed by the license
     8  // that can be found in the LICENSE file.
     9  
    10  package usb
    11  
    12  import (
    13  	"bytes"
    14  	"encoding/binary"
    15  )
    16  
    17  // CDC descriptor constants
    18  const (
    19  	// p39, Table 14: Communication Device Class Code
    20  	// USB Class Definitions for Communication Devices 1.1
    21  	COMMUNICATION_DEVICE_CLASS = 0x02
    22  
    23  	// p39, Table 15: Communication Interface Class Code
    24  	// USB Class Definitions for Communication Devices 1.1
    25  	COMMUNICATION_INTERFACE_CLASS = 0x02
    26  
    27  	// p40, Table 17: Data Interface Class Code
    28  	// USB Class Definitions for Communication Devices 1.1
    29  	DATA_INTERFACE_CLASS = 0x0a
    30  
    31  	// p44, Table 24: Type Values for the bDescriptorType Field,
    32  	// USB Class Definitions for Communication Devices 1.1
    33  	CS_INTERFACE = 0x24
    34  
    35  	// p64, Table 46: Class-Specific Request Codes,
    36  	// USB Class Definitions for Communication Devices 1.1
    37  	SET_ETHERNET_PACKET_FILTER = 0x43
    38  
    39  	// Maximum Segment Size
    40  	MSS = 1500 + 14
    41  )
    42  
    43  // p39, Table 16: Communication Interface Class SubClass Codes,
    44  // USB Class Definitions for Communication Devices 1.1
    45  const (
    46  	ACM_SUBCLASS = 0x02
    47  	ETH_SUBCLASS = 0x06
    48  )
    49  
    50  // p40, Table 17: Communication Interface Class Control Protocol Codes,
    51  // USB Class Definitions for Communication Devices 1.1
    52  const (
    53  	AT_COMMAND_PROTOCOL = 0x01
    54  )
    55  
    56  // p44, Table 25: bDescriptor SubType in Functional Descriptors,
    57  // USB Class Definitions for Communication Devices 1.1
    58  const (
    59  	HEADER                      = 0x00
    60  	CALL_MANAGEMENT             = 0x01
    61  	ABSTRACT_CONTROL_MANAGEMENT = 0x02
    62  	UNION                       = 0x06
    63  	ETHERNET_NETWORKING         = 0x0f
    64  )
    65  
    66  // CDCHeaderDescriptor implements
    67  // p45, Table 26: Class-Specific Descriptor Header Format, USB Class
    68  // Definitions for Communication Devices 1.1.
    69  type CDCHeaderDescriptor struct {
    70  	Length            uint8
    71  	DescriptorType    uint8
    72  	DescriptorSubType uint8
    73  	bcdCDC            uint16
    74  }
    75  
    76  // SetDefaults initializes default values for the USB CDC Header Functional
    77  // Descriptor.
    78  func (d *CDCHeaderDescriptor) SetDefaults() {
    79  	d.Length = 5
    80  	d.DescriptorType = CS_INTERFACE
    81  	d.DescriptorSubType = HEADER
    82  	// CDC 1.10
    83  	d.bcdCDC = 0x0110
    84  }
    85  
    86  // Bytes converts the descriptor structure to byte array format.
    87  func (d *CDCHeaderDescriptor) Bytes() []byte {
    88  	buf := new(bytes.Buffer)
    89  	binary.Write(buf, binary.LittleEndian, d)
    90  	return buf.Bytes()
    91  }
    92  
    93  // CDCCallManagementDescriptor implements
    94  // p45, Table 27: Call Management Functional Descriptor, USB Class Definitions
    95  // for Communication Devices 1.1.
    96  type CDCCallManagementDescriptor struct {
    97  	Length            uint8
    98  	DescriptorType    uint8
    99  	DescriptorSubType uint8
   100  	Capabilities      uint8
   101  	DataInterface     uint8
   102  }
   103  
   104  // SetDefaults initializes default values for the USB CDC Call Management
   105  // Descriptor.
   106  func (d *CDCCallManagementDescriptor) SetDefaults() {
   107  	d.Length = 5
   108  	d.DescriptorType = CS_INTERFACE
   109  	d.DescriptorSubType = CALL_MANAGEMENT
   110  }
   111  
   112  // Bytes converts the descriptor structure to byte array format.
   113  func (d *CDCCallManagementDescriptor) Bytes() []byte {
   114  	buf := new(bytes.Buffer)
   115  	binary.Write(buf, binary.LittleEndian, d)
   116  	return buf.Bytes()
   117  }
   118  
   119  // CDCAbstractCallManagementDescriptor implements
   120  // p46, Table 28: Abstract Control Management Functional Descriptor, USB Class
   121  // Definitions for Communication Devices 1.1.
   122  type CDCAbstractControlManagementDescriptor struct {
   123  	Length            uint8
   124  	DescriptorType    uint8
   125  	DescriptorSubType uint8
   126  	Capabilities      uint8
   127  }
   128  
   129  // SetDefaults initializes default values for the USB CDC Abstract Control
   130  // Management Descriptor.
   131  func (d *CDCAbstractControlManagementDescriptor) SetDefaults() {
   132  	d.Length = 4
   133  	d.DescriptorType = CS_INTERFACE
   134  	d.DescriptorSubType = ABSTRACT_CONTROL_MANAGEMENT
   135  }
   136  
   137  // Bytes converts the descriptor structure to byte array format.
   138  func (d *CDCAbstractControlManagementDescriptor) Bytes() []byte {
   139  	buf := new(bytes.Buffer)
   140  	binary.Write(buf, binary.LittleEndian, d)
   141  	return buf.Bytes()
   142  }
   143  
   144  // CDCUnionDescriptor implements
   145  // p51, Table 33: Union Interface Functional Descriptor, USB Class Definitions
   146  // for Communication Devices 1.1.
   147  type CDCUnionDescriptor struct {
   148  	Length            uint8
   149  	DescriptorType    uint8
   150  	DescriptorSubType uint8
   151  	MasterInterface   uint8
   152  	SlaveInterface0   uint8
   153  }
   154  
   155  // SetDefaults initializes default values for the USB CDC Union Functional
   156  // Descriptor.
   157  func (d *CDCUnionDescriptor) SetDefaults() {
   158  	d.Length = 5
   159  	d.DescriptorType = CS_INTERFACE
   160  	d.DescriptorSubType = UNION
   161  }
   162  
   163  // Bytes converts the descriptor structure to byte array format.
   164  func (d *CDCUnionDescriptor) Bytes() []byte {
   165  	buf := new(bytes.Buffer)
   166  	binary.Write(buf, binary.LittleEndian, d)
   167  	return buf.Bytes()
   168  }
   169  
   170  // CDCEthernetDescriptor implements
   171  // p56, Table 41: Ethernet Networking Functional Descriptor, USB Class
   172  // Definitions for Communication Devices 1.1.
   173  type CDCEthernetDescriptor struct {
   174  	Length             uint8
   175  	DescriptorType     uint8
   176  	DescriptorSubType  uint8
   177  	MacAddress         uint8
   178  	EthernetStatistics uint32
   179  	MaxSegmentSize     uint16
   180  	NumberMCFilters    uint16
   181  	NumberPowerFilters uint8
   182  }
   183  
   184  // SetDefaults initializes default values for the USB CDC Ethernet Networking
   185  // Functional Descriptor.
   186  func (d *CDCEthernetDescriptor) SetDefaults() {
   187  	d.Length = 13
   188  	d.DescriptorType = CS_INTERFACE
   189  	d.DescriptorSubType = ETHERNET_NETWORKING
   190  	d.MaxSegmentSize = MSS
   191  }
   192  
   193  // Bytes converts the descriptor structure to byte array format.
   194  func (d *CDCEthernetDescriptor) Bytes() []byte {
   195  	buf := new(bytes.Buffer)
   196  	binary.Write(buf, binary.LittleEndian, d)
   197  	return buf.Bytes()
   198  }