/lib/udev/tlp-usb-udev is in tlp 1.1-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | #!/bin/sh
# tlp - handle added usb devices
#
# Copyright (c) 2018 Thomas Koch <linrunner at gmx.net>
# This software is licensed under the GPL v2 or later.
# Remark: the calling udev rule is triggered for "base" devices only,
# not for the corresponding subdevices.
# --- Constants
readonly LOGGER=logger
readonly USBD=/sys/bus/usb/devices
readonly USB_TIMEOUT=2
readonly USB_TIMEOUT_MS=2000
readonly USB_WWAN_VENDORS="0bdb 05c6 1199"
readonly RUNDIR=/run/tlp
readonly USB_DONE=usb_done
readonly CONFFILE=/etc/default/tlp
# --- Subroutines
wordinlist () { # test if word in list
# $1: word, $2: whitespace-separated list of words
local word
if [ -n "${1-}" ]; then
for word in ${2-}; do
[ "${word}" != "${1}" ] || return 0 # exact match
done
fi
return 1 # no match
}
echo_debug () { # $1: tag; $2: msg; echo debug msg if tag matches
if wordinlist "$1" "$TLP_DEBUG"; then
$LOGGER -p debug -t "tlp[$$,$PPID]" "$2"
fi
}
# --- MAIN
# read config
[ -f $CONFFILE ] || exit 0
. $CONFFILE
# exit if TLP or autosuspend disabled
[ "$TLP_ENABLE" = "1" ] && [ "$USB_AUTOSUSPEND" = "1" ] || exit 0
# USB autosuspend has two principal operation modes:
#
# Mode 1 (optional):
# - System startup is handled by tlp-functions:set_usb_suspend()
# - Startup completion is signaled by "flag file" $USB_DONE
# - Newly added devices are handled by this udev script
# - Mode 1 is enabled by the private config variable X_TLP_USB_MODE=1
#
# Mode 2 (default):
# - Everything - including system startup, but not shutdown - is handled by this udev script
# do exit if mode 1 and no startup completion flag
[ "$X_TLP_USB_MODE" = "1" ] && [ ! -f $RUNDIR/$USB_DONE ] && exit 0
# get args
usbdev=/sys$1
# handle device
if [ -f $usbdev/power/autosuspend ] || [ -f $usbdev/power/autosuspend_delay_ms ]; then
# device is autosuspendable
# apply autosuspend
ctrlf="control"
autof="autosuspend_delay_ms"
vendor="$(cat $usbdev/idVendor 2> /dev/null)"
usbid="$vendor:$(cat $usbdev/idProduct 2> /dev/null)"
busdev="Bus $(cat $usbdev/busnum) Dev $(cat $usbdev/devnum 2> /dev/null)"
dclass="$(cat $usbdev/bDeviceClass 2> /dev/null)"
control="auto"
exc=""
chg=0
drvlist=""
# trace only: get drivers for all subdevices
if [ "$X_USB_DRIVER_TRACE" = "1" ]; then
drvlist=$(for dl in $usbdev/*:*/driver; do readlink $dl | \
sed -r 's/.+\///'; done | sort -u | tr '\n' ' ')
drvlist="(${drvlist% })"
fi
if wordinlist "$usbid" "$USB_WHITELIST"; then
# device is in whitelist -- whitelist always wins
control="auto"
exc="_dev_white"
elif wordinlist "$usbid" "$USB_BLACKLIST"; then
# device is in blacklist
control="on"
exc="_dev_black"
else
# wait for subdevices to populate
sleep 0.5
# check for hid subdevices
for subdev in $usbdev/*:*; do
if [ "$(cat $subdev/bInterfaceClass 2> /dev/null)" = "03" ]; then
control="on"
exc="_hid_black"
break
fi
done
if [ -z "$exc" ]; then
# check for bluetooth devices
USB_BLACKLIST_BTUSB=${USB_BLACKLIST_BTUSB:-0} # default is include
if [ "$USB_BLACKLIST_BTUSB" = "1" ] \
&& [ "$dclass" = "e0" ] \
&& [ "$(cat $usbdev/bDeviceSubClass 2> /dev/null)" = "01" ] \
&& [ "$(cat $usbdev/bDeviceProtocol 2> /dev/null)" = "01" ]; then
control="on"
exc="_btusb_black"
fi
fi # bluetooth
if [ -z "$exc" ]; then
# check for phone devices
USB_BLACKLIST_PHONE=${USB_BLACKLIST_PHONE:-0} # default is include
if [ "$USB_BLACKLIST_PHONE" = "1" ]; then
if [ "$vendor" = "0fca" ]; then
# RIM
if [ "$dclass" = "ef" ]; then
# RIM / BlackBerry
control="on"
exc="_phone_black"
elif [ "$dclass" = "00" ]; then
for subdev in $usbdev/*:*; do
if [ -d $subdev ]; then
if [ "$(cat $subdev/interface 2> /dev/null)" = "BlackBerry" ]; then
# Blackberry
control="on"
exc="_phone_black"
break
fi
fi
done
fi
elif [ "$vendor" = "045e" ] && [ "$dclass" = "ef" ]; then
# Windows Phone
control="on"
exc="_phone_black"
elif [ "$vendor" = "05ac" ] && [ "$(cat $usbdev/product 2> /dev/null)" = "iPhone" ]; then
# iPhone
control="on"
exc="_phone_black"
elif [ "$dclass" = "00" ]; then
# class defined at interface level, iterate subdevices
for subdev in $usbdev/*:*; do
if [ -d $subdev ]; then
if [ "$(cat $subdev/interface 2> /dev/null)" = "MTP" ]; then
# MTP: mostly Android
control="on"
exc="_phone_black"
break
elif [ "$(cat $subdev/bInterfaceClass 2> /dev/null)" = "ff" ] \
&& [ "$(cat $subdev/bInterfaceSubClass 2> /dev/null)" = "42" ] \
&& [ "$(cat $subdev/bInterfaceProtocol 2> /dev/null)" = "01" ]; then
# ADB: Android
control="on"
exc="_phone_black"
break
elif [ "$(cat $subdev/bInterfaceClass 2> /dev/null)" = "06" ] \
&& [ "$(cat $subdev/bInterfaceSubClass 2> /dev/null)" = "01" ] \
&& [ "$(cat $subdev/bInterfaceProtocol 2> /dev/null)" = "01" ]; then
# PTP: iPhone, Lumia et al.
# caveat: may also be a camera
control="on"
exc="_phone_black"
break
fi
fi
done
fi # dclass 00
fi # blacklist phone
fi # phone
if [ -z "$exc" ]; then
# check for printers
USB_BLACKLIST_PRINTER=${USB_BLACKLIST_PRINTER:-1} # default is exclude
if [ "$USB_BLACKLIST_PRINTER" = "1" ]; then
if [ "$dclass" = "00" ]; then
# check for printer subdevices
for subdev in $usbdev/*:*; do
if [ "$(cat $subdev/bInterfaceClass 2> /dev/null)" = "07" ]; then
control="on"
exc="_printer_black"
break
fi
done
fi
fi
fi # printer
if [ -z "$exc" ]; then
# check for wwan devices
USB_BLACKLIST_WWAN=${USB_BLACKLIST_WWAN:-1} # default is exclude
if [ "$USB_BLACKLIST_WWAN" = "1" ]; then
if [ "$dclass" != "00" ]; then
# check for cdc subdevices
for subdev in $usbdev/*:*; do
if [ "$(cat $subdev/bInterfaceClass 2> /dev/null)" = "0a" ]; then
control="on"
exc="_wwan_black"
break
fi
done
fi
if [ -z "$exc" ]; then
# check for vendors
if wordinlist "$vendor" "$USB_WWAN_VENDORS"; then
control="on"
exc="_wwan_black"
fi
fi
fi # blacklist wwan
fi # wwan
fi # !device blacklist
if [ -f $usbdev/power/control ]; then
if [ "$(cat $usbdev/power/control 2> /dev/null)" != "$control" ]; then
# Write actual changes only
{ printf '%s\n' "$control" > $usbdev/power/control; } 2> /dev/null
chg=1
fi
else
# level is deprecated
if [ "$(cat $usbdev/power/level 2> /dev/null)" != "$control" ]; then
# Write actual changes only
{ printf '%s\n' "$control" > $usbdev/power/level; } 2> /dev/null
chg=1
fi
ctrlf="level"
fi
if [ "$X_TLP_USB_SET_AUTOSUSPEND_DELAY" = "1" ]; then
# set autosuspend_delay
if [ -f $usbdev/power/autosuspend_delay_ms ]; then
{ printf '%s\n' $USB_TIMEOUT_MS > $usbdev/power/autosuspend_delay_ms; } 2> /dev/null
else
# autosuspend is deprecated
{ printf '%s\n' $USB_TIMEOUT > $usbdev/power/autosuspend; } 2> /dev/null
autof="autosuspend"
fi
echo_debug "usb" "udev_usb.$control$exc: $busdev ID $usbid $usbdev [$ctrlf $autof] $drvlist"
elif [ $chg -eq 1 ]; then
# default: change control but not autosuspend_delay, i.e. keep kernel default setting
echo_debug "usb" "udev_usb.$control$exc: $busdev ID $usbid $usbdev [$ctrlf] $drvlist"
else
# we didn't change anything actually
echo_debug "usb" "udev_usb.$control$exc: $busdev ID $usbid $usbdev $drvlist"
fi
fi
exit 0
|