2 * Linux PCI Utilities -- Manipulate PCI Configuration Registers
4 * Copyright (c) 1998 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 static struct pci_access *pacc;
25 struct pci_dev **dev_vector;
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];
32 static struct op *first_op, **last_op = &first_op;
34 static struct pci_dev **
35 select_devices(struct pci_filter *filt)
37 struct pci_dev *z, **a, **b;
40 for(z=pacc->devices; z; z=z->next)
41 if (pci_filter_match(filt, z))
43 a = b = xmalloc(sizeof(struct device *) * cnt);
44 for(z=pacc->devices; z; z=z->next)
45 if (pci_filter_match(filt, z))
52 exec_op(struct op *op, struct pci_dev *dev)
54 char *mm[] = { NULL, "%02x", "%04x", NULL, "%08x" };
55 char *m = mm[op->width];
60 printf("%02x:%02x.%x:%02x", dev->bus, dev->dev, dev->func, op->addr);
62 if (op->num_values >= 0)
63 for(i=0; i<op->num_values; i++)
68 printf(m, op->values[i]);
75 pci_write_byte(dev, addr, op->values[i]);
78 pci_write_word(dev, addr, op->values[i]);
81 pci_write_long(dev, addr, op->values[i]);
95 x = pci_read_byte(dev, addr);
98 x = pci_read_word(dev, addr);
101 x = pci_read_long(dev, addr);
113 execute(struct op *op)
115 struct pci_dev **vec = NULL;
116 struct pci_dev **pdev, *dev;
121 pdev = vec = op->dev_vector;
122 while (dev = *pdev++)
123 for(oops=op; oops && oops->dev_vector == vec; oops=oops->next)
125 while (op && op->dev_vector == vec)
131 scan_ops(struct op *op)
135 if (op->num_values >= 0)
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", },
215 static void usage(void) __attribute__((noreturn));
221 "Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n\
222 -f\t\tDon't complain if there's nothing to do\n\
224 -D\t\tList changes, don't commit them\n"
226 "<device>:\t-s [[<bus>]:][<slot>][.[<func>]]\n\
227 \t|\t-d [<vendor>]:[<device>]\n\
228 <reg>:\t\t<number>[.(B|W|L)]\n\
230 <values>:\t<value>[,<value>...]\n\
236 main(int argc, char **argv)
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 ;
243 if (argc == 2 && !strcmp(argv[1], "--version"))
245 puts("setpci version " PCIUTILS_VERSION);
254 while (argc && argv[0][0] == '-')
277 if (e = strchr(opts, *c))
296 if (!parse_generic_option(*e, pacc, arg))
320 unsigned long ll, lim;
324 if (!c[1] || !strchr("sd", c[1]))
327 d = (c[2] == '=') ? c+3 : c+2;
336 if (state != STATE_GOT_FILTER)
338 pci_filter_init(pacc, &filter);
339 state = STATE_GOT_FILTER;
344 if (d = pci_filter_parse_slot(&filter, d))
348 if (d = pci_filter_parse_id(&filter, d))
355 else if (state == STATE_INIT)
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;
370 for(e=d, n=1; *e; e++)
373 op = xmalloc(sizeof(struct op) + n*sizeof(unsigned int));
378 op = xmalloc(sizeof(struct op));
380 op->dev_vector = selected_devices;
391 op->width = 1; break;
393 op->width = 2; break;
395 op->width = 4; break;
402 ll = strtol(c, &f, 16);
406 for(r = pci_reg_names; r->name; r++)
407 if (!strcasecmp(r->name, c))
412 op->width = r->width;
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!");
424 ll = strtoul(d, &f, 16);
425 lim = (2 << ((op->width << 3) - 1)) - 1;
427 (ll > lim && ll < ~0UL - lim))
439 if (state == STATE_INIT)