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>
34 #define HID_REPORT_ID_MASK 0x000000ff
39 #define HID_REQ_GET_REPORT 0x01
40 #define HID_REQ_GET_IDLE 0x02
41 #define HID_REQ_GET_PROTOCOL 0x03
42 #define HID_REQ_SET_REPORT 0x09
43 #define HID_REQ_SET_IDLE 0x0A
44 #define HID_REQ_SET_PROTOCOL 0x0B
46 #define HID_USAGE_PAGE_LEN 76
49 u8 usage_page
[HID_USAGE_PAGE_LEN
];
52 #define SDP_READ_REGISTER 0x0101
53 #define SDP_WRITE_REGISTER 0x0202
54 #define SDP_WRITE_FILE 0x0404
55 #define SDP_ERROR_STATUS 0x0505
56 #define SDP_DCD_WRITE 0x0a0a
57 #define SDP_JUMP_ADDRESS 0x0b0b
58 #define SDP_SKIP_DCD_HEADER 0x0c0c
60 #define SDP_SECURITY_CLOSED 0x12343412
61 #define SDP_SECURITY_OPEN 0x56787856
63 #define SDP_WRITE_FILE_COMPLETE 0x88888888
64 #define SDP_WRITE_REGISTER_COMPLETE 0x128A8A12
65 #define SDP_SKIP_DCD_HEADER_COMPLETE 0x900DD009
66 #define SDP_ERROR_IMXHEADER 0x000a0533
68 #define SDP_COMMAND_LEN 16
81 SDP_STATE_RX_DCD_DATA
,
82 SDP_STATE_RX_FILE_DATA
,
83 SDP_STATE_TX_SEC_CONF
,
84 SDP_STATE_TX_SEC_CONF_BUSY
,
85 SDP_STATE_TX_REGISTER
,
86 SDP_STATE_TX_REGISTER_BUSY
,
88 SDP_STATE_TX_STATUS_BUSY
,
93 struct usb_function usb_function
;
95 struct usb_descriptor_header
**function
;
99 enum sdp_state next_state
;
101 u32 dnl_bytes_remaining
;
103 bool always_send_status
;
107 struct usb_request
*req
;
110 struct usb_ep
*in_ep
;
111 struct usb_request
*in_req
;
113 bool configuration_done
;
116 static struct f_sdp
*sdp_func
;
118 static inline struct f_sdp
*func_to_sdp(struct usb_function
*f
)
120 return container_of(f
, struct f_sdp
, usb_function
);
123 static struct usb_interface_descriptor sdp_intf_runtime
= {
124 .bLength
= sizeof(sdp_intf_runtime
),
125 .bDescriptorType
= USB_DT_INTERFACE
,
126 .bAlternateSetting
= 0,
128 .bInterfaceClass
= USB_CLASS_HID
,
129 .bInterfaceSubClass
= 0,
130 .bInterfaceProtocol
= 0,
131 /* .iInterface = DYNAMIC */
134 /* HID configuration */
135 static struct usb_class_hid_descriptor sdp_hid_desc
= {
136 .bLength
= sizeof(sdp_hid_desc
),
137 .bDescriptorType
= USB_DT_CS_DEVICE
,
139 .bcdCDC
= __constant_cpu_to_le16(0x0110),
141 .bNumDescriptors
= 1,
143 .bDescriptorType0
= USB_DT_HID_REPORT
,
144 .wDescriptorLength0
= HID_USAGE_PAGE_LEN
,
147 static struct usb_endpoint_descriptor in_desc
= {
148 .bLength
= USB_DT_ENDPOINT_SIZE
,
149 .bDescriptorType
= USB_DT_ENDPOINT
, /*USB_DT_CS_ENDPOINT*/
151 .bEndpointAddress
= 1 | USB_DIR_IN
,
152 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
153 .wMaxPacketSize
= 64,
157 static struct usb_descriptor_header
*sdp_runtime_descs
[] = {
158 (struct usb_descriptor_header
*)&sdp_intf_runtime
,
159 (struct usb_descriptor_header
*)&sdp_hid_desc
,
160 (struct usb_descriptor_header
*)&in_desc
,
164 /* This is synchronized with what the SoC implementation reports */
165 static struct hid_report sdp_hid_report
= {
167 0x06, 0x00, 0xff, /* Usage Page */
168 0x09, 0x01, /* Usage (Pointer?) */
169 0xa1, 0x01, /* Collection */
171 0x85, 0x01, /* Report ID */
172 0x19, 0x01, /* Usage Minimum */
173 0x29, 0x01, /* Usage Maximum */
174 0x15, 0x00, /* Local Minimum */
175 0x26, 0xFF, 0x00, /* Local Maximum? */
176 0x75, 0x08, /* Report Size */
177 0x95, 0x10, /* Report Count */
178 0x91, 0x02, /* Output Data */
180 0x85, 0x02, /* Report ID */
181 0x19, 0x01, /* Usage Minimum */
182 0x29, 0x01, /* Usage Maximum */
183 0x15, 0x00, /* Local Minimum */
184 0x26, 0xFF, 0x00, /* Local Maximum? */
185 0x75, 0x80, /* Report Size 128 */
186 0x95, 0x40, /* Report Count */
187 0x91, 0x02, /* Output Data */
189 0x85, 0x03, /* Report ID */
190 0x19, 0x01, /* Usage Minimum */
191 0x29, 0x01, /* Usage Maximum */
192 0x15, 0x00, /* Local Minimum */
193 0x26, 0xFF, 0x00, /* Local Maximum? */
194 0x75, 0x08, /* Report Size 8 */
195 0x95, 0x04, /* Report Count */
196 0x81, 0x02, /* Input Data */
198 0x85, 0x04, /* Report ID */
199 0x19, 0x01, /* Usage Minimum */
200 0x29, 0x01, /* Usage Maximum */
201 0x15, 0x00, /* Local Minimum */
202 0x26, 0xFF, 0x00, /* Local Maximum? */
203 0x75, 0x08, /* Report Size 8 */
204 0x95, 0x40, /* Report Count */
205 0x81, 0x02, /* Input Data */
210 static const char sdp_name
[] = "Serial Downloader Protocol";
213 * static strings, in UTF-8
215 static struct usb_string strings_sdp_generic
[] = {
217 { } /* end of list */
220 static struct usb_gadget_strings stringtab_sdp_generic
= {
221 .language
= 0x0409, /* en-us */
222 .strings
= strings_sdp_generic
,
225 static struct usb_gadget_strings
*sdp_generic_strings
[] = {
226 &stringtab_sdp_generic
,
230 static void sdp_rx_command_complete(struct usb_ep
*ep
, struct usb_request
*req
)
232 struct f_sdp
*sdp
= req
->context
;
233 int status
= req
->status
;
238 error("Status: %d", status
);
243 error("Unexpected report %d", report
);
247 struct sdp_command
*cmd
= req
->buf
+ 1;
249 debug("%s: command: %04x, addr: %08x, cnt: %u\n",
250 __func__
, be16_to_cpu(cmd
->cmd
),
251 be32_to_cpu(cmd
->addr
), be32_to_cpu(cmd
->cnt
));
253 switch (be16_to_cpu(cmd
->cmd
)) {
254 case SDP_READ_REGISTER
:
255 sdp
->always_send_status
= false;
256 sdp
->error_status
= 0x0;
258 sdp
->state
= SDP_STATE_TX_SEC_CONF
;
259 sdp
->dnl_address
= be32_to_cpu(cmd
->addr
);
260 sdp
->dnl_bytes_remaining
= be32_to_cpu(cmd
->cnt
);
261 sdp
->next_state
= SDP_STATE_TX_REGISTER
;
262 printf("Reading %d registers at 0x%08x... ",
263 sdp
->dnl_bytes_remaining
, sdp
->dnl_address
);
266 sdp
->always_send_status
= true;
267 sdp
->error_status
= SDP_WRITE_FILE_COMPLETE
;
269 sdp
->state
= SDP_STATE_RX_FILE_DATA
;
270 sdp
->dnl_address
= be32_to_cpu(cmd
->addr
);
271 sdp
->dnl_bytes_remaining
= be32_to_cpu(cmd
->cnt
);
272 sdp
->next_state
= SDP_STATE_IDLE
;
274 printf("Downloading file of size %d to 0x%08x... ",
275 sdp
->dnl_bytes_remaining
, sdp
->dnl_address
);
278 case SDP_ERROR_STATUS
:
279 sdp
->always_send_status
= true;
280 sdp
->error_status
= 0;
282 sdp
->state
= SDP_STATE_TX_SEC_CONF
;
283 sdp
->next_state
= SDP_STATE_IDLE
;
286 sdp
->always_send_status
= true;
287 sdp
->error_status
= SDP_WRITE_REGISTER_COMPLETE
;
289 sdp
->state
= SDP_STATE_RX_DCD_DATA
;
290 sdp
->dnl_bytes_remaining
= be32_to_cpu(cmd
->cnt
);
291 sdp
->next_state
= SDP_STATE_IDLE
;
293 case SDP_JUMP_ADDRESS
:
294 sdp
->always_send_status
= false;
295 sdp
->error_status
= 0;
297 sdp
->jmp_address
= be32_to_cpu(cmd
->addr
);
298 sdp
->state
= SDP_STATE_TX_SEC_CONF
;
299 sdp
->next_state
= SDP_STATE_JUMP
;
301 case SDP_SKIP_DCD_HEADER
:
302 sdp
->always_send_status
= true;
303 sdp
->error_status
= SDP_SKIP_DCD_HEADER_COMPLETE
;
305 /* Ignore command, DCD not supported anyway */
306 sdp
->state
= SDP_STATE_TX_SEC_CONF
;
307 sdp
->next_state
= SDP_STATE_IDLE
;
310 error("Unknown command: %04x\n", be16_to_cpu(cmd
->cmd
));
314 static void sdp_rx_data_complete(struct usb_ep
*ep
, struct usb_request
*req
)
316 struct f_sdp
*sdp
= req
->context
;
317 int status
= req
->status
;
320 int datalen
= req
->length
- 1;
323 error("Status: %d", status
);
328 error("Unexpected report %d", report
);
332 if (sdp
->dnl_bytes_remaining
< datalen
) {
334 * Some USB stacks require to send a complete buffer as
335 * specified in the HID descriptor. This leads to longer
336 * transfers than the file length, no problem for us.
338 sdp
->dnl_bytes_remaining
= 0;
340 sdp
->dnl_bytes_remaining
-= datalen
;
343 if (sdp
->state
== SDP_STATE_RX_FILE_DATA
) {
344 memcpy((void *)sdp
->dnl_address
, req
->buf
+ 1, datalen
);
345 sdp
->dnl_address
+= datalen
;
348 if (sdp
->dnl_bytes_remaining
)
353 switch (sdp
->state
) {
354 case SDP_STATE_RX_FILE_DATA
:
355 sdp
->state
= SDP_STATE_TX_SEC_CONF
;
357 case SDP_STATE_RX_DCD_DATA
:
358 sdp
->state
= SDP_STATE_TX_SEC_CONF
;
361 error("Invalid state: %d", sdp
->state
);
365 static void sdp_tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
367 struct f_sdp
*sdp
= req
->context
;
368 int status
= req
->status
;
371 error("Status: %d", status
);
375 switch (sdp
->state
) {
376 case SDP_STATE_TX_SEC_CONF_BUSY
:
377 /* Not all commands require status report */
378 if (sdp
->always_send_status
|| sdp
->error_status
)
379 sdp
->state
= SDP_STATE_TX_STATUS
;
381 sdp
->state
= sdp
->next_state
;
384 case SDP_STATE_TX_STATUS_BUSY
:
385 sdp
->state
= sdp
->next_state
;
387 case SDP_STATE_TX_REGISTER_BUSY
:
388 if (sdp
->dnl_bytes_remaining
)
389 sdp
->state
= SDP_STATE_TX_REGISTER
;
391 sdp
->state
= SDP_STATE_IDLE
;
394 error("Wrong State: %d", sdp
->state
);
395 sdp
->state
= SDP_STATE_IDLE
;
398 debug("%s complete --> %d, %d/%d\n", ep
->name
,
399 status
, req
->actual
, req
->length
);
402 static int sdp_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
404 struct usb_gadget
*gadget
= f
->config
->cdev
->gadget
;
405 struct usb_request
*req
= f
->config
->cdev
->req
;
406 struct f_sdp
*sdp
= f
->config
->cdev
->req
->context
;
407 u16 len
= le16_to_cpu(ctrl
->wLength
);
408 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
410 u8 req_type
= ctrl
->bRequestType
& USB_TYPE_MASK
;
412 debug("w_value: 0x%04x len: 0x%04x\n", w_value
, len
);
413 debug("req_type: 0x%02x ctrl->bRequest: 0x%02x sdp->state: %d\n",
414 req_type
, ctrl
->bRequest
, sdp
->state
);
416 if (req_type
== USB_TYPE_STANDARD
) {
417 if (ctrl
->bRequest
== USB_REQ_GET_DESCRIPTOR
) {
418 /* Send HID report descriptor */
419 value
= min(len
, (u16
) sizeof(sdp_hid_report
));
420 memcpy(req
->buf
, &sdp_hid_report
, value
);
421 sdp
->configuration_done
= true;
425 if (req_type
== USB_TYPE_CLASS
) {
426 int report
= w_value
& HID_REPORT_ID_MASK
;
428 /* HID (SDP) request */
429 switch (ctrl
->bRequest
) {
430 case HID_REQ_SET_REPORT
:
433 value
= SDP_COMMAND_LEN
+ 1;
434 req
->complete
= sdp_rx_command_complete
;
438 req
->complete
= sdp_rx_data_complete
;
446 req
->zero
= value
< len
;
447 value
= usb_ep_queue(gadget
->ep0
, req
, 0);
449 debug("ep_queue --> %d\n", value
);
457 static int sdp_bind(struct usb_configuration
*c
, struct usb_function
*f
)
459 struct usb_gadget
*gadget
= c
->cdev
->gadget
;
460 struct usb_composite_dev
*cdev
= c
->cdev
;
461 struct f_sdp
*sdp
= func_to_sdp(f
);
464 id
= usb_interface_id(c
, f
);
467 sdp_intf_runtime
.bInterfaceNumber
= id
;
471 /* allocate instance-specific endpoints */
472 ep
= usb_ep_autoconfig(gadget
, &in_desc
);
478 sdp
->in_ep
= ep
; /* Store IN EP for enabling @ setup */
480 cdev
->req
->context
= sdp
;
486 static void sdp_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
492 static struct usb_request
*alloc_ep_req(struct usb_ep
*ep
, unsigned length
)
494 struct usb_request
*req
;
496 req
= usb_ep_alloc_request(ep
, 0);
500 req
->length
= length
;
501 req
->buf
= memalign(CONFIG_SYS_CACHELINE_SIZE
, length
);
503 usb_ep_free_request(ep
, req
);
511 static struct usb_request
*sdp_start_ep(struct usb_ep
*ep
)
513 struct usb_request
*req
;
515 req
= alloc_ep_req(ep
, 64);
516 debug("%s: ep:%p req:%p\n", __func__
, ep
, req
);
521 memset(req
->buf
, 0, req
->length
);
522 req
->complete
= sdp_tx_complete
;
526 static int sdp_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
528 struct f_sdp
*sdp
= func_to_sdp(f
);
529 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
532 debug("%s: intf: %d alt: %d\n", __func__
, intf
, alt
);
534 result
= usb_ep_enable(sdp
->in_ep
, &in_desc
);
537 sdp
->in_req
= sdp_start_ep(sdp
->in_ep
);
538 sdp
->in_req
->context
= sdp
;
540 sdp
->in_ep
->driver_data
= cdev
; /* claim */
542 sdp
->altsetting
= alt
;
543 sdp
->state
= SDP_STATE_IDLE
;
548 static int sdp_get_alt(struct usb_function
*f
, unsigned intf
)
550 struct f_sdp
*sdp
= func_to_sdp(f
);
552 return sdp
->altsetting
;
555 static void sdp_disable(struct usb_function
*f
)
557 struct f_sdp
*sdp
= func_to_sdp(f
);
559 usb_ep_disable(sdp
->in_ep
);
567 static int sdp_bind_config(struct usb_configuration
*c
)
572 sdp_func
= memalign(CONFIG_SYS_CACHELINE_SIZE
, sizeof(*sdp_func
));
577 memset(sdp_func
, 0, sizeof(*sdp_func
));
579 sdp_func
->usb_function
.name
= "sdp";
580 sdp_func
->usb_function
.hs_descriptors
= sdp_runtime_descs
;
581 sdp_func
->usb_function
.descriptors
= sdp_runtime_descs
;
582 sdp_func
->usb_function
.bind
= sdp_bind
;
583 sdp_func
->usb_function
.unbind
= sdp_unbind
;
584 sdp_func
->usb_function
.set_alt
= sdp_set_alt
;
585 sdp_func
->usb_function
.get_alt
= sdp_get_alt
;
586 sdp_func
->usb_function
.disable
= sdp_disable
;
587 sdp_func
->usb_function
.strings
= sdp_generic_strings
;
588 sdp_func
->usb_function
.setup
= sdp_setup
;
590 status
= usb_add_function(c
, &sdp_func
->usb_function
);
595 int sdp_init(int controller_index
)
597 printf("SDP: initialize...\n");
598 while (!sdp_func
->configuration_done
) {
600 puts("\rCTRL+C - Operation aborted.\n");
603 usb_gadget_handle_interrupts(controller_index
);
609 static u32
sdp_jump_imxheader(void *address
)
611 flash_header_v2_t
*headerv2
= address
;
612 ulong (*entry
)(void);
614 if (headerv2
->header
.tag
!= IVT_HEADER_TAG
) {
615 printf("Header Tag is not an IMX image\n");
616 return SDP_ERROR_IMXHEADER
;
619 printf("Jumping to 0x%08x\n", headerv2
->entry
);
620 entry
= (void *)headerv2
->entry
;
623 /* The image probably never returns hence we won't reach that point */
627 static void sdp_handle_in_ep(void)
629 u8
*data
= sdp_func
->in_req
->buf
;
633 switch (sdp_func
->state
) {
634 case SDP_STATE_TX_SEC_CONF
:
635 debug("Report 3: HAB security\n");
638 status
= SDP_SECURITY_OPEN
;
639 memcpy(&data
[1], &status
, 4);
640 sdp_func
->in_req
->length
= 5;
641 usb_ep_queue(sdp_func
->in_ep
, sdp_func
->in_req
, 0);
642 sdp_func
->state
= SDP_STATE_TX_SEC_CONF_BUSY
;
645 case SDP_STATE_TX_STATUS
:
646 debug("Report 4: Status\n");
649 memcpy(&data
[1], &sdp_func
->error_status
, 4);
650 sdp_func
->in_req
->length
= 65;
651 usb_ep_queue(sdp_func
->in_ep
, sdp_func
->in_req
, 0);
652 sdp_func
->state
= SDP_STATE_TX_STATUS_BUSY
;
654 case SDP_STATE_TX_REGISTER
:
655 debug("Report 4: Register Values\n");
658 datalen
= sdp_func
->dnl_bytes_remaining
;
663 memcpy(&data
[1], (void *)sdp_func
->dnl_address
, datalen
);
664 sdp_func
->in_req
->length
= 65;
666 sdp_func
->dnl_bytes_remaining
-= datalen
;
667 sdp_func
->dnl_address
+= datalen
;
669 usb_ep_queue(sdp_func
->in_ep
, sdp_func
->in_req
, 0);
670 sdp_func
->state
= SDP_STATE_TX_REGISTER_BUSY
;
673 printf("Checking imxheader at 0x%08x\n", f_sdp
->jmp_address
);
674 status
= sdp_jump_imxheader((void *)f_sdp
->jmp_address
);
676 sdp_func
->next_state
= SDP_STATE_IDLE
;
677 sdp_func
->error_status
= status
;
679 /* Only send Report 4 if there was an error */
681 sdp_func
->state
= SDP_STATE_TX_STATUS
;
683 sdp_func
->state
= SDP_STATE_IDLE
;
690 void sdp_handle(int controller_index
)
692 printf("SDP: handle requests...\n");
695 puts("\rCTRL+C - Operation aborted.\n");
699 usb_gadget_handle_interrupts(controller_index
);
705 int sdp_add(struct usb_configuration
*c
)
709 id
= usb_string_id(c
->cdev
);
712 strings_sdp_generic
[0].id
= id
;
713 sdp_intf_runtime
.iInterface
= id
;
715 debug("%s: cdev: %p gadget: %p gadget->ep0: %p\n", __func__
,
716 c
->cdev
, c
->cdev
->gadget
, c
->cdev
->gadget
->ep0
);
718 return sdp_bind_config(c
);
721 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_sdp
, sdp_add
);