Skip to content

Commit 9cb0fbf

Browse files
jfischer-nokartben
authored andcommitted
usb: device_next: fix Get Status request response
We need to track the self-powered status of the device independently of the D6 bit in the bmAttributes value of the configuration descriptor because the Get Status request about the self-powered status is valid in address state and we support multiple configurations. Signed-off-by: Johann Fischer <[email protected]>
1 parent 467e15d commit 9cb0fbf

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

include/zephyr/usb/usbd.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ struct usbd_status {
254254
unsigned int suspended : 1;
255255
/** USB remote wake-up feature is enabled */
256256
unsigned int rwup : 1;
257+
/** USB device is self-powered */
258+
unsigned int self_powered : 1;
257259
/** USB device speed */
258260
enum usbd_speed speed : 2;
259261
};
@@ -1066,6 +1068,17 @@ bool usbd_is_suspended(struct usbd_context *uds_ctx);
10661068
*/
10671069
int usbd_wakeup_request(struct usbd_context *uds_ctx);
10681070

1071+
/**
1072+
* @brief Set the self-powered status of the USB device
1073+
*
1074+
* The status is used in the Self Powered field of the Get Status request
1075+
* response to indicate whether the device is currently self-powered.
1076+
*
1077+
* @param[in] uds_ctx Pointer to a device context
1078+
* @param[in] status Sets self-powered status if true, clears it otherwise
1079+
*/
1080+
void usbd_self_powered(struct usbd_context *uds_ctx, const bool status);
1081+
10691082
/**
10701083
* @brief Get actual device speed
10711084
*

samples/subsys/usb/common/sample_usbd_init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ struct usbd_context *sample_usbd_setup_device(usbd_msg_cb_t msg_cb)
159159
/* doc functions register end */
160160

161161
sample_fix_code_triple(&sample_usbd, USBD_SPEED_FS);
162+
usbd_self_powered(&sample_usbd, attributes & USB_SCD_SELF_POWERED);
162163

163164
if (msg_cb != NULL) {
164165
/* doc device init-and-msg start */

subsys/usb/device_next/usbd_ch9.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ static int sreq_get_status(struct usbd_context *const uds_ctx,
426426

427427
response = uds_ctx->status.rwup ?
428428
USB_GET_STATUS_REMOTE_WAKEUP : 0;
429+
response |= uds_ctx->status.self_powered ?
430+
USB_GET_STATUS_SELF_POWERED : 0;
429431
break;
430432
case USB_REQTYPE_RECIPIENT_ENDPOINT:
431433
response = usbd_ep_is_halted(uds_ctx, ep) ? BIT(0) : 0;

subsys/usb/device_next/usbd_device.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ int usbd_wakeup_request(struct usbd_context *const uds_ctx)
197197
return ret;
198198
}
199199

200+
void usbd_self_powered(struct usbd_context *uds_ctx, const bool status)
201+
{
202+
usbd_device_lock(uds_ctx);
203+
uds_ctx->status.self_powered = status;
204+
usbd_device_unlock(uds_ctx);
205+
}
206+
200207
bool usbd_is_suspended(struct usbd_context *uds_ctx)
201208
{
202209
return uds_ctx->status.suspended;

0 commit comments

Comments
 (0)