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 
21 from builtins import object
22 import os
23 from hwdata import DeviceMap
24 
25 
26 # Info pulled from - http://www.acm.uiuc.edu/sigops/roll_your_own/7.c.1.html
27 DEVICE_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 
88 BUS_LIST = [ 'pci', 'usb' ]
89 PCI_PATH='/sys/bus/pci/devices/'
90 DATA_LIST = [ 'vendor',
91  'device',
92  'subsystem_vendor',
93  'subsystem_device']
94 
95 def cat(file_name):
96  fd = open(file_name, 'r')
97  results = fd.readlines()
98  fd.close()
99  return results
100 
101 def 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 
109 pci = DeviceMap('pci')
110 usb = DeviceMap('usb')
111 
112 
113 
114 def 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 
125 class 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 
139 class PCIDevice( Device ):
140  bus = 'PCI'
141  def process(self):
142  PATH = '/sys/bus/pci/devices/' + self.id + '/'
143  self.vendorid = int(cat(PATH + 'vendor')[0].strip(), 16)
144  self.deviceid = int(cat(PATH + 'device')[0].strip(), 16)
145  self.subsysvendorid = int(cat(PATH + 'subsystem_vendor')[0].strip(), 16)
146  self.subsysdeviceid = int(cat(PATH + 'subsystem_device')[0].strip(), 16)
147  self.type = get_class(cat(PATH + 'class')[0].strip())
148  self.description = pci.subdevice(self.vendorid,
149  self.deviceid,
150  self.subsysvendorid,
151  self.subsysdeviceid)
152 
153 class USBDevice( Device ):
154  bus = 'USB'
155  def process(self):
156  PATH = '/sys/bus/usb/devices/' + self.id + '/'
157  self.vendorid = int(cat(PATH + 'idVendor')[0].strip(), 16)
158  self.deviceid = 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()
164  self.description = usb.device(self.vendorid,
165  self.deviceid,
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 
hardwareprofile.devicelist.USBDevice.process
def process(self)
Definition: devicelist.py:155
hardwareprofile.hwdata.DeviceMap
Definition: hwdata.py:44
hardwareprofile.devicelist.Device.vendorid
vendorid
Definition: devicelist.py:129
hardwareprofile.devicelist.Device.subsysdeviceid
subsysdeviceid
Definition: devicelist.py:134
hardwareprofile.devicelist.PCIDevice.process
def process(self)
Definition: devicelist.py:141
hardwareprofile.devicelist.Device.type
type
Definition: devicelist.py:131
hardwareprofile.devicelist.get_class
def get_class(class_id)
Definition: devicelist.py:101
hardwareprofile.devicelist.cat
def cat(file_name)
Definition: devicelist.py:95
hardwareprofile.devicelist.Device.deviceid
deviceid
Definition: devicelist.py:130
Device
A device containing images (ie. USB stick, CD, storage group etc)
Definition: imagemanager.cpp:35
hardwareprofile.devicelist.get_device_list
def get_device_list()
Definition: devicelist.py:168
decode
static int decode(unsigned char *vbiline, int scale0, int scale1)
Definition: cc.cpp:67
hardwareprofile.devicelist.Device.subsysvendorid
subsysvendorid
Definition: devicelist.py:133
hardwareprofile.devicelist.Device.__init__
def __init__(self, id)
Definition: devicelist.py:127
hardwareprofile.devicelist.Device.driver
driver
Definition: devicelist.py:135
hardwareprofile.devicelist.USBDevice
Definition: devicelist.py:153
hardwareprofile.devicelist.Device.id
id
Definition: devicelist.py:128
hardwareprofile.devicelist.device_factory
def device_factory(cls, id)
Definition: devicelist.py:114
hardwareprofile.devicelist.Device.description
description
Definition: devicelist.py:132
hardwareprofile.devicelist.Device.process
def process(self)
Definition: devicelist.py:136
hardwareprofile.devicelist.Device
Definition: devicelist.py:125