Skip to content

Commit

Permalink
Merge pull request #12 from ThyMYthOS/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
Fabien-Chouteau authored Jun 12, 2024
2 parents 0c6173d + 88e5bd6 commit 383c2a8
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 79 deletions.
26 changes: 13 additions & 13 deletions src/usb-device-control.adb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ package body USB.Device.Control is
-- Exception: handle set address request here
if This.Ctrl.Req.RType = (Dev, 0, Stand, Host_To_Device)
and then
This.Ctrl.Req.Request = 5 -- SET_ADDRESS
This.Ctrl.Req.Request = Req_Set_Address'Enum_Rep

then
This.UDC.Set_Address (UInt7 (This.Ctrl.Req.Value and 16#7F#));
Expand Down Expand Up @@ -252,21 +252,21 @@ package body USB.Device.Control is
end if;

case Req.Request is
when 0 => -- GET_STATUS
when Req_Get_Status'Enum_Rep =>
return Get_Status (This, Req);
when 1 => -- CLEAR_FEATURE
when Req_Clear_Feature'Enum_Rep =>
raise Program_Error with "CLEAR_FEATURE not implemented";
when 3 => -- SET_FEATURE
when Req_Set_Feature'Enum_Rep =>
raise Program_Error with "SET_FEATURE not implemented";
when 5 => -- SET_ADDRESS
when Req_Set_Address'Enum_Rep =>
return Set_Address (This, Req);
when 6 => -- GET_DESCRIPTOR
when Req_Get_Descriptor'Enum_Rep =>
return Get_Descriptor (This, Req);
when 7 => -- SET_DESCRIPTOR
when Req_Set_Descriptor'Enum_Rep =>
raise Program_Error with "SET_DESCRIPTOR not implemented";
when 8 => -- GET_CONFIGURATION
when Req_Get_Configuration'Enum_Rep =>
raise Program_Error with "GET_CONFIGURATION not implemented";
when 9 => -- SET_CONFIGURATION
when Req_Set_Configuration'Enum_Rep =>
return Set_Configuration (This, Req);
when others =>
return Not_Supported;
Expand All @@ -289,10 +289,10 @@ package body USB.Device.Control is
EP : constant EP_Addr := (Id, Dir);
begin
case Req.Request is
when 0 => -- GET_STATUS
when Req_Get_Status'Enum_Rep =>
raise Program_Error with "EP GET_STATUS not implemented";

when 1 => -- CLEAR_FEATURE
when Req_Clear_Feature'Enum_Rep =>
case Req.Value is
when 0 => -- HALT ENDPOINT
This.UDC.EP_Stall (EP, False);
Expand All @@ -301,7 +301,7 @@ package body USB.Device.Control is
raise Program_Error with "Invalid EP CLEAR_FEATURE";
end case;

when 3 => -- SET_FEATURE
when Req_Set_Feature'Enum_Rep =>
case Req.Value is
when 0 => -- HALT ENDPOINT
This.UDC.EP_Stall (EP, True);
Expand All @@ -310,7 +310,7 @@ package body USB.Device.Control is
raise Program_Error with "Invalid EP SET_FEATURE";
end case;

when 16#12# => -- SYNCH_FRAME
when Req_Sync_Feature'Enum_Rep =>
raise Program_Error with "EP SYNCH_FEATURE not implemented";

when others =>
Expand Down
34 changes: 21 additions & 13 deletions src/usb-device-hid.adb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ package body USB.Device.HID is

subtype Dispatch is Abstract_HID_Class'Class;

type Class_Request_Type is
(Get_Report, Get_Idle, Get_Protocol, Set_Report, Set_Idle, Set_Protocol);
for Class_Request_Type use (Get_Report => 1,
Get_Idle => 2,
Get_Protocol => 3,
Set_Report => 9,
Set_Idle => 10,
Set_Protocol => 11);

----------------
-- Initialize --
----------------
Expand Down Expand Up @@ -89,17 +98,16 @@ package body USB.Device.HID is
is
F : constant Natural := Data'First;

USB_DESC_TYPE_INTERFACE : constant := 4;
USB_DESC_TYPE_ENDPOINT : constant := 5;
USB_CLASS_HID : constant := 3;

begin
Data (F + 0 .. F + 24) :=
(9,
USB_DESC_TYPE_INTERFACE,
Dt_Interface'Enum_Rep,
0, -- This.Interface_Index,
0, -- Alternate setting
1, -- Number of endpoints
3, -- Class HID
USB_CLASS_HID, -- Class HID
0, -- Subclass
0, -- Interface protocol 0=none, 1=keyboard, 2=mouse
0, -- Str
Expand All @@ -114,9 +122,9 @@ package body USB.Device.HID is
Dispatch (This).Report_Descriptor'Length, 0, -- Descriptor length

7,
USB_DESC_TYPE_ENDPOINT,
Dt_Endpoint'Enum_Rep,
16#80# or UInt8 (This.EP), -- In EP
3, -- Interrupt EP
Interrupt'Enum_Rep, -- Interrupt EP
16#40#, 0, -- TODO: Max packet size
1 -- Polling interval
);
Expand Down Expand Up @@ -162,11 +170,11 @@ package body USB.Device.HID is

if Req.RType.Typ = Class and then Req.RType.Recipient = Iface then
case Req.Request is
when 1 => -- GET_REPORT
when Get_Report'Enum_Rep =>
return Not_Supported;
when 2 => -- GET_IDLE
when Get_Idle'Enum_Rep =>
return Not_Supported;
when 3 => -- GET_PROTOCOL
when Get_Protocol'Enum_Rep =>
return Not_Supported;
when others =>
raise Program_Error with "Unknown HID request";
Expand All @@ -175,7 +183,7 @@ package body USB.Device.HID is

if Req.RType.Typ = Stand
and then
Req.Request = 6 -- GET_DESCRIPTOR
Req.Request = Req_Get_Descriptor'Enum_Rep
then
declare
-- Index : constant UInt8 := UInt8 (Req.Value and 16#FF#);
Expand Down Expand Up @@ -217,17 +225,17 @@ package body USB.Device.HID is
begin
if Req.RType.Typ = Class and then Req.RType.Recipient = Iface then
case Req.Request is
when 9 => -- SET_REPORT
when Set_Report'Enum_Rep =>
declare
Typ : constant UInt8 := Utils.High (Req.Value);
ID : constant UInt8 := Utils.Low (Req.Value);
begin
return Dispatch (This).Set_Report (Typ, ID, Data);
end;
when 10 => -- SET_IDLE
when Set_Idle'Enum_Rep =>
This.Idle_State := Utils.High (Req.Value);
return Handled;
when 11 => -- SET_PROTOCOL
when Set_Protocol'Enum_Rep =>
return Not_Supported;
when others =>
raise Program_Error with "Unknown HID request";
Expand Down
49 changes: 28 additions & 21 deletions src/usb-device-midi.adb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ package body USB.Device.MIDI is

EP_Buffer_Size : constant := 64;

type Class_Request_Type is
(Get_Report, Get_Idle, Get_Protocol, Set_Report, Set_Idle, Set_Protocol);
for Class_Request_Type use (Get_Report => 1,
Get_Idle => 2,
Get_Protocol => 3,
Set_Report => 9,
Set_Idle => 10,
Set_Protocol => 11);

-------------
-- Receive --
-------------
Expand Down Expand Up @@ -231,9 +240,8 @@ package body USB.Device.MIDI is
Data : out UInt8_Array)
is
F : constant Natural := Data'First;
USB_DESC_TYPE_INTERFACE : constant := 4;
USB_DESC_TYPE_ENDPOINT : constant := 5;

USB_CLASS_AUDIO : constant := 1;
begin

pragma Style_Checks (Off);
Expand All @@ -245,11 +253,11 @@ package body USB.Device.MIDI is
-- descriptor follows inline:
Data (F + 0 .. F + 91) :=
(9, -- sizeof(usbDescrInterface): length of descriptor in bytes
USB_DESC_TYPE_INTERFACE, -- descriptor type
Dt_Interface'Enum_Rep, -- descriptor type
UInt8 (This.Interface_Index), -- index of this interface
0, -- alternate setting for this interface
0, -- endpoints excl 0: number of endpoint descriptors to follow
1, -- Class audio
USB_CLASS_AUDIO, -- Class audio
1, -- Subclass control
0, --
UInt8 (This.Iface_Str), -- string index for interface
Expand All @@ -275,11 +283,11 @@ package body USB.Device.MIDI is
-- B.4.1 Standard MS Interface Descriptor
-- descriptor follows inline:
9, -- length of descriptor in bytes
USB_DESC_TYPE_INTERFACE, -- descriptor type
Dt_Interface'Enum_Rep, -- descriptor type
UInt8 (This.Interface_Index + 1), -- index of this interface
0, -- alternate setting for this interface
2, -- endpoints excl 0: number of endpoint descriptors to follow
1, -- AUDIO
USB_CLASS_AUDIO, -- AUDIO
3, -- MIDI Streaming
0, -- unused
UInt8 (This.Iface_Str), -- string index for interface
Expand Down Expand Up @@ -339,9 +347,9 @@ package body USB.Device.MIDI is
-- B.5.1 Standard Bulk OUT Endpoint Descriptor
-- descriptor follows inline:
9, -- bLenght
USB_DESC_TYPE_ENDPOINT, -- bDescriptorType = endpoint
Dt_Endpoint'Enum_Rep, -- bDescriptorType = endpoint
UInt8 (This.EP), -- bEndpointAddress OUT endpoint number 1
2, -- bmAttributes: 2:Bulk, 3:Interrupt endpoint
Bulk'Enum_Rep, -- bmAttributes: 2:Bulk, 3:Interrupt endpoint
EP_Buffer_Size, 0, -- wMaxPacketSize
10, -- bInterval in ms
0, -- bRefresh
Expand All @@ -360,9 +368,9 @@ package body USB.Device.MIDI is
-- B.6.1 Standard Bulk IN Endpoint Descriptor
-- descriptor follows inline:
9, -- bLenght
USB_DESC_TYPE_ENDPOINT, -- bDescriptorType = endpoint
Dt_Endpoint'Enum_Rep, -- bDescriptorType = endpoint
16#80# or UInt8 (This.EP), -- bEndpointAddress IN endpoint number 1
2, -- bmAttributes: 2: Bulk, 3: Interrupt endpoint
Bulk'Enum_Rep, -- Bulk EP
EP_Buffer_Size, 0, -- wMaxPacketSize
10, -- bInterval in ms
0, -- bRefresh
Expand Down Expand Up @@ -427,18 +435,18 @@ package body USB.Device.MIDI is

if Req.RType.Typ = Class and then Req.RType.Recipient = Iface then
case Req.Request is
when 1 => -- GET_REPORT
when Get_Report'Enum_Rep =>
return Not_Supported;
when 2 => -- GET_IDLE
when Get_Idle'Enum_Rep =>
return Not_Supported;
when 3 => -- GET_PROTOCOL
when Get_Protocol'Enum_Rep =>
return Not_Supported;
when 9 => -- SET_REPORT
when Set_Report'Enum_Rep =>
return Not_Supported;
when 10 => -- SET_IDLE
This.Idle_State := UInt8 (Shift_Right (Req.Value, 8) and 16#FF#);
when Set_Idle'Enum_Rep =>
This.Idle_State := Utils.High (Req.Value);
return Handled;
when 11 => -- SET_PROTOCOL
when Set_Protocol'Enum_Rep =>
return Not_Supported;
when others =>
return Next_Callback;
Expand All @@ -447,12 +455,11 @@ package body USB.Device.MIDI is

if Req.RType.Typ = Stand
and then
Req.Request = 6 -- GET_DESCRIPTOR
Req.Request = Req_Get_Descriptor'Enum_Rep
then
declare
-- Index : constant UInt8 := UInt8 (Req.Value and 16#FF#);
Desc_Type : constant UInt8 :=
UInt8 (Shift_Right (Req.Value, 8) and 16#FF#);
-- Index : constant UInt8 := Utils.Low (Req.Value);
Desc_Type : constant UInt8 := Utils.High (Req.Value);

begin
case Desc_Type is
Expand Down
Loading

0 comments on commit 383c2a8

Please sign in to comment.