MythTV master
devicelist.py
Go to the documentation of this file.
1# -*- coding: utf-8 -*-
2
3# smolt - Fedora hardware profiler
4#
5# Copyright (C) 2010 Mike McGrath <mmcgrath@redhat.com>
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20
21from builtins import object
22import os
23from hwdata import DeviceMap
24
25
26# Info pulled from - http://www.acm.uiuc.edu/sigops/roll_your_own/7.c.1.html
27DEVICE_CLASS_LIST ={
28 '00' : 'NONE',
29 '0001' : 'VIDEO',
30 '01' : 'STORAGE',
31 '0100' : 'SCSI',
32 '0102' : 'FLOPPY',
33 '0103' : 'IPI',
34 '0104' : 'RAID',
35 '02' : 'NETWORK',
36 '0200' : 'ETHERNET',
37 '0201' : 'TOKEN_RING',
38 '0202' : 'FDDI',
39 '0203' : 'ATM',
40 '03' : 'VIDEO',
41 '04' : 'MULTIMEDIA',
42 '0400' : 'MULTIMEDIA_VIDEO',
43 '0401' : 'MULTIMEDIA_AUDIO',
44 '05' : 'MEMORY',
45 '0500' : 'RAM',
46 '0501' : 'FLASH',
47 '06' : 'BRIDGE',
48 '0600' : 'HOST/PCI',
49 '0601' : 'PCI/ISA',
50 '0602' : 'PCI/EISA',
51 '0603' : 'PCI/MICRO',
52 '0604' : 'PCI/PCI',
53 '0605' : 'PCI/PCMCIA',
54 '0606' : 'PCI/NUBUS',
55 '0607' : 'PCI/CARDBUS',
56 '07' : 'SIMPLE',
57 '070000' : 'XT_SERIAL',
58 '070001' : '16450_SERIAL',
59 '070002' : '16550_SERIAL',
60 '070100' : 'PARALLEL',
61 '070101' : 'BI_PARALLEL',
62 '070102' : 'ECP_PARALLEL',
63 '08' : 'BASE',
64 '080000' : '8259_INTERRUPT',
65 '080001' : 'ISA',
66 '080002' : 'EISA',
67 '080100' : '8237_DMA',
68 '080101' : 'ISA_DMA',
69 '080102' : 'ESA_DMA',
70 '080200' : '8254_TIMER',
71 '080201' : 'ISA_TIMER',
72 '080202' : 'EISA_TIMER',
73 '080300' : 'RTC',
74 '080301' : 'ISA_RTC',
75 '09' : 'INPUT',
76 '0900' : 'KEYBOARD',
77 '0901' : 'PEN',
78 '0902' : 'MOUSE',
79 '0A' : 'DOCKING',
80 '0B' : 'PROCESSOR',
81 '0C' : 'SERIAL',
82 '0C00' : 'FIREWIRE',
83 '0C01' : 'ACCESS',
84 '0C02' : 'SSA',
85 '0C03' : 'USB',
86 'FF' : 'MISC' }
87
88BUS_LIST = [ 'pci', 'usb' ]
89PCI_PATH='/sys/bus/pci/devices/'
90DATA_LIST = [ 'vendor',
91 'device',
92 'subsystem_vendor',
93 'subsystem_device']
94
95def cat(file_name):
96 fd = open(file_name, 'r')
97 results = fd.readlines()
98 fd.close()
99 return results
100
101def get_class(class_id):
102 for i in [ 8, 6, 4 ]:
103 try:
104 return DEVICE_CLASS_LIST[class_id[2:i].upper()]
105 except KeyError:
106 pass
107 return 'NONE'
108
109pci = DeviceMap('pci')
110usb = DeviceMap('usb')
111
112
113
114def device_factory(cls, id):
115 cls = eval(cls.upper() + 'Device')
116 device = cls(id)
117 try:
118 device.process()
119 except OSError:
120 pass
121 except IOError:
122 pass
123 return device
124
125class Device( object ):
126 bus = 'Unknown'
127 def __init__(self, id):
128 self.id = id
129 self.vendorid = None
130 self.deviceid = None
131 self.type = None
132 self.description = 'Unknown'
133 self.subsysvendorid = None
134 self.subsysdeviceid = None
135 self.driver = 'None'
136 def process(self):
137 pass
138
139class PCIDevice( Device ):
140 bus = 'PCI'
141 def process(self):
142 PATH = '/sys/bus/pci/devices/' + self.id + '/'
143 self.vendoridvendorid = int(cat(PATH + 'vendor')[0].strip(), 16)
144 self.deviceiddeviceid = int(cat(PATH + 'device')[0].strip(), 16)
145 self.subsysvendoridsubsysvendorid = int(cat(PATH + 'subsystem_vendor')[0].strip(), 16)
146 self.subsysdeviceidsubsysdeviceid = int(cat(PATH + 'subsystem_device')[0].strip(), 16)
147 self.typetype = get_class(cat(PATH + 'class')[0].strip())
148 self.descriptiondescription = pci.subdevice(self.vendoridvendorid,
149 self.deviceiddeviceid,
152
154 bus = 'USB'
155 def process(self):
156 PATH = '/sys/bus/usb/devices/' + self.id + '/'
157 self.vendoridvendorid = int(cat(PATH + 'idVendor')[0].strip(), 16)
158 self.deviceiddeviceid = int(cat(PATH + 'idProduct')[0].strip(), 16)
159 try:
160 desc = cat(PATH + 'product')[0].strip().decode('ASCII', errors='ignore')
161 except:
162 #The fedora python pkg is broken and doesn't like decode with options, so this is a fallback
163 desc = cat(PATH + 'product')[0].strip()
165 self.deviceiddeviceid,
166 alt=desc)
167
169 devices = {}
170 for bus in BUS_LIST:
171 PATH = '/sys/bus/' + bus + '/devices/'
172 if os.path.exists(PATH):
173 for device in os.listdir(PATH):
174 devices[device] = device_factory(bus, device)
175 return devices
176
A device containing images (ie. USB stick, CD, storage group etc)
def device_factory(cls, id)
Definition: devicelist.py:114