OSDN Git Service

Initial lollipop-x86 porting
[android-x86/device-generic-common.git] / io_switch / io_switch.c
1 /*
2  * Copyright (C) 2011 The Android-x86 Open Source Project
3  *
4  * by Chih-Wei Huang <cwhuang@linux.org.tw>
5  *
6  * Licensed under GPLv2 or later
7  *
8  */
9
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16
17 #define IO_SHIFT        0x68
18 #define IO_VALUE        0x62
19
20 void usage(int invalid)
21 {
22         if (invalid)
23                 fprintf(stderr, "Invalid parameter\n");
24         fprintf(stderr, "Usage : io_switch 0x?? 0x??\n");
25         exit(-1);
26 }
27
28 int main(int argc, char *argv[])
29 {
30         int fd, addr;
31         char val;
32
33         if (argc < 3)
34                 usage(0);
35
36         if (sscanf(argv[1], "0x%x", &addr) <= 0)
37                 usage(1);
38
39         if (sscanf(argv[2], "0x%hhx", &val) <= 0)
40                 usage(1);
41
42         printf("Writing 0x%x : 0x%hhx\n", addr, val);
43
44         fd = open("/dev/port", O_WRONLY);
45         if (fd < 0) {
46                 fprintf(stderr, "Open file failed\n");
47         } else {
48                 val += IO_VALUE;
49                 lseek(fd, addr + IO_SHIFT, SEEK_SET);
50                 write(fd, &val, 1);
51                 close(fd);
52         }
53
54         return (fd < 0);
55 }