github.com/f-secure-foundry/tamago@v0.0.0-20220307101044-d73fcdd7f11b/soc/imx6/usb/descriptor_cdc.go (about)

     1  // USB descriptor support
     2  // https://github.com/f-secure-foundry/tamago
     3  //
     4  // Copyright (c) F-Secure Corporation
     5  // https://foundry.f-secure.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  	// p44, Table 24: Type Values for the bDescriptorType Field,
    20  	// USB Class Definitions for Communication Devices 1.1
    21  	CS_INTERFACE = 0x24
    22  
    23  	HEADER_LENGTH              = 5
    24  	UNION_LENGTH               = 5
    25  	ETHERNET_NETWORKING_LENGTH = 13
    26  
    27  	// p64, Table 46: Class-Specific Request Codes,
    28  	// USB Class Definitions for Communication Devices 1.1
    29  	SET_ETHERNET_PACKET_FILTER = 0x43
    30  
    31  	HEADER              = 0
    32  	UNION               = 6
    33  	ETHERNET_NETWORKING = 15
    34  
    35  	// Maximum Segment Size
    36  	MSS = 1500 + 14
    37  )
    38  
    39  // CDCHeaderDescriptor implements
    40  // p45, Table 26: Class-Specific Descriptor Header Format, USB Class
    41  // Definitions for Communication Devices 1.1.
    42  type CDCHeaderDescriptor struct {
    43  	Length            uint8
    44  	DescriptorType    uint8
    45  	DescriptorSubType uint8
    46  	bcdCDC            uint16
    47  }
    48  
    49  // SetDefaults initializes default values for the USB CDC Header Functional
    50  // Descriptor.
    51  func (d *CDCHeaderDescriptor) SetDefaults() {
    52  	d.Length = HEADER_LENGTH
    53  	d.DescriptorType = CS_INTERFACE
    54  	d.DescriptorSubType = HEADER
    55  	// CDC 1.10
    56  	d.bcdCDC = 0x0110
    57  }
    58  
    59  // Bytes converts the descriptor structure to byte array format.
    60  func (d *CDCHeaderDescriptor) Bytes() []byte {
    61  	buf := new(bytes.Buffer)
    62  	binary.Write(buf, binary.LittleEndian, d)
    63  	return buf.Bytes()
    64  }
    65  
    66  // CDCUnionDescriptor implements
    67  // p51, Table 33: Union Interface Functional Descriptor, USB Class Definitions
    68  // for Communication Devices 1.1.
    69  type CDCUnionDescriptor struct {
    70  	Length            uint8
    71  	DescriptorType    uint8
    72  	DescriptorSubType uint8
    73  	MasterInterface   uint8
    74  	SlaveInterface0   uint8
    75  }
    76  
    77  // SetDefaults initializes default values for the USB CDC Union Functional
    78  // Descriptor.
    79  func (d *CDCUnionDescriptor) SetDefaults() {
    80  	d.Length = UNION_LENGTH
    81  	d.DescriptorType = CS_INTERFACE
    82  	d.DescriptorSubType = UNION
    83  }
    84  
    85  // Bytes converts the descriptor structure to byte array format.
    86  func (d *CDCUnionDescriptor) Bytes() []byte {
    87  	buf := new(bytes.Buffer)
    88  	binary.Write(buf, binary.LittleEndian, d)
    89  	return buf.Bytes()
    90  }
    91  
    92  // CDCEthernetDescriptor implements
    93  // p56, Table 41: Ethernet Networking Functional Descriptor, USB Class
    94  // Definitions for Communication Devices 1.1.
    95  type CDCEthernetDescriptor struct {
    96  	Length             uint8
    97  	DescriptorType     uint8
    98  	DescriptorSubType  uint8
    99  	MacAddress         uint8
   100  	EthernetStatistics uint32
   101  	MaxSegmentSize     uint16
   102  	NumberMCFilters    uint16
   103  	NumberPowerFilters uint8
   104  }
   105  
   106  // SetDefaults initializes default values for the USB CDC Ethernet Networking
   107  // Functional Descriptor.
   108  func (d *CDCEthernetDescriptor) SetDefaults() {
   109  	d.Length = ETHERNET_NETWORKING_LENGTH
   110  	d.DescriptorType = CS_INTERFACE
   111  	d.DescriptorSubType = ETHERNET_NETWORKING
   112  	d.MaxSegmentSize = MSS
   113  }
   114  
   115  // Bytes converts the descriptor structure to byte array format.
   116  func (d *CDCEthernetDescriptor) Bytes() []byte {
   117  	buf := new(bytes.Buffer)
   118  	binary.Write(buf, binary.LittleEndian, d)
   119  	return buf.Bytes()
   120  }