cros_ec_python.ioports.devportio
This file provides a way to interact with the /dev/port device file
as an alternative to the portio library.
The speed is pretty much on par with the portio library, so there is no
real downside to using this method.
1""" 2This file provides a way to interact with the `/dev/port` device file 3as an alternative to the [portio](https://pypi.org/project/portio/) library. 4 5The speed is pretty much on par with the `portio` library, so there is no 6real downside to using this method. 7""" 8 9from typing import IO 10 11from .baseportio import PortIOClass 12 13 14class DevPortIO(PortIOClass): 15 """ 16 A class to interact with the `/dev/port` device file on Linux. 17 """ 18 19 _dev_port: IO | None = None 20 21 def __init__(self): 22 """ 23 Initialize the `/dev/port` device file. 24 """ 25 self._dev_port = open("/dev/port", "r+b", buffering=0) 26 27 def __del__(self): 28 """ 29 Close the `/dev/port` device file. 30 """ 31 if self._dev_port: 32 self._dev_port.close() 33 34 def out_bytes(self, data: bytes, port: int) -> None: 35 """ 36 Write data to the specified port. 37 :param data: Data to write. 38 :param port: Port to write to. 39 """ 40 self._dev_port.seek(port) 41 self._dev_port.write(data) 42 43 def outb(self, data: int, port: int) -> None: 44 """ 45 Write a byte (8 bit) to the specified port. 46 :param data: Byte to write. 47 :param port: Port to write to. 48 """ 49 self.out_bytes(data.to_bytes(1, "little"), port) 50 51 def outw(self, data: int, port: int) -> None: 52 """ 53 Write a word (16 bit) to the specified port. 54 :param data: Word to write. 55 :param port: Port to write to. 56 """ 57 self.out_bytes(data.to_bytes(2, "little"), port) 58 59 def outl(self, data: int, port: int) -> None: 60 """ 61 Write a long (32 bit) to the specified port. 62 :param data: Long to write. 63 :param port: Port to write to. 64 """ 65 self.out_bytes(data.to_bytes(4, "little"), port) 66 67 def in_bytes(self, port: int, num: int) -> bytes: 68 """ 69 Read data from the specified port. 70 :param port: Port to read from. 71 :param num: Number of bytes to read. 72 :return: Data read. 73 """ 74 self._dev_port.seek(port) 75 return self._dev_port.read(num) 76 77 def inb(self, port: int) -> int: 78 """ 79 Read a byte (8 bit) from the specified port. 80 :param port: Port to read from. 81 :return: Byte read. 82 """ 83 return int.from_bytes(self.in_bytes(port, 1), "little") 84 85 def inw(self, port: int) -> int: 86 """ 87 Read a word (16 bit) from the specified port. 88 :param port: Port to read from. 89 :return: Word read. 90 """ 91 return int.from_bytes(self.in_bytes(port, 2), "little") 92 93 def inl(self, port: int) -> int: 94 """ 95 Read a long (32 bit) from the specified port. 96 :param port: Port to read from. 97 :return: Long read. 98 """ 99 return int.from_bytes(self.in_bytes(port, 4), "little") 100 101 def ioperm(self, port: int, num: int, turn_on: bool) -> None: 102 """ 103 `ioperm` stub function. It's not required for `/dev/port`. 104 """ 105 pass 106 107 def iopl(self, level: int) -> None: 108 """ 109 `iopl` stub function. It's not required for `/dev/port`. 110 """ 111 pass
15class DevPortIO(PortIOClass): 16 """ 17 A class to interact with the `/dev/port` device file on Linux. 18 """ 19 20 _dev_port: IO | None = None 21 22 def __init__(self): 23 """ 24 Initialize the `/dev/port` device file. 25 """ 26 self._dev_port = open("/dev/port", "r+b", buffering=0) 27 28 def __del__(self): 29 """ 30 Close the `/dev/port` device file. 31 """ 32 if self._dev_port: 33 self._dev_port.close() 34 35 def out_bytes(self, data: bytes, port: int) -> None: 36 """ 37 Write data to the specified port. 38 :param data: Data to write. 39 :param port: Port to write to. 40 """ 41 self._dev_port.seek(port) 42 self._dev_port.write(data) 43 44 def outb(self, data: int, port: int) -> None: 45 """ 46 Write a byte (8 bit) to the specified port. 47 :param data: Byte to write. 48 :param port: Port to write to. 49 """ 50 self.out_bytes(data.to_bytes(1, "little"), port) 51 52 def outw(self, data: int, port: int) -> None: 53 """ 54 Write a word (16 bit) to the specified port. 55 :param data: Word to write. 56 :param port: Port to write to. 57 """ 58 self.out_bytes(data.to_bytes(2, "little"), port) 59 60 def outl(self, data: int, port: int) -> None: 61 """ 62 Write a long (32 bit) to the specified port. 63 :param data: Long to write. 64 :param port: Port to write to. 65 """ 66 self.out_bytes(data.to_bytes(4, "little"), port) 67 68 def in_bytes(self, port: int, num: int) -> bytes: 69 """ 70 Read data from the specified port. 71 :param port: Port to read from. 72 :param num: Number of bytes to read. 73 :return: Data read. 74 """ 75 self._dev_port.seek(port) 76 return self._dev_port.read(num) 77 78 def inb(self, port: int) -> int: 79 """ 80 Read a byte (8 bit) from the specified port. 81 :param port: Port to read from. 82 :return: Byte read. 83 """ 84 return int.from_bytes(self.in_bytes(port, 1), "little") 85 86 def inw(self, port: int) -> int: 87 """ 88 Read a word (16 bit) from the specified port. 89 :param port: Port to read from. 90 :return: Word read. 91 """ 92 return int.from_bytes(self.in_bytes(port, 2), "little") 93 94 def inl(self, port: int) -> int: 95 """ 96 Read a long (32 bit) from the specified port. 97 :param port: Port to read from. 98 :return: Long read. 99 """ 100 return int.from_bytes(self.in_bytes(port, 4), "little") 101 102 def ioperm(self, port: int, num: int, turn_on: bool) -> None: 103 """ 104 `ioperm` stub function. It's not required for `/dev/port`. 105 """ 106 pass 107 108 def iopl(self, level: int) -> None: 109 """ 110 `iopl` stub function. It's not required for `/dev/port`. 111 """ 112 pass
A class to interact with the /dev/port device file on Linux.
DevPortIO()
22 def __init__(self): 23 """ 24 Initialize the `/dev/port` device file. 25 """ 26 self._dev_port = open("/dev/port", "r+b", buffering=0)
Initialize the /dev/port device file.
def
out_bytes(self, data: bytes, port: int) -> None:
35 def out_bytes(self, data: bytes, port: int) -> None: 36 """ 37 Write data to the specified port. 38 :param data: Data to write. 39 :param port: Port to write to. 40 """ 41 self._dev_port.seek(port) 42 self._dev_port.write(data)
Write data to the specified port.
Parameters
- data: Data to write.
- port: Port to write to.
def
outb(self, data: int, port: int) -> None:
44 def outb(self, data: int, port: int) -> None: 45 """ 46 Write a byte (8 bit) to the specified port. 47 :param data: Byte to write. 48 :param port: Port to write to. 49 """ 50 self.out_bytes(data.to_bytes(1, "little"), port)
Write a byte (8 bit) to the specified port.
Parameters
- data: Byte to write.
- port: Port to write to.
def
outw(self, data: int, port: int) -> None:
52 def outw(self, data: int, port: int) -> None: 53 """ 54 Write a word (16 bit) to the specified port. 55 :param data: Word to write. 56 :param port: Port to write to. 57 """ 58 self.out_bytes(data.to_bytes(2, "little"), port)
Write a word (16 bit) to the specified port.
Parameters
- data: Word to write.
- port: Port to write to.
def
outl(self, data: int, port: int) -> None:
60 def outl(self, data: int, port: int) -> None: 61 """ 62 Write a long (32 bit) to the specified port. 63 :param data: Long to write. 64 :param port: Port to write to. 65 """ 66 self.out_bytes(data.to_bytes(4, "little"), port)
Write a long (32 bit) to the specified port.
Parameters
- data: Long to write.
- port: Port to write to.
def
in_bytes(self, port: int, num: int) -> bytes:
68 def in_bytes(self, port: int, num: int) -> bytes: 69 """ 70 Read data from the specified port. 71 :param port: Port to read from. 72 :param num: Number of bytes to read. 73 :return: Data read. 74 """ 75 self._dev_port.seek(port) 76 return self._dev_port.read(num)
Read data from the specified port.
Parameters
- port: Port to read from.
- num: Number of bytes to read.
Returns
Data read.
def
inb(self, port: int) -> int:
78 def inb(self, port: int) -> int: 79 """ 80 Read a byte (8 bit) from the specified port. 81 :param port: Port to read from. 82 :return: Byte read. 83 """ 84 return int.from_bytes(self.in_bytes(port, 1), "little")
Read a byte (8 bit) from the specified port.
Parameters
- port: Port to read from.
Returns
Byte read.
def
inw(self, port: int) -> int:
86 def inw(self, port: int) -> int: 87 """ 88 Read a word (16 bit) from the specified port. 89 :param port: Port to read from. 90 :return: Word read. 91 """ 92 return int.from_bytes(self.in_bytes(port, 2), "little")
Read a word (16 bit) from the specified port.
Parameters
- port: Port to read from.
Returns
Word read.
def
inl(self, port: int) -> int:
94 def inl(self, port: int) -> int: 95 """ 96 Read a long (32 bit) from the specified port. 97 :param port: Port to read from. 98 :return: Long read. 99 """ 100 return int.from_bytes(self.in_bytes(port, 4), "little")
Read a long (32 bit) from the specified port.
Parameters
- port: Port to read from.
Returns
Long read.