]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/scram.c
ip/torrent: remove unneeded assignment
[plan9front.git] / sys / src / cmd / scram.c
1 #include <u.h>
2 #include </386/include/ureg.h>
3 #include <libc.h>
4 #include <aml.h>
5
6 int fd, iofd;
7 struct Ureg u;
8 ulong PM1a_CNT_BLK, PM1b_CNT_BLK, SLP_TYPa, SLP_TYPb;
9 enum {
10         SLP_EN = 0x2000,
11         SLP_TM = 0x1c00,
12 };
13
14 typedef struct Tbl Tbl;
15 struct Tbl {
16         uchar   sig[4];
17         uchar   len[4];
18         uchar   rev;
19         uchar   csum;
20         uchar   oemid[6];
21         uchar   oemtid[8];
22         uchar   oemrev[4];
23         uchar   cid[4];
24         uchar   crev[4];
25         uchar   data[];
26 };
27
28 enum {
29         Tblsz   = 4+4+1+1+6+8+4+4+4,
30 };
31
32 static ulong
33 get32(uchar *p){
34         return p[3]<<24 | p[2]<<16 | p[1]<<8 | p[0];
35 }
36
37 void
38 apm(void)
39 {
40         seek(fd, 0, 0);
41         if(write(fd, &u, sizeof u) < 0)
42                 sysfatal("write: %r");
43         seek(fd, 0, 0);
44         if(read(fd, &u, sizeof u) < 0)
45                 sysfatal("read: %r");
46         if(u.flags & 1)
47                 sysfatal("apm: %lux", (u.ax>>8) & 0xFF);
48 }
49
50 int
51 loadacpi(void)
52 {
53         void *r, **rr;
54         ulong l;
55         Tbl *t;
56         int n;
57
58         amlinit();
59         for(;;){
60                 t = malloc(sizeof(*t));
61                 if((n = readn(fd, t, Tblsz)) <= 0)
62                         break;
63                 if(n != Tblsz)
64                         return -1;
65                 l = get32(t->len);
66                 if(l < Tblsz)
67                         return -1;
68                 l -= Tblsz;
69                 t = realloc(t, sizeof(*t) + l);
70                 if(readn(fd, t->data, l) != l)
71                         return -1;
72                 if(memcmp("DSDT", t->sig, 4) == 0)
73                         amlload(t->data, l);
74                 else if(memcmp("SSDT", t->sig, 4) == 0)
75                         amlload(t->data, l);
76                 else if(memcmp("FACP", t->sig, 4) == 0){
77                         PM1a_CNT_BLK = get32(((uchar*)t) + 64);
78                         PM1b_CNT_BLK = get32(((uchar*)t) + 68);
79                 }
80         }
81         if(amleval(amlwalk(amlroot, "_S5"), "", &r) < 0)
82                 return -1;
83         if(amltag(r) != 'p' || amllen(r) < 2)
84                 return -1;
85         rr = amlval(r);
86         SLP_TYPa = amlint(rr[0]);
87         SLP_TYPb = amlint(rr[1]);
88         return 0;
89 }
90
91 void
92 outw(long addr, ushort val)
93 {
94         uchar buf[2];
95
96         if(addr == 0)
97                 return;
98         buf[0] = val;
99         buf[1] = val >> 8;
100         pwrite(iofd, buf, 2, addr);
101 }
102
103 void
104 wirecpu0(void)
105 {
106         char buf[128];
107         int ctl;
108
109         snprint(buf, sizeof(buf), "/proc/%d/ctl", getpid());
110         if((ctl = open(buf, OWRITE)) < 0){
111                 snprint(buf, sizeof(buf), "#p/%d/ctl", getpid());
112                 if((ctl = open(buf, OWRITE)) < 0)
113                         return;
114         }
115         write(ctl, "wired 0", 7);
116         close(ctl);
117 }
118
119 void
120 main()
121 {
122         wirecpu0();
123
124         if((fd = open("/dev/apm", ORDWR)) < 0)
125                 if((fd = open("#P/apm", ORDWR)) < 0)
126                         goto tryacpi;
127
128         u.ax = 0x530E;
129         u.bx = 0x0000;
130         u.cx = 0x0102;
131         apm();
132         u.ax = 0x5307;
133         u.bx = 0x0001;
134         u.cx = 0x0003;
135         apm();
136         
137 tryacpi:
138         if((fd = open("/dev/acpitbls", OREAD)) < 0)
139                 if((fd = open("#P/acpitbls", OREAD)) < 0)
140                         goto fail;
141         if((iofd = open("/dev/iow", OWRITE)) < 0)
142                 if((iofd = open("#P/iow", OWRITE)) < 0)
143                         goto fail;
144         if(loadacpi() < 0)
145                 goto fail;
146
147         outw(PM1a_CNT_BLK, ((SLP_TYPa << 10) & SLP_TM) | SLP_EN);
148         outw(PM1b_CNT_BLK, ((SLP_TYPb << 10) & SLP_TM) | SLP_EN);
149         sleep(100);
150
151         /*
152          * The SetSystemSleeping() example from the ACPI spec 
153          * writes the same value in both registers. But Linux/BSD
154          * write distinct values from the _Sx package (like the
155          * code above). The _S5 package on a HP DC5700 is
156          * Package(0x2){0x0, 0x7} and writing SLP_TYPa of 0 to
157          * PM1a_CNT_BLK seems to have no effect but 0x7 seems
158          * to work fine. So trying the following as a last effort.
159          */
160         SLP_TYPa |= SLP_TYPb;
161         outw(PM1a_CNT_BLK, ((SLP_TYPa << 10) & SLP_TM) | SLP_EN);
162         outw(PM1b_CNT_BLK, ((SLP_TYPa << 10) & SLP_TM) | SLP_EN);
163         sleep(100);
164
165 fail:
166         exits("scram");
167 }