Skip to contentSkip to conversation

Common power driver for Linux gadgets

RFC PATCH David Brownell 2 participants 8 messages Apr 13, 2007 – May 7, 2007

lore
Share & tools
lore
#0 lore raw
Quoted 4 lines

This driver used to stop code/logic duplication through different machines we porting at handhelds.org. pda_power register machs' power supplies, and will take care about notifying batteries about power changes through external power interface.

It gets USB power management wrong though. Have a look at two I2C drivers in drivers/i2c/chips: tps65010.c (which would talk to this API) and then isp1301_omap.c (which would talk to the tps65010 driver). The tps65010 chip accepts the same two power sources you're addressing here, as part of its battery charging responsibilities. [1]

Key points:

- The API needs to say *how much power* can be drawn. Common values are 8 mA (OTG peripherals before configuration), 100 mA (non-OTG ones before configuration), 500 mA (high power configurations) ... but the exact value depends on what's listed in the configuration descriptor for the chosen configuration, any value 0..500 mA (increments of two) could be appropriate.

- Sensing VBUS power is not the same thing as being allowed to consume it. Again, USB OTG devices are different: OTG hosts **SUPPLY** the current, so you really don't want to be telling the OTG transceiver to fire up its charge pump (say, 3.0V VBAT converted to 5V VBUS) while at the same time telling the battery manager to use that battery-derived VBUS current to recharge its battery! Not only would that waste power, but it also deprives the peripheral of the power it needs to draw.

In general, no component other than the USB peripheral (or host) controller driver has any business trying to control how VBUS power is used. It's likely to get it wrong ... as shown by this patch. ;)

And that's exactly why the USB gadget API has had calls to manage the USB power consumption since mid-2004. And I wonder why H5000 code evidently doesn't implement those calls.

- Dave

[1] You may find http://www.linux-usb.org/gadget/h2-otg.html useful too. It gives a high level map of the complicated OTG case, including those drivers, and points out how simpler peripheral-only systems could behave with the same drivers. However it does turn out to be useful if USB peripheral drivers have a "transciever" notion that can receive "VBUS present" interrupts, letting the peripheral controller driver power down almost *everything* to save power, and that otg_transceiver interface does that job.

#1 lore raw

Hello David,

Quoted 38 lines · David Brownell
Quoted 4 lines

This driver used to stop code/logic duplication through different machines we porting at handhelds.org. pda_power register machs' power supplies, and will take care about notifying batteries about power changes through external power interface.

It gets USB power management wrong though. Have a look at two I2C drivers in drivers/i2c/chips: tps65010.c (which would talk to this API) and then isp1301_omap.c (which would talk to the tps65010 driver). The tps65010 chip accepts the same two power sources you're addressing here, as part of its battery charging responsibilities. [1]

Key points:

- The API needs to say *how much power* can be drawn. Common values are 8 mA (OTG peripherals before configuration), 100 mA (non-OTG ones before configuration), 500 mA (high power configurations) ... but the exact value depends on what's listed in the configuration descriptor for the chosen configuration, any value 0..500 mA (increments of two) could be appropriate.

- Sensing VBUS power is not the same thing as being allowed to consume it. Again, USB OTG devices are different: OTG hosts **SUPPLY** the current, so you really don't want to be telling the OTG transceiver to fire up its charge pump (say, 3.0V VBAT converted to 5V VBUS) while at the same time telling the battery manager to use that battery-derived VBUS current to recharge its battery! Not only would that waste power, but it also deprives the peripheral of the power it needs to draw.

In general, no component other than the USB peripheral (or host) controller driver has any business trying to control how VBUS power is used. It's likely to get it wrong ... as shown by this patch. ;)

And that's exactly why the USB gadget API has had calls to manage the USB power consumption since mid-2004. And I wonder why H5000 code evidently doesn't implement those calls.

- Dave

Quoted 8 lines

[1] You may find http://www.linux-usb.org/gadget/h2-otg.html useful too. It gives a high level map of the complicated OTG case, including those drivers, and points out how simpler peripheral-only systems could behave with the same drivers. However it does turn out to be useful if USB peripheral drivers have a "transciever" notion that can receive "VBUS present" interrupts, letting the peripheral controller driver power down almost *everything* to save power, and that otg_transceiver interface does that job.

I see. Current devices I have just consumes power from USB host. I.e. they able to boot using only USB and discharged battery. :-/ Even more, HP iPaq hx4700 able to charge battery from USB (using that driver). We just setting USB charging GPIO, and it starts consume power from the host.

But I got the point, and yes I can't explain why it works correctly.

(1) Anyway, you're hinting that you'd want to see some "usb_vbus_start_consume_power(how much)" callback, which will be handled by gadget driver? Or it should just start using some gadget api? Or even pda_battery must become an "usb power supplicant" gadget itself to consume power by law?

-

I'm stuck in null-modem century, so don't wonder my USB-dumbness. Will read http://www.linux-usb.org/gadget/h2-otg.html, of course.

Though I'd appreciate answers for (1), thus I can think in right direction from the start.

Much thanks for your comments.

Signature · 4 lines
Anton Vorontsov
email: [email protected]
backup email: [email protected]
irc://irc.freenode.org/bd2
#2 lore raw
Quoted 8 lines · Anton Vorontsov

Hello David,

Quoted 6 lines

- The API needs to say *how much power* can be drawn...

- Sensing VBUS power is not the same thing as being allowed to consume it. Again, USB OTG devices are different: OTG hosts **SUPPLY** the current, ...

Quoted 5 lines

I see. Current devices I have just consumes power from USB host. I.e. they able to boot using only USB and discharged battery. :-/ Even more, HP iPaq hx4700 able to charge battery from USB (using that driver). We just setting USB charging GPIO, and it starts consume power from the host.

Sounds like it could be more power than the host expects it to consume; or in some cases, not as much as it could ... ISTR only the Ethernet gadget defaults to 100mA (in non-OTG configs), the others are set for only 2 mA (expecting self-powered configs).

Quoted 1 line

But I got the point, and yes I can't explain why it works correctly.

It probably doesn't work correctly. But it's not broken enough to fail badly.

Quoted 3 lines

(1) Anyway, you're hinting that you'd want to see some "usb_vbus_start_consume_power(how much)" callback, which will be handled by gadget driver?

The gadget API includes usb_gadget_vbus_draw(gadget, mA) which the gadget drivers (e.g. network, storage, tty) issue. How a given board implements that is best wrapped in a transceiver abstraction, which would also issue the usb_gadget_vbus_{connect,disconnect}(gadget) notifications back to the controller.

That is, in the pure-peripheral case there are at three entities involved outside the battery/power framework: the gadget driver (what protocol is used over USB), the peripheral controller driver (talking to USB hardware), and the transceiver (sensing VBUS and managing power state transitions of that controller).

In dumb cases (most reference boards!) the transceiver is stubbed out, and there's no power management either to put the USB stuff into lowpower mode when it's inactive, or to make use of VBUS power to help manage the battery (what battery?). In less dumb cases, VBUS is only used to sense whether the host is there. In vaguely smart cases VBUS will be used to power the USB hardware (saving maybe 40 mA of battery power in one case). Recharging the battery is for some reason not always supported even in product boards.

Quoted 3 lines

Or it should just start using some gadget api?
Or even pda_battery must become an "usb power supplicant" gadget itself to consume power by law?

I don't know about this "supplicant" thing. That's actually below what the gadget API exposes -- implementation detail, but you're thinking about how to implement such board-specific details. I guess I'd think that's a board-specific detail in the same way that the transceiver is; you'll observe isp1301_omap is where such things hook up in that example implementation.

- Dave

Quoted 14 lines

I'm stuck in null-modem century, so don't wonder my USB-dumbness. Will read http://www.linux-usb.org/gadget/h2-otg.html, of course.

Though I'd appreciate answers for (1), thus I can think in right direction from the start.

Much thanks for your comments.

Signature · 4 lines
Anton Vorontsov
email: [email protected]
backup email: [email protected]
irc://irc.freenode.org/bd2
#3 lore raw
Quoted 21 lines · David Brownell
Quoted 8 lines · Anton Vorontsov

Hello David,

Quoted 6 lines

- The API needs to say *how much power* can be drawn...

- Sensing VBUS power is not the same thing as being allowed to consume it. Again, USB OTG devices are different: OTG hosts **SUPPLY** the current, ...

Quoted 5 lines

I see. Current devices I have just consumes power from USB host. I.e. they able to boot using only USB and discharged battery. :-/ Even more, HP iPaq hx4700 able to charge battery from USB (using that driver). We just setting USB charging GPIO, and it starts consume power from the host.

Sounds like it could be more power than the host expects it to consume; or in some cases, not as much as it could ... ISTR only the Ethernet gadget defaults to 100mA (in non-OTG configs), the others are set for only 2 mA (expecting self-powered configs).

Quoted 4 lines
Quoted 1 line

But I got the point, and yes I can't explain why it works correctly.

It probably doesn't work correctly. But it's not broken enough to fail badly.

Can that comment be an explanation?

--- drivers/usb/gadget/pxa2xx_udc.c:
static const struct usb_gadget_ops pxa2xx_udc_ops = {

        .get_frame      = pxa2xx_udc_get_frame,
        .wakeup         = pxa2xx_udc_wakeup,
        .vbus_session   = pxa2xx_udc_vbus_session,
        .pullup         = pxa2xx_udc_pullup,
        // .vbus_draw ... boards may consume current from VBUS, up to
        // 100-500mA based on config.  the 500uA suspend ceiling means
        // that exclusively vbus-powered PXA designs violate USB specs.
};

Comparing to omap_udc.

--- drivers/usb/gadget/omap_udc.c
static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)

{
        struct omap_udc *udc;

        udc = container_of(gadget, struct omap_udc, gadget);
        if (udc->transceiver)
                return otg_set_power(udc->transceiver, mA);
        return -EOPNOTSUPP;
}
[...]
static struct usb_gadget_ops omap_gadget_ops = {
        .get_frame              = omap_get_frame,
        .wakeup                 = omap_wakeup,
        .set_selfpowered        = omap_set_selfpowered,
        .vbus_session           = omap_vbus_session,
        .vbus_draw              = omap_vbus_draw,
        .pullup                 = omap_pullup,

};

Regarding API. If you all you want is to know how much power you need to ask from VBUS, we can extend external power interface... thus suppliers could ask their power consumption requirements in mA/uA, and these requests will be forwarded to power supply driver, and power driver will forward that request to USB transceiver (via platform hook).

Signature · 4 lines
Anton Vorontsov
email: [email protected]
backup email: [email protected]
irc://irc.freenode.org/bd2
#4 lore raw
Quoted 9 lines · Anton Vorontsov
Quoted 4 lines
Quoted 1 line

But I got the point, and yes I can't explain why it works correctly.

It probably doesn't work correctly. But it's not broken enough to fail badly.

Can that comment be an explanation?

--- drivers/usb/gadget/pxa2xx_udc.c:
static const struct usb_gadget_ops pxa2xx_udc_ops = {

Quoted 8 lines
        .get_frame      = pxa2xx_udc_get_frame,
        .wakeup         = pxa2xx_udc_wakeup,
        .vbus_session   = pxa2xx_udc_vbus_session,
        .pullup         = pxa2xx_udc_pullup,
        // .vbus_draw ... boards may consume current from VBUS, up to
        // 100-500mA based on config.  the 500uA suspend ceiling means
        // that exclusively vbus-powered PXA designs violate USB specs.

That's basically a "plug in implementation here". Nobody's yet done that on a platform that _uses_ the VBUS power.

Quoted 5 lines

};

Comparing to omap_udc.

Quoted 8 lines

--- drivers/usb/gadget/omap_udc.c
static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)

{
        struct omap_udc *udc;

        udc = container_of(gadget, struct omap_udc, gadget);
        if (udc->transceiver)
                return otg_set_power(udc->transceiver, mA);

Where the transceiver would then delegate to something else, like the tps65010 driver.

Quoted 9 lines
        return -EOPNOTSUPP;
}
[...]
static struct usb_gadget_ops omap_gadget_ops = {
        .get_frame              = omap_get_frame,
        .wakeup                 = omap_wakeup,
        .set_selfpowered        = omap_set_selfpowered,
        .vbus_session           = omap_vbus_session,
        .vbus_draw              = omap_vbus_draw,

... which has most certainly been on platforms which are hooked up to draw power from VBUS.

Quoted 10 lines

.pullup = omap_pullup,
};

Regarding API. If you all you want is to know how much power you need to ask from VBUS, we can extend external power interface... thus suppliers could ask their power consumption requirements in mA/uA, and these requests will be forwarded to power supply driver, and power driver will forward that request to USB transceiver (via platform hook).

I don't folow what you're saying. The control flow *MUST* be that the USB stack provides the only indication of how much power may be drawn through the VBUS supply. Nothing else in the system has the knowledge of what's legal, and when.

If you want to talk about a "supplier", the way to put it might then be that the USB stack is saying "here's N mA power for you"; it's supplying the power, not the other way around.

There's no "ask" involved, since the host controls "N". So the host supplies, the USB gadget stack interprets that, and some power component must obeys. That includes rules like reducing VBUS draw to ~500 uA when the host suspends the USB link.

- Dave

#5 lore raw
Quoted 71 lines · David Brownell
Quoted 9 lines · Anton Vorontsov
Quoted 4 lines
Quoted 1 line

But I got the point, and yes I can't explain why it works correctly.

It probably doesn't work correctly. But it's not broken enough to fail badly.

Can that comment be an explanation?

--- drivers/usb/gadget/pxa2xx_udc.c:
static const struct usb_gadget_ops pxa2xx_udc_ops = {

Quoted 8 lines
        .get_frame      = pxa2xx_udc_get_frame,
        .wakeup         = pxa2xx_udc_wakeup,
        .vbus_session   = pxa2xx_udc_vbus_session,
        .pullup         = pxa2xx_udc_pullup,
        // .vbus_draw ... boards may consume current from VBUS, up to
        // 100-500mA based on config.  the 500uA suspend ceiling means
        // that exclusively vbus-powered PXA designs violate USB specs.

That's basically a "plug in implementation here". Nobody's yet done that on a platform that _uses_ the VBUS power.

Quoted 5 lines

};

Comparing to omap_udc.

Quoted 8 lines

--- drivers/usb/gadget/omap_udc.c
static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)

{
        struct omap_udc *udc;

        udc = container_of(gadget, struct omap_udc, gadget);
        if (udc->transceiver)
                return otg_set_power(udc->transceiver, mA);

Where the transceiver would then delegate to something else, like the tps65010 driver.

Quoted 9 lines
        return -EOPNOTSUPP;
}
[...]
static struct usb_gadget_ops omap_gadget_ops = {
        .get_frame              = omap_get_frame,
        .wakeup                 = omap_wakeup,
        .set_selfpowered        = omap_set_selfpowered,
        .vbus_session           = omap_vbus_session,
        .vbus_draw              = omap_vbus_draw,

.... which has most certainly been on platforms which are hooked up to draw power from VBUS.

Quoted 10 lines

.pullup = omap_pullup,
};

Regarding API. If you all you want is to know how much power you need to ask from VBUS, we can extend external power interface... thus suppliers could ask their power consumption requirements in mA/uA, and these requests will be forwarded to power supply driver, and power driver will forward that request to USB transceiver (via platform hook).

I don't folow what you're saying. The control flow *MUST* be that the USB stack provides the only indication of how much power may be drawn through the VBUS supply. Nothing else in the system has the knowledge of what's legal, and when.

Quoted 8 lines

If you want to talk about a "supplier", the way to put it might then be that the USB stack is saying "here's N mA power for you"; it's supplying the power, not the other way around.

There's no "ask" involved, since the host controls "N". So the host supplies, the USB gadget stack interprets that, and some power component must obeys. That includes rules like reducing VBUS draw to ~500 uA when the host suspends the USB link.

This framework (pda_power) support ability to charge from USB. But it is only dispatcher, which implements logic. Issues like what exact mA to draw, and how all that communicate to USB stack, left to specific devices and/or additional drivers, users of framework.

In this regard, "USB" is just a label. Can be replaced with "alternative power source".

Quoted 1 line

- Dave

Signature · 4 lines
Anton Vorontsov
email: [email protected]
backup email: [email protected]
irc://irc.freenode.org/bd2
#6 lore raw
In reply to David Brownell
Quoted 6 lines · David Brownell
Quoted 4 lines

This driver used to stop code/logic duplication through different machines we porting at handhelds.org. pda_power register machs' power supplies, and will take care about notifying batteries about power changes through external power interface.

It gets USB power management wrong though. ...

And that seems not to have been resolved in the version you reposted and then put into your GIT tree, either ... what's your plan for addressing the feedback, then?

Quoted 10 lines

Key points:

- The API needs to say *how much power* can be drawn. ...

- Sensing VBUS power is not the same thing as being allowed to consume it. Again, USB OTG devices are different...

In general, no component other than the USB peripheral (or host) controller driver has any business trying to control how VBUS power is used. It's likely to get it wrong ... as shown by this patch. ;)

I see on other LKML threads that folk are spreading misinformation about USB, like "no control signaling over USB for power control". I sincerly hope you're not believing that's correct!

Plus, I'm wondering why this doesn't try to leverage the voltage framework that's been posted (to the ARM list, most recently).

At some level there's not a lot different between saying "turn on the 3V3 supply to this MMC card slot, budgeting 80mA" and saying instead "let the system draw up to 200 mA from USB VBUS". The current goes in different directions in those examples, sure. Plus the usage of VBUS as a power _input_ is of course board-specific:
battery charging, or powering specific circuits (like USB), etc.

Then there are devices which _output_ power on VBUS of course ... so the only difference between that and the MMC example being how much of the power budget is consumed. Plus devices which either use VBUS as an input or an output, based on how they're hooked up.

All of those are differences that seem to fit better into the notion of a general purpose voltage framework than this "power" thing you've provided. (Batter management being a separate issue.)

Also that whole notion that there's only a handful of power "supplies" to manage ("ac", "main-battery", "usb") just flies in the face of how most boards actually work -- multiple voltage rails and switches, more types than battery/UPS/"AC"/USB, etc -- and the mechanisms needed to manage power when it's critical to eke out every last milliWatt.

- Dave

#7 lore raw
Quoted 11 lines · David Brownell
Quoted 6 lines · David Brownell
Quoted 4 lines

This driver used to stop code/logic duplication through different machines we porting at handhelds.org. pda_power register machs' power supplies, and will take care about notifying batteries about power changes through external power interface.

It gets USB power management wrong though. ...

And that seems not to have been resolved in the version you reposted and then put into your GIT tree, either ... what's your plan for addressing the feedback, then?

pda_power have nothing to do with USB at all, as I've said. It *monitors* MAINS/USB power supply state through *platform hooks*, and platform hooks will do things as caring how much current to draw, if charger used.

There is nothing wrong with pda_power. If you're hinting that for pda_power we can use not some platform hooks, but some generic USB/UDC/OTG/whatever api - okay, patches are welcome. I'm not usb expert.

Please, speak code, not USB words. I don't understand USB language. All I've understood, that we could not get as much current as we want/expect from USB. And I believe you.

Quoted 7 lines
Quoted 7 lines

Key points:

- The API needs to say *how much power* can be drawn. ...

- Sensing VBUS power is not the same thing as being allowed to consume it. Again, USB OTG devices are different...

Quoted 7 lines
Quoted 3 lines

In general, no component other than the USB peripheral (or host) controller driver has any business trying to control how VBUS power is used. It's likely to get it wrong ... as shown by this patch. ;)

I see on other LKML threads that folk are spreading misinformation about USB, like "no control signaling over USB for power control". I sincerly hope you're not believing that's correct!

And again, I believe you. But you're not helping.

Quoted 2 lines

Plus, I'm wondering why this doesn't try to leverage the voltage framework that's been posted (to the ARM list, most recently).

(1)

Quoted 7 lines

At some level there's not a lot different between saying "turn on the 3V3 supply to this MMC card slot, budgeting 80mA" and saying instead "let the system draw up to 200 mA from USB VBUS". The current goes in different directions in those examples, sure. Plus the usage of VBUS as a power _input_ is of course board-specific:
battery charging, or powering specific circuits (like USB), etc.

Quoted 8 lines

Then there are devices which _output_ power on VBUS of course ... so the only difference between that and the MMC example being how much of the power budget is consumed. Plus devices which either use VBUS as an input or an output, based on how they're hooked up.

All of those are differences that seem to fit better into the notion of a general purpose voltage framework than this "power" thing you've provided. (Batter management being a separate issue.)

^^^ This

Quoted 5 lines

Also that whole notion that there's only a handful of power "supplies" to manage ("ac", "main-battery", "usb") just flies in the face of how most boards actually work -- multiple voltage rails and switches, more types than battery/UPS/"AC"/USB, etc -- and the mechanisms needed to manage power when it's critical to eke out every last milliWatt.

And this ^^^ shows that you've completely misunderstood whole power supply (was battery) class. This is mostly monitoring class, think of hwmon. This class *monitors* what power supplies *attached* to the board, monitors state of the power. Drivers to this class is mostly *passive*.

pda_power is "active" in sense that it controls charger (usually through one GPIO pin) via platform code. Not more.

And if some driver will want to be "active" (i.e. battery driver can can control its voltage/current output), patches are welcome to do (1), i.e. registering "voltage provider" inside power supply class.

Quoted 1 line

- Dave

Thanks,

Signature · 4 lines
Anton Vorontsov
email: [email protected]
backup email: [email protected]
irc://irc.freenode.org/bd2