Skip to content

Commit 58d7d83

Browse files
committed
add optional hid set protocol callback
1 parent 99d7cfb commit 58d7d83

File tree

3 files changed

+115
-82
lines changed

3 files changed

+115
-82
lines changed

common/usbx_device_classes/inc/ux_device_class_hid.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/***************************************************************************
2-
* Copyright (c) 2024 Microsoft Corporation
3-
*
2+
* Copyright (c) 2024 Microsoft Corporation
3+
*
44
* This program and the accompanying materials are made available under the
55
* terms of the MIT License which is available at
66
* https://opensource.org/licenses/MIT.
7-
*
7+
*
88
* SPDX-License-Identifier: MIT
99
**************************************************************************/
1010

@@ -249,6 +249,7 @@ typedef struct UX_SLAVE_CLASS_HID_STRUCT
249249
UINT ux_device_class_hid_state;
250250
UINT (*ux_device_class_hid_callback)(struct UX_SLAVE_CLASS_HID_STRUCT *hid, UX_SLAVE_CLASS_HID_EVENT *);
251251
UINT (*ux_device_class_hid_get_callback)(struct UX_SLAVE_CLASS_HID_STRUCT *hid, UX_SLAVE_CLASS_HID_EVENT *);
252+
VOID (*ux_device_class_hid_set_protocol_callback)(struct UX_SLAVE_CLASS_HID_STRUCT *hid, ULONG protocol);
252253
VOID (*ux_slave_class_hid_instance_activate)(VOID *);
253254
VOID (*ux_slave_class_hid_instance_deactivate)(VOID *);
254255
UCHAR *ux_device_class_hid_report_address;
@@ -365,6 +366,10 @@ typedef struct UX_SLAVE_CLASS_HID_PARAMETER_STRUCT
365366
ULONG ux_device_class_hid_parameter_report_length;
366367
UINT (*ux_device_class_hid_parameter_callback)(struct UX_SLAVE_CLASS_HID_STRUCT *hid, UX_SLAVE_CLASS_HID_EVENT *);
367368
UINT (*ux_device_class_hid_parameter_get_callback)(struct UX_SLAVE_CLASS_HID_STRUCT *hid, UX_SLAVE_CLASS_HID_EVENT *);
369+
370+
/* Optional callback invoked when protocol changes (boot/report). */
371+
VOID (*ux_device_class_hid_parameter_set_protocol_callback)(struct UX_SLAVE_CLASS_HID_STRUCT *hid, ULONG protocol);
372+
368373
#if defined(UX_DEVICE_CLASS_HID_FLEXIBLE_EVENTS_QUEUE)
369374
ULONG ux_device_class_hid_parameter_event_max_number;
370375
ULONG ux_device_class_hid_parameter_event_max_length;

common/usbx_device_classes/src/ux_device_class_hid_control_request.c

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/***************************************************************************
2-
* Copyright (c) 2024 Microsoft Corporation
3-
*
2+
* Copyright (c) 2024 Microsoft Corporation
3+
*
44
* This program and the accompanying materials are made available under the
55
* terms of the MIT License which is available at
66
* https://opensource.org/licenses/MIT.
7-
*
7+
*
88
* SPDX-License-Identifier: MIT
99
**************************************************************************/
1010

1111
/**************************************************************************/
1212
/**************************************************************************/
13-
/** */
14-
/** USBX Component */
13+
/** */
14+
/** USBX Component */
1515
/** */
1616
/** Device HID Class */
1717
/** */
@@ -28,44 +28,44 @@
2828
#include "ux_device_stack.h"
2929

3030

31-
/**************************************************************************/
32-
/* */
33-
/* FUNCTION RELEASE */
34-
/* */
35-
/* _ux_device_class_hid_control_request PORTABLE C */
31+
/**************************************************************************/
32+
/* */
33+
/* FUNCTION RELEASE */
34+
/* */
35+
/* _ux_device_class_hid_control_request PORTABLE C */
3636
/* 6.1.12 */
3737
/* AUTHOR */
3838
/* */
3939
/* Chaoqiong Xiao, Microsoft Corporation */
4040
/* */
4141
/* DESCRIPTION */
42-
/* */
43-
/* This function manages the based sent by the host on the control */
44-
/* endpoints with a CLASS or VENDOR SPECIFIC type. */
45-
/* */
46-
/* INPUT */
47-
/* */
48-
/* hid Pointer to hid class */
49-
/* */
50-
/* OUTPUT */
51-
/* */
52-
/* None */
53-
/* */
54-
/* CALLS */
55-
/* */
56-
/* _ux_device_stack_transfer_request Transfer request */
42+
/* */
43+
/* This function manages the based sent by the host on the control */
44+
/* endpoints with a CLASS or VENDOR SPECIFIC type. */
45+
/* */
46+
/* INPUT */
47+
/* */
48+
/* hid Pointer to hid class */
49+
/* */
50+
/* OUTPUT */
51+
/* */
52+
/* None */
53+
/* */
54+
/* CALLS */
55+
/* */
56+
/* _ux_device_stack_transfer_request Transfer request */
5757
/* _ux_device_class_hid_report_get Process Get_Report request */
5858
/* _ux_device_class_hid_report_set Process Set_Report request */
5959
/* _ux_device_class_hid_descriptor_send Send requested descriptor */
60-
/* */
61-
/* CALLED BY */
62-
/* */
60+
/* */
61+
/* CALLED BY */
62+
/* */
6363
/* HID Class */
64-
/* */
65-
/* RELEASE HISTORY */
66-
/* */
67-
/* DATE NAME DESCRIPTION */
68-
/* */
64+
/* */
65+
/* RELEASE HISTORY */
66+
/* */
67+
/* DATE NAME DESCRIPTION */
68+
/* */
6969
/* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
7070
/* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
7171
/* prefixed UX to MS_TO_TICK, */
@@ -119,10 +119,10 @@ UX_SLAVE_CLASS_HID *hid;
119119

120120
/* Duration - upper byte of wValue. */
121121
duration = *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_VALUE + 1);
122-
122+
123123
/* Get the class container. */
124124
class_ptr = command -> ux_slave_class_command_class_ptr;
125-
125+
126126
/* Get the storage instance from this class container. */
127127
hid = (UX_SLAVE_CLASS_HID *) class_ptr -> ux_slave_class_instance;
128128

@@ -149,8 +149,8 @@ UX_SLAVE_CLASS_HID *hid;
149149

150150
/* Send the requested descriptor to the host. */
151151
_ux_device_class_hid_descriptor_send(hid, request_value, request_index, request_length);
152-
break;
153-
152+
break;
153+
154154
case UX_DEVICE_CLASS_HID_COMMAND_GET_IDLE:
155155
case UX_DEVICE_CLASS_HID_COMMAND_SET_IDLE:
156156

@@ -204,15 +204,43 @@ UX_SLAVE_CLASS_HID *hid;
204204

205205
case UX_DEVICE_CLASS_HID_COMMAND_GET_PROTOCOL:
206206

207-
/* Send the protocol. */
207+
/* Only boot subclass devices should respond to GET_PROTOCOL. */
208+
if ((hid->ux_slave_class_hid_interface == UX_NULL) ||
209+
(hid->ux_slave_class_hid_interface->ux_slave_interface_descriptor.bInterfaceSubClass != 0x01))
210+
{
211+
return (UX_ERROR);
212+
}
213+
214+
/* Send the protocol to host. */
208215
*transfer_request -> ux_slave_transfer_request_data_pointer = (UCHAR)hid -> ux_device_class_hid_protocol;
209216
_ux_device_stack_transfer_request(transfer_request, 1, request_length);
210217
break;
211218

212219
case UX_DEVICE_CLASS_HID_COMMAND_SET_PROTOCOL:
213220

221+
/* Check protocol must be 0 (Boot) or 1 (Report). */
222+
if ((request_value != UX_DEVICE_CLASS_HID_PROTOCOL_BOOT) &&
223+
(request_value != UX_DEVICE_CLASS_HID_PROTOCOL_REPORT))
224+
{
225+
/* Invalid value: not handled. */
226+
return (UX_ERROR);
227+
}
228+
229+
/* Only boot subclass devices are expected to handle SET_PROTOCOL. */
230+
if ((hid->ux_slave_class_hid_interface == UX_NULL) ||
231+
(hid->ux_slave_class_hid_interface->ux_slave_interface_descriptor.bInterfaceSubClass != 0x01))
232+
{
233+
/* Not a boot subclass: Reject per spec expectations. */
234+
return (UX_ERROR);
235+
}
236+
237+
/* If there is a callback defined by the application, send the protocol to it. */
238+
if (hid -> ux_device_class_hid_set_protocol_callback != UX_NULL)
239+
hid -> ux_device_class_hid_set_protocol_callback(hid, request_value);
240+
214241
/* Accept the protocol. */
215242
hid -> ux_device_class_hid_protocol = request_value;
243+
216244
break;
217245

218246
default:
@@ -224,4 +252,3 @@ UX_SLAVE_CLASS_HID *hid;
224252
/* It's handled. */
225253
return(UX_SUCCESS);
226254
}
227-

0 commit comments

Comments
 (0)