Files
bananarchy/vendor/edl/Library/usb.py
T
Luxferre d7992904bf Bring up nokia-sparkler SMP/cpufreq/lk2nd/cam/haptics, fix timer and EDL USB timeout
- Add 0013 early-SCM 4-core SMP + cpufreq kernel patch and clock-frequency DT nodes
- Complete nokia-sparkler lk2nd port, boot/X11 session startup, 180-deg cached camera, and 157 Hz haptic audio
- Replace root-only timer backend with permissionless sxmo_timer.sh and keypad/hook cancellation
- Fix EDL Firehose rawmode ACK, USB active-config preservation, *+# hint, and 30 s default USB timeout
- Sync a1120 (cocojamboo) prebuilts, docs, tests, and verified checksums in check-rebuild.sh
2026-11-16 11:45:38 +02:00

88 lines
3.3 KiB
Python

import usb.core # pyusb
import usb.util
DEFAULT_USB_TIMEOUT_MS = 30000
class usb_class():
def log(self,level,msg):
if level>1:
if self.debug==True:
print(msg)
else:
print(msg)
def __init__(self,vid=0x05c6, pid=0x9008, Debug=False, timeout_ms=DEFAULT_USB_TIMEOUT_MS):
self.vid=vid
self.pid=pid
self.debug=Debug
self.timeout_ms=timeout_ms
self.connected=False
def connect(self):
self.device = usb.core.find(idVendor=self.vid, idProduct=self.pid)
if self.device is None:
self.log(2, "Couldn't detect the device. Is it connected ?")
return False
try:
if self.device.is_kernel_driver_active(0):
self.device.detach_kernel_driver(0)
except:
pass
try:
self.configuration = self.device.get_active_configuration()
except usb.core.USBError:
self.configuration = None
if self.configuration is None:
self.device.set_configuration()
self.configuration = self.device.get_active_configuration()
self.log(2, self.configuration)
for itf_num in [0]:
itf = usb.util.find_descriptor(self.configuration,
bInterfaceNumber=itf_num)
try:
if self.device.is_kernel_driver_active(itf_num):
self.log(2, "Detaching kernel driver")
self.device.detach_kernel_driver(itf_num)
except:
self.log(2, "No kernel driver supported.")
usb.util.claim_interface(self.device, itf_num)
self.EP_OUT = usb.util.find_descriptor(itf,
# match the first OUT endpoint
custom_match= \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
self.EP_IN = usb.util.find_descriptor(itf,
# match the first OUT endpoint
custom_match= \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_IN)
self.device.default_timeout = self.timeout_ms
self.connected=True
return True
def write(self,command,pktsize=64,timeout=None):
eff_timeout=self.timeout_ms if timeout is None else timeout
pos=0
if command==b'':
self.device.write(self.EP_OUT, b'', eff_timeout)
else:
while pos<len(command):
self.device.write(self.EP_OUT,command[pos:pos+pktsize],eff_timeout)
pos+=pktsize
def read(self,length=0x80000,timeout=None):
eff_timeout=self.timeout_ms if timeout is None else timeout
tmp=b''
while (bytearray(tmp) == b''):
tmp=self.device.read(self.EP_IN, length, eff_timeout)
return bytearray(tmp)