blob: 9b7372815d4c24236385b0e52eca0277affea814 [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
Peng Fanf49d4702020-08-18 18:16:46 +080074#define SDP_EXIT 1
75
Stefan Agnerffbd5d02017-08-16 11:00:51 -070076struct sdp_command {
77 u16 cmd;
78 u32 addr;
79 u8 format;
80 u32 cnt;
81 u32 data;
82 u8 rsvd;
83} __packed;
84
85enum sdp_state {
86 SDP_STATE_IDLE,
87 SDP_STATE_RX_DCD_DATA,
88 SDP_STATE_RX_FILE_DATA,
89 SDP_STATE_TX_SEC_CONF,
90 SDP_STATE_TX_SEC_CONF_BUSY,
91 SDP_STATE_TX_REGISTER,
92 SDP_STATE_TX_REGISTER_BUSY,
93 SDP_STATE_TX_STATUS,
94 SDP_STATE_TX_STATUS_BUSY,
95 SDP_STATE_JUMP,
96};
97
98struct f_sdp {
99 struct usb_function usb_function;
100
101 struct usb_descriptor_header **function;
102
103 u8 altsetting;
104 enum sdp_state state;
105 enum sdp_state next_state;
106 u32 dnl_address;
Petr Štetiard991cc32018-11-23 14:37:52 +0100107 u32 dnl_bytes;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700108 u32 dnl_bytes_remaining;
109 u32 jmp_address;
110 bool always_send_status;
111 u32 error_status;
112
113 /* EP0 request */
114 struct usb_request *req;
115
116 /* EP1 IN */
117 struct usb_ep *in_ep;
118 struct usb_request *in_req;
119
120 bool configuration_done;
121};
122
123static struct f_sdp *sdp_func;
124
125static inline struct f_sdp *func_to_sdp(struct usb_function *f)
126{
127 return container_of(f, struct f_sdp, usb_function);
128}
129
130static struct usb_interface_descriptor sdp_intf_runtime = {
131 .bLength = sizeof(sdp_intf_runtime),
132 .bDescriptorType = USB_DT_INTERFACE,
133 .bAlternateSetting = 0,
134 .bNumEndpoints = 1,
135 .bInterfaceClass = USB_CLASS_HID,
136 .bInterfaceSubClass = 0,
137 .bInterfaceProtocol = 0,
138 /* .iInterface = DYNAMIC */
139};
140
141/* HID configuration */
142static struct usb_class_hid_descriptor sdp_hid_desc = {
143 .bLength = sizeof(sdp_hid_desc),
144 .bDescriptorType = USB_DT_CS_DEVICE,
145
146 .bcdCDC = __constant_cpu_to_le16(0x0110),
147 .bCountryCode = 0,
148 .bNumDescriptors = 1,
149
150 .bDescriptorType0 = USB_DT_HID_REPORT,
151 .wDescriptorLength0 = HID_USAGE_PAGE_LEN,
152};
153
154static struct usb_endpoint_descriptor in_desc = {
155 .bLength = USB_DT_ENDPOINT_SIZE,
156 .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
157
158 .bEndpointAddress = 1 | USB_DIR_IN,
159 .bmAttributes = USB_ENDPOINT_XFER_INT,
160 .wMaxPacketSize = 64,
161 .bInterval = 1,
162};
163
Ye Lia9e8fe02020-08-18 18:16:44 +0800164static struct usb_endpoint_descriptor in_hs_desc = {
165 .bLength = USB_DT_ENDPOINT_SIZE,
166 .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
167
168 .bEndpointAddress = 1 | USB_DIR_IN,
169 .bmAttributes = USB_ENDPOINT_XFER_INT,
170 .wMaxPacketSize = 512,
171 .bInterval = 1,
172};
173
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700174static struct usb_descriptor_header *sdp_runtime_descs[] = {
175 (struct usb_descriptor_header *)&sdp_intf_runtime,
176 (struct usb_descriptor_header *)&sdp_hid_desc,
177 (struct usb_descriptor_header *)&in_desc,
178 NULL,
179};
180
Ye Lia9e8fe02020-08-18 18:16:44 +0800181static struct usb_descriptor_header *sdp_runtime_hs_descs[] = {
182 (struct usb_descriptor_header *)&sdp_intf_runtime,
183 (struct usb_descriptor_header *)&sdp_hid_desc,
184 (struct usb_descriptor_header *)&in_hs_desc,
185 NULL,
186};
187
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700188/* This is synchronized with what the SoC implementation reports */
189static struct hid_report sdp_hid_report = {
190 .usage_page = {
191 0x06, 0x00, 0xff, /* Usage Page */
192 0x09, 0x01, /* Usage (Pointer?) */
193 0xa1, 0x01, /* Collection */
194
195 0x85, 0x01, /* Report ID */
196 0x19, 0x01, /* Usage Minimum */
197 0x29, 0x01, /* Usage Maximum */
198 0x15, 0x00, /* Local Minimum */
199 0x26, 0xFF, 0x00, /* Local Maximum? */
200 0x75, 0x08, /* Report Size */
201 0x95, 0x10, /* Report Count */
202 0x91, 0x02, /* Output Data */
203
204 0x85, 0x02, /* Report ID */
205 0x19, 0x01, /* Usage Minimum */
206 0x29, 0x01, /* Usage Maximum */
207 0x15, 0x00, /* Local Minimum */
208 0x26, 0xFF, 0x00, /* Local Maximum? */
209 0x75, 0x80, /* Report Size 128 */
210 0x95, 0x40, /* Report Count */
211 0x91, 0x02, /* Output Data */
212
213 0x85, 0x03, /* Report ID */
214 0x19, 0x01, /* Usage Minimum */
215 0x29, 0x01, /* Usage Maximum */
216 0x15, 0x00, /* Local Minimum */
217 0x26, 0xFF, 0x00, /* Local Maximum? */
218 0x75, 0x08, /* Report Size 8 */
219 0x95, 0x04, /* Report Count */
220 0x81, 0x02, /* Input Data */
221
222 0x85, 0x04, /* Report ID */
223 0x19, 0x01, /* Usage Minimum */
224 0x29, 0x01, /* Usage Maximum */
225 0x15, 0x00, /* Local Minimum */
226 0x26, 0xFF, 0x00, /* Local Maximum? */
227 0x75, 0x08, /* Report Size 8 */
228 0x95, 0x40, /* Report Count */
229 0x81, 0x02, /* Input Data */
230 0xc0
231 },
232};
233
234static const char sdp_name[] = "Serial Downloader Protocol";
235
236/*
237 * static strings, in UTF-8
238 */
239static struct usb_string strings_sdp_generic[] = {
240 [0].s = sdp_name,
241 { } /* end of list */
242};
243
244static struct usb_gadget_strings stringtab_sdp_generic = {
245 .language = 0x0409, /* en-us */
246 .strings = strings_sdp_generic,
247};
248
249static struct usb_gadget_strings *sdp_generic_strings[] = {
250 &stringtab_sdp_generic,
251 NULL,
252};
253
Andre Heidere1d461b2018-02-15 10:17:29 +0100254static inline void *sdp_ptr(u32 val)
255{
256 return (void *)(uintptr_t)val;
257}
258
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700259static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req)
260{
261 struct f_sdp *sdp = req->context;
262 int status = req->status;
263 u8 *data = req->buf;
264 u8 report = data[0];
265
266 if (status != 0) {
Andre Heiderb841e972018-02-15 07:08:55 +0100267 pr_err("Status: %d\n", status);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700268 return;
269 }
270
271 if (report != 1) {
Andre Heiderb841e972018-02-15 07:08:55 +0100272 pr_err("Unexpected report %d\n", report);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700273 return;
274 }
275
276 struct sdp_command *cmd = req->buf + 1;
277
278 debug("%s: command: %04x, addr: %08x, cnt: %u\n",
279 __func__, be16_to_cpu(cmd->cmd),
280 be32_to_cpu(cmd->addr), be32_to_cpu(cmd->cnt));
281
282 switch (be16_to_cpu(cmd->cmd)) {
283 case SDP_READ_REGISTER:
284 sdp->always_send_status = false;
285 sdp->error_status = 0x0;
286
287 sdp->state = SDP_STATE_TX_SEC_CONF;
288 sdp->dnl_address = be32_to_cpu(cmd->addr);
289 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
290 sdp->next_state = SDP_STATE_TX_REGISTER;
291 printf("Reading %d registers at 0x%08x... ",
292 sdp->dnl_bytes_remaining, sdp->dnl_address);
293 break;
294 case SDP_WRITE_FILE:
295 sdp->always_send_status = true;
296 sdp->error_status = SDP_WRITE_FILE_COMPLETE;
297
298 sdp->state = SDP_STATE_RX_FILE_DATA;
Frank Lic2bdb582020-04-29 10:35:11 +0800299 sdp->dnl_address = cmd->addr ? be32_to_cpu(cmd->addr) : CONFIG_SDP_LOADADDR;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700300 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
Petr Štetiard991cc32018-11-23 14:37:52 +0100301 sdp->dnl_bytes = sdp->dnl_bytes_remaining;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700302 sdp->next_state = SDP_STATE_IDLE;
303
304 printf("Downloading file of size %d to 0x%08x... ",
305 sdp->dnl_bytes_remaining, sdp->dnl_address);
306
307 break;
308 case SDP_ERROR_STATUS:
309 sdp->always_send_status = true;
310 sdp->error_status = 0;
311
312 sdp->state = SDP_STATE_TX_SEC_CONF;
313 sdp->next_state = SDP_STATE_IDLE;
314 break;
315 case SDP_DCD_WRITE:
316 sdp->always_send_status = true;
317 sdp->error_status = SDP_WRITE_REGISTER_COMPLETE;
318
319 sdp->state = SDP_STATE_RX_DCD_DATA;
320 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
321 sdp->next_state = SDP_STATE_IDLE;
322 break;
323 case SDP_JUMP_ADDRESS:
324 sdp->always_send_status = false;
325 sdp->error_status = 0;
326
Frank Lic2bdb582020-04-29 10:35:11 +0800327 sdp->jmp_address = cmd->addr ? be32_to_cpu(cmd->addr) : CONFIG_SDP_LOADADDR;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700328 sdp->state = SDP_STATE_TX_SEC_CONF;
329 sdp->next_state = SDP_STATE_JUMP;
330 break;
331 case SDP_SKIP_DCD_HEADER:
332 sdp->always_send_status = true;
333 sdp->error_status = SDP_SKIP_DCD_HEADER_COMPLETE;
334
335 /* Ignore command, DCD not supported anyway */
336 sdp->state = SDP_STATE_TX_SEC_CONF;
337 sdp->next_state = SDP_STATE_IDLE;
338 break;
339 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900340 pr_err("Unknown command: %04x\n", be16_to_cpu(cmd->cmd));
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700341 }
342}
343
344static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req)
345{
346 struct f_sdp *sdp = req->context;
347 int status = req->status;
348 u8 *data = req->buf;
349 u8 report = data[0];
350 int datalen = req->length - 1;
351
352 if (status != 0) {
Andre Heiderb841e972018-02-15 07:08:55 +0100353 pr_err("Status: %d\n", status);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700354 return;
355 }
356
357 if (report != 2) {
Andre Heiderb841e972018-02-15 07:08:55 +0100358 pr_err("Unexpected report %d\n", report);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700359 return;
360 }
361
362 if (sdp->dnl_bytes_remaining < datalen) {
363 /*
364 * Some USB stacks require to send a complete buffer as
365 * specified in the HID descriptor. This leads to longer
366 * transfers than the file length, no problem for us.
367 */
368 sdp->dnl_bytes_remaining = 0;
369 } else {
370 sdp->dnl_bytes_remaining -= datalen;
371 }
372
373 if (sdp->state == SDP_STATE_RX_FILE_DATA) {
Andre Heidere1d461b2018-02-15 10:17:29 +0100374 memcpy(sdp_ptr(sdp->dnl_address), req->buf + 1, datalen);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700375 sdp->dnl_address += datalen;
376 }
377
378 if (sdp->dnl_bytes_remaining)
379 return;
380
Petr Štetiard991cc32018-11-23 14:37:52 +0100381#ifndef CONFIG_SPL_BUILD
382 env_set_hex("filesize", sdp->dnl_bytes);
383#endif
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700384 printf("done\n");
385
386 switch (sdp->state) {
387 case SDP_STATE_RX_FILE_DATA:
388 sdp->state = SDP_STATE_TX_SEC_CONF;
389 break;
390 case SDP_STATE_RX_DCD_DATA:
391 sdp->state = SDP_STATE_TX_SEC_CONF;
392 break;
393 default:
Andre Heiderb841e972018-02-15 07:08:55 +0100394 pr_err("Invalid state: %d\n", sdp->state);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700395 }
396}
397
398static void sdp_tx_complete(struct usb_ep *ep, struct usb_request *req)
399{
400 struct f_sdp *sdp = req->context;
401 int status = req->status;
402
403 if (status != 0) {
Andre Heiderb841e972018-02-15 07:08:55 +0100404 pr_err("Status: %d\n", status);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700405 return;
406 }
407
408 switch (sdp->state) {
409 case SDP_STATE_TX_SEC_CONF_BUSY:
410 /* Not all commands require status report */
411 if (sdp->always_send_status || sdp->error_status)
412 sdp->state = SDP_STATE_TX_STATUS;
413 else
414 sdp->state = sdp->next_state;
415
416 break;
417 case SDP_STATE_TX_STATUS_BUSY:
418 sdp->state = sdp->next_state;
419 break;
420 case SDP_STATE_TX_REGISTER_BUSY:
421 if (sdp->dnl_bytes_remaining)
422 sdp->state = SDP_STATE_TX_REGISTER;
423 else
424 sdp->state = SDP_STATE_IDLE;
425 break;
426 default:
Andre Heiderb841e972018-02-15 07:08:55 +0100427 pr_err("Wrong State: %d\n", sdp->state);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700428 sdp->state = SDP_STATE_IDLE;
429 break;
430 }
431 debug("%s complete --> %d, %d/%d\n", ep->name,
432 status, req->actual, req->length);
433}
434
435static int sdp_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
436{
437 struct usb_gadget *gadget = f->config->cdev->gadget;
438 struct usb_request *req = f->config->cdev->req;
439 struct f_sdp *sdp = f->config->cdev->req->context;
440 u16 len = le16_to_cpu(ctrl->wLength);
441 u16 w_value = le16_to_cpu(ctrl->wValue);
442 int value = 0;
443 u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
444
445 debug("w_value: 0x%04x len: 0x%04x\n", w_value, len);
446 debug("req_type: 0x%02x ctrl->bRequest: 0x%02x sdp->state: %d\n",
447 req_type, ctrl->bRequest, sdp->state);
448
449 if (req_type == USB_TYPE_STANDARD) {
450 if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR) {
451 /* Send HID report descriptor */
452 value = min(len, (u16) sizeof(sdp_hid_report));
453 memcpy(req->buf, &sdp_hid_report, value);
454 sdp->configuration_done = true;
455 }
456 }
457
458 if (req_type == USB_TYPE_CLASS) {
459 int report = w_value & HID_REPORT_ID_MASK;
460
461 /* HID (SDP) request */
462 switch (ctrl->bRequest) {
463 case HID_REQ_SET_REPORT:
464 switch (report) {
465 case 1:
466 value = SDP_COMMAND_LEN + 1;
467 req->complete = sdp_rx_command_complete;
468 break;
469 case 2:
470 value = len;
471 req->complete = sdp_rx_data_complete;
472 break;
473 }
474 }
475 }
476
477 if (value >= 0) {
478 req->length = value;
479 req->zero = value < len;
480 value = usb_ep_queue(gadget->ep0, req, 0);
481 if (value < 0) {
482 debug("ep_queue --> %d\n", value);
483 req->status = 0;
484 }
485 }
486
487 return value;
488}
489
490static int sdp_bind(struct usb_configuration *c, struct usb_function *f)
491{
492 struct usb_gadget *gadget = c->cdev->gadget;
493 struct usb_composite_dev *cdev = c->cdev;
494 struct f_sdp *sdp = func_to_sdp(f);
495 int rv = 0, id;
496
497 id = usb_interface_id(c, f);
498 if (id < 0)
499 return id;
500 sdp_intf_runtime.bInterfaceNumber = id;
501
502 struct usb_ep *ep;
503
504 /* allocate instance-specific endpoints */
505 ep = usb_ep_autoconfig(gadget, &in_desc);
506 if (!ep) {
507 rv = -ENODEV;
508 goto error;
509 }
510
Ye Lia9e8fe02020-08-18 18:16:44 +0800511 if (gadget_is_dualspeed(gadget)) {
512 /* Assume endpoint addresses are the same for both speeds */
513 in_hs_desc.bEndpointAddress = in_desc.bEndpointAddress;
514 }
515
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700516 sdp->in_ep = ep; /* Store IN EP for enabling @ setup */
517
518 cdev->req->context = sdp;
519
520error:
521 return rv;
522}
523
524static void sdp_unbind(struct usb_configuration *c, struct usb_function *f)
525{
526 free(sdp_func);
527 sdp_func = NULL;
528}
529
530static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
531{
532 struct usb_request *req;
533
534 req = usb_ep_alloc_request(ep, 0);
535 if (!req)
536 return req;
537
538 req->length = length;
539 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
540 if (!req->buf) {
541 usb_ep_free_request(ep, req);
542 req = NULL;
543 }
544
545 return req;
546}
547
548
549static struct usb_request *sdp_start_ep(struct usb_ep *ep)
550{
551 struct usb_request *req;
552
Ye Li5af7ccf2020-08-18 18:16:45 +0800553 req = alloc_ep_req(ep, 65);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700554 debug("%s: ep:%p req:%p\n", __func__, ep, req);
555
556 if (!req)
557 return NULL;
558
559 memset(req->buf, 0, req->length);
560 req->complete = sdp_tx_complete;
561
562 return req;
563}
564static int sdp_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
565{
566 struct f_sdp *sdp = func_to_sdp(f);
567 struct usb_composite_dev *cdev = f->config->cdev;
Ye Lia9e8fe02020-08-18 18:16:44 +0800568 struct usb_gadget *gadget = cdev->gadget;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700569 int result;
570
571 debug("%s: intf: %d alt: %d\n", __func__, intf, alt);
572
Ye Lia9e8fe02020-08-18 18:16:44 +0800573 if (gadget_is_dualspeed(gadget) && gadget->speed == USB_SPEED_HIGH)
574 result = usb_ep_enable(sdp->in_ep, &in_hs_desc);
575 else
576 result = usb_ep_enable(sdp->in_ep, &in_desc);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700577 if (result)
578 return result;
579 sdp->in_req = sdp_start_ep(sdp->in_ep);
580 sdp->in_req->context = sdp;
581
582 sdp->in_ep->driver_data = cdev; /* claim */
583
584 sdp->altsetting = alt;
585 sdp->state = SDP_STATE_IDLE;
586
587 return 0;
588}
589
590static int sdp_get_alt(struct usb_function *f, unsigned intf)
591{
592 struct f_sdp *sdp = func_to_sdp(f);
593
594 return sdp->altsetting;
595}
596
597static void sdp_disable(struct usb_function *f)
598{
599 struct f_sdp *sdp = func_to_sdp(f);
600
601 usb_ep_disable(sdp->in_ep);
602
603 if (sdp->in_req) {
604 free(sdp->in_req);
605 sdp->in_req = NULL;
606 }
607}
608
609static int sdp_bind_config(struct usb_configuration *c)
610{
611 int status;
612
613 if (!sdp_func) {
614 sdp_func = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*sdp_func));
615 if (!sdp_func)
616 return -ENOMEM;
617 }
618
619 memset(sdp_func, 0, sizeof(*sdp_func));
620
621 sdp_func->usb_function.name = "sdp";
Ye Lia9e8fe02020-08-18 18:16:44 +0800622 sdp_func->usb_function.hs_descriptors = sdp_runtime_hs_descs;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700623 sdp_func->usb_function.descriptors = sdp_runtime_descs;
624 sdp_func->usb_function.bind = sdp_bind;
625 sdp_func->usb_function.unbind = sdp_unbind;
626 sdp_func->usb_function.set_alt = sdp_set_alt;
627 sdp_func->usb_function.get_alt = sdp_get_alt;
628 sdp_func->usb_function.disable = sdp_disable;
629 sdp_func->usb_function.strings = sdp_generic_strings;
630 sdp_func->usb_function.setup = sdp_setup;
631
632 status = usb_add_function(c, &sdp_func->usb_function);
633
634 return status;
635}
636
637int sdp_init(int controller_index)
638{
639 printf("SDP: initialize...\n");
640 while (!sdp_func->configuration_done) {
641 if (ctrlc()) {
642 puts("\rCTRL+C - Operation aborted.\n");
643 return 1;
644 }
Vincent Princef26cf932017-10-23 11:16:35 +0200645
646 WATCHDOG_RESET();
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700647 usb_gadget_handle_interrupts(controller_index);
648 }
649
650 return 0;
651}
652
653static u32 sdp_jump_imxheader(void *address)
654{
655 flash_header_v2_t *headerv2 = address;
656 ulong (*entry)(void);
657
658 if (headerv2->header.tag != IVT_HEADER_TAG) {
659 printf("Header Tag is not an IMX image\n");
660 return SDP_ERROR_IMXHEADER;
661 }
662
663 printf("Jumping to 0x%08x\n", headerv2->entry);
Andre Heidere1d461b2018-02-15 10:17:29 +0100664 entry = sdp_ptr(headerv2->entry);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700665 entry();
666
667 /* The image probably never returns hence we won't reach that point */
668 return 0;
669}
670
Frieder Schrempf09580e22019-06-04 21:56:29 +0200671#ifdef CONFIG_SPL_BUILD
Peng Fanf49d4702020-08-18 18:16:46 +0800672static ulong sdp_load_read(struct spl_load_info *load, ulong sector,
673 ulong count, void *buf)
Frieder Schrempf09580e22019-06-04 21:56:29 +0200674{
675 debug("%s: sector %lx, count %lx, buf %lx\n",
676 __func__, sector, count, (ulong)buf);
677 memcpy(buf, (void *)(load->dev + sector), count);
678 return count;
679}
Peng Fanf49d4702020-08-18 18:16:46 +0800680
681static ulong search_fit_header(ulong p, int size)
682{
683 int i;
684
685 for (i = 0; i < size; i += 4) {
686 if (genimg_get_format((const void *)(p + i)) == IMAGE_FORMAT_FIT)
687 return p + i;
688 }
689
690 return 0;
691}
692
693static ulong search_container_header(ulong p, int size)
694{
695 int i;
696 u8 *hdr;
697
698 for (i = 0; i < size; i += 4) {
699 hdr = (u8 *)(p + i);
700 if (*(hdr + 3) == 0x87 && *hdr == 0)
701 if (*(hdr + 1) != 0 || *(hdr + 2) != 0)
702 return p + i;
703 }
704 return 0;
705}
Frieder Schrempf09580e22019-06-04 21:56:29 +0200706#endif
707
Peng Fanf49d4702020-08-18 18:16:46 +0800708static int sdp_handle_in_ep(struct spl_image_info *spl_image)
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700709{
710 u8 *data = sdp_func->in_req->buf;
711 u32 status;
712 int datalen;
713
714 switch (sdp_func->state) {
715 case SDP_STATE_TX_SEC_CONF:
716 debug("Report 3: HAB security\n");
717 data[0] = 3;
718
719 status = SDP_SECURITY_OPEN;
720 memcpy(&data[1], &status, 4);
721 sdp_func->in_req->length = 5;
722 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
723 sdp_func->state = SDP_STATE_TX_SEC_CONF_BUSY;
724 break;
725
726 case SDP_STATE_TX_STATUS:
727 debug("Report 4: Status\n");
728 data[0] = 4;
729
730 memcpy(&data[1], &sdp_func->error_status, 4);
731 sdp_func->in_req->length = 65;
732 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
733 sdp_func->state = SDP_STATE_TX_STATUS_BUSY;
734 break;
735 case SDP_STATE_TX_REGISTER:
736 debug("Report 4: Register Values\n");
737 data[0] = 4;
738
739 datalen = sdp_func->dnl_bytes_remaining;
740
741 if (datalen > 64)
742 datalen = 64;
743
Andre Heidere1d461b2018-02-15 10:17:29 +0100744 memcpy(&data[1], sdp_ptr(sdp_func->dnl_address), datalen);
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700745 sdp_func->in_req->length = 65;
746
747 sdp_func->dnl_bytes_remaining -= datalen;
748 sdp_func->dnl_address += datalen;
749
750 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
751 sdp_func->state = SDP_STATE_TX_REGISTER_BUSY;
752 break;
753 case SDP_STATE_JUMP:
Stefan Agner3888da72017-08-16 11:00:52 -0700754 printf("Jumping to header at 0x%08x\n", sdp_func->jmp_address);
Andre Heidere1d461b2018-02-15 10:17:29 +0100755 status = sdp_jump_imxheader(sdp_ptr(sdp_func->jmp_address));
Stefan Agner3888da72017-08-16 11:00:52 -0700756
757 /* If imx header fails, try some U-Boot specific headers */
758 if (status) {
759#ifdef CONFIG_SPL_BUILD
Peng Fanf49d4702020-08-18 18:16:46 +0800760 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
761 sdp_func->jmp_address = (u32)search_container_header((ulong)sdp_func->jmp_address, sdp_func->dnl_bytes);
762 else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
763 sdp_func->jmp_address = (u32)search_fit_header((ulong)sdp_func->jmp_address, sdp_func->dnl_bytes);
764 if (sdp_func->jmp_address == 0)
765 panic("Error in search header, failed to jump\n");
766
767 printf("Found header at 0x%08x\n", sdp_func->jmp_address);
768
Frieder Schrempf09580e22019-06-04 21:56:29 +0200769 image_header_t *header =
770 sdp_ptr(sdp_func->jmp_address);
771#ifdef CONFIG_SPL_LOAD_FIT
772 if (image_get_magic(header) == FDT_MAGIC) {
773 struct spl_load_info load;
774
775 debug("Found FIT\n");
776 load.dev = header;
777 load.bl_len = 1;
Peng Fanf49d4702020-08-18 18:16:46 +0800778 load.read = sdp_load_read;
Frieder Schrempf09580e22019-06-04 21:56:29 +0200779 spl_load_simple_fit(spl_image, &load, 0,
780 header);
781
Peng Fanf49d4702020-08-18 18:16:46 +0800782 return SDP_EXIT;
Frieder Schrempf09580e22019-06-04 21:56:29 +0200783 }
784#endif
Peng Fanf49d4702020-08-18 18:16:46 +0800785 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
786 struct spl_load_info load;
787
788 load.dev = header;
789 load.bl_len = 1;
790 load.read = sdp_load_read;
791 spl_load_imx_container(spl_image, &load, 0);
792 return SDP_EXIT;
793 }
794
Stefan Agner3888da72017-08-16 11:00:52 -0700795 /* In SPL, allow jumps to U-Boot images */
796 struct spl_image_info spl_image = {};
Frieder Schrempf09580e22019-06-04 21:56:29 +0200797 spl_parse_image_header(&spl_image, header);
Stefan Agner3888da72017-08-16 11:00:52 -0700798 jump_to_image_no_args(&spl_image);
799#else
800 /* In U-Boot, allow jumps to scripts */
Simon Glass606f09b2019-12-28 10:45:04 -0700801 image_source_script(sdp_func->jmp_address, "script@1");
Stefan Agner3888da72017-08-16 11:00:52 -0700802#endif
803 }
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700804
805 sdp_func->next_state = SDP_STATE_IDLE;
806 sdp_func->error_status = status;
807
808 /* Only send Report 4 if there was an error */
809 if (status)
810 sdp_func->state = SDP_STATE_TX_STATUS;
811 else
812 sdp_func->state = SDP_STATE_IDLE;
813 break;
814 default:
815 break;
816 };
Peng Fanf49d4702020-08-18 18:16:46 +0800817
818 return 0;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700819}
820
Frieder Schrempf09580e22019-06-04 21:56:29 +0200821#ifndef CONFIG_SPL_BUILD
822int sdp_handle(int controller_index)
823#else
824int spl_sdp_handle(int controller_index, struct spl_image_info *spl_image)
825#endif
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700826{
Peng Fanf49d4702020-08-18 18:16:46 +0800827 int flag = 0;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700828 printf("SDP: handle requests...\n");
829 while (1) {
830 if (ctrlc()) {
831 puts("\rCTRL+C - Operation aborted.\n");
Frieder Schrempf09580e22019-06-04 21:56:29 +0200832 return -EINVAL;
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700833 }
834
Peng Fanf49d4702020-08-18 18:16:46 +0800835 if (flag == SDP_EXIT)
Frieder Schrempf09580e22019-06-04 21:56:29 +0200836 return 0;
Frieder Schrempf09580e22019-06-04 21:56:29 +0200837
Vincent Princef26cf932017-10-23 11:16:35 +0200838 WATCHDOG_RESET();
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700839 usb_gadget_handle_interrupts(controller_index);
840
Frieder Schrempf09580e22019-06-04 21:56:29 +0200841#ifdef CONFIG_SPL_BUILD
Peng Fanf49d4702020-08-18 18:16:46 +0800842 flag = sdp_handle_in_ep(spl_image);
Frieder Schrempf09580e22019-06-04 21:56:29 +0200843#else
Peng Fanf49d4702020-08-18 18:16:46 +0800844 flag = sdp_handle_in_ep(NULL);
Frieder Schrempf09580e22019-06-04 21:56:29 +0200845#endif
Stefan Agnerffbd5d02017-08-16 11:00:51 -0700846 }
847}
848
849int sdp_add(struct usb_configuration *c)
850{
851 int id;
852
853 id = usb_string_id(c->cdev);
854 if (id < 0)
855 return id;
856 strings_sdp_generic[0].id = id;
857 sdp_intf_runtime.iInterface = id;
858
859 debug("%s: cdev: %p gadget: %p gadget->ep0: %p\n", __func__,
860 c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
861
862 return sdp_bind_config(c);
863}
864
865DECLARE_GADGET_BIND_CALLBACK(usb_dnl_sdp, sdp_add);