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

     1  // USB mass storage 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  // Mass Storage constants
    18  const (
    19  	// p11, Table 4.5 - Bulk-Only Data Interface Descriptor,
    20  	// USB Mass Storage Class 1.0
    21  	MASS_STORAGE_CLASS           = 0x08
    22  	BULK_ONLY_TRANSPORT_PROTOCOL = 0x50
    23  
    24  	// p11, Table 1 — SubClass Codes Mapped to Command Block
    25  	// Specifications, MSCO Revision 1.4
    26  	SCSI_CLASS = 0x06
    27  
    28  	CBW_LENGTH        = 31
    29  	CBW_CB_MAX_LENGTH = 16
    30  
    31  	CBW_SIGNATURE = 0x43425355
    32  	CSW_SIGNATURE = 0x53425355
    33  
    34  	// p15, Table 5.3 - Command Block Status Values,
    35  	// USB Mass Storage Class 1.0
    36  	CSW_STATUS_COMMAND_PASSED = 0x00
    37  	CSW_STATUS_COMMAND_FAILED = 0x01
    38  	CSW_STATUS_PHASE_ERROR    = 0x02
    39  
    40  	// p7, 3.1 - 3.2, USB Mass Storage Class 1.0
    41  	BULK_ONLY_MASS_STORAGE_RESET = 0xff
    42  	GET_MAX_LUN                  = 0xfe
    43  )
    44  
    45  // CBW implements p13, 5.1 Command Block Wrapper (CBW),
    46  // USB Mass Storage Class 1.0
    47  type CBW struct {
    48  	Signature          uint32
    49  	Tag                uint32
    50  	DataTransferLength uint32
    51  	Flags              uint8
    52  	LUN                uint8
    53  	Length             uint8
    54  	CommandBlock       [16]byte
    55  }
    56  
    57  // SetDefaults initializes default values for the CBW descriptor.
    58  func (d *CBW) SetDefaults() {
    59  	d.Signature = CBW_SIGNATURE
    60  }
    61  
    62  // Bytes converts the descriptor structure to byte array format.
    63  func (d *CBW) Bytes() []byte {
    64  	buf := new(bytes.Buffer)
    65  	binary.Write(buf, binary.LittleEndian, d)
    66  	return buf.Bytes()
    67  }
    68  
    69  // CSW implements p14, 5.2 Command Status Wrapper (CSW), USB Mass Storage Class 1.0
    70  type CSW struct {
    71  	Signature   uint32
    72  	Tag         uint32
    73  	DataResidue uint32
    74  	Status      uint8
    75  }
    76  
    77  // SetDefaults initializes default values for the CSW descriptor.
    78  func (d *CSW) SetDefaults() {
    79  	d.Signature = CSW_SIGNATURE
    80  	d.Status = CSW_STATUS_COMMAND_PASSED
    81  }
    82  
    83  // Bytes converts the descriptor structure to byte array format.
    84  func (d *CSW) Bytes() []byte {
    85  	buf := new(bytes.Buffer)
    86  	binary.Write(buf, binary.LittleEndian, d)
    87  	return buf.Bytes()
    88  }