|
| 1 | +# SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +"""Agent for controlling FTDI data-bus GPIOs via bit-bang mode.""" |
| 3 | + |
| 4 | +import threading |
| 5 | + |
| 6 | +import usb.core |
| 7 | +import usb.util |
| 8 | + |
| 9 | +SIO_SET_BITMODE = 11 |
| 10 | +SIO_READ_PINS = 12 |
| 11 | +BITMODE_ASYNC_BITBANG = 1 |
| 12 | +OUT_REQTYPE = 0x40 |
| 13 | +IN_REQTYPE = 0xC0 |
| 14 | + |
| 15 | +USB_TIMEOUT = 1000 |
| 16 | +GPIO_MASK = 0xFF |
| 17 | +SUPPORTED_DEVICES = { |
| 18 | + 0x6010: 2, # FT2232C/D/H Dual UART/FIFO IC |
| 19 | + 0x6011: 4, # FT4232H Quad UART/MPSSE IC |
| 20 | + 0x6014: 1, # FT232HL/Q |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +class FTDIGPIO: |
| 25 | + def __init__(self, vendor_id, model_id, busnum, devnum, interface): |
| 26 | + self._validate_device(vendor_id, model_id, interface) |
| 27 | + self._interface = interface - 1 |
| 28 | + self._index = interface |
| 29 | + self._lock = threading.Lock() |
| 30 | + |
| 31 | + self._dev = self._find_device(vendor_id, model_id, busnum, devnum) |
| 32 | + self._detach_kernel_driver() |
| 33 | + try: |
| 34 | + cfg = self._dev.get_active_configuration() |
| 35 | + except usb.core.USBError: |
| 36 | + self._dev.set_configuration() |
| 37 | + self._detach_kernel_driver() |
| 38 | + cfg = self._dev.get_active_configuration() |
| 39 | + |
| 40 | + intf = cfg[(self._interface, 0)] |
| 41 | + usb.util.claim_interface(self._dev, self._interface) |
| 42 | + self._ep_out = usb.util.find_descriptor( |
| 43 | + intf, |
| 44 | + custom_match=lambda ep: usb.util.endpoint_direction(ep.bEndpointAddress) == usb.util.ENDPOINT_OUT, |
| 45 | + ) |
| 46 | + if self._ep_out is None: |
| 47 | + raise ValueError("FTDI output endpoint not found") |
| 48 | + |
| 49 | + def close(self): |
| 50 | + try: |
| 51 | + try: |
| 52 | + usb.util.release_interface(self._dev, self._interface) |
| 53 | + except usb.core.USBError: |
| 54 | + pass |
| 55 | + finally: |
| 56 | + usb.util.dispose_resources(self._dev) |
| 57 | + |
| 58 | + def _detach_kernel_driver(self): |
| 59 | + if self._dev.is_kernel_driver_active(self._interface): |
| 60 | + self._dev.detach_kernel_driver(self._interface) |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def _validate_device(vendor_id, model_id, interface): |
| 64 | + if vendor_id != 0x0403 or model_id not in SUPPORTED_DEVICES: |
| 65 | + raise ValueError("Unsupported FTDI GPIO device") |
| 66 | + if not 1 <= interface <= SUPPORTED_DEVICES[model_id]: |
| 67 | + raise ValueError("FTDI GPIO interface is not supported by this device") |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + def _find_device(vendor_id, model_id, busnum, devnum): |
| 71 | + for dev in usb.core.find(find_all=True, idVendor=vendor_id, idProduct=model_id): |
| 72 | + if dev.bus == busnum and dev.address == devnum: |
| 73 | + return dev |
| 74 | + raise ValueError("FTDI device not found") |
| 75 | + |
| 76 | + def _ctrl_out(self, request, value): |
| 77 | + self._dev.ctrl_transfer(OUT_REQTYPE, request, value, self._index, None, USB_TIMEOUT) |
| 78 | + |
| 79 | + def _ctrl_in(self, request, value, length): |
| 80 | + return bytes(self._dev.ctrl_transfer(IN_REQTYPE, request, value, self._index, length, USB_TIMEOUT)) |
| 81 | + |
| 82 | + def _set_bitmode(self, mask, mode): |
| 83 | + self._ctrl_out(SIO_SET_BITMODE, mask | (mode << 8)) |
| 84 | + |
| 85 | + def _write(self, data): |
| 86 | + self._ep_out.write(bytes(data), USB_TIMEOUT) |
| 87 | + |
| 88 | + @staticmethod |
| 89 | + def _validate_index(index): |
| 90 | + if not 0 <= index <= 7: |
| 91 | + raise ValueError("FTDI bit-bang GPIO only supports indexes 0-7") |
| 92 | + |
| 93 | + def _read_gpio_byte(self): |
| 94 | + data = self._ctrl_in(SIO_READ_PINS, 0, 1) |
| 95 | + if not data: |
| 96 | + raise TimeoutError("FTDI GPIO read returned no data") |
| 97 | + return data[0] |
| 98 | + |
| 99 | + def get(self, index): |
| 100 | + self._validate_index(index) |
| 101 | + with self._lock: |
| 102 | + value = self._read_gpio_byte() |
| 103 | + return bool(value & (1 << (index % 8))) |
| 104 | + |
| 105 | + def set(self, index, status): |
| 106 | + self._validate_index(index) |
| 107 | + mask = 1 << index |
| 108 | + with self._lock: |
| 109 | + output = self._read_gpio_byte() |
| 110 | + if status: |
| 111 | + output |= mask |
| 112 | + else: |
| 113 | + output &= ~mask |
| 114 | + self._set_bitmode(GPIO_MASK, BITMODE_ASYNC_BITBANG) |
| 115 | + self._write([output]) |
| 116 | + |
| 117 | + |
| 118 | +def _run_with_device(vendor_id, model_id, busnum, devnum, interface, callback): |
| 119 | + device = FTDIGPIO(vendor_id, model_id, busnum, devnum, interface) |
| 120 | + try: |
| 121 | + return callback(device) |
| 122 | + finally: |
| 123 | + device.close() |
| 124 | + |
| 125 | + |
| 126 | +def handle_get(vendor_id, model_id, busnum, devnum, interface, index): |
| 127 | + return _run_with_device( |
| 128 | + vendor_id, model_id, busnum, devnum, interface, |
| 129 | + lambda device: device.get(int(index)), |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +def handle_set(vendor_id, model_id, busnum, devnum, interface, index, status): |
| 134 | + _run_with_device( |
| 135 | + vendor_id, model_id, busnum, devnum, interface, |
| 136 | + lambda device: device.set(int(index), bool(status)), |
| 137 | + ) |
| 138 | + return True |
| 139 | + |
| 140 | + |
| 141 | +def handle_close(): |
| 142 | + return True |
| 143 | + |
| 144 | + |
| 145 | +methods = { |
| 146 | + "get": handle_get, |
| 147 | + "set": handle_set, |
| 148 | + "close": handle_close, |
| 149 | +} |
0 commit comments