Tizen 2.1 base
[framework/system/pciutils.git] / setpci.c
1 /*
2  *      The PCI Utilities -- Manipulate PCI Configuration Registers
3  *
4  *      Copyright (c) 1998--2008 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <unistd.h>
14
15 #define PCIUTILS_SETPCI
16 #include "pciutils.h"
17
18 static int force;                       /* Don't complain if no devices match */
19 static int verbose;                     /* Verbosity level */
20 static int demo_mode;                   /* Only show */
21
22 const char program_name[] = "setpci";
23
24 static struct pci_access *pacc;
25
26 struct value {
27   unsigned int value;
28   unsigned int mask;
29 };
30
31 struct op {
32   struct op *next;
33   struct pci_dev **dev_vector;
34   unsigned int addr;
35   unsigned int width;                   /* Byte width of the access */
36   int num_values;                       /* Number of values to write; <0=read */
37   struct value values[0];
38 };
39
40 static struct op *first_op, **last_op = &first_op;
41 static unsigned int max_values[] = { 0, 0xff, 0xffff, 0, 0xffffffff };
42
43 static struct pci_dev **
44 select_devices(struct pci_filter *filt)
45 {
46   struct pci_dev *z, **a, **b;
47   int cnt = 1;
48
49   for(z=pacc->devices; z; z=z->next)
50     if (pci_filter_match(filt, z))
51       cnt++;
52   a = b = xmalloc(sizeof(struct device *) * cnt);
53   for(z=pacc->devices; z; z=z->next)
54     if (pci_filter_match(filt, z))
55       *a++ = z;
56   *a = NULL;
57   return b;
58 }
59
60 static void
61 exec_op(struct op *op, struct pci_dev *dev)
62 {
63   char *formats[] = { NULL, "%02x", "%04x", NULL, "%08x" };
64   char *mask_formats[] = { NULL, "%02x->(%02x:%02x)->%02x", "%04x->(%04x:%04x)->%04x", NULL, "%08x->(%08x:%08x)->%08x" };
65   unsigned int x, y;
66   int i, addr;
67   int width = op->width;
68
69   if (verbose)
70     printf("%02x:%02x.%x:%02x", dev->bus, dev->dev, dev->func, op->addr);
71   addr = op->addr;
72   if (op->num_values >= 0)
73     {
74       for(i=0; i<op->num_values; i++)
75         {
76           if ((op->values[i].mask & max_values[width]) == max_values[width])
77             {
78               x = op->values[i].value;
79               if (verbose)
80                 {
81                   putchar(' ');
82                   printf(formats[width], op->values[i].value);
83                 }
84             }
85           else
86             {
87               switch (width)
88                 {
89                 case 1:
90                   y = pci_read_byte(dev, addr);
91                   break;
92                 case 2:
93                   y = pci_read_word(dev, addr);
94                   break;
95                 default:
96                   y = pci_read_long(dev, addr);
97                   break;
98                 }
99               x = (y & ~op->values[i].mask) | op->values[i].value;
100               if (verbose)
101                 {
102                   putchar(' ');
103                   printf(mask_formats[width], y, op->values[i].value, op->values[i].mask, x);
104                 }
105             }
106           if (!demo_mode)
107             {
108               switch (width)
109                 {
110                 case 1:
111                   pci_write_byte(dev, addr, x);
112                   break;
113                 case 2:
114                   pci_write_word(dev, addr, x);
115                   break;
116                 default:
117                   pci_write_long(dev, addr, x);
118                   break;
119                 }
120             }
121           addr += width;
122         }
123       if (verbose)
124         putchar('\n');
125     }
126   else
127     {
128       if (verbose)
129         printf(" = ");
130       switch (width)
131         {
132         case 1:
133           x = pci_read_byte(dev, addr);
134           break;
135         case 2:
136           x = pci_read_word(dev, addr);
137           break;
138         default:
139           x = pci_read_long(dev, addr);
140           break;
141         }
142       printf(formats[width], x);
143       putchar('\n');
144     }
145 }
146
147 static void
148 execute(struct op *op)
149 {
150   struct pci_dev **vec = NULL;
151   struct pci_dev **pdev, *dev;
152   struct op *oops;
153
154   while (op)
155     {
156       pdev = vec = op->dev_vector;
157       while (dev = *pdev++)
158         for(oops=op; oops && oops->dev_vector == vec; oops=oops->next)
159           exec_op(oops, dev);
160       while (op && op->dev_vector == vec)
161         op = op->next;
162     }
163 }
164
165 static void
166 scan_ops(struct op *op)
167 {
168   while (op)
169     {
170       if (op->num_values >= 0)
171         pacc->writeable = 1;
172       op = op->next;
173     }
174 }
175
176 struct reg_name {
177   unsigned int offset;
178   unsigned int width;
179   const char *name;
180 };
181
182 static const struct reg_name pci_reg_names[] = {
183   { 0x00, 2, "VENDOR_ID", },
184   { 0x02, 2, "DEVICE_ID", },
185   { 0x04, 2, "COMMAND", },
186   { 0x06, 2, "STATUS", },
187   { 0x08, 1, "REVISION", },
188   { 0x09, 1, "CLASS_PROG", },
189   { 0x0a, 2, "CLASS_DEVICE", },
190   { 0x0c, 1, "CACHE_LINE_SIZE", },
191   { 0x0d, 1, "LATENCY_TIMER", },
192   { 0x0e, 1, "HEADER_TYPE", },
193   { 0x0f, 1, "BIST", },
194   { 0x10, 4, "BASE_ADDRESS_0", },
195   { 0x14, 4, "BASE_ADDRESS_1", },
196   { 0x18, 4, "BASE_ADDRESS_2", },
197   { 0x1c, 4, "BASE_ADDRESS_3", },
198   { 0x20, 4, "BASE_ADDRESS_4", },
199   { 0x24, 4, "BASE_ADDRESS_5", },
200   { 0x28, 4, "CARDBUS_CIS", },
201   { 0x2c, 4, "SUBSYSTEM_VENDOR_ID", },
202   { 0x2e, 2, "SUBSYSTEM_ID", },
203   { 0x30, 4, "ROM_ADDRESS", },
204   { 0x3c, 1, "INTERRUPT_LINE", },
205   { 0x3d, 1, "INTERRUPT_PIN", },
206   { 0x3e, 1, "MIN_GNT", },
207   { 0x3f, 1, "MAX_LAT", },
208   { 0x18, 1, "PRIMARY_BUS", },
209   { 0x19, 1, "SECONDARY_BUS", },
210   { 0x1a, 1, "SUBORDINATE_BUS", },
211   { 0x1b, 1, "SEC_LATENCY_TIMER", },
212   { 0x1c, 1, "IO_BASE", },
213   { 0x1d, 1, "IO_LIMIT", },
214   { 0x1e, 2, "SEC_STATUS", },
215   { 0x20, 2, "MEMORY_BASE", },
216   { 0x22, 2, "MEMORY_LIMIT", },
217   { 0x24, 2, "PREF_MEMORY_BASE", },
218   { 0x26, 2, "PREF_MEMORY_LIMIT", },
219   { 0x28, 4, "PREF_BASE_UPPER32", },
220   { 0x2c, 4, "PREF_LIMIT_UPPER32", },
221   { 0x30, 2, "IO_BASE_UPPER16", },
222   { 0x32, 2, "IO_LIMIT_UPPER16", },
223   { 0x38, 4, "BRIDGE_ROM_ADDRESS", },
224   { 0x3e, 2, "BRIDGE_CONTROL", },
225   { 0x10, 4, "CB_CARDBUS_BASE", },
226   { 0x14, 2, "CB_CAPABILITIES", },
227   { 0x16, 2, "CB_SEC_STATUS", },
228   { 0x18, 1, "CB_BUS_NUMBER", },
229   { 0x19, 1, "CB_CARDBUS_NUMBER", },
230   { 0x1a, 1, "CB_SUBORDINATE_BUS", },
231   { 0x1b, 1, "CB_CARDBUS_LATENCY", },
232   { 0x1c, 4, "CB_MEMORY_BASE_0", },
233   { 0x20, 4, "CB_MEMORY_LIMIT_0", },
234   { 0x24, 4, "CB_MEMORY_BASE_1", },
235   { 0x28, 4, "CB_MEMORY_LIMIT_1", },
236   { 0x2c, 2, "CB_IO_BASE_0", },
237   { 0x2e, 2, "CB_IO_BASE_0_HI", },
238   { 0x30, 2, "CB_IO_LIMIT_0", },
239   { 0x32, 2, "CB_IO_LIMIT_0_HI", },
240   { 0x34, 2, "CB_IO_BASE_1", },
241   { 0x36, 2, "CB_IO_BASE_1_HI", },
242   { 0x38, 2, "CB_IO_LIMIT_1", },
243   { 0x3a, 2, "CB_IO_LIMIT_1_HI", },
244   { 0x40, 2, "CB_SUBSYSTEM_VENDOR_ID", },
245   { 0x42, 2, "CB_SUBSYSTEM_ID", },
246   { 0x44, 4, "CB_LEGACY_MODE_BASE", },
247   { 0x00, 0, NULL }
248 };
249
250 static void NONRET PCI_PRINTF(1,2)
251 usage(char *msg, ...)
252 {
253   va_list args;
254   va_start(args, msg);
255   if (msg)
256     {
257       fprintf(stderr, "setpci: ");
258       vfprintf(stderr, msg, args);
259       fprintf(stderr, "\n\n");
260     }
261   fprintf(stderr,
262 "Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n"
263 "\n"
264 "General options:\n"
265 "-f\t\tDon't complain if there's nothing to do\n"
266 "-v\t\tBe verbose\n"
267 "-D\t\tList changes, don't commit them\n"
268 "\n"
269 "PCI access options:\n"
270 GENERIC_HELP
271 "\n"
272 "Setting commands:\n"
273 "<device>:\t-s [[[<domain>]:][<bus>]:][<slot>][.[<func>]]\n"
274 "\t|\t-d [<vendor>]:[<device>]\n"
275 "<reg>:\t\t<number>[.(B|W|L)]\n"
276 "     |\t\t<name>\n"
277 "<values>:\t<value>[,<value>...]\n"
278 "<value>:\t<hex>\n"
279 "       |\t<hex>:<mask>\n");
280   exit(1);
281 }
282
283 int
284 main(int argc, char **argv)
285 {
286   enum { STATE_INIT, STATE_GOT_FILTER, STATE_GOT_OP } state = STATE_INIT;
287   struct pci_filter filter;
288   struct pci_dev **selected_devices = NULL;
289   char *opts = GENERIC_OPTIONS ;
290
291   if (argc == 2 && !strcmp(argv[1], "--version"))
292     {
293       puts("setpci version " PCIUTILS_VERSION);
294       return 0;
295     }
296   argc--;
297   argv++;
298
299   pacc = pci_alloc();
300   pacc->error = die;
301
302   while (argc && argv[0][0] == '-')
303     {
304       char *c = argv[0]+1;
305       char *d = c;
306       char *e;
307       while (*c)
308         switch (*c)
309           {
310           case 'v':
311             verbose++;
312             c++;
313             break;
314           case 'f':
315             force++;
316             c++;
317             break;
318           case 'D':
319             demo_mode++;
320             c++;
321             break;
322           case 0:
323             break;
324           default:
325             if (e = strchr(opts, *c))
326               {
327                 char *arg;
328                 c++;
329                 if (e[1] == ':')
330                   {
331                     if (*c)
332                       arg = c;
333                     else if (argc > 1)
334                       {
335                         arg = argv[1];
336                         argc--; argv++;
337                       }
338                     else
339                       usage(NULL);
340                     c = "";
341                   }
342                 else
343                   arg = NULL;
344                 if (!parse_generic_option(*e, pacc, arg))
345                   usage(NULL);
346               }
347             else
348               {
349                 if (c != d)
350                   usage(NULL);
351                 goto next;
352               }
353           }
354       argc--;
355       argv++;
356     }
357 next:
358
359   pci_init(pacc);
360   pci_scan_bus(pacc);
361
362   while (argc)
363     {
364       char *c = argv[0];
365       char *d, *e, *f;
366       int n, i;
367       struct op *op;
368       unsigned long ll;
369       unsigned int lim;
370
371       if (*c == '-')
372         {
373           if (!c[1] || !strchr("sd", c[1]))
374             usage(NULL);
375           if (c[2])
376             d = (c[2] == '=') ? c+3 : c+2;
377           else if (argc > 1)
378             {
379               argc--;
380               argv++;
381               d = argv[0];
382             }
383           else
384             usage(NULL);
385           if (state != STATE_GOT_FILTER)
386             {
387               pci_filter_init(pacc, &filter);
388               state = STATE_GOT_FILTER;
389             }
390           switch (c[1])
391             {
392             case 's':
393               if (d = pci_filter_parse_slot(&filter, d))
394                 die("-s: %s", d);
395               break;
396             case 'd':
397               if (d = pci_filter_parse_id(&filter, d))
398                 die("-d: %s", d);
399               break;
400             default:
401               usage(NULL);
402             }
403         }
404       else if (state == STATE_INIT)
405         usage(NULL);
406       else
407         {
408           if (state == STATE_GOT_FILTER)
409             selected_devices = select_devices(&filter);
410           if (!selected_devices[0] && !force)
411             fprintf(stderr, "setpci: Warning: No devices selected for `%s'.\n", c);
412           state = STATE_GOT_OP;
413           /* look for setting of values and count how many */
414           d = strchr(c, '=');
415           if (d)
416             {
417               *d++ = 0;
418               if (!*d)
419                 usage("Missing value");
420               for(e=d, n=1; *e; e++)
421                 if (*e == ',')
422                   n++;
423               op = xmalloc(sizeof(struct op) + n*sizeof(struct value));
424             }
425           else
426             {
427               n = -1;
428               op = xmalloc(sizeof(struct op));
429             }
430           op->dev_vector = selected_devices;
431           op->num_values = n;
432           e = strchr(c, '.');
433           if (e)
434             {
435               *e++ = 0;
436               if (e[1])
437                 usage("Missing width");
438               switch (*e & 0xdf)
439                 {
440                 case 'B':
441                   op->width = 1; break;
442                 case 'W':
443                   op->width = 2; break;
444                 case 'L':
445                   op->width = 4; break;
446                 default:
447                   usage("Invalid width \"%c\"", *e);
448                 }
449             }
450           else
451             op->width = 1;
452           ll = strtol(c, &f, 16);
453           if (f && *f)
454             {
455               const struct reg_name *r;
456               for(r = pci_reg_names; r->name; r++)
457                 if (!strcasecmp(r->name, c))
458                   break;
459               if (!r->name)
460                 usage("Unknown register \"%s\"", c);
461               if (e && op->width != r->width)
462                 usage("Explicit width doesn't correspond with the named register \"%s\"", c);
463               ll = r->offset;
464               op->width = r->width;
465             }
466           if (ll > 0x1000 || ll + op->width*((n < 0) ? 1 : n) > 0x1000)
467             die("Register number out of range!");
468           if (ll & (op->width - 1))
469             die("Unaligned register address!");
470           op->addr = ll;
471           /* read in all the values to be set */
472           for(i=0; i<n; i++)
473             {
474               e = strchr(d, ',');
475               if (e)
476                 *e++ = 0;
477               ll = strtoul(d, &f, 16);
478               lim = max_values[op->width];
479               if (f && *f && *f != ':')
480                 usage("Invalid value \"%s\"", d);
481               if (ll > lim && ll < ~0UL - lim)
482                 usage("Value \"%s\" is out of range", d);
483               op->values[i].value = ll;
484               if (f && *f == ':')
485                 {
486                   d = ++f;
487                   ll = strtoul(d, &f, 16);
488                   if (f && *f)
489                     usage("Invalid mask \"%s\"", d);
490                   if (ll > lim && ll < ~0UL - lim)
491                     usage("Mask \"%s\" is out of range", d);
492                   op->values[i].mask = ll;
493                   op->values[i].value &= ll;
494                 }
495               else
496                 op->values[i].mask = ~0U;
497               d = e;
498             }
499           *last_op = op;
500           last_op = &op->next;
501           op->next = NULL;
502         }
503       argc--;
504       argv++;
505     }
506   if (state == STATE_INIT)
507     usage("No operation specified");
508
509   scan_ops(first_op);
510   execute(first_op);
511
512   return 0;
513 }