Here's what I did to figure this out. First, I wrote a script to compare the output from some system info commands:
#!/bin/sh
write_logs(){
mkdir "$1"
ls /dev/disk/by-uuid/ > "$1"/uuids
lsusb -v > "$1"/lsusb-v
lsusb -t > "$1"/lsusb-t
ls -alh /sys/bus/usb/devices/ | sed -E 's/.+ -> //' | tail -n+4 > "$1"/sysbus
}
echo "Insert plug and press ENTER."
read VAR; write_logs a
echo "Remove plug and press ENTER."
read VAR; write_logs b
diff -r a b
I didn't catch that the output was any different at first, because it takes my system a couple seconds to recognize a new USB device is attached. If you run this script, wait a few seconds before pressing ENTER. When I did this, I realized that the output for these commands really did change.
For my phone, I can detect if it is plugged in with this:
lsusb -v 2>/dev/null | grep 'Motorola PCS' >/dev/null \
&& echo phone is plugged in
I can detect if anything is plugged in to a particular port with this:
ls -alh /sys/bus/usb/devices/ | grep pci0000:00/0000:00:1d.7/usb2/2-6 >/dev/null \
&& echo Something is plugged into the left USB port.
I found these grep patterns just by comparing output from the above script; you may have to modify them for your own system or your own phone.
I'll still have to play around with how to activate my detection function and run it periodically to see if the device has been removed (probably using chron). Also, I haven't figured out how to detect how much power is being used; this merely detects if anything is connected or not.
lsusb -v
can get you the info.