1 # Copyright 2008 The Trustees of Princeton University
2 # Author: Daniel Hokka Zakrisson
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
18 # * Neither the name of the copyright holder nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PRINCETON
26 # UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
32 # WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
35 PCI_BASE_CLASS_NETWORK=0x02L
36 PCI_BASE_CLASS_STORAGE=0x01L
40 """ This is a replacement to the pypciscan library."""
42 pci_cmd = os.popen("""/sbin/lspci -nvm | sed -e 's/\t/ /g' -e 's/ Class / /' -e 's/^/"/' -e 's/$/"/' -e 's/$/,/' -e 's/^"",$/],[/'""", 'r')
43 pci_str = "[" + pci_cmd.read() + "]"
44 pci_list = eval(pci_str)
47 # convert each entry into a dict. and convert strings to ints.
53 # There are two 'device' fields in the output. Append
54 # 'addr' for the bus address, identified by the extra ':'.
57 key = s[0].lower() + "addr"
59 value = int(s[1].strip(), 16)
64 pci_devlist.append(rec)
67 # convert this list of devices into the format expected by the
68 # consumer of get_devices()
69 for dev in pci_devlist:
70 if 'deviceaddr' not in dev:
73 subdev = dev.get('sdevice',PCI_ANY)
74 subvend = dev.get('svendor',PCI_ANY)
75 progif = dev.get('progif',0)
77 value = (dev['vendor'], dev['device'], subvend, subdev, dev['class'] << 8 | progif)
78 ret[dev['deviceaddr']] = value
82 # for convenience for the clients of pypci