--- embedtools/src/athctl.c 2010/10/28 23:25:59 1.1.2.7 +++ embedtools/src/athctl.c 2010/11/05 00:43:27 1.1.2.11 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ - * $Id: athctl.c,v 1.1.2.7 2010/10/28 23:25:59 misho Exp $ + * $Id: athctl.c,v 1.1.2.11 2010/11/05 00:43:27 misho Exp $ * *************************************************************************/ #include "global.h" @@ -26,13 +26,15 @@ Usage() " athctl [-v] -r <0xoffset> <0xMemory_Address>\n" " athctl [-v] -w <0xoffset> <0xMemory_Address> \n" " athctl [-v] -s <0xMemory_Address>\n" - " athctl [-v] -w <0xMemory_Address>\n" + " athctl [-v] -u <0xMemory_Address>\n" + " athctl [-v] -R <0xMemory_Address> [new_regdomain]\n" "\n" "\t-v\t\tVerbose ...\n" "\t-t\t\tGet current Atheros maximum range in meters\n" "\t-i \tApply to this Atheros interface number (like ath0 == 0)\n" "\t-d \tMode distance, meters to target\n" "\t-c \tMode distance, mS timeouts correction\n" + "\t-R\t\tRead or Write EEPROM Regulatory domain\n" "\t-s \tDump EEPROM to file\n" "\t-u \tUpdate EEPROM from file\n" "\t-r \tRead EEPROM word from PCI mapped memory address\n" @@ -170,13 +172,13 @@ devClose(void *basemem) munmap(basemem, ATH_PCI_MEM_SIZE); } -static inline short +static inline int readWord(u_char *mem, u_long offset) { register int timeout = ATH_ACCESS_TIMEOUT; u_long stat; - printf("Reading EEPROM memory %p+%lx ...\n", mem, offset); + VERB(9) printf("Reading EEPROM memory %p+%lx ...\n", mem, offset); ATH_OUT(mem, AR5211_EEPROM_CONF, 0); usleep(ATH_ACCESS_WAIT); /* enable eeprom access */ @@ -199,7 +201,7 @@ readWord(u_char *mem, u_long offset) } stat = ATH_IN(mem, AR5211_EEPROM_DATA); - return stat & 0x0000ffff; + return (stat & 0x0000ffff) << 16; } } @@ -207,14 +209,14 @@ readWord(u_char *mem, u_long offset) return -1; } -static inline short +static inline int writeWord(u_char *mem, u_long offset, u_short newval) { register int i = ATH_WRITE_RETRY, timeout; u_long pcicfg, stat; - u_short chk; + int chk; - printf("Writing EEPROM memory %p+%lx ...\n", mem, offset); + VERB(9) printf("Writing EEPROM memory %p+%lx ...\n", mem, offset); /* enable pci write access */ pcicfg = ATH_IN(mem, AR5K_PCICFG); ATH_OUT(mem, AR5K_PCICFG, (pcicfg & ~AR5K_PCICFG_SPWR_DN)); @@ -255,10 +257,10 @@ writeWord(u_char *mem, u_long offset, u_short newval) chk = readWord(mem, offset); if (chk == (u_short) -1) return -1; - if (chk == newval) + if ((u_short) (chk >> 16) == newval) return chk; else - VERB(1) printf("Write & Read don`t match 0x%04X != 0x%04X\n", newval, chk); + VERB(1) printf("Write & Read don`t match 0x%04X != 0x%04X\n", newval, (u_short) (chk >> 16)); if (i) printf("Warning:: Retrying EEPROM write ...\n"); } while (--i); @@ -271,23 +273,26 @@ static int dumpFile(const char *csName, u_char *mem) { register u_long i; - u_short data, d1, d2; - u_short eeprom[ATH_EEPROM_SIZE] = { 0 }; - int f; + u_short d1, d2; + u_char eeprom[ATH_EEPROM_SIZE] = { 0 }; + int f, data; - printf("Reading EEPROM memory %p ::\n", mem); + VERB(2) printf("Reading EEPROM memory %p ::\n", mem); for (i = 0; i < ATH_EEPROM_SIZE / 2; i++) { if (!(i % 0x40)) { - printf("0x%04lX [", i * 2); if (i) printf("]\n"); + printf("0x%04lX [", i * 2); } - if ((data = readWord(mem, i)) < 1) + if ((data = readWord(mem, i)) == -1) return -1; else { - d1 = data / 0x100; - d2 = data % 0x100; + d1 = ((u_short)(data >> 16)) / 0x100; + d2 = ((u_short)(data >> 16)) % 0x100; + + VERB(5) printf( "Current value 0x%04X on position 0x%04lX will change 0x%02X 0x%02X\n", + (u_short) (data >> 16), i, d1, d2); } eeprom[i * 2] = d2; @@ -298,7 +303,7 @@ dumpFile(const char *csName, u_char *mem) } printf("]\n"); - printf("Saving EEPROM to file %s ... ", csName); + VERB(2) printf("Saving EEPROM to file %s ... ", csName); f = open(csName, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (f == -1) { printf("Failed!\nError:: in create file %s #%d - %s\n", csName, @@ -320,11 +325,11 @@ static int flashFile(const char *csName, u_char *mem) { register u_long i; - u_short data, d1; - u_short eeprom[ATH_EEPROM_SIZE] = { 0 }; - int f; + u_short d1; + u_char eeprom[ATH_EEPROM_SIZE] = { 0 }; + int f, data; - printf("Reading EEPROM from file %s ... ", csName); + VERB(2) printf("Reading EEPROM from file %s ... ", csName); f = open(csName, O_RDONLY); if (f == -1) { printf("Failed!\nError:: in open file %s #%d - %s\n", csName, @@ -337,28 +342,30 @@ flashFile(const char *csName, u_char *mem) return 0; } close(f); - printf("OK!\n"); + VERB(2) printf("OK!\n"); - printf("Writing EEPROM memory %p ::\n", mem); + VERB(2) printf("Writing EEPROM memory %p ::\n", mem); for (i = 0; i < ATH_EEPROM_SIZE / 2; i++) { if (!(i % 0x40)) { - printf("0x%04lX [", i * 2); if (i) printf("]\n"); + printf("0x%04lX [", i * 2); } - if ((data = readWord(mem, i)) < 1) + if ((data = readWord(mem, i)) == -1) return -1; else d1 = eeprom[i * 2 + 1] * 0x100 + eeprom[i * 2]; + VERB(5) printf("eeprom_data=0x%04X read_d1=0x%04X\n", (u_char) (data >> 16), d1); - if (data == d1) + if (((u_short) (data >> 16)) == d1) printf("."); else { - printf("x"); if (writeWord(mem, i, d1) < 1) - return -1; + printf("!"); + else + printf("x"); } usleep(ATH_ACCESS_WAITDOWN); @@ -379,7 +386,7 @@ main(int argc, char **argv) u_short newval = 0; void *basemem = NULL; - while ((ch = getopt(argc, argv, "hvtr:w:i:d:c:u:s:")) != -1) + while ((ch = getopt(argc, argv, "hvRtr:w:i:d:c:u:s:")) != -1) switch (ch) { case 'v': Verbose++; @@ -414,6 +421,9 @@ main(int argc, char **argv) mode = 0x20; strlcpy(szName, optarg, MAXPATHLEN); break; + case 'R': + mode = 0x40; + break; case 'r': mode = 4; offset = strtoul(optarg, NULL, 0); @@ -446,6 +456,8 @@ main(int argc, char **argv) } else newval = (u_short) strtoul(argv[1], NULL, 0); } + if (mode & 0x40 && argv[1]) + newval = (u_short) strtoul(argv[1], NULL, 0); if (mode & 1) if ((ret = calcDistance(ino, dist, cor)) < 1) @@ -468,7 +480,7 @@ main(int argc, char **argv) devClose(basemem); return 3; } else - printf("EEPROM readed value 0x%04X\n", (u_short) ret); + printf("EEPROM readed value 0x%04X\n", (u_short) (ret >> 16)); devClose(basemem); } if (mode & 8) { @@ -478,7 +490,7 @@ main(int argc, char **argv) devClose(basemem); return 3; } else - printf("EEPROM writed value 0x%04X\n", (u_short) ret); + printf("EEPROM writed value 0x%04X\n", (u_short) (ret >> 16)); devClose(basemem); } @@ -498,6 +510,18 @@ main(int argc, char **argv) devClose(basemem); return 3; } + devClose(basemem); + } + + if (mode & 0x40) { + if (!(basemem = devOpen(baseaddr))) + return 2; + /* + if ((ret = regDomain(basemem, newval)) < 1) { + devClose(basemem); + return 3; + } + */ devClose(basemem); }