blob: eec7560fc2a770a926a20e5cc7601825c38a6dbc [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>
20#include <common.h>
21#include <console.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060022#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060023#include <log.h>
Stefan Agner5661f082017-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 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>
Vincent Prince8171dac2017-10-23 11:16:35 +020036#include <watchdog.h>
Stefan Agner5661f082017-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
74struct sdp_command {
75 u16 cmd;
76 u32 addr;
77 u8 format;
78 u32 cnt;
79 u32 data;
80 u8 rsvd;
81} __packed;
82
83enum sdp_state {
84 SDP_STATE_IDLE,
85 SDP_STATE_RX_DCD_DATA,
86 SDP_STATE_RX_FILE_DATA,
87 SDP_STATE_TX_SEC_CONF,
88 SDP_STATE_TX_SEC_CONF_BUSY,
89 SDP_STATE_TX_REGISTER,
90 SDP_STATE_TX_REGISTER_BUSY,
91 SDP_STATE_TX_STATUS,
92 SDP_STATE_TX_STATUS_BUSY,
93 SDP_STATE_JUMP,
94};
95
96struct f_sdp {
97 struct usb_function usb_function;
98
99 struct usb_descriptor_header **function;
100
101 u8 altsetting;
102 enum sdp_state state;
103 enum sdp_state next_state;
104 u32 dnl_address;
Petr Štetiarbb00a012018-11-23 14:37:52 +0100105 u32 dnl_bytes;
Stefan Agner5661f082017-08-16 11:00:51 -0700106 u32 dnl_bytes_remaining;
107 u32 jmp_address;
108 bool always_send_status;
109 u32 error_status;
110
111 /* EP0 request */
112 struct usb_request *req;
113
114 /* EP1 IN */
115 struct usb_ep *in_ep;
116 struct usb_request *in_req;
117
118 bool configuration_done;
119};
120
121static struct f_sdp *sdp_func;
122
123static inline struct f_sdp *func_to_sdp(struct usb_function *f)
124{
125 return container_of(f, struct f_sdp, usb_function);
126}
127
128static struct usb_interface_descriptor sdp_intf_runtime = {
129 .bLength = sizeof(sdp_intf_runtime),
130 .bDescriptorType = USB_DT_INTERFACE,
131 .bAlternateSetting = 0,
132 .bNumEndpoints = 1,
133 .bInterfaceClass = USB_CLASS_HID,
134 .bInterfaceSubClass = 0,
135 .bInterfaceProtocol = 0,
136 /* .iInterface = DYNAMIC */
137};
138
139/* HID configuration */
140static struct usb_class_hid_descriptor sdp_hid_desc = {
141 .bLength = sizeof(sdp_hid_desc),
142 .bDescriptorType = USB_DT_CS_DEVICE,
143
144 .bcdCDC = __constant_cpu_to_le16(0x0110),
145 .bCountryCode = 0,
146 .bNumDescriptors = 1,
147
148 .bDescriptorType0 = USB_DT_HID_REPORT,
149 .wDescriptorLength0 = HID_USAGE_PAGE_LEN,
150};
151
152static struct usb_endpoint_descriptor in_desc = {
153 .bLength = USB_DT_ENDPOINT_SIZE,
154 .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
155
156 .bEndpointAddress = 1 | USB_DIR_IN,
157 .bmAttributes = USB_ENDPOINT_XFER_INT,
158 .wMaxPacketSize = 64,
159 .bInterval = 1,
160};
161
Ye Lid10d4292020-08-18 18:16:44 +0800162static struct usb_endpoint_descriptor in_hs_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 = 512,
169 .bInterval = 1,
170};
171
Stefan Agner5661f082017-08-16 11:00:51 -0700172static struct usb_descriptor_header *sdp_runtime_descs[] = {
173 (struct usb_descriptor_header *)&sdp_intf_runtime,
174 (struct usb_descriptor_header *)&sdp_hid_desc,
175 (struct usb_descriptor_header *)&in_desc,
176 NULL,
177};
178
Ye Lid10d4292020-08-18 18:16:44 +0800179static struct usb_descriptor_header *sdp_runtime_hs_descs[] = {
180 (struct usb_descriptor_header *)&sdp_intf_runtime,
181 (struct usb_descriptor_header *)&sdp_hid_desc,
182 (struct usb_descriptor_header *)&in_hs_desc,
183 NULL,
184};
185
Stefan Agner5661f082017-08-16 11:00:51 -0700186/* This is synchronized with what the SoC implementation reports */
187static struct hid_report sdp_hid_report = {
188 .usage_page = {
189 0x06, 0x00, 0xff, /* Usage Page */
190 0x09, 0x01, /* Usage (Pointer?) */
191 0xa1, 0x01, /* Collection */
192
193 0x85, 0x01, /* Report ID */
194 0x19, 0x01, /* Usage Minimum */
195 0x29, 0x01, /* Usage Maximum */
196 0x15, 0x00, /* Local Minimum */
197 0x26, 0xFF, 0x00, /* Local Maximum? */
198 0x75, 0x08, /* Report Size */
199 0x95, 0x10, /* Report Count */
200 0x91, 0x02, /* Output Data */
201
202 0x85, 0x02, /* Report ID */
203 0x19, 0x01, /* Usage Minimum */
204 0x29, 0x01, /* Usage Maximum */
205 0x15, 0x00, /* Local Minimum */
206 0x26, 0xFF, 0x00, /* Local Maximum? */
207 0x75, 0x80, /* Report Size 128 */
208 0x95, 0x40, /* Report Count */
209 0x91, 0x02, /* Output Data */
210
211 0x85, 0x03, /* Report ID */
212 0x19, 0x01, /* Usage Minimum */
213 0x29, 0x01, /* Usage Maximum */
214 0x15, 0x00, /* Local Minimum */
215 0x26, 0xFF, 0x00, /* Local Maximum? */
216 0x75, 0x08, /* Report Size 8 */
217 0x95, 0x04, /* Report Count */
218 0x81, 0x02, /* Input Data */
219
220 0x85, 0x04, /* Report ID */
221 0x19, 0x01, /* Usage Minimum */
222 0x29, 0x01, /* Usage Maximum */
223 0x15, 0x00, /* Local Minimum */
224 0x26, 0xFF, 0x00, /* Local Maximum? */
225 0x75, 0x08, /* Report Size 8 */
226 0x95, 0x40, /* Report Count */
227 0x81, 0x02, /* Input Data */
228 0xc0
229 },
230};
231
232static const char sdp_name[] = "Serial Downloader Protocol";
233
234/*
235 * static strings, in UTF-8
236 */
237static struct usb_string strings_sdp_generic[] = {
238 [0].s = sdp_name,
239 { } /* end of list */
240};
241
242static struct usb_gadget_strings stringtab_sdp_generic = {
243 .language = 0x0409, /* en-us */
244 .strings = strings_sdp_generic,
245};
246
247static struct usb_gadget_strings *sdp_generic_strings[] = {
248 &stringtab_sdp_generic,
249 NULL,
250};
251
Andre Heidera64a6142018-02-15 10:17:29 +0100252static inline void *sdp_ptr(u32 val)
253{
254 return (void *)(uintptr_t)val;
255}
256
Stefan Agner5661f082017-08-16 11:00:51 -0700257static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req)
258{
259 struct f_sdp *sdp = req->context;
260 int status = req->status;
261 u8 *data = req->buf;
262 u8 report = data[0];
263
264 if (status != 0) {
Andre Heider24ccd0c2018-02-15 07:08:55 +0100265 pr_err("Status: %d\n", status);
Stefan Agner5661f082017-08-16 11:00:51 -0700266 return;
267 }
268
269 if (report != 1) {
Andre Heider24ccd0c2018-02-15 07:08:55 +0100270 pr_err("Unexpected report %d\n", report);
Stefan Agner5661f082017-08-16 11:00:51 -0700271 return;
272 }
273
274 struct sdp_command *cmd = req->buf + 1;
275
276 debug("%s: command: %04x, addr: %08x, cnt: %u\n",
277 __func__, be16_to_cpu(cmd->cmd),
278 be32_to_cpu(cmd->addr), be32_to_cpu(cmd->cnt));
279
280 switch (be16_to_cpu(cmd->cmd)) {
281 case SDP_READ_REGISTER:
282 sdp->always_send_status = false;
283 sdp->error_status = 0x0;
284
285 sdp->state = SDP_STATE_TX_SEC_CONF;
286 sdp->dnl_address = be32_to_cpu(cmd->addr);
287 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
288 sdp->next_state = SDP_STATE_TX_REGISTER;
289 printf("Reading %d registers at 0x%08x... ",
290 sdp->dnl_bytes_remaining, sdp->dnl_address);
291 break;
292 case SDP_WRITE_FILE:
293 sdp->always_send_status = true;
294 sdp->error_status = SDP_WRITE_FILE_COMPLETE;
295
296 sdp->state = SDP_STATE_RX_FILE_DATA;
Frank Li8f95c012020-04-29 10:35:11 +0800297 sdp->dnl_address = cmd->addr ? be32_to_cpu(cmd->addr) : CONFIG_SDP_LOADADDR;
Stefan Agner5661f082017-08-16 11:00:51 -0700298 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
Petr Štetiarbb00a012018-11-23 14:37:52 +0100299 sdp->dnl_bytes = sdp->dnl_bytes_remaining;
Stefan Agner5661f082017-08-16 11:00:51 -0700300 sdp->next_state = SDP_STATE_IDLE;
301
302 printf("Downloading file of size %d to 0x%08x... ",
303 sdp->dnl_bytes_remaining, sdp->dnl_address);
304
305 break;
306 case SDP_ERROR_STATUS:
307 sdp->always_send_status = true;
308 sdp->error_status = 0;
309
310 sdp->state = SDP_STATE_TX_SEC_CONF;
311 sdp->next_state = SDP_STATE_IDLE;
312 break;
313 case SDP_DCD_WRITE:
314 sdp->always_send_status = true;
315 sdp->error_status = SDP_WRITE_REGISTER_COMPLETE;
316
317 sdp->state = SDP_STATE_RX_DCD_DATA;
318 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
319 sdp->next_state = SDP_STATE_IDLE;
320 break;
321 case SDP_JUMP_ADDRESS:
322 sdp->always_send_status = false;
323 sdp->error_status = 0;
324
Frank Li8f95c012020-04-29 10:35:11 +0800325 sdp->jmp_address = cmd->addr ? be32_to_cpu(cmd->addr) : CONFIG_SDP_LOADADDR;
Stefan Agner5661f082017-08-16 11:00:51 -0700326 sdp->state = SDP_STATE_TX_SEC_CONF;
327 sdp->next_state = SDP_STATE_JUMP;
328 break;
329 case SDP_SKIP_DCD_HEADER:
330 sdp->always_send_status = true;
331 sdp->error_status = SDP_SKIP_DCD_HEADER_COMPLETE;
332
333 /* Ignore command, DCD not supported anyway */
334 sdp->state = SDP_STATE_TX_SEC_CONF;
335 sdp->next_state = SDP_STATE_IDLE;
336 break;
337 default:
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900338 pr_err("Unknown command: %04x\n", be16_to_cpu(cmd->cmd));
Stefan Agner5661f082017-08-16 11:00:51 -0700339 }
340}
341
342static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req)
343{
344 struct f_sdp *sdp = req->context;
345 int status = req->status;
346 u8 *data = req->buf;
347 u8 report = data[0];
348 int datalen = req->length - 1;
349
350 if (status != 0) {
Andre Heider24ccd0c2018-02-15 07:08:55 +0100351 pr_err("Status: %d\n", status);
Stefan Agner5661f082017-08-16 11:00:51 -0700352 return;
353 }
354
355 if (report != 2) {
Andre Heider24ccd0c2018-02-15 07:08:55 +0100356 pr_err("Unexpected report %d\n", report);
Stefan Agner5661f082017-08-16 11:00:51 -0700357 return;
358 }
359
360 if (sdp->dnl_bytes_remaining < datalen) {
361 /*
362 * Some USB stacks require to send a complete buffer as
363 * specified in the HID descriptor. This leads to longer
364 * transfers than the file length, no problem for us.
365 */
366 sdp->dnl_bytes_remaining = 0;
367 } else {
368 sdp->dnl_bytes_remaining -= datalen;
369 }
370
371 if (sdp->state == SDP_STATE_RX_FILE_DATA) {
Andre Heidera64a6142018-02-15 10:17:29 +0100372 memcpy(sdp_ptr(sdp->dnl_address), req->buf + 1, datalen);
Stefan Agner5661f082017-08-16 11:00:51 -0700373 sdp->dnl_address += datalen;
374 }
375
376 if (sdp->dnl_bytes_remaining)
377 return;
378
Petr Štetiarbb00a012018-11-23 14:37:52 +0100379#ifndef CONFIG_SPL_BUILD
380 env_set_hex("filesize", sdp->dnl_bytes);
381#endif
Stefan Agner5661f082017-08-16 11:00:51 -0700382 printf("done\n");
383
384 switch (sdp->state) {
385 case SDP_STATE_RX_FILE_DATA:
386 sdp->state = SDP_STATE_TX_SEC_CONF;
387 break;
388 case SDP_STATE_RX_DCD_DATA:
389 sdp->state = SDP_STATE_TX_SEC_CONF;
390 break;
391 default:
Andre Heider24ccd0c2018-02-15 07:08:55 +0100392 pr_err("Invalid state: %d\n", sdp->state);
Stefan Agner5661f082017-08-16 11:00:51 -0700393 }
394}
395
396static void sdp_tx_complete(struct usb_ep *ep, struct usb_request *req)
397{
398 struct f_sdp *sdp = req->context;
399 int status = req->status;
400
401 if (status != 0) {
Andre Heider24ccd0c2018-02-15 07:08:55 +0100402 pr_err("Status: %d\n", status);
Stefan Agner5661f082017-08-16 11:00:51 -0700403 return;
404 }
405
406 switch (sdp->state) {
407 case SDP_STATE_TX_SEC_CONF_BUSY:
408 /* Not all commands require status report */
409 if (sdp->always_send_status || sdp->error_status)
410 sdp->state = SDP_STATE_TX_STATUS;
411 else
412 sdp->state = sdp->next_state;
413
414 break;
415 case SDP_STATE_TX_STATUS_BUSY:
416 sdp->state = sdp->next_state;
417 break;
418 case SDP_STATE_TX_REGISTER_BUSY:
419 if (sdp->dnl_bytes_remaining)
420 sdp->state = SDP_STATE_TX_REGISTER;
421 else
422 sdp->state = SDP_STATE_IDLE;
423 break;
424 default:
Andre Heider24ccd0c2018-02-15 07:08:55 +0100425 pr_err("Wrong State: %d\n", sdp->state);
Stefan Agner5661f082017-08-16 11:00:51 -0700426 sdp->state = SDP_STATE_IDLE;
427 break;
428 }
429 debug("%s complete --> %d, %d/%d\n", ep->name,
430 status, req->actual, req->length);
431}
432
433static int sdp_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
434{
435 struct usb_gadget *gadget = f->config->cdev->gadget;
436 struct usb_request *req = f->config->cdev->req;
437 struct f_sdp *sdp = f->config->cdev->req->context;
438 u16 len = le16_to_cpu(ctrl->wLength);
439 u16 w_value = le16_to_cpu(ctrl->wValue);
440 int value = 0;
441 u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
442
443 debug("w_value: 0x%04x len: 0x%04x\n", w_value, len);
444 debug("req_type: 0x%02x ctrl->bRequest: 0x%02x sdp->state: %d\n",
445 req_type, ctrl->bRequest, sdp->state);
446
447 if (req_type == USB_TYPE_STANDARD) {
448 if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR) {
449 /* Send HID report descriptor */
450 value = min(len, (u16) sizeof(sdp_hid_report));
451 memcpy(req->buf, &sdp_hid_report, value);
452 sdp->configuration_done = true;
453 }
454 }
455
456 if (req_type == USB_TYPE_CLASS) {
457 int report = w_value & HID_REPORT_ID_MASK;
458
459 /* HID (SDP) request */
460 switch (ctrl->bRequest) {
461 case HID_REQ_SET_REPORT:
462 switch (report) {
463 case 1:
464 value = SDP_COMMAND_LEN + 1;
465 req->complete = sdp_rx_command_complete;
466 break;
467 case 2:
468 value = len;
469 req->complete = sdp_rx_data_complete;
470 break;
471 }
472 }
473 }
474
475 if (value >= 0) {
476 req->length = value;
477 req->zero = value < len;
478 value = usb_ep_queue(gadget->ep0, req, 0);
479 if (value < 0) {
480 debug("ep_queue --> %d\n", value);
481 req->status = 0;
482 }
483 }
484
485 return value;
486}
487
488static int sdp_bind(struct usb_configuration *c, struct usb_function *f)
489{
490 struct usb_gadget *gadget = c->cdev->gadget;
491 struct usb_composite_dev *cdev = c->cdev;
492 struct f_sdp *sdp = func_to_sdp(f);
493 int rv = 0, id;
494
495 id = usb_interface_id(c, f);
496 if (id < 0)
497 return id;
498 sdp_intf_runtime.bInterfaceNumber = id;
499
500 struct usb_ep *ep;
501
502 /* allocate instance-specific endpoints */
503 ep = usb_ep_autoconfig(gadget, &in_desc);
504 if (!ep) {
505 rv = -ENODEV;
506 goto error;
507 }
508
Ye Lid10d4292020-08-18 18:16:44 +0800509 if (gadget_is_dualspeed(gadget)) {
510 /* Assume endpoint addresses are the same for both speeds */
511 in_hs_desc.bEndpointAddress = in_desc.bEndpointAddress;
512 }
513
Stefan Agner5661f082017-08-16 11:00:51 -0700514 sdp->in_ep = ep; /* Store IN EP for enabling @ setup */
515
516 cdev->req->context = sdp;
517
518error:
519 return rv;
520}
521
522static void sdp_unbind(struct usb_configuration *c, struct usb_function *f)
523{
524 free(sdp_func);
525 sdp_func = NULL;
526}
527
528static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
529{
530 struct usb_request *req;
531
532 req = usb_ep_alloc_request(ep, 0);
533 if (!req)
534 return req;
535
536 req->length = length;
537 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
538 if (!req->buf) {
539 usb_ep_free_request(ep, req);
540 req = NULL;
541 }
542
543 return req;
544}
545
546
547static struct usb_request *sdp_start_ep(struct usb_ep *ep)
548{
549 struct usb_request *req;
550
Ye Li5dee7f02020-08-18 18:16:45 +0800551 req = alloc_ep_req(ep, 65);
Stefan Agner5661f082017-08-16 11:00:51 -0700552 debug("%s: ep:%p req:%p\n", __func__, ep, req);
553
554 if (!req)
555 return NULL;
556
557 memset(req->buf, 0, req->length);
558 req->complete = sdp_tx_complete;
559
560 return req;
561}
562static int sdp_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
563{
564 struct f_sdp *sdp = func_to_sdp(f);
565 struct usb_composite_dev *cdev = f->config->cdev;
Ye Lid10d4292020-08-18 18:16:44 +0800566 struct usb_gadget *gadget = cdev->gadget;
Stefan Agner5661f082017-08-16 11:00:51 -0700567 int result;
568
569 debug("%s: intf: %d alt: %d\n", __func__, intf, alt);
570
Ye Lid10d4292020-08-18 18:16:44 +0800571 if (gadget_is_dualspeed(gadget) && gadget->speed == USB_SPEED_HIGH)
572 result = usb_ep_enable(sdp->in_ep, &in_hs_desc);
573 else
574 result = usb_ep_enable(sdp->in_ep, &in_desc);
Stefan Agner5661f082017-08-16 11:00:51 -0700575 if (result)
576 return result;
577 sdp->in_req = sdp_start_ep(sdp->in_ep);
578 sdp->in_req->context = sdp;
579
580 sdp->in_ep->driver_data = cdev; /* claim */
581
582 sdp->altsetting = alt;
583 sdp->state = SDP_STATE_IDLE;
584
585 return 0;
586}
587
588static int sdp_get_alt(struct usb_function *f, unsigned intf)
589{
590 struct f_sdp *sdp = func_to_sdp(f);
591
592 return sdp->altsetting;
593}
594
595static void sdp_disable(struct usb_function *f)
596{
597 struct f_sdp *sdp = func_to_sdp(f);
598
599 usb_ep_disable(sdp->in_ep);
600
601 if (sdp->in_req) {
602 free(sdp->in_req);
603 sdp->in_req = NULL;
604 }
605}
606
607static int sdp_bind_config(struct usb_configuration *c)
608{
609 int status;
610
611 if (!sdp_func) {
612 sdp_func = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*sdp_func));
613 if (!sdp_func)
614 return -ENOMEM;
615 }
616
617 memset(sdp_func, 0, sizeof(*sdp_func));
618
619 sdp_func->usb_function.name = "sdp";
Ye Lid10d4292020-08-18 18:16:44 +0800620 sdp_func->usb_function.hs_descriptors = sdp_runtime_hs_descs;
Stefan Agner5661f082017-08-16 11:00:51 -0700621 sdp_func->usb_function.descriptors = sdp_runtime_descs;
622 sdp_func->usb_function.bind = sdp_bind;
623 sdp_func->usb_function.unbind = sdp_unbind;
624 sdp_func->usb_function.set_alt = sdp_set_alt;
625 sdp_func->usb_function.get_alt = sdp_get_alt;
626 sdp_func->usb_function.disable = sdp_disable;
627 sdp_func->usb_function.strings = sdp_generic_strings;
628 sdp_func->usb_function.setup = sdp_setup;
629
630 status = usb_add_function(c, &sdp_func->usb_function);
631
632 return status;
633}
634
635int sdp_init(int controller_index)
636{
637 printf("SDP: initialize...\n");
638 while (!sdp_func->configuration_done) {
639 if (ctrlc()) {
640 puts("\rCTRL+C - Operation aborted.\n");
641 return 1;
642 }
Vincent Prince8171dac2017-10-23 11:16:35 +0200643
644 WATCHDOG_RESET();
Stefan Agner5661f082017-08-16 11:00:51 -0700645 usb_gadget_handle_interrupts(controller_index);
646 }
647
648 return 0;
649}
650
651static u32 sdp_jump_imxheader(void *address)
652{
653 flash_header_v2_t *headerv2 = address;
654 ulong (*entry)(void);
655
656 if (headerv2->header.tag != IVT_HEADER_TAG) {
657 printf("Header Tag is not an IMX image\n");
658 return SDP_ERROR_IMXHEADER;
659 }
660
661 printf("Jumping to 0x%08x\n", headerv2->entry);
Andre Heidera64a6142018-02-15 10:17:29 +0100662 entry = sdp_ptr(headerv2->entry);
Stefan Agner5661f082017-08-16 11:00:51 -0700663 entry();
664
665 /* The image probably never returns hence we won't reach that point */
666 return 0;
667}
668
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200669#ifdef CONFIG_SPL_BUILD
670#ifdef CONFIG_SPL_LOAD_FIT
671static ulong sdp_fit_read(struct spl_load_info *load, ulong sector,
672 ulong count, void *buf)
673{
674 debug("%s: sector %lx, count %lx, buf %lx\n",
675 __func__, sector, count, (ulong)buf);
676 memcpy(buf, (void *)(load->dev + sector), count);
677 return count;
678}
679#endif
680#endif
681
682static void sdp_handle_in_ep(struct spl_image_info *spl_image)
Stefan Agner5661f082017-08-16 11:00:51 -0700683{
684 u8 *data = sdp_func->in_req->buf;
685 u32 status;
686 int datalen;
687
688 switch (sdp_func->state) {
689 case SDP_STATE_TX_SEC_CONF:
690 debug("Report 3: HAB security\n");
691 data[0] = 3;
692
693 status = SDP_SECURITY_OPEN;
694 memcpy(&data[1], &status, 4);
695 sdp_func->in_req->length = 5;
696 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
697 sdp_func->state = SDP_STATE_TX_SEC_CONF_BUSY;
698 break;
699
700 case SDP_STATE_TX_STATUS:
701 debug("Report 4: Status\n");
702 data[0] = 4;
703
704 memcpy(&data[1], &sdp_func->error_status, 4);
705 sdp_func->in_req->length = 65;
706 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
707 sdp_func->state = SDP_STATE_TX_STATUS_BUSY;
708 break;
709 case SDP_STATE_TX_REGISTER:
710 debug("Report 4: Register Values\n");
711 data[0] = 4;
712
713 datalen = sdp_func->dnl_bytes_remaining;
714
715 if (datalen > 64)
716 datalen = 64;
717
Andre Heidera64a6142018-02-15 10:17:29 +0100718 memcpy(&data[1], sdp_ptr(sdp_func->dnl_address), datalen);
Stefan Agner5661f082017-08-16 11:00:51 -0700719 sdp_func->in_req->length = 65;
720
721 sdp_func->dnl_bytes_remaining -= datalen;
722 sdp_func->dnl_address += datalen;
723
724 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
725 sdp_func->state = SDP_STATE_TX_REGISTER_BUSY;
726 break;
727 case SDP_STATE_JUMP:
Stefan Agnerccd7a4d2017-08-16 11:00:52 -0700728 printf("Jumping to header at 0x%08x\n", sdp_func->jmp_address);
Andre Heidera64a6142018-02-15 10:17:29 +0100729 status = sdp_jump_imxheader(sdp_ptr(sdp_func->jmp_address));
Stefan Agnerccd7a4d2017-08-16 11:00:52 -0700730
731 /* If imx header fails, try some U-Boot specific headers */
732 if (status) {
733#ifdef CONFIG_SPL_BUILD
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200734 image_header_t *header =
735 sdp_ptr(sdp_func->jmp_address);
736#ifdef CONFIG_SPL_LOAD_FIT
737 if (image_get_magic(header) == FDT_MAGIC) {
738 struct spl_load_info load;
739
740 debug("Found FIT\n");
741 load.dev = header;
742 load.bl_len = 1;
743 load.read = sdp_fit_read;
744 spl_load_simple_fit(spl_image, &load, 0,
745 header);
746
747 return;
748 }
749#endif
Stefan Agnerccd7a4d2017-08-16 11:00:52 -0700750 /* In SPL, allow jumps to U-Boot images */
751 struct spl_image_info spl_image = {};
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200752 spl_parse_image_header(&spl_image, header);
Stefan Agnerccd7a4d2017-08-16 11:00:52 -0700753 jump_to_image_no_args(&spl_image);
754#else
755 /* In U-Boot, allow jumps to scripts */
Simon Glass220a3a42019-12-28 10:45:04 -0700756 image_source_script(sdp_func->jmp_address, "script@1");
Stefan Agnerccd7a4d2017-08-16 11:00:52 -0700757#endif
758 }
Stefan Agner5661f082017-08-16 11:00:51 -0700759
760 sdp_func->next_state = SDP_STATE_IDLE;
761 sdp_func->error_status = status;
762
763 /* Only send Report 4 if there was an error */
764 if (status)
765 sdp_func->state = SDP_STATE_TX_STATUS;
766 else
767 sdp_func->state = SDP_STATE_IDLE;
768 break;
769 default:
770 break;
771 };
772}
773
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200774#ifndef CONFIG_SPL_BUILD
775int sdp_handle(int controller_index)
776#else
777int spl_sdp_handle(int controller_index, struct spl_image_info *spl_image)
778#endif
Stefan Agner5661f082017-08-16 11:00:51 -0700779{
780 printf("SDP: handle requests...\n");
781 while (1) {
782 if (ctrlc()) {
783 puts("\rCTRL+C - Operation aborted.\n");
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200784 return -EINVAL;
Stefan Agner5661f082017-08-16 11:00:51 -0700785 }
786
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200787#ifdef CONFIG_SPL_BUILD
788 if (spl_image->flags & SPL_FIT_FOUND)
789 return 0;
790#endif
791
Vincent Prince8171dac2017-10-23 11:16:35 +0200792 WATCHDOG_RESET();
Stefan Agner5661f082017-08-16 11:00:51 -0700793 usb_gadget_handle_interrupts(controller_index);
794
Frieder Schrempf2c72ead2019-06-04 21:56:29 +0200795#ifdef CONFIG_SPL_BUILD
796 sdp_handle_in_ep(spl_image);
797#else
798 sdp_handle_in_ep(NULL);
799#endif
Stefan Agner5661f082017-08-16 11:00:51 -0700800 }
801}
802
803int sdp_add(struct usb_configuration *c)
804{
805 int id;
806
807 id = usb_string_id(c->cdev);
808 if (id < 0)
809 return id;
810 strings_sdp_generic[0].id = id;
811 sdp_intf_runtime.iInterface = id;
812
813 debug("%s: cdev: %p gadget: %p gadget->ep0: %p\n", __func__,
814 c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
815
816 return sdp_bind_config(c);
817}
818
819DECLARE_GADGET_BIND_CALLBACK(usb_dnl_sdp, sdp_add);