]> mj.ucw.cz Git - pciutils.git/blob - setpci.c
Fixed the "dist" target.
[pciutils.git] / setpci.c
1 /*
2  *      $Id: setpci.c,v 1.11 2000/02/14 19:29:58 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, addr;
60
61   if (verbose)
62     printf("%02x:%02x.%x:%02x", dev->bus, dev->dev, dev->func, op->addr);
63   addr = op->addr;
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:
77             pci_write_byte(dev, addr, op->values[i]);
78             break;
79           case 2:
80             pci_write_word(dev, addr, op->values[i]);
81             break;
82           default:
83             pci_write_long(dev, addr, op->values[i]);
84             break;
85           }
86         addr += op->width;
87       }
88   else
89     {
90       if (verbose)
91         printf(" = ");
92       if (!demo_mode)
93         {
94           switch (op->width)
95             {
96             case 1:
97               x = pci_read_byte(dev, addr);
98               break;
99             case 2:
100               x = pci_read_word(dev, addr);
101               break;
102             default:
103               x = pci_read_long(dev, addr);
104               break;
105             }
106           printf(m, x);
107         }
108       else
109         putchar('?');
110     }
111   putchar('\n');
112 }
113
114 static void
115 execute(struct op *op)
116 {
117   struct pci_dev **vec = NULL;
118   struct pci_dev **pdev, *dev;
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
132 static void
133 scan_ops(struct op *op)
134 {
135   while (op)
136     {
137       if (op->num_values >= 0)
138         pacc->writeable = 1;
139       op = op->next;
140     }
141 }
142
143 struct reg_name {
144   int offset;
145   int width;
146   char *name;
147 };
148
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", },
214   { 0x00, 0, NULL }
215 };
216
217 static void usage(void) __attribute__((noreturn));
218
219 static void
220 usage(void)
221 {
222   fprintf(stderr,
223 "Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n\
224 -f\t\tDon't complain if there's nothing to do\n\
225 -v\t\tBe verbose\n\
226 -D\t\tList changes, don't commit them\n"
227 GENERIC_HELP
228 "<device>:\t-s [[<bus>]:][<slot>][.[<func>]]\n\
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\
233 ");
234   exit(1);
235 }
236
237 int
238 main(int argc, char **argv)
239 {
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 ;
244
245   if (argc == 2 && !strcmp(argv[1], "--version"))
246     {
247       puts("setpci version " PCIUTILS_VERSION);
248       return 0;
249     }
250   argc--;
251   argv++;
252
253   pacc = pci_alloc();
254   pacc->error = die;
255
256   while (argc && argv[0][0] == '-')
257     {
258       char *c = argv[0]+1;
259       char *d = c;
260       char *e;
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;
272           case 'D':
273             demo_mode++;
274             c++;
275             break;
276           case 0:
277             break;
278           default:
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               }
307           }
308       argc--;
309       argv++;
310     }
311 next:
312
313   pci_init(pacc);
314   pci_scan_bus(pacc);
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;
330           else if (argc > 1)
331             {
332               argc--;
333               argv++;
334               d = argv[0];
335             }
336           else
337             usage();
338           if (state != STATE_GOT_FILTER)
339             {
340               pci_filter_init(pacc, &filter);
341               state = STATE_GOT_FILTER;
342             }
343           switch (c[1])
344             {
345             case 's':
346               if (d = pci_filter_parse_slot(&filter, d))
347                 die("-s: %s", d);
348               break;
349             case 'd':
350               if (d = pci_filter_parse_id(&filter, d))
351                 die("-d: %s", d);
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;
370               if (!*d)
371                 usage();
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);
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)
417             die("Register number out of range!");
418           if (ll & (op->width - 1))
419             die("Unaligned register address!");
420           op->addr = ll;
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
444   scan_ops(first_op);
445   execute(first_op);
446
447   return 0;
448 }