MythTV  master
devicelist.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 
4 # smolt - Fedora hardware profiler
5 #
6 # Copyright (C) 2010 Mike McGrath <mmcgrath@redhat.com>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 
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):
96  fd = open(file, '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().decode()
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 
static int decode(unsigned char *vbiline, int scale0, int scale1)
Definition: cc.cpp:68
def device_factory(cls, id)
Definition: devicelist.py:114
A device containing images (ie. USB stick, CD, storage group etc)