2 * $Id: setpci.c,v 1.11 2000/02/14 19:29:58 mj Exp $
4 * Linux PCI Utilities -- Manipulate PCI Configuration Registers
6 * Copyright (c) 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * Can be freely distributed and used under the terms of the GNU GPL.
19 static int force; /* Don't complain if no devices match */
20 static int verbose; /* Verbosity level */
21 static int demo_mode; /* Only show */
23 static struct pci_access *pacc;
27 struct pci_dev **dev_vector;
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];
34 static struct op *first_op, **last_op = &first_op;
36 static struct pci_dev **
37 select_devices(struct pci_filter *filt)
39 struct pci_dev *z, **a, **b;
42 for(z=pacc->devices; z; z=z->next)
43 if (pci_filter_match(filt, z))
45 a = b = xmalloc(sizeof(struct device *) * cnt);
46 for(z=pacc->devices; z; z=z->next)
47 if (pci_filter_match(filt, z))
54 exec_op(struct op *op, struct pci_dev *dev)
56 char *mm[] = { NULL, "%02x", "%04x", NULL, "%08x" };
57 char *m = mm[op->width];
62 printf("%02x:%02x.%x:%02x", dev->bus, dev->dev, dev->func, op->addr);
64 if (op->num_values >= 0)
65 for(i=0; i<op->num_values; i++)
70 printf(m, op->values[i]);
77 pci_write_byte(dev, addr, op->values[i]);
80 pci_write_word(dev, addr, op->values[i]);
83 pci_write_long(dev, addr, op->values[i]);
97 x = pci_read_byte(dev, addr);
100 x = pci_read_word(dev, addr);
103 x = pci_read_long(dev, addr);
115 execute(struct op *op)
117 struct pci_dev **vec = NULL;
118 struct pci_dev **pdev, *dev;
123 pdev = vec = op->dev_vector;
124 while (dev = *pdev++)
125 for(oops=op; oops && oops->dev_vector == vec; oops=oops->next)
127 while (op && op->dev_vector == vec)
133 scan_ops(struct op *op)
137 if (op->num_values >= 0)
149 static struct reg_name pci_reg_names[] = {
150 { 0x00, 2, "VENDOR_ID", },
151 { 0x02, 2, "DEVICE_ID", },
152 { 0x04, 2, "COMMAND", },
153 { 0x06, 2, "STATUS", },
154 { 0x08, 1, "REVISION", },
155 { 0x09, 1, "CLASS_PROG", },
156 { 0x0a, 2, "CLASS_DEVICE", },
157 { 0x0c, 1, "CACHE_LINE_SIZE", },
158 { 0x0d, 1, "LATENCY_TIMER", },
159 { 0x0e, 1, "HEADER_TYPE", },
160 { 0x0f, 1, "BIST", },
161 { 0x10, 4, "BASE_ADDRESS_0", },
162 { 0x14, 4, "BASE_ADDRESS_1", },
163 { 0x18, 4, "BASE_ADDRESS_2", },
164 { 0x1c, 4, "BASE_ADDRESS_3", },
165 { 0x20, 4, "BASE_ADDRESS_4", },
166 { 0x24, 4, "BASE_ADDRESS_5", },
167 { 0x28, 4, "CARDBUS_CIS", },
168 { 0x2c, 4, "SUBSYSTEM_VENDOR_ID", },
169 { 0x2e, 2, "SUBSYSTEM_ID", },
170 { 0x30, 4, "ROM_ADDRESS", },
171 { 0x3c, 1, "INTERRUPT_LINE", },
172 { 0x3d, 1, "INTERRUPT_PIN", },
173 { 0x3e, 1, "MIN_GNT", },
174 { 0x3f, 1, "MAX_LAT", },
175 { 0x18, 1, "PRIMARY_BUS", },
176 { 0x19, 1, "SECONDARY_BUS", },
177 { 0x1a, 1, "SUBORDINATE_BUS", },
178 { 0x1b, 1, "SEC_LATENCY_TIMER", },
179 { 0x1c, 1, "IO_BASE", },
180 { 0x1d, 1, "IO_LIMIT", },
181 { 0x1e, 2, "SEC_STATUS", },
182 { 0x20, 2, "MEMORY_BASE", },
183 { 0x22, 2, "MEMORY_LIMIT", },
184 { 0x24, 2, "PREF_MEMORY_BASE", },
185 { 0x26, 2, "PREF_MEMORY_LIMIT", },
186 { 0x28, 4, "PREF_BASE_UPPER32", },
187 { 0x2c, 4, "PREF_LIMIT_UPPER32", },
188 { 0x30, 2, "IO_BASE_UPPER16", },
189 { 0x32, 2, "IO_LIMIT_UPPER16", },
190 { 0x38, 4, "BRIDGE_ROM_ADDRESS", },
191 { 0x3e, 2, "BRIDGE_CONTROL", },
192 { 0x10, 4, "CB_CARDBUS_BASE", },
193 { 0x14, 2, "CB_CAPABILITIES", },
194 { 0x16, 2, "CB_SEC_STATUS", },
195 { 0x18, 1, "CB_BUS_NUMBER", },
196 { 0x19, 1, "CB_CARDBUS_NUMBER", },
197 { 0x1a, 1, "CB_SUBORDINATE_BUS", },
198 { 0x1b, 1, "CB_CARDBUS_LATENCY", },
199 { 0x1c, 4, "CB_MEMORY_BASE_0", },
200 { 0x20, 4, "CB_MEMORY_LIMIT_0", },
201 { 0x24, 4, "CB_MEMORY_BASE_1", },
202 { 0x28, 4, "CB_MEMORY_LIMIT_1", },
203 { 0x2c, 2, "CB_IO_BASE_0", },
204 { 0x2e, 2, "CB_IO_BASE_0_HI", },
205 { 0x30, 2, "CB_IO_LIMIT_0", },
206 { 0x32, 2, "CB_IO_LIMIT_0_HI", },
207 { 0x34, 2, "CB_IO_BASE_1", },
208 { 0x36, 2, "CB_IO_BASE_1_HI", },
209 { 0x38, 2, "CB_IO_LIMIT_1", },
210 { 0x3a, 2, "CB_IO_LIMIT_1_HI", },
211 { 0x40, 2, "CB_SUBSYSTEM_VENDOR_ID", },
212 { 0x42, 2, "CB_SUBSYSTEM_ID", },
213 { 0x44, 4, "CB_LEGACY_MODE_BASE", },
217 static void usage(void) __attribute__((noreturn));
223 "Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n\
224 -f\t\tDon't complain if there's nothing to do\n\
226 -D\t\tList changes, don't commit them\n"
228 "<device>:\t-s [[<bus>]:][<slot>][.[<func>]]\n\
229 \t|\t-d [<vendor>]:[<device>]\n\
230 <reg>:\t\t<number>[.(B|W|L)]\n\
232 <values>:\t<value>[,<value>...]\n\
238 main(int argc, char **argv)
240 enum { STATE_INIT, STATE_GOT_FILTER, STATE_GOT_OP } state = STATE_INIT;
241 struct pci_filter filter;
242 struct pci_dev **selected_devices = NULL;
243 char *opts = GENERIC_OPTIONS ;
245 if (argc == 2 && !strcmp(argv[1], "--version"))
247 puts("setpci version " PCIUTILS_VERSION);
256 while (argc && argv[0][0] == '-')
279 if (e = strchr(opts, *c))
298 if (!parse_generic_option(*e, pacc, arg))
322 unsigned long ll, lim;
326 if (!c[1] || !strchr("sd", c[1]))
329 d = (c[2] == '=') ? c+3 : c+2;
338 if (state != STATE_GOT_FILTER)
340 pci_filter_init(pacc, &filter);
341 state = STATE_GOT_FILTER;
346 if (d = pci_filter_parse_slot(&filter, d))
350 if (d = pci_filter_parse_id(&filter, d))
357 else if (state == STATE_INIT)
361 if (state == STATE_GOT_FILTER)
362 selected_devices = select_devices(&filter);
363 if (!selected_devices[0] && !force)
364 fprintf(stderr, "setpci: Warning: No devices selected for `%s'.\n", c);
365 state = STATE_GOT_OP;
372 for(e=d, n=1; *e; e++)
375 op = xmalloc(sizeof(struct op) + n*sizeof(unsigned int));
380 op = xmalloc(sizeof(struct op));
382 op->dev_vector = selected_devices;
393 op->width = 1; break;
395 op->width = 2; break;
397 op->width = 4; break;
404 ll = strtol(c, &f, 16);
408 for(r = pci_reg_names; r->name; r++)
409 if (!strcasecmp(r->name, c))
414 op->width = r->width;
416 if (ll > 0x100 || ll + op->width*((n < 0) ? 1 : n) > 0x100)
417 die("Register number out of range!");
418 if (ll & (op->width - 1))
419 die("Unaligned register address!");
426 ll = strtoul(d, &f, 16);
427 lim = (2 << ((op->width << 3) - 1)) - 1;
429 (ll > lim && ll < ~0UL - lim))
441 if (state == STATE_INIT)