Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

greybus: firmware: Improve test application

It can take arguments not and we can perform all the operations with a
single binary, sorry for missing that initially.

Usage: ./firmware <gb-fw-mgmt-X> <type: interface/backend> <firmware-tag> <timeout>

And all of them have default values, etc.

Tested with a semco 13 MP module.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>

authored by

Viresh Kumar and committed by
Greg Kroah-Hartman
adac4b95 db295183

+96 -50
+96 -50
drivers/staging/greybus/Documentation/firmware/firmware.c
··· 60 60 61 61 #include "../../greybus_firmware.h" 62 62 63 - static const char *firmware_tag = "03"; /* S3 firmware */ 63 + #define FW_DEV_DEFAULT "/dev/gb-fw-mgmt-0" 64 + #define FW_TAG_INT_DEFAULT "s3f" 65 + #define FW_TAG_BCND_DEFAULT "bf_01" 66 + #define FW_UPDATE_TYPE_DEFAULT 0 67 + #define FW_TIMEOUT_DEFAULT 10000; 68 + 69 + static const char *firmware_tag; 70 + static const char *fwdev = FW_DEV_DEFAULT; 71 + static int fw_update_type = FW_UPDATE_TYPE_DEFAULT; 72 + static int fw_timeout = FW_TIMEOUT_DEFAULT; 64 73 65 74 static struct fw_mgmt_ioc_get_fw fw_info; 66 75 static struct fw_mgmt_ioc_intf_load_and_validate intf_load; 67 76 static struct fw_mgmt_ioc_backend_fw_update backend_update; 68 77 69 - int main(int argc, char *argv[]) 78 + static void usage(void) 70 79 { 71 - unsigned int timeout = 10000; 72 - char *fwdev; 73 - int fd, ret; 80 + printf("\nUsage: ./firmware <gb-fw-mgmt-X (default: gb-fw-mgmt-0)> <interface: 0, backend: 1 (default: 0)> <firmware-tag> (default: \"s3f\"/\"bf_01\") <timeout (default: 10000 ms)>\n"); 81 + } 74 82 75 - /* Make sure arguments are correct */ 76 - if (argc != 2) { 77 - printf("\nUsage: ./firmware <Path of the gb-fw-mgmt-X dev>\n"); 78 - return 0; 79 - } 80 - 81 - fwdev = argv[1]; 82 - 83 - printf("Opening %s firmware management device\n", fwdev); 84 - 85 - fd = open(fwdev, O_RDWR); 86 - if (fd < 0) { 87 - printf("Failed to open: %s\n", fwdev); 88 - ret = -1; 89 - goto close_fd; 90 - } 91 - 92 - /* Set Timeout */ 93 - printf("Setting timeout to %u ms\n", timeout); 94 - 95 - ret = ioctl(fd, FW_MGMT_IOC_SET_TIMEOUT_MS, &timeout); 96 - if (ret < 0) { 97 - printf("Failed to set timeout: %s (%d)\n", fwdev, ret); 98 - ret = -1; 99 - goto close_fd; 100 - } 83 + static int update_intf_firmware(int fd) 84 + { 85 + int ret; 101 86 102 87 /* Get Interface Firmware Version */ 103 88 printf("Get Interface Firmware Version\n"); ··· 91 106 if (ret < 0) { 92 107 printf("Failed to get interface firmware version: %s (%d)\n", 93 108 fwdev, ret); 94 - ret = -1; 95 - goto close_fd; 109 + return -1; 96 110 } 97 111 98 112 printf("Interface Firmware tag (%s), major (%d), minor (%d)\n", ··· 104 120 intf_load.status = 0; 105 121 intf_load.major = 0; 106 122 intf_load.minor = 0; 123 + 107 124 strncpy((char *)&intf_load.firmware_tag, firmware_tag, 108 125 GB_FIRMWARE_U_TAG_MAX_LEN); 109 126 ··· 112 127 if (ret < 0) { 113 128 printf("Failed to load interface firmware: %s (%d)\n", fwdev, 114 129 ret); 115 - ret = -1; 116 - goto close_fd; 130 + return -1; 117 131 } 118 132 119 133 if (intf_load.status != GB_FW_U_LOAD_STATUS_VALIDATED && 120 134 intf_load.status != GB_FW_U_LOAD_STATUS_UNVALIDATED) { 121 135 printf("Load status says loading failed: %d\n", 122 136 intf_load.status); 123 - ret = -1; 124 - goto close_fd; 137 + return -1; 125 138 } 126 139 127 140 printf("Interface Firmware (%s) Load done: major: %d, minor: %d, status: %d\n", 128 141 firmware_tag, intf_load.major, intf_load.minor, 129 142 intf_load.status); 130 143 144 + /* Initiate Mode-switch to the newly loaded firmware */ 145 + printf("Initiate Mode switch\n"); 146 + 147 + ret = ioctl(fd, FW_MGMT_IOC_MODE_SWITCH); 148 + if (ret < 0) 149 + printf("Failed to initiate mode-switch (%d)\n", ret); 150 + 151 + return ret; 152 + } 153 + 154 + static int update_backend_firmware(int fd) 155 + { 156 + int ret; 157 + 131 158 /* Get Backend Firmware Version */ 132 159 printf("Getting Backend Firmware Version\n"); 133 160 134 - strncpy((char *)&fw_info.firmware_tag, firmware_tag, 135 - GB_FIRMWARE_U_TAG_MAX_LEN); 136 161 fw_info.major = 0; 137 162 fw_info.minor = 0; 163 + strncpy((char *)&fw_info.firmware_tag, firmware_tag, 164 + GB_FIRMWARE_U_TAG_MAX_LEN); 138 165 139 166 ret = ioctl(fd, FW_MGMT_IOC_GET_BACKEND_FW, &fw_info); 140 167 if (ret < 0) { 141 168 printf("Failed to get backend firmware version: %s (%d)\n", 142 169 fwdev, ret); 143 - goto mode_switch; 170 + return -1; 144 171 } 145 172 146 173 printf("Backend Firmware tag (%s), major (%d), minor (%d)\n", ··· 168 171 ret = ioctl(fd, FW_MGMT_IOC_INTF_BACKEND_FW_UPDATE, &backend_update); 169 172 if (ret < 0) { 170 173 printf("Failed to load backend firmware: %s (%d)\n", fwdev, ret); 171 - goto mode_switch; 174 + return -1; 172 175 } 173 - 174 - printf("Backend Firmware (%s) Load done: status: %d\n", 175 - firmware_tag, backend_update.status); 176 176 177 177 if (backend_update.status != GB_FW_U_BACKEND_FW_STATUS_SUCCESS) { 178 178 printf("Load status says loading failed: %d\n", 179 179 backend_update.status); 180 + } else { 181 + printf("Backend Firmware (%s) Load done: status: %d\n", 182 + firmware_tag, backend_update.status); 180 183 } 181 184 182 - mode_switch: 183 - /* Initiate Mode-switch to the newly loaded firmware */ 184 - printf("Initiate Mode switch\n"); 185 + return 0; 186 + } 185 187 186 - ret = ioctl(fd, FW_MGMT_IOC_MODE_SWITCH); 187 - if (ret < 0) 188 - printf("Failed to initiate mode-switch (%d)\n", ret); 188 + int main(int argc, char *argv[]) 189 + { 190 + int fd, ret; 191 + 192 + if (argc > 1 && 193 + (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) { 194 + usage(); 195 + return -1; 196 + } 197 + 198 + if (argc > 1) 199 + fwdev = argv[1]; 200 + 201 + if (argc > 2) 202 + sscanf(argv[2], "%u", &fw_update_type); 203 + 204 + if (argc > 3) { 205 + firmware_tag = argv[3]; 206 + } else if (!fw_update_type) { 207 + firmware_tag = FW_TAG_INT_DEFAULT; 208 + } else { 209 + firmware_tag = FW_TAG_BCND_DEFAULT; 210 + } 211 + 212 + if (argc > 4) 213 + sscanf(argv[4], "%u", &fw_timeout); 214 + 215 + printf("Trying Firmware update: fwdev: %s, type: %s, tag: %s, timeout: %d\n", 216 + fwdev, fw_update_type == 0 ? "interface" : "backend", 217 + firmware_tag, fw_timeout); 218 + 219 + printf("Opening %s firmware management device\n", fwdev); 220 + 221 + fd = open(fwdev, O_RDWR); 222 + if (fd < 0) { 223 + printf("Failed to open: %s\n", fwdev); 224 + return -1; 225 + } 226 + 227 + /* Set Timeout */ 228 + printf("Setting timeout to %u ms\n", fw_timeout); 229 + 230 + ret = ioctl(fd, FW_MGMT_IOC_SET_TIMEOUT_MS, &fw_timeout); 231 + if (ret < 0) { 232 + printf("Failed to set timeout: %s (%d)\n", fwdev, ret); 233 + ret = -1; 234 + goto close_fd; 235 + } 236 + 237 + if (!fw_update_type) 238 + ret = update_intf_firmware(fd); 239 + else 240 + ret = update_backend_firmware(fd); 189 241 190 242 close_fd: 191 243 close(fd);