Advertisement
Guest User

Untitled

a guest
Nov 11th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 8.52 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. # Copyright (C) 2007  Michael Kurz     michi.kurz (at) googlemail.com
  4. # Copyright (C) 2007  Petr Tomasek     tomasek (#) etf,cuni,cz
  5. # Copyright (C) 2007  Carlos Corbacho  cathectic (at) gmail.com
  6. #
  7. # Version 0.6.1 (2007-11-08)
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 3
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  22.  
  23.  
  24. require 5.004;
  25.  
  26. use strict;
  27. use Fcntl;
  28. use POSIX;
  29. use File::Basename;
  30.  
  31. sub initialize_ioports
  32. {
  33.   sysopen (IOPORTS, "/dev/port", O_RDWR)
  34.     or die "/dev/port: $!\n";
  35.   binmode IOPORTS;
  36. }
  37.  
  38. sub close_ioports
  39. {
  40.   close (IOPORTS)
  41.     or print "Warning: $!\n";
  42. }
  43.  
  44.  
  45.  
  46. sub inb
  47. {
  48.   my ($res,$nrchars);
  49.   sysseek IOPORTS, $_[0], 0 or return -1;
  50.   $nrchars = sysread IOPORTS, $res, 1;
  51.   return -1 if not defined $nrchars or $nrchars != 1;
  52.   $res = unpack "C",$res ;
  53.   return $res;
  54. }
  55.  
  56. # $_[0]: value to write
  57. # $_[1]: port to write
  58. # Returns: -1 on failure, 0 on success.
  59. sub outb
  60. {
  61.   if ($_[0] > 0xff)
  62.   {
  63.     my ($package, $filename, $line, $sub) = caller(1);
  64.     print "\n*** Called outb with value=$_[1] from line $line\n",
  65.           "*** (in $sub). PLEASE REPORT!\n",
  66.           "*** Terminating.\n";
  67.     exit(-1);
  68.   }
  69.   my $towrite = pack "C", $_[0];
  70.   sysseek IOPORTS, $_[1], 0 or return -1;
  71.   my $nrchars = syswrite IOPORTS, $towrite, 1;
  72.   return -1 if not defined $nrchars or $nrchars != 1;
  73.   return 0;
  74. }
  75.  
  76. sub wait_write
  77. {
  78.     my $i = 0;
  79.     while ((inb($_[0]) & 0x02) && ($i < 10000)) {
  80.         sleep(0.01);
  81.         $i++;
  82.     }
  83.     return -($i == 10000);
  84. }
  85.  
  86. sub wait_read
  87. {
  88.     my $i = 0;
  89.     while (!(inb($_[0]) & 0x01) && ($i < 10000)) {
  90.         sleep(0.01);
  91.         $i++;
  92.     }
  93.     return -($i == 10000);
  94. }
  95.  
  96. sub wait_write_ec
  97. {
  98.     wait_write(0x66);
  99. }
  100.  
  101. sub wait_read_ec
  102. {
  103.     wait_read(0x66);
  104. }
  105.  
  106. sub send_ec
  107. {
  108.     if (!wait_write_ec()) { outb($_[0], 0x66); }
  109.     if (!wait_write_ec()) { outb($_[1], 0x62); }
  110. }
  111.  
  112. sub write_ec
  113. {
  114.     if (!wait_write_ec()) { outb(0x81, 0x66 ); }
  115.     if (!wait_write_ec()) { outb($_[0], 0x62); }
  116.     if (!wait_write_ec()) { outb($_[1], 0x62); }
  117. }
  118.  
  119. sub read_ec
  120. {
  121.     if (!wait_write_ec()) { outb(0x80, 0x66 ); }
  122.     if (!wait_write_ec()) { outb($_[0], 0x62); }
  123.     if (!wait_read_ec())  { inb(0x62); }
  124. }
  125.  
  126. sub write_kc
  127. {
  128.     if (!wait_write(0x64)) { outb($_[0], 0x64); }
  129.     if (!wait_write(0x64)) { outb($_[1], 0x60); }
  130. }
  131.  
  132. sub print_regs
  133. {
  134.     initialize_ioports();
  135.  
  136.     my @arr = ("00","10","20","30","40","50","60","70","80","90","A0","B0","C0","D0","E0","F0", "");
  137.  
  138.     my $i = 0;
  139.     my $t = 0;
  140.     print "\n  \t00\t01\t02\t03\t04\t05\t06\t07\t|\t08\t09\t0A\t0B\t0C\t0D\t0E\t0F\n";
  141.     print "  \t__\t__\t__\t__\t__\t__\t__\t__\t|\t__\t__\t__\t__\t__\t__\t__\t__\n";
  142.     print "00 |\t";
  143.     for ($i = 0; $i < 256; $i++)
  144.     {
  145.         $t = read_ec($i);
  146.         print $t;
  147.         print "\t";
  148.         if ((($i + 1) % 8) == 0){
  149.             if ((($i + 1) % 16) == 0) {
  150.                 if ($i != 255) { print "\n$arr[(($i-(($i + 1) % 16)) / 16) + 1] |\t"; }
  151.             } else {
  152.                 print "|\t";
  153.             }
  154.         }
  155.     }
  156.    
  157.     print "\n";
  158.    
  159.     close_ioports();
  160. }
  161.  
  162. sub write_temp
  163. {
  164.     initialize_ioports();
  165.     write_ec($_[0],$_[1]);
  166.     close_ioports();
  167. }
  168.  
  169. sub testnum
  170. {
  171.     my $i;
  172.     for ($i = 0; $i<256;$i++) {
  173.         if ($_[0] eq "$i") { return 1 };
  174.     }
  175.     return 0;
  176. }
  177.  
  178. my $ii;
  179.  
  180. if (!$ARGV[0]){
  181.         print "wrong arguments!\n";
  182.     print "usage:\n";
  183.     print "\'acer_ec regs\' \t\t\t\tdumps all ec registers\n";
  184.     print "\'acer_ec ledon\' \t\t\t\tswitch on 'mail LED' (WMID)\n";
  185.     print "\'acer_ec ledoff\' \t\t\t\tswitch off 'mail LED' (WMID)\n";
  186.     print "\'acer_ec getled\' \t\t\t\tget 'mail LED' status (WMID)\n";
  187.     print "\'acer_ec getled2\' \t\t\t\tget 'mail LED' status(AMW0)\n";
  188.     print "\'acer_ec getwireless\' \t\t\t\tget 'wireless' status (AMW0)\n";
  189.     print "\'acer_ec gettouch\' \t\t\t\tis the touchpad disabled?\n";
  190.     print "\'acer_ec setfanthresh <temp>\' \t\t\t\tset temperature threshhold to <temp>, DANGEROUS!\n";
  191.     print "\'acer_ec getfanthresh\' \t\t\t\tget temperature threshhold\n";
  192.     print "\'acer_ec <temp-number> <temperature>\' \tfor setting a temperature\n";
  193.     print "where <temp-number> is from 0-7, and <temperture> is from 0-255\n";
  194.     print "\'acer_ec ?= <reg>\' \t\tQuery register's value\n";
  195.     print "\'acer_ec := <reg> <val>\' \tSet register's value\n";
  196.     print "\'acer_ec +f <reg> <val>\' \tOr register's value with val (to set flags)\n";
  197.     print "\'acer_ec -f <reg> <val>\' \tAnd register's value with ~val (to clear flags)\n";
  198.     print "\'forcekc\' \tTry all possible values on writeable RAM of keyboard controller\n";
  199.     print "\'kcw <cmd> <val>\' \tWrite a command and a value to the keyboard controller\n";
  200. } elsif ($ARGV[0] eq "regs") {
  201.     print_regs();
  202. } elsif ($ARGV[0] eq "getled") {
  203.     # TM2490 only (WMID)
  204.     initialize_ioports();
  205.     if (read_ec(0x9f)&0x01) {
  206.         print "Mail LED on\n";
  207.     } else {
  208.         print "Mail LED off\n"; }
  209.     close_ioports();
  210. } elsif ($ARGV[0] eq "getled2") {
  211.     # Aspire 5020 only (AMW0)
  212.     initialize_ioports();
  213.     if (read_ec(0x0A)&0x80) {
  214.         print "Mail LED on\n";
  215.     } else {
  216.         print "Mail LED off\n"; }
  217.     close_ioports();
  218. } elsif ($ARGV[0] eq "getwireless") {
  219.     # Aspire 5020 only (AMW0)
  220.     initialize_ioports();
  221.     if (read_ec(0x0A)&0x4) {
  222.         print "Wireless on\n";
  223.     } else {
  224.         print "Wireless off\n"; }
  225.     close_ioports();
  226. } elsif ($ARGV[0] eq "gettouch") {
  227.     # TM2490 only - needs testing
  228.     initialize_ioports();
  229.     if (read_ec(0x9e)&0x08) {
  230.         print "touchpad disabled\n";
  231.     } else {
  232.         print "touchpad enabled\n"; }
  233.     close_ioports();
  234. } elsif ($ARGV[0] eq "?=") {
  235.     initialize_ioports();
  236.     my $r = hex($ARGV[1]);
  237.     printf("REG[0x%02x] == 0x%02x\n", $r, read_ec($r));
  238.     close_ioports();
  239. } elsif ($ARGV[0] eq ":=") {
  240.     initialize_ioports();
  241.     my $r = hex($ARGV[1]);
  242.     my $f = hex($ARGV[2]);
  243.     my $val = read_ec($r);
  244.     printf("REG[0x%02x] == 0x%02x\n", $r, $val);
  245.     printf("REG[0x%02x] := 0x%02x\n", $r, $f);
  246.         write_ec( $r, $f);
  247.     printf("REG[0x%02x] == 0x%02x\n", $r, read_ec($r));
  248.     close_ioports();
  249. } elsif ($ARGV[0] eq "+f") {
  250.     initialize_ioports();
  251.     my $r = hex($ARGV[1]);
  252.     my $f = hex($ARGV[2]);
  253.     my $val = read_ec($r);
  254.     printf("REG[0x%02x] == 0x%02x\n", $r, $val);
  255.     printf("REG[0x%02x] := 0x%02x\n", $r, $val | $f);
  256.         write_ec( $r, $val | $f);
  257.     printf("REG[0x%02x] == 0x%02x\n", $r, read_ec($r));
  258.     close_ioports();
  259. } elsif ($ARGV[0] eq "-f") {
  260.     initialize_ioports();
  261.     my $r = hex($ARGV[1]);
  262.     my $f = hex($ARGV[2]);
  263.     my $val = read_ec($r);
  264.     printf("REG[0x%02x] == 0x%02x\n", $r, $val);
  265.     printf("REG[0x%02x] := 0x%02x\n", $r, $val & ~$f);
  266.         write_ec( $r, $val & ~$f);
  267.     printf("REG[0x%02x] == 0x%02x\n", $r, read_ec($r));
  268.     close_ioports();
  269. } elsif ($ARGV[0] eq "ledon") {
  270.     # TM2490 only - needs testing
  271.     initialize_ioports();
  272.     if (!wait_write(0x64)) { outb(0x59, 0x64); }
  273.     if (!wait_write(0x64)) { outb(0x92,   0x60); }
  274.     close_ioports();
  275. } elsif ($ARGV[0] eq "ledoff") {
  276.     # TM2490 only - needs testing
  277.     initialize_ioports();
  278.     if (!wait_write(0x64)) { outb(0x59, 0x64); }
  279.     if (!wait_write(0x64)) { outb(0x93,   0x60); }
  280.     close_ioports();
  281. } elsif ($ARGV[0] eq "getfanthresh") {
  282.     initialize_ioports();
  283.     $ii=read_ec(0xa9);
  284.     close_ioports();
  285.         print "Temperature threshhold: $ii (celsius)\n";
  286. } elsif (($ARGV[0] eq "setfanthresh") && testnum($ARGV[1])) {
  287.     write_temp(0xA9,$ARGV[1]);
  288. } elsif ($ARGV[0] eq "setbright") {
  289.     # Aspire 5020 only (AMW0)
  290.     if ($ARGV[1] >= 0 && $ARGV[1] <= 15) {
  291.         write_temp(0x83, $ARGV[1]);
  292.     } else {
  293.         print "second argument must be a number between 0 and 15\n";
  294.     }
  295. } elsif ($ARGV[0] eq "forcekc") {
  296.     # Be smart - we only send the commands for writing to keyboard RAM
  297.     initialize_ioports();
  298.     my ($kbdata, $cont, $kbreg);
  299.     for ($kbreg = 0x40; $kbreg <= 0x5f; $kbreg++) {
  300.         for ($kbdata = 0; $kbdata < 256; $kbdata++) {
  301.             write_kc($kbreg, $kbdata);
  302.  
  303.             print sprintf("%0#4x", $kbreg), ", ", sprintf("%0#4x", $kbdata), "\n";
  304.             print "Continue? y/n: ";
  305.             $cont = <STDIN>;
  306.             if ($cont eq "n") {
  307.                 last;
  308.             }
  309.         }
  310.     }
  311.     close_ioports();
  312. } elsif ($ARGV[0] eq "kcw") {
  313.     initialize_ioports();
  314.     write_kc($ARGV[1], $ARGV[2]);
  315.     close_ioports();
  316. } else {
  317.     print "wrong arguments!\n";
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement