usb_modeswitch ver. 2.4.0 with data package 2016-06-12
[tomato.git] / release / src / router / usbmodeswitch / data / gen-rules.tcl
blob080900c4d69692d748154f0ac336ea11d75409a4
1 #!/usr/bin/env tclsh
3 # Usage: gen-rules.tcl [--set-version <date>]
5 # <date> should be in the form YYYYMMDD
7 # Config files are expected to be in subfolder "usb_modeswitch.d"
9 # A config file is expected to have one comment line containing
10 # a model name or other concise device specifications
13 # Default version string
14 set version "20160612"
16 # Devices excluded from Huawei catch-all rule
17 set x_huaweiList {12d1:1573}
19 if {[lindex $argv 0] == "--set-version" && [regexp {\d\d\d\d\d\d\d\d} [lindex $argv 1]]} {
20 set version [lindex $argv 1]
23 set template {ATTR{idVendor}=="+##+", ATTR{idProduct}=="#++#", RUN+="usb_modeswitch '%b/%k'"}
25 if {![file isdirectory usb_modeswitch.d]} {
26 puts "No \"usb_modeswitch.d\" subfolder found"
27 exit
30 set filelist [glob -nocomplain ./usb_modeswitch.d/*]
31 if {[llength $filelist] == 0} {
32 puts "The \"usb_modeswitch.d\" subfolder is empty"
33 exit
36 set wc [open "40-usb_modeswitch.rules" w]
38 # Writing file header with given version
40 puts -nonewline $wc {# Part of usb-modeswitch-data, version }
41 puts $wc $version
42 puts $wc {#
43 # Works with usb_modeswitch versions >= 2.2.2 (extension of PantechMode)
45 ACTION!="add|change", GOTO="modeswitch_rules_end"
47 # Adds a symlink "gsmmodem[n]" to the lowest ttyUSB port with interrupt
48 # transfer; checked against a list of known modems, or else no action
49 KERNEL=="ttyUSB*", ATTRS{bNumConfigurations}=="*", PROGRAM="usb_modeswitch --symlink-name %p %s{idVendor} %s{idProduct} %E{PRODUCT}", SYMLINK+="%c"
51 SUBSYSTEM!="usb", GOTO="modeswitch_rules_end"
53 # Adds the device ID to the "option" driver after a warm boot
54 # in cases when the device is yet unknown to the driver; checked
55 # against a list of known modems, or else no action
56 ATTR{bInterfaceClass}=="ff", ATTR{bInterfaceNumber}=="00", ATTRS{bNumConfigurations}=="*", RUN+="usb_modeswitch --driver-bind %p %s{idVendor} %s{idProduct} %E{PRODUCT}"
59 # Don't continue on "change" event, prevent trigger by changed configuration
60 ACTION!="add", GOTO="modeswitch_rules_end"
63 # Generic entry for most Huawei devices, excluding Android phones
64 ATTRS{idVendor}=="12d1", ATTRS{manufacturer}!="Android", ATTR{bInterfaceNumber}=="00", ATTR{bInterfaceClass}=="08", RUN+="usb_modeswitch '%b/%k'"}
67 set vendorList ""
68 set dvid ""
70 foreach idfile $filelist {
71 if {![regexp -nocase {./([0-9A-F]{4}:[0-9A-F]{4})} $idfile d id]} {continue}
72 if {[regexp -nocase {^12d1:} $id] && [lsearch $x_huaweiList $id] == -1} {continue}
73 if [info exists entry($id)] {
74 append entry($id) ", "
76 set rc [open $idfile r]
77 set buffer [read $rc]
78 close $rc
79 foreach line [split $buffer \n] {
80 set comment {}
81 regexp {# (.*)} $line d comment
82 if {[string length $comment] > 0} {
83 append entry($id) [string trim $comment]
84 break
88 foreach id_entry [lsort [array names entry]] {
89 set id [split $id_entry :]
90 set rule [regsub {\+##\+} $template [lindex $id 0]]
91 set rule [regsub {#\+\+#} $rule [lindex $id 1]]
92 puts $wc "\n# $entry($id_entry)\n$rule"
95 puts $wc {
96 LABEL="modeswitch_rules_end"}
97 close $wc