1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/*
* pcitweak.c
*
* Copyright 1999 by The XFree86 Project, Inc.
*
* Author: David Dawes <dawes@xfree86.org>
*/
/* $XFree86: xc/programs/Xserver/hw/xfree86/etc/pcitweak.c,v 1.14 2000/08/10 17:40:34 dawes Exp $ */
#include "X.h"
#include "os.h"
#include "xf86.h"
#include "xf86Priv.h"
#include "xf86_OSproc.h"
#include "xf86Pci.h"
#ifdef __CYGWIN__
#include <getopt.h>
#endif
#include <stdarg.h>
#include <stdlib.h>
#ifdef __linux__
/* to get getopt on Linux */
#ifndef __USE_POSIX2
#define __USE_POSIX2
#endif
#endif
#include <unistd.h>
#if defined(ISC) || defined(Lynx)
extern char *optarg;
extern int optind, opterr;
#endif
static void usage(void);
static Bool parsePciBusString(const char *id, int *bus, int *device, int *func);
static char *myname = NULL;
int
main(int argc, char *argv[])
{
char c;
PCITAG tag;
int bus, device, func;
Bool list = FALSE, rd = FALSE, wr = FALSE;
Bool byte = FALSE, halfword = FALSE;
int offset = 0;
CARD32 value = 0;
char *id = NULL, *end;
myname = argv[0];
while ((c = getopt(argc, argv, "bhlr:w:")) != -1) {
switch (c) {
case 'b':
byte = TRUE;
break;
case 'h':
halfword = TRUE;
break;
case 'l':
list = TRUE;
break;
case 'r':
rd = TRUE;
id = optarg;
break;
case 'w':
wr = TRUE;
id = optarg;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (list) {
xf86Verbose = 2;
xf86EnableIO();
xf86scanpci(0);
xf86DisableIO();
exit(0);
}
if (rd && wr)
usage();
if (wr && argc != 2)
usage();
if (rd && argc != 1)
usage();
if (byte && halfword)
usage();
if (rd || wr) {
if (!parsePciBusString(id, &bus, &device, &func)) {
fprintf(stderr, "%s: Bad PCI ID string\n", myname);
usage();
}
offset = strtoul(argv[0], &end, 0);
if (*end != '\0') {
fprintf(stderr, "%s: Bad offset\n", myname);
usage();
}
if (halfword) {
if (offset % 2) {
fprintf(stderr, "%s: offset must be a multiple of two\n",
myname);
exit(1);
}
} else if (!byte) {
if (offset % 4) {
fprintf(stderr, "%s: offset must be a multiple of four\n",
myname);
exit(1);
}
}
} else {
usage();
}
if (wr) {
value = strtoul(argv[1], &end, 0);
if (*end != '\0') {
fprintf(stderr, "%s: Bad value\n", myname);
usage();
}
}
xf86EnableIO();
/*
* This is needed to setup all the buses. Otherwise secondary buses
* can't be accessed.
*/
xf86scanpci(0);
tag = pciTag(bus, device, func);
if (rd) {
if (byte) {
printf("0x%02x\n", (unsigned int)pciReadByte(tag, offset) & 0xFF);
} else if (halfword) {
printf("0x%04x\n", (unsigned int)pciReadWord(tag, offset) & 0xFFFF);
} else {
printf("0x%08lx\n", (unsigned long)pciReadLong(tag, offset));
}
} else if (wr) {
if (byte) {
pciWriteByte(tag, offset, value & 0xFF);
} else if (halfword) {
pciWriteWord(tag, offset, value & 0xFFFF);
} else {
pciWriteLong(tag, offset, value);
}
}
xf86DisableIO();
exit(0);
}
static void
usage()
{
fprintf(stderr, "usage:\tpcitweak -l\n"
"\tpcitweak -r ID [-b | -h] offset\n"
"\tpcitweak -w ID [-b | -h] offset value\n"
"\n"
"\t\t-l -- list\n"
"\t\t-r -- read\n"
"\t\t-w -- write\n"
"\t\t-b -- read/write a single byte\n"
"\t\t-h -- read/write a single halfword (16 bit)\n"
"\t\tID -- PCI ID string in form bus:dev:func "
"(all in hex)\n");
exit(1);
}
Bool
parsePciBusString(const char *busID, int *bus, int *device, int *func)
{
/*
* The format is assumed to be "bus:device:func", where bus, device
* and func are hexadecimal integers. func may be omitted and assumed to
* be zero, although it doing this isn't encouraged.
*/
char *p, *s, *end;
s = strdup(busID);
p = strtok(s, ":");
if (p == NULL || *p == 0)
return FALSE;
*bus = strtoul(p, &end, 16);
if (*end != '\0')
return FALSE;
p = strtok(NULL, ":");
if (p == NULL || *p == 0)
return FALSE;
*device = strtoul(p, &end, 16);
if (*end != '\0')
return FALSE;
*func = 0;
p = strtok(NULL, ":");
if (p == NULL || *p == 0)
return TRUE;
*func = strtoul(p, &end, 16);
if (*end != '\0')
return FALSE;
return TRUE;
}
#include "xf86getpagesize.c"
|