]> git.ipfire.org Git - thirdparty/pciutils.git/blame - setpci.c
Added more warnings
[thirdparty/pciutils.git] / setpci.c
CommitLineData
e4842ff3 1/*
a27a33dd 2 * $Id: setpci.c,v 1.12 2002/03/30 15:39:24 mj Exp $
e4842ff3
MM
3 *
4 * Linux PCI Utilities -- Manipulate PCI Configuration Registers
5 *
a27a33dd 6 * Copyright (c) 1998 Martin Mares <mj@ucw.cz>
e4842ff3
MM
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>
727ce158 14#include <stdarg.h>
e4842ff3 15#include <unistd.h>
7aad822b 16
e4842ff3
MM
17#include "pciutils.h"
18
19static int force; /* Don't complain if no devices match */
20static int verbose; /* Verbosity level */
b2c9b373 21static int demo_mode; /* Only show */
e4842ff3 22
727ce158 23static struct pci_access *pacc;
e4842ff3
MM
24
25struct op {
26 struct op *next;
727ce158 27 struct pci_dev **dev_vector;
e4842ff3
MM
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
34static struct op *first_op, **last_op = &first_op;
35
727ce158 36static struct pci_dev **
e4842ff3
MM
37select_devices(struct pci_filter *filt)
38{
727ce158 39 struct pci_dev *z, **a, **b;
e4842ff3
MM
40 int cnt = 1;
41
727ce158
MM
42 for(z=pacc->devices; z; z=z->next)
43 if (pci_filter_match(filt, z))
e4842ff3
MM
44 cnt++;
45 a = b = xmalloc(sizeof(struct device *) * cnt);
727ce158
MM
46 for(z=pacc->devices; z; z=z->next)
47 if (pci_filter_match(filt, z))
e4842ff3
MM
48 *a++ = z;
49 *a = NULL;
50 return b;
51}
52
53static void
727ce158 54exec_op(struct op *op, struct pci_dev *dev)
e4842ff3
MM
55{
56 char *mm[] = { NULL, "%02x", "%04x", NULL, "%08x" };
57 char *m = mm[op->width];
b2c9b373 58 unsigned int x;
33bc28a5 59 int i, addr;
e4842ff3
MM
60
61 if (verbose)
727ce158 62 printf("%02x:%02x.%x:%02x", dev->bus, dev->dev, dev->func, op->addr);
33bc28a5 63 addr = op->addr;
b2c9b373
MM
64 if (op->num_values >= 0)
65 for(i=0; i<op->num_values; i++)
66 {
67 if (verbose)
68 {
69 putchar(' ');
70 printf(m, op->values[i]);
71 }
72 if (demo_mode)
73 continue;
74 switch (op->width)
75 {
76 case 1:
33bc28a5 77 pci_write_byte(dev, addr, op->values[i]);
b2c9b373
MM
78 break;
79 case 2:
33bc28a5 80 pci_write_word(dev, addr, op->values[i]);
b2c9b373
MM
81 break;
82 default:
33bc28a5 83 pci_write_long(dev, addr, op->values[i]);
b2c9b373
MM
84 break;
85 }
33bc28a5 86 addr += op->width;
b2c9b373 87 }
e4842ff3
MM
88 else
89 {
90 if (verbose)
b2c9b373
MM
91 printf(" = ");
92 if (!demo_mode)
93 {
94 switch (op->width)
95 {
96 case 1:
33bc28a5 97 x = pci_read_byte(dev, addr);
b2c9b373
MM
98 break;
99 case 2:
33bc28a5 100 x = pci_read_word(dev, addr);
b2c9b373
MM
101 break;
102 default:
33bc28a5 103 x = pci_read_long(dev, addr);
b2c9b373
MM
104 break;
105 }
b2c9b373
MM
106 printf(m, x);
107 }
108 else
109 putchar('?');
e4842ff3 110 }
b2c9b373 111 putchar('\n');
e4842ff3
MM
112}
113
114static void
115execute(struct op *op)
116{
727ce158
MM
117 struct pci_dev **vec = NULL;
118 struct pci_dev **pdev, *dev;
e4842ff3
MM
119 struct op *oops;
120
121 while (op)
122 {
123 pdev = vec = op->dev_vector;
124 while (dev = *pdev++)
125 for(oops=op; oops && oops->dev_vector == vec; oops=oops->next)
126 exec_op(oops, dev);
127 while (op && op->dev_vector == vec)
128 op = op->next;
129 }
130}
131
b2c9b373
MM
132static void
133scan_ops(struct op *op)
134{
b2c9b373
MM
135 while (op)
136 {
137 if (op->num_values >= 0)
727ce158 138 pacc->writeable = 1;
b2c9b373
MM
139 op = op->next;
140 }
141}
142
143struct reg_name {
144 int offset;
145 int width;
146 char *name;
147};
148
149static 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", },
214 { 0x00, 0, NULL }
215};
216
e4842ff3
MM
217static void usage(void) __attribute__((noreturn));
218
219static void
220usage(void)
221{
222 fprintf(stderr,
727ce158 223"Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n\
b2c9b373
MM
224-f\t\tDon't complain if there's nothing to do\n\
225-v\t\tBe verbose\n\
727ce158
MM
226-D\t\tList changes, don't commit them\n"
227GENERIC_HELP
228"<device>:\t-s [[<bus>]:][<slot>][.[<func>]]\n\
b2c9b373
MM
229\t|\t-d [<vendor>]:[<device>]\n\
230<reg>:\t\t<number>[.(B|W|L)]\n\
231 |\t\t<name>\n\
232<values>:\t<value>[,<value>...]\n\
e4842ff3
MM
233");
234 exit(1);
235}
236
237int
238main(int argc, char **argv)
239{
240 enum { STATE_INIT, STATE_GOT_FILTER, STATE_GOT_OP } state = STATE_INIT;
241 struct pci_filter filter;
727ce158
MM
242 struct pci_dev **selected_devices = NULL;
243 char *opts = GENERIC_OPTIONS ;
e4842ff3 244
7aad822b
MM
245 if (argc == 2 && !strcmp(argv[1], "--version"))
246 {
247 puts("setpci version " PCIUTILS_VERSION);
248 return 0;
249 }
e4842ff3
MM
250 argc--;
251 argv++;
727ce158
MM
252
253 pacc = pci_alloc();
254 pacc->error = die;
255
e4842ff3
MM
256 while (argc && argv[0][0] == '-')
257 {
258 char *c = argv[0]+1;
259 char *d = c;
727ce158 260 char *e;
e4842ff3
MM
261 while (*c)
262 switch (*c)
263 {
264 case 'v':
265 verbose++;
266 c++;
267 break;
268 case 'f':
269 force++;
270 c++;
271 break;
b2c9b373
MM
272 case 'D':
273 demo_mode++;
274 c++;
275 break;
e4842ff3
MM
276 case 0:
277 break;
278 default:
727ce158
MM
279 if (e = strchr(opts, *c))
280 {
281 char *arg;
282 c++;
283 if (e[1] == ':')
284 {
285 if (*c)
286 arg = c;
287 else if (argc > 1)
288 {
289 arg = argv[1];
290 argc--; argv++;
291 }
292 else
293 usage();
294 c = "";
295 }
296 else
297 arg = NULL;
298 if (!parse_generic_option(*e, pacc, arg))
299 usage();
300 }
301 else
302 {
303 if (c != d)
304 usage();
305 goto next;
306 }
e4842ff3
MM
307 }
308 argc--;
309 argv++;
310 }
311next:
312
727ce158
MM
313 pci_init(pacc);
314 pci_scan_bus(pacc);
e4842ff3
MM
315
316 while (argc)
317 {
318 char *c = argv[0];
319 char *d, *e, *f;
320 int n, i;
321 struct op *op;
322 unsigned long ll, lim;
323
324 if (*c == '-')
325 {
326 if (!c[1] || !strchr("sd", c[1]))
327 usage();
328 if (c[2])
329 d = (c[2] == '=') ? c+3 : c+2;
183251d7 330 else if (argc > 1)
e4842ff3
MM
331 {
332 argc--;
333 argv++;
334 d = argv[0];
335 }
336 else
337 usage();
338 if (state != STATE_GOT_FILTER)
339 {
727ce158 340 pci_filter_init(pacc, &filter);
e4842ff3
MM
341 state = STATE_GOT_FILTER;
342 }
343 switch (c[1])
344 {
345 case 's':
727ce158
MM
346 if (d = pci_filter_parse_slot(&filter, d))
347 die("-s: %s", d);
e4842ff3
MM
348 break;
349 case 'd':
727ce158
MM
350 if (d = pci_filter_parse_id(&filter, d))
351 die("-d: %s", d);
e4842ff3
MM
352 break;
353 default:
354 usage();
355 }
356 }
357 else if (state == STATE_INIT)
358 usage();
359 else
360 {
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;
366 d = strchr(c, '=');
367 if (d)
368 {
369 *d++ = 0;
b2c9b373
MM
370 if (!*d)
371 usage();
e4842ff3
MM
372 for(e=d, n=1; *e; e++)
373 if (*e == ',')
374 n++;
375 op = xmalloc(sizeof(struct op) + n*sizeof(unsigned int));
376 }
377 else
378 {
379 n = -1;
380 op = xmalloc(sizeof(struct op));
381 }
382 op->dev_vector = selected_devices;
383 op->num_values = n;
384 e = strchr(c, '.');
385 if (e)
386 {
387 *e++ = 0;
388 if (e[1])
389 usage();
390 switch (*e & 0xdf)
391 {
392 case 'B':
393 op->width = 1; break;
394 case 'W':
395 op->width = 2; break;
396 case 'L':
397 op->width = 4; break;
398 default:
399 usage();
400 }
401 }
402 else
403 op->width = 1;
404 ll = strtol(c, &f, 16);
b2c9b373
MM
405 if (f && *f)
406 {
407 struct reg_name *r;
408 for(r = pci_reg_names; r->name; r++)
409 if (!strcasecmp(r->name, c))
410 break;
411 if (!r->name || e)
412 usage();
413 ll = r->offset;
414 op->width = r->width;
415 }
416 if (ll > 0x100 || ll + op->width*((n < 0) ? 1 : n) > 0x100)
727ce158 417 die("Register number out of range!");
b2c9b373 418 if (ll & (op->width - 1))
727ce158 419 die("Unaligned register address!");
b2c9b373 420 op->addr = ll;
e4842ff3
MM
421 for(i=0; i<n; i++)
422 {
423 e = strchr(d, ',');
424 if (e)
425 *e++ = 0;
426 ll = strtoul(d, &f, 16);
427 lim = (2 << ((op->width << 3) - 1)) - 1;
428 if (f && *f ||
429 (ll > lim && ll < ~0UL - lim))
430 usage();
431 op->values[i] = ll;
432 d = e;
433 }
434 *last_op = op;
435 last_op = &op->next;
436 op->next = NULL;
437 }
438 argc--;
439 argv++;
440 }
441 if (state == STATE_INIT)
442 usage();
443
b2c9b373 444 scan_ops(first_op);
e4842ff3
MM
445 execute(first_op);
446
447 return 0;
448}