2 * f_sdp.c -- USB HID Serial Download Protocol
4 * Copyright (C) 2017 Toradex
5 * Author: Stefan Agner <stefan.agner@toradex.com>
7 * This file implements the Serial Download Protocol (SDP) as specified in
8 * the i.MX 6 Reference Manual. The SDP is a USB HID based protocol and
9 * allows to download images directly to memory. The implementation
10 * works with the imx_loader (imx_usb) USB client software on host side.
12 * Not all commands are implemented, e.g. WRITE_REGISTER, DCD_WRITE and
13 * SKIP_DCD_HEADER are only stubs.
15 * Parts of the implementation are based on f_dfu and f_thor.
17 * SPDX-License-Identifier: GPL-2.0+
25 #include <linux/usb/ch9.h>
26 #include <linux/usb/gadget.h>
27 #include <linux/usb/composite.h>
36 #define HID_REPORT_ID_MASK 0x000000ff
41 #define HID_REQ_GET_REPORT 0x01
42 #define HID_REQ_GET_IDLE 0x02
43 #define HID_REQ_GET_PROTOCOL 0x03
44 #define HID_REQ_SET_REPORT 0x09
45 #define HID_REQ_SET_IDLE 0x0A
46 #define HID_REQ_SET_PROTOCOL 0x0B
48 #define HID_USAGE_PAGE_LEN 76
51 u8 usage_page[HID_USAGE_PAGE_LEN];
54 #define SDP_READ_REGISTER 0x0101
55 #define SDP_WRITE_REGISTER 0x0202
56 #define SDP_WRITE_FILE 0x0404
57 #define SDP_ERROR_STATUS 0x0505
58 #define SDP_DCD_WRITE 0x0a0a
59 #define SDP_JUMP_ADDRESS 0x0b0b
60 #define SDP_SKIP_DCD_HEADER 0x0c0c
62 #define SDP_SECURITY_CLOSED 0x12343412
63 #define SDP_SECURITY_OPEN 0x56787856
65 #define SDP_WRITE_FILE_COMPLETE 0x88888888
66 #define SDP_WRITE_REGISTER_COMPLETE 0x128A8A12
67 #define SDP_SKIP_DCD_HEADER_COMPLETE 0x900DD009
68 #define SDP_ERROR_IMXHEADER 0x000a0533
70 #define SDP_COMMAND_LEN 16
83 SDP_STATE_RX_DCD_DATA,
84 SDP_STATE_RX_FILE_DATA,
85 SDP_STATE_TX_SEC_CONF,
86 SDP_STATE_TX_SEC_CONF_BUSY,
87 SDP_STATE_TX_REGISTER,
88 SDP_STATE_TX_REGISTER_BUSY,
90 SDP_STATE_TX_STATUS_BUSY,
95 struct usb_function usb_function;
97 struct usb_descriptor_header **function;
100 enum sdp_state state;
101 enum sdp_state next_state;
103 u32 dnl_bytes_remaining;
105 bool always_send_status;
109 struct usb_request *req;
112 struct usb_ep *in_ep;
113 struct usb_request *in_req;
115 bool configuration_done;
118 static struct f_sdp *sdp_func;
120 static inline struct f_sdp *func_to_sdp(struct usb_function *f)
122 return container_of(f, struct f_sdp, usb_function);
125 static struct usb_interface_descriptor sdp_intf_runtime = {
126 .bLength = sizeof(sdp_intf_runtime),
127 .bDescriptorType = USB_DT_INTERFACE,
128 .bAlternateSetting = 0,
130 .bInterfaceClass = USB_CLASS_HID,
131 .bInterfaceSubClass = 0,
132 .bInterfaceProtocol = 0,
133 /* .iInterface = DYNAMIC */
136 /* HID configuration */
137 static struct usb_class_hid_descriptor sdp_hid_desc = {
138 .bLength = sizeof(sdp_hid_desc),
139 .bDescriptorType = USB_DT_CS_DEVICE,
141 .bcdCDC = __constant_cpu_to_le16(0x0110),
143 .bNumDescriptors = 1,
145 .bDescriptorType0 = USB_DT_HID_REPORT,
146 .wDescriptorLength0 = HID_USAGE_PAGE_LEN,
149 static struct usb_endpoint_descriptor in_desc = {
150 .bLength = USB_DT_ENDPOINT_SIZE,
151 .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/
153 .bEndpointAddress = 1 | USB_DIR_IN,
154 .bmAttributes = USB_ENDPOINT_XFER_INT,
155 .wMaxPacketSize = 64,
159 static struct usb_descriptor_header *sdp_runtime_descs[] = {
160 (struct usb_descriptor_header *)&sdp_intf_runtime,
161 (struct usb_descriptor_header *)&sdp_hid_desc,
162 (struct usb_descriptor_header *)&in_desc,
166 /* This is synchronized with what the SoC implementation reports */
167 static struct hid_report sdp_hid_report = {
169 0x06, 0x00, 0xff, /* Usage Page */
170 0x09, 0x01, /* Usage (Pointer?) */
171 0xa1, 0x01, /* Collection */
173 0x85, 0x01, /* Report ID */
174 0x19, 0x01, /* Usage Minimum */
175 0x29, 0x01, /* Usage Maximum */
176 0x15, 0x00, /* Local Minimum */
177 0x26, 0xFF, 0x00, /* Local Maximum? */
178 0x75, 0x08, /* Report Size */
179 0x95, 0x10, /* Report Count */
180 0x91, 0x02, /* Output Data */
182 0x85, 0x02, /* Report ID */
183 0x19, 0x01, /* Usage Minimum */
184 0x29, 0x01, /* Usage Maximum */
185 0x15, 0x00, /* Local Minimum */
186 0x26, 0xFF, 0x00, /* Local Maximum? */
187 0x75, 0x80, /* Report Size 128 */
188 0x95, 0x40, /* Report Count */
189 0x91, 0x02, /* Output Data */
191 0x85, 0x03, /* Report ID */
192 0x19, 0x01, /* Usage Minimum */
193 0x29, 0x01, /* Usage Maximum */
194 0x15, 0x00, /* Local Minimum */
195 0x26, 0xFF, 0x00, /* Local Maximum? */
196 0x75, 0x08, /* Report Size 8 */
197 0x95, 0x04, /* Report Count */
198 0x81, 0x02, /* Input Data */
200 0x85, 0x04, /* Report ID */
201 0x19, 0x01, /* Usage Minimum */
202 0x29, 0x01, /* Usage Maximum */
203 0x15, 0x00, /* Local Minimum */
204 0x26, 0xFF, 0x00, /* Local Maximum? */
205 0x75, 0x08, /* Report Size 8 */
206 0x95, 0x40, /* Report Count */
207 0x81, 0x02, /* Input Data */
212 static const char sdp_name[] = "Serial Downloader Protocol";
215 * static strings, in UTF-8
217 static struct usb_string strings_sdp_generic[] = {
219 { } /* end of list */
222 static struct usb_gadget_strings stringtab_sdp_generic = {
223 .language = 0x0409, /* en-us */
224 .strings = strings_sdp_generic,
227 static struct usb_gadget_strings *sdp_generic_strings[] = {
228 &stringtab_sdp_generic,
232 static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req)
234 struct f_sdp *sdp = req->context;
235 int status = req->status;
240 error("Status: %d", status);
245 error("Unexpected report %d", report);
249 struct sdp_command *cmd = req->buf + 1;
251 debug("%s: command: %04x, addr: %08x, cnt: %u\n",
252 __func__, be16_to_cpu(cmd->cmd),
253 be32_to_cpu(cmd->addr), be32_to_cpu(cmd->cnt));
255 switch (be16_to_cpu(cmd->cmd)) {
256 case SDP_READ_REGISTER:
257 sdp->always_send_status = false;
258 sdp->error_status = 0x0;
260 sdp->state = SDP_STATE_TX_SEC_CONF;
261 sdp->dnl_address = be32_to_cpu(cmd->addr);
262 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
263 sdp->next_state = SDP_STATE_TX_REGISTER;
264 printf("Reading %d registers at 0x%08x... ",
265 sdp->dnl_bytes_remaining, sdp->dnl_address);
268 sdp->always_send_status = true;
269 sdp->error_status = SDP_WRITE_FILE_COMPLETE;
271 sdp->state = SDP_STATE_RX_FILE_DATA;
272 sdp->dnl_address = be32_to_cpu(cmd->addr);
273 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
274 sdp->next_state = SDP_STATE_IDLE;
276 printf("Downloading file of size %d to 0x%08x... ",
277 sdp->dnl_bytes_remaining, sdp->dnl_address);
280 case SDP_ERROR_STATUS:
281 sdp->always_send_status = true;
282 sdp->error_status = 0;
284 sdp->state = SDP_STATE_TX_SEC_CONF;
285 sdp->next_state = SDP_STATE_IDLE;
288 sdp->always_send_status = true;
289 sdp->error_status = SDP_WRITE_REGISTER_COMPLETE;
291 sdp->state = SDP_STATE_RX_DCD_DATA;
292 sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt);
293 sdp->next_state = SDP_STATE_IDLE;
295 case SDP_JUMP_ADDRESS:
296 sdp->always_send_status = false;
297 sdp->error_status = 0;
299 sdp->jmp_address = be32_to_cpu(cmd->addr);
300 sdp->state = SDP_STATE_TX_SEC_CONF;
301 sdp->next_state = SDP_STATE_JUMP;
303 case SDP_SKIP_DCD_HEADER:
304 sdp->always_send_status = true;
305 sdp->error_status = SDP_SKIP_DCD_HEADER_COMPLETE;
307 /* Ignore command, DCD not supported anyway */
308 sdp->state = SDP_STATE_TX_SEC_CONF;
309 sdp->next_state = SDP_STATE_IDLE;
312 error("Unknown command: %04x\n", be16_to_cpu(cmd->cmd));
316 static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req)
318 struct f_sdp *sdp = req->context;
319 int status = req->status;
322 int datalen = req->length - 1;
325 error("Status: %d", status);
330 error("Unexpected report %d", report);
334 if (sdp->dnl_bytes_remaining < datalen) {
336 * Some USB stacks require to send a complete buffer as
337 * specified in the HID descriptor. This leads to longer
338 * transfers than the file length, no problem for us.
340 sdp->dnl_bytes_remaining = 0;
342 sdp->dnl_bytes_remaining -= datalen;
345 if (sdp->state == SDP_STATE_RX_FILE_DATA) {
346 memcpy((void *)sdp->dnl_address, req->buf + 1, datalen);
347 sdp->dnl_address += datalen;
350 if (sdp->dnl_bytes_remaining)
355 switch (sdp->state) {
356 case SDP_STATE_RX_FILE_DATA:
357 sdp->state = SDP_STATE_TX_SEC_CONF;
359 case SDP_STATE_RX_DCD_DATA:
360 sdp->state = SDP_STATE_TX_SEC_CONF;
363 error("Invalid state: %d", sdp->state);
367 static void sdp_tx_complete(struct usb_ep *ep, struct usb_request *req)
369 struct f_sdp *sdp = req->context;
370 int status = req->status;
373 error("Status: %d", status);
377 switch (sdp->state) {
378 case SDP_STATE_TX_SEC_CONF_BUSY:
379 /* Not all commands require status report */
380 if (sdp->always_send_status || sdp->error_status)
381 sdp->state = SDP_STATE_TX_STATUS;
383 sdp->state = sdp->next_state;
386 case SDP_STATE_TX_STATUS_BUSY:
387 sdp->state = sdp->next_state;
389 case SDP_STATE_TX_REGISTER_BUSY:
390 if (sdp->dnl_bytes_remaining)
391 sdp->state = SDP_STATE_TX_REGISTER;
393 sdp->state = SDP_STATE_IDLE;
396 error("Wrong State: %d", sdp->state);
397 sdp->state = SDP_STATE_IDLE;
400 debug("%s complete --> %d, %d/%d\n", ep->name,
401 status, req->actual, req->length);
404 static int sdp_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
406 struct usb_gadget *gadget = f->config->cdev->gadget;
407 struct usb_request *req = f->config->cdev->req;
408 struct f_sdp *sdp = f->config->cdev->req->context;
409 u16 len = le16_to_cpu(ctrl->wLength);
410 u16 w_value = le16_to_cpu(ctrl->wValue);
412 u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;
414 debug("w_value: 0x%04x len: 0x%04x\n", w_value, len);
415 debug("req_type: 0x%02x ctrl->bRequest: 0x%02x sdp->state: %d\n",
416 req_type, ctrl->bRequest, sdp->state);
418 if (req_type == USB_TYPE_STANDARD) {
419 if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR) {
420 /* Send HID report descriptor */
421 value = min(len, (u16) sizeof(sdp_hid_report));
422 memcpy(req->buf, &sdp_hid_report, value);
423 sdp->configuration_done = true;
427 if (req_type == USB_TYPE_CLASS) {
428 int report = w_value & HID_REPORT_ID_MASK;
430 /* HID (SDP) request */
431 switch (ctrl->bRequest) {
432 case HID_REQ_SET_REPORT:
435 value = SDP_COMMAND_LEN + 1;
436 req->complete = sdp_rx_command_complete;
440 req->complete = sdp_rx_data_complete;
448 req->zero = value < len;
449 value = usb_ep_queue(gadget->ep0, req, 0);
451 debug("ep_queue --> %d\n", value);
459 static int sdp_bind(struct usb_configuration *c, struct usb_function *f)
461 struct usb_gadget *gadget = c->cdev->gadget;
462 struct usb_composite_dev *cdev = c->cdev;
463 struct f_sdp *sdp = func_to_sdp(f);
466 id = usb_interface_id(c, f);
469 sdp_intf_runtime.bInterfaceNumber = id;
473 /* allocate instance-specific endpoints */
474 ep = usb_ep_autoconfig(gadget, &in_desc);
480 sdp->in_ep = ep; /* Store IN EP for enabling @ setup */
482 cdev->req->context = sdp;
488 static void sdp_unbind(struct usb_configuration *c, struct usb_function *f)
494 static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
496 struct usb_request *req;
498 req = usb_ep_alloc_request(ep, 0);
502 req->length = length;
503 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
505 usb_ep_free_request(ep, req);
513 static struct usb_request *sdp_start_ep(struct usb_ep *ep)
515 struct usb_request *req;
517 req = alloc_ep_req(ep, 64);
518 debug("%s: ep:%p req:%p\n", __func__, ep, req);
523 memset(req->buf, 0, req->length);
524 req->complete = sdp_tx_complete;
528 static int sdp_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
530 struct f_sdp *sdp = func_to_sdp(f);
531 struct usb_composite_dev *cdev = f->config->cdev;
534 debug("%s: intf: %d alt: %d\n", __func__, intf, alt);
536 result = usb_ep_enable(sdp->in_ep, &in_desc);
539 sdp->in_req = sdp_start_ep(sdp->in_ep);
540 sdp->in_req->context = sdp;
542 sdp->in_ep->driver_data = cdev; /* claim */
544 sdp->altsetting = alt;
545 sdp->state = SDP_STATE_IDLE;
550 static int sdp_get_alt(struct usb_function *f, unsigned intf)
552 struct f_sdp *sdp = func_to_sdp(f);
554 return sdp->altsetting;
557 static void sdp_disable(struct usb_function *f)
559 struct f_sdp *sdp = func_to_sdp(f);
561 usb_ep_disable(sdp->in_ep);
569 static int sdp_bind_config(struct usb_configuration *c)
574 sdp_func = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*sdp_func));
579 memset(sdp_func, 0, sizeof(*sdp_func));
581 sdp_func->usb_function.name = "sdp";
582 sdp_func->usb_function.hs_descriptors = sdp_runtime_descs;
583 sdp_func->usb_function.descriptors = sdp_runtime_descs;
584 sdp_func->usb_function.bind = sdp_bind;
585 sdp_func->usb_function.unbind = sdp_unbind;
586 sdp_func->usb_function.set_alt = sdp_set_alt;
587 sdp_func->usb_function.get_alt = sdp_get_alt;
588 sdp_func->usb_function.disable = sdp_disable;
589 sdp_func->usb_function.strings = sdp_generic_strings;
590 sdp_func->usb_function.setup = sdp_setup;
592 status = usb_add_function(c, &sdp_func->usb_function);
597 int sdp_init(int controller_index)
599 printf("SDP: initialize...\n");
600 while (!sdp_func->configuration_done) {
602 puts("\rCTRL+C - Operation aborted.\n");
605 usb_gadget_handle_interrupts(controller_index);
611 static u32 sdp_jump_imxheader(void *address)
613 flash_header_v2_t *headerv2 = address;
614 ulong (*entry)(void);
616 if (headerv2->header.tag != IVT_HEADER_TAG) {
617 printf("Header Tag is not an IMX image\n");
618 return SDP_ERROR_IMXHEADER;
621 printf("Jumping to 0x%08x\n", headerv2->entry);
622 entry = (void *)headerv2->entry;
625 /* The image probably never returns hence we won't reach that point */
629 static void sdp_handle_in_ep(void)
631 u8 *data = sdp_func->in_req->buf;
635 switch (sdp_func->state) {
636 case SDP_STATE_TX_SEC_CONF:
637 debug("Report 3: HAB security\n");
640 status = SDP_SECURITY_OPEN;
641 memcpy(&data[1], &status, 4);
642 sdp_func->in_req->length = 5;
643 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
644 sdp_func->state = SDP_STATE_TX_SEC_CONF_BUSY;
647 case SDP_STATE_TX_STATUS:
648 debug("Report 4: Status\n");
651 memcpy(&data[1], &sdp_func->error_status, 4);
652 sdp_func->in_req->length = 65;
653 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
654 sdp_func->state = SDP_STATE_TX_STATUS_BUSY;
656 case SDP_STATE_TX_REGISTER:
657 debug("Report 4: Register Values\n");
660 datalen = sdp_func->dnl_bytes_remaining;
665 memcpy(&data[1], (void *)sdp_func->dnl_address, datalen);
666 sdp_func->in_req->length = 65;
668 sdp_func->dnl_bytes_remaining -= datalen;
669 sdp_func->dnl_address += datalen;
671 usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0);
672 sdp_func->state = SDP_STATE_TX_REGISTER_BUSY;
675 printf("Jumping to header at 0x%08x\n", sdp_func->jmp_address);
676 status = sdp_jump_imxheader((void *)sdp_func->jmp_address);
678 /* If imx header fails, try some U-Boot specific headers */
680 #ifdef CONFIG_SPL_BUILD
681 /* In SPL, allow jumps to U-Boot images */
682 struct spl_image_info spl_image = {};
683 spl_parse_image_header(&spl_image,
684 (struct image_header *)sdp_func->jmp_address);
685 jump_to_image_no_args(&spl_image);
687 /* In U-Boot, allow jumps to scripts */
688 source(sdp_func->jmp_address, "script@1");
692 sdp_func->next_state = SDP_STATE_IDLE;
693 sdp_func->error_status = status;
695 /* Only send Report 4 if there was an error */
697 sdp_func->state = SDP_STATE_TX_STATUS;
699 sdp_func->state = SDP_STATE_IDLE;
706 void sdp_handle(int controller_index)
708 printf("SDP: handle requests...\n");
711 puts("\rCTRL+C - Operation aborted.\n");
715 usb_gadget_handle_interrupts(controller_index);
721 int sdp_add(struct usb_configuration *c)
725 id = usb_string_id(c->cdev);
728 strings_sdp_generic[0].id = id;
729 sdp_intf_runtime.iInterface = id;
731 debug("%s: cdev: %p gadget: %p gadget->ep0: %p\n", __func__,
732 c->cdev, c->cdev->gadget, c->cdev->gadget->ep0);
734 return sdp_bind_config(c);
737 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_sdp, sdp_add);