0001 ### BITPIM 0002 ### 0003 ### Copyright (C) 2003-2004 Roger Binns <rogerb@rogerbinns.com> 0004 ### 0005 ### This program is free software; you can redistribute it and/or modify 0006 ### it under the terms of the BitPim license as detailed in the LICENSE file. 0007 ### 0008 ### $Id: usbscan.py 3577 2006-09-15 23:28:56Z djpham $ 0009 0010 "Scans the USB busses in the same way that comscan scans comm ports" 0011 0012 version="$Revision: 3577 $" 0013 0014 try: 0015 import native.usb as usb 0016 except ImportError: 0017 usb=None 0018 0019 import guihelper 0020 import usb_ids 0021 0022 ids=None 0023 needdriver=None 0024 0025 def usbscan(*args, **kwargs): 0026 0027 if usb is None: 0028 return [] 0029 0030 global ids 0031 if ids is None: 0032 ids=usb_ids.usb_ids() 0033 ids.add_data(guihelper.getresourcefile("usb.ids")) 0034 ids.add_data(guihelper.getresourcefile("bitpim_usb.ids")) 0035 global needdriver 0036 if needdriver is None: 0037 needdriver=[] 0038 for line in open(guihelper.getresourcefile("usb_needdriver.ids"), "rt"): 0039 line=line.strip() 0040 if line.startswith("#") or len(line)==0: 0041 continue 0042 prod,vend,iface=[int(x, 16) for x in line.split()] 0043 needdriver.append( (prod,vend,iface) ) 0044 0045 res=[] 0046 usb.UpdateLists() 0047 for bus in usb.AllBusses(): 0048 for device in bus.devices(): 0049 for iface in device.interfaces(): 0050 seenin=False 0051 seenout=False 0052 for ep in iface.endpoints(): 0053 if ep.isbulk(): 0054 if ep.direction()==ep.IN: 0055 seenin=True 0056 else: 0057 seenout=True 0058 if seenin and seenout: 0059 # we now have a device/interface that has bidirectional bulk endpoints 0060 name="usb::%s::%s::%d" % (bus.name(), device.name(), iface.number()) 0061 active=True 0062 available=False 0063 try: 0064 iface.openbulk().close() 0065 available=True 0066 except: 0067 pass 0068 v={'name': name, 'active': active, 'available': available, 0069 'libusb': True, 0070 'usb-vendor#': device.vendor(), 'usb-product#': device.product(), 0071 'usb-interface#': iface.number(), 0072 'VID': '0x%04X'%device.vendor(), 0073 'PID': '0x%04X'%device.product() } 0074 0075 if ( device.vendor(), device.product(), iface.number() ) in needdriver: 0076 v["available"]=False 0077 v['driver-required']=True 0078 0079 vend,prod,i=ids.lookupdevice(device.vendor(), device.product(), iface.number()) 0080 if vend is None: 0081 vend="#%04X" % (device.vendor(),) 0082 else: 0083 v['usb-vendor']=vend 0084 if prod is None: 0085 prod="#%04X" % (device.product(),) 0086 else: 0087 v['usb-product']=prod 0088 if i is None: 0089 i="#%02X" % (iface.number(),) 0090 else: 0091 v['usb-interface']=i 0092 hwinstance="USB Device - Vendor %s, Product %s, (Interface %s)" % \ 0093 (vend, prod, i) 0094 v['description']=hwinstance 0095 0096 prot=" / ".join([val for val in ids.lookupclass(*(iface.classdetails())) if val is not None]) 0097 if len(prot): 0098 v["protocol"]=prot 0099 0100 for n,i in ("usb-vendorstring", device.vendorstring), \ 0101 ("usb-productstring", device.productstring), \ 0102 ("usb-serialnumber", device.serialnumber): 0103 try: 0104 x=i() 0105 if x is not None: 0106 v[n]=x 0107 except: 0108 pass 0109 res.append(v) 0110 return res 0111 0112 def isusbsupported(): 0113 return usb is not None 0114 0115 if __name__=="__main__": 0116 res=usbscan() 0117 0118 output="UsbScan "+version+"\n\n" 0119 0120 for r in res: 0121 rkeys=r.keys() 0122 rkeys.sort() 0123 0124 output+=r['name']+":\n" 0125 offset=0 0126 for rk in rkeys: 0127 if rk=='name': continue 0128 v=r[rk] 0129 if not isinstance(v, type("")): v=`v` 0130 op=' %s: %s ' % (rk, v) 0131 if offset+len(op)>78: 0132 output+="\n"+op 0133 offset=len(op)+1 0134 else: 0135 output+=op 0136 offset+=len(op) 0137 0138 if output[-1]!="\n": 0139 output+="\n" 0140 output+="\n" 0141 offset=0 0142 0143 print output 0144 0145
Generated by PyXR 0.9.4