blob: 37f6281abfbecc9e7433232492b48ff608e26ae8 [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>
20#include <common.h>
21#include <console.h>
Simon Glass313112a2019-08-01 09:46:46 -060022#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060023#include <log.h>
Stefan Agnerffbd5d02017-08-16 11:00:51 -070024#include <malloc.h>
25
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>
Vincent Princef26cf932017-10-23 11:16:35 +020036#include <watchdog.h>
Stefan Agnerffbd5d02017-08-16 11:00:51 -070037
38#define HID_REPORT_ID_MASK 0x000000ff
39
40/*
41 * HID class requests
42 */
43#define HID_REQ_GET_REPORT 0x01
44#define HID_REQ_GET_IDLE 0x02
45#define HID_REQ_GET_PROTOCOL 0x03
46#define HID_REQ_SET_REPORT 0x09
47#define HID_REQ_SET_IDLE 0x0A
48#define HID_REQ_SET_PROTOCOL 0x0B
49
50#define HID_USAGE_PAGE_LEN 76
51
52struct hid_report {
53 u8 usage_page[HID_USAGE_PAGE_LEN];
54} __packed;
55
56#define SDP_READ_REGISTER 0x0101
57#define SDP_WRITE_REGISTER 0x0202
58#define SDP_WRITE_FILE 0x0404
59#define SDP_ERROR_STATUS 0x0505
60#define SDP_DCD_WRITE 0x0a0a
61#define SDP_JUMP_ADDRESS 0x0b0b
62#define SDP_SKIP_DCD_HEADER 0x0c0c
63
64#define SDP_SECURITY_CLOSED 0x12343412
65#define SDP_SECURITY_OPEN 0x56787856
66
67#define SDP_WRITE_FILE_COMPLETE 0x88888888
68#define SDP_WRITE_REGISTER_COMPLETE 0x128A8A12
69#define SDP_SKIP_DCD_HEADER_COMPLETE 0x900DD009
70#define SDP_ERROR_IMXHEADER 0x000a0533
71
72#define SDP_COMMAND_LEN 16
73
Sherry Sun52262982020-08-18 18:16:48 +080074#define SDP_HID_PACKET_SIZE_EP1 1024
75
Peng Fanf49d4702020-08-18 18:16:46 +080076#define SDP_EXIT 1
77
Stefan Agnerffbd5d02017-08-16 11:00:51 -070078struct sdp_command {
79 u16 cmd;
80 u32 addr;
81 u8 format;
82 u32 cnt;
83 u32 data;
84 u8 rsvd;
85} __packed;
86
87enum sdp_state {
88 SDP_STATE_IDLE,
Sherry Sun52262982020-08-18 18:16:48 +080089 SDP_STATE_RX_CMD,
Stefan Agnerffbd5d02017-08-16 11:00:51 -070090 SDP_STATE_RX_DCD_DATA,
91 SDP_STATE_RX_FILE_DATA,
Sherry Sun52262982020-08-18 18:16:48 +080092 SDP_STATE_RX_FILE_DATA_BUSY,
Stefan Agnerffbd5d02017-08-16 11:00:51 -070093 SDP_STATE_TX_SEC_CONF,
94 SDP_STATE_TX_SEC_CONF_BUSY,
95 SDP_STATE_TX_REGISTER,
96 SDP_STATE_TX_REGISTER_BUSY,
97 SDP_STATE_TX_STATUS,
98 SDP_STATE_TX_STATUS_BUSY,
99 SDP_STATE_JUMP,
100};
101
102struct f_sdp {
103 struct usb_function usb_function;
104
105 struct usb_descriptor_header **function;
106
107 u8 altsetting;
108 enum sdp_state state;
109 enum sdp_state next_state;
110 u32 dnl_address;
Petr Štetiard991cc32018-11-23 14:37:52 +0100111 u32 dnl_bytes;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700112 u32 dnl_bytes_remaining;
113 u32 jmp_address;
114 bool always_send_status;
115 u32 error_status;
116
117 /* EP0 request */
118 struct usb_request *req;
119
120 /* EP1 IN */
121 struct usb_ep *in_ep;
122 struct usb_request *in_req;
Sherry Sun52262982020-08-18 18:16:48 +0800123 /* EP1 OUT */
124 struct usb_ep *out_ep;
125 struct usb_request *out_req;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700126
127 bool configuration_done;
Sherry Sun52262982020-08-18 18:16:48 +0800128 bool ep_int_enable;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700129};
130
131static struct f_sdp *sdp_func;
132
133static inline struct f_sdp *func_to_sdp(struct usb_function *f)
134{
135 return container_of(f, struct f_sdp, usb_function);
136}
137
138static struct usb_interface_descriptor sdp_intf_runtime = {
139 .bLength = sizeof(sdp_intf_runtime),
140 .bDescriptorType = USB_DT_INTERFACE,
141 .bAlternateSetting = 0,
Sherry Sun52262982020-08-18 18:16:48 +0800142 .bNumEndpoints = 2,
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700143 .bInterfaceClass = USB_CLASS_HID,
144 .bInterfaceSubClass = 0,
145 .bInterfaceProtocol = 0,
146 /* .iInterface = DYNAMIC */
147};
148
149/* HID configuration */
150static struct usb_class_hid_descriptor sdp_hid_desc = {
151 .bLength = sizeof(sdp_hid_desc),
152 .bDescriptorType = USB_DT_CS_DEVICE,
153
154 .bcdCDC = __constant_cpu_to_le16(0x0110),
155 .bCountryCode = 0,
156 .bNumDescriptors = 1,
157
158 .bDescriptorType0 = USB_DT_HID_REPORT,
159 .wDescriptorLength0 = HID_USAGE_PAGE_LEN,
160};
161
162static struct usb_endpoint_descriptor in_desc = {
163 .bLength = USB_DT_ENDPOINT_SIZE,
164 .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
165
166 .bEndpointAddress = 1 | USB_DIR_IN,
167 .bmAttributes = USB_ENDPOINT_XFER_INT,
168 .wMaxPacketSize = 64,
169 .bInterval = 1,
170};
171
Sherry Sun52262982020-08-18 18:16:48 +0800172static struct usb_endpoint_descriptor out_desc = {
173 .bLength = USB_DT_ENDPOINT_SIZE,
174 .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
175
176 .bEndpointAddress = 1 | USB_DIR_OUT,
177 .bmAttributes = USB_ENDPOINT_XFER_INT,
178 .wMaxPacketSize = 64,
179 .bInterval = 1,
180};
181
Ye Lia9e8fe02020-08-18 18:16:44 +0800182static struct usb_endpoint_descriptor in_hs_desc = {
183 .bLength = USB_DT_ENDPOINT_SIZE,
184 .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
185
186 .bEndpointAddress = 1 | USB_DIR_IN,
187 .bmAttributes = USB_ENDPOINT_XFER_INT,
188 .wMaxPacketSize = 512,
Sherry Sun44f26b12020-08-18 18:16:49 +0800189 .bInterval = 3,
Ye Lia9e8fe02020-08-18 18:16:44 +0800190};
191
Sherry Sun52262982020-08-18 18:16:48 +0800192static struct usb_endpoint_descriptor out_hs_desc = {
193 .bLength = USB_DT_ENDPOINT_SIZE,
194 .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
195
196 .bEndpointAddress = 1 | USB_DIR_OUT,
197 .bmAttributes = USB_ENDPOINT_XFER_INT,
198 .wMaxPacketSize = SDP_HID_PACKET_SIZE_EP1,
Sherry Sun44f26b12020-08-18 18:16:49 +0800199 .bInterval = 3,
Sherry Sun52262982020-08-18 18:16:48 +0800200};
201
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700202static struct usb_descriptor_header *sdp_runtime_descs[] = {
203 (struct usb_descriptor_header *)&sdp_intf_runtime,
204 (struct usb_descriptor_header *)&sdp_hid_desc,
205 (struct usb_descriptor_header *)&in_desc,
Sherry Sun52262982020-08-18 18:16:48 +0800206 (struct usb_descriptor_header *)&out_desc,
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700207 NULL,
208};
209
Ye Lia9e8fe02020-08-18 18:16:44 +0800210static struct usb_descriptor_header *sdp_runtime_hs_descs[] = {
211 (struct usb_descriptor_header *)&sdp_intf_runtime,
212 (struct usb_descriptor_header *)&sdp_hid_desc,
213 (struct usb_descriptor_header *)&in_hs_desc,
Sherry Sun52262982020-08-18 18:16:48 +0800214 (struct usb_descriptor_header *)&out_hs_desc,
Ye Lia9e8fe02020-08-18 18:16:44 +0800215 NULL,
216};
217
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700218/* This is synchronized with what the SoC implementation reports */
219static struct hid_report sdp_hid_report = {
220 .usage_page = {
221 0x06, 0x00, 0xff, /* Usage Page */
222 0x09, 0x01, /* Usage (Pointer?) */
223 0xa1, 0x01, /* Collection */
224
225 0x85, 0x01, /* Report ID */
226 0x19, 0x01, /* Usage Minimum */
227 0x29, 0x01, /* Usage Maximum */
228 0x15, 0x00, /* Local Minimum */
229 0x26, 0xFF, 0x00, /* Local Maximum? */
230 0x75, 0x08, /* Report Size */
231 0x95, 0x10, /* Report Count */
232 0x91, 0x02, /* Output Data */
233
234 0x85, 0x02, /* Report ID */
235 0x19, 0x01, /* Usage Minimum */
236 0x29, 0x01, /* Usage Maximum */
237 0x15, 0x00, /* Local Minimum */
238 0x26, 0xFF, 0x00, /* Local Maximum? */
239 0x75, 0x80, /* Report Size 128 */
240 0x95, 0x40, /* Report Count */
241 0x91, 0x02, /* Output Data */
242
243 0x85, 0x03, /* Report ID */
244 0x19, 0x01, /* Usage Minimum */
245 0x29, 0x01, /* Usage Maximum */
246 0x15, 0x00, /* Local Minimum */
247 0x26, 0xFF, 0x00, /* Local Maximum? */
248 0x75, 0x08, /* Report Size 8 */
249 0x95, 0x04, /* Report Count */
250 0x81, 0x02, /* Input Data */
251
252 0x85, 0x04, /* Report ID */
253 0x19, 0x01, /* Usage Minimum */
254 0x29, 0x01, /* Usage Maximum */
255 0x15, 0x00, /* Local Minimum */
256 0x26, 0xFF, 0x00, /* Local Maximum? */
257 0x75, 0x08, /* Report Size 8 */
258 0x95, 0x40, /* Report Count */
259 0x81, 0x02, /* Input Data */
260 0xc0
261 },
262};
263
264static const char sdp_name[] = "Serial Downloader Protocol";
265
266/*
267 * static strings, in UTF-8
268 */
269static struct usb_string strings_sdp_generic[] = {
270 [0].s = sdp_name,
271 { } /* end of list */
272};
273
274static struct usb_gadget_strings stringtab_sdp_generic = {
275 .language = 0x0409, /* en-us */
276 .strings = strings_sdp_generic,
277};
278
279static struct usb_gadget_strings *sdp_generic_strings[] = {
280 &stringtab_sdp_generic,
281 NULL,
282};
283
Andre Heidere1d461b2018-02-15 10:17:29 +0100284static inline void *sdp_ptr(u32 val)
285{
286 return (void *)(uintptr_t)val;
287}
288
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700289static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req)
290{
291 struct f_sdp *sdp = req->context;
292 int status = req->status;
293 u8 *data = req->buf;
294 u8 report = data[0];
295
296 if (status != 0) {
Andre Heiderb841e972018-02-15 07:08:55 +0100297 pr_err("Status: %d\n", status);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700298 return;
299 }
300
301 if (report != 1) {
Andre Heiderb841e972018-02-15 07:08:55 +0100302 pr_err("Unexpected report %d\n", report);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700303 return;
304 }
305
306 struct sdp_command *cmd = req->buf + 1;
307
308 debug("%s: command: %04x, addr: %08x, cnt: %u\n",
309 __func__, be16_to_cpu(cmd->cmd),
310 be32_to_cpu(cmd->addr), be32_to_cpu(cmd->cnt));
311
312 switch (be16_to_cpu(cmd->cmd)) {
313 case SDP_READ_REGISTER:
314 sdp->always_send_status = false;
315 sdp->error_status = 0x0;
316
317 sdp->state = SDP_STATE_TX_SEC_CONF;
318 sdp->dnl_address = be32_to_cpu(cmd->addr);
319 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
320 sdp->next_state = SDP_STATE_TX_REGISTER;
321 printf("Reading %d registers at 0x%08x... ",
322 sdp->dnl_bytes_remaining, sdp->dnl_address);
323 break;
324 case SDP_WRITE_FILE:
325 sdp->always_send_status = true;
326 sdp->error_status = SDP_WRITE_FILE_COMPLETE;
327
328 sdp->state = SDP_STATE_RX_FILE_DATA;
Frank Lic2bdb582020-04-29 10:35:11 +0800329 sdp->dnl_address = cmd->addr ? be32_to_cpu(cmd->addr) : CONFIG_SDP_LOADADDR;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700330 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
Petr Štetiard991cc32018-11-23 14:37:52 +0100331 sdp->dnl_bytes = sdp->dnl_bytes_remaining;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700332 sdp->next_state = SDP_STATE_IDLE;
333
334 printf("Downloading file of size %d to 0x%08x... ",
335 sdp->dnl_bytes_remaining, sdp->dnl_address);
336
337 break;
338 case SDP_ERROR_STATUS:
339 sdp->always_send_status = true;
340 sdp->error_status = 0;
341
342 sdp->state = SDP_STATE_TX_SEC_CONF;
343 sdp->next_state = SDP_STATE_IDLE;
344 break;
345 case SDP_DCD_WRITE:
346 sdp->always_send_status = true;
347 sdp->error_status = SDP_WRITE_REGISTER_COMPLETE;
348
349 sdp->state = SDP_STATE_RX_DCD_DATA;
350 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
351 sdp->next_state = SDP_STATE_IDLE;
352 break;
353 case SDP_JUMP_ADDRESS:
354 sdp->always_send_status = false;
355 sdp->error_status = 0;
356
Frank Lic2bdb582020-04-29 10:35:11 +0800357 sdp->jmp_address = cmd->addr ? be32_to_cpu(cmd->addr) : CONFIG_SDP_LOADADDR;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700358 sdp->state = SDP_STATE_TX_SEC_CONF;
359 sdp->next_state = SDP_STATE_JUMP;
360 break;
361 case SDP_SKIP_DCD_HEADER:
362 sdp->always_send_status = true;
363 sdp->error_status = SDP_SKIP_DCD_HEADER_COMPLETE;
364
365 /* Ignore command, DCD not supported anyway */
366 sdp->state = SDP_STATE_TX_SEC_CONF;
367 sdp->next_state = SDP_STATE_IDLE;
368 break;
369 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900370 pr_err("Unknown command: %04x\n", be16_to_cpu(cmd->cmd));
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700371 }
372}
373
374static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req)
375{
376 struct f_sdp *sdp = req->context;
377 int status = req->status;
378 u8 *data = req->buf;
379 u8 report = data[0];
Sherry Sun52262982020-08-18 18:16:48 +0800380 int datalen = req->actual - 1;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700381
382 if (status != 0) {
Andre Heiderb841e972018-02-15 07:08:55 +0100383 pr_err("Status: %d\n", status);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700384 return;
385 }
386
387 if (report != 2) {
Andre Heiderb841e972018-02-15 07:08:55 +0100388 pr_err("Unexpected report %d\n", report);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700389 return;
390 }
391
392 if (sdp->dnl_bytes_remaining < datalen) {
393 /*
394 * Some USB stacks require to send a complete buffer as
395 * specified in the HID descriptor. This leads to longer
396 * transfers than the file length, no problem for us.
397 */
398 sdp->dnl_bytes_remaining = 0;
399 } else {
400 sdp->dnl_bytes_remaining -= datalen;
401 }
402
Sherry Sun52262982020-08-18 18:16:48 +0800403 if (sdp->state == SDP_STATE_RX_FILE_DATA_BUSY) {
Andre Heidere1d461b2018-02-15 10:17:29 +0100404 memcpy(sdp_ptr(sdp->dnl_address), req->buf + 1, datalen);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700405 sdp->dnl_address += datalen;
406 }
407
Sherry Sun52262982020-08-18 18:16:48 +0800408 if (sdp->dnl_bytes_remaining) {
409 sdp->state = SDP_STATE_RX_FILE_DATA;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700410 return;
Sherry Sun52262982020-08-18 18:16:48 +0800411 }
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700412
Petr Štetiard991cc32018-11-23 14:37:52 +0100413#ifndef CONFIG_SPL_BUILD
414 env_set_hex("filesize", sdp->dnl_bytes);
415#endif
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700416 printf("done\n");
417
418 switch (sdp->state) {
Sherry Sun52262982020-08-18 18:16:48 +0800419 case SDP_STATE_RX_FILE_DATA_BUSY:
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700420 sdp->state = SDP_STATE_TX_SEC_CONF;
421 break;
422 case SDP_STATE_RX_DCD_DATA:
423 sdp->state = SDP_STATE_TX_SEC_CONF;
424 break;
425 default:
Andre Heiderb841e972018-02-15 07:08:55 +0100426 pr_err("Invalid state: %d\n", sdp->state);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700427 }
428}
429
430static void sdp_tx_complete(struct usb_ep *ep, struct usb_request *req)
431{
432 struct f_sdp *sdp = req->context;
433 int status = req->status;
434
435 if (status != 0) {
Andre Heiderb841e972018-02-15 07:08:55 +0100436 pr_err("Status: %d\n", status);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700437 return;
438 }
439
440 switch (sdp->state) {
441 case SDP_STATE_TX_SEC_CONF_BUSY:
442 /* Not all commands require status report */
443 if (sdp->always_send_status || sdp->error_status)
444 sdp->state = SDP_STATE_TX_STATUS;
445 else
446 sdp->state = sdp->next_state;
447
448 break;
449 case SDP_STATE_TX_STATUS_BUSY:
450 sdp->state = sdp->next_state;
451 break;
452 case SDP_STATE_TX_REGISTER_BUSY:
453 if (sdp->dnl_bytes_remaining)
454 sdp->state = SDP_STATE_TX_REGISTER;
455 else
456 sdp->state = SDP_STATE_IDLE;
457 break;
458 default:
Andre Heiderb841e972018-02-15 07:08:55 +0100459 pr_err("Wrong State: %d\n", sdp->state);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700460 sdp->state = SDP_STATE_IDLE;
461 break;
462 }
463 debug("%s complete --> %d, %d/%d\n", ep->name,
464 status, req->actual, req->length);
465}
466
467static int sdp_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
468{
469 struct usb_gadget *gadget = f->config->cdev->gadget;
470 struct usb_request *req = f->config->cdev->req;
471 struct f_sdp *sdp = f->config->cdev->req->context;
472 u16 len = le16_to_cpu(ctrl->wLength);
473 u16 w_value = le16_to_cpu(ctrl->wValue);
474 int value = 0;
475 u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
476
477 debug("w_value: 0x%04x len: 0x%04x\n", w_value, len);
478 debug("req_type: 0x%02x ctrl->bRequest: 0x%02x sdp->state: %d\n",
479 req_type, ctrl->bRequest, sdp->state);
480
481 if (req_type == USB_TYPE_STANDARD) {
482 if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR) {
483 /* Send HID report descriptor */
484 value = min(len, (u16) sizeof(sdp_hid_report));
485 memcpy(req->buf, &sdp_hid_report, value);
486 sdp->configuration_done = true;
487 }
488 }
489
490 if (req_type == USB_TYPE_CLASS) {
491 int report = w_value & HID_REPORT_ID_MASK;
492
493 /* HID (SDP) request */
494 switch (ctrl->bRequest) {
495 case HID_REQ_SET_REPORT:
496 switch (report) {
497 case 1:
498 value = SDP_COMMAND_LEN + 1;
499 req->complete = sdp_rx_command_complete;
Sherry Sun52262982020-08-18 18:16:48 +0800500 sdp_func->ep_int_enable = false;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700501 break;
502 case 2:
503 value = len;
504 req->complete = sdp_rx_data_complete;
Sherry Sun52262982020-08-18 18:16:48 +0800505 sdp_func->state = SDP_STATE_RX_FILE_DATA_BUSY;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700506 break;
507 }
508 }
509 }
510
511 if (value >= 0) {
512 req->length = value;
513 req->zero = value < len;
514 value = usb_ep_queue(gadget->ep0, req, 0);
515 if (value < 0) {
516 debug("ep_queue --> %d\n", value);
517 req->status = 0;
518 }
519 }
520
521 return value;
522}
523
524static int sdp_bind(struct usb_configuration *c, struct usb_function *f)
525{
526 struct usb_gadget *gadget = c->cdev->gadget;
527 struct usb_composite_dev *cdev = c->cdev;
528 struct f_sdp *sdp = func_to_sdp(f);
529 int rv = 0, id;
530
531 id = usb_interface_id(c, f);
532 if (id < 0)
533 return id;
534 sdp_intf_runtime.bInterfaceNumber = id;
535
Sherry Sun52262982020-08-18 18:16:48 +0800536 struct usb_ep *ep_in, *ep_out;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700537
538 /* allocate instance-specific endpoints */
Sherry Sun52262982020-08-18 18:16:48 +0800539 ep_in = usb_ep_autoconfig(gadget, &in_desc);
540 if (!ep_in) {
541 rv = -ENODEV;
542 goto error;
543 }
544
545 ep_out = usb_ep_autoconfig(gadget, &out_desc);
546 if (!ep_out) {
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700547 rv = -ENODEV;
548 goto error;
549 }
550
Ye Lia9e8fe02020-08-18 18:16:44 +0800551 if (gadget_is_dualspeed(gadget)) {
552 /* Assume endpoint addresses are the same for both speeds */
553 in_hs_desc.bEndpointAddress = in_desc.bEndpointAddress;
Sherry Sun52262982020-08-18 18:16:48 +0800554 out_hs_desc.bEndpointAddress = out_desc.bEndpointAddress;
Ye Lia9e8fe02020-08-18 18:16:44 +0800555 }
556
Sherry Sun52262982020-08-18 18:16:48 +0800557 sdp->in_ep = ep_in; /* Store IN EP for enabling @ setup */
558 sdp->out_ep = ep_out;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700559
560 cdev->req->context = sdp;
561
562error:
563 return rv;
564}
565
566static void sdp_unbind(struct usb_configuration *c, struct usb_function *f)
567{
568 free(sdp_func);
569 sdp_func = NULL;
570}
571
572static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
573{
574 struct usb_request *req;
575
576 req = usb_ep_alloc_request(ep, 0);
577 if (!req)
578 return req;
579
580 req->length = length;
581 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
582 if (!req->buf) {
583 usb_ep_free_request(ep, req);
584 req = NULL;
585 }
586
587 return req;
588}
589
590
Sherry Sun52262982020-08-18 18:16:48 +0800591static struct usb_request *sdp_start_ep(struct usb_ep *ep, bool in)
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700592{
593 struct usb_request *req;
594
Sherry Sun52262982020-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 Agnerffbd5d02017-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 Sun52262982020-08-18 18:16:48 +0800610 if (in)
611 req->complete = sdp_tx_complete;
612 else
613 req->complete = sdp_rx_command_complete;
Stefan Agnerffbd5d02017-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 Lia9e8fe02020-08-18 18:16:44 +0800621 struct usb_gadget *gadget = cdev->gadget;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700622 int result;
623
624 debug("%s: intf: %d alt: %d\n", __func__, intf, alt);
625
Sherry Sun52262982020-08-18 18:16:48 +0800626 if (gadget_is_dualspeed(gadget) && gadget->speed == USB_SPEED_HIGH) {
Ye Lia9e8fe02020-08-18 18:16:44 +0800627 result = usb_ep_enable(sdp->in_ep, &in_hs_desc);
Sherry Sun52262982020-08-18 18:16:48 +0800628 result |= usb_ep_enable(sdp->out_ep, &out_hs_desc);
629 } else {
Ye Lia9e8fe02020-08-18 18:16:44 +0800630 result = usb_ep_enable(sdp->in_ep, &in_desc);
Sherry Sun52262982020-08-18 18:16:48 +0800631 result |= usb_ep_enable(sdp->out_ep, &out_desc);
632 }
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700633 if (result)
634 return result;
Sherry Sun52262982020-08-18 18:16:48 +0800635
636 sdp->in_req = sdp_start_ep(sdp->in_ep, true);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700637 sdp->in_req->context = sdp;
Sherry Sun52262982020-08-18 18:16:48 +0800638 sdp->out_req = sdp_start_ep(sdp->out_ep, false);
639 sdp->out_req->context = sdp;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700640
641 sdp->in_ep->driver_data = cdev; /* claim */
Sherry Sun52262982020-08-18 18:16:48 +0800642 sdp->out_ep->driver_data = cdev; /* claim */
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700643
644 sdp->altsetting = alt;
645 sdp->state = SDP_STATE_IDLE;
Sherry Sun52262982020-08-18 18:16:48 +0800646 sdp->ep_int_enable = true;
Stefan Agnerffbd5d02017-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 Sun52262982020-08-18 18:16:48 +0800663 usb_ep_disable(sdp->out_ep);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700664
665 if (sdp->in_req) {
Sherry Sun52262982020-08-18 18:16:48 +0800666 free(sdp->in_req->buf);
667 usb_ep_free_request(sdp->in_ep, sdp->in_req);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700668 sdp->in_req = NULL;
669 }
Sherry Sun52262982020-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 Agnerffbd5d02017-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 Lia9e8fe02020-08-18 18:16:44 +0800690 sdp_func->usb_function.hs_descriptors = sdp_runtime_hs_descs;
Stefan Agnerffbd5d02017-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 Vasut2bd299d2023-09-01 11:49:58 +0200705int sdp_init(struct udevice *udc)
Stefan Agnerffbd5d02017-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 Princef26cf932017-10-23 11:16:35 +0200713
Stefan Roese80877fa2022-09-02 14:10:46 +0200714 schedule();
Marek Vasut2bd299d2023-09-01 11:49:58 +0200715 dm_usb_gadget_handle_interrupts(udc);
Stefan Agnerffbd5d02017-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 Heidere1d461b2018-02-15 10:17:29 +0100732 entry = sdp_ptr(headerv2->entry);
Stefan Agnerffbd5d02017-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
Frieder Schrempf09580e22019-06-04 21:56:29 +0200739#ifdef CONFIG_SPL_BUILD
Peng Fanf49d4702020-08-18 18:16:46 +0800740static ulong sdp_load_read(struct spl_load_info *load, ulong sector,
741 ulong count, void *buf)
Frieder Schrempf09580e22019-06-04 21:56:29 +0200742{
743 debug("%s: sector %lx, count %lx, buf %lx\n",
744 __func__, sector, count, (ulong)buf);
745 memcpy(buf, (void *)(load->dev + sector), count);
746 return count;
747}
Peng Fanf49d4702020-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 Schrempf09580e22019-06-04 21:56:29 +0200774#endif
775
Pali Rohárdda8f882022-01-14 14:31:38 +0100776static int sdp_handle_in_ep(struct spl_image_info *spl_image,
777 struct spl_boot_device *bootdev)
Stefan Agnerffbd5d02017-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 Heidere1d461b2018-02-15 10:17:29 +0100813 memcpy(&data[1], sdp_ptr(sdp_func->dnl_address), datalen);
Stefan Agnerffbd5d02017-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 Agner3888da72017-08-16 11:00:52 -0700823 printf("Jumping to header at 0x%08x\n", sdp_func->jmp_address);
Andre Heidere1d461b2018-02-15 10:17:29 +0100824 status = sdp_jump_imxheader(sdp_ptr(sdp_func->jmp_address));
Stefan Agner3888da72017-08-16 11:00:52 -0700825
826 /* If imx header fails, try some U-Boot specific headers */
827 if (status) {
828#ifdef CONFIG_SPL_BUILD
Peng Fanf49d4702020-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 Glassbb7d3bb2022-09-06 20:26:52 -0600838 struct legacy_img_hdr *header =
Frieder Schrempf09580e22019-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");
845 load.dev = header;
846 load.bl_len = 1;
Peng Fanf49d4702020-08-18 18:16:46 +0800847 load.read = sdp_load_read;
Frieder Schrempf09580e22019-06-04 21:56:29 +0200848 spl_load_simple_fit(spl_image, &load, 0,
849 header);
850
Peng Fanf49d4702020-08-18 18:16:46 +0800851 return SDP_EXIT;
Frieder Schrempf09580e22019-06-04 21:56:29 +0200852 }
853#endif
Peng Fanf49d4702020-08-18 18:16:46 +0800854 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
855 struct spl_load_info load;
856
857 load.dev = header;
858 load.bl_len = 1;
859 load.read = sdp_load_read;
860 spl_load_imx_container(spl_image, &load, 0);
861 return SDP_EXIT;
862 }
863
Stefan Agner3888da72017-08-16 11:00:52 -0700864 /* In SPL, allow jumps to U-Boot images */
865 struct spl_image_info spl_image = {};
Pali Rohárdda8f882022-01-14 14:31:38 +0100866 struct spl_boot_device bootdev = {};
867 spl_parse_image_header(&spl_image, &bootdev, header);
Marek Vasuta0a4ce82023-03-07 08:42:33 +0100868 spl_board_prepare_for_boot();
Stefan Agner3888da72017-08-16 11:00:52 -0700869 jump_to_image_no_args(&spl_image);
870#else
871 /* In U-Boot, allow jumps to scripts */
Simon Glassdaee3ba2023-01-06 08:52:28 -0600872 cmd_source_script(sdp_func->jmp_address, NULL, NULL);
Stefan Agner3888da72017-08-16 11:00:52 -0700873#endif
874 }
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700875
876 sdp_func->next_state = SDP_STATE_IDLE;
877 sdp_func->error_status = status;
878
879 /* Only send Report 4 if there was an error */
880 if (status)
881 sdp_func->state = SDP_STATE_TX_STATUS;
882 else
883 sdp_func->state = SDP_STATE_IDLE;
884 break;
885 default:
886 break;
887 };
Peng Fanf49d4702020-08-18 18:16:46 +0800888
889 return 0;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700890}
891
Sherry Sun52262982020-08-18 18:16:48 +0800892static void sdp_handle_out_ep(void)
893{
894 int rc;
895
896 if (sdp_func->state == SDP_STATE_IDLE) {
897 sdp_func->out_req->complete = sdp_rx_command_complete;
898 rc = usb_ep_queue(sdp_func->out_ep, sdp_func->out_req, 0);
899 if (rc)
900 printf("error in submission: %s\n",
901 sdp_func->out_ep->name);
902 sdp_func->state = SDP_STATE_RX_CMD;
903 } else if (sdp_func->state == SDP_STATE_RX_FILE_DATA) {
904 sdp_func->out_req->complete = sdp_rx_data_complete;
905 rc = usb_ep_queue(sdp_func->out_ep, sdp_func->out_req, 0);
906 if (rc)
907 printf("error in submission: %s\n",
908 sdp_func->out_ep->name);
909 sdp_func->state = SDP_STATE_RX_FILE_DATA_BUSY;
910 }
911}
912
Frieder Schrempf09580e22019-06-04 21:56:29 +0200913#ifndef CONFIG_SPL_BUILD
Marek Vasut2bd299d2023-09-01 11:49:58 +0200914int sdp_handle(struct udevice *udc)
Frieder Schrempf09580e22019-06-04 21:56:29 +0200915#else
Marek Vasut2bd299d2023-09-01 11:49:58 +0200916int spl_sdp_handle(struct udevice *udc, struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100917 struct spl_boot_device *bootdev)
Frieder Schrempf09580e22019-06-04 21:56:29 +0200918#endif
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700919{
Peng Fanf49d4702020-08-18 18:16:46 +0800920 int flag = 0;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700921 printf("SDP: handle requests...\n");
922 while (1) {
923 if (ctrlc()) {
924 puts("\rCTRL+C - Operation aborted.\n");
Frieder Schrempf09580e22019-06-04 21:56:29 +0200925 return -EINVAL;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700926 }
927
Peng Fanf49d4702020-08-18 18:16:46 +0800928 if (flag == SDP_EXIT)
Frieder Schrempf09580e22019-06-04 21:56:29 +0200929 return 0;
Frieder Schrempf09580e22019-06-04 21:56:29 +0200930
Stefan Roese80877fa2022-09-02 14:10:46 +0200931 schedule();
Marek Vasut2bd299d2023-09-01 11:49:58 +0200932 dm_usb_gadget_handle_interrupts(udc);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700933
Frieder Schrempf09580e22019-06-04 21:56:29 +0200934#ifdef CONFIG_SPL_BUILD
Pali Rohárdda8f882022-01-14 14:31:38 +0100935 flag = sdp_handle_in_ep(spl_image, bootdev);
Frieder Schrempf09580e22019-06-04 21:56:29 +0200936#else
Pali Rohárdda8f882022-01-14 14:31:38 +0100937 flag = sdp_handle_in_ep(NULL, NULL);
Frieder Schrempf09580e22019-06-04 21:56:29 +0200938#endif
Sherry Sun52262982020-08-18 18:16:48 +0800939 if (sdp_func->ep_int_enable)
940 sdp_handle_out_ep();
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700941 }
942}
943
944int sdp_add(struct usb_configuration *c)
945{
946 int id;
947
948 id = usb_string_id(c->cdev);
949 if (id < 0)
950 return id;
951 strings_sdp_generic[0].id = id;
952 sdp_intf_runtime.iInterface = id;
953
954 debug("%s: cdev: %p gadget: %p gadget->ep0: %p\n", __func__,
955 c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
956
957 return sdp_bind_config(c);
958}
959
960DECLARE_GADGET_BIND_CALLBACK(usb_dnl_sdp, sdp_add);