123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/mman.h>
- #include <asm/page.h>
- #include <sys/sysctl.h>
- #include <linux/version.h>
- #define PATH_ARM_SYSTYPE "/etc/arm_systype"
- #define PATH_CPUINFO "/proc/cpuinfo"
- #define MAX_PORT 0x10000
- static struct {
- unsigned long int base;
- unsigned long int io_base;
- unsigned int shift;
- unsigned int initdone;
- } io;
- #define IO_BASE_FOOTBRIDGE 0x7c000000
- #define IO_SHIFT_FOOTBRIDGE 0
- static struct platform {
- const char *name;
- unsigned long int io_base;
- unsigned int shift;
- } platform[] = {
-
- {"Chalice-CATS", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
- {"DEC-EBSA285", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
- {"Corel-NetWinder", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
- {"Rebel-NetWinder", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
- };
- #define IO_ADDR(port) (io.base + ((port) << io.shift))
- static int
- init_iosys (void)
- {
- char systype[256];
- int i, n;
-
- #if LINUX_VERSION_CODE < 132119
- static int iobase_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_BASE };
- static int ioshift_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_SHIFT };
- #else
- static int iobase_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_BASE };
- static int ioshift_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_SHIFT };
- #endif
- size_t len = sizeof(io.base);
- if (! sysctl (iobase_name, 3, &io.io_base, &len, NULL, 0)
- && ! sysctl (ioshift_name, 3, &io.shift, &len, NULL, 0)) {
- io.initdone = 1;
- return 0;
- }
- n = readlink (PATH_ARM_SYSTYPE, systype, sizeof (systype) - 1);
- if (n > 0) {
- systype[n] = '\0';
- if (isdigit (systype[0])) {
- if (sscanf (systype, "%li,%i", &io.io_base, &io.shift) == 2) {
- io.initdone = 1;
- return 0;
- }
-
- }
- }
- else {
- FILE * fp;
- fp = fopen (PATH_CPUINFO, "r");
- if (! fp)
- return -1;
- while ((n = fscanf (fp, "Hardware\t: %256[^\n]\n", systype)) != EOF) {
- if (n == 1)
- break;
- else
- fgets (systype, 256, fp);
- }
- fclose (fp);
- if (n == EOF) {
-
- fprintf (stderr, "ioperm: Unable to determine system type.\n"
- "\t(May need " PATH_ARM_SYSTYPE " symlink?)\n");
- __set_errno (ENODEV);
- return -1;
- }
- }
-
- for (i = 0; i < sizeof (platform) / sizeof (platform[0]); ++i) {
- if (strcmp (platform[i].name, systype) == 0) {
- io.shift = platform[i].shift;
- io.io_base = platform[i].io_base;
- io.initdone = 1;
- return 0;
- }
- }
-
- __set_errno (EINVAL);
- return -1;
- }
- int ioperm (unsigned long int from, unsigned long int num, int turn_on)
- {
- if (! io.initdone && init_iosys () < 0)
- return -1;
-
- if (from >= MAX_PORT || from + num > MAX_PORT) {
- __set_errno (EINVAL);
- return -1;
- }
- if (turn_on) {
- if (! io.base) {
- int fd;
- fd = open ("/dev/mem", O_RDWR);
- if (fd < 0)
- return -1;
- io.base = (unsigned long int) mmap (0, MAX_PORT << io.shift,
- PROT_READ | PROT_WRITE,
- MAP_SHARED, fd, io.io_base);
- close (fd);
- if ((long) io.base == -1)
- return -1;
- }
- }
- return 0;
- }
- void
- outb(unsigned char b, unsigned long int port)
- {
- *((volatile unsigned char *)(IO_ADDR (port))) = b;
- }
- void
- outw(unsigned short b, unsigned long int port)
- {
- *((volatile unsigned short *)(IO_ADDR (port))) = b;
- }
- void
- outl(unsigned int b, unsigned long int port)
- {
- *((volatile unsigned long *)(IO_ADDR (port))) = b;
- }
- unsigned int
- inb (unsigned long int port)
- {
- return *((volatile unsigned char *)(IO_ADDR (port)));
- }
- unsigned int
- inw(unsigned long int port)
- {
- return *((volatile unsigned short *)(IO_ADDR (port)));
- }
- unsigned int
- inl(unsigned long int port)
- {
- return *((volatile unsigned long *)(IO_ADDR (port)));
- }
|