Skip to content

Commit dd43679

Browse files
jfischer-nojhedberg
authored andcommitted
usb: host: add command to get the current device configuration
Add command to get the current device configuration. Revise the shell part to have a set|get configuration subcommands. Signed-off-by: Johann Fischer <[email protected]>
1 parent 2cea6e0 commit dd43679

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

subsys/usb/host/usbh_ch9.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,30 @@ int usbh_req_set_cfg(struct usb_device *const udev,
194194
return ret;
195195
}
196196

197+
int usbh_req_get_cfg(struct usb_device *const udev,
198+
uint8_t *const cfg)
199+
{
200+
const uint8_t bmRequestType = USB_REQTYPE_DIR_TO_HOST << 7;
201+
const uint8_t bRequest = USB_SREQ_GET_CONFIGURATION;
202+
const uint16_t wLength = 1;
203+
struct net_buf *buf;
204+
int ret;
205+
206+
buf = usbh_xfer_buf_alloc(udev, wLength);
207+
if (!buf) {
208+
return -ENOMEM;
209+
}
210+
211+
ret = usbh_req_setup(udev, bmRequestType, bRequest, 0, 0, wLength, buf);
212+
if (ret == 0 && buf->len == wLength) {
213+
*cfg = buf->data[0];
214+
}
215+
216+
usbh_xfer_buf_free(udev, buf);
217+
218+
return ret;
219+
}
220+
197221
int usbh_req_set_alt(struct usb_device *const udev,
198222
const uint8_t iface, const uint8_t alt)
199223
{

subsys/usb/host/usbh_ch9.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ int usbh_req_set_address(struct usb_device *const udev,
4444
int usbh_req_set_cfg(struct usb_device *const udev,
4545
const uint8_t cfg);
4646

47+
int usbh_req_get_cfg(struct usb_device *const udev,
48+
uint8_t *const cfg);
49+
4750
int usbh_req_set_sfs_rwup(struct usb_device *const udev);
4851

4952
int usbh_req_clear_sfs_rwup(struct usb_device *const udev);

subsys/usb/host/usbh_shell.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ static int cmd_feature_set_prst(const struct shell *sh,
291291
return err;
292292
}
293293

294-
static int cmd_device_config(const struct shell *sh,
295-
size_t argc, char **argv)
294+
static int cmd_config_set(const struct shell *sh,
295+
size_t argc, char **argv)
296296
{
297297
uint8_t cfg;
298298
int err;
@@ -310,6 +310,23 @@ static int cmd_device_config(const struct shell *sh,
310310
return err;
311311
}
312312

313+
static int cmd_config_get(const struct shell *sh,
314+
size_t argc, char **argv)
315+
{
316+
uint8_t cfg;
317+
int err;
318+
319+
err = usbh_req_get_cfg(udev, &cfg);
320+
if (err) {
321+
shell_error(sh, "host: Failed to set configuration");
322+
} else {
323+
shell_print(sh, "host: Device 0x%02x, current configuration %u",
324+
udev->addr, cfg);
325+
}
326+
327+
return err;
328+
}
329+
313330
static int cmd_device_interface(const struct shell *sh,
314331
size_t argc, char **argv)
315332
{
@@ -484,11 +501,19 @@ SHELL_STATIC_SUBCMD_SET_CREATE(feature_clear_cmds,
484501
SHELL_SUBCMD_SET_END
485502
);
486503

504+
SHELL_STATIC_SUBCMD_SET_CREATE(config_cmds,
505+
SHELL_CMD_ARG(get, NULL, NULL,
506+
cmd_config_get, 1, 0),
507+
SHELL_CMD_ARG(set, NULL, "<configuration>",
508+
cmd_config_set, 2, 0),
509+
SHELL_SUBCMD_SET_END
510+
);
511+
487512
SHELL_STATIC_SUBCMD_SET_CREATE(device_cmds,
488513
SHELL_CMD_ARG(address, NULL, "<address>",
489514
cmd_device_address, 2, 0),
490-
SHELL_CMD_ARG(config, NULL, "<config>",
491-
cmd_device_config, 2, 0),
515+
SHELL_CMD_ARG(config, &config_cmds, "get|set configuration",
516+
NULL, 1, 0),
492517
SHELL_CMD_ARG(interface, NULL, "<interface> <alternate>",
493518
cmd_device_interface, 3, 0),
494519
SHELL_CMD_ARG(descriptor, &desc_cmds, "descriptor request",

0 commit comments

Comments
 (0)