@@ -15,14 +15,19 @@ class CrosEcLpc(CrosEcClass):
1515 Class to interact with the EC using the LPC interface.
1616 """
1717
18- def __init__ (self , init : bool = True , address : Int32 = None , portio : PortIO = PortIO ()):
18+ def __init__ (
19+ self , init : bool = True , address : Int32 = None , portio : PortIO | None = None
20+ ):
1921 """
2022 Detect and initialise the EC.
2123 :param init: Whether to initialise the EC on creation. Default is True.
2224 :param address: Specify a custom memmap address, will be detected if not specified.
2325 :param portio: PortIO object to use. Default is auto-detected.
2426 """
2527
28+ if portio is None :
29+ portio = PortIO ()
30+
2631 self .portio : PortIO = portio
2732 """PortIO object to use."""
2833
@@ -39,7 +44,10 @@ def detect() -> bool:
3944 """
4045 with open ("/proc/ioports" , "r" ) as f :
4146 for line in f :
42- if line .lstrip ()[:4 ] in (format (EC_LPC_ADDR_MEMMAP , "04x" ), format (EC_LPC_ADDR_MEMMAP_FWAMD , "04x" )):
47+ if line .lstrip ()[:4 ] in (
48+ format (EC_LPC_ADDR_MEMMAP , "04x" ),
49+ format (EC_LPC_ADDR_MEMMAP_FWAMD , "04x" ),
50+ ):
4351 return True
4452
4553 @staticmethod
@@ -53,10 +61,12 @@ def find_address(*addresses, portio: PortIO = PortIO()) -> int | None:
5361 if res := portio .ioperm (a , EC_MEMMAP_SIZE , True ):
5462 if res == errno .EPERM :
5563 raise PermissionError ("Permission denied. Try running as root." )
56- warnings .warn (f"ioperm returned { errno .errorcode [res ]} ({ res } ), skipping address { a } ..." )
64+ warnings .warn (
65+ f"ioperm returned { errno .errorcode [res ]} ({ res } ), skipping address { a } ..."
66+ )
5767 continue
5868 # Check for 'EC' in memory map
59- if portio .inw (a + EC_MEMMAP_ID ) == int .from_bytes (b'EC' , "little" ):
69+ if portio .inw (a + EC_MEMMAP_ID ) == int .from_bytes (b"EC" , "little" ):
6070 # Found it!
6171 return a
6272 else :
@@ -71,15 +81,23 @@ def ec_init(self) -> None:
7181 """
7282 # Find memmap address
7383 if self .address is None :
74- self .address = self .find_address (EC_LPC_ADDR_MEMMAP , EC_LPC_ADDR_MEMMAP_FWAMD , portio = self .portio )
84+ self .address = self .find_address (
85+ EC_LPC_ADDR_MEMMAP , EC_LPC_ADDR_MEMMAP_FWAMD , portio = self .portio
86+ )
7587 # find_address will leave ioperm enabled for the memmap
7688 if self .address is None :
7789 raise OSError ("Could not find EC!" )
7890
7991 # Request I/O permissions
80- if (res := self .portio .ioperm (EC_LPC_ADDR_HOST_DATA , EC_MEMMAP_SIZE , True )) or \
81- (res := self .portio .ioperm (EC_LPC_ADDR_HOST_CMD , EC_MEMMAP_SIZE , True )) or \
82- (res := self .portio .ioperm (EC_LPC_ADDR_HOST_PACKET , EC_LPC_HOST_PACKET_SIZE , True )):
92+ if (
93+ (res := self .portio .ioperm (EC_LPC_ADDR_HOST_DATA , EC_MEMMAP_SIZE , True ))
94+ or (res := self .portio .ioperm (EC_LPC_ADDR_HOST_CMD , EC_MEMMAP_SIZE , True ))
95+ or (
96+ res := self .portio .ioperm (
97+ EC_LPC_ADDR_HOST_PACKET , EC_LPC_HOST_PACKET_SIZE , True
98+ )
99+ )
100+ ):
83101 if res == errno .EPERM :
84102 raise PermissionError ("Permission denied. Try running as root." )
85103 else :
@@ -95,7 +113,9 @@ def ec_init(self) -> None:
95113 raise OSError ("No EC detected. Invalid status." )
96114
97115 # Check for 'EC' in memory map
98- if self .portio .inw (self .address + EC_MEMMAP_ID ) != int .from_bytes (b'EC' , "little" ):
116+ if self .portio .inw (self .address + EC_MEMMAP_ID ) != int .from_bytes (
117+ b"EC" , "little"
118+ ):
99119 raise OSError ("Invalid EC signature." )
100120
101121 self .ec_get_cmd_version ()
@@ -111,8 +131,15 @@ def wait_for_ec(self, status_addr: Int32 = EC_LPC_ADDR_HOST_CMD) -> None:
111131 while self .portio .inb (status_addr ) & EC_LPC_STATUS_BUSY_MASK :
112132 pass
113133
114- def ec_command_v2 (self , version : UInt8 , command : UInt32 , outsize : UInt16 , insize : UInt32 , data : bytes = None ,
115- warn : bool = True ):
134+ def ec_command_v2 (
135+ self ,
136+ version : UInt8 ,
137+ command : UInt32 ,
138+ outsize : UInt16 ,
139+ insize : UInt32 ,
140+ data : bytes = None ,
141+ warn : bool = True ,
142+ ):
116143 """
117144 Send a command to the EC and return the response. Uses the v2 command protocol over LPC. UNTESTED!
118145 :param version: Command version number (often 0).
@@ -123,10 +150,15 @@ def ec_command_v2(self, version: UInt8, command: UInt32, outsize: UInt16, insize
123150 :param warn: Whether to warn if the response size is not as expected. Default is True.
124151 :return: Response from the EC.
125152 """
126- warnings .warn ("Support for v2 commands haven't been tested! Open an issue on github if it does "
127- "or doesn't work: https://github.com/Steve-Tech/CrOS_EC_Python/issues" , RuntimeWarning )
153+ warnings .warn (
154+ "Support for v2 commands haven't been tested! Open an issue on github if it does "
155+ "or doesn't work: https://github.com/Steve-Tech/CrOS_EC_Python/issues" ,
156+ RuntimeWarning ,
157+ )
128158 csum = 0
129- args = bytearray (struct .pack ("BBBB" , EC_HOST_ARGS_FLAG_FROM_HOST , version , outsize , csum ))
159+ args = bytearray (
160+ struct .pack ("BBBB" , EC_HOST_ARGS_FLAG_FROM_HOST , version , outsize , csum )
161+ )
130162 # (flags: UInt8, command_version: UInt8, data_size: UInt8, checksum: UInt8)
131163
132164 # Copy data and start checksum
@@ -138,7 +170,7 @@ def ec_command_v2(self, version: UInt8, command: UInt32, outsize: UInt16, insize
138170 for i in range (len (args )):
139171 csum += args [i ]
140172
141- args [3 ] = csum & 0xff
173+ args [3 ] = csum & 0xFF
142174
143175 # Copy header
144176 for i in range (len (args )):
@@ -168,21 +200,31 @@ def ec_command_v2(self, version: UInt8, command: UInt32, outsize: UInt16, insize
168200 raise IOError ("Invalid response!" )
169201
170202 if response [2 ] != insize and warn :
171- warnings .warn (f"Expected { insize } bytes, got { response [2 ]} back from EC" , RuntimeWarning )
203+ warnings .warn (
204+ f"Expected { insize } bytes, got { response [2 ]} back from EC" ,
205+ RuntimeWarning ,
206+ )
172207
173208 # Read back data
174209 data = bytearray ()
175210 for i in range (response [2 ]):
176211 data .append (self .portio .inb (EC_LPC_ADDR_HOST_PARAM + i ))
177212 csum += data [i ]
178213
179- if response [3 ] != (csum & 0xff ):
214+ if response [3 ] != (csum & 0xFF ):
180215 raise IOError ("Checksum error!" )
181216
182217 return bytes (data )
183218
184- def ec_command_v3 (self , version : UInt8 , command : UInt32 , outsize : UInt16 , insize : UInt32 , data : bytes = None ,
185- warn : bool = True ) -> bytes :
219+ def ec_command_v3 (
220+ self ,
221+ version : UInt8 ,
222+ command : UInt32 ,
223+ outsize : UInt16 ,
224+ insize : UInt32 ,
225+ data : bytes = None ,
226+ warn : bool = True ,
227+ ) -> bytes :
186228 """
187229 Send a command to the EC and return the response. Uses the v3 command protocol over LPC.
188230 :param version: Command version number (often 0).
@@ -194,7 +236,11 @@ def ec_command_v3(self, version: UInt8, command: UInt32, outsize: UInt16, insize
194236 :return: Response from the EC.
195237 """
196238 csum = 0
197- request = bytearray (struct .pack ("BBHBxH" , EC_HOST_REQUEST_VERSION , csum , command , version , outsize ))
239+ request = bytearray (
240+ struct .pack (
241+ "BBHBxH" , EC_HOST_REQUEST_VERSION , csum , command , version , outsize
242+ )
243+ )
198244 # (struct_version: UInt8, checksum: UInt8, command: UInt16,
199245 # command_version: UInt8, reserved: UInt8, data_len: UInt16)
200246
@@ -211,7 +257,7 @@ def ec_command_v3(self, version: UInt8, command: UInt32, outsize: UInt16, insize
211257 for i in range (len (request )):
212258 csum += request [i ]
213259
214- request [1 ] = (- csum ) & 0xff
260+ request [1 ] = (- csum ) & 0xFF
215261
216262 # Copy header
217263 for i in range (len (request )):
@@ -245,15 +291,18 @@ def ec_command_v3(self, version: UInt8, command: UInt32, outsize: UInt16, insize
245291 raise IOError ("Invalid response!" )
246292
247293 if response [3 ] != insize and warn :
248- warnings .warn (f"Expected { insize } bytes, got { response [3 ]} back from EC" , RuntimeWarning )
294+ warnings .warn (
295+ f"Expected { insize } bytes, got { response [3 ]} back from EC" ,
296+ RuntimeWarning ,
297+ )
249298
250299 # Read back data
251300 data = bytearray ()
252301 for i in range (response [3 ]):
253302 data .append (self .portio .inb (EC_LPC_ADDR_HOST_PACKET + len (data_out ) + i ))
254303 csum += data [i ]
255304
256- if csum & 0xff :
305+ if csum & 0xFF :
257306 raise IOError ("Checksum error!" )
258307
259308 return bytes (data )
0 commit comments