Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | // SPDX-License-Identifier: GPL-2.0+ /* * Copyright (C) 2025 Canonical Ltd * * Sandbox TKey driver for testing TKey functionality in sandbox * Communicates with TKey devices via /dev/ttyACM0 */ #define LOG_CATEGORY UCLASS_TKEY #include <dm.h> #include <errno.h> #include <log.h> #include <malloc.h> #include <os.h> #include <linux/delay.h> #include <linux/string.h> #include <asm/unaligned.h> #include <tkey.h> /* * struct tkey_sandbox_priv - private information about sandbox * * @path: Path to the tkey device, e.g. "/dev/ttyACM0" * @fd: File descriptor */ struct tkey_sandbox_priv { char *path; int fd; }; static int tkey_sandbox_read(struct udevice *dev, void *buffer, int len, int timeout_ms) { struct tkey_sandbox_priv *priv = dev_get_priv(dev); u8 *buf = buffer; int total, ret; if (priv->fd < 0) return -EIO; log_debug("Reading %d bytes...\n", len); /* Read data in chunks until we get the full amount */ for (total = 0; total < len; total += ret) { ret = os_read(priv->fd, buf + total, len - total); log_debug("Read attempt returned: %d (total: %d/%d)\n", ret, total, len); if (ret < 0) { log_debug("Read failed with error %d\n", ret); return -EIO; } if (!ret) { if (!total) { log_debug("Read timeout - no data received\n"); return -EIO; } /* Partial read - break and return what we got */ log_debug("Partial read, got %x/%x bytes\n", total, len); break; } } log_debug("Read %d bytes:", total); for (int i = 0; i < total; i++) log_debug(" %02x", buf[i]); log_debug("\n"); return total; } static int tkey_sandbox_write(struct udevice *dev, const void *buffer, int len) { struct tkey_sandbox_priv *priv = dev_get_priv(dev); int ret; if (priv->fd < 0) return -EIO; log_debug("Writing %d bytes:", len); for (int i = 0; i < len; i++) log_debug(" %02x", ((u8*)buffer)[i]); log_debug("\n"); ret = os_write(priv->fd, buffer, len); if (ret < 0) { log_debug("Write failed with error %d\n", ret); return -EIO; } log_debug("Wrote %d bytes\n", ret); return ret; } static int tkey_sandbox_probe(struct udevice *dev) { struct tkey_sandbox_priv *priv = dev_get_priv(dev); const char *device_path; /* Get device path from device tree or use default */ device_path = dev_read_string(dev, "sandbox,device-path"); if (!device_path) device_path = "/dev/ttyACM0"; priv->path = strdup(device_path); if (!priv->path) return -ENOMEM; /* Open the serial device */ priv->fd = os_open(priv->path, OS_O_RDWR); if (priv->fd < 0) { log_debug("Failed to open %s (error %d)\n", priv->path, priv->fd); free(priv->path); return -ENOENT; } /* Configure serial port for raw mode */ if (os_tty_set_params(priv->fd) < 0) { log_debug("Failed to configure serial port %s\n", priv->path); os_close(priv->fd); free(priv->path); return -EIO; } log_debug("Connected to %s with serial parameters configured\n", priv->path); return 0; } static int tkey_sandbox_remove(struct udevice *dev) { struct tkey_sandbox_priv *priv = dev_get_priv(dev); if (priv->fd >= 0) { os_close(priv->fd); priv->fd = -1; } if (priv->path) { free(priv->path); priv->path = NULL; } log_debug("Disconnected\n"); return 0; } /* TKey uclass operations */ static const struct tkey_ops tkey_sandbox_ops = { .read = tkey_sandbox_read, .write = tkey_sandbox_write, }; static const struct udevice_id tkey_sandbox_ids[] = { { .compatible = "sandbox,tkey" }, { } }; U_BOOT_DRIVER(tkey_sandbox) = { .name = "tkey_sandbox", .id = UCLASS_TKEY, .of_match = tkey_sandbox_ids, .probe = tkey_sandbox_probe, .remove = tkey_sandbox_remove, .ops = &tkey_sandbox_ops, .priv_auto = sizeof(struct tkey_sandbox_priv), }; |