]>
Commit | Line | Data |
---|---|---|
e4842ff3 | 1 | /* |
e4842ff3 MM |
2 | * Linux PCI Utilities -- Manipulate PCI Configuration Registers |
3 | * | |
a27a33dd | 4 | * Copyright (c) 1998 Martin Mares <mj@ucw.cz> |
e4842ff3 MM |
5 | * |
6 | * Can be freely distributed and used under the terms of the GNU GPL. | |
7 | */ | |
8 | ||
9 | #include <stdio.h> | |
10 | #include <string.h> | |
11 | #include <stdlib.h> | |
727ce158 | 12 | #include <stdarg.h> |
e4842ff3 | 13 | #include <unistd.h> |
7aad822b | 14 | |
e4842ff3 MM |
15 | #include "pciutils.h" |
16 | ||
17 | static int force; /* Don't complain if no devices match */ | |
18 | static int verbose; /* Verbosity level */ | |
b2c9b373 | 19 | static int demo_mode; /* Only show */ |
e4842ff3 | 20 | |
727ce158 | 21 | static struct pci_access *pacc; |
e4842ff3 MM |
22 | |
23 | struct op { | |
24 | struct op *next; | |
727ce158 | 25 | struct pci_dev **dev_vector; |
e4842ff3 MM |
26 | unsigned int addr; |
27 | unsigned int width; /* Byte width of the access */ | |
28 | int num_values; /* Number of values to write; <0=read */ | |
29 | unsigned int values[0]; | |
30 | }; | |
31 | ||
32 | static struct op *first_op, **last_op = &first_op; | |
33 | ||
727ce158 | 34 | static struct pci_dev ** |
e4842ff3 MM |
35 | select_devices(struct pci_filter *filt) |
36 | { | |
727ce158 | 37 | struct pci_dev *z, **a, **b; |
e4842ff3 MM |
38 | int cnt = 1; |
39 | ||
727ce158 MM |
40 | for(z=pacc->devices; z; z=z->next) |
41 | if (pci_filter_match(filt, z)) | |
e4842ff3 MM |
42 | cnt++; |
43 | a = b = xmalloc(sizeof(struct device *) * cnt); | |
727ce158 MM |
44 | for(z=pacc->devices; z; z=z->next) |
45 | if (pci_filter_match(filt, z)) | |
e4842ff3 MM |
46 | *a++ = z; |
47 | *a = NULL; | |
48 | return b; | |
49 | } | |
50 | ||
51 | static void | |
727ce158 | 52 | exec_op(struct op *op, struct pci_dev *dev) |
e4842ff3 MM |
53 | { |
54 | char *mm[] = { NULL, "%02x", "%04x", NULL, "%08x" }; | |
55 | char *m = mm[op->width]; | |
b2c9b373 | 56 | unsigned int x; |
33bc28a5 | 57 | int i, addr; |
e4842ff3 MM |
58 | |
59 | if (verbose) | |
727ce158 | 60 | printf("%02x:%02x.%x:%02x", dev->bus, dev->dev, dev->func, op->addr); |
33bc28a5 | 61 | addr = op->addr; |
b2c9b373 MM |
62 | if (op->num_values >= 0) |
63 | for(i=0; i<op->num_values; i++) | |
64 | { | |
65 | if (verbose) | |
66 | { | |
67 | putchar(' '); | |
68 | printf(m, op->values[i]); | |
69 | } | |
70 | if (demo_mode) | |
71 | continue; | |
72 | switch (op->width) | |
73 | { | |
74 | case 1: | |
33bc28a5 | 75 | pci_write_byte(dev, addr, op->values[i]); |
b2c9b373 MM |
76 | break; |
77 | case 2: | |
33bc28a5 | 78 | pci_write_word(dev, addr, op->values[i]); |
b2c9b373 MM |
79 | break; |
80 | default: | |
33bc28a5 | 81 | pci_write_long(dev, addr, op->values[i]); |
b2c9b373 MM |
82 | break; |
83 | } | |
33bc28a5 | 84 | addr += op->width; |
b2c9b373 | 85 | } |
e4842ff3 MM |
86 | else |
87 | { | |
88 | if (verbose) | |
b2c9b373 MM |
89 | printf(" = "); |
90 | if (!demo_mode) | |
91 | { | |
92 | switch (op->width) | |
93 | { | |
94 | case 1: | |
33bc28a5 | 95 | x = pci_read_byte(dev, addr); |
b2c9b373 MM |
96 | break; |
97 | case 2: | |
33bc28a5 | 98 | x = pci_read_word(dev, addr); |
b2c9b373 MM |
99 | break; |
100 | default: | |
33bc28a5 | 101 | x = pci_read_long(dev, addr); |
b2c9b373 MM |
102 | break; |
103 | } | |
b2c9b373 MM |
104 | printf(m, x); |
105 | } | |
106 | else | |
107 | putchar('?'); | |
e4842ff3 | 108 | } |
b2c9b373 | 109 | putchar('\n'); |
e4842ff3 MM |
110 | } |
111 | ||
112 | static void | |
113 | execute(struct op *op) | |
114 | { | |
727ce158 MM |
115 | struct pci_dev **vec = NULL; |
116 | struct pci_dev **pdev, *dev; | |
e4842ff3 MM |
117 | struct op *oops; |
118 | ||
119 | while (op) | |
120 | { | |
121 | pdev = vec = op->dev_vector; | |
122 | while (dev = *pdev++) | |
123 | for(oops=op; oops && oops->dev_vector == vec; oops=oops->next) | |
124 | exec_op(oops, dev); | |
125 | while (op && op->dev_vector == vec) | |
126 | op = op->next; | |
127 | } | |
128 | } | |
129 | ||
b2c9b373 MM |
130 | static void |
131 | scan_ops(struct op *op) | |
132 | { | |
b2c9b373 MM |
133 | while (op) |
134 | { | |
135 | if (op->num_values >= 0) | |
727ce158 | 136 | pacc->writeable = 1; |
b2c9b373 MM |
137 | op = op->next; |
138 | } | |
139 | } | |
140 | ||
141 | struct reg_name { | |
142 | int offset; | |
143 | int width; | |
144 | char *name; | |
145 | }; | |
146 | ||
147 | static struct reg_name pci_reg_names[] = { | |
148 | { 0x00, 2, "VENDOR_ID", }, | |
149 | { 0x02, 2, "DEVICE_ID", }, | |
150 | { 0x04, 2, "COMMAND", }, | |
151 | { 0x06, 2, "STATUS", }, | |
152 | { 0x08, 1, "REVISION", }, | |
153 | { 0x09, 1, "CLASS_PROG", }, | |
154 | { 0x0a, 2, "CLASS_DEVICE", }, | |
155 | { 0x0c, 1, "CACHE_LINE_SIZE", }, | |
156 | { 0x0d, 1, "LATENCY_TIMER", }, | |
157 | { 0x0e, 1, "HEADER_TYPE", }, | |
158 | { 0x0f, 1, "BIST", }, | |
159 | { 0x10, 4, "BASE_ADDRESS_0", }, | |
160 | { 0x14, 4, "BASE_ADDRESS_1", }, | |
161 | { 0x18, 4, "BASE_ADDRESS_2", }, | |
162 | { 0x1c, 4, "BASE_ADDRESS_3", }, | |
163 | { 0x20, 4, "BASE_ADDRESS_4", }, | |
164 | { 0x24, 4, "BASE_ADDRESS_5", }, | |
165 | { 0x28, 4, "CARDBUS_CIS", }, | |
166 | { 0x2c, 4, "SUBSYSTEM_VENDOR_ID", }, | |
167 | { 0x2e, 2, "SUBSYSTEM_ID", }, | |
168 | { 0x30, 4, "ROM_ADDRESS", }, | |
169 | { 0x3c, 1, "INTERRUPT_LINE", }, | |
170 | { 0x3d, 1, "INTERRUPT_PIN", }, | |
171 | { 0x3e, 1, "MIN_GNT", }, | |
172 | { 0x3f, 1, "MAX_LAT", }, | |
173 | { 0x18, 1, "PRIMARY_BUS", }, | |
174 | { 0x19, 1, "SECONDARY_BUS", }, | |
175 | { 0x1a, 1, "SUBORDINATE_BUS", }, | |
176 | { 0x1b, 1, "SEC_LATENCY_TIMER", }, | |
177 | { 0x1c, 1, "IO_BASE", }, | |
178 | { 0x1d, 1, "IO_LIMIT", }, | |
179 | { 0x1e, 2, "SEC_STATUS", }, | |
180 | { 0x20, 2, "MEMORY_BASE", }, | |
181 | { 0x22, 2, "MEMORY_LIMIT", }, | |
182 | { 0x24, 2, "PREF_MEMORY_BASE", }, | |
183 | { 0x26, 2, "PREF_MEMORY_LIMIT", }, | |
184 | { 0x28, 4, "PREF_BASE_UPPER32", }, | |
185 | { 0x2c, 4, "PREF_LIMIT_UPPER32", }, | |
186 | { 0x30, 2, "IO_BASE_UPPER16", }, | |
187 | { 0x32, 2, "IO_LIMIT_UPPER16", }, | |
188 | { 0x38, 4, "BRIDGE_ROM_ADDRESS", }, | |
189 | { 0x3e, 2, "BRIDGE_CONTROL", }, | |
190 | { 0x10, 4, "CB_CARDBUS_BASE", }, | |
191 | { 0x14, 2, "CB_CAPABILITIES", }, | |
192 | { 0x16, 2, "CB_SEC_STATUS", }, | |
193 | { 0x18, 1, "CB_BUS_NUMBER", }, | |
194 | { 0x19, 1, "CB_CARDBUS_NUMBER", }, | |
195 | { 0x1a, 1, "CB_SUBORDINATE_BUS", }, | |
196 | { 0x1b, 1, "CB_CARDBUS_LATENCY", }, | |
197 | { 0x1c, 4, "CB_MEMORY_BASE_0", }, | |
198 | { 0x20, 4, "CB_MEMORY_LIMIT_0", }, | |
199 | { 0x24, 4, "CB_MEMORY_BASE_1", }, | |
200 | { 0x28, 4, "CB_MEMORY_LIMIT_1", }, | |
201 | { 0x2c, 2, "CB_IO_BASE_0", }, | |
202 | { 0x2e, 2, "CB_IO_BASE_0_HI", }, | |
203 | { 0x30, 2, "CB_IO_LIMIT_0", }, | |
204 | { 0x32, 2, "CB_IO_LIMIT_0_HI", }, | |
205 | { 0x34, 2, "CB_IO_BASE_1", }, | |
206 | { 0x36, 2, "CB_IO_BASE_1_HI", }, | |
207 | { 0x38, 2, "CB_IO_LIMIT_1", }, | |
208 | { 0x3a, 2, "CB_IO_LIMIT_1_HI", }, | |
209 | { 0x40, 2, "CB_SUBSYSTEM_VENDOR_ID", }, | |
210 | { 0x42, 2, "CB_SUBSYSTEM_ID", }, | |
211 | { 0x44, 4, "CB_LEGACY_MODE_BASE", }, | |
212 | { 0x00, 0, NULL } | |
213 | }; | |
214 | ||
e4842ff3 MM |
215 | static void usage(void) __attribute__((noreturn)); |
216 | ||
217 | static void | |
218 | usage(void) | |
219 | { | |
220 | fprintf(stderr, | |
727ce158 | 221 | "Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n\ |
b2c9b373 MM |
222 | -f\t\tDon't complain if there's nothing to do\n\ |
223 | -v\t\tBe verbose\n\ | |
727ce158 MM |
224 | -D\t\tList changes, don't commit them\n" |
225 | GENERIC_HELP | |
226 | "<device>:\t-s [[<bus>]:][<slot>][.[<func>]]\n\ | |
b2c9b373 MM |
227 | \t|\t-d [<vendor>]:[<device>]\n\ |
228 | <reg>:\t\t<number>[.(B|W|L)]\n\ | |
229 | |\t\t<name>\n\ | |
230 | <values>:\t<value>[,<value>...]\n\ | |
e4842ff3 MM |
231 | "); |
232 | exit(1); | |
233 | } | |
234 | ||
235 | int | |
236 | main(int argc, char **argv) | |
237 | { | |
238 | enum { STATE_INIT, STATE_GOT_FILTER, STATE_GOT_OP } state = STATE_INIT; | |
239 | struct pci_filter filter; | |
727ce158 MM |
240 | struct pci_dev **selected_devices = NULL; |
241 | char *opts = GENERIC_OPTIONS ; | |
e4842ff3 | 242 | |
7aad822b MM |
243 | if (argc == 2 && !strcmp(argv[1], "--version")) |
244 | { | |
245 | puts("setpci version " PCIUTILS_VERSION); | |
246 | return 0; | |
247 | } | |
e4842ff3 MM |
248 | argc--; |
249 | argv++; | |
727ce158 MM |
250 | |
251 | pacc = pci_alloc(); | |
252 | pacc->error = die; | |
253 | ||
e4842ff3 MM |
254 | while (argc && argv[0][0] == '-') |
255 | { | |
256 | char *c = argv[0]+1; | |
257 | char *d = c; | |
727ce158 | 258 | char *e; |
e4842ff3 MM |
259 | while (*c) |
260 | switch (*c) | |
261 | { | |
262 | case 'v': | |
263 | verbose++; | |
264 | c++; | |
265 | break; | |
266 | case 'f': | |
267 | force++; | |
268 | c++; | |
269 | break; | |
b2c9b373 MM |
270 | case 'D': |
271 | demo_mode++; | |
272 | c++; | |
273 | break; | |
e4842ff3 MM |
274 | case 0: |
275 | break; | |
276 | default: | |
727ce158 MM |
277 | if (e = strchr(opts, *c)) |
278 | { | |
279 | char *arg; | |
280 | c++; | |
281 | if (e[1] == ':') | |
282 | { | |
283 | if (*c) | |
284 | arg = c; | |
285 | else if (argc > 1) | |
286 | { | |
287 | arg = argv[1]; | |
288 | argc--; argv++; | |
289 | } | |
290 | else | |
291 | usage(); | |
292 | c = ""; | |
293 | } | |
294 | else | |
295 | arg = NULL; | |
296 | if (!parse_generic_option(*e, pacc, arg)) | |
297 | usage(); | |
298 | } | |
299 | else | |
300 | { | |
301 | if (c != d) | |
302 | usage(); | |
303 | goto next; | |
304 | } | |
e4842ff3 MM |
305 | } |
306 | argc--; | |
307 | argv++; | |
308 | } | |
309 | next: | |
310 | ||
727ce158 MM |
311 | pci_init(pacc); |
312 | pci_scan_bus(pacc); | |
e4842ff3 MM |
313 | |
314 | while (argc) | |
315 | { | |
316 | char *c = argv[0]; | |
317 | char *d, *e, *f; | |
318 | int n, i; | |
319 | struct op *op; | |
320 | unsigned long ll, lim; | |
321 | ||
322 | if (*c == '-') | |
323 | { | |
324 | if (!c[1] || !strchr("sd", c[1])) | |
325 | usage(); | |
326 | if (c[2]) | |
327 | d = (c[2] == '=') ? c+3 : c+2; | |
183251d7 | 328 | else if (argc > 1) |
e4842ff3 MM |
329 | { |
330 | argc--; | |
331 | argv++; | |
332 | d = argv[0]; | |
333 | } | |
334 | else | |
335 | usage(); | |
336 | if (state != STATE_GOT_FILTER) | |
337 | { | |
727ce158 | 338 | pci_filter_init(pacc, &filter); |
e4842ff3 MM |
339 | state = STATE_GOT_FILTER; |
340 | } | |
341 | switch (c[1]) | |
342 | { | |
343 | case 's': | |
727ce158 MM |
344 | if (d = pci_filter_parse_slot(&filter, d)) |
345 | die("-s: %s", d); | |
e4842ff3 MM |
346 | break; |
347 | case 'd': | |
727ce158 MM |
348 | if (d = pci_filter_parse_id(&filter, d)) |
349 | die("-d: %s", d); | |
e4842ff3 MM |
350 | break; |
351 | default: | |
352 | usage(); | |
353 | } | |
354 | } | |
355 | else if (state == STATE_INIT) | |
356 | usage(); | |
357 | else | |
358 | { | |
359 | if (state == STATE_GOT_FILTER) | |
360 | selected_devices = select_devices(&filter); | |
361 | if (!selected_devices[0] && !force) | |
362 | fprintf(stderr, "setpci: Warning: No devices selected for `%s'.\n", c); | |
363 | state = STATE_GOT_OP; | |
364 | d = strchr(c, '='); | |
365 | if (d) | |
366 | { | |
367 | *d++ = 0; | |
b2c9b373 MM |
368 | if (!*d) |
369 | usage(); | |
e4842ff3 MM |
370 | for(e=d, n=1; *e; e++) |
371 | if (*e == ',') | |
372 | n++; | |
373 | op = xmalloc(sizeof(struct op) + n*sizeof(unsigned int)); | |
374 | } | |
375 | else | |
376 | { | |
377 | n = -1; | |
378 | op = xmalloc(sizeof(struct op)); | |
379 | } | |
380 | op->dev_vector = selected_devices; | |
381 | op->num_values = n; | |
382 | e = strchr(c, '.'); | |
383 | if (e) | |
384 | { | |
385 | *e++ = 0; | |
386 | if (e[1]) | |
387 | usage(); | |
388 | switch (*e & 0xdf) | |
389 | { | |
390 | case 'B': | |
391 | op->width = 1; break; | |
392 | case 'W': | |
393 | op->width = 2; break; | |
394 | case 'L': | |
395 | op->width = 4; break; | |
396 | default: | |
397 | usage(); | |
398 | } | |
399 | } | |
400 | else | |
401 | op->width = 1; | |
402 | ll = strtol(c, &f, 16); | |
b2c9b373 MM |
403 | if (f && *f) |
404 | { | |
405 | struct reg_name *r; | |
406 | for(r = pci_reg_names; r->name; r++) | |
407 | if (!strcasecmp(r->name, c)) | |
408 | break; | |
409 | if (!r->name || e) | |
410 | usage(); | |
411 | ll = r->offset; | |
412 | op->width = r->width; | |
413 | } | |
414 | if (ll > 0x100 || ll + op->width*((n < 0) ? 1 : n) > 0x100) | |
727ce158 | 415 | die("Register number out of range!"); |
b2c9b373 | 416 | if (ll & (op->width - 1)) |
727ce158 | 417 | die("Unaligned register address!"); |
b2c9b373 | 418 | op->addr = ll; |
e4842ff3 MM |
419 | for(i=0; i<n; i++) |
420 | { | |
421 | e = strchr(d, ','); | |
422 | if (e) | |
423 | *e++ = 0; | |
424 | ll = strtoul(d, &f, 16); | |
425 | lim = (2 << ((op->width << 3) - 1)) - 1; | |
426 | if (f && *f || | |
427 | (ll > lim && ll < ~0UL - lim)) | |
428 | usage(); | |
429 | op->values[i] = ll; | |
430 | d = e; | |
431 | } | |
432 | *last_op = op; | |
433 | last_op = &op->next; | |
434 | op->next = NULL; | |
435 | } | |
436 | argc--; | |
437 | argv++; | |
438 | } | |
439 | if (state == STATE_INIT) | |
440 | usage(); | |
441 | ||
b2c9b373 | 442 | scan_ops(first_op); |
e4842ff3 MM |
443 | execute(first_op); |
444 | ||
445 | return 0; | |
446 | } |