]> git.ipfire.org Git - thirdparty/pciutils.git/blob - setpci.c
Rewrote the PCI Utilities. All PCI configuration space access has been
[thirdparty/pciutils.git] / setpci.c
1 /*
2 * $Id: setpci.c,v 1.9 1999/01/22 21:05:02 mj Exp $
3 *
4 * Linux PCI Utilities -- Manipulate PCI Configuration Registers
5 *
6 * Copyright (c) 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
7 *
8 * Can be freely distributed and used under the terms of the GNU GPL.
9 */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <unistd.h>
16
17 #include "pciutils.h"
18
19 static int force; /* Don't complain if no devices match */
20 static int verbose; /* Verbosity level */
21 static int demo_mode; /* Only show */
22
23 static struct pci_access *pacc;
24
25 struct op {
26 struct op *next;
27 struct pci_dev **dev_vector;
28 unsigned int addr;
29 unsigned int width; /* Byte width of the access */
30 int num_values; /* Number of values to write; <0=read */
31 unsigned int values[0];
32 };
33
34 static struct op *first_op, **last_op = &first_op;
35
36 static struct pci_dev **
37 select_devices(struct pci_filter *filt)
38 {
39 struct pci_dev *z, **a, **b;
40 int cnt = 1;
41
42 for(z=pacc->devices; z; z=z->next)
43 if (pci_filter_match(filt, z))
44 cnt++;
45 a = b = xmalloc(sizeof(struct device *) * cnt);
46 for(z=pacc->devices; z; z=z->next)
47 if (pci_filter_match(filt, z))
48 *a++ = z;
49 *a = NULL;
50 return b;
51 }
52
53 static void
54 exec_op(struct op *op, struct pci_dev *dev)
55 {
56 char *mm[] = { NULL, "%02x", "%04x", NULL, "%08x" };
57 char *m = mm[op->width];
58 unsigned int x;
59 int i;
60
61 if (verbose)
62 printf("%02x:%02x.%x:%02x", dev->bus, dev->dev, dev->func, op->addr);
63 if (op->num_values >= 0)
64 for(i=0; i<op->num_values; i++)
65 {
66 if (verbose)
67 {
68 putchar(' ');
69 printf(m, op->values[i]);
70 }
71 if (demo_mode)
72 continue;
73 switch (op->width)
74 {
75 case 1:
76 pci_write_byte(dev, op->addr, op->values[i]);
77 break;
78 case 2:
79 pci_write_word(dev, op->addr, op->values[i]);
80 break;
81 default:
82 pci_write_long(dev, op->addr, op->values[i]);
83 break;
84 }
85 }
86 else
87 {
88 if (verbose)
89 printf(" = ");
90 if (!demo_mode)
91 {
92 switch (op->width)
93 {
94 case 1:
95 x = pci_read_byte(dev, op->addr);
96 break;
97 case 2:
98 x = pci_read_word(dev, op->addr);
99 break;
100 default:
101 x = pci_read_long(dev, op->addr);
102 break;
103 }
104 printf(m, x);
105 }
106 else
107 putchar('?');
108 }
109 putchar('\n');
110 }
111
112 static void
113 execute(struct op *op)
114 {
115 struct pci_dev **vec = NULL;
116 struct pci_dev **pdev, *dev;
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
130 static void
131 scan_ops(struct op *op)
132 {
133 while (op)
134 {
135 if (op->num_values >= 0)
136 pacc->writeable = 1;
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
215 static void usage(void) __attribute__((noreturn));
216
217 static void
218 usage(void)
219 {
220 fprintf(stderr,
221 "Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n\
222 -f\t\tDon't complain if there's nothing to do\n\
223 -v\t\tBe verbose\n\
224 -D\t\tList changes, don't commit them\n"
225 GENERIC_HELP
226 "<device>:\t-s [[<bus>]:][<slot>][.[<func>]]\n\
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\
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;
240 struct pci_dev **selected_devices = NULL;
241 char *opts = GENERIC_OPTIONS ;
242
243 if (argc == 2 && !strcmp(argv[1], "--version"))
244 {
245 puts("setpci version " PCIUTILS_VERSION);
246 return 0;
247 }
248 argc--;
249 argv++;
250
251 pacc = pci_alloc();
252 pacc->error = die;
253
254 while (argc && argv[0][0] == '-')
255 {
256 char *c = argv[0]+1;
257 char *d = c;
258 char *e;
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;
270 case 'D':
271 demo_mode++;
272 c++;
273 break;
274 case 0:
275 break;
276 default:
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 }
305 }
306 argc--;
307 argv++;
308 }
309 next:
310
311 pci_init(pacc);
312 pci_scan_bus(pacc);
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;
328 else if (argc)
329 {
330 argc--;
331 argv++;
332 d = argv[0];
333 }
334 else
335 usage();
336 if (state != STATE_GOT_FILTER)
337 {
338 pci_filter_init(pacc, &filter);
339 state = STATE_GOT_FILTER;
340 }
341 switch (c[1])
342 {
343 case 's':
344 if (d = pci_filter_parse_slot(&filter, d))
345 die("-s: %s", d);
346 break;
347 case 'd':
348 if (d = pci_filter_parse_id(&filter, d))
349 die("-d: %s", d);
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;
368 if (!*d)
369 usage();
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);
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)
415 die("Register number out of range!");
416 if (ll & (op->width - 1))
417 die("Unaligned register address!");
418 op->addr = ll;
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
442 scan_ops(first_op);
443 execute(first_op);
444
445 return 0;
446 }