daily pastebin goal
16%
SHARE
TWEET

danomac's usbwakeup script

danomac Aug 22nd, 2012 207 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # This script attempts to read /sys/devices/ and identify usb ports and devices
  4. # that can be toggled to enable wakeup from sleep.
  5. #
  6. # Version: 2.0
  7. # Date written: August 20, 2012
  8. #
  9. # Copyright (C) 2012  danomac @ gentoo forums
  10. # Gentoo forum thread: http://forums.gentoo.org/viewtopic-t-933934.html
  11. # The forum thread has instructions and examples on on how to use.
  12. #
  13. # This library is free software; you can redistribute it and/or
  14. # modify it under the terms of the GNU Lesser General Public
  15. # License as published by the Free Software Foundation; either
  16. # version 2.1 of the License, or (at your option) any later version.
  17. #
  18. # This library is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21. # Lesser General Public License for more details.
  22.  
  23. # Return code documentation
  24. #
  25. #  1: No parameter / unknown parameter specified
  26. #  2: Multiple actions requested, can only process one at a time
  27. #  3: Effective user ID not root (root is required)
  28. #
  29. #  7: No action requested
  30. #  8: Quiet and verbose requested at the same time! Use one or the other.
  31. #  9: Kernel Dmesg` option requested with action other than list
  32. #
  33. # Internal errors:
  34. #
  35. # 10: toggleall() - first parameter missing (internal error)
  36. # 11: toggleusbdevice() - first parameter not specified (internal error)
  37. # 12: toggleusbdevice() - second parameter not specified (internal error)
  38. # 13: toggleusbdevice() - third parameter needed but not specified! (internal error)
  39.  
  40.  
  41. function listusbwakeup () {
  42.         # If quiet option is set, notify user it's being ignored
  43.         if $QUIET; then echo "Quiet option set; ignoring..."; echo ""; fi
  44.  
  45.         # Initialize counter (to total items found later)
  46.         COUNT=0
  47.  
  48.         echo "Listing USB hubs/devices and their wakeup status..."
  49.  
  50.         # If verbose output is requested, switch to full detail view
  51.         if $VERBOSE
  52.         then
  53.                 echo "
  54. USB ID :: Device* :: Status
  55.     Device full path
  56.     Vendor :: Device Description
  57. ---------------------------------"
  58.         else
  59.                 echo "
  60. USB ID    :: Device* :: Status :: Device Description
  61. ----------------------------------------------------"
  62.         fi
  63.  
  64.         # Search for all wakeup files under /sys/devices
  65.         for i in `find /sys/devices -type f -name wakeup`;
  66.         do
  67.                 # Extract the directory name where wakeup file resides
  68.                 DEVDIRNAME=`dirname $i`
  69.  
  70.                 # Extract the actual device name (remove the power directory)
  71.                 DEVNAME=`dirname $DEVDIRNAME`
  72.  
  73.                 # Now remove the directory path, leaving only the device's proper name (usb4, 4-1, etc)
  74.                 DEVPROPERNAME=`basename $DEVNAME`
  75.  
  76.                 # Check for a product name. If none is found we most likely aren't interested in it.
  77.                 if [ -e $DEVDIRNAME/../product ]
  78.                 then
  79.                         # We found one! Find other relevant information, if none found use a placeholder.
  80.                         if [ -e $DEVDIRNAME/../product ]; then PRODUCT=`cat $DEVDIRNAME/../product`; else PRODUCT="No product name"; fi
  81.                         if [ -e $DEVDIRNAME/../manufacturer ]; then VENDOR=`cat $DEVDIRNAME/../manufacturer`; else VENDOR="No vendor name"; fi
  82.                         if [ -e $DEVDIRNAME/../idVendor ]; then IDVENDOR=`cat $DEVDIRNAME/../idVendor`; else IDVENDOR="No VendorID"; fi
  83.                         if [ -e $DEVDIRNAME/../idProduct ]; then IDPRODUCT=`cat $DEVDIRNAME/../idProduct`; else IDPRODUCT="No Product ID"; fi
  84.  
  85.                         # If verbose output is selected, switch to full detail view
  86.                         if $VERBOSE
  87.                         then
  88.                                 echo "$IDVENDOR:$IDPRODUCT :: $DEVPROPERNAME :: `cat $i`"
  89.                                 echo "     $DEVNAME"
  90.                                 echo "     $VENDOR :: $PRODUCT"
  91.                                 echo ""
  92.                         else
  93.                                 echo "$IDVENDOR:$IDPRODUCT :: $DEVPROPERNAME :: `cat $i` :: $PRODUCT"
  94.                         fi
  95.  
  96.                         # Increment our totals counter
  97.                         COUNT=`expr $COUNT + 1`
  98.                 fi
  99.         done;
  100.  
  101.         # Need to format the output differently for normal mode by adding an extra blank line
  102.         if ! $VERBOSE; then echo ""; fi
  103.         echo "*Use the Device column to identify hubs/devices to be toggled."
  104.  
  105.         # If there's no results indiciate so
  106.         if [ $COUNT -eq 0 ]
  107.         then
  108.                 echo ""
  109.                 echo "  NO USB hubs/devices found, or none are toggle-able!"
  110.         else
  111.                 # Hey, we have results, show the total found
  112.                 echo ""
  113.                 echo "$COUNT USB hubs/devices listed."
  114.         fi
  115.  
  116.         # If the dmesg hints were requested, show them
  117.         if $KERNELDMESG
  118.         then
  119.                 echo "
  120. Hints from dmesg (use this to try to identify your keyboard and mouse, you
  121. may need to reboot to see anything relevant, see help for more info):
  122. "
  123.  
  124.                 dmesg | grep ^input
  125.         fi
  126. }
  127.  
  128. function toggleusbdevice () {
  129.         # toggleusbdevice (enabledevice:boolean, enableall:boolean, devname:string)
  130.         # First parameter is boolean. True to enable device; False to
  131.         # disable device.
  132.         # Second parameter is boolean. True to enable all relevant devices found,
  133.         # false to enable one device, specified in parameter 3.
  134.         # Third parameter is the name of the device from the Device column of list
  135.         # output
  136.  
  137.         # Make sure both parameters are passed to this function!
  138.         if [ -z "$1" ]; then exit 11; fi
  139.         if [ -z "$2" ]; then exit 12; fi
  140.  
  141.         # If specifying a single device to be changed, make sure parameter 3
  142.         # exists!
  143.         if ! $2
  144.         then
  145.                 if [ -z "$3" ]; then exit 13; fi
  146.         fi
  147.  
  148.         # Verbose detail is used throughout this function. Echo extra detail if
  149.         # requested
  150.         if $VERBOSE; then echo "Starting to parse wakeup devices found..."; fi
  151.  
  152.         # Search for all wakeup files under /sys/devices
  153.         for i in `find /sys/devices -type f -name wakeup`;
  154.         do
  155.                 # Extract the directory name where wakeup file resides
  156.                 DEVDIRNAME=`dirname $i`
  157.  
  158.                 # Extract the actual device name (remove the power directory)
  159.                 DEVNAME=`dirname $DEVDIRNAME`
  160.  
  161.                 # Now remove the directory path, leaving only the device's proper name (usb4, 4-1, etc)
  162.                 DEVPROPERNAME=`basename $DEVNAME`
  163.  
  164.                 # Are we updating one specific device or all of them?
  165.                 if ! $2
  166.                 then
  167.                         # Update individual device
  168.                         if $VERBOSE; then echo "Is this device ($DEVPROPERNAME) the same as the requested device ($3)?"; fi
  169.  
  170.                         # Check for a product name. If none is found we most likely aren't interested in it.
  171.                         if [ "$DEVPROPERNAME" == "$3" ]
  172.                         then
  173.                                 if $VERBOSE; then echo "YES! Attempting to change."; fi
  174.  
  175.                                 if $1
  176.                                 then
  177.                                         if $VERBOSE || ! $QUIET; then echo "Attempting to enable device ($DEVPROPERNAME)..."; fi
  178.                                         # Enable device
  179.                                         echo "enabled" > $i
  180.  
  181.                                         if $VERBOSE || ! $QUIET
  182.                                         then
  183.                                                 if [ $? -ne 0 ]; then echo "Failed."; else echo "Success!"; fi
  184.                                         fi
  185.  
  186.                                         exit 0
  187.                                 else
  188.                                         # Disable device
  189.                                         if $VERBOSE || ! $QUIET; then echo "Attempting to disable device ($DEVPROPERNAME)..."; fi
  190.                                         # Enable device
  191.                                         echo "disabled" > $i
  192.  
  193.                                         if $VERBOSE || ! $QUIET
  194.                                         then
  195.                                                 if [ $? -ne 0 ]; then echo "Failed."; else echo "Success!"; fi
  196.                                         fi
  197.  
  198.                                         exit 0
  199.                                 fi
  200.                         else
  201.                                 if $VERBOSE; then echo "...no."; fi
  202.                         fi
  203.                 else
  204.                         # Update all devices
  205.  
  206.                         if $VERBOSE; then echo "Does this device ($DEVPROPERNAME) look like a USB hub/device?"; fi
  207.  
  208.                         # Check for a product name. If none is found we most likely aren't interested in it.
  209.                         if [ -e $DEVDIRNAME/../product ]
  210.                         then
  211.                                 # If verbose output is selected, switch to full detail view
  212.                                 if $VERBOSE; then echo "YES!"; fi
  213.  
  214.                                 if $1
  215.                                 then
  216.                                         if $VERBOSE || ! $QUIET; then echo "Attempting to enable device ($DEVPROPERNAME)..."; fi
  217.                                         # Enable device
  218.                                         echo "enabled" > $i
  219.  
  220.                                         if $VERBOSE || ! $QUIET
  221.                                         then
  222.                                                 if [ $? -ne 0 ]; then echo "Failed."; else echo "Success!"; fi
  223.                                         fi
  224.                                 else
  225.                                         # Disable device
  226.                                         if $VERBOSE || ! $QUIET; then echo "Attempting to disable device ($DEVPROPERNAME)..."; fi
  227.  
  228.                                         # Enable device
  229.                                         echo "disabled" > $i
  230.  
  231.                                         if $VERBOSE || ! $QUIET
  232.                                         then
  233.                                                 if [ $? -ne 0 ]; then echo "Failed."; else echo "Success!"; fi
  234.                                         fi
  235.                                 fi
  236.                         else
  237.                                 if $VERBOSE; then echo "...no."; fi
  238.                         fi
  239.                 fi
  240.         done;
  241.  
  242.         # If updating only one device and we get this far it wasn't found.
  243.         if ! $2; then echo "Device not found."; fi
  244. }
  245.  
  246. function showhelp () {
  247.         # If quiet option is passed notify user it's being ignored
  248.         if $QUIET; then echo "Quiet option set; ignoring..."; fi
  249.  
  250.         echo "
  251. $0: a script that lists and toggles wakeup on usb hubs
  252.  
  253. Usage: $0 [options] [action] [parameters]
  254.  
  255. Actions:
  256.        -l : list available usb hubs/devices that can be toggled and their
  257.             current status (enabled/disabled)
  258.  
  259.        -a : enable wakeup on all discovered usb hubs/devices
  260.  
  261.             Use caution with this; it's better to identify individual
  262.             hubs/devices rather than enable them all. You might find
  263.             your computer woke itself unintentionally.
  264.  
  265.        -x : disable wakeup on all discovered usb hubs/devices
  266.  
  267.        -e : enable wakeup on specific hub/device (requires parameter,
  268.             see below.)
  269.  
  270.        -d : disable wakeup on specific hub/device (requires parameter,
  271.             see below.)
  272.  
  273.        -h : this screen
  274.  
  275. Options:
  276.        -q : Quiet mode for all but help and list actions
  277.  
  278.        -v : Enable verbose output
  279.  
  280.        -k : Try to show hints from dmesg (list action only)
  281.      
  282.             This dmesg option will show lines starting with input
  283.             in order to try to identify devices.
  284.  
  285.             It is usually presented as /sys/devices/*/usb?/*/input/input?.
  286.             If nothing useful is displayed, try rebooting as relevant
  287.             information can be out of the buffer if the PC has been on for
  288.             some time.
  289.  
  290. Parameters:
  291.        Both -e and -d actions require a parameter to search for, use -l to list.
  292.  
  293.        The parameter needed is in the Device column.
  294.  
  295.        Sample output of $0 -l:
  296.                Listing USB devices and their wakeup status...
  297.  
  298.                USB ID    :: Device* :: Status :: Device Description
  299.                ----------------------------------------------------
  300.                1d6b:0001 :: usb4 :: enabled :: UHCI Host Controller
  301.                046d:c508 :: 4-1 :: enabled :: USB Receiver
  302.                046d:c221 :: 4-2.1 :: enabled :: Gaming Keyboard
  303.  
  304.        The identifier needed for the parameter is usb4, 4-1, or 4-2.1.
  305.  
  306. No arguments will show this screen.
  307. "
  308. }
  309.  
  310. # Start of "main" routine
  311.  
  312. # Make sure user is root (script will not work as user)
  313. if [ $EUID -ne 0 ]; then echo "This script must be run as root."; exit 3; fi
  314.  
  315. # Initialize variables:
  316. #     QUIET=quiet mode (no echo) - default false. Toggled with -q option.
  317. #     KERNELDMESG=dmesg list option in list mode. Default false, toggled with
  318. #          -k option
  319. #     ACTION=default 0; used to make sure more than one action are not
  320. #          specified at the same time.
  321. #     VERBOSE=default false. Extra detail in some actions.
  322. QUIET=false
  323. KERNELDMESG=false
  324. VERBOSE=false
  325. ACTION=0
  326.  
  327. # Show help if no arguments provided at all
  328. if  [ $# -eq 0 ]; then showhelp; exit 1; fi
  329.  
  330. # Process arguments passed, setting environment appropriately.
  331. while getopts “hkqvlaxe:d:” OPTION
  332. do
  333.         case $OPTION in
  334.                 h)      # Help action
  335.                         # Make sure multiple actions are not chosen.
  336.                         if [ $ACTION -ne 0 ]; then echo "You can only declare one action at a time."; echo ""; showhelp; exit 2; fi
  337.  
  338.                         ACTION=1
  339.                         ;;
  340.                 q)      # Quiet option
  341.                         QUIET=true
  342.                         ;;
  343.                 k)      # Kernel dmesg in list mode option
  344.                         KERNELDMESG=true
  345.                         ;;
  346.                 v)      # Verbose option
  347.                         VERBOSE=true
  348.                         ;;
  349.                 l)      # List action
  350.                         # Make sure multiple actions are not chosen.
  351.                         if [ $ACTION -ne 0 ]; then echo "You can only declare one action at a time."; echo ""; showhelp; exit 2; fi
  352.  
  353.                         ACTION=2
  354.                         ;;
  355.                 a)      # Enable all USB hubs/devices action
  356.                         # Make sure multiple actions are not chosen.
  357.                         if [ $ACTION -ne 0 ]; then echo "You can only declare one action at a time."; echo ""; showhelp; exit 2; fi
  358.  
  359.                         ACTION=3
  360.                         ;;
  361.                 x)      # Disable all USB hubs/devices action
  362.                         # Make sure multiple actions are not chosen.
  363.                         if [ $ACTION -ne 0 ]; then echo "You can only declare one action at a time."; echo ""; showhelp; exit 2; fi
  364.  
  365.                         ACTION=4
  366.                         ;;
  367.                 e)      # Enable specific hub/device via find command action
  368.                         # Make sure multiple actions are not chosen.
  369.                         if [ $ACTION -ne 0 ]; then echo "You can only declare one action at a time."; echo ""; showhelp; exit 2; fi
  370.  
  371.                         ACTION=5
  372.  
  373.                         # Save arguments passed for later
  374.                         ENABLEUSB=$OPTARG
  375.  
  376.                         ;;
  377.                 d)      # Disable specific hub/device via find command action
  378.                         # Make sure multiple actions are not chosen.
  379.                         if [ $ACTION -ne 0 ]; then echo "You can only declare one action at a time."; echo ""; showhelp; exit 2; fi
  380.  
  381.                         ACTION=6
  382.  
  383.                         # Save arguments passed for later
  384.                         DISABLEUSB=$OPTARG
  385.                         ;;
  386.                 ?)      # Unknown parameter
  387.                         showhelp
  388.                         exit 1
  389.                         ;;
  390.         esac
  391. done
  392.  
  393. # Some sanity checks
  394. # Quiet and verbose can't be set at the same time
  395. if $QUIET && $VERBOSE; then echo "You can't specify quiet (-q) and verbose (-v) at the same time."; echo ""; exit 8; fi
  396.  
  397. # Kernel DMESG can only be used with the list action
  398. if $KERNELDMESG && (( $ACTION != 2 )); then echo "The hints from dmesg option (-k) can only be used with the list (-l) action."; echo ""; exit 9; fi
  399.  
  400. # Make sure an action was selected!
  401. if (($ACTION == 0 )); then echo "You haven't requested an action!"; exit 7; fi
  402.  
  403. # We seem to be safe, so actually perform the request
  404. case $ACTION in
  405.         1)      # help action
  406.                 showhelp
  407.                 exit
  408.                 ;;
  409.         2)      # list action
  410.                 listusbwakeup
  411.                 exit
  412.                 ;;
  413.         3)      # enable all devices
  414.                 toggleusbdevice true true
  415.                 exit
  416.                 ;;
  417.         4)      # disable all devices
  418.                 toggleusbdevice false true
  419.                 exit
  420.                 ;;
  421.         5)      # enable one device
  422.                 toggleusbdevice true false $ENABLEUSB
  423.                 exit
  424.                 ;;
  425.         6)      # disable one device
  426.                 toggleusbdevice false false $DISABLEUSB
  427.                 exit
  428.                 ;;
  429. esac
RAW Paste Data
Top