]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/aux/vga/io.c
audiohda: fix syntax error
[plan9front.git] / sys / src / cmd / aux / vga / io.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4
5 #include "pci.h"
6 #include "vga.h"
7
8 int curprintindex;
9
10 static int iobfd = -1;
11 static int iowfd = -1;
12 static int iolfd = -1;
13 static int biosfd = -1;
14 static int msrfd = -1;
15
16 enum {
17         Nctlchar        = 256,
18         Nattr           = 16,
19 };
20
21 static int ctlfd = -1;
22 static char ctlbuf[Nctlchar];
23 static int ctlclean;
24
25 static struct {
26         char*   attr;
27         char*   val;
28 } attr[Nattr];
29
30 static int
31 devopen(char* device, int mode)
32 {
33         int fd;
34
35         if((fd = open(device, mode)) < 0)
36                 error("devopen(%s, %d): %r\n", device, mode);
37         return fd;
38 }
39
40 uchar
41 inportb(long port)
42 {
43         uchar data;
44
45         if(iobfd == -1)
46                 iobfd = devopen("#P/iob", ORDWR);
47
48         if(pread(iobfd, &data, sizeof(data), port) != sizeof(data))
49                 error("inportb(0x%4.4lx): %r\n", port);
50         return data;
51 }
52
53 void
54 outportb(long port, uchar data)
55 {
56         if(iobfd == -1)
57                 iobfd = devopen("#P/iob", ORDWR);
58
59         if(pwrite(iobfd, &data, sizeof(data), port) != sizeof(data))
60                 error("outportb(0x%4.4lx, 0x%2.2uX): %r\n", port, data);
61 }
62
63 ushort
64 inportw(long port)
65 {
66         ushort data;
67
68         if(iowfd == -1)
69                 iowfd = devopen("#P/iow", ORDWR);
70
71         if(pread(iowfd, &data, sizeof(data), port) != sizeof(data))
72                 error("inportw(0x%4.4lx): %r\n", port);
73         return data;
74 }
75
76 void
77 outportw(long port, ushort data)
78 {
79         if(iowfd == -1)
80                 iowfd = devopen("#P/iow", ORDWR);
81
82         if(pwrite(iowfd, &data, sizeof(data), port) != sizeof(data))
83                 error("outportw(0x%4.4lx, 0x%2.2uX): %r\n", port, data);
84 }
85
86 ulong
87 inportl(long port)
88 {
89         ulong data;
90
91         if(iolfd == -1)
92                 iolfd = devopen("#P/iol", ORDWR);
93
94         if(pread(iolfd, &data, sizeof(data), port) != sizeof(data))
95                 error("inportl(0x%4.4lx): %r\n", port);
96         return data;
97 }
98
99 void
100 outportl(long port, ulong data)
101 {
102         if(iolfd == -1)
103                 iolfd = devopen("#P/iol", ORDWR);
104
105         if(pwrite(iolfd, &data, sizeof(data), port) != sizeof(data))
106                 error("outportl(0x%4.4lx, 0x%2.2luX): %r\n", port, data);
107 }
108
109 uvlong
110 rdmsr(long port)
111 {
112         uvlong data;
113
114         if(msrfd == -1)
115                 msrfd = devopen("#P/msr", ORDWR);
116
117         if(pread(msrfd, &data, sizeof(data), port) != sizeof(data))
118                 error("rdmsr(0x%4.4lx): %r\n", port);
119         return data;
120 }
121
122 void
123 wrmsr(long port, uvlong data)
124 {
125         if(msrfd == -1)
126                 msrfd = devopen("#P/msr", ORDWR);
127
128         if(pwrite(msrfd, &data, sizeof(data), port) != sizeof(data))
129                 error("wrmsr(0x%4.4lx, 0x%2.2lluX): %r\n", port, data);
130 }
131
132 static void
133 vgactlinit(void)
134 {
135         int nattr;
136         char *nl, *p, *vp;
137
138         if(ctlclean)
139                 return;
140
141         if(ctlfd == -1){
142                 ctlfd = devopen("#v/vgactl", ORDWR);
143                 memset(attr, 0, sizeof(attr));
144         }
145
146         seek(ctlfd, 0, 0);
147         nattr = read(ctlfd, ctlbuf, Nctlchar-1);
148         if(nattr < 0)
149                 error("vgactlr: read: %r\n");
150         ctlbuf[nattr] = 0;
151
152         nattr = 0;
153         vp = ctlbuf;
154         for(nl = strchr(ctlbuf, '\n'); nl; nl = strchr(nl, '\n')){
155
156                 *nl = '\0';
157                 if(p = strchr(vp, ' ')){
158                         *p++ = '\0';
159                         attr[nattr].attr = vp;
160                         if(*p == '\0')
161                                 error("vgactlr: bad format: <%s>\n", vp);
162                         attr[nattr].val = p;
163                 }
164                 else
165                         error("vgactlr: bad format: <%s>\n", vp);
166
167                 if(++nattr >= Nattr-2)
168                         error("vgactlr: too many attributes: %d\n", nattr);
169                 attr[nattr].attr = 0;
170
171                 vp = ++nl;
172         }
173
174         ctlclean = 1;
175 }
176
177 char*
178 vgactlr(char* a, char* v)
179 {
180         int i;
181
182         trace("vgactlr: look for %s\n", a);
183         vgactlinit();
184         for(i = 0; attr[i].attr; i++){
185                 if(strcmp(attr[i].attr, a) == 0){
186                         strcpy(v, attr[i].val);
187                         trace("vgactlr: value %s\n", v);
188                         return v;
189                 }
190         }
191         trace("vgactlr: %s not found\n", a);
192
193         return 0;
194 }
195
196 void
197 vgactlw(char* attr, char* val)
198 {
199         int len;
200         char buf[128];
201
202         if(ctlfd == -1)
203                 ctlfd = devopen("#v/vgactl", ORDWR);
204
205         seek(ctlfd, 0, 0);
206         len = sprint(buf, "%s %s", attr, val);
207         trace("+vgactlw %s\n", buf);
208         if(write(ctlfd, buf, len) != len)
209                 error("vgactlw: <%s>: %r\n",  buf);
210         trace("-vgactlw %s\n", buf);
211
212         ctlclean = 0;
213 }
214
215 void
216 vgactlpci(Pcidev *p)
217 {
218         char buf[64];
219         int len;
220
221         if(p == nil)
222                 return;
223         if(ctlfd == -1)
224                 ctlfd = devopen("#v/vgactl", ORDWR);
225         len = snprint(buf, sizeof(buf), "pcidev %lux", (ulong)p->tbdf);
226         seek(ctlfd, 0, 0);
227         /* ignore error for old kernel */
228         write(ctlfd, buf, len);
229
230         ctlclean = 0;
231 }
232
233 void
234 setpalette(int p, int r, int g, int b)
235 {
236         vgao(PaddrW, p);
237         vgao(Pdata, r);
238         vgao(Pdata, g);
239         vgao(Pdata, b);
240 }
241
242 static long
243 doreadbios(char* buf, long len, long offset)
244 {
245         if(biosfd < 0)
246                 biosfd = open("/dev/realmodemem", OREAD);
247         if(biosfd < 0)
248                 biosfd = open("#P/realmodemem", OREAD);
249         if(biosfd < 0)
250                 return -1;
251         seek(biosfd, offset, 0);
252         return read(biosfd, buf, len);
253 }
254
255 char*
256 readbios(long len, long offset)
257 {
258         static char bios[0x10000];
259         static long biosoffset;
260         static long bioslen;
261         int n;
262
263         if(biosoffset <= offset && offset+len <= biosoffset+bioslen)
264                 return bios+(offset - biosoffset);
265
266         if(len > sizeof(bios))
267                 error("enormous bios len %ld at %lux\n", len, offset);
268
269         n = doreadbios(bios, sizeof(bios), offset);
270         if(n < len)
271                 error("short bios read %ld at %lux got %d\n", len,offset, n);
272
273         biosoffset = offset;
274         bioslen = n;
275         return bios;
276 }
277
278 void
279 dumpbios(long size)
280 {
281         uchar *buf;
282         long offset;
283         int i, n;
284         char c;
285
286         buf = alloc(size);
287         offset = 0xC0000;
288         if(doreadbios((char*)buf, size, offset) != size)
289                 error("short bios read in dumpbios\n");
290
291         if(buf[0] != 0x55 || buf[1] != 0xAA){
292                 offset = 0xE0000;
293                 if(doreadbios((char*)buf, size, offset) != size)
294                         error("short bios read in dumpbios\n");
295                 if(buf[0] != 0x55 || buf[1] != 0xAA){
296                         free(buf);
297                         return;
298                 }
299         }
300
301         for(i = 0; i < size; i += 16){
302                 Bprint(&stdout, "0x%luX", offset+i);
303                 for(n = 0; n < 16; n++)
304                         Bprint(&stdout, " %2.2uX", buf[i+n]);
305                 Bprint(&stdout, "  ");
306                 for(n = 0; n < 16; n++){
307                         c = buf[i+n];
308                         if(c < 0x20 || c >= 0x7F)
309                                 c = '.';
310                         Bprint(&stdout, "%c", c);
311                 }
312                 Bprint(&stdout, "\n");
313         }
314         free(buf);
315 }
316
317 void*
318 alloc(ulong nbytes)
319 {
320         void *v;
321
322         if((v = malloc(nbytes)) == 0)
323                 error("alloc: %lud bytes - %r\n", nbytes);
324
325         return memset(v, 0, nbytes);
326 }
327
328 void
329 printitem(char* ctlr, char* item)
330 {
331         int n;
332
333         if(curprintindex){
334                 curprintindex = 0;
335                 Bprint(&stdout, "\n");
336         }
337
338         n = 0;
339         if(ctlr && *ctlr)
340                 n = Bprint(&stdout, "%s ", ctlr);
341         Bprint(&stdout, "%-*s", 20-n, item);
342 }
343
344 void
345 printreg(ulong data)
346 {
347         int width;
348
349         width = 3;
350         if((curprintindex % 16) == 0 && curprintindex){
351                 Bprint(&stdout, "\n");
352                 curprintindex = 0;
353                 width = 23;
354         }
355         if(curprintindex == 8)
356                 Bprint(&stdout, " -");
357         Bprint(&stdout, "%*.2luX", width, data);
358         curprintindex++;
359 }
360
361 static char *flagname[32] = {
362         [0x00]  "Fsnarf",
363         [0x01]  "Foptions",
364         [0x02]  "Finit",
365         [0x03]  "Fload",
366         [0x04]  "Fdump",
367
368         [0x08]  "Hpclk2x8",
369         [0x09]  "Upclk2x8",
370         [0x0A]  "Henhanced",
371         [0x0B]  "Uenhanced",
372         [0x0C]  "Hpvram",
373         [0x0D]  "Upvram",
374         [0x0E]  "Hextsid",
375         [0x0F]  "Uextsid",
376         [0x10]  "Hclk2",
377         [0x11]  "Uclk2",
378         [0x12]  "Hlinear",
379         [0x13]  "Ulinear",
380         [0x14]  "Hclkdiv",
381         [0x15]  "Uclkdiv",
382         [0x16]  "Hsid32",
383 };
384
385 void
386 printflag(ulong flag)
387 {
388         int i;
389         char first;
390
391         first = ' ';
392         for(i = 31; i >= 0; i--){
393                 if((flag & (1<<i)) == 0)
394                         continue;
395                 if(flagname[i])
396                         Bprint(&stdout, "%c%s", first, flagname[i]);
397                 else
398                         Bprint(&stdout, "%c0x%x", first, 1<<i);
399                 first = '|';
400         }
401 }