# Controlling USB device power off/save at boot?



## JAW (Apr 7, 2017)

On the command line I can use `usbconfig` like so;


```
# usbconfig -d ugen0.3 power_off
# usbconfig -d ugen0.4 power_save
```

However I would like this to happen automatically when booting, I could only find device hints for loader.conf to disable the drivers, not set the usb power status?

Thanks,
James


----------



## leebrown66 (Apr 8, 2017)

You'll have to write your own rc(8) script for that.


> When a script is executed at boot time, it is passed the string
> ``start'' as its first and only argument.  At shutdown time, it is
> passed the string ``stop'' as its first and only argument.



You can follow /etc/rc.d/swap for an example.


----------



## Phishfry (Apr 8, 2017)

That is what I was thinking too.
https://www.freebsd.org/doc/en_US.ISO8859-1/articles/rc-scripting/rcng-dummy.html

I also wonder if devd could do this.
https://www.freebsd.org/cgi/man.cgi?devd(8)
https://www.freebsd.org/cgi/man.cgi?devd.conf(5)


----------



## leebrown66 (Apr 8, 2017)

Phishfry - devd is the better choice here, you are right.  A usb device can come and go after a boot.  

I've actually been playing with devd today to automatically attach an encrypted disk with geli, then mount that into my filesystem, then nullfs mount that into my bacula jail.


----------



## JAW (Apr 8, 2017)

Thanks for the suggestions. My idea of using `usbconfig` during boot was completely flawed due to the differing enumeration of usb devices depending on what is plugged in to the USB ports... doh!

My aim is to save some power on my laptop by disabling the devices I never use, such as the Bluetooth Hub (ugen0.3/6) and SD card reader (ugen0.8), seen below;


```
ugen0.2: <product 0x7811 vendor 0x7392> at usbus0, cfg=0 md=HOST spd=HIGH (480Mbps) pwr=ON (500mA)
ugen0.3: <BRCM20702 Hub Apple Inc.> at usbus0, cfg=0 md=HOST spd=FULL (12Mbps) pwr=SAVE (94mA)
ugen0.6: <Bluetooth USB Host Controller Apple Inc.> at usbus0, cfg=0 md=HOST spd=FULL (12Mbps) pwr=ON (0mA)
ugen0.7: <Apple Internal Keyboard  Trackpad Apple Inc.> at usbus0, cfg=0 md=HOST spd=FULL (12Mbps) pwr=ON (500mA)
ugen0.8: <Card Reader Apple> at usbus0, cfg=0 md=HOST spd=SUPER (5.0Gbps) pwr=ON (224mA)
```

I first tried disabling drivers with device.hints in /boot/loader.conf such as the `umass` driver, but then no usb flash storage will work at all! So perhaps the /etc/devd.conf approach targetting the specific vendor/product id's of the above devices is the way to go?

Many Thanks,
James


----------



## Phishfry (Apr 10, 2017)

JAW said:


> targetting the specific vendor/product id's of the above devices is the way to go?


That or drive serial number or some other unique identifier. devd would allow a finer grained approach.

I would be interested in what you come up with. I plan on using some Pi3's on batteries and this would be useful for power consumption.

I ran a check on a drive laying around and it looks like iSerialNumber might be something to check.

```
@DELL:~ # usbconfig -u 1 -a 5 dump_device_desc
ugen1.5: <Ultra USB 3.0 SanDisk> at usbus1, cfg=0 md=HOST spd=HIGH (480Mbps) pwr=ON (224mA)

  bLength = 0x0012
  bDescriptorType = 0x0001
  bcdUSB = 0x0210
  bDeviceClass = 0x0000  <Probed by interface class>
  bDeviceSubClass = 0x0000
  bDeviceProtocol = 0x0000
  bMaxPacketSize0 = 0x0040
  idVendor = 0x0781
  idProduct = 0x5591
  bcdDevice = 0x0100
  iManufacturer = 0x0001  <SanDisk>
  iProduct = 0x0002  <Ultra USB 3.0>
  iSerialNumber = 0x0003  <4C530001070110116052>
  bNumConfigurations = 0x0001
```


----------



## Phishfry (Apr 10, 2017)

That might not work. I am also finding "no string" on some devices iSerialNumber.

https://lists.freebsd.org/pipermail/freebsd-questions/2013-March/249831.html
https://lists.freebsd.org/pipermail/freebsd-usb/2014-August/013164.html
https://lists.freebsd.org/pipermail/freebsd-usb/2013-May/012073.html


----------

