Linux-libre 4.9.174-gnu
[librecmc/linux-libre.git] / drivers / staging / greybus / Documentation / firmware / firmware.c
1 /*
2  * Sample code to test firmware-management protocol
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * Copyright(c) 2016 Google Inc. All rights reserved.
10  * Copyright(c) 2016 Linaro Ltd. All rights reserved.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License version 2 for more details.
20  *
21  * BSD LICENSE
22  *
23  * Copyright(c) 2016 Google Inc. All rights reserved.
24  * Copyright(c) 2016 Linaro Ltd. All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  *
30  *  * Redistributions of source code must retain the above copyright
31  *    notice, this list of conditions and the following disclaimer.
32  *  * Redistributions in binary form must reproduce the above copyright
33  *    notice, this list of conditions and the following disclaimer in
34  *    the documentation and/or other materials provided with the
35  *    distribution.
36  *  * Neither the name of Google Inc. or Linaro Ltd. nor the names of
37  *    its contributors may be used to endorse or promote products
38  *    derived from this software without specific prior written
39  *    permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
44  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR
45  * LINARO LTD. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
46  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
47  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
48  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
49  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
51  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52  */
53
54 #include <stdio.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <sys/ioctl.h>
58 #include <sys/stat.h>
59 #include <fcntl.h>
60
61 #include "../../greybus_firmware.h"
62
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;
73
74 static struct fw_mgmt_ioc_get_intf_version intf_fw_info;
75 static struct fw_mgmt_ioc_get_backend_version backend_fw_info;
76 static struct fw_mgmt_ioc_intf_load_and_validate intf_load;
77 static struct fw_mgmt_ioc_backend_fw_update backend_update;
78
79 static void usage(void)
80 {
81         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");
82 }
83
84 static int update_intf_firmware(int fd)
85 {
86         int ret;
87
88         /* Get Interface Firmware Version */
89         printf("Get Interface Firmware Version\n");
90
91         ret = ioctl(fd, FW_MGMT_IOC_GET_INTF_FW, &intf_fw_info);
92         if (ret < 0) {
93                 printf("Failed to get interface firmware version: %s (%d)\n",
94                         fwdev, ret);
95                 return -1;
96         }
97
98         printf("Interface Firmware tag (%s), major (%d), minor (%d)\n",
99                 intf_fw_info.firmware_tag, intf_fw_info.major,
100                 intf_fw_info.minor);
101
102         /* Try Interface Firmware load over Unipro */
103         printf("Loading Interface Firmware\n");
104
105         intf_load.load_method = GB_FW_U_LOAD_METHOD_UNIPRO;
106         intf_load.status = 0;
107         intf_load.major = 0;
108         intf_load.minor = 0;
109
110         strncpy((char *)&intf_load.firmware_tag, firmware_tag,
111                 GB_FIRMWARE_U_TAG_MAX_SIZE);
112
113         ret = ioctl(fd, FW_MGMT_IOC_INTF_LOAD_AND_VALIDATE, &intf_load);
114         if (ret < 0) {
115                 printf("Failed to load interface firmware: %s (%d)\n", fwdev,
116                         ret);
117                 return -1;
118         }
119
120         if (intf_load.status != GB_FW_U_LOAD_STATUS_VALIDATED &&
121             intf_load.status != GB_FW_U_LOAD_STATUS_UNVALIDATED) {
122                 printf("Load status says loading failed: %d\n",
123                         intf_load.status);
124                 return -1;
125         }
126
127         printf("Interface Firmware (%s) Load done: major: %d, minor: %d, status: %d\n",
128                 firmware_tag, intf_load.major, intf_load.minor,
129                 intf_load.status);
130
131         /* Initiate Mode-switch to the newly loaded firmware */
132         printf("Initiate Mode switch\n");
133
134         ret = ioctl(fd, FW_MGMT_IOC_MODE_SWITCH);
135         if (ret < 0)
136                 printf("Failed to initiate mode-switch (%d)\n", ret);
137
138         return ret;
139 }
140
141 static int update_backend_firmware(int fd)
142 {
143         int ret;
144
145         /* Get Backend Firmware Version */
146         printf("Getting Backend Firmware Version\n");
147
148         strncpy((char *)&backend_fw_info.firmware_tag, firmware_tag,
149                 GB_FIRMWARE_U_TAG_MAX_SIZE);
150
151 retry_fw_version:
152         ret = ioctl(fd, FW_MGMT_IOC_GET_BACKEND_FW, &backend_fw_info);
153         if (ret < 0) {
154                 printf("Failed to get backend firmware version: %s (%d)\n",
155                         fwdev, ret);
156                 return -1;
157         }
158
159         printf("Backend Firmware tag (%s), major (%d), minor (%d), status (%d)\n",
160                 backend_fw_info.firmware_tag, backend_fw_info.major,
161                 backend_fw_info.minor, backend_fw_info.status);
162
163         if (backend_fw_info.status == GB_FW_U_BACKEND_VERSION_STATUS_RETRY)
164                 goto retry_fw_version;
165
166         if ((backend_fw_info.status != GB_FW_U_BACKEND_VERSION_STATUS_SUCCESS)
167             && (backend_fw_info.status != GB_FW_U_BACKEND_VERSION_STATUS_NOT_AVAILABLE)) {
168                 printf("Failed to get backend firmware version: %s (%d)\n",
169                         fwdev, backend_fw_info.status);
170                 return -1;
171         }
172
173         /* Try Backend Firmware Update over Unipro */
174         printf("Updating Backend Firmware\n");
175
176         strncpy((char *)&backend_update.firmware_tag, firmware_tag,
177                 GB_FIRMWARE_U_TAG_MAX_SIZE);
178
179 retry_fw_update:
180         backend_update.status = 0;
181
182         ret = ioctl(fd, FW_MGMT_IOC_INTF_BACKEND_FW_UPDATE, &backend_update);
183         if (ret < 0) {
184                 printf("Failed to load backend firmware: %s (%d)\n", fwdev, ret);
185                 return -1;
186         }
187
188         if (backend_update.status == GB_FW_U_BACKEND_FW_STATUS_RETRY) {
189                 printf("Retrying firmware update: %d\n", backend_update.status);
190                 goto retry_fw_update;
191         }
192
193         if (backend_update.status != GB_FW_U_BACKEND_FW_STATUS_SUCCESS) {
194                 printf("Load status says loading failed: %d\n",
195                         backend_update.status);
196         } else {
197                 printf("Backend Firmware (%s) Load done: status: %d\n",
198                                 firmware_tag, backend_update.status);
199         }
200
201         return 0;
202 }
203
204 int main(int argc, char *argv[])
205 {
206         int fd, ret;
207
208         if (argc > 1 &&
209             (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
210                 usage();
211                 return -1;
212         }
213
214         if (argc > 1)
215                 fwdev = argv[1];
216
217         if (argc > 2)
218                 sscanf(argv[2], "%u", &fw_update_type);
219
220         if (argc > 3) {
221                 firmware_tag = argv[3];
222         } else if (!fw_update_type) {
223                 firmware_tag = FW_TAG_INT_DEFAULT;
224         } else {
225                 firmware_tag = FW_TAG_BCND_DEFAULT;
226         }
227
228         if (argc > 4)
229                 sscanf(argv[4], "%u", &fw_timeout);
230
231         printf("Trying Firmware update: fwdev: %s, type: %s, tag: %s, timeout: %d\n",
232                 fwdev, fw_update_type == 0 ? "interface" : "backend",
233                 firmware_tag, fw_timeout);
234
235         printf("Opening %s firmware management device\n", fwdev);
236
237         fd = open(fwdev, O_RDWR);
238         if (fd < 0) {
239                 printf("Failed to open: %s\n", fwdev);
240                 return -1;
241         }
242
243         /* Set Timeout */
244         printf("Setting timeout to %u ms\n", fw_timeout);
245
246         ret = ioctl(fd, FW_MGMT_IOC_SET_TIMEOUT_MS, &fw_timeout);
247         if (ret < 0) {
248                 printf("Failed to set timeout: %s (%d)\n", fwdev, ret);
249                 ret = -1;
250                 goto close_fd;
251         }
252
253         if (!fw_update_type)
254                 ret = update_intf_firmware(fd);
255         else
256                 ret = update_backend_firmware(fd);
257
258 close_fd:
259         close(fd);
260
261         return ret;
262 }