Controlling power to a USB port.

Discussion in 'Mac Programming' started by richifie, Jul 24, 2010.

  1. richifie macrumors newbie

    Oct 4, 2009
    Does anyone know the xcode coding i would use to control power to a USB port. I found this on the internet but it doesn't work on script editor or xcode:

    USB.port(2).power = on;

    I just want a two value variable, on and off, but i could use a gradient aswell (power = 1-100), acctually come to think of it that would be better but either would be useful.

    Thank you in advance,

  2. larkost macrumors 6502a

    Oct 13, 2007
    I don't believe that this is doable as I don't believe that the USB device drivers expose any way of doing this. You would need to change the USB chipset drivers to provide for this, and that is not going to be a trivial process (I am not even sure that the source code for newer chipsets is available in the darwin project).
  3. frankpuccino macrumors newbie

    Jul 24, 2010
    larkost, I don't know what you mean by "USB chipset."

    On modern Macs, the USB functionality is handled by the Platform Controller Hub not by some chip dedicated to USB.

    As for the poster's original question, I have to confess I'm not sure how to disable power to a USB port.

  4. larkost macrumors 6502a

    Oct 13, 2007
    This is not uniform, and on at least one current Mac is controlled by a separate chipset. Hense my use of that word.

    Additionally, the drivers are separate, especially the ones you would need to work this sort of thing.
  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Where did you find that code?

    I found it in only one place (other than this thread):

    Unfortunately, it's just pseudo-code. There is no real code that does this. It's an idealized hypothetical representation.

    If there is any way to control USB-port power, it will be through IOKit. Google keywords: IOKit Reference. Personally, I doubt you'll find anything, because controlling the power is something I would expect the driver to do by itself, rather than exposing as a controllable function.

    Please explain exactly what you're trying to accomplish by this power control.
  6. frankpuccino macrumors newbie

    Jul 24, 2010
    Really? Which current Mac?

    I'm not saying that you're wrong, but I'd be really
    surprised if Apple engineers decided to get chips
    just for USB on any of their desktops or laptops.

    For devices that sit on the USB bus, like iSight,
    keyboard, etc. YES of course. But just for a USB
    port, I'd be surprised.

  7. richifie thread starter macrumors newbie

    Oct 4, 2009
    Thanks to all your replies.

    You are correct that is where i found the code and wish it worked.

    I wanted to control power because i wanted to create a lamp that you connected to computer via usb (which i can easily make) but you could turn it off using an mac app that i would create using the piece of code i hoped to find here.

    Can anyone suggest a method of getting what i desire?
  8. notjustjay macrumors 603


    Sep 19, 2003
    Canada, eh?
    Perhaps put the lamp on a solid-state relay, controlled by a microcontroller that listens for commands to be transmitted via USB?

    When I used to play with this sort of thing, I would connect things to the parallel port, and the software would drive certain data or signal pins high or low, feeding a latch or relay or whatever. You could also use the old-style serial ports, driving signals on pins like DTR.

    If you search for "USB relay" you will see numerous devices which could do what you want.
  9. balamw Moderator


    Staff Member

    Aug 16, 2005
    New England
    If you read the USB article at Wikipedia you'll get a flavor for how tricky powering devices using USB ports can be:

    The host doesn't decide how much power to give the device, the device has to be smart enough to request the power from the host, and then negotiate with it. So notjustjay's idea is right. Get a USB relay or other "smart enough" device (Arduino, PIC, ...) to control your power draw and your device.

    Unfortunately, this means this won't be a <$10 project.


Share This Page