File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / athctl.c
Revision 1.1.2.3: download - view: text, annotated - select for diffs - revision graph
Mon Oct 25 12:56:27 2010 UTC (13 years, 8 months ago) by misho
Branches: tools1_0
added new codez

    1: /*************************************************************************
    2:  * (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com>
    3:  *  by Michael Pounov <misho@aitbg.com>
    4:  *
    5:  * $Author: misho $
    6:  * $Id: athctl.c,v 1.1.2.3 2010/10/25 12:56:27 misho Exp $
    7:  *
    8:  *************************************************************************/
    9: #include "global.h"
   10: #include "athctl.h"
   11: 
   12: 
   13: int Verbose;
   14: extern char compiled[], compiledby[], compilehost[];
   15: 
   16: 
   17: static void
   18: Usage()
   19: {
   20: 	printf(	"athCtl is tool for Atheros WiFi cards managment \n"
   21: 		"=== %s === %s@%s ===\n\n"
   22: 		"  Syntax: athctl [options] [0xMemory_Address]\n"
   23: 		"          athctl [-v] -t [-i <iface_no>]\n"
   24: 		"          athctl [-v] -c <timeout> [-i <iface_no>]\n"
   25: 		"          athctl [-v] -d <distance> [-i <iface_no>]\n"
   26: 		"          athctl [-v] -r <0xoffset> <0xMemory_Address>\n"
   27: 		"          athctl [-v] -w <0xoffset> <0xMemory_Address>\n"
   28: 		"          athctl [-v] -s <file> <0xMemory_Address>\n"
   29: 		"          athctl [-v] -w <file> <0xMemory_Address>\n"
   30: 		"\n"
   31: 		"\t-v\t\tVerbose ...\n"
   32: 		"\t-t\t\tGet current Atheros maximum range in meters\n"
   33: 		"\t-i <iface_no>\tApply to this Atheros interface number (like ath0 == 0)\n"
   34: 		"\t-d <distance>\tMode distance, meters to target\n"
   35: 		"\t-c <timeout>\tMode distance, mS timeouts correction\n"
   36: 		"\t-s <file>\tDump EEPROM to file\n"
   37: 		"\t-u <file>\tUpdate EEPROM from file\n"
   38: 		"\t-r <offset>\tRead EEPROM word from PCI mapped memory address\n"
   39: 		"\t-w <offset>\tWrite EEPROM word to PCI mapped memory address\n"
   40: 		"\n", compiled, compiledby, compilehost);
   41: }
   42: 
   43: static int
   44: calcDistance(int ifid, int dist, int cor)
   45: {
   46: 	int slottime[2], timeout[2];
   47: 	size_t len;
   48: 	char szStr[STRSIZ];
   49: 
   50: 	slottime[0] = 9 + (dist / 300) + (dist % 300 ? 1 : 0);
   51: 	timeout[0] = slottime[0] * 2 + 3 + cor;
   52: 	VERB(3) printf("Info:: slottime=%d timeout=%d\n", slottime[0], timeout[0]);
   53: 
   54: 	memset(szStr, 0, STRSIZ);
   55: 	snprintf(szStr, STRSIZ, SC_SLOTTIME, ifid);
   56: 	if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) {
   57: 		printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
   58: 		return -1;
   59: 	} else if (sysctlbyname(szStr, &slottime[1], &len, NULL, 0) == -1) {
   60: 		printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
   61: 		return -1;
   62: 	} else if (sysctlbyname(szStr, NULL, NULL, &slottime[0], sizeof slottime[0]) == -1) {
   63: 		printf("Error:: set sysctl %s from %d #%d - %s\n", szStr, slottime[1], 
   64: 				errno, strerror(errno));
   65: 		return -1;
   66: 	} else
   67: 		VERB(1) printf("Info:: set slottime(%d) from %d to %d ... OK!\n", len, 
   68: 				slottime[1], slottime[0]);
   69: 
   70: 	memset(szStr, 0, STRSIZ);
   71: 	snprintf(szStr, STRSIZ, SC_ACKTIMEOUT, ifid);
   72: 	if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) {
   73: 		printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
   74: 		return -1;
   75: 	} else if (sysctlbyname(szStr, &timeout[1], &len, NULL, 0) == -1) {
   76: 		printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
   77: 		return -1;
   78: 	} else if (sysctlbyname(szStr, NULL, NULL, &timeout[0], sizeof timeout[0]) == -1) {
   79: 		printf("Error:: set sysctl %s from %d #%d - %s\n", szStr, timeout[1], 
   80: 				errno, strerror(errno));
   81: 		return -1;
   82: 	} else
   83: 		VERB(1) printf("Info:: set acktimeout(%d) from %d to %d ... OK!\n", len, 
   84: 				timeout[1], timeout[0]);
   85: 
   86: 	memset(szStr, 0, STRSIZ);
   87: 	snprintf(szStr, STRSIZ, SC_CTSTIMEOUT, ifid);
   88: 	if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) {
   89: 		printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
   90: 		return -1;
   91: 	} else if (sysctlbyname(szStr, &timeout[1], &len, NULL, 0) == -1) {
   92: 		printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
   93: 		return -1;
   94: 	} else if (sysctlbyname(szStr, NULL, NULL, &timeout[0], sizeof timeout[0]) == -1) {
   95: 		printf("Error:: set sysctl %s from %d #%d - %s\n", szStr, timeout[1], 
   96: 				errno, strerror(errno));
   97: 		return -1;
   98: 	} else
   99: 		VERB(1) printf("Info:: set ctstimeout(%d) from %d to %d ... OK!\n", len, 
  100: 				timeout[1], timeout[0]);
  101: 
  102: 	return timeout[0];
  103: }
  104: 
  105: 
  106: static int
  107: calcTimeout(int ifid, int cor)
  108: {
  109: 	int slottime[2], timeout[2];
  110: 	size_t len;
  111: 	char szStr[STRSIZ];
  112: 
  113: 	memset(szStr, 0, STRSIZ);
  114: 	snprintf(szStr, STRSIZ, SC_SLOTTIME, ifid);
  115: 	if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) {
  116: 		printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
  117: 		return -1;
  118: 	} else if (sysctlbyname(szStr, &slottime[1], &len, NULL, 0) == -1) {
  119: 		printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
  120: 		return -1;
  121: 	} else
  122: 		VERB(1) printf("Info:: get slottime(%d) %d ... OK!\n", len, slottime[1]);
  123: 
  124: 	memset(szStr, 0, STRSIZ);
  125: 	snprintf(szStr, STRSIZ, SC_ACKTIMEOUT, ifid);
  126: 	if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) {
  127: 		printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
  128: 		return -1;
  129: 	} else if (sysctlbyname(szStr, &timeout[1], &len, NULL, 0) == -1) {
  130: 		printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
  131: 		return -1;
  132: 	} else
  133: 		VERB(1) printf("Info:: get acktimeout(%d) %d ... OK!\n", len, timeout[1]);
  134: 
  135: 	slottime[0] = (timeout[1] - 3 - cor) / 2;
  136: 	VERB(3) printf("Info:: calculated slottime=%d\n", slottime[0]);
  137: 	timeout[0] = (slottime[0] - 10) * 300;
  138: 	VERB(3) printf("Info:: calculated timeout=%d\n", timeout[0]);
  139: 
  140: 	return timeout[0];
  141: }
  142: 
  143: 
  144: static inline void *
  145: devOpen(u_long baseaddr)
  146: {
  147: 	int fd;
  148: 	void *basemem;
  149: 
  150: 	fd = open("/dev/mem", O_RDWR);
  151: 	if (fd == -1) {
  152: 		printf("Error:: open device #%d - %s\n", errno, strerror(errno));
  153: 		return NULL;
  154: 	}
  155: 	basemem = mmap(NULL, ATH_PCI_MEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, baseaddr);
  156: 	if (basemem == MAP_FAILED) {
  157: 		printf("Error:: map device #%d - %s\n", errno, strerror(errno));
  158: 		close(fd);
  159: 		return NULL;
  160: 	} else
  161: 		close(fd);
  162: 
  163: 	return basemem;
  164: }
  165: 
  166: static inline void
  167: devClose(void *basemem)
  168: {
  169: 	if (basemem)
  170: 		munmap(basemem, ATH_PCI_MEM_SIZE);
  171: }
  172: 
  173: /*
  174: static int
  175: readEEPROM()
  176: {
  177: 	register u_long i;
  178: 
  179: 	printf("Reading EEPROM ...\n");
  180: 	for (i = 0; i < ATH_EEPROM_SIZE / 2; i++) {
  181: 	}
  182: }
  183: */
  184: 
  185: // ----------------------------------------------------
  186: 
  187: int
  188: main(int argc, char **argv)
  189: {
  190: 	char ch, szName[MAXPATHLEN] = { 0 }, mode = 0;
  191: 	int ret = 0, dist = 0, cor = 0, ino = 0;
  192: 	u_long offset, baseaddr = (u_long) -1;
  193: 	void *basemem = NULL;
  194: 
  195: 	while ((ch = getopt(argc, argv, "hvtr:w:i:d:c:u:s:")) != -1)
  196: 		switch (ch) {
  197: 			case 'v':
  198: 				Verbose++;
  199: 				break;
  200: 			case 't':
  201: 				mode |= 2;
  202: 				break;
  203: 			case 'i':
  204: 				ino = strtol(optarg, NULL, 0);
  205: 				if (ino < 0) {
  206: 					printf("Error:: in interface number %d\n", ino);
  207: 					return 1;
  208: 				}
  209: 				break;
  210: 			case 'd':
  211: 				mode |= 1;
  212: 				dist = strtol(optarg, NULL, 0);
  213: 				if (dist < 1) {
  214: 					printf("Error:: in distance meters %d\n", dist);
  215: 					return 1;
  216: 				}
  217: 				break;
  218: 			case 'c':
  219: 				mode |= 1;
  220: 				cor = strtol(optarg, NULL, 0);
  221: 				break;
  222: 			case 's':
  223: 				mode = 0x10;
  224: 				strlcpy(szName, optarg, MAXPATHLEN);
  225: 				break;
  226: 			case 'u':
  227: 				mode = 0x20;
  228: 				strlcpy(szName, optarg, MAXPATHLEN);
  229: 				break;
  230: 			case 'r':
  231: 				mode = 4;
  232: 				offset = strtoul(optarg, NULL, 0);
  233: 				break;
  234: 			case 'w':
  235: 				mode = 8;
  236: 				offset = strtoul(optarg, NULL, 0);
  237: 				break;
  238: 			case 'h':
  239: 			default:
  240: 				Usage();
  241: 				return 1;
  242: 		}
  243: 	argc -= optind;
  244: 	argv += optind;
  245: 	if (argc && *argv)
  246: 		baseaddr = strtoul(*argv, NULL, 0);
  247: 	if (!mode) {
  248: 		printf("Error:: not selected mode for operation ...\n");
  249: 		return 1;
  250: 	}
  251: 	if (mode > 3 && baseaddr == (u_long) -1) {
  252: 		printf("Error:: in this mode for operation, must give memory mapped address ...\n");
  253: 		return 1;
  254: 	}
  255: 
  256: 	if (mode & 1)
  257: 		if ((ret = calcDistance(ino, dist, cor)) < 1)
  258: 			return 2;
  259: 	if (mode & 2) {
  260: 		if ((ret = calcTimeout(ino, cor)) < 1)
  261: 			return 2;
  262: 		else {
  263: 			VERB(1)
  264: 				printf("Maximum approximate distance ~%d meters\n", ret);
  265: 			else
  266: 				printf("~%d\n", ret);
  267: 		}
  268: 	}
  269: 
  270: 	if (mode & 4) {
  271: 		if (!(basemem = devOpen(baseaddr)))
  272: 			return 2;
  273: //		if ((ret = readWord(basemem)) < 1)
  274: //			return 3;
  275: 		devClose(basemem);
  276: 	}
  277: 
  278: 	return 0;
  279: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>