Initial import of pypciscan
[pypcilib.git] / pypciscan.c
1 /* Copyright 2007 The Trustees of Princeton University
2
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
5 are met: 
6
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9       
10 * Redistributions in binary form must reproduce the above
11 copyright notice, this list of conditions and the following
12 disclaimer in the documentation and/or other materials provided
13 with the distribution.
14       
15 * Neither the name of the copyright holder nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
18       
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PRINCETON
23 UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE. 
31
32 */
33
34 #include <Python.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 #include <pci/pci.h>
40
41
42 static PyObject *get_devices(PyObject *self, PyObject *args)
43 {
44         struct pci_access *pacc;
45         struct pci_dev *dev;
46         PyObject *ret;
47         char buf[128];
48
49         if ((pacc = pci_alloc()) == NULL)
50                 return PyErr_SetFromErrno(PyExc_OSError);
51
52         pci_init(pacc);
53
54         pci_scan_bus(pacc);
55
56         ret = PyDict_New();
57         if (!ret)
58                 return PyErr_SetFromErrno(PyExc_OSError);
59
60         for (dev = pacc->devices; dev; dev = dev->next) {
61                 u16 subvendor = -1, subdevice = -1;
62                 PyObject *value;
63
64                 pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS);
65                 if (dev->hdrtype == PCI_HEADER_TYPE_NORMAL) {
66                         subvendor = pci_read_word(dev, PCI_SUBSYSTEM_VENDOR_ID);
67                         subdevice = pci_read_word(dev, PCI_SUBSYSTEM_ID);
68                 }
69
70                 snprintf(buf, sizeof(buf), "%04x:%02x:%02x.%02x", dev->domain, dev->bus, dev->dev, dev->func);
71                 value = Py_BuildValue("iiiii", dev->vendor_id, dev->device_id,
72                                       subdevice, subvendor, dev->device_class);
73                 if (!value)
74                         return NULL;
75                 if (PyDict_SetItemString(ret, buf, value) == -1)
76                         return NULL;
77         }
78
79         return ret;
80 }
81
82
83 static PyMethodDef methods[] = {
84         { "get_devices", get_devices, METH_VARARGS,
85           "Returns a dict of PCI devices, indexed by the domain:bus:dev.func string, "
86           "each item consisting of [vendor, device, subvendor, subdevice, class]" },
87         { NULL, NULL, 0, NULL }
88 };
89
90 PyMODINIT_FUNC
91 initpypciscan(void)
92 {
93         PyObject *mod;
94         mod = Py_InitModule("pypciscan", methods);
95 }