blob: 89496917a61f86fd3e50464fbbedaefbdc237d5e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Agnerffbd5d02017-08-16 11:00:51 -07002/*
3 * f_sdp.c -- USB HID Serial Download Protocol
4 *
5 * Copyright (C) 2017 Toradex
6 * Author: Stefan Agner <stefan.agner@toradex.com>
7 *
8 * This file implements the Serial Download Protocol (SDP) as specified in
9 * the i.MX 6 Reference Manual. The SDP is a USB HID based protocol and
10 * allows to download images directly to memory. The implementation
11 * works with the imx_loader (imx_usb) USB client software on host side.
12 *
13 * Not all commands are implemented, e.g. WRITE_REGISTER, DCD_WRITE and
14 * SKIP_DCD_HEADER are only stubs.
15 *
16 * Parts of the implementation are based on f_dfu and f_thor.
Stefan Agnerffbd5d02017-08-16 11:00:51 -070017 */
18
19#include <errno.h>
Stefan Agnerffbd5d02017-08-16 11:00:51 -070020#include <console.h>
Simon Glass313112a2019-08-01 09:46:46 -060021#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060022#include <log.h>
Stefan Agnerffbd5d02017-08-16 11:00:51 -070023#include <malloc.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060024#include <linux/printk.h>
Stefan Agnerffbd5d02017-08-16 11:00:51 -070025
26#include <linux/usb/ch9.h>
27#include <linux/usb/gadget.h>
28#include <linux/usb/composite.h>
29
30#include <asm/io.h>
31#include <g_dnl.h>
32#include <sdp.h>
Stefan Agner3888da72017-08-16 11:00:52 -070033#include <spl.h>
34#include <image.h>
Stefan Agnerffbd5d02017-08-16 11:00:51 -070035#include <imximage.h>
Sean Anderson952ed672023-10-14 16:47:44 -040036#include <imx_container.h>
Vincent Princef26cf932017-10-23 11:16:35 +020037#include <watchdog.h>
Stefan Agnerffbd5d02017-08-16 11:00:51 -070038
39#define HID_REPORT_ID_MASK 0x000000ff
40
41/*
42 * HID class requests
43 */
44#define HID_REQ_GET_REPORT 0x01
45#define HID_REQ_GET_IDLE 0x02
46#define HID_REQ_GET_PROTOCOL 0x03
47#define HID_REQ_SET_REPORT 0x09
48#define HID_REQ_SET_IDLE 0x0A
49#define HID_REQ_SET_PROTOCOL 0x0B
50
51#define HID_USAGE_PAGE_LEN 76
52
53struct hid_report {
54 u8 usage_page[HID_USAGE_PAGE_LEN];
55} __packed;
56
57#define SDP_READ_REGISTER 0x0101
58#define SDP_WRITE_REGISTER 0x0202
59#define SDP_WRITE_FILE 0x0404
60#define SDP_ERROR_STATUS 0x0505
61#define SDP_DCD_WRITE 0x0a0a
62#define SDP_JUMP_ADDRESS 0x0b0b
63#define SDP_SKIP_DCD_HEADER 0x0c0c
64
65#define SDP_SECURITY_CLOSED 0x12343412
66#define SDP_SECURITY_OPEN 0x56787856
67
68#define SDP_WRITE_FILE_COMPLETE 0x88888888
69#define SDP_WRITE_REGISTER_COMPLETE 0x128A8A12
70#define SDP_SKIP_DCD_HEADER_COMPLETE 0x900DD009
71#define SDP_ERROR_IMXHEADER 0x000a0533
72
73#define SDP_COMMAND_LEN 16
74
Sherry Sun52262982020-08-18 18:16:48 +080075#define SDP_HID_PACKET_SIZE_EP1 1024
76
Peng Fanf49d4702020-08-18 18:16:46 +080077#define SDP_EXIT 1
78
Stefan Agnerffbd5d02017-08-16 11:00:51 -070079struct sdp_command {
80 u16 cmd;
81 u32 addr;
82 u8 format;
83 u32 cnt;
84 u32 data;
85 u8 rsvd;
86} __packed;
87
88enum sdp_state {
89 SDP_STATE_IDLE,
Sherry Sun52262982020-08-18 18:16:48 +080090 SDP_STATE_RX_CMD,
Stefan Agnerffbd5d02017-08-16 11:00:51 -070091 SDP_STATE_RX_DCD_DATA,
92 SDP_STATE_RX_FILE_DATA,
Sherry Sun52262982020-08-18 18:16:48 +080093 SDP_STATE_RX_FILE_DATA_BUSY,
Stefan Agnerffbd5d02017-08-16 11:00:51 -070094 SDP_STATE_TX_SEC_CONF,
95 SDP_STATE_TX_SEC_CONF_BUSY,
96 SDP_STATE_TX_REGISTER,
97 SDP_STATE_TX_REGISTER_BUSY,
98 SDP_STATE_TX_STATUS,
99 SDP_STATE_TX_STATUS_BUSY,
100 SDP_STATE_JUMP,
101};
102
103struct f_sdp {
104 struct usb_function usb_function;
105
106 struct usb_descriptor_header **function;
107
108 u8 altsetting;
109 enum sdp_state state;
110 enum sdp_state next_state;
111 u32 dnl_address;
Petr Štetiard991cc32018-11-23 14:37:52 +0100112 u32 dnl_bytes;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700113 u32 dnl_bytes_remaining;
114 u32 jmp_address;
115 bool always_send_status;
116 u32 error_status;
117
118 /* EP0 request */
119 struct usb_request *req;
120
121 /* EP1 IN */
122 struct usb_ep *in_ep;
123 struct usb_request *in_req;
Sherry Sun52262982020-08-18 18:16:48 +0800124 /* EP1 OUT */
125 struct usb_ep *out_ep;
126 struct usb_request *out_req;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700127
128 bool configuration_done;
Sherry Sun52262982020-08-18 18:16:48 +0800129 bool ep_int_enable;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700130};
131
132static struct f_sdp *sdp_func;
133
134static inline struct f_sdp *func_to_sdp(struct usb_function *f)
135{
136 return container_of(f, struct f_sdp, usb_function);
137}
138
139static struct usb_interface_descriptor sdp_intf_runtime = {
140 .bLength = sizeof(sdp_intf_runtime),
141 .bDescriptorType = USB_DT_INTERFACE,
142 .bAlternateSetting = 0,
Sherry Sun52262982020-08-18 18:16:48 +0800143 .bNumEndpoints = 2,
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700144 .bInterfaceClass = USB_CLASS_HID,
145 .bInterfaceSubClass = 0,
146 .bInterfaceProtocol = 0,
147 /* .iInterface = DYNAMIC */
148};
149
150/* HID configuration */
151static struct usb_class_hid_descriptor sdp_hid_desc = {
152 .bLength = sizeof(sdp_hid_desc),
153 .bDescriptorType = USB_DT_CS_DEVICE,
154
155 .bcdCDC = __constant_cpu_to_le16(0x0110),
156 .bCountryCode = 0,
157 .bNumDescriptors = 1,
158
159 .bDescriptorType0 = USB_DT_HID_REPORT,
160 .wDescriptorLength0 = HID_USAGE_PAGE_LEN,
161};
162
163static struct usb_endpoint_descriptor in_desc = {
164 .bLength = USB_DT_ENDPOINT_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
166
167 .bEndpointAddress = 1 | USB_DIR_IN,
168 .bmAttributes = USB_ENDPOINT_XFER_INT,
169 .wMaxPacketSize = 64,
170 .bInterval = 1,
171};
172
Sherry Sun52262982020-08-18 18:16:48 +0800173static struct usb_endpoint_descriptor out_desc = {
174 .bLength = USB_DT_ENDPOINT_SIZE,
175 .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
176
177 .bEndpointAddress = 1 | USB_DIR_OUT,
178 .bmAttributes = USB_ENDPOINT_XFER_INT,
179 .wMaxPacketSize = 64,
180 .bInterval = 1,
181};
182
Ye Lia9e8fe02020-08-18 18:16:44 +0800183static struct usb_endpoint_descriptor in_hs_desc = {
184 .bLength = USB_DT_ENDPOINT_SIZE,
185 .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
186
187 .bEndpointAddress = 1 | USB_DIR_IN,
188 .bmAttributes = USB_ENDPOINT_XFER_INT,
189 .wMaxPacketSize = 512,
Sherry Sun44f26b12020-08-18 18:16:49 +0800190 .bInterval = 3,
Ye Lia9e8fe02020-08-18 18:16:44 +0800191};
192
Sherry Sun52262982020-08-18 18:16:48 +0800193static struct usb_endpoint_descriptor out_hs_desc = {
194 .bLength = USB_DT_ENDPOINT_SIZE,
195 .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
196
197 .bEndpointAddress = 1 | USB_DIR_OUT,
198 .bmAttributes = USB_ENDPOINT_XFER_INT,
199 .wMaxPacketSize = SDP_HID_PACKET_SIZE_EP1,
Sherry Sun44f26b12020-08-18 18:16:49 +0800200 .bInterval = 3,
Sherry Sun52262982020-08-18 18:16:48 +0800201};
202
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700203static struct usb_descriptor_header *sdp_runtime_descs[] = {
204 (struct usb_descriptor_header *)&sdp_intf_runtime,
205 (struct usb_descriptor_header *)&sdp_hid_desc,
206 (struct usb_descriptor_header *)&in_desc,
Sherry Sun52262982020-08-18 18:16:48 +0800207 (struct usb_descriptor_header *)&out_desc,
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700208 NULL,
209};
210
Ye Lia9e8fe02020-08-18 18:16:44 +0800211static struct usb_descriptor_header *sdp_runtime_hs_descs[] = {
212 (struct usb_descriptor_header *)&sdp_intf_runtime,
213 (struct usb_descriptor_header *)&sdp_hid_desc,
214 (struct usb_descriptor_header *)&in_hs_desc,
Sherry Sun52262982020-08-18 18:16:48 +0800215 (struct usb_descriptor_header *)&out_hs_desc,
Ye Lia9e8fe02020-08-18 18:16:44 +0800216 NULL,
217};
218
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700219/* This is synchronized with what the SoC implementation reports */
220static struct hid_report sdp_hid_report = {
221 .usage_page = {
222 0x06, 0x00, 0xff, /* Usage Page */
223 0x09, 0x01, /* Usage (Pointer?) */
224 0xa1, 0x01, /* Collection */
225
226 0x85, 0x01, /* Report ID */
227 0x19, 0x01, /* Usage Minimum */
228 0x29, 0x01, /* Usage Maximum */
229 0x15, 0x00, /* Local Minimum */
230 0x26, 0xFF, 0x00, /* Local Maximum? */
231 0x75, 0x08, /* Report Size */
232 0x95, 0x10, /* Report Count */
233 0x91, 0x02, /* Output Data */
234
235 0x85, 0x02, /* Report ID */
236 0x19, 0x01, /* Usage Minimum */
237 0x29, 0x01, /* Usage Maximum */
238 0x15, 0x00, /* Local Minimum */
239 0x26, 0xFF, 0x00, /* Local Maximum? */
240 0x75, 0x80, /* Report Size 128 */
241 0x95, 0x40, /* Report Count */
242 0x91, 0x02, /* Output Data */
243
244 0x85, 0x03, /* Report ID */
245 0x19, 0x01, /* Usage Minimum */
246 0x29, 0x01, /* Usage Maximum */
247 0x15, 0x00, /* Local Minimum */
248 0x26, 0xFF, 0x00, /* Local Maximum? */
249 0x75, 0x08, /* Report Size 8 */
250 0x95, 0x04, /* Report Count */
251 0x81, 0x02, /* Input Data */
252
253 0x85, 0x04, /* Report ID */
254 0x19, 0x01, /* Usage Minimum */
255 0x29, 0x01, /* Usage Maximum */
256 0x15, 0x00, /* Local Minimum */
257 0x26, 0xFF, 0x00, /* Local Maximum? */
258 0x75, 0x08, /* Report Size 8 */
259 0x95, 0x40, /* Report Count */
260 0x81, 0x02, /* Input Data */
261 0xc0
262 },
263};
264
265static const char sdp_name[] = "Serial Downloader Protocol";
266
267/*
268 * static strings, in UTF-8
269 */
270static struct usb_string strings_sdp_generic[] = {
271 [0].s = sdp_name,
272 { } /* end of list */
273};
274
275static struct usb_gadget_strings stringtab_sdp_generic = {
276 .language = 0x0409, /* en-us */
277 .strings = strings_sdp_generic,
278};
279
280static struct usb_gadget_strings *sdp_generic_strings[] = {
281 &stringtab_sdp_generic,
282 NULL,
283};
284
Andre Heidere1d461b2018-02-15 10:17:29 +0100285static inline void *sdp_ptr(u32 val)
286{
287 return (void *)(uintptr_t)val;
288}
289
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700290static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req)
291{
292 struct f_sdp *sdp = req->context;
293 int status = req->status;
294 u8 *data = req->buf;
295 u8 report = data[0];
296
297 if (status != 0) {
Andre Heiderb841e972018-02-15 07:08:55 +0100298 pr_err("Status: %d\n", status);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700299 return;
300 }
301
302 if (report != 1) {
Andre Heiderb841e972018-02-15 07:08:55 +0100303 pr_err("Unexpected report %d\n", report);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700304 return;
305 }
306
307 struct sdp_command *cmd = req->buf + 1;
308
309 debug("%s: command: %04x, addr: %08x, cnt: %u\n",
310 __func__, be16_to_cpu(cmd->cmd),
311 be32_to_cpu(cmd->addr), be32_to_cpu(cmd->cnt));
312
313 switch (be16_to_cpu(cmd->cmd)) {
314 case SDP_READ_REGISTER:
315 sdp->always_send_status = false;
316 sdp->error_status = 0x0;
317
318 sdp->state = SDP_STATE_TX_SEC_CONF;
319 sdp->dnl_address = be32_to_cpu(cmd->addr);
320 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
321 sdp->next_state = SDP_STATE_TX_REGISTER;
322 printf("Reading %d registers at 0x%08x... ",
323 sdp->dnl_bytes_remaining, sdp->dnl_address);
324 break;
325 case SDP_WRITE_FILE:
326 sdp->always_send_status = true;
327 sdp->error_status = SDP_WRITE_FILE_COMPLETE;
328
329 sdp->state = SDP_STATE_RX_FILE_DATA;
Frank Lic2bdb582020-04-29 10:35:11 +0800330 sdp->dnl_address = cmd->addr ? be32_to_cpu(cmd->addr) : CONFIG_SDP_LOADADDR;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700331 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
Petr Štetiard991cc32018-11-23 14:37:52 +0100332 sdp->dnl_bytes = sdp->dnl_bytes_remaining;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700333 sdp->next_state = SDP_STATE_IDLE;
334
335 printf("Downloading file of size %d to 0x%08x... ",
336 sdp->dnl_bytes_remaining, sdp->dnl_address);
337
338 break;
339 case SDP_ERROR_STATUS:
340 sdp->always_send_status = true;
341 sdp->error_status = 0;
342
343 sdp->state = SDP_STATE_TX_SEC_CONF;
344 sdp->next_state = SDP_STATE_IDLE;
345 break;
346 case SDP_DCD_WRITE:
347 sdp->always_send_status = true;
348 sdp->error_status = SDP_WRITE_REGISTER_COMPLETE;
349
350 sdp->state = SDP_STATE_RX_DCD_DATA;
351 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
352 sdp->next_state = SDP_STATE_IDLE;
353 break;
354 case SDP_JUMP_ADDRESS:
355 sdp->always_send_status = false;
356 sdp->error_status = 0;
357
Frank Lic2bdb582020-04-29 10:35:11 +0800358 sdp->jmp_address = cmd->addr ? be32_to_cpu(cmd->addr) : CONFIG_SDP_LOADADDR;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700359 sdp->state = SDP_STATE_TX_SEC_CONF;
360 sdp->next_state = SDP_STATE_JUMP;
361 break;
362 case SDP_SKIP_DCD_HEADER:
363 sdp->always_send_status = true;
364 sdp->error_status = SDP_SKIP_DCD_HEADER_COMPLETE;
365
366 /* Ignore command, DCD not supported anyway */
367 sdp->state = SDP_STATE_TX_SEC_CONF;
368 sdp->next_state = SDP_STATE_IDLE;
369 break;
370 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900371 pr_err("Unknown command: %04x\n", be16_to_cpu(cmd->cmd));
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700372 }
373}
374
375static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req)
376{
377 struct f_sdp *sdp = req->context;
378 int status = req->status;
379 u8 *data = req->buf;
380 u8 report = data[0];
Sherry Sun52262982020-08-18 18:16:48 +0800381 int datalen = req->actual - 1;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700382
383 if (status != 0) {
Andre Heiderb841e972018-02-15 07:08:55 +0100384 pr_err("Status: %d\n", status);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700385 return;
386 }
387
388 if (report != 2) {
Andre Heiderb841e972018-02-15 07:08:55 +0100389 pr_err("Unexpected report %d\n", report);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700390 return;
391 }
392
393 if (sdp->dnl_bytes_remaining < datalen) {
394 /*
395 * Some USB stacks require to send a complete buffer as
396 * specified in the HID descriptor. This leads to longer
397 * transfers than the file length, no problem for us.
398 */
399 sdp->dnl_bytes_remaining = 0;
400 } else {
401 sdp->dnl_bytes_remaining -= datalen;
402 }
403
Sherry Sun52262982020-08-18 18:16:48 +0800404 if (sdp->state == SDP_STATE_RX_FILE_DATA_BUSY) {
Andre Heidere1d461b2018-02-15 10:17:29 +0100405 memcpy(sdp_ptr(sdp->dnl_address), req->buf + 1, datalen);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700406 sdp->dnl_address += datalen;
407 }
408
Sherry Sun52262982020-08-18 18:16:48 +0800409 if (sdp->dnl_bytes_remaining) {
410 sdp->state = SDP_STATE_RX_FILE_DATA;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700411 return;
Sherry Sun52262982020-08-18 18:16:48 +0800412 }
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700413
Petr Štetiard991cc32018-11-23 14:37:52 +0100414#ifndef CONFIG_SPL_BUILD
415 env_set_hex("filesize", sdp->dnl_bytes);
416#endif
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700417 printf("done\n");
418
419 switch (sdp->state) {
Sherry Sun52262982020-08-18 18:16:48 +0800420 case SDP_STATE_RX_FILE_DATA_BUSY:
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700421 sdp->state = SDP_STATE_TX_SEC_CONF;
422 break;
423 case SDP_STATE_RX_DCD_DATA:
424 sdp->state = SDP_STATE_TX_SEC_CONF;
425 break;
426 default:
Andre Heiderb841e972018-02-15 07:08:55 +0100427 pr_err("Invalid state: %d\n", sdp->state);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700428 }
429}
430
431static void sdp_tx_complete(struct usb_ep *ep, struct usb_request *req)
432{
433 struct f_sdp *sdp = req->context;
434 int status = req->status;
435
436 if (status != 0) {
Andre Heiderb841e972018-02-15 07:08:55 +0100437 pr_err("Status: %d\n", status);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700438 return;
439 }
440
441 switch (sdp->state) {
442 case SDP_STATE_TX_SEC_CONF_BUSY:
443 /* Not all commands require status report */
444 if (sdp->always_send_status || sdp->error_status)
445 sdp->state = SDP_STATE_TX_STATUS;
446 else
447 sdp->state = sdp->next_state;
448
449 break;
450 case SDP_STATE_TX_STATUS_BUSY:
451 sdp->state = sdp->next_state;
452 break;
453 case SDP_STATE_TX_REGISTER_BUSY:
454 if (sdp->dnl_bytes_remaining)
455 sdp->state = SDP_STATE_TX_REGISTER;
456 else
457 sdp->state = SDP_STATE_IDLE;
458 break;
459 default:
Andre Heiderb841e972018-02-15 07:08:55 +0100460 pr_err("Wrong State: %d\n", sdp->state);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700461 sdp->state = SDP_STATE_IDLE;
462 break;
463 }
464 debug("%s complete --> %d, %d/%d\n", ep->name,
465 status, req->actual, req->length);
466}
467
468static int sdp_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
469{
470 struct usb_gadget *gadget = f->config->cdev->gadget;
471 struct usb_request *req = f->config->cdev->req;
472 struct f_sdp *sdp = f->config->cdev->req->context;
473 u16 len = le16_to_cpu(ctrl->wLength);
474 u16 w_value = le16_to_cpu(ctrl->wValue);
475 int value = 0;
476 u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
477
478 debug("w_value: 0x%04x len: 0x%04x\n", w_value, len);
479 debug("req_type: 0x%02x ctrl->bRequest: 0x%02x sdp->state: %d\n",
480 req_type, ctrl->bRequest, sdp->state);
481
482 if (req_type == USB_TYPE_STANDARD) {
483 if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR) {
484 /* Send HID report descriptor */
485 value = min(len, (u16) sizeof(sdp_hid_report));
486 memcpy(req->buf, &sdp_hid_report, value);
487 sdp->configuration_done = true;
488 }
489 }
490
491 if (req_type == USB_TYPE_CLASS) {
492 int report = w_value & HID_REPORT_ID_MASK;
493
494 /* HID (SDP) request */
495 switch (ctrl->bRequest) {
496 case HID_REQ_SET_REPORT:
497 switch (report) {
498 case 1:
499 value = SDP_COMMAND_LEN + 1;
500 req->complete = sdp_rx_command_complete;
Sherry Sun52262982020-08-18 18:16:48 +0800501 sdp_func->ep_int_enable = false;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700502 break;
503 case 2:
504 value = len;
505 req->complete = sdp_rx_data_complete;
Sherry Sun52262982020-08-18 18:16:48 +0800506 sdp_func->state = SDP_STATE_RX_FILE_DATA_BUSY;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700507 break;
508 }
509 }
510 }
511
512 if (value >= 0) {
513 req->length = value;
514 req->zero = value < len;
515 value = usb_ep_queue(gadget->ep0, req, 0);
516 if (value < 0) {
517 debug("ep_queue --> %d\n", value);
518 req->status = 0;
519 }
520 }
521
522 return value;
523}
524
525static int sdp_bind(struct usb_configuration *c, struct usb_function *f)
526{
527 struct usb_gadget *gadget = c->cdev->gadget;
528 struct usb_composite_dev *cdev = c->cdev;
529 struct f_sdp *sdp = func_to_sdp(f);
530 int rv = 0, id;
531
532 id = usb_interface_id(c, f);
533 if (id < 0)
534 return id;
535 sdp_intf_runtime.bInterfaceNumber = id;
536
Sherry Sun52262982020-08-18 18:16:48 +0800537 struct usb_ep *ep_in, *ep_out;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700538
539 /* allocate instance-specific endpoints */
Sherry Sun52262982020-08-18 18:16:48 +0800540 ep_in = usb_ep_autoconfig(gadget, &in_desc);
541 if (!ep_in) {
542 rv = -ENODEV;
543 goto error;
544 }
545
546 ep_out = usb_ep_autoconfig(gadget, &out_desc);
547 if (!ep_out) {
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700548 rv = -ENODEV;
549 goto error;
550 }
551
Ye Lia9e8fe02020-08-18 18:16:44 +0800552 if (gadget_is_dualspeed(gadget)) {
553 /* Assume endpoint addresses are the same for both speeds */
554 in_hs_desc.bEndpointAddress = in_desc.bEndpointAddress;
Sherry Sun52262982020-08-18 18:16:48 +0800555 out_hs_desc.bEndpointAddress = out_desc.bEndpointAddress;
Ye Lia9e8fe02020-08-18 18:16:44 +0800556 }
557
Sherry Sun52262982020-08-18 18:16:48 +0800558 sdp->in_ep = ep_in; /* Store IN EP for enabling @ setup */
559 sdp->out_ep = ep_out;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700560
561 cdev->req->context = sdp;
562
563error:
564 return rv;
565}
566
567static void sdp_unbind(struct usb_configuration *c, struct usb_function *f)
568{
569 free(sdp_func);
570 sdp_func = NULL;
571}
572
573static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
574{
575 struct usb_request *req;
576
577 req = usb_ep_alloc_request(ep, 0);
578 if (!req)
579 return req;
580
581 req->length = length;
582 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
583 if (!req->buf) {
584 usb_ep_free_request(ep, req);
585 req = NULL;
586 }
587
588 return req;
589}
590
591
Sherry Sun52262982020-08-18 18:16:48 +0800592static struct usb_request *sdp_start_ep(struct usb_ep *ep, bool in)
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700593{
594 struct usb_request *req;
595
Sherry Sun52262982020-08-18 18:16:48 +0800596 if (in)
597 req = alloc_ep_req(ep, 65);
598 else
599 req = alloc_ep_req(ep, 2048);
600/*
601 * OUT endpoint request length should be an integral multiple of
602 * maxpacket size 1024, else we break on certain controllers like
603 * DWC3 that expect bulk OUT requests to be divisible by maxpacket size.
604 */
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700605 debug("%s: ep:%p req:%p\n", __func__, ep, req);
606
607 if (!req)
608 return NULL;
609
610 memset(req->buf, 0, req->length);
Sherry Sun52262982020-08-18 18:16:48 +0800611 if (in)
612 req->complete = sdp_tx_complete;
613 else
614 req->complete = sdp_rx_command_complete;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700615
616 return req;
617}
618static int sdp_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
619{
620 struct f_sdp *sdp = func_to_sdp(f);
621 struct usb_composite_dev *cdev = f->config->cdev;
Ye Lia9e8fe02020-08-18 18:16:44 +0800622 struct usb_gadget *gadget = cdev->gadget;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700623 int result;
624
625 debug("%s: intf: %d alt: %d\n", __func__, intf, alt);
626
Sherry Sun52262982020-08-18 18:16:48 +0800627 if (gadget_is_dualspeed(gadget) && gadget->speed == USB_SPEED_HIGH) {
Ye Lia9e8fe02020-08-18 18:16:44 +0800628 result = usb_ep_enable(sdp->in_ep, &in_hs_desc);
Sherry Sun52262982020-08-18 18:16:48 +0800629 result |= usb_ep_enable(sdp->out_ep, &out_hs_desc);
630 } else {
Ye Lia9e8fe02020-08-18 18:16:44 +0800631 result = usb_ep_enable(sdp->in_ep, &in_desc);
Sherry Sun52262982020-08-18 18:16:48 +0800632 result |= usb_ep_enable(sdp->out_ep, &out_desc);
633 }
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700634 if (result)
635 return result;
Sherry Sun52262982020-08-18 18:16:48 +0800636
637 sdp->in_req = sdp_start_ep(sdp->in_ep, true);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700638 sdp->in_req->context = sdp;
Sherry Sun52262982020-08-18 18:16:48 +0800639 sdp->out_req = sdp_start_ep(sdp->out_ep, false);
640 sdp->out_req->context = sdp;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700641
642 sdp->in_ep->driver_data = cdev; /* claim */
Sherry Sun52262982020-08-18 18:16:48 +0800643 sdp->out_ep->driver_data = cdev; /* claim */
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700644
645 sdp->altsetting = alt;
646 sdp->state = SDP_STATE_IDLE;
Sherry Sun52262982020-08-18 18:16:48 +0800647 sdp->ep_int_enable = true;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700648
649 return 0;
650}
651
652static int sdp_get_alt(struct usb_function *f, unsigned intf)
653{
654 struct f_sdp *sdp = func_to_sdp(f);
655
656 return sdp->altsetting;
657}
658
659static void sdp_disable(struct usb_function *f)
660{
661 struct f_sdp *sdp = func_to_sdp(f);
662
663 usb_ep_disable(sdp->in_ep);
Sherry Sun52262982020-08-18 18:16:48 +0800664 usb_ep_disable(sdp->out_ep);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700665
666 if (sdp->in_req) {
Sherry Sun52262982020-08-18 18:16:48 +0800667 free(sdp->in_req->buf);
668 usb_ep_free_request(sdp->in_ep, sdp->in_req);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700669 sdp->in_req = NULL;
670 }
Sherry Sun52262982020-08-18 18:16:48 +0800671 if (sdp->out_req) {
672 free(sdp->out_req->buf);
673 usb_ep_free_request(sdp->out_ep, sdp->out_req);
674 sdp->out_req = NULL;
675 }
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700676}
677
678static int sdp_bind_config(struct usb_configuration *c)
679{
680 int status;
681
682 if (!sdp_func) {
683 sdp_func = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*sdp_func));
684 if (!sdp_func)
685 return -ENOMEM;
686 }
687
688 memset(sdp_func, 0, sizeof(*sdp_func));
689
690 sdp_func->usb_function.name = "sdp";
Ye Lia9e8fe02020-08-18 18:16:44 +0800691 sdp_func->usb_function.hs_descriptors = sdp_runtime_hs_descs;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700692 sdp_func->usb_function.descriptors = sdp_runtime_descs;
693 sdp_func->usb_function.bind = sdp_bind;
694 sdp_func->usb_function.unbind = sdp_unbind;
695 sdp_func->usb_function.set_alt = sdp_set_alt;
696 sdp_func->usb_function.get_alt = sdp_get_alt;
697 sdp_func->usb_function.disable = sdp_disable;
698 sdp_func->usb_function.strings = sdp_generic_strings;
699 sdp_func->usb_function.setup = sdp_setup;
700
701 status = usb_add_function(c, &sdp_func->usb_function);
702
703 return status;
704}
705
Marek Vasut2bd299d2023-09-01 11:49:58 +0200706int sdp_init(struct udevice *udc)
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700707{
708 printf("SDP: initialize...\n");
709 while (!sdp_func->configuration_done) {
710 if (ctrlc()) {
711 puts("\rCTRL+C - Operation aborted.\n");
712 return 1;
713 }
Vincent Princef26cf932017-10-23 11:16:35 +0200714
Stefan Roese80877fa2022-09-02 14:10:46 +0200715 schedule();
Marek Vasut2bd299d2023-09-01 11:49:58 +0200716 dm_usb_gadget_handle_interrupts(udc);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700717 }
718
719 return 0;
720}
721
722static u32 sdp_jump_imxheader(void *address)
723{
724 flash_header_v2_t *headerv2 = address;
725 ulong (*entry)(void);
726
727 if (headerv2->header.tag != IVT_HEADER_TAG) {
728 printf("Header Tag is not an IMX image\n");
729 return SDP_ERROR_IMXHEADER;
730 }
731
732 printf("Jumping to 0x%08x\n", headerv2->entry);
Andre Heidere1d461b2018-02-15 10:17:29 +0100733 entry = sdp_ptr(headerv2->entry);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700734 entry();
735
736 /* The image probably never returns hence we won't reach that point */
737 return 0;
738}
739
Frieder Schrempf09580e22019-06-04 21:56:29 +0200740#ifdef CONFIG_SPL_BUILD
Peng Fanf49d4702020-08-18 18:16:46 +0800741static ulong sdp_load_read(struct spl_load_info *load, ulong sector,
742 ulong count, void *buf)
Frieder Schrempf09580e22019-06-04 21:56:29 +0200743{
744 debug("%s: sector %lx, count %lx, buf %lx\n",
745 __func__, sector, count, (ulong)buf);
Sean Anderson2b5f9c82023-11-08 11:48:38 -0500746 memcpy(buf, (void *)(load->priv + sector), count);
Frieder Schrempf09580e22019-06-04 21:56:29 +0200747 return count;
748}
Peng Fanf49d4702020-08-18 18:16:46 +0800749
750static ulong search_fit_header(ulong p, int size)
751{
752 int i;
753
754 for (i = 0; i < size; i += 4) {
755 if (genimg_get_format((const void *)(p + i)) == IMAGE_FORMAT_FIT)
756 return p + i;
757 }
758
759 return 0;
760}
761
762static ulong search_container_header(ulong p, int size)
763{
764 int i;
765 u8 *hdr;
766
767 for (i = 0; i < size; i += 4) {
768 hdr = (u8 *)(p + i);
769 if (*(hdr + 3) == 0x87 && *hdr == 0)
770 if (*(hdr + 1) != 0 || *(hdr + 2) != 0)
771 return p + i;
772 }
773 return 0;
774}
Frieder Schrempf09580e22019-06-04 21:56:29 +0200775#endif
776
Pali Rohárdda8f882022-01-14 14:31:38 +0100777static int sdp_handle_in_ep(struct spl_image_info *spl_image,
778 struct spl_boot_device *bootdev)
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700779{
780 u8 *data = sdp_func->in_req->buf;
781 u32 status;
782 int datalen;
783
784 switch (sdp_func->state) {
785 case SDP_STATE_TX_SEC_CONF:
786 debug("Report 3: HAB security\n");
787 data[0] = 3;
788
789 status = SDP_SECURITY_OPEN;
790 memcpy(&data[1], &status, 4);
791 sdp_func->in_req->length = 5;
792 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
793 sdp_func->state = SDP_STATE_TX_SEC_CONF_BUSY;
794 break;
795
796 case SDP_STATE_TX_STATUS:
797 debug("Report 4: Status\n");
798 data[0] = 4;
799
800 memcpy(&data[1], &sdp_func->error_status, 4);
801 sdp_func->in_req->length = 65;
802 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
803 sdp_func->state = SDP_STATE_TX_STATUS_BUSY;
804 break;
805 case SDP_STATE_TX_REGISTER:
806 debug("Report 4: Register Values\n");
807 data[0] = 4;
808
809 datalen = sdp_func->dnl_bytes_remaining;
810
811 if (datalen > 64)
812 datalen = 64;
813
Andre Heidere1d461b2018-02-15 10:17:29 +0100814 memcpy(&data[1], sdp_ptr(sdp_func->dnl_address), datalen);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700815 sdp_func->in_req->length = 65;
816
817 sdp_func->dnl_bytes_remaining -= datalen;
818 sdp_func->dnl_address += datalen;
819
820 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
821 sdp_func->state = SDP_STATE_TX_REGISTER_BUSY;
822 break;
823 case SDP_STATE_JUMP:
Stefan Agner3888da72017-08-16 11:00:52 -0700824 printf("Jumping to header at 0x%08x\n", sdp_func->jmp_address);
Andre Heidere1d461b2018-02-15 10:17:29 +0100825 status = sdp_jump_imxheader(sdp_ptr(sdp_func->jmp_address));
Stefan Agner3888da72017-08-16 11:00:52 -0700826
827 /* If imx header fails, try some U-Boot specific headers */
828 if (status) {
829#ifdef CONFIG_SPL_BUILD
Peng Fanf49d4702020-08-18 18:16:46 +0800830 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
831 sdp_func->jmp_address = (u32)search_container_header((ulong)sdp_func->jmp_address, sdp_func->dnl_bytes);
832 else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
833 sdp_func->jmp_address = (u32)search_fit_header((ulong)sdp_func->jmp_address, sdp_func->dnl_bytes);
834 if (sdp_func->jmp_address == 0)
835 panic("Error in search header, failed to jump\n");
836
837 printf("Found header at 0x%08x\n", sdp_func->jmp_address);
838
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600839 struct legacy_img_hdr *header =
Frieder Schrempf09580e22019-06-04 21:56:29 +0200840 sdp_ptr(sdp_func->jmp_address);
841#ifdef CONFIG_SPL_LOAD_FIT
842 if (image_get_magic(header) == FDT_MAGIC) {
843 struct spl_load_info load;
844
845 debug("Found FIT\n");
Sean Anderson2b5f9c82023-11-08 11:48:38 -0500846 load.priv = header;
Sean Anderson35f15fe2023-11-08 11:48:43 -0500847 spl_set_bl_len(&load, 1);
Peng Fanf49d4702020-08-18 18:16:46 +0800848 load.read = sdp_load_read;
Frieder Schrempf09580e22019-06-04 21:56:29 +0200849 spl_load_simple_fit(spl_image, &load, 0,
850 header);
851
Peng Fanf49d4702020-08-18 18:16:46 +0800852 return SDP_EXIT;
Frieder Schrempf09580e22019-06-04 21:56:29 +0200853 }
854#endif
Sean Anderson952ed672023-10-14 16:47:44 -0400855 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
856 valid_container_hdr((void *)header)) {
Peng Fanf49d4702020-08-18 18:16:46 +0800857 struct spl_load_info load;
858
Sean Anderson2b5f9c82023-11-08 11:48:38 -0500859 load.priv = header;
Sean Anderson35f15fe2023-11-08 11:48:43 -0500860 spl_set_bl_len(&load, 1);
Peng Fanf49d4702020-08-18 18:16:46 +0800861 load.read = sdp_load_read;
862 spl_load_imx_container(spl_image, &load, 0);
863 return SDP_EXIT;
864 }
865
Stefan Agner3888da72017-08-16 11:00:52 -0700866 /* In SPL, allow jumps to U-Boot images */
867 struct spl_image_info spl_image = {};
Pali Rohárdda8f882022-01-14 14:31:38 +0100868 struct spl_boot_device bootdev = {};
869 spl_parse_image_header(&spl_image, &bootdev, header);
Marek Vasuta0a4ce82023-03-07 08:42:33 +0100870 spl_board_prepare_for_boot();
Stefan Agner3888da72017-08-16 11:00:52 -0700871 jump_to_image_no_args(&spl_image);
872#else
873 /* In U-Boot, allow jumps to scripts */
Simon Glassdaee3ba2023-01-06 08:52:28 -0600874 cmd_source_script(sdp_func->jmp_address, NULL, NULL);
Stefan Agner3888da72017-08-16 11:00:52 -0700875#endif
876 }
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700877
878 sdp_func->next_state = SDP_STATE_IDLE;
879 sdp_func->error_status = status;
880
881 /* Only send Report 4 if there was an error */
882 if (status)
883 sdp_func->state = SDP_STATE_TX_STATUS;
884 else
885 sdp_func->state = SDP_STATE_IDLE;
886 break;
887 default:
888 break;
889 };
Peng Fanf49d4702020-08-18 18:16:46 +0800890
891 return 0;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700892}
893
Sherry Sun52262982020-08-18 18:16:48 +0800894static void sdp_handle_out_ep(void)
895{
896 int rc;
897
898 if (sdp_func->state == SDP_STATE_IDLE) {
899 sdp_func->out_req->complete = sdp_rx_command_complete;
900 rc = usb_ep_queue(sdp_func->out_ep, sdp_func->out_req, 0);
901 if (rc)
902 printf("error in submission: %s\n",
903 sdp_func->out_ep->name);
904 sdp_func->state = SDP_STATE_RX_CMD;
905 } else if (sdp_func->state == SDP_STATE_RX_FILE_DATA) {
906 sdp_func->out_req->complete = sdp_rx_data_complete;
907 rc = usb_ep_queue(sdp_func->out_ep, sdp_func->out_req, 0);
908 if (rc)
909 printf("error in submission: %s\n",
910 sdp_func->out_ep->name);
911 sdp_func->state = SDP_STATE_RX_FILE_DATA_BUSY;
912 }
913}
914
Frieder Schrempf09580e22019-06-04 21:56:29 +0200915#ifndef CONFIG_SPL_BUILD
Marek Vasut2bd299d2023-09-01 11:49:58 +0200916int sdp_handle(struct udevice *udc)
Frieder Schrempf09580e22019-06-04 21:56:29 +0200917#else
Marek Vasut2bd299d2023-09-01 11:49:58 +0200918int spl_sdp_handle(struct udevice *udc, struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100919 struct spl_boot_device *bootdev)
Frieder Schrempf09580e22019-06-04 21:56:29 +0200920#endif
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700921{
Peng Fanf49d4702020-08-18 18:16:46 +0800922 int flag = 0;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700923 printf("SDP: handle requests...\n");
924 while (1) {
925 if (ctrlc()) {
926 puts("\rCTRL+C - Operation aborted.\n");
Frieder Schrempf09580e22019-06-04 21:56:29 +0200927 return -EINVAL;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700928 }
929
Peng Fanf49d4702020-08-18 18:16:46 +0800930 if (flag == SDP_EXIT)
Frieder Schrempf09580e22019-06-04 21:56:29 +0200931 return 0;
Frieder Schrempf09580e22019-06-04 21:56:29 +0200932
Stefan Roese80877fa2022-09-02 14:10:46 +0200933 schedule();
Marek Vasut2bd299d2023-09-01 11:49:58 +0200934 dm_usb_gadget_handle_interrupts(udc);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700935
Frieder Schrempf09580e22019-06-04 21:56:29 +0200936#ifdef CONFIG_SPL_BUILD
Pali Rohárdda8f882022-01-14 14:31:38 +0100937 flag = sdp_handle_in_ep(spl_image, bootdev);
Frieder Schrempf09580e22019-06-04 21:56:29 +0200938#else
Pali Rohárdda8f882022-01-14 14:31:38 +0100939 flag = sdp_handle_in_ep(NULL, NULL);
Frieder Schrempf09580e22019-06-04 21:56:29 +0200940#endif
Sherry Sun52262982020-08-18 18:16:48 +0800941 if (sdp_func->ep_int_enable)
942 sdp_handle_out_ep();
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700943 }
944}
945
946int sdp_add(struct usb_configuration *c)
947{
948 int id;
949
950 id = usb_string_id(c->cdev);
951 if (id < 0)
952 return id;
953 strings_sdp_generic[0].id = id;
954 sdp_intf_runtime.iInterface = id;
955
956 debug("%s: cdev: %p gadget: %p gadget->ep0: %p\n", __func__,
957 c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
958
959 return sdp_bind_config(c);
960}
961
962DECLARE_GADGET_BIND_CALLBACK(usb_dnl_sdp, sdp_add);