Skip to content

Commit 1321723

Browse files
committed
Reworked USB to have individual max packet sizes per endpoint or per type
1 parent 6164c77 commit 1321723

7 files changed

Lines changed: 117 additions & 22 deletions

File tree

klib/usb/device/keyboard.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ namespace klib::usb::device {
966966
break;
967967
case hid::class_request::set_report:
968968
// check if the packet length is not above the max endpoint size
969-
if (packet.wLength > Usb::max_endpoint_size) {
969+
if (packet.wLength > Usb::max_endpoint_size.size(usb::control_endpoint, klib::usb::descriptor::transfer_type::control)) {
970970
// invalid length
971971
return usb::handshake::stall;
972972
}

klib/usb/device/mouse.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ namespace klib::usb::device {
565565
break;
566566
case hid::class_request::set_report:
567567
// check if the packet length is not above the max endpoint size
568-
if (packet.wLength > Usb::max_endpoint_size) {
568+
if (packet.wLength > Usb::max_endpoint_size.size(usb::control_endpoint, klib::usb::descriptor::transfer_type::control)) {
569569
// invalid length
570570
return usb::handshake::stall;
571571
}

klib/usb/usb/size.hpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#ifndef KLIB_USB_SIZE_HPP
2+
#define KLIB_USB_SIZE_HPP
3+
4+
#include <array>
5+
6+
#include "descriptor.hpp"
7+
8+
namespace klib::usb {
9+
/**
10+
* @brief Helper class to get the size of a specific endpoint by transfer type
11+
*
12+
* @note Gives the same interface as the other classes in this file
13+
*
14+
* @tparam Control
15+
* @tparam Isochronous
16+
* @tparam BulkSize
17+
* @tparam Interrupt
18+
*/
19+
template <
20+
uint16_t Control,
21+
uint16_t Isochronous,
22+
uint16_t BulkSize,
23+
uint16_t Interrupt
24+
>
25+
class endpoint_size_type {
26+
protected:
27+
// storage for all the sizes of every endpoint type
28+
const std::array<uint16_t, 4> s = {
29+
Control, Isochronous, BulkSize, Interrupt
30+
};
31+
32+
public:
33+
/**
34+
* @brief Get the size of a endpoint
35+
*
36+
* @param endpoint
37+
* @param type
38+
* @return constexpr auto
39+
*/
40+
constexpr auto size(const uint8_t endpoint, const descriptor::transfer_type& type) const {
41+
return s[static_cast<uint8_t>(type)];
42+
}
43+
};
44+
45+
/**
46+
* @brief Helper class to get the size of a specific endpoint by the endpoint number
47+
*
48+
* @note Gives the same interface as the other classes in this file
49+
*
50+
* @tparam EndpointCount
51+
* @tparam Values
52+
*/
53+
template <
54+
uint8_t EndpointCount,
55+
uint16_t... Values
56+
>
57+
class endpoint_size_endpoint {
58+
protected:
59+
static_assert(sizeof...(Values) == EndpointCount, "Invalid endpoint sizes received");
60+
61+
// storage for all the endpoint sizes
62+
const std::array<uint16_t, EndpointCount> s = {
63+
Values...
64+
};
65+
66+
public:
67+
/**
68+
* @brief Get the size of a endpoint
69+
*
70+
* @param endpoint
71+
* @param type
72+
* @return constexpr auto
73+
*/
74+
constexpr auto size(const uint8_t endpoint, const descriptor::transfer_type& type) const {
75+
return s[endpoint];
76+
}
77+
};
78+
}
79+
80+
#endif
81+

targets/chip/max32625/io/usb.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ namespace klib::max32625::io {
9696
// amount of endpoints supported by the max32625
9797
constexpr static uint8_t endpoint_count = 8;
9898

99-
// max size in a single endpoint
100-
constexpr static uint8_t max_endpoint_size = 64;
99+
// maximum endpoint sizes
100+
constexpr static klib::usb::endpoint_size_type<64, 64, 64, 64> max_endpoint_size = {};
101101

102102
// type to use in device functions
103103
using usb_type = usb<Usb, Device>;
@@ -697,7 +697,9 @@ namespace klib::max32625::io {
697697
// set the endpoint to a known state
698698
state[i] = {
699699
.is_busy = false,
700-
.max_size = static_cast<uint8_t>((i == 0) ? max_endpoint_size : 0),
700+
.max_size = static_cast<uint8_t>(
701+
(i == 0) ? max_endpoint_size.size(0, klib::usb::descriptor::transfer_type::control) : 0
702+
),
701703
.requested_size = 0,
702704
.transferred_size = 0,
703705
.callback = nullptr
@@ -781,7 +783,7 @@ namespace klib::max32625::io {
781783
);
782784

783785
// set the new endpoint size
784-
state[endpoint].max_size = klib::min(max_endpoint_size, size);
786+
state[endpoint].max_size = klib::min(size, max_endpoint_size.size(endpoint, type));
785787

786788
// store the ctrl data in the endpoint
787789
(*ep) = ctrl;

targets/core/atmel/atsam4s/usb.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ namespace klib::core::atsam4s::io {
5151
// amount of endpoints supported by the atsam4s
5252
constexpr static uint8_t endpoint_count = 8;
5353

54-
// max size in a single endpoint
55-
constexpr static uint8_t max_endpoint_size = 64;
54+
// maximum endpoint sizes
55+
constexpr static klib::usb::endpoint_size_endpoint<endpoint_count,
56+
64, 64, 64, 64, 512, 512, 64, 64
57+
> max_endpoint_size = {};
5658

5759
// type to use in device functions
5860
using usb_type = usb<Usb, Device>;
@@ -564,7 +566,9 @@ namespace klib::core::atsam4s::io {
564566
for (uint32_t i = 0; i < endpoint_count; i++) {
565567
// set the endpoint to a known state
566568
state[i].is_busy = false;
567-
state[i].max_size = static_cast<uint8_t>((i == 0) ? max_endpoint_size : 0);
569+
state[i].max_size = static_cast<uint8_t>(
570+
(i == 0) ? max_endpoint_size.size(0, klib::usb::descriptor::transfer_type::control) : 0
571+
);
568572
state[i].data = nullptr;
569573
state[i].requested_size = 0;
570574
state[i].transferred_size = 0;
@@ -649,7 +653,7 @@ namespace klib::core::atsam4s::io {
649653
const klib::usb::descriptor::transfer_type type, const uint32_t size)
650654
{
651655
// set the new endpoint size
652-
state[endpoint].max_size = size;
656+
state[endpoint].max_size = klib::min(size, max_endpoint_size.size(endpoint, type));
653657

654658
// reset the endpoint
655659
reset(endpoint, mode);

targets/core/cypress/mb9bf560l/usb.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
#include <span>
55

66
#include <klib/klib.hpp>
7-
#include <klib/usb/usb/usb.hpp>
87
#include <klib/math.hpp>
98
#include <klib/irq_helper.hpp>
109
#include <klib/usb/usb/setup.hpp>
10+
#include <klib/usb/usb/size.hpp>
11+
#include <klib/usb/usb/usb.hpp>
1112

1213
#include <io/port.hpp>
1314

@@ -51,8 +52,10 @@ namespace klib::core::mb9bf560l::io {
5152
// amount of endpoints supported by the lpc1756
5253
constexpr static uint8_t endpoint_count = 6;
5354

54-
// max size in a single endpoint
55-
constexpr static uint8_t max_endpoint_size = 64;
55+
// maximum endpoint sizes
56+
constexpr static klib::usb::endpoint_size_endpoint<endpoint_count,
57+
64, 256, 64, 64, 64, 64
58+
> max_endpoint_size = {};
5659

5760
// type to use in device functions
5861
using usb_type = usb<Usb, Device>;
@@ -171,7 +174,7 @@ namespace klib::core::mb9bf560l::io {
171174
Usb::port->UDCC |= 0x1;
172175

173176
// configure enpoint 0 endpoint size.
174-
Usb::port->EP0C = max_endpoint_size;
177+
Usb::port->EP0C = max_endpoint_size.size(0, klib::usb::descriptor::transfer_type::control);
175178

176179
// clear the reset flag
177180
Usb::port->UDCC &= ~(0x1 << 7);
@@ -661,7 +664,9 @@ namespace klib::core::mb9bf560l::io {
661664
for (uint32_t i = 0; i < endpoint_count; i++) {
662665
// set the endpoint to a known state
663666
state[i].is_busy = false;
664-
state[i].max_size = static_cast<uint8_t>((i == 0) ? max_endpoint_size : 0);
667+
state[i].max_size = static_cast<uint8_t>(
668+
(i == 0) ? max_endpoint_size.size(0, klib::usb::descriptor::transfer_type::control) : 0
669+
);
665670
state[i].data = nullptr;
666671
state[i].requested_size = 0;
667672
state[i].transferred_size = 0;
@@ -765,7 +770,7 @@ namespace klib::core::mb9bf560l::io {
765770
const uint16_t s = size & (endpoint == 1 ? 0x1ff : 0x7f);
766771

767772
// set the new endpoint size
768-
state[endpoint].max_size = s;
773+
state[endpoint].max_size = klib::min(s, max_endpoint_size.size(endpoint, type));
769774

770775
// set the endpoint configuration
771776
(*ep_control) = (

targets/core/nxp/lpc17xx/usb.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <klib/klib.hpp>
77
#include <klib/usb/usb/usb.hpp>
8+
#include <klib/usb/usb/size.hpp>
89
#include <klib/math.hpp>
910

1011
#include <io/power.hpp>
@@ -51,8 +52,8 @@ namespace klib::core::lpc17xx::io {
5152
// amount of endpoints supported by the lpc1756
5253
constexpr static uint8_t endpoint_count = 16;
5354

54-
// max size in a control endpoint
55-
constexpr static uint8_t max_control_endpoint_size = 64;
55+
// maximum endpoint sizes
56+
constexpr static klib::usb::endpoint_size_type<64, 1023, 64, 64> max_endpoint_size = {};
5657

5758
// type to use in device functions
5859
using usb_type = usb<Usb, Device>;
@@ -178,9 +179,9 @@ namespace klib::core::lpc17xx::io {
178179

179180
static void reset() {
180181
Usb::port->EPIND = 0;
181-
Usb::port->MAXPSIZE = max_control_endpoint_size;
182+
Usb::port->MAXPSIZE = max_endpoint_size.size(0, klib::usb::descriptor::transfer_type::control);
182183
Usb::port->EPIND = 1;
183-
Usb::port->MAXPSIZE = max_control_endpoint_size;
184+
Usb::port->MAXPSIZE = max_endpoint_size.size(0, klib::usb::descriptor::transfer_type::control);
184185

185186
while ((Usb::port->DEVINTST & 0x100) == 0) {
186187
// do nothing
@@ -733,7 +734,9 @@ namespace klib::core::lpc17xx::io {
733734
for (uint32_t i = 0; i < endpoint_count; i++) {
734735
// set the endpoint to a known state
735736
state[i].is_busy = false;
736-
state[i].max_size = static_cast<uint8_t>((i == 0) ? max_control_endpoint_size : 0);
737+
state[i].max_size = static_cast<uint8_t>(
738+
(i == 0) ? max_endpoint_size.size(0, klib::usb::descriptor::transfer_type::control) : 0
739+
);
737740
state[i].data = nullptr;
738741
state[i].requested_size = 0;
739742
state[i].transferred_size = 0;
@@ -853,7 +856,7 @@ namespace klib::core::lpc17xx::io {
853856
Usb::port->MAXPSIZE = size;
854857

855858
// set the new endpoint size
856-
state[endpoint].max_size = size;
859+
state[endpoint].max_size = klib::min(size, max_endpoint_size.size(ep, type));
857860

858861
// wait until the flag is updated
859862
while ((Usb::port->DEVINTST & 0x100) == 0) {

0 commit comments

Comments
 (0)