00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 import os
00023 from hwdata import DeviceMap
00024
00025
00026
00027 DEVICE_CLASS_LIST ={
00028 '00' : 'NONE',
00029 '0001' : 'VIDEO',
00030 '01' : 'STORAGE',
00031 '0100' : 'SCSI',
00032 '0102' : 'FLOPPY',
00033 '0103' : 'IPI',
00034 '0104' : 'RAID',
00035 '02' : 'NETWORK',
00036 '0200' : 'ETHERNET',
00037 '0201' : 'TOKEN_RING',
00038 '0202' : 'FDDI',
00039 '0203' : 'ATM',
00040 '03' : 'VIDEO',
00041 '04' : 'MULTIMEDIA',
00042 '0400' : 'MULTIMEDIA_VIDEO',
00043 '0401' : 'MULTIMEDIA_AUDIO',
00044 '05' : 'MEMORY',
00045 '0500' : 'RAM',
00046 '0501' : 'FLASH',
00047 '06' : 'BRIDGE',
00048 '0600' : 'HOST/PCI',
00049 '0601' : 'PCI/ISA',
00050 '0602' : 'PCI/EISA',
00051 '0603' : 'PCI/MICRO',
00052 '0604' : 'PCI/PCI',
00053 '0605' : 'PCI/PCMCIA',
00054 '0606' : 'PCI/NUBUS',
00055 '0607' : 'PCI/CARDBUS',
00056 '07' : 'SIMPLE',
00057 '070000' : 'XT_SERIAL',
00058 '070001' : '16450_SERIAL',
00059 '070002' : '16550_SERIAL',
00060 '070100' : 'PARALLEL',
00061 '070101' : 'BI_PARALLEL',
00062 '070102' : 'ECP_PARALLEL',
00063 '08' : 'BASE',
00064 '080000' : '8259_INTERRUPT',
00065 '080001' : 'ISA',
00066 '080002' : 'EISA',
00067 '080100' : '8237_DMA',
00068 '080101' : 'ISA_DMA',
00069 '080102' : 'ESA_DMA',
00070 '080200' : '8254_TIMER',
00071 '080201' : 'ISA_TIMER',
00072 '080202' : 'EISA_TIMER',
00073 '080300' : 'RTC',
00074 '080301' : 'ISA_RTC',
00075 '09' : 'INPUT',
00076 '0900' : 'KEYBOARD',
00077 '0901' : 'PEN',
00078 '0902' : 'MOUSE',
00079 '0A' : 'DOCKING',
00080 '0B' : 'PROCESSOR',
00081 '0C' : 'SERIAL',
00082 '0C00' : 'FIREWIRE',
00083 '0C01' : 'ACCESS',
00084 '0C02' : 'SSA',
00085 '0C03' : 'USB',
00086 'FF' : 'MISC' }
00087
00088 BUS_LIST = [ 'pci', 'usb' ]
00089 PCI_PATH='/sys/bus/pci/devices/'
00090 DATA_LIST = [ 'vendor',
00091 'device',
00092 'subsystem_vendor',
00093 'subsystem_device']
00094
00095 def cat(file):
00096 fd = open(file, 'r')
00097 results = fd.readlines()
00098 fd.close()
00099 return results
00100
00101 def get_class(class_id):
00102 for i in [ 8, 6, 4 ]:
00103 try:
00104 return DEVICE_CLASS_LIST[class_id[2:i].upper()]
00105 except KeyError:
00106 pass
00107 return 'NONE'
00108
00109 pci = DeviceMap('pci')
00110 usb = DeviceMap('usb')
00111
00112
00113
00114 def device_factory(cls, id):
00115 cls = eval(cls.upper() + 'Device')
00116 device = cls(id)
00117 try:
00118 device.process()
00119 except OSError:
00120 pass
00121 except IOError:
00122 pass
00123 return device
00124
00125 class Device( object ):
00126 bus = 'Unknown'
00127 def __init__(self, id):
00128 self.id = id
00129 self.vendorid = None
00130 self.deviceid = None
00131 self.type = None
00132 self.description = 'Unknown'
00133 self.subsysvendorid = None
00134 self.subsysdeviceid = None
00135 self.driver = 'None'
00136 def process(self):
00137 pass
00138
00139 class PCIDevice( Device ):
00140 bus = 'PCI'
00141 def process(self):
00142 PATH = '/sys/bus/pci/devices/' + self.id + '/'
00143 self.vendorid = int(cat(PATH + 'vendor')[0].strip(), 16)
00144 self.deviceid = int(cat(PATH + 'device')[0].strip(), 16)
00145 self.subsysvendorid = int(cat(PATH + 'subsystem_vendor')[0].strip(), 16)
00146 self.subsysdeviceid = int(cat(PATH + 'subsystem_device')[0].strip(), 16)
00147 self.type = get_class(cat(PATH + 'class')[0].strip())
00148 self.description = pci.subdevice(self.vendorid,
00149 self.deviceid,
00150 self.subsysvendorid,
00151 self.subsysdeviceid)
00152
00153 class USBDevice( Device ):
00154 bus = 'USB'
00155 def process(self):
00156 PATH = '/sys/bus/usb/devices/' + self.id + '/'
00157 self.vendorid = int(cat(PATH + 'idVendor')[0].strip(), 16)
00158 self.deviceid = int(cat(PATH + 'idProduct')[0].strip(), 16)
00159 try:
00160 desc = cat(PATH + 'product')[0].strip().decode('ASCII', errors='ignore')
00161 except:
00162
00163 desc = cat(PATH + 'product')[0].strip().decode()
00164 self.description = usb.device(self.vendorid,
00165 self.deviceid,
00166 alt=desc)
00167
00168 def get_device_list():
00169 devices = {}
00170 for bus in BUS_LIST:
00171 PATH = '/sys/bus/' + bus + '/devices/'
00172 if os.path.exists(PATH):
00173 for device in os.listdir(PATH):
00174 devices[device] = device_factory(bus, device)
00175 return devices
00176