Skip to content

Commit 6a87d4c

Browse files
committed
usb: improve ms compatible ID and function name string handling
Signed-off-by: Benedek Kupper <kupper.benedek@gmail.com>
1 parent 90f6b35 commit 6a87d4c

8 files changed

Lines changed: 21 additions & 13 deletions

File tree

c2usb/usb/df/class/cdc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function::get_base_functional_descriptors(class_info cinfo, uint8_t if_index, df
3939
};
4040
auto* descs = buffer.allocate<desc_set>(cinfo, if_index);
4141

42-
descs->if_assoc.iFunction = descs->iface.iInterface = to_istring(0);
42+
descs->if_assoc.iFunction = descs->iface.iInterface = name_istring();
4343

4444
return &descs->iface;
4545
}

c2usb/usb/df/class/hid.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ void function::describe_config(const config::interface& iface, uint8_t if_index,
113113

114114
iface_desc->bInterfaceNumber = if_index;
115115
iface_desc->bInterfaceClass = CLASS_CODE;
116-
iface_desc->bInterfaceSubClass = protocol_ != boot_protocol_mode::NONE;
117-
iface_desc->bInterfaceProtocol = static_cast<uint8_t>(protocol_);
118-
iface_desc->iInterface = to_istring(0);
116+
iface_desc->bInterfaceSubClass = protocol_mode() != boot_protocol_mode::NONE;
117+
iface_desc->bInterfaceProtocol = static_cast<uint8_t>(protocol_mode());
118+
iface_desc->iInterface = name_istring();
119119
iface_desc->bNumEndpoints = describe_endpoints(iface, buffer);
120120
assert((iface.endpoints()[0].address().direction() == direction::IN) and
121121
((1 == iface_desc->bNumEndpoints) or

c2usb/usb/df/class/hid.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class function : public app_base_function
7070
void control_setup_request(message& msg, const config::interface& iface) override;
7171
void control_data_complete(message& msg, const config::interface& iface) override;
7272

73+
boot_protocol_mode protocol_mode() const { return protocol_; }
7374
const boot_protocol_mode protocol_;
7475
};
7576

c2usb/usb/df/function.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class function : public polymorphic
4949
void init(const config::interface& iface, mac* m);
5050
void deinit(const config::interface& iface);
5151

52-
virtual const char* ms_compatible_id() const { return nullptr; }
52+
virtual std::string_view ms_compatible_id() const { return {}; }
5353

5454
virtual void transfer_complete([[maybe_unused]] ep_handle eph,
5555
[[maybe_unused]] const transfer& t)
@@ -93,7 +93,10 @@ class function : public polymorphic
9393

9494
virtual void send_string(uint8_t rel_index, string_message& smsg);
9595

96-
virtual uint8_t get_alt_setting([[maybe_unused]] const config::interface& iface) { return 0; }
96+
virtual uint8_t get_alt_setting([[maybe_unused]] const config::interface& iface) const
97+
{
98+
return 0;
99+
}
97100

98101
virtual void enable([[maybe_unused]] const config::interface& iface,
99102
[[maybe_unused]] uint8_t alt_sel)
@@ -138,6 +141,8 @@ class named_function : public function
138141

139142
void send_string(uint8_t rel_index, string_message& smsg) override;
140143

144+
istring name_istring() const { return (name_ != nullptr) ? to_istring(0) : 0; }
145+
141146
private:
142147
const char_t* const name_{};
143148
};

c2usb/usb/df/vendor/microsoft/os_extension.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@ void descriptors::get_msos2_function_subset(const config::interface& iface, uint
1717
df::buffer& buffer)
1818
{
1919
// the only interesting thing is the compatible ID
20-
auto* compat_id = iface.function().ms_compatible_id();
21-
if (compat_id == nullptr)
20+
auto compat_id = iface.function().ms_compatible_id();
21+
if (compat_id.empty())
2222
{
2323
return;
2424
}
2525
auto* func_header = buffer.allocate<usb::microsoft::function_subset_header>();
2626
func_header->bFirstInterface = iface_index;
2727

2828
auto* comp_id_desc = buffer.allocate<usb::microsoft::compatible_id>();
29-
std::string_view(compat_id).copy(comp_id_desc->CompatibleID,
30-
sizeof(comp_id_desc->CompatibleID));
29+
compat_id.copy(comp_id_desc->CompatibleID, sizeof(comp_id_desc->CompatibleID));
3130

3231
// When finished with the contents, save the total size of the subset
3332
func_header->wSubsetLength =

c2usb/usb/df/vendor/microsoft/xinput.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void xfunction::describe_config(const config::interface& iface, uint8_t if_index
2929
iface_desc->bInterfaceClass = xusb::CLASS_CODE;
3030
iface_desc->bInterfaceSubClass = xusb::SUBCLASS_CODE;
3131
iface_desc->bInterfaceProtocol = xusb::PROTOCOL_CODE;
32-
iface_desc->iInterface = to_istring(0);
32+
iface_desc->iInterface = name_istring();
3333
iface_desc->bNumEndpoints = describe_endpoints(iface, buffer);
3434
assert((iface_desc->bNumEndpoints == 2) and
3535
(iface.endpoints()[0].address().direction() == direction::IN) and

c2usb/usb/df/vendor/microsoft/xinput.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ class xfunction : public df::hid::app_base_function
3333
void describe_config(const config::interface& iface, uint8_t if_index,
3434
df::buffer& buffer) override;
3535

36-
const char* ms_compatible_id() const override { return usb::microsoft::xusb::COMPATIBLE_ID; }
36+
std::string_view ms_compatible_id() const override
37+
{
38+
return usb::microsoft::xusb::COMPATIBLE_ID;
39+
}
3740

3841
void enable(const config::interface& iface, uint8_t alt_sel) override;
3942
};

c2usb/usb/vendor/microsoft_xusb.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ inline namespace xusb22
3131
"GamepadStickUsage"=dword:31303532
3232
*/
3333

34-
constexpr const char* COMPATIBLE_ID = "XUSB20";
34+
constexpr std::string_view COMPATIBLE_ID{"XUSB20"};
3535

3636
constexpr uint8_t CLASS_CODE = 0xFF;
3737
constexpr uint8_t SUBCLASS_CODE = 0x5D;

0 commit comments

Comments
 (0)