[PATCH 1/4] usb: otg: Add function to set dr_mode

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Apr 20 03:42:35 PDT 2026


Hi,

On 4/20/26 11:02 AM, Sascha Hauer wrote:
> So far we can only set the dr_mode on the command line. Add a function
> for it.
> 
> Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
> ---
>  drivers/usb/otg/otgdev.c | 20 ++++++++++++++++++++
>  include/linux/usb/usb.h  |  1 +
>  2 files changed, 21 insertions(+)
> 
> diff --git a/drivers/usb/otg/otgdev.c b/drivers/usb/otg/otgdev.c
> index 5a86263430..50d8105be3 100644
> --- a/drivers/usb/otg/otgdev.c
> +++ b/drivers/usb/otg/otgdev.c
> @@ -74,6 +74,26 @@ int otg_device_get_mode(struct device *dev)
>  	return otg->cur_mode;
>  }
>  
> +int otg_device_set_dr_mode(struct device *dev, enum usb_dr_mode mode)
> +{
> +	struct otg_mode *otg;
> +	int ret;
> +
> +	if (dev->bus != &otg_bus_type)
> +		return -ENODEV;
> +
> +	otg = dev->priv;
> +
> +	ret = otg->set_mode_callback(otg->ctx, mode);
> +	if (ret)
> +		return ret;

This doesn't guard against switching from peripheral to host and back
unlike the param version, which only allows switching away from otg.

I think the existing logic should be moved into this function and
otg_set_mode changed to a wrapper around it.

> +
> +	otg->cur_mode = mode;
> +	otg->var_mode = mode;
> +
> +	return 0;
> +}
> +
>  int usb_register_otg_device(struct device *parent,
>  			    int (*set_mode)(void *ctx, enum usb_dr_mode mode), void *ctx)
>  {
> diff --git a/include/linux/usb/usb.h b/include/linux/usb/usb.h
> index c25f3d73c3..6aa9e55cc9 100644
> --- a/include/linux/usb/usb.h
> +++ b/include/linux/usb/usb.h
> @@ -477,6 +477,7 @@ int usb_register_otg_device(struct device *parent,
>  			    int (*set_mode)(void *ctx, enum usb_dr_mode mode), void *ctx);
>  
>  int otg_device_get_mode(struct device *dev);
> +int otg_device_set_dr_mode(struct device *dev, enum usb_dr_mode mode);
>  
>  extern struct bus_type otg_bus_type;
>  
> 

-- 
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |




More information about the barebox mailing list