blob: 36934b1bcf7ad0aedb44276f39c4356924bdaa3f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Agner5661f082017-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 Agner5661f082017-08-16 11:00:51 -070017 */
18
19#include <errno.h>
Stefan Agner5661f082017-08-16 11:00:51 -070020#include <console.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060021#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060022#include <log.h>
Stefan Agner5661f082017-08-16 11:00:51 -070023#include <malloc.h>
Simon Glass1e94b462023-09-14 18:21:46 -060024#include <linux/printk.h>
Stefan Agner5661f082017-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 Agnerccd7a4d2017-08-16 11:00:52 -070033#include <spl.h>
34#include <image.h>
Stefan Agner5661f082017-08-16 11:00:51 -070035#include <imximage.h>
Sean Andersonab121792023-10-14 16:47:44 -040036#include <imx_container.h>
Vincent Prince8171dac2017-10-23 11:16:35 +020037#include <watchdog.h>
Stefan Agner5661f082017-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 Sun9e06c5c2020-08-18 18:16:48 +080075#define SDP_HID_PACKET_SIZE_EP1 1024
76
Peng Fanb0e9f3e2020-08-18 18:16:46 +080077#define SDP_EXIT 1
78
Stefan Agner5661f082017-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 Sun9e06c5c2020-08-18 18:16:48 +080090 SDP_STATE_RX_CMD,
Stefan Agner5661f082017-08-16 11:00:51 -070091 SDP_STATE_RX_DCD_DATA,
92 SDP_STATE_RX_FILE_DATA,
Sherry Sun9e06c5c2020-08-18 18:16:48 +080093 SDP_STATE_RX_FILE_DATA_BUSY,
Stefan Agner5661f082017-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 Štetiarbb00a012018-11-23 14:37:52 +0100112 u32 dnl_bytes;
Stefan Agner5661f082017-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 Sun9e06c5c2020-08-18 18:16:48 +0800124 /* EP1 OUT */
125 struct usb_ep *out_ep;
126 struct usb_request *out_req;
Stefan Agner5661f082017-08-16 11:00:51 -0700127
128 bool configuration_done;
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800129 bool ep_int_enable;
Stefan Agner5661f082017-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 Sun9e06c5c2020-08-18 18:16:48 +0800143 .bNumEndpoints = 2,
Stefan Agner5661f082017-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 Sun9e06c5c2020-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 Lid10d4292020-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 Sun405217a2020-08-18 18:16:49 +0800190 .bInterval = 3,
Ye Lid10d4292020-08-18 18:16:44 +0800191};
192
Sherry Sun9e06c5c2020-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 Sun405217a2020-08-18 18:16:49 +0800200 .bInterval = 3,
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800201};
202
Stefan Agner5661f082017-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 Sun9e06c5c2020-08-18 18:16:48 +0800207 (struct usb_descriptor_header *)&out_desc,
Stefan Agner5661f082017-08-16 11:00:51 -0700208 NULL,
209};
210
Ye Lid10d4292020-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 Sun9e06c5c2020-08-18 18:16:48 +0800215 (struct usb_descriptor_header *)&out_hs_desc,
Ye Lid10d4292020-08-18 18:16:44 +0800216 NULL,
217};
218
Stefan Agner5661f082017-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 Heidera64a6142018-02-15 10:17:29 +0100285static inline void *sdp_ptr(u32 val)
286{
287 return (void *)(uintptr_t)val;
288}
289
Stefan Agner5661f082017-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 Heider24ccd0c2018-02-15 07:08:55 +0100298 pr_err("Status: %d\n", status);
Stefan Agner5661f082017-08-16 11:00:51 -0700299 return;
300 }
301
302 if (report != 1) {
Andre Heider24ccd0c2018-02-15 07:08:55 +0100303 pr_err("Unexpected report %d\n", report);
Stefan Agner5661f082017-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 Li8f95c012020-04-29 10:35:11 +0800330 sdp->dnl_address = cmd->addr ? be32_to_cpu(cmd->addr) : CONFIG_SDP_LOADADDR;
Stefan Agner5661f082017-08-16 11:00:51 -0700331 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
Petr Štetiarbb00a012018-11-23 14:37:52 +0100332 sdp->dnl_bytes = sdp->dnl_bytes_remaining;
Stefan Agner5661f082017-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 Li8f95c012020-04-29 10:35:11 +0800358 sdp->jmp_address = cmd->addr ? be32_to_cpu(cmd->addr) : CONFIG_SDP_LOADADDR;
Stefan Agner5661f082017-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 Yamada9b643e32017-09-16 14:10:41 +0900371 pr_err("Unknown command: %04x\n", be16_to_cpu(cmd->cmd));
Stefan Agner5661f082017-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 Sun9e06c5c2020-08-18 18:16:48 +0800381 int datalen = req->actual - 1;
Stefan Agner5661f082017-08-16 11:00:51 -0700382
383 if (status != 0) {
Andre Heider24ccd0c2018-02-15 07:08:55 +0100384 pr_err("Status: %d\n", status);
Stefan Agner5661f082017-08-16 11:00:51 -0700385 return;
386 }
387
388 if (report != 2) {
Andre Heider24ccd0c2018-02-15 07:08:55 +0100389 pr_err("Unexpected report %d\n", report);
Stefan Agner5661f082017-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 Sun9e06c5c2020-08-18 18:16:48 +0800404 if (sdp->state == SDP_STATE_RX_FILE_DATA_BUSY) {
Andre Heidera64a6142018-02-15 10:17:29 +0100405 memcpy(sdp_ptr(sdp->dnl_address), req->buf + 1, datalen);
Stefan Agner5661f082017-08-16 11:00:51 -0700406 sdp->dnl_address += datalen;
407 }
408
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800409 if (sdp->dnl_bytes_remaining) {
410 sdp->state = SDP_STATE_RX_FILE_DATA;
Stefan Agner5661f082017-08-16 11:00:51 -0700411 return;
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800412 }
Stefan Agner5661f082017-08-16 11:00:51 -0700413
Simon Glass371dc062024-09-29 19:49:48 -0600414#ifndef CONFIG_XPL_BUILD
Petr Štetiarbb00a012018-11-23 14:37:52 +0100415 env_set_hex("filesize", sdp->dnl_bytes);
416#endif
Stefan Agner5661f082017-08-16 11:00:51 -0700417 printf("done\n");
418
419 switch (sdp->state) {
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800420 case SDP_STATE_RX_FILE_DATA_BUSY:
Stefan Agner5661f082017-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 Heider24ccd0c2018-02-15 07:08:55 +0100427 pr_err("Invalid state: %d\n", sdp->state);
Stefan Agner5661f082017-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 Heider24ccd0c2018-02-15 07:08:55 +0100437 pr_err("Status: %d\n", status);
Stefan Agner5661f082017-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 Heider24ccd0c2018-02-15 07:08:55 +0100460 pr_err("Wrong State: %d\n", sdp->state);
Stefan Agner5661f082017-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 Sun9e06c5c2020-08-18 18:16:48 +0800501 sdp_func->ep_int_enable = false;
Stefan Agner5661f082017-08-16 11:00:51 -0700502 break;
503 case 2:
504 value = len;
505 req->complete = sdp_rx_data_complete;
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800506 sdp_func->state = SDP_STATE_RX_FILE_DATA_BUSY;
Stefan Agner5661f082017-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 Sun9e06c5c2020-08-18 18:16:48 +0800537 struct usb_ep *ep_in, *ep_out;
Stefan Agner5661f082017-08-16 11:00:51 -0700538
539 /* allocate instance-specific endpoints */
Sherry Sun9e06c5c2020-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 Agner5661f082017-08-16 11:00:51 -0700548 rv = -ENODEV;
549 goto error;
550 }
551
Ye Lid10d4292020-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 Sun9e06c5c2020-08-18 18:16:48 +0800555 out_hs_desc.bEndpointAddress = out_desc.bEndpointAddress;
Ye Lid10d4292020-08-18 18:16:44 +0800556 }
557
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800558 sdp->in_ep = ep_in; /* Store IN EP for enabling @ setup */
559 sdp->out_ep = ep_out;
Stefan Agner5661f082017-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
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800591static struct usb_request *sdp_start_ep(struct usb_ep *ep, bool in)
Stefan Agner5661f082017-08-16 11:00:51 -0700592{
593 struct usb_request *req;
594
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800595 if (in)
596 req = alloc_ep_req(ep, 65);
597 else
598 req = alloc_ep_req(ep, 2048);
599/*
600 * OUT endpoint request length should be an integral multiple of
601 * maxpacket size 1024, else we break on certain controllers like
602 * DWC3 that expect bulk OUT requests to be divisible by maxpacket size.
603 */
Stefan Agner5661f082017-08-16 11:00:51 -0700604 debug("%s: ep:%p req:%p\n", __func__, ep, req);
605
606 if (!req)
607 return NULL;
608
609 memset(req->buf, 0, req->length);
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800610 if (in)
611 req->complete = sdp_tx_complete;
612 else
613 req->complete = sdp_rx_command_complete;
Stefan Agner5661f082017-08-16 11:00:51 -0700614
615 return req;
616}
617static int sdp_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
618{
619 struct f_sdp *sdp = func_to_sdp(f);
620 struct usb_composite_dev *cdev = f->config->cdev;
Ye Lid10d4292020-08-18 18:16:44 +0800621 struct usb_gadget *gadget = cdev->gadget;
Stefan Agner5661f082017-08-16 11:00:51 -0700622 int result;
623
624 debug("%s: intf: %d alt: %d\n", __func__, intf, alt);
625
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800626 if (gadget_is_dualspeed(gadget) && gadget->speed == USB_SPEED_HIGH) {
Ye Lid10d4292020-08-18 18:16:44 +0800627 result = usb_ep_enable(sdp->in_ep, &in_hs_desc);
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800628 result |= usb_ep_enable(sdp->out_ep, &out_hs_desc);
629 } else {
Ye Lid10d4292020-08-18 18:16:44 +0800630 result = usb_ep_enable(sdp->in_ep, &in_desc);
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800631 result |= usb_ep_enable(sdp->out_ep, &out_desc);
632 }
Stefan Agner5661f082017-08-16 11:00:51 -0700633 if (result)
634 return result;
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800635
636 sdp->in_req = sdp_start_ep(sdp->in_ep, true);
Stefan Agner5661f082017-08-16 11:00:51 -0700637 sdp->in_req->context = sdp;
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800638 sdp->out_req = sdp_start_ep(sdp->out_ep, false);
639 sdp->out_req->context = sdp;
Stefan Agner5661f082017-08-16 11:00:51 -0700640
641 sdp->in_ep->driver_data = cdev; /* claim */
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800642 sdp->out_ep->driver_data = cdev; /* claim */
Stefan Agner5661f082017-08-16 11:00:51 -0700643
644 sdp->altsetting = alt;
645 sdp->state = SDP_STATE_IDLE;
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800646 sdp->ep_int_enable = true;
Stefan Agner5661f082017-08-16 11:00:51 -0700647
648 return 0;
649}
650
651static int sdp_get_alt(struct usb_function *f, unsigned intf)
652{
653 struct f_sdp *sdp = func_to_sdp(f);
654
655 return sdp->altsetting;
656}
657
658static void sdp_disable(struct usb_function *f)
659{
660 struct f_sdp *sdp = func_to_sdp(f);
661
662 usb_ep_disable(sdp->in_ep);
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800663 usb_ep_disable(sdp->out_ep);
Stefan Agner5661f082017-08-16 11:00:51 -0700664
665 if (sdp->in_req) {
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800666 free(sdp->in_req->buf);
667 usb_ep_free_request(sdp->in_ep, sdp->in_req);
Stefan Agner5661f082017-08-16 11:00:51 -0700668 sdp->in_req = NULL;
669 }
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800670 if (sdp->out_req) {
671 free(sdp->out_req->buf);
672 usb_ep_free_request(sdp->out_ep, sdp->out_req);
673 sdp->out_req = NULL;
674 }
Stefan Agner5661f082017-08-16 11:00:51 -0700675}
676
677static int sdp_bind_config(struct usb_configuration *c)
678{
679 int status;
680
681 if (!sdp_func) {
682 sdp_func = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*sdp_func));
683 if (!sdp_func)
684 return -ENOMEM;
685 }
686
687 memset(sdp_func, 0, sizeof(*sdp_func));
688
689 sdp_func->usb_function.name = "sdp";
Ye Lid10d4292020-08-18 18:16:44 +0800690 sdp_func->usb_function.hs_descriptors = sdp_runtime_hs_descs;
Stefan Agner5661f082017-08-16 11:00:51 -0700691 sdp_func->usb_function.descriptors = sdp_runtime_descs;
692 sdp_func->usb_function.bind = sdp_bind;
693 sdp_func->usb_function.unbind = sdp_unbind;
694 sdp_func->usb_function.set_alt = sdp_set_alt;
695 sdp_func->usb_function.get_alt = sdp_get_alt;
696 sdp_func->usb_function.disable = sdp_disable;
697 sdp_func->usb_function.strings = sdp_generic_strings;
698 sdp_func->usb_function.setup = sdp_setup;
699
700 status = usb_add_function(c, &sdp_func->usb_function);
701
702 return status;
703}
704
Marek Vasut6b84acc2023-09-01 11:49:58 +0200705int sdp_init(struct udevice *udc)
Stefan Agner5661f082017-08-16 11:00:51 -0700706{
707 printf("SDP: initialize...\n");
708 while (!sdp_func->configuration_done) {
709 if (ctrlc()) {
710 puts("\rCTRL+C - Operation aborted.\n");
711 return 1;
712 }
Vincent Prince8171dac2017-10-23 11:16:35 +0200713
Stefan Roese29caf932022-09-02 14:10:46 +0200714 schedule();
Marek Vasut6b84acc2023-09-01 11:49:58 +0200715 dm_usb_gadget_handle_interrupts(udc);
Stefan Agner5661f082017-08-16 11:00:51 -0700716 }
717
718 return 0;
719}
720
721static u32 sdp_jump_imxheader(void *address)
722{
723 flash_header_v2_t *headerv2 = address;
724 ulong (*entry)(void);
725
726 if (headerv2->header.tag != IVT_HEADER_TAG) {
727 printf("Header Tag is not an IMX image\n");
728 return SDP_ERROR_IMXHEADER;
729 }
730
731 printf("Jumping to 0x%08x\n", headerv2->entry);
Andre Heidera64a6142018-02-15 10:17:29 +0100732 entry = sdp_ptr(headerv2->entry);
Stefan Agner5661f082017-08-16 11:00:51 -0700733 entry();
734
735 /* The image probably never returns hence we won't reach that point */
736 return 0;
737}
738
Simon Glass371dc062024-09-29 19:49:48 -0600739#ifdef CONFIG_XPL_BUILD
Peng Fanb0e9f3e2020-08-18 18:16:46 +0800740static ulong sdp_load_read(struct spl_load_info *load, ulong sector,
741 ulong count, void *buf)
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200742{
743 debug("%s: sector %lx, count %lx, buf %lx\n",
744 __func__, sector, count, (ulong)buf);
Sean Anderson0c6c83e2023-11-08 11:48:38 -0500745 memcpy(buf, (void *)(load->priv + sector), count);
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200746 return count;
747}
Peng Fanb0e9f3e2020-08-18 18:16:46 +0800748
749static ulong search_fit_header(ulong p, int size)
750{
751 int i;
752
753 for (i = 0; i < size; i += 4) {
754 if (genimg_get_format((const void *)(p + i)) == IMAGE_FORMAT_FIT)
755 return p + i;
756 }
757
758 return 0;
759}
760
761static ulong search_container_header(ulong p, int size)
762{
763 int i;
764 u8 *hdr;
765
766 for (i = 0; i < size; i += 4) {
767 hdr = (u8 *)(p + i);
768 if (*(hdr + 3) == 0x87 && *hdr == 0)
769 if (*(hdr + 1) != 0 || *(hdr + 2) != 0)
770 return p + i;
771 }
772 return 0;
773}
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200774#endif
775
Pali Rohár2e0429b2022-01-14 14:31:38 +0100776static int sdp_handle_in_ep(struct spl_image_info *spl_image,
777 struct spl_boot_device *bootdev)
Stefan Agner5661f082017-08-16 11:00:51 -0700778{
779 u8 *data = sdp_func->in_req->buf;
780 u32 status;
781 int datalen;
782
783 switch (sdp_func->state) {
784 case SDP_STATE_TX_SEC_CONF:
785 debug("Report 3: HAB security\n");
786 data[0] = 3;
787
788 status = SDP_SECURITY_OPEN;
789 memcpy(&data[1], &status, 4);
790 sdp_func->in_req->length = 5;
791 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
792 sdp_func->state = SDP_STATE_TX_SEC_CONF_BUSY;
793 break;
794
795 case SDP_STATE_TX_STATUS:
796 debug("Report 4: Status\n");
797 data[0] = 4;
798
799 memcpy(&data[1], &sdp_func->error_status, 4);
800 sdp_func->in_req->length = 65;
801 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
802 sdp_func->state = SDP_STATE_TX_STATUS_BUSY;
803 break;
804 case SDP_STATE_TX_REGISTER:
805 debug("Report 4: Register Values\n");
806 data[0] = 4;
807
808 datalen = sdp_func->dnl_bytes_remaining;
809
810 if (datalen > 64)
811 datalen = 64;
812
Andre Heidera64a6142018-02-15 10:17:29 +0100813 memcpy(&data[1], sdp_ptr(sdp_func->dnl_address), datalen);
Stefan Agner5661f082017-08-16 11:00:51 -0700814 sdp_func->in_req->length = 65;
815
816 sdp_func->dnl_bytes_remaining -= datalen;
817 sdp_func->dnl_address += datalen;
818
819 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
820 sdp_func->state = SDP_STATE_TX_REGISTER_BUSY;
821 break;
822 case SDP_STATE_JUMP:
Stefan Agnerccd7a4d2017-08-16 11:00:52 -0700823 printf("Jumping to header at 0x%08x\n", sdp_func->jmp_address);
Andre Heidera64a6142018-02-15 10:17:29 +0100824 status = sdp_jump_imxheader(sdp_ptr(sdp_func->jmp_address));
Stefan Agnerccd7a4d2017-08-16 11:00:52 -0700825
826 /* If imx header fails, try some U-Boot specific headers */
827 if (status) {
Simon Glass371dc062024-09-29 19:49:48 -0600828#ifdef CONFIG_XPL_BUILD
Peng Fanb0e9f3e2020-08-18 18:16:46 +0800829 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
830 sdp_func->jmp_address = (u32)search_container_header((ulong)sdp_func->jmp_address, sdp_func->dnl_bytes);
831 else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
832 sdp_func->jmp_address = (u32)search_fit_header((ulong)sdp_func->jmp_address, sdp_func->dnl_bytes);
833 if (sdp_func->jmp_address == 0)
834 panic("Error in search header, failed to jump\n");
835
836 printf("Found header at 0x%08x\n", sdp_func->jmp_address);
837
Simon Glassf3543e62022-09-06 20:26:52 -0600838 struct legacy_img_hdr *header =
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200839 sdp_ptr(sdp_func->jmp_address);
840#ifdef CONFIG_SPL_LOAD_FIT
841 if (image_get_magic(header) == FDT_MAGIC) {
842 struct spl_load_info load;
843
844 debug("Found FIT\n");
Simon Glass3fd11272024-08-22 07:55:02 -0600845 spl_load_init(&load, sdp_load_read, header, 1);
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200846 spl_load_simple_fit(spl_image, &load, 0,
847 header);
848
Peng Fanb0e9f3e2020-08-18 18:16:46 +0800849 return SDP_EXIT;
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200850 }
851#endif
Sean Andersonab121792023-10-14 16:47:44 -0400852 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
853 valid_container_hdr((void *)header)) {
Peng Fanb0e9f3e2020-08-18 18:16:46 +0800854 struct spl_load_info load;
855
Simon Glass3fd11272024-08-22 07:55:02 -0600856 spl_load_init(&load, sdp_load_read, header, 1);
Peng Fanb0e9f3e2020-08-18 18:16:46 +0800857 spl_load_imx_container(spl_image, &load, 0);
858 return SDP_EXIT;
859 }
860
Stefan Agnerccd7a4d2017-08-16 11:00:52 -0700861 /* In SPL, allow jumps to U-Boot images */
862 struct spl_image_info spl_image = {};
Pali Rohár2e0429b2022-01-14 14:31:38 +0100863 struct spl_boot_device bootdev = {};
864 spl_parse_image_header(&spl_image, &bootdev, header);
Marek Vasut7f234702023-03-07 08:42:33 +0100865 spl_board_prepare_for_boot();
Stefan Agnerccd7a4d2017-08-16 11:00:52 -0700866 jump_to_image_no_args(&spl_image);
867#else
868 /* In U-Boot, allow jumps to scripts */
Simon Glass30f33332023-01-06 08:52:28 -0600869 cmd_source_script(sdp_func->jmp_address, NULL, NULL);
Stefan Agnerccd7a4d2017-08-16 11:00:52 -0700870#endif
871 }
Stefan Agner5661f082017-08-16 11:00:51 -0700872
873 sdp_func->next_state = SDP_STATE_IDLE;
874 sdp_func->error_status = status;
875
876 /* Only send Report 4 if there was an error */
877 if (status)
878 sdp_func->state = SDP_STATE_TX_STATUS;
879 else
880 sdp_func->state = SDP_STATE_IDLE;
881 break;
882 default:
883 break;
884 };
Peng Fanb0e9f3e2020-08-18 18:16:46 +0800885
886 return 0;
Stefan Agner5661f082017-08-16 11:00:51 -0700887}
888
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800889static void sdp_handle_out_ep(void)
890{
891 int rc;
892
893 if (sdp_func->state == SDP_STATE_IDLE) {
894 sdp_func->out_req->complete = sdp_rx_command_complete;
895 rc = usb_ep_queue(sdp_func->out_ep, sdp_func->out_req, 0);
896 if (rc)
897 printf("error in submission: %s\n",
898 sdp_func->out_ep->name);
899 sdp_func->state = SDP_STATE_RX_CMD;
900 } else if (sdp_func->state == SDP_STATE_RX_FILE_DATA) {
901 sdp_func->out_req->complete = sdp_rx_data_complete;
902 rc = usb_ep_queue(sdp_func->out_ep, sdp_func->out_req, 0);
903 if (rc)
904 printf("error in submission: %s\n",
905 sdp_func->out_ep->name);
906 sdp_func->state = SDP_STATE_RX_FILE_DATA_BUSY;
907 }
908}
909
Simon Glass371dc062024-09-29 19:49:48 -0600910#ifndef CONFIG_XPL_BUILD
Marek Vasut6b84acc2023-09-01 11:49:58 +0200911int sdp_handle(struct udevice *udc)
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200912#else
Marek Vasut6b84acc2023-09-01 11:49:58 +0200913int spl_sdp_handle(struct udevice *udc, struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100914 struct spl_boot_device *bootdev)
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200915#endif
Stefan Agner5661f082017-08-16 11:00:51 -0700916{
Peng Fanb0e9f3e2020-08-18 18:16:46 +0800917 int flag = 0;
Stefan Agner5661f082017-08-16 11:00:51 -0700918 printf("SDP: handle requests...\n");
919 while (1) {
920 if (ctrlc()) {
921 puts("\rCTRL+C - Operation aborted.\n");
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200922 return -EINVAL;
Stefan Agner5661f082017-08-16 11:00:51 -0700923 }
924
Peng Fanb0e9f3e2020-08-18 18:16:46 +0800925 if (flag == SDP_EXIT)
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200926 return 0;
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200927
Stefan Roese29caf932022-09-02 14:10:46 +0200928 schedule();
Marek Vasut6b84acc2023-09-01 11:49:58 +0200929 dm_usb_gadget_handle_interrupts(udc);
Stefan Agner5661f082017-08-16 11:00:51 -0700930
Simon Glass371dc062024-09-29 19:49:48 -0600931#ifdef CONFIG_XPL_BUILD
Pali Rohár2e0429b2022-01-14 14:31:38 +0100932 flag = sdp_handle_in_ep(spl_image, bootdev);
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200933#else
Pali Rohár2e0429b2022-01-14 14:31:38 +0100934 flag = sdp_handle_in_ep(NULL, NULL);
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200935#endif
Sherry Sun9e06c5c2020-08-18 18:16:48 +0800936 if (sdp_func->ep_int_enable)
937 sdp_handle_out_ep();
Stefan Agner5661f082017-08-16 11:00:51 -0700938 }
939}
940
941int sdp_add(struct usb_configuration *c)
942{
943 int id;
944
945 id = usb_string_id(c->cdev);
946 if (id < 0)
947 return id;
948 strings_sdp_generic[0].id = id;
949 sdp_intf_runtime.iInterface = id;
950
951 debug("%s: cdev: %p gadget: %p gadget->ep0: %p\n", __func__,
952 c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
953
954 return sdp_bind_config(c);
955}
956
957DECLARE_GADGET_BIND_CALLBACK(usb_dnl_sdp, sdp_add);