Skip to content

Commit

Permalink
Refecator the USB headers
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherJohnH committed Jun 22, 2024
1 parent 4f5d73a commit e0379b0
Show file tree
Hide file tree
Showing 9 changed files with 614 additions and 491 deletions.
475 changes: 0 additions & 475 deletions MTL/UsbTypes.h

This file was deleted.

20 changes: 12 additions & 8 deletions MTL/chip/rp2040/Usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@
#include <cstring>

#include "MTL/Periph.h"
#include "MTL/UsbTypes.h"
#include "MTL/CortexM0/NVIC.h"
#include "MTL/rp2040/Resets.h"

#include "USB/Device.h"
#include "USB/EndPoint.h"
#include "USB/Request.h"

#define LOG if (0) printf

namespace MTL {
Expand Down Expand Up @@ -238,7 +241,8 @@ class Usb : public Periph<USBReg,0x50110000>
{
ep0_in.setPID();

unsigned bytes = ep0_in.write(&device->descr, device->descr.length);
const USB::DeviceDescr& descr = device->getDescr();
unsigned bytes = ep0_in.write(&descr, descr.length);
ep0_in.startTx(std::min(bytes, unsigned(packet->length)));

LOG("GET_DESCR DEV %u\n", packet->length);
Expand Down Expand Up @@ -283,14 +287,14 @@ class Usb : public Periph<USBReg,0x50110000>
{
// Language descriptor
buffer.write(4);
buffer.write(USB::STRING);
buffer.write(USB::TYPE_STRING);
buffer.write(string[0]);
buffer.write(string[1]);
}
else
{
buffer.write(2 + len * 2);
buffer.write(USB::STRING);
buffer.write(USB::TYPE_STRING);

for(unsigned i = 0; i < len; ++i)
{
Expand Down Expand Up @@ -328,7 +332,7 @@ class Usb : public Periph<USBReg,0x50110000>
{
for(const auto& descr : interface.descr_list)
{
if (descr.getType() == USB::ENDPOINT)
if (descr.getType() == USB::TYPE_ENDPOINT)
{
const USB::EndPointDescr* ep_descr = (USB::EndPointDescr*)&descr;
EndPoint* ep = (EndPoint*) ep_descr->getImpl();
Expand Down Expand Up @@ -371,9 +375,9 @@ class Usb : public Periph<USBReg,0x50110000>
case USB::Request::GET_DESCRIPTOR:
switch(packet->value >> 8)
{
case USB::DEVICE: handleGetDeviceDescr(packet); break;
case USB::CONFIG: handleGetConfigDescr(packet); break;
case USB::STRING: handleGetStringDescr(packet); break;
case USB::TYPE_DEVICE: handleGetDeviceDescr(packet); break;
case USB::TYPE_CONFIG: handleGetConfigDescr(packet); break;
case USB::TYPE_STRING: handleGetStringDescr(packet); break;
}
break;

Expand Down
4 changes: 2 additions & 2 deletions MTL/UsbAudioControl.h → USB/Audio/Control.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#pragma once

#include "UsbTypes.h"
#include "USB/Descr.h"

namespace USB {

Expand All @@ -39,7 +39,7 @@ struct Header : public Descr
Header(List& list_) : Descr(list_, length) {}

uint8_t length{9};
uint8_t type{CS_INTERFACE};
uint8_t type{TYPE_CS_INTERFACE};
uint8_t sub_type{1};
uint16_t version_bcd{0x100};
uint16_t total_length{9};
Expand Down
12 changes: 6 additions & 6 deletions MTL/UsbMIDI.h → USB/Audio/MIDIStreaming.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#pragma once

#include "UsbTypes.h"
#include "USB/Descr.h"

namespace USB {

Expand Down Expand Up @@ -55,7 +55,7 @@ struct HeaderDescr : public Descr
{}

uint8_t length{7};
uint8_t type{CS_INTERFACE};
uint8_t type{TYPE_CS_INTERFACE};
uint8_t sub_type{HEADER};
uint16_t version_bcd{0x100}; // MIDI Streaming sub-class release 1.00
uint16_t total_length{0}; // XXX not clear what this is the total of
Expand All @@ -72,7 +72,7 @@ struct JackInDescr : public Descr
{}

uint8_t length{6};
uint8_t type{CS_INTERFACE};
uint8_t type{TYPE_CS_INTERFACE};
uint8_t sub_type{IN_JACK};
uint8_t jack_type;
uint8_t jack_id;
Expand All @@ -91,7 +91,7 @@ struct JackOutDescr : public Descr
{}

uint8_t length{7 + 2 * N};
uint8_t type{CS_INTERFACE};
uint8_t type{TYPE_CS_INTERFACE};
uint8_t sub_type{OUT_JACK};
uint8_t jack_type;
uint8_t jack_id;
Expand Down Expand Up @@ -119,7 +119,7 @@ struct CSEndPointDescr : public Descr
}

uint8_t length{4 + N};
uint8_t type{CS_ENDPOINT};
uint8_t type{TYPE_CS_ENDPOINT};
uint8_t sub_type{GENERAL};
uint8_t num_emb_midi_jack{N};
uint8_t assoc_jack_id[N] = {};
Expand All @@ -137,7 +137,7 @@ struct EndPointDescr : public Descr
}

uint8_t length{9};
uint8_t type{ENDPOINT};
uint8_t type{TYPE_ENDPOINT};
uint8_t addr{0};
uint8_t attr{0};
uint16_t max_packet_size{64};
Expand Down
190 changes: 190 additions & 0 deletions USB/Descr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
//------------------------------------------------------------------------------
// Copyright (c) 2024 John D. Haughton
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//------------------------------------------------------------------------------
// \brief USB descriptors

#pragma once

#include <cstdint>

#include "STB/List.h"

namespace USB {

// Device and interface class codes
enum Class : uint8_t
{
CLASS_INFO_IN_IFC = 0x00,
CLASS_AUDIO = 0x01,
CLASS_COMMS = 0x02,
CLASS_HID = 0x03,
CLASS_MASS_STORAGE = 0x08,
CLASS_VENDOR_SPEC = 0xFF
};

// Got to use something!
static const uint16_t DUMMY_VENDOR_ID = 0xC0DE;

// Descriptor type codes
enum DescrType : uint8_t
{
TYPE_DEVICE = 0x01,
TYPE_CONFIG = 0x02,
TYPE_STRING = 0x03,
TYPE_INTERFACE = 0x04,
TYPE_ENDPOINT = 0x05,
TYPE_CS_INTERFACE = 0x24,
TYPE_CS_ENDPOINT = 0x25
};


//! Base class for list element descriptors
class Descr : public STB::List<Descr>::Element
{
public:
using List = STB::List<Descr>;

Descr() = default;

Descr(const uint8_t& length_)
: raw(&length_)
{
}

Descr(List& list_, const uint8_t& length_)
: raw(&length_)
{
list_.push_back(this);
}

//! Set option implementation pointer
void setImpl(void* impl_) { impl = impl_; }

//! Returns pointer to first byte in descriptor
const uint8_t* getRaw() const { return raw; }

//! Returns length of descriptor as a reference
const uint8_t& getLength() const { return raw[0]; }

//! Returns type of descriptor
DescrType getType() const { return DescrType(raw[1]); }

void* getImpl() const { return impl; }

private:
const uint8_t* raw{};
void* impl{nullptr};
};


struct DeviceDescr
{
DeviceDescr() = default;

uint8_t length{0x12};
uint8_t type{TYPE_DEVICE};
uint16_t usb_bcd{0x200}; // USB 2.0
uint8_t clas{CLASS_INFO_IN_IFC};
uint8_t sub_class{0};
uint8_t protocol{0};
uint8_t max_packet_size0{64};
uint16_t vendor_id{DUMMY_VENDOR_ID};
uint16_t product_id{0x0001};
uint16_t device_bcd{0x0100}; // 1.0
uint8_t vendor_idx{0};
uint8_t product_idx{0};
uint8_t serial_num_idx{0};
uint8_t num_configs{0};

} __attribute__((__packed__));


struct ConfigDescr
{
ConfigDescr() = default;

uint8_t length{9};
uint8_t type{TYPE_CONFIG};
uint16_t total_length{0};
uint8_t num_interfaces{0};
uint8_t value{1};
uint8_t name_idx{0};
uint8_t attributes{0b10100000};
uint8_t max_power{50};

} __attribute__((__packed__));


struct InterfaceDescr : public Descr
{
InterfaceDescr() = default;

InterfaceDescr(List& list_, uint8_t class_, uint8_t sub_class_, uint8_t protocol_ = 0)
: Descr(list_, length)
, clas(class_)
, sub_class(sub_class_)
, protocol(protocol_)
{
}

uint8_t length{9};
uint8_t type{TYPE_INTERFACE};
uint8_t number{0};
uint8_t alternate_setting{0};
uint8_t num_endpoints{0};
uint8_t clas{CLASS_VENDOR_SPEC};
uint8_t sub_class{0};
uint8_t protocol{0};
uint8_t name_idx{0};

} __attribute__((__packed__));


struct EndPointDescr : public Descr
{
// Direction
static const uint8_t OUT = 0b00000000;
static const uint8_t IN = 0b10000000;

// Type
static const uint8_t CONTROL = 0b00;
static const uint8_t ISOCHrONOUS = 0b00;
static const uint8_t BULK = 0b10;
static const uint8_t INTERRUPt = 0b11;

EndPointDescr(List& list_, uint8_t dir_, uint8_t type_)
: Descr(list_, length)
, addr(dir_)
, attr(uint8_t(type_))
{
}

uint8_t length{7};
uint8_t type{TYPE_ENDPOINT};
uint8_t addr{0};
uint8_t attr{0};
uint16_t max_packet_size{64};
uint8_t interval{0};

} __attribute__((__packed__));


} // namespace USB
Loading

0 comments on commit e0379b0

Please sign in to comment.