Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

richifie

macrumors newbie
Original poster
Oct 4, 2009
11
0
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,

Richifie
 

larkost

macrumors 6502a
Oct 13, 2007
534
1
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).
 

frankpuccino

macrumors newbie
Jul 24, 2010
29
0
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).

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.

Frank
 

larkost

macrumors 6502a
Oct 13, 2007
534
1
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.

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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,755
8,445
A sea of green
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;

Where did you find that code?

I found it in only one place (other than this thread):
http://ask.metafilter.com/73936/Control-USB-Power-based-on-temperature-monitoring-in-Mac-OS-X

Unfortunately, it's just pseudo-code. There is no real code that does this. It's an idealized hypothetical representation.
http://en.wikipedia.org/wiki/Pseudocode

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.
 

frankpuccino

macrumors newbie
Jul 24, 2010
29
0
This is not uniform, and on at least one current Mac is controlled by a separate chipset. Hense my use of that word.

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.

Frank
 

richifie

macrumors newbie
Original poster
Oct 4, 2009
11
0
Where did you find that code?

I found it in only one place (other than this thread):
http://ask.metafilter.com/73936/Control-USB-Power-based-on-temperature-monitoring-in-Mac-OS-X

Unfortunately, it's just pseudo-code. There is no real code that does this. It's an idealized hypothetical representation.
http://en.wikipedia.org/wiki/Pseudocode

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.

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?
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
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.
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
If you read the USB article at Wikipedia http://en.wikipedia.org/wiki/Universal_Serial_Bus you'll get a flavor for how tricky powering devices using USB ports can be:

Some USB devices require more power than is permitted by the specifications for a single port. This is common for external hard and optical disc drives, and generally for devices with motors or lamps. Such devices can use an external power supply, which is allowed by the standard, or use a dual-input USB cable, one input of which is used for power and data transfer, the other solely for power, which makes the device a non-standard USB device. Some external hubs may, in practice, supply more power to USB devices than required by the specification but a standard-compliant device may not depend on this.

Some non-standard USB devices use the 5 V power supply without participating in a proper USB network which negotiates power draws with the host interface. These are usually referred to as USB decorations. The typical example is a USB-powered keyboard light; fans, mug coolers and heaters, battery chargers, miniature vacuum cleaners, and even miniature lava lamps are available. In most cases, these items contain no digital circuitry, and thus are not Standard compliant USB devices at all. This can theoretically cause problems with some computers;[specify] prior to the Battery Charging Specification, the USB specification required that devices connect in a low-power mode (100 mA maximum) and state how much current they need, before switching, with the host's permission, into high-power mode.

In addition to limiting the total average power used by the device, the USB specification limits the inrush current (i.e., that used to charge decoupling and filter capacitors) when the device is first connected. Otherwise, connecting a device could cause problems with the host's internal power. Also, USB devices are required to automatically enter ultra low-power suspend mode when the USB host is suspended. Nevertheless, many USB host interfaces do not cut off the power supply to USB devices when they are suspended since resuming from the suspended state would become a lot more complicated if they did.

There are also devices at the host end that do not support negotiation, such as battery packs that can power USB-powered devices; some provide power, while others pass through the data lines to a host PC. USB power adapters convert utility power and/or another power source (e.g., a car's electrical system) to run attached devices. Some of these devices can supply up to 1 A of current. Without negotiation, the powered USB device is unable to inquire if it is allowed to draw 100 mA, 500 mA, or 1 A.

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.

B
 

f54da

macrumors 6502
Dec 22, 2021
347
128
And moreover that solution is about controlling ports on a hub. I think it might be possible to control power to inbuilt usb-ports but it is probably all or nothing, and disabling it might take down some inbuilt devices as well (iirc on some older models trackpad/keyboard was also usb). The crudest way would be to find the right device in the iokit hierarchy and terminate it. But I can't see any use-case for which this is the best approach. If you want to toggle some device then do it via a dedicated device (e.g. some microcontroller or raspi) instead of taking offline your system's USB ports.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.