Skip to content

Commit

Permalink
Remove length fields as they are obsolete. The lengths is only required
Browse files Browse the repository at this point in the history
during the serialization and therefore is already hardcoded. The only
other convinience is the calculation of the descriptor size, but this is
already wrong. extern structs are padded and therefore do not have the
right sizes.

Signed-off-by: Tobias Kohlbau <[email protected]>
  • Loading branch information
tobiaskohlbau committed Feb 26, 2024
1 parent 64dc121 commit d7e602f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
1 change: 0 additions & 1 deletion build/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ pub const BuildEnvironment = struct {
if (options.target.configure) |configure| {
configure(fw.env, fw);
}


return fw;
}
Expand Down
33 changes: 11 additions & 22 deletions core/src/core/usb.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! 5. Call `usb.task()` within the main loop

const std = @import("std");
const sizeOfCheck = @import("../core.zig").sizeOfCheck;

/// USB Human Interface Device (HID)
pub const hid = @import("usb/hid.zig");
Expand Down Expand Up @@ -511,9 +512,7 @@ pub const Dir = enum(u8) {
};

/// Describes an endpoint within an interface
pub const EndpointDescriptor = extern struct {
/// Length of this struct, must be 7.
length: u8 = 7,
pub const EndpointDescriptor = struct {
/// Type of this descriptor, must be `Endpoint`.
descriptor_type: DescType,
/// Address of this endpoint, where the bottom 4 bits give the endpoint
Expand All @@ -530,7 +529,7 @@ pub const EndpointDescriptor = extern struct {

pub fn serialize(self: *const @This()) [7]u8 {
var out: [7]u8 = undefined;
out[0] = self.length;
out[0] = out.len;
out[1] = @intFromEnum(self.descriptor_type);
out[2] = self.endpoint_address;
out[3] = self.attributes;
Expand All @@ -542,9 +541,7 @@ pub const EndpointDescriptor = extern struct {
};

/// Description of an interface within a configuration.
pub const InterfaceDescriptor = extern struct {
/// Length of this structure, must be 9.
length: u8 = 9,
pub const InterfaceDescriptor = struct {
/// Type of this descriptor, must be `Interface`.
descriptor_type: DescType,
/// ID of this interface.
Expand All @@ -566,7 +563,7 @@ pub const InterfaceDescriptor = extern struct {

pub fn serialize(self: *const @This()) [9]u8 {
var out: [9]u8 = undefined;
out[0] = self.length;
out[0] = out.len;
out[1] = @intFromEnum(self.descriptor_type);
out[2] = self.interface_number;
out[3] = self.alternate_setting;
Expand All @@ -580,9 +577,7 @@ pub const InterfaceDescriptor = extern struct {
};

/// Description of a single available device configuration.
pub const ConfigurationDescriptor = extern struct {
/// Length of this structure, must be 9.
length: u8 = 9,
pub const ConfigurationDescriptor = struct {
/// Type of this descriptor, must be `Config`.
descriptor_type: DescType,
/// Total length of all descriptors in this configuration, concatenated.
Expand Down Expand Up @@ -610,7 +605,7 @@ pub const ConfigurationDescriptor = extern struct {

pub fn serialize(self: *const @This()) [9]u8 {
var out: [9]u8 = undefined;
out[0] = self.length;
out[0] = out.len;
out[1] = @intFromEnum(self.descriptor_type);
out[2] = @intCast(self.total_length & 0xff);
out[3] = @intCast((self.total_length >> 8) & 0xff);
Expand All @@ -625,9 +620,7 @@ pub const ConfigurationDescriptor = extern struct {

/// Describes a device. This is the most broad description in USB and is
/// typically the first thing the host asks for.
pub const DeviceDescriptor = extern struct {
/// Length of this structure, must be 18.
length: u8 = 18,
pub const DeviceDescriptor = struct {
/// Type of this descriptor, must be `Device`.
descriptor_type: DescType,
/// Version of the device descriptor / USB protocol, in binary-coded
Expand Down Expand Up @@ -658,7 +651,7 @@ pub const DeviceDescriptor = extern struct {

pub fn serialize(self: *const @This()) [18]u8 {
var out: [18]u8 = undefined;
out[0] = self.length;
out[0] = out.len;
out[1] = @intFromEnum(self.descriptor_type);
out[2] = @intCast(self.bcd_usb & 0xff);
out[3] = @intCast((self.bcd_usb >> 8) & 0xff);
Expand All @@ -682,9 +675,7 @@ pub const DeviceDescriptor = extern struct {

/// USB Device Qualifier Descriptor
/// This descriptor is mostly the same as the DeviceDescriptor
pub const DeviceQualifierDescriptor = extern struct {
/// Length of this structure, must be 18.
length: u8 = 10,
pub const DeviceQualifierDescriptor = struct {
/// Type of this descriptor, must be `Device`.
descriptor_type: DescType = DescType.DeviceQualifier,
/// Version of the device descriptor / USB protocol, in binary-coded
Expand All @@ -705,7 +696,7 @@ pub const DeviceQualifierDescriptor = extern struct {

pub fn serialize(self: *const @This()) [10]u8 {
var out: [10]u8 = undefined;
out[0] = self.length;
out[0] = out.len;
out[1] = @intFromEnum(self.descriptor_type);
out[2] = @intCast(self.bcd_usb & 0xff);
out[3] = @intCast((self.bcd_usb >> 8) & 0xff);
Expand All @@ -721,8 +712,6 @@ pub const DeviceQualifierDescriptor = extern struct {

/// Layout of an 8-byte USB SETUP packet.
pub const SetupPacket = extern struct {
/// Request type; in practice, this is always either OUT (host-to-device) or
/// IN (device-to-host), whose values are given in the `Dir` enum.
request_type: u8,
/// Request. Standard setup requests are in the `SetupRequest` enum.
/// Devices can extend this with additional types as long as they don't
Expand Down
6 changes: 3 additions & 3 deletions core/src/core/usb/hid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
//! The HID descriptor identifies the length and type of subordinate descriptors for device.

const std = @import("std");
const sizeOfCheck = @import("../../core.zig").sizeOfCheck;

// +++++++++++++++++++++++++++++++++++++++++++++++++
// Common Data Types
Expand Down Expand Up @@ -87,8 +88,7 @@ pub const DescType = enum(u8) {
};

/// USB HID descriptor
pub const HidDescriptor = extern struct {
length: u8 = 9,
pub const HidDescriptor = struct {
descriptor_type: DescType = DescType.Hid,
/// Numeric expression identifying the HID Class Specification release
bcd_hid: u16,
Expand All @@ -103,7 +103,7 @@ pub const HidDescriptor = extern struct {

pub fn serialize(self: *const @This()) [9]u8 {
var out: [9]u8 = undefined;
out[0] = self.length;
out[0] = out.len;
out[1] = @intFromEnum(self.descriptor_type);
out[2] = @intCast(self.bcd_hid & 0xff);
out[3] = @intCast((self.bcd_hid >> 8) & 0xff);
Expand Down
4 changes: 3 additions & 1 deletion examples/raspberrypi-rp2040/src/usb_device.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ pub var DEVICE_CONFIGURATION: usb.DeviceConfiguration = .{
},
.config_descriptor = &.{
.descriptor_type = usb.DescType.Config,
.total_length = @as(u8, @intCast(@sizeOf(usb.ConfigurationDescriptor) + @sizeOf(usb.InterfaceDescriptor) + @sizeOf(usb.EndpointDescriptor) + @sizeOf(usb.EndpointDescriptor))),
// This is calculated via the sizes of underlying descriptors contained in this configuration.
// ConfigurationDescriptor(9) + InterfaceDescriptor(9) * 1 + EndpointDescriptor(8) * 2
.total_length = 34,
.num_interfaces = 1,
.configuration_value = 1,
.configuration_s = 0,
Expand Down
4 changes: 3 additions & 1 deletion examples/raspberrypi-rp2040/src/usb_hid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ pub var DEVICE_CONFIGURATION: usb.DeviceConfiguration = .{
},
.config_descriptor = &.{
.descriptor_type = usb.DescType.Config,
.total_length = @as(u8, @intCast(@sizeOf(usb.ConfigurationDescriptor) + @sizeOf(usb.InterfaceDescriptor) + @sizeOf(usb.EndpointDescriptor) + @sizeOf(usb.EndpointDescriptor))),
// This is calculated via the sizes of underlying descriptors contained in this configuration.
// ConfigurationDescriptor(9) + InterfaceDescriptor(9) * 1 + EndpointDescriptor(8) * 2
.total_length = 34,
.num_interfaces = 1,
.configuration_value = 1,
.configuration_s = 0,
Expand Down

0 comments on commit d7e602f

Please sign in to comment.