#!/usr/bin/perl -w # Copyright 2005 Sun Microsystems, Inc. ALL RIGHTS RESERVED # Use of this software is authorized pursuant to the # terms of the license found at # http://developers.sun.com/berkeley_license.html # # for more USB info, refer to # http://www.sun.com/io_technologies/USB-Faq.html use strict; use Getopt::Std; our($opt_c, $opt_f, $opt_r, $opt_h, $opt_s, $opt_w, $opt_l, $opt_v, $opt_n); my ($PRTCONF, $ALIASES, $CMD, $BINDINGS, $EXISTING); my @words; my %usbdriverhash; my ($line, $driver, $i, $runcmdfile); my $argc = $#ARGV; my $existing_bindings = "/tmp/existingbindings$$"; my $driver_aliases = "/etc/driver_aliases"; my $cmd = "/tmp/update_drv_cmd$$"; my $default_bindings = "/etc/usb/usb-default-bindings"; my $reboot_needed; sub cleanup { close EXISTING; close ALIASES; close BINDINGS; close CMD; unlink $existing_bindings; unlink $cmd; } $SIG{'INT'} = sub { cleanup; die "\ninterrupted, exiting ....\n"; }; $SIG{'__DIE__'} = sub { cleanup; print "@_\n"; exit(1); }; getopts('cf:hlnrsvwy') or die "$0: get options failed\n"; if ($opt_h || ($argc == -1)) { $opt_h = 0; # to shut up perl -w print "usage: $0 [-f filename] [-chlnrsvwy]\n"; print "where:\n"; print "\t\t-c - checkpoint the current bindings to\n"; print "\t\t $default_bindings or filename specified\n"; print "\t\t using -f option\n"; print "\t\t-f filename - change default binding filename\n"; print "\t\t-h - this usage message\n"; print "\t\t-l - list current bindings\n"; print "\t\t-n - show but do not execute update_drv cmds\n"; print "\t\t-r - restore original bindings from\n"; print "\t\t $default_bindings or filename specified\n"; print "\t\t using -f option\n"; print "\t\t-s - secure bindings but save old bindings in\n"; print "\t\t $default_bindings or filename specified\n"; print "\t\t using -f option\n"; print "\t\t-v - verbose\n"; print "\t\t-w - wipe out all bindings\n"; exit (0); } # check options for common sense if ($opt_r && $opt_s) { die "$0: -r and -s are mutually exclusive\n"; } elsif ($opt_r && $opt_w) { die "$0: -r and -w are mutually exclusive\n"; } elsif ($opt_s && $opt_w) { die "$0: -s and -w are mutually exclusive\n"; } if ($opt_f) { $default_bindings = $opt_f; if ($opt_v) { print "default bindings file is $default_bindings\n"; } } # find existing bindings and create cmd file to remove them open (ALIASES, "<$driver_aliases") or die "$0: cannot open $driver_aliases\n"; open (EXISTING, "> $existing_bindings") or die "$0: cannot open $existing_bindings file\n"; open (CMD, "> $cmd") or die "$0: cannot open $cmd file\n"; while () { next unless $_ =~ / "usb,| "usbif| "usb,class| "usb[0-9]| "usb[a-f]/; print EXISTING $_; @words = split /\s+/, $_; $usbdriverhash{$words[0]} = 1; print CMD "update_drv -d -i '$words[1]' $words[0]\n"; } close EXISTING; close ALIASES; if ($opt_l || $opt_v) { $opt_l = 1; # to shutup -w if ($opt_v) { print "found " . keys(%usbdriverhash) . " USB drivers\n"; foreach (keys(%usbdriverhash)) { print "\t$_\n"; } } print "existing bindings:\n"; open (EXISTING, "<$existing_bindings") or die "$0: cannot read $existing_bindings file"; while () { print "\t", $_; } close EXISTING; } if ($opt_s) { open(PRTCONF, "prtconf -vD|") or die "$0: cannot execute prtconf -vD"; while () { foreach $driver (keys(%usbdriverhash)) { next unless ($_ =~ /driver name: $driver/); while () { if ($_ =~ "'compatible'") { $line = ; @words = split /\'/, $line; print CMD "update_drv -a -i ", "'\"$words[1]\"' $driver\n"; last; } } } } close PRTCONF; } if ($opt_r) { open(BINDINGS, "<$default_bindings") or die "$0: cannot open $default_bindings file\n"; while () { @words = split /\s+/; print CMD "update_drv -a -i ", "'$words[1]' $words[0]\n"; } close BINDINGS; } close CMD; if ($opt_v || $opt_n) { print "update_drv cmd file:\n"; open (CMD, "<$cmd") or die "cannot open $cmd file"; while () { print "\t", $_; } close CMD; } if ($opt_n) { $runcmdfile = 0; } elsif ($opt_v && ($opt_s || $opt_r || $opt_w)) { print "run update_drv cmds? [no]: "; $line = ; chomp $line; if ($line eq "y" || $line eq "ye" || $line eq "yes") { $runcmdfile = 1; } } elsif (!$opt_v && ($opt_s || $opt_r || $opt_w)) { $runcmdfile = 1; } if ($opt_c) { if ($opt_v) { print "saving initial default bindings in ", "$default_bindings\n"; } system ("mv $existing_bindings $default_bindings"); } if ($runcmdfile) { $reboot_needed = 0; open (CMD, "<$cmd") or die "cannot open $cmd file"; while () { if ($_ =~ "update_drv -d") { $reboot_needed = 1; last; } } close CMD; if ($opt_s || $opt_w) { if (!(-e "$default_bindings") || $opt_w) { if ($opt_v) { print "saving initial default bindings in ", "$default_bindings\n"; } system ("mv $existing_bindings $default_bindings"); } } if ($opt_v) { print "running cmd file now\n"; } system ("sh $cmd > /dev/null 2>&1"); if ($opt_s || $opt_w || $opt_r) { if ($reboot_needed) { print "please reboot the system now\n"; } } } cleanup;