]> mj.ucw.cz Git - pciutils.git/blob - lib/pci.h
168d9ad6c089976ee86e0980b795372e0f4036f2
[pciutils.git] / lib / pci.h
1 /*
2  *      $Id: pci.h,v 1.4 1999/07/07 11:23:11 mj Exp $
3  *
4  *      The PCI Library
5  *
6  *      Copyright (c) 1997--1999 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 #ifndef _PCI_LIB_H
12 #define _PCI_LIB_H
13
14 #include "config.h"
15
16 #ifdef HAVE_OWN_HEADER_H
17 #include "header.h"
18 #else
19 #include <linux/pci.h>
20 #endif
21
22 /*
23  *      Types
24  */
25
26 #include <linux/types.h>
27
28 typedef __u8 byte;
29 typedef __u8 u8;
30 typedef __u16 word;
31 typedef __u16 u16;
32 typedef __u32 u32;
33
34 #ifdef HAVE_LONG_ADDRESS
35 typedef unsigned long long pciaddr_t;
36 #else
37 typedef unsigned long pciaddr_t;
38 #endif
39
40 /*
41  *      PCI Access Structure
42  */
43
44 struct pci_methods;
45 struct nl_entry;
46
47 #define PCI_ACCESS_AUTO                 0       /* Autodetection (params: none) */
48 #define PCI_ACCESS_PROC_BUS_PCI         1       /* Linux /proc/bus/pci (params: path) */
49 #define PCI_ACCESS_SYSCALLS             2       /* pciconfig_read() syscalls (params: none) */
50 #define PCI_ACCESS_I386_TYPE1           3       /* i386 ports, type 1 (params: none) */
51 #define PCI_ACCESS_I386_TYPE2           4       /* i386 ports, type 2 (params: none) */
52 #define PCI_ACCESS_DUMP                 5       /* Dump file (params: filename) */
53 #define PCI_ACCESS_MAX                  6
54
55 struct pci_access {
56   /* Options you can change: */
57   unsigned int method;                  /* Access method */
58   char *method_params[PCI_ACCESS_MAX];  /* Parameters for the methods */
59   int writeable;                        /* Open in read/write mode */
60   int buscentric;                       /* Bus-centric view of the world */
61   char *id_file_name;                   /* Name of ID list file */
62   int numeric_ids;                      /* Don't resolve device IDs to names */
63   int debugging;                        /* Turn on debugging messages */
64
65   /* Functions you can override: */
66   void (*error)(char *msg, ...);        /* Write error message and quit */
67   void (*warning)(char *msg, ...);      /* Write a warning message */
68   void (*debug)(char *msg, ...);        /* Write a debugging message */
69
70   struct pci_dev *devices;              /* Devices found on this bus */
71
72   /* Fields used internally: */
73   struct pci_methods *methods;
74   char *nl_list;                        /* Name list cache */
75   struct nl_entry **nl_hash;
76   int fd;                               /* proc: fd */
77   int fd_rw;                            /* proc: fd opened read-write */
78   struct pci_dev *cached_dev;           /* proc: device the fd is for */
79 };
80
81 /* Initialize PCI access */
82 struct pci_access *pci_alloc(void);
83 void pci_init(struct pci_access *);
84 void pci_cleanup(struct pci_access *);
85
86 /* Scanning of devices */
87 void pci_scan_bus(struct pci_access *acc);
88 struct pci_dev *pci_get_dev(struct pci_access *acc, int bus, int dev, int func); /* Raw access to specified device */
89 void pci_free_dev(struct pci_dev *);
90
91 /*
92  *      Devices
93  */
94
95 struct pci_dev {
96   struct pci_dev *next;                 /* Next device in the chain */
97   word bus;                             /* Higher byte can select host bridges */
98   byte dev, func;                       /* Device and function */
99
100   /* These fields are set by pci_fill_info() */
101   int known_fields;                     /* Set of info fields already known */
102   word vendor_id, device_id;            /* Identity of the device */
103   int irq;                              /* IRQ number */
104   pciaddr_t base_addr[6];               /* Base addresses */
105   pciaddr_t size[6];                    /* Region sizes */
106   pciaddr_t rom_base_addr;              /* Expansion ROM base address */
107   pciaddr_t rom_size;                   /* Expansion ROM size */
108
109   /* Fields used internally: */
110   struct pci_access *access;
111   struct pci_methods *methods;
112   byte *cache;                          /* Cached information */
113   int cache_len;
114   int hdrtype;                          /* Direct methods: header type */
115   void *aux;                            /* Auxillary data */
116 };
117
118 #define PCI_ADDR_IO_MASK (~(pciaddr_t) 0x3)
119 #define PCI_ADDR_MEM_MASK (~(pciaddr_t) 0xf)
120
121 byte pci_read_byte(struct pci_dev *, int pos); /* Access to configuration space */
122 word pci_read_word(struct pci_dev *, int pos);
123 u32  pci_read_long(struct pci_dev *, int pos);
124 int pci_read_block(struct pci_dev *, int pos, byte *buf, int len);
125 int pci_write_byte(struct pci_dev *, int pos, byte data);
126 int pci_write_word(struct pci_dev *, int pos, word data);
127 int pci_write_long(struct pci_dev *, int pos, u32 data);
128 int pci_write_block(struct pci_dev *, int pos, byte *buf, int len);
129
130 int pci_fill_info(struct pci_dev *, int flags); /* Fill in device information */
131
132 #define PCI_FILL_IDENT          1
133 #define PCI_FILL_IRQ            2
134 #define PCI_FILL_BASES          4
135 #define PCI_FILL_ROM_BASE       8
136 #define PCI_FILL_SIZES          16
137 #define PCI_FILL_RESCAN         0x10000
138
139 void pci_setup_cache(struct pci_dev *, byte *cache, int len);
140
141 /*
142  *      Filters
143  */
144
145 struct pci_filter {
146   int bus, slot, func;                  /* -1 = ANY */
147   int vendor, device;
148 };
149
150 void pci_filter_init(struct pci_access *, struct pci_filter *);
151 char *pci_filter_parse_slot(struct pci_filter *, char *);
152 char *pci_filter_parse_id(struct pci_filter *, char *);
153 int pci_filter_match(struct pci_filter *, struct pci_dev *);
154
155 /*
156  *      Device names
157  */
158
159 char *pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, u32 arg1, u32 arg2);
160 void pci_free_name_list(struct pci_access *a);
161
162 #define PCI_LOOKUP_VENDOR 1
163 #define PCI_LOOKUP_DEVICE 2
164 #define PCI_LOOKUP_CLASS 4
165 #define PCI_LOOKUP_SUBSYSTEM 8
166 #define PCI_LOOKUP_NUMERIC 0x10000
167
168 #endif