-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_usb_priority.sh
executable file
·221 lines (183 loc) · 4.89 KB
/
set_usb_priority.sh
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
#!/bin/sh
# ***************************************************************************************
# set_usb_priority.sh
# ***************************************************************************************
DisplayHelp()
{
echo ""
echo "NAME"
echo " set_usb_priority.sh - Manages udev rules independently of the udev version."
echo ""
echo "SYNOPSIS"
echo " set_udev_rules.sh [ --add=<vendor-id> ][ --remove=<vendor-id> ][ --removeall ][ --help ]"
echo ""
echo "DESCRIPTION"
echo " This script is used to create and remove udev rules from the system."
echo " To allow access to USB3 Vision devices, you must add a rule based on"
echo " the device's vendor ID."
echo ""
}
IsValidVendorID()
{
if echo "$1" | grep -qE ^[0-9A-Fa-f]{4}$; then
return 1
else
return 0
fi
}
MakeRuleName()
{
local FILE
FILE=`echo $1 | awk '{print tolower($0)}'`
eval "$2='/etc/udev/rules.d/80-drivers-HikSDK-$FILE.rules'"
}
CreateRuleFile()
{
# Depending on the version of udev, we need to generate different type of files for the rules @#$@#$@
VERSION_UDEV=`udevadm --version`
if [ $VERSION_UDEV -gt 147 ]; then
> $FILENAME
cat > $1 <<__END__
ACTION=="add", SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="$2", MODE="0666", GROUP="plugdev"
__END__
else
> $FILENAME
cat > $1 <<__END__
SUBSYSTEM=="usb", SYSFS{idVendor}=="$2", MODE="0666"
__END__
fi
}
Add()
{
local FILENAME
if [ ! -d "/etc/udev/rules.d" ]; then
echo "Unable to find the folder that contains the rules (/etc/udev/rules.d)."
exit 3
fi
if ! which udevadm >/dev/null 2>&1; then
echo "Unable to find the udevadm tool to retrieve the current version of udev that is"
echo "installed on this distribution."
echo "You must add the rule manually."
exit 3
fi
MakeRuleName "$1" FILENAME
echo "Adding rules for vendor ID $1."
# Delete any existing rules with this name first
rm -f $FILENAME
# Create the new rule file
CreateRuleFile "$FILENAME" "$1"
# Set the permission to the files
chmod u=rw,g=r,o=r $FILENAME
# Reload the rules for the usb subsystem
udevadm trigger --action=add --subsystem-match=usb --attr-match idVendor="$1"
echo "The $FILENAME rule has been created."
}
Remove()
{
local FILENAME
if [ -d "/etc/udev/rules.d" ]; then
MakeRuleName "$1" FILENAME
echo "Removing the rules for vendor ID $1."
rm -f $FILENAME
if which udevadm >/dev/null 2>&1; then
udevadm trigger --action=change --subsystem-match=usb --attr-match idVendor="$1"
fi
echo "The rules for vendor ID $1 have been removed."
fi
}
RemoveAll()
{
local FILENAME
if [ -d "/etc/udev/rules.d" ]; then
echo "Removing all rules."
MakeRuleName "????" FILENAME
rm -f $FILENAME
# Recreate the pleora rule because it should be permanent
MakeRuleName "28b7" FILENAME
CreateRuleFile "$FILENAME" "28b7"
if which udevadm >/dev/null 2>&1; then
udevadm trigger --action=change --subsystem-match=usb
fi
echo "All rules have been removed."
fi
}
VENDORID=""
# Parse the input arguments
for i in $*
do
case $i in
--add_pleora)
VENDORID=28b7
ACTION="add"
;;
--add=0x*)
VENDORID=0x${i#*=}
ACTION="add"
;;
--add=0X*)
VENDORID=0X${i#*=}
ACTION="add"
;;
--add=*)
VENDORID=${i#*=}
ACTION="add"
;;
--add)
VENDORID=""
ACTION="add"
;;
--remove_pleora)
VENDORID=28b7
ACTION="remove"
;;
--remove=0x*)
VENDORID=0x${i#*=}
ACTION="remove"
;;
--remove=0X*)
VENDORID=0X${i#*=}
ACTION="remove"
;;
--remove=*)
VENDORID=${i#*=}
ACTION="remove"
;;
--remove)
VENDORID=""
ACTION="remove"
;;
--removeall)
VENDORID=""
ACTION="removeall"
;;
--help)
DisplayHelp
exit 0
;;
*)
# unknown option
DisplayHelp
exit 1
;;
esac
done
# Default variables
USER_ID=`id -u`
# Check required priviledge
if [ "$USER_ID" != "0" ]; then
echo "Can only run this script as superuser (root account)."
exit 1
fi
ACTION="add"
VENDORID="2bdf"
IsValidVendorID "$VENDORID"
# Now finally, we can process the information...
echo ""
if [ "$ACTION" = "add" ]; then
Add $VENDORID
elif [ "$ACTION" = "remove" ]; then
Remove $VENDORID
elif [ "$ACTION" = "removeall" ]; then
RemoveAll
fi
echo ""