]> mj.ucw.cz Git - pciutils.git/blob - setpci.c
pread()/pwrite() now should work even with glibc on Alpha.
[pciutils.git] / setpci.c
1 /*
2  *      $Id: setpci.c,v 1.3 1998/06/08 07:57:54 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 #define _GNU_SOURCE
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <asm/byteorder.h>
20
21 #include <asm/unistd.h>
22 #ifdef __GLIBC__
23 #include <syscall-list.h>
24 #endif
25
26 #include "pciutils.h"
27
28 static int force;                       /* Don't complain if no devices match */
29 static int verbose;                     /* Verbosity level */
30 static int demo_mode;                   /* Only show */
31
32 struct device {
33   struct device *next;
34   byte bus, devfn, mark;
35   word vendid, devid;
36   int fd, need_write;
37 };
38
39 static struct device *first_dev;
40
41 struct op {
42   struct op *next;
43   struct device **dev_vector;
44   unsigned int addr;
45   unsigned int width;                   /* Byte width of the access */
46   int num_values;                       /* Number of values to write; <0=read */
47   unsigned int values[0];
48 };
49
50 static struct op *first_op, **last_op = &first_op;
51
52 void *
53 xmalloc(unsigned int howmuch)
54 {
55   void *p = malloc(howmuch);
56   if (!p)
57     {
58       fprintf(stderr, "setpci: Unable to allocate %d bytes of memory\n", howmuch);
59       exit(1);
60     }
61   return p;
62 }
63
64 /*
65  * As libc doesn't support pread/pwrite yet, we have to call them directly
66  * or use lseek/read/write instead.
67  */
68 #ifdef __GLIBC__
69 static int
70 pread(unsigned int fd, void *buf, size_t size, loff_t where)
71 {
72   return syscall(SYS_pread, fd, buf, size, where);
73 }
74
75 static int
76 pwrite(unsigned int fd, void *buf, size_t size, loff_t where)
77 {
78   return syscall(SYS_pwrite, fd, buf, size, where);
79 }
80 #else
81 static _syscall4(int, pread, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
82 static _syscall4(int, pwrite, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
83 #endif
84
85 static void
86 scan_devices(void)
87 {
88   struct device **last = &first_dev;
89   byte line[256];
90   FILE *f;
91
92   if (!(f = fopen(PROC_BUS_PCI "/devices", "r")))
93     {
94       perror(PROC_BUS_PCI "/devices");
95       exit(1);
96     }
97   while (fgets(line, sizeof(line), f))
98     {
99       struct device *d = xmalloc(sizeof(struct device));
100       unsigned int dfn, vend;
101
102       sscanf(line, "%x %x", &dfn, &vend);
103       d->bus = dfn >> 8U;
104       d->devfn = dfn & 0xff;
105       d->vendid = vend >> 16U;
106       d->devid = vend & 0xffff;
107       d->fd = -1;
108       *last = d;
109       last = &d->next;
110     }
111   fclose(f);
112   *last = NULL;
113 }
114
115 static struct device **
116 select_devices(struct pci_filter *filt)
117 {
118   struct device *z, **a, **b;
119   int cnt = 1;
120
121   for(z=first_dev; z; z=z->next)
122     if (z->mark = filter_match(filt, z->bus, z->devfn, z->vendid, z->devid))
123       cnt++;
124   a = b = xmalloc(sizeof(struct device *) * cnt);
125   for(z=first_dev; z; z=z->next)
126     if (z->mark)
127       *a++ = z;
128   *a = NULL;
129   return b;
130 }
131
132 static void
133 exec_op(struct op *op, struct device *dev)
134 {
135   char *mm[] = { NULL, "%02x", "%04x", NULL, "%08x" };
136   char *m = mm[op->width];
137   unsigned int x;
138   int i;
139   __u32 x32;
140   __u16 x16;
141   __u8 x8;
142
143   if (!demo_mode && dev->fd < 0)
144     {
145       char name[64];
146       sprintf(name, PROC_BUS_PCI "/%02x/%02x.%x", dev->bus, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
147       if ((dev->fd = open(name, dev->need_write ? O_RDWR : O_RDONLY)) < 0)
148         {
149           perror(name);
150           exit(1);
151         }
152     }
153
154   if (verbose)
155     printf("%02x:%02x.%x:%02x", dev->bus, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), op->addr);
156   if (op->num_values >= 0)
157     for(i=0; i<op->num_values; i++)
158       {
159         if (verbose)
160           {
161             putchar(' ');
162             printf(m, op->values[i]);
163           }
164         if (demo_mode)
165           continue;
166         switch (op->width)
167           {
168           case 1:
169             x8 = op->values[i];
170             i = pwrite(dev->fd, &x8, 1, op->addr);
171             break;
172           case 2:
173             x16 = __cpu_to_le16(op->values[i]);
174             i = pwrite(dev->fd, &x16, 2, op->addr);
175             break;
176           default:
177             x32 = __cpu_to_le32(op->values[i]);
178             i = pwrite(dev->fd, &x32, 4, op->addr);
179             break;
180           }
181         if (i != (int) op->width)
182           {
183             fprintf(stderr, "Error writing to %02x:%02x.%d: %m", dev->bus, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
184             exit(1);
185           }
186       }
187   else
188     {
189       if (verbose)
190         printf(" = ");
191       if (!demo_mode)
192         {
193           switch (op->width)
194             {
195             case 1:
196               i = pread(dev->fd, &x8, 1, op->addr);
197               x = x8;
198               break;
199             case 2:
200               i = pread(dev->fd, &x16, 2, op->addr);
201               x = __le16_to_cpu(x16);
202               break;
203             default:
204               i = pread(dev->fd, &x32, 4, op->addr);
205               x = __le32_to_cpu(x32);
206               break;
207             }
208           if (i != (int) op->width)
209             {
210               fprintf(stderr, "Error reading from %02x:%02x.%d: %m", dev->bus, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
211               exit(1);
212             }
213           printf(m, x);
214         }
215       else
216         putchar('?');
217     }
218   putchar('\n');
219 }
220
221 static void
222 execute(struct op *op)
223 {
224   struct device **vec = NULL;
225   struct device **pdev, *dev;
226   struct op *oops;
227
228   while (op)
229     {
230       pdev = vec = op->dev_vector;
231       while (dev = *pdev++)
232         for(oops=op; oops && oops->dev_vector == vec; oops=oops->next)
233           exec_op(oops, dev);
234       while (op && op->dev_vector == vec)
235         op = op->next;
236     }
237 }
238
239 static void
240 scan_ops(struct op *op)
241 {
242   struct device **pdev, *dev;
243
244   while (op)
245     {
246       if (op->num_values >= 0)
247         {
248           pdev = op->dev_vector;
249           while (dev = *pdev++)
250             dev->need_write = 1;
251         }
252       op = op->next;
253     }
254 }
255
256 struct reg_name {
257   int offset;
258   int width;
259   char *name;
260 };
261
262 static struct reg_name pci_reg_names[] = {
263   { 0x00, 2, "VENDOR_ID", },
264   { 0x02, 2, "DEVICE_ID", },
265   { 0x04, 2, "COMMAND", },
266   { 0x06, 2, "STATUS", },
267   { 0x08, 1, "REVISION", },
268   { 0x09, 1, "CLASS_PROG", },
269   { 0x0a, 2, "CLASS_DEVICE", },
270   { 0x0c, 1, "CACHE_LINE_SIZE", },
271   { 0x0d, 1, "LATENCY_TIMER", },
272   { 0x0e, 1, "HEADER_TYPE", },
273   { 0x0f, 1, "BIST", },
274   { 0x10, 4, "BASE_ADDRESS_0", },
275   { 0x14, 4, "BASE_ADDRESS_1", },
276   { 0x18, 4, "BASE_ADDRESS_2", },
277   { 0x1c, 4, "BASE_ADDRESS_3", },
278   { 0x20, 4, "BASE_ADDRESS_4", },
279   { 0x24, 4, "BASE_ADDRESS_5", },
280   { 0x28, 4, "CARDBUS_CIS", },
281   { 0x2c, 4, "SUBSYSTEM_VENDOR_ID", },
282   { 0x2e, 2, "SUBSYSTEM_ID", },
283   { 0x30, 4, "ROM_ADDRESS", },
284   { 0x3c, 1, "INTERRUPT_LINE", },
285   { 0x3d, 1, "INTERRUPT_PIN", },
286   { 0x3e, 1, "MIN_GNT", },
287   { 0x3f, 1, "MAX_LAT", },
288   { 0x18, 1, "PRIMARY_BUS", },
289   { 0x19, 1, "SECONDARY_BUS", },
290   { 0x1a, 1, "SUBORDINATE_BUS", },
291   { 0x1b, 1, "SEC_LATENCY_TIMER", },
292   { 0x1c, 1, "IO_BASE", },
293   { 0x1d, 1, "IO_LIMIT", },
294   { 0x1e, 2, "SEC_STATUS", },
295   { 0x20, 2, "MEMORY_BASE", },
296   { 0x22, 2, "MEMORY_LIMIT", },
297   { 0x24, 2, "PREF_MEMORY_BASE", },
298   { 0x26, 2, "PREF_MEMORY_LIMIT", },
299   { 0x28, 4, "PREF_BASE_UPPER32", },
300   { 0x2c, 4, "PREF_LIMIT_UPPER32", },
301   { 0x30, 2, "IO_BASE_UPPER16", },
302   { 0x32, 2, "IO_LIMIT_UPPER16", },
303   { 0x38, 4, "BRIDGE_ROM_ADDRESS", },
304   { 0x3e, 2, "BRIDGE_CONTROL", },
305   { 0x10, 4, "CB_CARDBUS_BASE", },
306   { 0x14, 2, "CB_CAPABILITIES", },
307   { 0x16, 2, "CB_SEC_STATUS", },
308   { 0x18, 1, "CB_BUS_NUMBER", },
309   { 0x19, 1, "CB_CARDBUS_NUMBER", },
310   { 0x1a, 1, "CB_SUBORDINATE_BUS", },
311   { 0x1b, 1, "CB_CARDBUS_LATENCY", },
312   { 0x1c, 4, "CB_MEMORY_BASE_0", },
313   { 0x20, 4, "CB_MEMORY_LIMIT_0", },
314   { 0x24, 4, "CB_MEMORY_BASE_1", },
315   { 0x28, 4, "CB_MEMORY_LIMIT_1", },
316   { 0x2c, 2, "CB_IO_BASE_0", },
317   { 0x2e, 2, "CB_IO_BASE_0_HI", },
318   { 0x30, 2, "CB_IO_LIMIT_0", },
319   { 0x32, 2, "CB_IO_LIMIT_0_HI", },
320   { 0x34, 2, "CB_IO_BASE_1", },
321   { 0x36, 2, "CB_IO_BASE_1_HI", },
322   { 0x38, 2, "CB_IO_LIMIT_1", },
323   { 0x3a, 2, "CB_IO_LIMIT_1_HI", },
324   { 0x40, 2, "CB_SUBSYSTEM_VENDOR_ID", },
325   { 0x42, 2, "CB_SUBSYSTEM_ID", },
326   { 0x44, 4, "CB_LEGACY_MODE_BASE", },
327   { 0x00, 0, NULL }
328 };
329
330 static void usage(void) __attribute__((noreturn));
331
332 static void
333 usage(void)
334 {
335   fprintf(stderr,
336 "Usage: setpci [-fvD] (<device>+ <reg>[=<values>]*)*\n\
337 -f\t\tDon't complain if there's nothing to do\n\
338 -v\t\tBe verbose\n\
339 -D\t\tList changes, don't commit them\n\
340 <device>:\t-s [[<bus>]:][<slot>][.[<func>]]\n\
341 \t|\t-d [<vendor>]:[<device>]\n\
342 <reg>:\t\t<number>[.(B|W|L)]\n\
343      |\t\t<name>\n\
344 <values>:\t<value>[,<value>...]\n\
345 ");
346   exit(1);
347 }
348
349 int
350 main(int argc, char **argv)
351 {
352   enum { STATE_INIT, STATE_GOT_FILTER, STATE_GOT_OP } state = STATE_INIT;
353   struct pci_filter filter;
354   struct device **selected_devices = NULL;
355
356   if (argc == 2 && !strcmp(argv[1], "--version"))
357     {
358       puts("setpci version " PCIUTILS_VERSION);
359       return 0;
360     }
361   argc--;
362   argv++;
363   while (argc && argv[0][0] == '-')
364     {
365       char *c = argv[0]+1;
366       char *d = c;
367       while (*c)
368         switch (*c)
369           {
370           case 'v':
371             verbose++;
372             c++;
373             break;
374           case 'f':
375             force++;
376             c++;
377             break;
378           case 'D':
379             demo_mode++;
380             c++;
381             break;
382           case 0:
383             break;
384           default:
385             if (c != d)
386               usage();
387             goto next;
388           }
389       argc--;
390       argv++;
391     }
392 next:
393
394   scan_devices();
395
396   while (argc)
397     {
398       char *c = argv[0];
399       char *d, *e, *f;
400       int n, i;
401       struct op *op;
402       unsigned long ll, lim;
403
404       if (*c == '-')
405         {
406           if (!c[1] || !strchr("sd", c[1]))
407             usage();
408           if (c[2])
409             d = (c[2] == '=') ? c+3 : c+2;
410           else if (argc)
411             {
412               argc--;
413               argv++;
414               d = argv[0];
415             }
416           else
417             usage();
418           if (state != STATE_GOT_FILTER)
419             {
420               filter_init(&filter);
421               state = STATE_GOT_FILTER;
422             }
423           switch (c[1])
424             {
425             case 's':
426               if (d = filter_parse_slot(&filter, d))
427                 {
428                   fprintf(stderr, "setpci: -s: %s\n", d);
429                   return 1;
430                 }
431               break;
432             case 'd':
433               if (d = filter_parse_id(&filter, d))
434                 {
435                   fprintf(stderr, "setpci: -d: %s\n", d);
436                   return 1;
437                 }
438               break;
439             default:
440               usage();
441             }
442         }
443       else if (state == STATE_INIT)
444         usage();
445       else
446         {
447           if (state == STATE_GOT_FILTER)
448             selected_devices = select_devices(&filter);
449           if (!selected_devices[0] && !force)
450             fprintf(stderr, "setpci: Warning: No devices selected for `%s'.\n", c);
451           state = STATE_GOT_OP;
452           d = strchr(c, '=');
453           if (d)
454             {
455               *d++ = 0;
456               if (!*d)
457                 usage();
458               for(e=d, n=1; *e; e++)
459                 if (*e == ',')
460                   n++;
461               op = xmalloc(sizeof(struct op) + n*sizeof(unsigned int));
462             }
463           else
464             {
465               n = -1;
466               op = xmalloc(sizeof(struct op));
467             }
468           op->dev_vector = selected_devices;
469           op->num_values = n;
470           e = strchr(c, '.');
471           if (e)
472             {
473               *e++ = 0;
474               if (e[1])
475                 usage();
476               switch (*e & 0xdf)
477                 {
478                 case 'B':
479                   op->width = 1; break;
480                 case 'W':
481                   op->width = 2; break;
482                 case 'L':
483                   op->width = 4; break;
484                 default:
485                   usage();
486                 }
487             }
488           else
489             op->width = 1;
490           ll = strtol(c, &f, 16);
491           if (f && *f)
492             {
493               struct reg_name *r;
494               for(r = pci_reg_names; r->name; r++)
495                 if (!strcasecmp(r->name, c))
496                   break;
497               if (!r->name || e)
498                 usage();
499               ll = r->offset;
500               op->width = r->width;
501             }
502           if (ll > 0x100 || ll + op->width*((n < 0) ? 1 : n) > 0x100)
503             {
504               fprintf(stderr, "setpci: Register number out of range!\n");
505               return 1;
506             }
507           if (ll & (op->width - 1))
508             {
509               fprintf(stderr, "setpci: Unaligned register address!\n");
510               return 1;
511             }
512           op->addr = ll;
513           for(i=0; i<n; i++)
514             {
515               e = strchr(d, ',');
516               if (e)
517                 *e++ = 0;
518               ll = strtoul(d, &f, 16);
519               lim = (2 << ((op->width << 3) - 1)) - 1;
520               if (f && *f ||
521                   (ll > lim && ll < ~0UL - lim))
522                 usage();
523               op->values[i] = ll;
524               d = e;
525             }
526           *last_op = op;
527           last_op = &op->next;
528           op->next = NULL;
529         }
530       argc--;
531       argv++;
532     }
533   if (state == STATE_INIT)
534     usage();
535
536   scan_ops(first_op);
537   execute(first_op);
538
539   return 0;
540 }