home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / laptop-mode-tools / modules / usb-autosuspend < prev    next >
Encoding:
Text File  |  2012-05-20  |  5.4 KB  |  173 lines

  1. #! /bin/sh
  2. # Laptop mode tools module: usb-autosuspend.
  3. #
  4.  
  5. # Check whether a USB device is listed by ID
  6. listed_by_id() {
  7.     device=$1
  8.     list=$2
  9.     for usbid in $list; do
  10.         if ! echo $usbid | grep -q ':'; then
  11.             log "MSG" "WARNING: Invalid entry \"$usbid\" in \"$2\"."
  12.         fi
  13.         vendor=$(echo $usbid | cut -d: -f1)
  14.         product=$(echo $usbid | cut -d: -f2)
  15.         grep -qi $vendor $device/idVendor 2>/dev/null\
  16.          && grep -qi $product $device/idProduct 2>/dev/null\
  17.          && return 0
  18.     done
  19.     return 1
  20. }
  21.  
  22. # Check whether the USB driver type is blacklisted
  23. listed_by_type() {
  24.     device=$1
  25.     device_base=`basename $device`
  26.     list=$2
  27.     for driver_type in $list; do
  28.         if grep -q DRIVER=$driver_type $device/uevent; then
  29.             return 0
  30.         fi
  31.         # Check child devices as well.  The actual driver type is
  32.         # listed in a child device, not the top-level device.
  33.         for subdevice in $device/$device_base*; do
  34.             if [ -f $subdevice/uevent ]; then
  35.                 if grep -q DRIVER=$driver_type\
  36.                     $subdevice/uevent; then
  37.                     return 0
  38.                 fi
  39.             fi
  40.         done
  41.     done
  42.     return 1
  43. }
  44.  
  45. # Checks whether a device is blacklisted by either ID or driver type
  46. blacklisted() {
  47.     listed_by_id $1 "$AUTOSUSPEND_USBID_BLACKLIST"\
  48.     || listed_by_type $1 "$AUTOSUSPEND_USBTYPE_BLACKLIST"\
  49.     || return 1
  50.     return 0
  51. }
  52.  
  53. # Checks whether a device is whitelisted by either ID or driver type
  54. whitelisted() {
  55.     listed_by_id $1 "$AUTOSUSPEND_USBID_WHITELIST"\
  56.     || listed_by_type $1 "$AUTOSUSPEND_USBTYPE_WHITELIST"\
  57.     || return 1
  58.     return 0
  59. }
  60.  
  61. if [ x$CONTROL_USB_AUTOSUSPEND = x1 ] || [ x$ENABLE_AUTO_MODULES = x1 -a x$CONTROL_USB_AUTOSUSPEND = xauto ]; then
  62.     if [ $ON_AC -eq 1 ]; then
  63.         if [ "$ACTIVATE" -eq 1 ]; then
  64.             SUSPEND_USB_DEVICES="$LM_AC_SUSPEND_USB"
  65.         else
  66.             SUSPEND_USB_DEVICES="$NOLM_AC_SUSPEND_USB"
  67.         fi
  68.     else
  69.         SUSPEND_USB_DEVICES="$BATT_SUSPEND_USB"
  70.     fi
  71.  
  72.     if [ -d /sys/bus/usb/devices ]; then
  73.     if [ "$DEVICES" != "" ]; then
  74.         # If a list of devices has been provided, operate on only the
  75.         # listed USB devices.
  76.         DEVICE_LIST=""
  77.         for DEVICE in $DEVICES; do
  78.             DEVICE_LIST="$DEVICE_LIST $DEVICE"
  79.         done
  80.     else
  81.         # If no list was provided, operate on all USB devices
  82.         DEVICE_LIST=/sys/bus/usb/devices/*
  83.     fi
  84.     else
  85.     # This will rarely happen.
  86.     log "VERBOSE" "There are no USB devices."
  87.     fi
  88.  
  89.     if [ x$SUSPEND_USB_DEVICES = x1 ]; then
  90.       if [ -f /sys/module/usbcore/parameters/autosuspend ]; then
  91.           echo $AUTOSUSPEND_TIMEOUT > /sys/module/usbcore/parameters/autosuspend
  92.           log "VERBOSE" "Enabling autosuspend mode for USBCORE Controller, with timeout $AUTOSUSPEND_TIMEOUT."
  93.       else
  94.           log "VERBOSE" "Not enabling autosuspend mode for USBCORE Controller. Not Supported"
  95.       fi
  96.  
  97.       if [ "$DEVICE_LIST" != "" ]; then
  98.         for usb_device in $DEVICE_LIST; do
  99.           usb_device=`basename $usb_device`;
  100.  
  101.           USE_DEVICE=0
  102.           if [ x$AUTOSUSPEND_USE_WHITELIST = x1 ]; then
  103.             if whitelisted /sys/bus/usb/devices/$usb_device; then
  104.               USE_DEVICE=1
  105.             else
  106.               log "VERBOSE" "Device $usb_device not whitelisted, skipping auto suspend."
  107.             fi
  108.           else
  109.             if ! blacklisted /sys/bus/usb/devices/$usb_device; then
  110.               USE_DEVICE=1
  111.             else
  112.               logger "Device $usb_device is blacklisted, skipping auto suspend."
  113.             fi
  114.           fi
  115.  
  116.           if [ x$USE_DEVICE = x1 ]; then
  117.               if [ -f /sys/bus/usb/devices/$usb_device/power/autosuspend ]; then
  118.                   echo $AUTOSUSPEND_TIMEOUT > /sys/bus/usb/devices/$usb_device/power/autosuspend;
  119.                   log "VERBOSE" "Enabling auto suspend mode for usb device $usb_device."
  120.               else
  121.                   log "VERBOSE" "Not enabling auto suspend mode for usb device $usb_device"
  122.               fi
  123.  
  124.               if [ -f /sys/bus/usb/devices/$usb_device/power/control ]; then
  125.                   echo "auto" > /sys/bus/usb/devices/$usb_device/power/control;
  126.                   log "VERBOSE" "Enabling auto power level for usb device $usb_device."
  127.               elif [ -f /sys/bus/usb/devices/$usb_device/power/level ]; then
  128.                   echo "auto" > /sys/bus/usb/devices/$usb_device/power/level;
  129.                   log "VERBOSE" "Enabling auto power level for usb device $usb_device."
  130.               else
  131.                   log "VERBOSE" "Not enabling auto power level for usb device $usb_device"
  132.               fi
  133.           fi
  134.         done
  135.       fi
  136.     else
  137.       AUTOSUSPEND_TIMEOUT=0
  138.       if [ -f /sys/module/usbcore/parameters/autosuspend ]; then
  139.           echo $AUTOSUSPEND_TIMEOUT > /sys/module/usbcore/parameters/autosuspend
  140.           log "VERBOSE" "Disabling autosuspend mode for USBCORE Controller, with timeout $AUTOSUSPEND_TIMEOUT."
  141.       else
  142.           log "VERBOSE" "Not disabling autosuspend mode for USBCORE Controller. Not Supported"
  143.       fi
  144.  
  145.       if [ "$DEVICE_LIST" != "" ]; then
  146.           for usb_device in $DEVICE_LIST;
  147.           do
  148.           usb_device=`basename $usb_device`;
  149.           if [ -f /sys/bus/usb/devices/$usb_device/power/autosuspend ]; then
  150.               echo $AUTOSUSPEND_TIMEOUT > /sys/bus/usb/devices/$usb_device/power/autosuspend;
  151.               log "VERBOSE" "Disabling auto suspend mode for usb device $usb_device."
  152.           else
  153.               log "VERBOSE" "Not disabling auto suspend mode for usb device $usb_device"
  154.           fi
  155.  
  156.           if [ -f /sys/bus/usb/devices/$usb_device/power/control ]; then
  157.               echo "on" > /sys/bus/usb/devices/$usb_device/power/control;
  158.               log "VERBOSE" "Enabling ON power level for usb device $usb_device."
  159.           elif [ -f /sys/bus/usb/devices/$usb_device/power/level ]; then
  160.               echo "on" > /sys/bus/usb/devices/$usb_device/power/level;
  161.               log "VERBOSE" "Enabling ON power level for usb device $usb_device."
  162.           else
  163.               log "VERBOSE" "Not enabling ON power level for usb device $usb_device"
  164.           fi
  165.           done
  166.       fi
  167.     fi
  168. else
  169.     log "VERBOSE" "USB autosuspend is disabled."
  170. fi
  171.  
  172.