mirror of
https://github.com/lm-sensors/lm-sensors.git
synced 2026-10-28 12:40:42 +00:00
Add missing include.
Discard unused hexchar function.
Massive reindent.
Rework the command line parameters handling.
git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@2489 7894878c-1315-0410-8ee3-d5d059ff63e0
This commit is contained in:
1 parent
6f821d2ad8
commit
62b7916019
1 file changed
+129
-128
+129
-128
@@ -1,7 +1,8 @@
|
||||
/*
|
||||
isaset.c - isaset, a user-space program to write ISA registers
|
||||
Copyright (c) 2000 - 2004 Frodo Looijaard <frodol@dds.nl>, and
|
||||
Copyright (C) 2000 Frodo Looijaard <frodol@dds.nl>, and
|
||||
Mark D. Studebaker <mdsxyz123@yahoo.com>
|
||||
Copyright (C) 2004 The lm_sensors group
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -21,12 +22,13 @@
|
||||
/*
|
||||
Typical usage:
|
||||
isaset 0x295 0x296 0x10 0x12 Write 0x12 to address 0x10 using address/data registers
|
||||
isaset -f 0x5000 0x12 Write 0x12 to location 0x5010
|
||||
isaset -f 0x5010 0x12 Write 0x12 to location 0x5010
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/* To keep glibc2 happy */
|
||||
@@ -40,155 +42,154 @@
|
||||
unsigned long isa_io_base = 0; /* XXX for now */
|
||||
#endif /* __powerpc__ */
|
||||
|
||||
char hexchar(int i)
|
||||
{
|
||||
if ((i >= 0) && (i <= 9))
|
||||
return '0' + i;
|
||||
else if (i <= 15)
|
||||
return 'a' - 10 + i;
|
||||
else
|
||||
return 'X';
|
||||
}
|
||||
|
||||
void help(void)
|
||||
{
|
||||
fprintf(stderr,"Syntax: isaset ADDRREG DATAREG ADDR DATA (for chips with addr/data registers)\n");
|
||||
fprintf(stderr," isaset -f ADDR DATA (for chips with a flat address space)\n");
|
||||
fprintf(stderr,
|
||||
"Syntax for I2C-like access:\n"
|
||||
" isaset ADDRREG DATAREG ADDRESS VALUE\n"
|
||||
"Syntax for flat address space:\n"
|
||||
" isaset -f ADDRESS VALUE\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int addrreg, datareg = 0;
|
||||
unsigned char value, res, addr = 0;
|
||||
int flat = 0;
|
||||
char *end;
|
||||
int addrreg, datareg = 0, value, addr = 0;
|
||||
unsigned char res;
|
||||
int flat = 0;
|
||||
char *end;
|
||||
|
||||
if (argc < 4) {
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
if (argc < 4) {
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if(strcmp(argv[1], "-f")) {
|
||||
addrreg = strtol(argv[1],&end,0);
|
||||
} else {
|
||||
if(argc != 4) {
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
flat = 1;
|
||||
addrreg = strtol(argv[2],&end,0);
|
||||
}
|
||||
if (*end) {
|
||||
fprintf(stderr,"Error: Invalid address!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
if ((addrreg < 0) || (addrreg > 0xffff)) {
|
||||
fprintf(stderr,"Error: Address out of range!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
if (!strcmp(argv[1], "-f")) {
|
||||
flat = 1;
|
||||
}
|
||||
|
||||
if(flat) {
|
||||
value = strtol(argv[3],&end,0);
|
||||
if (*end) {
|
||||
fprintf(stderr,"Error: Invalid data!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
if (argc != 5-flat) {
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
datareg = strtol(argv[2],&end,0);
|
||||
if (*end) {
|
||||
fprintf(stderr,"Error: Invalid data register!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
if ((datareg < 0) || (datareg > 0xffff)) {
|
||||
fprintf(stderr,"Error: Data register out of range!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
addrreg = strtol(argv[1+flat], &end, 0);
|
||||
if (*end) {
|
||||
fprintf(stderr, "Error: Invalid address!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
if (addrreg < 0 || addrreg > (flat?0xffff:0x3fff)) {
|
||||
fprintf(stderr,
|
||||
"Error: Address out of range (0x0000-0x%04x)!\n",
|
||||
flat?0xffff:0x3fff);
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if(flat) {
|
||||
value = strtol(argv[3],&end,0);
|
||||
if (*end) {
|
||||
fprintf(stderr,"Error: Invalid addr!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
addr = strtol(argv[3],&end,0);
|
||||
if (*end) {
|
||||
fprintf(stderr,"Error: Invalid addr!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
value = strtol(argv[4],&end,0);
|
||||
if (*end) {
|
||||
fprintf(stderr,"Error: Invalid data!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!flat) {
|
||||
datareg = strtol(argv[2], &end, 0);
|
||||
if (*end) {
|
||||
fprintf(stderr, "Error: Invalid data register!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
if (datareg < 0 || datareg > 0x3fff) {
|
||||
fprintf(stderr, "Error: Data register out of range "
|
||||
"(0x0000-0x3fff)!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (getuid()) {
|
||||
fprintf(stderr,"Error: Can only be run as root (or make it suid root)\n");
|
||||
exit(1);
|
||||
}
|
||||
addr = strtol(argv[3], &end, 0);
|
||||
if (*end) {
|
||||
fprintf(stderr, "Error: Invalid address!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
if (addr < 0 || addr > 0xff) {
|
||||
fprintf(stderr, "Error: Address out of range "
|
||||
"(0x00-0xff)!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr," WARNING! Running this program can cause system crashes, "
|
||||
"data loss and worse!\n");
|
||||
if(flat)
|
||||
fprintf(stderr," I will write address 0x%04x with data 0x%02x\n",
|
||||
addrreg, value);
|
||||
else
|
||||
fprintf(stderr," I will write chip address 0x%04x with data 0x%02x\n"
|
||||
" using address register 0x%04x and "
|
||||
"data register 0x%04x.\n",
|
||||
addr, value, addrreg, datareg);
|
||||
fprintf(stderr," You have five seconds to reconsider and press CTRL-C!\n\n");
|
||||
sleep(5);
|
||||
value = strtol(argv[4-flat], &end, 0);
|
||||
if (*end) {
|
||||
fprintf(stderr, "Error: Invalid value!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
if (value < 0 || value > 0xff) {
|
||||
fprintf(stderr, "Error: Value out of range "
|
||||
"(0x00-0xff)!\n");
|
||||
help();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (getuid()) {
|
||||
fprintf(stderr, "Error: Can only be run as root "
|
||||
"(or make it suid root)\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fprintf(stderr, "WARNING! Running this program can cause "
|
||||
"system crashes, data loss and worse!\n");
|
||||
if (flat)
|
||||
fprintf(stderr, "I will write value 0x%02x to address "
|
||||
"0x%04x\n", value, addrreg);
|
||||
else
|
||||
fprintf(stderr, "I will write value 0x%02x to address "
|
||||
"0x%02x of chip with address register 0x%04x\n"
|
||||
"and data register 0x%04x.\n",
|
||||
value, addr, addrreg, datareg);
|
||||
fprintf(stderr, "You have five seconds to reconsider and press "
|
||||
"CTRL-C!\n\n");
|
||||
sleep(5);
|
||||
|
||||
#ifndef __powerpc__
|
||||
if ((datareg < 0x400) && (addrreg < 0x400) && !flat) {
|
||||
if(ioperm(datareg,1,1)) {
|
||||
fprintf(stderr,"Error: Could not ioperm() data register!\n");
|
||||
exit(1);
|
||||
}
|
||||
if(ioperm(addrreg,1,1)) {
|
||||
fprintf(stderr,"Error: Could not ioperm() address register!\n");
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
if(iopl(3)) {
|
||||
fprintf(stderr,"Error: Could not do iopl(3)!\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (!flat && datareg < 0x400 && addrreg < 0x400) {
|
||||
if (ioperm(datareg, 1, 1)) {
|
||||
fprintf(stderr, "Error: Could not ioperm() data "
|
||||
"register!\n");
|
||||
exit(1);
|
||||
}
|
||||
if (ioperm(addrreg, 1, 1)) {
|
||||
fprintf(stderr, "Error: Could not ioperm() address "
|
||||
"register!\n");
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
if (iopl(3)) {
|
||||
fprintf(stderr, "Error: Could not do iopl(3)!\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* write */
|
||||
if(flat) {
|
||||
outb(value, addrreg);
|
||||
/* write */
|
||||
if (flat) {
|
||||
outb(value, addrreg);
|
||||
} else {
|
||||
outb(addr, addrreg);
|
||||
outb(value,datareg);
|
||||
outb(addr, addrreg);
|
||||
outb(value, datareg);
|
||||
}
|
||||
|
||||
/* readback */
|
||||
if(flat) {
|
||||
res = inb(addrreg);
|
||||
/* readback */
|
||||
if (flat) {
|
||||
res = inb(addrreg);
|
||||
} else {
|
||||
outb(addr, addrreg);
|
||||
res = inb(datareg);
|
||||
outb(addr, addrreg);
|
||||
res = inb(datareg);
|
||||
}
|
||||
|
||||
if(res != value) {
|
||||
fprintf(stderr, "Warning - data mismatch - wrote 0x%.2x, read back 0x%.2x\n", value, res);
|
||||
if (res != value) {
|
||||
fprintf(stderr, "Warning: Data mismatch, wrote 0x%02x, "
|
||||
"read back 0x%02x\n", value, res);
|
||||
} else {
|
||||
fprintf(stderr, "Value 0x%x written, readback matched\n", value);
|
||||
fprintf(stderr, "Value 0x%02x written, readback matched\n",
|
||||
value);
|
||||
}
|
||||
exit(0);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
Reference in new issue
Block a user