2 * The PCI Utilities -- Manipulate PCI Configuration Registers
4 * Copyright (c) 1998--2006 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
17 static int force; /* Don't complain if no devices match */
18 static int verbose; /* Verbosity level */
19 static int demo_mode; /* Only show */
21 const char program_name[] = "setpci";
23 static struct pci_access *pacc;
32 struct pci_dev **dev_vector;
34 unsigned int width; /* Byte width of the access */
35 int num_values; /* Number of values to write; <0=read */
36 struct value values[0];
39 static struct op *first_op, **last_op = &first_op;
40 static unsigned int max_values[] = { 0, 0xff, 0xffff, 0, 0xffffffff };
42 static struct pci_dev **
43 select_devices(struct pci_filter *filt)
45 struct pci_dev *z, **a, **b;
48 for(z=pacc->devices; z; z=z->next)
49 if (pci_filter_match(filt, z))
51 a = b = xmalloc(sizeof(struct device *) * cnt);
52 for(z=pacc->devices; z; z=z->next)
53 if (pci_filter_match(filt, z))
60 exec_op(struct op *op, struct pci_dev *dev)
62 char *formats[] = { NULL, "%02x", "%04x", NULL, "%08x" };
63 char *mask_formats[] = { NULL, "%02x->(%02x:%02x)->%02x", "%04x->(%04x:%04x)->%04x", NULL, "%08x->(%08x:%08x)->%08x" };
66 int width = op->width;
69 printf("%02x:%02x.%x:%02x", dev->bus, dev->dev, dev->func, op->addr);
71 if (op->num_values >= 0)
73 for(i=0; i<op->num_values; i++)
75 if ((op->values[i].mask & max_values[width]) == max_values[width])
77 x = op->values[i].value;
81 printf(formats[width], op->values[i].value);
89 y = pci_read_byte(dev, addr);
92 y = pci_read_word(dev, addr);
95 y = pci_read_long(dev, addr);
98 x = (y & ~op->values[i].mask) | op->values[i].value;
102 printf(mask_formats[width], y, op->values[i].value, op->values[i].mask, x);
110 pci_write_byte(dev, addr, x);
113 pci_write_word(dev, addr, x);
116 pci_write_long(dev, addr, x);
132 x = pci_read_byte(dev, addr);
135 x = pci_read_word(dev, addr);
138 x = pci_read_long(dev, addr);
141 printf(formats[width], x);
147 execute(struct op *op)
149 struct pci_dev **vec = NULL;
150 struct pci_dev **pdev, *dev;
155 pdev = vec = op->dev_vector;
156 while (dev = *pdev++)
157 for(oops=op; oops && oops->dev_vector == vec; oops=oops->next)
159 while (op && op->dev_vector == vec)
165 scan_ops(struct op *op)
169 if (op->num_values >= 0)
181 static const struct reg_name pci_reg_names[] = {
182 { 0x00, 2, "VENDOR_ID", },
183 { 0x02, 2, "DEVICE_ID", },
184 { 0x04, 2, "COMMAND", },
185 { 0x06, 2, "STATUS", },
186 { 0x08, 1, "REVISION", },
187 { 0x09, 1, "CLASS_PROG", },
188 { 0x0a, 2, "CLASS_DEVICE", },
189 { 0x0c, 1, "CACHE_LINE_SIZE", },
190 { 0x0d, 1, "LATENCY_TIMER", },
191 { 0x0e, 1, "HEADER_TYPE", },
192 { 0x0f, 1, "BIST", },
193 { 0x10, 4, "BASE_ADDRESS_0", },
194 { 0x14, 4, "BASE_ADDRESS_1", },
195 { 0x18, 4, "BASE_ADDRESS_2", },
196 { 0x1c, 4, "BASE_ADDRESS_3", },
197 { 0x20, 4, "BASE_ADDRESS_4", },
198 { 0x24, 4, "BASE_ADDRESS_5", },
199 { 0x28, 4, "CARDBUS_CIS", },
200 { 0x2c, 4, "SUBSYSTEM_VENDOR_ID", },
201 { 0x2e, 2, "SUBSYSTEM_ID", },
202 { 0x30, 4, "ROM_ADDRESS", },
203 { 0x3c, 1, "INTERRUPT_LINE", },
204 { 0x3d, 1, "INTERRUPT_PIN", },
205 { 0x3e, 1, "MIN_GNT", },
206 { 0x3f, 1, "MAX_LAT", },
207 { 0x18, 1, "PRIMARY_BUS", },
208 { 0x19, 1, "SECONDARY_BUS", },
209 { 0x1a, 1, "SUBORDINATE_BUS", },
210 { 0x1b, 1, "SEC_LATENCY_TIMER", },
211 { 0x1c, 1, "IO_BASE", },
212 { 0x1d, 1, "IO_LIMIT", },
213 { 0x1e, 2, "SEC_STATUS", },
214 { 0x20, 2, "MEMORY_BASE", },
215 { 0x22, 2, "MEMORY_LIMIT", },
216 { 0x24, 2, "PREF_MEMORY_BASE", },
217 { 0x26, 2, "PREF_MEMORY_LIMIT", },
218 { 0x28, 4, "PREF_BASE_UPPER32", },
219 { 0x2c, 4, "PREF_LIMIT_UPPER32", },
220 { 0x30, 2, "IO_BASE_UPPER16", },
221 { 0x32, 2, "IO_LIMIT_UPPER16", },
222 { 0x38, 4, "BRIDGE_ROM_ADDRESS", },
223 { 0x3e, 2, "BRIDGE_CONTROL", },
224 { 0x10, 4, "CB_CARDBUS_BASE", },
225 { 0x14, 2, "CB_CAPABILITIES", },
226 { 0x16, 2, "CB_SEC_STATUS", },
227 { 0x18, 1, "CB_BUS_NUMBER", },
228 { 0x19, 1, "CB_CARDBUS_NUMBER", },
229 { 0x1a, 1, "CB_SUBORDINATE_BUS", },
230 { 0x1b, 1, "CB_CARDBUS_LATENCY", },
231 { 0x1c, 4, "CB_MEMORY_BASE_0", },
232 { 0x20, 4, "CB_MEMORY_LIMIT_0", },
233 { 0x24, 4, "CB_MEMORY_BASE_1", },
234 { 0x28, 4, "CB_MEMORY_LIMIT_1", },
235 { 0x2c, 2, "CB_IO_BASE_0", },
236 { 0x2e, 2, "CB_IO_BASE_0_HI", },
237 { 0x30, 2, "CB_IO_LIMIT_0", },
238 { 0x32, 2, "CB_IO_LIMIT_0_HI", },
239 { 0x34, 2, "CB_IO_BASE_1", },
240 { 0x36, 2, "CB_IO_BASE_1_HI", },
241 { 0x38, 2, "CB_IO_LIMIT_1", },
242 { 0x3a, 2, "CB_IO_LIMIT_1_HI", },
243 { 0x40, 2, "CB_SUBSYSTEM_VENDOR_ID", },
244 { 0x42, 2, "CB_SUBSYSTEM_ID", },
245 { 0x44, 4, "CB_LEGACY_MODE_BASE", },
250 usage(char *msg, ...)
256 fprintf(stderr, "setpci: ");
257 vfprintf(stderr, msg, args);
258 fprintf(stderr, "\n\n");
261 "Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n"
262 "-f\t\tDon't complain if there's nothing to do\n"
264 "-D\t\tList changes, don't commit them\n"
266 "<device>:\t-s [[[<domain>]:][<bus>]:][<slot>][.[<func>]]\n"
267 "\t|\t-d [<vendor>]:[<device>]\n"
268 "<reg>:\t\t<number>[.(B|W|L)]\n"
270 "<values>:\t<value>[,<value>...]\n"
272 " |\t<hex>:<mask>\n");
277 main(int argc, char **argv)
279 enum { STATE_INIT, STATE_GOT_FILTER, STATE_GOT_OP } state = STATE_INIT;
280 struct pci_filter filter;
281 struct pci_dev **selected_devices = NULL;
282 char *opts = GENERIC_OPTIONS ;
284 if (argc == 2 && !strcmp(argv[1], "--version"))
286 puts("setpci version " PCIUTILS_VERSION);
295 while (argc && argv[0][0] == '-')
318 if (e = strchr(opts, *c))
337 if (!parse_generic_option(*e, pacc, arg))
366 if (!c[1] || !strchr("sd", c[1]))
369 d = (c[2] == '=') ? c+3 : c+2;
378 if (state != STATE_GOT_FILTER)
380 pci_filter_init(pacc, &filter);
381 state = STATE_GOT_FILTER;
386 if (d = pci_filter_parse_slot(&filter, d))
390 if (d = pci_filter_parse_id(&filter, d))
397 else if (state == STATE_INIT)
401 if (state == STATE_GOT_FILTER)
402 selected_devices = select_devices(&filter);
403 if (!selected_devices[0] && !force)
404 fprintf(stderr, "setpci: Warning: No devices selected for `%s'.\n", c);
405 state = STATE_GOT_OP;
406 /* look for setting of values and count how many */
412 usage("Missing value");
413 for(e=d, n=1; *e; e++)
416 op = xmalloc(sizeof(struct op) + n*sizeof(struct value));
421 op = xmalloc(sizeof(struct op));
423 op->dev_vector = selected_devices;
430 usage("Missing width");
434 op->width = 1; break;
436 op->width = 2; break;
438 op->width = 4; break;
440 usage("Invalid width \"%c\"", *e);
445 ll = strtol(c, &f, 16);
448 const struct reg_name *r;
449 for(r = pci_reg_names; r->name; r++)
450 if (!strcasecmp(r->name, c))
453 usage("Unknown register \"%s\"", c);
454 if (e && op->width != r->width)
455 usage("Explicit width doesn't correspond with the named register \"%s\"", c);
457 op->width = r->width;
459 if (ll > 0x1000 || ll + op->width*((n < 0) ? 1 : n) > 0x1000)
460 die("Register number out of range!");
461 if (ll & (op->width - 1))
462 die("Unaligned register address!");
464 /* read in all the values to be set */
470 ll = strtoul(d, &f, 16);
471 lim = max_values[op->width];
472 if (f && *f && *f != ':')
473 usage("Invalid value \"%s\"", d);
474 if (ll > lim && ll < ~0UL - lim)
475 usage("Value \"%s\" is out of range", d);
476 op->values[i].value = ll;
480 ll = strtoul(d, &f, 16);
482 usage("Invalid mask \"%s\"", d);
483 if (ll > lim && ll < ~0UL - lim)
484 usage("Mask \"%s\" is out of range", d);
485 op->values[i].mask = ll;
486 op->values[i].value &= ll;
489 op->values[i].mask = ~0U;
499 if (state == STATE_INIT)
500 usage("No operation specified");