All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Chen <peter.chen@nxp.com>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Jun Li <lijun.kernel@gmail.com>,
	"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>
Subject: Re: port power is on again after turning off by user space
Date: Wed, 16 Dec 2020 02:56:20 +0000	[thread overview]
Message-ID: <20201216025549.GA5595@b29397-desktop> (raw)
In-Reply-To: <20201215155541.GA195633@rowland.harvard.edu>

On 20-12-15 10:55:41, Alan Stern wrote:
> On Tue, Dec 15, 2020 at 09:57:53AM +0000, Peter Chen wrote:
> >  
> > > > > Hi Alan,
> > > > >
> > > > > I use one HUB power control application
> > > > >
> > > (https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.
> > > com%2Fmvp%2Fuhubctl&amp;data=04%7C01%7Cpeter.chen%40nxp.com%7C
> > > 736ece19bc7a430c98b808d8a0b6975c%7C686ea1d3bc2b4c6fa92cd99c5c3016
> > > 35%7C0%7C0%7C637436053362151022%7CUnknown%7CTWFpbGZsb3d8eyJ
> > > WIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7
> > > C1000&amp;sdata=lptf1XO5yeb6lQbAFlKUrZ%2BEX5ATXQRftGwm26WowFA%
> > > 3D&amp;reserved=0) to investigate power switchable HUB, and find the kernel
> > > turns port power on again at drivers/usb/core/hub.c, after port power is turned
> > > off by user space.
> > > > >
> > > > > 5122                 if (hub_is_port_power_switchable(hub)
> > > > > 5123                                 && !port_is_power_on(hub,
> > > portstatus)
> > > > > 5124                                 && !port_dev->port_owner)
> > > > > 5125                         set_port_feature(hdev, port1,
> > > USB_PORT_FEAT_POWER);
> > > > >
> > > > > The main sequence for testing turn port power off like below:
> > > > >
> > > > > - uhubctl sends command to turn specifc port (eg, 2-1.4) power off.
> > > > > - devio at kernel gets that command, and send to hub.
> > > > > - port power is off, the hub_event is triggered due to port status is changed.
> > > > > - usb_disconnect is called, but port power is on again by kernel at function
> > > hub_port_connect.
> > > > >
> > > > > I can't find the code history why the port power needs to turn on after
> > > device is disconnected, do you know why?
> > > > > Any sugguestions to fix it? Thanks.
> > > >
> > > > Seems in this case the port need claimed by user app, I am seeing this
> > > > commit
> > > >
> > > > commit fbaecff06a7db4defa899a664fe2758e5161b39d
> > > > Author: Deepak Das <deepakdas.linux@gmail.com>
> > > > Date:   Wed Jan 21 23:39:58 2015 +0530
> > > >
> > > >     usb: core: hub: modify hub reset logic in hub driver
> > > >
> > > >     Currently if port power is turned off by user on hub port
> > > >     using USBDEVFS then port power is turned back ON
> > > >     by hub driver.
> > > >     This commit modifies hub reset logic in hub_port_connect() to prevent
> > > >     hub driver from turning back the port power ON if port is not owned
> > > >     by kernel.
> > > >
> > > >     Signed-off-by: Deepak Das <deepakdas.linux@gmail.com>
> > > >     Acked-by: Alan Stern <stern@rowland.harvard.edu>
> > > >     Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > > >
> > > > diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index
> > > > b4bfa3a..3e9c4d4 100644
> > > > --- a/drivers/usb/core/hub.c
> > > > +++ b/drivers/usb/core/hub.c
> > > > @@ -4655,9 +4655,13 @@ static void hub_port_connect(struct usb_hub
> > > > *hub, int port1, u16 portstatus,
> > > >         if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
> > > >                         test_bit(port1, hub->removed_bits)) {
> > > >
> > > > -               /* maybe switch power back on (e.g. root hub was reset)
> > > */
> > > > +               /*
> > > > +                * maybe switch power back on (e.g. root hub was reset)
> > > > +                * but only if the port isn't owned by someone else.
> > > > +                */
> > > >                 if (hub_is_port_power_switchable(hub)
> > > > -                               && !port_is_power_on(hub,
> > > portstatus))
> > > > +                               && !port_is_power_on(hub,
> > > portstatus)
> > > > +                               && !port_dev->port_owner)
> > > >                         set_port_feature(hdev, port1,
> > > > USB_PORT_FEAT_POWER);
> > > >
> > > >                 if (portstatus & USB_PORT_STAT_ENABLE)
> > > >
> > > 
> > > Yes, I saw this commit. But the port is owned by kernel, the device on the port
> > > could be enumerated by kernel, just the power on the port could be changed by
> > > user space.
> 
> You've got the general idea.
> 
> Normally ports are owned by the hub driver.  If one of them loses power 
> for some reason (for example, the user turns it off), the hub driver 
> will turn the power back on.  This is because the hub driver wants 
> ports to be powered at all times unless they are in runtime suspend.
> 
> The way to prevent the hub driver from managing the port power is to 
> claim the port for the user, by issuing the USBDEVFS_CLAIM_PORT ioctl.  
> Also, when that is done, the kernel wno't try to manage a device 
> attached to the port -- that is, the kernel won't automatically install 
> a configuration for a new device and it won't try to probe drivers for 
> the device's interfaces if the user installs a config.
> 

Alan, we have several use cases for power switchable HUB, one of the use
cases is USB port is managed by kernel (eg, needs mass storage
class), but needs to toggle port power, is it reasonable we add a sysfs
entry to support it?

-- 

Thanks,
Peter Chen

  reply	other threads:[~2020-12-16  2:57 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-15  3:31 port power is on again after turning off by user space Peter Chen
2020-12-15  5:02 ` Jun Li
2020-12-15  5:14   ` Peter Chen
2020-12-15  9:57     ` Peter Chen
2020-12-15 15:55       ` Alan Stern
2020-12-16  2:56         ` Peter Chen [this message]
2020-12-16 15:51           ` Alan Stern
2020-12-21  5:37             ` Peter Chen
2020-12-21 16:25               ` Alan Stern
2020-12-22  2:02                 ` Peter Chen
2020-12-22  2:35                   ` Alan Stern

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201216025549.GA5595@b29397-desktop \
    --to=peter.chen@nxp.com \
    --cc=lijun.kernel@gmail.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.