2011-12-25 17:40:32 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
$|=1;
|
|
|
|
|
|
|
|
my $DEV="/sys/bus/usb/devices";
|
|
|
|
|
|
|
|
sub getline{
|
|
|
|
my($path)=@_;
|
|
|
|
open(my $f,"<",$DEV."/".$path) || do {
|
|
|
|
# print "failed opening $path: $!\n";
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
my $l=<$f>;
|
|
|
|
close($f);
|
|
|
|
chomp($l);
|
|
|
|
return $l;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
my %found;
|
|
|
|
my %done;
|
|
|
|
|
|
|
|
sub gosub {
|
|
|
|
my ($dev,$grep)=@_;
|
|
|
|
my $dh;
|
|
|
|
my $dir;
|
|
|
|
opendir($dh, $DEV."/".$dev) || do {
|
|
|
|
# print "Failure for $dev?\n";
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
($dir)=grep {/$grep/} grep {/^[^.]/} readdir($dh);
|
|
|
|
if($dir eq ""){
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
close $dh;
|
|
|
|
$dev.="/".$dir;
|
|
|
|
return $dev;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub getdev{
|
|
|
|
my ($dev)=@_;
|
|
|
|
my $dh;
|
|
|
|
my $dir;
|
|
|
|
|
2012-01-03 01:15:42 +00:00
|
|
|
$dev=gosub($dev,":1") || return "";
|
2011-12-25 17:40:32 +00:00
|
|
|
$dev=gosub($dev,"^host");
|
2012-01-03 01:15:42 +00:00
|
|
|
$dev=gosub($dev,"^target") || return "";
|
2011-12-25 17:40:32 +00:00
|
|
|
$dev=~/target(.*)/;
|
2012-01-03 01:15:42 +00:00
|
|
|
$dev=gosub($dev,$1) || return "";
|
|
|
|
$dev=gosub($dev,"^block") || return "";
|
|
|
|
$dev=gosub($dev,".") || return "";
|
2011-12-25 17:40:32 +00:00
|
|
|
$dev=~m!/([^/]*)$!;
|
|
|
|
my $r=$1;
|
|
|
|
if($r!~/^sd/){
|
2012-01-03 01:15:42 +00:00
|
|
|
print "Strange device? $dev -> $r\n";
|
2011-12-25 17:40:32 +00:00
|
|
|
return "";
|
|
|
|
};
|
|
|
|
return $r;
|
|
|
|
};
|
|
|
|
|
|
|
|
my %pids;
|
|
|
|
use POSIX ":sys_wait_h";
|
|
|
|
|
|
|
|
my $kid;
|
|
|
|
$SIG{CHLD}=sub {
|
|
|
|
do {
|
|
|
|
$kid = waitpid(-1, WNOHANG);
|
|
|
|
delete $pids{$kid};
|
|
|
|
}while $kid>0;
|
|
|
|
};
|
|
|
|
|
|
|
|
sub dwim{
|
|
|
|
my($dev)=@_;
|
|
|
|
# print ("checking $dev\n");
|
|
|
|
my $pr=getline($dev."/idProduct");
|
|
|
|
return -1 if($pr eq "");
|
|
|
|
my $ve=getline($dev."/idVendor");
|
|
|
|
my $mf=getline($dev."/manufacturer");
|
|
|
|
# print "$pr:$ve\n";
|
|
|
|
if("$ve:$pr" eq "16c0:08ac") {
|
|
|
|
return 1 if $done{$dev}==1;
|
|
|
|
my $sdev=getdev($dev);
|
|
|
|
if ($sdev eq ""){
|
|
|
|
print "$dev not ready.\n";
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
print "Mass storage $dev -> $sdev\n";
|
|
|
|
|
|
|
|
my $pid = fork();
|
|
|
|
if (not defined $pid) {
|
|
|
|
die "fork failed\n";
|
|
|
|
} elsif ($pid == 0) {
|
2012-01-03 01:15:42 +00:00
|
|
|
system("./mass $sdev $dev");
|
2011-12-25 17:40:32 +00:00
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
$pids{$pid}=1;
|
|
|
|
}
|
|
|
|
$done{$dev}=1;
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
if("$ve:$pr" eq "04cc:0003") {
|
|
|
|
return 1 if $done{$dev}==2;
|
|
|
|
my $sdev=getdev($dev);
|
|
|
|
if ($sdev eq ""){
|
|
|
|
print "$dev not ready.\n";
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
print "flash $dev -> $sdev\n";
|
2012-01-03 01:15:42 +00:00
|
|
|
system("./flash $sdev $dev");
|
2011-12-25 17:40:32 +00:00
|
|
|
$done{$dev}=2;
|
|
|
|
return 1;
|
|
|
|
};
|
2012-01-03 01:15:42 +00:00
|
|
|
print "$ve:$pr [$mf] @ ";
|
2011-12-25 17:40:32 +00:00
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
my %disable;
|
|
|
|
while(1){
|
|
|
|
opendir(my $dh, $DEV);
|
2012-01-03 01:15:42 +00:00
|
|
|
my @paths=grep {/^[^.]/} readdir($dh);
|
2011-12-25 17:40:32 +00:00
|
|
|
close $dh;
|
|
|
|
# print "f: ",join(",",@files),"\n";
|
|
|
|
device:
|
|
|
|
for my $path (@paths){
|
|
|
|
next if ($disable{$path});
|
|
|
|
next if $path =~ /^usb/;
|
|
|
|
next if $path =~ /:/;
|
|
|
|
# print "p $path\n";
|
|
|
|
$found{$path}=1;
|
|
|
|
my $res=dwim($path);
|
|
|
|
if($res==-1){
|
2012-01-03 01:15:42 +00:00
|
|
|
print "$path -- ignoring it forever\n";
|
2011-12-25 17:40:32 +00:00
|
|
|
$disable{$path}=1;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
for my $a (keys %done){
|
|
|
|
if (!$found{$a}){
|
|
|
|
delete $done{$a};
|
|
|
|
print "$a vanished\n";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
%found=();
|
|
|
|
# print "\n";
|
|
|
|
sleep(1);
|
|
|
|
print scalar(keys %pids),"\r";
|
|
|
|
};
|