scanpci: Build fix for systems without <err.h>
[platform/upstream/libpciaccess.git] / scanpci / scanpci.c
1 /*
2  * (C) Copyright IBM Corporation 2006
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <unistd.h>
32
33 #ifdef HAVE_ERR_H
34 #include <err.h>
35 #else
36 # include <errno.h>
37 # include <string.h>
38 # define err(exitcode, format, args...) \
39    errx(exitcode, format ": %s", ## args, strerror(errno))
40 # define errx(exitcode, format, args...) \
41    { warnx(format, ## args); exit(exitcode); }
42 # define warn(format, args...) \
43    warnx(format ": %s", ## args, strerror(errno))
44 # define warnx(format, args...) \
45    fprintf(stderr, format "\n", ## args)
46 #endif
47
48 #include "pciaccess.h"
49
50
51 static void
52 print_pci_bridge( const struct pci_bridge_info * info )
53 {
54     printf( "  Bus: primary=%02x, secondary=%02x, subordinate=%02x, "
55             "sec-latency=%u\n",
56             info->primary_bus,
57             info->secondary_bus,
58             info->subordinate_bus,
59             info->secondary_latency_timer );
60     printf( "  I/O behind bridge: %08x-%08x\n",
61             info->io_base,
62             info->io_limit );
63     printf( "  Memory behind bridge: %08x-%08x\n",
64             info->mem_base,
65             info->mem_limit );
66     printf( "  Prefetchable memory behind bridge: %08llx-%08llx\n",
67             info->prefetch_mem_base,
68             info->prefetch_mem_limit );
69 }
70
71 static void
72 print_pci_device( struct pci_device * dev, int verbose )
73 {
74     const char * dev_name;
75     const char * vend_name;
76
77     vend_name = pci_device_get_vendor_name( dev );
78     dev_name = pci_device_get_device_name( dev );
79     if ( dev_name == NULL ) {
80         dev_name = "Device unknown";
81     }
82
83     printf("\npci ");
84     if (dev->domain != 0)
85         printf("domain 0x%04x ", dev->domain);
86     printf("bus 0x%04x cardnum 0x%02x function 0x%02x:"
87            " vendor 0x%04x device 0x%04x\n",
88            dev->bus,
89            dev->dev,
90            dev->func,
91            dev->vendor_id,
92            dev->device_id );
93     if ( vend_name != NULL ) {
94         printf( " %s %s\n", vend_name, dev_name );
95     }
96     else {
97         printf( " %s\n", dev_name );
98     }
99
100     if ( verbose ) {
101         unsigned   i;
102         uint16_t  command, status;
103         uint8_t   bist;
104         uint8_t   header_type;
105         uint8_t   latency_timer;
106         uint8_t   cache_line_size;
107         uint8_t   max_latency;
108         uint8_t   min_grant;
109         uint8_t   int_pin;
110
111
112         vend_name = pci_device_get_subvendor_name( dev );
113         dev_name = pci_device_get_subdevice_name( dev );
114         if ( dev_name == NULL ) {
115             dev_name = "Card unknown";
116         }
117
118         printf( " CardVendor 0x%04x card 0x%04x (",
119                 dev->subvendor_id,
120                 dev->subdevice_id );
121         if ( vend_name != NULL ) {
122             printf( "%s, %s)\n", vend_name, dev_name );
123         }
124         else {
125             printf( "%s)\n", dev_name );
126         }
127
128         pci_device_cfg_read_u16( dev, & command, 4 );
129         pci_device_cfg_read_u16( dev, & status,  6 );
130         printf( "  STATUS    0x%04x  COMMAND 0x%04x\n",
131                 status,
132                 command );
133         printf( "  CLASS     0x%02x 0x%02x 0x%02x  REVISION 0x%02x\n",
134                 (dev->device_class >> 16) & 0x0ff,
135                 (dev->device_class >>  8) & 0x0ff,
136                 (dev->device_class >>  0) & 0x0ff,
137                 dev->revision );
138
139         pci_device_cfg_read_u8( dev, & cache_line_size, 12 );
140         pci_device_cfg_read_u8( dev, & latency_timer, 13 );
141         pci_device_cfg_read_u8( dev, & header_type, 14 );
142         pci_device_cfg_read_u8( dev, & bist, 15 );
143
144         printf( "  BIST      0x%02x  HEADER 0x%02x  LATENCY 0x%02x  CACHE 0x%02x\n",
145                 bist,
146                 header_type,
147                 latency_timer,
148                 cache_line_size );
149
150         pci_device_probe( dev );
151         for ( i = 0 ; i < 6 ; i++ ) {
152             if ( dev->regions[i].base_addr != 0 ) {
153                 printf( "  BASE%u     0x%08x SIZE %d  %s",
154                         i,
155                         (intptr_t) dev->regions[i].base_addr,
156                         (size_t) dev->regions[i].size,
157                         (dev->regions[i].is_IO) ? "I/O" : "MEM" );
158
159                 if ( ! dev->regions[i].is_IO ) {
160                     if ( dev->regions[i].is_prefetchable ) {
161                         printf( " PREFETCHABLE" );
162                     }
163                 }
164
165                 printf( "\n" );
166             }
167         }
168
169         if ( dev->rom_size ) {
170             printf( "  BASEROM   0x%08x  addr 0x%08x\n",
171                     0, 0 );
172         }
173
174         pci_device_cfg_read_u8( dev, & int_pin, 61 );
175         pci_device_cfg_read_u8( dev, & min_grant, 62 );
176         pci_device_cfg_read_u8( dev, & max_latency, 63 );
177
178         printf( "  MAX_LAT   0x%02x  MIN_GNT 0x%02x  INT_PIN 0x%02x  INT_LINE 0x%02x\n",
179                 max_latency,
180                 min_grant,
181                 int_pin,
182                 dev->irq );
183
184         if ( (dev->device_class >> 16) == 0x06 ) {
185             const void * info;
186
187             if ( (info = pci_device_get_bridge_info(dev)) != NULL ) {
188                 print_pci_bridge( (const struct pci_bridge_info *) info );
189             }
190             else if ( (info = pci_device_get_pcmcia_bridge_info(dev)) != NULL ) {
191                 /* Nothing yet. */
192             }
193         }
194     }
195 }
196
197
198 int main( int argc, char ** argv )
199 {
200     struct pci_device_iterator * iter;
201     struct pci_device * dev;
202     int ret;
203     int verbose = 0;
204     int c;
205     int errors = 0;
206
207     while ((c = getopt(argc, argv, "v")) != -1) {
208         switch (c) {
209         case 'v':
210             verbose = 1;
211             break;
212         case '?':
213             errors++;
214         }
215     }
216     if (errors != 0) {
217         fprintf(stderr, "usage: %s [-v]\n", argv[0]);
218         exit(2);
219     }
220
221     ret = pci_system_init();
222     if (ret != 0)
223         err(1, "Couldn't initialize PCI system");
224
225     iter = pci_slot_match_iterator_create( NULL );
226
227     while ( (dev = pci_device_next( iter )) != NULL ) {
228         print_pci_device( dev, verbose );
229     }
230
231     pci_system_cleanup();
232     return 0;
233 }